保存我正在运行的顶层供以后使用

发布于 2024-09-28 02:20:52 字数 197 浏览 14 评论 0原文

ocamlghci 顶层工作时,我经常会构建一个重要的“上下文”,以解决需要更好的单词、值绑定、函数、加载的模块等的问题。有没有办法保存所有这些并稍后重新加载,以便我可以从上次中断的地方继续?或者更好的是,将整个文件转储为一个文本文件,可以重新加载或简单地修改为我可以编译为可执行文件的代码(例如通过添加 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

拧巴小姐 2024-10-05 02:20:52

HOL light 的用户也有类似的需求,他们使用检查点程序来保存顶层的快照。请参阅 caml 邮件列表上的此消息,或此 HOL 教程的第 8 页。

一般来说,最好将定义保留为源代码,而不是二进制顶级快照。许多工具允许将 .ml 文件快速加载到顶层以方便实验(emacs 模式等)。请参阅 HOL 教程中的警告:

在 HOL 中开发大型证明时,您应该始终将证明脚本保留为
准备重新加载的 OCaml 文件,而不是依赖 ckpt。这将允许证明
以后可以修改,被其他人使用等。但是,制作起来可以非常方便
中间快照,因此您不必加载大文件即可进一步处理证明。
这类似于编程中的常见情况:你应该始终保持你的
完整的源代码,但不想每次使用时都重新编译所有源代码
代码。

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.

自由范儿 2024-10-05 02:20:52

至少在 OCaml 中没有对此的内置支持。解决方案是使用 rlwrap 或任何其他 readline 包装器将您的输入历史记录记录到一个文件。例如:

> rlwrap -H mysession.ml ocaml

缺点是这还会记录有语法错误的输入,因此您必须将其清除。请注意,默认情况下,如果您在不使用 -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 :

> 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.

热鲨 2024-10-05 02:20:52

在 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.

疧_╮線 2024-10-05 02:20:52

ghci 使用 haskeline 作为命令行输入历史记录,因此您可以向上滚动以重复/编辑输入。您的输入历史记录通常记录在一个文件中,您可以在给定的目录中找到该文件,该文件为 ghci_history

System.Directory.getAppUserDataDirectory "ghc"

有多种命令可以探索“上下文”(: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 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.

南汐寒笙箫 2024-10-05 02:20:52

在ocaml中,您可以构建自己的顶层。它至少解决了加载模块的问题。

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

ocamlmktop 命令构建包含用户代码的 OCaml 顶层
在启动时预加载。

ocamlmktop 命令采用一组 .cmo 和 .cma 文件作为参数,
并将它们与实现 OCaml 的目标文件链接
顶层。典型用途是:

<预><代码> ocamlmktop -o mytoplevel foo.cmo bar.cmo gee.cmo

这将创建字节码文件 mytoplevel,其中包含 OCaml
顶层系统,加上三个 .cmo 文件中的代码。这
toplevel 是直接可执行的,并通过以下方式启动:

<前><代码> ./mytoplevel

这会进入一个常规的顶级循环,除了来自的代码
foo.cmo、bar.cmo 和 gee.cmo 已经加载到内存中,就像
您输入了:

 #load "foo.cmo";;
    #加载“bar.cmo”;;
    #加载“gee.cmo”;;

在顶层入口处。 Foo、Bar 和 Gee 模块不是
不过,打开了;你还需要做

 打开 Foo;;

你自己,如果这是你想要的。

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.

放肆 2024-10-05 02:20:52

这也一直困扰着我,所以我写了一个快速的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文