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

Joachim Breitner mail at joachim-breitner.de
Thu Jul 3 11:00:04 EDT 2008


Hi,

Am Donnerstag, den 03.07.2008, 15:55 +0200 schrieb Joachim Breitner:
> I have some code that I’ll put somewhere soon.

http://darcs.nomeata.de/odio/ODIO.hs now contains a simple
implementation of the idea, together with more explanation. To show what
the effect is, I wrote a very small program:

1> main = runODIO $ do
2>	c1 <- readFileOD' "inFile1"
3>	c2 <- readFileOD' "inFile2"
4>	c3 <- readFileOD' "inFile3"
5>	liftIO $ putStrLn "Some output"
6>	writeFileOD' "outFile1" (show (length c1 + length c2))
7>	c4 <- readFileOD' "inFile4"
8>	writeFileOD' "outFile2" (show (length c1 + length c3 + length c4))
9>	time <- liftIO $ getClockTime
A>	writeFileOD' "outFile3" (show time ++ c1)

and a script that runs this under various conditions
http://darcs.nomeata.de/odio/demo.sh with the output available at
http://darcs.nomeata.de/odio/demo.out. Note that the primes after the
function calls are just for the verbose variant for demonstration.

Some points to emphasize (you can verify them in the demo output).

 * The 9th line runs an arbitary IO action, so from then on, ODIO can’t
do anything else but to actually write out every file it should.

 * The 5th line does not have this effect. Because this gets desugared
to (>>), the special implementation of (>>) means that the next line
still sees the same dependency state as the before the call to liftIO.

 * A change to inFile3 causes outFile1 to be re-written, although from
looking at the code, _we_ know that this is not necessary, but the ODIO
monad can not tell. The programmer should have swapped the lines.

 * A change only to inFile4 means that outFile1 will not have to
generated, and thanks to lazyness and unsafeInterleaveIO, inFile2 will
not even opened.


Some additions that might be necessary for real world use:
 * ByteString interface
 * a variant of readFileOD with type “FilePath -> IO a -> ODIO a”
if, instead of reading the file directly, you want to call some external
parsing helper (e.g. to read exif data).
 * A even more verbose mode that tells you why exactly a write action
has to be done. This is why I keep a list of Files and Timestamps
around.


I hope this is a basis for even more discussion, and of course
http://darcs.nomeata.de/odio/ is a darcs repository, so feel free to
send patches.

Greetings,
Joachim

-- 
Joachim "nomeata" Breitner
  mail: mail at joachim-breitner.de | ICQ# 74513189 | GPG-Key: 4743206C
  JID: nomeata at joachim-breitner.de | http://www.joachim-breitner.de/
  Debian Developer: nomeata at debian.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: Dies ist ein digital signierter Nachrichtenteil
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20080703/38233501/attachment.bin


More information about the Haskell-Cafe mailing list