Docsity

CS 440 Lecture Notes: Type Checking - Prof. Alan Mattox Beckman, Study notes of Programming Languages

Toggle sidebar
bg1
CS 440 — Type Checking
Mattox Beckman
March 27, 2009
1 Introduction and Objectives
In order to express the meaning of a program, we need a formal language to capture these meanings.
One way to express meaning is to say something about the types of the expressions. By the end of
lecture, you should know
•how to structure a proof-tree
•how to use the type rules to prove the type of an expression
•write your own type rule for an expression
1.1 Why Type Checking is Good
Strongly Typed Languages
•e.g., ML, Haskell, most of C++, C
•You have to declare all your types at the beginning
•No type checks during run-time (faster, safer code)
Weakly Typed Languages
•e.g., Perl, Lisp, some parts of Java
•Very flexible programming
•Types must be checked at run-time or Bad Things happen
Advantages of Strongly Typed Languages
Engineering
•A program that type-checks is likely to be correct.
•Types constrain the use of a function.
Safety •Ever try casting an integer into a pointer in C?
1int i = 5;
2*((int *)i) = 10;
1
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download CS 440 Lecture Notes: Type Checking - Prof. Alan Mattox Beckman and more Study notes Programming Languages in PDF only on Docsity!

CS 440 — Type Checking

Mattox Beckman

March 27, 2009

1 Introduction and Objectives

In order to express the meaning of a program, we need a formal language to capture these meanings. One way to express meaning is to say something about the types of the expressions. By the end of lecture, you should know

  • how to structure a proof-tree
  • how to use the type rules to prove the type of an expression
  • write your own type rule for an expression

1.1 Why Type Checking is Good

Strongly Typed Languages

  • e.g., ML, Haskell, most of C++, C
  • You have to declare all your types at the beginning
  • No type checks during run-time (faster, safer code)

Weakly Typed Languages

  • e.g., Perl, Lisp, some parts of Java
  • Very flexible programming
  • Types must be checked at run-time or Bad Things happen

Advantages of Strongly Typed Languages

Engineering

  • A program that type-checks is likely to be correct.
  • Types constrain the use of a function.

Safety • Ever try casting an integer into a pointer in C?

1 int i = 5; 2 *((int *)i) = 10;

  • A type error in an untyped language can be very hard to detect.

Theory

  • It is much easier to verify the operation of type-correct code.

2 Formal Systems

2.1 Parts of a Formal System

To create a formal system, you must specify the following:

  • A set of symbols or an alphabet.
  • A definition of a valid sentence.
  • A set of transformation rules to make new valid sentences out of old ones.
  • A set of initial valid sentences.

You do NOT need:

  • An interpretation of those symbols. They are highly recommended, but the formal system can exist and do its work without one.

Example

Symbols S, (, ), Z, P , x, y.

Definition of a furbitz

  • Z is a furbitz. x and y are variables of type furbitz.
  • if x is a furbitz, then S(x) is a furbitz.
  • if x and y are furbitzi, then P (x, y) is a furbitz.

Definition of the gloppit relation

  • Z has the gloppit relation with Z.
  • If x and y have the gloppit relation, then S(x) and S(y) have the gloppit relation.
  • If α and β, then we can write αgβ.

True Sentences If αgβ, then also

  • P (S(α), β)gP (α, S(β)), and P (Z, α)gα

Does the Furbitz look familiar?

4 Typing Rules

4.1 Axioms

Constants ` n : int (assuming n is an int)

` true : bool

` false : bool

Variables Γ ` x : τ , if x : τ ∈ Γ

  • The Variable Rule is actually a bit more complicated in real life, but this form is sufficient.
  • These are rules that are true no matter what the context is.

4.2 Simple Rules

Arithmetic

Γ e 1 : int Γ e 2 : int Γ ` e 1 ⊕ e 2 : int

Relations

Γ e 1 : int Γ e 2 : int Γ ` e 1 ∼ e 2 : bool

Booleans

Γ e 1 : bool Γ e 2 : bool Γ ` e 1 && e 2 : bool

Γ e 1 : bool Γ e 2 : bool Γ ` e 1 || e 2 : bool

  • These are combination rules.

4.3 Simple Example

Suppose we want to prove that Γ ` (x ∗ 5 > 7)&& y : bool. Assume that Γ = [x : int ; y : bool ]

First thing: Write down the thing you are trying to prove, and put a bar over it.

Γ ` (x ∗ 5 > 7)&& y : bool

Look at the outermost expression. What kind of expression is this?

Use the rule

Γ e 1 : bool Γ e 2 : bool Γ ` e 1 && e 2 : bool

You need the “and” rule. It will tell you which parts of the goal need to be proved next.

4.4 Simple Example

Suppose we want to prove that Γ ` (x ∗ 5 > 7)&& y : bool. Assume that Γ = [x : int ; y : bool ]

Write parts on top and put a bar over them as well.

Γ x ∗ 5 > 7 : bool Γ y : bool Γ ` (x ∗ 5 > 7)&& y : bool

What to do next? Let’s work left to right. The expression we want next is a “greater” expression. (Besides, the y expression is already an axiom.)

4.5 Simple Example

Suppose we want to prove that Γ ` (x ∗ 5 > 7)&& y : bool. Assume that Γ = [x : int ; y : bool ]

Following the “greater” rule, we break the x * 5 > 7 into two parts.

Γ x ∗ 5 : int Γ 7 : int Γ x ∗ 5 > 7 : bool Γ y : bool Γ ` (x ∗ 5 > 7)&& y : bool

We will turn our attention to the multiplication now.

4.6 Simple Example

Suppose we want to prove that Γ ` (x ∗ 5 > 7)&& y : bool. Assume that Γ = [x : int ; y : bool ]

At this point, there are no more subtrees to expand out. We are done.

Γ x : int Γ 5 : int Γ x ∗ 5 : int Γ 7 : int Γ x ∗ 5 > 7 : bool Γ y : bool Γ ` (x ∗ 5 > 7)&& y : bool

4.7 Type Variables in Rules

If

Γ e 1 : bool Γ e 2 : τ Γ e 3 : τ Γ if e 1 then e 2 else e 3 : τ

Γ′^ = Γ ∪ {f : (α → β); g : (δ → α); x : δ} f (g x) : β Γ fun f g x −> f (g x) : (α → β) → (δ → α) → δ → β After using the function rule: Γ ∪ [x 1 : τ 1 ; · · · ; xn : τn] e : τ Γ fun x 1 · · · xn−>e : τ 1 → · · · → τn → τ Next, we use application. Γ e : τ 1 → τ 2 → · · · → τn → τ Γ e 1 : τ 1 · · · Γ en : τn Γ e e 1 e 2 · · · en : τ This give us...

Γ′^ f : (α → β) Γ′^ (g x) : (α) Γ′^ = Γ ∪ {f : (α → β); g : (δ → α); x : δ} f (g x) : β Γ fun f g x −> f (g x) : (α → β) → (δ → α) → δ → β The rule for parentheses is trivial... can you write it? Next, we’ll use application again.

Γ′^ ` f : (α → β)

Γ′^ g : (δ → α) Γ′^ x : δ Γ′^ (g x) : α Γ′^ = Γ ∪ {f : (α → β); g : (δ → α); x : δ} f (g x) : β Γ ` fun f g x −> f (g x) : (α → β) → (δ → α) → δ → β We are done. These rules are meant mainly to verify a type, but they can be used to infer a type as well.

4.11 Example: Type-inferencing Compose

At first, give everything a separate type.

Γ ` fun f g x −> f (g x) : α → β → δ → γ

Γ′^ = Γ ∪ {f : α; g : β; x : δ} f (g x) : γ Γ fun f g x −> f (g x) : α → β → δ → γ

Γ′^ f : α Γ′^ (g x) :??? Γ′^ = Γ ∪ {f : α; g : β; x : δ} f (g x) : γ Γ fun f g x −> f (g x) : α → β → δ → γ From here we see that α needs to be a function type. Also, we need to decide a type for (g x).

  • Let (g x) have type ν.
  • Let f have type α = ν → γ.

We make the appropriate substitutions....

Γ′^ f : (ν → γ) Γ′^ (g x) : ν Γ′^ = Γ ∪ {f : (ν → γ); g : β; x : δ} f (g x) : γ Γ fun f g x −> f (g x) : (ν → γ) → β → δ → γ

Now we use the application rule for (g x).

Γ′^ ` f : (ν → γ)

Γ′^ g : β Γ′^ x : δ Γ′^ (g x) : ν Γ′^ = Γ ∪ {f : (ν → γ); g : β; x : δ} f (g x) : γ Γ ` fun f g x −> f (g x) : (ν → γ) → β → δ → γ

  • We know that β needs to be δ → ν, because of our rule.

Substituting, we get...

Γ′^ ` f : (ν → γ)

Γ′^ g : (δ → ν) Γ′^ x : δ Γ′^ (g x) : ν Γ′^ = Γ ∪ {f : (ν → γ); g : (δ → ν); x : δ} f (g x) : γ Γ ` fun f g x −> f (g x) : (ν → γ) → (δ → ν) → δ → γ

Now we’re done.

4.12 Another example

Here’s an example I showed one time I gave this lecture.

Γ ∪ [x : τ ] x : τ Γ fun x −>x : τ → τ

Γ^1 ` id : τ → τ

Γ^1 id : τ → τ Γ^1 10 : int Γ^1 (id 10) : int Γ^1 id (id 10) : int Γ ` let id = fun x −>x in id (id 10) : int

  • Let Γ^1 = Γ ∪ [id : τ → τ ]
  • What would be different if we’d used let rec instead?