!
cut
Entity: predicate

Usage:
!
cut(Depth)

cut(1) is the same as !
cut(0) is the same as true
cut(0.5) cuts less than !
cut(2) cuts more than !

In Prolog the functions and predicates are multi-valued and can return more than one answer. Anyway, sometimes we need only the first answer and we want to take rid of the rest of them. For this purpose we use the predicate cut. The parameter Depth specifies which answers will be cut.

Let us have the clause:

head :-
  q1, q2, …, qn, cut(Depth), s1, s2, … , sk.

The cut predicate will affect only the predicates which are before it. So, it will not affect the predicates s1, s2, … , sk. If the Depth is 0.5 we will receive only the first answer of the conjunction q1, q2, …, qn but we will receive all possible answers of the head. If the Depth is 1 all other answers of the head will be cut. If the Depth is bigger than 1 the cut will affect also the predicates and heads which are higher in the proof tree.

In the second example the Depth parameter of cut is 2.5 instead of 1.5 because in the definition of -> (if-then-else) predicate there is included one call predicate which increases the depth by one. Look at Standard.add file to see the definition of -> (if-then-else). For the same reason the depth in the example db_read.pro is 3 instead of 2.

Example:
?-
  do(X), write(X), nl, fail.

do(X):-
  X is sub_string("abcd", _, 2),
  cut(0.5). % try this with 0, 1 and 3
do(X):-
  X="end".

Example:
?-
  X="", do(X), write(X), nl.

do(X):-
  repeat,
  read(Y, "Enter DATA", s),
  X:=X+Y,
  (not(yes_no("", "Do you have more data", ?))->
    cut(2.5), fail
  ),
  fail.
do(_).

See also:
once

Examples:
 OLE / db_read.pro