Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving my running toplevel for later

When working in the ocaml or ghci toplevels I often build up a significant "context" for want of a better word, values bound, functions, modules loaded, and so on. Is there a way to save all of that and reload it later so I can continue exactly where I left off? Or better yet, dump out the entire lot as a text file that could be reloaded or be trivially modified into code that I could compile into an executable (e.g. by adding a Main)?

like image 376
Gaius Avatar asked Oct 19 '10 09:10

Gaius


2 Answers

Users of HOL light have had similar needs, and they use a checkpointing program to save a snapshot of the toplevel. See this message on the caml mailing-list, or page 8 of this HOL tutorial.

In general it is better to keep the definitions as source code, rather than a binary toplevel snapshot. Numerous tools allow to quickly load a .ml file into the toplevel for easy experimentation (emacs modes, etc.). See the warning in the HOL tutorial:

When developing large proofs in HOL, you should always keep the proof script as an OCaml file ready to reload, rather than relying on ckpt. This will allow the proofs to be later modified, used by others etc. However, it can be very convenient to make intermediate snapshots so you do not have to load large files to work further on a proof. This is analogous to the usual situation in programming: you should always keep your complete source code, but don’t want to recompile all the sources each time you use the code.

like image 147
gasche Avatar answered Sep 17 '22 14:09

gasche


At least in OCaml there's no built-in support for that. On solution is to use rlwrap or any other readline wrapper to record your input's history to a file. For example :

> rlwrap -H mysession.ml ocaml 

The drawback is that this will also record the input that had syntax errors so you'll have to clean that out. Note that by default rlwrap will automatically save your input in ~/.ocaml_history if you invoke it without the -H option.

like image 20
Daniel Bünzli Avatar answered Sep 18 '22 14:09

Daniel Bünzli