Type checking and inference Using the type constraints approach Question 3 (cont’d): Typing the application (lambda (x) (x x)) STAGE-II: Construct type equations. The rules: 1. Atomic expressions / primitive-procedures: Construct equations using their types. 2. Lambda expressions: For (lambda (v1 . . . vn) e1 . . .em), construct: 3. Application expressions: For (f e1 . . .en), construct: Expression Equation (lambda(x) (x x)) T0 = [Tx → T1 ] (x x) Tx = [Tx → T1 ] Expression Var (lambda(x) (x x)) T0 (x x) T1 x Tx
Type checking and inference Using the type constraints approach Question 3 (cont’d): Typing the application (lambda (x) (x x)) STAGE-III: Solving the equations. For each equation: Replace vars by their substituting expressions. Both sides of the eq. are atomic? If equal, ignore equation. Else, output FAIL. Both sides are different vars? Apply the equation to the current 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. Equation Substitution 1. T0 = [Tx → T1 ] 2. Tx = [Tx → T1 ] Equation 1: Initially, the substitution is empty. Step 1 is ignored. Eq1 is moved to the substitution.
Type checking and inference Using the type constraints approach Question 3 (cont’d): Typing the application (lambda (x) (x x)) STAGE-III: Solving the equations. For each equation: Replace vars by their substituting expressions. Both sides of the eq. are atomic? If equal, ignore equation. Else, output FAIL. Both sides are different vars? Apply the equation to the current 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. Equation Substitution 2. Tx = [Tx → T1 ] T0 := [Tx → T1 ] Equation 1: Initially, the substitution is empty. Step 1 is ignored. Eq1 is moved to the substitution.
Type checking and inference Using the type constraints approach Question 3 (cont’d): Typing the application (lambda (x) (x x)) STAGE-III: Solving the equations. For each equation: Replace vars by their substituting expressions. Both sides of the eq. are atomic? If equal, ignore equation. Else, output FAIL. Both sides are different vars? Apply the equation to the current 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. Equation Substitution 2. Tx = [Tx → T1 ] T0 := [Tx → T1 ] Equation 2: Apply step 1: No change. Eq2 is moved to the substitution.
Type checking and inference Using the type constraints approach Question 3 (cont’d): Typing the application (lambda (x) (x x)) STAGE-III: Solving the equations. For each equation: Replace vars by their substituting expressions. Both sides of the eq. are atomic? If equal, ignore eq. Else, output FAIL. Both side are different vars? Apply the equation to the current 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. Equation Substitution T0 := [[Tx → T1 ] → T1 ] Tx := [Tx → T1 ] Equation 2: We got a circular substitution: Tx := [Tx → T1 ]