Recursion OCaml:具有一级模块和存在类型的递归
我试图创建一个函数,在深入递归的同时交替使用两个模块(相同类型)。我将模块作为参数传递,当我向模块中添加存在类型时,一切都出错了。让我有点吃惊的是,如果我使函数非递归(就像我发现的所有远程类似的例子一样),它就可以工作 以下是我认为最简单的示例(仅传递了一个模块):Recursion OCaml:具有一级模块和存在类型的递归,recursion,module,ocaml,existential-type,first-class,Recursion,Module,Ocaml,Existential Type,First Class,我试图创建一个函数,在深入递归的同时交替使用两个模块(相同类型)。我将模块作为参数传递,当我向模块中添加存在类型时,一切都出错了。让我有点吃惊的是,如果我使函数非递归(就像我发现的所有远程类似的例子一样),它就可以工作 以下是我认为最简单的示例(仅传递了一个模块): 为什么它不工作?我能做些什么使它工作?不确定是否完全理解您的错误,但在执行递归时,最好将类型注释置于最高级别。 以下是一个有效的版本: module type TEST = sig type t val foo : t -&
为什么它不工作?我能做些什么使它工作?不确定是否完全理解您的错误,但在执行递归时,最好将类型注释置于最高级别。 以下是一个有效的版本:
module type TEST =
sig
type t
val foo : t -> unit
end
let rec foo : type a. (module TEST with type t = a) -> a -> unit
= fun (module Test) arg ->
if true
then foo (module Test) arg
else Test.foo arg
Error: This expression has type
(module TEST with type t = a) -> a -> 'b
but an expression was expected of type
(module TEST with type t = a) -> a -> 'b
The type constructor a would escape its scope
module type TEST =
sig
type t
val foo : t -> unit
end
let rec foo : type a. (module TEST with type t = a) -> a -> unit
= fun (module Test) arg ->
if true
then foo (module Test) arg
else Test.foo arg