Download Type Systems and Type Checking: Understanding Static and Dynamic Types - Prof. Elsa Gunter and more Study notes Computer Science in PDF only on Docsity! Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up CS421 Lecture 6: Type Derivations1 Mark Hills mhills@cs.uiuc.edu University of Illinois at Urbana-Champaign June 12, 2006 1Based on slides by Mattox Beckman, as updated by Vikram Adve, Gul Agha, and Elsa Gunter Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up Introduction: Why Have Data Types? Data types play a key role in: ◮ the design of programs (through data abstraction); ◮ the analysis of programs (through type checking and type inference); ◮ the translation and execution of programs (through compile-time code generation). Underlying these uses are type systems that provide one way to express the meaning of a program. Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up Objectives By the end of lecture, you should know ◮ the terminology of types and type checking ◮ about type rules and how to structure a proof-tree ◮ how to use the type rules to check the type of an expression ◮ how to use the type rules to infer the type of an expression ◮ how to write your own type rule for an expression Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up Type A type t defines a set of possible data values ◮ E.g., short in C ≡ {i : −215 ≤ i ≤ 215 − 1} ◮ A value in this set is said to be of type t. Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up Type System The rules of a language that describe how to assign a type to each expression in a program are the type system. Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up Sound Type Systems A type system is sound if the type t(e) it assigns to expression e is always correct, i.e., whenever e is assigned type t and evaluates to value v , the value v is in the set of values defined for type t ◮ SML, OCAML, and Ada have sound type systems. ◮ Most implementations of C and C++ do not. Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up Type Errors An operation that is applied to an operand of an illegal type is a type error. When the operation may be applied to an operand of an illegal type, this is a potential type error. ◮ 1 +. 2.5 in OCAML ◮ atoi(); in C ◮ MyCls cref = (MyCls) X; in Java This last may or may not be a type error. Such an error must be detected at run time. Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up Strongly Typed Language A language is strongly typed when no application of an operator to its arguments can lead to a run-time type error. This depends strongly on the definitions of “type” and “type system” for the language. A sound type system is required, and expensive checks may be needed. ◮ C++ claimed to be “strongly typed”, but ◮ Union types allow creating a value at one type and using it at another ◮ Type coercions may cause unexpected (undesirable) effects ◮ No array bounds check (in fact, no runtime checks at all) ◮ SML, OCAML “strongly typed” but still must do dynamic array bounds checks, runtime type case analysis, and other checks Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up Weakly Typed Languages A language is weakly typed when applications of operators to operands can lead to run-time type errors or, worse, undetectable errors that do not cause the program to halt. ◮ E.g., Perl, C, C++, Fortran90, assembly languages ◮ Advantages: more flexible programming; faster code Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up Statically Typed Languages A static type is a type assigned to an expression at compile-time. A statically typed language is one where every expression is assigned a static type. ◮ Weakly Statically Typed: Assigned types are assumed to be correct and not checked. E.g., C, C++, Fortran90 ◮ Strongly Statically Typed: Assigned types are checked at compile-time or run-time E.g., ML, Haskell, Ada, OCAML, Java. Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up Dynamically Typed Language A dynamic type is a type assigned to a storage location at run time. A dynamically typed language is one where the type of an expression may not be determined until run time. ◮ Safe, but may incur significant run-time overheads ◮ Cost: Cannot generate unique code for some operations at compile-time ⇒ Generally used only in interpreted languages Examples: Lisp, Scheme, SmallTalk Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up Type Checking Type checking is a program analysis that attempts to check whether a program can generate type errors. ◮ Type checking assures that operations are applied to the right number of arguments of the right types ◮ Right type may mean same type as was specified, or may mean that there is a predefined implicit coercion that will be applied ◮ Used to resolve overloaded operations Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up Examples of Valid Type Judgments ◮ ⊢ true && false : bool ◮ [x : int ] ⊢ x + 3 : int ◮ [f : int → string ] ⊢ f (5) : string Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Format of Typing Rules Assumptions Γ ⊢ e1 : bool Γ ⊢ e2 : τ Γ ⊢ e3 : τ Γ ⊢ if e1 then e2 else e3 : τ Conclusion ◮ Core Idea: Type of expression determined by type of components ◮ If a rule has no assumptions, then it is called an axiom ◮ Γ may be left out if we don’t need a type environment ◮ Γ, e, and τ are parameterized – they may contain meta-variables used for typing Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Axioms – Constants ⊢ n : int (assuming n is an int) ⊢ true : bool ⊢ false : bool ◮ These are rules that are true no matter what the typing context ◮ n is a meta-variable representing any int Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Axioms – Variables There are many ways you may see this rule formatted. If Γ is a set, it may be shown as: Γ ⊢ x : τ if(x : τ) ∈ Γ It may also be shown as: x : τ ∈ Γ Γ ⊢ x : τ If Γ is treated as a list, order is important, and we will say Γ(x) = τ if x : τ ∈ Γ and no other definition of x is to the left of x : τ . Then, we will have: Γ ⊢ x : τ if Γ(x) = τ We will use the first. Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Simple Rules ◮ Rules for arithmetic operators (+,∗,etc): Γ ⊢ e1 : int Γ ⊢ e2 : int Γ ⊢ e1 ⊕ e2 : int ◮ Rules for relational operators (=,<,etc) are similar: Γ ⊢ e1 : int Γ ⊢ e2 : int Γ ⊢ e1 ∼ e2 : bool Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Simple Rules Booleans look similar, except for not, which has only one operand: Γ ⊢ e1 : bool Γ ⊢ e2 : bool Γ ⊢ e1 && e2 : bool Γ ⊢ e1 : bool Γ ⊢ e2 : bool Γ ⊢ e1 || e2 : bool Γ ⊢ e1 : bool Γ ⊢ ! e1 : bool Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Simple Example Suppose we want to prove that Γ ⊢ y|| (x + 3 > 6) : bool . Assume that Γ = [x : int ; y : bool ] First thing: Write down the thing you are trying to prove, and put a bar over it. Proofs are from the bottom up. ? Γ ⊢ y|| (x + 3 > 6) : bool Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Simple Example Suppose we want to prove that Γ ⊢ y|| (x + 3 > 6) : bool . Assume that Γ = [x : int ; y : bool ] Next, look at the outermost expression. What kind of expression is this? What rule has this as its conclusion? ? Γ ⊢ y|| (x + 3 > 6) : bool Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Simple Example Suppose we want to prove that Γ ⊢ y|| (x + 3 > 6) : bool . Assume that Γ = [x : int ; y : bool ] Next, look at the outermost expression. What kind of expression is this? What rule has this as its conclusion? ? Γ ⊢ y|| (x + 3 > 6) : bool Boolean or: Γ ⊢ e1 : bool Γ ⊢ e2 : bool Γ ⊢ e1|| e2 : bool This will tell us what we need to do next. Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Simple Example Suppose we want to prove that Γ ⊢ y|| (x + 3 > 6) : bool . Assume that Γ = [x : int ; y : bool ] Write parts on top and put a bar over them as well. ? Γ ⊢ y : bool ? Γ ⊢ x + 3 > 6 : bool Γ ⊢ y|| (x + 3 > 6) : bool Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Simple Example Suppose we want to prove that Γ ⊢ y|| (x + 3 > 6) : bool . Assume that Γ = [x : int ; y : bool ] Now, pick an assumption. We can work left to right. Which rule has Γ ⊢ y : bool as a conclusion? ? Γ ⊢ y : bool ? Γ ⊢ x + 3 > 6 : bool Γ ⊢ y|| (x + 3 > 6) : bool Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Simple Example Suppose we want to prove that Γ ⊢ y|| (x + 3 > 6) : bool . Assume that Γ = [x : int ; y : bool ] This is the variable axiom. Now, what rule has Γ ⊢ x + 3 > 6 : bool as a conclusion? Γ ⊢ y : bool ? Γ ⊢ x + 3 > 6 : bool Γ ⊢ y|| (x + 3 > 6) : bool Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Simple Example Suppose we want to prove that Γ ⊢ y|| (x + 3 > 6) : bool . Assume that Γ = [x : int ; y : bool ] This is the rule for the > relational operator. Again, we expand this out. Γ ⊢ y : bool ? Γ ⊢ x + 3 : int ? Γ ⊢ 6 : int Γ ⊢ x + 3 > 6 : bool Γ ⊢ y|| (x + 3 > 6) : bool Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Simple Example Suppose we want to prove that Γ ⊢ y|| (x + 3 > 6) : bool . Assume that Γ = [x : int ; y : bool ] Now, we need to select an assumption again. Which rule has Γ ⊢ 6 : int as its conclusion? Γ ⊢ y : bool ? Γ ⊢ x + 3 : int ? Γ ⊢ 6 : int Γ ⊢ x + 3 > 6 : bool Γ ⊢ y|| (x + 3 > 6) : bool Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Simple Example Suppose we want to prove that Γ ⊢ y|| (x + 3 > 6) : bool . Assume that Γ = [x : int ; y : bool ] This is the axiom for constants. Now, we can select the other assumption. Which rule has Γ ⊢ x + 3 : int as a conclusion? Γ ⊢ y : bool ? Γ ⊢ x + 3 : int Γ ⊢ 6 : int Γ ⊢ x + 3 > 6 : bool Γ ⊢ y|| (x + 3 > 6) : bool Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Simple Example Suppose we want to prove that Γ ⊢ y|| (x + 3 > 6) : bool . Assume that Γ = [x : int ; y : bool ] This is the rule for arithmetic operations. Γ ⊢ y : bool ? Γ ⊢ x : int ? Γ ⊢ 3 : int Γ ⊢ x + 3 : int Γ ⊢ 6 : int Γ ⊢ x + 3 > 6 : bool Γ ⊢ y|| (x + 3 > 6) : bool Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Simple Example Suppose we want to prove that Γ ⊢ y|| (x + 3 > 6) : bool . Assume that Γ = [x : int ; y : bool ] Now, we pick an assumption to prove again. Which rule has Γ ⊢ 3 : int as a conclusion? Γ ⊢ y : bool ? Γ ⊢ x : int ? Γ ⊢ 3 : int Γ ⊢ x + 3 : int Γ ⊢ 6 : int Γ ⊢ x + 3 > 6 : bool Γ ⊢ y|| (x + 3 > 6) : bool Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Simple Example Suppose we want to prove that Γ ⊢ y|| (x + 3 > 6) : bool . Assume that Γ = [x : int ; y : bool ] This is the axiom for constants. Which rule has Γ ⊢ x : int as a conclusion? Γ ⊢ y : bool ? Γ ⊢ x : int Γ ⊢ 3 : int Γ ⊢ x + 3 : int Γ ⊢ 6 : int Γ ⊢ x + 3 > 6 : bool Γ ⊢ y|| (x + 3 > 6) : bool Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Γ ⊢ (let rec one = 1 :: one in let x = 2 in fun y→ (x :: y :: one)) : int → int list Proof for #1: Which rule? ? Γ ∪ [one : int list ] ⊢ (1 :: one) : int list Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Γ ⊢ (let rec one = 1 :: one in let x = 2 in fun y→ (x :: y :: one)) : int → int list Proof for #1: This uses the application rule. #3 #4 Γ ∪ [one : int list ] ⊢ (1 :: one) : int list #3 = Γ∪ [one : int list ] ⊢ ((::)1) : int list → int list #4 = Γ ∪ [one : int list ] ⊢ one : int list Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Γ ⊢ (let rec one = 1 :: one in let x = 2 in fun y→ (x :: y :: one)) : int → int list Proof for #3: What rule applies here? ? Γ ∪ [one : int list ] ⊢ ((::)1) : int list → int list Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Γ ⊢ (let rec one = 1 :: one in let x = 2 in fun y→ (x :: y :: one)) : int → int list Proof for #3: This uses application again. ? (::) : int → int list → int list ? 1 : int Γ ∪ [one : int list ] ⊢ ((::)1) : int list → int list Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Γ ⊢ (let rec one = 1 :: one in let x = 2 in fun y→ (x :: y :: one)) : int → int list Proof for #3: Since (::) is predefined, we can treat this as a constant. 1 is also a constant. Thus, this part is done. (::) : int → int list → int list 1 : int Γ ∪ [one : int list ] ⊢ ((::)1) : int list → int list Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Γ ⊢ (let rec one = 1 :: one in let x = 2 in fun y→ (x :: y :: one)) : int → int list Proof for #4: What rule applies here? ? Γ ∪ [one : int list ] ⊢ one : int list Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Γ ⊢ (let rec one = 1 :: one in let x = 2 in fun y→ (x :: y :: one)) : int → int list Proof for #4: This is the variable rule. Γ ∪ [one : int list ] ⊢ one : int list Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Γ ⊢ (let rec one = 1 :: one in let x = 2 in fun y→ (x :: y :: one)) : int → int list Proof for #2: What rule applies here? ? Γ ∪ [one : int list ] ⊢ let x = 2 in fun y→ (x :: y :: one) : int → int list Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Γ ⊢ (let rec one = 1 :: one in let x = 2 in fun y→ (x :: y :: one)) : int → int list Proof for #2: We will use the let rule. Γ ∪ [one : int list ] ⊢ 2 : int #5 Γ ∪ [one : int list ] ⊢ let x = 2 in fun y→ (x :: y :: one) : int → int list #5 = Γ ∪ [one : int list , x : int ] ⊢ fun y→ (x :: y :: one) : int → int list Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Γ ⊢ (let rec one = 1 :: one in let x = 2 in fun y→ (x :: y :: one)) : int → int list Proof for #5: What rule should we use? ? Γ ∪ [one : int list , x : int ] ⊢ fun y→ (x :: y :: one) : int → int list Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Γ ⊢ (let rec one = 1 :: one in let x = 2 in fun y→ (x :: y :: one)) : int → int list Proof for #5: Function rule ? Γ ∪ [one : int list , x : int , y : int ] ⊢ (x :: y :: one) : int list Γ ∪ [one : int list , x : int ] ⊢ fun y→ (x :: y :: one) : int → int list Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Γ ⊢ (let rec one = 1 :: one in let x = 2 in fun y→ (x :: y :: one)) : int → int list Proof for #5: Now what? Two more applications... #6 #7 Γ ∪ [one : int list , x : int , y : int ] ⊢ (x :: y :: one) : int list Γ ∪ [one : int list , x : int ] ⊢ fun y→ (x :: y :: one) : int → int list #6 = Γ ∪ [one : int list , x : int , y : int ] ⊢ ((::)x) : int list → int list #7 = Γ ∪ [one : int list , x : int , y : int ] ⊢ (y :: one) : int list Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Γ ⊢ (let rec one = 1 :: one in let x = 2 in fun y→ (x :: y :: one)) : int → int list Proof for #6: ? Γ ∪ [one : int list , x : int , y : int ] ⊢ ((::)x) : int list → int list Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Γ ⊢ (let rec one = 1 :: one in let x = 2 in fun y→ (x :: y :: one)) : int → int list Proof for #7: ? Γ ∪ [one : int list , x : int , y : int ] ⊢ (y :: one) : int list Mark Hills CS421 Lecture 6: Type Derivations Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up Types as Specifications Type systems are a major area of research, and can be very involved, moreso than we have shown here. Types can be seen as specifications of desired program behavior. ◮ Types describe properties ◮ Different type systems describe different properties, eg ◮ Data is read-write versus read-only ◮ Operation has authority to access data ◮ Data came from “right” source ◮ Operation might or could not raise an exception ◮ Common type systems focus on types describing data layout and access methods Mark Hills CS421 Lecture 6: Type Derivations