**Monads **~ “Design Pattern” in Functional Programming

Examples: “Option”, “Some”,…

**Monads **~ “Design Pattern” in Functional Programming

Examples: “Option”, “Some”,…

**Monoid** = an **Algebraic** structure (“Algebra” ) with: Associativity & Identity

**Monad = Monoid + Endo-Functors**

https://livebook.manning.com/#!/book/functional-programming-in-scala/chapter-10/37

**Dr. Eugenia Cheng ‘s Lectures on Category Theory (2007)**

1. **Definition of Monad, Example: Monad for Monoids**

The name “monad” came from “monoid” and “triad”, which indicated that it is a triple (1 functor + 2 trasformations), monoidic algebraic structure.

Monad = **Mon**oid + Tri**ad**

Monad = Monoid (restricted to endofunctors)

**Note**: She was annoyed nobody had corrected her mistake in (red) Tμ . (I discovered it only on 2nd revision view few years later).

2. **Example2: **Monad for Categories

Monad for Small Categories (= Set)

https://tomcircle.wordpress.com/2017/04/28/monoid-and-monad/

What is the difference between Monoid and Monad? (Bartosz Milewski )

Monad (图解) ** : 单子**

**Functor: 函子**

2017 : (PhD Math made simple for IT programmers)

**Keywords**:

**Category****Monad**=**Monoid**+**Endofunctor****Lewvere Theory**

**Category Theory** is replacing **Set Theory** as the foundation of Math. Nowadays, few Advanced Math papers are written without using Category to explain, and this trend is spreading to IT through Functional Programming languages (** Google’s Kotlin, Haskell, Clojure**…) – the latest paradigm to replace Object-Oriented languages like Java, C++, etc, as a safer “Strong

**Examples of “Types” in IT:**

- Integers
- Real
- Double
- Boolean
- String
- etc

**T-program** defined in the following 6 examples:

- list (X), eg
*. {2, 5 , 3, 7, 1 }* - partial (X), eg:
*+1 (error msg)* - side-effect, eg:
*update a record* - continuation (X),
- non-det (X),
- prob-dist (X)

A **Monad** : a T-program which turns an arrow to a “Category” (ie + 2 properties: Identity & Associative).

**Proof: List Computation is a Category**

**Proof: Partial operation is a Category**

Monad is for only one Category. Lawvere Theory is more general.

**
Ref**:

**Slides**: http://www.math.jhu.edu/~eriehl/compose.pdf

Ref:

Excellent Intuitive **Stackoverflow** Answer: What is a Monad ?

https://www.quora.com/Why-do-we-need-Monad-to-encapsulate-side-effects-in-Haskell/answer/Bartosz-Milewski?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:

Quora: What is the difference between monoid and monad? by Mort Yao https://www.quora.com/What-is-the-difference-between-monoid-and-monad/answer/Mort-Yao?share=41115848&srid=ZyHj

A well-said, 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