[Haskell-cafe] stack overflow pain

Daniel Fischer daniel.is.fischer at googlemail.com
Wed Sep 21 10:16:08 CEST 2011


On Thursday 22 September 2011, 01:00:37, Tim Docker wrote: 
> 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.

I suspect that `applyAction x' produces a large thunk for several x in sas, 
and those blow the stack.

You could try forcing evaluation earlier,

mapM' :: (a -> IO [b]) -> [a] -> IO [b]
mapM' act (m:ms) = do
    xs <- act m
    yss <- length xs `seq` mapM' act ms
    return (xs ++ yss)
mapM' _ [] = return []

perhaps even forcing the values of xs (deepseq, if b is an NFData 
instance).
Depending on what your actual problem is, that could help or make it worse.



More information about the Haskell-Cafe mailing list