The cut should be used sparingly. There is a temptation to insert cuts experimentally into code that is not working correctly. If a test is unnecessary because a cut has guaranteed that it is true, it is good practice to say so in a comment at the appropriate place.
It is described by some as a controversial control facility [1] because it was added for efficiency reasons only and isn't a Horn clause.
Types
Green Cut
A use of a cut which only improves efficiency is referred to as a green cut. For example:
gamble(X) :- gotmoney(X),!.
gamble(X) :- gotcredit(X), + gotmoney(X).
This is called a green cut operator. The ! simply tells the interpreter to stop looking for alternatives. But you'll notice that if gotmoney(X) fails it will check the second rule. Checking for gotmoney(X) in the second rule seems useless since you already know that if Prolog is there then gotmoney(X) failed before, otherwise the second rule wouldn't be evaluated in the first place. However, by explicitly writing + gotmoney(X), you guarrantee that the second rule will always work, even if the first one is removed by accident or changed.
Red Cut
A cut that isn't a green cut is referred as a red cut, for example:
gamble(X) :- gotmoney(X),!.
gamble(X) :- gotcredit(X).
You depend on the proper placement of the cut operator and the order of the rules to determine their logical meaning. If by any reason the first rule is removed (e.g. by a cut-and-paste accident), the second rule will be broken, i.e., it will not guarrantee the rule + gotmoney(X).
References
[1] - Foundations of Logic Programming, Springer (1984).
Tutorial introductions
- Learn Prolog Now! by Patrick Blackburn, Johan Bos and Kristina Striegnitz
This article is licensed under the GNU Free Documentation License.
Last updated on Friday January 25, 2008 at 01:04:58 PST (GMT -0800)
View this article at Wikipedia.org - Edit this article at Wikipedia.org - Donate to the Wikimedia Foundation
Copyright © 2008, Dictionary.com, LLC. All rights reserved.













