Prolog As AI Programming Language - 1
Prolog As AI Programming Language - 1
LANGUAGE
PRINCIPLE OF LOGICAL
PROGRAMMING
3
FACTS AND RULES
4
FACTS…
6
THE SWI-PROLOG ENVIRONMENT
7
THE SWI-PROLOG…
8
STARTING PROLOG AND LOADING A
PROGRAM
This file can be opened in Prolog from the Start Menu,
by opening .pl files in the Windows explorer or using the
following command in the Prolog application.
?- [swi('demo/likes')].
Or if you have the absolute address we can use the
following
?- ['C:/Users/Documents/Prolog/parents'].
9
EXECUTING A QUERY
After loading a program, one can ask Prolog queries
about the program. The system responds with X =
<value> if it can prove the goal for a certain X.
The user can type the semi-colon (;) if (s)he wants
another solution, or RETURN if (s)he is satisfied, after
which Prolog will say True. If Prolog answers False, it
indicates it cannot find any (more) answers to the query.
Finally, Prolog can answer using an error message to
indicate the query or program contains an error.
10
SYNTAX OF PROLOG
What exactly are facts, rules and queries built out of?
Prolog Terms
11
PROLOG TERMS
Terms
Constants Variables
Atoms Numbers
12
ATOMS
14
VARIABLES
15
COMPLEX TERMS
Examples re
parent (pam, bob).
female(pam).
male(tom).
17
ARITY
The number of arguments a complex term has is
called its arity.
Examples:
female(pam). is a term with arity 1
parent (pam, bob). has arity 2
father(father(joy)) arity 1
parent/2
father/1
18
PROLOG
Defining relations by facts
It is especially well suited for solving problems that
involves objects and relations between objects.
19
Example : The Family Tree
Pam Tom
Bob Liz
Ann
Pat
Jim 20
The fact that Tom is a parent of Bob can be written in Prolog as:
parents (tom, bob).
21
The whole family tree of the above figure is defined by the
following Prolog program:
parent (pam, bob).
parent (tom, bob).
parent (tom, liz).
parent (bob, ann).
parent (bob, pat).
parent (pat, jim).
22
When this program has been communicated to the Prolog system,
prolog can be posed some questions about the parent relation.
For example: is Bob a parent of Pat? This questions can be
communicated to the Prolog system by typing into the terminal:
?-parent (bob, pat).
Having found this as an asserted fact in the program, Prolog will
answer: yes/true.
If it doesn’t find a clause that satisfies the relation, Prolog will
answer: no/false.
23
Who is Liz’s parent?
?-parent (X, liz).
The Prolog will tell us what the value of X is such that the above
statement is true. So the answer is: X=tom.
The question who are Bob’s children? Can be communicated to
Prolog as:
?-parent (bob, X).
24
We can ask other complicated questions like who is a grandparent
of jim? Our program don’t have a grandparent relation so the
query has to be broken down into two steps,
Who is a parent of jim? Assume that this is some Y.
Who is a parent of Y? Assume that this is some X.
Such a composed query is written in Prolog as a sequence of two
simple ones:
?-parent (Y, jim), parent (X,Y).
The answer will be:
X=bob
25
Y=pat
Defining relations by rules
We could define offspring in a similar way as the parent relation;
that is, by simply providing a list of simple facts about the
offspring relations
offspring (liz, tom).
26
For all X and Y,
Y is an offspring of X if
X is a parent of Y.
This formulation is already close to the formalism of Prolog. The
corresponding Prolog clause which has the same meaning is:
27
Before defining this relationship we define the female relation
female(pam).
male(tom).
male(bob).
female(liz).
female(pat).
female(ann).
male(jim).
Let us now add more family relation to our example program. The
specification of the mother relation can be based on the following logical
statement:
For all X and Y,
X is the mother of Y if
X is a parent of Y and
X is a female.
This is translated into Prolog as the following rule:
THE ANSWER IS IN THE PARENT.PL
FILE WHICH ACCOMPANIES THIS SLIDE.
30
WRITING FACTS ABOUT DIRECTED
GRAPH
b e
c g
a
d f
31
ANSWER
THE ANSWER IS IN THE GRAPH.PL
FILE WHICH ACCOMPANIES THIS SLIDE.
32
NEGATION AS FAILURE
33
PROPER USE OF NEGATION AS FAILURE
loves(bill,X) :- pretty(X), female(X), not(loves(tom,X)).
loves(tom,X):- famous(X), female(X), not(dead(X)).
female(marilym_monroe).
female(cindy_crawford).
female(martha_stewart). ?-loves(tom,X).
female(girl_next_door). X=cindy_crawford;
X=martha_stewart;
famous(marilym_monroe). ?-loves(bill,X).
famous(cindy_crawford). X=marilyn_monroe;
X=girl_next_door;
famous(martha_stewart).
pretty(marilym_monroe).
pretty(cindy_crawford).
pretty(girl_next_door).
34
dead(marilym_monroe).
PROLOG: FAIL & TRUE PREDICATES
How to represent “Mary likes all animals but snakes”?
If X is a snake then Mary likes X is not true
otherwise if X is an animal then Mary likes X
snake(cobra).
cat(persian).
animal(X):-snake(X);cat(X).
likes(mary,X):-snake(X),!,fail.
likes(mary,X):-animal(X).
?- likes(mary,cobra).
no
rule 1 failed because of fail and the ! told the interpreter not to try
rule 2 because since we have reached there(aka to the !), it must
35
be a snake
UNIFICATION
?- mia=mia. ?-K(s(g),Y)=K(X,t(k)).
True X=s(g)
Y=t(k)
?-mia=joy. ?- k(s(g),t(k)) = k(X,t(Y)).
X=s(g)
False Y=k
?- loves(X,X) = loves(marsellus,mia).
?- mia=X. no
X=mia
?- X=mia,X=joy.
38
False