Professional Documents
Culture Documents
Prolog
Prolog
Prolog
History
Kowalski: late 60s Logician who showed logical proof can support computation. Colmerauer: early 70s Developed early version of Prolog for natural language processing, mainly multiple parses. Warren: mid 70s First version of Prolog that was efficient.
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.
SWI-Prolog Notes
Free! Down Loadable To load a file:
consult( C:\\kibler\\prolog\\test).
For help:
help(predicate-name).
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.
Example
Rules
likes(john,X) :- likes(X,wine). % :- = if likes(john,X):- female(X), likes(X,john).
Query: ? - likes(john,Y).
Y = bill ; no.
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.
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.
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.
membership definition
member(H,[H|T]). % base case first. Why? member(H,[_|T]):- member(H,T). Use it.
Member Tests
?- member(3,X).
X = [3| _G109]. variable X= [_G11,3| _]. % _G.., system generated
% etc.
?- member(X,Y).
X = _G131, Y= [_G131|, _G321].
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] ;
Limitations
2nd order: Cant ask what is relationship between heart and lungs? Probabilities: What is likelihood of fire destroying Julian?