Prolog OR disjunction n n is same as

  • Slides: 23
Download presentation
Prolog OR (disjunction) n n “; ” is same as a logical OR It

Prolog OR (disjunction) n n “; ” is same as a logical OR It is also equivalent to using separate clauses. . . parent(X, Y) : - mother(X, Y). parent(X, Y) : - father(X, Y). SAME AS. . . parent(X, Y) : mother(X, Y) ; father(X, Y). n Although this saves writing some code, both versions are equivalent in efficiency. COSC 2 P 93 Prolog: Cut 1

Be careful with “; ”! n What is going on here? p(X) : (q(X,

Be careful with “; ”! n What is going on here? p(X) : (q(X, Y) ; (r(Y), (s(U, Y); a(U), b(U)) ; r(X)), t(X, Y); w(X) ; z(A), z(B), etc. . . n n Too many or’s can make programs very hard to debug (trace)! Rule of thumb: Don’t use or’s in order to reduce number of clauses. COSC 2 P 93 Prolog: Cut 2

Controlling Prolog execution n Prolog exhaustively searches the computation tree for solutions n n

Controlling Prolog execution n Prolog exhaustively searches the computation tree for solutions n n n There are lots of advantages of this scheme, most notably, searching for a solution, or multiple solutions, to a query. However, it can be expensive: n n If a goal fails, OR the user inputs ‘; ’ at the prompt, then backtracking reverts to the last place in which a clause was chosen, and tries the next. Sometimes there is only one solution, and it is a waste of time searching for others -- they don’t exist! Sometimes ‘failure’ after the first solution can take lots of time to infer Memory resources are used to contain the computation tree for backtracking. Prolog permits some user control of execution, in order to reduce backtracking. COSC 2 P 93 Prolog: Cut 3

1. If-then-else (If -> Then ; Else) n n n If goals in If

1. If-then-else (If -> Then ; Else) n n n If goals in If are true, then do Then; else do Else Once Then is executed, will go to Then or Else, but cannot backtrack from Then to Else, nor back to If. Backtracking will return multiple solutions in Then, and in Else. (If -> Then) n This is equivalent to: (If -> Then ; fail) COSC 2 P 93 Prolog: Cut 4

Example If-then-else n If N is prime, return ‘prime’, Else ‘notprime’. id. Num(N, prime)

Example If-then-else n If N is prime, return ‘prime’, Else ‘notprime’. id. Num(N, prime) : prime(N). id. Num(N, notprime) : + prime(N). n n Note that prime/N is called twice – with identical results! Prime testing might be very slow for large integers, so this is very wasteful. COSC 2 P 93 Prolog: Cut 5

Example if-then-else filter. Num(N, Ans) : (prime(N) -> Ans = prime ; Ans =

Example if-then-else filter. Num(N, Ans) : (prime(N) -> Ans = prime ; Ans = notprime). n Here, prime/1 is called once. No backtracking into it. COSC 2 P 93 Prolog: Cut 6

2. once n n Often, we want just one solution from a predicate. Multiple

2. once n n Often, we want just one solution from a predicate. Multiple answers may slow execution, due to needless backtracking. once(Goal): n n n This calls Goal, and returns first solution. Backtracking will immediately fail. Built-in to Sicstus Prolog. COSC 2 P 93 Prolog: Cut 7

Example of once/1 % delete(A, L, R): Delete A from L, resulting in R.

Example of once/1 % delete(A, L, R): Delete A from L, resulting in R. delete(A, [A|T], T). delete(A, [B|T], [B|T 2]) : delete(A, T, T 2). delete(_, [ ]). ? - delete(a, [a, b, a, c, a], L). L = [b, a, c, a] ? ; L = [a, b, a, c] ? ; L = [a, b, a, c, a] ? ; ? - once( delete(a, [a, b, a, c, a], L) ). L = [b, a, c, a] ? ; no COSC 2 P 93 Prolog: Cut 8

3. The cut, ! n The cut takes the form of a goal in

3. The cut, ! n The cut takes the form of a goal in a clause. n n Should use only one cut per clause. A predicate can have one or more clauses with cuts. Scheme: 1. all the clauses before the first clause with a cut are executed with normal backtracking. 2. if the goals before the cut fail, the cut does not activate, and the subsequent clause is used, as normal. 3. if the goals before the cut succeed, the cut activates: a) backtracking back to goals before the cut cannot occur b) backtracking to subsequent clauses after the one with the cut cannot occur --> that clause with the activated cut is “committed” c) the goals after the cut are executed with normal backtracking COSC 2 P 93 Prolog: Cut 9

The cut - example p(1). p(2). p(Y) : - q(3, Z), !, r(Z, Y).

The cut - example p(1). p(2). p(Y) : - q(3, Z), !, r(Z, Y). p(4). q(2, 4). q(3, 5). n n n r(5, 6). r(5, 7). Clauses 1, 2 are executed as normal In 3, the goal q(3, Z) executes; if it succeeds, then the ! is activated, and the goal r is executed as normal. However, in activating this cut. . . n n n %1 %2 %3 %4 clause 4 will not execute for this particular execution call clause 3 will not backtrack to q again (in this goal inference) Note that backtracking in r(Z, Y) occurs as expected, and hence you can still get multiple solutions from clause 3 COSC 2 P 93 Prolog: Cut 10

Green and red cuts n There are two usages of cuts: (i) Green cuts

Green and red cuts n There are two usages of cuts: (i) Green cuts (GOOD): cuts that prune execution branches that do not lead to useful solutions. (ii) Red cuts (BAD): cuts that prune valid solutions COSC 2 P 93 Prolog: Cut 11

Example: green cut % A list is bad if: (1) it is empty; (2)

Example: green cut % A list is bad if: (1) it is empty; (2) it has more than 100 items; or (3) it has an integer. test_list(L) : test_if_bad(L), !, write(‘Bad list, ’), nl, fail. test_list(L) : write(‘Good list’), nl. % print message and fail % print message and succeed test_if_bad([ ]). test_if_bad(L) : - length(L, N), N > 100. test_if_bad(L) : - member(X, L), integer(X). n n a “green cut”, because we know that only one of test_list clauses must succeed to say a list is bad. backtracking makes no sense with it. COSC 2 P 93 Prolog: Cut 12

Green cut n Note that we could do the same with if-then-else. . .

Green cut n Note that we could do the same with if-then-else. . . test_list(L) : (test_if_bad(L) -> write(‘Bad list, ’), nl, fail ; write(‘Good list’), nl ). COSC 2 P 93 Prolog: Cut 13

Example: red cut n A bad use of cut. . . parent(P, C) :

Example: red cut n A bad use of cut. . . parent(P, C) : - father(P, C), !. parent(P, C) : - mother(P, C). father(bob, sue). father(bob, kim). mother(mary, sue). mother(mary, kim). n A red cut: we get the first solution from father, and never give another valid solution again, from either father or mother! ? - parent(A, B). A = bob, B = sue ; no. COSC 2 P 93 Prolog: Cut 14

Red cut n Variation: parent(P, C) : - !, father(P, C). parent(P, C) :

Red cut n Variation: parent(P, C) : - !, father(P, C). parent(P, C) : - mother(P, C). (rest as before) ? - parent(A, B). A = bob, B = sue ; A = bob, B = kim ; no n Same as. . . parent(P, C) : - father(P, C). COSC 2 P 93 : Cut 15

Implementation: If-then-else n If-then-else is implemented with a cut: P : - (If ->

Implementation: If-then-else n If-then-else is implemented with a cut: P : - (If -> Then ; Else). Same as. . . P : - If, !, Then. Else. COSC 2 P 93 Prolog: Cut 16

Implementation: once/1 once(P) : call(P), ! or. . . once(P) : P, !. n

Implementation: once/1 once(P) : call(P), ! or. . . once(P) : P, !. n n This is a “meta-logical” call. Usually built into Prolog. COSC 2 P 93 Prolog: Cut 17

More cut examples n Example: a deterministic member/2: memberd/2 n deterministic clause: one that

More cut examples n Example: a deterministic member/2: memberd/2 n deterministic clause: one that returns one solution per call member(A, [A|_). member(A, [_|R]) : - member(A, R). memberd(A, [A|_]) : - !. memberd(A, [_ | R]) : - memberd(A, R). n n Here, as soon as memberd clause 1 finds a match, it succeeds Subsequent backtracking to memberd then fails, due to the cut n n we prevent memberd clause 2 from finding another match Note: memberd same as. . . ? - once(member(X, Y)). COSC 2 P 93 Prolog: Cut 18

Cut examples n Example: sum the integers between 1 and n sum_to(1, 1) :

Cut examples n Example: sum the integers between 1 and n sum_to(1, 1) : - !. sum_to(N, Sum) : M is N - 1, sum_to(M, Tmp), Sum is Tmp + N. n n Without the cut, backtracking proceeds to sum_to(0, _), sum_to(-1, . . . ) etc with the cut, when the case sum_to(1, 1) occurs, backtracking will not commence (via clause 2) again, a green cut: only one solution desired BUT. . . simply checking size of N in 2 nd clause will prevent need for a cut. . . COSC 2 P 93 Prolog: Cut 19

Take out the cut. . . sum_to(1, 1). sum_to(N, Sum) : N > 1,

Take out the cut. . . sum_to(1, 1). sum_to(N, Sum) : N > 1, M is N - 1, sum_to(M, Tmp), Sum is Tmp + N. COSC 2 P 93 Prolog: Cut 20

Cuts n Cuts are extralogical: they almost always destroy a program’s logical “declarative” reading.

Cuts n Cuts are extralogical: they almost always destroy a program’s logical “declarative” reading. Consider test_list example again. . . test_list(L) : - test_if_bad(L), !, write(‘Bad list, ’), nl, fail. test_list(L) : write(‘Good list’), nl. n n n Read literally, the second clause says that all lists are good lists! Hence we must now ascertain the meaning of this predicate by inspecting what the cut is doing. The second clause’s meaning is dependent upon the first clause. COSC 2 P 93 Prolog: Cut 21

Cuts n Cuts are unavoidable in many programs. Without them, the program can become

Cuts n Cuts are unavoidable in many programs. Without them, the program can become too large and inefficient n However, cuts usually ruin a logic program’s readability n Careless use of cuts can make a Prolog program unintelligible, and hard to debug. COSC 2 P 93 Prolog: Cut 22

When to use cuts 1. Try to make a declarative predicate if feasible: correct,

When to use cuts 1. Try to make a declarative predicate if feasible: correct, concise, efficient. 2. Else, use “->” (if-then-else) or once/1 if they help. 3. Else, use a cut if it is a green cut. 4. Red cut: use as rarely as possible. Document their function! COSC 2 P 93 Prolog: Cut 23