Download presentation

Presentation is loading. Please wait.

Published byEllie Wafer Modified about 1 year ago

1
Prolog The language of logic

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. Warren: mid 70’s First version of Prolog that was efficient.

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.

4
Strawberry Prolog

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.

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

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

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

9
A different program for max

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

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

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

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

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

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

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

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google