adjoint folds and unfolds
play

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


  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

  2. Adjoint Folds and Unfolds — Prologue MPC 2010 Section 1 Prologue University of Oxford — Ralf Hinze 2-48

  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

  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

  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

  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

  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

  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

  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 odd : Stack → Bool odd ( Empty ) = False odd ( Push ( 0 , x )) = odd x odd ( Push ( 1 , x )) = even x University of Oxford — Ralf Hinze 9-48

  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

  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

  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

  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

  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

  15. Adjoint Folds and Unfolds — Mendler-style folds MPC 2010 Section 2 Mendler-style folds University of Oxford — Ralf Hinze 15-48

  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

  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

  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

  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

  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

  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

  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

  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

  24. Adjoint Folds and Unfolds — Adjoint folds MPC 2010 Section 3 Adjoint folds University of Oxford — Ralf Hinze 24-48

  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

  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

  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

  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

  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

  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

  31. Adjoint Folds and Unfolds — Adjoint folds MPC 2010 3.3 Banana-split law = � λ x . Φ ( outl · x ) △ Ψ ( outr · x ) � L � Φ � L △ � Ψ � L University of Oxford — Ralf Hinze 31-48

  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

  33. Adjoint Folds and Unfolds — Adjunctions MPC 2010 Section 4 Adjunctions University of Oxford — Ralf Hinze 33-48

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend