next up previous
Next: Some Common Techniques Up: Cuts Previous: Using Cuts to

Just One More Thing About Cuts

At the start of the section on cuts, we said that they pruned the search space by telling the interpreter, once it has hit a cut, to forget about any other definitions for the predicate in which the cut appears. The cut also has another effect: it tells the interpreter not to look for any new solutions to any subgoals appearing in the body of the clause before the cut. The best way to explain this is by using an example, which you can find in Section 13.3.

Let's start with some test data:

baz(1, 2).
baz(2, 3).
baz(3, 4).

bar(3).
bar(4).

eek(3, a).
eek(3, b).
eek(4, c).

Now we define a predicate, foo/2, as follows:

foo(A, B):-
        baz(A, B),
        bar(B), !.

The cut at the end of this clause will commit to a single solution for baz(A, B) and for bar(B), so the behaviour of this program is:

| ?- foo(A, B).
     A=2
     B=3 ;
no

Let's now invent a new predicate, foo1/2:

foo1(A, B, C):-
        baz(A, B),
        bar(B), !,
        eek(B, C).

This has an extra subgoal after the cut, so the subgoals before the cut will be ``frozen'', as before, but the interpreter is free to generate new solutions for the subgoal after the cut. Thus the behaviour is:

| ?- foo1(A, B, C).
     A=2
     B=3
     C=a ;
     A=2
     B=3
     C=b ;
no



Dave Stuart Robertson
Tue Jul 7 10:44:26 BST 1998