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
45
votes
5 answers

Knowing when to use cut in prolog

I've took a course in which I learned some prolog. I couldn't figure out how / when to use cuts. Even though I get the general idea of cuts, I can't seem to use them properly. Can anyone explain it briefly or give a good tutorial (that's not…
matanc1
  • 6,525
  • 6
  • 37
  • 57
20
votes
6 answers

Longest common prefix (LCP) of a list of strings

lcs([ H|L1],[ H|L2],[H|Lcs]) :- !, lcs(L1,L2,Lcs). lcs([H1|L1],[H2|L2],Lcs):- lcs( L1 ,[H2|L2],Lcs1), lcs([H1|L1], L2 ,Lcs2), longest(Lcs1,Lcs2,Lcs), !. lcs(_,_,[]). longest(L1,L2,Longest) :- length(L1,Length1), …
blazing
  • 557
  • 2
  • 4
  • 20
20
votes
1 answer

Prolog - differences between red cut and green cut

I started learning prolog, and wanted to make the whole cuts thing clearer. I have read that "green cut doesnt change declarative meaning of the program, while red cut does". But, the meaning of the program isnt really pure declarative (just from…
rooster
  • 665
  • 1
  • 6
  • 12
18
votes
3 answers

Parsing in Prolog without cut?

I found this nice snippet for parsing lisp in Prolog (from here): ws --> [W], { code_type(W, space) }, ws. ws --> []. parse(String, Expr) :- phrase(expressions(Expr), String). expressions([E|Es]) --> ws, expression(E), ws, !, % single…
dnolen
  • 18,496
  • 4
  • 62
  • 71
12
votes
4 answers

Prolog append with cut operator

What problem can occur when we use append with cut operator? append2([],L,L):-!. append2([H|T],L,[H|TL]):-append2(T,L,TL). I have tried several different inputs, but it always succeeds. ?- append2([1,2],[5],L). L = [1, 2, 5]. ?-…
Ali Ismayilov
  • 1,707
  • 3
  • 22
  • 37
11
votes
1 answer

What is "!" in Prolog

Could somebody explain me what does "!" do in Prolog ? I don't understand it. Here I have a code that counts how many sublists of a heterogeneous list have mountain aspect. nrSubliste([], 0). nrSubliste([H|T], R):- is_list(H), …
LauraW
  • 193
  • 1
  • 1
  • 9
10
votes
2 answers

Are cuts that bad in programming?

I am taking an AI course this semester in which we are learning Prolog. Our lecturer has told us to try and avoid using cuts in our assignment, however, for a couple of the questions I can't seem to avoid using them. I'm just curious why are cuts…
Achaldo
  • 163
  • 8
9
votes
4 answers

What is the difference in execution if the cut '!' is present?

counter([],[]). counter([H|T],[[H,C1]|R]) :- counter(T,[[H,C]|R]),!, C1 is C+1. counter([H|T],[[H,1]|R]) :- counter(T,R). What is the effect of the "!" as I'm getting the same output for an input in both the above and below code? counter([],[]). …
Student_2017
  • 101
  • 3
9
votes
2 answers

What does the "-" symbol mean in Prolog when dealing with lists?

I was reading the answer to this question, p(X) :- read(A), q(A,X-[]). q(end,X-X) :- !. q(A,[A|X]-Y) :- read(B), q(B,X-Y). The code above uses the syntax List-List. I somewhat understand what is going on, but I want to know what exactly what…
ivt
  • 301
  • 3
  • 9
8
votes
4 answers

Use cut in Prolog to define a once_member/2 function

Disclaimer: This is informal and non-assessed coursework to do in my own time. I have tried it myself, failed and am now looking for some guidance. I am trying to implement a version of the member/2 function which will only return members for a list…
Pete Hamilton
  • 7,730
  • 6
  • 33
  • 58
8
votes
2 answers

Prolog : avoid redundant choice points (non-determinism) with and without cut operator

Firstly, I have read all other posts on SO regarding the usage of cuts in Prolog and definitely see the issues related to using them. However, there's still some unclarity for me and I'd like to settle this once and for all. In the trivial example…
SND
  • 1,552
  • 2
  • 16
  • 29
8
votes
1 answer

Implementing cut in tracing meta interpreter prolog

I have this tracing meta interpreter, altered from previous question Prolog unbind bound variable. I don't understand how to interpret cut. Thanks to user @false who told me that the cut is badly implemented, my question is, how should I implement…
Đrakenus
  • 540
  • 3
  • 20
7
votes
2 answers

Reading a cut ! in Prolog

I am reading through Learn Prolog Now! 's chapter on cuts and at the same time Bratko's Prolog Programming for Artificial Intelligence, Chapter 5: Controlling Backtracking. At first it seemed that a cut was a straight-forward way to mimic an if-else…
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
7
votes
4 answers

Cut and Fail in Prolog

Consider the following code: a(X) :- b(X),!,c(X),fail. a(X) :- d(X). b(1). b(4). c(1). c(3). d(4). The query a(X). produces 1 ?- a(X). false. 2 ?- but with this code a(X) :- b(X),!,c(X). a(X) :- d(X). b(1). b(4). c(1). c(3). d(4). The…
JAN
  • 21,236
  • 66
  • 181
  • 318
7
votes
3 answers

Implementing "cut" in a recursive descent parser

I'm implementing a PEG parser generator in Python, and I've had success so far, except with the "cut" feature, of which whomever knows Prolog must know about. The idea is that after a cut (!) symbol has been parsed, then no alternative options…
Apalala
  • 9,017
  • 3
  • 30
  • 48
1
2 3 4 5 6