Next: Prolog TermsBacktracking Up: Artificial Intelligence Programming Previous: The Main AI

The Basics of Prolog

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.
Some prolog facts are:


   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).

Facts consist of:

Note that facts (and rules, and questions) must end with a full stop.

Some prolog rules are:


   bird(X) :-
     animal(X),
     has_feathers(X).

   grandparent(X, Y) :-
     parent(X, Z),
     parent(Z, Y).

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.

``Running'' a prolog program involves asking Prolog questions (having loaded in your set of facts and rules). For example, you could ask:


   ?- lectures(alison, ai).

And prolog would give the answer ``yes''. If we ask a sequence of questions we might get:


   ?- lectures(alison, ai).
   yes
   ?- lectures(alison, databases).
   no

Questions can have variables in them, which may get instantiated (ie, get bound to particular values) when prolog tries to answer the question. Prolog will display the resulting bindings/instantiations of all the variables in the question. So we might have:


   ?- lectures(alison, Course).

   Course = ai

We can also ask the question the other way around, like:


   ?- lectures(Someone, ai).
 
   Someone = alison

Note that the variables Course and Someone can both take values of any type.
We can find out who lectures what by asking:


   ?- lectures(Someone, Something).

   Someone = alison
   Something = ai ;

   Someone = john
   Something = databases ;

   no

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:


   ?- bird(B).

and we have the facts and rule:


   animal(lion).
   animal(sparrow).
   has_feathers(sparrow).

   bird(X) :-
     animal(X),
     has_feathers(X).

Prolog will respond with


   B = sparrow.

Prolog matches bird(B) against the head 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 (backtracks) and tries B = sparrow. has_feathers(sparrow) is true, so that all works and prolog returns with B = sparrow as a possible solution.



Next: Prolog TermsBacktracking Up: Artificial Intelligence Programming Previous: The Main AI


alison@
Fri Aug 19 10:42:17 BST 1994