Adjoint Folds and Unfolds Or: Scything Through the Thicket of - - PowerPoint PPT Presentation

adjoint folds and unfolds
SMART_READER_LITE
LIVE PREVIEW

Adjoint Folds and Unfolds Or: Scything Through the Thicket of - - PowerPoint PPT Presentation

Adjoint Folds and Unfolds Prologue MPC 2010 Adjoint Folds and Unfolds Or: Scything Through the Thicket of Morphisms Ralf Hinze Computing Laboratory, University of Oxford Wolfson Building, Parks Road, Oxford, OX1 3QD, England


slide-1
SLIDE 1

Adjoint Folds and Unfolds — Prologue MPC 2010

Adjoint Folds and Unfolds

Or: Scything Through the Thicket of Morphisms 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/

June 2010

University of Oxford — Ralf Hinze 1-48

slide-2
SLIDE 2

Adjoint Folds and Unfolds — Prologue MPC 2010

Section 1 Prologue

University of Oxford — Ralf Hinze 2-48

slide-3
SLIDE 3

Adjoint Folds and Unfolds — Prologue MPC 2010

  • Mathematics of Program Construction,

375th Anniversary of the Groningen University, International Conference Groningen, The Netherlands, June 26–30 1989. LNCS 375.

  • Mike Spivey, A categorical approach to the theory of lists.
  • Grant Malcolm, Homomorphisms and promotability.

University of Oxford — Ralf Hinze 3-48

slide-4
SLIDE 4

Adjoint Folds and Unfolds — Prologue MPC 2010

1.1 Catamorphism

f = b ⇐ ⇒ f · in = b · F f

University of Oxford — Ralf Hinze 4-48

slide-5
SLIDE 5

Adjoint Folds and Unfolds — Prologue MPC 2010

1.1 Banana-split law

h △ k = h · F outl △ k · F outr

University of Oxford — Ralf Hinze 5-48

slide-6
SLIDE 6

Adjoint Folds and Unfolds — Prologue MPC 2010

1.1 Proof of banana-split law

(h △ k) · in = { split-fusion } h · in △ k · in = { fold-computation } h · F h △ k · F k = { split-computation } h · F (outl · (h △ k)) △ k · F (outr · (h △ k)) = { F functor } h · F outl · F (h △ k) △ k · F outr · F (h △ k) = { split-fusion } (h · F outl △ k · F outr) · F (h △ k)

University of Oxford — Ralf Hinze 6-48

slide-7
SLIDE 7

Adjoint Folds and Unfolds — Prologue MPC 2010

1.2 Example: total

data Stack = Empty | Push (Nat, Stack) total : Stack → Nat total (Empty) = 0 total (Push (n, s)) = n + total s

University of Oxford — Ralf Hinze 7-48

slide-8
SLIDE 8

Adjoint Folds and Unfolds — Prologue MPC 2010

1.3 Counterexample: stack

stack : (Stack, Stack) → Stack stack (Empty, bs) = bs stack (Push (a, as), bs) = Push (a, stack (as, bs))

University of Oxford — Ralf Hinze 8-48

slide-9
SLIDE 9

Adjoint Folds and Unfolds — Prologue MPC 2010

1.3 Counterexample: even and odd

even : Stack → Bool even (Empty) = True even (Push (0, x)) = even x even (Push (1, x)) = odd x

  • dd : Stack

→ Bool

  • dd

(Empty) = False

  • dd

(Push (0, x)) = odd x

  • dd

(Push (1, x)) = even x

University of Oxford — Ralf Hinze 9-48

slide-10
SLIDE 10

Adjoint Folds and Unfolds — Prologue MPC 2010

1.3 Counterexamples: fac and fib

data Nat = Zero | Succ Nat fac : Nat → Nat fac (Zero) = 1 fac (Succ n) = Succ n × fac n fib : Nat → Nat fib (Zero) = Zero fib (Succ Zero) = Succ Zero fib (Succ (Succ n)) = fib n + fib (Succ n)

University of Oxford — Ralf Hinze 10-48

slide-11
SLIDE 11

Adjoint Folds and Unfolds — Prologue MPC 2010

1.3 Counterexample: sum

data List a = Nil | Cons (a, List a) sum : List Nat → Nat sum (Nil) = 0 sum (Cons (a, as)) = a + sum as

University of Oxford — Ralf Hinze 11-48

slide-12
SLIDE 12

Adjoint Folds and Unfolds — Prologue MPC 2010

1.3 Counterexample: append

append : ∀ a . (List a, List a) → List a append (Nil, bs) = bs append (Cons (a, as), bs) = Cons (a, append (as, bs))

University of Oxford — Ralf Hinze 12-48

slide-13
SLIDE 13

Adjoint Folds and Unfolds — Prologue MPC 2010

1.3 Counterexample: concat

concat : ∀ a . List (List a) → List a concat (Nil) = Nil concat (Cons (l, ls)) = append (l, concat ls)

University of Oxford — Ralf Hinze 13-48

slide-14
SLIDE 14

Adjoint Folds and Unfolds — Prologue MPC 2010

1.4 Thicket of morphisms

  • Catamorphisms,
  • anamorphism,
  • paramorphism,
  • apomorphisms,
  • mutomorphisms,
  • zygomorphisms,
  • histomorphisms,
  • futomorphisms.

University of Oxford — Ralf Hinze 14-48

slide-15
SLIDE 15

Adjoint Folds and Unfolds — Mendler-style folds MPC 2010

Section 2 Mendler-style folds

University of Oxford — Ralf Hinze 15-48

slide-16
SLIDE 16

Adjoint Folds and Unfolds — Mendler-style folds MPC 2010

2.1 Semantics of recursive datatypes

data Stack = Empty | Push (Nat, Stack)

University of Oxford — Ralf Hinze 16-48

slide-17
SLIDE 17

Adjoint Folds and Unfolds — Mendler-style folds MPC 2010

2.1 Two-level types

Abstracting away from the recursive call. data Stack stack = Empty | Push (Nat, stack) instance Functor Stack where fmap f (Empty) = Empty fmap f (Push (n, s)) = Push (n, f s) Tying the recursive knot. newtype µf = In {in◦ : f (µf )} type Stack = µStack

University of Oxford — Ralf Hinze 17-48

slide-18
SLIDE 18

Adjoint Folds and Unfolds — Mendler-style folds MPC 2010

2.2 Semantics of recursive functions

total : µStack → Nat total (In (Empty)) = 0 total (In (Push (n, s))) = n + total s

University of Oxford — Ralf Hinze 18-48

slide-19
SLIDE 19

Adjoint Folds and Unfolds — Mendler-style folds MPC 2010

2.2 Abstracting away from the recursive call

total : (µStack → Nat) → (µStack → Nat) total total (In (Empty)) = 0 total total (In (Push (n, s))) = n + total s A function of this type has many fixed points.

University of Oxford — Ralf Hinze 19-48

slide-20
SLIDE 20

Adjoint Folds and Unfolds — Mendler-style folds MPC 2010

2.2 . . . and removing In

Abstracting away from the recursive call and removing In. total : ∀ x . (x → Nat) → (Stack x → Nat) total total (Empty) = 0 total total (Push (n, s)) = n + total s A function of this type has a unique fixed point. Tying the recursive knot. total : µStack → Nat total (In l) = total total l

University of Oxford — Ralf Hinze 20-48

slide-21
SLIDE 21

Adjoint Folds and Unfolds — Mendler-style folds MPC 2010

2.3 Initial fixed-point equations

An initial fixed-point equation in the unknown x : ❈(µF, A) has the syntactic form x · in = Ψ x , where the base function Ψ has type Ψ : ∀ X . ❈(X, A) → ❈(F X, A) . The naturality of Ψ ensures that x is uniquely defined.

University of Oxford — Ralf Hinze 21-48

slide-22
SLIDE 22

Adjoint Folds and Unfolds — Mendler-style folds MPC 2010

2.3 Mendler-style folds

x = ΨId ⇐ ⇒ x · in = Ψ x

University of Oxford — Ralf Hinze 22-48

slide-23
SLIDE 23

Adjoint Folds and Unfolds — Mendler-style folds MPC 2010

2.3 Proof of uniqueness

φ : ❈(F A, A) ≅ (∀ X . ❈(X, A) → ❈(F X, A)) x · in = Ψ x ⇐ ⇒ { isomorphism } x · in = φ (φ◦ Ψ) x ⇐ ⇒ { definition of φ◦, that is, φ◦ Ψ = Ψ id } x · in = φ (Ψ id) x ⇐ ⇒ { definition of φ, that is, φ f = λ κ . f · F κ } x · in = Ψ id · F x ⇐ ⇒ { initial algebras } x = Ψ id

University of Oxford — Ralf Hinze 23-48

slide-24
SLIDE 24

Adjoint Folds and Unfolds — Adjoint folds MPC 2010

Section 3 Adjoint folds

University of Oxford — Ralf Hinze 24-48

slide-25
SLIDE 25

Adjoint Folds and Unfolds — Adjoint folds MPC 2010

3.1 Recall stack

stack : (µStack, Stack) → Stack stack (In (Empty), bs) = bs stack (In (Push (a, as)), bs) = In (Push (a, stack (as, bs)))

University of Oxford — Ralf Hinze 25-48

slide-26
SLIDE 26

Adjoint Folds and Unfolds — Adjoint folds MPC 2010

3.2 Adjoint fixed-point equations

Idea: model the context by a functor. x · L in = Ψ x Requirement: the functor has to be left adjoint: L ⊣ R.

University of Oxford — Ralf Hinze 26-48

slide-27
SLIDE 27

Adjoint Folds and Unfolds — Adjoint folds MPC 2010

3.2 Adjunction

❈ ≺ L ⊥ R ≻ ❉ φ : ∀A B . ❈(L A, B) ≅ ❉(A, R B)

University of Oxford — Ralf Hinze 27-48

slide-28
SLIDE 28

Adjoint Folds and Unfolds — Adjoint folds MPC 2010

3.2 Adjoint initial fixed-point equations

An adjoint initial fixed-point equation in the unknown x : ❈(L (µF), A) has the syntactic form x · L in = Ψ x , where the base function Ψ has type Ψ : ∀ X : ❉ . ❈(L X, A) → ❈(L (F X), A) . The unique solution is called an adjoint fold. Furthermore, φ x is called the transposed fold.

University of Oxford — Ralf Hinze 28-48

slide-29
SLIDE 29

Adjoint Folds and Unfolds — Adjoint folds MPC 2010

3.2 Adjoint folds

x = ΨL ⇐ ⇒ x · L in = Ψ x

University of Oxford — Ralf Hinze 29-48

slide-30
SLIDE 30

Adjoint Folds and Unfolds — Adjoint folds MPC 2010

3.2 Proof of uniqueness

x · L in = Ψ x ⇐ ⇒ { adjunction } φ (x · L in) = φ (Ψ x) ⇐ ⇒ { naturality of φ, that is, φ f · h = φ (f · L h) } φ x · in = φ (Ψ x) ⇐ ⇒ { adjunction } φ x · in = (φ · Ψ · φ◦) (φ x) ⇐ ⇒ { universal property of Mendler-style folds } φ x = φ · Ψ · φ◦Id ⇐ ⇒ { adjunction } x = φ◦ φ · Ψ · φ◦Id

University of Oxford — Ralf Hinze 30-48

slide-31
SLIDE 31

Adjoint Folds and Unfolds — Adjoint folds MPC 2010

3.3 Banana-split law

ΦL △ ΨL = λ x . Φ (outl · x) △ Ψ (outr · x)L

University of Oxford — Ralf Hinze 31-48

slide-32
SLIDE 32

Adjoint Folds and Unfolds — Adjoint folds MPC 2010

3.3 Proof of banana-split law

(ΦL △ ΨL) · L in = { split-fusion } ΦL · L in △ ΨL · L in = { fold-computation } Φ ΦL △ Ψ ΨL = { split-computation } Φ (outl · (ΦL △ ΨL)) △ Ψ (outl · (ΦL △ ΨL))

University of Oxford — Ralf Hinze 32-48

slide-33
SLIDE 33

Adjoint Folds and Unfolds — Adjunctions MPC 2010

Section 4 Adjunctions

University of Oxford — Ralf Hinze 33-48

slide-34
SLIDE 34

Adjoint Folds and Unfolds — Adjunctions MPC 2010

4.1 Recall stack

stack : L (µStack) → Stack stack (In (Empty), bs) = bs stack (In (Push (a, as)), bs) = In (Push (a, stack (as, bs))) The type µStack is embedded in a context L: L A = A × Stack.

University of Oxford — Ralf Hinze 34-48

slide-35
SLIDE 35

Adjoint Folds and Unfolds — Adjunctions MPC 2010

4.1 Currying

❈ ≺ − × X ⊥ (−)X ≻ ❈ φ : ∀ A B . ❈(A × X, B) ≅ ❈(A, BX )

University of Oxford — Ralf Hinze 35-48

slide-36
SLIDE 36

Adjoint Folds and Unfolds — Adjunctions MPC 2010

4.1 Specialising adjoint equations

x · L in = Ψ x ⇐ ⇒ { definition of L } x · (in × id) = Ψ x ⇐ ⇒ { pointwise } x (in a, c) = Ψ x (a, c)

University of Oxford — Ralf Hinze 36-48

slide-37
SLIDE 37

Adjoint Folds and Unfolds — Adjunctions MPC 2010

4.1 stack as an adjoint fold

stack : ∀ x . (L x → Stack) → (L (Stack x) → Stack) stack stack (Empty, bs) = bs stack stack (Push (a, as), bs) = In (Push (a, stack (as, bs))) stack : L (µStack) → Stack stack (In as, bs) = stack stack (as, bs)

University of Oxford — Ralf Hinze 37-48

slide-38
SLIDE 38

Adjoint Folds and Unfolds — Adjunctions MPC 2010

4.1 The transpose of stack

R A = AStack The transposed fold is the curried variant of stack. stack : µStack → R Stack stack (In Empty) = λbs → bs stack (In (Push (a, as))) = λbs → In (Push (a, stack as bs))

University of Oxford — Ralf Hinze 38-48

slide-39
SLIDE 39

Adjoint Folds and Unfolds — Adjunctions MPC 2010

4.2 Recall even and odd

even : µStack → Bool even (In Empty) = True even (In (Push (0, x))) = even x even (In (Push (1, x))) = odd x

  • dd : µStack

→ Bool

  • dd

(In Empty) = False

  • dd

(In (Push (0, x))) = odd x

  • dd

(In (Push (1, x))) = even x

University of Oxford — Ralf Hinze 39-48

slide-40
SLIDE 40

Adjoint Folds and Unfolds — Adjunctions MPC 2010

4.2 Speaking categorically

evenOdd : ∆(µStack) → Bool, Bool The type µStack is embedded in a context ∆: ∆ : ❈ → ❈ × ❈ ∆A = A, A ∆f = f , f .

University of Oxford — Ralf Hinze 40-48

slide-41
SLIDE 41

Adjoint Folds and Unfolds — Adjunctions MPC 2010

4.2 Adjoints of the diagonal functor

φ : ∀ A B . ❈((+) A, B) ≅ (❈ × ❈)(A, ∆B) ❈ ≺ + ⊥ ∆ ≻ ❈ × ❈ ≺ ∆ ⊥ × ≻ ❈ φ : ∀ A B . (❈ × ❈)(∆A, B) ≅ ❈(A, (×) B)

University of Oxford — Ralf Hinze 41-48

slide-42
SLIDE 42

Adjoint Folds and Unfolds — Adjunctions MPC 2010

4.2 Specialising adjoint equations

x · ∆in = Ψ x ⇐ ⇒ x1 · in = Ψ1 x1, x2 and x2 · in = Ψ2 x1, x2 Here, x1 = Outl x, x2 = Outr x, Ψ1 = Outl · Ψ and Ψ2 = Outr · Ψ.

University of Oxford — Ralf Hinze 42-48

slide-43
SLIDE 43

Adjoint Folds and Unfolds — Adjunctions MPC 2010

4.2 A special case: paramorphisms

fac : µNat → Nat fac (In (Zero)) = 1 fac (In (Succ n)) = In (Succ (id n)) × fac n id : µNat → Nat id (In (Zero)) = In Zero id (In (Succ n)) = In (Succ (id n))

University of Oxford — Ralf Hinze 43-48

slide-44
SLIDE 44

Adjoint Folds and Unfolds — Adjunctions MPC 2010

4.2 A special case: histomorphisms

fib : µNat → Nat fib (In (Zero)) = 0 fib (In (Succ n)) = fib′ n fib′ : µNat → Nat fib′ (In (Zero)) = 1 fib′ (In (Succ n)) = fib n + fib′ n

University of Oxford — Ralf Hinze 44-48

slide-45
SLIDE 45

Adjoint Folds and Unfolds — Adjunctions MPC 2010

adjunction initial fixed-point equation final fixed-point equation L ⊣ R x · L in = Ψ x R out · x = Ψ x φ x · in = (φ · Ψ · φ◦) (φ x)

  • ut · φ◦ x = (φ◦ · Ψ · φ) (φ◦ x)

Id ⊣ Id standard fold standard unfold standard fold standard unfold (− × X) ⊣ (−X ) parametrised fold curried unfold fold to an exponential unfold from a pair (+) ⊣ ∆ recursion from a coproduct of mutual value recursion mutually recursive types mutual value recursion on single recursion from a mutually recursive types coproduct domain ∆ ⊣ (×) mutual value recursion recursion to a product of mutually recursive types single recursion to a mutual value recursion on product domain mutually recursive types LshX ⊣ (− X) — monomorphic unfold unfold from a left shift (− X) ⊣ RshX monomorphic fold — fold to a right shift LanJ ⊣ (− ◦ J) — polymorphic unfold unfold from a left Kan extension (− ◦ J) ⊣ RanJ polymorphic fold — fold to a right Kan extension

University of Oxford — Ralf Hinze 45-48

slide-46
SLIDE 46

Adjoint Folds and Unfolds — Epilogue MPC 2010

Section 5 Epilogue

University of Oxford — Ralf Hinze 46-48

slide-47
SLIDE 47

Adjoint Folds and Unfolds — Epilogue MPC 2010

5.1 Summary and related work

Summary:

  • Adjoint (un-) folds capture many recursion schemes:
  • catamorphisms and anamorphism,
  • folds with parameter,
  • paramorphism and apomorphisms,
  • mutomorphisms,
  • zygomorphisms,
  • (histomorphisms and futomorphisms).
  • Adjunctions play a central role.

Related work:

  • R. Bird, R. Paterson: “Generalised folds for nested

datatypes”.

University of Oxford — Ralf Hinze 47-48

slide-48
SLIDE 48

Adjoint Folds and Unfolds — Epilogue MPC 2010

5.2 Limitations

  • Simultaneous recursion does not fit under the umbrella.

zip : (List a, List b) → List (a, b) zip (Nil, bs) = Nil zip (as, Nil) = Nil zip (Cons (a, as), Cons (b, bs)) = Cons ((a, b), zip (as, bs))

  • However, one can establish

x = Ψ× ⇐ ⇒ x · (×) in = Ψ x using a different technique: colimits. See, R. Bird, R. Paterson: “Generalised folds for nested datatypes”.

University of Oxford — Ralf Hinze 48-48