[Haskell-cafe] Lazy IO and closing of file handles

Brandon Michael Moore brandon at regurgitate.ugcs.caltech.edu
Thu Mar 15 03:39:56 EDT 2007

On Wed, Mar 14, 2007 at 06:05:31PM -0700, David Brown wrote:
> Greg Fitzgerald wrote:
> > What we need is a library for a readonly filesystem.  That is, all
> > the same functions but pure.  I believe you could make this readonly
> > library by wrapping each readonly I/O function with
> > 'unsafeInterleaveIO' or 'unsafePerformIO', but I don't really
> > understand the consequences of using 'unsafe' calls, so I avoided it
> > myself.
> SlurpDirectory from darcs tries to do this.  It is likely just
> specific to darcs needs, but perhaps it would be a useful thing.
> I've thought about making something like this for the purposes of
> harchive, but concluded that I need too much control over when things
> happen.  Having a memory leak that tries to read an entire filesystem
> into memory isn't going to work well :-)
> Dave

I haven't looked at the darcs function, but for this problem something like

readFiles :: [FilePath] -> IO [String]
readFiles files = return (map (unsafePerformIO . readFile) files)


readFiles2 files = mapM (unsafeInterleaveIO . readFile) files

probably works, as long as you read each file completely before going to the
next. If the thunk returned by readFile was so lazy as to not even open the
file before being forced you might not need this sort of thing at all
(but it couldn't report problems at the readFile site). If you don't process
each file completely you could make accessing any list element force earlier
files with something like this

serialize [] = []
serialize (a:as) = a:serialize' a as
  where serialize' a (b:bs) = a `seq` b : serialize' b bs
        serialize' _ [] = []


More information about the Haskell-Cafe mailing list