gradual typing for delimited continuationsgradual typing for delimited continuations yusuke...

45
Gradual Typing for Delimited Continuations Yusuke Miyazaki 1 , Taro Sekiyama 2 , and Atsushi Igarashi 1 1: Graduate School of Informatics, Kyoto University 2: IBM Research - Tokyo

Upload: others

Post on 02-Oct-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Gradual Typing for Delimited Continuations

Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1

1: Graduate School of Informatics, Kyoto University2: IBM Research - Tokyo

Page 2: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Motivation• Control operators (e.g., call/cc, shift/reset, control/

prompt) can express various control structures:

• Exception handling, generators, backtracking…

• Some of them have well-studied static type systems

We want to make a gradually typed language with useful control operators

Page 3: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Why Gradual Typing?Exception handling in a gradually typed language

• If b is true, then raise an exception

let f (x : *) = try ... if b then raise (Exn x) else ... catch Exn (y : int) -> ...

"x" has dynamic type

"y" expects int type

Accepted statically,checked dynamically

"*" means dynamic type

Page 4: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Base LanguageThe statically typed language with delimited control operators shift and reset (Danvy & Filinski '89)

• Simply typed lambda calculus + shift/reset

• Shift and reset can express the followings:

• Exception handling, generators, backtracking…

• Well-studied static type system

Page 5: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Our Work:• Define a gradually typed language with shift/reset

based on Danvy and Filinski’s language

• Define a cast-inserting translation into the blame calculus with shift/reset (Sekiyama et al. '15)

• Discuss properties of

• Desired to hold in gradually typed languages(Siek et al. '15)

�Gs/r

�Gs/r

Page 6: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Outline• Introduction

• Background

• Semantics for Shift and Reset

• Type System for Shift and Reset

• Gradualizing Shift and Reset

• Related Work / Conclusion

Page 7: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Shift and ResetOperators to manipulate delimited continuations

• Reset delimits a continuation captured by shift in

• Shift captures the continuation up to the closest reset as a function value, binds to it, and executes

hei

Sk.e

(where is an evaluation context w/o reset)

hE[Sk.e]i 7�! hlet k = �x.hE[x]i in ei

e

k

e

E

Page 8: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Example: escape_if• An example which imitates exception handling

• Usage

• Examples of Reduction

hescape if true; 1 + 2i 7�!? �1

hescape if false; 1 + 2i 7�!? 3

escape if = �b. Sk. if b then �1 else k ()

h· · · ; (escape if b); ei

Page 9: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

1: The shift capturesthe delimited continuation

up to the closest reset2: Bind k to the captured

continuation as a function value

Reduction in Detail

�x.hx; 1 + 2i

3: Executes the body of the shift

hescape if b; 1 + 2i

h[]; 1 + 2i

�!? h(Sk. if b then �1 else k ()); 1 + 2i

�!? hif b then �1 else (�x.hx; 1 + 2i) ()i

Page 10: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

The Case Where b is False

hE[Sk.e]i 7�! hlet k = �x.hE[x]i in eihvi 7�! v

hescape if false; 1 + 2i

7�! h�x.hx; 1 + 2i ()i

7�!? hh3ii

7�!? 3

7�!? hif false then �1 else (�x.hx; 1 + 2i ())i

Page 11: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

The Case Where b is True

hE[Sk.e]i 7�! hlet k = �x.hE[x]i in eihvi 7�! v

hescape if true; 1 + 2i

7�! h�1i

7�! �1

7�!? hif true then �1 else (�x.hx; 1 + 2i ())i

Page 12: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Type System for shift/resetAnswer type:

the type of the closest resetThe type requestedby the context of e

�;↵ ` e : ⌧

Example h(Sk. if false then �1 else k ()); 1 + 2i

Note for experts: In this talk, we explain our work with a simplified type system which does not allow so-called answer type modification.

In the paper, our language allows answer type modification.

(Danvy & Filinski '89)

;; int ` (Sk. if false then �1 else k ()) : unit

Page 13: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Annotated with an answer type

Function Type for shift/reset

�↵�! ⌧

Answer type: the type of the closest reset

at call-point

Page 14: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Escape_if in Static Typing

The type of the argument The type of the return value

The type of the closest resetat call-point

escape if : boolint��! unit

hescape if b; 1 + 2i

Page 15: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Escape_if in Static Typing

Type Error!hescape if b; 3i OK

The type of e must be int

hescape if b; truei

� = escape if : boolint��! unit, b : bool

Page 16: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Outline• Introduction

• Background

• Gradualizing Shift and Reset

• Gradualizing Typing Rules

• Semantics of

• Properties of

• Related Work / Conclusion

�Gs/r

�Gs/r

Page 17: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

In our gradually typed language, can be of * type!

Escape_if in Gradual Typing

hescape if b; (e :: ?)i

OK: Type-checked at runtime

e

� = escape if : boolint��! unit, b : bool

Page 18: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

How to Gradualize?• Replace type equality with consistency in typing rules

• Problem: Which type equality is replaced?

• Our requirement for resulting rules:

• Type-checking procedure should be simple & decidable

• Our methodology:

• Consider a type-checking algorithm for the statically typed language

• Essentially the same idea as the Gradualizer (Cimini & Siek '16)

Page 19: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Type ConsistencyExtend consistency to answer types

Page 20: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Type Checking AlgorithmConsider a straightforward algorithm by induction on the term structure.

• Judgment:

• Algorithm:

• Annotations:

�;↵ ` e : ⌧ Answer type as an input

TypeCheck(�, e,↵) = ⌧

↵x : ⌧. e, hei⌧ Add type annotations

for answer types

Page 21: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

• and appear many times in this rule

• Questions:

• Where should we insert type consistency?

• Where should we NOT insert type consistency?

Gradualizing Rule: Application

↵ ⌧1

We show how to gradualize application rule by considering a type checking algorithm

Page 22: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Gradualizing Rule: Application

Inputs of the type checking algorithm TypeCheck(�, e1 e2,↵)

Page 23: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Gradualizing Rule: ApplicationWe have the following as inputs:

�, ↵, e1

Page 24: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Gradualizing Rule: ApplicationUse the algorithm recursively

TypeCheck(�, e1,↵) = ⌧1↵0�! ⌧2

Page 25: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Gradualizing Rule: ApplicationWe have the following as inputs:

�, ↵, e2

�, ↵, e2

Page 26: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Gradualizing Rule: ApplicationUse the algorithm recursively

TypeCheck(�, e2,↵) = ⌧ 01

Page 27: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Gradualizing Rule: Application

Insert type equality checking

The Original Rule

Page 28: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Gradualizing Rule: Application

Output of the type checking algorithm TypeCheck(�, e1 e2,↵) = ⌧2

Page 29: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Gradualizing Rule: Application

Where type equality inserted → Where consistency inserted

Page 30: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Gradualizing Rules: Application / Reset / Seq

Page 31: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Resulting Typing Rules

Note: We have another typing rule for application which has dynamic typee1

Page 32: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Example Again

The type of e can be int or *

OK: Type-checked at runtimeType Error!OK

hescape if b; (e :: ?)iint

hescape if b; 3iint

hescape if b; trueiint

� = escape if : boolint��! unit, b : bool

Page 33: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Cast InsertionDefine translation into the blame calculus with shift/reset (Sekiyama et al. '15) to give the semantics

• Cast

• Judgment �;↵ ` e f : ⌧

The expression in our gradually typed language

The cast-inserted expressionin the blame calculus

e : � )` ⌧

Page 34: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Cast Insertion Rule: Reset

Insert a cast where type consistency appears

Page 35: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Example of Cast Insertion

�; int ` e f : ?

� = escape if : boolint��! unit, b : bool

Dynamically check whether the term has int type or not

hescape if b; eiint hescape if b; (f : ? )` int)i

Page 36: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Modify escape_if to return an expression of dynamic type when b is true

Another Example

d escape if : bool?�! unit

d escape if = �b. Sk. if b then (e :: ?) else k ()

Page 37: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

D_escape_if in Gradual Typing

OK: Type-checked at runtime

hd escape if b; 1 + 2iint

How this term is checked at runtime?

� = d escape if : bool?�! unit, b : bool

Page 38: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Reduction of d_escape_ifhd escape if b; 1 + 2iint

h(d escape if : (bool?�! unit) )` (bool

int��! unit)) b; 1 + 2i

Cast-insertion

If b is true:

If b is false:

�!? hhei : ? )` inti

�!? hh1 + 2 : int )` ?i : ? )` inti

See Sekiyama et al. '15for the semantics of function type casts

Page 39: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Properties ofProved properties (correctness criteria for gradually typed languages (Siek et al. '15)):

• Conservative extension

• Type preservation of cast insertion

• Type soundness

• Blame-subtyping theorem

• Monotonicity w.r.t. precision

�Gs/r

Page 40: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Summary So Far• Define a gradually typed language with shift/reset

• Gradualize existing rules by considering the type checking algorithm

• allows answer type modification in the paper

• Define a cast-insertion translation

• Details are in the paper

• Prove some properties (correctness criteria)

�Gs/r

Page 41: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Outline• Introduction

• Background

• Gradualizing Shift and Reset

• Related Work / Conclusion

Page 42: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Related Work (1/2)The Gradualizer (Cimini & Siek '16)

• Methodology to gradualize simple type systems

• Includes essentially the same step as our work

• Fails to gradualize type system with shift/reset

• Does not work well to derive cast-insertion rules

Page 43: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Related Work (2/2)Constraining delimited control with contracts(Takikawa et al. '13)

• Contract system for the other control operators with complete monitoring property

• Contracts for control operators corresponds to casts of function type (annotated with answer types)

• The semantics of casts depends on the blame calculus with shift/reset (Sekiyama et al. '15)

Page 44: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

ConclusionA gradually typed language with delimited-control operators shift and reset

• Satisfies properties that appear in Siek et al. '15:

• Conservative extension

• Type preservation of cast insertion

• Type soundness

• Blame-subtyping theorem

• Monotonicity w.r.t. precision

Page 45: Gradual Typing for Delimited ContinuationsGradual Typing for Delimited Continuations Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1 1: Graduate School of Informatics, Kyoto

Future Work• Prove the Gradual Guarantee (Siek et al. '15)

• Type inference

• Combining type inference algorithms

• For gradually typed languages(Siek & Vachharajani '08, Garcia & Cimini '15)

• For shift/reset (Asai & Kameyama '07)