[Haskell-cafe] Need some advice around lazy IO

Konstantin Litvinenko to.darkangel at gmail.com
Mon Mar 18 18:11:11 CET 2013


On 03/18/2013 06:06 PM, Dan Doel wrote:
> Do note that deepSeq alone won't (I think) change anything in your
> current code. bug will deepSeq the file contents.

rfn fully evaluate 'bug' by reading all file content. Later hClose will 
close it and we done. Not reading all content will lead to semi closed 
handle, leaked in that case. Handle will be opened until hGetContents 
lazy list hit the end.

  And the cons will
> seq bug. But nothing is evaluating the cons. And further, the cons
> isn't seqing the tail, so none of that will collapse, either. So the
> file descriptors will still all be opened at once.
>
> Probably the best solution if you choose to go this way is:
>
>      bug <- evaluate (fileContents2Bug $!! str)
>
> which ties the evaluation of the file contents into the IO execution.
> At that point, deepSeqing the file is probably unnecessary, though,
> because evaluating the bug will likely allow the file contents to be
> collected.

evaluate do the same as $! - evaluate args to WHNF. That won't help in 
any way. Executing in IO monad doesn't imply strictness Thats why mixing 
lazy hGetContent with strict hOpen/hClose is so tricky.





More information about the Haskell-Cafe mailing list