[Haskell-cafe] sequence causing stack overflow on pretty small lists

Tom Ellis tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk
Tue Aug 27 16:24:32 CEST 2013


On Mon, Aug 26, 2013 at 12:05:14PM -0700, Bryan O'Sullivan wrote:
> On Mon, Aug 26, 2013 at 1:46 AM, Niklas Hambüchen <mail at nh2.me> wrote:
> > This is because sequence is implemented as
> >
> >      sequence (m:ms) = do x <- m
> >                           xs <- sequence ms
> >                           return (x:xs)
> >
> > and uses stack space when used on some [IO a].
> >
> 
> This problem is not due to sequence, which doesn't need to add any
> strictness here. It occurs because the functions in System.Random are
> excessively lazy. In particular, randomIO returns an unevaluated thunk.

I don't understand this.  The same stack overflow occurs with

    tenmil :: Int
    tenmil = 10 * 1000 * 1000
    
    main :: IO ()
    main = do  
            list <- replicateM tenmil (return ()) :: IO [()] 
            list `seq` return ()

"return ()" is not excessiely lazy, is it?  Could you explain further?

Tom




More information about the Haskell-Cafe mailing list