SlideShare a Scribd company logo
Typing




Michal P´se (CTU in Prague)
        ıˇ                    Object Programming Lect. 1: Type Systems   September 21, 2010   2 / 30
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
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
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
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
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
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
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
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
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
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
Type Algebra




Michal P´se (CTU in Prague)
        ıˇ                     Object Programming Lect. 1: Type Systems   September 21, 2010   11 / 30
Function Type




                                             A→B




 Michal P´se (CTU in Prague)
         ıˇ                    Object Programming Lect. 1: Type Systems   September 21, 2010   12 / 30
Product Type




                                              A×B




 Michal P´se (CTU in Prague)
         ıˇ                    Object Programming Lect. 1: Type Systems   September 21, 2010   13 / 30
Intersection Type




                                              A∩B




 Michal P´se (CTU in Prague)
         ıˇ                    Object Programming Lect. 1: Type Systems   September 21, 2010   14 / 30
Union Type




                                              A∪B




 Michal P´se (CTU in Prague)
         ıˇ                    Object Programming Lect. 1: Type Systems   September 21, 2010   15 / 30
Type System




Michal P´se (CTU in Prague)
        ıˇ                     Object Programming Lect. 1: Type Systems   September 21, 2010   16 / 30
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
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
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
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
Type System




Empty Environment

                                              ∅      ◦




 Michal P´se (CTU in Prague)
         ıˇ                    Object Programming Lect. 1: Type Systems   September 21, 2010   21 / 30
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
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
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
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
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
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
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
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
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

More Related Content

Viewers also liked

Introduction to Programming Languages
Introduction to Programming LanguagesIntroduction to Programming Languages
Introduction to Programming Languages
educationfront
 
Computer Languages....ppt
Computer Languages....pptComputer Languages....ppt
Computer Languages....ppthashgeneration
 
Computer Languages.
Computer Languages.Computer Languages.
Computer Languages.
Aditya Sheoran
 
Quantum programming
Quantum programmingQuantum programming
Quantum programming
Francisco J. Gálvez Ramírez
 
Computer languages
Computer languagesComputer languages
Computer languages
Buxoo Abdullah
 
Lect 1. introduction to programming languages
Lect 1. introduction to programming languagesLect 1. introduction to programming languages
Lect 1. introduction to programming languagesVarun Garg
 

Viewers also liked (7)

Introduction to Programming Languages
Introduction to Programming LanguagesIntroduction to Programming Languages
Introduction to Programming Languages
 
Computer languages 11
Computer languages 11Computer languages 11
Computer languages 11
 
Computer Languages....ppt
Computer Languages....pptComputer Languages....ppt
Computer Languages....ppt
 
Computer Languages.
Computer Languages.Computer Languages.
Computer Languages.
 
Quantum programming
Quantum programmingQuantum programming
Quantum programming
 
Computer languages
Computer languagesComputer languages
Computer languages
 
Lect 1. introduction to programming languages
Lect 1. introduction to programming languagesLect 1. introduction to programming languages
Lect 1. introduction to programming languages
 

Similar to Type Systems

Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple InheritanceMichal Píše
 
Image understanding and artificial intelligence
Image understanding and artificial intelligenceImage understanding and artificial intelligence
Image understanding and artificial intelligence
I MT
 

Similar to Type Systems (8)

Subtyping
SubtypingSubtyping
Subtyping
 
Prototype Languages
Prototype LanguagesPrototype Languages
Prototype Languages
 
Multiple Dispatch
Multiple DispatchMultiple Dispatch
Multiple Dispatch
 
Functional Concepts
Functional ConceptsFunctional Concepts
Functional Concepts
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple Inheritance
 
Reclassification
ReclassificationReclassification
Reclassification
 
Inheritance
InheritanceInheritance
Inheritance
 
Image understanding and artificial intelligence
Image understanding and artificial intelligenceImage understanding and artificial intelligence
Image understanding and artificial intelligence
 

Recently uploaded

Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Ashish Kohli
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
Assignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docxAssignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docx
ArianaBusciglio
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
What is the purpose of studying mathematics.pptx
What is the purpose of studying mathematics.pptxWhat is the purpose of studying mathematics.pptx
What is the purpose of studying mathematics.pptx
christianmathematics
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 

Recently uploaded (20)

Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
Assignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docxAssignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docx
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
What is the purpose of studying mathematics.pptx
What is the purpose of studying mathematics.pptxWhat is the purpose of studying mathematics.pptx
What is the purpose of studying mathematics.pptx
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 

Type Systems

  • 1.
  • 2. Typing Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 2 / 30
  • 3. 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
  • 4. 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
  • 5. 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
  • 6. 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
  • 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. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 7 / 30
  • 8. 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
  • 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(); } Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 8 / 30
  • 10. 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
  • 11. 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
  • 12. 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
  • 13. Type Algebra Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 11 / 30
  • 14. Function Type A→B Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 12 / 30
  • 15. Product Type A×B Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 13 / 30
  • 16. Intersection Type A∩B Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 14 / 30
  • 17. Union Type A∪B Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 15 / 30
  • 18. Type System Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 16 / 30
  • 19. 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
  • 20. 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
  • 21. 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
  • 22. 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
  • 23. Type System Empty Environment ∅ ◦ Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 21 / 30
  • 24. 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
  • 25. 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
  • 26. 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
  • 27. 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
  • 28. 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
  • 29. 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
  • 30. 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
  • 31. 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
  • 32. 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