Separate compilation

Oleg <[email protected]> Sat, 5 Apr 2025 13:26:53 +0900
Newsgroups gmane.comp.lang.caml.inria
Message-ID <Z/[email protected]>
In designing separate compilation, OCaml has decided to restrict the
correspondence between separately compiled modules and their
interfaces to one-to-one. The restriction is indeed limiting: making
the linking with alternative/improved implementations, and the
implementation extension/evolution ungainly (in fact, seemingly
impossible).

To be concrete: suppose we have an interface A.mli and two implementations 
of it: A1.ml and A2.ml. We also have the user code B.ml:
        open A
        ... using the operations of A
We want to compile B.ml and link that B.cmo with either
implementation. As posed, the problem seem unsolvable: we need either
modify the source B.ml to refer to a particular implementation, or turn
B.ml into a functor (which could be inconvenient, and also suboptimal:
calls to A operations become indirect). 

For the next problem, assume the interface U.mli and its
implementation U.ml. We want to extend them -- say, add a new
operation to the interface and the implementation -- without modifying
U.mli/U.ml and without cut-and-paste. That is, we want merely `include'
U and add the new operation -- making a `diff' so to speak. If U.mli
abstracts away the implementation details, and if they are needed to
implement the new operation, the problem seem unsolvable.

It turns out there are work-arounds for both problems, which solve
them, albeit not elegantly. The full explanation is a bit too long for
this message; please see
        https://okmij.org/ftp/ML/module-extensibility.html

The work-arounds are simple. Should OCaml developers be so inclined, they
may be incorporated in OCaml.