Define recursive signatures for modules

梦想与她 提交于 2019-12-07 01:32:57

问题


I know that it is possible to define recursive modules, does anyone know how to define recursive signatures? For instance, I would like to realize:

module type AAA = sig
  module Bbb : BBB
  type 'a t 
  val f : 'a Bbb.t -> 'a t
end

module type BBB = sig
  module Aaa : AAA
  type 'a t 
  val g : 'a Aaa.t -> 'a t
end

Could anyone help?


回答1:


You can't, as far as I can tell. The closest solution is to limit the "recursive" bits to what is actually needed to express each signature separately:

module type AA =
sig
  module B : sig type t end
  type t
  val f : unit -> B.t
end

module type BB =
sig
  module A : sig type t end
  type t
  val g : unit -> A.t
end

And then refine when you define the modules:

module rec A : AA with module B = B =
struct
  module B = B
  type t = int
  let f () = B.g ()
end
and B : BB with module A = A =
struct
  module A = A
  type t = int
  let g () = A.f ()
end

FWIW, one might think that it should be possible to express recursive signatures (with much repetition) by using recursive modules:

module rec AA :
sig
  module type T = sig module B : BB.T end
end =
struct
  module type T = sig module B : BB.T end
end
and BB :
sig
  module type T = sig module A : AA.T end
end =
struct
  module type T = sig module A : AA.T end
end

However, that does not work:

Error: Unbound module type BB.T



回答2:


You can write something like that :

module rec Aaa : sig
  type 'a t 
  val f : 'a Bbb.t -> 'a t
end = Aaa
and Bbb : sig
  type 'a t
  val g : 'a Aaa.t -> 'a t
end = Bbb


来源:https://stackoverflow.com/questions/9058598/define-recursive-signatures-for-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!