$= The cut operator \prologindex{\it cut} The cut operator may be used to reduce the amount of backtracking. As a manner of speaking, the cut is a means to make a program more determinate. Consider as an example the following definition of the predicate member. \pprog{member}{ .ds cmbr.pl } The effect of the cut in the first clause, written as an exclamation mark, is to cut off the search for an alternative solution after encountering for the first time an element of the list that is equal to the given item. A drawback of the use of a cut in this example is that the clauses for member can no longer be used in a relational way to generate all the elements of a list in succession, since the search for further solutions will be stopped after binding the variable to the front element of the list. Another example in which the cut is used to enforce functional behavior is given in the clauses computing the sign of a numerical value as either 1, 0 or -1. \pprog{sign}{ .ds csign.pl } In this definition the use of the cut could have been avoided by introducing a condition in the third clause, thus enforcing determinate behavior by explicit tests. The cut may be used for efficiency since once a clause for sign is successfully applied no further clauses will be tried. The dynamic effect of using a cut can be best explained in terms of the SLD-search strategy. Recall that when a goal literal is evaluated, all the clauses whose head matches the literal may be tried to find a solution. However, if a cut occurs in the body of one of these clauses, all the alternative clauses following that particular clause will be forgotten as soon as that cut becomes the left-most goal literal. In other words, all the alternatives immediately to the right of that clause will be pruned from the SLD-tree. Apart from deleting these alternatives, the cut also results in abstaining from the search to alternative solutions for the literals immediately preceding the cut in the body of the clause. As an example, consider the program \pprog{cut}{ .ds cut.pl } The goal ?- q(X). fails since after binding X to 0 by evaluating p(X) in the first clause for q, the cut prevents finding an alternative solution for p(X) when r(0) fails, and also prevents to try the second clause for q. In contrast, the goals ?- q(1). ?- q(2). both succeed, as is easily verified.
?- q(1). ?- q(2).