[Haskell-cafe] Either Monad and Laziness

Eric Velten de Melo ericvmelo at gmail.com
Tue Sep 11 18:36:52 CEST 2012


I am currently trying to rewrite the Graphics.Pgm library from hackage
to parse the PGM to a lazy array. The current implementation parses it
straight to UArray, which is strict.

The behaviour I want to achieve is like this: I want the program when
compiled to read from a file, parsing the PGM and at the same time
apply transformations to the entries as they are read and write them
back to another PGM file.

The problem is that even using a lazy array structure, because the
parser returns an Either structure it is only possible to know if the
parser was successful or not after the whole file is read, therefore
requiring it to read the entire file before applying the
transformations, ruining the property of laziness.

Is there some way to keep this from happening? Should I even want to
make it like this? Not really a real life situation, but imagine I
want to read a reaaaaally large PGM file which does not fit into RAM
memory and I don't want to be forced to have the whole array in the
memory at once.

One alternative I thought was parsing only the PGM header and then
read the rest of the input without using Parsec and the Either Monad.
In the event the data is corrupted, though, I would not know how to
recover from it.

Any thoughts? Hopefully I'm not saying anything really stupid.


More information about the Haskell-Cafe mailing list