20070524 Prolog 1
Introduction to Prolog
20070524 Prolog 2
History of Prolog
•
PROgramming in LOGic - based on logic
- a popular artificial intelligence language - Horn subset of first-order Predicate Logic
•
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 3
•
SWI-Prolog 5.6.34
(2007 released) http://www.swi-prolog.org/•
SWI-Prolog-Editor 3.03
http://lernen.bildung.hessen.de/informatik/swiprolog/indexe.htm
•
Visual Prolog 7.0
(March 26, 2007 released) http://www.visual-prolog.com/Successor of Turbo Prolog (by Borland)
•
SICStus
Prolog 4.0.1(May 17, 2007 released) http://www.sics.se/isl/sicstuswww/site/index.html•
GNU GProlog 1.3.0
http://www.gprolog.org/
•
……
Some Available Prolog Systems
20070524 Prolog 4
Screenshot
Some Basic Concepts
• Prolog character set - Uppercase letters, A-Z - Lowercase letters, a-z - Digits, 0-9
- Symbols, + - * / \ ^ , . ~ : . ? @ # $ &
• Term
- 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)
Some Basic Concepts
(cont.-1)•
A prolog program (.pl)
- consists of a sequence of clauses
•
A clause is either
- fact predicate(arg1, arg2, … argN).
- rule head :- body.
- query (or goal)
20070524 Prolog 7
• Query
- If there is a fact that matches the goal, then the query succeeds and responds 'yes’ otherwise the query fails and responds with 'no.‘
Some Basic Concepts
(cont.-2)room(kitchen). room('dinning room').
room(cellar).
?- room( kitchen ).
Yes
?- room( teacher ).
No
?- room( X ).
X=kitchen ; X=‘dinning room’ ; X=cellar ;
No ‘No' means there are no more answers
20070524 Prolog 8
Some Basic Concepts
(cont.-3)按File/Consult 選桌面/prologrun/room.pl
先點選執行 C:\Program Files\pl\bin\plwin.exe20070524 Prolog 9
Some Basic Concepts
(cont.-4)[yyy] or [‘yyy’]
表示 File/Consult 選桌面/prologrun/yyy.pl
20070524 Prolog 10
Some Basic Concepts
(cont.-5)[swi(‘xxx/yyy’)]
表示 File/Consult 選 C:/program files/pl/xxx/yyy.pl
20070524 Prolog 11
Some Basic Concepts
(cont.-6)location(apple, kitchen).
location(crackers, kitchen).
location(boiler, kitchen).
location(banana, classroom).
?- location(X, kitchen), edible(X).
X = apple ; X = crackers ; No
edible(apple).
edible(banana).
edible(crackers).
20070524 Prolog 12
Some Basic Concepts
(cont.-7)•
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 ] ]
20070524 Prolog 13
Some Basic Concepts
(cont.-8)'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 14
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
20070524 Prolog 15
A Good Prolog Tutorial
http://www.intranet.csupomona.edu/~jrfisher/www/
prolog_tutorial/pt_framer.html
20070524 Prolog 16
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).
Prolog Example 1
(cont.)Prolog Example 2
% 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 19
Prolog Example 2
(cont.)20070524 Prolog 20
Prolog Example 3
nat(0).
nat(N) :- nat(M), N is M + 1.
?- nat(N), write(N), nl, fail.
0 1 2 3 4
….
% Successive numbers are generated
% by backtracking
20070524 Prolog 21
Prolog Example 4
% 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 22
Prolog Example 4
(cont.)20070524 Prolog 23
Prolog Example 5
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 24
Prolog Example 5
(cont.-1)20070524 Prolog 25
Prolog Example 5
(cont.-2)http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/2_2.html
20070524 Prolog 26
Map Coloring
20070524 Prolog 27
Map Coloring
(cont.-1)adjacent(1,2). adjacent(2,1). adjacent(1,3).
adjacent(3,1). adjacent(1,4). adjacent(4,1).
adjacent(1,5). adjacent(5,1). adjacent(2,3).
adjacent(3,2). adjacent(2,4). adjacent(4,2).
adjacent(3,4). adjacent(4,3). adjacent(4,5).
adjacent(5,4).
?- adjacent(2,3).
Yes
?- adjacent(5,3).
No
?- adjacent(3,R).
R = 1 ; R = 2 ; R = 4 ; No
20070524 Prolog 28
Map Coloring
(cont.-2) 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
Who owns the zebra
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?
Who owns the zebra
(cont.-1)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
Street = [_House1, _House2, _House3, _House4, _House5],
•
The Englishman lives in the red House.
member(house(red, englishman, _, _, _), Street),
20070524 Prolog 31
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