Docsity

Types and Type Checking: Understanding Program Constructs and Operations, Thesis of Computer Science

Partial preview of the text

Download Types and Type Checking: Understanding Program Constructs and Operations and more Thesis Computer Science in PDF only on Docsity!

Lecture 10. Types and Type Checking

Wei Le

Type

I (^) Type is a property of program constructs such as expressions I (^) It defines a set of values (range of variables) and a set of operations on those values I (^) Classes are one instantiation of the modern notion of the type I (^) fields and methods of a Java class are meant to correspond to values and operations

Why do we need type systems

Consider the assembly language fragment

addi $r1, $r2, $r

What are the types of $r1, $r2, $r3?

Why do we need type systems

Consider the assembly language fragment

addi $r1, $r2, $r

What are the types of $r1, $r2, $r3?

I (^) Assembly language is untyped (MIPS assembly)

Why do we need type systems

I (^) It doesnt make sense to add a function pointer and an integer in C I (^) It does make sense to add two integers I (^) But both have the same assembly language implementation!

Use of Types

I (^) Detect errors: I (^) Memory errors, such as attempting to use an integer as a pointer. I (^) Violations of abstraction boundaries, such as using a private field from outside a class. I (^) Help compilation: I (^) When Python sees x+y, its type systems tells it almost nothing about types of x and y, so code must be general. I (^) In C, C++, Java, code sequences for x+y are smaller and faster, because representations are known.

Inference Rule

From English to an Inference Rule

I (^) Rules of inference are a compact notation of if-then statements I (^) Symbol ∧ is ”and” I (^) Symbol ⇒ is ”if-then” I (^) x : T is ”x has type T”

From English to an Inference Rule

From English to an Inference Rule

Two Rules

Example

Static and Dynamic Types

Relations of Static and Dynamic Types in Simple Type

Systems