You are on page 1of 15

CUTTING AND NEGATION.

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?

Cut is a control facility that prevents backtracking by making it possible to formulate


mutually exclusive conclusion through rules of the form: if condition then conclusion1
otherwise conclusion2.
A program with backtracking.
 The relation between X and Y can be specified by three rules:

Rule 1: if X < 3 then Y = 0

Rule2: if 3 ≤ X and X < 6 then Y = 2

Rule3: if 6 < X then Y = 4

 This can be written in Prolog as a binary relation f(X, Y) as follows:

f(X, 0) :- X < 3. % Rule 1

f( X, 2) :- 3 =< X, X < 6. % Rule 2

f( X, 4) :- 6 =< X. % Rule 3

 This program assumes that before f( X, Y) is executed X is already instantiated to a number, as


this is required by the comparison operators.
Experiment 1:
 If we pose the question;

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

 Our program, rewritten with cuts, is:

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);

where max = X if X ≥ Y and max = Y if X < Y.

 The Prolog clauses:

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.

 This can written in Prolog using a cut as:

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.

if X < 0, then X is of integerType negative.

otherwise, X is of integerType positive.

Prolog with cut:

integerType(0, zero) :- X = 0, !.

integerType(X, negative) :- X < 0, !.

integerType(X, positive) .

Query:

?- integerType(-6, X), writeln(X).

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:

 It improves the efficiency of the program – No backtracking, which takes time.

 Enhances the expressive power of the language.

 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(X) is the way to implement negation in Prolog;


however not(X) does not mean that X is false, it means that X can't be proven true.
Prolog assumes that if it can’t prove an assertion, then the assertion is false.

 The not relation definition.

if Goal succeeds then not(Goal) fails,

otherwise not(Goal) succeeds.

 This can be written in Prolog as:

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.

 The key to saying this in Prolog is:

if X and Y match then different( X, Y) fails,

otherwise different( X, Y) succeeds.

Using cut and fail:

different(X, Y) :-

X = Y, !, fail;

true.

Using not.

different(X, Y) :-

not (X = Y).

You might also like