Consider the following
'All men are mortal':
We can express this as the following Prolog rule
mortal(X) :-
human(X).
The clause can be read in two ways (called either a declarative or a
procedural interpretation). The declarative interpretation is "For a given
X, X is mortal if X is human." The procedural interpretation is "To prove
the main goal that X is mortal, prove the subgoal that X is human."
If we now pose the question to Prolog
?- mortal(socrates).
The Prolog interpreter would respond as follows:
yes
Why was this? Well in order to solve the query ?- mortal(socrates)., we used the rule we saw previously. This said that in order to prove someone mortal, we had to prove them to be human. Thus from the goal Prolog generates the subgoal of showing human(socrates).
In the above example we were able to match human(socrates) against the database described at the top of this card. In Prolog we say that the subgoal succeeded, and as a result the overall goal succeeded. We know when this happens because Prolog prints yes.
?- 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.
fun(X) :- /* an item is fun if */
red(X), /* the item is red */
car(X). /* and it is a car */
fun(X) :- /* or an item is fun if */
blue(X), /* the item is blue */
bike(X). /* 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. Something is fun if it is a red and a car or blue and a bike, or if
it is ice cream. These three options are represent in Prolog by three
clauses of the predicate fun. Just like we saw for pure facts, Prolog will
start from the first clause (beit a rule or fact) of fun and try that. If
that does not succeed, we try the next clause. We only fail when we run
out of rules or facts to try.
The program
Above is both our previous program for finding fun items and facts describing some red and blue items and some cars and bikes. Let's now use the above program and see if a harley_davidson is fun. To do this we can ask Prolog the following question.
?- fun(harley_davidson). /* to which Prolog will reply */
yes /* to show the program succeeded */
To execute this query, Prolog will first see if harley_davidson is red,
however only vw_beatles and ford_escorts are defined as being red. Hence
the query red(harley_davidson) will fail. This means that the first clause
of fun will fail. As a result, we now try the second clause of fun. This
will mean that we will attempt the subgoals blue(harley_davidson) and
bike(harley_davidson). Both these goals match facts in the database. As a
result fun succeeds as we saw above.
We can also ask our program to find fun items for us. To do this we can pose the following question.
?- fun(What).
To which Prolog will reply
What=vw_beatle
yes
Let's see how Prolog deals with this query. Firstly we will try the first
clause of fun. This results in us trying the goal red(What). This
succeeds matching the first clause of red with the binding What=vw_beatle.
Now we attempt the goal car(vw_beatle). This matches the first clause of
car, and, as a result, the fun goal succeeds.
Exercise 5
Given the database below, study the queries underneath it.
Indicate whether you think a particular query will succeed or fail by
answer yes or no using the buttons.