From Control Effects to Typed Continuation Passing Appeared in POPL ’03 See www.cs.bham.ac.uk/~hxt/research/effects.pdf Hayo Thielecke University of Birmingham 0
Introduction Earlier work: linear continuation passing; excluded call/cc . Effect system: best of both worlds, imperative and functional. Can use effects, but confine them to regions. Related to implementation: confine lifetimes (of cells or indeed con- tinuations). [Gifford, Lucassen, Jouvelot] and [Tofte and Talpin] 1
Continuation-passing style enjoys nice properties Answer type polymorphism: Γ ⊢ M : ∀ α. ( A → α ) → α Naturality, or indifference to prompts: M K = K ( M ( λx.x )) Linear continuation passing: Γ ⊢ M : ( A → α ) ⊸ α But adding call/cc breaks them all. 2
Effect systems c M : A e 1 Γ ⊢ → B ! e 2 Γ ⊢ c N : A ! e 3 Γ ⊢ c MN : B ! e 1 ∪ e 2 ∪ e 3 Γ , x : A ⊢ c M : B ! e c λx.M : A e Γ ⊢ → B ! ∅ Γ ⊢ c V : A ! ∅ α / ∈ Tyvar (Γ) Γ ⊢ c V : ∀ α.A ! ∅ Γ ⊢ c M : A ! ρ (Masking) ∈ ( Reg ( A ) ∪ Reg (Γ)) ρ / Γ ⊢ c newreg M : A ! ∅ 3
Control effects Jouvelot and Gifford, 1988: goto ρ and comefrom ρ effects. Simplification: we use just ρ for both of them. Idea: Peirce’s law (( α → β ) → α ) → α refined with regions. → α ) ρ ∪ e c call/cc : ∀ ρ. (( α ρ → β ) e Γ ⊢ → α ! ∅ Further simplification: at most singleton effects; ρ or ∅ . 4
Examples Downward continuation: h : A ρ → B ⊢ c V ( hW ) : A ! ρ If the jump is local, we can mask the effect: ⊢ c newreg ( call/cc ( λh.V ( hW ))) : A ! ∅ Upward continuation: c call/cc ( λk.λx.k ( λy.x )) : A ρ ⊢ → A ! ρ 5
CPS transform on types and effects For each region ρ , we assume a fresh type variable α ρ . A ∅ → B = ∀ α. ( B → α ) → ( A → α ) where α is fresh A ρ → B = ( B → α ρ ) → ( A → α ρ ) ∀ α.A = ∀ α.A ∀ ρ.A ∀ α ρ .A = α = α If a region ρ is not free in A , then α ρ is not free in A . 6
From regions to answer type polymorphism If Γ ⊢ c M : A ! ρ then Γ ⊢ M : ( A → α ρ ) → α ρ If Γ ⊢ c M : A ! ∅ then Γ ⊢ M : ∀ α. ( A → α ) → α where α is fresh Hence we can apply parametricity reasoning. (Theorems for free) 7
Purity and linearity The effect system demands for purity of an application the purity of the parts: c M : A ∅ ⊢ → B ! ∅ ⊢ c N : A ! ∅ ⊢ c MN : B ! ∅ Linear typing says: terms with free variables from the linear zone can only be the arguments of linear functions. For the CPSed term to be linear in its continuation: MN = λk. ( λm. N ( λn. m kn )) M ���� ���� ���� ���� ⊸ ⊸ ⊸ ⊸ 8
From control effects to polymorphic CPS Effect system Polymorphic CPS region answer type effect sharing constraint for answer types effect masking ∀ -intro for answer type The source language has effects, the target is functional, but poly- morphic. 9
Further work • Need to add recursion and assignment (store-passing?) • Pitts’s work on parametricity and operational equivalence could be applied (evaluation context ∼ = continuation). • Relate this to compiling and typed assembly language. • Stack of regions [Tofte&Talpin] 10
Recommend
More recommend