Slide 14.22: Rules
Slide 14.24: Rules (cont.)
Home

Rules (Cont.)


Rule III
We can also use variables within queries. For example, we might wish to see if there is somebody who is mortal. This is done by the following line.
   ?- mortal( P ).
The Prolog interpreter responds.
   P = socrates
   yes
This means that Prolog was able to prove the goal by binding the variable P to socrates. This was done by again proving someone was mortal by proving the subgoal that they were human. Prolog thus asked if there was any P that was human. This matches against the clause human(socrates) thereby binding P to socrates. This binding is then passed back to the parent goal, and the results in the printout we saw above.

Rule IV
Sometimes we may wish to specify alternative ways of proving a particular thing. This we can do by using different rules and facts with the same name. For example, we can represent the sentence “Something is fun if its a red car or a blue bike or it is ice cream” as follows:
   fun(X) :-             /* An item is fun if the item */
      red(X), car(X).    /*  is red and it is a car. */
   fun(X) :-             /* Or an item is fun if the item */
      blue(X), bike(X).  /*  is blue and it is a bike. */
   fun(ice_cream).       /* And ice cream is also fun. */
This program says that we have three ways of finding out if something is fun. These three options are represent in Prolog by three clauses of the predicate fun. If that does not succeed, we try the next clause. We only fail when we run out of rules or facts to try.