1.4 An Example

Use the emacs editor to create a file called family.pl.

When you have created a new file window, type the following, being particularly careful to type exactly what appears here, including full stops and commas:

male(harry).  
female(liz).  
 
parent(phil, chas).  
parent(liz, chas).  
parent(chas,harry).  
parent(chas,wills).  
 
 
grandmother(GM, C):-  
    mother(GM, P),  
    parent(P, C).  
 
mother(M,C):-  
    female(M),  
    parent(M, C).  

1.4.1 Saving a file

Before you can do anything with this, you must save your file. Use Save from the File menu (or Ctrl-x Ctrl-s).

1.4.2 Consulting a file

Now that you have created a file, you need to inform Prolog of its contents. You do this by consulting the file. There are two ways to do this.

First, use the mouse to select the SWI-Prolog window in which Prolog is running. Then, type either

|?- consult(family).

or

|?- [family].

Note: It is conventional to give Prolog source files the extension .pl. SWI-Prolog knows about this, so you do not need to give the full filename and extension when consulting it. In fact, if you do type consult(test.pl). or [family.pl], you will receive an error message for your pains!

This is the standard procedure in all Prolog systems.

Alternatively, put the cursor in your emacs file window and use the key combination Ctrl-c Ctrl-b (For ‘consult buffer’ — windows are called ‘buffers’ in emacs-speak).

If your file contains no errors, you should receive a message in the SWI-prolog window telling you that the file has been consulted successfully. You are now in a position to get Prolog to do some work.

1.4.3 Posing a goal

Once you have consulted your file, you are in a position to get Prolog to do some work.To get Prolog to do some computation, you need to issue a goal. Click on the Prolog window and type the following (|? is the Prolog prompt):

|?- grandmother(liz, Who).

Type this exactly, including the capital W and the final full-stop, and, hit <return>. Prolog will respond:1

 Who = harry

Next, type a semi-colon followed by <return>. Prolog will respond

Who = wills

Again, type a semi-colon followed by <return>. Prolog will respond:

no

This means that you have now received all the responses to the goal that Prolog can compute on the basis of the information supplied in your program file.

1.4.4 Tracing

You will frequently want to see what Prolog does while it is evaluating your programs. This is often necessary when you are trying to track down bugs in your program. Unfortunately, Prolog’s debugging environment is not very good. To trace the execution of a goal (such as grandmother(liz, Who).), type

|?-trace.

and then a goal, such as

|?- grandmother(liz, Who).

Prolog will then output to the screen the sequence of steps it carries out in executing this goal

  ?- grandmother(liz, Who).  
   Call: (6) grandmother(liz, _G396) ? creep  
   Call: (7) mother(liz, _G450) ? creep  
   Call: (8) female(liz) ? creep  
   Exit: (8) female(liz) ? creep  
   Call: (8) parent(liz, _G450) ? creep  
   Exit: (8) parent(liz, chas) ? creep  
   Exit: (7) mother(liz, chas) ? creep  
   Call: (7) parent(chas, _G396) ? creep  
   Exit: (7) parent(chas, harry) ? creep  
   Exit: (6) grandmother(liz, harry) ? creep  
 
 
Who = harry  
 
 
Yes  
 
[debug]  ?-

At each point where it calls a goal, Prolog pauses and requires a carriage return from you before proceeding.

To turn off tracing, type

| ?- nodebug. <return>

(Note the full stop!)