Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the `mappend` infix alias `<>` instead of `+`?

Tags:

haskell

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.

like image 671
Robert Zaremba Avatar asked May 21 '14 22:05

Robert Zaremba


2 Answers

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.

like image 69
limp_chimp Avatar answered Nov 10 '22 12:11

limp_chimp


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).

like image 28
duplode Avatar answered Nov 10 '22 13:11

duplode