Simple Facts

We will enter some simple facts about 3 toy blocks called a, b and c. We will record information on their colour. There are several ways to get this information into Prolog. I shall describe one way for the moment: preloading the data from a file that you had previously created, using the -s"switch" on the prolog command.

Create a file with the fact(s) you want to load into Prolog in it, using your favourite text editor (like emacs). Let's assume you call your file data1 and that it has the fact colour(a, red) in it:

 $ cat data1
 colour(a, red).

We now start Prolog using the following form of the Unix prolog command:

 $ swipl -s data1
 ... welcome message ...
 ?-

Prolog now stores the fact in data1 in its database .

The fact that you entered is supposed to indicate that block a is red.

The full stop (.) at the end of each line of the file data1 indicates to Prolog that this is the end of a fact (or clause ). If you leave out the full stop, Prolog will become confused. It will issue an error message something like the following

ERROR: /import/.../yourlogin/data1:1: Syntax error: Unexpected end of file

If this happens, type control-D to exit Prolog, and have a look at your data1 file, with emacs or whatever. Find the mistake, and fix it. Then start Prolog again, with prolog -s data1 as before.

We can now ask Prolog if it can prove that block a is red by asking the following query .

 ?- colour(a, red).
 true.

Prolog attempted to prove this goal by checking its memory.

The reason Prolog can prove a is red is that we asserted that fact in the previous step. We can ask if Prolog can prove that block b is blue.

?- colour(b, blue).
false.

It cannot prove it as we have not told Prolog this fact. We now exit Prolog (by typing a control-D), and add a line with colour(b, blue). on it to the file data1. Then we restart Prolog:

 % swipl -s data1
 ... messages and welcome ...
 ?- colour(a, red).
 true.
 ?- colour(b, blue).
 true.

Now Prolog can prove that block b is blue.

We will assume that block c is painted half red and half blue. That is, it is both red and blue. For this we need to add two facts to file data1:

colour(c, red).
colour(c, blue).

Quit Prolog, edit data1, restart Prolog, and use the following queries to test this.

?- colour(c, red).
?- colour(c, blue).

Variables

A query can contain variables which represent terms that are not known by the user. For example, to find the colour of block a, we can use the query...

 ?- colour(a, X).

Here, X, is a variable. The name of a variable must begin with a capital letter or an underscore character, "_".

Prolog responds by finding a possible value for X where the goal can be proven.

 X = red

After finding this binding , Prolog waits for the user to type either a <return>/<Enter> or a semicolon (;). If the user types a <return>, Prolog stops looking for bindings that make the query provable, and says true.. If the user types a semicolon, Prolog looks for more bindings, and if it can't find any, as here, responds false.. Try it one way, then repeat the query and try it the other way.

To find all blocks which are coloured red, we could use the query...

?- colour(Block, red).
 Block = a ;
 Block = c.

Remember - you have to type the semicolon. Here the goal can be proven for two different values of the Block variable.

If you ask a query with variables, and Prolog can't prove the goal for any value of the variables, then Prolog will respond with "false." just as before. Try this to find the colour of block d...

?- colour(d, X).

Getting the Facts

Please download this file and save it to your directory:

family.pl

Note: on the CSE machines, the following command might work:

$ cp /home/cs3411/public_html/Labs/family.pl family.pl

View the copied file using your preferred text viewer (e.g. cat, view or more). The file shows a list of facts which represent a family tree. For your own benefit, you should draw a diagram of the family tree represented in the program and annotate each node with gender and date-of-birth information.

Running Queries

Start SWI prolog with the family.pl program. (From now on we'll leave out Prolog's welcome message completely.):

$ swipl -s family.pl
?-

Remember, if you make a mistake and you want to get a fresh Prolog prompt (:), you can send the interupt signal, control-C, and then typing a for abort. You can quit SWI prolog by sending the end-of-file signal, control-D, at the prompt.

Prolog will read in the family.pl program and prompt you for input. Ask Prolog if Albert is the parent of Peter by entering the query :

 ?- parent(albert, peter).

Prolog responds with

 true.

to indicate that the goal was proven (as it was contained in the family.pl program).

Ask Prolog if Albert is the parent of Brian by entering the query:

 ?- parent(albert, brian).

SWI prolog responds with

 false.

to indicate that the goal could not be proven from the family.pl program.

If you make the mistake of not ending your query with a full-stop, then Prolog will issue a | and will then wait until some more stuff is entered. Try this now:

?- parent(albert, jim)
SWI prolog will just respond with...
 |
...until you enter the full-stop ...
 | .
true.

Resource created Saturday 06 February 2021, 09:11:00 PM.


Back to top

COMP3411/COMP9814 21T1 (Artificial Intelligence) is powered by WebCMS3
CRICOS Provider No. 00098G