Skip to content

MonadZero and MonadPlus seem redundant? #51

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

Closed
LukaJCB opened this issue Jan 31, 2019 · 10 comments
Closed

MonadZero and MonadPlus seem redundant? #51

LukaJCB opened this issue Jan 31, 2019 · 10 comments

Comments

@LukaJCB
Copy link

LukaJCB commented Jan 31, 2019

Given the laws of Monad that state apply = ap as well as the laws of Alternative that state
empty <*> f = empty, we can practically derive the MonadZero law empty >>= f = empty.
Thus, it seems to me that we do not need to specify this law in addition to the laws already present.

Furthermore, MonadPlus has the requirement that >>= distribute over <|>. Though as it extends Alternative which already requires distribution of <*> over <|> and again, since <*> = ap we already get that property. Am I missing something here?

@garyb
Copy link
Member

garyb commented Jan 31, 2019

Hmm yeah, that does seem right!

@hdgarrood
Copy link
Contributor

Sort of related: #24, #35. Does this mean Maybe isn't a valid Alternative?

@hdgarrood
Copy link
Contributor

To add to that: it's not entirely clear to me that if we have <*> distributing over <|> plus apply = ap, that this implies that >>= distributes over <|>. Do you have a proof of this?

@LukaJCB
Copy link
Author

LukaJCB commented Jan 31, 2019

I don't have a proof and maybe I'm wrong here, what is the case where Maybe doesn't distribute?

@hdgarrood
Copy link
Contributor

> x = Just false
> y = Just true
> f cond = if cond then Just 0 else Nothing

> (x <|> y) >>= f
Nothing

> (x >>= f) <|> (y >>= f)
(Just 0)

This is why Maybe has MonadZero but not MonadPlus.

@LukaJCB
Copy link
Author

LukaJCB commented Jan 31, 2019

Very interesting! Of course the above won't work for <*> so I guess that distinction is safe. I wonder if my misconception also extends to absorption.

@hdgarrood
Copy link
Contributor

Yeah, I've verified that Maybe is indeed a valid Alternative so MonadPlus isn't redundant. I'm not sure about absorption though: all of the monads I can think of which have Alternative instances satisfy both the <*> and >>= absorption laws.

@hdgarrood
Copy link
Contributor

Oops, the MonadZero question isn't yet resolved.

@hdgarrood hdgarrood reopened this Apr 21, 2019
@hdgarrood
Copy link
Contributor

I think you’re right about MonadZero: suppose F is a type constructor with valid Monad and Alternative instances. Now the type of its empty is forall a. F a, which means that it can’t possibly contain a value of whatever type a ends up being instantiated as. Now if we write empty >>= f, the implementation of >>= can’t apply f at any point, because we don’t have anything to apply it to. Therefore empty >>= f can’t depend on the behaviour of f. Therefore empty >>= f is the same as empty >>= g for any fixed g whose result type is the same as f. In particular, if we pick g = pure, the right identity monad law ensures that empty >>= pure = empty, and so empty >>= f = empty for all f.

@hdgarrood
Copy link
Contributor

I'm going to close this, since the MonadPlus question appears resolved, and we can continue the discussion of what to do about MonadZero in #62.

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

3 participants