Cut (logic programming)
The cut, in Prolog, is a goal, written as The cut should be used sparingly. While cuts can be inserted into code containing errors, 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.[1] Some programmers call the cut a controversial control facility[2] because it was added for efficiency reasons only and is not a logical formula. TypesGreen cutThe use of a cut that only improves efficiency is referred to as a green cut. Green cuts are used to make programs more efficient without changing program output. For example: gamble(X) :- gotmoney(X),!.
gamble(X) :- gotcredit(X), \+ gotmoney(X).
This is called a green cut operator. The ! tells the interpreter to stop looking for alternatives; however, if Red cutA cut that is not a green cut is referred to as a red cut, for example: gamble(X) :- gotmoney(X),!.
gamble(X) :- gotcredit(X).
Proper placement of the cut operator and the order of the rules are required to determine their logical meaning. If for any reason the first rule is removed (e.g. by a cut-and-paste accident) or moved after the second one, the second rule will be broken, i.e., it will not guarantee the rule Harmful cutA cut which affects the correctness of the program is a harmful cut. For example: min(X,Y,X) :- !, X =< Y. % the ! Is harmful because if Y<X
min(X,Y,Y) :- Y < X. % it will not let to continue to the second rule and will fail
References
|
Portal di Ensiklopedia Dunia