Cut operator scope when subsequent goals fail after unification succeeds
0 reputation · 19 Nov 2022, 11:29 UTC
0 reputation · 19 Nov 2022, 11:29 UTC
In ISO Prolog and major implementations such as SWI-Prolog, the cut operator (!) discards choice points created since the parent goal was invoked. Unification of the clause head occurs before the cut is reached; if unification fails the cut is never executed and alternative clauses remain available. When unification succeeds but a later goal in the same clause fails, the cut has already removed the choice points for that clause and for any goals to its left.
The documentation states that the cut is local to the predicate in which it appears, yet the exact moment at which choice points are destroyed—relative to the failure of a goal positioned after the cut—affects whether the engine can backtrack into earlier disjunctions or sibling clauses. This boundary determines whether a program exhibits green-cut (efficiency-only) or red-cut (semantic-change) behavior.
35275 reputation · 19 Nov 2022, 14:03 UTC
!) is executed during forward resolution, not when a later goal fails.;) or if-then-else (->) entered after the head unified. It does not remove choice points that existed before the predicate was called (i.e., the caller's context).p :- !, fail. and p :- true. then query ?- p. If the query succeeds, the cut is green (only intra-clause pruning). If it fails, the cut is red (inter-clause pruning, ISO standard).When a clause head unifies, the engine creates a choice point for the next clause of the same predicate. Execution proceeds into the clause body. Upon encountering !, the engine immediately discards every choice point created after the predicate invocation — including the clause-selection choice point. If a subsequent goal in that clause fails, there are no remaining choice points for this predicate, so the entire predicate fails. The engine does not backtrack into earlier clauses.
% File: cut_test.pl
p :- !, fail.
p :- true.
Run in any ISO-conforming Prolog:
?- [cut_test].
?- p.
ISO 13211-1 §7.8.3 defines cut/0 as discarding choice points created since the parent goal invocation. All major implementations conform to this predicate-local boundary.
(Condition -> Then ; Else), a cut in Then only prunes choice points created inside Then; the Else branch remains accessible if Then fails after the cut.Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.