SlideShare a Scribd company logo
a brief introduction to type constraints
outline
Introduction
Type Constraints
Defining Type Constraints
Type Constraint Generation
Applications
1
introduction
type checking
The type checker is an integral part of a compiler.
1
1Courtesy Atanas Rountev
3
type checking
For statically-typed languages, checks that assignments are
compatible.
4
type checking
For statically-typed languages, checks that assignments are
compatible.
Questions
1. Why is that necessary?
4
type checking
For statically-typed languages, checks that assignments are
compatible.
Questions
1. Why is that necessary?
Answers
1. ∙ Prevent data loss.
∙ Ensure that “protocols” (e.g., method calls) are valid.
4
type checking
For statically-typed languages, checks that assignments are
compatible.
Questions
1. Why is that necessary?
2. Are assignments the only thing that needs to be checked?
Answers
1. ∙ Prevent data loss.
∙ Ensure that “protocols” (e.g., method calls) are valid.
4
type checking
For statically-typed languages, checks that assignments are
compatible.
Questions
1. Why is that necessary?
2. Are assignments the only thing that needs to be checked?
Answers
1. ∙ Prevent data loss.
∙ Ensure that “protocols” (e.g., method calls) are valid.
2. Also comparisons (prevents errors).
4
type checking
For statically-typed languages, checks that assignments are
compatible.
Questions
1. Why is that necessary?
2. Are assignments the only thing that needs to be checked?
3. What about dynamic languages? Do they need to be type
checked?
Answers
1. ∙ Prevent data loss.
∙ Ensure that “protocols” (e.g., method calls) are valid.
2. Also comparisons (prevents errors).
4
type checking
For statically-typed languages, checks that assignments are
compatible.
Questions
1. Why is that necessary?
2. Are assignments the only thing that needs to be checked?
3. What about dynamic languages? Do they need to be type
checked?
Answers
1. ∙ Prevent data loss.
∙ Ensure that “protocols” (e.g., method calls) are valid.
2. Also comparisons (prevents errors).
3. No. Why?
∙ Static has two notions of types. Dynamic only has one.
∙ Can be used for type inference.
4
example
String studentName;
What is the type of studentName?
5
example
String studentName;
What is the type of studentName?
Integer waitlisted = // ...
Integer numOfStudents = 20 + waitlisted;
What is the type of the expression 20 + waitlisted?
5
example
1 List registeredStudents = new ArrayList();
2 // ...
3 registeredStudents = new HashSet();
Is the assignment at line 1 type-correct? What about at line 3?
2Courtesy CodeJava. 6
example
1 List registeredStudents = new ArrayList();
2 // ...
3 registeredStudents = new HashSet();
Is the assignment at line 1 type-correct? What about at line 3?
2
2Courtesy CodeJava. 6
type constraints
type constraints
∙ Type constraints [Palsberg and Schwartzbach, 1994; Tip et. al,
2011] can be used for type checking, type inference, and others.
8
type constraints
∙ Type constraints [Palsberg and Schwartzbach, 1994; Tip et. al,
2011] can be used for type checking, type inference, and others.
∙ Denote the (sub)typing relationships for each program element
that must hold between corresponding expressions for that
portion of the system to be considered well-typed.
8
type constraints
∙ Type constraints [Palsberg and Schwartzbach, 1994; Tip et. al,
2011] can be used for type checking, type inference, and others.
∙ Denote the (sub)typing relationships for each program element
that must hold between corresponding expressions for that
portion of the system to be considered well-typed.
∙ A complete program is type-correct if all constraints implied by
all program elements hold.
8
outline
Introduction
Type Constraints
Defining Type Constraints
Type Constraint Generation
Applications
9
notation and terminology
[E] the type of expression or declaration element E.
[M] the declared return type of method M.
Decl(M) the type that contains method M.
Decl(F) the type that contains field F.
T ≤ T T is equal to T, or T is a subtype of T.
T < T T is a proper subtype of T, i.e., T ≤ T ∧ T T .
10
notation and terminology
[E] the type of expression or declaration element E.
[M] the declared return type of method M.
Decl(M) the type that contains method M.
Decl(F) the type that contains field F.
T ≤ T T is equal to T, or T is a subtype of T.
T < T T is a proper subtype of T, i.e., T ≤ T ∧ T T .
Interfaces?
If T and T (types) are both classes, it’s clear that T < T means T
extends T. But, what if T or T is an interface?
10
notation and terminology
[E] the type of expression or declaration element E.
[M] the declared return type of method M.
Decl(M) the type that contains method M.
Decl(F) the type that contains field F.
T ≤ T T is equal to T, or T is a subtype of T.
T < T T is a proper subtype of T, i.e., T ≤ T ∧ T T .
Interfaces?
If T and T (types) are both classes, it’s clear that T < T means T
extends T. But, what if T or T is an interface?
Two relationships: implements and extends.
10
notation and terminology
[E] the type of expression or declaration element E.
[M] the declared return type of method M.
Decl(M) the type that contains method M.
Decl(F) the type that contains field F.
T ≤ T T is equal to T, or T is a subtype of T.
T < T T is a proper subtype of T, i.e., T ≤ T ∧ T T .
Interfaces?
If T and T (types) are both classes, it’s clear that T < T means T
extends T. But, what if T or T is an interface?
Two relationships: implements and extends.
∙ If T is a class and T is an interface, T < T means that T
implements T.
10
notation and terminology
[E] the type of expression or declaration element E.
[M] the declared return type of method M.
Decl(M) the type that contains method M.
Decl(F) the type that contains field F.
T ≤ T T is equal to T, or T is a subtype of T.
T < T T is a proper subtype of T, i.e., T ≤ T ∧ T T .
Interfaces?
If T and T (types) are both classes, it’s clear that T < T means T
extends T. But, what if T or T is an interface?
Two relationships: implements and extends.
∙ If T is a class and T is an interface, T < T means that T
implements T.
∙ If both T and T are interfaces, then T < T means that T extends
T.
10
Let α be a constraint variable, which can be a type constant T, [E], i.e.,
the type of an expression or declaration element E, Decl(M), i.e., the
type declaring method M, or Decl(F), the type declaring field F. Then,
a type constraint can be one of:
∙ αi αj, i.e., αi is defined to be the same as αj,
∙ αi ≤ αj, i.e., αi must be equal to or a subtype of αj,
∙ αi = αj, i.e., αi ≤ αj ∧ αj ≤ αi, and
∙ αi < αj, i.e., αi ≤ αj ∧ αj αi.
11
outline
Introduction
Type Constraints
Defining Type Constraints
Type Constraint Generation
Applications
12
inferring type constraints
program construct implied type constraint(s)
assignment Ei = Ej
[Ej] ≤ [Ei] (1)
13
inferring type constraints
program construct implied type constraint(s)
assignment Ei = Ej
[Ej] ≤ [Ei] (1)
Example
Code:
List registeredStudents = new ArrayList();
13
inferring type constraints
program construct implied type constraint(s)
assignment Ei = Ej
[Ej] ≤ [Ei] (1)
Example
Code:
List registeredStudents = new ArrayList();
Inferred Type Constraint(s): ?
13
inferring type constraints
program construct implied type constraint(s)
assignment Ei = Ej
[Ej] ≤ [Ei] (1)
Example
Code:
List registeredStudents = new ArrayList();
Inferred Type Constraint(s): ?
[new ArrayList()] ≤ [registeredStudents]
13
inferring type constraints
program construct implied type constraint(s)
assignment Ei = Ej
[Ej] ≤ [Ei] (1)
Example
Code:
List registeredStudents = new ArrayList();
Inferred Type Constraint(s): ?
[new ArrayList()] ≤ [registeredStudents]
Is it true?
13
inferring type constraints
program construct implied type constraint(s)
assignment Ei = Ej
[Ej] ≤ [Ei] (1)
Example
Code:
List registeredStudents = new ArrayList();
Inferred Type Constraint(s): ?
[new ArrayList()] ≤ [registeredStudents]
Is it true?
Example
Code:
registeredStudents = new HashSet();
13
inferring type constraints
program construct implied type constraint(s)
assignment Ei = Ej
[Ej] ≤ [Ei] (1)
Example
Code:
List registeredStudents = new ArrayList();
Inferred Type Constraint(s): ?
[new ArrayList()] ≤ [registeredStudents]
Is it true?
Example
Code:
registeredStudents = new HashSet();
Inferred Type Constraint(s): ?
13
inferring type constraints
program construct implied type constraint(s)
assignment Ei = Ej
[Ej] ≤ [Ei] (1)
Example
Code:
List registeredStudents = new ArrayList();
Inferred Type Constraint(s): ?
[new ArrayList()] ≤ [registeredStudents]
Is it true?
Example
Code:
registeredStudents = new HashSet();
Inferred Type Constraint(s): ?
[new HashSet()] ≤ [registeredStudents]
13
inferring type constraints
program construct implied type constraint(s)
assignment Ei = Ej
[Ej] ≤ [Ei] (1)
Example
Code:
List registeredStudents = new ArrayList();
Inferred Type Constraint(s): ?
[new ArrayList()] ≤ [registeredStudents]
Is it true?
Example
Code:
registeredStudents = new HashSet();
Inferred Type Constraint(s): ?
[new HashSet()] ≤ [registeredStudents]
Is it true?
13
3
3Courtesy CodeJava.
14
inferring type constraints
program construct implied type constraint(s)
access E.f to field F
[E.f] [F] (2)
[E] ≤ Decl(F) (3)
15
inferring type constraints
program construct implied type constraint(s)
access E.f to field F
[E.f] [F] (2)
[E] ≤ Decl(F) (3)
Example
Code:
class Student {
String name;
}
Student s = new Student();
s.name = ”Aairah”;
15
inferring type constraints
program construct implied type constraint(s)
access E.f to field F
[E.f] [F] (2)
[E] ≤ Decl(F) (3)
Example
Code:
class Student {
String name;
}
Student s = new Student();
s.name = ”Aairah”;
Inferred Type Constraint(s): ?
15
inferring type constraints
program construct implied type constraint(s)
access E.f to field F
[E.f] [F] (2)
[E] ≤ Decl(F) (3)
Example
Code:
class Student {
String name;
}
Student s = new Student();
s.name = ”Aairah”;
Inferred Type Constraint(s): ?
[s.name] String
15
inferring type constraints
program construct implied type constraint(s)
access E.f to field F
[E.f] [F] (2)
[E] ≤ Decl(F) (3)
Example
Code:
class Student {
String name;
}
Student s = new Student();
s.name = ”Aairah”;
Inferred Type Constraint(s): ?
[s.name] String
[s] ≤ Decl(name)
[s] ≤ Student
15
inferring type constraints
program construct implied type constraint(s)
access E.f to field F
[E.f] [F] (2)
[E] ≤ Decl(F) (3)
Example
Code:
class Student {
String name;
}
Student s = new Student();
s.name = ”Aairah”;
Inferred Type Constraint(s): ?
[s.name] String
[s] ≤ Decl(name)
[s] ≤ Student
Is it true?
15
inferring type constraints
program construct implied type constraint(s)
access E.f to field F
[E.f] [F] (4)
[E] ≤ Decl(F) (5)
Example
Code:
class Student {
String name;
}
Student s = new Student();
s.age = 5;
16
inferring type constraints
program construct implied type constraint(s)
access E.f to field F
[E.f] [F] (4)
[E] ≤ Decl(F) (5)
Example
Code:
class Student {
String name;
}
Student s = new Student();
s.age = 5;
Inferred Type Constraint(s): ?
16
inferring type constraints
program construct implied type constraint(s)
access E.f to field F
[E.f] [F] (4)
[E] ≤ Decl(F) (5)
Example
Code:
class Student {
String name;
}
Student s = new Student();
s.age = 5;
Inferred Type Constraint(s): ?
[s.age] ?
16
inferring type constraints
program construct implied type constraint(s)
access E.f to field F
[E.f] [F] (4)
[E] ≤ Decl(F) (5)
Example
Code:
class Student {
String name;
}
Student s = new Student();
s.age = 5;
Inferred Type Constraint(s): ?
[s.age] ?
[s] ≤ Decl(age)
[s] ≤ ?
16
inferring type constraints
program construct implied type constraint(s)
access E.f to field F
[E.f] [F] (4)
[E] ≤ Decl(F) (5)
Example
Code:
class Student {
String name;
}
Student s = new Student();
s.age = 5;
Inferred Type Constraint(s): ?
[s.age] ?
[s] ≤ Decl(age)
[s] ≤ ?
age is undefined.
16
applications
where else are type constraints used?
Type constraints can be used in other ways other than for type
checking. For example:
∙ Type inference.
∙ Given how a variable is used, what type do we think it is?
∙ Constraint variables can be used in implied type constraints.
∙ Solve the constraints.
∙ Has been used to infer type parameters (generics) for raw types.
∙ Refactoring.
∙ Source-to-source semantics-preserving transformations.
∙ Used to ensure method dispatch semantics are the same before
and after transformation.
18
for further reading
J. Palsberg and M. I. Schwartzbach, Object-oriented type systems.
John Wiley and Sons Ltd., 1994.
F. Tip, R. M. Fuhrer, A. Kieżun, M. D. Ernst, I. Balaban, and
B. De Sutter, “Refactoring using type constraints,” ACM
Transactions on Programming Languages and Systems, vol. 33,
no. 3, pp. 9:1–9:47, May 2011.
19
Questions?
20

More Related Content

What's hot

Lecture 4 classes ii
Lecture 4 classes iiLecture 4 classes ii
Lecture 4 classes ii
the_wumberlog
 
2 variables and data types
2   variables and data types2   variables and data types
2 variables and data types
Tuan Ngo
 
Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving Techniques
valarpink
 
Chaps 1-3-ai-prolog
Chaps 1-3-ai-prologChaps 1-3-ai-prolog
Chaps 1-3-ai-prolog
saru40
 

What's hot (20)

Fundamentals of Computing and C Programming - Part 2
Fundamentals of Computing and C Programming - Part 2Fundamentals of Computing and C Programming - Part 2
Fundamentals of Computing and C Programming - Part 2
 
Theory of Computation Lecture Notes
Theory of Computation Lecture NotesTheory of Computation Lecture Notes
Theory of Computation Lecture Notes
 
ITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in javaITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in java
 
Lecture 4 classes ii
Lecture 4 classes iiLecture 4 classes ii
Lecture 4 classes ii
 
2 variables and data types
2   variables and data types2   variables and data types
2 variables and data types
 
Fundamentals of Computing and C Programming - Part 1
Fundamentals of Computing and C Programming - Part 1Fundamentals of Computing and C Programming - Part 1
Fundamentals of Computing and C Programming - Part 1
 
2nd PUC Computer science chapter 5 review of c++
2nd PUC Computer science chapter 5   review of c++2nd PUC Computer science chapter 5   review of c++
2nd PUC Computer science chapter 5 review of c++
 
Java: Primitive Data Types
Java: Primitive Data TypesJava: Primitive Data Types
Java: Primitive Data Types
 
Fafl notes [2010] (sjbit)
Fafl notes [2010] (sjbit)Fafl notes [2010] (sjbit)
Fafl notes [2010] (sjbit)
 
Module 1:Introduction
Module 1:IntroductionModule 1:Introduction
Module 1:Introduction
 
Programming construction tools
Programming construction toolsProgramming construction tools
Programming construction tools
 
Java unit 2
Java unit 2Java unit 2
Java unit 2
 
5 introduction-to-c
5 introduction-to-c5 introduction-to-c
5 introduction-to-c
 
Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving Techniques
 
Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)
 
Csharp4 basics
Csharp4 basicsCsharp4 basics
Csharp4 basics
 
+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II Notes+2 Computer Science - Volume II Notes
+2 Computer Science - Volume II Notes
 
Parsing
ParsingParsing
Parsing
 
Chaps 1-3-ai-prolog
Chaps 1-3-ai-prologChaps 1-3-ai-prolog
Chaps 1-3-ai-prolog
 
Btech i pic u-2 datatypes and variables in c language
Btech i pic u-2 datatypes and variables in c languageBtech i pic u-2 datatypes and variables in c language
Btech i pic u-2 datatypes and variables in c language
 

Similar to A Brief Introduction to Type Constraints

Applying Generics
Applying GenericsApplying Generics
Applying Generics
Bharat17485
 

Similar to A Brief Introduction to Type Constraints (20)

Ch6
Ch6Ch6
Ch6
 
From DOT to Dotty
From DOT to DottyFrom DOT to Dotty
From DOT to Dotty
 
Calculating Projections via Type Checking
Calculating Projections via Type CheckingCalculating Projections via Type Checking
Calculating Projections via Type Checking
 
Ch6.ppt
Ch6.pptCh6.ppt
Ch6.ppt
 
Applying Generics
Applying GenericsApplying Generics
Applying Generics
 
Types, classes and concepts
Types, classes and conceptsTypes, classes and concepts
Types, classes and concepts
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
 
Swift Generics
Swift GenericsSwift Generics
Swift Generics
 
Intro to modelling_wur
Intro to modelling_wurIntro to modelling_wur
Intro to modelling_wur
 
Generic Programming in java
Generic Programming in javaGeneric Programming in java
Generic Programming in java
 
Python variables and data types.pptx
Python variables and data types.pptxPython variables and data types.pptx
Python variables and data types.pptx
 
Declare Your Language: Name Resolution
Declare Your Language: Name ResolutionDeclare Your Language: Name Resolution
Declare Your Language: Name Resolution
 
Foundations of Programming Part II
Foundations of Programming Part IIFoundations of Programming Part II
Foundations of Programming Part II
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Declarative Semantics Definition - Static Analysis and Error Checking
Declarative Semantics Definition - Static Analysis and Error CheckingDeclarative Semantics Definition - Static Analysis and Error Checking
Declarative Semantics Definition - Static Analysis and Error Checking
 
QB104545.pdf
QB104545.pdfQB104545.pdf
QB104545.pdf
 
Generics
GenericsGenerics
Generics
 
Chapter 6 Intermediate Code Generation
Chapter 6   Intermediate Code GenerationChapter 6   Intermediate Code Generation
Chapter 6 Intermediate Code Generation
 
Generics in java
Generics in javaGenerics in java
Generics in java
 
12TypeSystem.pdf
12TypeSystem.pdf12TypeSystem.pdf
12TypeSystem.pdf
 

More from Raffi Khatchadourian

Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
Raffi Khatchadourian
 
Automated Evolution of Feature Logging Statement Levels Using Git Histories a...
Automated Evolution of Feature Logging Statement Levels Using Git Histories a...Automated Evolution of Feature Logging Statement Levels Using Git Histories a...
Automated Evolution of Feature Logging Statement Levels Using Git Histories a...
Raffi Khatchadourian
 
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Raffi Khatchadourian
 
Actor Concurrency Bugs: A Comprehensive Study on Symptoms, Root Causes, API U...
Actor Concurrency Bugs: A Comprehensive Study on Symptoms, Root Causes, API U...Actor Concurrency Bugs: A Comprehensive Study on Symptoms, Root Causes, API U...
Actor Concurrency Bugs: A Comprehensive Study on Symptoms, Root Causes, API U...
Raffi Khatchadourian
 
An Empirical Study of Refactorings and Technical Debt in Machine Learning Sys...
An Empirical Study of Refactorings and Technical Debt in Machine Learning Sys...An Empirical Study of Refactorings and Technical Debt in Machine Learning Sys...
An Empirical Study of Refactorings and Technical Debt in Machine Learning Sys...
Raffi Khatchadourian
 
Automated Evolution of Feature Logging Statement Levels Using Git Histories a...
Automated Evolution of Feature Logging Statement Levels Using Git Histories a...Automated Evolution of Feature Logging Statement Levels Using Git Histories a...
Automated Evolution of Feature Logging Statement Levels Using Git Histories a...
Raffi Khatchadourian
 
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams
Safe Automated Refactoring for Intelligent Parallelization of Java 8 StreamsSafe Automated Refactoring for Intelligent Parallelization of Java 8 Streams
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams
Raffi Khatchadourian
 
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams
Safe Automated Refactoring for Intelligent Parallelization of Java 8 StreamsSafe Automated Refactoring for Intelligent Parallelization of Java 8 Streams
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams
Raffi Khatchadourian
 
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams ...
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams ...Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams ...
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams ...
Raffi Khatchadourian
 
A Tool for Optimizing Java 8 Stream Software via Automated Refactoring
A Tool for Optimizing Java 8 Stream Software via Automated RefactoringA Tool for Optimizing Java 8 Stream Software via Automated Refactoring
A Tool for Optimizing Java 8 Stream Software via Automated Refactoring
Raffi Khatchadourian
 
Proactive Empirical Assessment of New Language Feature Adoption via Automated...
Proactive Empirical Assessment of New Language Feature Adoption via Automated...Proactive Empirical Assessment of New Language Feature Adoption via Automated...
Proactive Empirical Assessment of New Language Feature Adoption via Automated...
Raffi Khatchadourian
 

More from Raffi Khatchadourian (20)

Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
Towards Safe Automated Refactoring of Imperative Deep Learning Programs to Gr...
 
Automated Evolution of Feature Logging Statement Levels Using Git Histories a...
Automated Evolution of Feature Logging Statement Levels Using Git Histories a...Automated Evolution of Feature Logging Statement Levels Using Git Histories a...
Automated Evolution of Feature Logging Statement Levels Using Git Histories a...
 
A Tool for Rejuvenating Feature Logging Levels via Git Histories and Degree o...
A Tool for Rejuvenating Feature Logging Levels via Git Histories and Degree o...A Tool for Rejuvenating Feature Logging Levels via Git Histories and Degree o...
A Tool for Rejuvenating Feature Logging Levels via Git Histories and Degree o...
 
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
 
Actor Concurrency Bugs: A Comprehensive Study on Symptoms, Root Causes, API U...
Actor Concurrency Bugs: A Comprehensive Study on Symptoms, Root Causes, API U...Actor Concurrency Bugs: A Comprehensive Study on Symptoms, Root Causes, API U...
Actor Concurrency Bugs: A Comprehensive Study on Symptoms, Root Causes, API U...
 
An Empirical Study of Refactorings and Technical Debt in Machine Learning Sys...
An Empirical Study of Refactorings and Technical Debt in Machine Learning Sys...An Empirical Study of Refactorings and Technical Debt in Machine Learning Sys...
An Empirical Study of Refactorings and Technical Debt in Machine Learning Sys...
 
Automated Evolution of Feature Logging Statement Levels Using Git Histories a...
Automated Evolution of Feature Logging Statement Levels Using Git Histories a...Automated Evolution of Feature Logging Statement Levels Using Git Histories a...
Automated Evolution of Feature Logging Statement Levels Using Git Histories a...
 
An Empirical Study on the Use and Misuse of Java 8 Streams
An Empirical Study on the Use and Misuse of Java 8 StreamsAn Empirical Study on the Use and Misuse of Java 8 Streams
An Empirical Study on the Use and Misuse of Java 8 Streams
 
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams
Safe Automated Refactoring for Intelligent Parallelization of Java 8 StreamsSafe Automated Refactoring for Intelligent Parallelization of Java 8 Streams
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams
 
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams
Safe Automated Refactoring for Intelligent Parallelization of Java 8 StreamsSafe Automated Refactoring for Intelligent Parallelization of Java 8 Streams
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams
 
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams ...
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams ...Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams ...
Safe Automated Refactoring for Intelligent Parallelization of Java 8 Streams ...
 
A Tool for Optimizing Java 8 Stream Software via Automated Refactoring
A Tool for Optimizing Java 8 Stream Software via Automated RefactoringA Tool for Optimizing Java 8 Stream Software via Automated Refactoring
A Tool for Optimizing Java 8 Stream Software via Automated Refactoring
 
Porting the NetBeans Java 8 Enhanced For Loop Lambda Expression Refactoring t...
Porting the NetBeans Java 8 Enhanced For Loop Lambda Expression Refactoring t...Porting the NetBeans Java 8 Enhanced For Loop Lambda Expression Refactoring t...
Porting the NetBeans Java 8 Enhanced For Loop Lambda Expression Refactoring t...
 
Towards Safe Refactoring for Intelligent Parallelization of Java 8 Streams
Towards Safe Refactoring for Intelligent Parallelization of Java 8 StreamsTowards Safe Refactoring for Intelligent Parallelization of Java 8 Streams
Towards Safe Refactoring for Intelligent Parallelization of Java 8 Streams
 
Proactive Empirical Assessment of New Language Feature Adoption via Automated...
Proactive Empirical Assessment of New Language Feature Adoption via Automated...Proactive Empirical Assessment of New Language Feature Adoption via Automated...
Proactive Empirical Assessment of New Language Feature Adoption via Automated...
 
Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...
 
Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...
 
Automated Refactoring of Legacy Java Software to Default Methods Talk at ICSE...
Automated Refactoring of Legacy Java Software to Default Methods Talk at ICSE...Automated Refactoring of Legacy Java Software to Default Methods Talk at ICSE...
Automated Refactoring of Legacy Java Software to Default Methods Talk at ICSE...
 
Poster on Automated Refactoring of Legacy Java Software to Default Methods
Poster on Automated Refactoring of Legacy Java Software to Default MethodsPoster on Automated Refactoring of Legacy Java Software to Default Methods
Poster on Automated Refactoring of Legacy Java Software to Default Methods
 
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMU
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMUAutomated Refactoring of Legacy Java Software to Default Methods Talk at GMU
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMU
 

Recently uploaded

Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
YibeltalNibretu
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 

Recently uploaded (20)

Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 

A Brief Introduction to Type Constraints

  • 1. a brief introduction to type constraints
  • 2. outline Introduction Type Constraints Defining Type Constraints Type Constraint Generation Applications 1
  • 4. type checking The type checker is an integral part of a compiler. 1 1Courtesy Atanas Rountev 3
  • 5. type checking For statically-typed languages, checks that assignments are compatible. 4
  • 6. type checking For statically-typed languages, checks that assignments are compatible. Questions 1. Why is that necessary? 4
  • 7. type checking For statically-typed languages, checks that assignments are compatible. Questions 1. Why is that necessary? Answers 1. ∙ Prevent data loss. ∙ Ensure that “protocols” (e.g., method calls) are valid. 4
  • 8. type checking For statically-typed languages, checks that assignments are compatible. Questions 1. Why is that necessary? 2. Are assignments the only thing that needs to be checked? Answers 1. ∙ Prevent data loss. ∙ Ensure that “protocols” (e.g., method calls) are valid. 4
  • 9. type checking For statically-typed languages, checks that assignments are compatible. Questions 1. Why is that necessary? 2. Are assignments the only thing that needs to be checked? Answers 1. ∙ Prevent data loss. ∙ Ensure that “protocols” (e.g., method calls) are valid. 2. Also comparisons (prevents errors). 4
  • 10. type checking For statically-typed languages, checks that assignments are compatible. Questions 1. Why is that necessary? 2. Are assignments the only thing that needs to be checked? 3. What about dynamic languages? Do they need to be type checked? Answers 1. ∙ Prevent data loss. ∙ Ensure that “protocols” (e.g., method calls) are valid. 2. Also comparisons (prevents errors). 4
  • 11. type checking For statically-typed languages, checks that assignments are compatible. Questions 1. Why is that necessary? 2. Are assignments the only thing that needs to be checked? 3. What about dynamic languages? Do they need to be type checked? Answers 1. ∙ Prevent data loss. ∙ Ensure that “protocols” (e.g., method calls) are valid. 2. Also comparisons (prevents errors). 3. No. Why? ∙ Static has two notions of types. Dynamic only has one. ∙ Can be used for type inference. 4
  • 12. example String studentName; What is the type of studentName? 5
  • 13. example String studentName; What is the type of studentName? Integer waitlisted = // ... Integer numOfStudents = 20 + waitlisted; What is the type of the expression 20 + waitlisted? 5
  • 14. example 1 List registeredStudents = new ArrayList(); 2 // ... 3 registeredStudents = new HashSet(); Is the assignment at line 1 type-correct? What about at line 3? 2Courtesy CodeJava. 6
  • 15. example 1 List registeredStudents = new ArrayList(); 2 // ... 3 registeredStudents = new HashSet(); Is the assignment at line 1 type-correct? What about at line 3? 2 2Courtesy CodeJava. 6
  • 17. type constraints ∙ Type constraints [Palsberg and Schwartzbach, 1994; Tip et. al, 2011] can be used for type checking, type inference, and others. 8
  • 18. type constraints ∙ Type constraints [Palsberg and Schwartzbach, 1994; Tip et. al, 2011] can be used for type checking, type inference, and others. ∙ Denote the (sub)typing relationships for each program element that must hold between corresponding expressions for that portion of the system to be considered well-typed. 8
  • 19. type constraints ∙ Type constraints [Palsberg and Schwartzbach, 1994; Tip et. al, 2011] can be used for type checking, type inference, and others. ∙ Denote the (sub)typing relationships for each program element that must hold between corresponding expressions for that portion of the system to be considered well-typed. ∙ A complete program is type-correct if all constraints implied by all program elements hold. 8
  • 20. outline Introduction Type Constraints Defining Type Constraints Type Constraint Generation Applications 9
  • 21. notation and terminology [E] the type of expression or declaration element E. [M] the declared return type of method M. Decl(M) the type that contains method M. Decl(F) the type that contains field F. T ≤ T T is equal to T, or T is a subtype of T. T < T T is a proper subtype of T, i.e., T ≤ T ∧ T T . 10
  • 22. notation and terminology [E] the type of expression or declaration element E. [M] the declared return type of method M. Decl(M) the type that contains method M. Decl(F) the type that contains field F. T ≤ T T is equal to T, or T is a subtype of T. T < T T is a proper subtype of T, i.e., T ≤ T ∧ T T . Interfaces? If T and T (types) are both classes, it’s clear that T < T means T extends T. But, what if T or T is an interface? 10
  • 23. notation and terminology [E] the type of expression or declaration element E. [M] the declared return type of method M. Decl(M) the type that contains method M. Decl(F) the type that contains field F. T ≤ T T is equal to T, or T is a subtype of T. T < T T is a proper subtype of T, i.e., T ≤ T ∧ T T . Interfaces? If T and T (types) are both classes, it’s clear that T < T means T extends T. But, what if T or T is an interface? Two relationships: implements and extends. 10
  • 24. notation and terminology [E] the type of expression or declaration element E. [M] the declared return type of method M. Decl(M) the type that contains method M. Decl(F) the type that contains field F. T ≤ T T is equal to T, or T is a subtype of T. T < T T is a proper subtype of T, i.e., T ≤ T ∧ T T . Interfaces? If T and T (types) are both classes, it’s clear that T < T means T extends T. But, what if T or T is an interface? Two relationships: implements and extends. ∙ If T is a class and T is an interface, T < T means that T implements T. 10
  • 25. notation and terminology [E] the type of expression or declaration element E. [M] the declared return type of method M. Decl(M) the type that contains method M. Decl(F) the type that contains field F. T ≤ T T is equal to T, or T is a subtype of T. T < T T is a proper subtype of T, i.e., T ≤ T ∧ T T . Interfaces? If T and T (types) are both classes, it’s clear that T < T means T extends T. But, what if T or T is an interface? Two relationships: implements and extends. ∙ If T is a class and T is an interface, T < T means that T implements T. ∙ If both T and T are interfaces, then T < T means that T extends T. 10
  • 26. Let α be a constraint variable, which can be a type constant T, [E], i.e., the type of an expression or declaration element E, Decl(M), i.e., the type declaring method M, or Decl(F), the type declaring field F. Then, a type constraint can be one of: ∙ αi αj, i.e., αi is defined to be the same as αj, ∙ αi ≤ αj, i.e., αi must be equal to or a subtype of αj, ∙ αi = αj, i.e., αi ≤ αj ∧ αj ≤ αi, and ∙ αi < αj, i.e., αi ≤ αj ∧ αj αi. 11
  • 27. outline Introduction Type Constraints Defining Type Constraints Type Constraint Generation Applications 12
  • 28. inferring type constraints program construct implied type constraint(s) assignment Ei = Ej [Ej] ≤ [Ei] (1) 13
  • 29. inferring type constraints program construct implied type constraint(s) assignment Ei = Ej [Ej] ≤ [Ei] (1) Example Code: List registeredStudents = new ArrayList(); 13
  • 30. inferring type constraints program construct implied type constraint(s) assignment Ei = Ej [Ej] ≤ [Ei] (1) Example Code: List registeredStudents = new ArrayList(); Inferred Type Constraint(s): ? 13
  • 31. inferring type constraints program construct implied type constraint(s) assignment Ei = Ej [Ej] ≤ [Ei] (1) Example Code: List registeredStudents = new ArrayList(); Inferred Type Constraint(s): ? [new ArrayList()] ≤ [registeredStudents] 13
  • 32. inferring type constraints program construct implied type constraint(s) assignment Ei = Ej [Ej] ≤ [Ei] (1) Example Code: List registeredStudents = new ArrayList(); Inferred Type Constraint(s): ? [new ArrayList()] ≤ [registeredStudents] Is it true? 13
  • 33. inferring type constraints program construct implied type constraint(s) assignment Ei = Ej [Ej] ≤ [Ei] (1) Example Code: List registeredStudents = new ArrayList(); Inferred Type Constraint(s): ? [new ArrayList()] ≤ [registeredStudents] Is it true? Example Code: registeredStudents = new HashSet(); 13
  • 34. inferring type constraints program construct implied type constraint(s) assignment Ei = Ej [Ej] ≤ [Ei] (1) Example Code: List registeredStudents = new ArrayList(); Inferred Type Constraint(s): ? [new ArrayList()] ≤ [registeredStudents] Is it true? Example Code: registeredStudents = new HashSet(); Inferred Type Constraint(s): ? 13
  • 35. inferring type constraints program construct implied type constraint(s) assignment Ei = Ej [Ej] ≤ [Ei] (1) Example Code: List registeredStudents = new ArrayList(); Inferred Type Constraint(s): ? [new ArrayList()] ≤ [registeredStudents] Is it true? Example Code: registeredStudents = new HashSet(); Inferred Type Constraint(s): ? [new HashSet()] ≤ [registeredStudents] 13
  • 36. inferring type constraints program construct implied type constraint(s) assignment Ei = Ej [Ej] ≤ [Ei] (1) Example Code: List registeredStudents = new ArrayList(); Inferred Type Constraint(s): ? [new ArrayList()] ≤ [registeredStudents] Is it true? Example Code: registeredStudents = new HashSet(); Inferred Type Constraint(s): ? [new HashSet()] ≤ [registeredStudents] Is it true? 13
  • 38. inferring type constraints program construct implied type constraint(s) access E.f to field F [E.f] [F] (2) [E] ≤ Decl(F) (3) 15
  • 39. inferring type constraints program construct implied type constraint(s) access E.f to field F [E.f] [F] (2) [E] ≤ Decl(F) (3) Example Code: class Student { String name; } Student s = new Student(); s.name = ”Aairah”; 15
  • 40. inferring type constraints program construct implied type constraint(s) access E.f to field F [E.f] [F] (2) [E] ≤ Decl(F) (3) Example Code: class Student { String name; } Student s = new Student(); s.name = ”Aairah”; Inferred Type Constraint(s): ? 15
  • 41. inferring type constraints program construct implied type constraint(s) access E.f to field F [E.f] [F] (2) [E] ≤ Decl(F) (3) Example Code: class Student { String name; } Student s = new Student(); s.name = ”Aairah”; Inferred Type Constraint(s): ? [s.name] String 15
  • 42. inferring type constraints program construct implied type constraint(s) access E.f to field F [E.f] [F] (2) [E] ≤ Decl(F) (3) Example Code: class Student { String name; } Student s = new Student(); s.name = ”Aairah”; Inferred Type Constraint(s): ? [s.name] String [s] ≤ Decl(name) [s] ≤ Student 15
  • 43. inferring type constraints program construct implied type constraint(s) access E.f to field F [E.f] [F] (2) [E] ≤ Decl(F) (3) Example Code: class Student { String name; } Student s = new Student(); s.name = ”Aairah”; Inferred Type Constraint(s): ? [s.name] String [s] ≤ Decl(name) [s] ≤ Student Is it true? 15
  • 44. inferring type constraints program construct implied type constraint(s) access E.f to field F [E.f] [F] (4) [E] ≤ Decl(F) (5) Example Code: class Student { String name; } Student s = new Student(); s.age = 5; 16
  • 45. inferring type constraints program construct implied type constraint(s) access E.f to field F [E.f] [F] (4) [E] ≤ Decl(F) (5) Example Code: class Student { String name; } Student s = new Student(); s.age = 5; Inferred Type Constraint(s): ? 16
  • 46. inferring type constraints program construct implied type constraint(s) access E.f to field F [E.f] [F] (4) [E] ≤ Decl(F) (5) Example Code: class Student { String name; } Student s = new Student(); s.age = 5; Inferred Type Constraint(s): ? [s.age] ? 16
  • 47. inferring type constraints program construct implied type constraint(s) access E.f to field F [E.f] [F] (4) [E] ≤ Decl(F) (5) Example Code: class Student { String name; } Student s = new Student(); s.age = 5; Inferred Type Constraint(s): ? [s.age] ? [s] ≤ Decl(age) [s] ≤ ? 16
  • 48. inferring type constraints program construct implied type constraint(s) access E.f to field F [E.f] [F] (4) [E] ≤ Decl(F) (5) Example Code: class Student { String name; } Student s = new Student(); s.age = 5; Inferred Type Constraint(s): ? [s.age] ? [s] ≤ Decl(age) [s] ≤ ? age is undefined. 16
  • 50. where else are type constraints used? Type constraints can be used in other ways other than for type checking. For example: ∙ Type inference. ∙ Given how a variable is used, what type do we think it is? ∙ Constraint variables can be used in implied type constraints. ∙ Solve the constraints. ∙ Has been used to infer type parameters (generics) for raw types. ∙ Refactoring. ∙ Source-to-source semantics-preserving transformations. ∙ Used to ensure method dispatch semantics are the same before and after transformation. 18
  • 51. for further reading J. Palsberg and M. I. Schwartzbach, Object-oriented type systems. John Wiley and Sons Ltd., 1994. F. Tip, R. M. Fuhrer, A. Kieżun, M. D. Ernst, I. Balaban, and B. De Sutter, “Refactoring using type constraints,” ACM Transactions on Programming Languages and Systems, vol. 33, no. 3, pp. 9:1–9:47, May 2011. 19