Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define recursive signatures for modules

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?

like image 262
SoftTimur Avatar asked Jan 30 '12 02:01

SoftTimur


2 Answers

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
like image 173
Andreas Rossberg Avatar answered Sep 21 '22 23:09

Andreas Rossberg


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
like image 26
Çağdaş Bozman Avatar answered Sep 21 '22 23:09

Çağdaş Bozman