关于符号的定义和值的问题

发布于 2024-11-06 02:50:44 字数 432 浏览 1 评论 0原文

Definition“知道”符号值的定义方式:使用SetSetDelayed。但如何呢?据我了解,在为符号分配值后,评估器的分配方式没有任何区别:使用 SetSetDelayed。它可以通过函数 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

舞袖。长 2024-11-13 02:50:44

OwnValues[a] = {HoldPattern[a] ->; 3}; OwnValues[a] 给出 {HoldPattern[a] :> 3} 而不是 {HoldPattern[a] -> 3}Definition[a] 显示了人们可以期待的内容。该定义可能以 Rule 的形式在内部存储,但通过 OwnValues 转换为 RuleDelayed,以抑制对定义的 rhs 的评估。这个假设与我最初的理解相矛盾,即 Set 和 SetDelayed 分配的值之间没有区别。这些定义可能以不同的形式存储:相应的 RuleRuleDelayed,但从评估者的角度来看是等效的。

有趣的是,MemoryInUse[] 如何取决于定义的类型。

在下面的实验中,我在没有前端的交互式会话中使用了 Mathematica 5.2 的内核。使用 Mathematica 6 和 7 的内核将会得到不同的结果。原因之一是在这些版本中Set< /code> 默认情况下重载

首先,我评估 $HistoryLength=0; 是否有 DownValuesInOut 变量不影响我的结果。但似乎即使当 $HistoryLength 设置为 0 时,当前 输入行的 In[$Line] 值仍然会被存储和删除输入新的输入后。这可能就是为什么 MemoryInUse[] 的第一次评估结果总是与第二次不同的原因。

这是我得到的:

面向学生的 Mathematica 5.2:Microsoft Windows 版本

版权所有 1988-2005 Wolfram Research, Inc.

-- 终端图形已初始化--

在[1]中:= $HistoryLength=0;

In[2]:= MemoryInUse[]

输出[2]= 1986704

In[3]:= MemoryInUse[]

输出[3]= 1986760

In[4]:= MemoryInUse[]

输出[4]= 1986760

在[5]中:= a=2;

In[6]:= MemoryInUse[]

输出[6]= 1986848

In[7]:= MemoryInUse[]

输出[7]= 1986824

In[8]:= MemoryInUse[]

输出[8]= 1986824

在[9]:= a:=2;

In[10]:= MemoryInUse[]

输出[10]= 1986976

In[11]:= MemoryInUse[]

输出[11]= 1986952

In[12]:= MemoryInUse[]

输出[12]= 1986952

在[13]中:= a=2;

In[14]:= MemoryInUse[]

输出[14]= 1986848

In[15]:= MemoryInUse[]

输出[15]= 1986824

In[16]:= MemoryInUse[]

输出[16]= 1986824

可以看到,定义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} but Definition[a] shows what one can expect. Probably this definition is stored internally in the form of Rule but is converted to RuleDelayed by OwnValues 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 by Set and SetDelayed. Probably such definitions are stored in different forms: Rule and RuleDelayed 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 having DownValues for In and Out variables not affecting my results. But it seems that even when $HistoryLength is set to 0 the value of In[$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 of MemoryInUse[] always differs from the second.

Here is what I have got:

Mathematica 5.2 for Students: Microsoft Windows Version

Copyright 1988-2005 Wolfram Research, Inc.

-- Terminal graphics initialized --

In[1]:= $HistoryLength=0;

In[2]:= MemoryInUse[]

Out[2]= 1986704

In[3]:= MemoryInUse[]

Out[3]= 1986760

In[4]:= MemoryInUse[]

Out[4]= 1986760

In[5]:= a=2;

In[6]:= MemoryInUse[]

Out[6]= 1986848

In[7]:= MemoryInUse[]

Out[7]= 1986824

In[8]:= MemoryInUse[]

Out[8]= 1986824

In[9]:= a:=2;

In[10]:= MemoryInUse[]

Out[10]= 1986976

In[11]:= MemoryInUse[]

Out[11]= 1986952

In[12]:= MemoryInUse[]

Out[12]= 1986952

In[13]:= a=2;

In[14]:= MemoryInUse[]

Out[14]= 1986848

In[15]:= MemoryInUse[]

Out[15]= 1986824

In[16]:= MemoryInUse[]

Out[16]= 1986824

One can see that defining a=2; increases MemoryInUse[] by 1986824-1986760=64 bytes. Replacing it with the definition a:=2; increases MemoryInUse[] by 1986952-1986824=128 bytes. And replacing the latter definition with the former reverts MemoryInUse[] to 1986824 bytes. It means that delayed definitions require 128 bytes more than immediate definitions.

Of course this experiment does not prove my hypothesis.

听风吹 2024-11-13 02:50:44

可以通过未记录的 new-in-8 符号 Language`ExtendedDefinitionLanguage`ExtendedFullDefinition 访问符号的完整定义。引用Oleksandr Rasputinov

“如果有人很好奇, Language`ExtendedDefinitionLanguage`ExtendedFullDefinition 是与 DefinitionFullDefinition 类似,但以可以在另一个内核中重现的方式捕获符号的定义,例如,defs = Language`ExtendedFullDefinition。 [sym] 返回一个 Language`DefinitionList 对象 用于恢复定义的语法非常不规则:Language`ExtendedFullDefinition[] = defs,其中。 defsLanguage`DefinitionList 请注意,Language`ExtendedFullDefinition 采用 ExcludedContexts 选项,而 Language` ExtendedDefinition 没有。”

Complete definition for a symbol can be accessed via undocumented new-in-8 symbols Language`ExtendedDefinition and Language`ExtendedFullDefinition. Citing Oleksandr Rasputinov:

"If anyone is curious, Language`ExtendedDefinition and Language`ExtendedFullDefinition are analogous to Definition and FullDefinition 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 a Language`DefinitionList object. The syntax used to restore the definition is highly irregular: Language`ExtendedFullDefinition[] = defs, where defs is a Language`DefinitionList. Note that Language`ExtendedFullDefinition takes the ExcludedContexts option whereas Language`ExtendedDefinition does not."

帅气称霸 2024-11-13 02:50:44

Information 调用 Definition,并且 Definition(或 FullDefinition)上的跟踪未显示任何内容。我必须假设这是一个访问 *Values 表之外的数据的低级函数。也许它保留了当时解析的原始定义表达式的副本。

Information calls Definition, and a Trace on Definition (or FullDefinition) 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文