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

optional is too constrained. #45

Closed
HuwCampbell opened this issue Apr 3, 2020 · 4 comments
Closed

optional is too constrained. #45

HuwCampbell opened this issue Apr 3, 2020 · 4 comments

Comments

@HuwCampbell
Copy link

I don't think optional needs the Plus part of Alternative.

optional :: forall f a. Alt f => Applicative f => f a -> f (Maybe a)
optional a = map Just a <|> pure Nothing

I have had to locally fork this for an experiment I'm doing.

@HuwCampbell
Copy link
Author

The reason is that Plus requires that an empty element exists, but that's not always true.

srghma added a commit to srghma/purescript-maybe that referenced this issue May 3, 2020
@hdgarrood
Copy link
Contributor

I think this makes sense. For a concrete use case, I can see the less constrained version being useful for a parsing library, where you might want to provide an Alt instance for trying one parser and then trying some other parser if the first one doesn't succeed, but where you don't want to provide a Plus instance, because that requires you to introduce the concept of an "empty" (i.e. useless) error message.

Haskell's semigroupoids library provides the less constrained version of optional too: https://hackage.haskell.org/package/semigroupoids-5.3.4/docs/Data-Functor-Alt.html#v:optional

@hdgarrood
Copy link
Contributor

Ha, I just read your optparse PR and realised that's pretty much exactly your reasoning too! I came across this same issue independently in hdgarrood/aeson-better-errors#12.

JordanMartinez pushed a commit that referenced this issue Oct 14, 2020
@hdgarrood
Copy link
Contributor

hdgarrood commented Oct 14, 2020

Resolved by #47. Thanks!

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

2 participants