关于符号的定义和值的问题
Definition
“知道”符号值的定义方式:使用Set
或SetDelayed
。但如何呢?据我了解,在为符号分配值后,评估器的分配方式没有任何区别:使用 Set
或 SetDelayed
。它可以通过函数 OwnValues
来说明,该函数始终返回带有 Head
RuleDelayed
的定义。 Definiton
如何获取此信息?
In[1]:= a=5;b:=5;
Definition[a]
Definition[b]
OwnValues[a]
Out[2]= a=5
Out[3]= b:=5
Out[4]= {HoldPattern[a]:>5}
Definition
"knows" the way how a value for a symbol was defined: using Set
or SetDelayed
. But how? As I understand, after a value for a symbol was assigned there is no any difference for the evaluator how it was assigned: by using Set
or SetDelayed
. It can be illustrated by the function OwnValues
which always returns definitions with the Head
RuleDelayed
. How Definiton
obtains this information?
In[1]:= a=5;b:=5;
Definition[a]
Definition[b]
OwnValues[a]
Out[2]= a=5
Out[3]= b:=5
Out[4]= {HoldPattern[a]:>5}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
OwnValues[a] = {HoldPattern[a] ->; 3}; OwnValues[a]
给出{HoldPattern[a] :> 3}
而不是{HoldPattern[a] -> 3}
但Definition[a]
显示了人们可以期待的内容。该定义可能以Rule
的形式在内部存储,但通过OwnValues
转换为RuleDelayed
,以抑制对定义的 rhs 的评估。这个假设与我最初的理解相矛盾,即 Set 和 SetDelayed 分配的值之间没有区别。这些定义可能以不同的形式存储:相应的Rule
和RuleDelayed
,但从评估者的角度来看是等效的。有趣的是,
MemoryInUse[]
如何取决于定义的类型。在下面的实验中,我在没有前端的交互式会话中使用了 Mathematica 5.2 的内核。使用 Mathematica 6 和 7 的内核将会得到不同的结果。原因之一是在这些版本中
Set< /code> 默认情况下重载
。
首先,我评估
$HistoryLength=0;
是否有DownValues
的In
和Out
变量不影响我的结果。但似乎即使当$HistoryLength
设置为 0 时,当前 输入行的In[$Line]
值仍然会被存储和删除输入新的输入后。这可能就是为什么MemoryInUse[]
的第一次评估结果总是与第二次不同的原因。这是我得到的:
可以看到,定义
a=2;
将MemoryInUse[]
增加1986824-1986760=64 字节。将其替换为定义a:=2;
会使MemoryInUse[]
增加 1986952-1986824=128 字节。将后一个定义替换为前一个定义会将MemoryInUse[]
恢复为 1986824 字节。这意味着延迟定义比立即定义多需要 128 个字节。当然这个实验并不能证明我的假设。
OwnValues[a] = {HoldPattern[a] -> 3}; OwnValues[a]
gives{HoldPattern[a] :> 3}
instead of{HoldPattern[a] -> 3}
butDefinition[a]
shows what one can expect. Probably this definition is stored internally in the form ofRule
but is converted toRuleDelayed
byOwnValues
for suppressing of evaluation of the r.h.s of the definition. This hypothesis contradicts my original understanding that there are no difference between values assigned bySet
andSetDelayed
. Probably such definitions are stored in different forms:Rule
andRuleDelayed
correspondingly but are equivalent from the evaluator's point of view.It is interesting to see how
MemoryInUse[]
depends on the kind of definition.In the following experiment I used the kernel of Mathematica 5.2 in interactive session without the FrontEnd. With the kernels of Mathematica 6 and 7 one will get different results. One reason for this is that in these versions
Set
is overloaded by default.First of all I evaluate
$HistoryLength=0;
for havingDownValues
forIn
andOut
variables not affecting my results. But it seems that even when$HistoryLength
is set to 0 the value ofIn[$Line]
for current input line is still stored and removed after entering new input. This is likely the reason why result of the first evaluation ofMemoryInUse[]
always differs from the second.Here is what I have got:
One can see that defining
a=2;
increasesMemoryInUse[]
by 1986824-1986760=64 bytes. Replacing it with the definitiona:=2;
increasesMemoryInUse[]
by 1986952-1986824=128 bytes. And replacing the latter definition with the former revertsMemoryInUse[]
to 1986824 bytes. It means that delayed definitions require 128 bytes more than immediate definitions.Of course this experiment does not prove my hypothesis.
可以通过未记录的 new-in-8 符号
Language`ExtendedDefinition
和Language`ExtendedFullDefinition
访问符号的完整定义。引用Oleksandr Rasputinov:“如果有人很好奇,
Language`ExtendedDefinition
和Language`ExtendedFullDefinition
是与Definition
和FullDefinition
类似,但以可以在另一个内核中重现的方式捕获符号的定义,例如,defs = Language`ExtendedFullDefinition。 [sym]
返回一个Language`DefinitionList
对象 用于恢复定义的语法非常不规则:Language`ExtendedFullDefinition[] = defs
,其中。defs
是Language`DefinitionList
请注意,Language`ExtendedFullDefinition
采用ExcludedContexts
选项,而Language` ExtendedDefinition
没有。”Complete definition for a symbol can be accessed via undocumented new-in-8 symbols
Language`ExtendedDefinition
andLanguage`ExtendedFullDefinition
. Citing Oleksandr Rasputinov:"If anyone is curious,
Language`ExtendedDefinition
andLanguage`ExtendedFullDefinition
are analogous toDefinition
andFullDefinition
but capture the definition of a symbol in such a way as it can be reproduced in another kernel. For example,defs = Language`ExtendedFullDefinition[sym]
returns aLanguage`DefinitionList
object. The syntax used to restore the definition is highly irregular:Language`ExtendedFullDefinition[] = defs
, wheredefs
is aLanguage`DefinitionList
. Note thatLanguage`ExtendedFullDefinition
takes theExcludedContexts
option whereasLanguage`ExtendedDefinition
does not."Information
调用Definition
,并且Definition
(或FullDefinition
)上的跟踪未显示任何内容。我必须假设这是一个访问*Values
表之外的数据的低级函数。也许它保留了当时解析的原始定义表达式的副本。Information
callsDefinition
, and a Trace onDefinition
(orFullDefinition
) shows nothing. I must assume that this is a low level function that accesses data outside of the*Values
tables. Perhaps it keeps a copy of the original definition expressions as they were parsed at that time.