Steakfly
Steakfly

Reputation: 467

Mutually recursive module and functor in OCaml

I have defined an interface A to be used by several functors, and notably by MyFunctor :

module type A = sig
    val basic_func: ...
    val complex_func: ...
end

module MyFunctor :
    functor (SomeA : A) ->
struct
    ...
    let complex_impl params =
        ...
        (* Here I call 'basic_func' from SomeA *)
        SomeA.basic_func ...
        ...
end

Now I want to define a module B with implements the interface A. In particular, the implementation of complex_func should use basic_func through complex_impl in MyFunctor :

module B = struct
    let basic_func = ...

    let complex_func ... =
        let module Impl = MyFunctor(B) in
        Impl.complex_impl ...
end

However, this code doesn't compile as B is not fully declared in the context of MyFunctor(B). Obviously B depends on MyFunctor(B), which itself depends on B, so I tried to use the rec keyword on module B, but it didn't work out.

So, is it possible to do something like this ? It would be useful as I have several modules B_1, ..., B_n that use the same implementation of B_k.complex_func in terms of B_k.basic_func.

Or is there a better pattern for my problem ? I know that I can declare complex_impl as a regular function taking basic_func as a parameter, without using a functor at all :

let complex_impl basic_func params =
    ...
    basic_func ...
    ...

But in my case complex_impl uses many basic functions of A, and I think that the paradigm of functors is clearer and less error-prone.

Edit : I followed this answer, but in fact, A uses some type t that is specialized in B :

module type A = sig
    type t
    val basic_func: t -> unit
    val complex_func: t -> unit
end

module MyFunctor :
    functor (SomeA : A) ->
struct
    let complex_impl (x : SomeA.t) =
        SomeA.basic_func x
        ...
end

module rec B : A = struct
    type t = int
    val basic_func (x : t) = ...
    val complex_func (x : t) =
        let module Impl = MyFunctor(B) in
        Impl.complex_impl x
end

And now I get the error (for x at line Impl.complex_impl x) :

This expression has type t = int but an expression was expected of type B.t

Edit 2 : I solved this second problem with the following code :

module rec B :
    A with type t = int
= struct
    type t = int
    ...
end

Upvotes: 1

Views: 1084

Answers (1)

J. Abrahamson
J. Abrahamson

Reputation: 74354

You can use recursive modules just like you'd write recursive let bindings

module type A = sig
    val basic_func   : unit -> int
    val complex_func : unit -> int
end

module MyFunctor =
  functor (SomeA : A) ->
  struct
    let complex_impl = SomeA.basic_func
end

module rec B : A = struct
    let basic_func () = 0
    let complex_func () =
      let module Impl = MyFunctor(B) in
      Impl.complex_impl ()
end

Note (a) the module rec bit in the definition of B and (b) that I am required to provide a module signature for a recursive module definition.

# B.basic_func ();;
- : int = 0
# B.complex_func ();;
- : int = 0

There's a small caveat, however, in that this only works because the signature A has only values which are function types. It is thus known as a "safe module". If basic_func and complex_func were values instead of function types then it would fail upon compilation

Error: Cannot safely evaluate the definition
       of the recursively-defined module B

Upvotes: 5

Related Questions