-
Almost every resource on Prolog is unanimous in that the cut operator should always be avoided where possible. I have yet to see clear guidance on where you should use the cut operator. I'm asking because as I go through the available learning resources, I will occasionally run across rules which feature the cut operator -- my mind does not know if I should reject this example and to rewrite it a different way or if the example provided is a good example of when to use the cut operator. So when should you use the cut operator -- or is it always avoidable? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
Every known computation can be expressed in Prolog without https://www.metalevel.at/prolog/showcases/turing.pl Thus, I find it hard to think of even a hypothetical case where one could justifiably say: "you SHOULD use One note about Prolog terminology: ?- current_op(Pri, Fix, !). false. |
Beta Was this translation helpful? Give feedback.
-
I'm afraid, not exactly/directly what you want (the clear guidance where one should use the cut; I, too, wanted once something like that), but there is an answer, or the answer (and many his answers) by @UWN (false) on stackoverflow: Knowing when to use cut in prolog Also, for me, an older paper: "Pruning in logic programming" by Lee Naish was helpful to get intuition. Unfortunately, the paper is not freely available. I read only a small part of it, but I had the "click" in my mind... For me, the key passage from the paper was: "[...] cut in Prolog, is best understood as the combination of two operations: • once prevents alternative solutions to calls before the cut being considered; I'm only a Prolog beginner (and not going to be more than a beginner) and I do not use the cut operator: I write only small programs in Prolog to run "off-line" and I want to see Prolog as a declarative programming language. Personally, I don't need Prolog to be a conventional programming language with the maximal possible efficiency (in the context of the full Prolog system). Of course, the paper has a section "The danger", the section I didn't read, I don't need it; I want declarative/relational/logical/simple to reasoning Prolog even for a beginner = without the cut. Here is the first sentence from "The danger" section: "A danger of adding pruning operators to a logic programming language is that the result is not a logic programming language." |
Beta Was this translation helpful? Give feedback.
Every known computation can be expressed in Prolog without
!/0
. One can show this by implementing a Turing machine in that subset of Prolog, which I have done with:https://www.metalevel.at/prolog/showcases/turing.pl
Thus, I find it hard to think of even a hypothetical case where one could justifiably say: "you SHOULD use
!/0
". With every known computation being expressible without using!/0
, where would such a justification come from? It seems far more likely that it can be expressed not only in one (which is certain to exist), but even in several different additional ways without using!/0
.One note about Prolog terminology:
!/0
is not an operator: