Unit 2 Introduction To Prolog: Part 1 The Prolog Language
Unit 2 Introduction To Prolog: Part 1 The Prolog Language
Unit 2
Introduction to Prolog
1
Introduction
PROgramming in LOGic
Emphasis on what rather than how
Logic Machine
Basic Machine
Facts
Predicate Interpretation
Questions:
pam tom
Is Bob a parent of Pat?
?- parent( bob, pat).
?- parent( liz, pat). bob liz
?- parent( tom, ben).
ann pat
Who is Liz’s parent?
?- parent( X, liz).
jim
parent
jim
10
1.1 Defining relations by
facts
Questions: pam tom
parent( X, Y).
ann pat
Do Ann and Pat have a
common parent?
jim
?- parent( X, ann),
parent( X, pat).
Example of usage of
variable
Facts:
likes(john,flowers).
likes(john,mary).
likes(paul,mary).
Question:
?- likes(john,X)
Answer:
X=flowers and wait
;
mary
;
no
Conjunctions
<head> :- <body>
Read ‘:-’ as ‘if’.
E.G.
likes(john,X) :- likes(X,cricket).
“John likes X if X likes cricket”.
i.e., “John likes anyone who likes cricket”.
Rules always end with ‘.’.
Prolog Arithmetic Operators
15
Prolog can be used to perform arithmetic.
Prolog supports the common arithmetic
operators:
•+ addition
•- subtraction
•* multiplication
•/ division
•% remainder
Arithmetic expressions can be used as the
right-hand argument of the is operator to
assign the result of the expression to a
variable.
16
?- X is 1+2*3.
X = 7
yes
?- X is 2*3+1.
X = 7
yes
?- X is (1+2)*3.
X = 9
yes
?- X is 2*(3+1).
X = 8
yes
?- X is -(4+6).
X = -10
yes
17 Logical operators
0 false
1 true
variable unknown truth value
universally quantified
atom
variable
~ Expr logical NOT
Expr + Expr logical OR
Expr * Expr logical AND
Expr # Expr exclusive OR
Var ^ Expr existential quantification
Expr =:= Expr equality
Expr =\= Expr disequality (same as #)
Expr =< Expr less or equal (implication)
Expr >= Expr greater or equal
Expr < Expr less than
Expr > Expr greater than
18
19
This succeeds because likes and mary are the same in both
terms, and pizza alphabetically precedes plums.
20
1.1 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
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.
21
1.2 Defining relations by
rules
Facts:
female( pam). % Pam is female
female( liz).
female( ann).
female( pat). tom
pam
male( tom). % Tom is male
male( bob).
male( jim). bob liz
ann pat
Define the “offspring()” relation:
Fact: offspring( liz, tom).
Rule: offspring( Y, X) :- parent( X, Y).
jim
For all X and Y,
Y is an offspring of X if
X is a parent of Y.
22
1.2 Defining relations by
rules
Rules have:
A condition part (body)
the right-hand side of the rule
A conclusion part (head)
the left-hand side of the rule
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).
?- offspring( liz, tom).
?- offspring( X, Y).
23
1.2 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.
24
1.2 Defining relations by
rules
Define the “grandparent” relation:
grandparent( X, Z) :-
parent( X, Y), parent( Y, Z).
25
1.2 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).
?- sister( X, pat).
?- sister( pat, pat).
Pat is a sister to herself?!
26
1.2 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)
27
1.2 Defining relations by
rules
Prolog clauses consist of
Head
Body: a list of goal separated by commas (,)
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
31
1.4 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).
parent( tom, liz).
?- predecessor( tom, pat). parent( bob, ann).
parent( bob, pat).
How does the Prolog system actually parent( pat, jim).
find a proof sequence?
Prolog first tries that clause which appears first in the
tom program. (rule pr1)
pam
Now, X= tom, Z = pat.
The goal predecessor( tom, pat) is then replace by
bob liz
parent( tom, pat).
There is no clause in the program whose head matches the
ann pat goal parent( tom, pat).
Prolog backtracks to the original goal in order to try an
alternative way (rule pr2).
jim
32
1.4 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).
Apply rule pr2, X = tom, Z = pat, parent( bob, ann).
parent( bob, pat).
but Y is not instantiated yet. parent( pat, jim).
The top goal predecessor( tom, pat) is replaces by two goals:
parent( tom, Y)
pam tom
predecessor( Y, pat)
The first goal matches one of the facts. (Y = bob)
bob liz
The remaining goal has become
predecessor( bob, pat)
ann pat
Using rule pr1, this goal can be satisfied.
predecessor( bob, pat) :- parent( bob, pat)
jim
Question Answering in
presence of rules
Facts
male (ram).
male (shyam).
female (sita).
female (gita).
parents (shyam, gita, ram).
parents (sita, gita, ram).
Question Answering: Y/N type: is sita the
sister of shyam?
?- sister_of (sita, shyam)
parents(sita,M,F) parents(shyam,M,F)
female(sita)
parents(shyam,gita,ram)
parents(sita,gita,ram)
success
Question Answering: wh-type: whose
sister is sita?
?- ?- sister_of (sita, X)
parents(sita,M,F) parents(Y,M,F)
female(sita)
parents(Y,gita,ram)
parents(sita,gita,ram)
parents(shyam,gita,ram)
Success
Y=shyam
36 1.5 Declarative and procedural
meaning of programs
Two levels of meaning of Prolog programs:
The declarative meaning
concerned only with the relations defined by the program
determines what will be the output of the program
The programmer should concentrate mainly on the
declarative meaning and avoid being distracted by the
executional details.
The procedural meaning
determines how this output is obtained
determines how the relations are actually evaluated by
the Prolog system
The procedural aspects cannot be completely ignored by
the programmer for practical reasons of executional
efficiency.
37 Matching