[Haskell-cafe] Are there standard idioms for lazy, pure error handling?

Malcolm Wallace malcolm.wallace at cs.york.ac.uk
Mon Nov 30 01:08:50 EST 2009

> I'm working on a library which needs to operate on large data sets, so
> I'd like to use lazy values. ...
> ----------------------------------------
> import qualified Data.Text as T
> parse :: TL.Text -> [Either ParseError Event]
> ----------------------------------------

I would say that this is the most desirable approach, if you are  
generating a sequence, and want lazy processing of the elements.   
Indeed, in my own experience, this is the only reasonable way to deal  
with very large datasets, without running out of memory.

> The main problem I see with these is that they don't indicate or
> enforce that an error terminates the event/text streams. The first
> allows multiple errors in a row, or events to follow an error.

Are you sure that there can be no error recovery, to continue with  
events after a mal-formed event has been discarded?  In many cases, it  
is possible.  However, if you really want to terminate the stream at  
the first error, and to reflect this in the type, then I guess you can  
define your own list type:

data ListThenError e a = Cons a (ListThenError e a)
                        | Error e

Of course this has the disadvantage that then your consumer must  
change to use this type too.


More information about the Haskell-Cafe mailing list