所谓的“逻辑纯度"是指.在Prolog中? [英] What is meant by "logical purity" in Prolog?

查看:68
本文介绍了所谓的“逻辑纯度"是指.在Prolog中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

逻辑纯度"是什么意思(在Prolog编程中)? 标签信息的问题仅使用Horn子句的程序" ,但是,像 if_/3 这样的谓词又如何呢? ,使用尽可能多的剪切以及各种元逻辑(正确的术语是什么?var/1等)谓语,即低级内容.

What is meant by "logical purity" (in the context of Prolog programming)? The logical-purity tag info says "programs using only Horn clauses", but then, how would predicates like if_/3 qualify, using as much as it does the cut, and the various meta-logical (what's the proper terminology? var/1 and such) predicates, i.e. the low-level stuff.

我知道它可以达到某种纯粹"的效果,但是,这到底是什么意思呢?

I get it that it achieves some "pure" effect, but what does this mean, precisely?

有关更具体的说明,请说明if_/3在逻辑上是纯净的,例如在使用中所见. 在此答案中?

For a more concrete illustration, please explain how does if_/3 qualify as logically pure, seen in use e.g. in this answer?

推荐答案

让我们首先习惯于逻辑程序的声明性阅读.

Let us first get used to a declarative reading of logic programs.

声明性地,Prolog程序声明什么是正确的.

Declaratively, a Prolog program states what is true.

例如

natural_number(0).
natural_number(s(X)) :-
        natural_number(X).

第一个子句指出:0是自然数.

The first clause states: 0 is a natural number.

第二个子句指出:如果 X是自然数,然后 s(X)是自然数.

The second clause states: If X is a natural number, then s(X) is a natural number.

现在让我们考虑更改此程序的影响.例如,当我们更改这两个子句的顺序时,会发生什么变化?

Let us now consider the effect of changes to this program. For example, what changes when we change the order of these two clauses?

natural_number(s(X)) :-
        natural_number(X).
natural_number(0).

声明性地,交换子句的顺序不会以任何方式改变程序的预期含义(析取是可交换的).

Declaratively, exchanging the order of clauses does not change the intended meaning of the program in any way (disjunction is commutative).

操作上,也就是说,考虑到Prolog的实际执行策略,显然不同的子句顺序通常会产生显着差异.

Operationally, that is, taking into account the actual execution strategy of Prolog, different clause orders clearly often make a signifcant difference.

但是,无论选择的子句顺序如何,纯Prolog代码的一个极好的特性都得以保留:

However, one extremely nice property of pure Prolog code is preserved regardless of chosen clause ordering:

如果针对子句O1的查询Q 成功,则 Q 不会失败,并具有不同的顺序O2.

If a query Q succeeds with respect to a clause ordering O1, then Q does not fail with a different ordering O2.

请注意,我不是 ,不是说Q总是也成功以不同的顺序进行:这是因为查询也可能循环或产生不同顺序的错误.

Note that I am not saying that Q always also succeeds with a different ordering: This is because the query may also loop or yield an error with different orderings.

对于两个查询Q1Q2,我们说G1更通用的,只要它在语法统一方面包含G2即可.例如,查询?- parent_child(P, C).比查询?- parent_child(0, s(0)).更通用.

For two queries Q1 and Q2, we say that G1 is more general iff it subsumes G2 with respect to syntactic unification. For example, the query ?- parent_child(P, C). is more general than the query ?- parent_child(0, s(0))..

现在,使用纯Prolog程序,可以拥有另一个非常不错的属性:

Now, with pure Prolog programs, another extremely nice property holds:

如果查询Q1成功,则每个更常见的查询Q2 都不会 失败.

If a query Q1 succeeds, then every more general query Q2 does not fail.

再次请注意,Q2可能会循环而不是成功.

Note, again, that Q2 may loop instead of succeeding.

现在考虑您提到的var/1的情况,并考虑相关谓词nonvar/1.假设我们有:

Consider now the case of var/1 which you mention, and think of the related predicate nonvar/1. Suppose we have:

my_pred(V) :-
        nonvar(V).

什么时候举行?显然,只要参数不是变量,它就成立.

When does this hold? Clearly, it holds iff the argument is not a variable.

按预期,我们得到:

?- my_pred(a).
true.

但是,对于更通用的查询?- my_pred(X).,我们得到:

However, for the more general query ?- my_pred(X)., we get:

?- my_pred(X).
false.

这样的谓词称为 non-monotonic ,由于该属性,您不能将其视为真实关系:这是因为上面的答案false在逻辑上意味着存在没有任何解决方案,但是在前面的示例中,我们看到有 一个解决方案.因此,从逻辑上讲,通过添加约束构建的更具体的查询使查询成功:

Such a predicate is called non-monotonic, and you cannot treat it as a true relation due to this property: This is because the answer false above logically means that there are no solutions whatsoever, yet in the immediately preceding example, we see that there is a solution. So, illogically, a more specific query, built by adding a constraint, makes the query succeed:

?- X = a, my_pred(X).
true.

因此,对此类谓词的推理极为复杂,以至于用它们进行编程根本毫无乐趣.它使声明式调试变得不可能,并且很难声明保留的任何属性.例如,仅在上述联合查询中交换子目标的顺序将使其失败:

Thus, reasoning about such predicates is extremely complicated, to the point that it is no fun at all to program with them. It makes declarative debugging impossible, and hard to state any properties that are preserved. For instance, just swapping the order of subgoals in the above conjunctive query will make it fail:

?- my_pred(X), X = a.
false.

因此,我强烈建议保留在Prolog的纯单调子集中,这允许按照上面概述的方式进行陈述性推理.

Hence, I strongly suggest to stay within the pure monotonic subset of Prolog, which allows the declarative reasoning along the lines outlined above.

CLP(FD)约束,dif/2等都是 pure :无论您使用哪种模式,顺序等,您都不能欺骗这些谓词以给出逻辑上无效的答案.使用它们. if_/3也满足此属性.另一方面,var/1nonvar/1integer/1!/0,具有副作用的谓词等都是在逻辑上引用正在描述的声明性世界之外的内容,因此不能被认为是纯.

CLP(FD) constraints, dif/2 etc. are all pure in this sense: You cannot trick these predicates into giving logically invalid answers, no matter the modes, orders etc. in which you use them. if_/3 also satisfies this property. On the other hand, var/1, nonvar/1, integer/1, !/0, predicates with side-effects etc. are all extra-logically referencing something outside the declarative world that is being described, and can thus not be considered pure.

编辑:澄清一下:我在这里提到的漂亮属性绝不是详尽无遗的.纯粹的Prolog代码还具有许多其他非常有价值的属性,通过它们您可以了解逻辑编程的荣耀.例如,在纯Prolog代码中,添加子句最多​​可以 extend ,而不能缩小解决方案的集合;添加 goal 最多只能 narrow ,永不扩展,等等.

EDIT: To clarify: The nice properties I mention here are in no way exhaustive. Pure Prolog code exhibits many other extremely valuable properties through which you can perceive the glory of logic programming. For example, in pure Prolog code, adding a clause can at most extend, never narrow, the set of solutions; adding a goal can at most narrow, never extend, it etc.

使用单个逻辑外原语可能并且通常会已经破坏了其中许多属性.因此,例如,每次使用!/0时,都应将其视为进入纯净内心的 cut ,并尝试为伤害这些属性感到遗憾和羞愧.

Using a single extra-logical primitive may, and typically will, already destroy many of these properties. Therefore, for example, every time you use !/0, consider it a cut right into the heart of purity, and try to feel regret and shame for wounding these properties.

好的Prolog书籍至少会开始介绍或包含许多鼓励这种声明性观点的提示,指导您考虑更一般的查询,保留的属性等.不好的Prolog书籍对此并不会多说,通常最终恰好使用了不纯净的语言元素,破坏了语言的最宝贵和最美丽的属性.

A good Prolog book will at least begin to introduce or contain many hints to encourage such a declarative view, guide you to think about more general queries, properties that are preserved etc. Bad Prolog books will not say much about this and typically end up using exactly those impure language elements that destroy the language's most valuable and beautiful properties.

一种名为Prolog的很棒的教学环境,它广泛利用这些属性来进行声明性调试,称为 GUPU ,我强烈建议您查看这些想法.乌尔里希·纽默克尔(Ulrich Neumerkel)慷慨地提出了一个核心思想,该思想在他的环境中得到了部分使用,它可以作为 library(diadem ).请参阅源文件,以获取有关如何以声明方式调试意外失败的目标的好示例:该库系统地构建 still 失败的查询的概括.当然,使用纯代码 可以完美地进行这种推理.

An awesome Prolog teaching environment that makes extensive use of these properties to implement declarative debugging is called GUPU, I highly recommend to check out these ideas. Ulrich Neumerkel has generously made one core idea that is used in his environment partly available as library(diadem). See the source file for a good example on how to declaratively debug a goal that fails unexpectedly: The library systematically builds generalizations of the query that still fail. This reasoning of course works perfectly with pure code.

这篇关于所谓的“逻辑纯度"是指.在Prolog中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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