Type Inference using Type Constraints

Slides:



Advertisements
Similar presentations
Type Inference David Walker COS 320. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Advertisements

Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
1 Chapter 4:Constraint Logic Programs Where we learn about the only programming concept rules, and how programs execute.
Letrec fact(n) = if zero?(n) then 1 else *(n, (fact sub1(n))) 4.4 Type Inference Type declarations aren't always necessary. In our toy typed language,
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Type checking © Marcelo d’Amorim 2010.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
1 Constraint Programming Maurizio Gabbrielli Universita’ di Bologna Slides by: K. Marriott.
Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction.
1 Chapter 1: Constraints What are they, what do they do and what can I use them for.
Type Inference David Walker COS 441. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Type Inference David Walker CS 510, Fall Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs.
Finding angles with algebraic expressions
Solving Absolute Value Equations
LINEAR EQUATION IN TWO VARIABLES. System of equations or simultaneous equations – System of equations or simultaneous equations – A pair of linear equations.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
MCA 202: Discrete Mathematics Instructor Neelima Gupta
PPL Static Verification: Type Inference Lecture Notes: Chapter 2.
Type checking and inference Applications of typing axioms / rules are replaced with type equations. A solution to the equations assigns types for every.
Solving Equations by Adding or Subtracting Objective: Solve equations using the addition and subtraction properties of equalities.
Solving Multi- Step Equations. And we don’t know “Y” either!!
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
PPL Applicative and Normal Form Verification, Type Checking and Inference.
7.4. 5x + 2y = 16 5x + 2y = 16 3x – 4y = 20 3x – 4y = 20 In this linear system neither variable can be eliminated by adding the equations. In this linear.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Daniel Kroening and Ofer Strichman Decision Procedures An Algorithmic Point of View Deciding Combined Theories.
Algorithm Analysis (Time complexity). Software development cycle -Four phases: 1.Analysis 2.Design Algorithm Design an algorithm to solve the problem.
Systems of Equations By Dr. Marinas. Solving Systems Graphing Method Substitution Method Elimination (or Adding) Method.
Notes 6.5, Date__________ (Substitution). To solve using Substitution: 1.Solve one equation for one variable (choose the variable with a coefficient of.
Type checking and inference Question 2: Typing the application (if #t (+ 1 2) 3) STAGE-I: Renaming ExpressionVariable (if #t (+ 1 2) 3) T0 (+ 1 2) T1 +
* Collect the like terms 1. 2a = 2a x -2x + 9 = 6x z – – 5z = 2z - 6.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Leftovers: LET Syntax & Formal Semantics Static Verification: Type Inference Lecture Notes: Chapter 2.
Lesson 10 Type Reconstruction
Operational Semantics of Scheme
What are they, what do they do and what can I use them for.
Type Correctness.
Equations Quadratic in form factorable equations
Solve Systems of Equations by Elimination
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Env. Model Implementation
Variables on Both Sides with Equations
Type checking and inference
Solving Systems using Substitution
6-3 Solving Systems Using Elimination
EQ: How do I solve an equation in one variable?
2 Understanding Variables and Solving Equations.
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
Practice session #4: Static Type Correctness Algorithms:
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
3.6 Interpreter: Recursion
Solving 1-Step Integer Equations
Solving systems using substitution
Solving Equations 3x+7 –7 13 –7 =.
Solving Equations with Variables on Both Sides
Solving Absolute Value Equations
Solving Equations with Variables on Both Sides
Assignments and Procs w/Params
Completing the Square.
Equations Quadratic in form factorable equations
Solving Absolute Value Equations
Solving Absolute Value Equations
Solving Absolute Value Equations
Solving Absolute Value Equations
3.2 Solving Linear Systems Algebraically
One-step addition & subtraction equations: fractions & decimals
Solving Absolute Value Equations
Presentation transcript:

Type Inference using Type Constraints שפה שהינה typed, היא שפה התומכת בסמנטיקה שלה בטיפוסים. היא מאפשרת לייחס טיפוסים שונים לערכים ולמבנים של ערכים. כל השפות הפרקטיות הן כאלה. שפות שהן typed כוללות גם אוסף של חוקים לפיהם ניתן לשלב טיפוסים באופן נכון. המטרה של בדיקת / הסקת טיפוסים היא להבטיח שהשימוש בטיפוסים בשפה הוא נכון ולמנוע את החישוב במקרה שלא. Scheme היא fully typed, בעלת un-typed syntax ותהליך הסקת הטיפוסים בה נעשה תוך כדי תהליך החישוב. כלומר, היא dynamically typed.

Type Inference using Type Constraints Applications of typing axioms / rules are replaced with type equations. A 3-stages algorithm. Given an expression, e: Rename bound variables in e. Assign type variables to all sub-expressions of e. Construct type equations. Solve the equations. Question 1: Typing the expression ((lambda (f1 x1) (f1 x1)) sqrt 4) Exp: ((lambda ([f1:[N->N]] [x1:N]):N (f1 x1)) sqrt 4) STAGE-I: Rename bound variables ((lambda (f x) (f x)) sqrt 4) STAGE-II: Assign type variables to all sub-exps Expression Var ((lambda(f x) (f x)) sqrt 4) T0 (lambda(f x) (f x)) T1 (f x) T2 f Tf x Tx sqrt Tsqrt 4 Tnum4

Type Inference using Type Constraints Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-III: Construct type equations. For atomic exps / primitive-procs, construct equations using their types. For (lambda (v1 . . . vn) e1 . . .em), construct: T(lambda(v1…vn) e1…en) =[Tv1*…*Tvn -> Ten] For (f e1 . . .en), construct: Tf =[Te1*…*Ten -> T(f e1 … en)] Expression Equation ((lambda (f x) (f x)) sqrt 4) T1 = [Tsqrt*Tnum4→T0] (lambda (f x) (f x)) T1 = [Tf*Tx→T2] Sqrt Tsqrt = [Number → Number] 4 Tnum4 = Number (f x) Tf = [Tx→T2] Expression Var ((lambda(f x) (f x)) sqrt 4) T0 (lambda(f x) (f x)) T1 (f x) T2 f Tf x Tx sqrt Tsqrt 4 Tnum4

Type Inference using Type Constraints Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 1. T1 =[Tsqrt*Tnum4→T0] 2. T1 =[Tf*Tx→T2] 3. Tf =[Tx→T2] 4. Tsqrt = [N → N] 5. Tnum4 =N Equation 1: Step 1: the empty sub is applied to Eq1. Eq1 is applied to the sub.

Type Inference using Type Constraints Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 2. T1 =[Tf*Tx→T2] T1:=[Tsqrt*Tnum4→T0] 3. Tf =[Tx→T2] 4. Tsqrt = [N → N] 5. Tnum4 =N Equation 1: Step 1: the empty sub is applied to Eq1. Eq1 is applied to the sub.

Type Inference using Type Constraints Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 2. T1:=[Tf*Tx→T2] T1:=[Tsqrt*Tnum4→T0] 3. Tf:=[Tx→T2] 4. Tsqrt := [N → N] 5. Tnum4:=N Equation 2: Step 1: The sub is applied to Eq2: [Tf* Tx -> T2] = [Tsqrt* Tnum4-> T0] Step 5: Equations are split and Eq2 is removed.

Type Inference using Type Constraints Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 2. T1:=[Tf*Tx→T2] T1:=[Tsqrt*Tnum4→T0] 3. Tf:=[Tx→T2] 4. Tsqrt := [N → N] 5. Tnum4:=N 6. Tf:=Tsqrt 7. Tx:=Tnum4 8. T2:=T0 Equation 2: Step 1: The sub is applied to Eq2: [Tf* Tx -> T2] = [Tsqrt* Tnum4-> T0] Step 5: Equations are split and Eq2 is removed.

Type Inference using Type Constraints Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 3. Tf:=[Tx→T2] T1:=[Tsqrt*Tnum4→T0] 4. Tsqrt := [N → N] 5. Tnum4:=N 6. Tf:=Tsqrt 7. Tx:=Tnum4 8. T2:=T0 Equation 3: Step 1: the sub is applied to Eq3. No change. Eq3 is applied to the substitution.

Type Inference using Type Constraints Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 4. Tsqrt := [N → N] T1:=[Tsqrt*Tnum4→T0] 5. Tnum4:=N 3. Tf:=[Tx→T2] 6. Tf:=Tsqrt 7. Tx:=Tnum4 8. T2:=T0 Equation 3: Step 1: the sub is applied to Eq3. No change. Eq3 is applied to the substitution.

Type Inference using Type Constraints Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 4. Tsqrt := [N → N] T1:=[Tsqrt*Tnum4→T0] 5. Tnum4:=N Tf:=[Tx→T2] 6. Tf:=Tsqrt 7. Tx:=Tnum4 8. T2:=T0 Equation 4: Step 1: the sub is applied to Eq4. No change. Eq4 is applied to the substitution: Occurrences of Tsqrt are substituted by [N→N]

Type Inference using Type Constraints Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 5. Tnum4:=N T1:=[[N → N]*Tnum4→T0] 6. Tf:=Tsqrt Tf:=[Tx→T2] 7. Tx:=Tnum4 Tsqrt := [N → N] 8. T2:=T0 Equation 4: Step 1: the sub is applied to Eq4. No change. Eq4 is applied to the substitution: Occurrences of Tsqrt are substituted by [N→N]

Type Inference using Type Constraints Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 5. Tnum4:=N T1:=[[N → N]*Tnum4→T0] 6. Tf:=Tsqrt Tf:=[Tx→T2] 7. Tx:=Tnum4 Tsqrt := [N → N] 8. T2:=T0 Equation 5: Step 1: the sub is applied to Eq5. No change. Eq5 is applied to the substitution: Occurrences of Tnum4 are substituted by N

Type Inference using Type Constraints Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 6. Tf:=Tsqrt T1:=[[N → N]*N→T0] 7. Tx:=Tnum4 Tf:=[Tx→T2] 8. T2:=T0 Tsqrt := [N → N] Tnum4:=N Equation 5: Step 1: the sub is applied to Eq5. No change. Eq5 is applied to the substitution: Occurrences of Tnum4 are substituted by N

Type Inference using Type Constraints Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 6. Tf:=Tsqrt T1:=[[N → N]*N→T0] 7. Tx:=Tnum4 Tf:=[Tx→T2] 8. T2:=T0 Tsqrt := [N → N] Tnum4:=N Equation 6: Step 1: the sub is applied to Eq6, Tf and Tsqrt are substituted: [Tx→T2]=[N → N] Step 5: Equations are split and Eq6 is removed.

Type Inference using Type Constraints Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 6. Tf:=Tsqrt T1:=[[N → N]*N→T0] 7. Tx:=Tnum4 Tf:=[Tx→T2] 8. T2:=T0 Tsqrt := [N → N] 9. Tx:=N Tnum4:=N 10. T2:=N Equation 6: Step 1: the sub is applied to Eq6, Tf and Tsqrt are substituted: [Tx→T2]=[N → N] Step 5: Equations are split and Eq6 is removed.

Type Inference using Type Constraints Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 7. Tx:=Tnum4 T1:=[[N → N]*N→T0] 8. T2:=T0 Tf:=[Tx→T2] 9. Tx:=N Tsqrt := [N → N] 10. T2:=N Tnum4:=N Equation 7: Step 1: Tnum4 is substituted: Tx=N Step 3: Eq7 is applied to the substitution: Any occurrence of Tx is substituted for N

Type Inference using Type Constraints Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 7. Tx:=N T1:=[[N → N]*N→T0] 8. T2:=T0 Tf:=[Tx→T2] 9. Tx:=N Tsqrt := [N → N] 10. T2:=N Tnum4:=N Equation 7: Step 1: Tnum4 is substituted: Tx=N Step 3: Eq7 is applied to the substitution: Any occurrence of Tx is substituted for N

Type Inference using Type Constraints Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 8. T2:=T0 T1:=[[N → N]*N→T0] 9. Tx:=N Tf:=[N→T2] 10. T2:=N Tsqrt := [N → N] Tnum4:=N Tx:=N Equation 7: Step 1: Tnum4 is substituted: Tx=N Step 3: Eq7 is applied to the substitution: Any occurrence of Tx is substituted for N

Type Inference using Type Constraints Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 8. T2:=T0 T1:=[[N → N]*N→T0] 9. Tx:=N Tf:=[N→T2] 10. T2:=N Tsqrt := [N → N] Tnum4:=N Tx:=N Equation 8: step 1: the sub is applied to Eq8. No change. Step 3: Eq8 is applied to the sub: Any occurrence of T2 is substituted for T0

Type Inference using Type Constraints Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 9. Tx:=N T1:=[[N → N]*N→T0] 10. T2:=N Tf:=[N→T0] Tsqrt := [N → N] Tnum4:=N Tx:=N T2:=T0 Equation 8: step 1: the sub is applied to Eq8. No change. Eq8 is applied to the sub: Any occurrence of T2 is substituted for T0

Type Inference using Type Constraints Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 9. Tx:=N T1:=[[N → N]*N→T0] 10. T2:=N Tf:=[N→T0] Tsqrt := [N → N] Tnum4:=N Tx:=N T2:=T0 Equation 9: Step 1: Tx is substituted: N=N Step 2: Eq9 is removed.

Type Inference using Type Constraints Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 9. N:=N T1:=[[N → N]*N→T0] 10. T2:=N Tf:=[N→T0] Tsqrt := [N → N] Tnum4:=N Tx:=N T2:=T0 Equation 9: Step 1: Tx is substituted: N=N Step 2: Eq9 is removed.

Type Inference using Type Constraints Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 10. T2:=N T1:=[[N → N]*N→T0] Tf:=[N→T0] Tsqrt := [N → N] Tnum4:=N Tx:=N T2:=T0 Equation 10: step 1: T2 is substituted: T0=N Step 3: Eq10 is applied to the sub: Any occurrence of T0 is substituted for N

Type Inference using Type Constraints Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 10. T0:=N T1:=[[N → N]*N→T0] Tf:=[N→T0] Tsqrt := [N → N] Tnum4:=N Tx:=N T2:=T0 Equation 10: step 1: T2 is substituted: T0=N Step 3: Eq10 is applied to the sub: Any occurrence of T0 is substituted for N

Type Inference using Type Constraints Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution T1:=[[N → N]*N→N] Tf:=[N→N] Tsqrt := [N → N] Tnum4:=N Tx:=N T2:=N T0:=N Equation 10: step 1: T2 is substituted: T0=N Step 3: Eq10 is applied to the sub: Any occurrence of T0 is substituted for N

Type Inference using Type Constraints Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution T1:=[[N → N]*N→N] Tf:=[N→N] Tsqrt := [N → N] Tnum4:=N Tx:=N T2:=N T0:=N Equation 10: step 1: T2 is substituted: T0=N Step 3: Eq10 is applied to the sub: Any occurrence of T0 is substituted for N The inference succeeds! The type of T0 is Number Exp: ((lambda ([f1:[N->N]] [x1:N]):N (f1 x1)) sqrt 4)

Type Inference using Type Constraints Question 2: Extending the typing mechanism for if expressions. Recall the typing rule for if expressions (if p c a): For every: type environment _Tenv, expressions _e1, _e2, _e3, and type expressions _S1, _S2: if _Tenv |- _e1:Boolean, _Tenv |- _e2:_S2, _Tenv |- _e3:_S2, Then _Tenv |- (if _e1 _e2 _e3):_S2 Observations: P is of type Boolean c and a have the same type: _S2. This is also the type for the entire if expression. What would be the equation for if in the constraints approach? For the expression (if p c a), construct: Tc=Ta, Tif =Tc

Type Inference using Type Constraints Example: Typing the expression (if #t (+ 1 2) 3) Exp: Typing the expression (if [#t:T#t] [(+ 1 2):T1] [3:Tnum3]):T0 STAGE-I: Rename bound variables Not needed, no references of variables. STAGE-II: Assign type variables to all sub-exps Expression Var (if #t (+ 1 2) 3) T0 (+ 1 2) T1 + T+ #t T#t 1 Tnum1 2 Tnum2 3 Tnum3

Type Inference using Type Constraints Example: Typing the expression (if #t (+ 1 2) 3) STAGE-III: Construct type equations. Expression Equation (if #t (+ 1 2) 3) T1=Tnum3 T0 = T1 (+ 1 2) T+=[Tnum1*Tnum2->T1] + T+->[N*N->N] #t T#t = Boolean 1 Tnum1 = Number 2 Tnum2 = Number 3 Tnum3 = Number For atomic exps / primitive-procs, construct equations using their types. For (lambda (v1 . . . vn) e1 . . .em), construct: T(lambda(v1…vn) e1…en) =[Tv1*…*Tvn -> Ten] For (f e1 . . .en), construct: Tf =[Te1*…*Ten -> T(f e1 … en)] For (if p c a), construct: Tc=Ta, Tif =Tc Expression Var (if #t (+ 1 2) 3) T0 (+ 1 2) T1 + T+ #t T#t 1 Tnum1 2 Tnum2 3 Tnum3

Type Inference using Type Constraints Example: Typing the expression (if #t (+ 1 2) 3) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 1. T1=Tnum3 2. T0 = T1 3. T+=[Tnum1*Tnum2->T1] 4. T+->[N*N->N] 5. T#t = B 6. Tnum1 = N 7. Tnum2 = N 8. Tnum3 = N Equation 1: Step 1: the sub is applied to Eq1. Eq1 is applied to the sub.

Type Inference using Type Constraints Example: Typing the expression (if #t (+ 1 2) 3) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 2. T0 = T1 1. T1=Tnum3 3. T+=[Tnum1*Tnum2->T1] 4. T+->[N*N->N] 5. T#t = B 6. Tnum1 = N 7. Tnum2 = N 8. Tnum3 = N Equation 1: Step 1: the sub is applied to Eq1. Eq1 is applied to the sub.

Type Inference using Type Constraints Example: Typing the expression (if #t (+ 1 2) 3) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 2. T0 = T1 1. T1=Tnum3 3. T+=[Tnum1*Tnum2->T1] 4. T+->[N*N->N] 5. T#t = B 6. Tnum1 = N 7. Tnum2 = N 8. Tnum3 = N Equation 2: Step 1: T1 is substituted: T0 = Tnum3 Step3: Eq2 is applied to the sub.

Type Inference using Type Constraints Example: Typing the expression (if #t (+ 1 2) 3) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 2. T0 = Tnum3 1. T1=Tnum3 3. T+=[Tnum1*Tnum2->T1] 4. T+->[N*N->N] 5. T#t = B 6. Tnum1 = N 7. Tnum2 = N 8. Tnum3 = N Equation 2: Step 1: T1 is substituted: T0 = Tnum3 Step3: Eq2 is applied to the sub.

Type Inference using Type Constraints Example: Typing the expression (if #t (+ 1 2) 3) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 3. T+=[Tnum1*Tnum2->T1] 1. T1=Tnum3 4. T+->[N*N->N] 2. T0 = Tnum3 5. T#t = B 6. Tnum1 = N 7. Tnum2 = N 8. Tnum3 = N Equation 2: Step 1: T1 is substituted: T0 = Tnum3 Step3: Eq2 is applied to the sub.

Type Inference using Type Constraints Example: Typing the expression (if #t (+ 1 2) 3) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 3. T+=[Tnum1*Tnum2->T1] 1. T1=Tnum3 4. T+->[N*N->N] 2. T0 = Tnum3 5. T#t = B 6. Tnum1 = N 7. Tnum2 = N 8. Tnum3 = N Equation 3: Step 1: The sub is applied to Eq3: T+ = [Tnum1*Tnum2->Tnum3] Step3: Eq3 is applied to the sub.

Type Inference using Type Constraints Example: Typing the expression (if #t (+ 1 2) 3) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 3. T+=[Tnum1*Tnum2->Tnum3] 1. T1=Tnum3 4. T+->[N*N->N] 2. T0 = Tnum3 5. T#t = B 6. Tnum1 = N 7. Tnum2 = N 8. Tnum3 = N Equation 1: Step 3: The sub is applied to Eq3: T+ = [Tnum1*Tnum2->Tnum3] Step3: Eq3 is applied to the sub.

Type Inference using Type Constraints Example: Typing the expression (if #t (+ 1 2) 3) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 4. T+->[N*N->N] 1. T1=Tnum3 5. T#t = B 2. T0 = Tnum3 6. Tnum1 = N 3. T+=[Tnum1*Tnum2->Tnum3] 7. Tnum2 = N 8. Tnum3 = N Equation 1: Step 3: The sub is applied to Eq3: T+ = [Tnum1*Tnum2->Tnum3] Step3: Eq3 is applied to the sub.

Type Inference using Type Constraints Example: Typing the expression (if #t (+ 1 2) 3) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 4. T+->[N*N->N] 1. T1=Tnum3 5. T#t = B 2. T0 = Tnum3 6. Tnum1 = N 3. T+=[Tnum1*Tnum2->Tnum3] 7. Tnum2 = N 8. Tnum3 = N Equation 4: Step 1: The sub is applied to Eq4: [Tnum1*Tnum2->Tnum3] = [N * N -> N] Step5: Eq4 is split into existing equations, then removed.

Type Inference using Type Constraints Example: Typing the expression (if #t (+ 1 2) 3) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 4. T+->[N*N->N] 1. T1=Tnum3 5. T#t = B 2. T0 = Tnum3 6. Tnum1 = N 3. T+=[Tnum1*Tnum2->Tnum3] 7. Tnum2 = N 8. Tnum3 = N Equation 4: Step 1: The sub is applied to Eq4: [Tnum1*Tnum2->Tnum3] = [N * N -> N] Step5: Eq4 is split into existing equations, then removed.

Type Inference using Type Constraints Example: Typing the expression (if #t (+ 1 2) 3) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 5. T#t = B 1. T1=Tnum3 6. Tnum1 = N 2. T0 = Tnum3 7. Tnum2 = N 3. T+=[Tnum1*Tnum2->Tnum3] 8. Tnum3 = N Applying the sub to Eq 5-8 makes no difference. Applying the equations to the sub, we get…

Type Inference using Type Constraints Example: Typing the expression (if #t (+ 1 2) 3) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 1. T1=N 2. T0 = N 3. T+=[N*N->N] 5. T#t = B 6. Tnum1 = N 7. Tnum2 = N 8. Tnum3 = N The inference succeeds The expression is well-typed The type of T0 is Number (if [#t:Boolean] [(+ 1 2):N] [3:N]):N

Type Inference using Type Constraints Question 3: Typing the application ((lambda (f1 x1) (f1 x1)) 4 sqrt) ((lambda ([f1:Tf] [x1:Tx]):T0 (f1 x1)) 4 sqrt) STAGE-I: Rename bound variables ((lambda (f x) (f x)) 4 sqrt) STAGE-II: Assign type variables to all sub-exps Expression Var ((lambda(f x) (f x)) sqrt 4) T0 (lambda(f x) (f x)) T1 (f x) T2 f Tf x Tx 4 Tnum4 sqrt Tsqrt

Type Inference using Type Constraints Question 3 (cont’d): Typing the application ((lambda (f x) (f x)) 4 sqrt) STAGE-III: Construct type equations. For atomic exps / primitive-procs, construct equations using their types. For (lambda (v1 . . . vn) e1 . . .em), construct: T(lambda(v1…vn) e1…en) =[Tv1*…*Tvn -> Ten] For (f e1 . . .en), construct: Tf =[Te1*…*Ten -> T(f e1 … en)] For (if p c a), construct: Tc=Ta, Tif =Tc Expression Equation ((lambda (f x) (f x)) 4 sqrt) T1:=[Tnum4*Tsqrt→T0] (lambda (f x) (f x)) T1:=[Tf*Tx→T2] 4 Tnum4:=N sqrt Tsqrt := [N → N] (f x) Tf:=[Tx→T2] Expression Var ((lambda(f x) (f x)) sqrt 4) T0 (lambda(f x) (f x)) T1 (f x) T2 f Tf x Tx 4 Tnum4 sqrt Tsqrt

Type Inference using Type Constraints Question 3 (cont’d): Typing the application ((lambda (f x) (f x)) 4 sqrt) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 2. T1:=[Tf*Tx→T2] T1:=[Tnum4*Tsqrt→T0] 3. Tf:=[Tx→T2] 4. Tnum4:=N 5. Tsqrt := [N → N] Equation 1: Apply step 1: Initially, the substitution is empty. Eq1 is moved to the substitution.

Type Inference using Type Constraints Question 2 (cont’d): Typing the application ((lambda (f x) (f x)) 4 sqrt) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 2. T1:=[Tf*Tx→T2] T1:=[Tnum4*Tsqrt→T0] 3. Tf:=[Tx→T2] 4. Tnum4:=N 5. Tsqrt := [N → N] Equation 2: step 1: T1 is replaced by current substitution: [Tf* Tx -> T2] = [Tnum4* Tsqrt -> T0] Both side are composite, apply step 5: Equations are split and Eq2 is removed.

Type Inference using Type Constraints Question 2 (cont’d): Typing the application ((lambda (f x) (f x)) 4 sqrt) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 2. T1:=[Tf*Tx→T2] T1:=[Tnum4*Tsqrt→T0] 3. Tf:=[Tx→T2] 4. Tnum4:=N 5. Tsqrt := [N → N] 6. Tf:=Tnum4 7. Tx:=Tsqrt 8. T2:=T0 Equation 2: step 1: T1 is replaced by current substitution: [Tf* Tx -> T2] = [Tnum4* Tsqrt -> T0] Both side are composite, apply step 5: Equations are split and Eq2 is removed.

Type Inference using Type Constraints Question 2 (cont’d): Typing the application ((lambda (f x) (f x)) 4 sqrt) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 3. Tf:=[Tx→T2] T1:=[Tnum4*Tsqrt→T0] 4. Tnum4:=N 5. Tsqrt := [N → N] 6. Tf:=Tnum4 7. Tx:=Tsqrt 8. T2:=T0 Equation 3: step 1: No change. Eq3 is added to the substitution.

Type Inference using Type Constraints Question 2 (cont’d): Typing the application ((lambda (f x) (f x)) 4 sqrt) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 4. Tnum4:=N T1:=[Tnum4*Tsqrt→T0] 5. Tsqrt := [N → N] Tf:=[Tx→T2] 6. Tf:=Tnum4 7. Tx:=Tsqrt 8. T2:=T0 Equation 3: step 1: No change. Eq3 is added to the substitution.

Type Inference using Type Constraints Question 2 (cont’d): Typing the application ((lambda (f x) (f x)) 4 sqrt) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 4. Tnum4:=N T1:=[Tnum4*Tsqrt→T0] 5. Tsqrt := [N → N] Tf:=[Tx→T2] 6. Tf:=Tnum4 7. Tx:=Tsqrt 8. T2:=T0 Equation 4: step 1: No change. Eq4 is added to the substitution: Any occurrence of Tnum4 is substituted by N

Type Inference using Type Constraints Question 2 (cont’d): Typing the application ((lambda (f x) (f x)) 4 sqrt) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 5. Tsqrt := [N → N] T1:=[N*Tsqrt→T0] 6. Tf:=Tnum4 Tf:=[Tx→T2] 7. Tx:=Tsqrt Tnum4:=N 8. T2:=T0 Equation 4: step 1: No change. Eq4 is added to the substitution: Any occurrence of Tnum4 is substituted by N

Type Inference using Type Constraints Question 2 (cont’d): Typing the application ((lambda (f x) (f x)) 4 sqrt) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 5. Tsqrt := [N → N] T1:=[N*Tsqrt→T0] 6. Tf:=Tnum4 Tf:=[Tx→T2] 7. Tx:=Tsqrt Tnum4:=N 8. T2:=T0 Equation 5: step 1: No change. Eq5 is added to the substitution: Any occurrence of Tsqrt is substituted by [N→N]

Type Inference using Type Constraints Question 2 (cont’d): Typing the application ((lambda (f x) (f x)) 4 sqrt) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 6. Tf:=Tnum4 T1:=[N*[N → N]→T0] 7. Tx:=Tsqrt Tf:=[Tx→T2] 8. T2:=T0 Tnum4:=N Tsqrt := [N → N] Equation 5: step 1: No change. Eq5 is added to the substitution: Any occurrence of Tsqrt is substituted by [N→N]

Type Inference using Type Constraints Question 2 (cont’d): Typing the application ((lambda (f x) (f x)) 4 sqrt) STAGE-IV: Solving the equations. For each equation: Apply the current substitution to the equation (replace vars by their substituting expressions). Both sides of the eq. are atomic types? If not equal, output FAIL. Else, do nothing. One side is a variable? Apply the equation to the substitution. Add the equation to the substitution. A circular substitution occurred? Output FAIL. Both side are composite with the same type constructor? Split into equations between corresponding components and add to the set of equations. Equation Substitution 6. Tf:=Tnum4 T1:=[N*[N → N]→T0] 7. Tx:=Tsqrt Tf:=[Tx→T2] 8. T2:=T0 Tnum4:=N Tsqrt := [N → N] Equation 6: step 1: Tf and Tnum4 are substituted: [Tx→T2]=N We get a conflicting equation and neither of the further steps is valid. THE EXPRESSION IS NOT WELL-TYPED.