Artificial Intelligence 3451: UNIT: 07 Prolog Programming
Artificial Intelligence 3451: UNIT: 07 Prolog Programming
UNIT: 07
PROLOG PROGRAMMING
Lecture: 10
Suitable for problems that involve structured objects and relations between
them.
Examples:
1. Red sphere is behind green box.
2. If object X is closer to the observer than object Y and object Y is closer
than object Z than X is closer than Z.
2
SWI-Prolog
SWI-Prolog is a good, standard Prolog for Windows and
Linux
It's licensed under GPL, therefore free
Downloadable from: http://www.swi-prolog.org/
https://www.swi-prolog.org/download/stable
3
Syllogisms
“Prolog” is all about programming in logic.
Aristotle described syllogisms 2300 years ago
Sample syllogism:
Socrates is a man.
All men are mortal.
Therefore, Socrates is mortal.
This is logic. Can Prolog do it?
4
Forward and backward reasoning
A syllogism gives two premises, then asks, "What can we
conclude?"
This is forward reasoning -- from premises to conclusions
it's inefficient when you have lots of premises
Instead, you ask Prolog specific questions
Prolog uses backward reasoning -- from (potential)
conclusions to facts
5
Syllogisms in Prolog
Syllogism Prolog
Socrates is a man.
All men are mortal. man(socrates).
Is Socrates mortal?
mortal(X) :- man(X).
?- mortal(socrates).
7
Facts English meanings
food(burger). // burger is a food
food(sandwich). // sandwich is a food
food(pizza). // pizza is a food
lunch(sandwich). // sandwich is a lunch
dinner(pizza). // pizza is a dinner
Rules
Queries / Goals
?- food(pizza). // Is pizza a food?
Leyla Ali
Omar Nour
Meriam khaled
Zahra
9
INTRODUCTION TO PROLOG
This program consists of 6 Clauses. Each clause declares one fact about the
relation parent.
10
INTRODUCTION TO PROLOG
11
INTRODUCTION TO PROLOG
12
INTRODUCTION TO PROLOG
13
INTRODUCTION TO PROLOG
Note: the logical meaning remains the same if we change the order of the two
requirements.
Answer: X= omar Y= ali; X= omar Y=leyla
Question: Who are ali grandchildren?
In prolog ?- parent(ali,X) parent (X,Y).
Answer: X= omar y=khaled; X=omar Y=meriam;
14
INTRODUCTION TO PROLOG
Answer no
15