O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

A Brief Introduction to Type Constraints

39 visualizações

Publicada em

Type constraint lecture for the type systems section of CSCI 46000/79521 at CUNY Hunter College

Publicada em: Educação
  • Seja o primeiro a comentar

  • Seja a primeira pessoa a gostar disto

A Brief Introduction to Type Constraints

  1. 1. a brief introduction to type constraints
  2. 2. outline Introduction Type Constraints Defining Type Constraints Type Constraint Generation Applications 1
  3. 3. introduction
  4. 4. type checking The type checker is an integral part of a compiler. 1 1Courtesy Atanas Rountev 3
  5. 5. type checking For statically-typed languages, checks that assignments are compatible. 4
  6. 6. type checking For statically-typed languages, checks that assignments are compatible. Questions 1. Why is that necessary? 4
  7. 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. 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. 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. 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. 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. 12. example String studentName; What is the type of studentName? 5
  13. 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. 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. 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
  16. 16. type constraints
  17. 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. 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. 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. 20. outline Introduction Type Constraints Defining Type Constraints Type Constraint Generation Applications 9
  21. 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. 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. 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. 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. 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. 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. 27. outline Introduction Type Constraints Defining Type Constraints Type Constraint Generation Applications 12
  28. 28. inferring type constraints program construct implied type constraint(s) assignment Ei = Ej [Ej] ≤ [Ei] (1) 13
  29. 29. inferring type constraints program construct implied type constraint(s) assignment Ei = Ej [Ej] ≤ [Ei] (1) Example Code: List registeredStudents = new ArrayList(); 13
  30. 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. 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. 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. 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. 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. 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. 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
  37. 37. 3 3Courtesy CodeJava. 14
  38. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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
  49. 49. applications
  50. 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. 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
  52. 52. Questions? 20

×