How to convert a Mathematica dump file to a definition list?

As stated in the documentation, " DumpSave writes the definitions in binary format, which is optimized for Mathematica input." Is there a way to convert the Mathematica binary dump file back to a list of definitions without evaluating them? Import["file.mx","HeldExpression"] does not work ...

+7
source share
2 answers

DumpSave stores the values ​​associated with the symbol, i.e. OwnValues , DownValues , UpValues , SubValues , NValues , FormatValues , FormatValues .

The entire evaluation was done in a Mathematica session, and then DumpSave saved the result.

These values ​​are stored in internal format. Reading MX files creates only characters and fills them with these values, reading this internal format back bypassing the evaluator.

Perhaps you could share a problem that prompted you to ask this question.


[EDIT] Clarification of the issue raised by Alexei. MX files retain the internal representation of character definitions. Mathematica seems to be internally tracking:
 f[x_Real] := x^2 + 1 DumpSave[FileNameJoin[{$HomeDirectory, "Desktop", "set_delayed.mx"}], f]; Remove[f] f[x_Real] = x^2 + 1; DumpSave[FileNameJoin[{$HomeDirectory, "Desktop", "set.mx"}], f]; setBytes = Import[FileNameJoin[{$HomeDirectory, "Desktop", "set.mx"}], "Byte"]; setDelayedBytes = Import[FileNameJoin[{$HomeDirectory, "Desktop", "set_delayed.mx"}], "Byte"]; 

You can use SequenceAlignment[setBytes, setDelayedBytes] to see the difference. I do not know why this is done, but my point is. All evaluation of values ​​constructed using Set was already done in a Mathematica session before they were saved using DumpSave . When an MX file is read, the internal view is read back into Mathematica sessions, and evaluation of the loaded definitions is not actually performed.

+6
source

You can assign Rule instead of RuleDelayed DownValues, which is equivalent to direct definitions. The right part of the task remains unappreciated and is literally copied, therefore, the command corresponding

 Clear[f]; f[x_Real] = x^2 + 1; DumpSave["f.mx", f]; Clear[f]; f = a; << f.mx; Definition[f] 

will be

 Clear[f]; f = a; DownValues[f] := {f[x_Real] -> x^2 + 1} Definition[f] 

f = a

f [x_Real] = x ^ 2 + 1

(see an example of your Clear[f]; f = a; f[x_Real] = x^2 + 1; Definition[f] , which does not work, instead assigning a rule to a[x_Real] ). This also applies to previous assignments x .


Change This is not reliable for side effects of the right side, as shown in the comments below. To assign a value to a downvalue, avoiding any evaluation, you can use the undocumented System`Private`ValueList , as shown below:

 Clear[f]; f := Print["f is evaluated!"]; DownValues[f] := System`Private`ValueList[f[x_Real] -> Print["definition is evaluated!"]]; 

(no conclusion)


Note that the assignment turned out to be seemingly converted to pending rules:

 DownValues[f] 

{HoldPattern [f [x_Real]]:> x ^ 2 + 1}

but Definition (and Save ) show that the difference from a := was internally saved. I do not know why DownValues do not display the truth.

To answer the original question, you would probably best import the dump file and export the corresponding characters with Save , and then, if you expect this to be loaded into the kernel, corrupted by the previous definitions, convert the assignments to destinations before DownValues . as described above. It might be easier to cover variables in a private context before exporting, although this is what system files do to prevent conflicts.

+1
source

All Articles