150 likes | 288 Views
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 ”
E N D
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” • Formal type system rules: Post system
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
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
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
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
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)
Simple Language Example: Checking Statements (cont’d) e : boolean s : if e then s : S ifEthenS1 { S.type := ifE.type = booleanthenS1.typeelsetype_error }
Simple Language Example: Statements (cont’d) e: boolean s : while e do s : S whileEdoS1 { S.type := ifE.type = booleanthenS1.typeelsetype_error }
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 }
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) }…
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 }
Simple Language Example: Checking Expressions (cont’d) e1 : boolean e2 : boolean e1ande2 : boolean E E1andE2{ E.type := ifE1.type = booleanandE2.type = booleanthenbooleanelsetype_error }
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 }
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