Top Banner
ype 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) Using the type constraints approach STAGE-I: Renaming Expression Var ((lambda(f x) (f x)) sqrt 4) T 0 (lambda(f x) (f x)) T 1 (f x) T 2 f T f x T x sqrt T sqrt 4 T num4 STAGE-II: Assign type variables to all sub-expressions
22

Type checking and inference Applications of typing axioms / rules are replaced with type equations. A solution to the equations assigns types for every.

Jan 03, 2016

Download

Documents

Ronald Chandler
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Type checking and inference Applications of typing axioms / rules are replaced with type equations. A solution to the equations assigns types for every.

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)

Using the type constraints approach

STAGE-I: Renaming

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

STAGE-II: Assign type variables to all sub-expressions

Page 2: Type checking and inference Applications of typing axioms / rules are replaced with type equations. A solution to the equations assigns types for every.

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 (v1 . . . vn) e1 . . .em), construct:

3. Application expressions:For (f e1 . . .en), construct:

mnmn evveevvlambda TTTT *...*

111 ......

nn eefeef TTTT ...11

*...*

Expression Equation

sqrt Tsqrt = [Number → Number]4 Tnum4=Number

((lambda (f x) (f x)) sqrt 4) T1=[Tsqrt*Tnum4→T0]

(lambda (f x) (f x)) T1=[Tf*Tx→T2]

(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

Page 3: Type checking and inference Applications of typing axioms / rules are replaced with type equations. A solution to the equations assigns types for every.

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 (v1 . . . vn) e1 . . .em), construct:

3. Application expressions:For (f e1 . . .en), construct:

mnmn evveevvlambda TTTT *...*

111 ......

nn eefeef TTTT ...11

*...*

Expression Equation

sqrt Tsqrt = [Number → Number]4 Tnum4=Number

((lambda (f x) (f x)) sqrt 4) T1=[Tsqrt*Tnum4→T0]

(lambda (f x) (f x)) T1=[Tf*Tx→T2]

(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

Page 4: Type checking and inference Applications of typing axioms / rules are replaced with type equations. A solution to the equations assigns types for every.

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.

Equation Substitution

1. T1=[Tsqrt*Tnum4→T0] { }

2. T1=[Tf*Tx→T2]

3. Tf=[Tx→T2]4. Tsqrt = [Number → Number]5. Tnum4=Number

Equation 1:- Initially, the substitution is empty. Step 1 is ignored.- Eq1 is moved to the substitution.

Page 5: Type checking and inference Applications of typing axioms / rules are replaced with type equations. A solution to the equations assigns types for every.

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.

Equation Substitution

2. T1=[Tf*Tx→T2] T1:=[Tsqrt*Tnum4→T0]

3. Tf=[Tx→T2]4. Tsqrt = [Number → Number]5. Tnum4=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.

Page 6: Type checking and inference Applications of typing axioms / rules are replaced with type equations. A solution to the equations assigns types for every.

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 Substitution

2. T1=[Tf*Tx→T2] T1:=[Tsqrt*Tnum4→T0]

3. Tf=[Tx→T2]4. Tsqrt = [Number → Number]5. Tnum4=Number

Equation 2:- Apply step 1: T1 is replaced by current substitution:

[Tsqrt* Tnum4-> T0] = [Tf* Tx -> T2]

- 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.

Page 7: Type checking and inference Applications of typing axioms / rules are replaced with type equations. A solution to the equations assigns types for every.

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 Substitution

3. Tf=[Tx→T2] T1:=[Tsqrt*Tnum4→T0]4. Tsqrt = [Number → Number]5. Tnum4=Number6. Tf=Tsqrt

7. Tx=Tnum4

8. T2=T0

Equation 2:- Apply step 1: T1 is replaced by current substitution:

[Tsqrt* Tnum4-> T0] = [Tf* Tx -> T2]

- 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.

Page 8: Type checking and inference Applications of typing axioms / rules are replaced with type equations. A solution to the equations assigns types for every.

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 Substitution

3. Tf=[Tx→T2] T1:=[Tsqrt*Tnum4→T0]4. Tsqrt = [Number → Number]5. Tnum4=Number6. Tf=Tsqrt

7. Tx=Tnum4

8. T2=T0

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.

Page 9: Type checking and inference Applications of typing axioms / rules are replaced with type equations. A solution to the equations assigns types for every.

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 Substitution

4. Tsqrt = [Number → Number] T1:=[Tsqrt*Tnum4→T0]Tf:=[Tx→T2]5. Tnum4=Number

6. Tf=Tsqrt

7. Tx=Tnum4

8. T2=T0

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.

Page 10: Type checking and inference Applications of typing axioms / rules are replaced with type equations. A solution to the equations assigns types for every.

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 Substitution

4. Tsqrt = [Number → Number] T1:=[Tsqrt*Tnum4→T0]Tf:=[Tx→T2]5. Tnum4=Number

6. Tf=Tsqrt

7. Tx=Tnum4

8. T2=T0

Equation 4:- Apply step 1: No change.- Eq4 is added to the substitution: The substitution is

updated by substituting Tsqrt 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.

Page 11: Type checking and inference Applications of typing axioms / rules are replaced with type equations. A solution to the equations assigns types for every.

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 Substitution

5. Tnum4=Number T1:=[[Number → Number]*Tnum4→T0]Tf:=[Tx→T2]Tsqrt := [Number → Number]

6. Tf=Tsqrt

7. Tx=Tnum4

8. T2=T0

Equation 4:- Apply step 1: No change.- Eq4 is added to the substitution: The substitution is

updated by substituting Tsqrt 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.

Page 12: Type checking and inference Applications of typing axioms / rules are replaced with type equations. A solution to the equations assigns types for every.

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 Substitution

5. Tnum4=Number T1:=[[Number → Number]*Tnum4→T0]6. Tf=Tsqrt Tf:=[Tx→T2]7. Tx=Tnum4 Tsqrt := [Number → Number]8. T2=T0

Equation 5:- Apply step 1: No change.- Eq5 is added to the substitution: The substitution is

updated by substituting Tnum4 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.

Page 13: Type checking and inference Applications of typing axioms / rules are replaced with type equations. A solution to the equations assigns types for every.

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 Substitution

6. Tf=Tsqrt T1:=[[Number → Number]*Number→T0]Tf:=[Tx→T2]Tsqrt := [Number → Number]Tnum4:=Number

7. Tx=Tnum4

8. T2=T0

Equation 5:- Apply step 1: No change.- Eq5 is added to the substitution: The substitution is

updated by substituting Tnum4 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.

Page 14: Type checking and inference Applications of typing axioms / rules are replaced with type equations. A solution to the equations assigns types for every.

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 Substitution

6. Tf=Tsqrt T1:=[[Number → Number]*Number→T0]Tf:=[Tx→T2]Tsqrt := [Number → Number]Tnum4:=Number

7. Tx=Tnum4

8. T2=T0

Equation 6:- Apply step 1: Tf , Tsqrt are replaced by current substitution:

[Tx→T2] = [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.

Page 15: Type checking and inference Applications of typing axioms / rules are replaced with type equations. A solution to the equations assigns types for every.

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 Substitution

7. Tx=Tnum4 T1:=[[Number → Number]*Number→T0]Tf:=[Tx→T2]Tsqrt := [Number → Number]Tnum4:=Number

8. T2=T0

9. Tx=Number10. T2=Number

Equation 6:- Apply step 1: Tf , Tsqrt T1 is replaced by current substitution:

[Tx→T2]=[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.

Page 16: Type checking and inference Applications of typing axioms / rules are replaced with type equations. A solution to the equations assigns types for every.

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 Substitution

7. Tx=Tnum4 T1:=[[Number → Number]*Number→T0]Tf:=[Tx→T2]Tsqrt := [Number → Number]Tnum4:=Number

8. T2=T0

9. Tx=Number10. T2=Number

Equation 7:- Apply step 1: Tnum4 is replaced by current substitution:

Tx=Number

- Eq7 is added to the substitution: The substitution is updated by substituting Tx 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.

Page 17: Type checking and inference Applications of typing axioms / rules are replaced with type equations. A solution to the equations assigns types for every.

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 Substitution

8. T2=T0 T1:=[[Number → Number]*Number→T0]Tf:=[Number→T2]Tsqrt := [Number → Number]Tnum4:=NumberTx:=Number

9. Tx=Number10. T2=Number

Equation 7:- Apply step 1: Tnum4 is replaced by current substitution:

Tx=Number

- Eq7 is added to the substitution: The substitution is updated by substituting Tx 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.

Page 18: Type checking and inference Applications of typing axioms / rules are replaced with type equations. A solution to the equations assigns types for every.

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 Substitution

8. T2=T0 T1:=[[Number → Number]*Number→T0]Tf:=[Number→T2]Tsqrt := [Number → Number]Tnum4:=NumberTx:=Number

9. Tx=Number10. T2=Number

Equation 8:- Apply step 1: no change.

- Eq8 is added to the substitution: The substitution is updated by substituting T2 for T0

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.

Page 19: Type checking and inference Applications of typing axioms / rules are replaced with type equations. A solution to the equations assigns types for every.

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 Substitution

9. Tx=Number T1:=[[Number → Number]*Number→T0]Tf:=[Number→T0]Tsqrt := [Number → Number]Tnum4:=NumberTx:=NumberT2:=T0

10. T2=Number

Equation 8:- Apply step 1: no change.

- Eq8 is added to the substitution: The substitution is updated by substituting T2 for T0

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.

Page 20: Type checking and inference Applications of typing axioms / rules are replaced with type equations. A solution to the equations assigns types for every.

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 Substitution

9. Tx=Number T1:=[[Number → Number]*Number→T0]Tf:=[Number→T0]Tsqrt := [Number → Number]Tnum4:=NumberTx:=NumberT2:=T0

10. T2=Number

Equation 9:- Apply step 1: Tx 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.

Page 21: Type checking and inference Applications of typing axioms / rules are replaced with type equations. A solution to the equations assigns types for every.

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 Substitution

10. T2=Number T1:=[[Number → Number]*Number→T0]Tf:=[Number→T0]Tsqrt := [Number → Number]Tnum4:=NumberTx:=NumberT2:=T0

Equation 10:- Apply step 1: no change.

- Eq8 is added to the substitution: The substitution is updated by substituting T2 for T0.

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.

Page 22: Type checking and inference Applications of typing axioms / rules are replaced with type equations. A solution to the equations assigns types for every.

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 Substitution

T1:=[[Number → umber]*Number→Number]Tf:=[Number→Number]Tsqrt := [Number → Number]Tnum4:=NumberTx:=NumberT2:=NumberT0:=Number

Equation 10:- Apply step 1: no change.

- Eq8 is added to the substitution: The substitution is updated by substituting T0 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.