1 / 15

Lecture # 20

Lecture # 20. Type Systems. Type Systems. A type system defines a set of types and rules to assign types to programming language constructs Informal type system rules, for example “ if both operands of addition are of type integer, then the result is of type integer ”

jerome
Download Presentation

Lecture # 20

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture # 20 Type Systems

  2. Type Systems • A type system defines a set of types and rules to assign types to programming language constructs • Informal type system rules, for example “if both operands of addition are of type integer, then the result is of type integer” • Formal type system rules: Post system

  3. Type Rules in Post System Notation Type judgmentse : where e is an expression and  is a type Environment maps objects v to types :(v) =  (v) =   v :   (v) =   e :   v := e : void   e1 : integer e2 : integer     e1+e2 : integer  

  4. Type System Example Environment  is a set of name, type pairs, for example:  = { x,integer, y,integer, z,char, 1,integer, 2,integer } From  and rules we can check types:type checking = theorem proving The proof that x := y + 2 is typed correctly: (y) = integer (2) = integer y : integer 2 : integer     y + 2 : integer (x) = integer   x := y + 2 : void  

  5. A Simple Language Example E true  false  literal  num  id  EandE  E+E  E[E]  E^ P D;SD D;D  id : TT boolean  char  integer  array[ num ] ofT  ^TS id :=E  ifEthenS  whileEdoS  S;S Pointer to T Pascal-like pointer dereference operator

  6. Simple Language Example: Declarations D id : T { addtype(id.entry, T.type) }T boolean { T.type := boolean }T char { T.type := char }T integer { T.type := integer }T array[ num ] ofT1 { T.type := array(1..num.val, T1.type) }T ^T1 { T.type := pointer(T1) Parametric types:type constructor

  7. Simple Language Example: Checking Statements (v) =   e :   v := e : void   S id :=E { S.type := ifid.type = E.type thenvoidelsetype_error } Note: the type of id is determined by scope’s environment:id.type = lookup(id.entry)

  8. Simple Language Example: Checking Statements (cont’d) e : boolean  s :     if e then s :   S ifEthenS1 { S.type := ifE.type = booleanthenS1.typeelsetype_error }

  9. Simple Language Example: Statements (cont’d)  e: boolean  s :     while e do s :   S whileEdoS1 { S.type := ifE.type = booleanthenS1.typeelsetype_error }

  10. Simple Language Example: Checking Statements (cont’d)  s1 : void  s2 : void   s1; s2: void   S S1;S2 { S.type := ifS1.type = void and S2.type = void then void elsetype_error }

  11. Simple Language Example: Checking Expressions (v) =   v :   E true { E.type = boolean }E false { E.type = boolean }E literal { E.type = char }E num { E.type = integer } E id { E.type = lookup(id.entry) }…

  12. Simple Language Example: Checking Expressions (cont’d) e1 : integer e2 : integer      e1+e2 : integer  E E1+E2 { E.type := ifE1.type = integerandE2.type = integerthenintegerelsetype_error }

  13. Simple Language Example: Checking Expressions (cont’d) e1 : boolean e2 : boolean     e1ande2 : boolean   E E1andE2{ E.type := ifE1.type = booleanandE2.type = booleanthenbooleanelsetype_error }

  14. Simple Language Example: Checking Expressions (cont’d) e2 : integer  e1 : array(s, )     e1[e2] :   E E1[E2] { E.type := ifE1.type = array(s, t) andE2.type = integerthentelsetype_error }

  15. Type Conversion and Coercion • Type conversion is explicit, for example using type casts • Type coercion is implicitly performed by the compiler to generate code that converts types of values at runtime (typically to narrow or widen a type) • Both require a type system to check and infer types from (sub)expressions

More Related