Recursion in two level type theories for effects Work in progress - - PowerPoint PPT Presentation

recursion in two level type theories for effects
SMART_READER_LITE
LIVE PREVIEW

Recursion in two level type theories for effects Work in progress - - PowerPoint PPT Presentation

Recursion in two level type theories for effects Work in progress Rasmus Ejlers Mgelberg IT University of Copenhagen EffTT, Tallinn, Dec. 2007 p. 1/27 Two level type theories Moggi introduced monads as general framework for modelling


slide-1
SLIDE 1

Recursion in two level type theories for effects

Work in progress

Rasmus Ejlers Møgelberg IT University of Copenhagen

EffTT, Tallinn, Dec. 2007 – p. 1/27

slide-2
SLIDE 2

Two level type theories

Moggi introduced monads as general framework for modelling effects 2-level type theories based on distinction between computation types and value types This has been used in Levy’s Call-By-Push-Value (CBPV) And in recent work with Alex Simpson on parametricity Today: Recursion in 2-level type theories

EffTT, Tallinn, Dec. 2007 – p. 2/27

slide-3
SLIDE 3

Two levels of types

Value types A, B ::= FA | A → B | A ⊸ B Computation types A, B ::= FA | A → B Computation types ⊆ Value types

EffTT, Tallinn, Dec. 2007 – p. 3/27

slide-4
SLIDE 4

Terms

Terms in context Γ | − ⊢ t: A Γ | x: B ⊢ t: A .

EffTT, Tallinn, Dec. 2007 – p. 4/27

slide-5
SLIDE 5

Terms

Terms in context Γ | − ⊢ t: A Γ | x: B ⊢ t: A . Terms t, s ::= x | λx: A. t | s(t) | λ◦x: A. t

EffTT, Tallinn, Dec. 2007 – p. 4/27

slide-6
SLIDE 6

Typing rules

Γ, x: A |− ⊢ x: A Γ |x: A ⊢ x: A Γ, x: A |∆ ⊢ t: B Γ |∆ ⊢ λx: A. t: A → B Γ |x: A ⊢ t: B Γ |− ⊢ λ

  • x: A. t: A ⊸ B

Γ |∆ ⊢ s: A → B Γ |− ⊢ t: A Γ |∆ ⊢ s(t): B Γ |− ⊢ s: A ⊸ B Γ |∆ ⊢ t: A Γ |∆ ⊢ s(t): B

EffTT, Tallinn, Dec. 2007 – p. 5/27

slide-7
SLIDE 7

Typing rules, monadic type

Γ |− ⊢ t: B Γ |− ⊢ !t: F B Γ |∆ ⊢ t: F B Γ, x: B |− ⊢ u: A Γ |∆ ⊢ let !x be t in u: A

EffTT, Tallinn, Dec. 2007 – p. 6/27

slide-8
SLIDE 8

Algebraic operations

Specialise the theory to printing by adding printa : A → A for each computation type A and each a in alphabet A

EffTT, Tallinn, Dec. 2007 – p. 7/27

slide-9
SLIDE 9

Algebraic operations

Specialise the theory to printing by adding printa : A → A for each computation type A and each a in alphabet A Or to global state by adding lookupA : (nat → A) → loc → A updateA : A → nat → loc → A for each computation type A

EffTT, Tallinn, Dec. 2007 – p. 7/27

slide-10
SLIDE 10

Maps commuting with operations

Maps of homomorphism type f : A ⊸ B commute with

  • perations

Examples: printa(f(x)) = f(printa(x)) lookupB,l(λx: nat. f(t)) = f(lookupA,l(λx: nat. t))

EffTT, Tallinn, Dec. 2007 – p. 8/27

slide-11
SLIDE 11

Models

V cartesian closed, T strong monad on V VT

U

  • V

⊣ F

  • Where VT category of algebras ξ: TX → X for monad

T Model value types V[ [A] ] ∈ V Model computation types VT[ [B] ] ∈ VT V[ [B] ] = U(VT[ [B] ])

EffTT, Tallinn, Dec. 2007 – p. 9/27

slide-12
SLIDE 12

Models II

More generally adjunctions C

U

  • V

⊣ F

  • satisfying certain conditions (e.g. C(X, Y) should be an
  • bject of V) can be used.

EffTT, Tallinn, Dec. 2007 – p. 10/27

slide-13
SLIDE 13

Example: Printing

PA

U

  • Set

⊣ A⋆×(−)

  • PA cat. of sets with left A⋆ action (X, · : A⋆ × X → X)

EffTT, Tallinn, Dec. 2007 – p. 11/27

slide-14
SLIDE 14

Example: Printing

PA

U

  • Set

⊣ A⋆×(−)

  • PA cat. of sets with left A⋆ action (X, · : A⋆ × X → X)

→ interpreted as ordinary maps in sets, ⊸ as homomorphisms

EffTT, Tallinn, Dec. 2007 – p. 11/27

slide-15
SLIDE 15

Example: Printing

PA

U

  • Set

⊣ A⋆×(−)

  • PA cat. of sets with left A⋆ action (X, · : A⋆ × X → X)

→ interpreted as ordinary maps in sets, ⊸ as homomorphisms printa : X → X is interpreted as a · (−)

EffTT, Tallinn, Dec. 2007 – p. 11/27

slide-16
SLIDE 16

Polymorphism and effects

Type theory for (parametric) polymorphism and effects Value types A, B ::= X | A → B | ∀X. A | X | A ⊸ B | ∀X. A Computation types A, B ::= A → B | ∀X. A | X | ∀X. A . F can be encoded using parametricity: FA = ∀X. (A → X) → X.

EffTT, Tallinn, Dec. 2007 – p. 12/27

slide-17
SLIDE 17

Inductive and coinductive types in PE

Parametric polymorphism allows for encoding of (co)inductive value and computation types

EffTT, Tallinn, Dec. 2007 – p. 13/27

slide-18
SLIDE 18

Inductive and coinductive types in PE

Parametric polymorphism allows for encoding of (co)inductive value and computation types e.g. inductive computation types µ◦X. A

def

= ∀X. (A ⊸ X) → X (X +ve in A)

EffTT, Tallinn, Dec. 2007 – p. 13/27

slide-19
SLIDE 19

Inductive and coinductive types in PE

Parametric polymorphism allows for encoding of (co)inductive value and computation types e.g. inductive computation types µ◦X. A

def

= ∀X. (A ⊸ X) → X (X +ve in A) Generalise encodings from combination of recursion and parametric polymorphism

EffTT, Tallinn, Dec. 2007 – p. 13/27

slide-20
SLIDE 20

Inductive and coinductive types in PE

Parametric polymorphism allows for encoding of (co)inductive value and computation types e.g. inductive computation types µ◦X. A

def

= ∀X. (A ⊸ X) → X (X +ve in A) Generalise encodings from combination of recursion and parametric polymorphism Fixed points + inductive and coinductive types give general recursive types

EffTT, Tallinn, Dec. 2007 – p. 13/27

slide-21
SLIDE 21

Inductive and coinductive types in PE

Parametric polymorphism allows for encoding of (co)inductive value and computation types e.g. inductive computation types µ◦X. A

def

= ∀X. (A ⊸ X) → X (X +ve in A) Generalise encodings from combination of recursion and parametric polymorphism Fixed points + inductive and coinductive types give general recursive types Can this be done for general effects?

EffTT, Tallinn, Dec. 2007 – p. 13/27

slide-22
SLIDE 22

How to add recursion?

Would like to add fixpoint operator fixX : (X → X) → X Should X be a value type or computation type?

EffTT, Tallinn, Dec. 2007 – p. 14/27

slide-23
SLIDE 23

How to add recursion?

Would like to add fixpoint operator fixX : (X → X) → X Should X be a value type or computation type? Would also like reasoning principles such as the uniformity principle: h ◦ f = g ◦ h ⇒ h(fix f) = fix g (if h(⊥) = ⊥)

EffTT, Tallinn, Dec. 2007 – p. 14/27

slide-24
SLIDE 24

How to add recursion?

Would like to add fixpoint operator fixX : (X → X) → X Should X be a value type or computation type? Would also like reasoning principles such as the uniformity principle: h ◦ f = g ◦ h ⇒ h(fix f) = fix g (if h(⊥) = ⊥) This suggests three notions of maps: →, ⊸ as above and ◦ → for strict maps

EffTT, Tallinn, Dec. 2007 – p. 14/27

slide-25
SLIDE 25

Examples of monads

Models are monads on Cpo (complete partial orders)

EffTT, Tallinn, Dec. 2007 – p. 15/27

slide-26
SLIDE 26

Examples of monads

Models are monads on Cpo (complete partial orders) Examples: State and recursion TX = (S × X)S

⊥ (where S = ωL

discrete cpo of states) Printing and recursion: TX = µZ. (A × Z + X)⊥ (where A is alphabet) Commutative combination of print and recursion: TX = (A⋆ × X)⊥

EffTT, Tallinn, Dec. 2007 – p. 15/27

slide-27
SLIDE 27

Examples of monads

Models are monads on Cpo (complete partial orders) Examples: State and recursion TX = (S × X)S

⊥ (where S = ωL

discrete cpo of states) Printing and recursion: TX = µZ. (A × Z + X)⊥ (where A is alphabet) Commutative combination of print and recursion: TX = (A⋆ × X)⊥ In all cases TX is pointed, and any algebra for T is pointed

EffTT, Tallinn, Dec. 2007 – p. 15/27

slide-28
SLIDE 28

Semantic picture

Cppo⊥

UL

  • CpoT

U

  • UT

Cpo

FT

  • L ⊣
  • ULU = UT

EffTT, Tallinn, Dec. 2007 – p. 16/27

slide-29
SLIDE 29

Semantic picture

Cppo⊥

UL

  • CpoT

U

  • UT

Cpo

FT

  • L ⊣
  • ULU = UT

Thm: U has a left adjoint

EffTT, Tallinn, Dec. 2007 – p. 16/27

slide-30
SLIDE 30

Pointed homsets

Given a pair of algebras ξ: TX → X, ξ′ : TY → Y the homset CpoT(ξ, ξ′) is a cpo. Is it a pointed cpo?

EffTT, Tallinn, Dec. 2007 – p. 17/27

slide-31
SLIDE 31

Pointed homsets

Given a pair of algebras ξ: TX → X, ξ′ : TY → Y the homset CpoT(ξ, ξ′) is a cpo. Is it a pointed cpo? Thm: For T given by operations, CpoT(ξ, ξ′) contains λx: X. ⊥ for all ξ, ξ′ iff all operations are strict.

EffTT, Tallinn, Dec. 2007 – p. 17/27

slide-32
SLIDE 32

Pointed homsets

Given a pair of algebras ξ: TX → X, ξ′ : TY → Y the homset CpoT(ξ, ξ′) is a cpo. Is it a pointed cpo? Thm: For T given by operations, CpoT(ξ, ξ′) contains λx: X. ⊥ for all ξ, ξ′ iff all operations are strict. Condition holds for state: TX = (S × X)S

⊥ and

commutative combination of print and recursion TX = (A⋆ × X)⊥

EffTT, Tallinn, Dec. 2007 – p. 17/27

slide-33
SLIDE 33

Pointed homsets

Given a pair of algebras ξ: TX → X, ξ′ : TY → Y the homset CpoT(ξ, ξ′) is a cpo. Is it a pointed cpo? Thm: For T given by operations, CpoT(ξ, ξ′) contains λx: X. ⊥ for all ξ, ξ′ iff all operations are strict. Condition holds for state: TX = (S × X)S

⊥ and

commutative combination of print and recursion TX = (A⋆ × X)⊥ Condition does not hold for usual combination of print and recursion TX = µZ. (A × Z + X)⊥

EffTT, Tallinn, Dec. 2007 – p. 17/27

slide-34
SLIDE 34

Type theory for strict operations

CpoT

U

  • Cppo⊥

F ⊣

  • EffTT, Tallinn, Dec. 2007 – p. 18/27
slide-35
SLIDE 35

Type theory for strict operations

CpoT

U

  • Cppo⊥

F ⊣

  • Value types in Cppo⊥, computation types in CpoT

A ::= FA | A ◦ → B A ::= FA | A ◦ → B | A ⊸ B | LA Define A → B = LA ◦ → B

EffTT, Tallinn, Dec. 2007 – p. 18/27

slide-36
SLIDE 36

Terms

Type judgements are of the form: Γ |∆ |Σ ⊢ t: A

EffTT, Tallinn, Dec. 2007 – p. 19/27

slide-37
SLIDE 37

Terms

Type judgements are of the form: Γ |∆ |Σ ⊢ t: A Fixed point combinator: fixX : (X → X) → X

EffTT, Tallinn, Dec. 2007 – p. 19/27

slide-38
SLIDE 38

Terms

Type judgements are of the form: Γ |∆ |Σ ⊢ t: A Fixed point combinator: fixX : (X → X) → X Uniformity principle: if h: X ◦ → Y then h ◦ f = g ◦ h ⇒ h(fixX f) = fixY g

EffTT, Tallinn, Dec. 2007 – p. 19/27

slide-39
SLIDE 39

Terms

Type judgements are of the form: Γ |∆ |Σ ⊢ t: A Fixed point combinator: fixX : (X → X) → X Uniformity principle: if h: X ◦ → Y then h ◦ f = g ◦ h ⇒ h(fixX f) = fixY g Operations are strict, e.g., lookupA : (nat ◦ → A) ◦ → (loc ◦ → A) updateA : A ◦ → (nat ◦ → loc ◦ → A)

EffTT, Tallinn, Dec. 2007 – p. 19/27

slide-40
SLIDE 40

Recursive types

Thm: If T is generated by strict operations then CpoT is parametrised algebraically compact wrt. continuous functors Consequence: Can add nested recursive value types and nested recursive computation types to type theory Reasoning principles for recursive types as in Freyd’s / Pitts’ work

EffTT, Tallinn, Dec. 2007 – p. 20/27

slide-41
SLIDE 41

Conclusions

Combinations of recursion and effects fall in two categories: Strict and non-strict operations Have sketched type theory for case of strict operations Uniformity principle for fixed points Special case: Uniformity of fixed points with respect to operations generating effects Recursive computation types In non-strict case we can not model recursive computation types

EffTT, Tallinn, Dec. 2007 – p. 21/27

slide-42
SLIDE 42

Typing rules

Γ, x: B |− |− ⊢ x: B Γ |x: B |− ⊢ x: B Γ |− |x: A ⊢ x: A Γ |∆, x: B |Σ ⊢ t: C Γ |∆ |Σ ⊢ λx: B. t: B ◦ → C Γ |∆ |Σ ⊢ s: B ◦ → C Γ |∆′ |− ⊢ t: B dom(∆) ∩ dom(∆′) = ∅ Γ |∆, ∆′ |Σ ⊢ s(t): C

EffTT, Tallinn, Dec. 2007 – p. 22/27

slide-43
SLIDE 43

Typing rules

Γ |∆ |x: A ⊢ t: B Γ |∆ |− ⊢ λ

  • x: B. t: A ⊸ B

Γ |∆ |− ⊢ s: A ⊸ B Γ |∆′ |Σ ⊢ t: A dom(∆) ∩ dom(∆′) = ∅ Γ |∆, ∆′ |Σ ⊢ s(t): B Γ |− |− ⊢ t: B Γ |− |− ⊢ !t: LB Γ |∆ |− ⊢ t: LB Γ, x: B |∆′ |Σ ⊢ u: A Γ |∆, ∆′ |Σ ⊢ let !x be t in u: A

EffTT, Tallinn, Dec. 2007 – p. 23/27

slide-44
SLIDE 44

Typing rules

Γ |∆ |− ⊢ t: B Γ |∆ |− ⊢ [t] : FB Γ |∆ |Σ ⊢ t: FB Γ |∆′, x: B |− ⊢ u: A dom(∆) ∩ dom(∆′) = ∅ Γ |∆, ∆′ |Σ ⊢ let [x] be t in u: A

EffTT, Tallinn, Dec. 2007 – p. 24/27

slide-45
SLIDE 45

Equations

(λx: A. t)(u) = t[u/x] λx: A. t(x) = t if t: A ◦ → B and x FV(t) (λ

  • x: A. t)(u) = t[u/x]

λ

  • x: A. t(x) = t

if t: A ⊸ B and x FV(t)

EffTT, Tallinn, Dec. 2007 – p. 25/27

slide-46
SLIDE 46

Equations

Γ |− |− ⊢ t: B Γ, x: B |∆ |Σ ⊢ u: A Γ |∆ |Σ ⊢ let !x be !t in u = u[t/x] Γ |∆ |− ⊢ t: LA Γ | y: LA |Σ ⊢ u: B Γ |∆ |Σ ⊢ let !x be t in u[!x/y] = u[t / y]

EffTT, Tallinn, Dec. 2007 – p. 26/27

slide-47
SLIDE 47

Equations

Γ |∆ |− ⊢ t: B Γ |∆′, x: B |− ⊢ u: A Γ |∆, ∆′ |− ⊢ let [x] be [t] in u = u[t/x] Γ |∆ |Σ ⊢ t: FA Γ |∆′ | y: FA ⊢ u: B Γ |∆, ∆′ |Σ ⊢ let [x] be t in u[[x] /y] = u[t / y]

EffTT, Tallinn, Dec. 2007 – p. 27/27