SlideShare a Scribd company logo
1 of 47
Download to read offline
Gradual Typing for Generics
Lintaro Ina and Atsushi Igarashi
Kyoto University
2011-10-26 @ OOPSLA 2011
Lintaro Ina Gradual Typing for Generics 1
Dynamic vs. Static Typing
•+ flexible
•- unexpected
run-time errors
•+ reliable
•- tedious to write type
annotations
Lintaro Ina Gradual Typing for Generics 2
Gradual Typing [’06 Siek and Taha]
◮ combines benefits of both sides
◮ Dynamic and static types are both allowed
◮ Statically typed part never goes wrong
prototype product release
◮ allows evolutionary development
◮ from flexible scripts
◮ to reliable programs
Lintaro Ina Gradual Typing for Generics 3
Recipe for Gradual Typing [1/3]
1 Adding dynamic type dyn
◮ Allow potentially safe code
Lintaro Ina Gradual Typing for Generics 4
Recipe for Gradual Typing [1/3]
1 Adding dynamic type dyn
◮ Allow potentially safe code
dyn x = ...;
x.anyMethod();
String y = x;
y.length();
Lintaro Ina Gradual Typing for Generics 4
Recipe for Gradual Typing [1/3]
1 Adding dynamic type dyn
◮ Allow potentially safe code
dyn x = ...;
x.anyMethod();
String y = x;
y.length();
Lintaro Ina Gradual Typing for Generics 4
Recipe for Gradual Typing [2/3]
2 Translation to an intermediate language
◮ Explicit run-time checks in dyn-parts
◮ dyn-free parts do not change
dyn x = ...;
x.anyMethod();
String y = x;
y.length();
Lintaro Ina Gradual Typing for Generics 5
Recipe for Gradual Typing [2/3]
2 Translation to an intermediate language
◮ Explicit run-time checks in dyn-parts
◮ dyn-free parts do not change
dyn x = ...;
x.anyMethod();
String y = x;
y.length();
dyn x = ...;
invoke(x, anyMethod);
String y = String x;
y.length();
Lintaro Ina Gradual Typing for Generics 5
Recipe for Gradual Typing [3/3]
3 Proof of type safety
◮ Only run-time checks may fail
◮ The translation preserves typeability
Lintaro Ina Gradual Typing for Generics 6
Recipe for Gradual Typing
1 Adding dynamic type dyn
◮ Allow potentially safe code
2 Translation to an intermediate language
◮ Explicit run-time checks in dyn-parts
◮ dyn-free parts do not change
3 Proof of type safety
◮ Only run-time checks may fail
◮ The translation preserves typeability
Lintaro Ina Gradual Typing for Generics 7
Our Goal
A gradual type system for Java
Lintaro Ina Gradual Typing for Generics 8
Our Goal
A gradual type system for Java
Theory
◮ A type system
◮ Proofs of properties
Implementation
◮ A type checker
◮ Byte-code generation for the current JVM
Lintaro Ina Gradual Typing for Generics 8
Our Goal
A gradual type system for Java
Theory
◮ A type system
◮ Proofs of properties
Implementation
◮ A type checker
◮ Byte-code generation for the current JVM
Lintaro Ina Gradual Typing for Generics 8
Issues
◮ Class-based nominal type system
◮ cf. structural type system [’07 Siek and Taha]
◮ Generics
◮ What can we do with List<dyn>?
◮ Matching with the current Java architectures
◮ Each class compiles to a single class file
◮ Behavior of statically typed parts
Lintaro Ina Gradual Typing for Generics 9
What We Do and Don’t
Do
◮ Gradual type system for nominal subtyping
◮ Fine-grained integration with generics
◮ Flexible compatibility relation
◮ Ensuring type safety
◮ Bounded dynamic types
◮ Run-time checks for parametric types
Don’t
◮ Blame tracking
Lintaro Ina Gradual Typing for Generics 10
What We Do and Don’t
Do
◮ Gradual type system for nominal subtyping
◮ Fine-grained integration with generics
◮ Flexible compatibility relation
◮ Ensuring type safety
◮ Bounded dynamic types
◮ Run-time checks for parametric types
Don’t
◮ Blame tracking
Lintaro Ina Gradual Typing for Generics 10
Generics and Dynamic Types
◮ List<dyn> can be used as List<whatever>
◮ You can’t do this in C# 4.0 [’10 Bierman, et al.]
class List<X> { X head; List<X> tail }
List<dyn> ls = ...;
ls.head.anyMethod();
List<String> sls = ls;
Lintaro Ina Gradual Typing for Generics 11
Generics and Dynamic Types
◮ List<dyn> can be used as List<whatever>
◮ You can’t do this in C# 4.0 [’10 Bierman, et al.]
class List<X> { X head; List<X> tail }
List<dyn> ls = ...;
ls.head.anyMethod();
List<String> sls = ls;
◮ How does type checking go?
Lintaro Ina Gradual Typing for Generics 11
Static Compatibility [1/2]
Static Compatibility S T
S x = ...;
T y = x;
// Is this allowed by the compiler?
◮ Checks whether type S is compatible to type T
◮ Used instead of normal subtype relation <:
Lintaro Ina Gradual Typing for Generics 12
Static Compatibility [2/2]
Static Compatibility S T
◮ Includes <:
◮ Co- and contra-variant
◮ List<String> List<dyn>
◮ List<dyn> List<String>
are both OK
Lintaro Ina Gradual Typing for Generics 13
Static Compatibility [2/2]
Static Compatibility S T
◮ Includes <:
◮ Co- and contra-variant
◮ List<String> List<dyn>
◮ List<dyn> List<String>
are both OK
/* the compiler should not reject this */
List<dyn> ls1 = new List<String>(...);
// List<String> List<dyn>
List<String> ls2 = ls1;
// List<dyn> List<String>
Lintaro Ina Gradual Typing for Generics 13
Bounded Dynamic Types [1/2]
class NCell<X extends Number> {
/* in a single NCell.class */
X x; void foo() { this.x.intValue(); } }
Lintaro Ina Gradual Typing for Generics 14
Bounded Dynamic Types [1/2]
class NCell<X extends Number> {
/* in a single NCell.class */
X x; void foo() { this.x.intValue(); } }
Lintaro Ina Gradual Typing for Generics 14
Bounded Dynamic Types [1/2]
class NCell<X extends Number> {
/* in a single NCell.class */
X x; void foo() { this.x.intValue(); } }
new NCell<dyn>(new Integer(...)).x.bar();
new NCell<dyn>(new Integer(...)).foo();
new NCell<dyn>("abc").foo();
Lintaro Ina Gradual Typing for Generics 14
Bounded Dynamic Types [1/2]
class NCell<X extends Number> {
/* in a single NCell.class */
X x; void foo() { this.x.intValue(); } }
new NCell<dyn>(new Integer(...)).x.bar();
new NCell<dyn>(new Integer(...)).foo();
new NCell<dyn>("abc").foo();
Lintaro Ina Gradual Typing for Generics 14
Bounded Dynamic Types [1/2]
class NCell<X extends Number> {
/* in a single NCell.class */
X x; void foo() { this.x.intValue(); } }
new NCell<dyn>(new Integer(...)).x.bar();
new NCell<dyn>(new Integer(...)).foo();
new NCell<dyn>("abc").foo();
Lintaro Ina Gradual Typing for Generics 14
Bounded Dynamic Types [1/2]
class NCell<X extends Number> {
/* in a single NCell.class */
X x; void foo() { this.x.intValue(); } }
new NCell<dyn>(new Integer(...)).x.bar();
new NCell<dyn>(new Integer(...)).foo();
new NCell<dyn>("abc").foo(); // reject
Lintaro Ina Gradual Typing for Generics 14
Bounded Dynamic Types [1/2]
class NCell<X extends Number> {
/* in a single NCell.class */
X x; void foo() { this.x.intValue(); } }
new NCell<dyn>(new Integer(...)).x.bar();
new NCell<dyn>(new Integer(...)).foo();
new NCell<dyn>("abc").foo(); // reject
How?
Lintaro Ina Gradual Typing for Generics 14
Bounded Dynamic Types [2/2]
class NCell<X extends Number> {
/* in a single NCell.class */
X x; void foo() { this.x.intValue(); } }
// constructor type
NCell<X>:X → NCell<X>
NCell<dyn>:dyn → NCell<dyn>
Lintaro Ina Gradual Typing for Generics 15
Bounded Dynamic Types [2/2]
class NCell<X extends Number> {
/* in a single NCell.class */
X x; void foo() { this.x.intValue(); } }
// constructor type
NCell<X>:X → NCell<X>
NCell<dyn>:dyn<Number> → NCell<dyn<Number>>
Lintaro Ina Gradual Typing for Generics 15
Bounded Dynamic Types [2/2]
class NCell<X extends Number> {
/* in a single NCell.class */
X x; void foo() { this.x.intValue(); } }
// constructor type
NCell<X>:X → NCell<X>
NCell<dyn>:dyn<Number> → NCell<dyn<Number>>
Bounded Dynamic Type dyn<N>
◮ T dyn<N> if T <: N
◮ T dyn<N> if T <: N
Lintaro Ina Gradual Typing for Generics 15
Bounded Dynamic Types [2/2]
class NCell<X extends Number> {
/* in a single NCell.class */
X x; void foo() { this.x.intValue(); } }
// constructor type
NCell<X>:X → NCell<X>
NCell<dyn>:dyn<Number> → NCell<dyn<Number>>
Bounded Dynamic Type dyn<N>
◮ T dyn<N> if T <: N
◮ T dyn<N> if T <: N
String dyn<Number> (∵ String <: Number)
Lintaro Ina Gradual Typing for Generics 15
Run-time checks
List<dyn> ls = new List<Integer>(...);
ls.head.anyMethod();
List<String> sls = ls;
Lintaro Ina Gradual Typing for Generics 16
Run-time checks
◮ Assume List<dyn> as a citizen of the
dynamically typed world
List<dyn> ls = new List<Integer>(...);
ls.head.anyMethod();
List<String> sls = ls;
Lintaro Ina Gradual Typing for Generics 16
Run-time checks
◮ Assume List<dyn> as a citizen of the
dynamically typed world
List<dyn> ls = List<dyn> new List...;
invoke(ls.head, anyMethod);
List<String> sls = List<String> ls;
Lintaro Ina Gradual Typing for Generics 16
Run-time checks
◮ Assume List<dyn> as a citizen of the
dynamically typed world
List<dyn> ls = List<dyn> new List...;
invoke(ls.head, anyMethod);
List<String> sls = List<String> ls;
◮ When does T e succeed?
Lintaro Ina Gradual Typing for Generics 16
Run-time Compatibility [1/2]
Run-time Compatibility S :≺ T
T new S(...);
// Does this succeed at run time?
◮ Checks whether a value of S is compatible to T
◮ Used instead of normal subtype relation <:
Lintaro Ina Gradual Typing for Generics 17
Run-time Compatibility [2/2]
Run-time Compatibility S :≺ T
◮ Includes <:
◮ Co-variant but not contra-variant
◮ List<String> :≺ List<dyn>
◮ List<dyn> :≺ List<String>
Lintaro Ina Gradual Typing for Generics 18
Run-time Compatibility [2/2]
Run-time Compatibility S :≺ T
◮ Includes <:
◮ Co-variant but not contra-variant
◮ List<String> :≺ List<dyn>
◮ List<dyn> :≺ List<String>
List<dyn> ls1 = new List<dyn>(...);
ls1.head = new Integer(1);
List<String> ls2 = ls1;
ls2.head.length();
Lintaro Ina Gradual Typing for Generics 18
Run-time Compatibility [2/2]
Run-time Compatibility S :≺ T
◮ Includes <:
◮ Co-variant but not contra-variant
◮ List<String> :≺ List<dyn>
◮ List<dyn> :≺ List<String>
List<dyn> ls1 = new List<dyn>(...);
ls1.head = new Integer(1);
List<String> ls2 = ls1;
ls2.head.length(); /* should not fail
because it’s statically typed */
Lintaro Ina Gradual Typing for Generics 18
Run-time Compatibility [2/2]
Run-time Compatibility S :≺ T
◮ Includes <:
◮ Co-variant but not contra-variant
◮ List<String> :≺ List<dyn>
◮ List<dyn> :≺ List<String>
List<dyn> ls1 = new List<dyn>(...);
ls1.head = new Integer(1);
List<String> ls2 = ls1; /* run-time error
List<dyn> :≺ List<String> */
ls2.head.length(); // this does not execute
Lintaro Ina Gradual Typing for Generics 18
Formalization
◮ Extend Featherweight GJ (FGJ) with dyn
◮ Give a translation to an intermediate language
◮ and :≺ instead of normal subtyping <:
Surface language Intermediate language
FGJ + dyn FGJ + dyn + run-time checks
compatibility compatibility :≺
typing (typing)
reduction
Lintaro Ina Gradual Typing for Generics 19
Compatibility Relations
Auxiliary Relation S ≺ T
◮ In S, dyns in T are replaced with non-dyns
∆ ⊢ T ≺ T
∆ ⊢ S ≺ T
∆ ⊢ C<S> ≺ C<T>
∆ ⊢ T <: S ∆ ⊢ S ≺ N
∆ ⊢ T ≺ dyn<N>
Definitions
◮ Static compatibility S T
def
= S ≻ ∃
U <: ∃
V ≺ T
◮ Run-time compatibility S :≺ T
def
= S <: ∃
U ≺ T
Lintaro Ina Gradual Typing for Generics 20
Properties
Weak Type Safety in the Intermediate Language
An execution of a program of type T
1 results in a value of type S ( :≺ T), or
2 results in a failure of a run-time check, or
3 does not stop
Translation from the Surface to the Intermediate
A well-typed program can translate and
◮ typeability is preserved
◮ statically typed parts do not change
Lintaro Ina Gradual Typing for Generics 21
Conclusions
◮ Gradual typing for class-based languages
◮ Fine-grained integration with generics
◮ Static and run-time compatibility
◮ Bounded dynamic types
◮ Formalization
◮ FGJ + dyn + run-time checks
◮ Type safety and translation theorem
Lintaro Ina Gradual Typing for Generics 22
Future Work
◮ Decidability of and :≺
:≺ decidable?
at least semidecidable?
◮ Full compiler with a large scale evaluation
◮ Integration with method overloading,
wildcards, etc.
◮ Blame tracking instead of run-time
compatibility checks
Lintaro Ina Gradual Typing for Generics 23
THANK YOU!

More Related Content

What's hot

Latest C Interview Questions and Answers
Latest C Interview Questions and AnswersLatest C Interview Questions and Answers
Latest C Interview Questions and AnswersDaisyWatson5
 
Java moderno java para Jedis episodio I
Java moderno  java para  Jedis  episodio IJava moderno  java para  Jedis  episodio I
Java moderno java para Jedis episodio IRoan Brasil Monteiro
 
Lambda Expressions in Java
Lambda Expressions in JavaLambda Expressions in Java
Lambda Expressions in JavaErhan Bagdemir
 
Finite automata examples
Finite automata examplesFinite automata examples
Finite automata examplesankitamakin
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsNewCircle Training
 
Finite Automata
Finite AutomataFinite Automata
Finite Automataparmeet834
 
Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java langer4711
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentationVan Huong
 
Best practices in Java
Best practices in JavaBest practices in Java
Best practices in JavaMudit Gupta
 
Java 8 - Project Lambda
Java 8 - Project LambdaJava 8 - Project Lambda
Java 8 - Project LambdaRahman USTA
 
Programming in scala - 1
Programming in scala - 1Programming in scala - 1
Programming in scala - 1Mukesh Kumar
 
SAS Global Forum 2008 presentation
SAS Global Forum 2008 presentationSAS Global Forum 2008 presentation
SAS Global Forum 2008 presentationandrearoofe
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8icarter09
 

What's hot (19)

Latest C Interview Questions and Answers
Latest C Interview Questions and AnswersLatest C Interview Questions and Answers
Latest C Interview Questions and Answers
 
Java moderno java para Jedis episodio I
Java moderno  java para  Jedis  episodio IJava moderno  java para  Jedis  episodio I
Java moderno java para Jedis episodio I
 
Lambda Expressions in Java
Lambda Expressions in JavaLambda Expressions in Java
Lambda Expressions in Java
 
Finite automata examples
Finite automata examplesFinite automata examples
Finite automata examples
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & Streams
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
Finite automata
Finite automataFinite automata
Finite automata
 
Best practices in Java
Best practices in JavaBest practices in Java
Best practices in Java
 
Java 8 - Project Lambda
Java 8 - Project LambdaJava 8 - Project Lambda
Java 8 - Project Lambda
 
Programming in scala - 1
Programming in scala - 1Programming in scala - 1
Programming in scala - 1
 
SAS Global Forum 2008 presentation
SAS Global Forum 2008 presentationSAS Global Forum 2008 presentation
SAS Global Forum 2008 presentation
 
Java 8 Lambda and Streams
Java 8 Lambda and StreamsJava 8 Lambda and Streams
Java 8 Lambda and Streams
 
Java 8 by example!
Java 8 by example!Java 8 by example!
Java 8 by example!
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8
 
study of java
study of java study of java
study of java
 

Viewers also liked

はてなブックマーク in Scala
はてなブックマーク in Scalaはてなブックマーク in Scala
はてなブックマーク in ScalaLintaro Ina
 
安全性を証明するために知っておくべき4つのこと
安全性を証明するために知っておくべき4つのこと安全性を証明するために知っておくべき4つのこと
安全性を証明するために知っておくべき4つのことshibataka000
 
はてなブックマークのトピックページの裏側 in YAPC::Asia Tokyo 2015
はてなブックマークのトピックページの裏側 in YAPC::Asia Tokyo 2015はてなブックマークのトピックページの裏側 in YAPC::Asia Tokyo 2015
はてなブックマークのトピックページの裏側 in YAPC::Asia Tokyo 2015Shunsuke Kozawa
 
Emacs上のターミナルを最強に
Emacs上のターミナルを最強にEmacs上のターミナルを最強に
Emacs上のターミナルを最強にLintaro Ina
 
『BrandSafe はてな』のアドベリフィケーションのしくみ
『BrandSafe はてな』のアドベリフィケーションのしくみ『BrandSafe はてな』のアドベリフィケーションのしくみ
『BrandSafe はてな』のアドベリフィケーションのしくみLintaro Ina
 
論理と計算のしくみ 5.3 型付きλ計算 (前半)
論理と計算のしくみ 5.3 型付きλ計算 (前半)論理と計算のしくみ 5.3 型付きλ計算 (前半)
論理と計算のしくみ 5.3 型付きλ計算 (前半)Lintaro Ina
 
ビットコインの基礎知識と世界的なトレンド
ビットコインの基礎知識と世界的なトレンドビットコインの基礎知識と世界的なトレンド
ビットコインの基礎知識と世界的なトレンドKoichiro Wada
 
はてなブックマークの新機能における自然言語処理の活用
はてなブックマークの新機能における自然言語処理の活用はてなブックマークの新機能における自然言語処理の活用
はてなブックマークの新機能における自然言語処理の活用Shunsuke Kozawa
 

Viewers also liked (9)

はてなブックマーク in Scala
はてなブックマーク in Scalaはてなブックマーク in Scala
はてなブックマーク in Scala
 
計算可能実数とは
計算可能実数とは計算可能実数とは
計算可能実数とは
 
安全性を証明するために知っておくべき4つのこと
安全性を証明するために知っておくべき4つのこと安全性を証明するために知っておくべき4つのこと
安全性を証明するために知っておくべき4つのこと
 
はてなブックマークのトピックページの裏側 in YAPC::Asia Tokyo 2015
はてなブックマークのトピックページの裏側 in YAPC::Asia Tokyo 2015はてなブックマークのトピックページの裏側 in YAPC::Asia Tokyo 2015
はてなブックマークのトピックページの裏側 in YAPC::Asia Tokyo 2015
 
Emacs上のターミナルを最強に
Emacs上のターミナルを最強にEmacs上のターミナルを最強に
Emacs上のターミナルを最強に
 
『BrandSafe はてな』のアドベリフィケーションのしくみ
『BrandSafe はてな』のアドベリフィケーションのしくみ『BrandSafe はてな』のアドベリフィケーションのしくみ
『BrandSafe はてな』のアドベリフィケーションのしくみ
 
論理と計算のしくみ 5.3 型付きλ計算 (前半)
論理と計算のしくみ 5.3 型付きλ計算 (前半)論理と計算のしくみ 5.3 型付きλ計算 (前半)
論理と計算のしくみ 5.3 型付きλ計算 (前半)
 
ビットコインの基礎知識と世界的なトレンド
ビットコインの基礎知識と世界的なトレンドビットコインの基礎知識と世界的なトレンド
ビットコインの基礎知識と世界的なトレンド
 
はてなブックマークの新機能における自然言語処理の活用
はてなブックマークの新機能における自然言語処理の活用はてなブックマークの新機能における自然言語処理の活用
はてなブックマークの新機能における自然言語処理の活用
 

Similar to Gradual Typing for Generics

FUNDAMENTALS OF PYTHON LANGUAGE
 FUNDAMENTALS OF PYTHON LANGUAGE  FUNDAMENTALS OF PYTHON LANGUAGE
FUNDAMENTALS OF PYTHON LANGUAGE Saraswathi Murugan
 
Language Integrated Query - LINQ
Language Integrated Query - LINQLanguage Integrated Query - LINQ
Language Integrated Query - LINQDoncho Minkov
 
Chapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteChapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteTushar B Kute
 
Mario Fusco - Lazy Java - Codemotion Milan 2018
Mario Fusco - Lazy Java - Codemotion Milan 2018Mario Fusco - Lazy Java - Codemotion Milan 2018
Mario Fusco - Lazy Java - Codemotion Milan 2018Codemotion
 
Complete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScriptComplete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScriptEPAM Systems
 
The Swift Compiler and Standard Library
The Swift Compiler and Standard LibraryThe Swift Compiler and Standard Library
The Swift Compiler and Standard LibrarySantosh Rajan
 
LineairDBの紹介 MySQL編
LineairDBの紹介 MySQL編LineairDBの紹介 MySQL編
LineairDBの紹介 MySQL編Sho Nakazono
 
Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to Compilersvijaya603274
 
Input processing and output in Python
Input processing and output in PythonInput processing and output in Python
Input processing and output in PythonMSB Academy
 
Input processing and output in Python
Input processing and output in PythonInput processing and output in Python
Input processing and output in PythonRaajendra M
 
The Dynamic Language is not Enough
The Dynamic Language is not EnoughThe Dynamic Language is not Enough
The Dynamic Language is not EnoughLukas Renggli
 

Similar to Gradual Typing for Generics (20)

Pythonppt28 11-18
Pythonppt28 11-18Pythonppt28 11-18
Pythonppt28 11-18
 
FUNDAMENTALS OF PYTHON LANGUAGE
 FUNDAMENTALS OF PYTHON LANGUAGE  FUNDAMENTALS OF PYTHON LANGUAGE
FUNDAMENTALS OF PYTHON LANGUAGE
 
Language Integrated Query - LINQ
Language Integrated Query - LINQLanguage Integrated Query - LINQ
Language Integrated Query - LINQ
 
Chapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteChapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B Kute
 
Lazy Java
Lazy JavaLazy Java
Lazy Java
 
Mario Fusco - Lazy Java - Codemotion Milan 2018
Mario Fusco - Lazy Java - Codemotion Milan 2018Mario Fusco - Lazy Java - Codemotion Milan 2018
Mario Fusco - Lazy Java - Codemotion Milan 2018
 
Lazy java
Lazy javaLazy java
Lazy java
 
Lazy Java
Lazy JavaLazy Java
Lazy Java
 
Complete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScriptComplete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScript
 
The Swift Compiler and Standard Library
The Swift Compiler and Standard LibraryThe Swift Compiler and Standard Library
The Swift Compiler and Standard Library
 
C language
C languageC language
C language
 
Compilers Design
Compilers DesignCompilers Design
Compilers Design
 
LineairDBの紹介 MySQL編
LineairDBの紹介 MySQL編LineairDBの紹介 MySQL編
LineairDBの紹介 MySQL編
 
Static Analysis
Static AnalysisStatic Analysis
Static Analysis
 
Pcd question bank
Pcd question bank Pcd question bank
Pcd question bank
 
Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to Compilers
 
Input processing and output in Python
Input processing and output in PythonInput processing and output in Python
Input processing and output in Python
 
Input processing and output in Python
Input processing and output in PythonInput processing and output in Python
Input processing and output in Python
 
The Dynamic Language is not Enough
The Dynamic Language is not EnoughThe Dynamic Language is not Enough
The Dynamic Language is not Enough
 
Variables and Data Types
Variables and Data TypesVariables and Data Types
Variables and Data Types
 

Recently uploaded

Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsHubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsSérgio Sacani
 
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |aasikanpl
 
Physiochemical properties of nanomaterials and its nanotoxicity.pptx
Physiochemical properties of nanomaterials and its nanotoxicity.pptxPhysiochemical properties of nanomaterials and its nanotoxicity.pptx
Physiochemical properties of nanomaterials and its nanotoxicity.pptxAArockiyaNisha
 
Artificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PArtificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PPRINCE C P
 
Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)PraveenaKalaiselvan1
 
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral AnalysisRaman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral AnalysisDiwakar Mishra
 
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptxUnlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptxanandsmhk
 
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCESTERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCEPRINCE C P
 
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bNightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bSérgio Sacani
 
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Sérgio Sacani
 
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...Sérgio Sacani
 
G9 Science Q4- Week 1-2 Projectile Motion.ppt
G9 Science Q4- Week 1-2 Projectile Motion.pptG9 Science Q4- Week 1-2 Projectile Motion.ppt
G9 Science Q4- Week 1-2 Projectile Motion.pptMAESTRELLAMesa2
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxkessiyaTpeter
 
Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Nistarini College, Purulia (W.B) India
 
Spermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatidSpermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatidSarthak Sekhar Mondal
 
Disentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTDisentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTSérgio Sacani
 
Caco-2 cell permeability assay for drug absorption
Caco-2 cell permeability assay for drug absorptionCaco-2 cell permeability assay for drug absorption
Caco-2 cell permeability assay for drug absorptionPriyansha Singh
 

Recently uploaded (20)

Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsHubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
 
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Mukherjee Nagar(Delhi) |
 
Physiochemical properties of nanomaterials and its nanotoxicity.pptx
Physiochemical properties of nanomaterials and its nanotoxicity.pptxPhysiochemical properties of nanomaterials and its nanotoxicity.pptx
Physiochemical properties of nanomaterials and its nanotoxicity.pptx
 
Artificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PArtificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C P
 
9953056974 Young Call Girls In Mahavir enclave Indian Quality Escort service
9953056974 Young Call Girls In Mahavir enclave Indian Quality Escort service9953056974 Young Call Girls In Mahavir enclave Indian Quality Escort service
9953056974 Young Call Girls In Mahavir enclave Indian Quality Escort service
 
Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)
 
The Philosophy of Science
The Philosophy of ScienceThe Philosophy of Science
The Philosophy of Science
 
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral AnalysisRaman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
 
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptxUnlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
 
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCESTERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
 
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bNightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
 
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
 
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
 
G9 Science Q4- Week 1-2 Projectile Motion.ppt
G9 Science Q4- Week 1-2 Projectile Motion.pptG9 Science Q4- Week 1-2 Projectile Motion.ppt
G9 Science Q4- Week 1-2 Projectile Motion.ppt
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
 
Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...
 
Spermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatidSpermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatid
 
CELL -Structural and Functional unit of life.pdf
CELL -Structural and Functional unit of life.pdfCELL -Structural and Functional unit of life.pdf
CELL -Structural and Functional unit of life.pdf
 
Disentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTDisentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOST
 
Caco-2 cell permeability assay for drug absorption
Caco-2 cell permeability assay for drug absorptionCaco-2 cell permeability assay for drug absorption
Caco-2 cell permeability assay for drug absorption
 

Gradual Typing for Generics

  • 1. Gradual Typing for Generics Lintaro Ina and Atsushi Igarashi Kyoto University 2011-10-26 @ OOPSLA 2011 Lintaro Ina Gradual Typing for Generics 1
  • 2. Dynamic vs. Static Typing •+ flexible •- unexpected run-time errors •+ reliable •- tedious to write type annotations Lintaro Ina Gradual Typing for Generics 2
  • 3. Gradual Typing [’06 Siek and Taha] ◮ combines benefits of both sides ◮ Dynamic and static types are both allowed ◮ Statically typed part never goes wrong prototype product release ◮ allows evolutionary development ◮ from flexible scripts ◮ to reliable programs Lintaro Ina Gradual Typing for Generics 3
  • 4. Recipe for Gradual Typing [1/3] 1 Adding dynamic type dyn ◮ Allow potentially safe code Lintaro Ina Gradual Typing for Generics 4
  • 5. Recipe for Gradual Typing [1/3] 1 Adding dynamic type dyn ◮ Allow potentially safe code dyn x = ...; x.anyMethod(); String y = x; y.length(); Lintaro Ina Gradual Typing for Generics 4
  • 6. Recipe for Gradual Typing [1/3] 1 Adding dynamic type dyn ◮ Allow potentially safe code dyn x = ...; x.anyMethod(); String y = x; y.length(); Lintaro Ina Gradual Typing for Generics 4
  • 7. Recipe for Gradual Typing [2/3] 2 Translation to an intermediate language ◮ Explicit run-time checks in dyn-parts ◮ dyn-free parts do not change dyn x = ...; x.anyMethod(); String y = x; y.length(); Lintaro Ina Gradual Typing for Generics 5
  • 8. Recipe for Gradual Typing [2/3] 2 Translation to an intermediate language ◮ Explicit run-time checks in dyn-parts ◮ dyn-free parts do not change dyn x = ...; x.anyMethod(); String y = x; y.length(); dyn x = ...; invoke(x, anyMethod); String y = String x; y.length(); Lintaro Ina Gradual Typing for Generics 5
  • 9. Recipe for Gradual Typing [3/3] 3 Proof of type safety ◮ Only run-time checks may fail ◮ The translation preserves typeability Lintaro Ina Gradual Typing for Generics 6
  • 10. Recipe for Gradual Typing 1 Adding dynamic type dyn ◮ Allow potentially safe code 2 Translation to an intermediate language ◮ Explicit run-time checks in dyn-parts ◮ dyn-free parts do not change 3 Proof of type safety ◮ Only run-time checks may fail ◮ The translation preserves typeability Lintaro Ina Gradual Typing for Generics 7
  • 11. Our Goal A gradual type system for Java Lintaro Ina Gradual Typing for Generics 8
  • 12. Our Goal A gradual type system for Java Theory ◮ A type system ◮ Proofs of properties Implementation ◮ A type checker ◮ Byte-code generation for the current JVM Lintaro Ina Gradual Typing for Generics 8
  • 13. Our Goal A gradual type system for Java Theory ◮ A type system ◮ Proofs of properties Implementation ◮ A type checker ◮ Byte-code generation for the current JVM Lintaro Ina Gradual Typing for Generics 8
  • 14. Issues ◮ Class-based nominal type system ◮ cf. structural type system [’07 Siek and Taha] ◮ Generics ◮ What can we do with List<dyn>? ◮ Matching with the current Java architectures ◮ Each class compiles to a single class file ◮ Behavior of statically typed parts Lintaro Ina Gradual Typing for Generics 9
  • 15. What We Do and Don’t Do ◮ Gradual type system for nominal subtyping ◮ Fine-grained integration with generics ◮ Flexible compatibility relation ◮ Ensuring type safety ◮ Bounded dynamic types ◮ Run-time checks for parametric types Don’t ◮ Blame tracking Lintaro Ina Gradual Typing for Generics 10
  • 16. What We Do and Don’t Do ◮ Gradual type system for nominal subtyping ◮ Fine-grained integration with generics ◮ Flexible compatibility relation ◮ Ensuring type safety ◮ Bounded dynamic types ◮ Run-time checks for parametric types Don’t ◮ Blame tracking Lintaro Ina Gradual Typing for Generics 10
  • 17. Generics and Dynamic Types ◮ List<dyn> can be used as List<whatever> ◮ You can’t do this in C# 4.0 [’10 Bierman, et al.] class List<X> { X head; List<X> tail } List<dyn> ls = ...; ls.head.anyMethod(); List<String> sls = ls; Lintaro Ina Gradual Typing for Generics 11
  • 18. Generics and Dynamic Types ◮ List<dyn> can be used as List<whatever> ◮ You can’t do this in C# 4.0 [’10 Bierman, et al.] class List<X> { X head; List<X> tail } List<dyn> ls = ...; ls.head.anyMethod(); List<String> sls = ls; ◮ How does type checking go? Lintaro Ina Gradual Typing for Generics 11
  • 19. Static Compatibility [1/2] Static Compatibility S T S x = ...; T y = x; // Is this allowed by the compiler? ◮ Checks whether type S is compatible to type T ◮ Used instead of normal subtype relation <: Lintaro Ina Gradual Typing for Generics 12
  • 20. Static Compatibility [2/2] Static Compatibility S T ◮ Includes <: ◮ Co- and contra-variant ◮ List<String> List<dyn> ◮ List<dyn> List<String> are both OK Lintaro Ina Gradual Typing for Generics 13
  • 21. Static Compatibility [2/2] Static Compatibility S T ◮ Includes <: ◮ Co- and contra-variant ◮ List<String> List<dyn> ◮ List<dyn> List<String> are both OK /* the compiler should not reject this */ List<dyn> ls1 = new List<String>(...); // List<String> List<dyn> List<String> ls2 = ls1; // List<dyn> List<String> Lintaro Ina Gradual Typing for Generics 13
  • 22. Bounded Dynamic Types [1/2] class NCell<X extends Number> { /* in a single NCell.class */ X x; void foo() { this.x.intValue(); } } Lintaro Ina Gradual Typing for Generics 14
  • 23. Bounded Dynamic Types [1/2] class NCell<X extends Number> { /* in a single NCell.class */ X x; void foo() { this.x.intValue(); } } Lintaro Ina Gradual Typing for Generics 14
  • 24. Bounded Dynamic Types [1/2] class NCell<X extends Number> { /* in a single NCell.class */ X x; void foo() { this.x.intValue(); } } new NCell<dyn>(new Integer(...)).x.bar(); new NCell<dyn>(new Integer(...)).foo(); new NCell<dyn>("abc").foo(); Lintaro Ina Gradual Typing for Generics 14
  • 25. Bounded Dynamic Types [1/2] class NCell<X extends Number> { /* in a single NCell.class */ X x; void foo() { this.x.intValue(); } } new NCell<dyn>(new Integer(...)).x.bar(); new NCell<dyn>(new Integer(...)).foo(); new NCell<dyn>("abc").foo(); Lintaro Ina Gradual Typing for Generics 14
  • 26. Bounded Dynamic Types [1/2] class NCell<X extends Number> { /* in a single NCell.class */ X x; void foo() { this.x.intValue(); } } new NCell<dyn>(new Integer(...)).x.bar(); new NCell<dyn>(new Integer(...)).foo(); new NCell<dyn>("abc").foo(); Lintaro Ina Gradual Typing for Generics 14
  • 27. Bounded Dynamic Types [1/2] class NCell<X extends Number> { /* in a single NCell.class */ X x; void foo() { this.x.intValue(); } } new NCell<dyn>(new Integer(...)).x.bar(); new NCell<dyn>(new Integer(...)).foo(); new NCell<dyn>("abc").foo(); // reject Lintaro Ina Gradual Typing for Generics 14
  • 28. Bounded Dynamic Types [1/2] class NCell<X extends Number> { /* in a single NCell.class */ X x; void foo() { this.x.intValue(); } } new NCell<dyn>(new Integer(...)).x.bar(); new NCell<dyn>(new Integer(...)).foo(); new NCell<dyn>("abc").foo(); // reject How? Lintaro Ina Gradual Typing for Generics 14
  • 29. Bounded Dynamic Types [2/2] class NCell<X extends Number> { /* in a single NCell.class */ X x; void foo() { this.x.intValue(); } } // constructor type NCell<X>:X → NCell<X> NCell<dyn>:dyn → NCell<dyn> Lintaro Ina Gradual Typing for Generics 15
  • 30. Bounded Dynamic Types [2/2] class NCell<X extends Number> { /* in a single NCell.class */ X x; void foo() { this.x.intValue(); } } // constructor type NCell<X>:X → NCell<X> NCell<dyn>:dyn<Number> → NCell<dyn<Number>> Lintaro Ina Gradual Typing for Generics 15
  • 31. Bounded Dynamic Types [2/2] class NCell<X extends Number> { /* in a single NCell.class */ X x; void foo() { this.x.intValue(); } } // constructor type NCell<X>:X → NCell<X> NCell<dyn>:dyn<Number> → NCell<dyn<Number>> Bounded Dynamic Type dyn<N> ◮ T dyn<N> if T <: N ◮ T dyn<N> if T <: N Lintaro Ina Gradual Typing for Generics 15
  • 32. Bounded Dynamic Types [2/2] class NCell<X extends Number> { /* in a single NCell.class */ X x; void foo() { this.x.intValue(); } } // constructor type NCell<X>:X → NCell<X> NCell<dyn>:dyn<Number> → NCell<dyn<Number>> Bounded Dynamic Type dyn<N> ◮ T dyn<N> if T <: N ◮ T dyn<N> if T <: N String dyn<Number> (∵ String <: Number) Lintaro Ina Gradual Typing for Generics 15
  • 33. Run-time checks List<dyn> ls = new List<Integer>(...); ls.head.anyMethod(); List<String> sls = ls; Lintaro Ina Gradual Typing for Generics 16
  • 34. Run-time checks ◮ Assume List<dyn> as a citizen of the dynamically typed world List<dyn> ls = new List<Integer>(...); ls.head.anyMethod(); List<String> sls = ls; Lintaro Ina Gradual Typing for Generics 16
  • 35. Run-time checks ◮ Assume List<dyn> as a citizen of the dynamically typed world List<dyn> ls = List<dyn> new List...; invoke(ls.head, anyMethod); List<String> sls = List<String> ls; Lintaro Ina Gradual Typing for Generics 16
  • 36. Run-time checks ◮ Assume List<dyn> as a citizen of the dynamically typed world List<dyn> ls = List<dyn> new List...; invoke(ls.head, anyMethod); List<String> sls = List<String> ls; ◮ When does T e succeed? Lintaro Ina Gradual Typing for Generics 16
  • 37. Run-time Compatibility [1/2] Run-time Compatibility S :≺ T T new S(...); // Does this succeed at run time? ◮ Checks whether a value of S is compatible to T ◮ Used instead of normal subtype relation <: Lintaro Ina Gradual Typing for Generics 17
  • 38. Run-time Compatibility [2/2] Run-time Compatibility S :≺ T ◮ Includes <: ◮ Co-variant but not contra-variant ◮ List<String> :≺ List<dyn> ◮ List<dyn> :≺ List<String> Lintaro Ina Gradual Typing for Generics 18
  • 39. Run-time Compatibility [2/2] Run-time Compatibility S :≺ T ◮ Includes <: ◮ Co-variant but not contra-variant ◮ List<String> :≺ List<dyn> ◮ List<dyn> :≺ List<String> List<dyn> ls1 = new List<dyn>(...); ls1.head = new Integer(1); List<String> ls2 = ls1; ls2.head.length(); Lintaro Ina Gradual Typing for Generics 18
  • 40. Run-time Compatibility [2/2] Run-time Compatibility S :≺ T ◮ Includes <: ◮ Co-variant but not contra-variant ◮ List<String> :≺ List<dyn> ◮ List<dyn> :≺ List<String> List<dyn> ls1 = new List<dyn>(...); ls1.head = new Integer(1); List<String> ls2 = ls1; ls2.head.length(); /* should not fail because it’s statically typed */ Lintaro Ina Gradual Typing for Generics 18
  • 41. Run-time Compatibility [2/2] Run-time Compatibility S :≺ T ◮ Includes <: ◮ Co-variant but not contra-variant ◮ List<String> :≺ List<dyn> ◮ List<dyn> :≺ List<String> List<dyn> ls1 = new List<dyn>(...); ls1.head = new Integer(1); List<String> ls2 = ls1; /* run-time error List<dyn> :≺ List<String> */ ls2.head.length(); // this does not execute Lintaro Ina Gradual Typing for Generics 18
  • 42. Formalization ◮ Extend Featherweight GJ (FGJ) with dyn ◮ Give a translation to an intermediate language ◮ and :≺ instead of normal subtyping <: Surface language Intermediate language FGJ + dyn FGJ + dyn + run-time checks compatibility compatibility :≺ typing (typing) reduction Lintaro Ina Gradual Typing for Generics 19
  • 43. Compatibility Relations Auxiliary Relation S ≺ T ◮ In S, dyns in T are replaced with non-dyns ∆ ⊢ T ≺ T ∆ ⊢ S ≺ T ∆ ⊢ C<S> ≺ C<T> ∆ ⊢ T <: S ∆ ⊢ S ≺ N ∆ ⊢ T ≺ dyn<N> Definitions ◮ Static compatibility S T def = S ≻ ∃ U <: ∃ V ≺ T ◮ Run-time compatibility S :≺ T def = S <: ∃ U ≺ T Lintaro Ina Gradual Typing for Generics 20
  • 44. Properties Weak Type Safety in the Intermediate Language An execution of a program of type T 1 results in a value of type S ( :≺ T), or 2 results in a failure of a run-time check, or 3 does not stop Translation from the Surface to the Intermediate A well-typed program can translate and ◮ typeability is preserved ◮ statically typed parts do not change Lintaro Ina Gradual Typing for Generics 21
  • 45. Conclusions ◮ Gradual typing for class-based languages ◮ Fine-grained integration with generics ◮ Static and run-time compatibility ◮ Bounded dynamic types ◮ Formalization ◮ FGJ + dyn + run-time checks ◮ Type safety and translation theorem Lintaro Ina Gradual Typing for Generics 22
  • 46. Future Work ◮ Decidability of and :≺ :≺ decidable? at least semidecidable? ◮ Full compiler with a large scale evaluation ◮ Integration with method overloading, wildcards, etc. ◮ Blame tracking instead of run-time compatibility checks Lintaro Ina Gradual Typing for Generics 23