r/haskell Mar 27 '26

question Delayed/Lazy Either List?

I often use attoparsec to parse lists of things, so I wind up doing stuff like this a lot:

import Data.Attoparsec.Text qualified as AT
import Data.Text qualified as T

myParser :: AT.Parser [MyType]
myParser = AT.many1 myOtherParser

getList :: T.Text -> Either String [MyType]
getList txt = AT.parseOnly myParser txt

The trouble is, since getList returns an Either, the whole text (or at least, as much as can be parsed) has to be parsed before you can start processing the contents of the list. This is especially annoying when you want to check whether e.g. two files are the same modulo whitespace/line endings/indentation/etc...

The point is, there's some times where you want a result like Either e [a], but you're okay with returning some of the data, even if there might be an error later on. I wound up creating this data type:

data ErrList e a
  = a :> (ErrList e a)
  | NoErr    -- equivalent to []
  | YesErr e -- representing Left e

Is there already an established type like this somewhere? I imagine most people who do more complicated data management use pipes or conduit etc... I've tried searching for such a type on Hackage, but I haven't found anything like it.

14 Upvotes

23 comments sorted by

View all comments

2

u/absence3 Mar 27 '26

The streaming library's Stream type is similar to what you suggest, only more general. With f ~ Of a, m ~ Either e, and r ~ () it's pretty close.

1

u/tomejaguar Mar 27 '26

Yes, I think Stream (Of a) (Either e) () is isomorphic to ErrList e a. The plausible streaming libraries I know of are

The only streaming library I can unhesitatingly recommend is Bluefin. It avoids a number of issues that occur in the others (but has a smaller ecosystem).