Alternative instance for Either

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Alternative instance for Either

Nathan van Doorn
Proposal: add:

instance Monoid e => Alternative (Either e) where
  empty = Left mempty
  Left a <|> Left b = Left (a `mappend` b)
  Right a <|> _ = Right a
  _ <|> Right b = Right b

instance Monoid e => MonadPlus (Either e) where
  ...

to base.

This is a reasonably obvious instance which I am pretty sure is law abiding.

It'd be useful for defining a series of computations which may fail, where we only care about one success.

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Alternative instance for Either

Tony Morris-4

Alternatively :) or more like, something to think about, an Alt instance for Either.

class Alt f where
  (<!>) :: f a -> f a -> f a

instance Alt Either where
  Right b <!> _ = Right b
  Left a <!> x = x

https://hackage.haskell.org/package/semigroupoids/docs/Data-Functor-Alt.html

There is something iffy about Monoid e => Alternative (Either e) but I can't put my finger on it.


On 06/14/2018 08:37 PM, Nathan van Doorn wrote:
Proposal: add:

instance Monoid e => Alternative (Either e) where
  empty = Left mempty
  Left a <|> Left b = Left (a `mappend` b)
  Right a <|> _ = Right a
  _ <|> Right b = Right b

instance Monoid e => MonadPlus (Either e) where
  ...

to base.

This is a reasonably obvious instance which I am pretty sure is law abiding.

It'd be useful for defining a series of computations which may fail, where we only care about one success.


_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries


_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

signature.asc (499 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Alternative instance for Either

Chris Wong-2
I think the iffy feeling comes from the proposal breaking the distributive law. For example:

(Right f <|> Right g) <*> Left x
  = Left x

but

(Right f <*> Left x) <|> (Right g <*> Left x)
  = Left (x <> x)

On Thu, Jun 14, 2018, 22:42 Tony Morris <[hidden email]> wrote:

Alternatively :) or more like, something to think about, an Alt instance for Either.

class Alt f where
  (<!>) :: f a -> f a -> f a

instance Alt Either where
  Right b <!> _ = Right b
  Left a <!> x = x

https://hackage.haskell.org/package/semigroupoids/docs/Data-Functor-Alt.html

There is something iffy about Monoid e => Alternative (Either e) but I can't put my finger on it.


On 06/14/2018 08:37 PM, Nathan van Doorn wrote:
Proposal: add:

instance Monoid e => Alternative (Either e) where
  empty = Left mempty
  Left a <|> Left b = Left (a `mappend` b)
  Right a <|> _ = Right a
  _ <|> Right b = Right b

instance Monoid e => MonadPlus (Either e) where
  ...

to base.

This is a reasonably obvious instance which I am pretty sure is law abiding.

It'd be useful for defining a series of computations which may fail, where we only care about one success.


_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries