Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* One more question about the module system
@ 1996-03-25 17:22 pbrisset
  1996-03-28 14:16 ` Xavier Leroy
  0 siblings, 1 reply; 3+ messages in thread
From: pbrisset @ 1996-03-25 17:22 UTC (permalink / raw)
  To: caml-list


Here is my problem:

I have a program implemented in several files (say m.ml, m1.ml and m2.ml).
This program depends on a module L and the dependency is expressed
using the .mli/open mechanism. So I have:

	(* m.ml *)
	open L
	open M1
	...

	(* m1.ml *)
	open L
	open M2
	...

	(* m2.ml *)
	open L
	...

I want to transform this program in order to have a functor of argument L
(i.e. I want to use twice my program with two different module L's). So
I want to be able to write (in a main.ml):

	module ML1 = M(L1)
	module ML2 = M(L2)
	...

 where M is the functor.

I use the following idea: because my different (implicit) modules
depend on L, I have to translate them into (explicit) functors. Then, I
apply the functors for m1.ml and m2.ml in the main module defined in m.ml.
The m1.ml module can no longer include an "open M2" since M2 is a functor:
m1.ml have to speak about M2 through the instantiation of M2 done in M.
So I start to write:

	(* m.mli *)
	module type L = ...
	module M(X : L) :
	  sig
	    module M1 : sig ... X.t ... end
	    module M2 : sig ... X.t ... end
	    val main : ...
	  end

	(* m.ml *)
	module M(X : L) =
	  struct
	    module M1 = M1.Make(X)
	    module M2 = M2.Make(X)
	    (* previous m.ml *)
	    let main = ...
          end

	(* m1.mli *)
	module type L = ...
	module Make(X : L) :
	  sig
	    type t1 = ...
	    val v1 : M.M(X).M2.t2
	    ...
	  end

	(* m2.mli *)
	module type L = ...
	module Make(X : L) :
	  sig
	    type t2 = ...
	    val v2 : ...
          end

For these files, it is OK but I cannot write m1.ml :

	(* m1.ml *)
	module type L = ...
	module Make(X : L) =
	  struct
	    (* previous m1.ml *)
	    let v1 = ... M.M(X).M2.v2 ...
                         ^^^^^^^^^^^^
	  end

The underlined expression is not valid. So my idea does not work.

Any hint to "functorize" my package ?


--------------
Pascal Brisset
ENAC, 7 avenue Edouard Belin, BP 4005, F-31055 Toulouse Cedex, France
Tel: +33 62 17 40 53, E-mail: pbrisset@eis.enac.dgac.fr, Fax: +33 62 17 41 43





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

* Re: One more question about the module system
  1996-03-25 17:22 One more question about the module system pbrisset
@ 1996-03-28 14:16 ` Xavier Leroy
  0 siblings, 0 replies; 3+ messages in thread
From: Xavier Leroy @ 1996-03-28 14:16 UTC (permalink / raw)
  To: pbrisset; +Cc: caml-list


> I have a program implemented in several files (say m.ml, m1.ml and m2.ml).
> This program depends on a module L and the dependency is expressed
> using the .mli/open mechanism. So I have:
> 
> 	(* m.ml *)
> 	open L
> 	open M1
> 	...
> 
> 	(* m1.ml *)
> 	open L
> 	open M2
> 	...
> 
> 	(* m2.ml *)
> 	open L
> 	...
> 
> I want to transform this program in order to have a functor of argument L
> (i.e. I want to use twice my program with two different module L's). So
> I want to be able to write (in a main.ml):
> 
> 	module ML1 = M(L1)
> 	module ML2 = M(L2)
> 	...
> 
>  where M is the functor.
> 
> I use the following idea: because my different (implicit) modules
> depend on L, I have to translate them into (explicit) functors. Then, I
> apply the functors for m1.ml and m2.ml in the main module defined in m.ml.
> The m1.ml module can no longer include an "open M2" since M2 is a functor:
> m1.ml have to speak about M2 through the instantiation of M2 done in M.

No, but M1.Make can take L as a parameter, apply M2.Make to it, bind
the result with a "module" binding, and "open" the result. E.g.

(* m1.ml *)
        module Make(L: MTYPE_L) = struct
          module M2L = M2.Make(L)
          open L
          open M2L
          ...
        end
(* m.ml *)
        module Make(L: MTYPE_L) = struct
          module M1L = M1.Make(L)
          open L
          open M1L
          ...
        end

However, it's often more flexible to avoid applying functors inside
functors. Instead, just pass extra parameters to your functors:

(* m1.ml *)
        module Make(L: MTYPE_L)(Some_M2: MTYPE_M2) = struct
          open L
          open Some_M2
          ...
        end
(* m.ml *)
        module Make(L: MTYPE_L)(Some_M1: MTYPE_M1) = struct
          open L
          open Some_M1
          ...
        end

(You will probably need to express sharing constraints between type
components of the parameters. Express them with "with type" or
"with module", e.g.
        module Make(L: MTYPE_L)(Some_M2: MTYPE_M2 with type t = L.t) ...
)

Then do all the functor applications in a "main" file:

        module L = L1 (* or L2 *)
        module M2 = M2.Make(L)
        module M1 = M1.Make(L)(M2)
        module M = M.Make(L)(M1)

The advantage of this approach is that you can also provide
"hand-made" M1 or M2 modules (not produced by application of M1.Make
or M2.Make), so you get more flexibility. The disadvantage is
increased verbosity and number of declarations needed.

- Xavier Leroy





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

* Re: One more question about the module system
@ 1996-03-29 14:10 pbrisset
  0 siblings, 0 replies; 3+ messages in thread
From: pbrisset @ 1996-03-29 14:10 UTC (permalink / raw)
  To: caml-list


...
> (* m1.ml *)
>         module Make(L: MTYPE_L)(Some_M2: MTYPE_M2) = struct
>           open L
>           open Some_M2
>           ...
>         end
> (* m.ml *)
>         module Make(L: MTYPE_L)(Some_M1: MTYPE_M1) = struct
>           open L
>           open Some_M1
>           ...
>         end
> 
> (You will probably need to express sharing constraints between type
> components of the parameters. Express them with "with type" or
> "with module", e.g.
>         module Make(L: MTYPE_L)(Some_M2: MTYPE_M2 with type t = L.t) ...
> )
> 
> Then do all the functor applications in a "main" file:
> 
>         module L = L1 (* or L2 *)
>         module M2 = M2.Make(L)
>         module M1 = M1.Make(L)(M2)
>         module M = M.Make(L)(M1)
> 
> The advantage of this approach is that you can also provide
> "hand-made" M1 or M2 modules (not produced by application of M1.Make
> or M2.Make), so you get more flexibility. The disadvantage is
> increased verbosity and number of declarations needed.
> 
> - Xavier Leroy

 I eventually found the same solution. Because I wanted a single "big"
module, I wrote:

	(* m.ml *)
	module Make(L: SIG_L) = struct
	  module M2 = M2.Make(L)
	  open M2
	  module M1 = M1.Make(L, M2)
	  open M1
	  ...

 I also checked that runtime efficiency is not modified by all these
functorizations.

--Pascal





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

end of thread, other threads:[~1996-04-01 12:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-03-25 17:22 One more question about the module system pbrisset
1996-03-28 14:16 ` Xavier Leroy
1996-03-29 14:10 pbrisset

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