Docsity

Type Checking in Cool: Lecture 16 - Prof. Sean P. Peisert, Study Guides, Projects, Research of Computer Science

Partial preview of the text

Download Type Checking in Cool: Lecture 16 - Prof. Sean P. Peisert and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

Type Checking in Cool

(continued)

Lecture 16 (no lecture 15) Dr. Sean Peisert – ECS 142 – Spring 2009 1

Status

Midterm and Parser grades this week...

< 3 weeks to go on project 3 2

Type Environments

Let O() be a function from ObjectIdentifiers

to Types, where O(v) = T

The sentence O ⊢ e : T

is read: “Under the assumption that variables have the types given by O, it is provable that the expression e has the type T. 4

Modified Rules

5 i is an integer

O ⊢ i : Int

O ⊢ e 1 : Int O ⊢ e 2 : Int O ⊢ e1 + e 2 : Int

[Int]

[Add]

  • The type environment is added to the earlier rules:

So far...

7 i is an integer

O ⊢ i : Int

⊢ e 1 : Int ⊢ e 2 : Int ⊢ e1 + e 2 : Int

[Int]

[Add]

We have type rules to specify the type checking phase

Now

8

More (complicated) typing rules

Connections between typing rules and

safety of execution

Let

O[T 0 /x] means O modified to return T 0 on

argument x (e.g., in the Let base expr) and

behave as O on all other arguments:

10

O[T 0 /x] ⊢ e 1 : T 1

O ⊢ let x : T 0 in e 1 : T 1

[Let-No-Init]

OT 0 /x = T 0

OT 0 /x = O(y)

Let. Example.

Consider the cool expression: let x : T 0 in (let y : T 1 in Ex,y) + (let x : T 2 in Fx,y)

(where Ex,y and Fx,y are some Cool expression that contains occurrences of “x” and “y”)

Scope

of “y” is Ex,y

of outer “x” is Ex,y

of inner “x” is Fx,y

This is captured precisely in the typing rule 11

Typing Example Diagram 13

Let with Initialization

14

O ⊢ e 0 : T 0

O[T 0 /x] ⊢ e 1 : T 1

O ⊢ let x : T 0 ← e 0 in e 1 : T 1

[Let-Init]

This rule is weak. Why?

Subtyping 16

  • Define a relation X ≤ Y on classes to say that

An object of type X could be used when one of type Y is acceptable, or equivalently:

X conforms with Y

In Cool this means that X is a subtype of Y

Define a relation ≤ on classes

X ≤ X

X ≤ Y if X inherits from Y

X ≤ Z if X ≤ Y and Y ≤ Z

Let with Initialization

17

O ⊢ e 0 : T

T ≤ T 0

O[T 0 /x] ⊢ e 1 : T 1

O ⊢ let x : T 0 ← e 0 in e 1 : T 1

[Let-Init]

Both rules for let are correct

But more programs type check with the latter

Expressiveness of Static Type Systems 19

  • A static type system enables a compiler to detect many common programming errors
  • The cost is that 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 also more complex.

Dynamic and Static Types 20

  • The dynamic type of an object is the class C that is used in the “new C” expression that creates the object.

A run-time notion

Even languages that are not statically typed have the notion of dynamic type

  • The static type of an expression is a notation that captures all possible dynamic types the expression could take

A compile-time notion.