SlideShare uma empresa Scribd logo
1 de 28
Baixar para ler offline
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]

Mais conteúdo relacionado

Mais procurados

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
 

Mais procurados (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
 

Semelhante a 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
 

Semelhante a 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
 

Mais 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
 

Mais 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
 

Último

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
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 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 

Último (20)

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
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 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 

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]