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

Accumulating Monad instance? #11

Open
Warbo opened this issue Oct 18, 2017 · 0 comments
Open

Accumulating Monad instance? #11

Warbo opened this issue Oct 18, 2017 · 0 comments

Comments

@Warbo
Copy link

Warbo commented Oct 18, 2017

Looking through the source, we have AccValidation which is has no Monad but does accumulate errors, and we have Validation which is a Monad but doesn't accumulate errors. Is there a reason that AccValidation doesn't have a Monad instance? For example, I've used this in my own code:

instance (Semigroup e) => Monad (AccValidation e) where
    return = pure
    (AccSuccess x) >>= f = f x
    (AccFailure e) >>= f = AccFailure e

This seems to obey the monad laws. Is there some reason to avoid it? The haddocks on hackage specifically say that AccValidation is an example of an Applicative which isn't a Monad, but the above implementation seems to contradict that assertion.

The Semigroup constraint is there to appease the existing instance of the Applicative superclass (now that Monad is a subclass of Applicative). My intuition is that two operations combined "applicatively" are independent, and hence we're able to report errors from both (given a suitable way to combine such errors, i.e. Semigroup), whilst two operations combined "monadically" are 'chained', such that the second operation is applied to successful results from the first, and hence if the first operation fails we have no way to know what errors the second operation "might have encountered" (since we have no input to run it on).

tonymorris added a commit that referenced this issue Oct 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant