Introduction To Prolog PDF

You might also like

You are on page 1of 21

TME 2073 Intelligent

Systems

Introduction to Prolog
Programming:
A do-it yourself (DIY) Prolog

1
Objectives
introduces the basic concepts of Prolog
programming,
 gets you started on programming yourself, and
 shows you how to do some natural language
processing using Prolog.

2
What is Prolog?

• Prolog (programming in logic) is a logic-based programming language:


programs correspond to sets of logical formulas and the Prolog
interpreter uses logical methods to resolve queries.

• Prolog is a declarative language: you specify what problem you want to


solve rather than how to solve it.

• Prolog is very useful in some problem areas, such as artificial


intelligence, natural language processing, databases, . . . , but pretty
useless in others, such as for instance graphics or numerical algorithms.

• The objective of this lecture is to introduce you to the most basic


concepts of the Prolog programming language.

3
Introduction
A prolog Program consists of:

Declaring some facts about objects and


relationships
Declaring some rules about objects and
relationships
Declaring question about objects and
relationships

4
Facts
John likes Mary
Prolog: likes(john, mary).

The followings are important:


 The name of all relationship and objects must begin
with a small lowercase letter
 The relationship is written first and the objects are
separated by coma and enclosed with a pair of
brackets.
 A period “.” must end a fact.
5
Facts
valuable(gold). - gold is valuable
female(natasha). - natasha is female
father(john, mary) - john is the father of mary.

Write the following in facts in prolog:

1. Harry is a wizard.
2. Ron is a wizard.
3. Hermione is a wizard.
4. Uncle vernon is a muggle
5. Aunt petunia is a muggle.
6. Crookshank chases scabbars.

6
Facts
wizard(harry).
wizard(ron).
wizard(hermione).
muggle(uncle_vernon).
muggle(aunt_petunia).
chases(crookshanks, scabbars).

7
Questions/query
? – own(mary, book).

• Prolog system looks for facts from the database


that match the fact in question.

• Two facts match if their predicates are the same


and if their corresponding arguments each are
the same.

8
?- wizard(harry).
yes
?- chases(crookshanks,scabbars).
yes
?- muggle(harry).
no
?- muggle(dumbledore).
no
?- wizard(dumbledore).
no
?- witch(hermione).
ERROR: Undefined procedure: witch/1

9
Atoms and Variables
• All terms that consist of letters, numbers, and the underscore and
start with a non-capital letter are atoms: harry, uncle vernon, ritaSkeeter, nimbus2000, .
...

• All terms that are enclosed in single quotes are atoms:


’Professor Dumbledore’, ’(@ *+ ’, . . . .
• Certain special symbols are also atoms: +, ,, . . . .

• Variables are names that stands for some objects to be determined by Prolog. A
variable can be either instantiated or not instantiated.
• All terms that consist of letters, numbers, and the underscore and start with a capital
letter or an underscore are variables: X, Hermione, ron, . . . .

• For example:

?- likes(john, X).
?- likes(mary, X).

10
Conjunctions
• Conjunction and use of variables can be
combined to form a complicated query.

• Example:
• Is there anything that John and Mary both like?
This question consists of two goals:
• First, find out if there is some X that Mary like
• Then, find out if John likes whatever X is

11
Backtracking
• ?- likes(mary, X), likes(john, X).

likes(mary, pizza).
likes(mary, swimming).
likes(john, swimming).
likes(john, mary).

1. 1st goal succeeds, X = pizza


2. Next, attempt to satisfy the second goal
3. The second goal fails
4. Next, backtrack: forget the previous value of X, and
attempt to re-satisfy the goal.
12
Backtracking
• ?- likes(mary, X), likes(john, X).

likes(mary, pizza).
likes(mary, swimming).
likes(john, swimming).
likes(john, mary).

5. The first goal succeeds again, X = swimming


6. Next attempt to satisfy the second goal
7. The second goal succeds
8. Prolog notifies you success, and waits for a reply

13
Rules
eating(dudley).
happy(aunt petunia) :- happy(dudley).
happy(uncle vernon) :-happy(dudley) , unhappy(harry).
happy(dudley) :- kicking(dudley,harry).
happy(dudley) :- eating(dudley).

if ... then ...: If happy(dudley) is true, then happy(aunt


petunia) is true.

and: If happy(dudley) is true and unhappy(harry) is true,


then happy(uncle vernon) is true.
14
?- happy(dudley).
yes

?- happy(aunt_petunia).
yes

?- happy(uncle_vernon).
yes

?- happy(X).
X = aunt petunia ;
X = dudley ;
no

15
Rules
• Rules are of the form Head :- Body.
• Like facts and queries, they have to be followed
by a full stop.

happy(aunt petunia) :- happy(dudley).


happy(uncle vernon) :- happy(dudley),
unhappy(harry).

16
Rules
sisterof(X,Y) :-
female(X).
parents(X, M, F).
parents(Y, M,F).

X is a female
X has mother M and Father F
Y has the same mother and father as X does
17
male(carlisle).
male(edward).
female(alice).
female(esme).
parents(edward, esme, carlisle).
parents(alice, esme, carlisle).

sisterof(X,Y) :-
female(X).
parents(X, M, F).
parents(Y, M, F).

?- sisterof(alice, edward)

18
How does a Prolog proceed?
1. X becomes instantiated to alice
Y becomes instantiated to edward

2. Prolog tries first


female(alice)
This goal is satisfied from a database of facts

3. Prolog searches for parents (alice, M, F) where M and F will match


against any arguments because they are uninstantiated. A matching
fact is parent (alice, esme, carlisle) so the goal succeeds. Prolog marks
the goal place in the database and seconds that:

M become instantiated to esme and


F become instantiated to carlisle

19
How does a Prolog proceed?
4. Now prolog searches for
parents(edward, esme, carlisle) because Y is
instantiated to edward from the question and M
and F are instantiated to esme and carlisle for
the previous goal. Since it is the last goal in the
conjunction, the entire goal succeeds and the
fact sisterof(alice, edward) is established as
true.

20
Summary
• Asserting facts about objects
• Asking questions about the facts
• Using variables and what their scopes are
• Conjunctions as a way of saying and representing
relationships in the form of rules
• An introduction of backtracking

21

You might also like