Proposal: Add Data.Semigroup to base, as a superclass of Monoid

Edward Kmett ekmett at gmail.com
Tue Jun 11 23:50:48 CEST 2013


I find myself somewhat on the fence about this proposal.

Ultimately, I'd like to see semigroups in base.

That said doing so with the current changes in flight is somewhat awkward,
and introducing it carries a lot of distributed work.

We have two outstanding proposals that have received overwhelming support:
namely the Applicative => Monad (AMP) proposal and the generalization of
the types in the Prelude for mapM, etc. to use Foldable and Traversable
(FT). Between these two proposals, we wind up with the need to bring in
Foldable, Traversable, Applicative and Monoid into the Prelude. Applicative
as a superclass of Monad and for traverse and Monoid because Foldable
brings in foldMap.

Consequently fixing the situation to make Semigroup a superclass of Monoid
isn't something that can be done purely by adding a module to base, but it
would infect Prelude.
*
*
This makes it have to clear a much higher bar!

A real proposal that involved adding Semigroup as a superclass of Monoid
could possibly be concocted like David's AMP patch, whereupon Monoid would
be given a default mappend = (<>)

That could enable users to just have to write an extra instance line like
they would for the AMP, but it has some immediate headaches.

In particular (<>) has been in use in pretty printing libraries since time
immemorial. Suddenly exporting a version from the Prelude is likely to be a
fairly breaking change to those libraries as the associativity of the (<>)
provided by Data.Monoid (and the one provided by Data.Semigroup) both
disagree with the (<>) in Text.PrettyPrint.HughesPJ, which is in the
platform already. This could lead to some rather annoying breakages --
worse, silent breakages. I seem to recall that there was some concern that
this would change the output of some pretty printing in GHC when the
associativity issue was last raised on this list over the introduction of
(<>).

I think to have a serious proposal that could actually be accepted, a lot
of work would have to be put into a patch and then it would need to be
explored how much the associativity issue bites users in practice along
with gauging the amount of effort that would be involved in retrofitting
semigroup instances into virtually everyone's libraries.

My knee jerk reaction is to try to put this off until the dust has settled
from the AMP and FT changes and push back with a request for the necessary
analysis.

-Edward


On Tue, Jun 11, 2013 at 5:05 PM, Mario Blažević <blamario at acanac.net> wrote:

> On 06/11/13 14:46, John Wiegley wrote:
>
>> 1. I propose that we add the following package to base:
>>
>>       http://hackage.haskell.org/**packages/archive/semigroups/0.**
>> 9.2/doc/html/Data-Semigroup.**html<http://hackage.haskell.org/packages/archive/semigroups/0.9.2/doc/html/Data-Semigroup.html>
>>
>>     This is somewhat in the spirit of the AMP proposal: further improving
>> the
>>     correctness of our algebraic abstractions.
>>
>
>
>     I was wondering how much longer until this proposal came up. +1 from
> me in general, but I have some quibbles with details.
>
>
>
>  2. That we make Semigroup a superclass of Monoid, so that (minimally):
>>
>>       class Semigroup a where
>>           (<>) :: a -> a -> a
>>
>>       class Semigroup a => Monoid a where
>>           mempty :: a
>>           mconcat :: [a] -> a
>>           mconcat = foldr (<>) mempty
>>
>>       mappend :: Semigroup a => a -> a -> a
>>       mappend = (<>)
>>
>
>
>     +1, though I'd prefer to leave mappend restricted to the Monoid class.
> In the long term, I'd rather have it killed off than kept as a synonym for
> (<>). Besides, it's m(onoid)append.
>
>
>
>
>  3. (Optional, recommended) There are other useful functions that can be
>> added
>>     to Semigroup, such as sconcat and times1p, but I will let Edward
>> speak to
>>     whether those should be proposed at this time.
>>
>
>
>     They should be added in now or never, there's no reason to break
> compatibility twice. I don't think times1p can be accepted in its current
> form as it depends on a different library. Edward, can you make a concrete
> proposal for these?
>
>
>
>> 4. (Optional, recommended) That we fix the Monoid instance for Maybe to
>> be:
>>
>>      instance Semigroup a => Monoid (Maybe a) where
>>           mempty = Nothing
>>
>
>       instance Semigroup a => Semigroup (Maybe a) where
>>           Just x <> Just y = Just (x <> y)
>>           Nothing <> x = x
>>           x <> Nothing = x
>>
>
>
> +1
>
>
>
>
> ______________________________**_________________
> Libraries mailing list
> Libraries at haskell.org
> http://www.haskell.org/**mailman/listinfo/libraries<http://www.haskell.org/mailman/listinfo/libraries>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/libraries/attachments/20130611/1f46f096/attachment-0001.htm>


More information about the Libraries mailing list