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
6
votes
1 answer

Strange operator (!) in Prolog

hi(g,plus(A,B),int) :- hi(g,A,int),hi(g,B,int),!. in the above statement what does the '!' sign at the end of the statement do ?
user425243
6
votes
3 answers

Avoiding the use of cut in Prolog absolute value predicate

I have implemented the following function in prolog with the following code: abs2(X, Y) :- X < 0, Y is -X. abs2(X, X) :- X >= 0, !. How can I implement this function without the use of cut ("!")?
Olhovsky
  • 5,466
  • 3
  • 36
  • 47
6
votes
2 answers

Make Prolog return one solution and stop showing the query option

I'm new to prolog, and am experimenting with how to get it to stop querying after it finds one answer. I'm using this code: member1(L,[L|_]). member1(L,[_|RS]) :- member1(L,RS),!. The result is: | ?-…
user3026297
  • 63
  • 1
  • 1
  • 3
6
votes
1 answer

What are the optimal green cuts for successor arithmetics sum?

To grok green cuts in Prolog I am trying to add them to the standard definition of sum in successor arithmetics (see predicate plus in What's the SLD tree for this query?). The idea is to "clean up" the output as much as possible by eliminating all…
Pietro Braione
  • 1,123
  • 8
  • 22
5
votes
1 answer

Prolog -- better way to eliminate duplicate answer in special case?

I was having trouble with these two lines: list_swizzle(L, [], L). list_swizzle([], L, L). The problem was that if the both of the first two arguments are the empty list, the first two statements would both be used, returning the same answer.…
Nathan
  • 214
  • 2
  • 11
5
votes
4 answers

How can I simulate a soft cut in Prolog?

How can I simulate a soft cut I *-> T; E in ISO Prolog? I has side effects, so I can not call it multiple times. Except for the last requirement, I think the following definition works: if_(I, T, E) :- not(not(I)) -> call((I, T)); …
Ed McMan
  • 521
  • 2
  • 15
5
votes
4 answers

How to check if any statisfying clause exists in Prolog without backtracking through all different paths?

Let's say I have the following: parent(alice, charlie). parent(bob, charlie). parent(bob, diane). parent(alice, diane). parent(bob, eve). parent(alice, eve). % people are siblings of each other if they share a parent % and aren't the same…
SQB
  • 3,926
  • 2
  • 28
  • 49
4
votes
1 answer

Is !/0 supposed to cut through (\+)/1 or not?

On the one hand: $ sicstus SICStus 4.6.0 (x86_64-linux-glibc2.17): Mon Apr 6 09:23:37 PDT 2020 [...] | ?- \+ (!,false) ; X = 1. yes ... on the other hand ... $ gprolog GNU Prolog 1.4.5 (64 bits) [...] | ?- \+ (!,false) ; X = 1. true ? ; X =…
repeat
  • 18,496
  • 4
  • 54
  • 166
4
votes
1 answer

swi prolog: conjunction and cut

I am trying to implement a prolog interpreter in java. I am trying to figure out how the ',' operator should work. I tried to implement an equivalent rule like this: and(A, B) :- A, B. I am testing my implementation based on the logic base below…
wolare
  • 41
  • 2
4
votes
2 answers

Inserting a cut in Prolog to make a relation clause bound but bidirectional

Consider the following Prolog program: transform([], []). transform([X | Xs],[Y | Ys]):- isSpecial(X), isSpecial(Y), transformSpecial(X,Y), transform(Xs,Ys). transform([X | Xs],[ X | Ys]):- …
Jsevillamol
  • 2,425
  • 2
  • 23
  • 46
4
votes
1 answer

How does pruning choice points in the code below make it more efficient (Prolog)?

In the code given below, there is the ! (cut) that prunes the choice point for efficiency. I am pretty certain that the reverse predicate and the agent_do_moves predicate are essential. solve_task(Task,Cost):- …
SDG
  • 2,260
  • 8
  • 35
  • 77
4
votes
4 answers

Prolog, fail and do not backtrack

Is there any build-in predicate in SWI-Prolog that will always fail AND prevent machine from backtracking - it is stop the program from executing immediately (this is not what fail/0 does)? I could use cuts, but I don't like them. Doing something…
Jerry
  • 279
  • 3
  • 7
4
votes
1 answer

Optimize program using green and red cuts

The following Prolog program determines the insurance premium. The insurance premium depends on the vehicle's horsepower and the driver's age. calculateCarInsurance(PS, Insurance) :- PS < 60, Insurance = 100. calculateCarInsurance(PS, Insurance) :-…
4
votes
1 answer

Cuts at the end of recursive predicates in Prolog

pred(Args). pred(Args) :- goalA, goalB, !, pred(Args). pred(Args) :- goalC, goalD, !, pred(Args). Normally I would write a recursive predicate that was concerned with memory performance something along the lines of…
nedned
  • 3,552
  • 8
  • 38
  • 41
4
votes
3 answers

What's the SLD tree for this query?

Let's consider the following Prolog program (from "The Art of Prolog"): natural_number(0). natural_number(s(X)) :- natural_number(X). plus(X, 0, X) :- natural_number(X). plus(X, s(Y), s(Z)) :- plus(X, Y, Z). and the query: ?- plus(s(s(s(0))),…
Pietro Braione
  • 1,123
  • 8
  • 22