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

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

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

Higher-Order Functions

CS153: Compilers Lecture 14: Type Checking

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

Higher-Order Functions (Part I)

cs173: Programming Languages Final Exam

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

CS558 Programming Languages

Principles of Programming Languages

Adding GADTs to OCaml the direct approach

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

Lecture 15 CIS 341: COMPILERS

Comp 311: Sample Midterm Examination

State. Substitution relies on an identifer having a fxed value

Tracing Ambiguity in GADT Type Inference

ML 4 Unification Algorithm

Typing Control. Chapter Conditionals

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

CS558 Programming Languages

Non-Functional Procedures

CS558 Programming Languages

Programming Languages Assignment #7

CSE-321: Assignment 8 (100 points)

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

Chapter 6 Intermediate Code Generation

CMSC 330: Organization of Programming Languages

Simple Unification-based Type Inference for GADTs

Lecture 12: Conditional Expressions and Local Binding

Principles of Programming Languages

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

Mini-ML. CS 502 Lecture 2 8/28/08

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

COMP520 - GoLite Type Checking Specification

Semantics of programming languages

Chapter 22: Type Reconstruction (Type Inference)

CMSC 330: Organization of Programming Languages. Rust Basics

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

Lecture #23: Conversion and Type Inference

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

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

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

Static Checking and Type Systems

Semantics of programming languages

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

CMSC 330: Organization of Programming Languages. Operational Semantics

Type Checking and Type Inference

CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan

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

CIS 341 Final Examination 4 May 2017

COMP520 - GoLite Type Checking Specification

CS153: Compilers Lecture 16: Local Optimization II

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

Typed Lambda Calculus and Exception Handling

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

Metaprogramming assignment 3

Types and Static Type Checking (Introducing Micro-Haskell)

Implementing Recursion

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

Semantics of programming languages

Motivation At the very begining Ni won t be complete until it has static typing 1 Maintenance needs nipkgs: 1M sloc Errors hard to spot 1 Eelco Dolstr

Lecture 3: Expressions

CS4215 Programming Language Implementation. Martin Henz

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

Overloading, Type Classes, and Algebraic Datatypes

Types and Type Inference

Typing Data. Chapter Recursive Types Declaring Recursive Types

Typed Racket: Racket with Static Types

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

Recap: Functions as first-class values

An Introduction to Functions

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

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

ENGI 1020 Introduction to Computer Programming J U L Y 5, R E Z A S H A H I D I

Programming Languages

Programming Languages Lecture 14: Sum, Product, Recursive Types

CPSC 411, 2015W Term 2 Midterm Exam Date: February 25, 2016; Instructor: Ron Garcia

Begin at the beginning

Flang typechecker Due: February 27, 2015

COP5621 Exam 3 - Spring 2005

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Programming Languages

Harvard School of Engineering and Applied Sciences Computer Science 152

CS 320: Concepts of Programming Languages

Types, Type Inference and Unification

How does ML deal with +?

MIT Top-Down Parsing. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

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

Compilers and computer architecture: Semantic analysis

Types and Static Type Checking (Introducing Micro-Haskell)

Semantic Processing (Part 2)

CSCE 314 Programming Languages. Type System

Axiomatic Rules. Lecture 18: Axiomatic Semantics & Type Safety. Correctness using Axioms & Rules. Axiomatic Rules. Steps in Proof

Types and Type Inference

CS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Transcription:

Type Declarations Γ <num> : num [... <id> τ... ] <id> : τ Γ true : bool Γ false : bool Γ e 1 : num Γ e 2 : num Γ {+ e 1 e 2 } : num Γ e 1 : bool Γ e 2 : τ 0 Γ e 3 : τ 0 Γ {if e 1 e 2 e 3 } : τ 0 Γ[ <id> τ 1 ] e : τ 0 Γ {fun {<id> : τ 1 } e} : (τ 1 τ 0 ) Γ e 0 : (τ 1 τ 0 ) Γ e 1 : τ 1 Γ {e 0 e 1 } : τ 0 1

Type Inference Γ <num> : num [... <id> τ... ] <id> : τ Γ true : bool Γ false : bool Γ e 1 : num Γ e 2 : num Γ {+ e 1 e 2 } : num Γ e 1 : bool Γ e 2 : τ 0 Γ e 3 : τ 0 Γ {if e 1 e 2 e 3 } : τ 0 Γ[ <id> τ 1 ] e : τ 0 Γ {fun {<id>} e} : (τ 1 τ 0 ) Γ e 0 : (τ 1 τ 0 ) Γ e 1 : τ 1 Γ {e 0 e 1 } : τ 0 2

Do the rules still work? (Yes.) [x num] x : num [x num] 1 : num [x num] {+ x 1} : num {fun {x} {+ x 1}} : (num -> num) But how do we implement them now? 3

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}} 4

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>)? 5

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

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

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

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

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

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 11

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

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

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

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 15

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 16

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 17

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

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

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

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

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 22

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

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

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

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 26

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

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

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

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

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

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

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

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

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

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

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 37

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

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

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

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

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 42

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 43

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 44

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)]) 45

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)]) 46

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

Type Unification (define unify! : (Type Type -> ()) (lambda (t1 t2) (type-case Type t1 [vart (b) (type-case MaybeType (unbox b) [some (t1-2) (unify! t1-2 t2)] [none () (type-case Type t2 [vart (b2) (type-case MaybeType (unbox b2) [some (t2-2) (unify! t1 t2-2)] [none () (if (eq? b b2) (values) (begin (set-box! b (some t2)) (values)))])] [else...])])] [numt ()...] [boolt ()...] [arrowt (d1 r1)...]))) 48

Type Unification (define unify! : (Type Type -> ()) (lambda (t1 t2) (type-case Type t1 [vart (b) (type-case MaybeType (unbox b) [some (t1-2) (unify! t1-2 t2)] [none () (type-case Type t2 [vart (b2)...] [else (if (occurs? b t2) (error 'type-check "failed") (begin (set-box! b (some t2)) (values)))])])] [numt ()...] [boolt ()...] [arrowt (d1 r1)...]))) 49

Type Unification (define unify! : (Type Type -> ()) (lambda (t1 t2) (type-case Type t1 [vart (b)...] [numt () (type-case Type t2 [vart (b) (unify! t2 t1)] [numt () (values)] [else (error 'type-check "failed")])] [boolt () (type-case Type t2 [vart (b) (unify! t2 t1)] [boolt () (values)] [else (error 'type-check "failed")])] [arrowt (d1 r1) (type-case Type t2 [vart (b) (unify! t2 t1)] [arrowt (d2 r2) (begin (unify! d1 d2) (unify! r1 r2))] [else (error 'type-check "failed")])]))) 50

Occurs Check (define occurs? : ((boxof MaybeType) Type -> boolean) (lambda (b t) (type-case Type t [vart (b2) (or (eq? b b2) (type-case MaybeType (unbox b2) [none () false] [some (t2-2) (occurs? b t2-2)]))] [numt () false] [arrowt (d r) (or (occurs? b d) (occurs? b r))]))) 51

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

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

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))]...))) 54

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))))]...))) 55

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

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)...)]...))) 57

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))]...))) 58

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)...]...))) 59

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)...)]...))) 60

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)))]...))) 61

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)))]...))) 62