https://blog.scottnonnenberg.com/whatsamonaddiggingintohaskell/
Tag Archives: monad
Haskell Lectures Monad
https://www.quora.com/WhydoweneedMonadtoencapsulatesideeffectsinHaskell/answer/BartoszMilewski?share=e10473c1&srid=ZyHj
“Monads are NOT about side effects. They are about composition. There are ways to express side effects using pure functions. But if you want to compose them, you need a monad”. Watch this video:
Monoid and Monad
Quora: What is the difference between monoid and monad? by Mort Yao https://www.quora.com/Whatisthedifferencebetweenmonoidandmonad/answer/MortYao?share=41115848&srid=ZyHj
A wellsaid, perhaps the briefest ever answer is: A monad is just a monoid in the category of endofunctors.
A monoid is defined as an algebraic structure (generally, a set) M with a binary operation (multiplication) • : M × M → M and an identity element (unit) η : 1 → M, following two axioms:
i. Associativity
∀ a, b, c ∈ M, (a • b) • c = a • (b • c)
ii. Identity
∃ e ∈ M ∀ a ∈ M, e • a = a • e = a
When specifying an endofunctor T : X → X (which is a functor that maps a category to itself) as the set M, the Cartesian product of two sets is just the composition of two endofunctors; what you get from here is a monad, with these two natural transformations:
1. The binary operation is just a functor composition μ : T × T → T
2. The identity element is just an identity endofunctor η : I → T
Satisfied the monoid axioms (i. & ii.), a monad can be seen as a monoid which is an endofunctor together with two natural transformations.
The name “monad” came from “monoid” and “triad”, which indicated that it is a triple (1 functor + 2 trasformations), monoidic algebraic structure.
In other words, monoid is a more general, abstract term. When applying it to the category of endofunctors, we have a monad.
State Monad: Milewski
Don’t fear the Monad
Brian Beckman:
You can understand Monad without too much Category Theory.
Functional Programming = using functions to compose from small functions to very complex software (eg. Nuclear system, driverless car software…).
Advantages of Functional Programming:
 Strong Types Safety: detect bugs at compile time.
 Data Protection thru Immutability: Share data safely in Concurrent / Parallel processing.
 Software ‘Componentisation’ ie Modularity : Each function always returns the same result, ease of software reliability testing.
Each “small” function is a Monoid.
f : a > a (from input of type ‘a‘ , returns type ‘a’)
g: a > a
compose h from f & g : (strong TYPING !!)
h = f。g : a > a
[Note] : Object in Category, usually called Type in Haskell, eg. ‘a’ = Integer)
You already know a Monoid (or Category in general) : eg Clock
 Objects: 1 2 3 …12 (hours)
 Arrow (Morphism): rule “+”:
 Law of Associativity:
x + (y + z) = (x + y) + z  Identity (or “Unit”): (“12”):
x + 12 = 12 + x = x

7 + 10 = 17 mod 12 = 5
More general than Monoid is a “Monoidal” Category where: (instead of only single object ‘a’, now more “a b c…”)
f : a > b
g: b > c
h = f。g : a > c
Function under composition Associative rule and with an Identity => Monoid
Monad (M): a way to manage the sideeffects (I/O, exception , SQL Database, etc) within the Functional Programming way like monoidal categories: ie associative composition, identity.
Remark: For the last 60 years in Software, there have been 2 camps:
 BottomUp Design: from hardware foundation, build performancebased languages: Fortran, C, C++, C#, Java…
 TopDown Design: from Mathematics foundation, build functional languages (LambdaCalculus, Lisp, Algo, Smalltalk, Haskell…).
 F# (Microsoft) is the middleground between 1 & 2.
Ref: What is a Monad ?
Monad = chaining operations with binding “>>=”
 Possible use: allows to write minilanguage, parser…
BM Category Theory 10: Monad & Monoid
10.1 Monad
Analogy :
Function : compose “.“, Id
Monad: compose “>>=“, return
Imperative (with side effects eg. state, I/O, exception ) to Pure function by hiding or embellishment in Pure function but return “embellished” result.
10. 2 Monoid
Monoid in category of endo functors = Monad
Ref Book :
What is the significance of monoids in category theory? by Bartosz Milewski