Controlling Backtracking : Cuts

19
cs774 (Prasad) L6Backtracking 1 Controlling Backtracking : Cuts [email protected] http://www.knoesis.org/ tkprasad/

description

Controlling Backtracking : Cuts. [email protected] http://www.knoesis.org/tkprasad/. Motivation. Efficiency by preventing needless backtracking Cf. CFL Parsing Lookaheads vs Backtracking vs Dynamic programming Avoiding duplicates or resatisfaction Prolog as programming language - PowerPoint PPT Presentation

Transcript of Controlling Backtracking : Cuts

Page 1: Controlling Backtracking : Cuts

cs774 (Prasad) L6Backtracking 1

Controlling Backtracking : Cuts

[email protected]://www.knoesis.org/tkprasad/

Page 2: Controlling Backtracking : Cuts

Motivation• Efficiency by preventing needless

backtracking– Cf. CFL Parsing

• Lookaheads vs Backtracking vs Dynamic programming

• Avoiding duplicates or resatisfaction• Prolog as programming language

– Tailoring control strategy

cs774 (Prasad) L6Backtracking 2

Page 3: Controlling Backtracking : Cuts

Efficiency Cut • Preventing backtracking when faced with

mutually disjoint cases

sign(X, positive) :- X > 0.sign(X, negative) :- X < 0.sign(X, zero).

cs774 (Prasad) L6Backtracking 3

Page 4: Controlling Backtracking : Cuts

(cont’d)• Good use of cut if declarative reading

preserved• In general, reordering of clauses may not preserve

the declarative meaning.

sign(X, positive) :- X > 0, !.sign(X, negative) :- X < 0, !.sign(X, zero).

cs774 (Prasad) L6Backtracking 4

Page 5: Controlling Backtracking : Cuts

Green Cut vs Red Cut

• A use of a cut which improves only efficiency of a program, without altering its declarative meaning, is referred to as a green cut.

– In practice, prunes away irrelevant proofs, or proofs bound to fail.

– In practice, use them when mutually exclusive cases occur, or if non-determinism can be minimized.

cs774 (Prasad) L6Backtracking 5

Page 6: Controlling Backtracking : Cuts

Green Cut vs Red Cut• A use of a cut that alters the declarative meaning

of a program is referred to as a red cut.– In practice, eliminates unwanted logical

solutions • Note that mature Prolog implementations index

clauses for a predicate on the principal functor of the first argument in the head of a clause thereby reducing non-determinism.pp(ff(_),_,_) :- q1(…), …, qn(…).

cs774 (Prasad) L6Backtracking 6

Page 7: Controlling Backtracking : Cuts

Implementing Conditional• “p(x) :- if c(x) then r(x) else q(x)”

p(X) :- c(X), !, r(X).p(X) :- q(X).

• In this case, reordering of clauses changes the meaning.

cs774 (Prasad) L6Backtracking 7

Page 8: Controlling Backtracking : Cuts

Avoiding Duplicatesmember(X,[X|L]) :- !.member(X,[_|L]) :- member(X,L).• Without cut, the goal

?- member(a, [a, a, a]) will succeed thrice.

• Note that with cut member-predicate cannot be used to generate/enumerate elements in a list. That is, invertibility is destroyed.

cs774 (Prasad) L6Backtracking 8

Page 9: Controlling Backtracking : Cuts

Bugs with Cutsmin(X, Y, X) :- X =< Y, !.min(X, Y, Y).• Flaw: the goal

?- min(3,6,6) does not fail.

• Reason: min(3,6,6) does not unify with min(X,Y,X).

• Fix : change first rule to:

min(X, Y, Z) :- X =< Y,!,Z = X.

cs774 (Prasad) L6Backtracking 9

Page 10: Controlling Backtracking : Cuts

Effect of CutMatching Rule:H :- B1, B2, …, Bm,!, …, Bn.

Goal:?-G.When “!” is encountered, the bindings for variables in goals B1, …,Bm, and G are frozen. That is, alternate solutions to B1, …, Bm, and remaining alternatives for G are discarded.Note that the ordering of body literals and clauses becomes significant in the presence of cut.

cs774 (Prasad) L6Backtracking 10

Page 11: Controlling Backtracking : Cuts

Effect of Cutp(X) :- q(X), r(X), !, s(X).p(d).Declarative Reading (ignoring cut): { p(d)}Procedural reading (using cut): { p(d)}

?- p(X). -> one solution?- p(a). -> no?- p(d). -> yes

cs774 (Prasad) L6Backtracking 11

Page 12: Controlling Backtracking : Cuts

Effect of Cutp(X) :- q(X), r(X), !, s(X).p(d).

• Additional Facts: {q(a),r(a),s(a)}Declarative Reading : { p(a), p(d)}

Procedural reading : { p(a)} ?- p(X). -> X = a?- p(a). -> yes?- p(d). -> yescs774 (Prasad) L6Backtracking 12

Page 13: Controlling Backtracking : Cuts

(cont’d)p(X) :- q(X), r(X), !, s(X).p(d).

• Additional Facts: {q(b),s(b)}Declarative Reading (ignoring cut): { p(d)}Procedural reading (using cut): { p(d)}

?- p(X). -> X = d?- p(a). -> no?- p(d). -> yes

cs774 (Prasad) L6Backtracking 13

Page 14: Controlling Backtracking : Cuts

(cont’d)p(X) :- q(X), r(X), !, s(X).p(d).

• Additional Facts: {q(c),r(c)}Declarative Reading : { p(d)}

Procedural reading : { } ?- p(X). -> no?- p(c). -> no?- p(d). -> yes

cs774 (Prasad) L6Backtracking 14

Page 15: Controlling Backtracking : Cuts

(cont’d)p(X) :- q(X), r(X), !, s(X).p(d).

• Additional Facts: {q(a),r(a),s(a), q(b),r(b),s(b)}

?- p(X). Declarative Reading (ignoring cut): { p(a), p(b), p(d)}Procedural reading (using cut):

{ p(a)} cs774 (Prasad) L6Backtracking 15

Page 16: Controlling Backtracking : Cuts

Disadvantages of Cut• Destroys the declarative reading : Need to know

the behavior of the interpreter to understand the meaning (“side-effects”)p :- a, b.p :- c.

p :- a, !, b. p :- c. p :- c. p :- a, !, b.

cs774 (Prasad) L6Backtracking 16

cbap )(

)()( cabap

)( bacp

Page 17: Controlling Backtracking : Cuts

Conditional vs Cuttp(X,Y) :- q(X) -> r(Y) ; s(Y).tp(m,m).tp(n,n).

is not equivalent tosp(X,Y) :- q(X),!,r(Y).

sp(X,Y) :- s(Y).sp(m,m).sp(n,n).

q(a). q(c). r(e). r(f). s(g). s(h).

cs774 (Prasad) L6Backtracking 17

Page 18: Controlling Backtracking : Cuts

(cont’d)

• ?- tp(X,Y).

X = a Y = eX = a Y = fX = m Y = mX = n Y = n

– Local cut

• ?- sp(X,Y).

X = a Y = eX = a Y = f – sp-goal is not

resatisfied for sp-facts because of the cut.

cs774 (Prasad) L6Backtracking 18

Page 19: Controlling Backtracking : Cuts

Other uses of Cut• Implementing default or otherwise clause. a(X,Y) :- p1(X), !, q1(X,Y).…a(X,Y) :- pn(X), !, qn(X,Y).a(X,Y) :- r(X,Y).

• Cut-Fail combination to formalize conditions under which failure is guaranteed. a(X) :- p(X), !, fail.

cs774 (Prasad) L6Backtracking 19