DI PPL Prolog Intro
DI PPL Prolog Intro
PROLOG- Introduction(Contd..)
Traditional programming languages
are said to be procedural
Procedural programmer must specify in
detail how to solve a problem:
In purely declarative languages, the
programmer only states what the problem
is and leaves the rest to the language
system
http://www.swi-prolog.org/
DI-AI Lab
5 20-Aug-20
Relations
Prolog programs specify relationships among objects
and properties of objects.
When we say, "John owns the book", we are declaring
the ownership relationship between two objects: John
and the book.
When we ask, "Does John own the book?" we are
trying to find out about a relationship.
Relationships can also rules such as:
Two people are sisters
if they are both female and
they have the same parents.
A rule allows us to find out about a relationship even if
the relationship isn't explicitly stated as a fact.
DI-AI Lab
6 20-Aug-20
Programming in Prolog
DI-AI Lab
7 20-Aug-20
Facts
Properties of objects, or relationships between
objects; "Dr Turing lectures in course 9020", is
written in Prolog as: lectures(turing, 9020).
Notice that:
names of properties/relationships begin with lower
case letters. the relationship name appears as the
first term
objects appear as comma-separated arguments
within parentheses. A period "." must end a fact.
objects also begin with lower case letters. They also can begin
with digits (like 9020), and can be strings of characters enclosed
in quotes (as in reads(fred, "War and Peace")).
lectures(turing, 9020). is also called a predicate
DI-AI Lab
8 20-Aug-20
Variables
Suppose we want to ask, "What course does Turing
teach"? This could be written as: Is there a course, X,
that Turing teaches? The variable X stands for an
object that the questioner does not know about yet.
To answer the question, Prolog has to find out the
value of X, if it exists.
As long as we do not know the value of a
variable it is said to be unbound.
When a value is found, the variable is said to
bound to that value. The name of a variable must
begin with a capital letter or an underscore character,
"_".
DI-AI Lab
9 20-Aug-20
DI-AI Lab
10 20-Aug-20
DI-AI Lab
11 20-Aug-20
pat).
Questions: ?- parent( tom,
bob liz
Is Bob a parent ben).
of Pat? ?- parent(
bob, pat). pam
children? ?- parent(
bob, X).
Who is Liz‟s parent? ann pat jim
?- parent( X, liz).
DI-AI Lab
12 20-Aug-20
parent
ann pat
Who is a grandparent of
Jim?
?- parent( Y, jim), jim
parent( X, Y). X
Y grandparent
parent
DI-AI Lab
13 20-Aug-20 jim
DI-AI Lab
14 20-Aug-20
Defining relations by rules
is male male( bob).
Facts: male( jim).
female( pam). % bob liz
Pam is female
female( liz).
female( ann).
female( pat).
pam
male( tom). % Tom tom
parent( X, Y). For all X and Y,
Y is an offspring of X if
Define the “offspring()” X is a parent of Y.
ann pat jim
relation: Fact: offspring( liz,
tom).
Rule: offspring( Y, X) :-
DI-AI Lab
15 20-Aug-20
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).
X
?- offspring( X, Y).
parent offspring
Y
DI-AI Lab
16 20-Aug-20
female
X
mother
parent
DI-AI Lab
17 20-Aug-20
Defining relations by rules
Define the “grandparent”
relation: grandparent( X, Z)
:-
parent( X, Y), parent( Y, Z).
parent
Ygrandparent
parent
DI-AI Lab
18 20-Aug-20
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).
Z
?- sister( pat, pat). parent
parent
Pat is a sister to herself?!
X Y
female sister
DI-AI Lab
19 20-Aug-20
parent
parent
X Y
female sister
DI-AI Lab
20 20-Aug-20
DI-AI Lab
21 20-Aug-20
Recursive rules
Define the “predecessor()” relation
X
X
predecessor parent parent
parent parent
Y
Y1 predecessor
X
Y2
parent
Y predecessor Z
parentZ
DI-AI Lab
23 20-Aug-20
Recursive rules
parent( X, Y), (2) Y is a predecessor
predecessor( Y, Z). of Z.
Define the
“predecessor” For all X and Z, ?- predecessor( pam,
relation X is a predecessor of Z X).
predecessor( X, Z):- if
parent( X, Z). there is a Y such that
predecessor( X, Z):- (1) X is a parent of Y
and
:
X
Z
parent
Y1
predecessor
predecessor
DI-AI Lab
24 20-Aug-20
Recursive rules
% Figure 1.8 The family program. parent( bob, pat).
parent( pat, jim).
parent( pam, bob).
parent( tom, bob). female( pam).
parent( tom, liz). female( liz).
parent( bob, ann). female( ann).
female( pat). parent( Z, Y),
male( tom). female( X),
male( bob). X \=Y.
male( jim).
offspring( Y, X) :- parent( X, Y). predecessor( X, Z) :- % Rule pr1
mother( X, Y) :- parent( X, Y), parent( X, Z).
female( X).
predecessor( X, Z) :- % Rule pr2
grandparent( X, Z) :- parent( X, Y), parent( X, Y),
parent( Y, Z). predecessor( Y, Z).
sister( X, Y) :-
parent( Z, X),
DI-AI Lab
25 20-Aug-20
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
DI-AI Lab
26 20-Aug-20
pam
predecessor( tom, The first goal
tom pat) is replaces by two matches one of the
goals: parent( tom, facts. (Y = bob)
bob liz
Y)
ann pat jim become
predecessor( bob, pat)
The remaining goal has Using rule pr1, this goal
can be satisfied. parent( bob, pat)
predecessor( bob, pat) :-
DI-AI Lab
29 20-Aug-20
By rule pr1
By rule pr2
parent( tom, Y)
yes
By rule pr1
Trace
predecessor( tom, pat)
parent( pam, bob). parent( tom, bob).
parent( tom, liz).
parent( bob, ann). By rule pr1
By rule pr2 parent( tom, Y)
parent( bob, pat).
parent( pat, jim).
predecessor( X, Z) :- parent( X, Z). % Rule pr1 predecessor( Y, pat) parent( tom, pat)
predecessor( X, Z) :- parent( X, Y), % Rule pr2
predecessor( Y, Z). no
| ?- predecessor( tom, pat).
1 1 Call: predecessor(tom,pat) ?
2 2 Call: parent(tom,pat) ?
2 2 Fail: parent(tom,pat) ?
2 2 Call: parent(tom,_79) ?
2 2 Exit: parent(tom,bob) ? predecessor( bob, pat)
3 2 Call: predecessor(bob,pat) ?
4 3 Call: parent(bob,pat) ? By rule pr1
4 3 Exit: parent(bob,pat) ?
3 2 Exit: predecessor(bob,pat) ? parent( bob, pat)
1 1 Exit: predecessor(tom,pat) ?
yes
true ?
By fact
DI-AI Lab
31 20-Aug-20
DI-AI Lab
32 20-Aug-20
DI-AI Lab
33 20-Aug-20
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jo
dy). party.
20-Aug-20
DI-AI Lab 34
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jo
dy). party.
?-
20-Aug-20
DI-AI Lab 35
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jo
dy). party.
?- woman(mia).
20-Aug-20
DI-AI Lab 36
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jo
dy). party.
?-
woman(mia).
yes
?-
20-Aug-20
DI-AI Lab 37
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jo
dy). party.
?- woman(mia).
yes
?- playsAirGuitar(jody).
20-Aug-20
DI-AI Lab 38
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jo
dy). party.
?- woman(mia).
yes
?-
playsAirGuitar(jody).
yes
?-
20-Aug-20
DI-AI Lab 39
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jo
dy). party.
?- woman(mia).
yes
?-
playsAirGuitar(jody).
yes
?-
playsAirGuitar(mia).
no
20-Aug-20
DI-AI Lab 40
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jo
dy). party.
?- tattoed(jody).
20-Aug-20
DI-AI Lab 41
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jo
dy). party.
?-
tattoed(jody).
no
?-
20-Aug-20
DI-AI Lab 42
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.
?- tattoed(jody).
ERROR: predicate tattoed/1 not
defined. ?-
20-Aug-20
DI-AI Lab 43
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jo
dy). party.
?- party.
20-Aug-20
DI-AI Lab 44
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jo
dy). party.
?- party.
yes
?-
20-Aug-20
DI-AI Lab 45
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jo
dy). party.
?- rockConcert.
20-Aug-20
DI-AI Lab 46
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jo
dy). party.
?-
rockConcert.
no
?-
20-Aug-20
DI-AI Lab 47
Knowledge Base 2
happy(yolanda).
listens2music(mia).
listens2music(yolanda):-
happy(yolanda). playsAirGuitar(mia):-
listens2music(mia).
playsAirGuitar(yolanda):-
listens2music(yolanda).
20-Aug-20
DI-AI Lab 48
Knowledge Base 2
listens2musi
happy(yolan c(mia).
da). fact
listens2music(yolanda):-
happy(yolanda). playsAirGuitar(mia):-
listens2music(mia).
playsAirGuitar(yolanda):-
listens2music(yolanda).
20-Aug-20
DI-AI Lab 49
Knowledge Base 2
mia).
happy(yolanda fact
). fact
listens2music(
listens2music(yolanda):-
happy(yolanda). playsAirGuitar(mia):-
listens2music(mia).
playsAirGuitar(yolanda):-
listens2music(yolanda).
20-Aug-20
DI-AI Lab 50
Knowledge Base 2
mia).
happy(yolanda fact
). fact
listens2music(
playsAirGuitar(yolanda):-
listens2music(yolanda):-
listens2music(yolanda).
happy(yolanda).
rule
playsAirGuitar(mia):-
listens2music(mia).
20-Aug-20
DI-AI Lab 51
Knowledge Base 2
mia).
happy(yolanda fact
). fact
listens2music(
playsAirGuitar(yolanda):-
listens2music(yolanda):-
listens2music(yolanda).
happy(yolanda).
rule rule
playsAirGuitar(mia):-
listens2music(mia).
20-Aug-20
DI-AI Lab 52
Knowledge Base 2
mia).
happy(yolanda fact
). fact
listens2music(
listens2music(yolanda).
listens2music(yolanda):-
rule
happy(yolanda).
rule
playsAirGuitar(mia):-
rule
listens2music(mia).
playsAirGuitar(yolanda):-
20-Aug-20
DI-AI Lab 53
Knowledge Base 2
happy(yolanda).
listens2music(mia).
listens2music(yolanda):-
happy(yolanda). playsAirGuitar(mia):-
listens2music(mia).
playsAirGuitar(yolanda):-
listens2music(yolanda).
head body
20-Aug-20
DI-AI Lab 54
Knowledge Base 2
happy(yolanda).
listens2music(mia).
listens2music(yolanda):-
happy(yolanda). playsAirGuitar(mia):-
listens2music(mia).
playsAirGuitar(yolanda):-
listens2music(yolanda).
?-
20-Aug-20
DI-AI Lab 55
Knowledge Base 2
happy(yolanda).
listens2music(mia).
listens2music(yolanda):-
happy(yolanda). playsAirGuitar(mia):-
listens2music(mia).
playsAirGuitar(yolanda):-
listens2music(yolanda).
?- playsAirGuitar(mia).
yes
?-
20-Aug-20
DI-AI Lab 56
Knowledge Base 2
happy(yolanda).
listens2music(mia).
listens2music(yolanda):-
happy(yolanda). playsAirGuitar(mia):-
listens2music(mia).
playsAirGuitar(yolanda):-
listens2music(yolanda).
?- playsAirGuitar(mia).
yes
?- playsAirGuitar(yolanda).
yes
20-Aug-20
DI-AI Lab 57
Clauses
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
20-Aug-20
DI-AI Lab 58
Predicates
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
20-Aug-20
DI-AI Lab 59
Knowledge Base 3
happy(vincent).
listens2music(butch).
playsAirGuitar(vincent):- listens2music(vincent),
happy(vincent). playsAirGuitar(butch):- happy(butch).
playsAirGuitar(butch):- listens2music(butch).
20-Aug-20
DI-AI Lab 60
Expressing Conjunction
happy(vincent).
listens2music(butch).
playsAirGuitar(vincent):- listens2music(vincent),
happy(vincent). playsAirGuitar(butch):- happy(butch).
playsAirGuitar(butch):- listens2music(butch).
20-Aug-20
DI-AI Lab 61
Knowledge Base 3
happy(vincent).
listens2music(butch).
playsAirGuitar(vincent):- listens2music(vincent),
happy(vincent). playsAirGuitar(butch):- happy(butch).
playsAirGuitar(butch):- listens2music(butch).
?- playsAirGuitar(vincent).
no
?-
20-Aug-20
DI-AI Lab 62
Knowledge Base 3
happy(vincent).
listens2music(butch).
playsAirGuitar(vincent):- listens2music(vincent),
happy(vincent). playsAirGuitar(butch):- happy(butch).
playsAirGuitar(butch):- listens2music(butch).
?- playsAirGuitar(butch).
yes
?-
20-Aug-20
DI-AI Lab 63
Expressing Disjunction
happy(vincent).
listens2music(butch).
playsAirGuitar(vincent):- listens2music(vincent),
happy(vincent). playsAirGuitar(butch):-
happy(butch).
playsAirGuitar(butch):- listens2music(butch).
happy(vincent).
listens2music(butch).
playsAirGuitar(vincent):- listens2music(vincent),
happy(vincent). playsAirGuitar(butch):- happy(butch);
listens2music(butch).
20-Aug-20
DI-AI Lab 64
20-Aug-20
DI-AI Lab 65
Knowledge Base 4
woman(mia).
woman(jody).
woman(yolanda).
loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin,
honey_bunny).
loves(honey_bunny,
pumpkin).
20-Aug-20
DI-AI Lab 66
Prolog Variables
woman(mia).
woman(jody).
woman(yolanda).
loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin,
honey_bunny).
loves(honey_bunny,
pumpkin).
?- woman(X).
20-Aug-20
DI-AI Lab 67
Variable Instantiation
woman(mia).
woman(jody).
woman(yolanda).
loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin,
honey_bunny).
loves(honey_bunny,
pumpkin).
?- woman(X).
X=mia
20-Aug-20
DI-AI Lab 68
Asking Alternatives
woman(mia).
woman(jody).
woman(yolanda).
loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin,
honey_bunny).
loves(honey_bunny,
pumpkin).
?- woman(X).
X=mia;
20-Aug-20
DI-AI Lab 69
Asking Alternatives
woman(mia).
woman(jody).
woman(yolanda).
loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin,
honey_bunny).
loves(honey_bunny,
pumpkin).
?- woman(X).
X=mia;
X=jody
20-Aug-20
DI-AI Lab 70
Asking Alternatives
woman(mia).
woman(jody).
woman(yolanda).
loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin,
honey_bunny).
loves(honey_bunny,
pumpkin).
?- woman(X).
X=mia;
X=jody;
X=yolanda
20-Aug-20
DI-AI Lab 71
Asking Alternatives
woman(mia).
woman(jody).
woman(yolanda).
loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin,
honey_bunny).
loves(honey_bunny,
pumpkin).
?- woman(X).
X=mia;
X=jody;
X=yolanda;
no
20-Aug-20
DI-AI Lab 72
Knowledge Base 4
woman(mia).
woman(jody).
woman(yolanda).
loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).
?- loves(marsellus,X), woman(X).
20-Aug-20
DI-AI Lab 73
Knowledge Base 4
woman(mia).
woman(jody).
woman(yolanda).
loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin,
honey_bunny).
loves(honey_bunny,
pumpkin).
?- loves(marsellus,X),
woman(X). X=mia
yes
?-
20-Aug-20
DI-AI Lab 74
Knowledge Base 4
woman(mia).
woman(jody).
woman(yolanda).
loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin,
honey_bunny).
loves(honey_bunny,
pumpkin).
?- loves(pumpkin,X), woman(X).
20-Aug-20
DI-AI Lab 75
Knowledge Base 4
woman(mia).
woman(jody).
woman(yolanda).
loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin,
honey_bunny).
loves(honey_bunny,
pumpkin).
?- loves(pumpkin,X),
woman(X). no
?-
20-Aug-20
DI-AI Lab 76
Knowledge Base 5
loves(vincent,mia).
loves(marsellus,mia).
loves(pumpkin,
honey_bunny).
loves(honey_bunny,
pumpkin).
Knowledge Base 5
loves(vincent,mia).
loves(marsellus,mia).
loves(pumpkin,
honey_bunny).
loves(honey_bunny,
pumpkin).
jealous(X,Y):- loves(X,Z),
loves(Y,Z). ?-
jealous(marsellus,W).
20-Aug-20
DI-AI Lab 78
Knowledge Base 5
loves(vincent,mia).
loves(marsellus,mia).
loves(pumpkin,
honey_bunny).
loves(honey_bunny,
pumpkin).
?-
jealous(marsellus,W).
W=vincent
?-
20-Aug-20
DI-AI Lab 79
Prolog Syntax
• What exactly are facts, rules and queries built out of?
Terms
Terms
Constants Variables
Constants Variables
Atoms Numbers
Atoms Numbers
20-Aug-20
DI-AI Lab 80
Atoms
• A sequence of characters of upper-case letters,
lower-case letters, digits, or underscore,
starting with a lowercase letter
• Examples: butch, big_kahuna_burger, playGuitar
20-Aug-20
DI-AI Lab 81
Numbers
• Integers: 12, -34,
22342 • Floats:
34573.3234
20-Aug-20
DI-AI Lab 82
Variables
• A sequence of characters of upper-case letters,
lower-case letters, digits, or underscore,
starting with either an uppercase letter or an
underscore
• Examples:
20-Aug-20
DI-AI Lab 83