صفحه 1:
10016 0300
1
صفحه 2:
Prolog
(Programming in Logic)
Has been around since 70’s
Prolog is an un-typed language
Prolog is a declarative programming language
Prolog is based on First Order (predicate) Logic
Prolog was selected as the language of the fifth
generation computers by the Japanese
Prolog, is a suitable language for solving problems
involving objects and relationships among these
objects.
A Prolog program consists of facts and rules.
صفحه 3:
Applications of Prolog
AI programming
Relational databases
Natural languages
Machine learning
Robot planning
Symbolic solution of equations
Chemical structure analysis
Expert Systems
Mechanical Theorem Provers
صفحه 4:
olving a problem using Prolog involves
Declaring facts
Defining rules
Asking questions
iry likes john.
ae john).
cate(in lowercase) Arguments (in lowercase)
1e beginning with a lowercase letter is an atom
ends with a full stop.
nents are seperated with commas.
rder of arguments is arbitrary but the order chosen must b
consistently.
-may have an arbitrary number of arguments.
صفحه 5:
All Prolog data objects are terms. For instance,
mary
likes(mary, john).
and the variable X
are all terms.
A term may be simple or compound.
A simple term may be
An atom
A number
Or a variable.
A compound term consists of a functor followed by a
number of
arguments. The number of arguments is referred to as
the arity of the
functor.
صفحه 6:
ugh we have to be consistent in the use of predicate and ol
‘s, they are arbitrary. We could have declared the above fa
1(m,j).
would, however, make the program less readable.
Other examples of facts
interesting(declarative programming).
useful(declarative programming).
male(john).
female(mary).
father(john,mary).
great(massey).
has_a_job(mary).
happy(john).
plays(mary,john, tennis).
valuable(money).
gives(john,mary,money).
صفحه 7:
rolog database is a collection of facts (and rules).
scree sea
را ت۱۱
likes(tom,mouse).
000
42 نا
ل ا نا
likes(john, book).
likes(mary,book).
Likes(tom,john).
Now that we have some facts, we may ask
ize).
JB ere yee ery re
صفحه 8:
0
> getting a no answer because we do not have a fact in the
se to indicate that mary and john know each other. This do
‘an that the statement is false in reality. It just means that :
ased on the knowledge available.
17 ask questions involving variables. If we were interested i
; that like john, we would formulate the following query:
(
7
|
صفحه 9:
Prolog tries to resatisfy the question everytime we enter
a semicolon “;”.
Prolog uses a pointer called a place marker to keep track
01 طعموعة عط
he Saeco anything. Every time the variable
0و
a value like mary in the above example, we say that X is
ا ا
to that value. In the example above, X was instantiated
زتنس ند ۱
.مها مر aN)
نا abn Query
۳ رازه۰ ور( Y=jerry;
مت سنا = i
eas oe Y=john;
,0ع امسطصام) عسل
صصص )صملا ae Y=mouse;
زا
0 i 1) 7
صفحه 10:
صفحه 11:
Saul ory
In the above database we have a fact indicating that
mary likes john.
This however does not make the fact that john also likes
mary true. If
we wanted that to be true, we would have to add a
مات
۱ make this a generalization by adding a rule
۳ دهعت
صفحه 12:
Jther examples of rules:
X):-valuable(X
X is a bird if
it is an animal and
it has feathers.
is_a_bird(X):- is_an_animal(X),has(X,feathers).
is brother of Y if
X is a male and
X and Y have the same parents.
is_brother_of(X,Y):-male(X),
parents(X, Father, Mother),
parents(Y, Father, Mother).
صفحه 13:
Another database
صفحه 14:
-is brother of(greg,andrew).
Yes
۱ 1 وت الت ا ات
Yes
?- is brother _of(greg,X).
X = greg;
X = jennifer ;
X = andrew ;
No
male(andrew).
male(john).
male(george).
male(greg).
male(adam).
female(mary).
female(jennifer).
female(eve).
parents(john,george,mary).
parents(greg,adam,eve).
parents(jennifer, adam,eve).
parents(andrew, adam,eve).
is_brother_of(X,¥):-male(X),
parents(X, Father, Mother),
parents(Y, Father, Mother).
= greg is_sister_of(X,Y):-female(X), كما ممح عد
= 2 arents(X, Father, Mother),
ue andrew ; parents ۱۷, Mother
ما اما امس مها
صفحه 15:
SWI-Prolog
% d:/Prolog/sis.pl compiled 0.00 sec, 2,048 bytes
Welcome to SWI-Prolog (Version 4.0.9)
Copyright (c) 1990-2000 University of Amsterdam.
Copy policy: GPL-2 (see www.gnu.org)
For help, use ?- help(Topic). or ?- apropos(Word).
?-is_sister_of(X,Y).
عكتسموز < 216
ز و۲ < ۷
0
ا ا
: ااعلمة ع لآ
No
0 ل رم
صفحه 16:
صفحه 17:
Welcome to SWI-Prolog (Version 4.0.9)
Copyright (c) 1990-2000 University of
Amsterdam.
Copy policy: GPL-2 (see www.gnu.org)
For help, use ?- help(Topic). or ?-
apropos(Word).
y Prolog now.
صفحه 18:
?- append ([abc,def],[ghi,lmn],L).
L = [abe, def, ghi, Imn]
Yes
?- append([abc,def],L, [abc,def,ghi,lmn]).
L = [ghi, Imn]
صفحه 19:
Consult mode
You can enter consult mode by typing in “[user]”
قط خدج ”2-”
۱3
forget the period at the end of your input. At the “|:”
, type in what you want entered into your database.
ale(john).
male(mary).
ther(john,mary).
eat(massey).
1S_a_job(mary).
1e last fact is entered and at the “|:” prompt, enter a «
» consult mode. An example is shown on the next slide
صفحه 20:
?- [user].
: likes(tom, jerry).
: likes(mary,john).
: likes(tom,mouse).
: likes(tom,jerry).
: likes(Gjerry,cheeze).
: likes(mary, fruit).
: likesGohn,book).
: knows(mary,book).
: knows(tom, john).
% user compiled 0.01 sec, 64 bytes
كد
0007
۱۱۱ Tea ae
.لططامز ,لقص )ىم 1ن[
likes(tom, mouse).
.وز بحدهغ )م111
likes(jerry, cheeze).
likes(mary, fruit),
likes(john, book).
knows(mary, book).
.لصطه زيصدم]) دم دعل
0 ]| ۵5
صفحه 21:
Listing your predicates
صفحه 22:
?- listing(likes/2). /* we could have said “listing(likes).” in
this case*/
likes(tom, jerry).
likes(mary, john).
likes(tom, mouse).
likes(tom, jerry).
likes(jerry, cheeze).
likes(john, book).
?- listing(knows).
knows(mary,book)
knows(tom,john).
صفحه 23:
صفحه 24:
?- trace.
Yes
[trace] ?- likes(X,Y).
Call: (7) likes(_G332, _G333) ?
creep
Exit: (7) likes(tom, jerry) ? creep
X= tom
= jerry;
Redo: (7) likes(_G332, _G333) ?
creep
Exit: (7) likes(mary, john) ? creep
X= mary
Y = john
Redo: (7) likes(_G332, G333) ?
creep
Exit: (7) likes(tom, mouse) ? creep
X=tom
Y = mouse ;
Redo: (7) likes(_G332, _G333) ?
creep
: (7) likes(tom, jerry) ? creep
صفحه 25:
2 < 7
Y = cheeze ;
Redo: (7) likes(_G332,
_G333) ? creep
Exit: (7) likes(mary, fruit) ?
creep
X = mary
Y= fruit,
Redo: (7) likes(_G332,
_G333) ? creep
Exit: (7) likes(john, book) ?
creep
X = john
Y = book;
Fail: (7) likes(_G332, G333) ?
صفحه 26:
صفحه 27:
owns(john, book).
owns(victoria, boo
owns(george, book
owns(john,book(prolog,clocksin_and_m\
owns(victoria, book(prolog,bratko)).
owns(john,book(prolog,author(clocksin_and_mellish),year(1994
owns(victoria, book(prolog,author(bratko),year(2001),edit}
oe 6 نخس 9 مسا
صفحه 28:
صفحه 29:
صفحه 30:
صفحه 31:
?- concen(X,Y).
X=us
91.6667
Y=
Yes
?- concen(X,Y).
X=us
; 91.6667
china
و وا
nz
Y=
x
Y=
X=
Y=16;
X = india
304.136 ;
Y=
No
Logic Programming
Prolog
Lecture 12 Introduction to Prolog
1
Prolog
(Programming in Logic)
Has been around since 70’s
Prolog is an un-typed language
Prolog is a declarative programming language
Prolog is based on First Order (predicate) Logic
Prolog was selected as the language of the fifth
generation computers by the Japanese
Prolog, is a suitable language for solving problems
involving objects and relationships among these
objects.
A
Prolog
program
consists
of facts
and rules. New York
W.F.
Clocksin,
Programming
in Prolog,
Springer-Verlag
Inc., 1998.
Bratko, I (2001), PROLOG: programming for artificial
intelligence, Third edition,
Addison-Wesley.
Lecture 12 Introduction to Prolog
2
Applications of Prolog
AI programming
Relational databases
Natural languages
Machine learning
Robot planning
Symbolic solution of equations
Chemical structure analysis
Expert Systems
Mechanical Theorem Provers
Lecture 12 Introduction to Prolog
3
olving a problem using Prolog involves
Declaring facts
Defining rules
Asking questions
ary likes john.
es(mary, john).
cate(in lowercase) Arguments (in lowercase)
me beginning with a lowercase letter is an atom
t ends with a full stop.
ments are seperated with commas.
rder of arguments is arbitrary but the order chosen must be
consistently.
t may have an arbitrary number of arguments.
Lecture 12 Introduction to Prolog
4
All Prolog data objects are terms. For instance,
mary
likes(mary, john).
and the variable X
are all terms.
A term may be simple or compound.
A simple term may be
An atom
A number
Or a variable.
A compound term consists of a functor followed by a
number of
arguments. The number of arguments is referred to as
the arity of the
Lecture 12 Introduction to Prolog
5
functor.
ough we have to be consistent in the use of predicate and ob
es, they are arbitrary. We could have declared the above fac
l(m,j).
would, however, make the program less readable.
Other examples of facts
interesting(declarative_programming).
useful(declarative_programming).
male(john).
female(mary).
father(john,mary).
great(massey).
has_a_job(mary).
happy(john).
plays(mary,john,tennis).
valuable(money).
Lecture 12 Introduction to Prolog
gives(john,mary,money).
6
rolog database is a collection of facts (and rules).
likes(tom,jerry).
likes(mary,john).
likes(tom,mouse).
likes(tom,jerry).
likes(jerry,cheeze).
likes(mary,fruit).
likes(john,book).
likes(mary,book).
Likes(tom,john).
Now that we have some facts, we may ask
questions.
?- likes(jerry,cheeze).
yes
?- likes(jerry,mary). Lecture 12 Introduction to Prolog
7
ws(mary,john).
e getting a no answer because we do not have a fact in the
ase to indicate that mary and john know each other. This do
ean that the statement is false in reality. It just means that i
based on the knowledge available.
ay ask questions involving variables. If we were interested in
s that like john, we would formulate the following query:
(X,john).
ry;
m;
Lecture 12 Introduction to Prolog
likes(tom,jerry).
likes(mary,john).
likes(tom,mouse).
likes(tom,jerry).
likes(jerry,cheeze).
likes(mary,fruit).
likes(john,book).
likes(mary,book).
likes(tom,john).
8
Prolog tries to resatisfy the question everytime we enter
a semicolon “;”.
Prolog uses a pointer called a place marker to keep track
of the search
The variable matches anything. Every time the variable
for
solutions.
X ismore
bound
to
a value like mary in the above example, we say that X is
instantiated
to that value. In the example above, X was instantiated
likes(tom,jerry).
to mary once
Who likes what?
likes(mary,john).
likes(tom,mouse).
and to tom the next time. ?-likes(X,Y)
Another Query
likes(tom,jerry).
X=tom,
Y=jerry;
likes(jerry,cheeze).
X=mary,
Y=john;
likes(mary,fruit).
X=tom,
likes(john,book).
likes(mary,book).
Y=mouse;
likes(tom,john).
X=tom,
Y=jerry;
Lecture 12 Introduction
to Prolog
9
Rules
Lecture 12 Introduction to Prolog
10
Rules
In the above database we have a fact indicating that
mary likes john.
This however does not make the fact that john also likes
mary true. If
we wanted that to be true, we would have to add a
relevant fact to the
database.
We could make this a generalization by adding a rule
to ourlikes(john,mary).
database as follows:
likes(X,Y):-likes(Y,X).
Rule head
if
Rule body
Lecture 12 Introduction to Prolog
11
Other examples of rules:
likes(john,X):-valuable(X).
To define a rule indicating that
X is a bird if
it is an animal and
it has feathers.
We would say,
is_a_bird(X):- is_an_animal(X),has(X,feathers).
To define the rule,
X is brother of Y if
X is a male and
X and Y have the same parents.
we would say
is_brother_of(X,Y):-male(X),
parents(X, Father, Mother),
parents(Y, Father, Mother).
Lecture 12 Introduction to Prolog
12
Another database
male(andrew).
male(john).
male(george).
male(greg).
male(adam).
female(mary).
female(jennifer).
female(eve).
parents(john,george,mary).
parents(greg,adam,eve).
parents(jennifer, adam,eve).
parents(andrew, adam,eve).
is_brother_of(X,Y):-male(X),
parents(X, Father, Mother),
parents(Y, Father, Mother).
is_sister_of(X,Y):-female(X),
parents(X, Father, Mother),
Lecture 12 Introduction
to Prolog
parents(Y,
Father,
Mother).
13
- is_brother_of(greg,andrew).
Yes
?- is_sister_of(jennifer,greg).
Yes
?- is_brother_of(greg,X).
X = greg ;
X = jennifer ;
X = andrew ;
No
?- is_brother_of(X,Y).
X = andrew
Y = greg ;
X = andrew
Y = jennifer ;
X = andrew
Y = andrew ;
X = john
Y = john ;
X = greg
Y = greg ;
X = greg
Y = jennifer ;
X = greg
Y = andrew ;
No
male(andrew).
male(john).
male(george).
male(greg).
male(adam).
female(mary).
female(jennifer).
female(eve).
parents(john,george,mary).
parents(greg,adam,eve).
parents(jennifer, adam,eve).
parents(andrew, adam,eve).
is_brother_of(X,Y):-male(X),
parents(X, Father, Mother),
parents(Y, Father, Mother).
is_sister_of(X,Y):-female(X),
parents(X, Father, Mother),
Lecture 12 Introduction to Prolog
14
parents(Y, Father, Mother).
SWI-Prolog
% d:/Prolog/sis.pl compiled 0.00 sec, 2,048 bytes
Welcome to SWI-Prolog (Version 4.0.9)
Copyright (c) 1990-2000 University of Amsterdam.
Copy policy: GPL-2 (see www.gnu.org)
For help, use ?- help(Topic). or ?- apropos(Word).
?- is_sister_of(X,Y).
X = jennifer
Y = greg ;
X = jennifer
Y = jennifer ;
X = jennifer
Y = andrew ;
No
?-
Lecture 12 Introduction to Prolog
15
SWI-Prolog
Lecture 12 Introduction to Prolog
16
SWI-Prolog
Welcome to SWI-Prolog (Version 4.0.9)
Copyright (c) 1990-2000 University of
Amsterdam.
Copy policy: GPL-2 (see www.gnu.org)
For help, use ?- help(Topic). or ?apropos(Word).
?- query Prolog now.
You may
Use the built-in Predicate “halt” to
exit Prolog.
Lecture 12 Introduction to Prolog
17
Query mode
?- append([abc,def],[ghi,lmn],L).
L = [abc, def, ghi, lmn]
Yes
?- append([abc,def],L,[abc,def,ghi,lmn]).
L = [ghi, lmn]
Yes
?-
Lecture 12 Introduction to Prolog
18
:
Consult mode
You can enter consult mode by typing in “[user]”
at the “?-”
[user].
Prompt:
forget the period at the end of your input. At the “|:”
t, type in what you want entered into your database.
male(john).
emale(mary).
ather(john,mary).
reat(massey).
as_a_job(mary).
he last fact is entered and at the “|:” prompt, enter a c
e consult mode. An example is shown on the next slide
Lecture 12 Introduction to Prolog
19
?- [user].
|: likes(tom,jerry).
|: likes(mary,john).
|: likes(tom,mouse).
|: likes(tom,jerry).
|: likes(jerry,cheeze).
|: likes(mary,fruit).
|: likes(john,book).
|: knows(mary,book).
|: knows(tom,john).
|:
% user compiled 0.01 sec, 64 bytes
Yes
?- listing.
likes(tom, jerry).
likes(mary, john).
likes(tom, mouse).
likes(tom, jerry).
likes(jerry, cheeze).
likes(mary, fruit).
likes(john, book).
knows(mary,book).
knows(tom,john).
Lecture 12 Introduction to Prolog
Yes
20
Listing your predicates
listing.
listing(likes/2).
Lecture 12 Introduction to Prolog
21
?- listing(likes/2). /* we could have said “listing(likes).” in
this case*/
likes(tom, jerry).
likes(mary, john).
likes(tom, mouse).
likes(tom, jerry).
likes(jerry, cheeze).
likes(john,book).
?- listing(knows).
knows(mary,book).
knows(tom,john).
Lecture 12 Introduction to Prolog
22
Tracing your program
trace.
notrace.
Or simply n.
spy(likes/2).
nospy
Lecture 12 Introduction to Prolog
23
?- trace.
Yes
[trace] ?- likes(X,Y).
Call: (7) likes(_G332, _G333) ?
creep
Exit: (7) likes(tom, jerry) ? creep
X = tom
Y = jerry ;
Redo: (7) likes(_G332, _G333) ?
creep
Exit: (7) likes(mary, john) ? creep
X = mary
Y = john ;
Redo: (7) likes(_G332, _G333) ?
creep
Exit: (7) likes(tom, mouse) ? creep
X = tom
Y = mouse ;
Redo: (7) likes(_G332, _G333) ?
creep
Exit: (7) likes(tom, jerry) ? Lecture
creep
12 Introduction to Prolog
24
X = jerry
Y = cheeze ;
Redo: (7) likes(_G332,
_G333) ? creep
Exit: (7) likes(mary, fruit) ?
creep
X = mary
Y = fruit ;
Redo: (7) likes(_G332,
_G333) ? creep
Exit: (7) likes(john, book) ?
creep
X = john
Y = book ;
Lecture 12 Introduction to Prolog
Fail: (7) likes(_G332, _G333)
?
25
Structures
Lecture 12 Introduction to Prolog
26
owns(john, book).
owns(victoria, book).
owns(george,book).
owns(john,book(prolog,clocksin_and_mellish)).
owns(victoria,book(prolog,bratko)).
owns(john,book(prolog,author(clocksin_and_mellish),year(1994
owns(victoria,book(prolog,author(bratko),year(2001),editi
wns(john,book(prolog,author( _ ),year( X ),edition( _ ))),X >19
Lecture 12 Introduction to Prolog
27
Arithmetic
Lecture 12 Introduction to Prolog
28
Arithmetic Operators
+
addition
X+Y
subtraction
X-Y
*
multiplication
X*Y
/
division
X/Y
//
division (integer) X // Y
mod
modulo (remainder)
X
mod Y
**
power
X **
Y
?- A is 2+3, B is A-1, C is A*2, D is C/B, E is
is
equals
Z is
C // B.
X+Y
A=5
B=4
C = 10
D = 2.5
Lecture 12 Introduction to Prolog
29
population(us,275000).
population(china,12620
00).
population(nz,4000).
Population(india,10000
00).
land(us,3000).
land(china,4000).
land(nz,250).
land(india,3288).
concen(X,Y):population(X,P),
land(X,L),
Y is P / L.
Lecture 12 Introduction to Prolog
30
?- concen(X,Y).
X = us
Y = 91.6667
Yes
?- concen(X,Y).
X = us
Y = 91.6667 ;
X = china
Y = 315.5 ;
X = nz
Y = 16 ;
X = india
Y = 304.136 ;
No
Lecture 12 Introduction to Prolog
31