Lecture-1 (Introduction To Prolog)
Lecture-1 (Introduction To Prolog)
Prolog
(Programming In Logic)
2
Artificial Intelligence
Introduction to Prolog
Theory
Introduction to Prolog
Facts, Rules and Queries
Prolog Syntax
implementation of DEC10
compiler by Warren
Can we deduce from these rules and the fact if event C happens?
1. happens(A) → happens(B),and happens(A) = TRUE. So, happens(B) = TRUE.
2. happens(B) → happens(C),and happens(B) = TRUE. So, happens(C) = TRUE.
int main()
{
int A_happens, B_happens, C_happens;
int A_happens = 1;
if (A_happens == 1)
B_happens = 1;
else
B_happens = 0;
if (B_happens == 0)
C_happens = 1;
else
C_happens = 0;
printf("%d", C_happens);
return 0;
}
happens(b) :- happens(a).
happens(c) :- happens(b).
happens(a).
1. Facts
2. Queries
3. Rules
.
Fact terminates with a period ( ).
0-9
Integers: 12, -34, 22342
Floats: 34573.3234
Examples:
father(abraham, isaac).
father(abraham, X). Here substitution is {X=isaac}.
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.
?- woman(mia).
true
?- playsAirGuitar(jody).
?- woman(mia).
true
?- playsAirGuitar(jody).
true
?-
?- woman(mia).
true
?- playsAirGuitar(jody).
true
?- playsAirGuitar(mia).
false
Jisan Mahmud 4/29/2016
36
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.
?- tattoed(jody).
?- tattoed(jody).
ERROR: predicate tattoed/1 not defined.
?-
?- party.
?- party.
yes
?-
?- rockConcert.
?- rockConcert.
no
?-
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):-
listens2music(yolanda).
head body
?-
?- playsAirGuitar(mia).
true
?-
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
?- playsAirGuitar(mia).
true.
?- playsAirGuitar(yolanda).
true.
?-
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
?- playsAirGuitar(mia).
true.
?- playsAirGuitar(yolanda).
true.
?- playsAirGuitar(amanda).
false.
?-
Jisan Mahmud 4/29/2016
53
Clauses
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
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).
playsAirGuitar(butch):- listens2music(butch).
?- playsAirGuitar(vincent).
false
?-
?- playsAirGuitar(butch).
true
?-
happy(vincent).
listens2music(butch).
playsAirGuitar(vincent):- listens2music(vincent), happy(vincent).
playsAirGuitar(butch):- happy(butch); listens2music(butch).
woman(mia).
woman(jody).
woman(yolanda).
loves(vincent, mia).
% Careful, this doesn’t necessarily mean that loves(mia, vincent) is a fact
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).
loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).
?- woman(X).
loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).
?- woman(X).
X=mia
loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).
?- woman(X).
X=mia;
loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).
?- woman(X).
X=mia;
X=jody
loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).
?- woman(X).
X=mia;
X=jody;
X=yolanda
loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).
?- woman(X).
X=mia;
X=jody;
X=yolanda;
loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).
?- loves(marsellus,X), woman(X).
loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).
?- loves(marsellus,X), woman(X).
X=mia
yes
?-
loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).
?- loves(pumpkin,X), woman(X).
loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).
?- loves(pumpkin,X), woman(X).
false
?-
loves(vincent,mia).
loves(marsellus,mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).
?- jealous(marsellus,W).
?- jealous(marsellus,W).
W=vincent
?-
?- jealous(vincent, vincent).
What will be the output of the query
?- jealous(vincent, vincent).
true.
?
But, in real life, does this really make sense? We should have written the rule
in a way, so that the result of these types of queries(where we query the
‘jealous’ relation with the same names) would return false. We should add
another term to the rule using conjunction, which checks that the first and
second arguments are not equal.
76
Corrected Knowledge Base 5
loves(vincent,mia).
loves(marsellus,mia).
loves(jack, mia).
loves(jack, kate).
jealous(X,Y):- loves(X,Z), loves(Y,Z), \+(X == Y).
% Alternatively,
% jealous(X, Y) :- loves(X, Z), loves(Y, Z), X \== Y.
Terms
Simple Terms
Complex Terms
Constants Variables
Atoms Numbers
Examples:
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
This knowledge base defines
happy/1
listens2music/1
playsAirGuitar/1
Example.
bonus(Number) :- Number is 2 + 3.
?- bonus(3).
No
?- bonus(5).
Yes
?- bonus(X).
X = 5
Example.
= and is have
| ?- X is 5 + 2. different meanings
X = 7
yes
= means term assignment
| ?- X = 5 + 2.
is means arithmetic assignment
X = 5+2
yes
| ?- X is 5.3 + 7.
X = 12.300000000000001
yes
Integers are coerced
| ?-
to float
|?-trace.
yes
{trace}
|?-north_of(madrid, tokyo).
1 1 Call: north_of(madrid,tokyo) ?
2 2 Exit: location(madrid,48,3) ?
1 1 Exit: north_of(madrid,tokyo) ?
Yes
{trace}
| ?-
|?-notrace.
The debugger is switched off
Yes
|?-
Thanks
?
Jisan Mahmud 4/29/2016