Shifting the Blame A blame calculus with delimited control Taro Sekiyama* Atsushi Igarashi Soichiro Ueda Kyoto University Gradual typing.

Slides:



Advertisements
Similar presentations
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Advertisements

Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Static Contract Checking for Haskell Dana N. Xu University of Cambridge Ph.D. Supervisor: Simon Peyton Jones Microsoft Research Cambridge.
- Vasvi Kakkad.  Formal -  Tool for mathematical analysis of language  Method for precisely designing language  Well formed model for describing and.
The lambda calculus David Walker CS 441. the (untyped) lambda calculus a language of functions e ::= x | \x.e | e1 e2 v ::= \x.e there are several different.
Current Techniques in Language-based Security David Walker COS 597B With slides stolen from: Steve Zdancewic University of Pennsylvania.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
1 PROPERTIES OF A TYPE ABSTRACT INTERPRETATER. 2 MOTIVATION OF THE EXPERIMENT § a well understood case l type inference in functional programming à la.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Introduction to Programming Languages Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University.
Type checking © Marcelo d’Amorim 2010.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
1 Space-Efficient Gradual Typing David Herman Northeastern University Aaron Tomb, Cormac Flanagan University of California, Santa Cruz.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
Control Flow Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
1 Enforcing Confidentiality in Low-level Programs Andrew Myers Cornell University.
Misc. Announcements Assignment available end of the day today –Due back in 11/03 (after break) Will also update slides on website –Today Midterm next week.
Type Checking in Cool Alex Aiken (Modified by Mooly Sagiv)
Catriel Beeri Pls/Winter 2004/05 types 65  A type-checking algorithm The task: (since we start with empty H, why is the goal not just E?) The rule set.
MinML: an idealized programming language CS 510 David Walker.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Cormac Flanagan University of California, Santa Cruz Hybrid Type Checking.
Software Engineering Prof. Dr. Bertrand Meyer March 2007 – June 2007 Chair of Software Engineering Static program checking and verification Slides: Based.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
CSC-682 Cryptography & Computer Security Sound and Precise Analysis of Web Applications for Injection Vulnerabilities Pompi Rotaru Based on an article.
Typed Lambda Calculus Chapter 9 Benjamin Pierce Types and Programming Languages.
Lesson 4 Typed Arithmetic Typed Lambda Calculus 1/21/02 Chapters 8, 9, 10.
Sound Haskell Dana N. Xu University of Cambridge Joint work with Simon Peyton Jones Microsoft Research Cambridge Koen Claessen Chalmers University of Technology.
Typing Local Control and State Using Flow Analysis Arjun Guha, Claudiu Saftoiu, and Shriram Krishnamurthi 1.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.
Chapter 3 Part II Describing Syntax and Semantics.
12/2/20151 GC16/3011 Functional Programming Lecture 2 The Lambda Calculus: A Simple Introduction.
Implementing a Dependently Typed λ -Calculus Ali Assaf Abbie Desrosiers Alexandre Tomberg.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
Just Enough Type Theory or, Featherweight Java A Simple Formal Model of Objects Jonathan Aldrich
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
CMSC 330: Organization of Programming Languages Lambda Calculus and Types.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
COMP 412, FALL Type Systems II C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Types and Programming Languages Lecture 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CMSC 330: Organization of Programming Languages Operational Semantics.
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Prof. Necula CS 164 Lecture 171 Operational Semantics of Cool ICOM 4029 Lecture 10.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
The Ins and Outs of Gradual Type Inference Avik Chaudhuri Basil Hosmer Adobe Systems Aseem Rastogi Stony Brook University.
Comp 205: Comparative Programming Languages The Untyped λ-calculus representing values Lecture notes, exercises, etc., can be found at:
Chapter 4: Types. Why Have Types? Detect simple kinds of errors: int square(int x) { return x*x; }... int bogus = square(“hello”); String bogus2 = square(3);
COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
An Integrated Theory of Type-Based Static and Dynamic Verification Taro Sekiyama Dept. of Communications and Computer Engineering Graduate School of Informatics.
Language-Based Security: Overview of Types Deepak Garg Foundations of Security and Privacy October 27, 2009.
CS314 – Section 5 Recitation 9
Programming Languages and Compilers (CS 421)
CSE-321 Programming Languages Simply Typed -Calculus
Type Systems Terms to learn about types: Related concepts: Type
Lesson 4 Typed Arithmetic Typed Lambda Calculus
Stateful Manifest Contracts
Chapter 4: Types.
FP Foundations, Scheme In Text: Chapter 14.
Language-based Security
CSCE 314: Programming Languages Dr. Dylan Shell
Type Systems Terms to learn about types: Related concepts: Type
Recursive Procedures and Scopes
Static Contract Checking for Haskell
Abstract Types Defined as Classes of Variables
Presentation transcript:

Shifting the Blame A blame calculus with delimited control Taro Sekiyama* Atsushi Igarashi Soichiro Ueda Kyoto University Gradual typing

Two styles of typing Static typing Dynamic typing Consistency of programs is checked before their execution Good: Early bug detection/Optimization NG: Rejecting programs without no bugs E.g., ML, Haskell, Java, etc. Consistency of programs is checked during their execution Good: Flexible and rapid development : More burdens to detect bugs E.g., Scheme, Python, Perl, etc.

Two styles of typing Dynamic typing Gradual typing Static typing where … static and dynamic typing are integrated typed and untyped code coexist –Programs are annotated with types “gradually

Two styles of typing Dynamic typing Gradual typing Static typing where typed and untyped code coexist Programs are annotated with types “gradually”

Two styles of typing Dynamic typing Gradual typing Static typing

Gradual typing [Tobin-Hochstadt&Felleisen’06,Siek&Taha’07] Integration of static and dynamic typing Typed and untyped code can coexist and interact Program

Blame calculus [Tobin-Hochstadt&Felleisen’06,Wadler&Findler’09] A simply typed lambda calculus to model intermediate languages for gradual typing The Dynamic type (Dyn for short) –The type given to untyped code Casts (type coercions) t : S T –A device to interact typed and untyped code intDyn

Blame calculus [Tobin-Hochstadt&Felleisen’06,Wadler&Findler’09] A typed lambda calculus to model intermediate languages for gradual typing The Dynamic type (Dyn for short) –The type for untyped code Casts (type coercions) s : S T –A type-directed mechanism to monitor value flows between typed and untyped code Term s of type S is coerced to type T

Blame calculus [Tobin-Hochstadt&Felleisen’06,Wadler&Findler’09] A typed lambda calculus to model intermediate languages for gradual typing The Dynamic type (Dyn for short) –The type for untyped code Casts (type coercions) s : S T –coerce term s of type S to type T –are used to monitor value flows between typed and untyped code

Example let x = if … then succ else true let y : string = if x then “true” else “false” Gradually typed lang. let x : Dyn = if…then succ : int→int Dyn else true : bool Dyn let y : string = if (x : Dyn bool ) then “true” else “false” Blame calculus Untyped code is given Dyn Injection of typed values into untyped code Run-time test to check that the dynamic value is a Boolean

Cast semantics TypedUntyped 1 : intDyn

Cast semantics TypedUntyped Dyn : int1int

Cast semantics TypedUntyped Dyn : int1bool Cast fails! 1 is not a Boolean

This work Extending gradual typing with delimited- control operators shift/reset –An extension of blame calculus [Tobin-Hochstadt &Felleisen’06,Wadler&Findler’09] with a new form of cast Defining continuation passing style (CPS) transformation for the extended calculus Investigating three properties –Type soundness –Soundness of the CPS transformation –Blame Theorem

What blame calculus should guarantee If something wrong happens, it is detected as cast failure Type Soundness Statically typed terms are never sources of cast failure Blame Theorem E.g., an integer is called as a function

What gradual typing should guarantee If something wrong happens, it is detected as cast failure Well typed sides do not cause cast failure E.g., an integer is called as functions NOT trivial in a blame calculus with control operators

What gradual typing should guarantee If something wrong happens, it is detected as cast failure E.g., an integer is called as functions ? How should we design blame calculus to guarantee it?

Design criterion of blame calculus All value flows between typed and untyped parts should be monitored by casts All uses of dynamic values are completely monitored

Problem in introducing control operators naively The criterion is not satisfied Terms with control operators can: return a value capture a continuation call a continuation

Terms with control operators can: return a value capture a continuation call a continuation Problem in introducing control operators naively monitored by the early blame calculus The criterion is not satisfied

Terms with control operators can: return a value capture a continuation call a continuation Problem in introducing control operators naively monitored by the early blame calculus We design a system to monitor these actions The criterion is not satisfied

Problem in introducing control operators naively The criteria is invalidated Values can be passed via captured continuations not to be monitored by casts

This work Extending the blame calculus with delimited- control operators shift/reset –A new form of cast to satisfy the criteria Defining continuation passing style (CPS) transformation for the extended calculus Investigating three properties –Type soundness –Soundness of the CPS transformation –Blame Theorem

This work Extends the blame calculus with delimited- control operators shift/reset –A new form of cast to monitor capturing and calling continuations Defines continuation passing style (CPS) transformation for the extended calculus Investigates three properties –Type soundness –Blame Theorem –Soundness of the CPS transformation can implement various control effects

Challenge for Type Soundness All value flows between typed and untyped parts have to be monitored by casts Paraphrase of Type Soundness It isn’t trivial to satisfy this property

Challenge for Type Soundness Because terms with control operators can take more actions To return a value To capture a continuation To call a continuation Paraphrase of Type Soundness It isn’t trivial to satisfy this property All value flows between typed and untyped parts have to be monitored by casts

Because terms with control operators can take more actions To return a value To capture a continuation To call a continuation monitored by the early blame calculus Challenge for Type Soundness Paraphrase of Type Soundness It isn’t trivial to satisfy this property All value flows between typed and untyped parts have to be monitored by casts

Because terms with control operators can take more actions To return a value To capture a continuation To call a continuation monitored by the early blame calculus Challenge for Type Soundness Paraphrase of Type Soundness It isn’t trivial to satisfy this property All value flows between typed and untyped parts have to be monitored by casts We design a system to monitor these

Challenge in the presence of control operators All value flows between typed and untyped parts should be monitored by casts It isn’t trivial to satisfy the crucial property: All uses of dynamic values are completely monitored

Challenge in the presence of control operators All value flows between typed and untyped parts should be monitored by casts It isn’t trivial to satisfy the crucial property: Because terms with control operators can take more actions To return a value To capture a continuation To call a continuation All uses of dynamic values are completely monitored

Challenge in the presence of control operators All value flows between typed and untyped parts should be monitored by casts It isn’t trivial to satisfy the crucial property: All uses of dynamic values are completely monitored Because terms with control operators can take more actions To return a value To capture a continuation To call a continuation monitored by the early blame calculus

Challenge in the presence of control operators All value flows between typed and untyped parts should be monitored by casts It isn’t trivial to satisfy the crucial property: Because terms with control operators can take more actions To return a value To capture a continuation To call a continuation We design a system to monitor these monitored by the early blame calculus All uses of dynamic values are completely monitored

Problem in introducing control operators naively The criterion is not satisfied Terms with control operators can: return a value capture a continuation call a continuation

Terms with control operators can: return a value capture a continuation call a continuation Problem in introducing control operators naively monitored by the early blame calculus The criterion is not satisfied

Terms with control operators can: return a value capture a continuation call a continuation Problem in introducing control operators naively monitored by the early blame calculus We design a system to monitor these actions The criterion is not satisfied

This work Extending the blame calculus with delimited- control operators shift/reset –A new form of cast for shift/reset is supported Defining continuation passing style (CPS) transformation for the extended calculus Investigating three properties –Type soundness –Soundness of the CPS transformation –Blame Theorem

Outline 1.Introduction 2.Background: blame calculus (without shift/reset) 3.Problem with control operators 4.Our extension of the blame calculus with shift/reset

Blame calculus [Tobin-Hochstadt&Felleisen’06,Wadler&Findler’09] A simply typed lambda calculus to model intermediate languages for gradually typing The Dynamic type (Dyn for short) –The type given to untyped code Casts (type coercions) t : S T –A device to interact typed and untyped code intDyn

Blame calculus [Wadler&Findler’09] λ with Dyn and casts Types S, T ::= int | … | S → T | Dyn Terms s, t ::= 1 | + | … | λ x.s | s t | s : S T → the type for untyped code monitors that the value of s at S can behave as T

Cast semantics 1 : int Dyn 1 : int

Cast semantics 1 : int Dyn

Cast semantics 1 1

: Dyn int 1 : int 1 : Dyn bool 1 : int Cast fails! 1 is not a Boolean

Cast semantics : Dyn int 1

Cast semantics 1 Cast fails! 1 is not a Boolean : Dyn bool 1 1

Cast semantics λx.s λx. x+1 x λx. λx.s

Cast semantics ( ) v : int→int v’ let v (v’ <= int

Cast semantics v : (int→int) Dyn λx:Dyn.v (x :

Cast semantics v : (int→int) Dyn λx:Dyn.v (x :

Example Source let x = if … then

Example let x = if … then succ else true let y : string = if x then “true” else “false” Gradually typed lang. let x : Dyn = if…then succ : int→int Dyn else true : bool Dyn let y : string = if (x : Dyn bool ) then “true” else “false” Blame calculus

Example let x = if … then succ else true let y : string = if x then “true” else “false” Gradually typed lang. let x = Dyn if…then succ : int→int Dyn else true : bool Dyn let y : string = if (x : Dyn bool ) then “true” else “false” Blame calculus

Example let x = if … then succ else true let y : string = if x then “true” else “false” Gradually typed lang. let x = Dyn if…then succ : int→int Dyn else true : bool Dyn let y : string = if (x : Dyn bool ) then “true” else “false” Blame calculus

Notation let x : Dyn = if…then succ : int→int Dyn else true : bool Dyn let y : string = if (x : Dyn bool ) then “true” else “false” Blame calculus typed code untyped code cast

Notation let x = Dyn if…then succ : int→int Dyn else true : bool Dyn let y : string = if (x : Dyn bool ) then “true” else “false” Blame calculus typed code untyped code cast

Notation let x = Dyn if…then succ : int→int Dyn else true : bool Dyn let y : string = if (x : Dyn bool ) then “true” else “false” Blame calculus typed code untyped code cast

Notation let x = Dyn if…then succ Dyn else true Dyn let y : string = if (x) then Dyn “true” else “false” Blame calculus typed code untyped code cast

Example let x = if … then succ else true let y : string = if x then “true” else “false” Gradually typed lang. let x = Dyn if…then succ Dyn else true Dyn let y : string = if x then Dyn “true” else “false” Blame calculus

Example let x = if … then succ else true let y : string = if x then “true” else “false” Gradually typed lang. let x = Dyn if…then succ Dyn else true Dyn let y : string = if x then Dyn “true” else “false” Blame calculus

let x = if … then … else First-order cast typed to untyped true true : bool Dyn Casts from base types to Dyn always succeeds Result values have the target value (true) and its type (bool)

Cast semantics true : bool Dyn Casts from base types to Dyn always succeed Result values have the target value (true) and its type (bool) true bool

Cast semantics : Dyn bool true Casts from Dyn succeed if the tagged type matches with the target type true bool

Cast semantics : Dyn int Casts from Dyn fail if the tagged type doesn’t match with the target type Cast failure bool ≠ int true bool

Cast semantics succ : (int→int) Dyn : Dyn→Dyn λx. succ x x : Dyn int (succ …) : int Dyn Casts from function types to Dyn generate wrappers of the target function All value flows between typed and untyped parts are monitored by casts

Cast semantics succ : (int→int) Dyn λx. succ x (succ …) : int Dyn Casts from function types to Dyn generate wrappers of the target function All value flows between typed and untyped parts are monitored by casts x : Dyn int Dyn→Dyn

Cast semantics succ : (int→int) Dyn λx. succ x (succ …) : int Dyn Casts from function types to Dyn generate wrappers of the target function All value flows between typed and untyped parts are monitored by casts x : Dyn int Func

Cast semantics succ : (int→int) Dyn λx. let y = succ in y x (let …) : int Dyn Casts from function types to Dyn generate wrappers of the target function All value flows between typed and untyped parts are monitored by casts x : Dyn int Func

Cast semantics ( ) v : int→int v’ let v (v’ <= int

Cast semantics 1 1

: Dyn int 1 : int 1 : Dyn bool 1 : int Cast fails! 1 is not a Boolean

Cast semantics : Dyn int 1

Cast semantics 1 Cast fails! 1 is not a Boolean : Dyn bool 1 1

Cast semantics λx.s λx. x+1 x λx. λx.s

Cast semantics ( ) v : int→int v’ let v (v’ <= int

Cast semantics v : (int→int) Dyn λx:Dyn.v (x :

Cast semantics v : (int→int) Dyn λx:Dyn.v (x :

Example Source let x = if … then

Cast semantics TypedUntyped Dyn 1 int

Cast semantics TypedUntyped Dyn : int1int

Cast semantics TypedUntyped Dyn : int1bool Cast fails! 1 is not a Boolean

Cast semantics TypedUntyped Dyn : int1 λ : int → bool

Cast semantics TypedUntyped λ x. : int → bool Dyn

Problem in the presence of control operators The standard cast semantics would overlook value flows between typed and untyped code STLC: functions interact with callers STLC with control operators: functions interact not only callers but also contexts

Outline 1.Introduction 2.Background: blame calculus (without shift/reset) 3.Problem with control operators 4.Our extension of the blame calculus with shift/reset

Problem with control operators Cast semantics has to be extended to deal with types of contexts STLC: functions interact with callers STLC with control operators: functions interact not only callers but also contexts Cf. Takikawa et al. [ESOP’13]

Problem with control operators All value flows between typed and untyped code are NOT monitored Values can be passed via captured continuations not to be monitored by casts

Problem with control operators NOT all value flows between typed and untyped code are monitored because the standard cast semantics cannot monitor capturing and calling continuations

Shift and reset CPS-based operators to manipulate “delimited” continuations Reset 〈 s 〉 delimits continuations in s Shift (S k. s) captures continuations up to the closest reset as k 〈 E[S k. s] 〉 〈 s[k:=λx. 〈 E[x] 〉 ] 〉 where E is an evaluation context without reset used to impl. exceptions, backtracking, monads, etc. 〈 v 〉 v

Shift and reset CPS-based operators to manipulate “delimited” continuations Reset 〈 s 〉 delimits continuations in s Shift (S k. s) captures continuations up to the closest reset as k 〈 E[S k. s] 〉 〈 s[k:=λx. 〈 E[x] 〉 ] 〉 where E is an evaluation context without reset used to impl. exceptions, backtracking, monads, etc.

Reduction example 〈 3 + (S k. (k 1) == 4) 〉 = 〈 E[S k. (k 1) == 4] 〉 where E = 3 + [] 〈 (k 1) == 4 〉 λx. 〈 3 + x 〉 〈〈 3+1 〉 == 4 〉 * true

1) == 4 〉 (λx. 〈 3 + x 〉 ) k Reduction example 〈 3 + (S k. (k 1) == 4) 〉 = 〈 E[S k. (k 1) == 4] 〉 where E = 3 + [] 〈(〈( captured continuation 〈〈 3+1 〉 == 4 〉 * true

1) == 4 〉 k Reduction example 〈 3 + (S k. (k 1) == 4) 〉 = 〈 E[S k. (k 1) == 4] 〉 where E = 3 + [] 〈(〈(

1) == 4 〉 (λx. 〈 3 + x 〉 ) k Reduction example 〈 3 + (S k. (k 1) == 4) 〉 = 〈 E[S k. (k 1) == 4] 〉 where E = 3 + [] 〈(〈( captured continuation 〈〈 3+1 〉 == 4 〉 * true

Reduction example 〈 3 + (S k. (k 1) == 4) 〉 = 〈 E[S k. (k 1) == 4] 〉 where E = 3 + [] 〈 ( k 1) == 4 〉

v = λx:int. 3 + (S k. (k 1) == x) Problem of the standard cast semantics in the presence of shift/reset v : (int→int) Dyn λy. v y Func

Problem of the standard cast semantics in the presence of shift/reset v = λx:int. 3 + (S k. (k 1) == x) 〈 f ((λy. ) 4) 〉 v y 〈 f ( ) 〉 v 4 : Dyn int 4 〈 f ( ) 〉 v 4 〈 f ( ) 〉 3 + (S k. (k 1) == 4) v : (int→int) Dyn Typed function v is called under the untyped context (“context” is the body of the reset)

Problem of the standard cast semantics in the presence of shift/reset v = λx:int. 3 + (S k. (k 1) == x) 〈 f ((λy. ) 4) 〉 v y 〈 f ( ) 〉 v 4 : Dyn int 4 〈 f ( ) 〉 v 4 〈 f ( ) 〉 3 + (S k. (k 1) == 4) v : (int→int) Dyn

Problem of the standard cast semantics in the presence of shift/reset … 〈 1) == 4 〈 f ( ) 〉 3 + (S k. (k 1) == 4) k ( 〉

Problem of the standard cast semantics in the presence of shift/reset … 〈 1) == 4 〈 f ( ) 〉 3 + (S k. (k 1) == 4) k ( 〉 captured continuation

Problem of the standard cast semantics in the presence of shift/reset … 〈 1) == 4 〈 f ( ) 〉 3 + (S k. (k 1) == 4) k ( 〉 captured continuation

Problem of the standard cast semantics in the presence of shift/reset … 〈 ( ) 1) == 4 〈 f ( ) 〉 3 + (S k. (k 1) == 4) ( 〉 あいう λz. 〈 〉 f ( ) 3 + z captured continuation

λz. 〈 〉 Problem of the standard cast semantics in the presence of shift/reset … 〈 1) == 4 〈 f ( ) 〉 3 + (S k. (k 1) == 4) All value flows between typed and untyped parts are NOT monitored k ( captured continuation 〉 f ( ) 3 + z

λz. 〈 〉 Problem of the standard cast semantics in the presence of shift/reset … 〈 1) == 4 〈 f ( ) 〉 3 + (S k. (k 1) == 4) All value flows between typed and untyped parts are NOT monitored 1) == 4 〉 (λx. 〈 3 + x 〉 ) k ( captured continuation 〉 f ( ) 3 + z

λz. 〈 〉 f ( ) Problem of the standard cast semantics in the presence of shift/reset … 〈 〉 (k 1) == z 〈 f ( ) 〉 3 + (S k. (k 1) == 4) All value flows between typed and untyped parts are NOT monitored

Problem of the standard cast semantics in the presence of shift/reset v = λx:int. 3 + (S k. (k 1) == x) 〈 f ((λy. ) 4) 〉 let z = v in z y v : (int→int) Dyn

Problem of the standard cast semantics in the presence of shift/reset v = λx:int. 3 + (S k. (k 1) == x) 〈 f ((λy. ) 4) 〉 v y 〈 f ( ) 〉 v 4 : Dyn int 4 〈 f ( ) 〉 v 4 〈 f ( ) 〉 3 + (S k. (k 1) == 4) v : (int→int) Dyn Since (let x = e in x) ≈ e

Problem of the standard cast semantics in the presence of shift/reset v = λx:int. 3 + (S k. (k 1) == x) 〈 f ( ) 〉 v 4 : Dyn int 4 〈 f ( ) 〉 v 4 〈 f ( ) 〉 3 + (S k. (k 1) == 4) 〈 f ( 4) 〉 v 〈 f ((λy. ) 4) 〉 v y

Problem of the standard cast semantics in the presence of shift/reset … 〈 (k 1) == 4 〈 f ( ) 〉 3 + (S k. (k 1) == 4) 〉

Problem of the standard cast semantics in the presence of shift/reset … 〈 (k 1) == 4 〈 f ( ) 〉 3 + (S k. (k 1) == 4) 〉 captured continuation

Problem of the standard cast semantics in the presence of shift/reset … 〈 f ( ) 〉 3 + (S k. (k 1) == 4) 〈 ( 1) == 4 〉 k

Problem of the standard cast semantics in the presence of shift/reset … 〈 f ( ) 〉 3 + (S k. (k 1) == 4) 〈 ( 1) == 4 〉 k captured continuation

Problem of the standard cast semantics in the presence of shift/reset … 〈 ( 1) == 4 〈 f ( ) 〉 3 + (S k. (k 1) == 4) 〉 ( λz. 〈 〉 ) f ( ) 3 + z captured continuation

Problem of the standard cast semantics in the presence of shift/reset … 〈 ( 1) == 4 〈 f ( ) 〉 3 + (S k. (k 1) == 4) 〉 ( λz. 〈 〉 ) f ( ) 3 + z NOT all value flows between typed and untyped parts are monitored

Our solution 〈 f (v’ 4) 〉 In an ideal semantics: Changing the cast semantics so that typed and untyped functions are called in typed and untyped contexts, resp. v : int→int Dyn y 〈 let x = v in 〉

Outline 1.Introduction 2.Background: blame calculus (without shift/reset) 3.Problem with control operators 4.Our extension of the blame calculus with shift/reset

How does the problem happen? … v = λx:int. 3 + (S k. (k 1) == x) An untyped continuation is captured and sent to typed code 〈 f ( ) 〉 3 + (S k. (k 1) == 4) Captured continuations are expected to be typed The captured continuation is untyped f ( ) 3 + z

Our solution Changing the cast semantics so that: Typed code captures only typed continuations Untyped code captures only untyped continuations

Our solution Changing the cast semantics so that: Typed code captures only typed continuations Untyped code captures only untyped continuations

Ideal cast semantics 〈 f (v’ 4) 〉 v : int→int Dyn 〈 let x = v in 〉 4 Typed function v should be called in the typed context f x The cast for the argument type 〈〉 Untyped function f should be called in the untyped context The cast for the return type Appropriate casts should be inserted 〜 〈 f (v 4) 〉 (if all casts are ignored)

Our cast semantics 〈 f (v’ 4) 〉 v : int→int Dyn 〈 let x = v in 〉 4 f x 〈〉 The cast for the return type 〈 f (v 4) 〉 (if all casts are ignored) guarantees captured continuations in untyped function f are untyped Appropriate casts are inserted The result of v is passed to f guarantees captured continuations in typed function v are typed ≈ How is type information for additional casts obtained? By following Danvy&Filinski’s type system for shift/reset

Our cast semantics 〈 let x = v in 〉 4 f x 〈〉 〈 f (v 4) 〉 (if all casts are ignored) ≈ 〈 f (v’ 4) 〉 v : int→int Dyn

Our cast semantics 〈 let x = v in 〉 4 f x 〈〉 〈 f (v 4) 〉 (if all casts are ignored) ≈ 〈 f (v’ 4) 〉 v : int→int Dyn

Our cast semantics 〈 let x = v in 〉 4 f x 〈〉 〈 f (v 4) 〉 (if all casts are ignored) ≈ v 4 〈 f (v’ 4) 〉 v : int→int Dyn The target function v is called with the argument to v’

Our cast semantics 〈 let x = v in 〉 4 f x 〈〉 〈 f (v 4) 〉 (if all casts are ignored) ≈ v 4 〈 f (v’ 4) 〉 v : int→int Dyn The cast for the argument type

Our cast semantics 〈 let x = v in 〉 4 f x 〈〉 〈 f (v 4) 〉 (if all casts are ignored) ≈ 〈〉 〈 f (v’ 4) 〉 v : int→int Dyn guarantees captured continuations in typed function v are typed

Our cast semantics 〈 let x = v in 〉 4 f x 〈〉 〈 f (v 4) 〉 (if all casts are ignored) ≈ f x 〈 f (v’ 4) 〉 v : int→int Dyn The result x of v is passed to f

Our cast semantics 〈 let x = v in 〉 4 f x 〈〉 〈 f (v 4) 〉 (if all casts are ignored) ≈ f x 〈 f (v’ 4) 〉 v : int→int Dyn The cast for the return type

Our cast semantics 〈 let x = v in 〉 4 f x 〈〉 〈 f (v 4) 〉 (if all casts are ignored) ≈ 〈〉 〈 f (v’ 4) 〉 v : int→int Dyn guarantees captured continuations in untyped function f are untyped

Our cast semantics 〈 let x = v in 〉 4 f x 〈〉 〈 f (v 4) 〉 (if all casts are ignored) ≈ 〈 f (v’ 4) 〉 v : int→int Dyn Additional casts are constructed by using type information given in Danvy&Filinski’s type system

Our cast semantics 〈 let x = v in 〉 4 f x 〈〉 〈 f (v 4) 〉 (if all casts are ignored) ≈ 〈 f (v’ 4) 〉 v : int→int Dyn

Our cast semantics 〈 f (v’ 4) 〉 〈 let x = v in 〉 4 f x 〈〉 〈 f (v 4) 〉 (if all casts are ignored) ≈ v : int/ int →int/ bool Dyn requires that the closest reset’s body at call point result in an integer 〈 … 〉 : Dyn int to check the outer reset’s body returns an integer

Our cast semantics 〈 f (v’ 4) 〉 〈 let x = v in 〉 4 f x 〈〉 〈 f (v 4) 〉 (if all casts are ignored) ≈ v : int/ int →int/ bool Dyn guarantees that the closest reset at call point returns a Boolean 〈 … 〉 : bool Dyn to inject Boolean values to untyped code

Our cast semantics 〈 f (v’ 4) 〉 〈 let x = v in 〉 4 f x 〈〉 〈 f (v 4) 〉 (if all casts are ignored) ≈ v : int/ int →int/ bool Dyn

Our cast semantics 〈 f (v’ 4) 〉 v : int→int Dyn v 4 The cast for the argument type Rewritten to (let x = v 4 in x) ( e ≈ (let x = e in x) )

Our cast semantics 〈 f (v’ 4) 〉 v : int→int Dyn 〈 let x = v in 〉 4 f x 〈〉 The cast for the return type 〈 f (v 4) 〉 (if all casts are ignored) guarantees captured continuations in untyped function f are untyped Appropriate casts are inserted with help of Danvy&Filisnki’ type system The result of v is passed to f guarantees captured continuations in typed function v are typed ≈

Challenges to achieve the ideal cast semantics How to manipulate the context where the cast result is used How to obtain type information for the additional casts By following Danvy&Filinski’s type system for shift/reset By manipulating the context captured by shift ✔

Our solution 〈 f (v’ 4) 〉 In an ideal semantics: Changing the cast semantics so that typed and untyped functions are called in typed and untyped contexts, resp. v : S Dyn y 〈 〈f〉〈f〉 x let x = v in 〉

Outline

Program syntax Types S, T, α, β ::= int | … | Dyn | S / α → T / β Terms s, t ::= 1 | + | … | λx.s | s t | s : S T | S k. s | 〈 s 〉

Type system for shift/reset [Danvy&Filinski’89] Γ; α├ s : T ; β the type of the closest reset’s body the result type of the closest reset Example: 〈 1 + (S k. …) 〉 && false ; ├ S k. … : ; int bool int

Type system for shift/reset [Danvy&Filinski’89] Γ; α├ s : T ; β the type of the closest reset’s body the result type of the closest reset Example: 〈 1 + (S k. …) 〉 && false ; ├ S k. … : ; ? ? ?

Type system for shift/reset [Danvy&Filinski’89] Γ; α├ s : T ; β the type of the closest reset’s body the result type of the closest reset Example: 〈 1 + (S k. …) 〉 && false ; ├ S k. … : ; ? ? int

Type system for shift/reset [Danvy&Filinski’89] Γ; α├ s : T ; β the type of the closest reset’s body the result type of the closest reset Example: 〈 1 + (S k. …) 〉 && false ; ├ S k. … : ; int ?

Type system for shift/reset [Danvy&Filinski’89] Γ; α├ s : T ; β the type of the closest reset’s body the result type of the closest reset Example: 〈 1 + (S k. …) 〉 && false ; ├ S k. … : ; int bool int

S / α → T / β the result type of the closest reset at call point the type of the closest reset’s body at call point Type system for shift/reset [Danvy&Filinski’89]

Issues to achieve the ideal cast semantics How to manipulate the context where the cast result is used How to obtain type information for the additional casts By following Danvy&Filinski’s type system for shift/reset By manipulating the context captured by shift ✔ ✔

λx. Cast semantics v : (S/α→T/β) Dyn 〈 〉〈 〉 k (v ) x S k. The type of the body of the reset The return type of the body of the reset Func 〈 … 〉 : β Dyn k (…) : Dyn α

The problematic reduction is resolved v = λx:int. 3 + (S k. (k 1) == x) 〈 f ( 4) 〉 * s* s 〜 〈 let x = v in 〉 v v : int/int→int/bool Dyn 〉 4 〈f〉〈f〉 x 〈 … 〉 : bool Dyn (corresp. to β Dyn) 〈 … 〉 : Dyn int (corresp. to Dyn α) corresponds to the continuation captured by the cast result

Our cast semantics 〈 f (v’ 4) 〉 v : int/ int →int/ bool Dyn 〈 let x = v in 〉 4 f x 〈〉 〈 … 〉 : Dyn int (corresp. to Dyn α) since α is the type of the reset’s body 〈 … 〉 : bool Dyn (corresp. to β Dyn) since β is the result type of the reset

: Dyn/Dyn→Dyn/Dyn λx. Cast semantics v : (S/α→T/β) Dyn (v ) x x : Dyn S(v …) : T Dyn

: Dyn/Dyn→Dyn/Dyn λx. Cast semantics v : (S/α→T/β) Dyn (v ) x

: Dyn/Dyn→Dyn/Dyn λx. Cast semantics v : (S/α→T/β) Dyn 〈 〉〈 〉 k (v ) x S k.

: Dyn/Dyn→Dyn/Dyn λx. Cast semantics v : (S/α→T/β) Dyn 〈 〉〈 〉 k (v ) x S k.

: Dyn/Dyn→Dyn/Dyn λx. Cast semantics v : (S/α→T/β) Dyn 〈 〉〈 〉 k (v ) x S k. k (…) : Dyn α

: Dyn/Dyn→Dyn/Dyn λx. Cast semantics v : (S/α→T/β) Dyn 〈 〉〈 〉 k (v ) x S k. k (…) : Dyn α 〈 … 〉 : β Dyn

: Dyn/Dyn→Dyn/Dyn λx. S k. 〈 〉〈 〉 k Cast semantics v : (S/α→T/β) Dyn x : Dyn int (succ …) : int Dyn (v ) x

The problematic reduction is resolved v : (int/int→int/bool) Dyn λy. S k. v = λx:int. 3 + (S k. (k 1) == x) 〈〉〈〉 k (v ) y : Dyn/Dyn→Dyn/Dyn

The problematic reduction is resolved v : (int/int→int/bool) Dyn λy. S k. v = λx:int. 3 + (S k. (k 1) == x) 〈〉〈〉 k (v ) y : Dyn/Dyn→Dyn/Dyn

The problematic reduction is resolved v = λx:int. 3 + (S k. (k 1) == x) 〈 f ((λy.S k. ) 4) 〉 〈〉〈〉 k (v ) y * s* s 〜 〈 (k 1) == 4 〉 λz. 〈 f 〉 (3 + z) 〈 … 〉 : bool Dyn (corresp. to β Dyn) 〈 … 〉 : Dyn int (corresp. to Dyn α)

The problematic reduction is resolved v = λx:int. 3 + (S k. (k 1) == x) 〈 f ((λy.S k. ) 4) 〉 〈〉〈〉 k (v ) y * 〈 f ((λy. ) 4) 〉 v y λz. 〈 〉 f ( ) 〈 〉 (k 1) == z

The problematic reduction is resolved v = λx:int. 3 + (S k. (k 1) == x) 〈 f ((λy.S k. ) 4) 〉 λz. 〈 f z 〉 v 4 〈 f ( ) 〉 v 4 〈 f ( ) 〉 3 + (S k. (k 1) == 4) 〈〉〈〉 k (v ) y 〈 f (S k. ) 〉 〈〉〈〉 k (v ) 4 〈 〉 〈〉〈〉 k (v ) 4

Problem of the standard cast semantics in the presence of shift/reset … All value flows between typed and untyped parts are NOT monitored 〈 〉 〈〉〈〉 k (v ) 4 λz. 〈 f z 〉 v = λx:int. 3 + (S k. (k 1) == x)

The problematic reduction is resolved v = λx:int. 3 + (S k. (k 1) == x) 〈 f ((λy. ) 4) 〉 v y 〈 f ( ) 〉 v 4 v 4 〈 f ( ) 〉 3 + (S k. (k 1) == 4)

Our calculus Program syntax: Types S, T, α, β ::= int | … | Dyn | S/α → T/β Terms s, t ::= 1 | + | … | λx.s | s t | s : S T | S k. s | 〈 s 〉 Type system: Semantics: shift/reset + our cast semantics Γ; α├ s : T ; β

Type soundness If 〈 s 〉 is a well typed, closed term, then: 〈 s 〉 diverges; 〈 s 〉 v for some v; or some cast in 〈 s 〉 fails * via Progress and Preservation If something wrong happens, it is detected as cast failure

Outline 1.Introduction 2.Background: blame calculus (without shift/reset) 3.Problem with control operators 4.Our extension of the blame calculus with shift/reset 5.Properties a.Soundness of CPS transformation b.Blame Theorem

In the paper… A formal system including run-time terms Support for “blame” Blame Theorem –Statically typed terms are never responsible for cast failure CPS transformation Sounenss of the CPS transformation –Preservation of Type –Preservation of Equality responsibility for cast failure

CPS transformation A technique to implement control operators –Programs with control operators are transformed to ones without them CPS transformation for shift/reset is well studied

Our CPS transformation [[ ・ ]] [[ ・ ]] transforms terms/types in our calculus to ones in the simply typed blame calculus The definition is standard except for casts [[s : S T]] := λk. [[s]] (λx. x : [[S]] [[T]]) [[Dyn]] := Dyn [[S/α→T/β]] := [[S]]→([[T]]→[[α]])→[[β]]

Soundness of the CPS transformation If s t, then [[s]] 〜 [[t]] where 〜 is an equational system with usual call- by-value axioms and a few additional axiomas If Γ;α├ s : T ; β, then [[Γ]]├ [[s]] : ([[T]]→[[α]])→[[β]] Preservation of Equality Preservation of Type

Blame Theorem (Cf. [Tobin-Hochstadt&Felleisen’06,Wadler&Findler’09] Cast failure is always triggered by untyped code That is, in a fully typed program, cast failure never happens

In the paper… A formal system including run-time terms and typing rules CPS transformation for run-time terms Support for “blame” Blame Theorem –More precisely typed sides are never blamed responsibility of cast failure

Related work Constraining delimited control with contracts [Takikawa et al., ESOP’13] studies “macro” gradual typing with: Control operators not based on CPS –powerful enough to express shift/reset Contract system –allows refined type information to be represented Modules are fully typed or fully untyped

Conclusion An extension of blame calculus with shift/reset Cast semantic to monitor capturing and calling continuations Three properties investigated –Type soundness –Blame Theorem –Soundness of the CPS transformation