next up previous
Next: Map Colouring Up: Code Examples Previous: Cut Demonstration

More on Cuts

 

% Some test data on which we shall test various procedures which contain cuts.

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

bar(3).
bar(4).

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


% EXAMPLE 1 : Cut as final subgoal returns only one solution, although
%             several would be possible if the cut were removed.
%
%       | ?- foo(A, B).
%
%            A=2
%            B=3 ;
%       no

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

% EXAMPLE 2 : A third subgoal is added after the cut in the code from EXAMPLE 1
%             We now get two solutions because eek/2 is not frozen by the cut.
%
%       | ?- foo1(A, B, C).
%
%            A=2
%            B=3
%            C=a ;
%
%            A=2
%            B=3
%            C=b ;
%       no

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


% EXAMPLE 3 : Produces the same results as EXAMPLE 2 but using foo/2 as
%             a subgoal.
%
%       | ?- foo2(A, B, C).
%
%            A=2
%            B=3
%            C=a ;
%
%            A=2
%            B=3
%            C=b ;
%       no

foo2(A, B, C):-
        foo(A, B),
        eek(B, C).



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