You are on page 1of 40

Part 1 The Prolog Language

Unit 2
Introduction to Prolog
1
Introduction

 PROgramming in LOGic
 Emphasis on what rather than how

Problem in Declarative Form

Logic Machine

Basic Machine
Facts

 John is father of Mary


 father(john,mary).
 Names of relationship and objects
must begin with a lower-case letter.
 Relationship is written first (typically
the predicate of the sentence).
 Objects are written separated by
commas and are enclosed by a pair
of round brackets.
 The full stop character ‘.’ must
come at the end of a fact.
More facts

Predicate Interpretation

valuable(gold) Gold is valuable.

owns(john,gold) John owns gold.

father(john,mary) John is the father of


Mary
gives (john,book,mary) John gives the book to
Mary
5
Defining relations by facts

 Given a whole family  The tree defined by


tree the Prolog program:
pam tom
parent( pam, bob).
% Pam is a parent of
Bob
bob liz
parent( tom, bob).
parent( tom, liz).
ann pat parent( bob, ann).
parent( bob, pat).
parent( pat, jim).
jim
Questions

 Questions based on facts


 Answered by matching
Two facts match if their predicates are
same (spelt the same way) and the
arguments each are same.

 If matched, prolog answers yes, else


no.
 No does not mean falsity.
Variables

 Always begin with a capital letter


 ?- likes (john,X).
 ?- likes (john, Something).
 But not
 ?- likes (john,something)
8
Defining relations by facts

 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

 Who are Bob’s children?


 ?- parent( bob, X).
9
1.1 Defining relations by
facts tom
pam
 Questions:
 Who is a parent of whom? bob liz
 Find X and Y such that X is a parent of
Y.
 ?- parent( X, Y). ann pat

 Who is a grandparent of jim


Jim? X
 ?- parent( Y, jim),
parent
parent( X, Y).
Y grandparent

parent
jim
10
1.1 Defining relations by
facts
 Questions: pam tom

 Who are Tom’s grandchildren?


 ?- parent( tom, X), bob liz

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

 Use ‘,’ and pronounce it as and.


 Example
 Facts:
 likes(mary,food).
 likes(mary,tea).
 likes(john,tea).
 likes(john,mary)
 ?-
 likes(mary,X),likes(john,X).
 Meaning is anything liked by Mary also liked by
John?
Rules

 Statements about objects and their


relationships
 Expess
 If-then conditions
 I use an umbrella if there is a rain
 use(i, umbrella) :- occur(rain).
 Generalizations
 All men are mortal
 mortal(X) :- man(X).
 Definitions
 An animal is a bird if it has feathers
 bird(X) :- animal(X), has_feather(X).
Syntax

 <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

?- likes(mary, pizza) @< likes(mary, plums).


true.

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 (,)

 Prolog clauses are of three types:


 Facts:
 declare things that are always true
 facts are clauses that have a head and the empty
body
 Rules:
 declare things that are true depending on a given
condition
 rules have the head and the (non-empty) body
 Questions:
 the user can ask the program what things are true
 questions only have the body
28
1.2 Defining relations by
rules
 A variable can be substituted by another object.

 Variables are assumed to be universally quantified


and are read as “for all”.
 For example:
hasachild( X) :- parent( X, Y).
can be read in two way
(a) For all X and Y,
if X is a parent of Y then X has a child.
(b) For all X,
X has a child if there is some Y such that X
is a parent of Y.
29
1.3 Recursive rules
 Define the “predecessor()” relation
30
1.3 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
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

 Two terms match, if they are equal or if they contain


variables that can be instantiated in such a way that
the resulting terms are equal.
That means that the terms mia and mia match, because
they are the same atom. Similarly, the terms 42 and
42 match, because they are the same number, the
terms X and X match, because they are the same
variable, and the terms woman(mia) and woman(mia)
match, because they are the same complex term. The
terms woman(mia) and woman(vincent), however, do
not match, as they are not the same (and neither of
them contains a variable that could be instantiated to
make them the same).
38 Backtracking
 Backtracking is a process. When a subgoal fails, the Prolog
system traces its steps backwards to the previous goal and tries
to resatisfy it.
 This means that all variables that were bound to a value when
that goal was satisfied are now made free again. Then the Prolog
system tries to resatisfy that goal by matching with the next
clause starting at the clause just after the one that matched the
subgoal.
 This continues until either the subgoal is satisfied, or until the
program database has been exhausted, at which point Prolog
backtracks yet again to the subgoal that was before the current
subgoal.
39 Lists
 In Prolog list elements are enclosed by brackets and separated by
commas.
 [1,2,3,4],[[mary,joe],[bob,carol,ted,alice]] []
 Another way to represent a list is to use the head/tail notation [H|
T]. Here the head of the list, H, is separated from the tail of the list,
T, by a vertical bar. The tail of a list is the original list with its first
element removed. The tail of a list is always a list, even if it's the
empty list.
In Prolog, the H|T notation is used together with unification to
combine and break up lists. For example, suppose we have the
following list:
[bob,carol,ted,alice]
40 Lists Example
 Here's the various matches we would obtain using H|T:
 [X|Y] matches with X=bob Y=[carol,ted,alice]
 [X,Y|Z] matches with X=bob, Y=carol, Z=[ted,alice]
 [X,Y,Z|W] matches with X=bob, Y=carol, Z=ted W=[alice]
 [X,Y,Z,W|V] matches with X=bob, Y=carol, Z=ted,
W=alice and V=[]
 [X,Y,Z,Y] won't match because Y=carol and carol != alice
 [X,Y,Z,W,V|U] won't match because the list does not contain
5 elements
 We can also build lists using unification and H|T notation.
Suppose L unifies with [X|Y] and X=bob and
Y=[carol,ted,alice]. Then L=[bob,carol,ted,alice].

You might also like