SlideShare ist ein Scribd-Unternehmen logo
1 von 29
Downloaden Sie, um offline zu lesen
A Tale of Two Monads: Category-theoretic and
Computational viewpoints
Liang-Ting Chen
What is … a monad?
Functional Programmer:
What is … a monad?
Functional Programmer:
• a warm, fuzzy, little thing
Monica Monad, by FalconNL
What is … a monad?
Functional Programmer:
• a warm, fuzzy, little thing
• return and bind with monad
laws
class Monad m where	
(>>=) ::m a->(a -> m b)->m b
return::a ->m a	
!
-- monad laws 	
return a >>= k = k a	
m >>= return = m	
m >>= (x-> k x >>= h) =

(m >>= k) >>= h
What is … a monad?
Functional Programmer:
• a warm, fuzzy, little thing
• return and bind with monad
laws
• a programmable semicolon
• do { x' <- return x; f x’} 

≡ do { f x }

• do { x <- m; return x }

≡ do { m }

• do { y <- do { x <- m; f x }
g y }

≡ do { x <- m; do { y <- f
x; g y }}

≡ do { x <- m; y <- f x; g y
}
What is … a monad?
Functional Programmer:
• a warm, fuzzy, little thing
• return and bind with monad
laws
• a programmable semicolon
• E. Moggi, “Notions of
computation and monads”, 1991
T : Obj(C) ! Obj(C)
⌘A : A ! TA
( )⇤
: hom(A, TB) ! hom(TA, TB)
⌘⇤
A = idTA
⌘A; f⇤
= f
f⇤
; g⇤
= (f; g)⇤
Kleisli Triple
monad laws
` M :
` [M]T : T
(return)
` M : T⌧ x : ⌧ ` N : T
` letT (x ( M) in N : T
(bind)
“Hey, mathematician! What is a monad?”,

you asked.
“A monad in X is just a monoid in the category
of endofunctors of X, what’s the problem?”
–Philip Wadler
–James Iry, A Brief, Incomplete and Mostly Wrong History of
Programming Languages
“A monad in X is just a monoid in the category
of endofunctors of X, what’s the problem?”
–Saunders Mac Lane, Categories for the Working Mathematician, p.138
“A monad in X is just a monoid in the category
of endofunctors of X, with product × replaced
by composition of endofunctors and unit set by
the identity endofunctor.”
What is … a monad?
Mathematician:
• a monoid in the category of
endofunctors T3 Tµ
//
µT
✏✏
T2
µ
✏✏
T2
µ
// T
T
T⌘
//
id
T2
µ
✏✏
T
⌘T
oo
id~~
T
monad laws
monad on a category
T : C ! C
⌘: I ˙!T
µ: T2
˙!T
What is … a monad?
Mathematician:
• a monoid in the category of
endofunctors
• a monoid in the endomorphism
category K(a,a) of a bicategory K
• 0-cell a;
• 1-cell t: a ! a;
• 2-cell ⌘: 1a ! t, and µ: tt ! t
ttt
tµ
//
µt
✏✏
tt
µ
✏✏
tt µ
// t
t
t⌘
//
id
tt
µ
✏✏
t
⌘t
oo
id

t
monad in a bicategory
monad laws
What is … a monad?
Mathematician:
• a monoid in the category of
endofunctors
• a monoid in the endomorphism
category K(a,a) of a bicategory K
• …
from Su Horng’s slide
Monads in Haskell, the Abstract Ones
• class Functor m => Monad m where

unit :: a -> m a -- η 

join :: m (m a) -> m a -- μ	
• --join . (fmap join) = join . join

--join . (fmap unit) = join . unit = id
T3 Tµ
//
µT
✏✏
T2
µ
✏✏
T2
µ
// T
T
T⌘
//
id
T2
µ
✏✏
T
⌘T
oo
id~~
T
Kleisli Triples and Monads are Equivalent (Manes 1976)
• fmap :: Monad m => (a -> b) -> m a -> m b

fmap f x = x >>= return . f



join :: Monad m => m (m a) -> m a

join x = x >>= id

-- id :: m a -> m a
Kleisli Triples and Monads are Equivalent (Manes 1976)
• fmap :: Monad m => (a -> b) -> m a -> m b

fmap f x = x >>= return . f



join :: Monad m => m (m a) -> m a

join x = x >>= id

-- id :: m a -> m a
• (>>=) :: Monad m => m a -> (a -> m b) -> m b

x >>= f = join (fmap f x)

-- fmap f :: m a -> m (m b)
–G. M. Kelly and A. J. Power, Adjunctions whose counits are
coequalizers, and presentations of finitary enriched monads, 1993.
Monads are derivable from algebraic
operations and equations if and only if they
have finite rank.
An Algebraic Theory: Monoid
• a set M with
• a nullary operation ✏: 1 ! M
• a binary operation •: M ⇥ M ! M
satisfying
• associativity: (a • b) • c = a • (b • c)
• identity: a • ✏ = ✏ • a = a
Monoids in Haskell:
class Monoid a where	
mempty :: a	
-- ^ Identity of 'mappend'	
mappend :: a -> a -> a	
-- ^ An associative operation	
!
instance Monoid [a] where	
mempty = []	
mappend = (++)	
!
instance Monoid b => Monoid (a -> b) where	
mempty _ = mempty	
mappend f g x = f x `mappend` g x
An Algebraic Theory: Semi-lattice
• a set L with
• a binary operation _: M ⇥ M ! M
satisfying
• commutativity: a _ b = b _ a
• associativity: a _ (b _ c) = (a _ b) _ c
• idenpotency: a _ a = a
Semi-lattices in Haskell
class SemiLattice a where	
join :: a -> a -> a	
!
instance SemiLattice Bool where	
join = (||)	
!
instance SemiLattice v => SemiLattice (k -> v) where	
f `join` g = x -> f x `join` g x	
!
instance SemiLattice IntSet where	
join = union
An Algebraic Theory (defined as a type class in Haskell)
• a set of operations 2 ⌃ and ar( ) 2 N
• a set of equations with variables, e.g. 1( 1(x, y), z) = 1(x, 1(y, z))
A Model of an Algebraic Theory (an instance)
• a set M with
• an n-ary function M for each operation with ar( ) = n
satisfying each equation
A Monad with Finite Rank
MX =
[
{ Mi[MS] | i: S ✓f X }
(Mi: MS ! MX)
• maybe
• exceptions
• nondeterminism
• side-effects
but continuations is not algebraic
Examples of Algebraic Effects
X 7! X + E
X 7! (X ⇥ State)State
X 7! Pfin(X)
X 7! R(RX )
X 7! X + 1
Algebraic Theory of Exception
A monadic program
f :: A -> B + E	
corresponds to a homomorphism between free algebras
• nullary operations raisee for each e 2 E
• no equations
Why Algebraic Effects?
• Various ways of combination, e.g. sum, product,
distribution, etc.
• Equational reasoning of monadic programming is simpler.
• A classification of effects: a deeper insight.
Conclusion
• Moggi’s formulation solves fundamental problems, e.g. a
unified approach to I/O.
• Mathematicians bring new ideas to functional
programming, e.g. algebraic effects, modular
construction of effects
• Still an ongoing area
Conclusion
• Moggi’s formulation solves fundamental problems, e.g. a
unified approach to I/O.
• Mathematicians bring new ideas to functional
programming, e.g. algebraic effects, modular
construction of effects
• Still an ongoing area

Weitere ähnliche Inhalte

Was ist angesagt?

derogatory and non derogatory matrices
derogatory and non derogatory matricesderogatory and non derogatory matrices
derogatory and non derogatory matricesKomal Singh
 
Eigenvalues and eigenvectors
Eigenvalues and eigenvectorsEigenvalues and eigenvectors
Eigenvalues and eigenvectorsiraq
 
Eigen values and eigen vectors
Eigen values and eigen vectorsEigen values and eigen vectors
Eigen values and eigen vectorsRiddhi Patel
 
Seismic data processing (mathematical foundations)
Seismic data processing (mathematical foundations)Seismic data processing (mathematical foundations)
Seismic data processing (mathematical foundations)Amin khalil
 
Eigen value , eigen vectors, caley hamilton theorem
Eigen value , eigen vectors, caley hamilton theoremEigen value , eigen vectors, caley hamilton theorem
Eigen value , eigen vectors, caley hamilton theoremgidc engineering college
 
Eigen values and eigenvectors
Eigen values and eigenvectorsEigen values and eigenvectors
Eigen values and eigenvectorsAmit Singh
 
Eigenvalues and Eigenvectors (Tacoma Narrows Bridge video included)
Eigenvalues and Eigenvectors (Tacoma Narrows Bridge video included)Eigenvalues and Eigenvectors (Tacoma Narrows Bridge video included)
Eigenvalues and Eigenvectors (Tacoma Narrows Bridge video included)Prasanth George
 
Eigen value and eigen vector
Eigen value and eigen vectorEigen value and eigen vector
Eigen value and eigen vectorRutvij Patel
 
4. Linear Algebra for Machine Learning: Eigenvalues, Eigenvectors and Diagona...
4. Linear Algebra for Machine Learning: Eigenvalues, Eigenvectors and Diagona...4. Linear Algebra for Machine Learning: Eigenvalues, Eigenvectors and Diagona...
4. Linear Algebra for Machine Learning: Eigenvalues, Eigenvectors and Diagona...Ceni Babaoglu, PhD
 
Eigenvalues and Eigenvectors
Eigenvalues and EigenvectorsEigenvalues and Eigenvectors
Eigenvalues and EigenvectorsVinod Srivastava
 
Diagonalization and eigen
Diagonalization and eigenDiagonalization and eigen
Diagonalization and eigenParesh Parmar
 
2. Linear Algebra for Machine Learning: Basis and Dimension
2. Linear Algebra for Machine Learning: Basis and Dimension2. Linear Algebra for Machine Learning: Basis and Dimension
2. Linear Algebra for Machine Learning: Basis and DimensionCeni Babaoglu, PhD
 
Eigen values and eigen vectors engineering
Eigen values and eigen vectors engineeringEigen values and eigen vectors engineering
Eigen values and eigen vectors engineeringshubham211
 
Maths-->>Eigenvalues and eigenvectors
Maths-->>Eigenvalues and eigenvectorsMaths-->>Eigenvalues and eigenvectors
Maths-->>Eigenvalues and eigenvectorsJaydev Kishnani
 
Applied numerical methods lec13
Applied numerical methods lec13Applied numerical methods lec13
Applied numerical methods lec13Yasser Ahmed
 
Eigen values and eigen vectors
Eigen values and eigen vectorsEigen values and eigen vectors
Eigen values and eigen vectorstirath prajapati
 
Seismic data processing introductory lecture
Seismic data processing introductory lectureSeismic data processing introductory lecture
Seismic data processing introductory lectureAmin khalil
 

Was ist angesagt? (20)

derogatory and non derogatory matrices
derogatory and non derogatory matricesderogatory and non derogatory matrices
derogatory and non derogatory matrices
 
eigenvalue
eigenvalueeigenvalue
eigenvalue
 
Eigen value and vectors
Eigen value and vectorsEigen value and vectors
Eigen value and vectors
 
Eigenvalues and eigenvectors
Eigenvalues and eigenvectorsEigenvalues and eigenvectors
Eigenvalues and eigenvectors
 
Eigen values and eigen vectors
Eigen values and eigen vectorsEigen values and eigen vectors
Eigen values and eigen vectors
 
Seismic data processing (mathematical foundations)
Seismic data processing (mathematical foundations)Seismic data processing (mathematical foundations)
Seismic data processing (mathematical foundations)
 
Eigen value , eigen vectors, caley hamilton theorem
Eigen value , eigen vectors, caley hamilton theoremEigen value , eigen vectors, caley hamilton theorem
Eigen value , eigen vectors, caley hamilton theorem
 
Eigen values and eigenvectors
Eigen values and eigenvectorsEigen values and eigenvectors
Eigen values and eigenvectors
 
Eigenvalues
EigenvaluesEigenvalues
Eigenvalues
 
Eigenvalues and Eigenvectors (Tacoma Narrows Bridge video included)
Eigenvalues and Eigenvectors (Tacoma Narrows Bridge video included)Eigenvalues and Eigenvectors (Tacoma Narrows Bridge video included)
Eigenvalues and Eigenvectors (Tacoma Narrows Bridge video included)
 
Eigen value and eigen vector
Eigen value and eigen vectorEigen value and eigen vector
Eigen value and eigen vector
 
4. Linear Algebra for Machine Learning: Eigenvalues, Eigenvectors and Diagona...
4. Linear Algebra for Machine Learning: Eigenvalues, Eigenvectors and Diagona...4. Linear Algebra for Machine Learning: Eigenvalues, Eigenvectors and Diagona...
4. Linear Algebra for Machine Learning: Eigenvalues, Eigenvectors and Diagona...
 
Eigenvalues and Eigenvectors
Eigenvalues and EigenvectorsEigenvalues and Eigenvectors
Eigenvalues and Eigenvectors
 
Diagonalization and eigen
Diagonalization and eigenDiagonalization and eigen
Diagonalization and eigen
 
2. Linear Algebra for Machine Learning: Basis and Dimension
2. Linear Algebra for Machine Learning: Basis and Dimension2. Linear Algebra for Machine Learning: Basis and Dimension
2. Linear Algebra for Machine Learning: Basis and Dimension
 
Eigen values and eigen vectors engineering
Eigen values and eigen vectors engineeringEigen values and eigen vectors engineering
Eigen values and eigen vectors engineering
 
Maths-->>Eigenvalues and eigenvectors
Maths-->>Eigenvalues and eigenvectorsMaths-->>Eigenvalues and eigenvectors
Maths-->>Eigenvalues and eigenvectors
 
Applied numerical methods lec13
Applied numerical methods lec13Applied numerical methods lec13
Applied numerical methods lec13
 
Eigen values and eigen vectors
Eigen values and eigen vectorsEigen values and eigen vectors
Eigen values and eigen vectors
 
Seismic data processing introductory lecture
Seismic data processing introductory lectureSeismic data processing introductory lecture
Seismic data processing introductory lecture
 

Andere mochten auch

Category Theory for Mortal Programmers
Category Theory for Mortal ProgrammersCategory Theory for Mortal Programmers
Category Theory for Mortal ProgrammersStephan February
 
Functional programming techniques in regular JavaScript
Functional programming techniques in regular JavaScriptFunctional programming techniques in regular JavaScript
Functional programming techniques in regular JavaScriptPavel Klimiankou
 
Functional Programming in C# and F#
Functional Programming in C# and F#Functional Programming in C# and F#
Functional Programming in C# and F#Alfonso Garcia-Caro
 
Pause 2017 Overview Document v3
Pause 2017 Overview Document v3Pause 2017 Overview Document v3
Pause 2017 Overview Document v3David C. Fary
 
3 131203214847-phpapp02
3 131203214847-phpapp023 131203214847-phpapp02
3 131203214847-phpapp02Kat Thrus
 
[FT-8][banacorn] Socket.IO for Haskell Folks
[FT-8][banacorn] Socket.IO for Haskell Folks[FT-8][banacorn] Socket.IO for Haskell Folks
[FT-8][banacorn] Socket.IO for Haskell FolksFunctional Thursday
 
Very final powerpoint for apps for good (Team Appocolypse)
Very final powerpoint for apps for good (Team Appocolypse)Very final powerpoint for apps for good (Team Appocolypse)
Very final powerpoint for apps for good (Team Appocolypse)TeamAppocolypse
 
Apps for good powerpoint
Apps for good powerpointApps for good powerpoint
Apps for good powerpointTeamAppocolypse
 
Evaluation Part 2
Evaluation Part 2Evaluation Part 2
Evaluation Part 2SophieB23
 
Evaluation Part 3
Evaluation Part 3Evaluation Part 3
Evaluation Part 3SophieB23
 
Classificador de padrões para determinar suceptibilidade ao fungo h em folhas...
Classificador de padrões para determinar suceptibilidade ao fungo h em folhas...Classificador de padrões para determinar suceptibilidade ao fungo h em folhas...
Classificador de padrões para determinar suceptibilidade ao fungo h em folhas...Juan Pablo Ochoa
 
[FLOLAC'14][scm] Functional Programming Using Haskell
[FLOLAC'14][scm] Functional Programming Using Haskell[FLOLAC'14][scm] Functional Programming Using Haskell
[FLOLAC'14][scm] Functional Programming Using HaskellFunctional Thursday
 
The Internet Vs the Mall
The Internet Vs the Mall The Internet Vs the Mall
The Internet Vs the Mall Agassi Alex
 
Representacion de power point
Representacion de power point Representacion de power point
Representacion de power point Evelyn Mendoza
 
Slide - Maxime Bartier
Slide - Maxime BartierSlide - Maxime Bartier
Slide - Maxime BartierEsgaGraphix
 
Aplicação de algoritmos genético e elipsoidal no
Aplicação de algoritmos genético e elipsoidal noAplicação de algoritmos genético e elipsoidal no
Aplicação de algoritmos genético e elipsoidal noJuan Pablo Ochoa
 
1.7 functional programming
1.7 functional programming1.7 functional programming
1.7 functional programmingfuturespective
 

Andere mochten auch (20)

Category Theory for Mortal Programmers
Category Theory for Mortal ProgrammersCategory Theory for Mortal Programmers
Category Theory for Mortal Programmers
 
Functional programming techniques in regular JavaScript
Functional programming techniques in regular JavaScriptFunctional programming techniques in regular JavaScript
Functional programming techniques in regular JavaScript
 
Functional Programming in C# and F#
Functional Programming in C# and F#Functional Programming in C# and F#
Functional Programming in C# and F#
 
Groovy Monads
Groovy MonadsGroovy Monads
Groovy Monads
 
Pause 2017 Overview Document v3
Pause 2017 Overview Document v3Pause 2017 Overview Document v3
Pause 2017 Overview Document v3
 
3 131203214847-phpapp02
3 131203214847-phpapp023 131203214847-phpapp02
3 131203214847-phpapp02
 
[FT-8][banacorn] Socket.IO for Haskell Folks
[FT-8][banacorn] Socket.IO for Haskell Folks[FT-8][banacorn] Socket.IO for Haskell Folks
[FT-8][banacorn] Socket.IO for Haskell Folks
 
3.9 Resume Years
3.9 Resume Years3.9 Resume Years
3.9 Resume Years
 
Very final powerpoint for apps for good (Team Appocolypse)
Very final powerpoint for apps for good (Team Appocolypse)Very final powerpoint for apps for good (Team Appocolypse)
Very final powerpoint for apps for good (Team Appocolypse)
 
Apps for good powerpoint
Apps for good powerpointApps for good powerpoint
Apps for good powerpoint
 
Evaluation Part 2
Evaluation Part 2Evaluation Part 2
Evaluation Part 2
 
Evaluation Part 3
Evaluation Part 3Evaluation Part 3
Evaluation Part 3
 
Team Appocolypse
Team AppocolypseTeam Appocolypse
Team Appocolypse
 
Classificador de padrões para determinar suceptibilidade ao fungo h em folhas...
Classificador de padrões para determinar suceptibilidade ao fungo h em folhas...Classificador de padrões para determinar suceptibilidade ao fungo h em folhas...
Classificador de padrões para determinar suceptibilidade ao fungo h em folhas...
 
[FLOLAC'14][scm] Functional Programming Using Haskell
[FLOLAC'14][scm] Functional Programming Using Haskell[FLOLAC'14][scm] Functional Programming Using Haskell
[FLOLAC'14][scm] Functional Programming Using Haskell
 
The Internet Vs the Mall
The Internet Vs the Mall The Internet Vs the Mall
The Internet Vs the Mall
 
Representacion de power point
Representacion de power point Representacion de power point
Representacion de power point
 
Slide - Maxime Bartier
Slide - Maxime BartierSlide - Maxime Bartier
Slide - Maxime Bartier
 
Aplicação de algoritmos genético e elipsoidal no
Aplicação de algoritmos genético e elipsoidal noAplicação de algoritmos genético e elipsoidal no
Aplicação de algoritmos genético e elipsoidal no
 
1.7 functional programming
1.7 functional programming1.7 functional programming
1.7 functional programming
 

Ähnlich wie [FT-11][ltchen] A Tale of Two Monads

Seismic data processing lecture 3
Seismic data processing lecture 3Seismic data processing lecture 3
Seismic data processing lecture 3Amin khalil
 
Category Theory made easy with (ugly) pictures
Category Theory made easy with (ugly) picturesCategory Theory made easy with (ugly) pictures
Category Theory made easy with (ugly) picturesAshwin Rao
 
Reduction Monads and Their Signatures
Reduction Monads and Their SignaturesReduction Monads and Their Signatures
Reduction Monads and Their SignaturesMarco Maggesi
 
Lambda Calculus & Functional programming
Lambda Calculus & Functional programmingLambda Calculus & Functional programming
Lambda Calculus & Functional programmingAlexander Nemish
 
Linear Algebra Presentation including basic of linear Algebra
Linear Algebra Presentation including basic of linear AlgebraLinear Algebra Presentation including basic of linear Algebra
Linear Algebra Presentation including basic of linear AlgebraMUHAMMADUSMAN93058
 
Subgradient Methods for Huge-Scale Optimization Problems - Юрий Нестеров, Cat...
Subgradient Methods for Huge-Scale Optimization Problems - Юрий Нестеров, Cat...Subgradient Methods for Huge-Scale Optimization Problems - Юрий Нестеров, Cat...
Subgradient Methods for Huge-Scale Optimization Problems - Юрий Нестеров, Cat...Yandex
 
Lesson 2: A Catalog of Essential Functions
Lesson 2: A Catalog of Essential FunctionsLesson 2: A Catalog of Essential Functions
Lesson 2: A Catalog of Essential FunctionsMatthew Leingang
 
DSP2_slides_04_adaptievefiltering.pdf
DSP2_slides_04_adaptievefiltering.pdfDSP2_slides_04_adaptievefiltering.pdf
DSP2_slides_04_adaptievefiltering.pdfRakuoane
 
Computer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdfComputer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdfjannatulferdousmaish
 
Data Analysis and Algorithms Lecture 1: Introduction
 Data Analysis and Algorithms Lecture 1: Introduction Data Analysis and Algorithms Lecture 1: Introduction
Data Analysis and Algorithms Lecture 1: IntroductionTayyabSattar5
 
Pshs 3rd yr_functions_young_einstein
Pshs 3rd yr_functions_young_einsteinPshs 3rd yr_functions_young_einstein
Pshs 3rd yr_functions_young_einsteinRenee Tan
 
Pshs 3rd yr_functions
Pshs 3rd yr_functionsPshs 3rd yr_functions
Pshs 3rd yr_functionsRenee Tan
 
A MATLAB project on LCR circuits
A MATLAB project on LCR circuitsA MATLAB project on LCR circuits
A MATLAB project on LCR circuitssvrohith 9
 

Ähnlich wie [FT-11][ltchen] A Tale of Two Monads (20)

Comonads in Haskell
Comonads in HaskellComonads in Haskell
Comonads in Haskell
 
Seismic data processing lecture 3
Seismic data processing lecture 3Seismic data processing lecture 3
Seismic data processing lecture 3
 
Category Theory made easy with (ugly) pictures
Category Theory made easy with (ugly) picturesCategory Theory made easy with (ugly) pictures
Category Theory made easy with (ugly) pictures
 
Reduction Monads and Their Signatures
Reduction Monads and Their SignaturesReduction Monads and Their Signatures
Reduction Monads and Their Signatures
 
Lambda Calculus & Functional programming
Lambda Calculus & Functional programmingLambda Calculus & Functional programming
Lambda Calculus & Functional programming
 
Linear Algebra Presentation including basic of linear Algebra
Linear Algebra Presentation including basic of linear AlgebraLinear Algebra Presentation including basic of linear Algebra
Linear Algebra Presentation including basic of linear Algebra
 
Presentation gauge field theory
Presentation gauge field theoryPresentation gauge field theory
Presentation gauge field theory
 
Subgradient Methods for Huge-Scale Optimization Problems - Юрий Нестеров, Cat...
Subgradient Methods for Huge-Scale Optimization Problems - Юрий Нестеров, Cat...Subgradient Methods for Huge-Scale Optimization Problems - Юрий Нестеров, Cat...
Subgradient Methods for Huge-Scale Optimization Problems - Юрий Нестеров, Cat...
 
bv_cvxslides (1).pdf
bv_cvxslides (1).pdfbv_cvxslides (1).pdf
bv_cvxslides (1).pdf
 
Q
QQ
Q
 
Lesson 2: A Catalog of Essential Functions
Lesson 2: A Catalog of Essential FunctionsLesson 2: A Catalog of Essential Functions
Lesson 2: A Catalog of Essential Functions
 
DSP2_slides_04_adaptievefiltering.pdf
DSP2_slides_04_adaptievefiltering.pdfDSP2_slides_04_adaptievefiltering.pdf
DSP2_slides_04_adaptievefiltering.pdf
 
Computer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdfComputer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdf
 
Data Analysis and Algorithms Lecture 1: Introduction
 Data Analysis and Algorithms Lecture 1: Introduction Data Analysis and Algorithms Lecture 1: Introduction
Data Analysis and Algorithms Lecture 1: Introduction
 
Pshs 3rd yr_functions_young_einstein
Pshs 3rd yr_functions_young_einsteinPshs 3rd yr_functions_young_einstein
Pshs 3rd yr_functions_young_einstein
 
Pshs 3rd yr_functions
Pshs 3rd yr_functionsPshs 3rd yr_functions
Pshs 3rd yr_functions
 
introduction to matlab.pptx
introduction to matlab.pptxintroduction to matlab.pptx
introduction to matlab.pptx
 
A bit about мcmc
A bit about мcmcA bit about мcmc
A bit about мcmc
 
A MATLAB project on LCR circuits
A MATLAB project on LCR circuitsA MATLAB project on LCR circuits
A MATLAB project on LCR circuits
 
Differential calculus
Differential calculusDifferential calculus
Differential calculus
 

Kürzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
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
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
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
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
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
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
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
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
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
 
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
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 

Kürzlich hochgeladen (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
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...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
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
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
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...
 
+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...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
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
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
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...
 
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
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 

[FT-11][ltchen] A Tale of Two Monads

  • 1. A Tale of Two Monads: Category-theoretic and Computational viewpoints Liang-Ting Chen
  • 2. What is … a monad? Functional Programmer:
  • 3. What is … a monad? Functional Programmer: • a warm, fuzzy, little thing Monica Monad, by FalconNL
  • 4. What is … a monad? Functional Programmer: • a warm, fuzzy, little thing • return and bind with monad laws class Monad m where (>>=) ::m a->(a -> m b)->m b return::a ->m a ! -- monad laws return a >>= k = k a m >>= return = m m >>= (x-> k x >>= h) =
 (m >>= k) >>= h
  • 5. What is … a monad? Functional Programmer: • a warm, fuzzy, little thing • return and bind with monad laws • a programmable semicolon • do { x' <- return x; f x’} 
 ≡ do { f x }
 • do { x <- m; return x }
 ≡ do { m }
 • do { y <- do { x <- m; f x } g y }
 ≡ do { x <- m; do { y <- f x; g y }}
 ≡ do { x <- m; y <- f x; g y }
  • 6. What is … a monad? Functional Programmer: • a warm, fuzzy, little thing • return and bind with monad laws • a programmable semicolon • E. Moggi, “Notions of computation and monads”, 1991 T : Obj(C) ! Obj(C) ⌘A : A ! TA ( )⇤ : hom(A, TB) ! hom(TA, TB) ⌘⇤ A = idTA ⌘A; f⇤ = f f⇤ ; g⇤ = (f; g)⇤ Kleisli Triple monad laws ` M : ` [M]T : T (return) ` M : T⌧ x : ⌧ ` N : T ` letT (x ( M) in N : T (bind)
  • 7. “Hey, mathematician! What is a monad?”,
 you asked.
  • 8. “A monad in X is just a monoid in the category of endofunctors of X, what’s the problem?” –Philip Wadler
  • 9. –James Iry, A Brief, Incomplete and Mostly Wrong History of Programming Languages “A monad in X is just a monoid in the category of endofunctors of X, what’s the problem?”
  • 10. –Saunders Mac Lane, Categories for the Working Mathematician, p.138 “A monad in X is just a monoid in the category of endofunctors of X, with product × replaced by composition of endofunctors and unit set by the identity endofunctor.”
  • 11. What is … a monad? Mathematician: • a monoid in the category of endofunctors T3 Tµ // µT ✏✏ T2 µ ✏✏ T2 µ // T T T⌘ // id T2 µ ✏✏ T ⌘T oo id~~ T monad laws monad on a category T : C ! C ⌘: I ˙!T µ: T2 ˙!T
  • 12. What is … a monad? Mathematician: • a monoid in the category of endofunctors • a monoid in the endomorphism category K(a,a) of a bicategory K • 0-cell a; • 1-cell t: a ! a; • 2-cell ⌘: 1a ! t, and µ: tt ! t ttt tµ // µt ✏✏ tt µ ✏✏ tt µ // t t t⌘ // id tt µ ✏✏ t ⌘t oo id  t monad in a bicategory monad laws
  • 13. What is … a monad? Mathematician: • a monoid in the category of endofunctors • a monoid in the endomorphism category K(a,a) of a bicategory K • … from Su Horng’s slide
  • 14. Monads in Haskell, the Abstract Ones • class Functor m => Monad m where
 unit :: a -> m a -- η 
 join :: m (m a) -> m a -- μ • --join . (fmap join) = join . join
 --join . (fmap unit) = join . unit = id T3 Tµ // µT ✏✏ T2 µ ✏✏ T2 µ // T T T⌘ // id T2 µ ✏✏ T ⌘T oo id~~ T
  • 15. Kleisli Triples and Monads are Equivalent (Manes 1976) • fmap :: Monad m => (a -> b) -> m a -> m b
 fmap f x = x >>= return . f
 
 join :: Monad m => m (m a) -> m a
 join x = x >>= id
 -- id :: m a -> m a
  • 16. Kleisli Triples and Monads are Equivalent (Manes 1976) • fmap :: Monad m => (a -> b) -> m a -> m b
 fmap f x = x >>= return . f
 
 join :: Monad m => m (m a) -> m a
 join x = x >>= id
 -- id :: m a -> m a • (>>=) :: Monad m => m a -> (a -> m b) -> m b
 x >>= f = join (fmap f x)
 -- fmap f :: m a -> m (m b)
  • 17. –G. M. Kelly and A. J. Power, Adjunctions whose counits are coequalizers, and presentations of finitary enriched monads, 1993. Monads are derivable from algebraic operations and equations if and only if they have finite rank.
  • 18. An Algebraic Theory: Monoid • a set M with • a nullary operation ✏: 1 ! M • a binary operation •: M ⇥ M ! M satisfying • associativity: (a • b) • c = a • (b • c) • identity: a • ✏ = ✏ • a = a
  • 19. Monoids in Haskell: class Monoid a where mempty :: a -- ^ Identity of 'mappend' mappend :: a -> a -> a -- ^ An associative operation ! instance Monoid [a] where mempty = [] mappend = (++) ! instance Monoid b => Monoid (a -> b) where mempty _ = mempty mappend f g x = f x `mappend` g x
  • 20. An Algebraic Theory: Semi-lattice • a set L with • a binary operation _: M ⇥ M ! M satisfying • commutativity: a _ b = b _ a • associativity: a _ (b _ c) = (a _ b) _ c • idenpotency: a _ a = a
  • 21. Semi-lattices in Haskell class SemiLattice a where join :: a -> a -> a ! instance SemiLattice Bool where join = (||) ! instance SemiLattice v => SemiLattice (k -> v) where f `join` g = x -> f x `join` g x ! instance SemiLattice IntSet where join = union
  • 22. An Algebraic Theory (defined as a type class in Haskell) • a set of operations 2 ⌃ and ar( ) 2 N • a set of equations with variables, e.g. 1( 1(x, y), z) = 1(x, 1(y, z))
  • 23. A Model of an Algebraic Theory (an instance) • a set M with • an n-ary function M for each operation with ar( ) = n satisfying each equation
  • 24. A Monad with Finite Rank MX = [ { Mi[MS] | i: S ✓f X } (Mi: MS ! MX)
  • 25. • maybe • exceptions • nondeterminism • side-effects but continuations is not algebraic Examples of Algebraic Effects X 7! X + E X 7! (X ⇥ State)State X 7! Pfin(X) X 7! R(RX ) X 7! X + 1
  • 26. Algebraic Theory of Exception A monadic program f :: A -> B + E corresponds to a homomorphism between free algebras • nullary operations raisee for each e 2 E • no equations
  • 27. Why Algebraic Effects? • Various ways of combination, e.g. sum, product, distribution, etc. • Equational reasoning of monadic programming is simpler. • A classification of effects: a deeper insight.
  • 28. Conclusion • Moggi’s formulation solves fundamental problems, e.g. a unified approach to I/O. • Mathematicians bring new ideas to functional programming, e.g. algebraic effects, modular construction of effects • Still an ongoing area
  • 29. Conclusion • Moggi’s formulation solves fundamental problems, e.g. a unified approach to I/O. • Mathematicians bring new ideas to functional programming, e.g. algebraic effects, modular construction of effects • Still an ongoing area