Name: Anonymous 2014-12-13 9:12
However, this comes with the caveats of using WriterT [a], which is that it will leak space like crazy for a large assembly computation (even if you don't log anything!).
This is wrong.
*Main Control.Monad> take 2 . flip evalState 0 . execWriterT $ forever (byte 4)
[4,4]
Also, when complied with -O2 then
main = print . flip evalState 0 . execWriterT $ forever (byte 4)
appears to run in constant memory.
------------------------------------------
Huh. I guess that works because the whole stack is lazy. However, if you make the base State monad strict then WriterT leaks space:
>>> import Control.Monad.Trans.Writer
>>> import Control.Monad.Trans.State.Strict
>>> import Control.Monad.Trans.Class
>>> import Control.Monad
>>> runStateT (execWriterT (forever (lift (put 1)))) 0 -- Leaks space