The document provides an introduction to type constraints. It defines type constraints as denoting the subtyping relationships that must hold between program elements for a program to be considered well-typed. Type constraints can be inferred from program constructs like assignments and field accesses based on implied subtype relationships. They can be used for applications like type checking, type inference, and refactoring by solving the generated constraint variables and subtyping relationships.
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
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
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
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
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