From: James Stein on
This looks like a bug to me, but I know appearances can deceive!
Cell 1 below defines two symbols: foo and list.
After evaluation of Cell1, I thought
Cell 2 would dump and restore symbol 'foo' but it does not.
Now here is puzzlement: If you change DumpSave's second argument
from 'list' to '{foo}', the code works !-- even though these are
"identical" expressions: {foo} === list evaluates to True.
Here is a clue to what might be the problem: Despite the fact that
foo === list [ [ 1 ] ], they have different DownValues.
Can two symbols with different DownValues really be identical?
(Well, to quibble, they are not both symbols... )

(* Define a list of Symbols used in compound Heads: *)
ClearAll [ foo, list ];
foo[ 1 ] = { 1, 2, 3 };
list = { foo };

(* Cell 2 fails *)
DumpSave [ "foo.mx", list ];
ClearAll [ foo ];
Get [ "foo.mx" ]; (* Get what was dumped *)

Definition[foo] (* Alas-- we get'Null' for foo! *)


This is a simplified example, of course. I have hundreds of symbols,

not just 'foo', and each is used as a compound head in several different
ways.

I was hoping to dump all these definitions to a single file.

I hope I'm missing something that will make me feel dumb...


From: Leonid Shifrin on
Hi James,

Without knowing anything about the inner workings of DumpSave -
the expressions
list and {foo} are not identical for DumpSave since it has the HoldRest
attribute.
This means that DumpSave does not evaluate any but the first of its
arguments,
and then at the pattern-matching stage, it can not know that <list> is a
List,
while it does know this for {foo} without evaluating it. I discussed this
issue
in my book:

http://www.mathprogramming-intro.org/book/node408.html

if you use DumpSave [ "foo.mx", Evaluate[list] ], you should get the same
result as
with DumpSave [ "foo.mx", {foo} ], as long as foo has OwnValues or UpValues
or
SubValues but not OwnValues. The latter case is a bit harder, since by
calling
Evaluate you will attempt to dump not <foo> but its actual value. But is
such case,
you may face this problem even earlier when putting you variables into a
list <list>.
I'd suggest a more universal way - to wrap your symbols in Hold.

In[1]:=
Clear[myDumpSave];
myDumpSave[fname_String, Hold[vars__Symbol]] :=
DumpSave[fname, {vars}];

In[2]:=
ClearAll[f, g, h];
f = 1;
g[x_] := x^2;
h[x_][y_] := x + y;

Here is how you could use it:

In[6]:=
myHeldSymbols = Hold[f, g, h];
myDumpSave["foo.mx", myHeldSymbols];
ClearAll[f, g, h];
Get["foo.mx"];
List @@ Map[Definition, Hold[f, g, h]] // InputForm

Out[10]//InputForm=
{f = 1, g[x_] := x^2, h[x_][y_] := x + y}


Hope this helps.


Regards,
Leonid







On Fri, Jun 4, 2010 at 4:01 PM, James Stein <mathgroup(a)stein.org> wrote:

> This looks like a bug to me, but I know appearances can deceive!
> Cell 1 below defines two symbols: foo and list.
> After evaluation of Cell1, I thought
> Cell 2 would dump and restore symbol 'foo' but it does not.
> Now here is puzzlement: If you change DumpSave's second argument
> from 'list' to '{foo}', the code works !-- even though these are
> "identical" expressions: {foo} === list evaluates to True.
> Here is a clue to what might be the problem: Despite the fact that
> foo === list [ [ 1 ] ], they have different DownValues.
> Can two symbols with different DownValues really be identical?
> (Well, to quibble, they are not both symbols... )
>
> (* Define a list of Symbols used in compound Heads: *)
> ClearAll [ foo, list ];
> foo[ 1 ] = { 1, 2, 3 };
> list = { foo };
>
> (* Cell 2 fails *)
> DumpSave [ "foo.mx", list ];
> ClearAll [ foo ];
> Get [ "foo.mx" ]; (* Get what was dumped *)
>
> Definition[foo] (* Alas-- we get'Null' for foo! *)
>
>
> This is a simplified example, of course. I have hundreds of symbols,
>
> not just 'foo', and each is used as a compound head in several different
> ways.
>
> I was hoping to dump all these definitions to a single file.
>
> I hope I'm missing something that will make me feel dumb...
>
>
>