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

ffi
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

rg

r cut and negation 138

ìtrol the execution of a program

apter we will look at another con-

. The cut also extends the expres- kind of negation, called 'negation mption'.

ssary for satisfying a goal. Auto-

cept because it relieves the pro-

acking explicitly. On the other

iciency in a program. Therefore

  • acktracking. We can do this in

rmple program whose execution rose points at which the back-

rt due to pollution. Figure 5.1

  • f the pollutant. and the degree

?. The relation between X and Y

5.1.1

5.1 Preventing backtracking 127

State of alert Y

normal

alert 1

alert2

  • 3

u

"oncentrationX Figure 5.1 State of alert as a function of pollution level.

as follows:

f( X, normal) :- X < 3.

  • /o Rule 1

f( X, alertl) :- 3 :< X, X < 6.

  • /oRtlJe 2

f( X, alert2) :- 6 :< X.

  • /o Rule 3

This program, of course, assumes that X is already instantiated to a number before

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

turn by using the cut mechanism.

Experiment 1

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:

?- f( 2, Y\, Y : alertl..

Let us analyse how Prolog looks for an answer. When executing the frrst goal, f(z,Y), Y becomes instantiated to normal. So the second goal becomes

normal : alertl

which fails, and so does the whole goal list. This is straightforward, but before

admitting that the goal list is not satisfiable, Prolog tries, through backtracking, two

useless alternatives. The detailed trace is shown in Figure 5.2.

The three rules about the f relation are mutually exclusive so that one of them

at most will succeed. Therefore we know that as soon as one rule succeeds there is no point in trying to use the other rules, as they are bound to fail. But Prolog

  • f 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.

State of alert Y

normal

aler J

Figure 5.1 State of ¡

as follows:

f( X, normal) :- f( X, alertl) :- 3

¡f( X, alert2) :- 6

This program, of c

f(X,Y) is executed,

We will make I some source of in

turn by using the

Experiment 1

Suppose that the r

was X: 2. Now i

wonder whether t. To find out about

assume the user a¡

?-f(2,Y), Y:al

Let us analyse how Y tlecomes instant

normal : alertl

which fails, and s

admitting that the

useless alternativer

The three rules

at most will succe is no point in tryi

  • f course does no

known to succee(

backtracking at tJ

We can do this t

inserted between

cuts, is: f( X, normal) :- f( X, alertl) :- 3 f( X, alert2) :- 6

ro

5.1 Preventing backtracking 126 5.2 Examples of using cut 131 5.3 Negation as failure 135 5.4 Closed world assumption, and problems with cut and negation 138

We have already seen that a programmer can control the execution of a program

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

as failure' and associated with the 'closed world assumption'.

ffi

Preventi ng backtracki ng

lling Backtracking

5.1.1

Prolog will automatically backtrack if this is necessary for satisfying a goal. Auto'

matic backtracking is a useful programming concept because it relieves the pro-

grammer of the burden of programming backtracking explicitly. On the other hand, uncontrolled backtracking may cause inefficiency in a program. Therefore

we sometimes want to control, or to prevent, backtracking. We can do this in

Prolog by using the 'cut' facility. Let us first study the behaviour of a simple example program whose execution

involves some backtracking. We will identify those points at which the back-

tracking is useless and leads to inefficiency. Consider a regulation about the state of alert due to pollution. Figure 5.1

shows the relation between the concentration X of the pollutant and the degree

  • f alert Y, which can be normal, or alertl, or alert2. The relation between X and Y

can be specified by three rules:

RuIe 1: if X < 3 thenY: normal RuIe 2: if3 < X andX< 6thenY: alertl

Rule 3: if 6 < XthenY:aleftZ

This can be written in Prolog as a binary relation:

f(x, Ð

slide-2
SLIDE 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.

slide-3
SLIDE 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.

slide-4
SLIDE 4

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. fail

slide-5
SLIDE 5

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. backtrack

slide-6
SLIDE 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.

slide-7
SLIDE 7

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. fail

slide-8
SLIDE 8

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. backtrack

slide-9
SLIDE 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.

slide-10
SLIDE 10

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. fail

slide-11
SLIDE 11

It is important tc

time the cuts no

  • f the program. '

and the user susl ?- f(2, alertl).

yes

Try to work out

Also, work out w ?- f(2, Ð, Y: no

These examples

But there may b

analysed later in

7.

3,

(c

6,

Try ruIe 3:

f( X, normal) :- f( X, alertl) :- ) f( X, alert2).

This program prr

version, but it is :

r40w remove the

f( X, normal) : f( X, alertl) :- f( X, alert2).

We can now oÍ

whenever they a

ifX<3then'

: otherwise if X

  • therwise Y :

This trace revealr

not true (7 < 3 fa

  • nce the flrst tesl
  • f the first. Therr

be omitted. The

following, more

Try rule L:

Tryrule 2:

This may produ<

?- f( 1, Y).

Y: nomal;

Y : alertl; Y : alert2;

no

128 Chapter 5 Controlling Backtracking

Rule I Y = normal

2<3

normal = alertl

f(2,Y)

Y = alertl

Rule 2

Y = alertl

3=<2

2<6

alertl = alertL

Rule 3

Y = alert2

6 =<2

alerf2 = alertl no no CUT

normal = alertl Figure 5.2 At the point marked 'CUT' we already know that the rules 2 and 3 are bound

to fail. The cut will now prevent backtracking at the points at which it appears in the

  • program. If we now ask:

?- Í(2, Y), Y : alertl.

Prolog will produce the same left-hand branch as in Figure 5.2. This branch will fail

at the goal normal: alertl. Now Prolog will try to backtrack, but not beyond the

point marked '!' in the program. The alternative branches that correspond to 'rule

2' and 'rule 3' will not be generated.

The new program, equipped with cuts, is in general more efficient than the

  • riginal version without cuts. When the execution fails, the new program will

recognize this sooner than the original program.

To conclude, we have improved the efficiency by adding cuts. If the cuts are now removed in this example, the program will still produce the same result; it

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 the program were not affected. We will see later that using a cut may affect the

results as well.

5.1.2 Experiment 2

Let us now perform a second experiment with the second version of oul

  • program. Suppose we ask:

?- f(7,9.

Y : aleft2

Let us trace what has happened. All three rules were tried before the answer was

  • btained. This produced the following sequence of goals:

no

No cut in function f. The cut in function f2.

slide-12
SLIDE 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.

slide-13
SLIDE 13

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.

slide-14
SLIDE 14

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.

Commits to this choice.

slide-15
SLIDE 15

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.

Commits to this choice.

fail

slide-16
SLIDE 16

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. fail

Discarded.

slide-17
SLIDE 17

It is important tc

time the cuts no

  • f the program. '

and the user susl ?- f(2, alertl).

yes

Try to work out

Also, work out w ?- f(2, Ð, Y: no

These examples

But there may b

analysed later in

7.

3,

(c

6,

Try ruIe 3:

f( X, normal) :- f( X, alertl) :- ) f( X, alert2).

This program prr

version, but it is :

r40w remove the

f( X, normal) : f( X, alertl) :- f( X, alert2).

We can now oÍ

whenever they a

ifX<3then'

: otherwise if X

  • therwise Y :

This trace revealr

not true (7 < 3 fa

  • nce the flrst tesl
  • f the first. Therr

be omitted. The

following, more

Try rule L:

Tryrule 2:

This may produ<

?- f( 1, Y).

Y: nomal;

Y : alertl; Y : alert2;

no

128 Chapter 5 Controlling Backtracking

Rule I Y = normal

2<3

normal = alertl

f(2,Y)

Y = alertl

Rule 2

Y = alertl

3=<2

2<6

alertl = alertL

Rule 3

Y = alert2

6 =<2

alerf2 = alertl no no CUT

normal = alertl Figure 5.2 At the point marked 'CUT' we already know that the rules 2 and 3 are bound

to fail. The cut will now prevent backtracking at the points at which it appears in the

  • program. If we now ask:

?- Í(2, Y), Y : alertl.

Prolog will produce the same left-hand branch as in Figure 5.2. This branch will fail

at the goal normal: alertl. Now Prolog will try to backtrack, but not beyond the

point marked '!' in the program. The alternative branches that correspond to 'rule

2' and 'rule 3' will not be generated.

The new program, equipped with cuts, is in general more efficient than the

  • riginal version without cuts. When the execution fails, the new program will

recognize this sooner than the original program.

To conclude, we have improved the efficiency by adding cuts. If the cuts are now removed in this example, the program will still produce the same result; it

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 the program were not affected. We will see later that using a cut may affect the

results as well.

5.1.2 Experiment 2

Let us now perform a second experiment with the second version of oul

  • program. Suppose we ask:

?- f(7,9.

Y : aleft2

Let us trace what has happened. All three rules were tried before the answer was

  • btained. This produced the following sequence of goals:

no

No cut in function f. The cut in function f2. Discarded.

slide-18
SLIDE 18

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

The meaning of the cut mechanism

slide-19
SLIDE 19

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

The meaning of the cut mechanism

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

slide-20
SLIDE 20

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

The meaning of the cut mechanism

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

slide-21
SLIDE 21

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

The meaning of the cut mechanism

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

The clause containing the cut.

slide-22
SLIDE 22

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

The meaning of the cut mechanism

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

The clause containing the cut. Head of the clause containing the cut.

slide-23
SLIDE 23

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

The meaning of the cut mechanism

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

C is the parent goal. The clause containing the cut. Head of the clause containing the cut.

slide-24
SLIDE 24

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

The meaning of the cut mechanism

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

C is the parent goal. Cuts always succeed.

slide-25
SLIDE 25

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

The meaning of the cut mechanism

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

C is the parent goal. Commits the system to these choices.

slide-26
SLIDE 26

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

The meaning of the cut mechanism

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

C is the parent goal. Cut fails on backtracking. Success only possible through S, T, U.

slide-27
SLIDE 27

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

The meaning of the cut mechanism

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

C is the parent goal. Discarded.

slide-28
SLIDE 28

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

The meaning of the cut mechanism

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

Computing m

The procedure

1

relation

ï maxl X, Y, Ma

where Max: X

This correspond max(X, Y, X) max(X, Y, Y)

These two rules ¿

will fail. If the

economical forn

IfX>Ythen

  • therwise Ma

This is written ir

max(X, Y, X)

max( X, Y, Y).

It should be note

max(X,Y,Max) tt

incorrect use illr

?- max( 3, 1, 1)

yes

The following re

max( X, Y, Ma

X>:Y,!,M

5.2.2 Single-solutior

We have been ur

member( X, L)

Max: Y

Examples o

É"å;n

,..;:i::::

5.2.1

Therefore the cr

will be'invisible

will remain acti

130 Chapter 5 Controlling Backtracking

A more precise meaning of the cut mechanism is as follows:

Let us call the 'parent goal' the goal that mâtched the head of the clause

containing the cut. \Alhen 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 alternatives between the parent goal and the cut are discarded. To clarify this definition, consider a clause of the form:

H :- 81, 82,..., Bm, !,..., Bn. Let us assume that this clause was invoked by a goal G that matched H. Then G is the parent goal. At the moment that the cut is encountered, the system has already

found some solution of the goals 81,..., Bm. When the cut is executed, this

(current) solution of 81,..., Bm becomes frozen and all possible remaining

alternatives are discarded. Also, the goal G now becomes committed to this clause:

any attempt to match G with the head of some other clause is precluded.

Let us apply these rules to the following example:

C:-P,Q,R,!,S,T,U

C.:- V.

A:- B, C, D.

?- A.

Here A" Bf C, D, P, etc. have the syntax of terms. The cut will affect the execution of

the goal C as illustrated by Figure 5.3. Backtracking will be possible within the goal

list P, Q, R; however, as soon as the cut is reached, all alternative solutions of the

goal list P, Q, R are suppressed. The alternative clause about C,

C:-V. will also be discarded. However, backtracking will still be possible within the goal

list S, I U. The 'parent goal' of the clause containing the cut is the goal C in the

clause:

A:- B, C, D.

A

/.r

t."

s -rràc

D

P

a

  • ->_-.>--

<__ I <__ U

+

  • --> R---> S

Figure 5.3 The effect of the cut on the execution. Starting with A, the solid arrows

indicate the sequence of calls; the dashed affows indicate backtracking. There is 'one

way traffic'between R and S.

If Q fails, the query can succeed through V. If T fails, the query cannot reach V.

slide-29
SLIDE 29

Generalized De Morgan

  • ¬(∃R : (R)) ≡ (∀R : ¬(R))

Suppose you have a database of facts that includes all the good_food restaurants and all the expensive restaurants, if any. The database represents a poor neighborhood if there are no expensive restaurants. Prolog can determine the truth of the LHS, but it cannot determine the truth of the RHS.

slide-30
SLIDE 30

Generalized De Morgan

  • ¬(∃R : (R)) ≡ (∀R : ¬(R))

Even if R is not instantiated, Prolog can search for the existence of an expensive R with backtracking. If it succeeds in finding fact

expensive( geoffreys).

it has proved that the neighborhood is not poor. If it fails to find any expensive facts it has proved that the neighborhood is poor. Suppose you have a database of facts that includes all the good_food restaurants and all the expensive restaurants, if any. The database represents a poor neighborhood if there are no expensive restaurants. Prolog can determine the truth of the LHS, but it cannot determine the truth of the RHS.

slide-31
SLIDE 31

Generalized De Morgan

  • ¬(∃R : (R)) ≡ (∀R : ¬(R))

Even if R is not instantiated, Prolog can search for the existence of an expensive R with backtracking. If it succeeds in finding fact

expensive( geoffreys).

it has proved that the neighborhood is not poor. If it fails to find any expensive facts it has proved that the neighborhood is poor. Suppose you have a database of facts that includes all the good_food restaurants and all the expensive restaurants, if any. The database represents a poor neighborhood if there are no expensive restaurants. Prolog can determine the truth of the LHS, but it cannot determine the truth of the RHS. If R is not instantiated, Prolog cannot search over all the restaurants. It does not have a set of atoms to use in its search.