Why is there no Monoid instance for Either?

Francesco Mazzoli f at mazzo.li
Thu Nov 27 17:29:28 UTC 2014


Hi,

Lately, I found myself wanting to use the following Monoid instance:

````
instance (Monoid b) => Monoid (Either a b) where
  mempty = Right mempty

  Left e  `mappend` _       = Left e
  _       `mappend` Left e  = Left e
  Right x `mappend` Right y = Right (x `mappend` y)
````

It would seem quite justified to me, given the left-bias of Either
elsewhere in base.  Is there any particular reason why it shouldn't be
defined?

It would break the instance for Either in `semigroups`, which is

````
instance Semigroup (Either a b) where
  Left _ <> b = b
  a      <> _ = a
````

But I think mine is more useful, and has the additional advantage of
having `mappend` to behave very similar to the `mappend` for `Maybe`.

Francesco


More information about the Libraries mailing list