Trouble Importing/Exporting in MX format

I want to save some definitions to an .mx file in order to be able to load them quickly. However, I run into a strange problem.

If I do,

Export["file1.txt", Definition[test1]]


I get a file containing the definitions I want (this is human readable, so I can check the file contains what I want) and I can then load it with Get["file1"] after which the definitions have been loaded.

However, when I do the same with the MX format:

Export["file2.mx", Definition[test1], "MX"]


and then Get["file2.mx"] it returns Null and does not load the definitions (of course here I cannot check whether the mx file really contains the correct definitions so I don't know whether the issue is in saving or loading the file).

What am I doing wrong?

• In your first example I get "Export::infer: Cannot infer format of file file1." are you sure you are not giving a file extension? – rhermans May 24 '18 at 16:30
• @rhermans, sorry I am. – Kvothe May 24 '18 at 16:32

The issue

From the documentation of Definition:

Definition[symbol] prints as all values and attributes defined for symbol.

(emphasis mine)

The key here is the word "prints": Definition[symbol] only looks like the definitions of symbol, it's not actually replaced by them:

f[x_]:=x^2
Definition@f
FullForm@Definition@f
(* f[x_]:=x^2 *)
(* Definition[f] *)


Exporting in a human readable format causes the StandardForm of the expression to be exported (please correct me if it's another form), which is why it works. Exporting to "MX" exports the FullForm, which does not do what you want:

file=FileNameJoin@{\$TemporaryDirectory,"test.mx"};
Export[file,Definition@f];
FullForm@Import[file]
(* Definition[f] *)


How to do what you want

Basically, see DumpSave.

• Thanks. I should have known. I have been burned by this before, but I forgot. – Kvothe May 24 '18 at 16:35