SlideShare a Scribd company logo
Category Theory
Theory and Applications for Functional
Programming
Format of Talk
1. Introduce definition from Category Theory
2. Use something from Scala as an example
3. Show it satisfies the definition.
4. Do ^^ until Monad defined
5. Prove that definition given in FP is equivalent to
definition given in Category Theory
Plus random points about application and practicalities
along the way
Note about foundations
● Category Theory is itself a foundation of mathematics,
and strictly speaking you don’t need Set Theory to do
Category Theory
● Nevertheless a lot of examples and language used to
explain Category Theory is actually borrowed from Set
Theory
● I will do the same as it makes things much easier
(strictly speaking I’m using the von Neumann–Bernays–Gödel (NBG) set theory Axiomatization, which
is a conservative extension of ZFC that allows us to talk about Proper Classes (e.g. Class of all Sets)
Definition - Category
1. a class ob(C) of objects
2. a class hom(C) of morphisms, or arrows, or maps, between the objects. Each morphism f has a unique
source object a and target object b where a and b are in ob(C). We write f: a → b, and we say "f is a morphism
from a to b".
3. for every three objects a, b and c, a binary operation hom(a, b) × hom(b, c) → hom(a, c) called composition
of morphisms; the composition of f : a → b and g : b → c is written as g ∘ f or gf. (Some authors use
"diagrammatic order", writing f;g or fg.)
such that the following axioms hold:
a. (associativity) if f : a → b, g : b → c and h : c → d then h ∘ (g ∘ f) = (h ∘ g) ∘ f, and
b. (identity) for every object x, there exists a morphism 1x
: x → x (some authors write idx
) called the identity morphism for x, such
that for every morphism f : a → b, we have 1b
∘ f = f = f ∘ 1a
.
Example - Scala S
Let the set of Types in Scala be Ob(C)
Let the set of 1 param Functions in Scala be hom(C)
NOTATION: Methods of a Type A, that return a type B that take no params can
be equivalently considered as 1 param functions f: A -> B. Therefore I will
interchange method invocation and function application henceforth.
Composition o is defined as simply normal function composition, now
For any f, g, h (types obv) we have
(h o (g o f))(x) = (g o f)(h(x)) = f(g(h(x))) = f((h o g)(x)) = ((h o g) o f)(x) -
associativity
For any T, id_T : T -> T is defined by for any x is a T, id_T(x) = x, clearly this is
an identity
Definition - Functor
Example - Parameterized Types
Many parameterized types in Scala can be viewed as Functors with their map operation;
Let S be the Scala Category, and F: S -> S
associate any T in Ob(S) to List[T] in Ob(S)
associate any f: A -> B (for any A, B in Ob(S)) to map(f): List[A] -> List[B]. Now for any T in Ob(S)
F(id_T)(someList) = someList.map(x => x) = (x => x)(someList) = id_List[T] = id_F[T]
- so satisfies identity preservation
And it’s obvious that someList.map(f).map(g) = someList.map(g o f), so satisfies composition
preservation
Practical Point
When you write a parameterized type in an API in Scala with a map function, you are telling the API
user that map(f).map(g) is the same as map(f o g). So in Scalding, it’s often convenient to chain map
operations together for readability, rather than compose the functions - but Scalding is clever, it will
compose the functions for you so that your still O(N) not O(2N), O(3N) etc.
Definition - Natural Transformation
.If F and G are functors between the categories C and D, then a natural transformation η from F to G associates to every object X in C a
morphism ηX
: F(X) → G(X) between objects of D, called the component of η at X, such that for every morphism f : X → Y in C we have:
Example - Flatten
Let F: S -> S and G: S -> S be the Option[Option[ _ ]] Functor and Option[ _ ] Functor respectively.
NOTATION: will be sloppy henceforth
Let f: X -> Y in Hom(S)
So F(f): Option[Option[ X ]] -> Option[Option[ Y ] is .map(_.map(f))
G(f): Option[X] -> Option[Y] is .map(f)
Example - Flatten - Continued
Let N_x be .flatten[x], then flatten is a Natural Transformation:
N_Y = flatten[Y]: Option[Option[Y]] -> Option[Y]
N_X = flatten[X]: Option[Option[X]] -> Option[X]
So N_Y o F(f) = .map(_.map(f)).flatten
and G(f) o N_X = .flatten.map(f). Now
Some(Some(x)).map(_.map(f)).flatten = Some(Some(x).map(f)).flatten = Some(Some(f(x)).flatten
= Some(f(x)) = Some(x).map(f) = Some(Some(x)).flatten.map(f)
Note there are many more natural transformations, like if we defined toList on Option.
Practical Point
Knowing an operation is a natural transformation makes refactoring easier.
Definition - Monad!!
Example - Option Monad
Let F be the Option Functor, then combined with the flatten natural transformation M we have a
monad: For any X in Ob(S)
F(M_X) = map(_.flatten) : Option[Option[Option[X]]] -> Option[Option[X]]
M_F(X) = M_Option[X] = flatten[Option[X]]: Option[Option[Option[X]]] -> Option[Option[X]]
so
M_X o F(M_X) = .map(_.flatten).flatten : Option[Option[Option[X]]] -> Option[X]
M_X o M_F(X) = .flatten.flatten : Option[Option[Option[X]]] -> Option[X]
Let’s check these are equal
Some(Some(Some(x))).map(_.flatten).flatten = Some(Some(Some(x)).flatten).flatten
= Some(Some(x)).flatten = Some(x) = Some(Some(x)).flatten =
Some(Some(Some(x))).flatten.flatten
Therefore we have the first coherence condition ...
Example - Option Monad continued
Now our Identity natural transformation will be the Some function, i.e.
N_X = Some: X -> Option[X] (which is the same as Id(X) -> Option[X])
so
F(N_X) = .map(Some), so
M_X o F(N_X) = .map(Some).flatten, which is clearly the identity Functor (other way round - exercise)
Definition - Monad in FP
In functional programming a monadic Type M is simply defined in terms of
flatMap, where:
For any f: X -> M[Y], g: Y -> M[Z], and any x: M[X]
x.flatMap(f).flatMap(g) = x.flatMap(f(_).flatMap(g))
and there exists a neutral element N: X -> M[X], where
x.flatMap(N) = x
Theorem - Equivalence
The two previous definitions are equivalent when we make the following substitution
.map(f).flatten for flatMap(f) - (*)
Proof:
.flatten.flatten = .map(_.flatten).flatten - Monad Category Theory
=> .map(f(_).map(g)).flatten.flatten = .map(f(_).map(g)).map(_.flatten).flatten
- by substituting in .map(f(_).map(g))
Now RHS = .map(f(_).map(g).flatten).flatten - by Functor Composition Preservation
= .flatMap(f(_).map(g).flatten) - by (*)
= .flatMap(f(_).flatMap(g)) - by (*)
...
Proof continued
Now LHS = x.map(f(_)).map(_.map(g)).flatten.flatten - by Functor Composition Preservation
= x.map(f).flatten.map(g).flatten - since flatten is a natural transformation (recall earlier slide)
= x.flatMap(f).flatMap(g) - by (*) twice.
Therefore
.flatMap(f(_).flatMap(g)) = .flatMap(f).flatMap(g)
It remains to show the identity conditions (exercise)
Further Reading
Monoids - Used in Reduce operations in Map Reduce to parallelize operations
that cumulate a single value. E.g. + is a monoid.
Covariance and Contravariance - Used in Typing rules for type inference
Summary of Applications
1. Using Category Theoretic notions in code is a little like a formalization of
design patterns
2. When a reader sees a particular notion, they need to use less cognitive
resources to comprehend the code by familiarity
3. It’s easier to refactor code due to known equivalences, some of these
equivalences are even used by Intellij (and ReSharper for LINQ) for the auto-
refactor shortcuts
4. Sometimes APIs allow the user to write readable code, but the resulting
compiled code will be in it’s most computationally efficient representation.
5. Compilers use concepts in Category Theory
6. State hiding FP design

More Related Content

What's hot

Monoids - Part 2 - with examples using Scalaz and Cats
Monoids - Part 2 - with examples using Scalaz and CatsMonoids - Part 2 - with examples using Scalaz and Cats
Monoids - Part 2 - with examples using Scalaz and Cats
Philip Schwarz
 
NUMPY
NUMPY NUMPY
Simple IO Monad in 'Functional Programming in Scala'
Simple IO Monad in 'Functional Programming in Scala'Simple IO Monad in 'Functional Programming in Scala'
Simple IO Monad in 'Functional Programming in Scala'
Philip Schwarz
 
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Philip Schwarz
 
Modular Module Systems
Modular Module SystemsModular Module Systems
Modular Module Systemsleague
 
Sequence and Traverse - Part 3
Sequence and Traverse - Part 3Sequence and Traverse - Part 3
Sequence and Traverse - Part 3
Philip Schwarz
 
Sequence and Traverse - Part 1
Sequence and Traverse - Part 1Sequence and Traverse - Part 1
Sequence and Traverse - Part 1
Philip Schwarz
 
Array
ArrayArray
Functor Composition
Functor CompositionFunctor Composition
Functor Composition
Philip Schwarz
 
Monads do not Compose
Monads do not ComposeMonads do not Compose
Monads do not Compose
Philip Schwarz
 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)
PyData
 
Introduction to Monads in Scala (1)
Introduction to Monads in Scala (1)Introduction to Monads in Scala (1)
Introduction to Monads in Scala (1)stasimus
 
Seminar PSU 10.10.2014 mme
Seminar PSU 10.10.2014 mmeSeminar PSU 10.10.2014 mme
Seminar PSU 10.10.2014 mme
Vyacheslav Arbuzov
 
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov VyacheslavSeminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov VyacheslavVyacheslav Arbuzov
 
Templates2
Templates2Templates2
Templates2
zindadili
 
Lec3
Lec3Lec3
Introduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning ProgrammersIntroduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning Programmers
Kimikazu Kato
 
Python for R Users
Python for R UsersPython for R Users
Python for R Users
Ajay Ohri
 
Contravariant functors in scala
Contravariant functors in scalaContravariant functors in scala
Contravariant functors in scala
Piotr Paradziński
 

What's hot (20)

Monoids - Part 2 - with examples using Scalaz and Cats
Monoids - Part 2 - with examples using Scalaz and CatsMonoids - Part 2 - with examples using Scalaz and Cats
Monoids - Part 2 - with examples using Scalaz and Cats
 
NUMPY
NUMPY NUMPY
NUMPY
 
Simple IO Monad in 'Functional Programming in Scala'
Simple IO Monad in 'Functional Programming in Scala'Simple IO Monad in 'Functional Programming in Scala'
Simple IO Monad in 'Functional Programming in Scala'
 
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
 
Modular Module Systems
Modular Module SystemsModular Module Systems
Modular Module Systems
 
Sequence and Traverse - Part 3
Sequence and Traverse - Part 3Sequence and Traverse - Part 3
Sequence and Traverse - Part 3
 
Sequence and Traverse - Part 1
Sequence and Traverse - Part 1Sequence and Traverse - Part 1
Sequence and Traverse - Part 1
 
Array
ArrayArray
Array
 
Functor Composition
Functor CompositionFunctor Composition
Functor Composition
 
Monads do not Compose
Monads do not ComposeMonads do not Compose
Monads do not Compose
 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)
 
Introduction to Monads in Scala (1)
Introduction to Monads in Scala (1)Introduction to Monads in Scala (1)
Introduction to Monads in Scala (1)
 
Seminar PSU 10.10.2014 mme
Seminar PSU 10.10.2014 mmeSeminar PSU 10.10.2014 mme
Seminar PSU 10.10.2014 mme
 
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov VyacheslavSeminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
 
Templates2
Templates2Templates2
Templates2
 
Lec3
Lec3Lec3
Lec3
 
Perm winter school 2014.01.31
Perm winter school 2014.01.31Perm winter school 2014.01.31
Perm winter school 2014.01.31
 
Introduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning ProgrammersIntroduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning Programmers
 
Python for R Users
Python for R UsersPython for R Users
Python for R Users
 
Contravariant functors in scala
Contravariant functors in scalaContravariant functors in scala
Contravariant functors in scala
 

Viewers also liked

Présentation Kivy (et projets associés) à Pycon-fr 2013
Présentation Kivy (et projets associés) à Pycon-fr 2013Présentation Kivy (et projets associés) à Pycon-fr 2013
Présentation Kivy (et projets associés) à Pycon-fr 2013
Gabriel Pettier
 
Why functional why scala
Why functional  why scala Why functional  why scala
Why functional why scala
Neville Li
 
Scala the language matters
Scala the language mattersScala the language matters
Scala the language matters
Xiaojun REN
 
Thinking functional-in-scala
Thinking functional-in-scalaThinking functional-in-scala
Thinking functional-in-scala
Knoldus Inc.
 
Introduction to Option monad in Scala
Introduction to Option monad in ScalaIntroduction to Option monad in Scala
Introduction to Option monad in Scala
Jan Krag
 
Developers Summit 2015 - Scala Monad
Developers Summit 2015 - Scala MonadDevelopers Summit 2015 - Scala Monad
Developers Summit 2015 - Scala Monad
Sangwon Han
 
Advanced Functional Programming in Scala
Advanced Functional Programming in ScalaAdvanced Functional Programming in Scala
Advanced Functional Programming in Scala
Patrick Nicolas
 
Scala: functional programming for the imperative mind
Scala: functional programming for the imperative mindScala: functional programming for the imperative mind
Scala: functional programming for the imperative mind
Sander Mak (@Sander_Mak)
 
Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming Fundamentals
Shahriar Hyder
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scalapramode_ce
 

Viewers also liked (10)

Présentation Kivy (et projets associés) à Pycon-fr 2013
Présentation Kivy (et projets associés) à Pycon-fr 2013Présentation Kivy (et projets associés) à Pycon-fr 2013
Présentation Kivy (et projets associés) à Pycon-fr 2013
 
Why functional why scala
Why functional  why scala Why functional  why scala
Why functional why scala
 
Scala the language matters
Scala the language mattersScala the language matters
Scala the language matters
 
Thinking functional-in-scala
Thinking functional-in-scalaThinking functional-in-scala
Thinking functional-in-scala
 
Introduction to Option monad in Scala
Introduction to Option monad in ScalaIntroduction to Option monad in Scala
Introduction to Option monad in Scala
 
Developers Summit 2015 - Scala Monad
Developers Summit 2015 - Scala MonadDevelopers Summit 2015 - Scala Monad
Developers Summit 2015 - Scala Monad
 
Advanced Functional Programming in Scala
Advanced Functional Programming in ScalaAdvanced Functional Programming in Scala
Advanced Functional Programming in Scala
 
Scala: functional programming for the imperative mind
Scala: functional programming for the imperative mindScala: functional programming for the imperative mind
Scala: functional programming for the imperative mind
 
Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming Fundamentals
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
 

Similar to Monad presentation scala as a category

Frp2016 3
Frp2016 3Frp2016 3
Frp2016 3
Kirill Kozlov
 
13 05-curl-and-divergence
13 05-curl-and-divergence13 05-curl-and-divergence
13 05-curl-and-divergence
Abdelrahman Elshaer
 
(2015 06-16) Three Approaches to Monads
(2015 06-16) Three Approaches to Monads(2015 06-16) Three Approaches to Monads
(2015 06-16) Three Approaches to Monads
Lawrence Evans
 
Scala collection methods flatMap and flatten are more powerful than monadic f...
Scala collection methods flatMap and flatten are more powerful than monadic f...Scala collection methods flatMap and flatten are more powerful than monadic f...
Scala collection methods flatMap and flatten are more powerful than monadic f...
Philip Schwarz
 
Domain-and-Range-of-a-Function
Domain-and-Range-of-a-FunctionDomain-and-Range-of-a-Function
Domain-and-Range-of-a-Function
EmeraldAcaba
 
Formal methods 8 - category theory (last one)
Formal methods   8 - category theory (last one)Formal methods   8 - category theory (last one)
Formal methods 8 - category theory (last one)
Vlad Patryshev
 
Differential Equations Assignment Help
Differential Equations Assignment HelpDifferential Equations Assignment Help
Differential Equations Assignment Help
Math Homework Solver
 
Basic Calculus.docx
Basic Calculus.docxBasic Calculus.docx
Basic Calculus.docx
jericranoco
 
Differential Equations Assignment Help
Differential Equations Assignment HelpDifferential Equations Assignment Help
Differential Equations Assignment Help
Maths Assignment Help
 
Derivative rules.docx
Derivative rules.docxDerivative rules.docx
Derivative rules.docx
Rose Mary Tania Arini
 
Goldie chapter 4 function
Goldie chapter 4 functionGoldie chapter 4 function
Goldie chapter 4 function
Sarah Sue Calbio
 
A Commutative Alternative to Fractional Calculus on k-Differentiable Functions
A Commutative Alternative to Fractional Calculus on k-Differentiable FunctionsA Commutative Alternative to Fractional Calculus on k-Differentiable Functions
A Commutative Alternative to Fractional Calculus on k-Differentiable FunctionsMatt Parker
 
math camp
math campmath camp
math camp
ssuser8cde591
 
Integration
IntegrationIntegration
Integration
sakhi pathak
 
Integration material
Integration material Integration material
Integration material
Surya Swaroop
 
And or graph problem reduction using predicate logic
And or graph problem reduction using predicate logicAnd or graph problem reduction using predicate logic
And or graph problem reduction using predicate logic
Mohanlal Sukhadia University (MLSU)
 
Calculus- Basics
Calculus- BasicsCalculus- Basics
Calculus- Basics
Rabin BK
 
vvvvvvvvvvvvvL2A_CurveRepresentations.pdf
vvvvvvvvvvvvvL2A_CurveRepresentations.pdfvvvvvvvvvvvvvL2A_CurveRepresentations.pdf
vvvvvvvvvvvvvL2A_CurveRepresentations.pdf
Khalil Alhatab
 
Dokumen.tips mathematics ii-institute-of-aeronautical-engineering-pptpdfadvan...
Dokumen.tips mathematics ii-institute-of-aeronautical-engineering-pptpdfadvan...Dokumen.tips mathematics ii-institute-of-aeronautical-engineering-pptpdfadvan...
Dokumen.tips mathematics ii-institute-of-aeronautical-engineering-pptpdfadvan...
Mahmood Adel
 

Similar to Monad presentation scala as a category (20)

Frp2016 3
Frp2016 3Frp2016 3
Frp2016 3
 
13 05-curl-and-divergence
13 05-curl-and-divergence13 05-curl-and-divergence
13 05-curl-and-divergence
 
(2015 06-16) Three Approaches to Monads
(2015 06-16) Three Approaches to Monads(2015 06-16) Three Approaches to Monads
(2015 06-16) Three Approaches to Monads
 
Scala collection methods flatMap and flatten are more powerful than monadic f...
Scala collection methods flatMap and flatten are more powerful than monadic f...Scala collection methods flatMap and flatten are more powerful than monadic f...
Scala collection methods flatMap and flatten are more powerful than monadic f...
 
Domain-and-Range-of-a-Function
Domain-and-Range-of-a-FunctionDomain-and-Range-of-a-Function
Domain-and-Range-of-a-Function
 
The integral
The integralThe integral
The integral
 
Formal methods 8 - category theory (last one)
Formal methods   8 - category theory (last one)Formal methods   8 - category theory (last one)
Formal methods 8 - category theory (last one)
 
Differential Equations Assignment Help
Differential Equations Assignment HelpDifferential Equations Assignment Help
Differential Equations Assignment Help
 
Basic Calculus.docx
Basic Calculus.docxBasic Calculus.docx
Basic Calculus.docx
 
Differential Equations Assignment Help
Differential Equations Assignment HelpDifferential Equations Assignment Help
Differential Equations Assignment Help
 
Derivative rules.docx
Derivative rules.docxDerivative rules.docx
Derivative rules.docx
 
Goldie chapter 4 function
Goldie chapter 4 functionGoldie chapter 4 function
Goldie chapter 4 function
 
A Commutative Alternative to Fractional Calculus on k-Differentiable Functions
A Commutative Alternative to Fractional Calculus on k-Differentiable FunctionsA Commutative Alternative to Fractional Calculus on k-Differentiable Functions
A Commutative Alternative to Fractional Calculus on k-Differentiable Functions
 
math camp
math campmath camp
math camp
 
Integration
IntegrationIntegration
Integration
 
Integration material
Integration material Integration material
Integration material
 
And or graph problem reduction using predicate logic
And or graph problem reduction using predicate logicAnd or graph problem reduction using predicate logic
And or graph problem reduction using predicate logic
 
Calculus- Basics
Calculus- BasicsCalculus- Basics
Calculus- Basics
 
vvvvvvvvvvvvvL2A_CurveRepresentations.pdf
vvvvvvvvvvvvvL2A_CurveRepresentations.pdfvvvvvvvvvvvvvL2A_CurveRepresentations.pdf
vvvvvvvvvvvvvL2A_CurveRepresentations.pdf
 
Dokumen.tips mathematics ii-institute-of-aeronautical-engineering-pptpdfadvan...
Dokumen.tips mathematics ii-institute-of-aeronautical-engineering-pptpdfadvan...Dokumen.tips mathematics ii-institute-of-aeronautical-engineering-pptpdfadvan...
Dokumen.tips mathematics ii-institute-of-aeronautical-engineering-pptpdfadvan...
 

Recently uploaded

Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
PrashantGoswami42
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
MuhammadTufail242431
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 

Recently uploaded (20)

Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 

Monad presentation scala as a category

  • 1. Category Theory Theory and Applications for Functional Programming
  • 2. Format of Talk 1. Introduce definition from Category Theory 2. Use something from Scala as an example 3. Show it satisfies the definition. 4. Do ^^ until Monad defined 5. Prove that definition given in FP is equivalent to definition given in Category Theory Plus random points about application and practicalities along the way
  • 3. Note about foundations ● Category Theory is itself a foundation of mathematics, and strictly speaking you don’t need Set Theory to do Category Theory ● Nevertheless a lot of examples and language used to explain Category Theory is actually borrowed from Set Theory ● I will do the same as it makes things much easier (strictly speaking I’m using the von Neumann–Bernays–Gödel (NBG) set theory Axiomatization, which is a conservative extension of ZFC that allows us to talk about Proper Classes (e.g. Class of all Sets)
  • 4. Definition - Category 1. a class ob(C) of objects 2. a class hom(C) of morphisms, or arrows, or maps, between the objects. Each morphism f has a unique source object a and target object b where a and b are in ob(C). We write f: a → b, and we say "f is a morphism from a to b". 3. for every three objects a, b and c, a binary operation hom(a, b) × hom(b, c) → hom(a, c) called composition of morphisms; the composition of f : a → b and g : b → c is written as g ∘ f or gf. (Some authors use "diagrammatic order", writing f;g or fg.) such that the following axioms hold: a. (associativity) if f : a → b, g : b → c and h : c → d then h ∘ (g ∘ f) = (h ∘ g) ∘ f, and b. (identity) for every object x, there exists a morphism 1x : x → x (some authors write idx ) called the identity morphism for x, such that for every morphism f : a → b, we have 1b ∘ f = f = f ∘ 1a .
  • 5. Example - Scala S Let the set of Types in Scala be Ob(C) Let the set of 1 param Functions in Scala be hom(C) NOTATION: Methods of a Type A, that return a type B that take no params can be equivalently considered as 1 param functions f: A -> B. Therefore I will interchange method invocation and function application henceforth. Composition o is defined as simply normal function composition, now For any f, g, h (types obv) we have (h o (g o f))(x) = (g o f)(h(x)) = f(g(h(x))) = f((h o g)(x)) = ((h o g) o f)(x) - associativity For any T, id_T : T -> T is defined by for any x is a T, id_T(x) = x, clearly this is an identity
  • 7. Example - Parameterized Types Many parameterized types in Scala can be viewed as Functors with their map operation; Let S be the Scala Category, and F: S -> S associate any T in Ob(S) to List[T] in Ob(S) associate any f: A -> B (for any A, B in Ob(S)) to map(f): List[A] -> List[B]. Now for any T in Ob(S) F(id_T)(someList) = someList.map(x => x) = (x => x)(someList) = id_List[T] = id_F[T] - so satisfies identity preservation And it’s obvious that someList.map(f).map(g) = someList.map(g o f), so satisfies composition preservation Practical Point When you write a parameterized type in an API in Scala with a map function, you are telling the API user that map(f).map(g) is the same as map(f o g). So in Scalding, it’s often convenient to chain map operations together for readability, rather than compose the functions - but Scalding is clever, it will compose the functions for you so that your still O(N) not O(2N), O(3N) etc.
  • 8. Definition - Natural Transformation .If F and G are functors between the categories C and D, then a natural transformation η from F to G associates to every object X in C a morphism ηX : F(X) → G(X) between objects of D, called the component of η at X, such that for every morphism f : X → Y in C we have:
  • 9. Example - Flatten Let F: S -> S and G: S -> S be the Option[Option[ _ ]] Functor and Option[ _ ] Functor respectively. NOTATION: will be sloppy henceforth Let f: X -> Y in Hom(S) So F(f): Option[Option[ X ]] -> Option[Option[ Y ] is .map(_.map(f)) G(f): Option[X] -> Option[Y] is .map(f)
  • 10. Example - Flatten - Continued Let N_x be .flatten[x], then flatten is a Natural Transformation: N_Y = flatten[Y]: Option[Option[Y]] -> Option[Y] N_X = flatten[X]: Option[Option[X]] -> Option[X] So N_Y o F(f) = .map(_.map(f)).flatten and G(f) o N_X = .flatten.map(f). Now Some(Some(x)).map(_.map(f)).flatten = Some(Some(x).map(f)).flatten = Some(Some(f(x)).flatten = Some(f(x)) = Some(x).map(f) = Some(Some(x)).flatten.map(f) Note there are many more natural transformations, like if we defined toList on Option. Practical Point Knowing an operation is a natural transformation makes refactoring easier.
  • 12. Example - Option Monad Let F be the Option Functor, then combined with the flatten natural transformation M we have a monad: For any X in Ob(S) F(M_X) = map(_.flatten) : Option[Option[Option[X]]] -> Option[Option[X]] M_F(X) = M_Option[X] = flatten[Option[X]]: Option[Option[Option[X]]] -> Option[Option[X]] so M_X o F(M_X) = .map(_.flatten).flatten : Option[Option[Option[X]]] -> Option[X] M_X o M_F(X) = .flatten.flatten : Option[Option[Option[X]]] -> Option[X] Let’s check these are equal Some(Some(Some(x))).map(_.flatten).flatten = Some(Some(Some(x)).flatten).flatten = Some(Some(x)).flatten = Some(x) = Some(Some(x)).flatten = Some(Some(Some(x))).flatten.flatten Therefore we have the first coherence condition ...
  • 13. Example - Option Monad continued Now our Identity natural transformation will be the Some function, i.e. N_X = Some: X -> Option[X] (which is the same as Id(X) -> Option[X]) so F(N_X) = .map(Some), so M_X o F(N_X) = .map(Some).flatten, which is clearly the identity Functor (other way round - exercise)
  • 14. Definition - Monad in FP In functional programming a monadic Type M is simply defined in terms of flatMap, where: For any f: X -> M[Y], g: Y -> M[Z], and any x: M[X] x.flatMap(f).flatMap(g) = x.flatMap(f(_).flatMap(g)) and there exists a neutral element N: X -> M[X], where x.flatMap(N) = x
  • 15. Theorem - Equivalence The two previous definitions are equivalent when we make the following substitution .map(f).flatten for flatMap(f) - (*) Proof: .flatten.flatten = .map(_.flatten).flatten - Monad Category Theory => .map(f(_).map(g)).flatten.flatten = .map(f(_).map(g)).map(_.flatten).flatten - by substituting in .map(f(_).map(g)) Now RHS = .map(f(_).map(g).flatten).flatten - by Functor Composition Preservation = .flatMap(f(_).map(g).flatten) - by (*) = .flatMap(f(_).flatMap(g)) - by (*) ...
  • 16. Proof continued Now LHS = x.map(f(_)).map(_.map(g)).flatten.flatten - by Functor Composition Preservation = x.map(f).flatten.map(g).flatten - since flatten is a natural transformation (recall earlier slide) = x.flatMap(f).flatMap(g) - by (*) twice. Therefore .flatMap(f(_).flatMap(g)) = .flatMap(f).flatMap(g) It remains to show the identity conditions (exercise)
  • 17. Further Reading Monoids - Used in Reduce operations in Map Reduce to parallelize operations that cumulate a single value. E.g. + is a monoid. Covariance and Contravariance - Used in Typing rules for type inference
  • 18. Summary of Applications 1. Using Category Theoretic notions in code is a little like a formalization of design patterns 2. When a reader sees a particular notion, they need to use less cognitive resources to comprehend the code by familiarity 3. It’s easier to refactor code due to known equivalences, some of these equivalences are even used by Intellij (and ReSharper for LINQ) for the auto- refactor shortcuts 4. Sometimes APIs allow the user to write readable code, but the resulting compiled code will be in it’s most computationally efficient representation. 5. Compilers use concepts in Category Theory 6. State hiding FP design