You are on page 1of 15

Artificial Intelligence 3451

UNIT: 07
PROLOG PROGRAMMING
Lecture: 10

Farhad Muhammad Riaz


PROLOG = PROgramming in LOGic
 Three basic mechanisms among others:
1. Pattern Matching,
2. Tree based data structuring
3. Back-Tracking.

 Suitable for problems that involve structured objects and relations between
them.

 It allows symbolic computation.

 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

Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt

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?

Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt

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

Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt

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

6 CSC 8520 Spring 2013. Paula Matuszek


Facts, rules, and queries
 Fact: Socrates is a man.
 man(socrates).
 Rule: All men are mortal.
 mortal(X) :- man(X).
 Query: Is Socrates mortal?
 mortal(socrates).
 Queries have the same form as facts

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

// Every food is a meal OR


meal(X) :- food(X).
Anything is a meal if it is a food

Queries / Goals
?- food(pizza). // Is pizza a food?

?- meal(X), lunch(X). // Which food is meal and lunch? 

?- dinner(sandwich). // Is sandwich a dinner?

(1)   ?- meal(X), dinner(X).


(2)   ?- meal(What).
(3)   ?- meal(X), dinner(Y).
Prolog
 basic concepts by an Example:

Leyla Ali

Omar Nour

Meriam khaled

Zahra

Let’s consider the Parent relation

9
INTRODUCTION TO PROLOG

 DEFINING RELATIONS BY FACTS

 This relation can be defined by the following Prolog proram:


parent(leyla, omar).
parent(ali, omar).
parent(omar, meriam).
parent(omar, khaled).
parent(ali, nour).
parent(khaled, zahra).

 This program consists of 6 Clauses. Each clause declares one fact about the
relation parent.

 The clause parent(omar, meriam). Is an instance of the relation parent. A


relation is defined as a set of instances.

10
INTRODUCTION TO PROLOG

What can we do with this program?

Let’s ask the system questions about the relation parent:

 Question: is omar parent of khaled?


In prolog ?- parent(omar, khaled).
 Answer: yes

11
INTRODUCTION TO PROLOG

 Question: is leyla parent of meriam?


In prolog ?- parent(leyla, meriam).
 Answer: no

 Question: who is zahra parent?


In prolog ?- parent(X, zahra).
The system tells what is the value of X for which the
statement is true.
 Answer: X= khaled

12
INTRODUCTION TO PROLOG

 Question: who are omar children?


In prolog ?- parent(omar,X).
 Answer: X= meriam
X= khaled
no

 Question: Who is parent of whom?


In prolog ?- parent(X,Y).

 Answer: X= leyla Y=omar; X=ali Y=omar; X=omar Y=meriam; X=omar


Y= khaled; X=ali Y=nour; X=khaled y=zahra;

13
INTRODUCTION TO PROLOG

 Question: who is grandparent of khaled?


In prolog ?- parent(X, khaled) parent(Y, X).

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

 Question: Do meriam and nour have a common parent?


In prolog ?- parent(X, meriam) parent(X,nour).

First who is parent of meriam X ?


Second is (this same) X parent of nour ?

 Answer no

15

You might also like