Type checking and inference Using the type constraints

  • Slides: 5
Download presentation
Type checking and inference Using the type constraints approach Question 3 (cont’d): Typing the

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 / primitiveprocedures: Construct equations using their types. 2. Lambda expressions: For (lambda (v 1. . . vn) e 1. . . em), construct: Expression Equation (lambda(x) (x x) T 0 = [Tx → T 1 ] Tx = [Tx → T 1 ] Expression Var (lambda(x) (x x) T 0 T 1 Tx x 3. Application expressions: For (f e 1. . . en), construct:

Type checking and inference Using the type constraints approach Question 3 (cont’d): Typing the

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: Equation 1. Replace vars by their substituting expressions. 1. T 0 = [Tx → T 1 ] 2. Tx = [Tx → T 1 ] 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. Substitution Equation 1: - Initially, the substitution is empty. Step 1 is ignored. - Eq 1 is moved to the substitution.

Type checking and inference Using the type constraints approach Question 3 (cont’d): Typing the

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: 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. Equation Substitution 2. Tx = [Tx → T 1 ] T 0 : = [Tx → T 1 ] Equation 1: - Initially, the substitution is empty. Step 1 is ignored. - Eq 1 is moved to the substitution.

Type checking and inference Using the type constraints approach Question 3 (cont’d): Typing the

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: 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. Equation Substitution 2. Tx = [Tx → T 1 ] T 0 : = [Tx → T 1 ] Equation 2: - Apply step 1: No change. - Eq 2 is moved to the substitution.

Type checking and inference Using the type constraints approach Question 3 (cont’d): Typing the

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: 1. Replace vars by their substituting expressions. 2. Both sides of the eq. are atomic? 1. If equal, ignore eq. 2. Else, output FAIL. 3. Both side 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. Equation Substitution T 0 : = [[Tx → T 1 ] Tx : = [Tx → T 1 ] Equation 2: - We got a circular substitution: Tx : = [Tx → T 1 ]