Prolog tutorial
-
Upload
garapatiavinash -
Category
Documents
-
view
130 -
download
0
description
Transcript of Prolog tutorial
for CS 561, Spring 2012
Instructors: Profs. Liang Huang and Kenji Sagae{liangh,sagae}@usc
TA: Harris Chiu <chichiu@usc>
based on J. R. Fisher’s tutorial (Pomona College):http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/contents.html#2
Course Homepage:http://www-bcf.usc.edu/~liangh/cs561/
Prolog = Programming by Logic• Prolog is different from all languages you’ve learned
• Prolog is a Logic Programming (LP) Language
• you only need to specify the goals (what)
• but not the strategy to reach this goal (how)
• Prolog figures it out for you automatically!
• this is called “declarative programming” (alone with functional programming, FP)
• C/C++/Java/Python are Imperative (IP) Languages
• you specify how to reach some goal (instructions)
• but leaving the real goal implicit (in comments)
• LP/FP is cleaner, safer, prettier, while IP is dirtier but faster 2
Predicates, Facts, Rules, Variables
3
cat(tom).
?- cat(tom).true.
?- cat(X).X = tom.
animal(X):- cat(X).
?- animal(tom).true.
?- animal(X).X = tom
(atomic) factcat() is a predicatetom is a constant
yes/no question (no variable)
whack questionsX is an (unbound) variable
inference rule
yes/no question (no variable)
wh questionsX is an (unbound) variable
N.B.:variable names
start with capitalized letter:e.g. Who, What
Family Example
4
mother_child(trude, sally). father_child(tom, sally).father_child(tom, erica).father_child(mike, tom). sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y). parent_child(X, Y) :- father_child(X, Y).parent_child(X, Y) :- mother_child(X, Y).
?- sibling(sally, erica).true.
?- sibling(X, Y).X = Y, Y = sally ;X = sally,Y = erica ;X = erica,Y = sally ;X = Y, Y = erica ;X = Y, Y = tom ;X = Y, Y = sally.
(share a parent)
press “;” for more answers and return to stop.note that X is his/her sibling! how to fix that?
sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y), X \== Y.
?- sibling(X, X).false.
Installation
• Two versions available
• SWI-Prolog -- powerful
• GNU Prolog (gprolog) -- lightweight
• We use SWI-Prolog http://www.swi-prolog.org/
• SWI-Prolog is much better than gprolog
• arbitrary precision arithmetic
• dynamics, memoization, etc.
• most Prolog tutorials and AI textbooks assume SWI
• please install SWI-Prolog on your own computer!5
The Interpreter
6
[<lhuang@Mac OS X:~>] swipl% library(swi_hooks) compiled into pce_swi_hooks 0.01 sec, 3,928 bytesWelcome to SWI-Prolog (Multi-threaded, 64 bits, Version 5.10.4)Copyright (c) 1990-2011 University of Amsterdam, VU AmsterdamSWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,and you are welcome to redistribute it under certain conditions.Please visit http://www.swi-prolog.org for details.
For help, use ?- help(Topic). or ?- apropos(Word).
?- [user].
Negation
7
?- [user].bachelor(P) :- male(P), not(married(P)). |: |: male(henry). |: male(tom). |: |: married(tom). |: % user://1 compiled 0.00 sec, 1,704 bytestrue.
?- bachelor(henry).true.
?- bachelor(tom).false.
?- bachelor(Who).Who = henry .
bachelor(P) :- male(P), not(married(P)).
male(henry). male(tom).
married(tom).
Factorial (N!)
8
fact(0,1).
fact(N,F) :- N>0, N1 is N-1, fact(N1,F1), F is N * F1.
?- f(5,X).X = 120 .
?- f(5,100).false.
?- f(100,X).X = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 .
How does it work
9
fact(0,1).
fact(N,F) :- N>0, N1 is N-1, fact(N1,F1), F is N * F1.
?- trace. % The debugger will first creep -- showing everything (trace). yes [trace] ?- fact(3,X). (1) 0 Call: fact(3,_8140) ? (1) 1 Head [2]: fact(3,_8140) ? (2) 1 Call (built-in): 3>0 ? (2) 1 Done (built-in): 3>0 ? (3) 1 Call (built-in): _8256 is 3-1 ? (3) 1 Done (built-in): 2 is 3-1 ? (4) 1 Call: fact(2, _8270) ? ... (1) 0 Exit: fact(3,6) ? X=6 [trace] ?- notrace. % The debugger is switched off yes
Loading Programs
10
[<lhuang@Mac OS X:~>] swipl?- consult('fact.pro').% fact.pro compiled 0.00 sec, 1,112 bytestrue.
?- fact(5,X).X = 120 .
?- [user].fact(7,8).Warning: user://1:38: Redefined static procedure fact/2|: % user://1 compiled 0.00 sec, 120 bytestrue.
?- fact(7,8).true.
?- fact(0,1).false.
?- ['fact.pro'].% fact.pro compiled 0.00 sec, 432 bytestrue.
Editing Programs in SWIPL
11
?- reconsult('p.pl').
Fibonacci
12
fib(0, 0).
fib(1, 1).
fib(X, Y) :-X > 1, X2 is X – 2, fib(X2, Y2), X1 is X – 1, fib(X1, Y1), Y is Y1 + Y2.
Fibonacci
13
fib(0, 0).
fib(1, 1).
fib(X, Y) :- X > 1, X2 is X – 2, fib(X2, Y2), X1 is X – 1, fib(X1, Y1), Y is Y1 + Y2.
First AI Program: Map Colorings
14
Graph Representation: V1
15
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).
Graph Representation: V1
16
?- adjacent(2,3). true. ?- adjacent(5,3). false. ?- adjacent(3,R). R = 1 ; R = 2 ; R = 4 ; false.
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).
Graph Representation: V2
17
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(X, Y) :- adjacent(Y, X).
Graph Representation: V2
18
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(X, Y) :- adjacent(Y, X).
?- adjacent(3,R). R = 4 ;R = 1 ;R = 2 ;R = 4 ;R = 1 ;R = 2 ;...
different order (previously 1-2-4);and infinitely many
solutions!
Graph Representation: V2
19
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(X, Y) :- adjacent(Y, X).
why infinite loop?here is the derivation tree:
adj(3, R): R=4; adj(R, 3): R=1; R=2; adj(3, R): R=4; adj(R, 3): ...
Colorings
20
color(1,red,a). color(1,red,b). color(2,blue,a). color(2,blue,b). color(3,green,a). color(3,green,b). color(4,yellow,a). color(4,blue,b). color(5,blue,a). color(5,green,b).
Conflict
21
conflict(Coloring) :- adjacent(X,Y), color(X,Color,Coloring), color(Y,Color,Coloring).
Conflict (using adjacency V1)
22
?- conflict(a). false. ?- conflict(b).true ;true ;false.?- conflict(Which). Which = b ;Which = b ;false.
conflict(Coloring) :- adjacent(X,Y), color(X,Color,Coloring), color(Y,Color,Coloring).
two conflicts in b
Another Conflict Definition
23
conflict(R1,R2,Coloring) :- adjacent(R1,R2), color(R1,Color,Coloring), color(R2,Color,Coloring).
conflict(Coloring) :- adjacent(X,Y), color(X,Color,Coloring), color(Y,Color,Coloring).
overloading is fine in Prolog
Another Conflict Definition
24
conflict(R1,R2,Coloring) :- adjacent(R1,R2), color(R1,Color,Coloring), color(R2,Color,Coloring).
?- conflict(R1,R2,b). R1 = 2 R2 = 4 ?- conflict(R1,R2,b),color(R1,C,b). R1 = 2 R2 = 4 C = blue
Conflict (using adjacency V2)
25
?- conflict(a). ... (waits for ever) ?- conflict(b).true ;true ;true ;... (infinite loop)
conflict(Coloring) :- adjacent(X,Y), color(X,Color,Coloring), color(Y,Color,Coloring).
How to fix adjacency V2?
26
conflict(Coloring) :- adjacent(X,Y), color(X,Color,Coloring), color(Y,Color,Coloring).
edge(1,2). edge(1,3). edge(1,4). edge(1,5). edge(2,3). edge(2,4). edge(3,4). edge(4,5).
adjacent(X,Y):- edge(Y,X).adjacent(X,Y):- edge(X,Y).