1 / 12

# COMP313A Programming Languages

COMP313A Programming Languages. Logic Programming (5). Lecture Outline. Cut operator Negation as Failure Control Information. Backtracking PROGRAM big(bear). %clause 1 big(elephant). %clause 2 small(cat). %clause 3 brown(bear). %clause 4 black(cat). %clause 5

## COMP313A Programming Languages

E N D

### Presentation Transcript

1. COMP313A Programming Languages Logic Programming (5)

2. Lecture Outline • Cut operator • Negation as Failure • Control Information

3. Backtracking PROGRAM big(bear). %clause 1 big(elephant). %clause 2 small(cat). %clause 3 brown(bear). %clause 4 black(cat). %clause 5 grey(elephant). %clause 6 dark(Z) :- black(Z). %clause 7 dark(Z) :- brown(Z). %clause 8 ?dark(X), big(X).

4. Controlling Backtracking move(1,8). move(1,6). move(2,9). move(2,7). move(3,8). move(3,4). move(4,3). move(4,9). move(6,1). move(6,7). move(7,2). move(7,6). move(8,1). move(8,3). move(9,2). move(9,4). 1 2 3 4 5 6 7 8 9 Knight’s Tour Problem

5. Controlling Backtracking… • Find all the two-move pathspath2(X,Y) :- move(X,Z), move(Z,Y). path2(1,W). ? W = 1 ? W = 3 ? W = 1 ? W = 7

6. Controlling Backtracking… path2(X,Y) :- move(X,Z), !, move(Z,Y). ? path2(1, W). ? W = 1 ? W = 3 ! is the cut operator

7. Controlling Backtracking… path2(X,Y) :- move(X,Z), move(Z,Y),!. ? path2(1, W).

8. Controlling Recursive Calls predicate path determines if there is a path between two nodes. path1(Z,Z,L). path1(X,Z,L) :- move(X,Y), not (member(Y,L)), path(Y, Z, [Y|L]). path2((Z,Z,L). path2(X,Z,L) :- move(X,Y), not (member(Y,L)), path(Y, Z, [Y|L]), ! . path1(1,3, []). path2(1,3, []).

9. Negation as Failure • Closed world assumption • The goal not (X) succeeds whenever the goal X fails! ?mother(amy, bob). ?not(parent(amy, bob)). Why did it fail?

10. Negation as Failure • Nonmonotonic reasoning – more information sometimes means fewer things can be proved human(bob). ?human(X). X = bob ?not human(X). no ?not (not human(X)). X = X why?

11. Control Information • Prolog uses a depth first search strateggy • Therefore order is important pred1(X,Z) :- parent(X,Z). pred1(X,Z) :- parent(X,Y), pred1(Y,Z). pred2(X,Z) :- pred2(Y,Z), parent(X,Y). pred2(X,Z) :- parent(X,Z).

12. parent(pam,bob). parent(tom,bob). parent(tom,liz). parent(bob, ann). parent(bob, pat). parent(pat, jim). ? pred(tom, pat).

More Related