Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Parameterizing multiple modules using functors
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-08-21 (14:37)
From: Xavier Leroy <xavier.leroy@i...>
Subject: Re: [Caml-list] Parameterizing multiple modules using functors
> Here's the issue.  Consider a project consisting of multiple modules with
> lots of interdependencies.   Imagine then that I want to add some
> flexibility to the system by abstracting out some of the structure of the
> system using a functor.  Thus, I start out with modules A, B, C, and I
> want to parameterize all these modules, let's say by the same module X.  
> So what I want is a version of the program where I can use A.F(X) intead
> of A, B.F(X) instead of B, and so on.  The problem is that I want to allow
> references between these modules.  Thus, B.F(X) needs access to A.F(X),
> and C.F(X) needs access to both A.F(X) and B.F(X).  The question is, is
> there a reasonable way to achieve this?

There are two approaches, but I'm not sure you'll find them "reasonable".
One is as you described:

> I can think of an unreasonable way.  I can write the relevant functors so
> that they can be embedded in a unified module U as follows:
> module U =
> struct
>    module A = A.F(X)
>    module B = B.F(A,X)
>    module C = C.F(A,B,X)
> end
> In other words, B and C and require in their functors explicit arguments
> corresponding to the other modulse they need.  This works, but it's
> awfully ugly, not least because you have to write down some awful module
> signatures in B and C to make this work.

The other is to write:

module F (X: SIGX) =
    module A = ...
    module B = ...
    module C = ...

This way, B and C automatically reference A as instantiated for X.
You do lose separate compilation of A, B and C, though.  You can still
put A, B and C in separate files and generate the code above from the
files, perhaps with the use of a literate programming tool, but the
generated file will have to be recompiled entirely every time one of
the sub-modules change.

- Xavier Leroy

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: