Professional Documents
Culture Documents
CUT
Prolog will automatically backtrack if this is necessary for satisfying a goal. Automatic
backtracking is a useful programming concept because it relieves the programmer 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.
What is cut?
f( X, 4) :- 6 =< X. % Rule 3
?- f(1,Y), 2 < Y.
When executing the first goal, f( 1, Y), Y becomes instantiated to 0. So, the second goal becomes 2 < 0, 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, the two other alternatives. The three rules about the f
relation are mutually exclusive so that one of them at most will succeed. Therefore we, not Prolog, know
that as soon as one rule succeeds there is no point in trying to use the others, as they are bound to fail. We
can therefore, tell Prolog explicitly not to backtrack using cut mechanism. The 'cut' is written as ! and is
inserted between goals as a kind of pseudo-goal.
f( X, 0) :- X < 3, !.
f( X, 2) :- 3 =< X, X < 6, !.
f( X, 4) :- 6 =< X.
The ! will prevent backtracking at the point that it appears in the program. If we now ask;
?- f( 1, Y), 2 < Y.
We will get 2 < 0 a condition that fails. Prolog will try to backtrack, but not beyond the
point marked with ! in the program. The alternative branches that corresponding to ‘Rule
2' and 'Rule 3' will not be generated.
More Examples.
1. Computing maximum.
The procedure for finding the larger of two numbers can be programmed as a relation.
max( X, Y, Max);
max( X, Y, X) :- X >= Y.
max( X, Y, Y) :- X < Y.
These two rules are mutually exclusive. If the first one succeeds then the second one will
fail. If the first one fails then the second must succeed. Therefore a more economical
formulation, with 'otherwise', is possible:
if X ≥ Y then max = X,
otherwise max = Y.
max( X, Y, X) :- X >= Y, !.
max( X, Y, Y).
2. Finding out if an integer is zero, negative or positive.
if X = 0, then X is zero.
integerType(0, zero) :- X = 0, !.
integerType(X, positive) .
Query:
Output:
negative.
Types of Cut.
Green cut – Removal of Cut does not change the meaning of the program. It will affect the
procedural meaning of the program but won’t affect the declarative meaning. Therefore,
it does not affect the readability of the program and is mainly used to avoid any wasted
computations.
Red cut – Alters the actual meaning of the program. It affects both the declarative and
procedural meaning of the program. It will affect the program’s readability, is used to
avoid any wasted computations, and introduces semantics. If not used cautiously, it can
affect the output in an arbitrary manner.
Advantages and disadvantages of cut.
Advantages:
We can specify mutual exclusive rules using cut. If Condition then Conclusion1 otherwise
Conclusion2.
Disadvantages:
In a program with cut a change in the order of the clauses may affect the declarative
meaning of the program. – Placing ! in the wrong place may affect what the program was
actually supposed to mean.
NEGATION
The concept of logical negation in Prolog is problematical, in the sense that the only
method that Prolog can use to tell if a proposition is false is to try to prove it (from the
facts and rules that it has been told about), and then if this attempt fails, it concludes that
the proposition is false. This is referred to as negation as failure.
An obvious problem is that Prolog may not have been told some critical fact or rule, so
that it will not be able to prove the proposition. In such a case, the falsity of the
proposition is only relative to the "mini-world-model" defined by the facts and rules
known to the Prolog interpreter. This is sometimes referred to as the closed-world
assumption.
Prolog is represented in modern Prolog interpreter using the symbol \+ which is supposed
to be a mnemonic for not provable with the \ standing for not and + for provable.
Examples:
1. round(sphere).
Queries:
?- round(earth).
?- \+ round(earth).
Output:
False
True.
2 ?- \+ (2 = 4).
True.
3 ?- not(2 = 4).
True.
not
not(P) :-
P, !, fail;
true.
Examples:
1. “Mary likes all animals but snakes”
Without using not.
likes(mary, X) :-
Snake(X), !, fail;
Animal(X).
With not:
likes(mary, X) :-
Animal(X),
not Snake(X).
2. Difference of X and Y.
X and Y are different if they do not match.
different(X, Y) :-
X = Y, !, fail;
true.
Using not.
different(X, Y) :-
not (X = Y).