Cut (logic programming)

Cut (logic programming)

The cut, in Prolog, is a goal, written as !, which always succeeds, but cannot be backtracked past. It is best used to prevent unwanted backtracking, for example, to prevent extra solutions being found by Prolog and avoid additional computations that are not desired or required in a program.

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.

Contents

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 guarantee 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 for 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 guarantee the rule \+ gotmoney(X).

References

  1. ^ Foundations of Logic Programming, Springer (1984).

Tutorial introductions


Wikimedia Foundation. 2010.

Игры ⚽ Нужна курсовая?

Look at other dictionaries:

  • Cut — may refer to: The act of cutting, the separation of an object into two through acutely directed force Contents 1 Mathematics 2 Computing 3 …   Wikipedia

  • Logic — For other uses, see Logic (disambiguation). Philosophy …   Wikipedia

  • Cut-elimination theorem — The cut elimination theorem (or Gentzen s Hauptsatz) is the central result establishing the significance of the sequent calculus. It was originally proved by Gerhard Gentzen 1934 in his landmark paper Investigations in Logical Deduction for the… …   Wikipedia

  • Mercury (programming language) — For Mercury Autocode, see Autocode. Mercury Paradigm(s) Logic, functional Appeared in 1995 Designed by Zoltán Somogyi …   Wikipedia

  • Mathematical logic — (also known as symbolic logic) is a subfield of mathematics with close connections to foundations of mathematics, theoretical computer science and philosophical logic.[1] The field includes both the mathematical study of logic and the… …   Wikipedia

  • Resolution (logic) — In mathematical logic and automated theorem proving, resolution is a rule of inference leading to a refutation theorem proving technique for sentences in propositional logic and first order logic. In other words, iteratively applying the… …   Wikipedia

  • Linear logic — In mathematical logic, linear logic is a type of substructural logic that denies the structural rules of weakening and contraction . The interpretation is of hypotheses as resources : every hypothesis must be consumed exactly once in a proof.… …   Wikipedia

  • Subject-oriented programming — Programming paradigms Agent oriented Automata based Component based Flow based Pipelined Concatenative Concurrent computing …   Wikipedia

  • Programmable logic controller — A programmable logic controller (PLC) or programmable controller is a digital computer used for automation of industrial processes, such as control of machinery on factory assembly lines. PLCs are used in many different industries and machines… …   Wikipedia

  • Dynamic programming — For the programming paradigm, see Dynamic programming language. In mathematics and computer science, dynamic programming is a method for solving complex problems by breaking them down into simpler subproblems. It is applicable to problems… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”