Introduction to Prolog

advertisement
History of Prolog
•
- based on logic
Introduction to Prolog
- a popular artificial intelligence language
- Horn subset of first-order Predicate Logic
•
•
•
20070524 Prolog
1
SWI-Prolog 5.6.34 (2007 released)
•
SWI-Prolog-Editor 3.03
•
Visual Prolog 7.0 (March 26, 2007 released)
Developed by
Robert A. Kowalski (Univ. of Edinburgh) and
Alain Colmerauer (Univ. of Aix-Marseille)
in early of 1970
First Prolog Interpreter by Colmerauer
First Prolog Compiler by David Warren
(Univ. of Edinburgh)
20070524 Prolog
Some Available Prolog Systems
•
PROgramming in LOGic
2
Screenshot
http://www.swi-prolog.org/
http://lernen.bildung.hessen.de/informatik/swiprolog/indexe.htm
http://www.visual-prolog.com/
Successor of Turbo Prolog (by Borland)
•
SICStus Prolog 4.0.1 (May 17, 2007 released)
•
GNU GProlog 1.3.0
•
……
http://www.sics.se/isl/sicstuswww/site/index.html
http://www.gprolog.org/
20070524 Prolog
3
Some Basic Concepts
•
20070524 Prolog
4
Some Basic Concepts (cont.-1)
Prolog character set
•
- Uppercase letters, A-Z
A prolog program (.pl)
- consists of a sequence of clauses
- Lowercase letters, a-z
- Digits, 0-9
- Symbols, + - * / \ ^ , . ~ : . ? @ # $ &
•
•
Term
- fact
predicate(arg1, arg2, … argN).
- rule
head :- body.
- query (or goal)
- a single data structure as the foundation of the language
- can be integer,
atom (beginning with a lowercase, or quoted by ‘),
variable (beginning with a uppercase),
structure (complex term, functor(arg1, arg2))
e.g.
may
date(Day, june, 2002)
20070524 Prolog
A clause is either
5
20070524 Prolog
6
1
Some Basic Concepts (cont.-2)
Some Basic Concepts (cont.-3)
• Query
先點選執行 C:\Program Files\pl\bin\plwin.exe
- If there is a fact that matches the goal, then the query
succeeds and responds 'yes’ otherwise the query fails and
responds with 'no.‘
room(kitchen).
room('dinning room').
room(cellar).
?- room( kitchen ).
Yes
?- room( teacher ).
No
按File/Consult 選桌面/prologrun/room.pl
?- room( X ).
X=kitchen ;
X=‘dinning room’ ;
X=cellar ;
‘No' means there are no more answers
No
20070524 Prolog
7
20070524 Prolog
8
Some Basic Concepts (cont.-4)
Some Basic Concepts (cont.-5)
[yyy] or [‘yyy’]
[swi(‘xxx/yyy’)]
表示 File/Consult 選桌面/prologrun/yyy.pl
20070524 Prolog
表示 File/Consult 選 C:/program files/pl/xxx/yyy.pl
9
20070524 Prolog
Some Basic Concepts (cont.-6)
Some Basic Concepts (cont.-7)
•
location(apple, kitchen).
location(crackers, kitchen).
location(boiler, kitchen).
location(banana, classroom).
List
- a collection of terms
- denoted by square brackets with the terms separated
by commas
- or denoted by [X | Y]
X is the first element of the list, called the head.
Y is the list of remaining elements, called the tail.
e.g.
[a, b, c]
[a | [ b, c ] ]
[a | [ b | [ c ] ] ]
5 different representations
[a | [ b | [ c | [ ] ] ] ]
[a, b | [c ] ]
edible(apple).
edible(banana).
edible(crackers).
?- location(X, kitchen), edible(X).
X = apple ;
X = crackers ;
No
20070524 Prolog
10
11
20070524 Prolog
12
2
Some Basic Concepts (cont.-8)
Some Basic Concepts (cont.-9)
member(X,[X|R]).
% X is a member of a list whose first
element is X.
member(X,[Y|R]) :- member(X,R). % X is a member of a list
% whose tail is R if X is a member of R.
?- member(2,[1,2,3]).
Yes
?- member(X,[1,2,3]).
X=1;
X=2;
X=3;
No
'car' refers to the first element of a list,
'cdr' refers to the tail or rest of the list, and
'cons' is the list constructor.
car([X|Y],X).
cdr([X|Y],Y).
cons(X,R,[X|R]).
20070524 Prolog
13
A Good Prolog Tutorial
14
Prolog Example 1
The facts about the Dutch Royal Family
%?- parent(beat,alex).
%?- parent(beat,X).
%?- parent(jul,X).
mother(wil,jul).
mother(jul,beat). mother(jul,chris).
mother(beat,friso). father(hend,jul). father(bern,beat).
father(bern,chris). father(claus,friso).
parent(X, Y) :- mother(X,Y).
parent(X, Y) :- father(X,Y).
http://www.intranet.csupomona.edu/~jrfisher/www/
prolog_tutorial/pt_framer.html
20070524 Prolog
20070524 Prolog
15
20070524 Prolog
16
Prolog Example 2
Prolog Example 1 (cont.)
% sum(L,N) which succeeds where N is the sum of the
integers
% in the list L. (Assume that list L will be given,
% and that this list contains only integers).
%
% ?- sum([1,2,3,4], N).
sum([],0).
sum([H|T],N) :sum(T,N1),
N is N1 + H.
20070524 Prolog
17
20070524 Prolog
18
3
Prolog Example 2 (cont.)
Prolog Example 3
nat(0).
nat(N) :- nat(M), N is M + 1.
?- nat(N), write(N), nl, fail.
0
1
% Successive numbers are generated
2
% by backtracking
3
4
….
20070524 Prolog
19
Prolog Example 4
20070524 Prolog
20
Prolog Example 4 (cont.)
% Hanoi tower
% ?- move(3,left,right,center).
move(1,X,Y,_) :write('Move top disk from '),
write(X),
write(' to '),
write(Y),
nl.
move(N,X,Y,Z) :N>1,
M is N-1,
move(M,X,Z,Y),
move(1,X,Y,_),
move(M,Z,Y,X).
20070524 Prolog
21
Prolog Example 5
20070524 Prolog
22
Prolog Example 5 (cont.-1)
factorial(0,1).
factorial(N,F) :N>0,
N1 is N-1,
factorial(N1,F1),
F is N * F1.
?- factorial(3,W).
W=6
Yes
?- factorial(3,6).
Yes
?- factorial(5,2).
No
20070524 Prolog
23
20070524 Prolog
24
4
Prolog Example 5 (cont.-2)
Map Coloring
http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/2_2.html
20070524 Prolog
25
Map Coloring (cont.-1)
adjacent(1,2).
adjacent(3,1).
adjacent(1,5).
adjacent(3,2).
adjacent(3,4).
adjacent(5,4).
adjacent(2,1).
adjacent(1,4).
adjacent(5,1).
adjacent(2,4).
adjacent(4,3).
conflict(R1,R2,Coloring) :adjacent(R1,R2),
color(R1,Color,Coloring),
color(R2,Color,Coloring).
?- conflict(R1,R2,b).
R1 = 2
R2 = 4
Yes
?- conflict(R1,R2,b),color(R1,C,b).
R1 = 2
R2 = 4
C = blue
Yes
?- adjacent(2,3).
Yes
?- adjacent(5,3).
No
?- adjacent(3,R).
R=1;
R=2;
R=4;
No
27
Who owns the zebra
20070524 Prolog
28
Who owns the zebra(cont.-1)
There are five houses.
Each house has it's own unique color.
All homeowners are of different nationalities.
They all have different pets.
They all drink different drinks.
They all smoke different cigarettes.
The Englishman lives in the red House.
The Swede has a dog.
The Dane drinks Tea.
The green house is on the left side of the white house.
In the green house they drink coffee.
The man who smokes Pall Mall has birds.
In the yellow house they smoke Dunhill.
In the middle house they drink milk.
The Norwegian lives in the First house.
The man who smokes Blend lives next door to the house with cats.
In the house next to the house with a horse, they smoke Dunhill.
The man who smokes Blue Master drinks beer.
The German smokes Prince.
The Norwegian lives next to the blue house.
They drink water in the house next to where they smoke Blend.
Who owns the Zebra?
20070524 Prolog
26
Map Coloring (cont.-2)
adjacent(1,3).
adjacent(4,1).
adjacent(2,3).
adjacent(4,2).
adjacent(4,5).
20070524 Prolog
20070524 Prolog
next_to(X, Y, List) :- is_right(X, Y, List).
next_to(X, Y, List) :- is_right(Y, X, List).
is_right(R, L, [L | [R | _]]).
is_right(R, L, [_ | Rest]) :- is_right(R, L, Rest).
•
house(C, N, P, D, S) predicate
C: the color of the house
N: the nationality of the resident
P: the resident's pet
D: the resident's drink
S: the resident's cigarette
•
There are five houses
•
The Englishman lives in the red House.
Street = [_House1, _House2, _House3, _House4, _House5],
member(house(red, englishman, _, _, _), Street),
29
20070524 Prolog
30
5
Who owns the zebra(cont.-2)
?- owns_zebra(Street, Who).
Street = [house(yellow, norwegian, fox, _G473, kools),
house(blue, ukrainian, horse, tea, chesterfields),
house(red, englishman, snails, milk, old_gold),
house(ivory, spaniard, dog, orange_juice, lucky_strike),
house(green, japanese, zebra, coffee, parliaments)]
Who = japanese
Yes
20070524 Prolog
31
6
Download