保存我正在运行的顶层供以后使用
在 ocaml
或 ghci
顶层工作时,我经常会构建一个重要的“上下文”,以解决需要更好的单词、值绑定、函数、加载的模块等的问题。有没有办法保存所有这些并稍后重新加载,以便我可以从上次中断的地方继续?或者更好的是,将整个文件转储为一个文本文件,可以重新加载或简单地修改为我可以编译为可执行文件的代码(例如通过添加 Main)?
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
HOL light 的用户也有类似的需求,他们使用检查点程序来保存顶层的快照。请参阅 caml 邮件列表上的此消息,或此 HOL 教程的第 8 页。
一般来说,最好将定义保留为源代码,而不是二进制顶级快照。许多工具允许将 .ml 文件快速加载到顶层以方便实验(emacs 模式等)。请参阅 HOL 教程中的警告:
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:
至少在 OCaml 中没有对此的内置支持。解决方案是使用 rlwrap 或任何其他 readline 包装器将您的输入历史记录记录到一个文件。例如:
缺点是这还会记录有语法错误的输入,因此您必须将其清除。请注意,默认情况下,如果您在不使用 -H 选项的情况下调用 rlwrap,它会自动将您的输入保存在 ~/.ocaml_history 中。
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 :
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.
在 Haskell 中,只需使用
:e file
即可。这将打开标准编辑器并允许您编辑一些文件。然后,使用:r
重新加载它。它将自动重新编译。请注意,此后所有“临时”定义的函数都将丢失。请参阅文档以获取更多信息。
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.
ghci
使用 haskeline 作为命令行输入历史记录,因此您可以向上滚动以重复/编辑输入。您的输入历史记录通常记录在一个文件中,您可以在给定的目录中找到该文件,该文件为 ghci_history有多种命令可以探索“上下文”(:show绑定、:show模块、:def、 ..)但它们的输出不足以重现您的会话(尽管无论如何都值得了解它们)。
一般来说,将 ghci 会话与打开的编辑器窗口结合起来的建议是合理的:如果它不仅仅是一次性定义,即使只是为了调试目的,最好将其包含在要加载的模块中到 ghci 中,以便您可以重用它。
哦,如果“上下文”是指您想要加载的一些默认设置或模块,那么在每个项目的基础上,还有
ghci
的 配置文件。也可以方便地定义您自己的 ghci 命令。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 asghci_history
in the directory given byThere 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 intoghci
, 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 ownghci
commands.在ocaml中,您可以构建自己的顶层。它至少解决了加载模块的问题。
http://caml.inria.fr/pub/docs/手册-ocaml/toplevel.html#sec278
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
这也一直困扰着我,所以我写了一个快速的 python/expect 脚本 来在每个 ghci 会话开始时重播 ghci_history。
它不是很精致。例如,它总是重播整个历史记录,这可能会很慢。
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.