Running a Prolog program involves
The version of Prolog we will be using is one which runs on a variety of different platforms and is called SWI-Prolog. At York, it is accessible on the university’s Windows network. (Information about how to get your own copy is provided at the end of this documentation.)
You should be rewarded by the appearance of a new window containing some welcome messages and the Prolog prompt |?- , which indicates that Prolog is ready for input.
You may find it convenient to create a shortcut to the Prolog application on your desktop.
To create a Prolog program, you must use an editor to create a file. There are a number of editors available and you can use whatever you prefer. If you have no preference, I suggest using the emacs editor that comes with SWI-prolog. It is Prolog-aware and will format and colour-code Prolog programs for you, as well as providing a number of other Prolog-related conveniences. To use emacs,
Here is how you create a Prolog program.
All these course notes are available over the web by using a browser such as Internet Explorer. The URL is:
http://www-users.york.ac.uk/ sjh1/
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). |
Before you can do anything with this, you must save your file. Use Save from the File menu (or Ctrl-x Ctrl-s).
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.
To get Prolog to do some computation, you need to issue a goal. Type
|?- 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.
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!)
You may be in the (good) habit of organising your work by putting material related to a particular topic into a (sub-)directory. If you decide to do this with your Prolog files, you need to be aware of difference in the way you specify the filename using consult.
If the file is in the root directory of your M drive, and the file has the form <filename>.pl, you simply type
| ?- consult(<filename>). <return>
|
If, however, you have created a directory called, say myprolog and your file <filename>.pl lies inside that directory, the above instruction will not work and you will receive an error message.
What you have to do is:
| ?- consult('myprolog/<filename>.pl'). <return>
|
Alternatively, start Prolog and select Consult or Edit from the File menu and navigate to the relevant folder.