Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* [Caml-list] Separate compilation
@ 2025-04-05  4:26 Oleg
  2025-04-07 21:55 ` Edwin Török
  0 siblings, 1 reply; 10+ messages in thread
From: Oleg @ 2025-04-05  4:26 UTC (permalink / raw)
  To: caml-list


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.


^ permalink raw reply	[flat|nested] 10+ messages in thread
* [Caml-list] separate compilation
@ 2011-09-08 14:26 Walter Cazzola
  2011-09-08 14:33 ` Philippe Wang
  2011-09-08 14:33 ` Esther Baruk
  0 siblings, 2 replies; 10+ messages in thread
From: Walter Cazzola @ 2011-09-08 14:26 UTC (permalink / raw)
  To: OCaML Mailing List

[-- Attachment #1: Type: TEXT/PLAIN, Size: 1142 bytes --]

Dear Experts,
I'm a sort of newbe so be patient with if I ask something too silly
(I've googled around but I didn't find anything really usefull).

At the moment I'm investigating on how the OCaML module system and the
separate compilation works.

Nothing original I took the priority queue example from the user manual
and split it into three files (attached): one with the structure (A.ml)
one with the signature (A.mli) and one with a silly main (main.ml).

Following the instructions in the manual I have tried to compile each
file without linking but the only that compiles with success is the file
with the signature since it doesn't refer to any other file.

When I compile the structure or the main I get this error:
   >ocamlc -c A.ml
   File "A.ml", line 17, characters 39-52:
   Error: Unbound module type CharPQueueAbs

I have tried to put #use or #load in the A.ml and main.ml file but the
only result is to get a syntax error (e.g., with #use "A.mli" at the
beggining of A.ml)

   >ocamlc -c A.ml
   File "A.ml", line 1, characters 0-1:
   Error: Syntax error

What am I doing wrong? Any help is appreciate, thanks.

Walter

-- 

[-- Attachment #2: Type: TEXT/PLAIN, Size: 271 bytes --]

module type CharPQueueAbs =
   sig
     type priority = int         (* still concrete *)
     type char_queue            (* now abstract *)
     val empty : char_queue
     val insert : char_queue -> int -> char -> char_queue
     exception QueueIsEmpty
   end;;

[-- Attachment #3: Type: TEXT/PLAIN, Size: 572 bytes --]

#use "A.mli"

module PrioQueue =
   struct
     type priority = int
     type char_queue = Empty | Node of priority * char * char_queue * char_queue
     exception QueueIsEmpty
     
     let empty = Empty
     let rec insert queue prio elt =
       match queue with
         Empty -> Node(prio, elt, Empty, Empty)
       | Node(p, e, left, right) ->
           if prio <= p
           then Node(prio, elt, insert right p e, left)
           else Node(p, e, insert right prio elt, left)
   end;;

module AbstractPrioQueue = (PrioQueue: CharPQueueAbs);;

[-- Attachment #4: Type: TEXT/PLAIN, Size: 59 bytes --]

open AbstractPrioQueue;;

let x = insert empty 1 'a' ;;

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2025-04-07 21:55 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-04-05  4:26 [Caml-list] Separate compilation Oleg
2025-04-07 21:55 ` Edwin Török
  -- strict thread matches above, loose matches on Subject: below --
2011-09-08 14:26 [Caml-list] separate compilation Walter Cazzola
2011-09-08 14:33 ` Philippe Wang
2011-09-08 14:40   ` Walter Cazzola
2011-09-08 14:33 ` Esther Baruk
2011-09-08 14:42   ` Walter Cazzola
2011-09-08 15:55     ` AUGER Cedric
2011-09-09  6:50       ` Walter Cazzola
2011-09-09  7:06         ` David Allsopp

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox