Why is there no Monoid instance for Either?

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

Why is there no Monoid instance for Either?

Francesco Mazzoli
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
[hidden email]
http://www.haskell.org/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Why is there no Monoid instance for Either?

Andreas Abel
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
> [hidden email]
> 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

[hidden email]
http://www2.tcs.ifi.lmu.de/~abel/
_______________________________________________
Libraries mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Why is there no Monoid instance for Either?

Francesco Mazzoli
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 <[hidden email]> 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
>> [hidden email]
>> 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
>
> [hidden email]
> http://www2.tcs.ifi.lmu.de/~abel/
_______________________________________________
Libraries mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/libraries