Type Fusion — Prologue AMAST 2010 Type Fusion 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-30
Type Fusion — Prologue AMAST 2010 Section 1 Prologue University of Oxford — Ralf Hinze 2-30
Type Fusion — Prologue AMAST 2010 1.1 Memoisation Say, you want to memoise the function f : Nat → V so that it caches previously computed values. University of Oxford — Ralf Hinze 3-30
Type Fusion — Prologue AMAST 2010 1.1 Table : interface Given the interface data Table v lookup : ∀ v . Table v → ( Nat → v ) tabulate : ∀ v . ( Nat → v ) → Table v , we can memoize f as follows memo-f : Nat → V memo-f = lookup ( tabulate f ). University of Oxford — Ralf Hinze 4-30
Type Fusion — Prologue AMAST 2010 1.1 Table : implementation data Nat = Zero | Succ Nat data Table v = Node { zero : v , succ : Table v } lookup ( Node { zero = t } ) Zero = t lookup ( Node { succ = t } ) ( Succ n ) = lookup t n tabulate f = Node { zero = f Zero , succ = tabulate (λ n → f ( Succ n )) } University of Oxford — Ralf Hinze 5-30
Type Fusion — Prologue AMAST 2010 1.1 Correctness tabulate : V Nat ≅ Table V : lookup University of Oxford — Ralf Hinze 6-30
Type Fusion — Prologue AMAST 2010 1.2 Type firstification The first-order datatype data Stack = Empty | Push ( Nat , Stack ) is an instance of the second-order datatype data List a = Nil | Cons ( a , List a ). University of Oxford — Ralf Hinze 7-30
Type Fusion — Prologue AMAST 2010 1.2 Correctness Λ - drop : List Nat ≅ Stack : Λ - lift University of Oxford — Ralf Hinze 8-30
Type Fusion — Prologue AMAST 2010 1.3 Type specialisation Lists of optional values, List ( Maybe A ) with data Maybe a = Nothing | Just a , can be represented more compactly using the tailor-made data Sequ a = Done | Skip ( Sequ a ) | Yield ( a , Sequ a ). University of Oxford — Ralf Hinze 9-30
Type Fusion — Prologue AMAST 2010 1.3 Correctness List ( Maybe A ) ≅ Sequ A University of Oxford — Ralf Hinze 10-30
Type Fusion — Type fusion AMAST 2010 Section 2 Type fusion University of Oxford — Ralf Hinze 11-30
Type Fusion — Type fusion AMAST 2010 2.1 Type fusion G L F ❈ ≺ ≻ ❈ ≺ ≻ ❉ ≺ ⊥ ≻ ❉ G R F L (µ F ) ≅ µ G ⇐ = L ◦ F ≅ G ◦ L ν F ≅ R (ν G ) ⇐ = F ◦ R ≅ R ◦ G University of Oxford — Ralf Hinze 12-30
Type Fusion — Type fusion AMAST 2010 2.2 Interlude: adjoint folds 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 . University of Oxford — Ralf Hinze 13-30
Type Fusion — Type fusion AMAST 2010 τ : L (µ F ) ≅ µ G ⇐ = swap : L ◦ F ≅ G ◦ L University of Oxford — Ralf Hinze 14-30
Type Fusion — Type fusion AMAST 2010 Definition of τ and τ ◦ 2.3 G ( L (µ F )) ≻ ≺ swap G τ swap ◦ G τ ◦ ≺ ≻ L ( F (µ F )) G (µ G ) L in L in in in ⋎ ⋎ τ ◦ L (µ F ) ≺ ≻ µ G τ τ ◦ · in = L in · swap ◦ · G τ ◦ τ · L in = in · G τ · swap and University of Oxford — Ralf Hinze 15-30
Type Fusion — Type fusion AMAST 2010 Proof of τ · τ ◦ = id µ G 2.3 (τ · τ ◦ ) · in { definition of τ ◦ } = τ · L in · swap ◦ · G τ ◦ = { definition of τ } in · G τ · swap · swap ◦ · G τ ◦ = { inverses } in · G τ · G τ ◦ = { G functor } in · G (τ · τ ◦ ) The equation x · in = in · G x has a unique solution. Since id is also a solution, the result follows. University of Oxford — Ralf Hinze 16-30
Type Fusion — Type fusion AMAST 2010 Proof of τ ◦ · τ = id L (µ F ) 2.3 (τ ◦ · τ) · L in = { definition of τ } τ ◦ · in · G τ · swap { definition of τ ◦ } = L in · swap ◦ · G τ ◦ · G τ · swap { G functor } = L in · swap ◦ · G (τ ◦ · τ) · swap Again, x · L in = L in · swap ◦ · G x · swap has a unique solution. And again, id is also solution, which implies the result. University of Oxford — Ralf Hinze 17-30
Type Fusion — Applications AMAST 2010 Section 3 Applications University of Oxford — Ralf Hinze 18-30
Type Fusion — Applications AMAST 2010 3.1 Recall: type firstification The first-order datatype data Stack = Empty | Push ( Nat , Stack ) is an instance of the second-order datatype data List a = Nil | Cons ( a , List a ). Correctness: Λ - drop : List Nat ≅ Stack : Λ - lift . University of Oxford — Ralf Hinze 19-30
Type Fusion — Applications AMAST 2010 3.1 Speaking categorically App Nat (µ LIST ) ≅ µ Stack ⇐ = App Nat ◦ LIST ≅ Stack ◦ App Nat where App X : ❈ ❉ → ❈ App X F = F X App X ☛ = ☛ X is ‘type application’. University of Oxford — Ralf Hinze 20-30
Type Fusion — Applications AMAST 2010 3.2 Recall: type specialisation Lists of optional values, List ◦ Maybe with data Maybe a = Nothing | Just a , can be represented more compactly using the tailor-made data Sequ a = Done | Skip ( Sequ a ) | Yield ( a , Sequ a ). Correctness: List ◦ Maybe ≅ Sequ . University of Oxford — Ralf Hinze 21-30
Type Fusion — Applications AMAST 2010 3.2 Speaking categorically Pre Maybe (µ LIST ) ≅ µ SEQU ⇐ = Pre Maybe ◦ LIST ≅ SEQU ◦ Pre Maybe where Pre J : ❊ ❉ → ❊ ❈ Pre J F = F ◦ J Pre J ☛ = ☛ ◦ J is pre-composition, also written ❊ J . University of Oxford — Ralf Hinze 22-30
Type Fusion — Applications AMAST 2010 3.3 Recall: memoisation Functions from the natural numbers data Nat = Zero | Succ Nat can be memoised using streams data Table val = Node { zero : val , succ : Table val } . Correctness: ( − ) Nat ≅ Table University of Oxford — Ralf Hinze 23-30
Type Fusion — Applications AMAST 2010 3.3 Speaking categorically ν TABLE ≅ Exp (µ Nat ) ⇐ = TABLE ◦ Exp ≅ Exp ◦ Nat where Exp : ❈ → ( ❈ ❈ ) op Λ V . V K Exp K = Λ V . V f Exp f = is curried (!) exponentiation. University of Oxford — Ralf Hinze 24-30
Type Fusion — Applications AMAST 2010 3.3 Laws of exponentials V 0 ≅ 1 V 1 ≅ V V A × V B V A + B ≅ V A × B ( V B ) A ≅ Exp 0 ≅ K 1 Exp 1 ≅ Id Exp A ˙ Exp ( A + B ) ≅ × Exp B Exp ( A × B ) ≅ Exp A ◦ Exp B University of Oxford — Ralf Hinze 25-30
Type Fusion — Applications AMAST 2010 3.3 Exp is a left adjoint G ≻ ( ❈ ❈ ) op ≺ Exp F ( ❈ ❈ ) op ≺ ≻ ❈ ≺ ⊥ ≻ ❈ G Sel F University of Oxford — Ralf Hinze 26-30
Type Fusion — Applications AMAST 2010 3.3 Deriving the right adjoint ( ❈ ❈ ) op ( Exp A , B ) { definition of − op } ≅ ❈ ❈ ( B , Exp A ) ≅ { natural transformation as an end } ∀ X : ❈ . ❈ ( B X , Exp A X ) ≅ { definition of Exp } ∀ X : ❈ . ❈ ( B X , X A ) { − × Y ⊣ ( − ) Y and Y × Z ≅ Z × Y } ≅ ∀ X : ❈ . ❈ ( A , X B X ) ≅ { the functor ❈ ( A , − ) preserves ends } ❈ ( A , ∀ X : ❈ . X B X ) { define Sel B = ∀ X : ❈ . X B X } ≅ ❈ ( A , Sel B ) University of Oxford — Ralf Hinze 27-30
Type Fusion — Applications AMAST 2010 • Since Exp is a contravariant functor, τ and swap live in an opposite category. ≻ ( ❈ ❈ ) op ≺ Exp G F ( ❈ ❈ ) op ≺ ≻ ❈ ≺ ⊥ ≻ ❈ G Sel F • Type fusion in terms of arrows in ❈ ❈ : τ : ν G ≅ Exp (µ F ) swap : G ◦ Exp ≅ Exp ◦ F . ⇐ = • The isomorphism τ : ν G ˙ → Exp (µ F ) is a curried look-up function that maps a memo table to an exponential. • The inverse τ ◦ : Exp (µ F ) ˙ → ν G is a transformation that tabulates a given exponential. University of Oxford — Ralf Hinze 28-30
Type Fusion — Epilogue AMAST 2010 Section 4 Epilogue University of Oxford — Ralf Hinze 29-30
Type Fusion — Epilogue AMAST 2010 4.0 Summary and related work Summary: • Type fusion generalises • type firstification, • type specialisation, • memoisation or tabulation. • Adjunctions play a central role. Related work: • Backhouse, Bijsterveld, van Geldrop, van der Woude, Categorical fixed point calculus . CTCS ’95. • Hinze, Adjoint folds and unfolds . MPC ’10. University of Oxford — Ralf Hinze 30-30
Recommend
More recommend