OCaml仿函数和类型问题

时间:2016-08-03 12:07:37

标签: types ocaml functor

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

如何引用'a t定义的FOOable类型,因为无法使用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

1 个答案:

答案 0 :(得分:4)

您只需输入'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之间的链接在MakeFOO定义之外不可见(但是这可能就是你要找的东西。)