前言中的保护条款? [英] Guard clauses in prolog?

查看:91
本文介绍了前言中的保护条款?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它们存在吗?它们是如何实现的?

Do they exist? How are they implemented?

SWI-Prolog的关联谓词(freezewhendif等)具有警卫的功能.它们如何适合首选的Prolog编程风格?

The coroutining predicates of SWI-Prolog (freeze, when, dif etc.) have the functionality of guards. How do they fit in the preferred Prolog programming style?

我对逻辑编程(与Prolog一起使用)非常陌生,并且对它不是纯粹的声明性事实感到困惑,即使在非常简单的情况下,也需要过程上的考虑(请参见

I am very new to logic programming (with Prolog and altogether) and somewhat confused by the fact that it is not purely declarative, and requires procedural considerations even in very simple cases (see this question about using \== or dif). Am I missing something important?

推荐答案

首先是一个术语问题:在任何情况下,freeze/2when/2dif/2都不被称为警卫.防护出现在 CHR 或其他相关语言如

First a terminological issue: Neither freeze/2 nor when/2 nor dif/2 are called guards under any circumstance. Guards appear in such extensions as CHR, or related languages as GHC (link in Japanese) or other Concurrent logic programming languages ; you even (under certain restrictions) might consider clauses of the form

:-后卫, !, ...

在这种情况下,

as子句包含后卫和削减将被称为提交.但是,没有一个适用于上述基元.警卫颇受迪杰斯特拉(Dijkstra)1975年的警卫命令语言的启发.

as clauses containing a guard and the cut would be in this case rather called a commit. But none applies to above primitives. Guards are rather inspired by Dijkstra's Guarded Command Language of 1975.

freeze(X, Goal)(最初称为geler)与when(nonvar(X), Goal)相同,并且在声明上都等效于Goal.与防护装置的功能没有直接关系.但是,当与if-then-else一起使用时,您可以实现这种保护.但这是完全不同的.

freeze(X, Goal) (originally called geler) is the same as when(nonvar(X), Goal) and they both are declaratively equivalent to Goal. There is no direct relation to the functionality of guards. However, when used together with if-then-else you might implement such a guard. But that is pretty different.

freeze/2和类似的结构在一段时间内被视为改善Prolog执行机制的一般方法.但是,事实证明它们使用起来非常脆弱.通常,他们过于保守,因此不必要地拖延了目标.也就是说,几乎每个有趣的查询都会产生一个比目鱼"的答案,如下所示.而且,终止程序和非终止程序之间的界限现在要复杂得多.对于终止的纯单调Prolog程序,在程序中添加一些终止目标将保留整​​个程序的终止.但是,使用freeze/2不再是这种情况.然后从概念的角度来看,freeze/2并没有得到系统顶层的很好支持:只有少数系统以全面的方式(例如SICStus)显示了延迟的目标,这对于理解成功/答案与解决方案之间的差异至关重要.有了延迟的目标,Prolog现在可能会得出一个没有解决方案的答案:

freeze/2 and similar constructs were for some time considered as a general way to improve Prolog's execution mechanism. However, they turned out to be very brittle to use. Often, they were too conservative thus delaying goals unnecessarily. That is, almost every interesting query produced a "floundering" answer as the query below. Also, the borderline between terminating and non-terminating programs is now much more complex. For pure monotonic Prolog programs that terminate, adding some terminating goal into the program will preserve termination of the entire program. However, with freeze/2 this is no longer the case. Then from a conceptual viewpoint, freeze/2 was not very well supported by the toplevels of systems: Only a few systems showed the delayed goals in a comprehensive manner (e.g. SICStus) which is crucial to understand the difference between success/answers and solution. With delayed goals Prolog may now produce an answer that has no solution as this one:


?- freeze(X, X = 1), freeze(X, X = 2).
freeze(X, X=1),
freeze(X, X=2).

freeze/2的另一个困难是终止条件的确定要困难得多.因此,虽然freeze本来可以解决所有终止问题,但通常会产生新的问题.

Another difficulty with freeze/2 was that termination conditions are much more difficult to determine. So, while freeze was supposed to solve all the problems with termination, it often created new problems.

还有与freeze/2相关的更多技术难题,尤其是制表和其他防止循环的技术.清楚地考虑目标freeze(X, Y = 1),即使尚未绑定Y现在也是1,它仍然等待X首先绑定.现在,实现可以考虑为目标g(Y)制表. g(Y)现在将没有解决方案,或者只有一个解决方案Y = 1.由于目标无法直接看到目标freeze,因此该结果现在将作为g/1 解决方案存储.

And there are also more technical difficulties related to freeze/2 in particular w.r.t tabling and other techniques to prevent loops. Consider a goal freeze(X, Y = 1) clearly, Y is now 1 even if it is not yet bound, it still awaits X to be bound first. Now, an implementation might consider tabling for a goal g(Y). g(Y) will now have either no solution or exactly one solution Y = 1. This result would now be stored as the only solution for g/1 since the freeze-goal was not directly visible to the goal.

出于这种原因,freeze/2被视为约束逻辑编程的起点.

It is for such reasons that freeze/2 is considered the goto of constraint logic programming.

另一个问题是dif/2,今天已将其视为约束.与freeze/2和其他相关原语相比,约束可以更好地管理一致性并保持更好的终止属性.这主要是由于以下事实:约束条件引入了定义良好的语言,同时可以证明具体的属性,并且已经开发了特定的算法并且不允许总体目标.但是,即使对于他们,也有可能获得不是解决方案的答案.有关 CLP中的答案和成功的更多信息.

Another issue is dif/2 which today is considered a constraint. In contrast to freeze/2 and the other coroutining primitives, constraints are much better able to manage consistency and also maintain much better termination properties. This is primarily due to the fact that constraints introduce a well defined language were concrete properties can be proven and specific algorithms have been developed and do not permit general goals. However, even for them it is possible to obtain answers that are not solutions. More about answer and success in CLP.

这篇关于前言中的保护条款?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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