Saving my running toplevel for later

↘锁芯ラ 提交于 2019-12-03 14:37:31

问题


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)?


回答1:


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.




回答2:


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.




回答3:


In Haskell, just use :e file. This opens the standard editor and lets you edit some file. Afterwards, use :r to reload it. It will be automatically recompiled.

Please notice, that all your "ad-hoc" defined functions will be lost after this. Refer to the doc for more information.




回答4:


ghci uses haskeline for commandline input history, so you can scroll up to repeat/edit inputs. Your input history is usually recorded in a file, which you can find as ghci_history in the directory given by

System.Directory.getAppUserDataDirectory "ghc"

There are various commands to explore the 'context' (:show bindings, :show modules, :def, ..) but their output won't suffice to reproduce your session (though it is worth knowing about them anyway).

In general, the advice to combine your ghci session with an open editor window is sound: if it is more than a throwaway definition, even if just for debugging purposes, better include it in a module to be loaded into ghci, so that you can reuse it.

Oh, and if by 'context', you mean some default settings or modules you want loaded, on a per-project basis, there is also ghci's configuration file. Also handy for defining your own ghci commands.




回答5:


In ocaml, you can build your own top-level. It solves problem with loaded modules at least.

http://caml.inria.fr/pub/docs/manual-ocaml/toplevel.html#sec278

The ocamlmktop command builds OCaml toplevels that contain user code preloaded at start-up.

The ocamlmktop command takes as argument a set of .cmo and .cma files, and links them with the object files that implement the OCaml toplevel. The typical use is:

    ocamlmktop -o mytoplevel foo.cmo bar.cmo gee.cmo

This creates the bytecode file mytoplevel, containing the OCaml toplevel system, plus the code from the three .cmo files. This toplevel is directly executable and is started by:

    ./mytoplevel

This enters a regular toplevel loop, except that the code from foo.cmo, bar.cmo and gee.cmo is already loaded in memory, just as if you had typed:

    #load "foo.cmo";;
    #load "bar.cmo";;
    #load "gee.cmo";;

on entrance to the toplevel. The modules Foo, Bar and Gee are not opened, though; you still have to do

    open Foo;;

yourself, if this is what you wish.




回答6:


This has always bothered me too, so I wrote a quick python/expect script to replay ghci_history at the beginning of each ghci session.

It's not very polished. For example it always replays the whole history and that could be slow.



来源:https://stackoverflow.com/questions/3966925/saving-my-running-toplevel-for-later

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!