Docsity

Typing Rules, Dispatch Rules and Self Type - Lecture Slides | CS 4610, Study notes of Programming Languages

Partial preview of the text

Download Typing Rules, Dispatch Rules and Self Type - Lecture Slides | CS 4610 and more Study notes Programming Languages in PDF only on Docsity!

More More

Static Static

Semantics Semantics

One-Slide Summary

  • (^) Typing rules formalize the semantics checks necessary to validate a program. Well-typed programs do not go wrong.
  • (^) Subtyping relations (·) and least-upper-bounds (lub) are powerful tools for type-checking dynamic dispatch.
  • We will use^ SELF_TYPEC for “C or any subtype of C”. It will show off the subtlety of type systems and allow us to check methods that return self objects.

Assignment

What is this thing? What’s `? O? ·? [Assign]

O ` id à e

1

: T

1

O(id) = T

0

O ` e

1

: T

1

T

1

· T

0

Initialized Attributes

  • Let^ O C (x) = T for all attributes x:T in class C
  • O C represents the class-wide scope
  • (^) we “preload” the environment O with all attributes
  • (^) Attribute initialization is similar to let, except for the scope of names [Attr-Init]

O

C

` id : T

0

à e

1

O

C

(id) = T

0

O

C

` e

1

: T

1

T

1

· T

0

If-Then-Else example

  • (^) Consider the class hierarchy
  • (^) … and the expression if … then new A else new B fi
  • (^) Its type should allow for the dynamic type to be both A or B - (^) Smallest supertype is P

P

A B

Least Upper Bounds

  • (^) Define: lub(X,Y) to be the least upper bound of X and Y. lub(X,Y) is Z if - (^) X · Z Æ Y · Z Z is an upper bound - (^) X · Z’ Æ Y · Z’ ⇒ Z · Z’ Z is least among upper bounds
  • (^) In Cool, the least upper bound of two types is their least common ancestor in the inheritance tree

Case

  • (^) The rule for case expressions takes a lub over all branches [Case] O **case e 0 of x 1 :T 1** ) **e 1 ; …; x n : T n** ) **e n ; esac : lub(T 1 ’,…,T n ’) O** e 0 : T 0 O[T 1 /x 1 ] **e 1 : T 1 ’ … O[T n /x n ]** e n : T n ’

Method Dispatch

  • (^) There is a problem with type checking method calls:
  • (^) We need information about the formal parameters and return type of f [Dispatch] O **e 0 .f(e 1 ,…,e n ) :? O** e 0 : T 0 O **e 1 : T 1 … O** e n : T n

An Extended Typing Judgment

  • (^) Now we have two environments: O and M
  • (^) The form of the typing judgment is O, M ` e : T read as: “with the assumption that the object identifiers have types as given by O and the method identifiers have signatures as given by M, the expression e has type T”

The Method Environment

  • (^) The method environment must be added to all rules
  • (^) In most cases, M is passed down but not actually used - (^) Example of a rule that does not use M: - (^) Only the dispatch rules uses M [Add] O, M ` e 1
  • e 2 : Int O, M ` e 1

: T

1 O, M ` e 2

: T

2

Static Dispatch

  • (^) Static dispatch is a variation on normal dispatch
  • (^) The method is found in the class explicitly named by the programmer (not via e 0
  • (^) The inferred type of the dispatch expression must conform to the specified type

Static Dispatch (Cont.)

[StaticDispatch] O, M ` e 0 @T.f(e 1 ,…,e n

) : T

n+

O, M ` e 0

: T

0 O, M ` e 1

: T

1 … O, M ` e n

: T

n T 0

· T

M(T, f) = (T 1

’,…,T

n

’, T

n+

T

i

· T

i ’ (for 1 · i · n)

Flexibility vs. Soundness

  • (^) Recall that type systems have two conflicting goals: - (^) Give flexibility to the programmer - (^) Prevent valid programs from “going wrong” - (^) Milner, 1981: “Well-typed programs do not go wrong”
  • (^) An active line of research is in the area of inventing more flexible type systems while preserving soundness

Dynamic And Static Types

  • (^) The dynamic type of an object is?
  • (^) The static type of an expression is?
  • (^) You tell me!