Bratko, Chapter 5: Controlling Backtracking - have seen some control by ordering clauses - example: sort2(L1,L2) % sort a list of two numbers - unnecessary backtracking - unnecessary conditions - introduce cut - allows removal of preconditions, but then can't remove cuts - the cut '!' - the cut is a goal that always succeeds, but has side-effects - when the cut is reached in a clause, all choices made since the head of the clause was selected are frozen, including the choice of this clause among alternatives matching the original goal - general examples: A :- B,C,D. A :- R,S. B :- E,F. B :- G. C :- H,I,!,J,K. C :- L,M. D :- O,P. D :- Q. ?- A. ?- B, !, D. - using cut to implement 'not' - e.g., peak, base and intermediate block - problem with asking yes/no questions - negation as failure - the 'fail' goal always fails, causing clause to fail - e.g., can_fly(X) :- bird(X), not ostrich(X). - not(P) :- P, !, fail ; true. - :- op(500,fx,not). - 'true' always succeeds (but no side-effects) - problems with cut and negation - program becomes less declarative - changing clause order with cuts may change declarative meaning (red cuts) - e.g., p :- a, !, b. without cut p = ab v c P :- c. with cut p = ab v ~ac - "green" (innocent) cuts do not change the declarative meaning - not P does not imply the opposite, just that can't prove P - closed-world assumption - trace restaurant example (see below) - more examples - merge from mergesort HW2 - write a program to do ??? using two versions: one without cuts and one with cuts - last([a,b,c]) = c | ?- trace. % The debugger will first creep -- showing everything (trace) yes [trace] | ?- reasonable(X), good_standard(X). (1) 0 Call: reasonable(_8919) ? (1) 1 Head [1]: reasonable(_8919) ? (2) 1 Call: not expensive(_8919) ? (2) 2 Head [1]: not expensive(_8919) ? (3) 2 Call (built_in): call(user:expensive(_8919)) ? (4) 3 Call: expensive(_8919) ? (4) 4 Head [1]: expensive(_8919) ? (4) 3 Done: expensive(jeanluis) ? (3) 2 Done (built_in): call(user:expensive(jeanluis)) ? (2) 1 Fail: not expensive(_8919) ? (1) 0 Fail: reasonable(_8919) ? no [trace] | ?-