1 Extensional Universal Types for Call-by-Value Kazuyuki Asada RIMS 1 July 2008
2 Overview (2/32) We give: 1. the λ c 2 η -calculus (and λ c 2-calculus): a second-order polymorphic call-by-value calculus with extensional universal types 2. λ c 2 η -models: categorical semantics for λ c 2 η -calculus monadic λ c 2 η -models: categorical semantics for λ c 2 η -calculus with the focus on monadic metalanguages like Haskell 3. relevant parametric models: domain theoretic concrete models of monadic λ c 2 η -models with a reasonable class of monads including: Lifting, Exception, Global State, Input/Output, and List monad
4 Introduction SystemF (4/32) Types σ ::= α b σ σ α.σ ( b: base types ) Terms M ::= x c σ λx σ.m MM Λα.M Mσ (c σ : constans) Typing Rules: All the rules in the λ-calculus, and: Ξ, α Γ M : σ Ξ Γ Λα.M : α.σ (α FTV (Γ)) Ξ Γ M : α.σ Ξ Γ Mτ : σ [τ/α] Axioms: All the axioms in the λ-calculus, and: (Λα.M) σ = M [σ/α] Λα.Mα = M (α FTV (M)) (Girard 1972, Reynolds 1974)
5 Introduction Polymorphic Encodings (5/32) 1 := α.α α σ τ := α. (σ τ α) α 0 := α.α σ + τ := α. (σ α) (τ α) α α.sigma := β. ( α. (σ β)) β µγ.σ := γ. ((σ γ) γ) νγ.σ := γ. ((γ σ) γ) (α FTV (σ, τ)) (α FTV (σ, τ)) (β α, β FTV (σ))
6 Introduction Parametricity (6/32) relational parametricity is: (Reynolds 1983) (binary) relations R τ τ between types relational interpretation σ [ R/ α ] of types σ with relations R the parametricity schema: β. u : ( α.σ [ α, β ]). τ. τ. R τ τ. ( uτ ( σ [ R/α, eqβ / β ]) uτ ) linear parametricity is: (Plotkin 1993) relations R τ τ are restricted to admissible relations admissible relations are intuitively subalgebras of product of the two types
7 Introduction Example of Parametric Reasoning For any u : α.α α, Λα.λa α.a = u α. (λa α.a = uα) α. a: α. (a = uαa) (type η-equality) (η-equality) (7/32) For α and a: α, let Then by parametricity, R := λx α.a α α uα (R R) uα Clearly, Hence ara (uαa) R (uαa) a = uαa
8 Introduction λ c -calculus and η-equality (8/32) Types Terms Values Evaluation Contexts σ M V E ::= b σ σ σ σ ::= x c σ λx σ.m MM M, M π 1 M π 2 M ::= x c σ λx σ.m V, V π 1 V π 2 V ::= [ ] EM VE E, M V, E π 1 E π 2 E (λx σ.m) V = M [V/x] λx σ.v x = V (x FV (V)) V = (V : ) π i V 1, V 2 = V i (i = 1, 2) π 1 V, π 2 V = V (λm σ.e [m]) M = E [M] (m FV (E)) λ c 2 η -calculus: (Moggi 1988) (Λα.M) σ = M [σ/α] Λα.Mα = M (α FTV (M))
10 Calculi 1 λ c -calculus Types Terms Values Evaluation Contexts x:σ Γ Γ x : σ σ (10/32) M ::= x c σ λx σ.m MM M, M π 1 M π 2 M V E ::= b σ σ σ σ ::= x c σ λx σ.m V, V π 1 V π 2 V Γ, x:σ M : τ Γ c σ : σ Γ λx σ.m : σ τ Γ M : σ Γ N : τ Γ : Γ M, N : σ τ ::= [ ] EM VE E, M V, E π 1 E π 2 E Γ M : σ τ Γ N : σ Γ MN : τ Γ M : σ τ Γ π 2 M : τ Γ M : σ τ Γ π 1 M : σ (λx σ.m) V = M [V/x] λx σ.v x = V (x FV (V)) V = (V : ) π i V 1, V 2 = V i (i = 1, 2) π 1 V, π 2 V = V (λm σ.e [m]) M = E [M] (m FV (E)) (Moggi 1988)
11 Calculi 1 λ c 2-calculus (11/32) Types σ ::=... α α.σ Values V ::=... Λα.M Terms M ::=... Λα.M Mσ Evaluation Contexts E ::=... Eσ Typing Rules: All the rules in the λ c -calculus, and: Ξ, α Γ M : σ Ξ Γ Λα.M : α.σ (α FTV (Γ)) Ξ Γ M : α.σ Ξ Γ Mτ : σ [τ/α] Axioms: All the axioms in the λ c -calculus (with E extended as above), and: (Λα.M) σ = M [σ/α] Λα.Vα = V (α FTV (V))
12 Calculi 1 λ c 2 η -calculus (12/32) Types σ ::=... α α.σ Values V ::=... Λα.V Vσ Terms M ::=... Λα.M Mσ Evaluation Contexts E ::=... Eσ Typing Rules: the same as those in the λ c 2-calculus. Axioms: All the axioms in the λ c -calculus (with E extended as above), and: (Λα.M) σ = M [σ/α] Λα.Mα = M (α FTV (M))
13 Calculi 1 λ c 2-calculus vs. λ c 2 η -calculus λ c 2-calculus: (13/32) Types σ ::=... α α.σ Values V ::=... Λα.M Terms M ::=... Λα.M Mσ Evaluation Contexts E ::=... Eσ (Λα.M) σ = M [σ/α] Λα.Vα = V (α FTV (V)) λ c 2 η -calculus: Types σ ::=... α α.σ Values V ::=... Λα.V Vσ Terms M ::=... Λα.M Mσ Evaluation Contexts E ::=... Eσ (Λα.M) σ = M [σ/α] Λα.Mα = M (α FTV (M)) (cf. ML)
14 Calculi 1 The Class of Monads (14/32) T ::= γ 1 T T T C νγ.t C T T µγ.t Lifting: Exception: Global State: Input: Output: List: ( ) 1 ( ) E (( ) S ) S µβ. ( ) β U µβ. ( ) (U β) µβ.1 ( ) β
15 1 Categorical Semantics
16 Categorical Semantics 1 Polymorphic λ c -models (16/32) Definition 1. A polymorphic λ c -model consists of (i) a cartesian polymorphic fibration, i.e. a fibration p : E B which has products in the base category, generic object Ω and fibred products, (ii) a fibred strong monad T on p satisfying the equalizing requirement fiberwise, and (iii) fibred Kleisli exponentials: E I ( ) X u E I F I (E T ) I (X ( )) u T ( ) u E X F J J E J (E T ) J (u X ( ))
17 Categorical Semantics 1 λ c 2-model Definition 2. Let p be a polymorphic λ c -model, F Ω be the change-of-base of the Kleisli embedding F along ( ) Ω, and π ( ),Ω be the weakening fibred functor. (17/32) Kleisli simple Ω-products is a fibred right adjoint functor to the composite of F Ω and π ( ),Ω, and a λ c 2-model is a polymorphic λ c -model p which has Kleisli simple Ω-products. π ( ),Ω E p E (Ω) p Ω B F Ω p Ω T E (Ω) T ( ) Ω p E B F p T E T E I π I,Ω u E I Ω F I Ω (E T ) I Ω I (u Ω) T π J,Ω F E J J Ω E J Ω (E T ) J Ω J
18 Categorical Semantics 1 λ c 2 η -models and Monadic λ c 2 η -models (18/32) Definition 3. A λ c 2 η -model is a polymorphic λ c -model p whose Kleisli fibration p T has simple Ω-products. Definition 4. A monadic λ c 2 η -model is a polymorphic λ c -model such that p and p T has simple Ω-products and the Kleisli embedding F : p p T preserves them. T p p T effect value (identity on objects) F U computation
19 Categorical Semantics 1 A Characterization of Monadic λ c 2 η -model (19/32) Lemma 5. Let p be a polymorphic λ c -model s.t. p has simple Ω-products. p is a monadic λ c 2 η -model, (i.e., p T has simple Ω-products and F : p p T preserves simple Ω-products,) if and only if the underlying fibred endofunctor of the monad T preserves simple Ω-products. For a λ2-fibration and a fibred strong monad on it satisfying the equalizing requirement, if the underlying endofunctor preserves simple Ω-products, then they form a monadic λ c 2 η -model.
20 2 Concrete Models
21 Concrete Models 2 Classes of Monads for Monadic λ c 2 η -models (21/32) T ::= γ 1 T T T C νγ.t (1) T ::= γ 1 T T T C νγ.t C (2) T ::= γ 1 T T T C νγ.t C T T µγ.t (3) (1) universality (2) universality + parametricity (3) universality + parametricity + additional terms (fixed-point operator)
22 Concrete Models 2 Products and Powers Lemma 6. Let B be a cartesian category, and K be any object of B. (22/32) (1) The subcategory of Fib B consisting of all fibrations having simple K-products, and all fibred functors preserving simple K-products, is cartesian subcategory. (2) Let p be a fibration having simple K-products and fibred finite products. Then the fibred functors : p p p, and 1 : 1 p preserve simple K-products. (3) Moreover assume that p is a fibred CCC. Then for any X over 1 in p, the power fibred functor X ( ) : p p preserves simple K-products.
23 Concrete Models 2 Final Coalgebras (23/32) Definition 7. For a fibred functor F : q p p, if F has pointwise final coalgebras and the reindexings preserve them, then we can define the final coalgebras fibred functor fcf : q p. Lemma 8. Let B be a cartesian category, K be an object of B, p, q be fibrations having simple K-products, and F : q p p be a fibred functor such that its transposition λf : q p p has final coalgebras as above. Then, if F preserves simple K-products, the fibred functor fc (λf) : q p also preserves simple K-products. (Typically q = p n.)
24 Concrete Models 2 Constants (24/32) For a λ2-fibration p and any object X over 1, the constant fibred functor X : 1 p preserves simple Ω-products if we assume parametricity. This is possible by all already known types of parametricity including relational parametricity, linear parametricity, and focal parametricity. At this point, we can construct global state monads (( ) S ) S and output monads µβ. ( ) + (U β) ( ) (µβ.1 + (U β)).
25 Concrete Models 2 Coproducts (25/32) λu: ( α. (σ + τ)). case (u1) of in 0 a in 0 (Λα.case (uα) of in 0 a a in 1 b this case nothing ) in 1 b in 1 (Λα.case (uα) of in 0 a this case nothing in 1 b b ) : α. (σ + τ) α.σ + α.τ We use for this case nothing.
26 Concrete Models 2 Initial Algebras (26/32) let f : (( α.µβ.1 + σ β) (µβ.1 + α.σ β)) = λu: ( α.µβ.1 + σ β).case (u1) of Nil Nil Cons(a, as ) Cons( Λα.case (uα) of Nil this case nothing Cons(a, as) a, f (Λα.case (uα) of Nil this case nothing Cons(a, as) as ) ) We use a fixed-point operator.
27 Concrete Models 2 Linear Parametric Models (27/32) p := l! L l! U F T T p p T U T l :a PILL Y model (Birkedal et al. 2008), i.e., λ2-version of linear categories which can also model fixed-point operators Assumption. L preserves simple Ω-products.
28 Concrete Models 2 Relevant Parametricity (28/32) Contraction term formation rule: and, moreover Ξ Γ;, x : σ, y : σ M : τ Ξ Γ;, x : σ M [ x/y ] : τ ρ adm σ τ M : σ τ σ τ ( M 1 ρ = ) (x : σ, y : τ ).ρ (π 1 M x, y, π 2 M x, y ) adm σ τ
29 Concrete Models 2 Separated Sums (29/32) Proposition 9. For a model satisfying the assumption thus far, its separated sums preserve simple Ω-products. p 2 L 2 l 2 p U l +
30 Concrete Models 3 Linearly Initial Algebras (30/32) Proposition 10. For a model l satisfying the assumption thus far, let q be a fibration which has the same base category and generic object Ω as those of l and p, and has simple Ω-products. Then for a fibred functor F : q p p preserving simple Ω-products, if the transposition λf has initial algebras and also final coalgebras, then the linearly initial algebras fibred functor U ia (λf ) : q p also preserves simple Ω-products. q p q U q l q F p L l F p U l ia(λf )
31 Concrete Models 3 Summary and Example (31/32) T ::= γ 1 T T T C νγ.t C T T µγ.t Theorem 11. For a model l satisfying the assumption thus far, let T be a fibred strong monad on p satisfying the equalizing requirement. If T is constructed inductively as above, then T and the λ2-fibration p form a monadic λ c 2 η -model. Example 12. (PFam (AP(D) ), PFam (AP(D)),...) : the models of linear Plotkin-Abadi logic constructed from domain theoretic PERs (L. Birkedal, R. E. Møgelberg, and R. L. Petersen, 2007)
32 Conclusion 3 Future Work (32/32) Dependent type theoretical approach is also available. powerset monads? continuations monads? all lifting monads? all commutative relevant monads?