[Haskell-cafe] stack overflow pain

Tim Docker tim at dockerz.net
Thu Sep 22 01:00:37 CEST 2011


On 21/09/11 02:39, Heinrich Apfelmus wrote:
> Tim Docker wrote:
>> I'm getting a stack overflow exception in code like this:
>>
>>         -- applyAction :: A -> IO [B]
>>
>>         ....
>>         vs <- fmap concat $ mapM applyAction sas
>>         return vs
>>
>> I don't get it if I change the code to this:
>>
>>         -- applyAction :: A -> IO [B]
>>
>>         ....
>>         mapM_ applyAction sas
>>         return []
>>
>> But of course, I need the results from the actions. I know that
>> the returned list contains approximately 1 million items.
>>
>> Any suggestions on how I should rewrite the first code snippet
>> to not blow the stack?
>
> Of course, a list of 1 million items is going to take a lot of memory, 
> unless you generate it lazily. Unfortunately  mapM  cannot generate 
> its result lazily because it has to execute all IO actions before 
> returning the list of results.

I'm OK with it taking a lot of memory. I should have enough. It's the 
stack overflow exception I'm struggling with.

> I'm not entirely sure whether the stack overflow happens in this part 
> of your code, though. What happens if you change it to
>
>     map_ applyAction sas
>     return [1..1000000]
>
> ? If this still throws a stack overflow, then problem is in the part 
> of the code that consumes said list.

I believe the error is happening in the concat because there are 
subsequent IO actions that fail to execute. ie the code is equivalent to:

         vs <- fmap concat $ mapM applyAction sas
         someOtherAction
         consume vs

and someOtherAction seems not to be run. However, to be sure, I'll 
confirm with code akin to what you suggest above.

Tim




More information about the Haskell-Cafe mailing list