COP 4020 Programming Languages Logic Programming Prof Xin

  • Slides: 21
Download presentation
COP 4020 Programming Languages Logic Programming Prof. Xin Yuan

COP 4020 Programming Languages Logic Programming Prof. Xin Yuan

Topics n n More on Prolob Prolog by examples 12/19/2021 COP 4020 Spring 2013

Topics n n More on Prolob Prolog by examples 12/19/2021 COP 4020 Spring 2013 2

Control Features n Prolog offers built-in constructs to support a form of control-flow +

Control Features n Prolog offers built-in constructs to support a form of control-flow + G negates a (sub-)goal G ¨ ! (cut) terminates backtracking for a predicate ¨ fail always fails (so to trigger backtracking) ¨ n Examples ¨ ¨ ¨ ? - + member(b, [a, b, c]). no ? - + member(b, []). yes We can (re)define: if(Cond, Then, Else) : - Cond, !, Then. if(Cond, Then, Else) : - Else. ? - if(true, X=a, X=b). X = a ; % type '; ' to try to find more solutions no ? - if(fail, X=a, X=b). X = b ; % type '; ' to try to find more solutions no 12/19/2021 COP 4020 Spring 2014 3

Control Features n Prolog offers built-in constructs to support a form of control-flow G

Control Features n Prolog offers built-in constructs to support a form of control-flow G 1 ; G 2 forms an “or”: try G 1 then G 2 if G 1 failed ¨ G 1 -> G 2 ; G 3 forms an if-then-else: if G 1 then G 2 else G 3 ¨ true is a true goal and acts like a no-op ¨ repeat always succeeds (for looping) ¨ n Examples: We can use an “or” instead of two clauses: rainy(C) : - ( C = rochester ; C = seattle ). ¨ Using an if-then-else: guess(X) : - ( X = heads -> format(“~s~n”, [“Guessed it!”]) ; format(“~s~n”, [“Sorry, no luck”]) ). ¨ Repeat drawing a random X until X=1 (and then cuts backtracking): do_until_one : - repeat, X is random(10), X = 1, !. ¨ 12/19/2021 COP 4020 Spring 2014 4

Meta-Logical n Meta-logical predicates perform operations that require reasoning about terms ¨ ¨ ¨

Meta-Logical n Meta-logical predicates perform operations that require reasoning about terms ¨ ¨ ¨ n X == Y true if X is identical to Y (vice versa X = Y) var(X) true if X is uninstantiated (vice versa nonvar(Y)) ground(X) true if X is a ground term, has no variables atom(X) true if X is an atom functor(X, F, N) true if X is a functor F with N arguments X =. . [F, A 1, A 2, …, An] true if X a functor F with arguments Impact: ¨ Pure logic has no ordering constraints, e. g. X “or” Y = Y “or” X n X=3, var(X), X=3. Meta-logical predicates are order sensitive and influence control ¨ Theoretically, cannot be expressed using predicate definitions with a finite number of clauses ¨ 12/19/2021 COP 4020 Spring 2014 5

Example 1 fact(n) = 1*2*…*n n n fact(1, 1). fact(N, X) : - M

Example 1 fact(n) = 1*2*…*n n n fact(1, 1). fact(N, X) : - M is N-1, fact(M, Y), X is Y*N. 12/19/2021 COP 4020 Spring 2014 6

Example 2 n n Sum the elements of a list sum([1, 2, 3], X).

Example 2 n n Sum the elements of a list sum([1, 2, 3], X). X=6 12/19/2021 COP 4020 Spring 2014 7

Example 3 n n Sum the elements of a list sum([1, 2, 3], X).

Example 3 n n Sum the elements of a list sum([1, 2, 3], X). X=6 sum([], 0). sum([X|T], Y) : - sum(T, Y 1), Y is X + Y 1. 12/19/2021 COP 4020 Spring 2014 8

Example 4 n n n Generate a list of n copies of x Fill(3,

Example 4 n n n Generate a list of n copies of x Fill(3, x, Y) Y=[x, x, x] 12/19/2021 COP 4020 Spring 2014 9

Example 4 n n n Generate a list of n copies of x Fill(3,

Example 4 n n n Generate a list of n copies of x Fill(3, x, Y) Y=[x, x, x] fill(0, _, []). fill(N, X, [X|T]): - M is N-1, fill(M, X, T). 12/19/2021 COP 4020 Spring 2014 10

Example 5 n Replace x with y in list xs subst(3, 0, [8, 2,

Example 5 n Replace x with y in list xs subst(3, 0, [8, 2, 3, 4, 3, 5], X). X=[8, 2, 0, 4, 0, 5] 12/19/2021 COP 4020 Spring 2014 11

Example 5 n Replace x with y in list xs subst(3, 0, [8, 2,

Example 5 n Replace x with y in list xs subst(3, 0, [8, 2, 3, 4, 3, 5], X). X=[8, 2, 0, 4, 0, 5] replace 1(_, _, []). replace 1(X, Y, [X|T], [Y|T 1]) : - replace 1(X, Y, T, T 1). replace 1(X, Y, [Z|T], [Z|T 1]) : - replace 1(X, Y, T, T 1). 12/19/2021 COP 4020 Spring 2014 12

Example 6 n Search on binary search tree, where [] are leaves and [val,

Example 6 n Search on binary search tree, where [] are leaves and [val, left, right] is a node bsearch[4, [3, [], [4, []]], X). X=1 12/19/2021 COP 4020 Spring 2014 13

Example 6 n Search on binary search tree, where [] are leaves and [val,

Example 6 n Search on binary search tree, where [] are leaves and [val, left, right] is a node bsearch[4, [3, [], [4, []]], X). X=1 bsearch(_, [], 0). bsearch(X, [X | _], 1). bsearch(X, [Y, L, _], Z) : - X < Y, bsearch(X, L, Z). bsearch(X, [Y, _, R], Z) : - X > Y, bsearch(X, R, Z). 12/19/2021 COP 4020 Spring 2014 14

Example 7 n Binary tree insertion, where [] are leaves and [val, left, right]

Example 7 n Binary tree insertion, where [] are leaves and [val, left, right] is a node binsert(X, [], []]). binsert(X, [X | T]). binsert(X, [Y, L, R], [Y, L 1, R]) : - X < Y, binsert(X, L, L 1). binsert(X, [Y, L, R], [Y, L, R 1]) : - X > Y, bsearch(X, R, R 1). 12/19/2021 COP 4020 Spring 2014 15

Example 8 n n Scheme: (genlist ‘(1 2 3 4)) => (() (1 2)

Example 8 n n Scheme: (genlist ‘(1 2 3 4)) => (() (1 2) (1 2 3 4)) Prolog removelast([_], []). removelast([X|T], [X|T 1]) : - removelast(T, T 1). append(X, [], [X]). append(X, [Y|T], [Y|T 1]): - append(X, T, T 1). genlist([], [[]]). genlist(X, Y) : - removelast(X, T), genlist(T, Y 1), append(X, Y 1, Y). 12/19/2021 COP 4020 Spring 2014 16

Example 9 n n Search an item in list, if found, return original list;

Example 9 n n Search an item in list, if found, return original list; otherwise, insert at the beginning of the list. Search: search(_, [], 0). search(X, [X|_], 1). search(X, [_|T], C) : - search(X, T, C). 12/19/2021 COP 4020 Spring 2014 17

Example 9 n n Search an item in list, if found, return original list;

Example 9 n n Search an item in list, if found, return original list; otherwise, insert at the beginning of the list. Searchinsert searchinsert(X, Y, Y): - search(X, Y, 1). searchinsert(X, Y, [X|Y]) : - search(X, Y, 0). 12/19/2021 COP 4020 Spring 2014 18

Example 10 n Remove all items in a list equal to x ¨ (remove

Example 10 n Remove all items in a list equal to x ¨ (remove 1 ‘(1 2 3 4 1 2)) => (2 3 4 2) remove(_, []). remove(X, [X|T], T 1): - remove(X, T, T 1). remove(X, [Y|T], [Y|T 1]) : - remove(X, T, T 1). 12/19/2021 COP 4020 Spring 2014 19

Example 11 n n Insert into a sorted list (insertsort 14 ‘(0 10 20

Example 11 n n Insert into a sorted list (insertsort 14 ‘(0 10 20 30 40)) => (0 10 14 20 30 40) insertsorted(X, [], [X]). insertsorted(X, [Y|T], [X, Y|T]) : - X =< Y. insertsorted(X, [Y|T], [Y|T 1]) : - X > Y, insertsorted(X, T, T 1). 12/19/2021 COP 4020 Spring 2014 20

Example 12 n Insertion sort isort([], []). isort([X|T], Y): - isort(T, T 1), insertsorted(X,

Example 12 n Insertion sort isort([], []). isort([X|T], Y): - isort(T, T 1), insertsorted(X, T 1, Y). 12/19/2021 COP 4020 Spring 2014 21