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.