Prolog 匿名变量 [英] Prolog anonymous variable

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

问题描述

这是我对 Prolog 变量的理解.

Here is what I have understood about Prolog variables.

  1. 单下划线代表匿名变量,每次出现就像一个新变量.

  1. A single underscore stands for anonymous variable, which is like a new variable each time it occurs.

像_W这样的下划线开头的变量名不是匿名变量.或者,Prolog 内部生成的变量名,比如 _G189,不被认为是匿名的:

A variable name starting with underscore like _W is not an anonymous variable. Or, the variable names generated inside Prolog, like _G189, is not considered anonymous:

?- append([1,2],X,Y).
X = _G189
Y = [1, 2|_G189]

你能帮我理解一下吗?

顺便说一句,我从一些教程中得到了上面的例子,但是当我在 SWI-Prolog 版本 6 中运行它时,我得到了以下信息:

By the way, I got the above example from some tutorials, but when I run it in SWI-Prolog version 6, I get the following:

?- append([1,2],X,Y).
Y = [1, 2|X].

谢谢你.

推荐答案

变量

匿名变量 _ 是唯一的变量,其中不同的出现代表不同的变量.其他以 _ 开头的变量不是匿名的.不同的出现是指相同的变量(在相同的范围内).但是,如果一个不以下划线开头的变量只出现一次,像 SWI 这样的许多 Prologs 会警告你:

Variables

The anonymous variable _ is the only variable where different occurrences represent different variables. Other variables that start with _ are not anonymous. Different occurrences refer to the same variable (within the same scope). However, many Prologs like SWI will warn you should a variable not starting with an underscore occur only once:

?- [user].
a(V).
Warning: user://1:9:
        Singleton variables: [V]

您必须将该变量重命名为 _V 以避免该警告.这有助于程序员更好地发现变量名中的拼写错误.在许多系统中还有一些这样的限制.

You have to rename that variable to _V to avoid that warning. This is a help for programmers to better spot typos in variable names. There are some more such restrictions in many systems.

a(_V,_V).
Warning: user://1:12:
        Singleton-marked variables appearing more than once: [_V]

同样,这只是一个警告.如果您希望以 _ 开头的变量应该出现两次(没有警告),请改写 __.但最好坚持使用更有意义的名称,不要以 _ 开头.

Again, this is only a warning. If you want that a variable starting with _ should occur twice (without warning), write __ instead. But better stick to more meaningful names without a starting _.

你从 Prolog 的顶层循环中得到的是答案;尤其是答案替换.它们用于代表解决方案(这是我们真正感兴趣的).有几种方法可以表示答案替换.您正在使用的教程似乎是指一个非常旧的 SWI 版本.我会说这个版本可能有 15 到 20 年的历史.

What you get from Prolog's top level loop are answers ; and in particular answer substitutions. They serve to represent solutions (that's what we are really interested in). There are several ways how answer substitutions may be represented. The tutorial you are using seems to refer to a very old version of SWI. I would say that this version is maybe 15 to 20 years old.

?- append([1,2],X,Y).
X = _G189
Y = [1, 2|_G189]

不过,给出的答案并没有错:引入了一个新的辅助变量_G189.

However, the answer given is not incorrect: A new auxiliary variable _G189 is introduced.

较新版本的 SWI 和许多其他系统试图最小化输出,避免辅助变量.所以

Newer versions of SWI and many other systems try to minimize the output, avoiding auxiliary variables. So

?- append([1,2],X,Y).
Y = [1, 2|X].

一样好.这是较新"版本(也有 6 年历史)的答案.请注意,这个答案比第一个答案告诉您更多:它不仅更简洁地向您显示答案替换,而且还告诉您完全有这个答案(仅此而已).看到末尾的点 . 了吗?这意味着:这里没有更多的答案.否则会有一个 ; 用于下一个答案.

is just as fine. It is the answer of a "newer" version (also some 6 years old). Note that this answer tells you much more than the first one: Not only does it show you the answer substitution more compactly, but it also tells you that there is exactly this one answer (and no more). See the dot . at the end? This means: There is no more here to answer. Otherwise there would be a ; for the next answer.

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

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