lecture 9b: prolog heshaam faili [email protected] university of tehran the language of logic...
TRANSCRIPT
![Page 1: Lecture 9b: Prolog Heshaam Faili hfaili@ece.ut.ac.ir University of Tehran The language of logic Concepts Examples](https://reader036.vdocuments.us/reader036/viewer/2022070411/56649f3d5503460f94c5ca6f/html5/thumbnails/1.jpg)
Lecture 9b: Prolog
Heshaam [email protected]
rUniversity of
Tehran
The language of logic
Concepts
Examples
![Page 2: Lecture 9b: Prolog Heshaam Faili hfaili@ece.ut.ac.ir University of Tehran The language of logic Concepts Examples](https://reader036.vdocuments.us/reader036/viewer/2022070411/56649f3d5503460f94c5ca6f/html5/thumbnails/2.jpg)
2
History
Kowalski: late 60’s Logician who showed logical proof can support computation.
Colmerauer: early 70’s Developed early version of Prolog for natural language processing, mainly multiple parses.
Warren: mid 70’s First version of Prolog that was efficient.
![Page 3: Lecture 9b: Prolog Heshaam Faili hfaili@ece.ut.ac.ir University of Tehran The language of logic Concepts Examples](https://reader036.vdocuments.us/reader036/viewer/2022070411/56649f3d5503460f94c5ca6f/html5/thumbnails/3.jpg)
3
Characteristics
Prolog approximates first-order logic. Every program is a set of Horn clauses. Inference is by resolution. Search is by backtracking with
unification. Basic data structure is term or tree. Variables are unknowns not locations. Prolog does not distinguish between
inputs and outputs. It solves relations/predicates.
![Page 4: Lecture 9b: Prolog Heshaam Faili hfaili@ece.ut.ac.ir University of Tehran The language of logic Concepts Examples](https://reader036.vdocuments.us/reader036/viewer/2022070411/56649f3d5503460f94c5ca6f/html5/thumbnails/4.jpg)
4
SWI-Prolog Notes Free! Down Loadable To load a file:
consult( ‘C:\\prolog\\test’). For help:
help(predicate-name). “ ; “ will give you next solution. ‘%’ comment listing(member) will give definition.
![Page 5: Lecture 9b: Prolog Heshaam Faili hfaili@ece.ut.ac.ir University of Tehran The language of logic Concepts Examples](https://reader036.vdocuments.us/reader036/viewer/2022070411/56649f3d5503460f94c5ca6f/html5/thumbnails/5.jpg)
5
Example
Facts: () likes(john,mary). likes(john,X). % Variables begin with
capital Queries
?- likes(X,Y). X=john, y=Mary. % hit “;” for more ?- likes(X,X). X=john.
![Page 6: Lecture 9b: Prolog Heshaam Faili hfaili@ece.ut.ac.ir University of Tehran The language of logic Concepts Examples](https://reader036.vdocuments.us/reader036/viewer/2022070411/56649f3d5503460f94c5ca6f/html5/thumbnails/6.jpg)
6
Example
Rules likes(john,X) :- likes(X,wine). % :- = if likes(john,X):- human(X), likes(X,john).
Note: variables are dummy. Standardized apart
Some Facts: likes(bill,wine). human(mary). human(sue).
Query: ? - likes(john,Y). Y = bill ; no.
![Page 7: Lecture 9b: Prolog Heshaam Faili hfaili@ece.ut.ac.ir University of Tehran The language of logic Concepts Examples](https://reader036.vdocuments.us/reader036/viewer/2022070411/56649f3d5503460f94c5ca6f/html5/thumbnails/7.jpg)
7
Family
father(a,b). father(e,d). mother(c,b). mother(d,f). parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). grandfather(X,Y):-
father(X,Z),parent(Z,Y).% Do your own for practice.
![Page 8: Lecture 9b: Prolog Heshaam Faili hfaili@ece.ut.ac.ir University of Tehran The language of logic Concepts Examples](https://reader036.vdocuments.us/reader036/viewer/2022070411/56649f3d5503460f94c5ca6f/html5/thumbnails/8.jpg)
8
Informal Summary
Program is facts + rules. (horn clauses).
Query = conjunct of predicates. First set of bindings for variables
that solve query are reported. If none, then Prolog returns no.
Use “;” to get other solutions. Can be viewed as constraint
satisfaction program.
![Page 9: Lecture 9b: Prolog Heshaam Faili hfaili@ece.ut.ac.ir University of Tehran The language of logic Concepts Examples](https://reader036.vdocuments.us/reader036/viewer/2022070411/56649f3d5503460f94c5ca6f/html5/thumbnails/9.jpg)
9
MapColoring
color(r). color(g). color(b). colormap(C1,C2,C3):-
color(C1),color(C2),color(C3), C1\==C2,C1\==C3, C2\==C3.
Query: colormap(X,Y,Z). X = r, Y= g, Z=b.
![Page 10: Lecture 9b: Prolog Heshaam Faili hfaili@ece.ut.ac.ir University of Tehran The language of logic Concepts Examples](https://reader036.vdocuments.us/reader036/viewer/2022070411/56649f3d5503460f94c5ca6f/html5/thumbnails/10.jpg)
10
Unification: (matching) terms
Two terms UNIFY if there is a common substitution for all variables which makes them identical.
f(g(X),Y) = f(Z,Z). % = cheap unification X = _G225, Y=g(_G225).
Look at parse tree for each term. variables match variable matches anything (set the binding) function symbols only match identical function
symbols.
![Page 11: Lecture 9b: Prolog Heshaam Faili hfaili@ece.ut.ac.ir University of Tehran The language of logic Concepts Examples](https://reader036.vdocuments.us/reader036/viewer/2022070411/56649f3d5503460f94c5ca6f/html5/thumbnails/11.jpg)
11
Satisfiability: uses unification
sat(true). % base case sat(not(false)). % base case sat(or(X,Y)):- sat(X). sat(or(X,Y)):-sat(Y). sat(and(X,Y)):-sat(X),sat(Y).
test1(X,Y):- sat(and(not(X),X)). test2(X,Y):- sat(and(X,not(Y))).
![Page 12: Lecture 9b: Prolog Heshaam Faili hfaili@ece.ut.ac.ir University of Tehran The language of logic Concepts Examples](https://reader036.vdocuments.us/reader036/viewer/2022070411/56649f3d5503460f94c5ca6f/html5/thumbnails/12.jpg)
12
List Operator [H |T]
[a,b,c] is a list in Prolog. [H|T] = [a,b,c] results in
H = a i.e. the head of list T = [b,c] i.e. the tail of the list.
membership definition member(H,[H|T]). % base case first.
Why? member(H,[_|T]) :- member(H,T). Use it.
![Page 13: Lecture 9b: Prolog Heshaam Faili hfaili@ece.ut.ac.ir University of Tehran The language of logic Concepts Examples](https://reader036.vdocuments.us/reader036/viewer/2022070411/56649f3d5503460f94c5ca6f/html5/thumbnails/13.jpg)
13
Member Tests
?- member(3,X). X = [3| _G109].
% _G.., system generated variable X= [_G11,3| _]. % etc.
?- member(X,Y). X = _G131, Y= [_G131|, _G321].
![Page 14: Lecture 9b: Prolog Heshaam Faili hfaili@ece.ut.ac.ir University of Tehran The language of logic Concepts Examples](https://reader036.vdocuments.us/reader036/viewer/2022070411/56649f3d5503460f94c5ca6f/html5/thumbnails/14.jpg)
14
Permutation & Insert
insert(X,L, [X|L]). insert(X,[H|T],[H|T1]):- insert(X,T,T1).
perm([],[]). perm([H|T],P):-
perm(T,T1),insert(H,T1,P).
![Page 15: Lecture 9b: Prolog Heshaam Faili hfaili@ece.ut.ac.ir University of Tehran The language of logic Concepts Examples](https://reader036.vdocuments.us/reader036/viewer/2022070411/56649f3d5503460f94c5ca6f/html5/thumbnails/15.jpg)
15
DFS
% solve(goal, solution Path)% s(state, successor-state)dfs(N,[N]) :- goal(N).dfs(N,[N|Sol1]):- s(N,N1), dfs(N1,Sol1).
s(a,b). s(a,c). s(b,d). s(b,e). s(c,f).s(c,g). s(d,h). s(e,i). s(e,j). s(f,k).goal(i). goal(f).
?- dfs(a,N). N = [a, b, e, i] ; N = [a, c, f] ;
![Page 16: Lecture 9b: Prolog Heshaam Faili hfaili@ece.ut.ac.ir University of Tehran The language of logic Concepts Examples](https://reader036.vdocuments.us/reader036/viewer/2022070411/56649f3d5503460f94c5ca6f/html5/thumbnails/16.jpg)
16
Cut !
P :- a, b. P :- c.
P :- a, !, b.P :- c.
P (ab)c
P (ab)(a c)
Use to ignore backtracking: performance issue
![Page 17: Lecture 9b: Prolog Heshaam Faili hfaili@ece.ut.ac.ir University of Tehran The language of logic Concepts Examples](https://reader036.vdocuments.us/reader036/viewer/2022070411/56649f3d5503460f94c5ca6f/html5/thumbnails/17.jpg)
17
MAX
MAX(X,Y,X) :- X>=Y.MAX(X,Y,Y).
?- MAX(3,2,X).
X=3 ; X = 2 !!!!!
MAX(X,Y,X) :- X>=Y, !.MAX(X,Y,Y).
![Page 18: Lecture 9b: Prolog Heshaam Faili hfaili@ece.ut.ac.ir University of Tehran The language of logic Concepts Examples](https://reader036.vdocuments.us/reader036/viewer/2022070411/56649f3d5503460f94c5ca6f/html5/thumbnails/18.jpg)
18
Factorial
factorial(0,1). factorial(N,F) :-
N>0, N1 is N-1, factorial(N1,F1), F is N * F1.
![Page 19: Lecture 9b: Prolog Heshaam Faili hfaili@ece.ut.ac.ir University of Tehran The language of logic Concepts Examples](https://reader036.vdocuments.us/reader036/viewer/2022070411/56649f3d5503460f94c5ca6f/html5/thumbnails/19.jpg)
19
Hanoi Tower 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).
?- move(3,left,right,center). Move top disk from left to right Move top disk from left to center Move top disk from right to center Move top disk from left to right Move top disk from center to left Move top disk from center to right Move top disk from left to right yes
![Page 20: Lecture 9b: Prolog Heshaam Faili hfaili@ece.ut.ac.ir University of Tehran The language of logic Concepts Examples](https://reader036.vdocuments.us/reader036/viewer/2022070411/56649f3d5503460f94c5ca6f/html5/thumbnails/20.jpg)
20
More examples
http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/contents.html
![Page 21: Lecture 9b: Prolog Heshaam Faili hfaili@ece.ut.ac.ir University of Tehran The language of logic Concepts Examples](https://reader036.vdocuments.us/reader036/viewer/2022070411/56649f3d5503460f94c5ca6f/html5/thumbnails/21.jpg)
21
Limitations
2nd order: Can’t ask what is relationship between heart and lungs?
Probabilities: What is likelihood of fire destroying Julian?
![Page 22: Lecture 9b: Prolog Heshaam Faili hfaili@ece.ut.ac.ir University of Tehran The language of logic Concepts Examples](https://reader036.vdocuments.us/reader036/viewer/2022070411/56649f3d5503460f94c5ca6f/html5/thumbnails/22.jpg)
22
?