SWI Prolog 中的变量名 [英] Variable Names in SWI Prolog

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

问题描述

我一直在使用 chr 库和 jpl 接口.不过我有一个一般性的询问.我将约束从 SWI Prolog 发送到我的 CHR 程序中的 java 类的实例.例如,如果输入约束是 leq(A,B),则变量的名称不见了,出现的变量名称以 _G 开头.即使我尝试在不使用界面的情况下打印 leq(A,B) 也会发生这种情况.似乎每当处理变量时,名称都会替换为新的名称.我的问题是是否有办法做回映射.比如有没有办法知道_G123对应的是A等等.非常感谢.

I have been using the chr library along with the jpl interface. I have a general inquiry though. I send the constraints from SWI Prolog to an instance of a java class from within my CHR program. The thing is if the input constraint is leq(A,B) for example, the names of the variables are gone, and the variable names that appear start with _G. This happens even if I try to print leq(A,B) without using the interface at all. It appears that whenever the variable is processed the name is replaced with a fresh one. My question is whether there is a way to do the mapping back. For example whether there is a way to know that _G123 corresponds to A and so on. Thank you very much.

推荐答案

(此问题与 CHR 无关,也不特定于 SWI).

(This question has nothing to do with CHR nor is it specific to SWI).

您在编写 Prolog 程序时使用的变量名称已被 Prolog 系统完全丢弃.原因是此信息不能用于准确打印变量.该变量可能有几个独立的实例.因此,需要为变量名称添加一些唯一标识符.此外,在运行时维护该信息会产生大量开销.

The variable names you use when writing a Prolog program are discarded completely by the Prolog system. The reason is that this information cannot be used to print variables accurately. There might be several independent instances of that variable. So one would need to add some unique identifier to the variable name. Also, maintaining that information at runtime would incur significant overheads.

要看到这一点,请考虑谓词 mylist/1.

To see this, consider a predicate mylist/1.

?- [user].
|: mylist([]).
|: mylist([_E|Es]) :- mylist(Es).
|: % user://2 compiled 0.00 sec, 4 clauses
true.

在这里,我们为列表的每个元素使用了变量 _E.顶层现在打印所有具有唯一标识符的元素:

Here, we have used the variable _E for each element of the list. The toplevel now prints all those elements with a unique identifier:

?- mylist(Fs).
Fs = [] ;
Fs = [_G295] ;
Fs = [_G295, _G298] .
Fs = [_G295, _G298, _G301] .

第二个答案可能会打印为 Fs = [_E] 代替.但是第三个呢?它不能打印为 Fs = [_E,_E] 因为元素是不同的变量.所以像 Fs = [_E_295,_E_298] 这样的东西是我们能得到的最好的结果.但是,这意味着需要进行大量额外的簿记工作.

The second answer might be printed as Fs = [_E] instead. But what about the third? It cannot be printed as Fs = [_E,_E] since the elements are different variables. So something like Fs = [_E_295,_E_298] is the best we could get. However, this would imply a lot of extra book keeping.

但还有另一个原因,为什么将源代码变量名称与运行时变量相关联会导致极端复杂:在不同的地方,该变量可能具有不同的名称.下面是一个人为的例子来说明这一点:

But there is also another reason, why associating source code variable names with runtime variables would lead to extreme complexities: In different places, that variable might have a different name. Here is an artificial example to illustrate this:

p1([_A,_B]).

p2([_B,_A]).

和查询:

?- p1(L), p2(L).
L = [_G337, _G340].

你想要什么名字,这两个元素应该有什么?第一个元素的名称可能是 _A_B,或者更好:_A_or_B.或者,甚至 _Ap1_and_Bp2.这对谁有好处?

What names, would you like, these two elements should have? The first element might have the name _A or _B or maybe even better: _A_or_B. Or, even _Ap1_and_Bp2. For whom will this be a benefit?

注意,在顶层的查询中提到的变量名称被保留:

Note that the variable names mentioned in the query at the toplevel are retained:

?- Fs = [_,F|_], mylist(Fs).
Fs = [_G231, F] ;
Fs = [_G231, F, _G375] ;
Fs = [_G231, F, _G375, _G378] 

所以有一种方法可以获取该信息.关于如何在阅读term时获取SWI和YAP中的变量名称,请参考这个问题.

So there is a way to get that information. On how to obtain the names of variables in SWI and YAP while reading a term, please refer to this question.

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

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