gradual typing for delimited continuationsgradual typing for delimited continuations yusuke...
TRANSCRIPT
Gradual Typing for Delimited Continuations
Yusuke Miyazaki1, Taro Sekiyama2, and Atsushi Igarashi1
1: Graduate School of Informatics, Kyoto University2: IBM Research - Tokyo
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
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
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
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
Outline• Introduction
• Background
• Semantics for Shift and Reset
• Type System for Shift and Reset
• Gradualizing Shift and Reset
• Related Work / Conclusion
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
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
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
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
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
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
Annotated with an answer type
Function Type for shift/reset
�↵�! ⌧
Answer type: the type of the closest reset
at call-point
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
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
Outline• Introduction
• Background
• Gradualizing Shift and Reset
• Gradualizing Typing Rules
• Semantics of
• Properties of
• Related Work / Conclusion
�Gs/r
�Gs/r
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
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)
Type ConsistencyExtend consistency to answer types
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
• 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
Gradualizing Rule: Application
Inputs of the type checking algorithm TypeCheck(�, e1 e2,↵)
Gradualizing Rule: ApplicationWe have the following as inputs:
�, ↵, e1
Gradualizing Rule: ApplicationUse the algorithm recursively
TypeCheck(�, e1,↵) = ⌧1↵0�! ⌧2
Gradualizing Rule: ApplicationWe have the following as inputs:
�, ↵, e2
�, ↵, e2
Gradualizing Rule: ApplicationUse the algorithm recursively
TypeCheck(�, e2,↵) = ⌧ 01
Gradualizing Rule: Application
Insert type equality checking
The Original Rule
Gradualizing Rule: Application
Output of the type checking algorithm TypeCheck(�, e1 e2,↵) = ⌧2
Gradualizing Rule: Application
Where type equality inserted → Where consistency inserted
Gradualizing Rules: Application / Reset / Seq
Resulting Typing Rules
Note: We have another typing rule for application which has dynamic typee1
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
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 : � )` ⌧
Cast Insertion Rule: Reset
Insert a cast where type consistency appears
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
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 ()
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
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
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
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
Outline• Introduction
• Background
• Gradualizing Shift and Reset
• Related Work / Conclusion
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
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)
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
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)