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

  1. Objects: 1 2 3 …12 (hours)
  2. Arrow (Morphism): rule “+”: 
    • 7 + 10 = 17 mod 12 = 5
  3. Law of Associativity:
    x + (y + z) = (x + y) + z
  4. 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: 

  1. Bottom-Up Design: from hardware foundation,  build performance-based languages: Fortran, C, C++, C#, Java…
  2. Top-Down Design: from Mathematics foundation, build functional languages (Lambda-Calculus, Lisp, Algo, Smalltalk, Haskell…). 
  3. 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…

Category Theory II 2: Limits, Higher order functors

1.2 Introduction to Limit 

Analogy : Product to Cones (Limit)

2.1 Five categories used to define Limit:

  1. Index category (I)
  2. Category C: Functors (constant ,  D)
  3. Cones (Lim D)
  4. Functor Category [I, C ]:objects are (consant,D ), morphsms are natural transormations
  5. Set category of Hom-Set Cones [I,C] to Hom-Set C (c , Lim D )

2.2 Naturality

3.1 Examples: Equalizer

CoLimit = duality of Limit (inverted cone = co-Cone)

Functor Continuity = preserve Limit

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 

Category Theory 9: Natural Transformations, BiCategories

In essence, in all kinds of Math, we do 3 things: 

1) Find Pattern among objects (numbers, shapes, …), 
2) Operate inside the objects (+ – × / …), 
3) Swap the object without modifying it (rotate, flip, move around, exchange…).

Category  consists of :
1) Find pattern thru Universal Construction in Objects (Set, Group, Ring, Vector Space, anything )
2) Functor which operates on 1).
3) Natural Transformation as in 3).

\boxed {\text {Natural Transformation}}
\Updownarrow

\boxed {\text {Morphism of Functors}}

Analogy:

Functors (F, G) := operation inside a container 
\boxed { F :: X \to F_{X}, \:  F :: Y \to F_{Y}}

\boxed {G :: X \to G_{X}, \: G :: Y \to G_{Y}}

Natural Transformation  ({\eta_{X}, \eta_{Y}}) := swap the content ( F_{X} \text { with } G_{X},  F_{Y} \text { with } G_{Y} ) in the container without modifying it.
\boxed{\eta_{X} :: F_{X} \to G_{X} , \: \eta_{Y} :: F_{Y} \to G_{Y}}

9.2 Bicategories 

“Diagram Chasing”:

2- Category

Cat = Category of categories (C, D)

The functors {F, G} instead of being a Set (“Hom-Set”) – like functions  form a function object “Exponentialfunctors also form a category, noted : \boxed {[C,D] = D^{C} }

BiCategory (different from 2-Category): the Associativity and Identity are not equal (as in 2-Category),  but only up to Isomorphism.
Note : when n is infinity,  n-Category & Groupoid (HOTT: Homotopy Type Theory)

Reading Book: chap 10