Department of Computer Science



next up previous
Next: Predicates and terms Up: A goal directed programming Previous: Goals


Rules


Subsections

It is a very simple step to name such a conjoint goal and to include it in the program as a rule.

      | ?- [user].  
      onebelow(X,Y) :- supports(X,Z), supports(Z,Y).
      ^d 
      [eof] 
      | ?-
which can be read as:
for any value of X and any value of Y such that X supports some other block Z and that block Z supports Y it is true that X is one block below Y.
Alternatively it can be read as saying:
in order to see if for any values of X, Y and Z, X is one block below Y, satisfy the sub-goals supports(X,Z) and supports(Z,Y).
Giving the interpreter the goal:
      | ?- onebelow(a,d).
will cause it to match the ``head'' of the rule binding X to a and Y to d, it will then attempt the conjoint subgoals:
      supports(a,Z), supports(Z,d).
which will succeed, as shown above, and so the onebelow goal will succeed.

Programs usually consist of large numbers of rules rather than lots of facts since rules allow the discovery of relationships that hold between any values.

Rules with Alternatives

Rules can also express alternatives, ie. there can be different sets of sub-goals that if satisfied will enable a conclusion to be inferred. Consider:

    below(X,Y) :- supports(X,Y).  
    below(X,Y) :- supports(X,Z), below(Z,Y).
these rules express the idea that: a block is below any other block if it directly or indirectly supports it, so if X supports Y then they it is below Y OR, if that is not the case, X might still be below Y if there exists some other block Z that X directly supports and which in turn is below of Y.

If given the goal:

      below(a,b).
the matched rule is:
      below(X,Y) :- supports(X,Y)
and it will succeed immediately. If however the goal:
      below(a,d).
is given the first rule will fail, ``a'' does not support ``d'' but there is an alternative rule that will be matched and be tried:
      below(X,Y) :- supports(X,Z), below(Z,Y).
this rule will first establish the sub-goal:
      supports(a,Z)
and try to find a value for Z. A value it can find is Z=b, it then successfully attempts the next sub-goal with the values of the variables Z=b and Y=d:
      below(b,d)
which will succeed.


next up previous
Next: Predicates and terms Up: A goal directed programming Previous: Goals


Page generated: 2002-11-04 by Bob Dickerson

© University of Hertfordshire Higher Education Corporation (1998)

Disclaimer