Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Type Systems and Type Checking: Understanding Strongly and Weakly Typed Languages - Prof. , Study notes of Computer Science

A lecture note from cs421 at the university of illinois at urbana-champaign, focusing on the concepts of data types, strongly and weakly typed languages, static and dynamic typing, type checking, and type declarations and inference. It covers the importance of data types in programming design, the terminology of types and type checking, and the use of type rules to structure proof-trees for checking and inferring the type of expressions.

Typology: Study notes

Pre 2010

Uploaded on 03/13/2009

koofers-user-p1i
koofers-user-p1i 🇺🇸

10 documents

1 / 69

Toggle sidebar

Related documents


Partial preview of the text

Download Type Systems and Type Checking: Understanding Strongly and Weakly Typed Languages - Prof. and more Study notes Computer Science in PDF only on Docsity! Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up CS421 Lecture 12: Type Derivations1 Mark Hills mhills@cs.uiuc.edu University of Illinois at Urbana-Champaign June 24, 2008 1Based on slides by Mattox Beckman, as updated by Vikram Adve, Gul Agha, and Elsa Gunter Mark Hills CS421 Lecture 12: Type Derivations 1 / 42 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 12: Type Derivations 2 / 42 Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up Types and Type Systems Strongly/Weakly Typed Static/Dynamic Typing Type Checking Type Declarations and Inference Type Equivalence, Compatibility, Conversion 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 12: Type Derivations 5 / 42 Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up Types and Type Systems Strongly/Weakly Typed Static/Dynamic Typing Type Checking Type Declarations and Inference Type Equivalence, Compatibility, Conversion 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 12: Type Derivations 6 / 42 Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up Types and Type Systems Strongly/Weakly Typed Static/Dynamic Typing Type Checking Type Declarations and Inference Type Equivalence, Compatibility, Conversion 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 12: Type Derivations 7 / 42 Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up Types and Type Systems Strongly/Weakly Typed Static/Dynamic Typing Type Checking Type Declarations and Inference Type Equivalence, Compatibility, Conversion 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 12: Type Derivations 10 / 42 Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up Types and Type Systems Strongly/Weakly Typed Static/Dynamic Typing Type Checking Type Declarations and Inference Type Equivalence, Compatibility, Conversion 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 12: Type Derivations 11 / 42 Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up Types and Type Systems Strongly/Weakly Typed Static/Dynamic Typing Type Checking Type Declarations and Inference Type Equivalence, Compatibility, Conversion 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 12: Type Derivations 12 / 42 Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up Types and Type Systems Strongly/Weakly Typed Static/Dynamic Typing Type Checking Type Declarations and Inference Type Equivalence, Compatibility, Conversion Dynamic Types: An Example 1 CL-USER(1): (defvar *list1* ’(1 2 3)) 2 *LIST1* 3 CL-USER(2): (defvar *list2* ’("three" "short" "words")) 4 *LIST2* 5 CL-USER(3): (defvar *a* (car *list1*)) 6 *A* 7 CL-USER(4): (defvar *b* (car (cdr *list1*))) 8 *B* 9 CL-USER(5): (+ *a* *b*) 10 3 Mark Hills CS421 Lecture 12: Type Derivations 15 / 42 Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up Types and Type Systems Strongly/Weakly Typed Static/Dynamic Typing Type Checking Type Declarations and Inference Type Equivalence, Compatibility, Conversion Dynamic Types: An Example (part 2) 1 CL-USER(6): (setf *a* (car *list2*)) 2 "three" 3 CL-USER(7): (setf *b* (car (cdr *list2*))) 4 "short" 5 CL-USER(8): (concatenate ’string *a* *b*) 6 "threeshort" 7 CL-USER(9): (+ *a* *b*) 8 Error: ‘"three"’ is not of the expected type ‘NUMBER’ Mark Hills CS421 Lecture 12: Type Derivations 16 / 42 Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up Types and Type Systems Strongly/Weakly Typed Static/Dynamic Typing Type Checking Type Declarations and Inference Type Equivalence, Compatibility, Conversion Static Type Checking ◮ performed after parsing, but before code generation – considered part of the compiler front-end ◮ type of every variable and type signature of every operator must be known at compile time ◮ can eliminate need to store type information with data object if no dynamic checks are needed – type erasure ◮ catches many programming errors early – before execution ◮ cannot check types that depend on dynamically computed values, like array bounds Mark Hills CS421 Lecture 12: Type Derivations 17 / 42 Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up Types and Type Systems Strongly/Weakly Typed Static/Dynamic Typing Type Checking Type Declarations and Inference Type Equivalence, Compatibility, Conversion Type Declarations A type declaration is the explicit assignment of a type to a variable or function in the program source ◮ must be checked in a strongly typed language ◮ often not necessary for strong typing or even static typing Mark Hills CS421 Lecture 12: Type Derivations 20 / 42 Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up Types and Type Systems Strongly/Weakly Typed Static/Dynamic Typing Type Checking Type Declarations and Inference Type Equivalence, Compatibility, Conversion Type Inference A program analysis that attempts to identify the type of an expression from the program context of the expression; also called type reconstruction ◮ fully static type inference first introduced by Robin Milner in ML ◮ Haskell, OCaml, and SML all use type inference ◮ As we’ve noticed, records are a problem for type inference Mark Hills CS421 Lecture 12: Type Derivations 21 / 42 Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up Types and Type Systems Strongly/Weakly Typed Static/Dynamic Typing Type Checking Type Declarations and Inference Type Equivalence, Compatibility, Conversion Type Equivalence and Compatibility Two types that represent the same underlying data items are said to be equivalent. Equivalence comes in two forms: ◮ Name equivalence, where two types are equivalent if they share the same name ◮ Structural equivalence, where two types are equivalent if they have the same structure Sometimes equivalence is not necessary. In these cases, operations may just need a compatible type. Mark Hills CS421 Lecture 12: Type Derivations 22 / 42 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 12: Type Derivations 25 / 42 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 12: Type Derivations 26 / 42 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 12: Type Derivations 27 / 42 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 12: Type Derivations 30 / 42 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 12: Type Derivations 31 / 42 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 12: Type Derivations 31 / 42 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 12: Type Derivations 31 / 42 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 12: Type Derivations 31 / 42 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 12: Type Derivations 31 / 42 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 12: Type Derivations 31 / 42 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 12: Type Derivations 31 / 42 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 12: Type Derivations 31 / 42 Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Function Application Γ ⊢ e1 : τ1 → τ2 Γ ⊢ e2 : τ1 Γ ⊢ e1 e2 : τ2 ◮ If you have a function expression e1 of type τ1 → τ2, applied to an argument expression e2 of type τ1, the resulting expression has type τ2 Mark Hills CS421 Lecture 12: Type Derivations 33 / 42 Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Function Application Example Γ ⊢ print int : int → unit Γ ⊢ 5 : int Γ ⊢ print int 5 : unit ◮ e1 = print int ◮ e2 = 5 ◮ τ1 = int ◮ τ2 = unit . Mark Hills CS421 Lecture 12: Type Derivations 34 / 42 Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Function Application Example 2 Γ ⊢ map print int : int list → unit list Γ ⊢ [3; 7] : int list Γ ⊢ map print int [3; 7] : unit list ◮ e1 = map print int ◮ e2 = [3; 7] ◮ τ1 = int list ◮ τ2 = unit list Mark Hills CS421 Lecture 12: Type Derivations 35 / 42 Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Let and Letrec ◮ Let Rule: Γ ⊢ e1 : τ Γ ∪ [x : τ ] ⊢ e2 : τ ′ Γ ⊢ let x = e1 in e2 : τ ′ ◮ Letrec Rule: Γ ∪ [x : τ ] ⊢ e1 : τ Γ ∪ [x : τ ] ⊢ e2 : τ ′ Γ ⊢ let rec x = e1 in e2 : τ ′ Mark Hills CS421 Lecture 12: Type Derivations 38 / 42 Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec Some caveats... The above system cannot handle polymorphism like we find in OCaml. Specifically, we have meta-variables in our proof logic, but no type variables in the type language. To handle this properly, we would need: ◮ Object level type variables ◮ Type quantification (universal types) ◮ let and letrec rules to introduce polymorphism ◮ Explicit rules to eliminate (instantiate) polymorphism Mark Hills CS421 Lecture 12: Type Derivations 39 / 42 Introduction and Objectives Terminology Type Judgments Typing Rules Wrap-Up General Format Axioms Simple Rules Example 1 Type Variables Functions Let and Letrec A more advanced example Γ ⊢ (let rec one = 1 :: one in let x = 2 in fun y− >(x :: y :: one)) : int → int list Mark Hills CS421 Lecture 12: Type Derivations 40 / 42 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 12: Type Derivations 41 / 42 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 12: Type Derivations 41 / 42 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 12: Type Derivations 41 / 42 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 12: Type Derivations 41 / 42 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 12: Type Derivations 41 / 42 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 12: Type Derivations 41 / 42 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 12: Type Derivations 41 / 42 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 12: Type Derivations 41 / 42 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 12: Type Derivations 41 / 42
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved