OCaml 仿函数和类型问题

OCaml functors and type issues

module type FOOable = sig
  type 'a t
  val foo : 'a -> 'a t
end

module type FOO_FUCNTOR =
  functor (Elt : FOOable) ->
    sig
      type 'a t
      val foo_alias : 'a -> 'a t
      (* ... *)
    end

我如何引用FOOable定义的类型'a t,因为它不能使用Elt.t

所以在这个例子中它会变成 type 'a t = 'a list

module MakeFoo : FOO_FUNCTOR =
  functor (Elt : FOOable) ->
  struct
    type 'a t = ??? (* I want the type 'a t belonging to Elt *)
    let foo_alias = Elt.foo
  end

module FooableList = struct 
  type = 'a list
  let foo x = [x]
end

module FooList = MakeFoo(FooableList)

let a = FooList.foo_alias 2

您只需输入 'a Elt.t,您一定会输入正确的类型。

module MakeFoo : FOO_FUNCTOR =
  functor (Elt : FOOable) ->
  struct
    type 'a t = 'a Elt.t
    let foo_alias = Elt.foo
  end

注意在FOO_FUNCTOR的定义中,类型相等是隐藏的,'a t'a Elt.t之间的link在[=之外是不可见的15=] 定义(但这可能就是你要找的)。