# Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.

## Presentation on theme: "Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed."— Presentation transcript:

Prolog The language of logic

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. Warren: mid 70’s 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.

Strawberry Prolog

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). Some Facts: –likes(bill,wine). female(mary). female(sue). 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).

Finding max in a list max(X, [X|T]):- geq(X,T). max(X, [Y|T]):- Y =< X, max(X, T). geq(X, [Y]):- Y =< X. geq(X, [H|T]):- H =< 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.

A different program for max

Merge sorting in Prolog merge(X,[],X). merge([], X, X). merge([X|Y],[P|Q],[X|Z]):- X =< P, merge(Y,[P|Q],Z). merge([X|Y],[P|Q],[P|Z]):- P =< X, merge([X|Y],Q,Z). % ?-merge([3, 17], [11, 12, 20, 26], X), write(X), nl.

Merge sorting in Prolog split([X,Y], [X], [Y]). split([X],[],[X]). split([X,Y|Z],[X|X1], [Y|X2]):-split(Z, X1, X2). %?-split([12, 6, 7, 11, 9], X, Y), write(X), nl, write(Y), nl.

Merge 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.

Example 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: Compiling the file: F:\fall09\cs480fa09\programs\prefix_sum 0 errors, 0 warnings. [1,4,0,8,10] Yes.

Prolog 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, [H|T], [P|Q]):- 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.

Prolog function to implement prefix sum We can use add to write prefix_sum: prefix_sum([X],[X]). prefix_sum([H|T], [H|T1]):- 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.

Permutation & Insert Write a program to generate all the permutations of a given sequence. insert(X,L, [X|L]). insert(X,[H|T],[H|T1]):- insert(X,T,T1). ?-insert(1, [3, 2, 4], X), write(X), nl, fail. perm([],[]). perm([H|T],P):-perm(T,T1),insert(H,T1,P). ?-perm([1, 2, 3, 4], X), write(X), nl, fail.

Download ppt "Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed."

Similar presentations