Controlling Backtracking - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Controlling Backtracking

Description:

Prolog automatically backtracks in its attempts to satisfy a ... Sometimes, backtracking is a source of inefficiency ... max/3 (with cut); max1/3 (without cut) ... – PowerPoint PPT presentation

Number of Views:103
Avg rating:3.0/5.0
Slides: 14
Provided by: fatih8
Category:

less

Transcript and Presenter's Notes

Title: Controlling Backtracking


1
Controlling Backtracking
  • Notes for Ch.5 of Bratko
  • For CENG 421 Fall03

2
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
A Double-step Function
  • if X lt 3 then Y 0
  • if 3 lt X and X lt 6 then Y 2
  • if 6 lt X then Y 4
  • In Prolog,
  • f( X,0) - X lt 3. Rule 1
  • f( X,2) - 3 lt X, X lt 6. Rule 2
  • f( X,4) - 6 lt 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
Experiment 1 ch5_1.pl
  • trace 6 ?- f( 1,Y), Y gt 2.
  • Call (7) f(1, _G423) ? creep
  • Call (8) 1lt3 ? creep
  • Exit (8) 1lt3 ? 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) 3lt1 ? creep are tried
    anyway
  • Fail (8) 3lt1 ? creep
  • Redo (7) f(1, _G423) ? creep
  • Call (8) 6lt1 ? creep
  • Fail (8) 6lt1 ? creep
  • Fail (7) f(1, _G423) ? creep
  • No

5
Using the cut ch5_2.pl
  • f( X,0) - X lt 3, !. commit!
  • f( X,2) - 3 lt X, X lt 6, !. commit!
  • f( X,4) - 6 lt X.
  • trace 9 ?- f( 1,Y), Y gt 2.
  • Call (7) f(1, _G423) ? creep
  • Call (8) 1lt3 ? creep
  • Exit (8) 1lt3 ? 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
Green and Red Cuts
  • f( X,0) - X lt 3, !. if X lt 3 then Y 0
  • f( X,2) - X lt 6, !. otherwise, if X lt 6 then
    Y 2
  • f( _,4). otherwise Y 4 (ch5_3.pl)
  • But the following is incorrect
  • f( X,0) - X lt 3.
  • f( X,2) - X lt 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
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
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 gt Y, !, Max X.
  • max2( _,Y,Max) - Max Y.
  • deterministic membership (member1/2)

9
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
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
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
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
Logical Negation
  • Logical negation cannot be handled using Prologs
    goal tree mechanism
  • See HoleInGoalTrees.ppt
Write a Comment
User Comments (0)
About PowerShow.com