I have a little problem with typing of OCaml modules, and I fear that the answer is
“can’t get there from here”. But I’m not sure, so I figured I’d ask and see if
there’s something I’m missing.
In the following, each of the modules L, PA1, PA2 have many types and
values/entrypoints. Many, many. So maintaining the module-types of
these modules would be … tiresome. The reason I want to convert
these modules into functors, is that their implementation is partially
imperative, with references among the values.
The problem: Suppose I have three modules, L, PA1, and PA2. They’re written
as plain old files, and have various types and values. Each depends on the
previous ones in the sequence. I want to convert these into functors, so
that I can have multiple instances of each. So I’d have a functor PA1
that took an instantiated module L as argument, and then a functor PA2 that
took instantiated L and PA1 as arguments. Something like the following.
In the case where these weren’t functors, I didn’t need to write out MLI files.
But now that I have functors, I need to write out module-types for L(), PA1(),
PA2(). I’ve done so below, but it seems to me that I ought to be able to
infer those types (PA1TY, PA2TY)
It feels like there ought to be some way to use “module type of” and
destructive module substitution to do the trick, but it isn’t obvious to me.
Thank you for any advice.
module type LTY = sig
type t
val f : t -> t
end
module L() : LTY = struct
type t = A | B
let f x = x
end
module PA (L : LTY) = struct
module L = L
let f = L.f
end
module type PATY = sig
module L : LTY
val f : L.t -> L.t
end
module type PBTY = sig
module L: LTY
module PA : PATY with module L = L
val f : L.t -> L.t
end
module PB(L : LTY)(PA : PATY with module L = L) : (PBTY with module L = L and module PA = PA) = struct
module L = L
module PA = PA
let f = PA.f
end
(* using these functors *)
module L1 = L()
module L2 = L()
module PA1 = PA(L1)
module PA2 = PA(L2)
module PB1 = PB(L1)(PA1)
module PB2 = PB(L2)(PA2)