[Haskell-cafe] commutative monoid?
qdunkan at gmail.com
Sat Jun 25 07:34:37 CEST 2011
Not being especially mathematically inclined, every once and a while I
get a little panicked when I notice that, e.g. Data.Map mappend is a
plain left-biased union, and doesn't actually mappend the values of
Then I remember that monoids are associative, not commutative, and
it's totally allowed to do that. Even if you mappend the values, the
result is only as commutative as the monoid instance for the values.
But clearly you can make that choice. For instance, Maybe's Monoid
wants to mappend the elements. This is always a little disconcerting
to me because the MonadPlus instance for Maybe doesn't do that.
So there's a range of possible Monoid instances for each type, and
maybe they were chosen by historical happenstance rather than some
kind of "principle monoid" (is there such a thing?). Is there a name
for the thing that's like a monoid, but the operator is commutative
too? That would narrow down the implementation choices, e.g. Data.Map
would have to combine the values. So it seems like if your operation
is commutative, you can put it in "c-monoid" and not rely so much on
the happenstance of the instances, since they are more constrained.
And of course you are then free to reorder things, which is a nice bit
So is there a typeclass for that?
This article is about the mathematical concept. For the alien
creatures in the Doctor Who adventure, see The Ark (Doctor Who).
I guess I never saw that episode... but it sounds like they're closer
to warm fuzzies than the slimy sleeping bag monster I imagined.
More information about the Haskell-Cafe