Download presentation
Presentation is loading. Please wait.
Published byRonald Chandler Modified over 9 years ago
1
Type checking and inference Applications of typing axioms / rules are replaced with type equations. A solution to the equations assigns types for every sub-expression. A 4-stages algorithm. Question 1: Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-I: Renaming ExpressionVar ((lambda(f x) (f x)) sqrt 4) T0T0 (lambda(f x) (f x)) T1T1 (f x)T2T2 fTfTf xTxTx sqrtT sqrt 4T num4 STAGE-II: Assign type variables to all sub-expressions
2
Type checking and inference Using the type constraints approach Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-III: Construct type equations The rules: 1. Atomic expressions / primitive- procedures: Construct equations using their types. 2. Lambda expressions: For (lambda (v 1... v n ) e 1...e m ), construct: 3. Application expressions: For (f e 1...e n ), construct: ExpressionEquation sqrt T sqrt = [Number → Number] 4 T num4 =Number ((lambda (f x) (f x)) sqrt 4)T 1 =[T sqrt *T num4 →T 0 ] (lambda (f x) (f x))T 1 =[T f *T x →T 2 ] (f x)T f =[T x →T 2 ] ExpressionVar ((lambda(f x) (f x)) sqrt 4) T0T0 (lambda(f x) (f x)) T1T1 (f x)T2T2 fTfTf xTxTx sqrtT sqrt 4T num4
3
Type checking and inference Using the type constraints approach Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-III: Construct type equations The rules: 1. Atomic expressions / primitive- procedures: Construct equations using their types. 2. Lambda expressions: For (lambda (v 1... v n ) e 1...e m ), construct: 3. Application expressions: For (f e 1...e n ), construct: ExpressionEquation sqrt T sqrt = [Number → Number] 4 T num4 =Number ((lambda (f x) (f x)) sqrt 4)T 1 =[T sqrt *T num4 →T 0 ] (lambda (f x) (f x))T 1 =[T f *T x →T 2 ] (f x)T f =[T x →T 2 ] ExpressionVar ((lambda(f x) (f x)) sqrt 4) T0T0 (lambda(f x) (f x)) T1T1 (f x)T2T2 fTfTf xTxTx sqrtT sqrt 4T num4
4
Type checking and inference Using the type constraints approach Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-IV: Solving the equations. For each equation: 1.Replace vars by their substituting expressions. 2.Both sides of the eq. are atomic? 1.If equal, ignore equation. 2.Else, output FAIL. 3.Both sides are different vars? 1.Apply the equation to the current substitution. 2.Add the equation to the substitution. 4.A circular substitution occurred? Output FAIL. 5.Both side are composite with the same type constructor? Split into equations between corresponding components. EquationSubstitution 1. T 1 =[T sqrt *T num4 →T 0 ]{ } 2. T 1 =[T f *T x →T 2 ] 3. T f =[T x →T 2 ] 4. T sqrt = [Number → Number] 5. T num4 =Number Equation 1: -Initially, the substitution is empty. Step 1 is ignored. -Eq1 is moved to the substitution.
5
Type checking and inference Using the type constraints approach Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-III: Solving the equations. Equation 1: -Initially, the substitution is empty. Step 1 is ignored. -Eq1 is moved to the substitution. EquationSubstitution 2. T 1 =[T f *T x →T 2 ]T 1 :=[T sqrt *T num4 →T 0 ] 3. T f =[T x →T 2 ] 4. T sqrt = [Number → Number] 5. T num4 =Number For each equation: 1.Replace vars by their substituting expressions. 2.Both sides of the eq. are atomic? 1.If equal, ignore equation. 2.Else, output FAIL. 3.Both sides are different vars? 1.Apply the equation to the current substitution. 2.Add the equation to the substitution. 4.A circular substitution occurred? Output FAIL. 5.Both side are composite with the same type constructor? Split into equations between corresponding components.
6
Type checking and inference Using the type constraints approach Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-III: Solving the equations. EquationSubstitution 2. T 1 =[T f *T x →T 2 ]T 1 :=[T sqrt *T num4 →T 0 ] 3. T f =[T x →T 2 ] 4. T sqrt = [Number → Number] 5. T num4 =Number Equation 2: [T sqrt * T num4 -> T 0 ] = [T f * T x -> T 2 ] -Apply step 1: T1 is replaced by current substitution: [T sqrt * T num4 -> T 0 ] = [T f * T x -> T 2 ] -Both side are composite, apply step 5: Equations are split and Eq2 is removed. For each equation: 1.Replace vars by their substituting expressions. 2.Both sides of the eq. are atomic? 1.If equal, ignore equation. 2.Else, output FAIL. 3.Both sides are different vars? 1.Apply the equation to the current substitution. 2.Add the equation to the substitution. 4.A circular substitution occurred? Output FAIL. 5.Both side are composite with the same type constructor? Split into equations between corresponding components.
7
Type checking and inference Using the type constraints approach Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-III: Solving the equations. EquationSubstitution 3. T f =[T x →T 2 ]T 1 :=[T sqrt *T num4 →T 0 ] 4. T sqrt = [Number → Number] 5. T num4 =Number 6. T f =T sqrt 7. T x =T num4 8. T 2 =T 0 Equation 2: [T sqrt * T num4 -> T 0 ] = [T f * T x -> T 2 ] -Apply step 1: T1 is replaced by current substitution: [T sqrt * T num4 -> T 0 ] = [T f * T x -> T 2 ] -Both side are composite, apply step 5: Equations are split and Eq2 is removed. For each equation: 1.Replace vars by their substituting expressions. 2.Both sides of the eq. are atomic? 1.If equal, ignore equation. 2.Else, output FAIL. 3.Both sides are different vars? 1.Apply the equation to the current substitution. 2.Add the equation to the substitution. 4.A circular substitution occurred? Output FAIL. 5.Both side are composite with the same type constructor? Split into equations between corresponding components.
8
Type checking and inference Using the type constraints approach Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-III: Solving the equations. EquationSubstitution 3. T f =[T x →T 2 ]T 1 :=[T sqrt *T num4 →T 0 ] 4. T sqrt = [Number → Number] 5. T num4 =Number 6. T f =T sqrt 7. T x =T num4 8. T 2 =T 0 Equation 3: -Apply step 1: No change. -Eq3 is added to the substitution. For each equation: 1.Replace vars by their substituting expressions. 2.Both sides of the eq. are atomic? 1.If equal, ignore equation. 2.Else, output FAIL. 3.Both sides are different vars? 1.Apply the equation to the current substitution. 2.Add the equation to the substitution. 4.A circular substitution occurred? Output FAIL. 5.Both side are composite with the same type constructor? Split into equations between corresponding components.
9
Type checking and inference Using the type constraints approach Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-III: Solving the equations. EquationSubstitution 4. T sqrt = [Number → Number] T 1 :=[T sqrt *T num4 →T 0 ] T f :=[T x →T 2 ] 5. T num4 =Number 6. T f =T sqrt 7. T x =T num4 8. T 2 =T 0 Equation 3: -Apply step 1: No change. -Eq3 is added to the substitution. For each equation: 1.Replace vars by their substituting expressions. 2.Both sides of the eq. are atomic? 1.If equal, ignore equation. 2.Else, output FAIL. 3.Both sides are different vars? 1.Apply the equation to the current substitution. 2.Add the equation to the substitution. 4.A circular substitution occurred? Output FAIL. 5.Both side are composite with the same type constructor? Split into equations between corresponding components.
10
Type checking and inference Using the type constraints approach Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-III: Solving the equations. EquationSubstitution 4. T sqrt = [Number → Number] T 1 :=[T sqrt *T num4 →T 0 ] T f :=[T x →T 2 ] 5. T num4 =Number 6. T f =T sqrt 7. T x =T num4 8. T 2 =T 0 Equation 4: -Apply step 1: No change. -Eq4 is added to the substitution: The substitution is updated by substituting T sqrt for [Number → Number] For each equation: 1.Replace vars by their substituting expressions. 2.Both sides of the eq. are atomic? 1.If equal, ignore equation. 2.Else, output FAIL. 3.Both sides are different vars? 1.Apply the equation to the current substitution. 2.Add the equation to the substitution. 4.A circular substitution occurred? Output FAIL. 5.Both side are composite with the same type constructor? Split into equations between corresponding components.
11
Type checking and inference Using the type constraints approach Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-III: Solving the equations. EquationSubstitution 5. T num4 =Number T 1 :=[[Number → Number]*T num4 →T 0 ] T f :=[T x →T 2 ] T sqrt := [Number → Number] 6. T f =T sqrt 7. T x =T num4 8. T 2 =T 0 Equation 4: -Apply step 1: No change. -Eq4 is added to the substitution: The substitution is updated by substituting T sqrt for [Number → Number] For each equation: 1.Replace vars by their substituting expressions. 2.Both sides of the eq. are atomic? 1.If equal, ignore equation. 2.Else, output FAIL. 3.Both sides are different vars? 1.Apply the equation to the current substitution. 2.Add the equation to the substitution. 4.A circular substitution occurred? Output FAIL. 5.Both side are composite with the same type constructor? Split into equations between corresponding components.
12
Type checking and inference Using the type constraints approach Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-III: Solving the equations. EquationSubstitution 5. T num4 =Number T 1 :=[[Number → Number]*T num4 →T 0 ] 6. T f =T sqrt T f :=[T x →T 2 ] 7. T x =T num4 T sqrt := [Number → Number] 8. T 2 =T 0 Equation 5: -Apply step 1: No change. -Eq5 is added to the substitution: The substitution is updated by substituting T num4 for Number For each equation: 1.Replace vars by their substituting expressions. 2.Both sides of the eq. are atomic? 1.If equal, ignore equation. 2.Else, output FAIL. 3.Both sides are different vars? 1.Apply the equation to the current substitution. 2.Add the equation to the substitution. 4.A circular substitution occurred? Output FAIL. 5.Both side are composite with the same type constructor? Split into equations between corresponding components.
13
Type checking and inference Using the type constraints approach Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-III: Solving the equations. EquationSubstitution 6. T f =T sqrt T 1 :=[[Number → Number]*Number→T 0 ] T f :=[T x →T 2 ] T sqrt := [Number → Number] T num4 :=Number 7. T x =T num4 8. T 2 =T 0 Equation 5: -Apply step 1: No change. -Eq5 is added to the substitution: The substitution is updated by substituting T num4 for Number For each equation: 1.Replace vars by their substituting expressions. 2.Both sides of the eq. are atomic? 1.If equal, ignore equation. 2.Else, output FAIL. 3.Both sides are different vars? 1.Apply the equation to the current substitution. 2.Add the equation to the substitution. 4.A circular substitution occurred? Output FAIL. 5.Both side are composite with the same type constructor? Split into equations between corresponding components.
14
Type checking and inference Using the type constraints approach Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-III: Solving the equations. EquationSubstitution 6. T f =T sqrt T 1 :=[[Number → Number]*Number→T 0 ] T f :=[T x →T 2 ] T sqrt := [Number → Number] T num4 :=Number 7. T x =T num4 8. T 2 =T 0 Equation 6: [T x →T 2 ] = [Number → Number] -Apply step 1: T f, T sqrt are replaced by current substitution: [T x →T 2 ] = [Number → Number] -Both side are composite, apply step 5: Equations are split and Eq6 is removed. For each equation: 1.Replace vars by their substituting expressions. 2.Both sides of the eq. are atomic? 1.If equal, ignore equation. 2.Else, output FAIL. 3.Both sides are different vars? 1.Apply the equation to the current substitution. 2.Add the equation to the substitution. 4.A circular substitution occurred? Output FAIL. 5.Both side are composite with the same type constructor? Split into equations between corresponding components.
15
Type checking and inference Using the type constraints approach Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-III: Solving the equations. EquationSubstitution 7. T x =T num4 T 1 :=[[Number → Number]*Number→T 0 ] T f :=[T x →T 2 ] T sqrt := [Number → Number] T num4 :=Number 8. T 2 =T 0 9. T x =Number 10. T 2 =Number Equation 6: -Apply step 1: T f, T sqrt T1 is replaced by current substitution: [T x →T 2 ]=[Number → Number] -Both side are composite, apply step 5: Equations are split and Eq6 is removed. For each equation: 1.Replace vars by their substituting expressions. 2.Both sides of the eq. are atomic? 1.If equal, ignore equation. 2.Else, output FAIL. 3.Both sides are different vars? 1.Apply the equation to the current substitution. 2.Add the equation to the substitution. 4.A circular substitution occurred? Output FAIL. 5.Both side are composite with the same type constructor? Split into equations between corresponding components.
16
Type checking and inference Using the type constraints approach Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-III: Solving the equations. EquationSubstitution 7. T x =T num4 T 1 :=[[Number → Number]*Number→T 0 ] T f :=[T x →T 2 ] T sqrt := [Number → Number] T num4 :=Number 8. T 2 =T 0 9. T x =Number 10. T 2 =Number Equation 7: -Apply step 1: T num4 is replaced by current substitution: T x =Number -Eq7 is added to the substitution: The substitution is updated by substituting T x for Number For each equation: 1.Replace vars by their substituting expressions. 2.Both sides of the eq. are atomic? 1.If equal, ignore equation. 2.Else, output FAIL. 3.Both sides are different vars? 1.Apply the equation to the current substitution. 2.Add the equation to the substitution. 4.A circular substitution occurred? Output FAIL. 5.Both side are composite with the same type constructor? Split into equations between corresponding components.
17
Type checking and inference Using the type constraints approach Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-III: Solving the equations. EquationSubstitution 8. T 2 =T 0 T 1 :=[[Number → Number]*Number→T 0 ] T f :=[Number→T 2 ] T sqrt := [Number → Number] T num4 :=Number T x :=Number 9. T x =Number 10. T 2 =Number Equation 7: -Apply step 1: T num4 is replaced by current substitution: T x =Number -Eq7 is added to the substitution: The substitution is updated by substituting T x for Number For each equation: 1.Replace vars by their substituting expressions. 2.Both sides of the eq. are atomic? 1.If equal, ignore equation. 2.Else, output FAIL. 3.Both sides are different vars? 1.Apply the equation to the current substitution. 2.Add the equation to the substitution. 4.A circular substitution occurred? Output FAIL. 5.Both side are composite with the same type constructor? Split into equations between corresponding components.
18
Type checking and inference Using the type constraints approach Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-III: Solving the equations. EquationSubstitution 8. T 2 =T 0 T 1 :=[[Number → Number]*Number→T 0 ] T f :=[Number→T 2 ] T sqrt := [Number → Number] T num4 :=Number T x :=Number 9. T x =Number 10. T 2 =Number Equation 8: -Apply step 1: no change. -Eq8 is added to the substitution: The substitution is updated by substituting T 2 for T 0 For each equation: 1.Replace vars by their substituting expressions. 2.Both sides of the eq. are atomic? 1.If equal, ignore equation. 2.Else, output FAIL. 3.Both sides are different vars? 1.Apply the equation to the current substitution. 2.Add the equation to the substitution. 4.A circular substitution occurred? Output FAIL. 5.Both side are composite with the same type constructor? Split into equations between corresponding components.
19
Type checking and inference Using the type constraints approach Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-III: Solving the equations. EquationSubstitution 9. T x =Number T 1 :=[[Number → Number]*Number→T 0 ] T f :=[Number→T 0 ] T sqrt := [Number → Number] T num4 :=Number T x :=Number T 2 :=T 0 10. T 2 =Number Equation 8: -Apply step 1: no change. -Eq8 is added to the substitution: The substitution is updated by substituting T 2 for T 0 For each equation: 1.Replace vars by their substituting expressions. 2.Both sides of the eq. are atomic? 1.If equal, ignore equation. 2.Else, output FAIL. 3.Both sides are different vars? 1.Apply the equation to the current substitution. 2.Add the equation to the substitution. 4.A circular substitution occurred? Output FAIL. 5.Both side are composite with the same type constructor? Split into equations between corresponding components.
20
Type checking and inference Using the type constraints approach Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-III: Solving the equations. EquationSubstitution 9. T x =Number T 1 :=[[Number → Number]*Number→T 0 ] T f :=[Number→T 0 ] T sqrt := [Number → Number] T num4 :=Number T x :=Number T 2 :=T 0 10. T 2 =Number Equation 9: Number=Number -Apply step 1: T x is replaced by current substitution: Number=Number -Step 3: both sides are atomic types and the equation is ignored. For each equation: 1.Replace vars by their substituting expressions. 2.Both sides of the eq. are atomic? 1.If equal, ignore equation. 2.Else, output FAIL. 3.Both sides are different vars? 1.Apply the equation to the current substitution. 2.Add the equation to the substitution. 4.A circular substitution occurred? Output FAIL. 5.Both side are composite with the same type constructor? Split into equations between corresponding components.
21
Type checking and inference Using the type constraints approach Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-III: Solving the equations. EquationSubstitution 10. T 2 =Number T 1 :=[[Number → Number]*Number→T 0 ] T f :=[Number→T 0 ] T sqrt := [Number → Number] T num4 :=Number T x :=Number T 2 :=T 0 Equation 10: -Apply step 1: no change. -Eq8 is added to the substitution: The substitution is updated by substituting T 2 for T 0. For each equation: 1.Replace vars by their substituting expressions. 2.Both sides of the eq. are atomic? 1.If equal, ignore equation. 2.Else, output FAIL. 3.Both sides are different vars? 1.Apply the equation to the current substitution. 2.Add the equation to the substitution. 4.A circular substitution occurred? Output FAIL. 5.Both side are composite with the same type constructor? Split into equations between corresponding components.
22
Type checking and inference Using the type constraints approach Question 1 (cont’d): Typing the application ((lambda (f x) (f x)) sqrt 4) STAGE-III: Solving the equations. EquationSubstitution T 1 :=[[Number → umber]*Number→Number] T f :=[Number→Number] T sqrt := [Number → Number] T num4 :=Number T x :=Number T 2 :=Number T 0 :=Number Equation 10: -Apply step 1: no change. -Eq8 is added to the substitution: The substitution is updated by substituting T 0 with Number. The type inference succeeds. The inferred type is: Number For each equation: 1.Replace vars by their substituting expressions. 2.Both sides of the eq. are atomic? 1.If equal, ignore equation. 2.Else, output FAIL. 3.Both sides are different vars? 1.Apply the equation to the current substitution. 2.Add the equation to the substitution. 4.A circular substitution occurred? Output FAIL. 5.Both side are composite with the same type constructor? Split into equations between corresponding components.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.