Question on Definition and values of symbols
Asked Answered
A

3

8

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}
Anguiano answered 7/5, 2011 at 5:17 Comment(2)
Another good question Alexey.Hipped
There is one very important use case where it makes a real difference what was used in assignment - Set or SetDelayed: the Part assignment. Confront tst = {1, 2, 3}; tst[[2]] = 5; tst (works all right) with tst1 := {1, 2, 3}; tst1[[2]] = 5 (gives assignment error, "no immediate value" for tst1). And of course, OwnValues for tst and tst1 look the same, just as you observed. Given your observation on memory consumption, I'd guess that delayed definitions may use some intermediate internal variables, while immediate ones point straight to the memory where the data is stored.Catiline
A
7

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.

Anguiano answered 7/5, 2011 at 11:16 Comment(1)
+1 for your continued probing of the inner workings of MathematicaHipped
A
4

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."

Anguiano answered 8/1, 2014 at 10:50 Comment(0)
H
3

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.

Hipped answered 7/5, 2011 at 6:14 Comment(8)
Not at the parsing time: a = 1 + 1; Definition[a] gives a=2. I suppose that in really OwnValues keep the information on the definitions but does not show it: if we evaluate OwnValues[a] = {HoldPattern[a] -> 3}; Definition[a] we get a = 3.Anguiano
@Alexey on the first point, I did not mean "parse" I meant "evaluation" (my mistake). On the second, good observation, and it contradicts what I supposed. Also try: OwnValues[a] = {HoldPattern[a] :> 3}; Definition[a] I need to think about this.Hipped
@Sjoerd @Mr. Did you see this one? math.stackexchange.com/questions/35591/…Electromotive
@belisarius Do you mean this specific question? I didn't even know that MO had a Mathematica tag.Spano
@Sjoerd My intention was to alert you both on that :)Electromotive
@belisarius I'll try to track that one as well then. Pity reputation starts at base zero again. No way to merge MO and SO rep?Spano
@Sjoerd MO is not the same as math.stackexchange. One intimidates me substantially more than the other. Just sayin' :)Adversity
@Sjoerd I've 143 there without being done almost nothing, so I guess my accounts are mergedElectromotive

© 2022 - 2024 — McMap. All rights reserved.