0% found this document useful (0 votes)
24 views87 pages

DI PPL Prolog Intro

- Prolog is a declarative programming language where the programmer specifies goals and relationships between objects rather than how to solve problems procedurally. - Prolog programs define relationships using facts, which state explicit relationships, and rules, which define implicit relationships. - Key concepts include predicates to represent relationships, variables to represent unknown objects, and defining new relations using existing relations in rules.

Uploaded by

rhprathish2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views87 pages

DI PPL Prolog Intro

- Prolog is a declarative programming language where the programmer specifies goals and relationships between objects rather than how to solve problems procedurally. - Prolog programs define relationships using facts, which state explicit relationships, and rules, which define implicit relationships. - Key concepts include predicates to represent relationships, variables to represent unknown objects, and defining new relations using existing relations in rules.

Uploaded by

rhprathish2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 87

PROLOG- Introduction

 Invented early seventies by Alain Colmerauer


in France and Robert Kowalski in Britain
 Prolog = Programming in Logic
 Prolog is a declarative programming
language unlike most common
programming languages.  In a
declarative language
 the programmer specifies a goal to be achieved
 the Prolog system works out how to achieve it
DI-AI Lab
4 20-Aug-20

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

 Declare facts describing explicit relationships


between objects and properties objects might have
 (e.g. Mary likes pizza, grass has_colour green, Fido
is_a_dog, Mizuki taught Paul Japanese )
 Define rules defining implicit relationships between
objects  (e.g. the sister rule above) and/or rules defining
implicit object properties (e.g. X is a parent if there is a Y
such that Y is a child of X).
One then uses the system by:
 asking questions above relationships between
objects, and/or about object properties (e.g. does
Mary like pizza? is Joe a parent?)

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

Defining relations by facts


 Given a whole family tree  The tree defined by the Prolog
program:
parent( pam,
bob). % Pam is
pam bob liz a parent of Bob
tom
parent( tom, tom, liz). ann).
bob). parent( parent( bob,
parent( bob,
ann pat jim
pat). parent(
pat, jim).

DI-AI Lab
10 20-Aug-20
DI-AI Lab
11 20-Aug-20

Defining relations by facts


 ?- parent( liz, tom

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).

 Who are Bob‟s

DI-AI Lab
12 20-Aug-20

Defining relations by facts


whom? pam
tom
 Find X and Y such
 Questions:
that X is a parent of
 Who is a parent of
Y.  ?- parent( X, Y). bob liz

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

Defining relations by facts


grandchildren?  ?- tom
parent( tom, X),
 Questions: parent( X, Y). bob liz
 Who are Tom‟s pam
parent( X, pat).
ann pat jim

 Do Ann and Pat have a


common parent?  ?-
parent( X, ann),

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

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).
X
 ?- offspring( X, Y).
parent offspring

Y
DI-AI Lab
16 20-Aug-20

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

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
DI-AI Lab
20 20-Aug-20

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

DI-AI Lab
21 20-Aug-20

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 someY such that X is a parent of Y.
DI-AI Lab
22 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

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 conjectured ()
theorem().
 Example:
 Axioms: All men are fallible ().
Socrates is a man.
 Theorem: Socrates is fallible.
 For all X, if X is a man then X is fallible.
fallible( X) :- man( X).
man( socrates).
 ?- fallible( socrates).
DI-AI Lab
27 20-Aug-20
How Prolog answers questions
predecessor( X, Z) :- parent( X, Z). % Rule pr1
predecessor( X, Z) :- parent( X, Y), % Rule pr2
predecessor( Y, Z). actually find a proof sequence?
parent( pam, bob). parent( tom, bob).
parent( tom, liz). parent( bob, ann).
 ?- predecessor( tom, pat). parent( bob, pat). parent( pat, jim).
 How does the Prolog system
 Prolog first tries that  The goal
clause which appears predecessor( tom, pat)
pam
first in the program. (rule is then replace by
tom pr1) parent( tom, pat).
 Now, X= tom, Z = pat.
bob liz

ann pat jim parent( tom, pat).


 Prolog backtracks to the original
goal in order to try an alternative
 There is no clause in the program
way (rule pr2).
whose head matches the goal
DI-AI Lab
28 20-Aug-20

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). yet.
parent( tom, liz). parent( bob, ann).
 Apply rule pr2, X = tom, Z = parent( bob, pat). parent( pat, jim).
pat, but Y is not instantiated
 The top goal  predecessor( Y, pat)

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

How Prolog answers questions

predecessor( tom, pat)

By rule pr1
By rule pr2

parent( tom, Y)

predecessor( Y, pat) parent( tom, pat)


parent( bob, ann). parent(
 The execution of Prolog is
bob, pat). parent( pat, jim).
the searching for such path.
no By fact

parent( tom, bob) Y = bob


 The top goal is satisfied
when a path is found from the predecessor( bob, pat)
root node to a leaf node
parent( pam, bob). parent( labeled „yes‟.
tom, bob). parent( tom, liz).
parent( bob, pat)

yes
By rule pr1

predecessor( X, Z) :- parent( X, Z). % Rule pr1 derivation diagrams


predecessor( X, Z) :- parent( X, Y), % Rule pr2
DI-AI Lab
30 20-Aug-20 predecessor( Y, Z).

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

parent( tom, bob) Y = bob


derivation diagrams

DI-AI Lab
31 20-Aug-20
DI-AI Lab
32 20-Aug-20

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.

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).

There are five clauses in this knowledge base: two


facts, and three rules.

The end of a clause is marked with a full stop.

20-Aug-20
DI-AI Lab 58

Predicates
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).

There are three predicates


in this knowledge base:
happy, listens2music, and playsAirGuitar

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).

The comma “," expresses conjunction in Prolog

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

Prolog and Logic


• Clearly Prolog has something to do
with logic • Operators
– Implication :-
– Conjunction ,
– Disjunction ;
• Use of modus ponens
• Negation

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).

jealous(X,Y):- loves(X,Z), loves(Y,Z).


20-Aug-20
DI-AI Lab 77

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(X,Y):- loves(X,Z), loves(Y,Z).

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

Simple Terms Complex Terms Simple Terms Complex 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

• An arbitrary sequence of characters


enclosed in single quotes
• Examples: 'Vincent', 'Five dollar shake',
'@$%' • A sequence of special characters
• Examples: : , ; . :-

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:

X, Y, Variable, Vincent, _tag

20-Aug-20
DI-AI Lab 83

You might also like