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
0
votes
0 answers

Prolog: how to check if the currently evaluated clause was cut?

As in the title, for example test(X) :- ( was_cut -> X = yep ; X = nope ). test(X) :- !, ( was_cut -> X = yep ; X = nope ). test(X) :- X = none. Would yield ?- test(X) X = nope; X = yep. Asking certainly about SWI and/or SICSTUS…
radrow
  • 6,419
  • 4
  • 26
  • 53
0
votes
1 answer

Why doesn't prolog stop on cut?

I would like to get this result: ?- numberMatrixLines(1,[[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1]],X). X = [machinePenalty(1,[1,1,1,1,1,1,1,1]), machinePenalty(2 [1,1,1,1,1,1,1,1]), machinePenalty(3,[1,1,1,1,1,1,1,1])] I try…
Mandy
  • 145
  • 6
0
votes
0 answers

Prolog, result correctly given but false is printed

I have for example the following function computing the set of all internal nodes in a binary tree (nodes which have at least one child, i.e. are no leaves) internals(nil,[]). internals(t(_,nil,nil),[]). internals(t(X,L,nil),[X|S]) :- L = t(_,_,_),…
Wouter Vandenputte
  • 1,948
  • 4
  • 26
  • 50
0
votes
1 answer

Prolog: 'cut' in query vs. rules/facts

Doing exercise 10.4 on learnprolognow, could someone explain to me or help me visualize why for ?- p(X),p(Y) we get: X=1,Y=1; X=1,Y=2; X=2, Y=1; X=2, Y=1. And not just X=1, Y=1; X=1, Y=2. I think I'm misunderstanding how the cut happens, when it's…
user452306
  • 139
  • 4
  • 9
0
votes
0 answers

Prolog program to count down from a number using recursion

Lately I have been learning Prolog and tried to write a program to count down from a number to another given number using recursion but it seems to be not working, the code is as follows: count_down(L, L) :- !. count_down(L, H) :- write(H), nl, …
0
votes
0 answers

prolog logic and cut

I have a some problem understaing the flow of prolog. here is the code: h(X):- 1 is X mod 2, write(X), nl, 0 is X mod 3, !, fail. h(_). t(_,[],0). t(M,[_|LS],1):-member(M,LS),write('member'), nl,fail. t(_,[H|_],H). r([X|LS],R):-h(X), M is 2*X+1,t(M,…
0
votes
1 answer

How to use the cut operator?

I've been learning and I came across a problem. I understood the cut operator from the tutorials but I'm trying to solve a problem and I cannot understand the solution. Problem: If the car color is red, made in Italy, then it's a Ferrari. If it's…
user2559578
  • 143
  • 1
  • 10
0
votes
1 answer

How to prevent recursion after looping once

I just realized that was a dumb question. Curious if anyone can still find a loophole though. Source code: married(trump,obama). married(trump,goat). married(pepee,pepper). married(X,Y) :- married(Y,X),!. % not awesome because of infinite…
John Doe
  • 55
  • 1
  • 2
0
votes
1 answer

Prolog: "if then else", using cut

This is an easy question: I've seen this example in a Prolog text book. It is implementing if-then-else using a cut. if_then_else(P, Q, R) :- P, !, Q. if_then_else(P, Q, R) :- R. Can anyone explain what this program is doing, and why it is useful?
John
  • 303
  • 2
  • 12
0
votes
0 answers

Using cut, ERROR: Out of local stack

Here is my code, just focus on the 3 last rules: father(costac,anne). mother(mary,mike). husband(costac,mary). sex_male(costac). sex_male(mike). sex_female(mary). sex_female(anne). mother(X,Y):- father(Z,Y), …
user5572167
0
votes
2 answers

Stoping generator in first answer, use return instead

I am using too much to my taste the pattern (after every possible solution branch of the search). This is the code to find boggle words in given square. It had a bug if the words are not preselected to include only those whose letter pairs are…
Tony Veijalainen
  • 5,447
  • 23
  • 31
0
votes
2 answers

Prolog Use of Cuts

I am re-writing the following function in Prolog: V1: f(X,Y):- X < 2, Y is X+1. f(X,3):- 2 =< X, X < 5. f(X,Y):- 5 =< X, Y is 8-X. As V2: f(X,Y) :- X < 2, Y is X + 1. f(X,Y) :- X >= 2, X < 5, Y is 3. f(X,Y) :- X >= 5, Y…
MrD
  • 4,986
  • 11
  • 48
  • 90
-1
votes
1 answer

How does the '!' (cut) operator exactly work in Prolog?

I'm learning Prolog and I'm having some difficulties to understand how this particular operator works. I've been told it's used to stop backtracking during recursivity, but I can't quite picture it. What would be an example in a non-declarative…
jack
  • 9
  • 2
-2
votes
2 answers

Why do we use '!' in prolog

This is the code that i am trying to understand. co(X) :- co(X,[],L). co([],A,A):- write(A). co([X|Xs], A, L) :- p(X-Z,A,R), !, Z1 is Z+1, co(Xs, [X-Z1|R], L). co([X|Xs], A, L) :- co(Xs, [X-1|A], L). p(X-Y,[X-Y|R],R):- !. p(X,[H|Y], [H|Z]) :-…
Squirtle
  • 151
  • 1
  • 3
  • 11
1 2 3 4 5
6