lecture10

lecture10 - Lecture Outline Type systems and their...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Prof. Aiken CS 143 Lecture 10 1 Type Checking in COOL (II) Lecture 10 Prof. Aiken CS 143 Lecture 10 2 Lecture Outline • Type systems and their expressiveness • Type checking with SELF_TYPE in COOL • Error recovery in semantic analysis Prof. Aiken CS 143 Lecture 10 3 Expressiveness of Static Type Systems • Static type systems detect common errors • But some correct programs are disallowed – Some argue for dynamic type checking instead – Others argue for more expressive static type checking • But more expressive type systems are more complex Prof. Aiken CS 143 Lecture 10 4 Dynamic And Static Types • The dynamic type of an object is the class C that is used in the “ new C ” expression that created it – A run-time notion – Even languages that are not statically typed have the notion of dynamic type • The static type of an expression captures all dynamic types the expression could have – A compile-time notion Prof. Aiken CS 143 Lecture 10 5 Dynamic and Static Types. (Cont.) • In early type systems the set of static types correspond directly with the dynamic types • Soundness theorem: for all expressions E dynamic_type(E) = static_type(E) (in all executions, E evaluates to values of the type inferred by the compiler) • This gets more complicated in advanced type systems Prof. Aiken CS 143 Lecture 10 6 Dynamic and Static Types in COOL • A variable of static type A can hold values of static type B , if B A class A { … } class B inherits A {…} class Main { x:A new A; x new B; } x has static type A Here, x ’s value has dynamic type A Here, x ’s value has dynamic type B
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Prof. Aiken CS 143 Lecture 10 7 Dynamic and Static Types Soundness theorem for the Cool type system: " E. dynamic_type(E) static_type(E) Why is this Ok? – All operations that can be used on an object of type C can also be used on an object of type C’ C • Such as fetching the value of an attribute • Or invoking a method on the object – Subclasses only add attributes or methods – Methods can be redefined but with same type ! Prof. Aiken CS 143 Lecture 10 8 An Example class Count { i : int 0; inc () : Count { { i i + 1; self; } }; }; • Class Count incorporates a counter • The inc method works for any subclass • But there is disaster lurking in the type system Prof. Aiken CS 143 Lecture 10 9 An Example (Cont.) • Consider a subclass Stock of Count class Stock inherits Count { name : String; -- name of item }; class Main { Stock a (new Stock).inc (); a.name … }; • And the following use of Stock : Type checking error ! Prof. Aiken CS 143 Lecture 10 10 What Went Wrong? (new Stock).inc() has dynamic type Stock • So it is legitimate to write Stock a (new Stock).inc () • But this is not well-typed – (new Stock).inc() has static type Count • The type checker “loses” type information – This makes inheriting inc useless – So, we must redefine inc
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

lecture10 - Lecture Outline Type systems and their...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online