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
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
Two levels of types Value types A , B :: = F A | A → B | A ⊸ B Computation types A , B F A | A → B :: = Computation types ⊆ Value types EffTT, Tallinn, Dec. 2007 – p. 3/27
Terms Terms in context Γ | − ⊢ t : A Γ | x : B ⊢ t : A . EffTT, Tallinn, Dec. 2007 – p. 4/27
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
Typing rules Γ | x : A ⊢ x : A Γ , x : A |− ⊢ x : A Γ | x : A ⊢ t : B Γ , x : A | ∆ ⊢ t : B ◦ x : A . t : A ⊸ 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
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
Algebraic operations Specialise the theory to printing by adding print a : A → A for each computation type A and each a in alphabet A EffTT, Tallinn, Dec. 2007 – p. 7/27
Algebraic operations Specialise the theory to printing by adding print a : A → A for each computation type A and each a in alphabet A Or to global state by adding lookup A : ( nat → A ) → loc → A update A : A → nat → loc → A for each computation type A EffTT, Tallinn, Dec. 2007 – p. 7/27
Maps commuting with operations Maps of homomorphism type f : A ⊸ B commute with operations Examples: print a ( f ( x )) = f (print a ( x )) lookup B , l ( λ x : nat . f ( t )) = f (lookup A , l ( λ x : nat . t )) EffTT, Tallinn, Dec. 2007 – p. 8/27
� � Models V cartesian closed, T strong monad on V V T ⊣ F U V Where V T category of algebras ξ : TX → X for monad T Model value types V [ [ A ] ] ∈ V Model computation types V T [ ] ∈ V T [ B ] ] = U ( V T [ V [ [ B ] [ B ] ]) EffTT, Tallinn, Dec. 2007 – p. 9/27
� � Models II More generally adjunctions C ⊣ F U V satisfying certain conditions (e.g. C ( X , Y ) should be an object of V ) can be used. EffTT, Tallinn, Dec. 2007 – p. 10/27
� � Example: Printing PA A ⋆ × ( − ) ⊣ U Set PA cat. of sets with left A ⋆ action ( X , · : A ⋆ × X → X ) EffTT, Tallinn, Dec. 2007 – p. 11/27
� � Example: Printing PA A ⋆ × ( − ) ⊣ U Set 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
� � Example: Printing PA A ⋆ × ( − ) ⊣ U Set PA cat. of sets with left A ⋆ action ( X , · : A ⋆ × X → X ) → interpreted as ordinary maps in sets, ⊸ as homomorphisms print a : X → X is interpreted as a · ( − ) EffTT, Tallinn, Dec. 2007 – p. 11/27
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: F A = ∀ X . ( A → X ) → X . EffTT, Tallinn, Dec. 2007 – p. 12/27
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
Inductive and coinductive types in PE Parametric polymorphism allows for encoding of (co)inductive value and computation types e.g. inductive computation types def µ ◦ X . A = ∀ X . ( A ⊸ X ) → X ( X +ve in A) EffTT, Tallinn, Dec. 2007 – p. 13/27
Inductive and coinductive types in PE Parametric polymorphism allows for encoding of (co)inductive value and computation types e.g. inductive computation types def µ ◦ X . A = ∀ X . ( A ⊸ X ) → X ( X +ve in A) Generalise encodings from combination of recursion and parametric polymorphism EffTT, Tallinn, Dec. 2007 – p. 13/27
Inductive and coinductive types in PE Parametric polymorphism allows for encoding of (co)inductive value and computation types e.g. inductive computation types def µ ◦ X . A = ∀ 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
Inductive and coinductive types in PE Parametric polymorphism allows for encoding of (co)inductive value and computation types e.g. inductive computation types def µ ◦ X . A = ∀ 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
How to add recursion? Would like to add fixpoint operator fix X : ( X → X ) → X Should X be a value type or computation type? EffTT, Tallinn, Dec. 2007 – p. 14/27
How to add recursion? Would like to add fixpoint operator fix X : ( 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
How to add recursion? Would like to add fixpoint operator fix X : ( 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
Examples of monads Models are monads on Cpo (complete partial orders) EffTT, Tallinn, Dec. 2007 – p. 15/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 ) ⊥ EffTT, Tallinn, Dec. 2007 – p. 15/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
� � � � Semantic picture Cppo ⊥ � � � U � � L ⊣ U L � � � � F T � � Cpo T � Cpo ⊥ U T U L U = U T EffTT, Tallinn, Dec. 2007 – p. 16/27
� � � � Semantic picture Cppo ⊥ � � � U � � L ⊣ U L � � � � F T � � Cpo T � Cpo ⊥ U T U L U = U T Thm: U has a left adjoint EffTT, Tallinn, Dec. 2007 – p. 16/27
Pointed homsets Given a pair of algebras ξ : TX → X , ξ ′ : TY → Y the homset Cpo T ( ξ, ξ ′ ) is a cpo. Is it a pointed cpo? EffTT, Tallinn, Dec. 2007 – p. 17/27
Pointed homsets Given a pair of algebras ξ : TX → X , ξ ′ : TY → Y the homset Cpo T ( ξ, ξ ′ ) is a cpo. Is it a pointed cpo? Thm: For T given by operations, Cpo T ( ξ, ξ ′ ) contains λ x : X . ⊥ for all ξ, ξ ′ iff all operations are strict. EffTT, Tallinn, Dec. 2007 – p. 17/27
Pointed homsets Given a pair of algebras ξ : TX → X , ξ ′ : TY → Y the homset Cpo T ( ξ, ξ ′ ) is a cpo. Is it a pointed cpo? Thm: For T given by operations, Cpo T ( ξ, ξ ′ ) 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
Pointed homsets Given a pair of algebras ξ : TX → X , ξ ′ : TY → Y the homset Cpo T ( ξ, ξ ′ ) is a cpo. Is it a pointed cpo? Thm: For T given by operations, Cpo T ( ξ, ξ ′ ) 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
� � Type theory for strict operations Cpo T F ⊣ U Cppo ⊥ EffTT, Tallinn, Dec. 2007 – p. 18/27
� � Type theory for strict operations Cpo T F ⊣ U Cppo ⊥ Value types in Cppo ⊥ , computation types in Cpo T A :: = F A | A ◦ → B A :: = F A | A ◦ → B | A ⊸ B | L A Define A → B = L A ◦ → B EffTT, Tallinn, Dec. 2007 – p. 18/27
Recommend
More recommend