How do I read "IO is not lazy" ?<br> Is IO (>>=) forcing the evaluation of its arguments, causing the unwanted neverending loop?<br>And, this happens even in (MonadTrans t => t IO) (>>=) ?<br><br>
Thanks<br><br>paolino<br><br><div class="gmail_quote">2008/12/31 Ryan Ingram <span dir="ltr"><<a href="mailto:ryani.spam@gmail.com" target="_blank">ryani.spam@gmail.com</a>></span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
IO is not lazy; you never make it to "print".<br>
<br>
Consider this program:<br>
<br>
> k = f 0 where<br>
> f n = do<br>
> lift (print n)<br>
> tell [n]<br>
> f (n+1)<br>
<br>
> weird :: IO [Int]<br>
> weird = do<br>
> (_, ns) <- runWriterT k<br>
> return (take 20 ns)<br>
<br>
What should "weird" print? According to "k", it prints every Int from<br>
0 up. Aside from the extra printing, it has the same behavior as your<br>
writer.<br>
<br>
For the result of a WriterT to be lazy readable, you need both the<br>
monoid to be lazy readable, and the transformed monad to be lazy,<br>
which IO isn't.<br>
<br>
-- ryan<br>
<br>
2008/12/31 Paolino <<a href="mailto:paolo.veronelli@gmail.com" target="_blank">paolo.veronelli@gmail.com</a>>:<br>
<div><div></div><div>> As someone suggested me, I can read the logs from Writer and WriterT as<br>
> computation goes by,<br>
> if the monoid for the Writer is lazy readable.<br>
> This has been true until I tried to put the IO inside WriterT<br>
><br>
><br>
>> {-# LANGUAGE FlexibleContexts #-}<br>
>> import Control.Monad.Writer<br>
><br>
><br>
>> k :: (MonadWriter [Int] m) => m [Int]<br>
><br>
>> k = let f x = tell [x] >> f (x + 1) in f 0<br>
><br>
><br>
>> works :: [Int]<br>
>> works = snd $ runWriter k<br>
><br>
><br>
>> hangs :: IO [Int]<br>
>> hangs = snd `liftM` runWriterT k<br>
><br>
><br>
>> main = take 20 `liftM` hangs >>= print<br>
><br>
><br>
><br>
> The main hangs both interpreted and compiled on ghc 6.10.1.<br>
><br>
> The issue is not exposing with IO alone as<br>
><br>
> main = print "test" >> main<br>
><br>
> is a working program.<br>
><br>
> Thanks for explanations.<br>
><br>
><br>
> paolino<br>
><br>
><br>
</div></div>> _______________________________________________<br>
> Haskell-Cafe mailing list<br>
> <a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
> <a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
><br>
><br>
</blockquote></div><br>