5

In Haskell, why is the infix alias of mappend (from class Monoid) <> instead of +? In algebra courses + is usually used for the binary operator of a monoid.

duplode
  • 33,731
  • 7
  • 79
  • 150
Robert Zaremba
  • 8,081
  • 7
  • 47
  • 78
  • xmonad uses [`<+>`](http://xmonad.org/xmonad-docs/xmonad/XMonad-ManageHook.html#v:-60--43--62-) for `mappend`; IIRC this was introduced before `<>` was added to the standard libraries. – raymonad May 21 '14 at 22:53

2 Answers2

14

The function + is specific to numbers, and moreover, it's only one way to implement Monoid for numbers (* is equally valid). Similarly, with booleans, it would be equally valid to use && and ||. Using the symbol + suggests that Monoids are about addition specifically, when really they're just about any associative operation.

It is true that, at least in my experience, one is likely to use mappend in a fashion resembling addition: concatenating lists or vectors, taking unions of sets or maps, etc, etc. However, the Haskell mindset favors generality and adherence to mathematical principles over (arguably) what is more intuitive. It's certainly reasonable, in my opinion, to think of mappend as a sort of general addition, and make adjustments in the cases where it isn't.

limp_chimp
  • 13,475
  • 17
  • 66
  • 105
  • That is also a pretty good reason IMO. I only didn't mention it because it is not uncommon to see people suggesting that `++` should be used as an alias (e.g. [classy-prelude](http://hackage.haskell.org/package/classy-prelude-0.9.2/docs/ClassyPrelude.html)). – duplode May 21 '14 at 22:53
  • Also I agree with Boolean, `*` and `+` are not symmetrical. `+` is a group where `*` is not (until remote 0). Therefore, `+` is somehow *superior* to `*` and not equivalent. It would make sense IMHO to use it as the default monoid for `Num` – mb14 Jul 08 '14 at 08:52
  • In support of this answer, it is worth noting that this was the motivation originally given for the suggestion of using `<>` as the infix synonym of `mappend`. See [this Libraries mailing list message from Ross Paterson](https://mail.haskell.org/pipermail/libraries/2009-September/012540.html). – duplode Apr 27 '19 at 14:28
5

Partly due to the principle of least astonishment and partly because there are at least two sensible monoid instances for numbers (namely, Sum and Product from Data.Monoid).

Community
  • 1
  • 1
duplode
  • 33,731
  • 7
  • 79
  • 150
  • 1
    thanks for *principle of least astonishment* link. It would be possible to write `Product 5 + Product 2`, but that really can make astonishment. – Robert Zaremba May 21 '14 at 22:52