0

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, and even when we try this p(V,6) the program answers with false But when we reverse the order of the cut symbol, like this :

p(0,Y) :- Y is 0, !.
p(X,Y) :- A is X-1, p(A,B), Y is B+X.

The program answers correctly the first query, but for the second one it returns the famous "Arguments are not sufficiently instantiated" error.

false
  • 10,264
  • 13
  • 101
  • 209
ybouncer
  • 1
  • 2

1 Answers1

1

As I answered in another question, even though its name is an exclamation mark instead of a word and it does something outside normal logic, cut is "just another predicate" in the sense that it gets called only if execution gets to it:

p(0,Y) :- Y is 0, !.

When you query p(V,2) the test Y is 0 fails, triggering backtracking, and the cut is never executed.

TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87