[Haskell-cafe] A Monad for on-demand file generation?

David Roundy droundy at darcs.net
Tue Jul 1 08:41:31 EDT 2008

On Tue, Jul 01, 2008 at 10:22:35AM +0000, Joachim Breitner wrote:
> Hi,
> Am Dienstag, den 01.07.2008, 11:53 +0200 schrieb Ketil Malde:
> > Joachim Breitner <mail at joachim-breitner.de> writes:
> > 
> > >> 1) unsafeInterleaveIO seems like a big hammer to use for this problem,
> > >> and there are a lot of gotchas involved that you may not have fully
> > >> thought out.  But you do meet the main criteria (file being read is
> > >> assumed to be constant for a single run of the program).
> > 
> > > Any other gotcha? 
> > 
> > The one that springs to mind is that you might run out of file
> > handles. At least on Linux, that's a precious resource.
> but at least then, (unsafeInterleaveIO readFile) is actually better than
> (readFile), because if I consume the files in sequence and complete,
> they will be opened and closed in sequence with the first one, but be
> opened all at once with the second. At least it won’t be worse, because
> the file will not be closed later, and possibly opened later.

Indeed, the best option (in my opinion) would be

unsafeInterleaveIO readFileStrict

(where you might need to write readFileStrict).  In darcs, we use lazy IO a
lot, but never lazily read a file, precisely due to the open file handle
issue.  This works pretty well, and your scenario is precisely the one in
which unsafeInterleaveIO shines.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20080701/478887dc/attachment.bin

More information about the Haskell-Cafe mailing list