Prolog Tutorial 1: Facts and Rules

James Power, 1997.


Since we've just met facts and rules, we want to get some practice with using them.


The Rules

The program we wrote in the last tutorial was a fairly small one, so we won't be adding many rules.

Test your program by loading it into Prolog after each modification, and running the following queries against it:

(Do this now, before you change anything!)

The difference between facts and rules is that rules are conditional, and use Prolog's "if" operator.

For the moment, we'll just be using three operators in Prolog:

OperatorMeaning
:-if
,and
;or

Open the file in the text editor and try adding in rules to express the following:

Do these one at a time, testing the above queries each time


The Family Tree Example

Suppose that we want to represent a family tree, so that we can ask questions like "is John related to ...", or "list all John's sisters" and so on.

The basic entities will be people; the properties we will want to look at will be father, mother, brother, sister, ..... We choose three basic predicates, male, female and parent, which will describe a family by a series of facts.

Take the following family tree as an example:

                              James I
                                 |
                                 |
                +----------------+-----------------+
                |                                  |
             Charles I                          Elizabeth
                |                                  |
                |                                  |
     +----------+------------+                     |
     |          |            |                     |
 Catherine   Charles II   James II               Sophia
                                                   |
                                                   |
                                                   |
                                                George I

In Prolog we represent this as:

  % male(P) is true when P is male
  male(james1).
  male(charles1).
  male(charles2).
  male(james2).
  male(george1).

  % female(P) is true when P is female
  female(catherine).
  female(elizabeth).
  female(sophia).

  % parent(C,P) is true when C has a parent called P
  parent(charles1, james1).
  parent(elizabeth, james1).
  parent(charles2, charles1).
  parent(catherine, charles1).
  parent(james2, charles1).
  parent(sophia, elizabeth).
  parent(george1, sophia).

Start a new file in your text editor (call it "family.pl"), and copy and paste the above program into it.

We can now formulate some queries (try entering these yourself):

Try adding the following rules to the program, and check the results:

Remember that "and" in Prolog is represented using a comma. Also, the connection between predicates should be made by sharing variables (and not by embedding one predicate inside another).

If you get this done, can you add rules for:


Written by James Power
Released under the GNU Free Documentation License
Last revised: 22 Oct 2006.