Prolog ("PROgrammation en LOGique")

Thinking as Computation, 3, 4, Appendix A, Appendix B, Appendix C, Appendix D

(Hector Levesque (author of CT) has made available a collection of prolog programs. I recommend that you download them. This will make it easier to work with some of the examples in the book.)


Prolog is a computer language based on logic programming.

Download SWI Prolog or use the online version (which is probably the simplest option).

For a straightforward and helpful general tutorial on Prolog, see Learn Prolog Now!

In Prolog, programs are definite programs. The way Prolog works will be partly familiar to you on the basis of the last lecture. On the left, there are two examples of clauses in the propositional calculus. On the right, there are the corresponding ways they are written in Prolog.


a               |               a.                      (fact)
                |
a ∨ ¬b ∨ ¬c     |               a :- b, c.              (rule) (head :- body)

The following equivalent formulations helps to clarify the correspondence:


a ∨ ¬b ∨ ¬c       iff           a ∨ ¬(b ∧ c)
                                (b ∧ c) → a
                                a ← (b ∧ c)
                                a :- b, c.   


Queries are understood as negative clauses. The following is a negative clause:

¬a ∨ ¬b ∨ ¬c

This negative clause is equivalent to

¬(a ∧ b ∧ c)

In Prolog, this negative clause corresponds to a query and is written as

?- a, b, c.



The basic evaluation step of Prolog is to replace a query call by the body (or what is sometimes called the tail) of the program clause whose head it matches.

(This corresponds to an application of the inference rule called resolution. Given two clauses presented in disjunctive format, resolution eliminates a pair of complementary literals and constructs a new clause (the resolvent) from the literals that remain. Two literals are complimentary if and only if one is the negation of the other.)


In Prolog                               In logic

query           ?-a,d,e.                ¬a ∨ ¬d  ∨ ¬e
program clause  a:-b,c.                  a ∨ ¬b ∨ ¬c
derived query   ?-b,c,d,e.              ¬b ∨ ¬c  ∨ ¬d ∨ ¬e

The logical relationship is {query, program clause} ⊢ derived query



Here are some screen shots from the session in which I was playing around with Prolog and a simple KB.

This is how my screen looks when I start Prolog:

Welcome to SWI-Prolog (Multi-threaded, 32 bits, Version 6.6.1)
Copyright (c) 1990-2013 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

1 ?- 

Here is how it looks when I enter (or assert) some facts and rules into the KB. (For some explanation of the four database manipulation commands in Prolog, see Database Manipulation in the tutorial.) The fact that I assert is that "Socrates is a man." The rule that I assert is that "whatever is a man is mortal":


Welcome to SWI-Prolog (Multi-threaded, 32 bits, Version 6.6.1)
Copyright (c) 1990-2013 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

1 ?- assert(man(socrates)).
true.

2 ?- assert( (mortal(X):-man(X)) ).
true.

3 ?- 

Here is how it looks when I ask "whether Socrates is a man," "whether something is mortal," and "whether something is man":


Welcome to SWI-Prolog (Multi-threaded, 32 bits, Version 6.6.1)
Copyright (c) 1990-2013 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

1 ?- assert(man(socrates)).
true.

2 ?- assert(mortal(X):-man(X)).
true.

3 ?- man(socrates).
true.

4 ?- mortal(X).
X = socrates.

5 ?- man(X).
X = socrates.

6 ?- 


• The "Family" example (CT, 42). Here is a listing of the facts and rules:

% This is the Prolog version of the family example

child(john,sue).   child(john,sam).   
child(jane,sue).   child(jane,sam).   
child(sue,george).   child(sue,gina). 

male(john).   male(sam).     male(george). 
female(sue).  female(jane).  female(june). 

parent(Y,X) :- child(X,Y).
father(Y,X) :- child(X,Y), male(Y).
opp_sex(X,Y) :- male(X), female(Y). 
opp_sex(Y,X) :- male(X), female(Y). 
grand_father(X,Z) :- father(X,Y), parent(Y,Z).

I double-clicked on the icon, Prolog loaded the file, and I asked some questions:

% p:/Work Desktop/family.pl compiled 0.02 sec, 18 clauses
Welcome to SWI-Prolog (Multi-threaded, 32 bits, Version 6.6.1)
Copyright (c) 1990-2013 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

1 ?- father(sue,X).
false.

2 ?- father(sam,X),female(X).
X = jane.

3 ?- child(X,sam).
X = john ;
X = jane.

4 ?- male(president_crow).
false.

5 ?- 

• To test your understanding, here is a question about the fourth query in the family example.

President Crow (the president of ASU) is male, but Prolog responds with "false" to the query is President Crow male. What is going on here? To answer, explain what "false" means in this context. It cannot mean that "President Crow is male" is false, but it must mean that something is false. What is it?



• Here is how the Pulp Fiction example looks in Prolog on my Arch Linux machine. (This machine is newer than the one I used in the previous examples, so this example looks a little different). I consult a file I created called "pulpfiction." This file contains the program. I list the two predicates. I ask a few questions. I halt Prolog.

	
tom:arch [~]
% swipl
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.2.3)
Copyright (c) 1990-2015 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

?- [pulpfiction].
true.

?- listing(loves).
loves(vincent, mia).
loves(marcellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

true.

?- listing(jealous).
jealous(A, B) :-
	loves(A, C),
	loves(B, C).

true.

?- loves(mia,vincent).
false.

?- jealous(marcellus, W).
W = vincent .

?- halt.

tom:arch [~]
% 

	


• This is not a course in computer programming, but you should play around some with Prolog. The easiest way to do this is to use the online version, in which you can cut and paste programs and queries. This is straightforward to do. It is illuminating, and you might even have a little fun.









move on g back