Type Fusion Ralf Hinze Computing Laboratory, University of Oxford - - PowerPoint PPT Presentation

type fusion
SMART_READER_LITE
LIVE PREVIEW

Type Fusion Ralf Hinze Computing Laboratory, University of Oxford - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

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

slide-2
SLIDE 2

Type Fusion — Prologue AMAST 2010

Section 1 Prologue

University of Oxford — Ralf Hinze 2-30

slide-3
SLIDE 3

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

slide-4
SLIDE 4

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

slide-5
SLIDE 5

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

slide-6
SLIDE 6

Type Fusion — Prologue AMAST 2010

1.1 Correctness

tabulate : V Nat ≅ Table V : lookup

University of Oxford — Ralf Hinze 6-30

slide-7
SLIDE 7

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

slide-8
SLIDE 8

Type Fusion — Prologue AMAST 2010

1.2 Correctness

Λ-drop : List Nat ≅ Stack : Λ-lift

University of Oxford — Ralf Hinze 8-30

slide-9
SLIDE 9

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

slide-10
SLIDE 10

Type Fusion — Prologue AMAST 2010

1.3 Correctness

List (Maybe A) ≅ Sequ A

University of Oxford — Ralf Hinze 10-30

slide-11
SLIDE 11

Type Fusion — Type fusion AMAST 2010

Section 2 Type fusion

University of Oxford — Ralf Hinze 11-30

slide-12
SLIDE 12

Type Fusion — Type fusion AMAST 2010

2.1 Type fusion

❈ ≺ G G ≻ ❈ ≺ L ⊥ R ≻ ❉ ≺ F F ≻ ❉ L (µF) ≅ µG ⇐ = L ◦ F ≅ G ◦ L νF ≅ R (νG) ⇐ = F ◦ R ≅ R ◦ G

University of Oxford — Ralf Hinze 12-30

slide-13
SLIDE 13

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

slide-14
SLIDE 14

Type Fusion — Type fusion AMAST 2010

τ : L (µF) ≅ µG ⇐ = swap : L ◦ F ≅ G ◦ L

University of Oxford — Ralf Hinze 14-30

slide-15
SLIDE 15

Type Fusion — Type fusion AMAST 2010

2.3 Definition of τ and τ◦

G (L (µF)) L (F (µF)) ≺ swap swap◦ ≻ G (µG) ≺ G τ G τ◦ ≻ L (µF) L in L in ⋎ ≺ τ◦ τ ≻ µG in in ⋎ τ·L in = in·G τ·swap and τ◦·in = L in·swap◦·G τ◦

University of Oxford — Ralf Hinze 15-30

slide-16
SLIDE 16

Type Fusion — Type fusion AMAST 2010

2.3 Proof of τ · τ◦ = idµG

(τ · τ◦) · 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

slide-17
SLIDE 17

Type Fusion — Type fusion AMAST 2010

2.3 Proof of τ◦ · τ = idL (µF)

(τ◦ · τ) · 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

slide-18
SLIDE 18

Type Fusion — Applications AMAST 2010

Section 3 Applications

University of Oxford — Ralf Hinze 18-30

slide-19
SLIDE 19

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

slide-20
SLIDE 20

Type Fusion — Applications AMAST 2010

3.1 Speaking categorically

AppNat (µLIST) ≅ µStack ⇐ = AppNat ◦ LIST ≅ Stack ◦ AppNat where AppX : ❈❉ → ❈ AppX F = F X AppX ☛ = ☛ X is ‘type application’.

University of Oxford — Ralf Hinze 20-30

slide-21
SLIDE 21

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

slide-22
SLIDE 22

Type Fusion — Applications AMAST 2010

3.2 Speaking categorically

PreMaybe (µLIST) ≅ µSEQU ⇐ = PreMaybe ◦ LIST ≅ SEQU ◦ PreMaybe where PreJ : ❊❉ → ❊❈ PreJ F = F ◦ J PreJ ☛ = ☛ ◦ J is pre-composition, also written ❊J.

University of Oxford — Ralf Hinze 22-30

slide-23
SLIDE 23

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

slide-24
SLIDE 24

Type Fusion — Applications AMAST 2010

3.3 Speaking categorically

νTABLE ≅ Exp (µNat) ⇐ = TABLE ◦ Exp ≅ Exp ◦ Nat where Exp : ❈ → (❈❈)op Exp K = Λ V . V K Exp f = Λ V . V f is curried (!) exponentiation.

University of Oxford — Ralf Hinze 24-30

slide-25
SLIDE 25

Type Fusion — Applications AMAST 2010

3.3 Laws of exponentials

V 0 ≅ 1 V 1 ≅ V V A+B ≅ V A × V B V A×B ≅ (V B)A Exp 0 ≅ K 1 Exp 1 ≅ Id Exp (A + B) ≅ Exp A ˙ × Exp B Exp (A × B) ≅ Exp A ◦ Exp B

University of Oxford — Ralf Hinze 25-30

slide-26
SLIDE 26

Type Fusion — Applications AMAST 2010

3.3 Exp is a left adjoint

(❈❈)op ≺ G G ≻ (❈❈)op ≺ Exp ⊥ Sel ≻ ❈ ≺ F F ≻ ❈

University of Oxford — Ralf Hinze 26-30

slide-27
SLIDE 27

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

slide-28
SLIDE 28

Type Fusion — Applications AMAST 2010

  • Since Exp is a contravariant functor, τ and swap live in

an opposite category. (❈❈)op ≺ G G ≻ (❈❈)op ≺ Exp ⊥ Sel ≻ ❈ ≺ F 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

slide-29
SLIDE 29

Type Fusion — Epilogue AMAST 2010

Section 4 Epilogue

University of Oxford — Ralf Hinze 29-30

slide-30
SLIDE 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