SlideShare une entreprise Scribd logo
1  sur  28
Télécharger pour lire hors ligne
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]

Contenu connexe

Tendances

Category Theory for Programmers
Category Theory for ProgrammersCategory Theory for Programmers
Category Theory for ProgrammersSantosh Rajan
 
Functionsandpigeonholeprinciple
FunctionsandpigeonholeprincipleFunctionsandpigeonholeprinciple
FunctionsandpigeonholeprincipleShiwani Gupta
 
Ecfft zk studyclub 9.9
Ecfft zk studyclub 9.9Ecfft zk studyclub 9.9
Ecfft zk studyclub 9.9Alex Pruden
 
Types of functions 05272011
Types of functions 05272011Types of functions 05272011
Types of functions 05272011Boyet Aluan
 
CBSE Class 12 Mathematics formulas
CBSE Class 12 Mathematics formulasCBSE Class 12 Mathematics formulas
CBSE Class 12 Mathematics formulasParth Kshirsagar
 
Functions and its Applications in Mathematics
Functions and its Applications in MathematicsFunctions and its Applications in Mathematics
Functions and its Applications in MathematicsAmit Amola
 
Equivalence relations
Equivalence relationsEquivalence relations
Equivalence relationsTarun Gehlot
 
Functions
FunctionsFunctions
FunctionsGaditek
 
Function in Mathematics
Function in MathematicsFunction in Mathematics
Function in Mathematicsghhgj jhgh
 
Type Parameterization
Type ParameterizationType Parameterization
Type ParameterizationKnoldus Inc.
 
Effective way to code in Scala
Effective way to code in ScalaEffective way to code in Scala
Effective way to code in ScalaKnoldus Inc.
 
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 1Hang Zhao
 
Object Recognition with Deformable Models
Object Recognition with Deformable ModelsObject Recognition with Deformable Models
Object Recognition with Deformable Modelszukun
 

Tendances (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
 

Similaire à Category Theory made easy with (ugly) pictures

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 diagramsRay Sameshima
 
Functions for Grade 10
Functions for Grade 10Functions for Grade 10
Functions for Grade 10Boipelo Radebe
 
[SEMINAR] 2nd Tues, 14 May, 2019
[SEMINAR] 2nd Tues, 14 May, 2019[SEMINAR] 2nd Tues, 14 May, 2019
[SEMINAR] 2nd Tues, 14 May, 2019Naoto Agawa
 
Yoneda lemma and string diagrams
Yoneda lemma and string diagramsYoneda lemma and string diagrams
Yoneda lemma and string diagramsRay Sameshima
 
304-Digital Lecture.pptx
304-Digital Lecture.pptx304-Digital Lecture.pptx
304-Digital Lecture.pptxssusera136fd
 
Seismic data processing lecture 3
Seismic data processing lecture 3Seismic data processing lecture 3
Seismic data processing lecture 3Amin khalil
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheoryKnoldus Inc.
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheoryMeetu Maltiar
 
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 theoryHeinrich Hartmann
 
Afm chapter 4 powerpoint
Afm chapter 4 powerpointAfm chapter 4 powerpoint
Afm chapter 4 powerpointvolleygurl22
 

Similaire à Category Theory made easy with (ugly) pictures (20)

Integration material
Integration material Integration material
Integration material
 
Integration
IntegrationIntegration
Integration
 
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
 

Plus de 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 MakingAshwin 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 SearchAshwin Rao
 
Fundamental Theorems of Asset Pricing
Fundamental Theorems of Asset PricingFundamental Theorems of Asset Pricing
Fundamental Theorems of Asset PricingAshwin 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 LearningAshwin 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 OperatorsAshwin 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 ExecutionAshwin 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 FoundationsAshwin 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 TheoryAshwin Rao
 
Value Function Geometry and Gradient TD
Value Function Geometry and Gradient TDValue Function Geometry and Gradient TD
Value Function Geometry and Gradient TDAshwin 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 ProblemAshwin Rao
 
Policy Gradient Theorem
Policy Gradient TheoremPolicy Gradient Theorem
Policy Gradient TheoremAshwin 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 MathematicsAshwin 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 SecuritiesAshwin 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 NetworkAshwin Rao
 
Demystifying the Bias-Variance Tradeoff
Demystifying the Bias-Variance TradeoffDemystifying the Bias-Variance Tradeoff
Demystifying the Bias-Variance TradeoffAshwin Rao
 
Risk Pooling sensitivity to Correlation
Risk Pooling sensitivity to CorrelationRisk Pooling sensitivity to Correlation
Risk Pooling sensitivity to CorrelationAshwin Rao
 
OmniChannelNewsvendor
OmniChannelNewsvendorOmniChannelNewsvendor
OmniChannelNewsvendorAshwin Rao
 

Plus de 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
 

Dernier

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024SynarionITSolutions
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024The Digital Insurer
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 

Dernier (20)

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

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]