lecture10
lecture10
CS143
Lecture 10
2
Expressiveness of Static Type Systems
3
Dynamic and Static Types
4
Dynamic and Static Types. (Cont.)
5
Dynamic and Static Types in COOL
class A { … }
class B inherits A {…}
class Main { Here, x’s value has
x has static x:A ← new A; dynamic type A
type A … Here, x’s value has
x ← new B; dynamic type B
…
}
7
An Example
8
An Example (Cont.)
class Main {
Stock a ← (new Stock).inc (); Type checking error !
… a.name …
};
9
What Went Wrong?
• So it is legitimate to write
Stock a ← (new Stock).inc ()
10
SELF_TYPE to the Rescue
• Insight:
– inc returns “self”
– Therefore the return value has same type as “self”
– Which could be Count or any subtype of Count!
11
SELF_TYPE to the Rescue (Cont.)
12
Notes About SELF_TYPE
13
SELF_TYPE and Dynamic Types (Example)
14
SELF_TYPE and Dynamic Types (Example)
15
Type Checking
16
Operations on SELF_TYPE
17
Extending ≤
1. SELF_TYPEC ≤ SELF_TYPEC
• In Cool we never need to compare SELF_TYPEs coming from
different classes
2. SELF_TYPEC ≤ T1 if C ≤ T1
• SELF_TYPEC can be any subtype of C
• This includes C itself
• Thus this is the most flexible rule we can allow
18
Extending ≤ (Cont.)
19
Extending lub(T,T’)
20
Where Can SELF_TYPE Appear in COOL?
21
Where Can SELF_TYPE Appear in COOL?
3. let x : T in E
• T can be SELF_TYPE
• x has a type ≤ SELF_TYPEC
4. new T
• T can be SELF_TYPE
• Creates an object of the same type as self
5. m@T(E1,…,En)
• T cannot be SELF_TYPE
22
Where Can SELF_TYPE Not Appear in COOL?
6. m(x : T) : T’ { … }
• Only T’ can be SELF_TYPE !
24
Type Checking Rules
• The next step is to design type rules using SELF_TYPE for each
language construct
• Most of the rules remain the same except that ≤ and lub are the new
ones
• Example:
O(Id) = T0
O,M,C ⊢ e1 : T0
T1 ≤ T0
O,M,C ⊢ Id ← e1 : T1
25
What is Different?
O,M,C ⊢ e0 : T0
⠇
O,M,C ⊢ en : Tn
M(T0, f) = (T'1,…,T'n,T'n+1)
T'n+1 ≠ SELF_TYPE
Ti ≤ T'i 1≤i≤n
O,M,C ⊢ e0.f(e1,…,en) : T'n+1
26
What is Different?
O,M,C ⊢ e0 : T0
⠇
O,M,C ⊢ en : Tn
M(T0, f) = (T'1,…,T'n, SELF_TYPE)
Ti ≤ T'i 1≤i≤n
O,M,C ⊢ e0.f(e1,…,en) : T0
27
What is Different?
28
Static Dispatch
O,M,C ⊢ e0 : T0
⠇
O,M,C ⊢ en : Tn
T0 ≤ T
M(T, f) = (T1’,…,Tn’,Tn+1’)
Tn+1’ ≠ SELF_TYPE
Ti ≤ Ti ’ 1≤i≤n
O,M,C ⊢ [email protected](e1,…,en) : Tn+1’
29
Static Dispatch
O,M,C ⊢ e0 : T0
⠇
O,M,C ⊢ en : Tn
T0 ≤ T
M(T, f) = (T1’,…,Tn’,SELF_TYPE)
Ti ≤ Ti ’ 1 ≤ i ≤ n
O,M,C ⊢ [email protected](e1,…,en) : T0
30
Static Dispatch
31
New Rules
32
Summary of SELF_TYPE
33
Why Cover SELF_TYPE ?
34
Error Recovery
• The Problem:
– What type is assigned to an expression with no legitimate type?
– This type will influence the typing of the enclosing expression
35
Error Recovery Attempt
let y : Int ← x + 2 in y + 3
• Assume x is undeclared, then its type is Object
• But now we have Object + Int
• This will generate another typing error
• We then say that that Object + Int = Object
• Then the initializer’s type will not be Int
⇒ a workable solution but with cascading errors
36
Better Error Recovery
• We can introduce a new type called No_type for use with ill-typed
expressions
let y : Int ← x + 2 in y + 3
37
Notes
38