SlideShare a Scribd company logo
CATEGORY THEORY FOUNDATIONALS
MADE EASY WITH (UGLY) PICTURES
Ashwin Rao
This is a sequel to the 3-hour
course we did previously on
Abstract Algebra
WHY ARE WE DOING THIS?
• Well, some of us are trying to learn Haskell
• And there is a school of thought that says one must learn Category Theory (CT) first
• While I don’t quite agree with this, I do think a very basic intro to CT is essential
• First PreReq is the contents of Hammack’s Book of Proof – Sets, Logic, Functions etc.
• Second PreReq is the crash-course on Abstract Algebra I had done earlier:
https://www.slideshare.net/cover_drive/abstract-algebra-in-3-hours
• We will combine rigorous definitions with plenty of (ugly) pictures and intuition
• After this course, you will understand the “much ridiculed” but important statement: “A monad in C is
just a monoid in the category of endofunctors of C, with product ⨂ as composition of endofunctors and
unit as the identity endofunctor.”
• After this course, you will also be able to make more sense of wiki pages on CT topics
• For a more detailed (and very nice) coverage of CT, Bartosz Milewski e-book hits the spot!
• I’ve also found sigfpe (Dan Piponi)’s blog posts extremely valuable.
Definition of A Category
Category C consists of: Class
†
of objects Obj(C) and Class of arrows Arr(C)
where each arrow f has a source X in Obj(C) and a target Y in Obj(C) denoted f: X → Y
Arr(X,Y) denotes the class of arrows from source X to target Y (some authors use the term
“morphism“ instead of “arrow“).
Binary composition operation ∘	on arrows f and g denoted as g ∘ f
									∘	: Arr(Y,Z) x Arr(X,Y) → Arr(X,Z)
∘ operation has two properties:
1. Associative: h ∘ (g ∘ f) = (h ∘ g) ∘ f
2. Identity: For all X in Obj(C), there exists an arrow 1X in Arr(X,X) such that for all f in Arr(X,Y),
1Y ∘ f = f ∘ 1X = f
The default intuition of a category should not be as nodes and edges.
Think of an object as a set, and of an arrow as a function, with special properties assigned to
the sets (objects) and functions (arrows). Rely on visuals and examples to develop intuition.
† Treat the technical term “Class” as “Set” (typically refering to “set of sets”) for the purpose of this class (no pun intended!). For a precise
understanding of Class versus Set, one has to refer to Zermelo-Frankel-Choice Theory which is beyond the scope of this class.
Examples of Categories
• SET – Objects are Sets, and Arrows are Functions (across the Sets)
• GRP – Objects are Groups, and Arrows are Homomorphisms (across the
Groups)
• VEC– Objects are Vector Spaces, and Arrows are Linear Transformations
(across the Vector Spaces)
• POS – Objects are Elements of a Partially Ordered Set, and Arrows are ≤
(across the Elements)
• HASK – Objects are Haskell Types, and Arrows are Haskell Functions
WTF are Functors and Natural Transformations?
A Functor F is a “mapping” from objects and arrows of a Category C to objects and arrows of a
category D with the following properties:
A. For all X in Obj(C), F(X) is in Obj(D)
B. For all f : X → Y in Arr(C), F(f) : F(X) → F(Y) is in Arr(D) such that:
1. For all X in Obj(C), F(1X) = 1F(X)
2. For all f : X → Y and g : Y → Z in Arr(C), F(g ∘ f) = F(g) ∘ F(f)
So, a Functor is a “structure-preserving“ map from one category to another.
A Natural Transformation 𝜂 from Functor F to Functor G (𝜂 : F → G) associates to every X in Obj(C), an
arrow 𝜂(: F(X) → G(X) in Arr(D) such that for all arrows f : X → Y in Arr(C),
𝜂) ∘ 𝐹 𝑓 = 𝐺(𝑓) ∘ 𝜂(
Don‘t panic – some (helpful) “ugly“ pictures are coming up J
The Functor Category
In the previous picture, collapse each of the two “objects arrays” (one for each of the two
functors F and G) into a single object.
Also collapse the “arrows array” (for the natural transformation 𝜂) into a single arrow.
So you can visualize each collapsed object corresponding to a Functor, and each collapsed
arrow corresponding to a natural transformation.
This “Collapsed Category” is called the Functor Category, where the objects are the
Functors and the arrows are the natural transforms.
The Functor Category will be very useful as we get into advanced topics.
The Hask Category
Hask is the Haskell category where each Haskell Type is an object in Hask and each Haskell
function f : X → Y (for Types X and Y) is an arrow in Hask.
Arr(X, Y) is the class of functions from X to Y. Arr(X,Y) is a Type and hence, an object in Hask.
The identity function for each Type X is in the class Arr(X, X).
Functor T : Hask → Hask generates higher-order Type T X from each Type X
(X-type-parametric polymorphism).
fmap generates structure-preserving functions T f : T X → T Y from f : X → Y
Functor Typeclass overloading of fmap (ad-hoc polymorphism) gives various functor instances
T1,T2, …. and their fmaps.
Natural Transformation 𝜂 between T1,T2, ... take you across these higher-order Types T1X, T2X, ...
Hask examples of Functors and Natural Transformations
[a] and Maybe a are examples of Functors (type-parameterized by a)
Now consider the functions maybeToList and listToMaybe
λ> :t maybeToList
maybeToList :: Maybe a -> [a]
λ> :t listToMaybe
listToMaybe :: [a] -> Maybe a
As you can see, maybeToList and listToMaybe are Natural Transformations.
Connecting this example to some of the pictures we drew earlier is quite helpful, IMO J
Are we ready for the M word yet?
A Monad in a Category C consists of:
• Functor M : C → C
• Natural Transformation 𝜂 : 1C → M (1C is the C → C identity functor)
• Natural Transformation 𝜇 : M ∘ M → M (M ∘ M, abbreviated as M2, also a C → C functor)
Furthermore, we require the following so-called coherence conditions:
• 𝜇	 ∘ 𝑀𝜇 = 	𝜇	 ∘ 	𝜇𝑀 (as natural transformations M3 → M)
• 𝜇	 ∘ 𝑀𝜂 = 	𝜇	 ∘ 	𝜂𝑀 =	13	(as natural transformations M → M)
Practically, Monads let us compose f : X → M(Y) with g : Y → M(Z) into h : X → M(Z)
In Haskell, ≫= ∷ 𝑀	𝑎	 → 𝑎	 → 𝑀	𝑏 → 𝑀	𝑏 enables this monadic composition
𝜂 corresponds to 𝑟𝑒𝑡𝑢𝑟𝑛 ∷ 𝑎	 → 𝑀	𝑎 and 𝜇 corresponds to joi𝑛 ∷ 𝑀	𝑀	𝑎	 → 𝑀	𝑎
Also, the Monad Typeclass laws are simply the coherence conditions expressed in code J
Coherence Condition 1 illustrated with the [a] Monad
λ> [join [[3,2,4], [9,2]], join [[3,4], [1], [9,0,8]], join [[1,3], [7]]] – This is 𝑀𝜇
[[3,2,4,9,2],[3,4,1,9,0,8],[1,3,7]]
λ> join [join [[3,2,4], [9,2]], join [[3,4], [1], [9,0,8]], join [[1,3], [7]]] – This is µ	 ∘ 𝑀𝜇
[3,2,4,9,2,3,4,1,9,0,8]
λ> join [[[3,2,4], [9,2]], [[3,4], [1], [9,0,8]]] – This is 𝜇𝑀
[[3,2,4],[9,2],[3,4],[1],[9,0,8]]
λ> join (join [[[3,2,4], [9,2]], [[3,4], [1], [9,0,8]]]) – This is µ	 ∘ 𝜇𝑀
[3,2,4,9,2,3,4,1,9,0,8]
So, 𝜇	 ∘ 𝑀𝜇 = 𝜇	 ∘ 	𝜇𝑀 is same as the code: join . (fmap join) == join . join
Coherence Condition 2 illustrated with the [a] Monad
λ> let ret = return :: a -> [a]
λ> ret [4,8,1,2] – This is 𝜂𝑀
[[4,8,1,2]]
λ> join (ret [4,8,1,2]) – This is 𝜇	 ∘ 	𝜂𝑀
[4,8,1,2]
λ> [ret 4, ret 8, ret 1, ret 2] – This is 𝑀𝜂
[[4],[8],[1],[2]]
λ> join [ret 4, ret 8, ret 1, ret 2] – This is 𝜇 ∘ 𝑀𝜂
[4,8,1,2]
So, 𝜇	 ∘ 𝜂𝑀 = 	𝜇	 ∘ 𝑀𝜂 = 13 is same as the code: join . return == join . (fmap return) == id
Kleisli Category : A good way to conceptualize Monads
A Monad <𝑀, 𝜂, 𝜇> enables us to compose f : X → M(Y) with g : Y → M(Z) into h : X → M(Z)
To do this, we have to express h : X → M(Z) as a composition of the following 3 arrows
• f : X → M(Y) – This is the basic morphism we start with
• M(g) : M(Y) → M(M(Z)) – We need this “functored“ morphism M(g) to go forward from M(Y)
• 𝜇Z : M(M(Z) → M(Z) – We need this morphism 𝜇Z generated from the natural transformation 𝜇
to reduce the higher-order object M(M(Z)) to the desired object M(Z)
Now consider a Category CT (called the Kleisli Category) derived from the original Category C
• Each object of C is also an object of CT
• Each arrow X → M(Y) of C gives us the arrow X →T Y in CT (known as Kleisli arrows)
• Each composition 𝜇B ∘ 𝑀 𝑔 ∘ 𝑓 in C gives us the composition 𝑔	 ∘D 𝑓 in CT
Kleisli arrows compose naturally in the Kleisli Category (a good way to conceptualize Monads).
Monoidal Category
A Monoidal Category C involves :
• BiFunctor ⨂ ∶ 𝐶	×	𝐶	 → 𝐶 (refered to as the monoidal product)
• Object I (refered to as identity object)
• Coherence conditions expressing ⨂ associativity and left/right identity laws
The idea is that ⨂ combines any two objects to yield an object (akin to Monoids).
Note that ⨂ will also apply (in a natural way) on the arrows across the objects of C.
Example 1: Hask is a Monoidal Category where ⨂ is simply the Cartesian Product of
Types (i.e., objects), which naturally produces a Cartesian Product on Functions (i.e., arrows)
across those Types. Any singleton Type will behave as I.
Example 2: Recall the Functor Category we covered earlier (objects are Functors and
arrows are Natural Transformations). This is a Monoidal Category where ⨂ (on objects) is
the composition of functors and ⨂ (on arrows) is the composition of natural transformations.
Identity Functor will behave as I.
Monoid Object
A Monoid Object M in a Monoidal Category < 𝐶, ⨂, 𝐼 > involves :
• Arrow 𝜂 ∶ 𝐼	 → 𝑀 (akin to monoid unit, i.e., monoid identity element)
• Arrow µ ∶ 𝑀⨂𝑀	 → 𝑀	(akin to monoid multiplication)
such that the following two coherence conditions apply:
• 𝜇 𝑀⨂𝜇 𝑀⨂𝑀 = 	𝜇 𝜇 𝑀⨂𝑀 ⨂𝑀 = 𝑀 (akin to associativity in monoids)
• 𝜇 𝑀⨂𝜂 𝐼 = 	𝜇 𝜂 𝐼 ⨂𝑀 = 𝑀 (akin to left/right identity laws in monoids)
The idea is that if we peer inside the object M, 𝜇 operates like closed monoid multiplication
on elements within M, and 𝜂𝑥 in M operates like monoid identity for any x in I.
Monoid Objects in the Functor Category
Now let us consider monoid objects in the Functor Category (viewed as a Monoidal Category)
If we squint hard at the coherence conditions for the monoid object (specialized to the Functor
Category), we see that they reduce to the coherence conditions we had stated for a Monad.
In other words, “A monad in C is just a monoid in the category of endofunctors of C, with
product ⨂ as composition of endofunctors and unit as the identity endofunctor.”
This provides an alternative mental model of monads – viewing them as monoids.
Let‘s use the [a] Monad in Hask to develop intuition.
λ> join [[2,3,7], [9,1], [8,6,3,9]] – Remember, join is same as 𝜇 (Monad View)
[2,3,7,9,1,8,6,3,9]
λ> mconcat [[2,3,7], [9,1], [8,6,3,9]] – mconcat “mappends” the lists (Monoid View)
[2,3,7,9,1,8,6,3,9]

More Related Content

What's hot

Functions in mathematics
Functions in mathematicsFunctions in mathematics
Functions in mathematics
Burhanuddin Shabbir
 
Category Theory for Programmers
Category Theory for ProgrammersCategory Theory for Programmers
Category Theory for Programmers
Santosh Rajan
 
Functionsandpigeonholeprinciple
FunctionsandpigeonholeprincipleFunctionsandpigeonholeprinciple
Functionsandpigeonholeprinciple
Shiwani Gupta
 
Ecfft zk studyclub 9.9
Ecfft zk studyclub 9.9Ecfft zk studyclub 9.9
Ecfft zk studyclub 9.9
Alex Pruden
 
Topics in Category Theory
Topics in Category TheoryTopics in Category Theory
Topics in Category Theory
Heinrich Hartmann
 
Relations and functions
Relations and functionsRelations and functions
Relations and functions
nitishguptamaps
 
Types of functions 05272011
Types of functions 05272011Types of functions 05272011
Types of functions 05272011Boyet Aluan
 
Relations and functions
Relations and functionsRelations and functions
Relations and functions
Urmila Bhardwaj
 
CBSE Class 12 Mathematics formulas
CBSE Class 12 Mathematics formulasCBSE Class 12 Mathematics formulas
CBSE Class 12 Mathematics formulas
Parth Kshirsagar
 
Functions and its Applications in Mathematics
Functions and its Applications in MathematicsFunctions and its Applications in Mathematics
Functions and its Applications in Mathematics
Amit Amola
 
Equivalence relations
Equivalence relationsEquivalence relations
Equivalence relationsTarun Gehlot
 
Functions
FunctionsFunctions
Functions
Gaditek
 
Functions
FunctionsFunctions
Function in Mathematics
Function in MathematicsFunction in Mathematics
Function in Mathematics
ghhgj jhgh
 
Type Parameterization
Type ParameterizationType Parameterization
Type Parameterization
Knoldus Inc.
 
Effective way to code in Scala
Effective way to code in ScalaEffective way to code in Scala
Effective way to code in Scala
Knoldus Inc.
 
Monad Fact #4
Monad Fact #4Monad Fact #4
Monad Fact #4
Philip Schwarz
 
Submodularity slides
Submodularity slidesSubmodularity slides
Submodularity slidesdragonthu
 
Fp in scala part 1
Fp in scala part 1Fp in scala part 1
Fp in scala part 1
Hang Zhao
 
Object Recognition with Deformable Models
Object Recognition with Deformable ModelsObject Recognition with Deformable Models
Object Recognition with Deformable Modelszukun
 

What's hot (20)

Functions in mathematics
Functions in mathematicsFunctions in mathematics
Functions in mathematics
 
Category Theory for Programmers
Category Theory for ProgrammersCategory Theory for Programmers
Category Theory for Programmers
 
Functionsandpigeonholeprinciple
FunctionsandpigeonholeprincipleFunctionsandpigeonholeprinciple
Functionsandpigeonholeprinciple
 
Ecfft zk studyclub 9.9
Ecfft zk studyclub 9.9Ecfft zk studyclub 9.9
Ecfft zk studyclub 9.9
 
Topics in Category Theory
Topics in Category TheoryTopics in Category Theory
Topics in Category Theory
 
Relations and functions
Relations and functionsRelations and functions
Relations and functions
 
Types of functions 05272011
Types of functions 05272011Types of functions 05272011
Types of functions 05272011
 
Relations and functions
Relations and functionsRelations and functions
Relations and functions
 
CBSE Class 12 Mathematics formulas
CBSE Class 12 Mathematics formulasCBSE Class 12 Mathematics formulas
CBSE Class 12 Mathematics formulas
 
Functions and its Applications in Mathematics
Functions and its Applications in MathematicsFunctions and its Applications in Mathematics
Functions and its Applications in Mathematics
 
Equivalence relations
Equivalence relationsEquivalence relations
Equivalence relations
 
Functions
FunctionsFunctions
Functions
 
Functions
FunctionsFunctions
Functions
 
Function in Mathematics
Function in MathematicsFunction in Mathematics
Function in Mathematics
 
Type Parameterization
Type ParameterizationType Parameterization
Type Parameterization
 
Effective way to code in Scala
Effective way to code in ScalaEffective way to code in Scala
Effective way to code in Scala
 
Monad Fact #4
Monad Fact #4Monad Fact #4
Monad Fact #4
 
Submodularity slides
Submodularity slidesSubmodularity slides
Submodularity slides
 
Fp in scala part 1
Fp in scala part 1Fp in scala part 1
Fp in scala part 1
 
Object Recognition with Deformable Models
Object Recognition with Deformable ModelsObject Recognition with Deformable Models
Object Recognition with Deformable Models
 

Similar to Category Theory made easy with (ugly) pictures

Integration
IntegrationIntegration
Integration
sakhi pathak
 
Integration material
Integration material Integration material
Integration material
Surya Swaroop
 
The Yoneda lemma and String diagrams
The Yoneda lemma and String diagramsThe Yoneda lemma and String diagrams
The Yoneda lemma and String diagrams
Ray Sameshima
 
Functions for Grade 10
Functions for Grade 10Functions for Grade 10
Functions for Grade 10Boipelo Radebe
 
Akshay
AkshayAkshay
[SEMINAR] 2nd Tues, 14 May, 2019
[SEMINAR] 2nd Tues, 14 May, 2019[SEMINAR] 2nd Tues, 14 May, 2019
[SEMINAR] 2nd Tues, 14 May, 2019
Naoto Agawa
 
Yoneda lemma and string diagrams
Yoneda lemma and string diagramsYoneda lemma and string diagrams
Yoneda lemma and string diagrams
Ray Sameshima
 
304-Digital Lecture.pptx
304-Digital Lecture.pptx304-Digital Lecture.pptx
304-Digital Lecture.pptx
ssusera136fd
 
Seismic data processing lecture 3
Seismic data processing lecture 3Seismic data processing lecture 3
Seismic data processing lecture 3
Amin khalil
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheory
Knoldus Inc.
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheoryMeetu Maltiar
 
Ch 3 lessons
Ch  3 lessons Ch  3 lessons
Ch 3 lessons
mikewilmes
 
Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Bryan O'Sullivan
 
Notes on Intersection theory
Notes on Intersection theoryNotes on Intersection theory
Notes on Intersection theory
Heinrich Hartmann
 
Afm chapter 4 powerpoint
Afm chapter 4 powerpointAfm chapter 4 powerpoint
Afm chapter 4 powerpointvolleygurl22
 

Similar to Category Theory made easy with (ugly) pictures (20)

Integration
IntegrationIntegration
Integration
 
Integration material
Integration material Integration material
Integration material
 
Fuzzy Logic_HKR
Fuzzy Logic_HKRFuzzy Logic_HKR
Fuzzy Logic_HKR
 
452Paper
452Paper452Paper
452Paper
 
The Yoneda lemma and String diagrams
The Yoneda lemma and String diagramsThe Yoneda lemma and String diagrams
The Yoneda lemma and String diagrams
 
.
..
.
 
Functions for Grade 10
Functions for Grade 10Functions for Grade 10
Functions for Grade 10
 
Akshay
AkshayAkshay
Akshay
 
[SEMINAR] 2nd Tues, 14 May, 2019
[SEMINAR] 2nd Tues, 14 May, 2019[SEMINAR] 2nd Tues, 14 May, 2019
[SEMINAR] 2nd Tues, 14 May, 2019
 
Yoneda lemma and string diagrams
Yoneda lemma and string diagramsYoneda lemma and string diagrams
Yoneda lemma and string diagrams
 
Report
ReportReport
Report
 
304-Digital Lecture.pptx
304-Digital Lecture.pptx304-Digital Lecture.pptx
304-Digital Lecture.pptx
 
Seismic data processing lecture 3
Seismic data processing lecture 3Seismic data processing lecture 3
Seismic data processing lecture 3
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheory
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheory
 
Ch 3 lessons
Ch  3 lessons Ch  3 lessons
Ch 3 lessons
 
Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Real World Haskell: Lecture 6
Real World Haskell: Lecture 6
 
project
projectproject
project
 
Notes on Intersection theory
Notes on Intersection theoryNotes on Intersection theory
Notes on Intersection theory
 
Afm chapter 4 powerpoint
Afm chapter 4 powerpointAfm chapter 4 powerpoint
Afm chapter 4 powerpoint
 

More from Ashwin Rao

Stochastic Control/Reinforcement Learning for Optimal Market Making
Stochastic Control/Reinforcement Learning for Optimal Market MakingStochastic Control/Reinforcement Learning for Optimal Market Making
Stochastic Control/Reinforcement Learning for Optimal Market Making
Ashwin Rao
 
Adaptive Multistage Sampling Algorithm: The Origins of Monte Carlo Tree Search
Adaptive Multistage Sampling Algorithm: The Origins of Monte Carlo Tree SearchAdaptive Multistage Sampling Algorithm: The Origins of Monte Carlo Tree Search
Adaptive Multistage Sampling Algorithm: The Origins of Monte Carlo Tree Search
Ashwin Rao
 
Fundamental Theorems of Asset Pricing
Fundamental Theorems of Asset PricingFundamental Theorems of Asset Pricing
Fundamental Theorems of Asset Pricing
Ashwin Rao
 
Evolutionary Strategies as an alternative to Reinforcement Learning
Evolutionary Strategies as an alternative to Reinforcement LearningEvolutionary Strategies as an alternative to Reinforcement Learning
Evolutionary Strategies as an alternative to Reinforcement Learning
Ashwin Rao
 
Principles of Mathematical Economics applied to a Physical-Stores Retail Busi...
Principles of Mathematical Economics applied to a Physical-Stores Retail Busi...Principles of Mathematical Economics applied to a Physical-Stores Retail Busi...
Principles of Mathematical Economics applied to a Physical-Stores Retail Busi...
Ashwin Rao
 
Understanding Dynamic Programming through Bellman Operators
Understanding Dynamic Programming through Bellman OperatorsUnderstanding Dynamic Programming through Bellman Operators
Understanding Dynamic Programming through Bellman Operators
Ashwin Rao
 
Stochastic Control of Optimal Trade Order Execution
Stochastic Control of Optimal Trade Order ExecutionStochastic Control of Optimal Trade Order Execution
Stochastic Control of Optimal Trade Order Execution
Ashwin Rao
 
A.I. for Dynamic Decisioning under Uncertainty (for real-world problems in Re...
A.I. for Dynamic Decisioning under Uncertainty (for real-world problems in Re...A.I. for Dynamic Decisioning under Uncertainty (for real-world problems in Re...
A.I. for Dynamic Decisioning under Uncertainty (for real-world problems in Re...
Ashwin Rao
 
Overview of Stochastic Calculus Foundations
Overview of Stochastic Calculus FoundationsOverview of Stochastic Calculus Foundations
Overview of Stochastic Calculus Foundations
Ashwin Rao
 
Risk-Aversion, Risk-Premium and Utility Theory
Risk-Aversion, Risk-Premium and Utility TheoryRisk-Aversion, Risk-Premium and Utility Theory
Risk-Aversion, Risk-Premium and Utility Theory
Ashwin Rao
 
Value Function Geometry and Gradient TD
Value Function Geometry and Gradient TDValue Function Geometry and Gradient TD
Value Function Geometry and Gradient TD
Ashwin Rao
 
Stanford CME 241 - Reinforcement Learning for Stochastic Control Problems in ...
Stanford CME 241 - Reinforcement Learning for Stochastic Control Problems in ...Stanford CME 241 - Reinforcement Learning for Stochastic Control Problems in ...
Stanford CME 241 - Reinforcement Learning for Stochastic Control Problems in ...
Ashwin Rao
 
HJB Equation and Merton's Portfolio Problem
HJB Equation and Merton's Portfolio ProblemHJB Equation and Merton's Portfolio Problem
HJB Equation and Merton's Portfolio Problem
Ashwin Rao
 
Policy Gradient Theorem
Policy Gradient TheoremPolicy Gradient Theorem
Policy Gradient Theorem
Ashwin Rao
 
A Quick and Terse Introduction to Efficient Frontier Mathematics
A Quick and Terse Introduction to Efficient Frontier MathematicsA Quick and Terse Introduction to Efficient Frontier Mathematics
A Quick and Terse Introduction to Efficient Frontier Mathematics
Ashwin Rao
 
Towards Improved Pricing and Hedging of Agency Mortgage-backed Securities
Towards Improved Pricing and Hedging of Agency Mortgage-backed SecuritiesTowards Improved Pricing and Hedging of Agency Mortgage-backed Securities
Towards Improved Pricing and Hedging of Agency Mortgage-backed Securities
Ashwin Rao
 
Recursive Formulation of Gradient in a Dense Feed-Forward Deep Neural Network
Recursive Formulation of Gradient in a Dense Feed-Forward Deep Neural NetworkRecursive Formulation of Gradient in a Dense Feed-Forward Deep Neural Network
Recursive Formulation of Gradient in a Dense Feed-Forward Deep Neural Network
Ashwin Rao
 
Demystifying the Bias-Variance Tradeoff
Demystifying the Bias-Variance TradeoffDemystifying the Bias-Variance Tradeoff
Demystifying the Bias-Variance Tradeoff
Ashwin Rao
 
Risk Pooling sensitivity to Correlation
Risk Pooling sensitivity to CorrelationRisk Pooling sensitivity to Correlation
Risk Pooling sensitivity to Correlation
Ashwin Rao
 
OmniChannelNewsvendor
OmniChannelNewsvendorOmniChannelNewsvendor
OmniChannelNewsvendorAshwin Rao
 

More from Ashwin Rao (20)

Stochastic Control/Reinforcement Learning for Optimal Market Making
Stochastic Control/Reinforcement Learning for Optimal Market MakingStochastic Control/Reinforcement Learning for Optimal Market Making
Stochastic Control/Reinforcement Learning for Optimal Market Making
 
Adaptive Multistage Sampling Algorithm: The Origins of Monte Carlo Tree Search
Adaptive Multistage Sampling Algorithm: The Origins of Monte Carlo Tree SearchAdaptive Multistage Sampling Algorithm: The Origins of Monte Carlo Tree Search
Adaptive Multistage Sampling Algorithm: The Origins of Monte Carlo Tree Search
 
Fundamental Theorems of Asset Pricing
Fundamental Theorems of Asset PricingFundamental Theorems of Asset Pricing
Fundamental Theorems of Asset Pricing
 
Evolutionary Strategies as an alternative to Reinforcement Learning
Evolutionary Strategies as an alternative to Reinforcement LearningEvolutionary Strategies as an alternative to Reinforcement Learning
Evolutionary Strategies as an alternative to Reinforcement Learning
 
Principles of Mathematical Economics applied to a Physical-Stores Retail Busi...
Principles of Mathematical Economics applied to a Physical-Stores Retail Busi...Principles of Mathematical Economics applied to a Physical-Stores Retail Busi...
Principles of Mathematical Economics applied to a Physical-Stores Retail Busi...
 
Understanding Dynamic Programming through Bellman Operators
Understanding Dynamic Programming through Bellman OperatorsUnderstanding Dynamic Programming through Bellman Operators
Understanding Dynamic Programming through Bellman Operators
 
Stochastic Control of Optimal Trade Order Execution
Stochastic Control of Optimal Trade Order ExecutionStochastic Control of Optimal Trade Order Execution
Stochastic Control of Optimal Trade Order Execution
 
A.I. for Dynamic Decisioning under Uncertainty (for real-world problems in Re...
A.I. for Dynamic Decisioning under Uncertainty (for real-world problems in Re...A.I. for Dynamic Decisioning under Uncertainty (for real-world problems in Re...
A.I. for Dynamic Decisioning under Uncertainty (for real-world problems in Re...
 
Overview of Stochastic Calculus Foundations
Overview of Stochastic Calculus FoundationsOverview of Stochastic Calculus Foundations
Overview of Stochastic Calculus Foundations
 
Risk-Aversion, Risk-Premium and Utility Theory
Risk-Aversion, Risk-Premium and Utility TheoryRisk-Aversion, Risk-Premium and Utility Theory
Risk-Aversion, Risk-Premium and Utility Theory
 
Value Function Geometry and Gradient TD
Value Function Geometry and Gradient TDValue Function Geometry and Gradient TD
Value Function Geometry and Gradient TD
 
Stanford CME 241 - Reinforcement Learning for Stochastic Control Problems in ...
Stanford CME 241 - Reinforcement Learning for Stochastic Control Problems in ...Stanford CME 241 - Reinforcement Learning for Stochastic Control Problems in ...
Stanford CME 241 - Reinforcement Learning for Stochastic Control Problems in ...
 
HJB Equation and Merton's Portfolio Problem
HJB Equation and Merton's Portfolio ProblemHJB Equation and Merton's Portfolio Problem
HJB Equation and Merton's Portfolio Problem
 
Policy Gradient Theorem
Policy Gradient TheoremPolicy Gradient Theorem
Policy Gradient Theorem
 
A Quick and Terse Introduction to Efficient Frontier Mathematics
A Quick and Terse Introduction to Efficient Frontier MathematicsA Quick and Terse Introduction to Efficient Frontier Mathematics
A Quick and Terse Introduction to Efficient Frontier Mathematics
 
Towards Improved Pricing and Hedging of Agency Mortgage-backed Securities
Towards Improved Pricing and Hedging of Agency Mortgage-backed SecuritiesTowards Improved Pricing and Hedging of Agency Mortgage-backed Securities
Towards Improved Pricing and Hedging of Agency Mortgage-backed Securities
 
Recursive Formulation of Gradient in a Dense Feed-Forward Deep Neural Network
Recursive Formulation of Gradient in a Dense Feed-Forward Deep Neural NetworkRecursive Formulation of Gradient in a Dense Feed-Forward Deep Neural Network
Recursive Formulation of Gradient in a Dense Feed-Forward Deep Neural Network
 
Demystifying the Bias-Variance Tradeoff
Demystifying the Bias-Variance TradeoffDemystifying the Bias-Variance Tradeoff
Demystifying the Bias-Variance Tradeoff
 
Risk Pooling sensitivity to Correlation
Risk Pooling sensitivity to CorrelationRisk Pooling sensitivity to Correlation
Risk Pooling sensitivity to Correlation
 
OmniChannelNewsvendor
OmniChannelNewsvendorOmniChannelNewsvendor
OmniChannelNewsvendor
 

Recently uploaded

Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 

Recently uploaded (20)

Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 

Category Theory made easy with (ugly) pictures

  • 1. CATEGORY THEORY FOUNDATIONALS MADE EASY WITH (UGLY) PICTURES Ashwin Rao This is a sequel to the 3-hour course we did previously on Abstract Algebra
  • 2. WHY ARE WE DOING THIS? • Well, some of us are trying to learn Haskell • And there is a school of thought that says one must learn Category Theory (CT) first • While I don’t quite agree with this, I do think a very basic intro to CT is essential • First PreReq is the contents of Hammack’s Book of Proof – Sets, Logic, Functions etc. • Second PreReq is the crash-course on Abstract Algebra I had done earlier: https://www.slideshare.net/cover_drive/abstract-algebra-in-3-hours • We will combine rigorous definitions with plenty of (ugly) pictures and intuition • After this course, you will understand the “much ridiculed” but important statement: “A monad in C is just a monoid in the category of endofunctors of C, with product ⨂ as composition of endofunctors and unit as the identity endofunctor.” • After this course, you will also be able to make more sense of wiki pages on CT topics • For a more detailed (and very nice) coverage of CT, Bartosz Milewski e-book hits the spot! • I’ve also found sigfpe (Dan Piponi)’s blog posts extremely valuable.
  • 3. Definition of A Category Category C consists of: Class † of objects Obj(C) and Class of arrows Arr(C) where each arrow f has a source X in Obj(C) and a target Y in Obj(C) denoted f: X → Y Arr(X,Y) denotes the class of arrows from source X to target Y (some authors use the term “morphism“ instead of “arrow“). Binary composition operation ∘ on arrows f and g denoted as g ∘ f ∘ : Arr(Y,Z) x Arr(X,Y) → Arr(X,Z) ∘ operation has two properties: 1. Associative: h ∘ (g ∘ f) = (h ∘ g) ∘ f 2. Identity: For all X in Obj(C), there exists an arrow 1X in Arr(X,X) such that for all f in Arr(X,Y), 1Y ∘ f = f ∘ 1X = f The default intuition of a category should not be as nodes and edges. Think of an object as a set, and of an arrow as a function, with special properties assigned to the sets (objects) and functions (arrows). Rely on visuals and examples to develop intuition. † Treat the technical term “Class” as “Set” (typically refering to “set of sets”) for the purpose of this class (no pun intended!). For a precise understanding of Class versus Set, one has to refer to Zermelo-Frankel-Choice Theory which is beyond the scope of this class.
  • 4.
  • 5.
  • 6.
  • 7. Examples of Categories • SET – Objects are Sets, and Arrows are Functions (across the Sets) • GRP – Objects are Groups, and Arrows are Homomorphisms (across the Groups) • VEC– Objects are Vector Spaces, and Arrows are Linear Transformations (across the Vector Spaces) • POS – Objects are Elements of a Partially Ordered Set, and Arrows are ≤ (across the Elements) • HASK – Objects are Haskell Types, and Arrows are Haskell Functions
  • 8. WTF are Functors and Natural Transformations? A Functor F is a “mapping” from objects and arrows of a Category C to objects and arrows of a category D with the following properties: A. For all X in Obj(C), F(X) is in Obj(D) B. For all f : X → Y in Arr(C), F(f) : F(X) → F(Y) is in Arr(D) such that: 1. For all X in Obj(C), F(1X) = 1F(X) 2. For all f : X → Y and g : Y → Z in Arr(C), F(g ∘ f) = F(g) ∘ F(f) So, a Functor is a “structure-preserving“ map from one category to another. A Natural Transformation 𝜂 from Functor F to Functor G (𝜂 : F → G) associates to every X in Obj(C), an arrow 𝜂(: F(X) → G(X) in Arr(D) such that for all arrows f : X → Y in Arr(C), 𝜂) ∘ 𝐹 𝑓 = 𝐺(𝑓) ∘ 𝜂( Don‘t panic – some (helpful) “ugly“ pictures are coming up J
  • 9.
  • 10.
  • 11.
  • 12. The Functor Category In the previous picture, collapse each of the two “objects arrays” (one for each of the two functors F and G) into a single object. Also collapse the “arrows array” (for the natural transformation 𝜂) into a single arrow. So you can visualize each collapsed object corresponding to a Functor, and each collapsed arrow corresponding to a natural transformation. This “Collapsed Category” is called the Functor Category, where the objects are the Functors and the arrows are the natural transforms. The Functor Category will be very useful as we get into advanced topics.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17. The Hask Category Hask is the Haskell category where each Haskell Type is an object in Hask and each Haskell function f : X → Y (for Types X and Y) is an arrow in Hask. Arr(X, Y) is the class of functions from X to Y. Arr(X,Y) is a Type and hence, an object in Hask. The identity function for each Type X is in the class Arr(X, X). Functor T : Hask → Hask generates higher-order Type T X from each Type X (X-type-parametric polymorphism). fmap generates structure-preserving functions T f : T X → T Y from f : X → Y Functor Typeclass overloading of fmap (ad-hoc polymorphism) gives various functor instances T1,T2, …. and their fmaps. Natural Transformation 𝜂 between T1,T2, ... take you across these higher-order Types T1X, T2X, ...
  • 18.
  • 19. Hask examples of Functors and Natural Transformations [a] and Maybe a are examples of Functors (type-parameterized by a) Now consider the functions maybeToList and listToMaybe λ> :t maybeToList maybeToList :: Maybe a -> [a] λ> :t listToMaybe listToMaybe :: [a] -> Maybe a As you can see, maybeToList and listToMaybe are Natural Transformations. Connecting this example to some of the pictures we drew earlier is quite helpful, IMO J
  • 20. Are we ready for the M word yet? A Monad in a Category C consists of: • Functor M : C → C • Natural Transformation 𝜂 : 1C → M (1C is the C → C identity functor) • Natural Transformation 𝜇 : M ∘ M → M (M ∘ M, abbreviated as M2, also a C → C functor) Furthermore, we require the following so-called coherence conditions: • 𝜇 ∘ 𝑀𝜇 = 𝜇 ∘ 𝜇𝑀 (as natural transformations M3 → M) • 𝜇 ∘ 𝑀𝜂 = 𝜇 ∘ 𝜂𝑀 = 13 (as natural transformations M → M) Practically, Monads let us compose f : X → M(Y) with g : Y → M(Z) into h : X → M(Z) In Haskell, ≫= ∷ 𝑀 𝑎 → 𝑎 → 𝑀 𝑏 → 𝑀 𝑏 enables this monadic composition 𝜂 corresponds to 𝑟𝑒𝑡𝑢𝑟𝑛 ∷ 𝑎 → 𝑀 𝑎 and 𝜇 corresponds to joi𝑛 ∷ 𝑀 𝑀 𝑎 → 𝑀 𝑎 Also, the Monad Typeclass laws are simply the coherence conditions expressed in code J
  • 21.
  • 22. Coherence Condition 1 illustrated with the [a] Monad λ> [join [[3,2,4], [9,2]], join [[3,4], [1], [9,0,8]], join [[1,3], [7]]] – This is 𝑀𝜇 [[3,2,4,9,2],[3,4,1,9,0,8],[1,3,7]] λ> join [join [[3,2,4], [9,2]], join [[3,4], [1], [9,0,8]], join [[1,3], [7]]] – This is µ ∘ 𝑀𝜇 [3,2,4,9,2,3,4,1,9,0,8] λ> join [[[3,2,4], [9,2]], [[3,4], [1], [9,0,8]]] – This is 𝜇𝑀 [[3,2,4],[9,2],[3,4],[1],[9,0,8]] λ> join (join [[[3,2,4], [9,2]], [[3,4], [1], [9,0,8]]]) – This is µ ∘ 𝜇𝑀 [3,2,4,9,2,3,4,1,9,0,8] So, 𝜇 ∘ 𝑀𝜇 = 𝜇 ∘ 𝜇𝑀 is same as the code: join . (fmap join) == join . join
  • 23. Coherence Condition 2 illustrated with the [a] Monad λ> let ret = return :: a -> [a] λ> ret [4,8,1,2] – This is 𝜂𝑀 [[4,8,1,2]] λ> join (ret [4,8,1,2]) – This is 𝜇 ∘ 𝜂𝑀 [4,8,1,2] λ> [ret 4, ret 8, ret 1, ret 2] – This is 𝑀𝜂 [[4],[8],[1],[2]] λ> join [ret 4, ret 8, ret 1, ret 2] – This is 𝜇 ∘ 𝑀𝜂 [4,8,1,2] So, 𝜇 ∘ 𝜂𝑀 = 𝜇 ∘ 𝑀𝜂 = 13 is same as the code: join . return == join . (fmap return) == id
  • 24.
  • 25. Kleisli Category : A good way to conceptualize Monads A Monad <𝑀, 𝜂, 𝜇> enables us to compose f : X → M(Y) with g : Y → M(Z) into h : X → M(Z) To do this, we have to express h : X → M(Z) as a composition of the following 3 arrows • f : X → M(Y) – This is the basic morphism we start with • M(g) : M(Y) → M(M(Z)) – We need this “functored“ morphism M(g) to go forward from M(Y) • 𝜇Z : M(M(Z) → M(Z) – We need this morphism 𝜇Z generated from the natural transformation 𝜇 to reduce the higher-order object M(M(Z)) to the desired object M(Z) Now consider a Category CT (called the Kleisli Category) derived from the original Category C • Each object of C is also an object of CT • Each arrow X → M(Y) of C gives us the arrow X →T Y in CT (known as Kleisli arrows) • Each composition 𝜇B ∘ 𝑀 𝑔 ∘ 𝑓 in C gives us the composition 𝑔 ∘D 𝑓 in CT Kleisli arrows compose naturally in the Kleisli Category (a good way to conceptualize Monads).
  • 26. Monoidal Category A Monoidal Category C involves : • BiFunctor ⨂ ∶ 𝐶 × 𝐶 → 𝐶 (refered to as the monoidal product) • Object I (refered to as identity object) • Coherence conditions expressing ⨂ associativity and left/right identity laws The idea is that ⨂ combines any two objects to yield an object (akin to Monoids). Note that ⨂ will also apply (in a natural way) on the arrows across the objects of C. Example 1: Hask is a Monoidal Category where ⨂ is simply the Cartesian Product of Types (i.e., objects), which naturally produces a Cartesian Product on Functions (i.e., arrows) across those Types. Any singleton Type will behave as I. Example 2: Recall the Functor Category we covered earlier (objects are Functors and arrows are Natural Transformations). This is a Monoidal Category where ⨂ (on objects) is the composition of functors and ⨂ (on arrows) is the composition of natural transformations. Identity Functor will behave as I.
  • 27. Monoid Object A Monoid Object M in a Monoidal Category < 𝐶, ⨂, 𝐼 > involves : • Arrow 𝜂 ∶ 𝐼 → 𝑀 (akin to monoid unit, i.e., monoid identity element) • Arrow µ ∶ 𝑀⨂𝑀 → 𝑀 (akin to monoid multiplication) such that the following two coherence conditions apply: • 𝜇 𝑀⨂𝜇 𝑀⨂𝑀 = 𝜇 𝜇 𝑀⨂𝑀 ⨂𝑀 = 𝑀 (akin to associativity in monoids) • 𝜇 𝑀⨂𝜂 𝐼 = 𝜇 𝜂 𝐼 ⨂𝑀 = 𝑀 (akin to left/right identity laws in monoids) The idea is that if we peer inside the object M, 𝜇 operates like closed monoid multiplication on elements within M, and 𝜂𝑥 in M operates like monoid identity for any x in I.
  • 28. Monoid Objects in the Functor Category Now let us consider monoid objects in the Functor Category (viewed as a Monoidal Category) If we squint hard at the coherence conditions for the monoid object (specialized to the Functor Category), we see that they reduce to the coherence conditions we had stated for a Monad. In other words, “A monad in C is just a monoid in the category of endofunctors of C, with product ⨂ as composition of endofunctors and unit as the identity endofunctor.” This provides an alternative mental model of monads – viewing them as monoids. Let‘s use the [a] Monad in Hask to develop intuition. λ> join [[2,3,7], [9,1], [8,6,3,9]] – Remember, join is same as 𝜇 (Monad View) [2,3,7,9,1,8,6,3,9] λ> mconcat [[2,3,7], [9,1], [8,6,3,9]] – mconcat “mappends” the lists (Monoid View) [2,3,7,9,1,8,6,3,9]