type checking and inference applications of typing axioms / rules are replaced with type equations....

22
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

Upload: ronald-chandler

Post on 03-Jan-2016

219 views

Category:

Documents


2 download

TRANSCRIPT

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

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

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

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.