Principles of Programming Languages

Similar documents
Principles of Programming Languages

עקרונות שפות תכנות, סמסטר ב' 2016 תרגול 7: הסקת טיפוסים סטטית. Axiomatic Type Inference

Principles of Programming Languages

Comp 311: Sample Midterm Examination

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

עקרונות שפות תכנות 2018 תרגול 8 Type Inference System

Functional Programming. Pure Functional Programming

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

LECTURE 16. Functional Programming

Module 6. Knowledge Representation and Logic (First Order Logic) Version 2 CSE IIT, Kharagpur

CS 314 Principles of Programming Languages

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

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

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

CMSC 330: Organization of Programming Languages

Variables. Substitution

User-defined Functions. Conditional Expressions in Scheme

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

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Typed Scheme: Scheme with Static Types

CS 314 Principles of Programming Languages

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

6.821 Programming Languages Handout Fall MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Compvter Science

;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CS558 Programming Languages

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Introduction to Scheme

Principles of Programming Languages, Spring 2016 Assignment 5 Logic Programming

Principles of Programming Languages

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

Typed Racket: Racket with Static Types

A Brief Introduction to Scheme (II)

CS 314 Principles of Programming Languages

Mid-Term 2 Grades

CSCC24 Functional Programming Scheme Part 2

Project 2: Scheme Interpreter

Programming Languages

CS61A Midterm 2 Review (v1.1)

Overloading, Type Classes, and Algebraic Datatypes

A Small Interpreted Language

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

Lecture 15 CIS 341: COMPILERS

6.001 Notes: Section 8.1

Flang typechecker Due: February 27, 2015

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Lecture 12: Conditional Expressions and Local Binding

CSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018

Functional Programming. Pure Functional Languages

Denotational Semantics. Domain Theory

Functional Programming. Pure Functional Languages

The Substitution Model

Bindings & Substitution

Organization of Programming Languages CS3200/5200N. Lecture 11

Principles of Programming Languages

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

Scheme: Strings Scheme: I/O

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Lecture #23: Conversion and Type Inference

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

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

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Program Analysis: Lecture 02 Page 1 of 32

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Functional Languages. Hwansoo Han

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

Functional Programming - 2. Higher Order Functions

Building a system for symbolic differentiation

Chapter 15 Functional Programming Languages

Name EID. (calc (parse '{+ {with {x {+ 5 5}} {with {y {- x 3}} {+ y y} } } z } ) )

Lambda the Ultimate. Corky Cartwright Vivek Sarkar Department of Computer Science Rice University

Programming Languages

cs173: Programming Languages Final Exam

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Principles of Programming Languages

Scheme Quick Reference

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

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

CS558 Programming Languages

Scheme Quick Reference

Programming Languages Third Edition

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

Normal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation?

CSE341 Autumn 2017, Final Examination December 12, 2017

Software Paradigms (Lesson 4) Functional Programming Paradigm

Discussion 12 The MCE (solutions)

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

Formal Systems and their Applications

FP Foundations, Scheme

SML A F unctional Functional Language Language Lecture 19

Higher-Order Logic. Specification and Verification with Higher-Order Logic

From Syntactic Sugar to the Syntactic Meth Lab:

Type Processing by Constraint Reasoning

CS153: Compilers Lecture 14: Type Checking

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Pierce Ch. 3, 8, 11, 15. Type Systems

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Transcription:

Principles of Programming Languages Slides by Dana Fisman based on book by Mira Balaban and lecuture notes by Michael Elhadad Lesson 15 Type Inference Collaboration and Management Dana Fisman www.cs.bgu.ac.il/~ppl172 1

Review of Last Lecture Scheme L4 define L1 only primitive ops and types L2 L3 cond lambda cons car cdr '() list letrec L5 Example of a fully typed expr: (define [g : [number * number -> number]] (lambda ([x : number] [y : number]) : number (+ x y))

Type checker { b:boolean, x:number, f:[empty -> T2], g:t2 } Fully typed expression E, type environment TEnv Type Checker ((lambda ([y : Ty] Type of E Applying non-proc / Wrong # of args / Wrong type of args / Non-boolean test

Type Checking System Typing Axioms o o For Literals (Booleans, Numbers) For Primitive Operators (+,-,*,/,<,>,=,not) Typing Inference Rules o o o o For Procedures For Applications For If

Type Checking Alg. Overview The alg. is a typical syntax-driven traversal of the expression AST. All the nodes in the AST are exhaustively traversed. On each node, we apply a typing rule and compute a type value. We maintain an environment reflecting the scope of variables o When a new scope is created (in procedure expressions), we extend the environment with the type of the declared parameters. o When variables are bound (e.g. in application expressions, let or letrec expressions) we check they adhere to the declared type

Type Checker vs. Interpreter Type Checker Interpreter Static Analysis: sees only program text Binds identifiers to types Compresses set of values (even of size) into types Always terminates Parses the body of an expr exactly once Dynamic Analysis: runs over actual data Binds identifiers to values (or locations) Treats elements of a certain type distinctly Might not terminate Parses the body of an expr zero to times

Type Inference More ambitious than type checking Type annotations may be missing (or nonexisting at all) Type inference algorithm needs not only to determine the expression type and that it is safe, but to infer the missing type annotations. The algorithm will rely on the operation of unification

Type Checking vs. Type Inference Type Checking Type Inference Fully Typed Expression e, Type Environment Tenv Partially Annotated Expression e Type Checker Type Inference Type of e wrt. Tenv Type- Error #... A typing Statement Tenv ` e : t Type- Error #...

Type Inference using Type Equations We will present an algorithm extending the type checker with type equations Type equations will be constructed for every sub-expression The type checking/inference procedure turns into a type equation solver

Type Inference Plan for today: o o o first learn to infer types manually then devise the algorithm First of all, lets recall the heart of type inference system: the axioms and inference rules

Recalling the Axioms and Inference Rules Typing axiom for atomic literals: Typing axiom Number: For every type environment _Tenv and number _n: _Tenv ` (num_exp _n) : Number Typing axiom Boolean : For every type environment _Tenv and boolean _b: _Tenv ` (bool_exp _b) : Boolean

Recalling the Axioms and Inference Rules Typing axiom for primitive ops: Typing axiom for +: For every type environment _Tenv: _Tenv ` + : [Number * * Number -> Number ] Typing axiom for <: For every type environment _Tenv: _Tenv ` < : [Number * Number -> Boolean] Typing axiom for not: For every type environment _Tenv and type expressions _S: _Tenv ` not: [_S -> Boolean]

Recalling the Axioms and Inference Rules Typing inference rule for Procedure: (lambda exp.) For every type environment _Tenv, variables _x1,..., _xn, n >= 0 expressions _e1,..., _em, m >= 1, and type expressions _S1,...,_Sn, _U1,...,_Um : Procedure with parameters (n > 0): If _Tenv o {_x1:_s1,..., _xn:_sn } ` _ei:_ui for all i = 1..m, Then _Tenv ` (lambda (_x1... _xn ) _e1... _em) : [_S1 *... * _Sn -> _Um] Parameter-less Procedure (n = 0): If _Tenv ` _ei:_ui for all i = 1..m, Then _Tenv `(lambda () _e1... _em) : [Empty -> _Um]

Recalling the Axioms and Inference Rules Typing inference rule for If: For every type environment _Tenv, expressions _test, _then, _else type expressions _S If _Tenv ` _test: Boolean _Tenv ` _then: _S _Tenv ` _else: _S Then _Tenv ` (if _test _then _else) : _S

Recalling the Axioms and Inference Rules Typing inference rule for Application: For every: type environment _Tenv, expressions _f, _e1,..., _en, n 0, and type expressions _S1,..., _Sn, _S: Procedure with parameters (n > 0): If _Tenv ` _f:[_s1*...*_sn -> _S], _Tenv ` _e1:_s1,... _Tenv ` _en:_sn Then _Tenv ` (_f _e1... _en):_s Parameter-less Procedure (n = 0): If _Tenv ` _f:[empty -> _S] Then _Tenv ` (_f):_s

Axioms and Inference Rules Typing inference rule for let: For every: type environment _Tenv, variables _x1,..., _xn, n >= 0 expressions _e1,..., _en, n 0, and expressions _b1,..., _bn, m 0, and type expressions _S1,..., _Sn, _U1,,_Um: Procedure with parameters (n > 0): If _Tenv {_x1 : _S1,..., _xn : _Sn } ` _bi : _Ui for all i = 1..n _Tenv ` _ei:_si for all i = 1..m Then _Tenv (let ([_x1 _e1]... [_xn _en]) _b1... _bm) : _Um

Axioms and Inference Rules Monotonicity Typing inference rule For every: type environments _Tenv, _Tenv expressions _e, n 0, and type expressions _S: If _Tenv ` _e :_S, Then _Tenv _Tenv ` _e :_S

Type Inference Ex. 1 Consider ((lambda (x) (+ x 3)) 5) With type annotations: ((lambda ( [x : Tx]) : T1 (+ x 3)) 5) We want to infer Tx and T1

Type Inference Ex. 1 ((lambda ([x : Tx]) : T1 (+ x 3)) 5) Tapp ((lambda (x) (+ x 3)) 5) Tproc (lambda (x) (+ x 3)) 5 Tn5 (+ x 3) T1 T+ + x 3 Tx Tn3

Type Inference Ex. 1 Tproc T+ [Num * Num -> Num] ((lambda (x) (+ x 3)) 5) (lambda (x) (+ x 3)) 5 (+ x 3) T1 + x 3 Tx Tapp Tn3 Number Tn5 Number Tproc = [Tx -> T1] T+ = [Tx * Tn3 -> T1] Tproc = [Tn5 -> Tapp] T+= [Num*Num->Num] Tx = Num T1 = Num Tproc = [Num->Num] Tapp = Num

Type Inference Ex. 1b Tapp ((lambda (x) (+ x 3)) 5) Tproc T+ [Num * Num -> Num] (lambda (x) (+ x 3)) (+ x 3) T1 + x 3 Tx Tn3 Number 'five Tfive Symbol Tproc = [Tx -> T1] T+ = [Tx * Tn3 -> T1] Tproc = [Tfive -> Tapp] T+= [Num*Num->Num] Tx = Num T1 = Num Tproc = [Num->Num] Tfive = Num

Type Inference Ex. 1c Tproc (lambda (x) (+ x 3)) We do not know what the type of x is and whether it is a Number (+ x 3) T1 Still, type inference will terminate fine. T+ [Num * Num -> Num] + x 3 Tx Tn3 Number but the inference alg. will return that Tproc : [Number -> Number] in an env. where x is assigned a Number

Type Inference Ex. 2 Consider (let ([x 1]) (lambda (f y) (f (+ x y)))) With type annotations: (let ([[x : Tx]) 1]) (lambda ([f : Tf] [y : Ty]) : Tres (f (+ x y)))) We want to infer Tx, Tf, Ty and Tres

Type Inference Ex. 2 Tlet (let ([x 1]) (lambda (f y) (f (+ x y)))) Tx x 1 Tn1 (lambda (f y) (f (+ x y))) Tproc (f (+ x y)) Tres f Tf (+ x y) Tplus (let ([x : Tx]) 1]) (lambda ([f : Tf]) [y : Ty]) : Tres (f (+ x y)))) + x y T+ Tx Ty

Type Inference Ex. 2 Tproc = [Tf * Ty -> Tres] T+= [Tx * Ty -> Tplus] T+= [Num * Num -> Num] Tx = Num Ty = Num Tplus = Num Tf = [Tplus -> Tres] Tf = [Tnum -> Tres] x 1 Tn1 Tproc = [[Tnum -> Tres] *Tnum] -> Tres Tlet = Tproc Tx Tlet = [[Tnum -> Tres] *Tnum] -> Tres (let ([x 1]) (lambda (f y) (f (+ x y)))) (lambda (f y) (f (+ x y))) f Tf (f (+ x y)) T+ Tlet (+ x y) + x y Tx Tproc Tres Tplus Ty

Type Inference Ex. 2 (let ([[x : Tx] 1]) (lambda ([f : Tf] [y : Ty]) : Tres (f (+ x y)))) We inferred Tx = Number, Ty = Number, Tf = [Number -> Tres], Tlet = [[Number -> Tres] * Number] -> Tres

Type Inference Ex. 3 Consider (lambda (x) (x x)) With type annotations: (lambda ([x : Tx]) : T1 (x x)) Tproc (lambda (x) (x x)) (x x) T1 We want to infer Tx and T1 x Tx x Tx

Type Inference Ex. 3 Tproc = [Tx -> T1] Tx = [Tx -> T1] Tproc (lambda (x) (x x)) T1 (x x) x Tx x Tx

Type Inference Algorithm using Type Equations The Alg. we present preforms type inference by solving type equations. Type equation solvers use the unification algorithm for unifying type expressions and producing consistent substitution of type variables which makes all equations equal.

Type Inference Algorithm using Type Equations The method has 4 stages: 1. Rename bound variables in given expression e. 2. Assign type variables to all sub-expressions of e. 3. Construct type equations. 4. Solve the equations. To define it formally we need to develop formal definitions for type-substitution and unifiers.

Type Substitution A type substitution s o is a mapping from a finite set of type variables to a finite set of type expressions s : TypeVars -> TypeExpr o such that s(t) does not refer to T. Substitutions are written using set notations o s : {T1=Boolean, T2=[Number->Boolean]} o s' :{T1=Number, T2=[[Number->T3]->T3]} o s" :{T1=Number, T2=[[Number->T3]->T2]}

Substitution Application Given a type expression T and a substitution s, the notation T s (or just Ts) denotes the application of s to T. The application T s consistently and simultaneously replaces all occurrences of type variables Ti in T by their mapped type expressions s(ti). E.g. [[T1->T2]->T2] {T1=Boolean, T2=[T3->T3]} = [[Boolean->[T3->T3]] -> [T3->T3]]

Type Instance / More General We say that a type expression T' is an instance of a type expression T, if there is a type substitution s such that T s = T'. T is more general than T', if T' is an instance of T. The following type expressions are instances of [T -> T]: o [Number -> Number] = [T->T] o {T = Number} o [Symbol -> Symbol] = [T->T] {T = Symbol} o [[Number->Number] -> [Number->Number]] = [T->T] {T = [Number->Number]} o [[Number->T1] -> [Number->T1]] = [T->T] {T = [Number->T1]}

Composition of Type Substitution The composition of type-substitutions s1 and s2, denoted s1 s2, is an operation that either results in a type-substitution, or fails. Intuitively, it adds the information on type variables in s2 to the information we already have in s1. Formally, it is defined as follows: 1. If type variable T is defined in both s1 and s2 then if there is a contradiction between s2(t) and s1(t) then composition fails, otherwise, if s2(t) is an instance of s1(t) then T is removed from s1 else it is removed from s2. 2. s2 is applied to the RHS of the type-expressions of s1 3. The (possibly modified) s2 is added to the (possibly modified) s1 4. Identity bindings, i.e., s2(t) = T, are removed. 5. If for some variable, (s1 s2)(t) refers to T, the composition fails.

Composition of Type Substitution For example, {T1=Number, T2=[[Number->T3] -> T3]} {T3=Boolean} = {T1=Number, T2 = [[Number->Boolean]->Boolean], T3 = Boolean} {T1=[T->T], T2=[[Number->T3] -> T1]} {T3=Boolean, T1=[Number->Number]} = {T1 = [Number->Number], T2 = [[Number->Boolean]->[Number->Number]], T3 = Boolean} {T1=[T->T], T2=[[Number->T3] -> T1]} {T3=Boolean, T1=Boolean} = fails

Renaming Renaming is the operation of consistent renaming of type variables within a type expression, by new type symbols, that do not occur in the type expression. Renamed type expressions are equivalent: o [[T1 -> T2]*T1 -> T2] [[S1 -> T2] * S1 -> T2] o [[T1 -> T2]*T1 -> T2] [[S1 -> S2] * S1 -> S2] o [[T1 -> T2]*T1 -> T2] [[T1 -> T2] * S2 -> T2] The variables in the substituting expressions should be new. o [[T1 -> T2]*T1 -> T2] [[T2 -> T2] * T2 -> T2] o [[T1 -> T2]*T1 -> T2] [[[T1->T2] -> T2] * [T1->T2] -> T2]

Unification & Unifier Defs. Unification is an operation that makes type expressions identical by application of the same type substitution to both expressions. When such a substitution can be found it is called a unifier of the two expressions. Let Te1 = [S * [Number -> S] -> S] Te2 = [Pair(T1) * T2 -> T3] Can you find a substitution that will unify them? Yes! TeU = {S=Pair(T1), T2=[Number->S], T3=Pair(T1)} is a unifier for Te1 & Te2. Proof: Te1 o TeU = [Pair(T1) * [Number->Pair(T1)] -> Pair(T1)] Te2 o TeU = [Pair(T1) * [Number->Pair(T1)] -> Pair(T1)]

Unification Ex. Let Te3 = [S * [Number -> S1] -> S] Te4 = [Pair(T1) * [T1 -> T1] -> T2] o Te3 & Te4 are unifiable by TeU = {S=Pair(Number), T2=Pair(Number), T1=Number, S1 = Number} Let Te5 = [S * [Number -> S] -> S] Te6 = [Pair(T1) * [T1 -> T1] -> T2] o Te5 & Te6 are not unifiable because we need to resolve the equalities S = Pair(T1) S = T2 [Number -> S] = [T1 -> T1] hence S = T1 which is not compatible with T1 = Pair(T1)

Most General Unifier (MGU) Unifiable type expressions can be unified by multiple unifiers. For example, Te7 = [S * S -> S] and Te8 = [Pair(T1) * T2 -> T2] are unifiable by the unifiers: TeU1 = {S=Pair(T1), T2=Pair(T1)} TeU2 = {S=Pair(Number), T2=Pair(Number)} TeU3 = {S=Pair(Boolean), T2=Pair(Boolean)} etc. Which is more general than the others? The unifier TeU1 is the most general unifier since it substitutes only the necessary type variables, without making additional assumptions about the replaced terms. All other unifiers are obtained from it by application of additional substitutions.

Most General Unifier (MGU) The most general unifier is unique, up to consistent renaming. It is called the most general unifier (mgu) of the two type expressions. The function unify(te1, TE2) returns o the mgu of TE1 and TE2 if it can be found and o false otherwise (indicating that TE1 and TE2 cannot be unified). We will use this function in our algorithm.

Type Inference w. Equations Step by Step Let s run the type inference algorithm step by step, on the following expression: (lambda (f g) (lambda (x) (f (+ x (g 3))))) First, let s consider it with type annotations: (lambda ([f : Tf] [g : Tg]) : T1 (lambda ([x : Tx]) : T2 (f (+ x (g 3)))))

Stage 1 : Renaming Not needed in this example, because all declared variables already have distinct names

Stage 2: Assign Type Variables Expression Variable (lambda (f g) (lambda (x) (f (+ x (g 3))))) T0 (lambda (x) (f (+ x (g 3)))) T1 (f (+ x (g 3))) T2 f Tf (+ x (g 3)) T3 + T+ x Tx (g 3) T4 g Tg 3 Tnum3

Stage 2: Assign Type Variables Expression Variable (lambda (f g) (lambda (x) (f (+ x (g 3))))) T0 (lambda (x) (f (+ x (g 3)))) T1 (f (+ x (g 3))) T2 f Tf (+ x (g 3)) T3 + T+ x Tx (g 3) T4 g Tg 3 Tnum3 (lambda (f g) (lambda (x) (f (+ x (g 3))))) T0 T1 T2 T3 T4

Stage 3: Construct type Equations We construct type equations involving the type variables we have defined. The type equations are constructed according to the corresponding typing axioms or typing inference rules. We thus start with equations for the atoms. o In our example, this adds two equations: Eq1: Tnum3 = Number Eq2: T+ = [Number * Number -> Number] (lambda (f g) (lambda (x) (f (+ x (g 3))))) T0 T1 T2 T3 T4

Stage 3: Construct type Equations What equations do procedure definitions create? For expression E = (lambda (v1...vn) e1... em) with n > 0, construct the equation: T E = [T v1 *... * T vn -> T em ]. For expression E = (lambda ( ) e1... em) construct the equation: T E = [Empty -> T em ].

Stage 3: Construct type Equations In our example we will create 2 equations following the inference rule for procedure definition: For E0 = (lambda (f g) (lambda (x) (f (+ x (g 3))))) we create Eq3: T0 = [Tf * Tg -> T1] For E1 = (lambda (x) (f (+ x (g 3)))) we create Eq4: T1 = [Tx -> T2] (lambda (f g) (lambda (x) (f (+ x (g 3))))) T0 T1 T2 T3 T4

Stage 3: Construct type Equations What equations do procedure applications create? For expression ae = (f e1... en) with n > 0, construct the equation: T f = [T e1 *... * T en -> T ae ]. For expression E = (f) construct the equation: T f = [Empty -> T ae ].

Stage 3: Construct type Equations In our example we will create 3 equations following the inference rule for procedure application: For E2 = (f (+ x (g 3))) we create Eq5: Tf = [T3-> T2] For E3 = (+ x (g 3)) we create Eq5: T+ = [Tx * T4 -> T3] For E4 = (g 3) we create Eq6: Tg = [Tnum3 -> T4] (lambda (f g) (lambda (x) (f (+ x (g 3))))) T0 T1 T2 T3 T4

Stage 3: Construct type Equations Equation Expression Tnum3 = Number 3 T+ = Number * Number -> Number + T0 = [Tf * Tg -> T1] E0 T1 = [Tx -> T2] E1 Tf = [T3 -> T2] E2 T+ = [Tx * T4 -> T3] E3 Tg = [Tnum3-> T4] E4 (lambda (f g) (lambda (x) (f (+ x (g 3))))) T0 T1 T2 T3 T4

Stage 4 : Solve the Equations Idea: The equations are solved by gradually producing typesubstitutions for all type variables. The solution is processed by considering the equations one by one. For an expression e, the algorithm infers a type t if the final type-substitution maps its variable Te to t. If an expression e has an inferred type then all of its sub-expressions have an inferred type as well. Circular type-substitution causes failure. If the procedure outputs FAIL then either there is a type error or the constructed type equations are too weak.

Alg. for Solving Equations Input: Equations - a set of type equations Output: A type substitution or FAIL Initialization: substitution := {} Repeat for each equation [te1 = te2] in Equations : 1. Apply the current substitution to the equation (replace vars by their substituting expressions). te1 = te1 substitution te2 = te2 substitution equation := [ te1 = te2 ] 2. Both sides of the eq. te1 and te2 are atomic types? If te1 te2 output FAIL. Else, do nothing. 3. One side of the eq. te1 or te2 is a variable? Say, te1 = T. Apply the substitution to the equation: equation = [ T = te2 ] Add the equation to the substitution: substitution := substitution {T = te2 } 4. A circular substitution occurred? Output FAIL. 5. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of Equations Return substitution

Stage 4 : Solve the Equations Let s apply the algorithm to the set of equations we got from Stage 3 Equations Repeat Return 1. Apply 2. Both atomic types? 3. One a variable? 4. Circular? 5. Both composite of same type? T0 = [Tf * Tg -> T1] T1 = [Tx -> T2] Tf = [T3 -> T2] T+ = [Tx * T4 -> T3] Tg = [Tnum3-> T4] Tnum3 = Number T+ = Number * Number -> Number

Stage 4 : Solve the Equations Equations Substitution 1: T0 = [Tf * Tg -> T1] {} 2: T1 = [Tx -> T2] 3: Tf = [T3 -> T2] 4: T+ = [Tx * T4 -> T3] 5: Tg = [Tnum3-> T4] 6: Tnum3 = Number 7: T+ = Number * Number -> Number Equations Substitution 2: T1 = [Tx -> T2] {T0 = [Tf * Tg -> T1]} 3: Tf = [T3 -> T2] 4: T+ = [Tx * T4 -> T3] 5: Tg = [Tnum3-> T4] 6: Tnum3 = Number 7: T+ = Number * Number -> Number

Stage 4 : Solve the Equations Equations Substitution 2: T1 = [Tx -> T2] {T0 = [Tf*Tg -> T1]} 3: Tf = [T3 -> T2] 4: T+ = [Tx * T4 -> T3] 5: Tg = [Tnum3-> T4] 6: Tnum3 = Number 7: T+ = Number * Number -> Number Equations Substitution 3: Tf = [T3 -> T2] {T0 = [Tf*Tg -> [Tx->T2]], 4: T+ = [Tx * T4 -> T3] T1 = [Tx -> T2] } 5: Tg = [Tnum3-> T4] 6: Tnum3 = Number 7: T+ = Number * Number -> Number

Stage 4 : Solve the Equations Equations Substitution 3: Tf = [T3 -> T2] {T0 = [Tf * Tg -> [Tx->T2]], 4: T+ = [Tx * T4 -> T3] T1 = [Tx -> T2] } 5: Tg = [Tnum3-> T4] 6: Tnum3 = Number 7: T+ = Number * Number -> Number Equations Substitution 4: T+ = [Tx * T4 -> T3] {T0 = [[T3->T2] * Tg -> [Tx->T2]], 5: Tg = [Tnum3-> T4] T1 = [Tx -> T2], 6: Tnum3 = Number Tf = [T3 -> T2]} 7: T+ = Number * Number -> Number

Stage 4 : Solve the Equations Equations Substitution 4: T+ = [Tx * T4 -> T3] {T0 = [[T3->T2] * Tg -> [Tx->T2]], 5: Tg = [Tnum3-> T4] T1 = [Tx -> T2], 6: Tnum3 = Number Tf = [T3 -> T2]} 7: T+ = Number * Number -> Number Equations Substitution 5: Tg = [Tnum3-> T4] {T0 = [[T3->T2] * Tg -> [Tx->T2]], 6: Tnum3 = Number T1 = [Tx -> T2], 7: T+ = Number * Number -> Number Tf = [T3 -> T2], T+ = [Tx * T4 -> T3]}

Stage 4 : Solve the Equations Equations Substitution 5: Tg = [Tnum3 -> T4] {T0 = [[T3->T2] * Tg -> [Tx->T2]], 6: Tnum3 = Number T1 = [Tx -> T2], 7: T+ = Number * Number -> Number Tf = [T3 -> T2], T+ = [Tx * T4 -> T3]} Equations Substitution 6: Tnum3 = Number {T0 = [[T3->T2]*[Tnum3->T4]->[Tx->T2]], 7: T+ = Number * Number -> Number T1 = [Tx -> T2], Tf = [T3 -> T2], T+ = [Tx * T4 -> T3], Tg = [Tnum3-> T4]}

Stage 4 : Solve the Equations Equations Substitution 6: Tnum3 = Number {T0 = [[T3->T2]*[Tnum3->T4]->[Tx->T2]], 7: T+ = Number * Number -> Number T1 = [Tx -> T2], Tf = [T3 -> T2], T+ = [Tx * T4 -> T3], Tg = [Tnum3-> T4]} Equations Substitution 7: T+ = Number * Number -> Number {T0 = [[T3->T2]*[Number->T4]->[Tx->T2]], T1 = [Tx -> T2], Tf = [T3 -> T2], T+ = [Tx * T4 -> T3], Tg = [Number -> T4], Tnum3 = Number}

Stage 4 : Solve the Equations Equations Substitution 7: T+ = Number * Number -> Number {T0 = [[T3->T2]*[Number->T4]->[Tx->T2]], T1 = [Tx -> T2], Tf = [T3 -> T2], 7: Tx * T4 -> T3 = Number * Number -> Number T+ = [Tx * T4 -> T3], Tg = [Number -> T4], Tnum3 = Number} Equations Substitution 8: Tx = Number {T0 = [[T3->T2]*[Number->T4]->[Tx->T2]], 9: T4 = Number T1 = [Tx -> T2], 10: T3 = Number Tf = [T3 -> T2], T+ = [Tx * T4 -> T3], Tg = [Number -> T4], Tnum3 = Number}

Stage 4 : Solve the Equations Equations Substitution 8: Tx = Number {T0 = [[T3->T2]*[Number->T4]->[Tx->T2]], 9: T4 = Number T1 = [Tx -> T2], 10: T3 = Number Tf = [T3 -> T2], T+ = [Tx * T4 -> T3], Tg = [Number -> T4], Tnum3 = Number} Equations Substitution {T0 = [[Number ->T2]*[Number-> Number]->[Number ->T2]], T1 = [Number -> T2], Tf = [Number -> T2], T+ = [Number * Number -> Number], Tg = [Number -> Number], Tnum3 = Number, Tx = Number, T4 = Number, T3 = Number}

Completing the Ex. {T0 = [[Number ->T2]*[Number-> Number]->[Number ->T2]], T1 = [Number -> T2], Tf = [Number -> T2], T+ = [Number * Number -> Number], Tg = [Number -> Number], Tnum3 = Number, Tx = Number, T4 = Number, T3 = Number} Recall that we started with the following expression (lambda ([f : Tf] [g : Tg]) : T1 (lambda ([x : Tx]) : T2 (f (+ x (g 3))))) We can now use the resulting substitution to fully annotate it (lambda ([f : [Number -> T2]] [g : [Number -> Number]]) : [Number -> T2] (lambda ([x : Number]) : T2 (f (+ x (g 3)))))