1/28 Linear Types for Continuations Alex Simpson LFCS, School of Informatics University of Edinburgh, UK Joint work with: Jeff Egger (LFCS, Edinburgh) Rasmus Møgelberg (ITU, Copenhagen)
Linear Types for Continuations 2/28 Part 1 Some CPS Translations
Linear Types for Continuations 3/28 Call-by-value CPS translation Type translation: (σ τ) Rv = σ Rv (τ Rv R) R (σ τ) Rv = σ Rv τ Rv Term translation: Γ t: τ = Γ Rv t Rv : (τ Rv R) R Faithful but not full (can also model control operators) (Kleisli category of (( ) R) R monad)
Linear Types for Continuations 4/28 Call-by-name CPS translation Type translation: (σ τ) Rn = (σ Rn R) τ Rn (σ τ) Rn = σ Rn + τ Rn Term translation: Γ t: τ = Γ Rn R t Rn : τ Rn R Faithful but not full (can also model control operators) (Opposite of Kleisli category of (( ) R) R monad)
Linear Types for Continuations 5/28 Cbv linearly-used CPS translation Type translation: (σ τ) Rv = σ Rv (τ Rv R) R (σ τ) Rv = σ Rv τ Rv Term translation: Γ t: τ = Γ Rv t Rv : (τ Rv R) R Full and faithful [Hasegawa 2002] (Kleisli category of (( ) R) R monad on intuitionistic category)
Linear Types for Continuations 6/28 Cbn linearly-used CPS translation Type translation: (σ τ) Rn =!(σ Rn R) τ Rn (σ τ) Rn = σ Rn τ Rn Term translation: Γ t: τ = Γ Rn R t Rn : τ Rn R Full and faithful [Hasegawa 2004] (Opposite of Kleisli category of (( ) R) R monad on linear category)
Linear Types for Continuations 7/28 Part 2 The Enriched Effect Calculus
Linear Types for Continuations 8/28 The Enriched Effect Calculus (EEC) [EMS 2009] A more general target language than ILL Based on distinction between values and computations. Cf. [Filinski 1996], [Levy 1999, 2004] A value is a pervasive static object, it just is. Values can be copied and discarded. There is no natural notion of linear function between general value types. A computation has a dynamic side: it can be computed/invoked/evaluated/executed. There is a natural intuition of linear functions between computation types: the argument computation is executed exactly once. Linear functions are generalised evaluation contexts/stacks.
Linear Types for Continuations 9/28 EEC type system Value types: A, B,... ::= α α 1 A B A B!A A B!A B 0 A B Computation types: A, B,... ::= α 1 A B A B!A!A B 0 A B. Typing judgements: Γ t: A Γ z : A t: B The linearly-used CPS translations land in the EEC fragment of ILL.
Linear Types for Continuations 10/28 Example rules Rules are inherited from ILL. e.g., Γ, x: A t: B Γ λx: A. t: A B Γ t: A Γ! t:! A Γ z : A t: B Γ λz : A. t: A B Γ s: A B Γ t: A Γ s(t): B Γ t:! A Γ, x: A u: B Γ let!x t in u: B Γ s: A B Γ t: A Γ s[t]: B We have the Girard isomorphism: A B =!A B
Linear Types for Continuations 11/28 Other isomorphisms of EEC (!A B) C = A (B C) = B (A C) (As value types)!a!b =!(A B)!1 A = A!(A B) C =!A!B C!A 0 = 0!A (B C) = (!A B) (!A C) (As computation types) These isomorphisms are all familiar from ILL
Linear Types for Continuations 12/28 Differences from linear logic 1. Distinction between computation types and value types. 2. A B is not assumed to be a computation type itself. Thus (A B) C value type (A B) C computation (hence value) type (A B) C not available A B C not available 3.!A B is the application of a single primitive type-constructor!( ) ( ) to A and B. (There is no symmetric.) 4. Commutativity does not hold, i.e., let!x s in let!y t in u let!y t in let!x s in u
Linear Types for Continuations 13/28 Models of EEC A model of the enriched effect calculus is given by [EMS 2009]: categories V (value types) and C (computation types) V is cartesian closed (models 1, A B, A B) C is V-enriched (models A B) C has V-powers (models A B) and V-copowers (models!a B) C has finite V-enriched products (models 1, A B) and coproducts (models 0, A B) a V-enriched adjunction F U : C V (models!a) We write the entire model as F U : C V. (All structure other than the adjunction is determined by universal properties.)
Linear Types for Continuations 14/28 Models of ILL are models of EEC A linear/nonlinear model [Benton 1995] is given by symmetric monoidal closed category C (the linear category), a cartesian closed category V (the intuitionistic category) and a monoidal adjunction F U : C V. If C also has finite products and coproducts then the model is said to have additives Proposition [EMS 2009] If F U : C V is a linear/nonlinear model with additives then it is a model of the enriched effect calculus. N.B. The linear-logic syntax of EEC agrees with the interpretation of ILL in a linear/nonlinear model. EEC is a fragment of ILL interpretable in a wider class of models.
Linear Types for Continuations 15/28 Models of computational effects expand to models of EEC Call-by-push-value (CBPV) [Levy 1999,2004] is a type theory for computational effects extending Moggi s computational metalanguage [Moggi 1991] to include call-by-name. Adjunction models [Levy 2005] are the natural models of call-by-push-value (CBPV), generalising Moggi s strong monads. Every model of EEC is an adjunction model of CBPV Theorem [EMS 2009] Every adjunction model of CBPV fully embeds in a model of the enriched effect calculus. EEC is a conservative extension of CBPV (hence of Moggi s computational metalanguage) with a gain in expressivity and (essentially) no loss in range of applicability
Linear Types for Continuations 16/28 EEC as a metalanguage for effects Understand!A as Moggi s type T A of computations (with effects) that produce results of type A The call-by-value (cbv) translation [Moggi] translates a simple type σ to a value type σ v. The call-by-name (cbn) translation (cf. [Filinski, Levy]) translates σ to a computation type σ n. (σ τ) v = σ v τ v (σ τ) n = σ n τ n (σ τ) v = σ v!τ v (σ τ) n = σ n τ n. Typing judgements Γ t: σ get translated to: Γ v t v :!σ v The new linear connectives are not used! Γ n t n : σ n
Linear Types for Continuations 17/28 Part 3 Linearly-used CPS in EEC
Linear Types for Continuations 18/28 Linearly-used CPS translation of EEC to itself [EMS 2010] Let R be a chosen computation type. A value type A translates to a value type A VR. A computation type A translates to a computation type A CR. These translations satisfy A VR = A CR R. A typing judgement Γ t: A translates to Γ VR t VR : A VR A typing judgement Γ z : A u: B translates to Γ VR z : B CR u CR : A CR
Linear Types for Continuations 19/28 α VR = α α VR = α CR R α CR = α 1 VR = 1 1 CR = 0 (A B) VR = A VR B VR (A B) CR = A CR B CR (A B) VR = A VR B VR (A B) CR =! A VR B CR (!A) VR = (!A) CR R (!A) CR = A VR R (A B) VR = B CR A CR (!A B) VR = (!A B) CR R (!A B) CR = A VR B CR (0) VR = (0) CR R (0) CR = 1 (A B) VR = (A B) CR R (A B) CR = A CR B CR R VR = R R CR =! 1
Linear Types for Continuations 20/28 Recovering cbv linearly-used CPS translation (σ τ) Rv = σ Rv (τ Rv R) R (σ τ) Rv = σ Rv τ Rv Γ t: τ = Γ Rv t Rv : (τ Rv R) R Recovered as: σ Rv t Rv = (σ v ) VR = (t v ) VR
Linear Types for Continuations 21/28 Recovering cbn linearly-used CPS translation (σ τ) Rn =!(σ Rn R) τ Rn (σ τ) Rn = σ Rn τ Rn Γ t: τ = Γ Rn R t Rn : τ Rn R Recovered as: σ Rn = (σ n ) CR t Rn = (t n ) VR
Linear Types for Continuations 22/28 Fundamental theorem of EEC translation Theorem (Involution property [EMS 2010]) We have isomorphisms A = A VR VR A = CR CR A modulo which t = t VR VR and u = u CR CR. Our proof of this is semantic. The translation arises as a self-duality of the initial (syntactic) model of EEC. (Obtaining a syntactic proof looks like a formidable exercise.) Corollary The translations ( ) VR and ( ) CR are full and faithful. Corollary The translations ( ) Rv and ( ) Rn are full and faithful. [Hasegawa 2002, 2004] has analogous results for the ( ) Rv and ( ) Rn translations into ILL rather than EEC
Linear Types for Continuations 23/28 Part 4 Adding Control to EEC
Linear Types for Continuations 24/28 EEC + Control New value type: stk A. New typing rules: Γ, x: stk A t: A Γ letstk x. t: A Γ t: stk A Γ u: A Γ chngstk t. u: B Γ t: stk B Γ z : A u: B Γ t [z]u: stk A Γ ignored: stk 0 Plus suitable equations. Modelled on CBPV + Control [Levy 2004]
Linear Types for Continuations 25/28 A reformulation of EEC + Control Interpret stk A as A 0 and add canonical isomorphisms A = (A 0) 0 I.e., add constants: r A : ((A 0) 0) A and equations making them inverse to the canonical λz : A. λy : A 0. y[z] : A (A 0) 0 This appears in [MS 2007] in the context of a polymorphic language. (N.B. Canonical A = (A 0) 0 is incompatible with ILL)
Linear Types for Continuations 26/28 Jump With Argument (JWA) If we add canonical isomorphisms for computation and value types we obtain Jump With Argument [Levy 2004]. A = (A 0) 0 (1) A = (A 0) 0 (2) Principle (2) is harmless in the sense that EEC + (1) + (2) can be interpreted in EEC + (1) without changing the computation types. This is a reformulation of the equivalence between CBPV + Control and JWA in [Levy 2004]
Linear Types for Continuations 27/28 Summary The enriched effect calculus conservatively extends effect calculi (Moggi s computational metalanguage, CBPV) with much of the expressivity of intuitionistic linear logic. Models of EEC strictly generalises models of ILL. The cbv and cbn liinearly-used CPS translations extend to a translation of EEC into itself. (Such a self translation is not available for for ILL or CBPV.) Control can be added to EEC by simple axioms involving. (Such axioms do not make sense in ILL.) In general EEC is a promising language for representing linear usage of effects. E.g., linear-usage of (local) state [Møgelberg & Staton].
Linear Types for Continuations 28/28 EMS literature references [MS 2007] Relational parametricity for control considered as a computational effect. MFPS 2007. [EMS 2009] Enriching an effect calculus with linear types. CSL 2009. [EMS 2010] Linearly-used Continuations in the Enriched Effect Calculus. FoSSaCS 2010.