7

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?

2 Answers 2

6

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
  • Thanks for your answer... the closest solution is to limit the "recursive" bits ==> Could you please elaborate more about the limits of your solution?
    – SoftTimur
    Jan 31, 2012 at 13:42
  • 1
    Well, this doesn't allow you to express arbitrary recursion between signatures, because you need to be able to isolate a self-contained subset of each signature as a kind of forward declaration. Also, you are repeating each of those subsets in two places -- but naming and include-ing them can help there. In my reply I didn't bother to do that, because the relevant subsets (type t) are small enough. Jan 31, 2012 at 15:25
4

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
1
  • Thanks for your comment, but I really want to give a name to the signature, for instance, AAA aor BBB... you did not mention that in your answer...
    – SoftTimur
    Jan 30, 2012 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.