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
3
votes
2 answers

In writing a purely relational prolog program, is it ok to use a carefully placed cut?

I am trying my hand in writing a relational prolog program that manages a key, value store. The initial code is taken from some lecture slides i found on the internet (http://people.eng.unimelb.edu.au/pstuckey/book/course.html -- see: using data…
user2193970
  • 345
  • 2
  • 10
3
votes
1 answer

Commutativity of Cut Operator in Prolog

I'm currently studying Prolog, and in one of the notes I'm reading an example is given of how to use the cut operator correctly. Consider the following function to remove all elements of a particular value from a list. rm(_,[],[]). rm(A,[A|L],R) :-…
Jacob Denson
  • 391
  • 1
  • 4
  • 14
3
votes
2 answers

Problems Understanding the Cut(!) Operator with example(Prolog Language)

I have the following rule: noRepetition([]). noRepetition([Elem|Rest]):- not(member(Elem,Rest)), !, noRepetition(Rest). This rule is made to decide if a list has no repeating elements in it and the member rule decides if a particular element…
JmanxC
  • 377
  • 2
  • 16
3
votes
1 answer

Prolog - understandig the use of cut

I can't understand clearly the use of cut. For example in this case: flatten, is it really needed? It works for me even without both cut predicates (I tried removing). What are the cases that can cause the backtracking going to the cut? Removing the…
rok
  • 2,574
  • 3
  • 23
  • 44
3
votes
2 answers

Prolog program: another way to define a functor

In my last exam I had to write a Prolog predicate called double/2, according to the following instructions: double(X, Y) should be true if Y is a list of integers of the same length of X, in which every even number of X is replace with its double. …
Matteo
  • 539
  • 3
  • 9
3
votes
4 answers

Cut(!) vs return

I am developing a predicate in Prolog and it is possible for it to terminate before the end of it. For that reason I was looking for a command similar to return; (C++). I used a cut ! but I'm doubtful as to what it literally stands for and if it…
GRoutar
  • 1,311
  • 1
  • 15
  • 38
3
votes
1 answer

How to pattern match wrapped variables in PROLOG without the cut?

I have a very simple question for Prolog programmers. This should be very easy, but I don't have any experience with that language so please help. I am trying to interpret some simple programming language in Prolog. In this language i can have two…
Tony Babarino
  • 3,355
  • 4
  • 32
  • 44
3
votes
2 answers

Using the Cut to Increase Efficiency

If I have the following knowledge base, how do I add a cut to the parent_of term so that if an X is already determined to be a father, prolog won't attempt to "check" if X is also a…
Charles Khunt
  • 2,375
  • 5
  • 25
  • 25
2
votes
2 answers

Is it possible to express Prolog's cut in first order logic?

In conversations around Prolog's cut operator !/0, I have only ever heard it described in terms of choice points and Prolog's execution model. It also seems to me that cut was introduced to give more "imperative" control over the search procedure,…
jweightman
  • 328
  • 1
  • 12
2
votes
0 answers

Clauses in (lambda)prolog starting with a cut

I am reading the paper Implementing Type Theory in Higher Order Constraint Logic Programming, and on p7 I see the following lambda-prolog code: % KAM-like rules in CPS style whd1 (app M N) S Ks Kf :- !, Ks [] M [N|S]. whd1 (lam T F1) [N|NS] Ks Kf :-…
2
votes
1 answer

Am I using "cut" too much?

I'm new to Prolog and logic programing in general, I'm writing a small theorem prover for fun, and in doing so I wrote a normalization procedure. I wanted this procedure to be deterministic and steadfast, so I wrote something like this : normal(S,…
Xenos
  • 155
  • 5
2
votes
1 answer

Meta-interpreting logical cut in Prolog (Edited to accommodate comments)

I am implementing a geometric theorem prover in Sicstus Prolog; and in order to get over the problem of backtracking over I/O, I am using a meta-interpreter. This last however does not seem to handle cuts as expected. My meta-interpreter looks like…
user10906061
2
votes
3 answers

Breaking between/3 based "loop" in SWI-Prolog while maintaining choice points that follow it

I need to iterate over a range of numbers (e.g. using between/3) from 1 to Length where Length is a length of a given list. Iterated number, say N, is then applied to a predicate, say do_something until it stops failing. That is, do_something…
Valera Grishin
  • 441
  • 3
  • 9
2
votes
2 answers

What does ! symbol mean in prolog?

I really need to know what does this symbol mean when comes after and symbol... For example look at this code glitter(yes) :- agent_location(X, Y), gold(X, Y), ! .
learner
  • 51
  • 2
  • 10
2
votes
2 answers

Prolog, about how to form better clauses

I have following clauses: num_parent(adam, X) :- !, X = 0. num_parent(eve, X) :- !, X = 0. num_parent(X, 2). When I typed the query: num_parent(eve,X). It only returns: X = 0. which is what I want. But when I typed this query:…