[Haskell-cafe] commutative monoid?

Evan Laforge qdunkan at gmail.com
Sat Jun 25 18:00:05 CEST 2011

>> So is there a typeclass for that?
> There might be one hidden in one of the attempts at redesigning the
> numeric hierarchy (e.g., Numeric Prelude), but there's not a canonical
> typeclass for them. Unfortunately it's not really a good match for the
> typeclass system since it doesn't introduce any new operations, it only
> introduces laws--- which aren't verified nor enforced.
> Though, if you happen to know the property does hold, then you're free to
> take advantage of it. (E.g., for the Maybe instance which uses a semigroup
> operation, if the semigroup op is commutative then so is the monoid op.)

That's a good point about laws vs. typeclasses.  Though I think if I
were doing something that relied on commutativity for e.g. Maybe I
would still define my own typeclass.  That way at least there is
documentation in the name without having to put in a comment
everywhere saying "btw I'm relying on this xyz", and if I want to give
the same treatment to some other types then I don't have to deal with
newtype wrappers.  While I'm sure 'map Newtype biglist' can be
optimized away, I'm not so sure about 'Map.map (second Newtype)

But in any case, it's fine the stdlib doesn't have one, because it's
easy enough to write your own.


More information about the Haskell-Cafe mailing list