[Haskell-cafe] Haskell maximum stack depth

Adrian Hey ahey at iee.org
Tue Feb 5 13:50:44 EST 2008

Luke Palmer wrote:
> On Feb 5, 2008 2:50 AM, Adrian Hey <ahey at iee.org> wrote:
>> I think it bites a lot less often than it otherwise would because most
>> people will deliberately chose to use heap in preference to stack (at
>> least when writing "eager" code) just to avoid the problem. But it still
>> bites pretty often anyway with "lazy" code for unexpected reasons.
>> Arguably such situations are indeed a "bug" more often than not, but
>> I still think terminating the program unnecessarily (at 8M stack) is
>> bad default policy.
> Please, after all this, do give an example.

If you mean an example of coding style and choice of stack vs. heap,
I already have..


If you mean an example of it biting with lazy code, this is discussed
so often you'd be spoiled for choice if you search the mailing list
archives. Here's a good one..


This example actually shows the problem twice. In one case it's solvable
by using foldl' instead of foldl. In the other case there's nothing
that can reasonably be done by the user short of editing the source
code of Data.Map to provide a stricter insertWith function (which
has now been added IIRC).

The problem is repeated applying of a strict function lazily. When the
result is eventually demanded the stack "blows up". In the mean time
you sometimes have an unnecessary space (heap) leak too (with counters
for example).

Adrian Hey

More information about the Haskell-Cafe mailing list