Title: Prolog
1Prolog
2History
- Kowalski late 60s Logician who showed logical
proof can support computation. - Colmerauer early 70s Developed early version of
Prolog for natural language processing. - Warren mid 70s First version of Prolog that was
efficient.
3Characteristics
- 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.
4Strawberry Prolog
5Example
- Facts ()
- likes(john,mary).
- likes(john,X). Variables begin with capital
- Queries
- ?- likes(X,Y).
- Xjohn, yMary. hit for more
- ?- likes(X,X).
- Xjohn.
6Example
- Rules
- likes(john,X) - likes(X,wine). - if
- likes(john,X)- female(X), likes(X,john).
- Some Facts
- likes(bill,wine). female(mary). female(sue).
- Query ? - likes(john,Y).
- Y bill
- no.
7Family
- 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).
8Finding max in a list
max(X, XT)- geq(X,T). max(X, YT)- Y lt X,
max(X, T). geq(X, Y)- Y lt X. geq(X, HT)-
H lt X, geq(X, T). ?-geq(11, 8, 9, 11,
12). no ?-max(24, 23, 8, 24, 21, 14).
Yes ?-max(24, 23, 8, 24, 21, 14). Yes.
9A different program for max
10Merge sorting in Prolog
merge(X,,X). merge(, X, X). merge(XY,PQ,
XZ)- X lt P, merge(Y,PQ,Z). merge(XY,P
Q,PZ)- P lt X, merge(XY,Q,Z).
?-merge(3, 17, 11, 12, 20, 26, X), write(X),
nl.
11Merge sorting in Prolog split(X,Y, X,
Y). split(X,,X). split(X,YZ,XX1,
YX2)-split(Z, X1, X2). ?-split(12, 6, 7,
11, 9, X, Y), write(X), nl, write(Y), nl.
12Merge sorting in Prolog msort(,). msort(X,
X). msort(X,Y)- split(X, X1, X2),
msort(X1,Y1), msort(X2, Y2),
merge(Y1, Y2, Y). ?-msort(6, 12, 20, 7, 3, 21,
36, 14, -3, X), write(X), nl.
13Example involving an arithmetic operation Write
a Prolog program to compute the prefix sums of
elements in an array. Example ?-prefix_sum(1,
3, -4, 8, 2, X), write(X), nl. Output Compilin
g the file F\fall09\cs480fa09\programs\prefix_su
m 0 errors, 0 warnings. 1,4,0,8,10 Yes.
14Prolog function to implement prefix sum First
we create a function add that adds X to each
member of a list add(X,Y,Z)- Z is X
Y. add(X, HT, PQ)- P is X H,
add(X,T,Q). some test cases ?-add(3, -2, 12,
8, 3, X), write(X), nl. ?-add(3, -2, X),
write(X), nl.
15Prolog function to implement prefix sum We can
use add to write prefix_sum prefix_sum(X,X).
prefix_sum(HT, HT1)- prefix_sum(T, T2),
add(H, T2, T1), nl. ?-prefix_sum(2, 1, X),
write(X), nl. ?-prefix_sum(1, 3, -4, 8, 2, X),
write(X), nl.
16Permutation Insert
- Write a program to generate all the permutations
of a given sequence. - insert(X,L, XL).
- insert(X,HT,HT1)- insert(X,T,T1).
- ?-insert(1, 3, 2, 4, X), write(X), nl, fail.
- perm(,).
- perm(HT,P)-perm(T,T1),insert(H,T1,P).
- ?-perm(1, 2, 3, 4, X), write(X), nl, fail.