From Control Effects to Typed Continuation Passing Appeared in POPL - - PowerPoint PPT Presentation

from control effects to typed continuation passing
SMART_READER_LITE
LIVE PREVIEW

From Control Effects to Typed Continuation Passing Appeared in POPL - - PowerPoint PPT Presentation

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 .


slide-1
SLIDE 1

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

slide-2
SLIDE 2

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

slide-3
SLIDE 3

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

slide-4
SLIDE 4

Effect systems Γ ⊢

c M : A e1

→ B ! e2 Γ ⊢

c N : A ! e3

Γ ⊢

c MN : B ! e1 ∪ e2 ∪ e3

Γ, x : A ⊢

c M : B ! e

Γ ⊢

c λx.M : A e

→ B ! ∅ Γ ⊢

c V : A ! ∅

Γ ⊢

c V : ∀α.A ! ∅

α / ∈ Tyvar(Γ) (Masking) Γ ⊢

c M : A ! ρ

Γ ⊢

c newreg M : A ! ∅

ρ / ∈ (Reg(A) ∪ Reg(Γ))

3

slide-5
SLIDE 5

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. Γ ⊢

c call/cc : ∀ρ.((α ρ

→ β) e → α) ρ∪e → α ! ∅ Further simplification: at most singleton effects; ρ or ∅.

4

slide-6
SLIDE 6

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

slide-7
SLIDE 7

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

slide-8
SLIDE 8

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

slide-9
SLIDE 9

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

  • nly be the arguments of linear functions.

For the CPSed term to be linear in its continuation: MN = λk.

M

(λm. N

(λn. m

kn))

8

slide-10
SLIDE 10

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

slide-11
SLIDE 11

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