A monad is just a monoid in the category of endofunctors, what’s the problem?
A monoid is…
- A set, S
- An operation, • : S × S -> S
- An element of S, e : 1 -> S
Examples:
0 + 7 == 7 + 0 == 7
(• ≡ +, S ≡ Natural Numbers, Identity ≡ 0)
[] ++ [1,2,3] == [1,2,3] ++ [] == [1,2,3]
(• ≡ ++, S ≡ [Int], Identity ≡ [])
{} union {apple} == {apple} union {} == {apple}
(• ≡ union, S ≡ {fruits}, Identity ≡ {})
…satisfying these laws:
- (a • b) • c = a • (b • c), for all a, b and c in S
- e • a = a = a • e, for all a in S
A monad is…
- An endofunctor, T : X -> X
- A natural transformation, μ : T × T -> T, where × means functor composition
- A natural transformation, η : I -> T, where I is the identity endofunctor on X
…satisfying these laws:
- μ(μ(T × T) × T)) = μ(T × μ(T × T))
- μ(η(T)) = T = μ(T(η))
Tying it all together
- a monad is a structure that defines a way to combine functions,
- analogously to how a monoid is a structure that defines a way to combine objects,
- where the method of combination is associative,
- and where there is a special ‘No-op’ that can be combined with any Something to result in Something unchanged.
(Full article: http://stackoverflow.com/questions/3870088/a-monad-is-just-a-monoid-in-the-category-of-endofunctors-whats-the-problem)