ffi
play

ffi wonder whether t. Preventi ng backtracki ng To find out about - PowerPoint PPT Presentation

State of alert Y normal aler 0 J Figure 5.1 State of lling Backtracking ro as follows: f( X, normal) :- 5.1 Preventing backtracking 126 5.2 Examples of using cut 131 f( X, alertl) :- 3 5.3 Negation as failure 135 f( X, alert2) :- 6 5.4


  1. State of alert Y normal aler 0 J Figure 5.1 State of ¡ lling Backtracking ro as follows: f( X, normal) :- 5.1 Preventing backtracking 126 5.2 Examples of using cut 131 f( X, alertl) :- 3 5.3 Negation as failure 135 ¡f( X, alert2) :- 6 5.4 Closed world assumption, and problems with cut and negation 138 This program, of c f(X,Y) is executed, We will make I some source of in We have already seen that a programmer can control the execution of a program turn by using the through the ordering of clauses and goals. ln this chapter we will look at another con- trol facility, called tut', for preventing backtracking. The cut also extends the expres- sive power of Prolog and enables the definition of a kind of negation, called 'negation Experiment 1 5.1.1 as failure' and associated with the 'closed world assumption'. Suppose that the r was X: 2. Now i ffi wonder whether t. Preventi ng backtracki ng To find out about assume the user a¡ Prolog will automatically backtrack if this is necessary for satisfying a goal. Auto' ?-f(2,Y), Y:al matic backtracking is a useful programming concept because it relieves the pro- grammer of the burden of programming backtracking explicitly. On the other Let us analyse how hand, uncontrolled backtracking may cause inefficiency in a program. Therefore 5.1 Preventing backtracking 127 Y tlecomes instant we sometimes want to control, or to prevent, backtracking. We can do this in normal : alertl Prolog by using the 'cut' facility. State of alert Y Let us first study the behaviour of a simple example program whose execution which fails, and s normal alert2 alert 1 involves some backtracking. We will identify those points at which the back- admitting that the o 3 u tracking is useless and leads to inefficiency. useless alternativer Consider a regulation about the state of alert due to pollution. Figure 5.1 The three rules "oncentrationX shows the relation between the concentration X of the pollutant and the degree at most will succe Figure 5.1 State of alert as a function of pollution level. of alert Y, which can be normal, or alertl, or alert2. The relation between X and Y rg is no point in tryi can be specified by three rules: of course does no known to succee( RuIe 1: if X < 3 thenY: normal as follows: backtracking at tJ RuIe 2: if3 < X andX< 6thenY: alertl f( X, normal) :- X < 3. o/o Rule 1 We can do this t Rule 3: if 6 < XthenY:aleftZ inserted between f( X, alertl) :- 3 :< X, X < 6. o/oRtlJe 2 This can be written in Prolog as a binary relation: cuts, is: f( X, alert2) :- 6 :< X. o/o Rule 3 f(x, Ð f( X, normal) :- r cut and negation 138 f( X, alertl) :- 3 This program, of course, assumes that X is already instantiated to a number before f( X, alert2) :- 6 f(X, Ð is executed, as required by the comparison operators. We will make two experiments with this program. Each experiment will reveal some source of inefficiency in the program, and we will remove each source in ìtrol the execution of a program turn by using the cut mechanism. apter we will look at another con- . The cut also extends the expres- kind of negation, called 'negation Experiment 1 5.1.1 mption'. Suppose that the concentration X of the pollutant was measured, and the result was X : 2. Now an administrator, not being familiar with the regulations, may wonder whether the concentration 2 is unsafe and should be the cause of alarm. To find out about this, Prolog can be asked the question: ?- f(2, aleftl). But let us assume the user asks the following, equivalent question: ssary for satisfying a goal. Auto- ?- f( 2, Y\, Y : alertl.. cept because it relieves the pro- acking explicitly. On the other Let us analyse how Prolog looks for an answer. When executing the frrst goal, f(z,Y), iciency in a program. Therefore Y becomes instantiated to normal. So the second goal becomes acktracking. We can do this in normal : alertl rmple program whose execution which fails, and so does the whole goal list. This is straightforward, but before rose points at which the back- admitting that the goal list is not satisfiable, Prolog tries, through backtracking, two useless alternatives. The detailed trace is shown in Figure 5.2. rt due to pollution. Figure 5.1 The three rules about the f relation are mutually exclusive so that one of them of the pollutant. and the degree at most will succeed. Therefore we know that as soon as one rule succeeds there ?. The relation between X and Y is no point in trying to use the other rules, as they are bound to fail. But Prolog of course does not know this. In the example of Figure 5.2, rule I has become known to succeed at the point indicated by 'CUT'. In order to prevent futile backtracking at this point we have to tell Prolog explicitly not to backtrack. We can do this by using the cut mechanism. The cut is written as '!' and is inserted between goals as a kind of pseudo-goal. Our program, rewritten with cuts, is: f( X, normal) :- X < 3, !. f( X, alertl) :- 3 :< X, X < 6, !. f( X, alert2) :- 6 :< X.

  2. f( X, normal) :- X < 3. f( X, alert1) :- 3 =< X, X < 6. f( X, alert2) :- 6 =< X. ?- f( 2, Y), Y = alert1. f( 2, Y), Y = alert1. X < 3. 3 =< X, X < 6. 6 =< X.

  3. f( X, normal) :- X < 3. f( X, alert1) :- 3 =< X, X < 6. f( X, alert2) :- 6 =< X. ?- f( 2, Y), Y = alert1. f( 2, Y), Y = alert1. X < 3. 3 =< X, X < 6. 6 =< X.

  4. f( X, normal) :- X < 3. f( X, alert1) :- 3 =< X, X < 6. f( X, alert2) :- 6 =< X. ?- f( 2, Y), Y = alert1. fail f( 2, Y), Y = alert1. X < 3. 3 =< X, X < 6. 6 =< X.

  5. f( X, normal) :- X < 3. f( X, alert1) :- 3 =< X, X < 6. f( X, alert2) :- 6 =< X. ?- f( 2, Y), Y = alert1. backtrack f( 2, Y), Y = alert1. X < 3. 3 =< X, X < 6. 6 =< X.

  6. f( X, normal) :- X < 3. f( X, alert1) :- 3 =< X, X < 6. f( X, alert2) :- 6 =< X. ?- f( 2, Y), Y = alert1. f( 2, Y), Y = alert1. X < 3. 3 =< X, X < 6. 6 =< X.

  7. f( X, normal) :- X < 3. f( X, alert1) :- 3 =< X, X < 6. f( X, alert2) :- 6 =< X. ?- f( 2, Y), Y = alert1. fail f( 2, Y), Y = alert1. X < 3. 3 =< X, X < 6. 6 =< X.

  8. f( X, normal) :- X < 3. f( X, alert1) :- 3 =< X, X < 6. f( X, alert2) :- 6 =< X. ?- f( 2, Y), Y = alert1. backtrack f( 2, Y), Y = alert1. X < 3. 3 =< X, X < 6. 6 =< X.

  9. f( X, normal) :- X < 3. f( X, alert1) :- 3 =< X, X < 6. f( X, alert2) :- 6 =< X. ?- f( 2, Y), Y = alert1. f( 2, Y), Y = alert1. X < 3. 3 =< X, X < 6. 6 =< X.

  10. f( X, normal) :- X < 3. f( X, alert1) :- 3 =< X, X < 6. f( X, alert2) :- 6 =< X. ?- f( 2, Y), Y = alert1. fail f( 2, Y), Y = alert1. X < 3. 3 =< X, X < 6. 6 =< X.

  11. No cut in function f . The cut in function f2 . 128 Chapter 5 Controlling Backtracking Try rule L: 7. f(2,Y) Y = alertl Tryrule 2: 3, (c Rule I Rule 2 6, Rule 3 Try ruIe 3: Y = normal Y = alertl Y = alert2 This trace revealr not true (7 < 3 fa 2<3 3=<2 6 =<2 once the flrst tesl 2<6 normal = alertl alerf2 = alertl alertl = alertL of the first. Therr be omitted. The no no following, more CUT ifX<3then' : otherwise if X normal = alertl otherwise Y : no We can now oÍ Figure 5.2 At the point marked 'CUT' we already know that the rules 2 and 3 are bound whenever they a to fail. f( X, normal) : f( X, alertl) :- f( X, alert2). The cut will now prevent backtracking at the points at which it appears in the This program prr program. If we now ask: version, but it is : ?- Í(2, Y), Y : alertl. r40w remove the Prolog will produce the same left-hand branch as in Figure 5.2. This branch will fail f( X, normal) :- at the goal normal: alertl. Now Prolog will try to backtrack, but not beyond the f( X, alertl) :- ) point marked '!' in the program. The alternative branches that correspond to 'rule f( X, alert2). 2' and 'rule 3' will not be generated. This may produ< The new program, equipped with cuts, is in general more efficient than the ?- f( 1, Y). original version without cuts. When the execution fails, the new program will Y: nomal; recognize this sooner than the original program. Y : alertl; To conclude, we have improved the efficiency by adding cuts. If the cuts are Y : alert2; now removed in this example, the program will still produce the same result; it no will sometimes only spend more time. In our case, by introducing the cut we only changed tl:re procedurøl meaning of the program; that is, the results of It is important tc the program were not affected. We will see later that using a cut may affect the time the cuts no results as well. of the program. ' and the user susl ?- f(2, alertl). 5.1.2 Experiment 2 yes Let us now perform a second experiment with the second version of oul Try to work out Also, work out w program. Suppose we ask: ?- f(2, Ð , Y: ?- f(7,9. Y : aleft2 no These examples Let us trace what has happened. All three rules were tried before the answer was But there may b obtained. This produced the following sequence of goals: analysed later in

  12. f2( X, normal) :- X < 3, !. f2( X, alert1) :- 3 =< X, X < 6, !. f2( X, alert2) :- 6 =< X. ?- f2( 2, Y), Y = alert1. f2( 2, Y), Y = alert1. X < 3, !. 3 =< X, X < 6, !. 6 =< X.

Recommend


More recommend