generic programming with adjunctions
play

Generic Programming with Adjunctions Ralf Hinze Computing - PowerPoint PPT Presentation

Generic Programming with Adjunctions 0.0 Generic Programming with Adjunctions Ralf Hinze Computing Laboratory, University of Oxford Wolfson Building, Parks Road, Oxford, OX1 3QD, England ralf.hinze@comlab.ox.ac.uk


  1. Generic Programming with Adjunctions Constructions on categories 2.2 2.2 Outl , Outr and ∆ • projection functors: Outl : C × D → C ; Outr : C × D → D ; Outl � A , B � = A ; Outr � A , B � = B ; Outl � f , g � = f ; Outr � f , g � = g . • diagonal functor: ∆ : C → C × C ; ∆ A = � A , A � ; ∆ f = � f , f � . University of Oxford — Ralf Hinze 31-172

  2. Generic Programming with Adjunctions Constructions on categories 2.2 2.2 The hom-functor • C ( − , = ) : C op × C → Set , • action on objects: C ( − , = ) � A , B � = C ( A , B ) , • action on arrows: C ( − , = ) � f , g � = λ h . g · h · f , • shorthand: C ( f , g ) h = g · h · f . University of Oxford — Ralf Hinze 32-172

  3. Generic Programming with Adjunctions Initial and final objects 2.3 2.3 Initial object The object 0 is initial if for each object B ∈ C there is exactly one arrow from 0 to B , denoted ¡ B (pronounce “gnab”). ¡ B 0 ≻ B University of Oxford — Ralf Hinze 33-172

  4. Generic Programming with Adjunctions Initial and final objects 2.3 2.3 Final object Dually, 1 is a final object if for each object A ∈ C there is a unique arrow from A to 1, denoted ! A (pronounce “bang”). ! A A ≻ 1 University of Oxford — Ralf Hinze 34-172

  5. Generic Programming with Adjunctions Products 2.4 2.4 Product The product of two objects B 1 and B 2 consists of • an object written B 1 × B 2 , • a pair of arrows outl : B 1 × B 2 → B 1 and outr : B 1 × B 2 → B 2 , and satisfies the following universal property : • for each object A , • for each pair of arrows f 1 : A → B 1 and f 2 : A → B 2 , • there exists an arrow f 1 △ f 2 : A → B 1 × B 2 such that f 1 = outl · g ∧ f 2 = outr · g ⇐ ⇒ f 1 △ f 2 = g , for all g : A → B 1 × B 2 . University of Oxford — Ralf Hinze 35-172

  6. Generic Programming with Adjunctions Products 2.4 2.4 Product A . . . . . f 2 f 1 f 1 △ f 2 . . . . . ⋎ ≺ ≻ B 1 ≺ outl B 1 × B 2 outr ≻ B 2 University of Oxford — Ralf Hinze 36-172

  7. Generic Programming with Adjunctions Products 2.4 2.4 Laws • computation laws : f 1 outl · ( f 1 △ f 2 ) ; = f 2 = outr · ( f 1 △ f 2 ), • reflection law : outl △ outr = id A × B . University of Oxford — Ralf Hinze 37-172

  8. Generic Programming with Adjunctions Products 2.4 2.4 Laws • fusion law : ( f 1 △ f 2 ) · h = f 1 · h △ f 2 · h , • action of × on arrows: f 1 × f 2 = f 1 · outl △ f 2 · outr , • functor fusion law : ( k 1 × k 2 ) · ( f 1 △ f 2 ) k 1 · f 1 △ k 2 · f 2 , = • outl and outr are natural transformations: k 1 · outl = outl · ( k 1 × k 2 ) ; k 2 · outr = outr · ( k 1 × k 2 ). University of Oxford — Ralf Hinze 38-172

  9. Generic Programming with Adjunctions Products 2.4 2.4 Proof of functor fusion ( k 1 × k 2 ) · ( f 1 △ f 2 ) = { definition of × } ( k 1 · outl △ k 2 · outr ) · ( f 1 △ f 2 ) = { fusion } k 1 · outl · ( f 1 △ f 2 ) △ k 2 · outr · ( f 1 △ f 2 ) { computation } = k 1 · f 1 △ k 2 · f 2 University of Oxford — Ralf Hinze 39-172

  10. Generic Programming with Adjunctions Products 2.4 2.4 Naturality • fusion and functor fusion: ( △ ) : ∀ A B . ( C × C )( ∆ A , B ) → C ( A , × B ), • naturality of outl and outr : outl : ∀ B . C ( × B , Outl B ) ; outr : ∀ B . C ( × B , Outr B ), or more succinctly � outl , outr � : ∀ B . ( C × C )( ∆ ( × B ), B ). University of Oxford — Ralf Hinze 40-172

  11. Generic Programming with Adjunctions Adjunctions 2.5 2.5 Adjunction L C ≺ ⊥ ≻ D R φ : ∀ A B . C ( L A , B ) ≅ D ( A , R B ) University of Oxford — Ralf Hinze 41-172

  12. Generic Programming with Adjunctions Adjunctions 2.5 2.5 Adjoints, adjuncts and units • left and right adjoints: φ ◦ (η · g ), L g = R f = φ ( f · ǫ), • left and right adjuncts: φ ◦ g = ǫ · L g , φ f = R f · η, • counit and unit: φ ◦ id , ǫ = η = φ id . University of Oxford — Ralf Hinze 42-172

  13. Generic Programming with Adjunctions Adjunctions 2.5 2.5 Adjoints of the diagonal functor f = � outl , outr � · ∆ g ⇐ ⇒ △ f = g + ∆ C ≺ ≻ C × C ≺ ⊥ ⊥ ≻ C ∆ × f = ▽ g ⇐ ⇒ ∆ f · � inl , inr � = g University of Oxford — Ralf Hinze 43-172

  14. Generic Programming with Adjunctions Adjunctions 2.5 2.5 Left adjoint of the forgetful functor Mon ≺ List ⊥ ≻ Set U University of Oxford — Ralf Hinze 44-172

  15. Generic Programming with Adjunctions Adjunctions 2.5 φ ◦ introduction / elimination φ elimination / introduction Universal property f = φ ◦ g ⇐ ⇒ φ f = g ǫ : C ( L ( R B ), B ) η : D ( A , R ( L A )) ǫ = φ ◦ id φ id = η — / computation law computation law / — η -rule / β -rule β -rule / η -rule f = φ ◦ (φ f ) φ (φ ◦ g ) = g reflection law / — — / reflection law simple η -rule / simple β -rule simple β -rule / simple η -rule id = φ ◦ η φ ǫ = id University of Oxford — Ralf Hinze 45-172

  16. Generic Programming with Adjunctions Adjunctions 2.5 φ ◦ introduction / elimination φ elimination / introduction Universal property f = φ ◦ g ⇐ ⇒ φ f = g functor fusion law / — — / fusion law φ ◦ is natural in A φ is natural in A φ ◦ g · L h = φ ◦ ( g · h ) φ f · h = φ ( f · L h ) fusion law / — — / functor fusion law φ ◦ is natural in B φ is natural in B k · φ ◦ g = φ ◦ ( R k · g ) R k · φ f = φ ( k · f ) ǫ is natural in B η is natural in A k · ǫ = ǫ · L ( R k ) R ( L h ) · η = η · h University of Oxford — Ralf Hinze 46-172

  17. Generic Programming with Adjunctions Yoneda lemma 2.6 2.6 Yoneda lemma Let H : C → Set be a functor, and let B ∈ C be an object. H B ≅ C ( B , − ) ˙ → H The functions witnessing the isomorphism are φ s = λ κ . H κ s , φ ◦ α = α B id B . NB Continuation-passing style is a special case: H = C ( A , − ) . University of Oxford — Ralf Hinze 47-172

  18. Generic Programming with Adjunctions 3.0 Part 3 Adjoint folds and unfolds University of Oxford — Ralf Hinze 48-172

  19. Generic Programming with Adjunctions 3.0 3.0 Outline 11. Semantics of datatypes 12. Mendler-style folds and unfolds 13. Adjoint folds and unfolds University of Oxford — Ralf Hinze 49-172

  20. Generic Programming with Adjunctions Semantics of datatypes 3.1 3.1 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 50-172

  21. Generic Programming with Adjunctions Semantics of datatypes 3.1 3.1 Fixed-point equations • both Stack and total are given by recursion equations, • meaning of x = Ψ x ? • a solves the equation iff a is a fixed point of Ψ , • Ψ is called the base function. University of Oxford — Ralf Hinze 51-172

  22. Generic Programming with Adjunctions Semantics of datatypes 3.1 3.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 52-172

  23. Generic Programming with Adjunctions Semantics of datatypes 3.1 3.1 Speaking categorically • functor: Stack A = 1 + Nat × A , • a Stack -algebra: total : Stack Nat → Nat total ( Empty ) = 0 total ( Push ( n , s )) = n + s • total = zero ▽ plus , • Stack -algebra: � Nat , total � . University of Oxford — Ralf Hinze 53-172

  24. Generic Programming with Adjunctions Semantics of datatypes 3.1 3.1 The category of F -algebras Alg ( F ) • let F : C → C be an endofunctor, • objects: � A , a � with A ∈ C and a ∈ C ( F A , A ) , • arrows: F -homomorphisms, h : � A , a � → � B , b � if h ∈ C ( A , B ) such that h · a = b · F h , F h ≻ F B F A F A F B a a b b ⋎ ⋎ ⋎ ⋎ A B A ≻ B h • identity: id A : � A , a � → � A , a � , • composition: in C . University of Oxford — Ralf Hinze 54-172

  25. Generic Programming with Adjunctions Semantics of datatypes 3.1 3.1 The category of F -coalgebras Coalg ( F ) • let F : C → C be an endofunctor, • objects: � A , a � with A ∈ C and a ∈ C ( A , F A ) , • arrows: F -homomorphisms, h : � A , a � → � B , b � if h ∈ C ( A , B ) such that F h · a = b · h , h A ≻ B A B a a b b ⋎ ⋎ ⋎ ⋎ F A F B F A F h ≻ F B • identity: id A : � A , a � → � A , a � , • composition: in C . University of Oxford — Ralf Hinze 55-172

  26. Generic Programming with Adjunctions Semantics of datatypes 3.1 3.1 Fixed points of functors • initial object in Alg ( F ) : initial F -algebra � µ F , in � , • µ F is the least fixed point of F , • in : F (µ F ) ≅ µ F , • final object in Coalg ( F ) : final F -coalgebra � ν F , out � , • ν F is the greatest fixed point of F , • out : ν F ≅ F (ν F ) . University of Oxford — Ralf Hinze 56-172

  27. Generic Programming with Adjunctions Semantics of datatypes 3.1 3.1 Coq: inductive and coinductive types Inductive Nat : Type : = | Zero : Nat | Succ : Nat → Nat . Inductive Stack : Type : = | Empty : Stack | Push : Nat → Stack → Stack . CoInductive Stream : Type : = | Cons : Nat → Stream → Stream . University of Oxford — Ralf Hinze 57-172

  28. Generic Programming with Adjunctions Mendler-style folds and unfolds 3.2 3.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 58-172

  29. Generic Programming with Adjunctions Mendler-style folds and unfolds 3.2 3.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 59-172

  30. Generic Programming with Adjunctions Mendler-style folds and unfolds 3.2 3.2 . . . 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 60-172

  31. Generic Programming with Adjunctions Mendler-style folds and unfolds 3.2 3.2 Example: from data Sequ = Next ( Nat , Sequ ) from : Nat → Sequ from n = Next ( n , from ( n + 1 )) University of Oxford — Ralf Hinze 61-172

  32. Generic Programming with Adjunctions Mendler-style folds and unfolds 3.2 3.2 Two-level types and functions data Sequ sequ = Next ( Nat , sequ ) from : ∀ x . ( Nat → x ) → ( Nat → Sequ x ) from from n = Next ( n , from ( n + 1 )) from : Nat → ν Sequ = Out ◦ ( from from n ) . from n University of Oxford — Ralf Hinze 62-172

  33. Generic Programming with Adjunctions Mendler-style folds and unfolds 3.2 3.2 Initial fixed-point equations An initial fixed-point equation in the unknown x ∈ C (µ F , A ) has the syntactic form x · in = Ψ x , where the base function Ψ has type Ψ : ∀ X . C ( X , A ) → C ( F X , A ) . The naturality of Ψ ensures termination . University of Oxford — Ralf Hinze 63-172

  34. Generic Programming with Adjunctions Mendler-style folds and unfolds 3.2 3.2 Guarded by destructors Ψ x · in ◦ x = x ∈ C (µ F , A ) Ψ : ∀ X . C ( X , A ) → C ( F X , A ) in ◦ Ψ x µ F ≻ F (µ F ) ≻ A University of Oxford — Ralf Hinze 64-172

  35. Generic Programming with Adjunctions Mendler-style folds and unfolds 3.2 3.2 Mendler-style folds x = � Ψ � Id ⇐ ⇒ x · in = Ψ x University of Oxford — Ralf Hinze 65-172

  36. Generic Programming with Adjunctions Mendler-style folds and unfolds 3.2 3.2 Proof of uniqueness φ : C ( F A , A ) ≅ ( ∀ X . C ( X , A ) → C ( F X , A )) x · in = Ψ x ⇐ ⇒ { isomorphism } x · in = φ (φ ◦ Ψ ) x { definition of φ ◦ : φ ◦ Ψ = Ψ id } ⇐ ⇒ x · in = φ ( Ψ id ) x { definition of φ : φ f = λ κ . f · F κ } ⇐ ⇒ x · in = Ψ id · F x ⇐ ⇒ { initial algebras } x = � Ψ id � University of Oxford — Ralf Hinze 66-172

  37. Generic Programming with Adjunctions Mendler-style folds and unfolds 3.2 3.2 Final fixed-point equations A final fixed-point equation in the unknown x ∈ C ( A , ν F ) has the syntactic form out · x = Ψ x , where the base function Ψ has type Ψ : ∀ X . C ( A , X ) → C ( A , F X ) . The naturality of Ψ ensures productivity . University of Oxford — Ralf Hinze 67-172

  38. Generic Programming with Adjunctions Mendler-style folds and unfolds 3.2 3.2 Guarded by constructors out ◦ · Ψ x x = x ∈ C ( A , ν F ) Ψ : ∀ X . C ( A , X ) → C ( A , F X ) ≻ F (ν F ) out ◦ Ψ x A ≻ ν F University of Oxford — Ralf Hinze 68-172

  39. Generic Programming with Adjunctions Mendler-style folds and unfolds 3.2 3.2 Mendler-style unfolds x = [ ( Ψ ) ] Id ⇐ ⇒ out · x = Ψ x University of Oxford — Ralf Hinze 69-172

  40. Generic Programming with Adjunctions Mendler-style folds and unfolds 3.2 3.2 Mutual type recursion data Tree = Node Nat Trees data Trees = Nil | Cons ( Tree , Trees ) flattena : Tree → Stack flattena ( Node n ts ) = Push ( n , flattens ts ) flattens : Trees → Stack flattens ( Nil ) = Empty flattens ( Cons ( t , ts )) = stack ( flattena t , flattens ts ) University of Oxford — Ralf Hinze 70-172

  41. Generic Programming with Adjunctions Mendler-style folds and unfolds 3.2 3.2 Speaking categorically Idea: view Tree and Trees as a fixed point in a product category . T � A , B � = � Nat × B , 1 + A × B � flatten ∈ ( C × C )(µ T , � Stack , Stack � ) University of Oxford — Ralf Hinze 71-172

  42. Generic Programming with Adjunctions Mendler-style folds and unfolds 3.2 3.2 Specialising fixed-point equations An equation in C × D corresponds to two equations, one in C and one in D . x · in = Ψ x ⇐ ⇒ x 1 · in 1 = Ψ 1 � x 1 , x 2 � and x 2 · in 2 = Ψ 2 � x 1 , x 2 � Here, x 1 = Outl x , x 2 = Outr x , in 1 = Outl in , in 2 = Outr in , Ψ 1 = Outl · Ψ and Ψ 2 = Outr · Ψ . University of Oxford — Ralf Hinze 72-172

  43. Generic Programming with Adjunctions Mendler-style folds and unfolds 3.2 3.2 Parametric datatypes data Perfect a = Zero a | Succ ( Perfect ( a , a )) size : ∀ a . Perfect a → Nat size ( Zero a ) = 1 size ( Succ p ) = 2 ∗ size p University of Oxford — Ralf Hinze 73-172

  44. Generic Programming with Adjunctions Mendler-style folds and unfolds 3.2 3.2 Speaking categorically Idea: view Perfect as a fixed point in a functor category . P F = Λ A . A + F ( A × A ) The second-order functor F sends a functor to a functor. size : µ P ˙ → K Nat NB K : D → D C is the constant functor K A = Λ B . A . University of Oxford — Ralf Hinze 74-172

  45. Generic Programming with Adjunctions Mendler-style folds and unfolds 3.2 3.2 Specialising fixed-point equations x · in = Ψ x ⇐ ⇒ x A · in A = Ψ x A NB Type application and abstraction are invisible in Haskell. University of Oxford — Ralf Hinze 75-172

  46. Generic Programming with Adjunctions Mendler-style folds and unfolds 3.2 initial fixed-point equation final fixed-point equation x · in = Ψ x out · x = Ψ x inductive type coinductive type Set standard fold standard unfold continuous coalgebra Cpo — continuous unfold continuous algebra continuous coalgebra Cpo ⊥ strict continuous fold strict continuous unfold ( µ F ≅ ν F ) mutually rec. ind. types mutually rec. coind. types C × D mutually rec. folds mutually rec. unfolds inductive type functor coinductive type functor D C higher-order fold higher-order unfold University of Oxford — Ralf Hinze 76-172

  47. Generic Programming with Adjunctions Adjoint folds and unfolds 3.3 3.3 Counterexample: 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 77-172

  48. Generic Programming with Adjunctions Adjoint folds and unfolds 3.3 3.3 Counterexample: nats and squares nats : Nat → ν Sequ = Out ◦ ( Next ( n , squares n )) nats n squares : Nat → ν Sequ = Out ◦ ( Next ( n ∗ n , nats ( n + 1 ))) squares n University of Oxford — Ralf Hinze 78-172

  49. Generic Programming with Adjunctions Adjoint folds and unfolds 3.3 3.3 Adjoint fixed-point equations Idea: model the context by a functor. x · L in = Ψ x R out · x = Ψ x Requirement: the functors have to be adjoint: L ⊣ R . University of Oxford — Ralf Hinze 79-172

  50. Generic Programming with Adjunctions Adjoint folds and unfolds 3.3 3.3 Adjoint initial fixed-point equations An adjoint initial fixed-point equation in the unknown x ∈ C ( L (µ F ), A ) has the syntactic form x · L in = Ψ x , where the base function Ψ has type Ψ : ∀ X : D . C ( L X , A ) → C ( L ( F X ), A ) . The unique solution is called an adjoint fold . Furthermore, φ x is called the transposed fold . University of Oxford — Ralf Hinze 80-172

  51. Generic Programming with Adjunctions Adjoint folds and unfolds 3.3 3.3 Proof of uniqueness x · L in = Ψ x ⇐ ⇒ { adjunction } φ ( x · L in ) = φ ( Ψ x ) ⇐ ⇒ { naturality of φ : φ 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 81-172

  52. Generic Programming with Adjunctions Adjoint folds and unfolds 3.3 3.3 Adjoint folds x = � Ψ � L ⇐ ⇒ x · L in = Ψ x University of Oxford — Ralf Hinze 82-172

  53. Generic Programming with Adjunctions Adjoint folds and unfolds 3.3 3.3 Banana-split law = � λ x . Φ ( outl · x ) △ Ψ ( outr · x ) � L � Φ � L △ � Ψ � L University of Oxford — Ralf Hinze 83-172

  54. Generic Programming with Adjunctions Adjoint folds and unfolds 3.3 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 84-172

  55. Generic Programming with Adjunctions Adjoint folds and unfolds 3.3 3.3 Adjoint final fixed-point equations An adjoint final fixed-point equation in the unknown x ∈ D ( A , R (ν F )) has the syntactic form R out · x = Ψ x , where the base function Ψ has type Ψ : ∀ X : C . D ( A , R X ) → D ( A , R ( F X )) . The unique solution is called an adjoint unfold . University of Oxford — Ralf Hinze 85-172

  56. Generic Programming with Adjunctions Adjoint folds and unfolds 3.3 3.3 Adjoint unfolds x = [ ( Ψ ) ] R ⇐ ⇒ R out · x = Ψ x University of Oxford — Ralf Hinze 86-172

  57. Generic Programming with Adjunctions 4.0 Part 4 Adjunctions University of Oxford — Ralf Hinze 87-172

  58. Generic Programming with Adjunctions 4.0 4.0 Outline 14. Identity 15. Currying 16. Mutual Value Recursion 17. Type Application 18. Type Composition University of Oxford — Ralf Hinze 88-172

  59. Generic Programming with Adjunctions Identity 4.1 4.1 Recall: Adjoint fixed-point equations x · L in = Ψ x R out · x = Ψ x Requirement: the functors have to be adjoint: L ⊣ R . University of Oxford — Ralf Hinze 89-172

  60. Generic Programming with Adjunctions Identity 4.1 4.1 Identity Id C ≺ ⊥ ≻ C Id φ : ∀ A B . C ( Id A , B ) ≅ C ( A , Id B ) Adjoint fixed-point equations subsume Mendler-style ones. University of Oxford — Ralf Hinze 90-172

  61. Generic Programming with Adjunctions Currying 4.2 4.2 Recall: stack stack : (µ Stack , 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 L f = f × id Stack . University of Oxford — Ralf Hinze 91-172

  62. Generic Programming with Adjunctions Currying 4.2 4.2 Currying C ≺ − × X ⊥ ≻ C ( − ) X φ : ∀ A B . C ( A × X , B ) ≅ C ( A , B X ) University of Oxford — Ralf Hinze 92-172

  63. Generic Programming with Adjunctions Currying 4.2 4.2 Specialising adjoint equations x · L in = Ψ x R out · x = Ψ x ⇐ ⇒ { definition of L } { definition of R } ⇐ ⇒ x · ( in × id ) = Ψ x ( out · ) · x = Ψ x ⇐ ⇒ { pointwise } ⇐ ⇒ { pointwise } x ( in a , c ) = Ψ x ( a , c ) out ( x a c ) = Ψ x a c University of Oxford — Ralf Hinze 93-172

  64. Generic Programming with Adjunctions Currying 4.2 4.2 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 94-172

  65. Generic Programming with Adjunctions Currying 4.2 4.2 The transpose of stack R A = A Stack R f = f id Stack 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 95-172

  66. Generic Programming with Adjunctions Currying 4.2 4.2 Recall: 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 96-172

  67. Generic Programming with Adjunctions Currying 4.2 4.2 Two-level types data LIST list a = Nil | Cons ( a , list a ) instance ( Functor list ) ⇒ Functor ( LIST list ) where fmap f ( Nil ) = Nil fmap f ( Cons ( a , as )) = Cons ( f a , fmap f as ) append : ∀ a . (µ LIST a , List a ) → List a append ( In ( Nil ), bs ) = bs append ( In ( Cons ( a , as )), bs ) = In ( Cons ( a , append ( as , bs ))) University of Oxford — Ralf Hinze 97-172

  68. Generic Programming with Adjunctions Currying 4.2 4.2 append as a natural transformation Definining ( F ˙ × G ) A = F A × G A , we can view append as a natural transformation: append : List ˙ × List ˙ → List . We have to find the right adjoint of the lifted product − ˙ × H . University of Oxford — Ralf Hinze 98-172

  69. Generic Programming with Adjunctions Currying 4.2 4.2 Deriving the right adjoint G H A ≅ { Yoneda lemma } → G H C ( A , − ) ˙ × H ⊣ − H } ≅ { requirement: − ˙ C ( A , − ) ˙ × H ˙ → G ≅ { natural transformation } ∀ X : C . C ( A , X ) × H X → G X { − × X ⊣ − X } ≅ ∀ X : C . C ( A , X ) → ( G X ) H X . NB We assume that the functor category is Set C so G H : C → Set . University of Oxford — Ralf Hinze 99-172

  70. Generic Programming with Adjunctions Currying 4.2 4.2 The transpose of append append ′ : List ˙ → List List In Haskell: append ′ : ∀ a . List a → ∀ x . ( a → x ) → ( List x → List x ) append ′ as = λ f → λ bs → append ( fmap f as , bs ). NB append ′ combines append with fmap . University of Oxford — Ralf Hinze 100-172

Recommend


More recommend