Proposal: Make Semigroup as a superclass of Monoid

Edward Kmett ekmett at gmail.com
Tue Apr 7 04:52:58 UTC 2015


The most likely path forward would bring in:

class Semigroup a where
  (<>) :: a -> a -> a
  (... and sconcat :: NonEmpty a -> a probably hidden in a submodule to
avoid bringing NonEmpty into Prelude, and times1p possibly bikeshedded to
stimes1p for naming consistency, both probably exiled to Data.Semigroup)

class Semigroup a => Monoid a where
  mappend :: a -> a -> a
  mappend = (<>)

  mempty :: a

with a path towards eventually removing mappend from Monoid in 7.18+, which
would get you more or less to your ObBikeshed in the long run, minus a
couple of things, plus a couple of others.

(and possibly a top level mtimes can then be defined in terms of stimes1p
and mempty, giving peasant exponentiation for log time construction of many
monoids and O(1) for idempotent ones.)

As an aside: having a concat that is parameterized on Foldable on the other
hand as a member of the class actually turns out to almost paradoxically
prevent you from doing almost any optimizations for it. (I say almost,
since because we added some members to Foldable in 7.10 this isn't quite
the case any more.) Also to fold you need at least one member, so
Foldable/[] is too weak.

-Edward

On Mon, Apr 6, 2015 at 11:15 PM, wren romano <winterkoninkje at gmail.com>
wrote:

> I'm +1 for getting the Prelude to eventually say:
>
>     class Semigroup a where
>         mappend :: a -> a -> a
>         mconcat :: [a] -> a
>
>     (<>) = mappend
>     infixr 6 <>
>
>     class Semigroup a => Monoid a where
>         mempty :: a
>
>     instance Semigroup a => Monoid (Maybe a) where...
>
>
> The long path seems the most sensible way to get here with a minimum
> of breakage. Tis a shame it'll take so long to complete.
>
>
>
>
> (ObBikeshed: Honestly, I'd rather see:
>
>     class Semigroup a where
>         (<>) :: a -> a -> a
>         concat :: [a] -> a -- or: concat :: Foldable f => f a -> a
>
> ...getting rid of the mwhatever naming scheme and avoiding taking two
> names for one function. The name "concat" is pretty ubiquitously used
> for monomorphic variants of the [a]->a type. The notable exception
> being Foldable.concat, which relies specifically on using the [a]
> semigroup for no especially interesting reason I can discern. But I'm
> pretty sure that ship has sailed unless we give up hope of providing a
> breakage-free upgrade path.)
>
> --
> Live well,
> ~wren
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20150407/f2b8917b/attachment.html>


More information about the Libraries mailing list