Type Soundness. Type soundness is a theorem of the form If e : τ, then running e never produces an error

Similar documents
Type Declarations. Γ <num> : num [... <id> τ... ] <id> : τ Γ true : bool Γ false : bool Γ e 1 : num Γ e 2 : num Γ. {+ e 1 e 2 } : num

Type Declarations. [... <id> τ... ] <id> : τ. Γ <num> : number. Γ true : boolean. Γ false : boolean. Γ e 1 : number.

Typed Recursion {with {mk-rec : (((num -> num) -> (num -> num)) -> (num -> num)) {fun {body : ((num -> num) -> (num -> num))} {{fun {fx :

Types and evaluation

Mid-Term 2 Grades

"Good" vs. "Bad" Expressions. ; interp-expr FAE... -> FAE-Value

Higher-Order Functions

Quiz. Question #1: What is the value of the following expression? {+ 1 2} Wrong answer: 0 Wrong answer: 42. Answer: 3 1-4

CS153: Compilers Lecture 14: Type Checking

Higher-Order Functions (Part I)

cs173: Programming Languages Final Exam

CS558 Programming Languages

Comp 311: Sample Midterm Examination

Adding GADTs to OCaml the direct approach

Principles of Programming Languages

1. For every evaluation of a variable var, the variable is bound.

Tracing Ambiguity in GADT Type Inference

Boolean expressions. Elements of Programming Languages. Conditionals. What use is this?

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

CMSC 330: Organization of Programming Languages. Rust Basics

Typing Control. Chapter Conditionals

Lecture #23: Conversion and Type Inference

Non-Functional Procedures

Hard deadline: 3/28/15 1:00pm. Using software development tools like source control. Understanding the environment model and type inference.

At build time, not application time. Types serve as statically checkable invariants.

COMP520 - GoLite Type Checking Specification

CS558 Programming Languages

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

CS558 Programming Languages

Chapter 22: Type Reconstruction (Type Inference)

Recap: Functions as first-class values

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

Principles of Programming Languages

Simple Unification-based Type Inference for GADTs

Implementing Recursion

Functions & First Class Function Values

An Introduction to Functions

CMSC 330: Organization of Programming Languages

Static Analysis of Dynamically Typed Languages made Easy

State. Substitution relies on an identifer having a fxed value

Lecture 15 CIS 341: COMPILERS

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

Begin at the beginning

COMP520 - GoLite Type Checking Specification

ML 4 Unification Algorithm

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Midterm 1. CMSC 430 Introduction to Compilers Spring Instructions Total 100. Name: March 14, 2012

How does ML deal with +?

Variables and Bindings

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

Programming Languages Lecture 14: Sum, Product, Recursive Types

Two Problems. Programming Languages and Compilers (CS 421) Type Inference - Example. Type Inference - Outline. Type Inference - Example

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

Functional Programming. Pure Functional Programming

Threads. {seqn {spawn EXPR 1 } {spawn EXPR 2 }} Runs EXPR 1 and EXPR 2 in any order, even interleaved with each other

CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan

Programming Languages

CMSC 330: Organization of Programming Languages. Operational Semantics

Programs as data Parsing cont d; first-order functional language, type checking

Structurally Recursive Descent Parsing. Nils Anders Danielsson (Nottingham) Joint work with Ulf Norell (Chalmers)

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Programming Languages

Overloading, Type Classes, and Algebraic Datatypes

Metaprogramming assignment 3

Goal. CS152: Programming Languages. Lecture 15 Parametric Polymorphism. What the Library Likes. What The Client Likes. Start simpler.

CSE341 Spring 2017, Final Examination June 8, 2017

CS61A Midterm 2 Review (v1.1)

CSE341 Spring 2017, Final Examination June 8, 2017

Project 6 Due 11:59:59pm Thu, Dec 10, 2015

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

CIS 341 Final Examination 4 May 2017

Discussion 12 The MCE (solutions)

Programming Languages Assignment #7

Where is ML type inference headed?

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

Principles of Programming Languages 2017W, Functional Programming

Midterm 2. CMSC 430 Introduction to Compilers Fall Instructions Total 100. Name: November 19, 2014

Flang typechecker Due: February 27, 2015

Static Checking and Type Systems

Procedures. EOPL3: Section 3.3 PROC and App B: SLLGEN

COS 320. Compiling Techniques

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

Recap: ML s Holy Trinity

CSE-321: Assignment 8 (100 points)

Typed Racket: Racket with Static Types

The Typed Racket Guide

Lecture 3: Expressions

Programming Languages

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

CS 320: Concepts of Programming Languages

Typical workflow. CSE341: Programming Languages. Lecture 17 Implementing Languages Including Closures. Reality more complicated

Ornaments in ML. Thomas Williams, Didier Rémy. April 18, Inria - Gallium

CSCE 314 Programming Languages. Type System

Programming Languages

Lecture 12: Conditional Expressions and Local Binding

CIS 120 Midterm I February 16, 2015 SOLUTIONS

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial

CSE341 Spring 2016, Final Examination June 6, 2016

Transcription:

Type Soundness Type soundness is a theorem of the form If e : τ, then running e never produces an error 1

Type Soundness Type soundness is a theorem of the form If e : τ, then running e never produces an error If we add division and arrays, then some errors are ok: If e : τ, then running e never produces an error except divide-by-zero or array-out-of-bounds In general, soundness rules out a certain class of run-time errors 2

Type Soundness Type soundness is a theorem of the form If e : τ, then running e never produces an error If we add division and arrays, then some errors are ok: If e : τ, then running e never produces an error except divide-by-zero or array-out-of-bounds In general, soundness rules out a certain class of run-time errors Soundness fails bug in type rules or bug in eval 3

Quiz What is the type of the following expression? {fun {x} {+ x 1}} 4

Quiz What is the type of the following expression? {fun {x} {+ x 1}} Answer: Yet another trick question; it s not an expression in our typed language, because the argument type is missing 5

Quiz What is the type of the following expression? {fun {x} {+ x 1}} Answer: Yet another trick question; it s not an expression in our typed language, because the argument type is missing But it seems like the answer should be (num num) 6

Type Inference Type inference is the process of inserting type annotations where the programmer omits them We ll use explicit question marks, to make it clear where types are omitted {fun {x :?} {+ x 1}} 7

Type Inference Type inference is the process of inserting type annotations where the programmer omits them We ll use explicit question marks, to make it clear where types are omitted {fun {x :?} {+ x 1}} <typeexpr> ::= num bool (<typeexpr> -> <typeexpr>)? 8

Type Inference {fun {x :?} {+ x 1} } 9

Type Inference {fun {x :?} {+ x 1} } T 1 10

Type Inference {fun {x :?} {+ x 1} } T 1 num 11

Type Inference {fun {x :?} {+ x 1} } T 1 num num T 1 = num 12

Type Inference {fun {x :?} {+ x 1} } T 1 num (num num) num T 1 = num 13

Type Inference {fun {x :?} {+ x 1} } T 1 num (num num) num T 1 = num Create a new type variable for each? Change type comparison to install type equivalences 14

Type Inference {fun {x :?} {+ x 1} } T 1 num (num num) num T 1 = num {fun {x :?} {if true 1 x} } 15

Type Inference {fun {x :?} {+ x 1} } T 1 num (num num) num T 1 = num {fun {x :?} {if true 1 x} } bool 16

Type Inference {fun {x :?} {+ x 1} } T 1 num (num num) num T 1 = num {fun {x :?} {if true 1 x} } bool int 17

Type Inference {fun {x :?} {+ x 1} } T 1 num (num num) num T 1 = num {fun {x :?} {if true 1 x} } bool int T 1 18

Type Inference {fun {x :?} {+ x 1} } T 1 num (num num) num T 1 = num {fun {x :?} {if true 1 x} } bool int T 1 num T 1 = num 19

Type Inference {fun {x :?} {+ x 1} } T 1 num (num num) num T 1 = num {fun {x :?} {if true 1 x} } bool int T 1 num (num num) T 1 = num 20

Type Inference: Impossible Cases {fun {x :?} {if x 1 x} } 21

Type Inference: Impossible Cases {fun {x :?} {if x 1 x} } T 1 22

Type Inference: Impossible Cases {fun {x :?} {if x 1 x} } T 1 num 23

Type Inference: Impossible Cases {fun {x :?} {if x 1 x} } T 1 num T 1 24

Type Inference: Impossible Cases {fun {x :?} {if x 1 x} } T 1 num T 1 no type: T 1 can't be both bool and num 25

Type Inference: Many Cases {fun {y :?} y} 26

Type Inference: Many Cases {fun {y :?} y} T 1 27

Type Inference: Many Cases {fun {y :?} y} T 1 (T 1 T 1 ) 28

Type Inference: Many Cases {fun {y :?} y} T 1 (T 1 T 1 ) Sometimes, more than one type works (num num) (bool bool) ((num bool) (num bool)) so the type checker leaves variables in the reported type 29

Type Inference: Function Calls {{fun {y :?} y} {fun {x :?} {+ x 1}}} 30

Type Inference: Function Calls {{fun {y :?} y} {fun {x :?} {+ x 1}}} (T 1 T 1 ) 31

Type Inference: Function Calls {{fun {y :?} y} {fun {x :?} {+ x 1}}} (T 1 T 1 ) (num num) 32

Type Inference: Function Calls {{fun {y :?} y} {fun {x :?} {+ x 1}}} (T 1 T 1 ) (num num) T 1 = (num num) 33

Type Inference: Function Calls {{fun {y :?} y} {fun {x :?} {+ x 1}}} (T 1 T 1 ) (num num) (num num) T 1 = (num num) 34

Type Inference: Function Calls {fun {y :?} {y 7} } 35

Type Inference: Function Calls {fun {y :?} {y 7} } T 1 36

Type Inference: Function Calls {fun {y :?} {y 7} } T 1 num 37

Type Inference: Function Calls {fun {y :?} {y 7} } T 1 num T 2 T 1 = (num T 2 ) 38

Type Inference: Function Calls {fun {y :?} {y 7} } T 1 num T 2 T 1 = (num T 2 ) ((num T 2 ) T 2 ) 39

Type Inference: Function Calls {fun {y :?} {y 7} } T 1 num T 2 T 1 = (num T 2 ) ((num T 2 ) T 2 ) In general, create a new type variable record for the result of a function call 40

Type Inference: Cyclic Equations {fun {x :?} {x x} } 41

Type Inference: Cyclic Equations {fun {x :?} {x x} } T 1 42

Type Inference: Cyclic Equations {fun {x :?} {x x} } T 1 T 1 43

Type Inference: Cyclic Equations T 1 can t be int T 1 can t be bool {fun {x :?} {x x} } T 1 T 1 no type: T 1 can't be (T 1...) Suppose T 1 is (T 2 T 3 ) T 2 must be T 1 So we won t get anywhere! 44

Type Inference: Cyclic Equations {fun {x :?} {x x} } T 1 T 1 no type: T 1 can't be (T 1...) The occurs check: When installing a type equivalence, make sure that the new type for T doesn t already contain T 45

Type Unification Unify a type variable T with a type τ 2 : If T is set to τ 1, unify τ 1 and τ 2 If τ 2 is already equivalent to T, succeed If τ 2 contains T, then fail Otherwise, set T to τ 2 and succeed Unify a type τ 1 to type τ 2 : If τ 2 is a type variable T, then unify T and τ 1 If τ 1 and τ 2 are both num or bool, succeed If τ 1 is (τ 3 τ 4 ) and τ 2 is (τ 5 τ 6 ), then unify τ 3 with τ 5 unify τ 4 with τ 6 Otherwise, fail 46

TIFAE Grammar <TIFAE> ::= <num> {+ <TIFAE> <TIFAE>} {- <TIFAE> <TIFAE>} <id> {fun {<id> : <TE>} <TIFAE>} {<TIFAE> <TIFAE>} {if0 <TIFAE> <TIFAE> <TIFAE>} {rec {<id> : <TE> <TIFAE>} <TIFAE>} <TE> ::= num (<TE> -> <TE>)? NEW 47

Representing Expressions (define-type TFAE [num (n : number)] [add (l : TFAE) (r : TFAE)] [sub (l : TFAE) (r : TFAE)] [id (name : symbol)] [fun (name : symbol) (t : TE) ; different (body : TFAE)] [app (rator : TFAE) (rand : TFAE)]) 48

Representing Type Variables (define-type Type [numt] [boolt] [arrowt (arg : Type) (result : Type)] [vart (is : (boxof MaybeType))]) (define-type TE [numte] [boolte] [arrowte (arg : TE) (result : TE)] [guesste]) (define-type MaybeType [none] [some (t : Type)]) 49

Parsing Types (define parse-type : (TE -> Type) (lambda (te) (type-case TE te [numte () (numt)] [arrowte (d r) (arrowt (parse-type d) (parse-type r))] [guesste () (vart (box (none)))]))) 50

Type Unification (define (unify! t1 t2 expr) (type-case Type t1 [vart (is1)...] [else (type-case Type t2 [vart (is2) (unify! t2 t1 expr)] [numt () (type-case Type t1 [numt () (values)] [else (type-error expr t1 t2)])] [boolt () (type-case Type t1 [boolt () (values)] [else (type-error expr t1 t2)])] [arrowt (a2 b2) (type-case Type t1 [arrowt (a1 b1) (begin (unify! a1 a2 expr) (unify! b1 b2 expr))] [else (type-error expr t1 t2)])])])) 51

Type Unification (define (unify! t1 t2 expr) (type-case Type t1 [vart (is1)...] [else...])) 52

Type Unification (define (unify! t1 t2 expr) (type-case Type t1 [vart (is1)...] [else...])) 53

Type Unification (define (unify! t1 t2 expr) (type-case Type t1 [vart (is1) (type-case MaybeType (unbox is1) [some (t3) (unify! t3 t2 expr)] [none ()...])] [else...])) 54

Type Unification (define (unify! t1 t2 expr) (type-case Type t1 [vart (is1) (type-case MaybeType (unbox is1) [some (t3) (unify! t3 t2 expr)] [none () (local [(define t3 (resolve t2))]...)])] [else...])) 55

Type Unification (define (unify! t1 t2 expr) (type-case Type t1 [vart (is1) (type-case MaybeType (unbox is1) [some (t3) (unify! t3 t2 expr)] [none () (local [(define t3 (resolve t2))] (if (eq? t1 t3) (values)...))])] [else...])) 56

Type Unification (define (unify! t1 t2 expr) (type-case Type t1 [vart (is1) (type-case MaybeType (unbox is1) [some (t3) (unify! t3 t2 expr)] [none () (local [(define t3 (resolve t2))] (if (eq? t1 t3) (values) (begin (set-box! is1 (some t3)) (values))))])] [else...])) 57

Type Unification Helpers (define (resolve t) (type-case Type t [vart (is) (type-case MaybeType (unbox is) [none () t] [some (t2) (resolve t2)])] [else t])) (define (occurs? r t) (type-case Type t [numt () false] [boolt () false] [arrowt (a b) (or (occurs? r a) (occurs? r b))] [vart (is) (or (eq? r t) (type-case MaybeType (unbox is) [none () false] [some (t2) (occurs? r t2)]))])) 58

TIFAE Type Checker (define typecheck : (FAE TypeEnv -> Type) (lambda (fae env) (type-case FAE fae... [num (n) (numt)]...))) 59

TIFAE Type Checker (define typecheck : (FAE TypeEnv -> Type) (lambda (fae env) (type-case FAE fae... [add (l r)... (typecheck l env)...... (typecheck r env)...]...))) 60

TIFAE Type Checker (define typecheck : (FAE TypeEnv -> Type) (lambda (fae env) (type-case FAE fae... [add (l r) (begin (unify! (typecheck l env) (numt) l) (unify! (typecheck r env) (numt) r) (numt))]...))) 61

TIFAE Type Checker (define typecheck : (FAE TypeEnv -> Type) (lambda (fae env) (type-case FAE fae... [id (name) (get-type name env)] [fun (name te body) (local [(define arg-type (parse-type te))] (arrowt arg-type (typecheck body (abind name arg-type env))))]...))) 62

TIFAE Type Checker (define typecheck : (FAE TypeEnv -> Type) (lambda (fae env) (type-case FAE fae... [app (fn arg)... (typecheck fn env)...... (typecheck arg env)...]...))) 63

TIFAE Type Checker (define typecheck : (FAE TypeEnv -> Type) (lambda (fae env) (type-case FAE fae... [app (fn arg) (local [(define result-type (vart (box (none))))]... (arrowt (typecheck arg env) result-type)... (typecheck fn env)...)]...))) 64

TIFAE Type Checker (define typecheck : (FAE TypeEnv -> Type) (lambda (fae env) (type-case FAE fae... [app (fn arg) (local [(define result-type (vart (box (none))))] (begin (unify! (arrowt (typecheck arg env) result-type) (typecheck fn env) fn) result-type))]...))) 65

TIFAE Type Checker (define typecheck : (FAE TypeEnv -> Type) (lambda (fae env) (type-case FAE fae... [if0 (test-expr then-expr else-expr)... (typecheck test-expr env)...... (typecheck then-expr env)...... (typecheck else-expr env)...]...))) 66

TIFAE Type Checker (define typecheck : (FAE TypeEnv -> Type) (lambda (fae env) (type-case FAE fae... [if0 (test-expr then-expr else-expr) (begin (unify! (typecheck test-expr env) (numt) test-expr)... (typecheck then-expr env)...... (typecheck else-expr env)...)]...))) 67

TIFAE Type Checker (define typecheck : (FAE TypeEnv -> Type) (lambda (fae env) (type-case FAE fae... [if0 (test-expr then-expr else-expr) (begin (unify! (typecheck test-expr env) (numt) test-expr) (local [(define test-ty (typecheck then-expr env))] (begin (unify! test-ty (typecheck else-expr env) else-expr) test-ty)))]...))) 68

TIFAE Type Checker (define typecheck : (FAE TypeEnv -> Type) (lambda (fae env) (type-case FAE fae... [rec (name ty rhs-expr body-expr) (local [(define rhs-ty (parse-type ty)) (define new-ds (abind name rhs-ty env))] (begin (unify! rhs-ty (typecheck rhs-expr new-ds) rhs-expr) (typecheck body-expr new-ds)))]...))) 69