Module OCaml在实现中重复整个签名
假设我有一个模块Module OCaml在实现中重复整个签名,module,ocaml,sml,Module,Ocaml,Sml,假设我有一个模块a,具有以下接口和实现文件: (* a.mli *) module type A_SIG = sig val f: int -> int ... end (* a.ml *) module A : A_SIG = struct let f x = x + 1 ... end 编译a.mli后接a.ml失败,错误为未绑定模块类型a\u SIG。复制实现文件中的整个签名可以修复它 为什么会这样?在SML中似乎有以下工作: (* a.sig *
a
,具有以下接口和实现文件:
(* a.mli *)
module type A_SIG =
sig
val f: int -> int
...
end
(* a.ml *)
module A : A_SIG =
struct
let f x = x + 1
...
end
编译a.mli
后接a.ml
失败,错误为未绑定模块类型a\u SIG
。复制实现文件中的整个签名可以修复它
为什么会这样?在SML中似乎有以下工作:
(* a.sig *)
signature A_SIG =
sig
val f: int -> int
...
end
(* a.sml *)
structure A : A_SIG =
struct
fun f x = x+1
...
end
我看到了,但我不清楚它的答案。文件
a.ml
隐式包装为模块a
,文件a.mli
隐式包装为模块类型a
所以下面的工作
(* A.mli *)
val f : int -> int
(* A.ml *)
let f x = x + 1
您可以从另一个模块以A.f
的形式访问f
或者如果你真的想要子模块,你可以写
(* a.mli *)
module type A_SIG =
sig
val f: int -> int
end
module A : A_SIG
(* a.ml *)
module type A_SIG =
sig
val f: int -> int
end
module A : A_SIG =
struct
let f x = x + 1
end
您可以从另一个模块作为A.A.f
访问f
,模块A
除了(子)模块A
之外,还将包含签名A\u SIG
标准ML的实现通常不会隐式地将文件内容包装为模块
为完整起见,请注意OCaml具有从模块“生成”模块类型的功能:
(* a.mli *)
module type A_SIG =
sig
val f: int -> int
end
module A : A_SIG
(* a.ml *)
module A =
struct
let f x = x + 1
end
module type A_SIG = module type of A