Why is there no Monoid instance for Either?

Francesco Mazzoli f at mazzo.li
Thu Nov 27 18:01:04 UTC 2014


Hi Andreas,

Well, we still made the decision to have a similarly arbitrary
instance for `Maybe`, so I thought that instance might be justified
too.

My use case: I'm traversing terms and I want to either collect
meta-variables, or fail.  So I'd like to use `Either Error
MetaVariables` to automatically handle the merging of results for
subterms.  In the end I defined my own type:
<https://github.com/bitonic/tog/blob/master/src/Data/Collect.hs>.


Francesco

On 27 November 2014 at 18:45, Andreas Abel <andreas.abel at ifi.lmu.de> wrote:
> 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