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

查看:35
本文介绍了知道何时在序言中使用 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 的使用方式常常是,没有cuts 的程序根本没有任何意义.这种切割称为红色切割.在更好的情况下,它用于实现非单调否定的粗略形式.在其他一些情况下,它一半是否定,一半是非常难以理解的程序含义.不仅是为了程序的读者,也是为了程序的作者.事实上,这样的使用往往会无意中缺乏坚定性.在任何情况下:这些剪辑都不会放置到现有程序中.他们应该从一开始就加入该计划.

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/2 产生实例化错误或 when/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_semide/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 追加

什么是后继算术总和的最佳绿色削减?

实现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.

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

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