Chapter 7 - Programming For AI
Chapter 7 - Programming For AI
Artificial Intelligence
Chapter 7: Programming for AI
Prolog Basics
1
What is Logic Programming
• Two types of programming languages:
– Imperative languages or procedural languages
(C, C++, VB, C#, Java, …).
– Declarative languages (prolog, lisp, …).
• Examples:
• X, Y, Z, Parent, Child, X1, Y1, X2, X5, X6,
….
Predicates
• A predicate has the form
– p(t1,...,tn)
– where p is an atom and t1,...,tn are variables or
atoms.
– n is the number of arguments of predicate p (written
as p/n which represents the signature of predicate).
– Predicates can be of 0-arg, 1-arg, 2-arg, …
– It can be either true or false.
• Examples:
– father(ali, ahmed).
Prolog Knowledge Base
14
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.
?-
?- woman(mia).
?- woman(mia).
yes
?-
?- woman(mia).
yes
?- playsAirGuitar(jody).
?- woman(mia).
yes
?- playsAirGuitar(jody).
yes
?-
?- woman(mia).
yes
?- playsAirGuitar(jody).
yes
?- playsAirGuitar(mia).
no
© Patrick Blackburn, Johan Bos & Kristina
Striegnitz
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.
?- teacher(jody).
?- teacher(jody).
ERROR: predicate teacher/1 not defined.
?-
?- party.
?- party.
yes
?-
?- rockConcert.
head body
?-
?- playsAirGuitar(mia).
yes
?-
?- playsAirGuitar(mia).
yes
?- playsAirGuitar(yolanda).
yes
?- playsAirGuitar(vincent).
no
?-
?- playsAirGuitar(butch).
yes
?-
if
Examples
relation father-of
represented by father-of
Semantic Net
Is-a
ahmad
Is-a
male
Examples
salem
male(ali). father-of
male(ahmed). father-of
male(salem).
father(salem, ali). Is-a
ali
male(A), male(B).
male
• ?- father(X,ali).
Examples
• ?- father(X,ali).
X = salem
male(ali).
?; male(ahmed).
no (or false) male(salem).
? father(salem, ali).
father(salem, ahmed).
brother(A,B) :- father(P,A),
father(P,B), A\==B,
male(A), male(B).
Examples male(ali).
male(ahmed).
male(salem).
• ?- father(X,ali). father(salem, ali).
X = salem father(salem, ahmed).
?; brother(A,B) :- father(P,A),
no father(P,B), A\==B,
? male(A), male(B).
• ?- father(X,Y), brother(Y,Z).
Examples male(ali).
male(ahmed).
male(salem).
• ?- father(X,ali). father(salem, ali).
X = salem father(salem, ahmed).
?; brother(A,B) :- father(P,A),
no father(P,B), A\==B,
? male(A), male(B).
• ?- father(X,Y), brother(Y,Z).