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

Type Systems

  • 2.
    Typing Michal P´se (CTUin Prague) ıˇ Object Programming Lect. 1: Type Systems September 21, 2010 2 / 30
  • 3.
    The Purpose ofTyping ”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 ”GoWrong” 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 ofTyping (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 ofTyping 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 TheyBoth 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 TheyBoth 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 ofTyping 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 ofType 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 → beginCL 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 ofa 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) VariableCreation 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) Variablesin 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) SequentialComposition Γ ◦ Γ : Γ ∪ {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 ofType 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. TypeSystems. 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