Upload
others
View
32
Download
0
Embed Size (px)
Citation preview
Introduction to Prolog
Dr. Hikmat Jaber
22/05/1434 1
Introduction
Prolog stands for Programming in logic.
Prolog consists of a series of rules and facts.
A Program is run by presenting some query
and seeing if this can be proved against
these rules and facts.
2
Simple Facts
Fact either consist of a particular item or a relation
between items.
Fact should always begin with a lowercase letter and
end with a full stop.
Fact can consist of letters, numbers and underscore.
Examples (Fact as a particular item):
(1) sunny. /* weather is sunny */
(2) saleh_is_cold. /* saleh is cold */
(3) raining. /* it is raining */
3
Simple Facts (cont.)
(4) ahmad_Forgot_His_Raincoat. /* ahmad forgot his raincoat */
(5) khalid_lost_50_Riyals. /* khalid has lost 50 Riyals */
(6) mohammad_footballer. /* mohammad is footballer */
Asking queries based on the above facts
?- is the prolog prompt
(1) ?- mohammad_is_cold.
no
(2) ?- ahmad_Forgot_His_Raincoat.
yes
4
Simple Facts (cont.)
(3) ?- raining.
yes
(4) ?- foggy.
no
Exercise 1: Which of the following are syntactically
correct facts.
(1) Hazlenuts
incorrect. Because it starts with a capital letter.
(2) khalidsRedCar
correct. Capital letters within the word are ok. 5
Simple Facts (cont.)
(3) 2Ideas
incorrect. Because it should not start with a number.
(4) Prolog
incorrect. Because it starts with a capital letter.
Exercise 2: Given the database below, study the
queries below it. Indicate whether you think the goal
will succeed or not by answering yes or no with
verification.
6
Simple Facts (cont.)
blue_box.
red_box.
green_circle.
blue_circle.
orange_triangle.
(1) ?- green_circle.
yes. Because it is a fact in the DB.
(2) ?- circle_green.
no. Because no such fact in the DB.
7
Simple Facts (cont.)
(3) ?- red_triangle.
no. Because no such fact in the DB.
(4) ?- red_box.
yes. Because it is a fact in the DB.
(5) ?- orange_Triangle.
no. Because no such fact in the DB
8
Facts with Arguments
Another type of facts is a relation. A general model is:
relation(<argument1>,<argument2>,....,<argumentN> ).
Ex: likes(ahmad, ali). /* ahmad likes ali */
Examples (Fact with Arguments):
It details who eats what in some world model:
eats(fares, oranges). /* fares eats oranges */
eats(fares, t_bone_steaks). /* fares eats T_bone_steaks */
eats(talal, apples). /* talal eats apples */
eats(jehad, apples). /* jehad eats apples */
eats(jehad, grapefruit). /* jehad eats grapefruit */ 9
Facts with Arguments (cont.)
The following are the queries and their answers:
?- eats(fares, oranges). /* does this match anything in the DB */
yes
?- eats(jehad, apples). /* do we have a fact that says jehad eats
yes apples */
?- eats(majed, apples). /* does majed eat apples */
no
?- eats(fares, apples). /* does fares eat apples */
no
10
Facts with Arguments (cont.)
Exercise (Fact with Arguments):
Consider the following DB that use age predicates:
age(jamal, 32). /* jamal is 32 years old */
age(ali, 41). /* ali is 41 */
age(ghaleb, 72). /* ghaleb is 72 */
age(imam, 2). /* imam is 2 */
age(tareq, 25). /* tareq is 25 */
What are the answers of the following queries:
11
Facts with Arguments (cont.)
?- age(imam, 2). /* is imam 2 years old? */
yes
?- ali(41). /* for some relation ali is it 41? */
no
?- age(imam, two). /* is imam two years old? */
no
?- age(Tareq, 25). /* is Tareq 25 years old? */
no
12
Variables and Unification
Explanation:
Suppose we have the following fact in our database:
eats(fares, mangoes).
How do we ask what fares eats ?
i.e. what does match with fares?
In this case we must use a variable.
query: ?- eats(fares, What).
answer: What = mangoes
yes
13
Variables and Unification (cont.)
Variable starts with a capital letter.
Examples:
X
VaRiAble
My_name
The process of matching or binding items with variables is
known as unification.
Example 1:
consider the following database:
loves(jehad, imam).
loves(fares, hobbies). 14
Variables and Unification (cont.)
Some queries using variable:
(1) ?- loves(jehad, Who) /* who does jehad love */
Who = imam
yes
(2) ?- loves(ali, Who) /* does ali love anybody */
no
(3) ?- loves(fares, Who) /* who does fares love */
Who = hobbies
yes
15
Variables and Unification (cont.)
Example 2:
Consider the following database showing of cassette
tapes. Notice the arguments to tape are:
tape(Album No., Artist, Album, Favorite Song)
tape(1, talal_maddah, destinies, maqadeer).
tape(2, mohammad_Abdu, wahdak, wahdak).
tape(3, mohammad_Abdu, wahdak, albariha).
tape(4, abdulmajeed, ghayeb_habebi, aaazElnas).
tape(5, rashed_majed, salamat, mertah).
16
Variables and Unification (cont.)
Queries:
(1) ?- tape(5, Artist, Album, Fav_Song).
/* what are the contents of tape 5 */
Artist = rashed_majed
Album = salamat
Fav_Song = mertah
(2) ?- tape(1, talal_maddah, destinies, Song). /* find just song */
Song = maqadeer
17
Variables and Unification (cont.)
Exercise:
Indicate the outcome of the proposed match using yes or
no. If you think the unification succeeds, write down any
binding made e.g. if (say) X gets bound to foo, then write
X=foo as the substitution:
• eats(fares, tomatoes).
• eats(Whom, What)
Do these unify (match)?
yes
Whom = fares and What = tomatoes
18
Variables and Unification (cont.)
• eats(fares, Food).
• eats(Person, jehad).
Do these unify (match)?
yes
Person = fares and Food = jehad
• cd(29, destinies, maqadeer).
• cd(A, B, help).
Do these unify (match)?
no
maqadeer and help do not unify
19
Variables and Unification (cont.)
• f(X, a).
• f(a, X).
Do these unify (match)?
yes
X = a
• likes(jamal, X).
• likes(X, saleh).
Do these unify (match)?
no
X cannot be bound to both jamal and saleh
20
Variables and Unification (cont.)
• f(X, Y).
• f(P, P).
Do these unify (match)?
yes
X=P and Y=P, A variable (such as X) can be bound to another
variable (such as P). In this case we can also infer that X=Y.
• f(foo, L).
• f(A1, A1).
Do these unify (match)?
yes
A1=foo and A1=L hence L=foo
21
Rules
Rules allows us to make conditional statements about
our world.
Each rule can have several variations, called clauses.
These clauses give us different choices about how to
perform inference about our world.
Example:
‘All men are mortal’ can be expressed as the following Prolog rule:
mortal(X) :-
human(X).
22
Rules (cont.)
The clause ‘All men are mortal’ can be read in two ways: - Declarative: “For a given X, X is mortal if X is human”. - Procedural: “To prove the main goal that X is mortal,
prove the sub-goal that X is human”. Let’s co ti ue our exa ple a d defi e the fact: ‘Socrates is human’ so that our program now looks as follows:
mortal(X) :-
human(X).
human(socrates). 23
Rule
Fact
Rules (cont.)
If we now pose the following question to Prolog:
?- mortal(socrates).
The Prolog interpreter would respond as follows:
yes
Why?
In order to solve the query [?- mortal(socrates)] we
use the rule (mortal(X):- human(X).).
This said that in order to prove someone mortal, we have
to prove he/she is human. Thus, from the goal, Prolog
generates the sub-goal of showing human(socrates).
The sub-goal succeeds & hence the overall goal succeeds. 24
Rules (cont.)
We can also use variables within queries. For example,
we wish to see if there is somebody who is mortal.
This is done by the following line:
?- mortal(P).
The Prolog interpreter responds:
P = socrates
yes
This means that Prolog was able to prove the goal by binding the variable P to socrates. This was done by again proving
someone was mortal by proving the sub-goal that they were
human. Prolog thus asked if there was any P that was human.
This matches against the clause human(socrates) thereby
binding P to socrates. 25
Rules (cont.)
Example:
Given the following database:
age(ahmad, 20).
age(ahmad, 21).
And suppose we posed the following query:
?- age(ahmad,P).
Answer:
P = 20 ? ;
P = 21
26
Rules (cont.)
To prove a particular thing by specifying alternative ways:
Sometimes we may wish to specify alternative ways of
proving a particular thing. We can do this by using
different rules and facts with the same name.
For example, we can represent the sentence:
Something is fun if its a red car or a blue bike or it is
ice cream
Answer:
27
Rules (cont.)
fun(X) :-
red(X),
car(X).
fun(X) :-
blue(X),
bike(X).
fun(ice_cream).
Three ways of finding out if something is fun. Something is fun
if it is a red and a car or blue and a bike, or if it is ice cream.
28
Rules
Facts
Rules (cont.)
Example:
- Given the above program.
- Suppose, the following database is given:
red(toyota).
car(toyota).
car(honda).
blue(suzuki).
bike(suzuki).
blue(toyota).
29
Rules (cont.)
- Queries:
1) ?- fun (toyota).
true? ;
no
2) ?- fun(honda).
no
3) ?- fun(suzuki).
yes
4) ?- car(Nissan).
Nissan=toyota ;
Nissan=honda
yes
5) ?- fun(What).
30
Put ; then Enter
Put ; then Enter
What=toyota; What=suzuki; What=ice_cream yes
Put ; then Enter
Rules (cont.)
Variable names in separate rules are totally independent,
just as if different variable names had been used.
The program
fun(X):-
red(X),
car(X).
fun(X):-
blue(X),
bike(X).
Variable name scoping is per-individual rule (called clause). 31
The program
fun(X_1):-
red(X_1),
car(X_1).
fun(X_2):-
blue(X_2),
bike(X_2).
Prolog deals them as follows
Rules (cont.)
Example (How Prolog executes a query against rules): Consider the following program:
fun(X):-
red(X),
car(X).
fun(X):-
blue(X),
bike(X).
car(toyota).
car(ford).
bike(yamaha).
red(toyota).
red(ford).
blue(yamaha).
32
rule
rule
facts
Let’s see how Prolog executes the query: ?- fun(yamaha).
Prolog will compare yamaha with
the first clause (car & red), if match
then succeeds otherwise, Prolog will
compare yamaha with the second
clause (blue & bike), if match then
succeeds otherwise fails. So:
?- fun(yamaha). yes
Rules (cont.)
Example (How Prolog executes all fun items): Consider the following program:
fun(X):-
red(X),
car(X).
fun(X):-
blue(X),
bike(X).
car(toyota).
car(ford).
bike(yamaha).
red(toyota).
red(ford).
blue(yamaha).
33
Let’s see how Prolog executes the query: ?- fun(What).
Prolog will try the first clause of fun. This
results in trying the goal red(What). This
succeeds matching the first clause of red with
the binding What=toyota. Now Prolog
attempts the goal car(toyota). This matches
the first clause of car, and, as a result, the fun
goal succeeds. So:
?- fun(What). What = …
Rules (cont.)
Exercise 1:
Indicate whether the following are syntactically correct:
1) a:- b, c, d:- e f.
No. Missing comma b/w e and f. Also :- should not be used
within the body of a rule.
2) happy(X):- a, b.
Yes
3) happy(X):- hasmoney(X) & has_friends(X).
No. & symbol is not recognized by Prolog. Use , (comma) for
AND.
4) fun(fesh):- blue(betty), bike(yamaha).
Yes
34
Rules (cont.)
Exercise 2: Given the database below:
likes(jehad, majed).
likes(jehad, trains).
likes(yousef, fast_cars).
likes(Person1, Person2):-
hobby(Person1, Hobby),
hobby(Person2, Hobby).
hobby(jehad, reading).
hobby(talal, sailing).
hobby(hasan, reading).
hobby(saleh, sailing).
35
Rules (cont.)
Study the queries underneath. Indicate whether you think a
particular query will succeed or fail by answer yes or no :
1) ?- likes(jehad, trains).
Yes
2) ?- likes(hasan, jehad).
Yes
3) ?- likes(talal, hasan).
No
4) ?- likes(jehad, hasan).
Yes
36
Examples
Predicate
likes (symbol, symbol)
Clauses
likes (emam, swimming).
likes (hasan, fishing).
likes (rashed, fishing).
likes (emam, reading).
Goal: likes (rashed, fishing)
yes
Goal: likes (emam, fishing)
no
Goal: likes (Y, fishing)
Y= hasan
Y=rashed
Goal: likes (emam, X)
X= swimming
X= reading
Goal: likes (X, Y)
X= emam Y= swimming
X= hasan Y= fishing
X= rashed Y= fishing
X= emam Y= reading 37
Search: Introducing Backtracking
Backtracking is the mechanism for finding multiple solution
Example: Suppose that we have the following database:
eats(fares, pears).
eats(fares, t_bone_steak).
eats(fares, apples).
We wa t to ask what are all the things that fares eats?
Query: ?- eats(fares, FoodItem).
Answer: FoodItem = pears ;
FoodItem = t_bone_steak ;
FoodItem = apples ;
no 38
Search: Backtracking in Rules
We can also have backtracking in rules.
Example: Consider the following program:
hold_party(X) :-
birthday(X),
happy(X).
birthday(talal).
birthday(fares).
birthday(hasan).
happy(mohammed).
happy(jehad).
happy(hasan). 39
Backtracking in Rules (cont.)
If we pose the query: ?- hold_party(Who).
Solution:
First attempts to find a clause of sub-goal birthday.
This Binding X in sub-goal birthday(X) to talal. Then
attempt the sub-goal happy(talal). This will fail.
As a result, Prolog a ktra ks … Binding X in sub-goal birthday (X) to fares. Then
attempts the sub-goal happy(fares). This will fail.
As a result, Prolog a ktra ks … A d so o … ===> Who = hasan
40
Example
Search Example: consider the following program:
fun(X) :- /* so ethi g is either fu e ause its … */
red(X), /* red */
car(X). /* and a car */
fun(X) :- /* or its fu if its … */
blue(X), /* blue */
bike(X). /* and a bike */
And consider the following database:
41
Example (cont.)
/* database of red items */
red(apple_1).
red(block_1).
red(car_27).
/* database of cars */
car(ahmad_27).
car(ali_57).
42
/* database of blue items */
blue(flower_3).
blue(glass_9).
blue(honda_81).
/* database of bikes */
bike(ahmad_27).
bike(my_bike).
bike(honda_81).
Example (cont.)
Let s ask what is a fu ite ? ?- fun(What).
Solution:
We have two clauses of fun. We start by the first clause:
• The first red item is apple_1, so X=apple_1.
• Next, we have to see if this is a car. i.e. is it
car(apple_1). The answer is no.
Now, Prolog backtracks in order to find another solution.
Thus it takes the second item (i.e. block_1) and repeats
the above process for the second item. 43
Example (cont.)
• The second red item is block_1, so X=block_1.
• Next, we have to see if this is a car. i.e. is it
car(block_1). This will fail and hence the answer is no.
Again, Prolog backtracks to find another solution. Thus it
takes the third item (i.e. car_27) and repeats the process
• The third red item is car_27, so X=car_27.
• we have to see if this is a car. i.e. is it car(car_27). This
will not match and hence the answer is no.
Again, Prolog backtracks. But now no more red items.
Thus Prolog moves on to the second clause and repeats
the same process to see if something is blue and bike. 44
Example (cont.)
• The first blue item is flower_3, so X=flower_3.
• Next, we have to see if this is a bike. i.e. is it
bike(flower_3). This fails and hence the answer is no.
Prolog backtracks to find another solution. But no more
blue items. Thus it takes the second item (i.e. glass_9)
and repeats the process
• The second blue item is glass_9, so X=glass_9.
• we have to see if this is a bike. i.e. is it bike(glass_9).
This will not match and hence the answer is no.
Prolog backtracks. Thus it takes the third item (i.e.
honda_81) and repeats the process. 45
Example (cont.)
• The third blue item is honda_81, so X=honda_81.
• we will see if this is a bike. i.e. is it bike(honda_81).
This matches and hence the answer is yes.
Prolog backtracks to find another solution. But no more
blue items. Thus Prolog moves on to the next clause.
But no more clauses and Prolog stops.
So the answer is:
What = honda_81
yes
46
Exercise
Exercise: consider the following program:
fun(X) :- /* so ethi g is either fu e ause its … */
red(X), /* red */
car(X). /* and a car */
fun(X) :- /* or its fu if its … */
blue(X), /* blue */
bike(X). /* and a bike */
And consider the following database:
47
Exercise (cont.)
/* database of red & car items */
red(cricket_ball).
red(my_hat).
red(car_27).
car(peugot).
car(rover).
48
/* database of blue & bike items */
blue(cheese).
blue(suzuki).
blue(honda).
bike(yamaha).
bike(suzuki).
bike(honda).
Consider the query ?- fun(What). For each of the following pairs of goals, which would you expect to see first for the above query:
Exercise (cont.)
1. fun(X)
red(X)
2. blue(cheese)
red(car_27)
3. bike(honda)
bike(yamaha)
4. blue(cheese)
bike(suzuki)
5. bike(cheese)
car(car_27) 49
Recursion
Recursion means a program calls itself until some
final point or a stopping condition is reached.
In Prolog, this means we have a first fact that acts as
stopping condition followed up by rule(s) that
performs some operations before re-invoking itself.
50
Example 1
Example 1:
on_route(amman).
on_route(Place) :-
move(Place, Method, NewPlace),
on_route(NewPlace).
move(home, taxi, kharj).
move(kharj, bus, riyadh).
move(riyadh, plane, amman).
Query ?- on_route(home). 51
To see if we have already reached amman. If so we stop.
To see if there is a move from current place to somewhere new and then recursive sees if the NewPlace is on_route to amman. facts
This program sees if it is possible to travel to amman from a particular place.
Example 1 (cont.)
on_route(amman).
on_route(Place) :-
move(Place, Method, NewPlace),
on_route(NewPlace).
52
Stopping condition
Perform some operation
Re-invoke itself
Example 1 (cont.)
on_route(home) match clause 2. It a t at h lause 1
e ause ho e a d a a do t at h. The second on_route clause consists of two sub-goals.
The first asks whether you can move from home to
some new location i.e. move(home, Method,
NewPlace). This succeeds with Method=taxi,
NewPlace=kharj. This says that yes we can move from
home by taking taxi to kharj.
Next we recursively see if we can find a route from
kharj to amman by doing the same thing again. This is
done by executing the new sub-goal on_route(kharj). 53
Example 1 (cont.)
on_route(kharj) match clause 2.
Again, move(kharj, Method, NewPlace) succeeds with
Method=bus, NewPlace=riyadh.
Next we recursively see if we can find a route from
riyadh to amman by doing the same thing again. This
is done by executing the sub-goal on_route(riyadh).
54
Example 1 (cont.)
on_route(riyadh) match clause 2.
Again, move(riyadh, Method, NewPlace) succeeds
with Method=plane, NewPlace=amman.
Next we try the recursive goal on_route(amman).
This now matches clause one of on_route and
succeeds.
The answer of ?- on_route(home) is yes.
55
Example 2
Suppose a program is given underneath:
parent(jehad,bader).
parent(bader,talal).
parent(talal,mousa).
ancestor(X,Y) :- parent(X,Y).
ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y).
Check the query: ?- ancestor(jehad,talal).
Solution:
56
Example 2 (cont.)
First ancestor clause fails [i.e. parent(jehad,talal) fails]
Second ancestor clause parent(jehad,Z) means
choosing parent(jehad,bader) and binding Z=bader.
And pose the recursive query ancestor(bader,talal).
Applying the ancestor rule again. The first clause
parent(bader,talal) succeeds.
Hence the answer of ?- ancestor(jehad,talal) is yes
57
Exercise Town1 ----->town2----->town3----->town4----->town5----->town6
A one way road links 6 towns. Write a program that
can work out if you can travel on that road. For
example here are two example program behaviors.
?- can_get(town2,town5).
Yes
?- can_get(town3,town1).
no
58
Exercise (cont.)
Solution:
move(town1,town2).
move(town2,town3).
move(town3,town4).
move(town4,town5).
move(town5,town6).
can_get(X,Y) :- move(X,Y).
can_get(X,Y) :- move(X,Z),
can_get(Z,Y).
59