Why is there no Monoid instance for Either?

Andreas Abel andreas.abel at ifi.lmu.de
Thu Nov 27 17:45:27 UTC 2014


Hi Francesco,

maybe you should add the use cases you have in mind, to strengthen your 
case.

A priori, it is not clear why an error-propagating monoid should be 
preferred over an error-correcting one.  (Both seem useful.)
Your proposal has the flavor of "All" whereas the Semigroup one 
corresponds to "Any".

Personally, I think neither of the alternatives is sufficiently 
canonical (both versions throw away information), thus, there shouldn't 
be instances (also not for Semigroup).

Cheers,
Andreas

On 27.11.2014 18:29, Francesco Mazzoli wrote:
> 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
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://www.haskell.org/mailman/listinfo/libraries
>


-- 
Andreas Abel  <><      Du bist der geliebte Mensch.

Department of Computer Science and Engineering
Chalmers and Gothenburg University, Sweden

andreas.abel at gu.se
http://www2.tcs.ifi.lmu.de/~abel/


More information about the Libraries mailing list