[Haskell-cafe] memory leak when using "forever"

Tom Ellis tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk
Fri Nov 4 12:48:48 UTC 2016


On Tue, Oct 04, 2016 at 06:56:48PM +0100, Tom Ellis wrote:
> On Tue, Oct 04, 2016 at 06:19:52PM +0100, Tom Ellis wrote:
> > My results below.  Looks like there's something wrong with *> for ReaderT
> > and StateT.
> 
> This seems to be how it executes
> 
>     let loop = return () *> loop in loop
>     in runReaderT loop ()
[...]

I'm adding an evaluation trace of 'forever' in terms of '>>' to this
discussion because the issue has arisen in


https://www.reddit.com/r/haskell/comments/5axh2j/two_methods_of_using_statet_with_pipes_one_of/


let loop = return () >> loop in loop
in runReaderT loop ()

-- Un-newtype
let loop = return () >> loop in loop
in loop ()

-- Un-newtype
let loop = return () >> loop in loop
in loop ()

-- Definition of >>
let loop = return () >>= (\_ -> loop) in loop
in loop ()

-- >>= for Reader in terms of >>= for m
let loop = \r -> (return () r) >>= (\_ -> loop r) in loop
in loop ()

let loop = \r -> (return () r) >>= (\_ -> loop r) in loop
in (\r -> (return () r) >>= (\_ -> loop r)) ()

let loop = \r -> (return () r) >>= (\_ -> loop r) in loop
in (return () ()) >>= (\_ -> loop ())

-- return () for ReaderT in terms of return () for m
let loop = \r -> (return () r) >>= (\_ -> loop r) in loop
in return () >>= (\_ -> loop ())


So for m = IO evaluation proceeds without a space leak.  We evaluate 'return
()' and then proceed to evaluate 'loop ()' without creating any new thunk.  

'forever' needs urgent fixing!

Tom


More information about the Haskell-Cafe mailing list