为什么会这样:
module type ENTRY = sig type t end
module type LOG = functor (E : ENTRY) -> sig type t end
这是一个有效的 LOG 实现
module Log :LOG = functor (LogEntry : ENTRY) ->
struct type t = LogEntry.t list end
但这不是
module Log (LogEntry: ENTRY) :LOG = struct
type t = LogEntry.t list end
Error: Signature mismatch:
Modules do not match: sig type t = LogEntry.t list end is not included in LOG
如果我从 Log 的两个定义中删除 sig 标签 (:LOG),那么它们将返回相同的类型,因为它们只是语法糖[1]
[1] http://caml.inria.fr/pub/docs/oreilly-book/html/book-ora132.html
最佳答案
错误消息令人困惑,但第一个示例通过而第二个示例失败的原因实际上非常简单。比较:
type entry = int
type log = int -> string
let log : log = fun s -> string_of_int s
和
let log (s : entry) : log = string_of_int s
模块情况下的错误消息表明模块字段未包含在仿函数中,因为未应用的仿函数没有字段。
预计到达时间:从逻辑上讲,仿函数不能有字段:函数/仿函数与数据结构/模块相比是“另一种野兽”。 -- 这使错误消息变得困惑,听起来我们被要求引入一个字段,尽管它已经存在于仿函数的结果中。
关于OCaml 仿函数的语法糖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20337229/