<html><head><title>The Basics of Prolog</title></head><body><p>
 </p><hr> <a name="tex2html422" href="http://www.macs.hw.ac.uk/%7Ealison/ai3notes/section2_3_3.html"><img src="BasicsOfProlog_files/next_motif.html" align="middle"></a> <a name="tex2html420" href="http://www.macs.hw.ac.uk/%7Ealison/ai3notes/chapter2_3.html"><img src="BasicsOfProlog_files/up_motif.html" align="middle"></a> <a name="tex2html414" href="http://www.macs.hw.ac.uk/%7Ealison/ai3notes/subsectionstar2_3_1_1.html"><img src="BasicsOfProlog_files/previous_motif.html" align="middle"></a> <a name="tex2html424" href="http://www.macs.hw.ac.uk/%7Ealison/ai3notes/tableofcontents2_1.html"><img src="BasicsOfProlog_files/contents_motif.html" align="middle"></a> <br>
<b> Next:</b> <a name="tex2html423" href="http://www.macs.hw.ac.uk/%7Ealison/ai3notes/section2_3_3.html"> Prolog TermsBacktracking </a>
<b>Up:</b> <a name="tex2html421" href="http://www.macs.hw.ac.uk/%7Ealison/ai3notes/chapter2_3.html"> Artificial Intelligence Programming </a>
<b> Previous:</b> <a name="tex2html415" href="http://www.macs.hw.ac.uk/%7Ealison/ai3notes/subsectionstar2_3_1_1.html"> The Main AI </a>
<hr> <p>
</p><h1><a name="SECTION0032000000000000000"> The Basics of Prolog</a></h1>
<p>
A prolog program consists of a set of facts and a set of
rules. There are no type declarations, initialisations or any other
stuff like that. Just some facts and rules. <br>Some prolog facts are:
</p><p>
</p><pre><tt>
   lectures(alison, ai).
   lectures(john, databases).
   female(alison).
   age(alison, 29). 		[Oh, OK, that was last year..]
   office(alison, s134).
   animal(lion).
   animal(sparrow).
   has_feathers(sparrow).</tt></pre>
<p>
Facts consist of:
</p><p>
</p><ul><li> A predicate name (or <em>functor</em>) such
as lectures, female, and office. This
must begin with a lower case letter.
</li><li> Zero or more arguments, such as alison, ai3,
and s134. 
</li></ul>
<p>
Note that facts (and rules, and questions) must end with a full stop.
</p><p>
Some prolog rules are:
</p><p>
</p><pre><tt>
   bird(X) :-
     animal(X),
     has_feathers(X).

   grandparent(X, Y) :-
     parent(X, Z),
     parent(Z, Y).</tt></pre>
<p>
You should read the prolog operator ``:-'' as ``if'',
while ``,'' can be read as meaning ``and''.
So the first rule says that ``X is a bird if X is an animal and
X has feathers'', while the second rule says that
``Y is X's grandparent if Z is X's parent and Y is Z's parent''.
All arguments beginning with a capital letter (such as X and Y)
are variables. (Note that variables are
NOT treated in the same manner as in conventional
programming languages - for example, they don't have to have
values). Any constant should NOT begin with
a capital letter else it will be treated as a variable. So, given the
fact capital(Scotland, Edinburgh) both arguments would
be treated as unbound variables.
</p><p>
``Running'' a prolog program involves asking Prolog questions
(having loaded in your set of facts and rules). For example,
you could ask:
</p><p>
</p><pre><tt>
   ?- lectures(alison, ai).</tt></pre>
<p>
And prolog would give the answer ``yes''. If we ask a 
sequence of questions we might get:
</p><p>
</p><pre><tt>
   ?- lectures(alison, ai).
   yes
   ?- lectures(alison, databases).
   no</tt></pre>
<p>
Questions can have variables in them, which may get <em>instantiated</em>
(ie, get bound to particular values) when prolog tries to answer the question.
Prolog will display the resulting bindings/instantiations<a name="tex2html1" href="http://www.macs.hw.ac.uk/%7Ealison/ai3notes/all.foot.html#910"><img src="BasicsOfProlog_files/foot_motif.html" align="middle"></a>
of all the variables in the question. So we might have:
</p><p>
</p><pre><tt>
   ?- lectures(alison, Course).

   Course = ai</tt></pre>
<p>
We can also ask the question the other way around, like:
</p><p>
</p><pre><tt>
   ?- lectures(Someone, ai).
 
   Someone = alison</tt></pre>
<p>
Note that the variables Course and Someone can both
take values of any type. <br>We can find out who lectures what by asking:
</p><p>
</p><pre><tt>
   ?- lectures(Someone, Something).

   Someone = alison
   Something = ai ;

   Someone = john
   Something = databases ;

   no</tt></pre>
<p>
By typing a semicolon (or, in Mac Prolog, clicking on ``next'') 
after Prolog prints out the first
set of bindings, we can see if there are any other possible bindings.
Prolog systematically goes through all its facts and rules and
tries to find all the ways it can associate variables with particular values
so that the initial query is satisfied.
We'll discuss in the next lecture how
it does this. However, as an example of how rules are used, suppose
we ask the question:
</p><p>
</p><pre><tt>
   ?- bird(B).</tt></pre>
<p>
and we have the facts and rule:
</p><p>
</p><pre><tt>
   animal(lion).
   animal(sparrow).
   has_feathers(sparrow).

   bird(X) :-
     animal(X),
     has_feathers(X).</tt></pre>
<p>
Prolog will respond with
</p><p>
</p><pre><tt>
   B = sparrow.</tt></pre>
<p>
Prolog <em>matches</em> bird(B) against the <em>head</em> of the
rule (bird(X)), and sets as new questions first animal(B) and then
has_feathers(B). Animal(B) can be satisfied by
binding B to lion. However, has_feathers(lion) isn't true,
so that doesn't work. Prolog goes back (<em>backtracks</em>) and
tries B = sparrow. has_feathers(sparrow) is true, so that
all works and prolog returns with B = sparrow as a possible solution.
</p><p>
</p><hr> <a name="tex2html422" href="http://www.macs.hw.ac.uk/%7Ealison/ai3notes/section2_3_3.html"><img src="BasicsOfProlog_files/next_motif.html" align="middle"></a> <a name="tex2html420" href="http://www.macs.hw.ac.uk/%7Ealison/ai3notes/chapter2_3.html"><img src="BasicsOfProlog_files/up_motif.html" align="middle"></a> <a name="tex2html414" href="http://www.macs.hw.ac.uk/%7Ealison/ai3notes/subsectionstar2_3_1_1.html"><img src="BasicsOfProlog_files/previous_motif.html" align="middle"></a> <a name="tex2html424" href="http://www.macs.hw.ac.uk/%7Ealison/ai3notes/tableofcontents2_1.html"><img src="BasicsOfProlog_files/contents_motif.html" align="middle"></a> <br>
<b> Next:</b> <a name="tex2html423" href="http://www.macs.hw.ac.uk/%7Ealison/ai3notes/section2_3_3.html"> Prolog TermsBacktracking </a>
<b>Up:</b> <a name="tex2html421" href="http://www.macs.hw.ac.uk/%7Ealison/ai3notes/chapter2_3.html"> Artificial Intelligence Programming </a>
<b> Previous:</b> <a name="tex2html415" href="http://www.macs.hw.ac.uk/%7Ealison/ai3notes/subsectionstar2_3_1_1.html"> The Main AI </a>
<hr> <p>
</p><hr>

<p></p><address>
<i>alison@ <br>
Fri Aug 19 10:42:17 BST 1994</i>
</address></body></html>