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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

(这个问题与 CHR 无关,也不是 SWI 特有的)。
编写 Prolog 程序时使用的变量名称将被 Prolog 系统完全丢弃。原因是该信息无法用于准确打印变量。该变量可能有多个独立实例。因此,需要在变量名称中添加一些唯一标识符。此外,在运行时维护该信息会产生大量开销。
要了解这一点,请考虑谓词
mylist/1
。在这里,我们为列表的每个元素使用了变量
_E
。顶层现在使用唯一标识符打印所有这些元素:第二个答案可能会打印为
Fs = [_E]
。但第三个呢?它不能打印为Fs = [_E,_E]
因为元素是不同的变量。所以像Fs = [_E_295,_E_298]
这样的东西是我们能得到的最好的。然而,这将意味着大量额外的簿记工作。但还有另一个原因,为什么将源代码变量名称与运行时变量关联起来会导致极度复杂:在不同的地方,该变量可能有不同的名称。下面是一个人工示例来说明这一点:
查询:
这两个元素应该有什么名称?第一个元素的名称可能是
_A
或_B
,或者甚至更好:_A_or_B
。或者,甚至_Ap1_and_Bp2
。这对谁来说有好处?请注意,顶层查询中提到的变量名称将被保留:
因此有一种方法可以获取该信息。关于在阅读术语时如何获取SWI和YAP中的变量名称,请参考这个问题。
(This question has nothing to do with CHR nor is it specific to SWI).
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.
To see this, consider a predicate
mylist/1
.Here, we have used the variable
_E
for each element of the list. The toplevel now prints all those elements with a unique identifier:The second answer might be printed as
Fs = [_E]
instead. But what about the third? It cannot be printed asFs = [_E,_E]
since the elements are different variables. So something likeFs = [_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:
And the query:
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:
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.