知道何时在 prolog 中使用 cut [英] Knowing when to use cut in prolog

查看:12
本文介绍了知道何时在 prolog 中使用 cut的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我参加了一门课程,在其中学习了一些序言.我不知道如何/何时使用削减.即使我了解削减的一般概念,但我似乎无法正确使用它们.任何人都可以简要解释一下或提供一个关于他们可以推荐的削减"的好教程(不是 learnprolognow.org)吗?

I've took a course in which I learned some prolog. I couldn't figure out how / when to use cuts. Even though I get the general idea of cuts, I can't seem to use them properly. Can anyone explain it briefly or give a good tutorial (that's not learnprolognow.org) on "cuts" that they can recommend?

推荐答案

TL;DR: 不要.

cut 修剪 Prolog 的搜索树.也就是说,给定一个没有切割的纯 Prolog 程序和有切割的相同程序,唯一的区别是有切割的程序可能在无结果的分支上花费的时间更少,因此效率更高;可能有更少的答案;它也可能会终止,而原始程序不会.

The cut prunes Prolog's search tree. That is, given a pure Prolog program without cut and the same program with cuts the only difference is that the program with cuts might spend less time in fruitless branches, and thus is more efficient ; might have fewer answers ; it might also terminate whereas the original program doesn't.

听起来很无害……甚至很有用,不是吗?嗯,大多数时候事情都比较复杂.

Sounds pretty harmless ... or even useful, doesn't it? Well, most of the time things are more complex.

剪辑通常以这样一种方式使用,即没有剪辑的程序根本没有任何有意义的意义.这种切割称为红色切割.在更好的情况下,它用于实现非单调否定的粗略形式.而在其他一些情况下,它一半是否定,一半是非常难以理解的程序意义.不仅适用于程序的读者,也适用于其作者.事实上,这种用法常常在无意中缺乏坚定性.在任何情况下:这些剪辑不会放置到现有程序中.他们应该从一开始就参与该计划.

Cuts are often used in a way such that the program without cuts has no sensible meaning at all. Such cuts are called red cuts. In the better cases it is used to implement a crude form of non-monotonic negation. And in some other cases it is half negation, half some procedural meaning that is very difficult to understand. Not only for the reader of the program but also for its writer. In fact, often such uses unintentionally lack steadfastness. In any case: these cuts are not placed into an existing program. They are meant to be in that program right from the beginning.

对于此类红色剪切的更结构化用法,最好使用 once/1(+)/1(;)/2 –if-then-else 像 ( If -> Then ; Else ) 代替.更好的是,尝试通过发出 instantiation_error 来防止此类构造被意外使用.或使用产生实例化错误的 iwhen/2when/2(在 SWI、YAP、SICStus 中提供)延迟目标.

For the more structured uses of such red cuts, better use once/1, (+)/1, or (;)/2 – if-then-else like ( If -> Then ; Else ) instead. Even better, try to guard such constructs against unintended uses by issuing instantiation_errors. Or use iwhen/2 which produces instantiation errors or when/2 (offered in SWI, YAP, SICStus) which delays goals.

删除无用选择点(以及冗余答案)的剪切称为绿色剪切.但请注意:您不能将它们放入您的程序中,只需按 ! 和一些 #00ff00.大多数情况下,您需要一个干净的只读保护程序来确保此剪切不会导致 #ff0000.还有一种简单的方法可以安全地删除一些剩余的选择点:call_semidet/1.以下是一些相关案例:

Cuts that remove useless choicepoints (and also redundant answers) are called green cuts. But beware: You cannot place them into your program simply pressing ! and some #00ff00. Most of the time you need a clean read-only guard to ensure that there is no way this cut turns #ff0000. There is also a simple way to remove some leftover choicepoints safely: call_semidet/1. Here are some related cases:

Prolog append with cut operator

什么是后继算术和的最佳绿色切割?

实现last"在序言中

最后,让我指出 cut 不是提交操作符.它有时有点像它,但需要很多限制才能成为一个.提交操作符不能(ab)用于实现 (+)/1.提交要求每个子句彼此独立地尝试.因此,每个子句都需要一个完整的保护;它不能依赖于仅在先尝试了其他一些条款之后才进行尝试.此外,必须在谓词的每个子句中发生提交.切割可以发生在任何地方.

Finally, let me point out that cut is not a commit-operator. It sometimes acts a bit like it, but would need lots of restrictions to be one. A commit-operator cannot be (ab)used to implement (+)/1. A commit requires that each clause is tried independently of each other. Each clause thus needs a full guard ; it cannot rely on being tried only after some other clauses have been tried first. Also, a commit would have to occur in each clause of a predicate. The cut can occur anywhere.

这篇关于知道何时在 prolog 中使用 cut的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆