Limiting resources on a per-function basis?

Glynn Clements glynn.clements at virgin.net
Fri Jan 2 15:42:26 EST 2004


Andreas Bauer wrote:

> > I would really like to be able to set resource limits on a per-function
> > basis.  I am envisioning something like:
> > 
> > limit :: l -> (a -> b) -> (a -> Maybe b)
> > limit lim fn = ...
> > 
> > which would convert a function so that it returns Nothing if a limit
> > (on stack, heap, time, etc.) is exceeded during evaluation of that
> > function.  Is there any hope of being able to do this within the
> > existing GHC libraries?
> 
> I am by no means a Haskell expert myself, but I did look into this
> issue for some time as well...
> 
> If this was possible (say, with respect to time), then the compiler
> would be able to determine whether your functions terminate, or not.
> This, however, is rarely ever possible.  Also, space consumption is
> hard to predict in face of general recursive structures, because you
> don't know how deep down the call sequence your function has to go.
> Sure, you could constrain your recursions, e.g. to tail recursion
> only, but for the general case, the answer for your question would be
> "no, it's not possible".

I don't think that he was interested in predicting in advance whether
the function could be computed within the limits, but simply in being
able to abort the evaluation at the point that a limit was reached,
analogous to the way that an OS implements resource limits for
processes.

Of course, the problem with this is lazy evaluation. In a strict
language, it would be relatively straightforward, assuming that you
can actually measure the resource consumption. In a lazy language, you
have to think about what "evaluation" really means.

Ultimately, I suspect that the inability to produce a sensible
definition would render any implementation issues moot.

-- 
Glynn Clements <glynn.clements at virgin.net>


More information about the Haskell-Cafe mailing list