[Haskell-cafe] letting go of file handles and Data.Binary

Bryan O'Sullivan bos at serpentine.com
Sun Apr 20 21:52:31 EDT 2008

Ben wrote:

> i played around with all that you suggested, and came to the conclusion
> that i don't understand seq!

That's certainly possible, but you also got the type of your first
forcing function wrong :-)

> strictDecodeFile :: Binary a => FilePath -> (a -> b) -> IO ()

>   encodeFile fname dat
>   strictDecodeFile fname (\x -> do print "strict 1"
>                                    print (x == dat))
>   removeFile fname

You provided a function that returns an IO action.  Note that
strictDecodeFile ensures that the result of force is evaluated to WHNF,
but it doesn't know what the *type* of the result is.  If you return an
IO action, there's no way it can be run, because the caller can't even
tell that you returned an IO action: it just knows that you returned
something of an unknown type "b".  If the IO action can't be run, the
comparison inside can't be performed, and so nothing useful actually

Instead, just provide a pure function to force the decoding: something
as simple as (==dat) will do.  Evaluating this to WHNF will reduce it to
the constructor True or False.  In order to produce a constructor,
enough of the decoded data should be demanded to suit your needs.

Developing a good enough mental model of how laziness works is a very
useful way to spend some time.


More information about the Haskell-Cafe mailing list