Monads from Comonads—Prologue WG 2.8 Marble Falls Monads from Comonads Comonads from Monads Ralf Hinze Computing Laboratory, University of Oxford Wolfson Building, Parks Road, Oxford, OX1 3QD, England ralf.hinze@comlab.ox.ac.uk http://www.comlab.ox.ac.uk/ralf.hinze/ March 2011 University of Oxford—Ralf Hinze 1-39
Monads from Comonads—Some context WG 2.8 Marble Falls Buy one get one free! A common form of sales promotion (BOGOF). University of Oxford—Ralf Hinze 2-39
Monads from Comonads—Some context WG 2.8 Marble Falls 1 Monads Monads, a success story. University of Oxford—Ralf Hinze 3-39
Monads from Comonads—Some context WG 2.8 Marble Falls A → M B University of Oxford—Ralf Hinze 4-39
Monads from Comonads—Some context WG 2.8 Marble Falls A monad consists of a functor M and natural transformations r : I → M j : MM → M The two operations have to go together: j · r M = id M j · M r = id M j · M j = j · j M MMM M j ≻ MM MM r M ≻ j ≻ id j M j M ≻ M j ≻ � � M r ≻ j ≻ M MM MM University of Oxford—Ralf Hinze 5-39
Monads from Comonads—Some context WG 2.8 Marble Falls 1 Comonads Comonads, not exactly a success story. University of Oxford—Ralf Hinze 6-39
Monads from Comonads—Some context WG 2.8 Marble Falls N A → B University of Oxford—Ralf Hinze 7-39
Monads from Comonads—Some context WG 2.8 Marble Falls A comonad consists of a functor N and natural transformations e : N → I d : N → NN The two operations have to go together: N e · d = id N e N · d = id N N d · d = d N · d d ≻ NN NN N d ≻ e N ≻ id d N d N ≻ N N e ≻ � � d ≻ ≻ NNN NN NN d N University of Oxford—Ralf Hinze 8-39
Monads from Comonads—Some context WG 2.8 Marble Falls The simplest of all: the product comonad . N = − × X e = outl d = id △ outr University of Oxford—Ralf Hinze 9-39
Monads from Comonads—Some context WG 2.8 Marble Falls Why has the product comonad not taken off? University of Oxford—Ralf Hinze 10-39
Monads from Comonads—Adjunctions WG 2.8 Marble Falls 2 Adjunctions • One of the most beautiful constructions in mathematics. • They allow us to transfer a problem to another domain. • They provide a framework for program transformations. University of Oxford—Ralf Hinze 11-39
Monads from Comonads—Adjunctions WG 2.8 Marble Falls Let C and D be categories. The functors L : C ← D and R : C → D are adjoint , L ⊣ R , L C ≺ ≻ D ⊥ R if and only if there is a bijection between the hom-sets C ( L A , B ) ∼ = D ( A , R B ) that is natural both in A and B . The witness of the isomorphism is called the left adjunct . It allows us to trade L in the source for R in the target of an arrow. Its inverse is the right adjunct . University of Oxford—Ralf Hinze 12-39
Monads from Comonads—Adjunctions WG 2.8 Marble Falls Perhaps the best-known example of an adjunction is currying. C ≺− × X = C ( A , B X ) Λ : C ( A × X , B ) ∼ ( − ) X ≻ C ⊥ The left adjunct Λ is also called curry and the right adjunct Λ ◦ is also called uncurry . University of Oxford—Ralf Hinze 13-39
Monads from Comonads—Adjunctions WG 2.8 Marble Falls C ≺− × X = C ( A , B X ) Λ : C ( A × X , B ) ∼ ( − ) X ≻ C ⊥ The images of the identity are function application and the return of the state monad. app = Λ ◦ id : C ( B X × X , B ) r = Λ id : C ( A , ( A × X ) X ) University of Oxford—Ralf Hinze 14-39
Monads from Comonads—Adjunctions WG 2.8 Marble Falls The images of the identity are the counit and the unit of the adjunction. ǫ : LR → I η : I → RL An alternative definition of adjunctions builds solely on these units, which have to satisfy ǫ L · L η = id L R ǫ · η R = id R LRL RLR ≻ ≻ R R η ǫ L L η ǫ ≻ ≻ id L id R ≻ ≻ L L R R University of Oxford—Ralf Hinze 15-39
Monads from Comonads—Adjunctions WG 2.8 Marble Falls 2 Comonads and monads Every adjunction L ⊣ R induces a comonad and a monad. N = LR M = RL e = ǫ r = η d = L η R j = R ǫ L University of Oxford—Ralf Hinze 16-39
Monads from Comonads—Adjunctions WG 2.8 Marble Falls The curry adjunction induces the state monad . M A = ( A × X ) X r = Λ id j = Λ ( app · app ) The monad supports stateful computations, where the state X is threaded through a program. University of Oxford—Ralf Hinze 17-39
Monads from Comonads—Adjunctions WG 2.8 Marble Falls The curry adjunction induces the costate comonad . N A = A X × X e = app d = ( Λ id ) × X The context can be seen as a store A X together with a memory location X , a focus of interest. University of Oxford—Ralf Hinze 18-39
Monads from Comonads—Program transformations WG 2.8 Marble Falls 3 Transforming natural transformations • Adjunctions provide a framework for program transformations. • All the operations we have encountered so far are natural transformations. • To deal effectively with those we develop a little theory of ‘natural transformation transformers’. University of Oxford—Ralf Hinze 19-39
Monads from Comonads—Program transformations WG 2.8 Marble Falls 3 Post-composition Every adjunction L ⊣ R gives rise to an adjunction L − ⊣ R − between functor categories. C E ≺ L − L C ≺ R − ≻ D E ≻ D then ⊥ ⊥ R C E ( LF , G ) ∼ = D E ( F , RG ) University of Oxford—Ralf Hinze 20-39
Monads from Comonads—Program transformations WG 2.8 Marble Falls We write ⌊−⌋ for the lifted left adjunct and ⌊−⌋ ◦ for its inverse. α : LF → G β : F → RG ⌊ β ⌋ ◦ : LF → G ⌊ α ⌋ : F → RG The lifted adjuncts can be defined in terms of the units of the underlying adjunction: ⌊ β ⌋ ◦ = ǫ G · L β ⌊ α ⌋ = R α · η F University of Oxford—Ralf Hinze 21-39
Monads from Comonads—Program transformations WG 2.8 Marble Falls 3 Pre-composition Post-composition dualizes to pre-composition. Consequently, every adjunction L ⊣ R also induces an adjunction − R ⊣ − L . E D ≺ − R L C ≺ − L ≻ E C ≻ D then ⊥ ⊥ R = E C ( F , GL ) E D ( FR , G ) ∼ University of Oxford—Ralf Hinze 22-39
Monads from Comonads—Program transformations WG 2.8 Marble Falls We write ⌈−⌉ ◦ for the lifted left adjunct and ⌈−⌉ for its inverse. β : FR → G α : F → GL ⌈ β ⌉ ◦ : F → GL ⌈ α ⌉ : FR → G Again, the lifted adjuncts can be defined in terms of the units of the underlying adjunction: ⌈ β ⌉ ◦ = β L · F η ⌈ α ⌉ = G ǫ · α R An aide-mémoire: ⌊−⌋ turns an L in the source to an R in the target, while ⌈−⌉ turns an L in the target to an R in the source. University of Oxford—Ralf Hinze 23-39
Monads from Comonads—Program transformations WG 2.8 Marble Falls 3 Transformation transformers If we combine ⌊−⌋ and ⌈−⌉ , we can send natural transformations of type LF → GL to transformations of type FR → RG . The order in which we apply the adjuncts does not matter. ⌈⌊ β ⌋ ◦ ⌉ ◦ = ⌊⌈ β ⌉ ◦ ⌋ ◦ ⌊⌈ α ⌉⌋ = ⌈⌊ α ⌋⌉ University of Oxford—Ralf Hinze 24-39
Monads from Comonads—Program transformations WG 2.8 Marble Falls If we assume that L and R are endofunctors, then we can nest ⌊−⌋ and ⌈−⌉ arbitrarily deep. α : L m F → GL n ⌈⌊ α ⌋ m ⌉ n : R n F → GR m An aide-mémoire: the number of ⌊ s corresponds to the number of L s in the source, and the number of ⌈ s corresponds to the number of L s in the target. University of Oxford—Ralf Hinze 25-39
Monads from Comonads—Comonads from monads WG 2.8 Marble Falls 4 Monads from comonads • Assume that a left adjoint is at the same time a comonad. • Then its right adjoint is a monad! • Dually, the left adjoint of a monad is a comonad. University of Oxford—Ralf Hinze 26-39
Monads from Comonads—Comonads from monads WG 2.8 Marble Falls The ‘transformation transformers’ allow us to systematically turn the comonadic operations into monadic ones and vice versa. e = ⌊ r ⌋ ◦ : L → I r = ⌊ e ⌋ : I → R d = ⌈⌈⌊ j ⌋ ◦ ⌉ ◦ ⌉ ◦ : L → LL j = ⌊⌈⌈ d ⌉⌉⌋ : RR → R University of Oxford—Ralf Hinze 27-39
Monads from Comonads—Comonads from monads WG 2.8 Marble Falls The curry adjunction, provides an example, where the left adjoint L = − × X is also a comonad. e = outl d = id △ outr Consequently, L ’s right adjoint R = ( − ) X is a monad with operations r = ⌊ outl ⌋ = Λ outl j = ⌊⌈⌈ id △ outr ⌉⌉⌋ = Λ ( app · ( app △ outr )) The resulting structure is known as the reader monad . University of Oxford—Ralf Hinze 28-39
Monads from Comonads—Comonads from monads WG 2.8 Marble Falls A × X → B ∼ = A → B X University of Oxford—Ralf Hinze 29-39
Monads from Comonads—Comonads from monads WG 2.8 Marble Falls Every (co)monad comes equipped with additional operations. The product comonad might provide a getter and an update operation: get = outr : L → ∆ X update ( f : X → X ) = id × f : L → L where ∆ X is the constant functor. The transforms of get and update correspond to operations called ask and local in the Haskell monad transformer library. ask = ⌊ outr ⌋ = Λ outr : I → R ∆ X local ( f : X → X ) = ⌊⌈ id × f ⌉⌋ = Λ ( app · ( id × f )) : R → R University of Oxford—Ralf Hinze 30-39
Recommend
More recommend