Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: modules local to functions.
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Andrew Conway <arc@l...>
Subject: Re: modules local to functions.


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.