COP4020 Programming Languages Logic Programming

advertisement

COP4020

Programming

Languages

Logic Programming

Prof. Xin Yuan

Topics

More on Prolob

Prolog by examples

4/12/2020 COP4020 Spring 2013

2

Control Features

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)

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

4/12/2020 COP4020 Spring 2014

3

Control Features

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

G

1

-> G

2

; G

3 forms an if-then-else: if G true is a true goal and acts like a no-op

1 failed then G

2 else G

3

 repeat always succeeds (for looping)

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, !.

4/12/2020 COP4020 Spring 2014

4

Meta-Logical

Meta-logical predicates perform operations that require reasoning about terms

 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,A1,A2,…,An] true if X a functor F with arguments

Impact:

 Pure logic has no ordering constraints, e.g. X “or” Y = Y “or” X

 X=3, var(X).

 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

4/12/2020 COP4020 Spring 2014

5

Example 1 fact(n) = 1*2*…*n

 fact(1, 1).

fact(N, X) :- M is N-1, fact(M, Y), X is Y*N.

4/12/2020 COP4020 Spring 2014

6

Example 2

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

X=6

4/12/2020 COP4020 Spring 2014

7

Example 3

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

X=6 sum([], 0).

sum([X|T], Y) :- sum(T, Y1), Y is X + Y1.

4/12/2020 COP4020 Spring 2014

8

Example 4

Generate a list of n copies of x

Fill(3, x, Y)

Y=[x,x,x]

4/12/2020 COP4020 Spring 2014

9

Example 4

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

4/12/2020 COP4020 Spring 2014

10

Example 5

 Replace x with y in list xs subst(3, 0, [8,2,3,4,3,5], X).

X=[8,2,0,4,0,5]

4/12/2020 COP4020 Spring 2014

11

Example 5

 Replace x with y in list xs subst(3, 0, [8,2,3,4,3,5], X).

X=[8,2,0,4,0,5] replace1(_, _, [], []).

replace1(X, Y, [X|T], [Y|T1]) :- replace1(X, Y, T, T1).

replace1(X, Y, [Z|T], [Z|T1]) :- replace1(X, Y, T, T1).

4/12/2020 COP4020 Spring 2014

12

Example 6

 Search on binary search tree, where [] are leaves and [ val, left, right ] is a node bsearch[4, [3, [], [4, [],[]]], X).

X=1

4/12/2020 COP4020 Spring 2014

13

Example 6

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

14

4/12/2020 COP4020 Spring 2014

Example 7

 Binary tree insertion, where [] are leaves and [ val, left, right ] is a node binsert(X, [], [X, [], []]).

binsert(X, [X | T], [X | T]).

binsert(X, [Y, L, R], [Y, L1, R]) :- X < Y, binsert(X, L, L1).

binsert(X, [Y, L, R], [Y, L, R1]) :- X > Y, bsearch(X, R, R1).

4/12/2020 COP4020 Spring 2014

15

Example 8

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

4))

Prolog removelast([_], []).

removelast([X|T], [X|T1]) :- removelast(T, T1).

append(X, [], [X]).

append(X, [Y|T], [Y|T1]):- append(X, T, T1).

genlist([], [[]]).

genlist(X, Y) :- removelast(X, T), genlist(T, Y1), append(X, Y1, Y).

16

4/12/2020 COP4020 Spring 2014

Example 9

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

4/12/2020 COP4020 Spring 2014

17

Example 9

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

4/12/2020 COP4020 Spring 2014

18

Example 10

 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], T1):- remove(X, T, T1).

remove(X, [Y|T], [Y|T1]) :- remove(X, T, T1).

4/12/2020 COP4020 Spring 2014

19

Example 11

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|T1]) :- X > Y, insertsorted(X, T, T1).

20

4/12/2020 COP4020 Spring 2014

Example 12

 Insertion sort isort([], []).

isort([X|T], Y):- isort(T, T1), insertsorted(X, T1, Y).

4/12/2020 COP4020 Spring 2014

21

Download