Professional Documents
Culture Documents
Prolog 01
Prolog 01
Chapter 1: Introduction
Ahmed Sallam
Slides based on original lecture slides Dr. Temur Kutsia
"What" vs "How"
2
Procedural programming
• The programmer has to specify how to get the output for
the range of required inputs.
• The programmer must know the appropriate algorithm.
Declarative programming
• Requires a more descriptive style.
• The programmer must know what relationships hold
between various entities.
Introduction 16-Feb-17
Example: List Concatenation
3
In procedural style:
list procedure cat(list a,list b)
{
list t = list u = copylist(a);
while (t.tail != nil) t = t.tail;
t.tail = b;
return u;
}
In declarative style:
cat([], L, L).
cat([H|T], L, [H|Z]) :- cat(T, L, Z).
Introduction 16-Feb-17
Logic Programming
4
Introduction 16-Feb-17
Historical Facts
5
1970-ies:
• Bob Kowalski.
"Predicate Logic as a Programming Language".
IFIP Congress, Stockholm
• Alain Colmerauer and his group.
Interpreter of the first logic programming language Prolog.
Marseille
Introduction 16-Feb-17
Prolog
6
Introduction 16-Feb-17
Prolog in Industrial Applications
7
Clarissa:
• A fully voice-operated procedure browser.
• Developed at NASA.
• Used on the International Space Station.
• Enables astronauts to give full attention to the task while
they navigate through complex procedures using spoken
commands.
• Implemented in SICStus Prolog.
Introduction 16-Feb-17
Prolog in Industrial Applications
8
Basics of PROLOG
Facts
Questions
Variables
Conjunction
Rules
Introduction 16-Feb-17
P ROLOG
10
Introduction 16-Feb-17
Relationships
11
Example
John owns the book
• The relationship: ownership
• The objects: book, John
Directional:
• John owns the book
• Not: The book owns John
Introduction 16-Feb-17
P ROLOG
12
Introduction 16-Feb-17
P ROLOG
13
Sisters Example:
A rule defining sisters and the facts about the people involved.
The user would ask:
Are these two people sisters?
The system would answer
yes (true) or no (false)
Introduction 16-Feb-17
Programming in P ROLOG
14
Introduction 16-Feb-17
Contents
15
Basics of PROLOG
Facts
Questions
Variables
Conjunction
Rules
Introduction 16-Feb-17
Parts of Fact
16
Introduction 16-Feb-17
0
Order of Objects
17
Introduction 16-Feb-17
Examples of Facts
18
Example
Gold is valuable.
valuable(gold)
Jane is a female.
female(jane)
John owns some gold.
owns(john,gold)
John is the father of Mary.
father(john,mary)
Introduction 16-Feb-17
Interpretation of Names
19
Introduction 16-Feb-17
Interpretation of Names
20
Introduction 16-Feb-17
Fact Terminology
21
Introduction 16-Feb-17
Database
22
Definition
In PROLOG , database is a collection of facts.
Introduction 16-Feb-17
Contents
23
Basics of PROLOG
Facts
Questions
Variables
Conjunction
Rules
Introduction 16-Feb-17
Questions
24
The database contains the facts from which the questions are
answered.
A Question can look exactly like a fact:
o wn s ( m a r y, b o o k ) .
The difference is in which mode one is in
Introduction 16-Feb-17
Questions
25
Question: ? - owns(mary,book ).
Meanins: If mary is interpreted as a person called Mary, and book is
interpreted as some particular book, then
?- owns(mary,book ).
means: Does Mary own the book?
Introduction 16-Feb-17
Database Search
26
Example
Facts in the database:
likes(joe,fish).
likes(joe,mary).
likes(mary,book).
likes(john,book).
Questions:
?- l i k e s ( j o e , m o n e y ) . no
?- l i k e s ( j o e , m a r y ) . yes
?- k i n g ( j o h n , f r a n c e ) . n o
Introduction 16-Feb-17
Knowledge
27
Question:
Is Socrates Greek?
?- g r e e k ( s o c r a t e s )
Variables.
Introduction 16-Feb-17
Contents
29
Basics of PROLOG
Facts
Questions
Variables
Conjunction
Rules
Introduction 16-Feb-17
Variables
30
Better to ask:
What does John like?
or
Does John like X?
(i.e. use variables)
Introduction 16-Feb-17
Question With Variables
31
Introduction 16-Feb-17
P ROLOG Answer
32
Database:
likes(john,flowers).
Question:
?- lik es(john,X).
P ROLOG answers:
X = f lower s
Introduction 16-Feb-17
Many Answers
33
Database:
likes(john,flowers).
likes(john,mary).
likes(paul,mary).
Question:
?-lik es( john,X ).
PROLOG answers:
X =f lower s
and the user acknowledges
X=mary
and the user acknowledges
no
Introduction 16-Feb-17
Placemarker
34
Introduction 16-Feb-17
Contents
35
Basics of PROLOG
Facts
Questions
Variables
Conjunction
Rules
Introduction 16-Feb-17
Conjunctions
36
Introduction 16-Feb-17
Conjunctions
37
Database:
likes(mary,food).
likes(mary,cola).
likes(john,cola).
likes(john,mary).
?- likes(john,mary), likes(mary,john).
Answer: no
A match for l i k e s ( j o h n , m a r y )
but none for l i k e s ( m a r y , j o h n )
Introduction 16-Feb-17
Conjunctions with Variables
38
Find out what Mary likes and then see if John likes it.
Introduction 16-Feb-17
Backtracking
39
Introduction 16-Feb-17
Match First
40
,
Cola).
,
Cola).
Introduction 16-Feb-17
Match Second
41
,
Cola).
,
Cola).
Introduction 16-Feb-17
Backtrack
42
, Cola
Cola).
,
Cola).
Cola
Introduction 16-Feb-17
Success
43
, Cola
Cola).
,
Cola).
Cola
Introduction 16-Feb-17
Contents
44
Basics of PROLOG
Facts
Questions
Variables
Conjunction
Rules
Introduction 16-Feb-17
Rules
45
Introduction 16-Feb-17
Rule Examples
46
Rules Define:
Introduction 16-Feb-17
Formulating Rules
47
Introduction 16-Feb-17
Rule Syntax
48
, Cola).
Introduction 16-Feb-17
Variable Scope
49
l i k e s ( j o hn , X ) : - l i k e s ( X , cola),l i k e s ( X , food).
l i k e s ( j o h n , mary):-
likes(mary,cola),likes(mary,food).
l i k e s ( j o h n , adam):- l i k e s ( a d a m , c o l a ) , l i k e s ( a d a m ,
food).
Introduction 16-Feb-17
Royal Parents
50
Example
The parents of X are Y and Z.
Y is the mother.
Z is the father.
Database:
male(albert).
m ale( edwar d) .
female(alice).
female(victoria).
parents(edward,victoria,albert).
parents(alice,victoria,albert).
Introduction 16-Feb-17
Sisters
51
Example
X is a sister of Y if:
X is female,
X has parents M and F,
Y has parents M and F.
Rule:
s is t er ( X , Y ) : - female(X),
parents(X,M,F),
parents(Y,M,F).
Introduction 16-Feb-17
Sisters Question
52
Question:
?- sister(alice,edward).
Introduction 16-Feb-17
Is Alice Edward’s Sister?
53
7 sister(alice,edward)
(1) male(albert).
X0=alice,
(2) m a l e ( e d wa rd ). Y0=edward
(3) female(alice). 3 female(alice),
(4) female(victoria). parents(alice,M0,F0),
p a re n t s (e dward, M 0 , F 0 ).
( 5 ) p a r e n t s ( e d wa r d ,
victoria, albert).
6 parents(alice,M0,F0),
(6) parents(alice, p a re n t s (e dward, M 0 , F 0 ).
victoria,albert).
M0=victoria,
(7) sister(X,Y):- F0=albert
fem ale(X),
5 parents(edward,victoria,albert).
p a r e n t s ( X, M , F ) ,
parents(Y,M,F).
•
Introduction 16-Feb-17
Is Alice Edward’s Sister?
54
7 sister(alice,edward)
(1) male(albert). X0=alice,
Y0=edward
(2) m a l e ( e d wa rd ).
3 female(alice),
(3) female(alice).
parents(alice,M0,F0),
(4) female(victoria). p a re n t s (e dward, M 0 , F 0 ).
( 5 ) p a r e n t s ( e d wa r d ,
victoria, albert). 6 parents(alice,M0,F0),
(6) parents(alice, p a re n t s (e dward, M 0 , F 0 ).
victoria,albert). M0=victoria,
F0=albert
(7) sister(X,Y):-
5 parents(edward,victoria,albert).
fem ale(X),
p a r e n t s ( X, M , F ) ,
•
parents(Y,M,F).
Introduction 16-Feb-17
Who’s Sister Is Alice?
55
7 sister(alice,X)
(1) male(albert).
X0=alic e,
Y0=X
(2) m a l e ( e d wa rd ).
(3) female(alice). 3 female(alice),
parents(alice,M0,F0),
(4) female(victoria). p a r e n t s ( X, M 0 , F 0 ) .
( 5 ) p a r e n t s ( e d wa r d ,
victoria, albert). 6 parents(alice,M0,F0),
(6) parents(alice, p a r e n t s ( X, M 0 , F 0 ) .
victoria,albert). M0=victoria
F0=albert
(7) sister(X,Y):-
5 parents(X,victoria,albert).
fem ale(X),
X=edward
p a r e n t s ( X, M , F ) ,
•
parents(Y,M,F).
Answer: X = edward.
Introduction 16-Feb-17
Useful Links
56
• SWI-Prolog:
• http://www.swi-prolog.org/
• SWI-Prolog Editor (Windows, SWI-x86 only)
http://lakk.bildung.hessen.de/netzwerk/faecher/informatik/swiprolog/ind
exe.html
• Prolog mode for (X)Emacs:
http://turing.ubishops.ca/home/bruda/emacs-prolog/
• Prolog newsgroup:
http://groups.google.com/groups?group=comp.lang.prolog
• Links from SWI-Prolog Web page:
http://www.swi-prolog.org/Links.html
Introduction 16-Feb-17