Prolog 关于未实例化变量的说法是什么?
What is Prolog saying about an uninstantiated variable?
假设我们要执行以下命令,SWI Prolog 响应:
?- write(X).
_13074
true.
什么是_13074
?这是地址吗?这是某种ID吗?我注意到我们每次都会得到不同的值。此外,为什么 Prolog 报告 true.
?这个序言是说 anything 可以与 X 统一吗?为什么这些按顺序出现?
如果我们将 X 统一为 1,那么结果就不同了。
?- X = 1, write(X).
1
X = 1.
What is _13074?
最简单的答案是它代表一个未实例化的变量。
更准确的说 Prolog Standard
anonymous variable: A variable (represented in a term or Prolog text by _) which differs from every other variable (and anonymous
variable) (see 6.1.2, 6.4.3)
instantiated: A variable is instantiated with respect to substitution if application of the substitution yields an atomic term or a compound term.
A term is instantiated if any of its variables are instantiated.
uninstantiated: A variable is uninstantiated when it is not instantiated.
variable: An object which may be instantiated to a term during execution.
named variable: A variable which is not an anonymous variable (see 6.1.2, 6.4.3)
所以显然所有这些都是自引用的,但简而言之,按照标准,有 匿名变量 _
和 命名变量 ,例如X
, Y
, Ls
.
请注意,标准并未说明 _
和后缀为数字的变量之间的区别,例如_13074
。这些是特定于实现的。
标准对写术语有注释,
7.10.5 Writing a term
When a term Term is output using write-term/3 (8.14.2) the action which is taken is defined by the rules below:
a) If Term is a variable, a character sequence representing that variable is output. The sequence begins with _ (underscore) and the remaining characters are implementation dependent. The same character sequence is used for each occurrence of a particular variable in Term. A different character sequence is used for each distinct variable in Term.
由于您特别提到了 SWI-Prolog,所以还有其他变量需要注意:
- named singleton variables AKA auxiliary variables
Named singletons start with a double underscore (__
) or a single underscore followed by an uppercase letter, E.g., __var
or _Var
.
Attribute variables - 提供一种通过挂钩属性变量的绑定来扩展 Prolog 统一算法 Holzbaur, 1992 的技术。 Prolog 社区没有就属性变量的确切定义和接口达成共识。 SWI-Prolog 接口与 Bart Demoen 在 2002 年为 hProlog Demoen 实现的接口相同。这个接口很简单,可以在所有可以 运行 Leuven CHR 系统的 Prolog 系统上使用(参见第 9 章和 Leuven CHR 页).
Global variables - 是名称(原子)和术语之间的关联。
我不打算深入研究变量,因为必须开始查看 SWI-Prolog C 级源代码才能真正获得更准确的理解,(ref)。我也不打算从标准中添加更多内容,因为最终必须在此处复制整个标准才能涵盖所有参考文献。
有关 Prolog 标准的更多定义,请参阅: 答案是一个社区维基,因此大多数用户都可以添加到其中,而 OP 不会获得积分,因此请随心所欲地投票。
Is this an address?
没有
有时你也会看到logic variable used but I don't plan to expand on that here, however for the record SWI-Prolog is NOT based on WAM it is based on A Portable Prolog Compiler。
见上7.10.5 写一个术语
Is this an ID of some sort?
我不会在关于 SWI-Prolog 的因果对话中反对这一点,但是这个简单的类比有足够的问题来分裂头发并开始 discussion/debate,例如blob be assigned to a variable? What is numbervars 可以吗?
见上7.10.5 写一个术语
I notice that we'll get a different value each time.
Prolog 标准使用单词 occurrence。
见上7.10.5 写一个术语
why does Prolog report true.?
Prolog 是一种逻辑语言,它 executes queries(目标)导致 true 或 false 或实例化值变量,但是可能会有副作用,例如写入文件、抛出异常等。
Prolog 标准规定
A.2.1.1 The General Resolution Algorithm
The general resolution of a goal G of a database P is defined by the following non-deterministic algorithm:
a) Start with the initial goal G which is an ordered conjunction of
predications.
b) If G is the singleton true then stop (success).
c) Choose a predication A in G (predication-choice)
d) If A is true, delete it, and proceed to step (b).
e) If no renamed clause in P has a head which unifies with A then stop (failure).
f) Choose a freshly renamed clause in P whose head H unifies with A (clause-choice) where σ = MGU(H, A) and B is the body of the clause,
g) Replace in G the predication A by the body B, flatten and apply the substitution σ.
h) Proceed to step (b).
另见:
Resolution
MGU
Is this Prolog saying that anything can be unified with X?
对于非常简单的 Prolog 实现 (ref) 那么这个问题就有意义了。在现实世界中,SWI-Prolog 更是如此,我不得不说并非在所有情况下都是如此。
对于大多数 Prolog 代码来说,语法统一是正在发生的事情的驱动力。请参阅:上面的 A.2.1.1 通用解析算法。但是,如果您开始考虑诸如 blob、属性、线程、异常等内容,那么您真的必须了解什么是变量,甚至是变量的种类以及该变量可以做什么,例如
?- X is true.
ERROR: Arithmetic: `true/0' is not a function
ERROR: In:
ERROR: [10] _4608 is true
ERROR: [9] toplevel_call(user:user: ...) at c:/program files/swipl/boot/toplevel.pl:1117
?- trie_new(A_trie).
A_trie = <trie>(0000000006E71DB0).
?- write(X).
_13074
true.
Why do these appear in the order they do?
write(X).
是用户输入的目标。
执行目标,在本例中具有写入当前输出流流的副作用,例如current_output/1,对于 SWI-Prolog 中出现的 X
,变量 X
未实例化并显示为 _13074
。
逻辑查询结束,逻辑查询结果非真即假。由于查询成功执行,result 为 true.
If we were to unify X with, say, 1, then the result is different.
?- X = 1, write(X).
1
X = 1.
我假设你问的是为什么最后没有 true。
带有 SWI-Prolog 的 IIRC,如果查询以一个变量开始,然后查询成功并且实例化的变量将被报告并且没有 true 或 [=127=然后会出现]false,例如
?- X = 1.
X = 1.
?- current_prolog_flag(double_quotes,V).
V = string.
?- X = 1, Y = 1, X = Y.
X = Y, Y = 1.
如果查询成功并且没有实例化变量,则查询将报告 true 例如
?- 1 = 1.
true.
?- current_prolog_flag(double_quotes,string).
true.
如果查询失败,查询将报告false 例如
?- X = 1, Y = 2, X = Y.
false.
?- current_prolog_flag(quotes,String).
false.
我怀疑这么多信息现在会让您询问更多细节,但我不会比这更深入,因为 SO 不是一个将讲座浓缩为答案的地方。我会尝试澄清所写的内容,但如果需要更多细节,希望 post 提出一个新的单独问题。
我知道此处提供的标准中的信息会留下很多失败的结果。如果您真的想要标准的详细信息,请像我们这些已经购买过的人一样购买标准。我知道它并不便宜,但对于像这样的问题,它是答案的来源。
假设我们要执行以下命令,SWI Prolog 响应:
?- write(X).
_13074
true.
什么是_13074
?这是地址吗?这是某种ID吗?我注意到我们每次都会得到不同的值。此外,为什么 Prolog 报告 true.
?这个序言是说 anything 可以与 X 统一吗?为什么这些按顺序出现?
如果我们将 X 统一为 1,那么结果就不同了。
?- X = 1, write(X).
1
X = 1.
What is _13074?
最简单的答案是它代表一个未实例化的变量。
更准确的说 Prolog Standard
anonymous variable: A variable (represented in a term or Prolog text by _) which differs from every other variable (and anonymous variable) (see 6.1.2, 6.4.3)
instantiated: A variable is instantiated with respect to substitution if application of the substitution yields an atomic term or a compound term.
A term is instantiated if any of its variables are instantiated.
uninstantiated: A variable is uninstantiated when it is not instantiated.
variable: An object which may be instantiated to a term during execution.
named variable: A variable which is not an anonymous variable (see 6.1.2, 6.4.3)
所以显然所有这些都是自引用的,但简而言之,按照标准,有 匿名变量 _
和 命名变量 ,例如X
, Y
, Ls
.
请注意,标准并未说明 _
和后缀为数字的变量之间的区别,例如_13074
。这些是特定于实现的。
标准对写术语有注释,
7.10.5 Writing a term
When a term Term is output using write-term/3 (8.14.2) the action which is taken is defined by the rules below:
a) If Term is a variable, a character sequence representing that variable is output. The sequence begins with _ (underscore) and the remaining characters are implementation dependent. The same character sequence is used for each occurrence of a particular variable in Term. A different character sequence is used for each distinct variable in Term.
由于您特别提到了 SWI-Prolog,所以还有其他变量需要注意:
- named singleton variables AKA auxiliary variables
Named singletons start with a double underscore (
__
) or a single underscore followed by an uppercase letter, E.g.,__var
or_Var
.
Attribute variables - 提供一种通过挂钩属性变量的绑定来扩展 Prolog 统一算法 Holzbaur, 1992 的技术。 Prolog 社区没有就属性变量的确切定义和接口达成共识。 SWI-Prolog 接口与 Bart Demoen 在 2002 年为 hProlog Demoen 实现的接口相同。这个接口很简单,可以在所有可以 运行 Leuven CHR 系统的 Prolog 系统上使用(参见第 9 章和 Leuven CHR 页).
Global variables - 是名称(原子)和术语之间的关联。
我不打算深入研究变量,因为必须开始查看 SWI-Prolog C 级源代码才能真正获得更准确的理解,(ref)。我也不打算从标准中添加更多内容,因为最终必须在此处复制整个标准才能涵盖所有参考文献。
有关 Prolog 标准的更多定义,请参阅:
Is this an address?
没有
有时你也会看到logic variable used but I don't plan to expand on that here, however for the record SWI-Prolog is NOT based on WAM it is based on A Portable Prolog Compiler。
见上7.10.5 写一个术语
Is this an ID of some sort?
我不会在关于 SWI-Prolog 的因果对话中反对这一点,但是这个简单的类比有足够的问题来分裂头发并开始 discussion/debate,例如blob be assigned to a variable? What is numbervars 可以吗?
见上7.10.5 写一个术语
I notice that we'll get a different value each time.
Prolog 标准使用单词 occurrence。
见上7.10.5 写一个术语
why does Prolog report true.?
Prolog 是一种逻辑语言,它 executes queries(目标)导致 true 或 false 或实例化值变量,但是可能会有副作用,例如写入文件、抛出异常等。
Prolog 标准规定
A.2.1.1 The General Resolution Algorithm
The general resolution of a goal G of a database P is defined by the following non-deterministic algorithm:
a) Start with the initial goal G which is an ordered conjunction of predications.
b) If G is the singleton true then stop (success).
c) Choose a predication A in G (predication-choice)
d) If A is true, delete it, and proceed to step (b).
e) If no renamed clause in P has a head which unifies with A then stop (failure).
f) Choose a freshly renamed clause in P whose head H unifies with A (clause-choice) where σ = MGU(H, A) and B is the body of the clause,
g) Replace in G the predication A by the body B, flatten and apply the substitution σ.
h) Proceed to step (b).
另见:
Resolution
MGU
Is this Prolog saying that anything can be unified with X?
对于非常简单的 Prolog 实现 (ref) 那么这个问题就有意义了。在现实世界中,SWI-Prolog 更是如此,我不得不说并非在所有情况下都是如此。
对于大多数 Prolog 代码来说,语法统一是正在发生的事情的驱动力。请参阅:上面的 A.2.1.1 通用解析算法。但是,如果您开始考虑诸如 blob、属性、线程、异常等内容,那么您真的必须了解什么是变量,甚至是变量的种类以及该变量可以做什么,例如
?- X is true.
ERROR: Arithmetic: `true/0' is not a function
ERROR: In:
ERROR: [10] _4608 is true
ERROR: [9] toplevel_call(user:user: ...) at c:/program files/swipl/boot/toplevel.pl:1117
?- trie_new(A_trie).
A_trie = <trie>(0000000006E71DB0).
?- write(X).
_13074
true.
Why do these appear in the order they do?
write(X).
是用户输入的目标。
执行目标,在本例中具有写入当前输出流流的副作用,例如current_output/1,对于 SWI-Prolog 中出现的 X
,变量 X
未实例化并显示为 _13074
。
逻辑查询结束,逻辑查询结果非真即假。由于查询成功执行,result 为 true.
If we were to unify X with, say, 1, then the result is different.
?- X = 1, write(X).
1
X = 1.
我假设你问的是为什么最后没有 true。
带有 SWI-Prolog 的 IIRC,如果查询以一个变量开始,然后查询成功并且实例化的变量将被报告并且没有 true 或 [=127=然后会出现]false,例如
?- X = 1.
X = 1.
?- current_prolog_flag(double_quotes,V).
V = string.
?- X = 1, Y = 1, X = Y.
X = Y, Y = 1.
如果查询成功并且没有实例化变量,则查询将报告 true 例如
?- 1 = 1.
true.
?- current_prolog_flag(double_quotes,string).
true.
如果查询失败,查询将报告false 例如
?- X = 1, Y = 2, X = Y.
false.
?- current_prolog_flag(quotes,String).
false.
我怀疑这么多信息现在会让您询问更多细节,但我不会比这更深入,因为 SO 不是一个将讲座浓缩为答案的地方。我会尝试澄清所写的内容,但如果需要更多细节,希望 post 提出一个新的单独问题。
我知道此处提供的标准中的信息会留下很多失败的结果。如果您真的想要标准的详细信息,请像我们这些已经购买过的人一样购买标准。我知道它并不便宜,但对于像这样的问题,它是答案的来源。