The Cut Operator
The cut, written !, commits Prolog to all choices made since entering the current clause. When execution passes through it, it removes the choice point for trying alternative clauses of the current predicate call, as well as the choice points of any goals to its left in the same clause body, preventing backtracking into them.
Cricket analogy: Once the third umpire's decision is confirmed as 'out', the on-field discussion is over -- no more appeals for that ball are considered -- just as Prolog's cut locks in the choices made so far and removes the choice points that could reopen them.
Green Cuts vs Red Cuts
A green cut is a pure optimization: it prunes away branches that would have failed anyway, so removing it wouldn't change the set of solutions a predicate can produce, only how fast it finds them. A red cut, by contrast, actually changes the program's logical meaning -- removing it would change which solutions are returned -- and is what makes idioms like if-then-else work correctly.
Cricket analogy: Choosing not to review a plumb lbw because no camera angle could possibly overturn it is a 'green cut' -- an efficiency choice that changes nothing about the outcome, unlike overturning a fair decision on a technicality, which is a 'red cut' that actually changes the result.
Cut and the If-Then-Else Idiom
Prolog's built-in Cond -> Then ; Else construct uses cut semantics internally: once Cond succeeds, it commits to that branch and never backtracks into Cond or tries Else. The same effect can be built explicitly from ordinary clauses, each ending in a cut, to create mutually exclusive classification logic where only one branch is ever taken for a given input.
Cricket analogy: Classifying a delivery as 'wide', then 'no-ball', then defaulting to 'legal' -- checking each condition in order and stopping at the first match -- mirrors the classify/2 pattern where each clause's cut commits to that branch and skips the rest.
classify(X, negative) :-
X < 0,
!.
classify(X, zero) :-
X =:= 0,
!.
classify(_, positive).
?- classify(-5, R).
R = negative.
?- classify(0, R).
R = zero.
?- classify(7, R).
R = positive.Common Pitfalls With Cut
A cut placed too early in a clause can prune away choice points before all valid alternatives have actually been explored, silently discarding correct solutions instead of just improving efficiency. Because cut also interacts with constructs like findall/3, bagof/3, and negation as failure, overusing it makes a predicate order- and mode-sensitive, and harder to reason about as a purely logical statement.
Cricket analogy: Declaring the match over after one boundary because it 'felt decisive' would wrongly discard overs of legitimate play still to come -- the same mistake a cut placed too early makes when it prunes away valid solutions Prolog hadn't reached yet.
Because cut removes choice points, a predicate that relies on it stops being purely declarative -- it can no longer be safely reordered, reused in a different mode (e.g., calling classify/2 with the second argument bound instead of the first), or composed with findall/bagof/setof without care, since those collect solutions and an ill-placed cut can silently return fewer than expected. Before adding !, write out on paper exactly which choice points it will remove, and test the predicate with all the argument-binding patterns you actually intend to use.
- The cut (!) commits Prolog to all choices made since entering the current clause, removing the parent goal's remaining alternatives and any choice points from goals to its left in the same body.
- A green cut is a pure optimization that doesn't change which solutions a predicate can produce.
- A red cut changes the program's logical meaning and is required for the predicate's intended behavior, such as classic if-then-else style predicates.
- The if-then-else idiom (Cond -> Then ; Else) uses cut internally to make branches mutually exclusive.
- Cut interacts with control constructs like findall/3, bagof/3, and negation as failure, and can change how many results they collect.
- Placing a cut too early in a clause can prune away solutions that were still logically valid.
- Predicates that rely heavily on cut become less flexible and less declarative, and should be documented clearly.
Practice what you learned
1. What does the cut (!) operator do when execution passes through it?
2. What is the difference between a green cut and a red cut?
3. In the classify/2 example using cut, why is ! needed after each condition check?
4. What risk does placing a cut too early in a clause create?
5. Why does relying on cut make a predicate less declarative?
Was this page helpful?
You May Also Like
Backtracking Explained
How Prolog recovers from failed goals and finds multiple solutions by returning to choice points, and what that means for how programs actually execute.
Negation as Failure
How Prolog's negation-as-failure (\+) works, why it reflects a closed-world assumption rather than classical logical negation, and the pitfalls of negating unbound goals.
Unification in Prolog
How Prolog's core matching mechanism works: when two terms unify, how atoms, numbers, variables, and compound terms are compared, and why the occurs check matters.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics