Questions tagged [prolog-cut]

Cut, spelled as an exclamation mark "!", is a special goal that serves as a control construct in the Prolog programming language.

The cut goal, written as ! in Prolog, is the main control operator. It always succeeds, and as a side effect prevents the current goal from backtracking before it.

left(a).
left(b).
right(x).
right(y).

no_cut(I,J)   :- left(I),    right(J).
with_cut(I,J) :- left(I), !, right(J).

With this setup, no_cut(I,J) succeeds normally for all (a/b, x/y) pairs.
with_cut(I,J) will only succeed for pairs with a: left will first succeed with a, the cut will succeed, and right will succeed with x. Then backtracking will have right succeed with y as well.

Without a cut, backtracking would retry left. But the cut pruned that part of the tree, so the search is over.

A cut doesn't affect the parent predicate's tree:

inner_cut(I,J) :- left(I), with_cut(I,J).

inner_cut is equivalent to no_cut.

Cuts can be used to improve efficiency when the programmer knows in advance that a clause can't succeed if another did. Such cuts are called green cuts:

green_cut(X): predicate1(X), !.
green_cut(X): predicate2(X), predicate3(X), ..., \+ predicate1(X).

Cuts that do change the outcome are called red cuts.

89 questions
2
votes
1 answer

Cutting the beginning of a clause and the relation between "cut", `!`, and `fail`

What does it mean to put the cut (!) at the very beginning of a clause? p(X,Y) :- !, q(X), r(X,Y). What is the difference between ! and fail and how are they related? thanks. I'm thinking that for fail, the predicate will just "fail" lol which…
Apex Predator
  • 171
  • 1
  • 3
  • 13
2
votes
1 answer

Cut on a higher level in Prolog

Say you have a program: a(X) :- b(X). a(X) :- c(X). b(a). b(b) :- !,fail. b(c). c(a). A query a(X) will return A=a; X=a. I'm looking however for a mechanism where the cut (!) does not only prevent one from further executing predicates in the same…
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
2
votes
2 answers

Prolog- How does a cut work in combination with OR operator?

I have written this little program: married(guy1, fem1). married(guy2, fem2). married_to(X,Y):-!, married(X,Y); married(Y,X). Prints: X = guy1, Y = fem1 ; X = guy2, Y = fem2. My purpose was to print the married couples but one time each. The…
user3309479
  • 329
  • 1
  • 5
  • 17
2
votes
2 answers

Finding query for which a prolog program gives incorrect result

This Prolog program defines the third argument to be the maximum value of the first two numeric arguments: max(X, Y, X) :- X >= Y, !. max(X, Y, Y). I think that this program works just fine. But I am told that it can give incorrect result. Can you…
Rayhunter
  • 243
  • 3
  • 9
2
votes
1 answer

Prolog cut behaviour doesn't make sense to me

I've come across very strange behaviour (to me) regarding a particular cut. From what I understood, once the execution passes a cut, it cannot backtrack back above it. But that is exactly what this code does. Can someone explain why it does…
kbirk
  • 3,906
  • 7
  • 48
  • 72
2
votes
4 answers

Removing duplicate solutions

My code merges two lists of lists, item by item, in the following way: mergeL([[a,b],[c,d]], [[1,2],[3,4]], Result). Result = [[a,b,1,2],[c,d,3,4]] And this is the code i use: mergeL([],[],[]). mergeL(List, [], List). mergeL([], List,…
Enoon
  • 421
  • 4
  • 17
1
vote
2 answers

committing to choices in the scope of catch/3

The following is small prolog meta-interpreter that interprets the cut using the catch/throw mechanism[1]. fruit(apple). fruit(orange) :- !. fruit(banana). % meta-interpreter that handles the cut prove(true) :- !. prove(!) :- !, ( true ; throw(cut)…
Penelope
  • 291
  • 6
1
vote
0 answers

handling the cut with a simple meta-interpreter

The following is a simple meta-interpreter, as seen in many guides/textbooks. prove(true) :- !. prove((A,B)):- !, prove(A), prove(B). prove(H) :- clause(H,B), prove(B), write(H), write(" <- "), writeln(B). The following is a simple program to…
Penelope
  • 291
  • 6
1
vote
3 answers

Non-termination of common reverse/2 implementation, and better solutions?

The following is a standard textbook definition of reverse(X,Y) which is true if the list Y is the reverse of the list X. The code is often used to introduce or illustrate the use of an accumulator. % recursive definition step([], L2,…
1
vote
1 answer

What is the role of cut at the base case?

gcd(X,X,X):- !. gcd(X,Y,Z):- X>Y, !, Inter is X - Y, gcd(Inter, Y, Z). gcd(X,Y,Z):- Inter is Y - X, gcd(X,Inter,Z). I get the if else nature of the second cut but I don't understand why program just aborts with the first…
Lin
  • 47
  • 5
1
vote
1 answer

Prolog Cut Query

In Prolog using the cut. Why is the effect of the following query to return the following: ?- !,false. no yet this query returns the following: ?- !;false. yes
General_9
  • 2,249
  • 4
  • 28
  • 46
1
vote
1 answer

Prolog recursion overflow

fact(1,1):-!. fact(N,F):- N1=N-1, fact(N1,F1), F=F1*N. It leads to the stackoverflow(not the site)! It shouldn't because of the cut(!). Does it work in SWI-Prolog?
Sergey
  • 11,548
  • 24
  • 76
  • 113
1
vote
1 answer

Cut and fail semantics in Prolog

What does the following code do? not(P) :- P, !, fail. not(P). And how does that work differently from the following 2 codes : not(P) :- P, !, fail. not(P) :- P, !.
sai_varshittha
  • 193
  • 1
  • 12
1
vote
1 answer

Cut at the end of a Prolog statement

I have encountered this cut which should return true if there exists an edge A-B or B-A for some node B of graph Graph. node(A,Graph) :- adjacent(A,_,Graph),!. The problem is I do not understand why removing this cut would have any effects on the…
Slazer
  • 4,750
  • 7
  • 33
  • 60
1
vote
0 answers

preventing backtracking without cut

Fairly new to prolog and trying to solve some exercises. Given a knowledge base of components component(ElementX,ElementY,Qty). ElementX uses ElementY in it's structure in quantity Qty. component(car, door, 4). component(car, wheel,…
idetodospoca
  • 73
  • 3
  • 12