Department of Computer Science



next up previous
Next: Rules Up: A goal directed programming Previous: Programs, the Interpreter and


Goals


Subsections

Now to try it out one can enter goals. A goal is a relationship and the system will say whether it holds (can be found) or not:

      | ?- supports(a,b).
       yes 
      | ?- supports(b,c).
       no 
      | ?- answer(42).  
       no 
      | ?-
notice that the system says no if it cannot find a fact corresponding to the goal.

Goals and Variables

Goals can contain variables (which are words starting with an upper case letter or an underline _). A variable will match any value, so if the goal relation name matches and so do the non-variable arguments the whole goal will match associating the matched constant with the variable.

      | ?- supports(c,X).  
       X = e
      | ?-
This can be read as ``are there any values of X such that block c supports X'', and it finds and reports the value of X.

There might be more than one value that could satisfy a goal:

      | ?- supports(a,X).  
       X = b ; 
       X = c ; 
       no
when it has satisfied the goal one way it waits and if the user enters ; it backtracks and attempts to find an alternative way to satisfy it, if the user enters a return it finishes.

Conjunction of goals

If a goal consists of two predicates separated by commas then the goal will only succeed if both can be satisfied.

      | ?- supports(a,X), supports(X,d).  
       X = b
here the first predicate is satisfied by fact: supports(a,b) where X is bound to b. And the second goal is tried with X bound to b giving: supports(b,d); this succeeds so the whole conjunction succeeds. Notice that a variable in a phrase of prolog can match any value but must have the same interpretation throughout the phrase.

However if the goal had been slightly different the behaviour would be more complicated:

      | ?- supports(a,X), supports(X,e).  
       X = c
It still succeeds and reports the name of the intermediate block. The sequence of events was:


next up previous
Next: Rules Up: A goal directed programming Previous: Programs, the Interpreter and


Page generated: 2002-11-04 by Bob Dickerson

© University of Hertfordshire Higher Education Corporation (1998)

Disclaimer