From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.6.10/8.6.6) id SAA26628 for caml-redistribution; Wed, 10 Jan 1996 18:43:19 +0100 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.6.10/8.6.6) with ESMTP id RAA24865 for ; Wed, 10 Jan 1996 17:23:54 +0100 Received: from victor.labri.u-bordeaux.fr (root@victor.labri.u-bordeaux.fr [147.210.8.168]) by concorde.inria.fr (8.7.1/8.7.1) with SMTP id RAA16811 for ; Wed, 10 Jan 1996 17:23:53 +0100 (MET) Received: from waves.labri.u-bordeaux.fr by victor.labri.u-bordeaux.fr (4.1/SM-mailhost-BORDEAUX-1.0) id AA08582; Wed, 10 Jan 96 17:24:38 +0100 Received: from [127.0.0.1] by waves.labri.u-bordeaux.fr (4.1/SM-BORDEAUX0.1) id AA09726; Wed, 10 Jan 96 17:22:33 +0100 Message-Id: <9601101622.AA09726@waves.labri.u-bordeaux.fr> From: Andrew Conway Reply-To: arc@labri.u-bordeaux.fr To: Xavier Leroy Cc: caml-list@pauillac.inria.fr Subject: Re: modules local to functions. In-Reply-To: Your message of "Wed, 10 Jan 1996 16:27:22 +0100." <199601101527.QAA22663@pauillac.inria.fr> Date: Wed, 10 Jan 1996 17:22:32 +0100 Sender: weis I wrote a while ago: > However, there are cases when I want to define a module in the middle > of a function [...] That is, I would like to be abe to do: Xavier Leroy replied: >I still need to be convinced of the usefulness of local functor >applications (Andrew Conway's example was too sketchy to illuminate me), >but it can definitely be done. OK, here comes my attempt. I want to write a function that uses a large number of arrays as a essential part of its process. These arrays contain small positive values, in a range specified in the function (eg 0 to 2). As these arrays are copied around very frequently, and memory consumption is significant, it is worth while having them as a packed array. So, I implemented a packed array functor that takes as arguement the number of bits needed, and returns a module implementing that packed array. I would like the code to be as memory efficient as reasonably possible, and so, for instance, when the range is 0 to 1, the packed array should be of just one bit per location. [ I am using it in a dynamic programming problem. It already wants to use more than the available memory, so I have to sacrifice time for memory already. Packing from 2 bits to 1 bits roughly divides the execution time by a factor of 4. And no, I can't see a significantly better algorithm unfortunately, and it is not from a lack of looking. ] There are other parameters to the function, and I would like to make a table of the value of the function for all parameters. At the moment what I do is declare (at file scope) a module PA with a fixed number of bits. I then generate that section of the table that can be done efficiently with that sized packed array. I then edit the source, recompile with a different sized packed array, and print out another section of the table, etc. This multiple-recompilation is an annoyance. In this particular case it is not a major problem as I only want to do it a few times (and a few times for debugging, and a few times when I change the function slightly). However, it is a real example of when declaring a module inside a function would significantly increase the straightforward expressability of the language. [ Note: I am aware that there are lots of work arounds, and in particular that one could regard functors as syntactic sugar, but they are very nice syntactic sugar for a really ugly alternative. ] Andrew.