Lecture 1 - Introduction to Prolog
Lecture 1 - Introduction to Prolog
Ivan Bratko
Addision Wesley
1
Part 1 The Prolog Language
Chapter 1
Introduction to Prolog
(Programming in Logic)
2
Introduction to PROLOG
In PROLOG, predicate expressions consist of the
predicate name, followed by zero or more
arguments enclosed in parentheses, separated by
commas.
Example: father(Ali, Ahmad) means that Ali is
the father of Ahmad
3
General Organization of a PROLOG
System
4
PROLOG Continued
Programs consist of facts and rules in the
general form of goals.
General form: p:- p1, p2, …, pN
p is called the rule’s head and the pi
represents the subgoals
Example:
parent(X,Y) :- father(X,Y)
X is the parent of Y if X is the father of Y
5
PROLOG Continued
• Examples of PROLOG predicates and
comments preceded by semicolons:
6
Defining relations by facts
Given
The tree
a whole
defined
family
by the
tree
Prolog program:
jim
7
Defining relations by facts
Questions (Queries):
Is Bob a parent of Pat? pam tom
ann pat
Who is Liz’s parent?
?- parent( X, liz).
jim
8
Defining relations by facts
pam tom
Questions:
Who is a parent of whom?
bob liz
Find X and Y such that X
is a parent of Y.
ann pat
?- parent( X, Y).
Jim? X
?- parent( Y, jim),
parent
parent( X, Y). Y grandparent
parent
jim
9
Defining relations by facts
tom
Questions: pam
parent( X, Y).
ann pat
Do Ann and Pat have a
common parent?
jim
?- parent( X, ann),
parent( X, pat).
10
Defining relations by facts
It is easy in Prolog to define a relation.
The user can easily query the Prolog system about
relations defined in the program.
A Prolog program consists of clauses. Each clause
terminates with a full stop.
The arguments of relations can be
Atoms: concrete objects or constants such as tom, pat
Variables: general objects such as X and Y
Questions to the system consist of one or more goals.
An answer to a question can be either positive
(succeeded) or negative (failed).
If several answers satisfy the question then Prolog will find
as many of them as desired by the user.
11
Defining relations by rules
Facts:
female( pam). % Pam is female
female( liz).
female( ann).
female( pat). pam tom
male( tom). % Tom is male
male( bob).
male( jim). bob liz
Example:
offspring( Y, X) :- parent( X, Y).
The rule is general in the sense that it is
applicable to any objects X and Y.
A special case of the general rule:
offspring( liz, tom) :- parent( tom, liz).
X
?- offspring( liz, tom).
?- offspring( X, Y). parent offspring
Y
13
Defining relations by rules
Define the “mother” relation:
mother( X, Y) :- parent( X, Y), female( X).
For all X and Y,
X is the mother of Y if
X is a parent of Y and
X is a female.
female
X
parent mother
14
Defining relations by rules
Define the “grandparent” relation:
grandparent( X, Z) :-
parent( X, Y), parent( Y, Z).
parent
Y grandparent
parent
15
Defining relations by rules
Define the “sister” relation:
sister( X, Y) :-
parent( Z, X), parent( Z, Y),
female(X).
For any X and Y,
X is a sister of Y if
(1) both X and Y have the same parent, and
(2) X is female.
?- sister( ann, pat). Z
?- sister( X, pat). parent
parent
?- sister( pat, pat).
Pat is a sister to herself?!
X Y
female
sister
16
Defining relations by rules
To correct the “sister” relation:
sister( X, Y) :-
parent( Z, X), parent( Z, Y),
female(X),
different( X, Y).
different (X, Y) is satisfied if and only if X and Y
are not equal. (Please try to define this function)
parent parent
X Y
female
sister
17
Defining relations by rules
Prolog clauses consist of
Head
Body: a list of goal separated by commas (,)
18
Defining relations by rules
A variable can be substituted by another object.
19
Recursive rules
Define the “predecessor()” relation
X X
parent predecessor
X
Y2
parent
Y predecessor parent
Z
parent
Z
20
Recursive rules
Define the “predecessor” relation
predecessor( X, Z):- parent( X, Z).
predecessor( X, Z):-
parent( X, Y), predecessor( Y, Z).
X
(2) Y is a predecessor of Z.
predecessor
:
?- predecessor( pam, X).
21
Recursive rules
mother(
% FigureX,1.8
Y) The
:- family program.
parent( X, Y),
parent(
female(pam,
X). bob).
parent( tom, bob).
parent( tom, liz).
grandparent( X, Z) :-
parent(
parent(bob,
X, Y),
ann).
parent(
parent(bob,
Y, Z).
pat).
parent( pat, jim).
sister( X, Y) :-
female(
parent(pam).
Z, X),
female(
parent(liz).
Z, Y),
female(
female(ann).
X),
female(
X \= Y.pat).
male( tom).
male( bob). X, Z) :- % Rule pr1
predecessor(
male(
parent(
jim).
X, Z).
predecessor(
offspring( Y, X)
X, :-
Z) :- % Rule pr2
parent( X, Y),
Y).
predecessor( Y, Z).
22
Recursive rules
Procedure:
In figure 1.8, there are two “predecessor relation”
clauses.
predecessor( X, Z) :- parent( X, Z).
predecessor( X, Z) :- parent( X, Y), predecessor( Y, Z).
Such a set of clauses is called a procedure.
Comments:
/* This is a comment */
% This is also a comment
23
Trace and Notrace
| ?- trace.
The debugger will first creep -- showing everything
(trace) …
(15 ms) yes X = bob
{trace} Z = jim
1 1 Redo: predecessor(bob,jim) ?
| ?- predecessor( X, Z). 3 2 Redo: predecessor(pat,jim) ?
1 1 Call: predecessor(_16,_17) ? 4 3 Call: parent(pat,_144) ?
2 2 Call: parent(_16,_17) ? 4 3 Exit: parent(pat,jim) ?
2 2 Exit: parent(pam,bob) ? …
1 1 Exit: predecessor(pam,bob) ? 4 3 Fail: parent(jim,_17) ?
4 3 Call: parent(jim,_144) ?
X = pam 4 3 Fail: parent(jim,_132) ?
Z = bob ? ; 3 2 Fail: predecessor(jim,_17) ?
1 1 Redo: predecessor(pam,bob) ? 1 1 Fail: predecessor(_16,_17) ?
2 2 Redo: parent(pam,bob) ?
2 2 Exit: parent(tom,bob) ? (266 ms) no
1 1 Exit: predecessor(tom,bob) ? {trace}
X = tom | ?- notrace.
Z = bob ? ; The debugger is switched off
yes
24
How Prolog answers questions
To answer a question, Prolog tries to satisfy all the goals.
To satisfy a goal means to demonstrate that the goal is true,
assuming that the relations in the program is true.
Prolog accepts facts and rules as a set of axioms, and the
user’s question as a proved theorem.
Example:
Axioms: All men are fallible.
john is a man.
Theorem: john is fallible.
For all X, if X is a man then X is fallible.
fallible( X) :- man( X).
man( john).
?- fallible( john).
25
How Prolog answers questions
predecessor( X, Z) :- parent( X, Z). % Rule pr1
predecessor( X, Z) :- parent( X, Y), % Rule pr2
predecessor( Y, Z).
parent( pam, bob).
parent( tom, bob).
?- predecessor( tom, pat). parent( tom, liz).
How does the Prolog system actually parent( bob, ann).
parent( bob, pat).
find a proof sequence? parent( pat, jim).
Prolog first tries that clause which appears first in the
bob liz
parent( tom, pat).
There is no clause in the program whose head matches
goals:
pam tom parent( tom, Y)
predecessor( Y, pat)
bob liz The first goal matches one of the facts. (Y = bob)
parent( tom, Y)
parent( tom, pat)
predecessor( Y, pat)
no By fact
Y = bob parent( tom, bob)
The top goal is
satisfied when a path predecessor( bob, pat)
is found from the root
node to a leaf node By rule pr1
parent( pam, bob). labeled ‘yes’.
parent( tom, bob). The execution of
parent( tom, liz). parent( bob, pat)
parent( bob, ann). Prolog is the searching
parent( bob, pat). for such path.
parent( pat, jim). yes
predecessor( X, Z) :- parent( X, Z). % Rule pr1
predecessor( X, Z) :- parent( X, Y), % Rule pr2
predecessor( Y, Z).
derivation diagrams
28
predecessor( X, Z) :- parent( X, Z). % Rule pr1
no By fact
Y = bob
parent( tom, bob)
| ?- predecessor( tom, pat).
1 1 Call: predecessor(tom,pat) ? predecessor( bob, pat)
2 2 Call: parent(tom,pat) ?
2 2 Fail: parent(tom,pat) ?
2 2 Call: parent(tom,_79) ? By rule pr1
2 2 Exit: parent(tom,bob) ?
3 2 Call: predecessor(bob,pat) ? parent( bob, pat)
4 3 Call: parent(bob,pat) ?
4 3 Exit: parent(bob,pat) ?
yes
3 2 Exit: predecessor(bob,pat) ?
1 1 Exit: predecessor(tom,pat) ?
derivation diagrams
true ?
29
Goal Searching in PROLOG
• Example Rules:
31