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

29
A Tale of Two Monads: Category-theoretic and Computational viewpoints Liang-Ting Chen

description

A Tale of Two Monads --by Liang-Ting Chen (ltchen) --on Functional Thursday Meetup 11

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

Page 1: [FT-11][ltchen] A Tale of Two Monads

A Tale of Two Monads: Category-theoretic and Computational viewpoints

Liang-Ting Chen

Page 2: [FT-11][ltchen] A Tale of Two Monads

What is … a monad?

Functional Programmer:

Page 3: [FT-11][ltchen] A Tale of Two Monads

What is … a monad?

Functional Programmer:• a warm, fuzzy, little thing

Monica Monad, by FalconNL

Page 4: [FT-11][ltchen] A Tale of Two Monads

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 am >>= return = mm >>= (\x-> k x >>= h) = (m >>= k) >>= h

Page 5: [FT-11][ltchen] A Tale of Two Monads

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

}

Page 6: [FT-11][ltchen] A Tale of Two Monads

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) inN : T�(bind)

Page 7: [FT-11][ltchen] A Tale of Two Monads

“Hey, mathematician! What is a monad?”,you asked.

Page 8: [FT-11][ltchen] A Tale of Two Monads

“A monad in X is just a monoid in the category of endofunctors of X, what’s the problem?”

–Philip Wadler

Page 9: [FT-11][ltchen] A Tale of Two Monads

–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?”

Page 10: [FT-11][ltchen] A Tale of Two Monads

–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.”

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

What is … a monad?

Mathematician:• a monoid in the category of

endofunctors T 3 Tµ//

µT✏✏

T 2

µ✏✏

T 2µ// T

TT⌘//

id

T 2

µ

✏✏

T⌘Too

id~~

T

monad laws

monad on a categoryT : C ! C

⌘ : I ˙�!T

µ : T 2 ˙�!T

Page 12: [FT-11][ltchen] A Tale of Two Monads

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

ttttµ//

µt✏✏

tt

µ✏✏

tt µ// t

tt⌘//

id��

tt

µ

✏✏

t⌘too

id��

t

monad in a bicategory

monad laws

Page 13: [FT-11][ltchen] A Tale of Two Monads

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

Page 14: [FT-11][ltchen] A Tale of Two Monads

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

T 3 Tµ//

µT✏✏

T 2

µ✏✏

T 2µ// T

TT⌘//

id

T 2

µ

✏✏

T⌘Too

id~~

T

Page 15: [FT-11][ltchen] A Tale of Two Monads

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

Page 16: [FT-11][ltchen] A Tale of Two Monads

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)

Page 17: [FT-11][ltchen] A Tale of Two Monads

–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.

Page 18: [FT-11][ltchen] A Tale of Two Monads

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

Page 19: [FT-11][ltchen] A Tale of Two Monads

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

Page 20: [FT-11][ltchen] A Tale of Two Monads

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

Page 21: [FT-11][ltchen] A Tale of Two Monads

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

Page 22: [FT-11][ltchen] A Tale of Two Monads

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))

Page 23: [FT-11][ltchen] A Tale of Two Monads

A Model of an Algebraic Theory (an instance)

• a set M with

• an n-ary function �M for each operation � with ar(�) = nsatisfying each equation

Page 24: [FT-11][ltchen] A Tale of Two Monads

A Monad with Finite Rank

MX =[

{Mi[MS] | i : S ✓f X }

(Mi : MS ! MX)

Page 25: [FT-11][ltchen] A Tale of Two Monads

• 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

Page 26: [FT-11][ltchen] A Tale of Two Monads

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

Page 27: [FT-11][ltchen] A Tale of Two Monads

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.

Page 28: [FT-11][ltchen] A Tale of Two Monads

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

Page 29: [FT-11][ltchen] A Tale of Two Monads

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