Presentation is loading. Please wait.

Presentation is loading. Please wait.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.

Similar presentations


Presentation on theme: "UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta."— Presentation transcript:

1 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta

2 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Preventing Backtracking Prolog automatically backtracks in its attempts to satisfy a goal (equivalently, in its traversal of a goal tree) Sometimes, backtracking is a source of inefficiency For example, Prolog, when requested for additional answers to a goal, may try clauses that are known to lead, eventually, to failure

3 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering A Double-step Function if X < 3 then Y = 0 if 3 =< X and X < 6 then Y = 2 if 6 =< X then Y = 4 In Prolog, f( X,0) :- X < 3. % Rule 1 f( X,2) :- 3 =< X, X < 6. % Rule 2 f( X,4) :- 6 =< X. % Rule 3 Assume that X is instantiated to a number when the program is used The program does not exploit the fact that the three rules are mutually exclusive

4 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Experiment 1: ch5_1.pl [trace] 6 ?- f( 1,Y), Y > 2. Call: (7) f(1, _G423) ? creep ^ Call: (8) 1<3 ? creep ^ Exit: (8) 1<3 ? creep Exit: (7) f(1, 0) ? creep No need to go beyond here! Redo: (7) f(1, _G423) ? creep But rules 2 and 3 ^ Call: (8) 3=<1 ? creep are tried anyway ^ Fail: (8) 3=<1 ? creep Redo: (7) f(1, _G423) ? creep ^ Call: (8) 6=<1 ? creep ^ Fail: (8) 6=<1 ? creep Fail: (7) f(1, _G423) ? creep No

5 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Using the cut: ch5_2.pl f( X,0) :- X < 3, !. % commit! f( X,2) :- 3 =< X, X < 6, !. % commit! f( X,4) :- 6 =< X. [trace] 9 ?- f( 1,Y), Y > 2. Call: (7) f(1, _G423) ? creep ^ Call: (8) 1<3 ? creep ^ Exit: (8) 1<3 ? creep Exit: (7) f(1, 0) ? creep Fail: (7) f(1, 0) ? creep since rule1 was “used”, No rules 2 and 3 are not tried “used” means: the cut in rule 1 was reached

6 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Green and Red Cuts f( X,0) :- X < 3, !. % if X < 3 then Y = 0 f( X,2) :- X < 6, !. % otherwise, if X < 6 then Y = 2 f( _,4). % otherwise Y = 4 (ch5_3.pl) But the following is incorrect f( X,0) :- X < 3. f( X,2) :- X < 6. f( _,4). Sometimes a cut changes the meaning of a program Red cuts change the meaning of a program (as in the program above) Green cuts do not change the meaning of a program (as in ch5_2.pl)

7 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Cut Mechanism Let the parent goal be the one that matches the head of the clause containing the cut When the cut is encountered as a goal, it succeeds immediately, but it commits the system to all choices made between the time the parent goal was invoked and the time the cut was encountered: all the remaining possibilities between the parent goal and the cut are discarded

8 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Examples Using Cut: ch5_4.pl max/3 (with cut); max1/3 (without cut) strange behavior when one of the arguments is a list of numbers: the first number in the list is used in the comparison. Cf. SWI-Prolog manual, section 4.6.1 problem with max( 3,1,1): it succeeds! max2/3 fixes this problem max2( X,Y,Max) :- X >= Y, !, Max = X. max2( _,Y,Max) :- Max = Y. deterministic membership (member1/2)

9 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Examples Using Cut: ch5_4.pl, Ctd. adding an element to a list without duplication add/3 classification into categories class/2 in both cases, the last argument should not be instantiated

10 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Negation as Failure different( X,Y) :- X = Y, !, fail. different( X,Y). Or: different( X,Y) :- X = Y, !, fail; true. not( P) :- P, !, fail ; true. op( 900, fy, [not, ‘\+’]). not P succeeds if P fails. different( X,Y) :- not( X = Y).

11 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Problem with Negation As Failure Cf. even/odd mutually recursive program (ch5_5.pl) A positive query ?-p(X) is interpreted as a proof for “there exists X s.t. P(X) is true” A negative query ?- not(p(X)) is interpreted as a proof for “for all X, P(X) is false)

12 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Problems with cut and negation The main problem with cut is that we may lose the valuable correspondence between declarative and procedural meanings of a program Prolog negation is based on the closed world assumption: “if something cannot be proven, it is false.” This leads to especially bad results when the argument of not is a term containing unistantiated variables Some Prolog dialects (e.g., NU-Prolog) try to delay not subgoals until all variables in them are instantiated, when possible

13 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Logical Negation Logical negation cannot be handled using Prolog’s goal tree mechanism See HoleInGoalTrees.ppt


Download ppt "UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta."

Similar presentations


Ads by Google