Monads from Comonads Comonads from Monads Ralf Hinze Computing - - PowerPoint PPT Presentation

monads from comonads comonads from monads
SMART_READER_LITE
LIVE PREVIEW

Monads from Comonads Comonads from Monads Ralf Hinze Computing - - PowerPoint PPT Presentation

Monads from ComonadsPrologue 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


slide-1
SLIDE 1

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

slide-2
SLIDE 2

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

slide-3
SLIDE 3

Monads from Comonads—Some context WG 2.8 Marble Falls

1 Monads

Monads, a success story.

University of Oxford—Ralf Hinze 3-39

slide-4
SLIDE 4

Monads from Comonads—Some context WG 2.8 Marble Falls

A → M B

University of Oxford—Ralf Hinze 4-39

slide-5
SLIDE 5

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 · rM = idM j · Mr = idM j · Mj = j · jM MM M id ≻ rM ≻ M j ≻ MM j ≻ Mr ≻ MMM Mj≻ MM MM jM

  • j ≻ M

j

  • University of Oxford—Ralf Hinze

5-39

slide-6
SLIDE 6

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

slide-7
SLIDE 7

Monads from Comonads—Some context WG 2.8 Marble Falls

N A → B

University of Oxford—Ralf Hinze 7-39

slide-8
SLIDE 8

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: Ne · d = idN eN · d = idN Nd · d = dN · d NN N id ≻ d ≻ N eN≻ NN Ne≻ d ≻ N d ≻ NN NN d

  • dN

≻ NNN Nd

  • University of Oxford—Ralf Hinze

8-39

slide-9
SLIDE 9

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

slide-10
SLIDE 10

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

slide-11
SLIDE 11

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

slide-12
SLIDE 12

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, C ≺ L ⊥ R ≻ D 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

slide-13
SLIDE 13

Monads from Comonads—Adjunctions WG 2.8 Marble Falls

Perhaps the best-known example of an adjunction is currying. C ≺− × X ⊥ (−)X ≻ C Λ : C (A × X , B) ∼ = C (A, BX ) The left adjunct Λ is also called curry and the right adjunct Λ◦ is also called uncurry.

University of Oxford—Ralf Hinze 13-39

slide-14
SLIDE 14

Monads from Comonads—Adjunctions WG 2.8 Marble Falls

C ≺− × X ⊥ (−)X ≻ C Λ : C (A × X , B) ∼ = C (A, BX ) The images of the identity are function application and the return of the state monad. app = Λ◦ id : C (BX × X , B) r = Λ id : C (A, (A × X )X )

University of Oxford—Ralf Hinze 14-39

slide-15
SLIDE 15

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η = idL Rǫ · ηR = idR LRL L idL ≻ L η ≻ L ǫ L ≻ RLR R idR ≻ η R ≻ R R ǫ ≻

University of Oxford—Ralf Hinze 15-39

slide-16
SLIDE 16

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 e = ǫ d = LηR M = RL r = η j = RǫL

University of Oxford—Ralf Hinze 16-39

slide-17
SLIDE 17

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

slide-18
SLIDE 18

Monads from Comonads—Adjunctions WG 2.8 Marble Falls

The curry adjunction induces the costate comonad. N A = AX × X e = app d = (Λ id) × X The context can be seen as a store AX together with a memory location X , a focus of interest.

University of Oxford—Ralf Hinze 18-39

slide-19
SLIDE 19

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

slide-20
SLIDE 20

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 ≺ L ⊥ R ≻ D then C E ≺ L− ⊥ R− ≻ DE C E (LF, G) ∼ = DE (F, RG)

University of Oxford—Ralf Hinze 20-39

slide-21
SLIDE 21

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 β : F → RG ⌊β⌋◦ : LF → G The lifted adjuncts can be defined in terms of the units of the underlying adjunction: ⌊α⌋ = Rα · ηF ⌊β⌋◦ = ǫG · Lβ

University of Oxford—Ralf Hinze 21-39

slide-22
SLIDE 22

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. C ≺ L ⊥ R ≻ D then E D ≺ −R ⊥ −L ≻ E C E D(FR, G) ∼ = E C (F, GL)

University of Oxford—Ralf Hinze 22-39

slide-23
SLIDE 23

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

slide-24
SLIDE 24

Monads from Comonads—Program transformations WG 2.8 Marble Falls

3 Transformation transformers

If we combine ⌊−⌋ and ⌈−⌉, we can send natural transformations

  • f 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

slide-25
SLIDE 25

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. α : LmF → GLn ⌈⌊α⌋m⌉n : RnF → GRm An aide-mémoire: the number of ⌊s corresponds to the number

  • f Ls in the source, and the number of ⌈s corresponds to the

number of Ls in the target.

University of Oxford—Ralf Hinze 25-39

slide-26
SLIDE 26

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

slide-27
SLIDE 27

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. r = ⌊e⌋ : I → R j = ⌊⌈⌈d⌉⌉⌋ : RR → R e = ⌊r⌋◦ : L → I d = ⌈⌈⌊j⌋◦⌉◦⌉◦ : L → LL

University of Oxford—Ralf Hinze 27-39

slide-28
SLIDE 28

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

  • perations

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

slide-29
SLIDE 29

Monads from Comonads—Comonads from monads WG 2.8 Marble Falls

A × X → B ∼ = A → BX

University of Oxford—Ralf Hinze 29-39

slide-30
SLIDE 30

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

  • peration:

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

slide-31
SLIDE 31

Monads from Comonads—Comonads from monads WG 2.8 Marble Falls

4 Proof

We have to show that the comonadic laws imply the monadic laws and vice versa. (It is sufficient to concentrate on natural transformations of type Lm → Ln and Rn → Rm.) The transformers enjoy functorial properties: ⌊⌈idLn⌉n⌋n = idRn ⌈⌊β · α⌋k⌉n = ⌈⌊α⌋k⌉m · ⌈⌊β⌋m⌉n ⌊⌈Lα⌉n+1⌋m+1 = ⌊⌈α⌉n⌋mR For reference, we call the last one flip law.

University of Oxford—Ralf Hinze 31-39

slide-32
SLIDE 32

Monads from Comonads—Comonads from monads WG 2.8 Marble Falls

The first comonadic unit law is equivalent to the first monadic unit law: Le · d = idL ⇐ ⇒ { inverses } ⌊⌈Le · d⌉⌋ = ⌊⌈idL⌉⌋ ⇐ ⇒ { preservation of composition and identity } ⌊⌈⌈d⌉⌉⌋ · ⌊⌊⌈Le⌉⌋⌋ = idR ⇐ ⇒ { flip law } ⌊⌈⌈d⌉⌉⌋ · ⌊e⌋R = idR ⇐ ⇒ { definition of j and definition of r } j · rR = idR

University of Oxford—Ralf Hinze 32-39

slide-33
SLIDE 33

Monads from Comonads—The wrong way round WG 2.8 Marble Falls

5 The wrong way round

  • Does the translation also work if the left adjoint is

simultaneously a monad?

  • The transformers happily take the monadic operations to

comonadic ones.

  • However, monadic programs of type A → L B are not in
  • ne-to-one correspondence to comonadic programs of type

R A → B.

University of Oxford—Ralf Hinze 33-39

slide-34
SLIDE 34

Monads from Comonads—The wrong way round WG 2.8 Marble Falls

If X is a monoid with operations [ ] : X and (+ +) : X × X → X , then L = − × X also has the structure of a monad. r a = (a, [ ]) j ((a, x1), x2) = (a, x1 + + x2) (For simplicity, we assume that we are working in Set.) This instance is known as the “write to a monoid” monad or simply the writer monad.

University of Oxford—Ralf Hinze 34-39

slide-35
SLIDE 35

Monads from Comonads—The wrong way round WG 2.8 Marble Falls

Its right adjoint R = (−)X is indeed a comonad, lovingly called the “read from a monoid” comonad. e f = f [ ] d f = λ x1 . λ x2 . f (x1 + + x2)

University of Oxford—Ralf Hinze 35-39

slide-36
SLIDE 36

Monads from Comonads—The wrong way round WG 2.8 Marble Falls

However, we cannot translate the accompanying infrastructure

  • f the writer monad. Consider the write operation.

write : X → L X write x = (x, x) The L is on the wrong side of the arrow, write is not natural, so it has no counterpart in the comonadic world.

University of Oxford—Ralf Hinze 36-39

slide-37
SLIDE 37

Monads from Comonads—Epilogue WG 2.8 Marble Falls

6 Summary

  • Monads: effectful computations.
  • Comonads: computations in context.
  • Adjunctions: a theory of program transformations.

C (L A, B) ∼ = D(A, R B) If L and R are endofunctors: Lm → Ln ∼ = Rn → Rm

  • If L is a comonad, then R is a monad. Furthermore,

comonadic programs are in one-to-one correspondence to monadic programs: L A → B ∼ = A → R B.

  • If L is a monad, then R is a comonad. However, monadic

programs are not in one-to-one correspondence to comonadic programs: A → L B ∼ = R A → B.

University of Oxford—Ralf Hinze 37-39

slide-38
SLIDE 38

Monads from Comonads—Epilogue WG 2.8 Marble Falls

6 Summary continued

The curry adjunction C ≺− × X ⊥ (−)X ≻ C Λ : C (A × X , B) ∼ = C (A, BX ) explains:

  • state monad,
  • costate comonad,
  • product comonad,
  • reader monad,
  • “write to a monoid” monad or writer monad,
  • “read from a monoid” comonad.

University of Oxford—Ralf Hinze 38-39

slide-39
SLIDE 39

Monads from Comonads—Appendix WG 2.8 Marble Falls

7 Post- and pre-composition

Functors can be composed, written simply using juxtaposition

  • KF. The operation K−, post-composing a functor K, is itself

functorial: K− : DC → E C (K−) F = KF (K−) α = Kα where (Kα) A = K (α A). Post-composition dualizes to pre-composition: −E : DC → DB (−E) F = FE (−E) α = αE where (αE) A = α (E A).

University of Oxford—Ralf Hinze 39-39