Prolog 中的嵌套谓词 [英] Nested Predicates In Prolog

查看:66
本文介绍了Prolog 中的嵌套谓词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个在另一个谓词的范围"内存在"的谓词.我需要这个的原因是因为两个谓词都使用相同的非常大的参数/数组,并且我想要嵌套的谓词多次进行自递归,所以我想避免复制相同的参数.那么,有什么办法可以在 Swi-Prolg 中做到这一点吗?

I am trying to write a predicate that ‘exists’ inside the ‘scope’ of another predicate . The reason I need this is because both predicates make use of the same very large parameters/arrays and the predicate I want to nest is doing self recurssion many times , so I want to avoid copying the same parameters . So , is there any way i can do this in Swi-Prolg ?

提前致谢.

推荐答案

您不需要.您必须意识到所有由 Prolog 变量名称命名"的术语已经是全局的,尽管当子句没有引用它们的名称时无法访问(并且名称是总是局部于子句).那个非常大的数组"在堆上.只需将名称传递给任何其他谓词,成本约为 0.

You don't need to. You have to realize that all the terms "named" by Prolog variable names are already global, although inaccessible when the clause doesn't have a name referencing them (and names are always local to a clause). That "very large array" is on the heap. Just pass the name to it to any other predicate at ~0 cost.

正如保罗·莫拉所说.

假设你有:

foo(BigArray) :- do_things(BigArray),do_more_things(BigArray).

假设 do_things/1 要么只是在位置 0 打印元素,如果它是一个实例化术语,或者将它设置为 bar 如果它是是一个新鲜术语:

Suppose do_things/1 either just prints the element at position 0 if it is an instantiated term, or sets it to bar if its is a fresh term:

do_things(BigArray) :- nth0(0,BigArray,Elem),nonvar(Elem),!,write(Elem).
do_things(BigArray) :- nth0(0,BigArray,Elem),var(Elem),!,Elem=bar.

如果在位置 0 上有一个新词,则返回到 foo/1 时,位置 0 上的原子 bar 是对调用者和 do_more_things/1 可见,因为 BigArray 指定的列表是全局术语".

If there was a fresh term on position 0, the, on return to foo/1, the atom bar on position 0 is visible to the caller and to do_more_things/1 because that list designated by BigArray is a "global term".

关于是否使用全局变量"的另一个问题的一些精确性:

SWI-Prolog 也有全局变量",它显然类似于 GNU Prolog 的全局变量":

SWI-Prolog also has "Global Variables", which are apparently similar to the GNU Prolog "Global Variables":

全局变量

我们读到:

全局变量是名称(原子)和术语之间的关联.它们与使用 assert/1<存储信息的方式不同/code>recorda/3.

Global variables are associations between names (atoms) and terms. They differ in various ways from storing information using assert/1 or recorda/3.

...这意味着它们的目的类似于 assert/1recorda/3 的目的:在 Prolog 顶层存储在查询终止后幸存的状态 -类似于程序的程序子句的存储方式.

...which means that their purpose is similar to the purpose of assert/1 and recorda/3: Storing state that survives query termination at the Prolog toplevel - similar to how program clauses of a program are stored.

我会说,只有在绝对需要时才使用它们.

I would say, use those only if absolutely needed.

另请阅读介绍:数据库,我们在其中找到:

Also read the intro: Database, where we find:

记录的数据库不是 ISO 标准的一部分,而是公平的得到广泛支持,特别是在基于爱丁堡"的实施中传统'.在 SWI-Prolog 中使用这个数据库的原因很少由于动态谓词的良好性能.

The recorded database is not part of the ISO standard but fairly widely supported, notably in implementations building on the‘Edinburgh tradition'. There are few reasons to use this database in SWI-Prolog due to the good performance of dynamic predicates.

这篇关于Prolog 中的嵌套谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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