Skip to content

MonadAccum instance for ParsecT #522

Open
@olafklinke

Description

@olafklinke

There exists an instance

(Stream s, MonadState st m) => MonadState st (ParsecT e s m)

and State is more expressive than Writer, in the sense that any State monad can implement the MonadWriter interface for monoidal state. Hence I wonder why there are no instances

(Stream s, MonadAccum w m) => MonadAccum w (ParsecT e s m)
(Stream s, MonadWriter w m) => MonadWriter w (ParsecT e s m) 

I claim that accumulating some value while parsing is a frequent use case and therefore should be supported by any parsing library. After all, ParsecT does accumulate its own errors and hints. Certainly tell a.k.a. add is easy enough to implement:

addP :: MonadAccum w m => w -> ParsecT e s m ()
addP w = ParsecT (\s _ _ whenOK _ -> add w >> (whenOK () s mempty))

and look can be implemented via lift:

lookP :: MonadAccum w m => ParsecT e s m w
lookP = lift look

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions