Type Systems

783 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
783
On SlideShare
0
From Embeds
0
Number of Embeds
42
Actions
Shares
0
Downloads
50
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Type Systems

  1. 1. Typing Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 2 / 30
  2. 2. The Purpose of Typing ”Well-typed programs never go wrong.” Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 3 / 30
  3. 3. What Does ”Go Wrong” Entail? Execution errors. Program crash. Divergence. ... Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 4 / 30
  4. 4. The Purpose of Typing (II) As a side effect, typed programs are also more likely to actually do what they are supposed to do. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 5 / 30
  5. 5. Two Kinds of Typing Static typing. Dynamic typing (more precisely: dynamic checking). Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 6 / 30
  6. 6. Why Do They Both Exist? It is provenly impossible to create a type system that rejects all incorrect programs (written in a real-world programming language) and accepts all correct ones. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 7 / 30
  7. 7. Why Do They Both Exist? It is provenly impossible to create a type system that rejects all incorrect programs (written in a real-world programming language) and accepts all correct ones. Benefits vs. costs trade-off: static typing rejects at least some incorrect programs at the expense of not accepting certain correct ones. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 7 / 30
  8. 8. Example class Foo { public void urgh() { ...} } class Bar { public void urgh() { ...} } void main() { Object var; if ( something()) var = new Foo(); else var = new Bar(); var.urgh(); } Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 8 / 30
  9. 9. Example class Foo { public void urgh() { ...} } class Bar { public void urgh() { ...} } void main() { Object var; if ( something()) var = new Foo(); else var = new Bar(); var.urgh(); } It is obvious that an object pointed at by var has method urgh, however, many type systems don’t provide any way of expressing it without modifying (and recompiling) definitions of Foo and Bar. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 8 / 30
  10. 10. Which Is Better? Debating which is better may (and probably will) get you into a violent flamewar. The real question is whether there will ever be a type system that does not stand in programmers’ way and rejects all incorrect programs. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 9 / 30
  11. 11. Relaxed Notions of Typing Untrapped errors vs. trapped errors, forbidden errors. Safe languages disallow untrapped errors (usually by a mixture of static and runtime checks). Strongly typed languages disallow forbidden errors (ditto). Weakly typed languages allow even for untrapped errors. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 10 / 30
  12. 12. Type Algebra Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 11 / 30
  13. 13. Function Type A→B Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 12 / 30
  14. 14. Product Type A×B Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 13 / 30
  15. 15. Intersection Type A∩B Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 14 / 30
  16. 16. Union Type A∪B Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 15 / 30
  17. 17. Type System Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 16 / 30
  18. 18. Two Components of Type Control Type system—a specification. Type checking algorithm—an algorithm. There may be zero or more than one distinct type checking algorithms for a particular type system. Type systems described in a formal language have lower probability of ambiguities, unconsidered corner cases etc. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 17 / 30
  19. 19. Example begin string s; s = "abc"; string t; t = s + "def"; end Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 18 / 30
  20. 20. Grammar P → begin CL end program CL → C ; CL sequential composition C→T I declaration I =E assignment T → string types int E→I identifier N numeral S string literal E1 + E2 sum of two subexpressions Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 19 / 30
  21. 21. Key Components of a Type System Specification Judgements. Type rules. Environment. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 20 / 30
  22. 22. Type System Empty Environment ∅ ◦ Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 21 / 30
  23. 23. Type System (II) Variable Creation I : string ∈ Γ I : int ∈ Γ Γ ◦ Γ ∪ {I : string } ◦ Γ ∪ {I : int} ◦ Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 22 / 30
  24. 24. Type System (III) Literals Γ ◦ Γ ◦ Γ S : string Γ N : int Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 23 / 30
  25. 25. Type System (IV) Variables in Environment I : string ∈ Γ I : int ∈ Γ Γ I : string Γ I : int Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 24 / 30
  26. 26. Type System (V) Sum Γ E : string Γ F : string Γ E : int Γ F : int Γ E + F : string Γ E + F : int Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 25 / 30
  27. 27. Type System (VI) Assignment Γ I : string Γ E : string Γ I : int Γ E : int Γ I =E : Γ I =E : Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 26 / 30
  28. 28. Type System (VII) Sequential Composition Γ ◦ Γ : Γ ∪ {I : string } ◦ Γ ∪ {I : string } C: Γ string I ; C : Γ ∪ {I : int} ◦ Γ ∪ {I : int} C: Γ int I ; C : Γ C: Γ CL : Γ C ; CL : Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 27 / 30
  29. 29. Type System (VIII) Program ∅ CL : begin CL end : Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 28 / 30
  30. 30. Other Uses of Type Information Semantics. Optimization. Static analysis. ... Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 29 / 30
  31. 31. See Luca Cardelli. Type Systems. ACM Computing Surveys 28, 1 (March 1996), 263-264. http://doi.acm.org/10.1145/234313.234418 Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 30 / 30

×