Re: modules local to functions.

Christian Boos (boos@gr6.u-strasbg.fr)
Tue, 9 Jan 96 09:04:10 +0100

Date: Tue, 9 Jan 96 09:04:10 +0100
From: boos@gr6.u-strasbg.fr (Christian Boos)
Message-Id: <9601090804.AA08645@gr6.u-strasbg.fr>
To: caml-list@pauillac.inria.fr
Subject: Re: modules local to functions.
In-Reply-To: <9601081207.AA14189@waves.labri.u-bordeaux.fr>

> I have a functor such as follows:
>
> module type Specifyn = sig val n : int end;;
>
> module Makespecific(N:Specifyn) = struct
> type mytype = int
> let mirror () : mytype = N.n
> end;;
>
> I can apply it at the main level like:
>
> module Fred = Makespecific(struct let n = 7 end);;
>
> However, there are cases when I want to define a module in the middle
> of a function (eg a functor of packed binary arrays where I want to (...)

Hello,

I have encountered a similar problem yesterday!
My understanding is that YOU CAN'T do that in CSL at all: functor
application means code generation, and there's no runtime code
generation!

One way to circumvent this problem is to change slightly the semantic
of your argument module, for example:

module type Specifyn = sig val n : unit -> int end
^^^^^^^

This should be not too inconvenient, and almost as simple to use as
yours:

module Makespecific(N:Specifyn) = struct
type mytype = int
let mirror () : mytype = N.n ()
^^
end

let toto_fred_n = ref 10

module Fred = Makespecific(struct let n () = !toto_fred_n end)

let toto digits =
toto_fred_n := digits;
Fred.mirror ()

(This particular solution may even be expressed simpler:

module type Specifyn = sig val n : int ref end
^^^

module Makespecific(N:Specifyn) = struct
type mytype = int
let mirror () : mytype = !N.n
^
end

module Fred_N = struct let n = ref 10 end

module Fred = Makespecific(Fred_N)

let toto digits =
Fred_N.n := digits;
Fred.mirror ()

)

Hope it helps!

Christian