Monads ~ “Design Pattern” in Functional Programming

Examples: “Option”, “Some”,…

# Monads 单子(Dr. Eugenia Cheng)

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

Ref:

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

Monad (图解)  : 单子
Functor: 函子

# Program = Category

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 Typed” languages for AI, BIG DATA…

$\boxed {\text {Type = Category }}$

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

$\boxed {\text {Monad = Lawvere Theory }}$

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

Ref
:

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

# Monoid and Monad

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: monad is just a monoid in the category of endofunctors.

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.