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
1
vote
1 answer

advice needed with prolog cut?

in this task i have a Prolog database filled with e.g. edge(1,0) edge(2,0) edge(1,3) an edge signifies that two points are joined. I am asked to write a function called reach(i,j,k) where i is the start point j is the end point and k is the number…
learner123
  • 961
  • 1
  • 10
  • 16
1
vote
1 answer

Prolog Cut operator

I defined my knowledge base as: edge(mammal,isa,animal). edge(human,isa,mammal). edge(simba,isa,human). edge(animal,swim,bybirth). edge(human,swim,mustlearn). path(X,Y) :- edge(X,isa,Y). path(X,Y) :- edge(X,isa,Z), path(Z,Y). swim(X,Y) :-…
Kevin
  • 25
  • 3
1
vote
2 answers

Using Cut and Not in Prolog Programming

I am new to Prolog Programming. So I want to know following. I know the use of Cut (!) in Prolog. Can somebody explain me, the use of Not in Prolog ,when and how to use Not & how to rewrite the following with out using Cut and avoiding the…
1
vote
1 answer

How to interpret this Prolog goal with a cut, and improve efficiency

I have been reading through the answers and comments of my previous question and I have tried applying the given explanations on an example from Bratko (Prolog Programming for Artificial Intelligence, p. 130), but I am not sure I understand it…
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
1
vote
0 answers

Refactoring cut in DCG, general principles? [answerd by comments]

Consider a clause with cut f(X) :- g(X), !, h(X). I think it is a common sense that we can refactor cuts as follows: h1(X) :- !, h(X). f(X) :- g(X), h1(X). However, for DCGs, this kind of macro-like refactoring for cuts doesn't seem to work. For…
kyagrd
  • 21
  • 4
1
vote
2 answers

Please explain the cut in the Bubblesort Prolog program?

I'm currently working trough the Bratko Prolog book and I am looking at the bubble-sort Program. I can't seem to figure out why the cut(!) is necessary. Say the cut isn't there, and Prolog would backtrack, how could it possibly find bad answers?…
Lieven Cardoen
  • 25,140
  • 52
  • 153
  • 244
1
vote
2 answers

Prolog cut operator behaviour

I have these clauses: a(1). a(2). b(a). c(A,B,C) :- a(A),d(B,C). c(A,B,C) :- b(A),d(B,C). d(B,C) :- a(B),!,a(C). d(B,_) :- b(B). When I run the query c(X,Y,Z) the answers are: X = 1, Y = 1, Z = 1 ; X = 1, Y = 1, Z = 2 ; X = 2, Y = 1, Z = 1 ; X = 2,…
Rayhunter
  • 243
  • 3
  • 9
1
vote
2 answers

how to prevent no in prolog?

Prolog, recursive function: i want it to print the C with each element of the list for example: C=30 and [H|T]= [-9,-10,-30] myfunc(C,[H|T]):- (\+([H|T]=[])), write(C), write(' with '), write(H), nl, myfunc(C,T). i check at the…
CSawy
  • 904
  • 2
  • 14
  • 25
0
votes
1 answer

The meaning of (:- !,) in Prolog

I am a newbie to Prolog and having a hard time understanding the meaning of the cut operator, !, in different contexts. For instance, what is the effect of it in this line here? Or how to read this line in plane English rewrite(p => q, F) :- !,…
XXX1010
  • 3
  • 2
0
votes
1 answer

How does the order of cut '!' symbol affect the backtracking in Prolog?

how does the order we put the cut symbol '!' inside a Prolog predicate affects the results? For example in this code: p(0,Y) :- !, Y is 0. p(X,Y) :- A is X-1, p(A,B), Y is B+X. When we test with a query like p(2,3). the program answers with true,…
ybouncer
  • 1
  • 2
0
votes
1 answer

Does Prolog's Cut predicate execute after a true/ yes call?

Say I have the following list of literals and a cut in my query: ?- c(X), d(X), e(X), !. if X=1 evaluates all the literals to true, does the cut, !, predicate, at the end, get called anyway or can the whole thing backtrack? From my own experience…
Connor
  • 867
  • 7
  • 18
0
votes
2 answers

Cut at the beginning of a clause

Consider the Prolog predicated p(integer),q(integer),r(integer) with the flow model (o) and the predicate s: p(1). q(1). r(1). p(2). q(2). r(2). s:-!,p(X),q(Y),r(Z),write(X,Y,Z),nl. Give the result of the following goal: s. Justify the answer. The…
0
votes
2 answers

Filling list (as an array) : without cut, backtracking leads to infinite recursion after giving a correct production

I need to initialize some lists with a finite number of elements, all the same. fill( 0, [], _ ) :- !. fill( Index, [Value|List], Value ) :- NextIndex is Index - 1, fill( NextIndex, List, Value ). The cut is mandatory to avoid infinite…
Aubin
  • 14,617
  • 9
  • 61
  • 84
0
votes
0 answers

Why does Cut doesn't prevent Visual Prolog from finding another solutions

If we use fail in a predicate after the cut (!), it somehow still finds another solutoins using backtrack, but I think it shouldn't. Consider the next example in Visual Prolog implement main open core, stdio class facts m : (integer…
Viacheslav Zhukov
  • 1,130
  • 9
  • 15
0
votes
2 answers

Choicepoint pruning needs a cut, but I think the compiler should be sharp enough to do it by itself

I'm doing an exercise of writing a between/3 that takes an additional step value. This is an interesting exercise, quickly showing: the advantage of tagged integers (i.e. use pos(X) instead of X if X is a positive integer to take advantage of…
David Tonhofer
  • 14,559
  • 5
  • 55
  • 51