Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MonadAccum instance for ParsecT #522

Open
olafklinke opened this issue Mar 30, 2023 · 5 comments
Open

MonadAccum instance for ParsecT #522

olafklinke opened this issue Mar 30, 2023 · 5 comments

Comments

@olafklinke
Copy link
Contributor

olafklinke commented Mar 30, 2023

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
@olafklinke
Copy link
Contributor Author

MonadAccum exists only in mtl >= 2.3 and the current lower bound is 2.2.2 so the bound in the dependency must be increased to be able to implement the instance.

@mrkkrp
Copy link
Owner

mrkkrp commented Mar 31, 2023

It is not there because at the time no such thing as MonadAccum existed in mtl. You are very welcome to open a PR adding this instance, which I will gladly merge.

@Lev135
Copy link
Contributor

Lev135 commented Jun 20, 2023

It's clear about MonadAccum, but why there is no MonadWriter instance (it seems to be old enough)? Are there any problems with it?

@olafklinke
Copy link
Contributor Author

olafklinke commented Jun 21, 2023 via email

@olafklinke
Copy link
Contributor Author

The PR is able to merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants