Just happened upon these illuminative insights for "Developing an intuition for Monoid" — here are the essentials, distilled and/or paraphrased-for-clarity.
Just a fancy moniker for any sets of values with both:
x <> y(or mappend) that is associative ie.
(x <> (y <> z)) == ((x <> y) <> z),
id(or mempty, or call it nil / default / unit if you will.. you get the idea) such that
(x <> id) == (id <> x) == x
(n + 0) == n
id = 0with
(<>) = (+)is a monoid for numbers
(n × 1) == n
id = 1with
(<>) = (×)is a monoid for numbers
(b || False) == b
id = Falsewith
(<>) = (||)is a monoid for booleans
(b && True) == b
id = Truewith
(<>) = (&&)is a monoid for booleans
(s ++ "") == s; for all lists,
(l ++ ) == l
id = /
id = ""with
(<>) = (++)is a monoid for lists/strings
So intuitively, if there's an obvious (associative) combining function
<> from both [X]-to-X and -to-Xdefault/empty (where also
X == (X <> Xdefault/empty), always) — then that <> with that Xdefault/empty make a monoid for all X.
(), why booleans/numbers can be seen as monoids but aren't
Monoidinstances by default, etc..
Data.Foldable instance for some container (list, map, etc..), you can write a generic
fold function that only takes one argument (the container).
fan instance of
aan instance of
Monoid), then instead of
foldr someFunc startVal containeryou can just write:
Foldablepart is what's important for the container.
Monoidhas no concept of a "container" whatsoever, it simply provides a "zero" value and a way to combine two values together.
So at the end of the day, this all just means that monoids serve as the language used by folds to tear down bigger structures. What exactly that language expresses is up to the
Monoid, and what it means to be torn down is up to the instance of
Grasping all of the above, we can now parse Diehl: «A monoid is a combination of a unit and a single associative operation over a set of values.» So far, I haven't encountered (neither before nor upon having absorbed the above findings) any pressing need of defining and wiring up custom
Monoid instances for explicit use with any
Foldables (or any other purpose), but it'll probably come up some obscure way when least expected.