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
 
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
 
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

Microphone- characteristics,carbon microphone, dynamic microphone.pptx
Microphone- characteristics,carbon microphone, dynamic microphone.pptxMicrophone- characteristics,carbon microphone, dynamic microphone.pptx
Microphone- characteristics,carbon microphone, dynamic microphone.pptxpriyankatabhane
 
Environmental Biotechnology Topic:- Microbial Biosensor
Environmental Biotechnology Topic:- Microbial BiosensorEnvironmental Biotechnology Topic:- Microbial Biosensor
Environmental Biotechnology Topic:- Microbial Biosensorsonawaneprad
 
Topic 9- General Principles of International Law.pptx
Topic 9- General Principles of International Law.pptxTopic 9- General Principles of International Law.pptx
Topic 9- General Principles of International Law.pptxJorenAcuavera1
 
Pests of jatropha_Bionomics_identification_Dr.UPR.pdf
Pests of jatropha_Bionomics_identification_Dr.UPR.pdfPests of jatropha_Bionomics_identification_Dr.UPR.pdf
Pests of jatropha_Bionomics_identification_Dr.UPR.pdfPirithiRaju
 
User Guide: Orion™ Weather Station (Columbia Weather Systems)
User Guide: Orion™ Weather Station (Columbia Weather Systems)User Guide: Orion™ Weather Station (Columbia Weather Systems)
User Guide: Orion™ Weather Station (Columbia Weather Systems)Columbia Weather Systems
 
Call Girls in Majnu Ka Tilla Delhi 🔝9711014705🔝 Genuine
Call Girls in Majnu Ka Tilla Delhi 🔝9711014705🔝 GenuineCall Girls in Majnu Ka Tilla Delhi 🔝9711014705🔝 Genuine
Call Girls in Majnu Ka Tilla Delhi 🔝9711014705🔝 Genuinethapagita
 
REVISTA DE BIOLOGIA E CIÊNCIAS DA TERRA ISSN 1519-5228 - Artigo_Bioterra_V24_...
REVISTA DE BIOLOGIA E CIÊNCIAS DA TERRA ISSN 1519-5228 - Artigo_Bioterra_V24_...REVISTA DE BIOLOGIA E CIÊNCIAS DA TERRA ISSN 1519-5228 - Artigo_Bioterra_V24_...
REVISTA DE BIOLOGIA E CIÊNCIAS DA TERRA ISSN 1519-5228 - Artigo_Bioterra_V24_...Universidade Federal de Sergipe - UFS
 
Introduction of Human Body & Structure of cell.pptx
Introduction of Human Body & Structure of cell.pptxIntroduction of Human Body & Structure of cell.pptx
Introduction of Human Body & Structure of cell.pptxMedical College
 
Fertilization: Sperm and the egg—collectively called the gametes—fuse togethe...
Fertilization: Sperm and the egg—collectively called the gametes—fuse togethe...Fertilization: Sperm and the egg—collectively called the gametes—fuse togethe...
Fertilization: Sperm and the egg—collectively called the gametes—fuse togethe...D. B. S. College Kanpur
 
Biological classification of plants with detail
Biological classification of plants with detailBiological classification of plants with detail
Biological classification of plants with detailhaiderbaloch3
 
bonjourmadame.tumblr.com bhaskar's girls
bonjourmadame.tumblr.com bhaskar's girlsbonjourmadame.tumblr.com bhaskar's girls
bonjourmadame.tumblr.com bhaskar's girlshansessene
 
Base editing, prime editing, Cas13 & RNA editing and organelle base editing
Base editing, prime editing, Cas13 & RNA editing and organelle base editingBase editing, prime editing, Cas13 & RNA editing and organelle base editing
Base editing, prime editing, Cas13 & RNA editing and organelle base editingNetHelix
 
GenAI talk for Young at Wageningen University & Research (WUR) March 2024
GenAI talk for Young at Wageningen University & Research (WUR) March 2024GenAI talk for Young at Wageningen University & Research (WUR) March 2024
GenAI talk for Young at Wageningen University & Research (WUR) March 2024Jene van der Heide
 
User Guide: Capricorn FLX™ Weather Station
User Guide: Capricorn FLX™ Weather StationUser Guide: Capricorn FLX™ Weather Station
User Guide: Capricorn FLX™ Weather StationColumbia Weather Systems
 
ECG Graph Monitoring with AD8232 ECG Sensor & Arduino.pptx
ECG Graph Monitoring with AD8232 ECG Sensor & Arduino.pptxECG Graph Monitoring with AD8232 ECG Sensor & Arduino.pptx
ECG Graph Monitoring with AD8232 ECG Sensor & Arduino.pptxmaryFF1
 
Dubai Calls Girl Lisa O525547819 Lexi Call Girls In Dubai
Dubai Calls Girl Lisa O525547819 Lexi Call Girls In DubaiDubai Calls Girl Lisa O525547819 Lexi Call Girls In Dubai
Dubai Calls Girl Lisa O525547819 Lexi Call Girls In Dubaikojalkojal131
 
Four Spheres of the Earth Presentation.ppt
Four Spheres of the Earth Presentation.pptFour Spheres of the Earth Presentation.ppt
Four Spheres of the Earth Presentation.pptJoemSTuliba
 
《Queensland毕业文凭-昆士兰大学毕业证成绩单》
《Queensland毕业文凭-昆士兰大学毕业证成绩单》《Queensland毕业文凭-昆士兰大学毕业证成绩单》
《Queensland毕业文凭-昆士兰大学毕业证成绩单》rnrncn29
 
Manassas R - Parkside Middle School 🌎🏫
Manassas R - Parkside Middle School 🌎🏫Manassas R - Parkside Middle School 🌎🏫
Manassas R - Parkside Middle School 🌎🏫qfactory1
 

Recently uploaded (20)

Microphone- characteristics,carbon microphone, dynamic microphone.pptx
Microphone- characteristics,carbon microphone, dynamic microphone.pptxMicrophone- characteristics,carbon microphone, dynamic microphone.pptx
Microphone- characteristics,carbon microphone, dynamic microphone.pptx
 
Environmental Biotechnology Topic:- Microbial Biosensor
Environmental Biotechnology Topic:- Microbial BiosensorEnvironmental Biotechnology Topic:- Microbial Biosensor
Environmental Biotechnology Topic:- Microbial Biosensor
 
Topic 9- General Principles of International Law.pptx
Topic 9- General Principles of International Law.pptxTopic 9- General Principles of International Law.pptx
Topic 9- General Principles of International Law.pptx
 
Pests of jatropha_Bionomics_identification_Dr.UPR.pdf
Pests of jatropha_Bionomics_identification_Dr.UPR.pdfPests of jatropha_Bionomics_identification_Dr.UPR.pdf
Pests of jatropha_Bionomics_identification_Dr.UPR.pdf
 
User Guide: Orion™ Weather Station (Columbia Weather Systems)
User Guide: Orion™ Weather Station (Columbia Weather Systems)User Guide: Orion™ Weather Station (Columbia Weather Systems)
User Guide: Orion™ Weather Station (Columbia Weather Systems)
 
Call Girls in Majnu Ka Tilla Delhi 🔝9711014705🔝 Genuine
Call Girls in Majnu Ka Tilla Delhi 🔝9711014705🔝 GenuineCall Girls in Majnu Ka Tilla Delhi 🔝9711014705🔝 Genuine
Call Girls in Majnu Ka Tilla Delhi 🔝9711014705🔝 Genuine
 
REVISTA DE BIOLOGIA E CIÊNCIAS DA TERRA ISSN 1519-5228 - Artigo_Bioterra_V24_...
REVISTA DE BIOLOGIA E CIÊNCIAS DA TERRA ISSN 1519-5228 - Artigo_Bioterra_V24_...REVISTA DE BIOLOGIA E CIÊNCIAS DA TERRA ISSN 1519-5228 - Artigo_Bioterra_V24_...
REVISTA DE BIOLOGIA E CIÊNCIAS DA TERRA ISSN 1519-5228 - Artigo_Bioterra_V24_...
 
Introduction of Human Body & Structure of cell.pptx
Introduction of Human Body & Structure of cell.pptxIntroduction of Human Body & Structure of cell.pptx
Introduction of Human Body & Structure of cell.pptx
 
Fertilization: Sperm and the egg—collectively called the gametes—fuse togethe...
Fertilization: Sperm and the egg—collectively called the gametes—fuse togethe...Fertilization: Sperm and the egg—collectively called the gametes—fuse togethe...
Fertilization: Sperm and the egg—collectively called the gametes—fuse togethe...
 
Biological classification of plants with detail
Biological classification of plants with detailBiological classification of plants with detail
Biological classification of plants with detail
 
bonjourmadame.tumblr.com bhaskar's girls
bonjourmadame.tumblr.com bhaskar's girlsbonjourmadame.tumblr.com bhaskar's girls
bonjourmadame.tumblr.com bhaskar's girls
 
Base editing, prime editing, Cas13 & RNA editing and organelle base editing
Base editing, prime editing, Cas13 & RNA editing and organelle base editingBase editing, prime editing, Cas13 & RNA editing and organelle base editing
Base editing, prime editing, Cas13 & RNA editing and organelle base editing
 
GenAI talk for Young at Wageningen University & Research (WUR) March 2024
GenAI talk for Young at Wageningen University & Research (WUR) March 2024GenAI talk for Young at Wageningen University & Research (WUR) March 2024
GenAI talk for Young at Wageningen University & Research (WUR) March 2024
 
User Guide: Capricorn FLX™ Weather Station
User Guide: Capricorn FLX™ Weather StationUser Guide: Capricorn FLX™ Weather Station
User Guide: Capricorn FLX™ Weather Station
 
ECG Graph Monitoring with AD8232 ECG Sensor & Arduino.pptx
ECG Graph Monitoring with AD8232 ECG Sensor & Arduino.pptxECG Graph Monitoring with AD8232 ECG Sensor & Arduino.pptx
ECG Graph Monitoring with AD8232 ECG Sensor & Arduino.pptx
 
Dubai Calls Girl Lisa O525547819 Lexi Call Girls In Dubai
Dubai Calls Girl Lisa O525547819 Lexi Call Girls In DubaiDubai Calls Girl Lisa O525547819 Lexi Call Girls In Dubai
Dubai Calls Girl Lisa O525547819 Lexi Call Girls In Dubai
 
Four Spheres of the Earth Presentation.ppt
Four Spheres of the Earth Presentation.pptFour Spheres of the Earth Presentation.ppt
Four Spheres of the Earth Presentation.ppt
 
《Queensland毕业文凭-昆士兰大学毕业证成绩单》
《Queensland毕业文凭-昆士兰大学毕业证成绩单》《Queensland毕业文凭-昆士兰大学毕业证成绩单》
《Queensland毕业文凭-昆士兰大学毕业证成绩单》
 
AZOTOBACTER AS BIOFERILIZER.PPTX
AZOTOBACTER AS BIOFERILIZER.PPTXAZOTOBACTER AS BIOFERILIZER.PPTX
AZOTOBACTER AS BIOFERILIZER.PPTX
 
Manassas R - Parkside Middle School 🌎🏫
Manassas R - Parkside Middle School 🌎🏫Manassas R - Parkside Middle School 🌎🏫
Manassas R - Parkside Middle School 🌎🏫
 

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