Space usage
Lennart Augustsson
lennart at augustsson.net
Fri Aug 20 08:11:01 EDT 2004
Simon Peyton-Jones wrote:
> I had a look at this. It's an old chestnut: lazy pattern matching. You
> have
> let ((commands, s), x) = run (read iters) 5
> in do ...do something with commands...
> print x
>
> Trouble is, the 'x' hangs onto both components of the pair, even though
> it only needs one.
>
> It's not enough to force the pair earlier, which you probably tried,
> because the state monad you are using is itself lazy, and uses masses of
> lazy pattern matching.
>
> You could return the state paired with each item, rather than just the
> state at the end, perhaps, or use a stricter state monad.
>
> This lazy-pattern-matching leak is a well-known problem, to which I do
> not know a good solution. There was a paper from Chalmers about 8 years
> ago about building more cleverness into the compiler, but it amounted to
> extending Core with a lazy tuple binding. Fair enough, but quite a big
> addition to Core and one I've never done.
Simon, I suggest that you take another look at Jan Sparud's paper.
No extension of Core is needed to implement it. All that is needed
is a strange (impure) primitive function. To maintain type correctness
you also need a family of functions that are really the identity, but
with different types. Read the paper, I think you could add it to ghc
without too much trouble.
-- Lennart
More information about the Glasgow-haskell-users
mailing list