https://en.m.wikipedia.org/wiki/Monad_(functional_programming)

# Tag Archives: monad

# Monoid in the Category of Endofunctors = Monad

# Functional C++ for Fun and Profit

# 函数概念并不难，理解“函”字是关键——函数概念如何理解】

【函数概念并不难，理解“函”字是关键——函数概念如何理解】

https://m.toutiaocdn.com/group/6714162037842248205/?app=news_article_lite×tamp=1563301386&req_id=201907170223060101520450386849493&group_id=6714162037842248205&tt_from=android_share&utm_medium=toutiao_android&utm_campaign=client_share

清. 李善兰 翻译 Function 为函数。函，信也。只能有一个收信人，所以 只有一个 f(x) 值。

…

The unique 1 single output of a function becomes very important for subsequent development in Math & IT:

functions are composable, associative, identify function，etc (distributive,… ) => it can be treated like vector => structure of a Vector Space “Vect”

Extended to..

“Vect” is a bigger structure “Category” in which “function of functions” is a

“Functor” （函子）F：F(f)

Example : F(f) = fmap (in Haskell)

fmap (+1) {2,7,6,3}

=> {3,8,7,4}

here F = fmap, f = +1

The Math branch in the study of functions is called “functional” 泛函。

IT : Functional Programming in Lisp, Haskell, Scala, ensure safety of guaranteed output by math function property. Any unexpected exception (side effects: IO, errors) is handled by a special function called “Monad” (endo-Functor).

(想看更多合你口味的内容，马上下载 今日头条)

http://app.toutiao.com/news_article/?utm_source=link

# A Functional Programmer’s Guide to Homotopy Type Theory (HoTT)

Since April 2019 until I re-visit this Youtube video on 12 August 2019, I can now totally understand his speech after a pause of 4 months by viewing other related Youtube (below prerequisite) videos on Category Theory, Type Theory, Homotopy Type Theory.

That is the technique of self-study:

- First go through the whole video,
- Don’t understand? view other related simpler videos.
- Repeat 1.

**Prerequisite** knowledge:

- Homotopy
- Type Theory
- Homotopy Type Theory
- Bijection = Isomorphism
- Functional Programming in Category Theory Concept: Monad & Applicative

**Two Key Takeaway Points:**

- In the Homotopy “Space” :
**Programs are points**in the space,**Paths are Types.** **“Univalence Axiom”**: Paths Induce Bijection, vice versa.

…

…

…

# Knowing Monads Through The Category Theory

https://dev.to/juaneto/knowing-monads-through-the-category-theory-1mea

While Mathematicians like to talk non-sensical abstract idea, Informaticians want to know how to apply the idea concretely:

**Mathematical Parlance**:

**Monad = ****Monoid**** +Endofunctor**

**Monoid = Identity + Associative**

**Endo-functor = functor between 2 ****same**** categories **

**IT Parlance:**

**Monad** is a ‘function’ to **wrap the ‘side effects’** (exception errors, I/O,… ) so that function composition in ‘**pipeline**‘ chained operation sequence is still possible in **pure** **FP** (Functional Programming, which forbids side-effects).

Some common Monads: ‘Maybe’, ‘List’, ‘Reader’…

This allows monads to simplify a wide range of problems, like handling potential undefined values (with the `Maybe`

monad), or keeping values within a flexible, well-formed list (using the `List`

monad). With a monad, a programmer can turn a complicated sequence of functions into a succinct pipeline that abstracts away additional data management, control flow, or side-effects.^{[2][3]}

__Exploring Monads in Scala Collections__

# ScalaFP: the Reasons Behind Monads

Why Monad:

1. Functional Programming:

“pure” function = no side effects

2. All applications without side effects are useless!

3. Monad wraps side-effect as if it is “pure” for FP composition.

https://dzone.com/articles/scalafp-lets-find-the-reasons-behind-monads

# Quora: Functional Programming / Side Effect / Monads

# “Arrow “: Functional Kotlin

`Λrrow`

is a functional programming library for the Kotlin programming language born from the fusion of KΛTEGORY and funKTionale.

Kategory and Funktionale were the two most relevant FP libraries for Kotlin in 2017.

# Kotlin Monad

1**. Monad Stack**

2. **Monad Transformer**

3. **Reader Monad**

**Ref**:

1. **Functional Kotlin**

http://functionalkotlin.com

2. **SICP: Sequence as conventional interfaces: eg. flatmap, map, reduce, fold**

https://dzone.com/articles/conventional-interfaces-in-functional-programming

# A Fistful of Monads

__Kotlin Monad (and Functor, Applicative)__

1. Functor “**map” (Kotlin) **(fmap or <$> in Haskell)

https://hackernoon.com/kotlin-functors-applicatives-and-monads-in-pictures-part-3-3-832d58d92445

2. **Monads** “**flatmap**” (>>= in Haskell)

** Haskell Monad**:

http://learnyouahaskell.com/a-fistful-of-monads

Do not fear Monoid / Monoidal Category / Monad:

__Monad in Haskell__

**F# Monad:**

（分享自知乎网）

# Monads – FP’s answer to Immutability

**Introduction**:

- The curse of Immutability in Functional Programming – no “Looping” (recursion ok), no Date, no Random, …no I/O …
- Monad is the Savior of “No Side Effect: IO Monads

**Promise of Monads (A)**

**Promise of Monads (B)**

# Scala Monads: Declutter Your Code With Monadic Design

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

Examples: “Option”, “Some”,…

# What is a Monoid / Monad – in Scala Functional Programming

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

**Monad = Monoid + Endo-Functors**

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

# 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 = **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: 函子**

# 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…

**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 ?

# What’s a Monad? Digging into Haskell

# Haskell Lectures 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:

# 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: 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 “+”:

7 + 10 = 17 **mod 12** = 5

- Law of
**Associativity**:

x + (y + z) = (x + y) + z **Identity (or “Unit”): (“12”)**:

x +**12**=**12**+ x = x

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 side-effects (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:

- Bottom-Up Design: from hardware foundation, build performance-based languages: Fortran, C, C++, C#, Java…
- Top-Down Design: from Mathematics foundation, build functional languages (Lambda-Calculus, Lisp, Algo, Smalltalk, Haskell…).
- F# (Microsoft) is the middle-ground between 1 & 2.

Ref: What is a Monad ?

**Monad** = chaining operations with binding “>>=”

**Possible use**: allows to write mini-language, parser…

《知乎》https://zhuanlan.zhihu.com/p/29542641?from=timeline

# 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 **