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

Contenu connexe

Tendances

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 CatsPhilip Schwarz
 
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 3Philip Schwarz
 
Sequence and Traverse - Part 1
Sequence and Traverse - Part 1Sequence and Traverse - Part 1
Sequence and Traverse - Part 1Philip 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 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
 
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 ProgrammersKimikazu Kato
 
Python for R Users
Python for R UsersPython for R Users
Python for R UsersAjay Ohri
 
Contravariant functors in scala
Contravariant functors in scalaContravariant functors in scala
Contravariant functors in scalaPiotr Paradziński
 

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

En vedette

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 2013Gabriel 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 mattersXiaojun REN
 
Thinking functional-in-scala
Thinking functional-in-scalaThinking functional-in-scala
Thinking functional-in-scalaKnoldus Inc.
 
Introduction to Option monad in Scala
Introduction to Option monad in ScalaIntroduction to Option monad in Scala
Introduction to Option monad in ScalaJan Krag
 
Developers Summit 2015 - Scala Monad
Developers Summit 2015 - Scala MonadDevelopers Summit 2015 - Scala Monad
Developers Summit 2015 - Scala MonadSangwon Han
 
Advanced Functional Programming in Scala
Advanced Functional Programming in ScalaAdvanced Functional Programming in Scala
Advanced Functional Programming in ScalaPatrick 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 mindSander Mak (@Sander_Mak)
 
Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming FundamentalsShahriar 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
 

En vedette (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
 

Similaire à Monad presentation scala as a category

(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 MonadsLawrence 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-FunctionEmeraldAcaba
 
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 HelpMath Homework Solver
 
Basic Calculus.docx
Basic Calculus.docxBasic Calculus.docx
Basic Calculus.docxjericranoco
 
Differential Equations Assignment Help
Differential Equations Assignment HelpDifferential Equations Assignment Help
Differential Equations Assignment HelpMaths Assignment Help
 
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
 
Integration material
Integration material Integration material
Integration material Surya Swaroop
 
Calculus- Basics
Calculus- BasicsCalculus- Basics
Calculus- BasicsRabin BK
 
vvvvvvvvvvvvvL2A_CurveRepresentations.pdf
vvvvvvvvvvvvvL2A_CurveRepresentations.pdfvvvvvvvvvvvvvL2A_CurveRepresentations.pdf
vvvvvvvvvvvvvL2A_CurveRepresentations.pdfKhalil 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
 

Similaire à 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 material
Integration material Integration material
Integration material
 
Integration
IntegrationIntegration
Integration
 
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...
 

Dernier

Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...tanu pandey
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01KreezheaRecto
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLManishPatel169454
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 

Dernier (20)

Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 

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