如何显示ocaml程序的每个声明的类型?
我喜欢在终端中使用 ocaml
来获取每个 ocaml 命令的交互式结果或类型,例如:
Objective Caml version 3.11.2
# let a = 5;;
val a : int = 5
但是当有很多命令时,我们将所有命令都放在像 这样的文件中是正常的test.ml
,然后编译它。目前,我使用ocamlc -o test test.ml
。但是当我在终端中进行 test
时,我看不到每个声明的类型,这很遗憾。
有谁知道如何证明这一点?非常感谢。
I like to use ocaml
in a terminal to get interactive result or type from each ocaml command, for instance:
Objective Caml version 3.11.2
# let a = 5;;
val a : int = 5
But when there are many commands, it is normal that we put all in a file like test.ml
, then compile it. At the moment, I use ocamlc -o test test.ml
. But when I do test
in a terminal, I could not see the types of each declaration, which is a pity.
Does anyone know how to show that? Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在顶层,您可以通过
#use "filename"
加载整个文件,这类似于直接在顶层键入。对于编译,您可以通过向编译器添加
-i
选项来生成类型信息。这会将定义的名称打印到标准输出,该名称可以通过管道传输到文件,以快速而肮脏的方式生成 mli 文件。有关更详细的类型信息,您可以添加
-annot
选项,它将打印文件中每个标签的类型、范围和尾部调用信息。必须对其进行解析,因为从文件中无法直接看出正在发生的情况。有 emacs 和 vim 的插件,可能还有 eclipse,可以为你做到这一点。另一个线程中讨论了有助于 ocaml 类型注释的工具。
In the toplevel you can load the entire file by,
#use "filename"
, and this is similar to typing directly into the top-level.For compiling, you can generate the the type information by adding the
-i
option to the compiler. This will print the defined names to standard out, which can be piped to a file for a quick and dirty way to generate an mli file.For more detailed type information you can add the
-annot
option, which will print the type, scope, and tail-call information for every label in the file. It would have to be parsed since it isn't directly obvious from the file what is happening. There are plugins for emacs and vim, and probably eclipse, that can do this for you.Tools to help in ocaml type annotations were discussed in another thread.