Presentation is loading. Please wait.

Presentation is loading. Please wait.

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 +

Similar presentations


Presentation on theme: "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 +"— Presentation transcript:

1 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 + T+ #t T#t 1 Tnum1 2 Tnum2 3 Tnum3 STAGE-II: Assign type variables to all sub-expressions

2 Type checking and inference Using the type constraints approach 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: Question 2: Typing the application (if #t (+ 1 2) 3) ExpressionVariable (if #t (+ 1 2) 3) T0 (+ 1 2) T1 + T+ #t T#t 1 Tnum1 2 Tnum2 3 Tnum3 ExpressionEquation (if #t (+ 1 2) 3) T1 = Tnum3 T0 = T1 (+ 1 2) T+ = [Tnum1 * Tnum2 -> T1] + T+ = [Number * Number -> Number] #t T#t = Boolean 1 Tnum1 = Number 2 Tnum2 = Number 3 Tnum3 = Number

3 Type checking and inference Using the type constraints approach 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. T1 = Tnum3 { } 2. T0 = T1 3. T+ = [Tnum1 * Tnum2 -> T1] 4. T+ = [Number * Number -> Number] 5. T#t = Boolean 6. Tnum1 = Number 7. Tnum2 = Number 8. Tnum3 = Number Equation 1: -Initially, the substitution is empty. Step 1 is ignored. -Eq1 is moved to the substitution. Question 2: Typing the application (if #t (+ 1 2) 3)

4 Type checking and inference Using the type constraints approach 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 2. T0 = T1 { T1 := Tnum3 } 3. T+ = [Tnum1 * Tnum2 -> T1] 4. T+ = [Number * Number -> Number] 5. T#t = Boolean 6. Tnum1 = Number 7. Tnum2 = Number 8. Tnum3 = Number Equation 1: -Initially, the substitution is empty. Step 1 is ignored. -Eq1 is moved to the substitution. Question 2: Typing the application (if #t (+ 1 2) 3)

5 Type checking and inference Using the type constraints approach STAGE-III: Solving the equations. Equation 2: -Apply step 1: T1 is replaced by current substitution. -Both side are different vars -> add eq. to sub. 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 2. T0 = T1 { T1 := Tnum3 } 3. T+ = [Tnum1 * Tnum2 -> T1] 4. T+ = [Number * Number -> Number] 5. T#t = Boolean 6. Tnum1 = Number 7. Tnum2 = Number 8. Tnum3 = Number Question 2: Typing the application (if #t (+ 1 2) 3)

6 Type checking and inference Using the type constraints approach STAGE-III: Solving the equations. Equation 2: -Apply step 1: T1 is replaced by current substitution. -Both side are different vars -> add eq. to sub. 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 2. T0 = Tnum3 { T1 := Tnum3 } 3. T+ = [Tnum1 * Tnum2 -> T1] 4. T+ = [Number * Number -> Number] 5. T#t = Boolean 6. Tnum1 = Number 7. Tnum2 = Number 8. Tnum3 = Number Question 2: Typing the application (if #t (+ 1 2) 3)

7 Type checking and inference Using the type constraints approach STAGE-III: Solving the equations. Equation 2: -Apply step 1: T1 is replaced by current substitution. -Both side are different vars -> add eq. to sub. 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 3. T+ = [Tnum1 * Tnum2 -> T1] { T1 := Tnum3, T0 := Tnum3 } 4. T+ = [Number * Number -> Number] 5. T#t = Boolean 6. Tnum1 = Number 7. Tnum2 = Number 8. Tnum3 = Number Question 2: Typing the application (if #t (+ 1 2) 3)

8 Type checking and inference Using the type constraints approach STAGE-III: 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 3. T+ = [Tnum1 * Tnum2 -> T1] { T1 := Tnum3, T0 := Tnum3 } 4. T+ = [Number * Number -> Number] 5. T#t = Boolean 6. Tnum1 = Number 7. Tnum2 = Number 8. Tnum3 = Number Question 2: Typing the application (if #t (+ 1 2) 3)

9 Type checking and inference Using the type constraints approach STAGE-III: 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 3. T+ = [Tnum1 * Tnum2 -> Tnum3] { T1 := Tnum3, T0 := Tnum3 } 4. T+ = [Number * Number -> Number] 5. T#t = Boolean 6. Tnum1 = Number 7. Tnum2 = Number 8. Tnum3 = Number Question 2: Typing the application (if #t (+ 1 2) 3)

10 Type checking and inference Using the type constraints approach STAGE-III: 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 4. T+ = [Number * Number -> Number] { T1 := Tnum3, T0 := Tnum3, T+=[Tnum1*Tnum2->Tnum3] } 5. T#t = Boolean 6. Tnum1 = Number 7. Tnum2 = Number 8. Tnum3 = Number Question 2: Typing the application (if #t (+ 1 2) 3)

11 Type checking and inference Using the type constraints approach STAGE-III: 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 4. T+ = [Number * Number -> Number] { T1 := Tnum3, T0 := Tnum3, T+=[Tnum1*Tnum2->Tnum3] } 5. T#t = Boolean 6. Tnum1 = Number 7. Tnum2 = Number 8. Tnum3 = Number Question 2: Typing the application (if #t (+ 1 2) 3)

12 Type checking and inference Using the type constraints approach STAGE-III: 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 4. [Tnum1 * Tnum2 -> Tnum3] = [Number * Number -> Number] { T1 := Tnum3, T0 := Tnum3, T+=[Tnum1*Tnum2->Tnum3] } 5. T#t = Boolean 6. Tnum1 = Number 7. Tnum2 = Number 8. Tnum3 = Number Question 2: Typing the application (if #t (+ 1 2) 3)

13 Type checking and inference Using the type constraints approach STAGE-III: 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 5. T#t = Boolean { T1 := Tnum3, T0 := Tnum3, T+=[Tnum1*Tnum2->Tnum3] } 6. Tnum1 = Number 7. Tnum2 = Number 8. Tnum3 = Number Question 2: Typing the application (if #t (+ 1 2) 3)

14 Type checking and inference Using the type constraints approach STAGE-III: 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 5. T#t = Boolean { T1 := Tnum3, T0 := Tnum3, T+=[Tnum1*Tnum2->Tnum3] } 6. Tnum1 = Number 7. Tnum2 = Number 8. Tnum3 = Number Question 2: Typing the application (if #t (+ 1 2) 3)

15 Type checking and inference Using the type constraints approach STAGE-III: 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 6. Tnum1 = Number { T1 := Tnum3, T0 := Tnum3, T+=[Tnum1*Tnum2->Tnum3], T#t = Boolean } 7. Tnum2 = Number 8. Tnum3 = Number Question 2: Typing the application (if #t (+ 1 2) 3)

16 Type checking and inference Using the type constraints approach STAGE-III: 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 6. Tnum1 = Number { T1 := Tnum3, T0 := Tnum3, T+ := [Tnum1*Tnum2 -> Tnum3], T#t := Boolean, } 7. Tnum2 = Number 8. Tnum3 = Number Question 2: Typing the application (if #t (+ 1 2) 3)

17 Type checking and inference Using the type constraints approach STAGE-III: 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 { T1 := Tnum3, T0 := Tnum3, T+ := [Tnum1*Tnum2 -> Tnum3], T#t := Boolean, Tnum1 := Number, Tnum2 := Number, Tnum3 := Number } Question 2: Typing the application (if #t (+ 1 2) 3)

18 Type checking and inference Using the type constraints approach STAGE-III: 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 { T1 := Number, T0 := Number, T+ := [Number*Number -> Number], T#t := Boolean, Tnum1 := Number, Tnum2 := Number, Tnum3 := Number } Question 2: Typing the application (if #t (+ 1 2) 3)

19 Type checking and inference Using the type constraints approach STAGE-III: 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 { T1 := Number, T0 := Number, T+ := [Number*Number -> Number], T#t := Boolean, Tnum1 := Number, Tnum2 := Number, Tnum3 := Number } Question 2: Typing the application (if #t (+ 1 2) 3) ExpressionVariable (if #t (+ 1 2) 3) T0 (+ 1 2) T1 + T+ #t T#t 1 Tnum1 2 Tnum2 3 Tnum3

20 Type checking and inference Using the type constraints approach STAGE-III: 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 { T1 := Number, T0 := Number, T+ := [Number*Number -> Number], T#t := Boolean, Tnum1 := Number, Tnum2 := Number, Tnum3 := Number } Question 2: Typing the application (if #t (+ 1 2) 3) ExpressionVariable (if #t (+ 1 2) 3) T0 (+ 1 2) T1 + T+ #t T#t 1 Tnum1 2 Tnum2 3 Tnum3


Download ppt "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 +"

Similar presentations


Ads by Google