The Cut
The cut commits to the choices made since the parent goal was called:
13.1 What ! does
The cut commits to the choices made since the parent goal was called:
No other clauses of the current predicate are tried.
No alternative solutions for goals to the left of ! in the same clause.
max(X, Y, X) :- X >= Y, !.
max(_, Y, Y).
?- max(5, 3, M). % M = 5 (no choice point left)13.2 Green vs red cuts
Green cut: removes only redundant choice points; meaning is unchanged if removed.
Red cut: changes the meaning; removing it gives wrong results.
Safer version:
sign(X, pos) :- X > 0, !.
sign(X, zero) :- X =:= 0, !.
sign(X, neg) :- X < 0. % all conditions explicit → greenmax(X, Y, X) :- X >= Y, !.
max(_, Y, Y). % red: relies on the cut
?- max(5, 3, 3). % TRUE — wrong! Head unification happens before the cutmax(X, Y, Z) :- ( X >= Y -> Z = X ; Z = Y ).13.3 Cut-fail combination
(This is how \+ can be defined: \+ G :- call(G), !, fail. \+ _.)
different(X, X) :- !, fail.
different(_, _).13.4 When to use the cut
To express determinism (once a clause matches, stop).
To improve efficiency by removing useless choice points.
Prefer if-then-else, once/1, memberchk/2, or pure alternatives (dif/2, if_/3, CLP(FD) reification) where possible — they're clearer and keep predicates more general.