OCaml 接口与签名?
我对 OCaml 中的接口与签名有点困惑。 据我所知,接口(.mli 文件)负责控制其他程序可以使用/调用哪些值。签名文件看起来完全相同,只是名称不同,以便您可以创建接口的不同实现。 例如,如果我想创建一个类似于 Java 中的集合的模块:
我会有这样的内容:
set.mli 文件:
type 'a set
val is_empty : 'a set -> bool
val ....
etc.
签名文件 (setType.ml)
module type Set = sig
type 'a set
val is_empty : 'a set -> bool
val ...
etc.
end
,然后实现将是另一个 .ml文件,例如 SpecialSet.ml,其中包含一个定义所有值及其用途的结构。
module SpecialSet : Set
struct
...
我对“签名”到底有什么作用以及它的用途有点困惑。它不就像一种界面吗?为什么同时需要 .mli 和 .ml?我看到的行中唯一的区别是它命名了模块。
我是否误解了这一点,或者这里还有其他事情发生?
I'm a bit confused about interfaces vs. signatures in OCaml.
From what I've read, interfaces (the .mli files) are what govern what values can be used/called by the other programs. Signature files look like they're exactly the same, except that they name it, so that you can create different implementations of the interface.
For example, if I want to create a module that is similar to a set in Java:
I'd have something like this:
the set.mli file:
type 'a set
val is_empty : 'a set -> bool
val ....
etc.
The signature file (setType.ml)
module type Set = sig
type 'a set
val is_empty : 'a set -> bool
val ...
etc.
end
and then an implementation would be another .ml file, such as SpecialSet.ml, which includes a struct that defines all the values and what they do.
module SpecialSet : Set
struct
...
I'm a bit confused as to what exactly the "signature" does, and what purpose it serves. Isn't it acting like a sort of interface? Why is both the .mli and .ml needed? The only difference in lines I see is that it names the module.
Am I misunderstanding this, or is there something else going on here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
OCaml 的模块系统绑定到单独的编译(
.ml
和.mli
文件对)。所以每个.ml
文件隐式定义了一个模块,每个.mli
文件定义了一个签名,如果有对应的.ml
文件,则该签名应用于该模块。在
.ml
或.mli
文件中使用显式语法来根据自己的喜好操作模块和接口非常有用。这允许签名约束,如S 中类型 t = Mt
所示。尤其重要的是,它提供了定义函子、由一个或多个模块参数化的模块的可能性:
module F (X : S) = struct ... end
。如果定义模块或签名的唯一方法是作为文件,那么所有这些都是不可能的。我不确定这如何回答你的问题,但我认为你的问题的答案可能是“是的,它就像你想象的那么简单,并且内部有
.mli
文件和显式签名的系统文件在您的示例中是多余的,除了这些简单的事情之外,在文件中操作模块和签名还可以实现更复杂的技巧”。OCaml's module system is tied into separate compilation (the pairs of
.ml
and.mli
files). So each.ml
file implicitly defines a module, each.mli
file defines a signature, and if there is a corresponding.ml
file that signature is applied to that module.It is useful to have an explicit syntax to manipulate modules and interfaces to one's liking inside a
.ml
or.mli
file. This allows signature constraints, as inS with type t = M.t
.Not least is the possibility it gives to define functors, modules parameterized by one or several modules:
module F (X : S) = struct ... end
. All these would be impossible if the only way to define a module or signature was as a file.I am not sure how that answers your question, but I think the answer to your question is probably "yes, it is as simple as you think, and the system of having
.mli
files and explicit signatures inside files is redundant on your example. Manipulating modules and signatures inside a file allows more complicated tricks in addition to these simple things".这个问题很旧,但也许这对某人有用:
幻灯片链接
这是来自 OCaml巴黎狄德罗大学的 MOOC。
This question is old but maybe this is useful to someone:
slide link
This is from the OCaml MOOC from Université Paris Diderot.