ICFP 2011 "Subtyping Delimited Continuations" slides

Post on 01-Dec-2014

375 views 0 download

description

Slides from my talk on ICFP 2011.

Transcript of ICFP 2011 "Subtyping Delimited Continuations" slides

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

Subtyping delimited continuations

Marek Materzok, Dariusz BiernackiSep 19, 2011

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

Evaluation contexts

Evaluation context is a ,,term with a hole”:

if sq(2) = 4 then 1 else 0

I green part – evaluation context

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

Partial evaluation contexts

One can consider partial contexts:

if sq(2) = 4 then 1 else 0

I green part – evaluation context

I yellow part – partial evaluation context

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

Contexts can have types

if sq(2) = 4 then 1 else 0

I green part – int→ ⊥I yellow part – int→ bool

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

Delimited control operators

They allow to reify partial contexts as functions.Delimited control has lots of applications, includingasynchronous I/O, Web programming, mobile code,linguistics, and so on.

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

Delimited control operators

Delimited control operators usually come in pairs:

I the delimiter, which marks where a context begins,

I capture operator, which reifies the context up to thenearest delimiter.

Example:

1 + 〈2 + Sf.f(f 3) 〉

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

Delimited control operators shift/reset

The most known and well explored delimited controloperators are shift/reset. The shift operator captures thecontext up to (and including) the nearest delimiter andresumes execution in a empty context.

〈K[Sf.e]〉 〈e{f/λx.〈K[x]〉}〉〈v〉 v

They have many nice and desirable properties, including asimple CPS translation and a type system guaranteeingtermination.However, they feel very restrictive.

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

Delimited control operators shift0/reset0

A variant of shift/reset operators. The difference is thatwhen shift0 executes, the execution resumes in thesurrounding context. This allows the shift0 operator to,,reach” beyond the nearest surrounding delimiter.

1 + 〈2 + 〈3 + S0f.S0g.f (g (g 4)) 〉 〉

The term above evaluates to 12. (f gets theyellow context , g gets the cyan one .)

Formally:

〈K[S0f.e]〉 e{f/λx.〈K[x]〉}

Shift kept the enclosing delimiter:

〈K[Sf.e]〉 〈e{f/λx.〈K[x]〉}〉

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

Delimited control operators shift0/reset0

A variant of shift/reset operators. The difference is thatwhen shift0 executes, the execution resumes in thesurrounding context. This allows the shift0 operator to,,reach” beyond the nearest surrounding delimiter.

1 + 〈2 + 〈3 + S0f.S0g.f (g (g 4)) 〉 〉

The term above evaluates to 12. (f gets theyellow context , g gets the cyan one .)

Formally:

〈K[S0f.e]〉 e{f/λx.〈K[x]〉}

Shift kept the enclosing delimiter:

〈K[Sf.e]〉 〈e{f/λx.〈K[x]〉}〉

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

Delimited control operators shift0/reset0

But shift0/reset0 seemed not to be as nice as shift/reset, sothey didn’t get much interest.It turned out that they are in a sense more fundamental thanshift/reset; the type system and CPS translation forshift/reset are a fragment of our type system and CPStranslation, and can be derived from them.

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

Example program – partition

The following function partitions a list of numbers into threeparts with all numbers less than, equal to, and greater thanthe given number. The relative order within the partitionsremains the same.

let partition a l =

let part l = case l {

Nil -> []

| Cons(h,t) ->

if h > a

then Cons(h, part t)

else if h == a

then @f. Cons(h, <f (part t)>)

else @f g. Cons(h, <g <f (part t)>>)

} in <<part l>>

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

Typing shift/reset

For shift/reset, we only need to maintain type informationabout the top context. This idea gives rise to the typesystem of Danvy and Filinski, with following judgements:

Γ; γ ` e : α; δ

Which mean that the expression e, when evaluated in acontext of type α→ γ, gives an answer of type δ.The function types are also annotated:

β; γ → α; δ

This type denotes a function which, when given a value oftype β and evaluated in a context of type α→ γ, gives ananswer of type δ.

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

Typing shift/reset

The subexpression of reset is run with an empty context,which has type α→ α. Requiring the final answer type γ ofthe subexpression to be equal to the surrounding context’sargument type enforces the composing requirement:

Γ;α ` e : α; γ

Γ; δ ` 〈e〉 : γ; δreset

The subexpression of shift is also run with an empty context(of type β → β). The enclosing context is captured, which isreflected in the type of f below:

Γ, f : (α; ρ→ γ; ρ);β ` e : β; δ

Γ; γ ` Sf.e : α; δshift

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

Anatomy of a typed context stack (shift/reset)

For shift/reset in the type system of Danvy and Filinski, forthe term typed:

; γ ` e : α; δ

The context stack looks like this:

βn−1 → βn

βn−2 → βn−1

. . .

β1 → β2

δ → β1

α→ γ context

metacontext

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

Typing shift0/reset0

Because the shift0 operator can access arbitrary contexts onthe stack, we need to maintain information about morecontexts than just the topmost one. Thus we introduceeffect annotations:

σ ::= ε | [α σ] α σ

We have typing judgements of the form:

Γ ` e : α σ

The meaning of e : β1 [α1 σ1] . . . βn [αn σn] β is that e givesa final answer of type β when evaluated in a context stackwith types βi

σi−→ αi. (Function types are also annotated.)

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

Typing shift0/reset0

Expressions with types annotated with ε (which we willomit) are pure (don’t have control effects). Thus thefollowing rules for variables and lambda abstractions:

Γ, x : α ` x : αvar

Γ, x : α ` e : β σ

Γ ` λx.e : ασ−→ β

abs

Shift0 and reset0 only eliminate or introduce a context:

Γ, f : ασ′−→ β ` e : γ σ

Γ ` S0f.e : α [β σ′] γ σshift0

Γ ` e : β [β] α σ

Γ ` 〈e〉 : α σreset0

But how to put a variable inside a reset0?

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

Typing shift0/reset0 – with subtyping!

We introduce subtyping on effect annotations. This allowsto ,,forget” about some contexts we know about, but onlywhen the context we forget can be composed with the onesbelow. This is formalized by the following rule:

α σ ≤ α′ σ′

ε ≤ [α σ] α′ σ′

We also have following structural rules:

ε ≤ εα2 σ2 ≤ α1 σ1 α′1 σ

′1 ≤ α′2 σ′2

[α1 σ1] α′1 σ′1 ≤ [α2 σ2] α

′2 σ′2

The notation α1 σ1 ≤ α2 σ2 is equivalent to α1 ≤ α2 andα2 ≤ σ2.

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

Success!

We can type all terms typable with Danvy and Filinski’s typesystem.And we can type many more, including partition!

a : int ` part : list(int) [list(int)] list(int) [list(int)] list(int)

The contexts on the stack are of type list(int)→ list(int).

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

Anatomy of a typed context stack (shift0/reset0)

For our type system, for the term typed:

` e : β1 [α1 σ1] . . . βn [αn σn] β

βn+m+1 → αn+m+1

βn+mσn+m−−−→ αn+m

. . .

βn+1σn+1−−−→ αn+1

βnσn−→ αn

. . .

β1σ1−→ α1

pure context

trail

contexts

metacontext

β ≤ βn+1 [αn+1 σn+1] . . . βn+m [αn+m σn+m] βn+m+1

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

What properties does our type system have?

I Strong type soundness – progress and type preservation.

I Termination.

I Decidable type inference.

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

Standard CPS translation for shift/reset

JxK = λk.k x

Jλx.eK = λk.k (λx.JeK)Je1 e2K = λk.Je1K (λf.Je2K (λx.f x k))

J〈e〉K = λk.k (JeK (λx.x))

JSf.eK = λk.JeK{f/λx.λk′.k(k′x)} (λx.x)

Only one continuation available – this translation isunsuitable for shift0 and reset0.

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

CPS translation for shift0/reset0

Use more lambdas to bind more of the context stack!

JxK =λk.k x

Jλx.eK =λk.k (λx.JeK)Je1e2K =λk.Je1K(λf.Je2K(λx.f x k))

J〈e〉K =JeK(λx.λk.k x)

JS0f.eK =λf.JeK

This translation preserves the semantics of the shift0/reset0control operators.

However, it doesn’t preserve types. The translation of thefollowing term (which is well-typed in our type system)cannot be given a type in the simply typed lambda calculus:

λx.S0f.f 〈f x〉

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

CPS translation for shift0/reset0

Use more lambdas to bind more of the context stack!

JxK =λk.k x

Jλx.eK =λk.k (λx.JeK)Je1e2K =λk.Je1K(λf.Je2K(λx.f x k))

J〈e〉K =JeK(λx.λk.k x)

JS0f.eK =λf.JeK

This translation preserves the semantics of the shift0/reset0control operators.However, it doesn’t preserve types. The translation of thefollowing term (which is well-typed in our type system)cannot be given a type in the simply typed lambda calculus:

λx.S0f.f 〈f x〉

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

Type-directed CPS translation

The solution is to make the translation directed by types.We want to have the property that if

Γ ` e : β1 [α1 σ1] . . . βn [αn σn] β

Then

JΓK ` JeK : (Jβ1K→ Jα1σ1K)→ · · · → (JβnK→ JαnσnK)→ JβK

Subtyping will be translated as coercion functions, with thefollowing property:

` Jα ≤ α′K : JαK→ Jα′K` Jα σ ≤ α′ σ′K : Jα σK→ Jα′ σ′K

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

Type-directed CPS translation

We translate types and annotated types as follows:

JαK =α

Jτ1σ−→ τ2K =Jτ1K→ Jτ2 σK

Jτ εK =JτKJτ [τ1 σ1] τ2 σ2K =(JτK→ Jτ1 σ1K)→ Jτ2 σ2K

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

Type-directed CPS translation

JxKvar =x

JeKsub(D,τ σ≤τ ′ σ′) =Jτ σ ≤ τ ′ σ′KJeKDJλx.eKabs(D) =λx.JeKD

JfeKapp-pure(D1,D2) =JfKD1JeKD2

JfeKapp(D1,D2) =λk.JfKD1(λf.JeKD2(λe.fek))

JS0f.eKshift0(D) =λf.JeKDJ〈e〉Kreset0(D) =JeKD(λx.x)

Fun fact: from this definition one can reconstruct thestandard CPS translation for shift and reset.

Subtypingdelimited

continuations

Marek Materzok,Dariusz Biernacki

Introduction

Evaluation contexts

Delimited control

Shift0 and reset0

Type system

Typing shift/reset

Typing shift0/reset0

CPS translation

Untyped translation

Type-directedtranslation

Conclusions

Conclusions

We have introduced a type system for shift0/reset0 operatorswith strong type soundness and termination, and two newCPS translations for these operators. These results provide asolid foundation for further research and for implementingthese operators in programming languages.Thank you for your attention!