[Haskell-cafe] Is it safe to use unsafePerformIO here?

Cristiano Paris cristiano.paris at gmail.com
Tue Sep 15 15:29:24 EDT 2009


On Tue, Sep 15, 2009 at 9:13 PM, Daniel Fischer
<daniel.is.fischer at web.de> wrote:
> ...
> Still, the body should be read lazily.
> I'm not sure, but the tracing message may be output because of its position.
>
> With
>
> where
>    readBody = withFile fn ReadMode $ \h -> do
>        b <- hGetContents h
>        seq b $ return (trace ("Read body from: " ++ fn) b)
>
> there's no tracing output.

Impressive as I can't spot the difference. But my question is: is the
chunk of data actually read or not? Consider the following code:

----
readBit fn = withFile fn ReadMode $ \h -> Bit <$> (hGetLine h >>=
return . read) <*> readBody
             where readBody = trace "In readBody"
                              $ withFile fn ReadMode
                                $ \h -> do b <- hGetContents h
                                           seq b $ return $ trace
("Read body from: " ++ fn) b
----

The message "In readBody" gets written, while "Read body from..."
don't. So? I think that the IO action is actually performed but the
result is simply discarded. Am I right?

Cristiano


More information about the Haskell-Cafe mailing list