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?
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With