定义模块的递归签名

Sof*_*mur 7 ocaml types module signature

我知道可以定义递归模块,有人知道如何定义递归签名吗?例如,我想知道:

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
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?

And*_*erg 6

据我所知,你不能.最接近的解决方案是将"递归"位限制为分别表达每个签名实际需要的位:

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
Run Code Online (Sandbox Code Playgroud)

然后在定义模块时进行优化:

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
Run Code Online (Sandbox Code Playgroud)

FWIW,人们可能认为应该可以通过使用递归模块来表达递归签名(重复次数很多):

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
Run Code Online (Sandbox Code Playgroud)

但是,这不起作用:

Error: Unbound module type BB.T
Run Code Online (Sandbox Code Playgroud)