Professional Documents
Culture Documents
Oliveira J. N. Why Category Theory Matters - A Functional Programmer's Perspective.
Oliveira J. N. Why Category Theory Matters - A Functional Programmer's Perspective.
net/publication/282868977
CITATIONS READS
0 4,608
1 author:
SEE PROFILE
Some of the authors of this publication are also working on these related projects:
All content following this page was uploaded by José Nuno Oliveira on 16 October 2015.
J.N. Oliveira
Inesc Tec & University of Minho
Abstract
How did this happen? A look into the past shows how FP has
been inspired by category theory.
Algebras
Functional programming is a data driven style of programming —
programs react to input data by delivering output data.
Ao
a
FA
Example: the Peano algebra
[zero,succ]
N0 o 1 + N0
Functors
Ao
a
a FA
h h Fh
b Bo b
FB
Homomorphisms
Example: the function (n×) which multiplies a natural number by
some given n is the following (1+)-homomorphism:
[zero,succ]
[zero, succ] N0 o 1 + N0
(n×) (n×) id+(n×)
[zero, (n+)] Bo [zero,(n+)]
1+B
Initial algebras
i µF o i
F µF
k= (|a|) ⇔ k Fk
a Ao a FA
Catamorphisms
Another example:
F X = 1 + A × X for some fixed set A
µF = A? (finite sequences of A s)
in = [nil, cons]
where
nil = [ ] (empty sequence)
cons (a, x) = a : x (sequence construction).
add (0, y ) = y
add (1 + x, y ) = 1 + add (x, y )
sq 0 = 0
sq (1 + x) = 2 x + 1 + sq x
msq 0 = return 0
msq (1 + x) = do {y ← msq x; print x; return (2 x + 1 + y )}
— what ???
Context Algebras Mendler Monads Annex References References
Mendler style
µF o in
Fµ x · in = Ψ x
oo
F
Ψ xoooo
x
ooo F x
wooooo
A o_ _ _ _ _ _ _ F A
a
(Ψ f ) · F h = Ψ (f · h)
holds (naturality of Ψ on X ).
Context Algebras Mendler Monads Annex References References
Mendler style
x · in = Ψ x (1)
(Ψ f ) · F h = Ψ (f · h)
Context Algebras Mendler Monads Annex References References
Mendler style
x · in = Ψ x
⇔ { naturality: Ψ x = (Ψ id) · F x }
x · in = (Ψ id) · F x
⇔ { catamorphism }
x = (|Ψ id|)
⇔ { choice of notation above }
x = h|Ψ|i
Calculational properties
x = h|Ψ|i ⇔ x · in = Ψ x (2)
h|Ψ|i · in = Ψ h|Ψ|i
id = h|Ψ|i ⇔ Ψ id = in
h · h|Φ|i = h|Ψ|i ⇐ h · (Φ f ) = Ψ (h · f )
Recall “counter-example”
add (0, y ) = y
add (1 + x, y ) = 1 + add (x, y )
that is
Clearly:
L µF o
L in
L µF L (F µF )
t
tt
x x ttt
t
ty tt Φ x
B B
Does x · (L in) = Φ x have a unique solution?
Context Algebras Mendler Monads Annex References References
Adjunctions:
C[ λ◦
s
L a R D (L A, B) ∼
= C (A, R B)
3
λ
D
In the example:
S[ uncurry
r
a C)
S (A × C , B) ∼ C
= 3 S(A, B )
(×C ) (
curry
S
Context Algebras Mendler Monads Annex References References
→ overleaf
Context Algebras Mendler Monads Annex References References
(λ x)·in (λ·Φ·λ ◦) (λ x)
RBo F µF = R B o F µF
⇔ { (2) for x := λ x , Ψ = λ · Φ · λ◦ }
h|λ·Φ·λ◦ |i
RBo µF = R B o
λx
µF
⇔ { isomorphism λ◦ }
λ◦ h|λ·Φ·λ◦ |i
Bo L µF = B o
x
L µF
Altogether:
L in = Φ id ⇔ id = h|Φ|iλ
h · h|Φ|iλ = h|Ψ|iλ ⇐ h · (Φ f ) = Ψ (h · f )
Two examples
From
adjunction
S[ λ◦ =uncurry
r
a C)
S (A × C , B) ∼ C
= 3 S(A, B )
(×C ) (
λ=curry
S
By primitive recursion,
sq 0 = 0
sq (1 + x) = 2 x + 1 + sq x
rewrites to
sq 0 = 0
sq (1 + n) = odd n + sq n
odd 0 = 1
odd (1 + n) = 2 + odd n
Adjunction ∆ a (×)
SZ λ◦ f =(π1 ·f ,π2 ·f )
r
∆ a (×) (S × S) (∆ A, (B, C )) ∼
= S (A, B × C )
2
λ (f ,g )=hf ,g i
S×S
(sq, odd) · (∆ inN0 ) = ([zero, add · hsq, oddi] , [one, (2+) · odd])
| {z }
Φ (sq,odd)
Adjunction ∆ a (×)
sqodd · inN0 = (Ψ id) · (id + hsq, oddi)
Ψ id
= { }
λ (Φ (λ◦ id))
= { λ◦ f = (π1 · f , π2 · f ) }
λ (Φ (π1 , π2 ))
= { definition of Φ }
λ ([zero, add · hπ1 , π2 i] , [one, (2+) · π2 ])
= { λ (f , g ) = hf , g i }
h[zero, add] , [one, (2+) · π2 ]i
Context Algebras Mendler Monads Annex References References
Adjunction ∆ a (×)
This leads to the adjoint solution
sqodd 0 = (0, 1)
sqodd (1 + n) = (s + o, 2 + o) where (s, o) = hsq, oddi n
More adjunctions
This also covers the dual case — final algebras and corresponding
morphisms (‘unfolds’).
Context Algebras Mendler Monads Annex References References
Monads
C[ λ◦ id /
L (R C )
R OC O x; C
xxx
a x
xx
L R k=λ f Lk
xx f
D A LA
define monads in a natural way — M = R · L is a monad:
µ=R (λ◦ id) η=λ id
M (M X ) /MX o X
Context Algebras Mendler Monads Annex References References
Monads
Kleisli categories
It has become practical to reason about monadic programs not in
the original category C but rather in the associated Kleisli
category CM arising from adjunction
LX =X RX =MX
a
Lf =η·f Rf =µ·Mf
µ η
where M (M X ) /MX o X is the monad of interest:
C[
λ◦
s
L a R CM (A, B) ∼
= C (A, M B)
2
λ
CM
By the way —“folk” FP notation for R f is (in Haskell syntax):
Rf x = do {a ← x; f a}
Context Algebras Mendler Monads Annex References References
Kleisli categories
(Enriched) Kleisli categories offer powerful frameworks for
reasoning about FPs, namely:
• Category of binary relations — cf. powerset monad —
homsets = Boolean algebras
• Category of stochastic matrices — cf. distribution monad
— cf. (typed) linear algebra.
Currently studying calculational properties offered by Kleisli
categories concerning our starting point, but now extended with a
monad on the output:
L µF o
L in
L µF L (F µF )
t
tt
x x ttt
t
y t Φx
t
MB MB
Context Algebras Mendler Monads Annex References References
Epilogue
Epilogue
Annex
Context Algebras Mendler Monads Annex References References
Running example
Solving
for add:
Running example
⇔ { coproducts }
curry add · zero = curry π2
curry add · succ = curry (succ · add)
⇔ { exponentials }
curry add · zero = id
curry add · succ = (succ·) · (curry add)
⇔ { }
curry add · inN0 = [id, (succ·) · (curry add)]
⇔ { }
curry add = h|[id, (succ·) · ( )]|i
References
Context Algebras Mendler Monads Annex References References