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

Slides:



Advertisements
Similar presentations
Solve an equation with variables on both sides
Advertisements

7.8 Equations Involving Radicals. Solving Equations Involving Radicals :  1. the term with a variable in the radicand on one side of the sign.  2. Raise.
PPL Static Verification: Type Inference Lecture Notes: Chapter 2.
Solving Equations by Adding or Subtracting Objective: Solve equations using the addition and subtraction properties of equalities.
Thursday, November Make sure your name is on Practice 3-5 and it is completed! 2. Today’s objective: SWBAT solve absolute value equations and inequalities.
Solving a System of Equations in Two Variables By Elimination Chapter 8.3.
Solving a System of Equations by SUBSTITUTION. GOAL: I want to find what x equals, and what y equals. Using substitution, I can say that x = __ and y.
The Equation Game. Why is an equation like a balance scale? 3 2x - 1 =
PPL Applicative and Normal Form Verification, Type Checking and Inference.
Lesson 1-8 Solving Addition and Subtraction Equations.
Solving Linear Systems by Substitution
Equations Students will be able to solve equations using mental math, or guess and check.
Systems of Equations By Dr. Marinas. Solving Systems Graphing Method Substitution Method Elimination (or Adding) Method.
Solving a System of Equations in Two Variables By Substitution Chapter 8.2.
Solve Equations With Variables on Both Sides. Steps to Solve Equations with Variables on Both Sides  1) Do distributive property  2) Combine like terms.
Warm-up. Systems of Equations: Substitution Solving by Substitution 1)Solve one of the equations for a variable. 2)Substitute the expression from step.
Solve a two-step equation by combining like terms EXAMPLE 2 Solve 7x – 4x = 21 7x – 4x = 21 Write original equation. 3x = 21 Combine like terms. Divide.
Solving equations with variable on both sides Part 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 +
6.5 Solving Exponential Equations SOLVE EXPONENTIAL EQUATIONS WITH THE SAME BASE. SOLVE EXPONENTIAL EQUATIONS WITH UNLIKE BASES.
PPL Leftovers: LET Syntax & Formal Semantics Static Verification: Type Inference Lecture Notes: Chapter 2.
Objective: Use factoring to solve quadratic equations. Standard(s) being met: 2.8 Algebra and Functions.
Type Correctness.
Equations Quadratic in form factorable equations
Solving Systems Using Substitution
Type Inference using Type Constraints
Variables on Both Sides with Equations
Type checking and inference
One-Step Equations with Subtraction
Warm up 11/1/ X ÷ 40.
Solving 1-Step Integer Equations
3-2: Solving Systems of Equations using Substitution
Solving Equations by Factoring and Problem Solving
Solving Systems using Substitution
Solving Linear Systems Algebraically
Solve a system of linear equation in two variables
6-3 Solving Systems Using Elimination
3-2: Solving Systems of Equations using Substitution
Solving Systems of Equations using Substitution
Do Now 1) t + 3 = – 2 2) 18 – 4v = 42.
What is an equation? An equation is a mathematical statement that two expressions are equal. For example, = 7 is an equation. Note: An equation.
EQ: How do I solve an equation in one variable?
3-2: Solving Systems of Equations using Substitution
2 Understanding Variables and Solving Equations.
2 Understanding Variables and Solving Equations.
1.4 Solving Absolute-Value Equations
Practice session #4: Static Type Correctness Algorithms:
Solving one- and two-step equations
If you can easily isolate one of the variables,
Warm Up Solve. 1. 2x + 9x – 3x + 8 = –4 = 6x + 22 – 4x 3. + = 5
Solving a System of Equations in Two Variables by the Addition Method
Solving 1-Step Integer Equations
Solving systems using substitution
Solving Equations with Variables on Both Sides
1.4 Solving Absolute-Value Equations
Solving Equations with Variables on Both Sides
3-2: Solving Systems of Equations using Substitution
Warm Up Check to see if the point is a solution for the
Completing the Square.
Equations Quadratic in form factorable equations
Section 6.1 Solving Inequalities Using Addition or Subtraction
Example 2B: Solving Linear Systems by Elimination
3-2: Solving Systems of Equations using Substitution
3-2: Solving Systems of Equations using Substitution
3-2: Solving Systems of Equations using Substitution
3-2: Solving Systems of Equations using Substitution
One-step addition & subtraction equations: fractions & decimals
The Substitution Method
Warm- Up: Solve by Substitution
Solving Linear Equations
Presentation 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) STAGE-I: Renaming ExpressionVar ((lambda(f x) (f x)) sqrt 4) T0T0 (lambda(f x) (f x)) T1T1 (f x)T2T2 fTfTf xTxTx sqrtT sqrt 4T num4 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 (v 1... v n ) e 1...e m ), construct: 3. Application expressions: For (f e 1...e n ), construct: ExpressionEquation sqrt T sqrt = [Number → Number] 4 T num4 =Number ((lambda (f x) (f x)) sqrt 4)T 1 =[T sqrt *T num4 →T 0 ] (lambda (f x) (f x))T 1 =[T f *T x →T 2 ] (f x)T f =[T x →T 2 ] ExpressionVar ((lambda(f x) (f x)) sqrt 4) T0T0 (lambda(f x) (f x)) T1T1 (f x)T2T2 fTfTf xTxTx sqrtT sqrt 4T num4

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 (v 1... v n ) e 1...e m ), construct: 3. Application expressions: For (f e 1...e n ), construct: ExpressionEquation sqrt T sqrt = [Number → Number] 4 T num4 =Number ((lambda (f x) (f x)) sqrt 4)T 1 =[T sqrt *T num4 →T 0 ] (lambda (f x) (f x))T 1 =[T f *T x →T 2 ] (f x)T f =[T x →T 2 ] ExpressionVar ((lambda(f x) (f x)) sqrt 4) T0T0 (lambda(f x) (f x)) T1T1 (f x)T2T2 fTfTf xTxTx sqrtT sqrt 4T num4

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. EquationSubstitution 1. T 1 =[T sqrt *T num4 →T 0 ]{ } 2. T 1 =[T f *T x →T 2 ] 3. T f =[T x →T 2 ] 4. T sqrt = [Number → Number] 5. T num4 =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. EquationSubstitution 2. T 1 =[T f *T x →T 2 ]T 1 :=[T sqrt *T num4 →T 0 ] 3. T f =[T x →T 2 ] 4. T sqrt = [Number → Number] 5. T num4 =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. EquationSubstitution 2. T 1 =[T f *T x →T 2 ]T 1 :=[T sqrt *T num4 →T 0 ] 3. T f =[T x →T 2 ] 4. T sqrt = [Number → Number] 5. T num4 =Number Equation 2: [T sqrt * T num4 -> T 0 ] = [T f * T x -> T 2 ] -Apply step 1: T1 is replaced by current substitution: [T sqrt * T num4 -> T 0 ] = [T f * T x -> T 2 ] -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. EquationSubstitution 3. T f =[T x →T 2 ]T 1 :=[T sqrt *T num4 →T 0 ] 4. T sqrt = [Number → Number] 5. T num4 =Number 6. T f =T sqrt 7. T x =T num4 8. T 2 =T 0 Equation 2: [T sqrt * T num4 -> T 0 ] = [T f * T x -> T 2 ] -Apply step 1: T1 is replaced by current substitution: [T sqrt * T num4 -> T 0 ] = [T f * T x -> T 2 ] -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. EquationSubstitution 3. T f =[T x →T 2 ]T 1 :=[T sqrt *T num4 →T 0 ] 4. T sqrt = [Number → Number] 5. T num4 =Number 6. T f =T sqrt 7. T x =T num4 8. T 2 =T 0 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. EquationSubstitution 4. T sqrt = [Number → Number] T 1 :=[T sqrt *T num4 →T 0 ] T f :=[T x →T 2 ] 5. T num4 =Number 6. T f =T sqrt 7. T x =T num4 8. T 2 =T 0 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. EquationSubstitution 4. T sqrt = [Number → Number] T 1 :=[T sqrt *T num4 →T 0 ] T f :=[T x →T 2 ] 5. T num4 =Number 6. T f =T sqrt 7. T x =T num4 8. T 2 =T 0 Equation 4: -Apply step 1: No change. -Eq4 is added to the substitution: The substitution is updated by substituting T sqrt 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. EquationSubstitution 5. T num4 =Number T 1 :=[[Number → Number]*T num4 →T 0 ] T f :=[T x →T 2 ] T sqrt := [Number → Number] 6. T f =T sqrt 7. T x =T num4 8. T 2 =T 0 Equation 4: -Apply step 1: No change. -Eq4 is added to the substitution: The substitution is updated by substituting T sqrt 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. EquationSubstitution 5. T num4 =Number T 1 :=[[Number → Number]*T num4 →T 0 ] 6. T f =T sqrt T f :=[T x →T 2 ] 7. T x =T num4 T sqrt := [Number → Number] 8. T 2 =T 0 Equation 5: -Apply step 1: No change. -Eq5 is added to the substitution: The substitution is updated by substituting T num4 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. EquationSubstitution 6. T f =T sqrt T 1 :=[[Number → Number]*Number→T 0 ] T f :=[T x →T 2 ] T sqrt := [Number → Number] T num4 :=Number 7. T x =T num4 8. T 2 =T 0 Equation 5: -Apply step 1: No change. -Eq5 is added to the substitution: The substitution is updated by substituting T num4 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. EquationSubstitution 6. T f =T sqrt T 1 :=[[Number → Number]*Number→T 0 ] T f :=[T x →T 2 ] T sqrt := [Number → Number] T num4 :=Number 7. T x =T num4 8. T 2 =T 0 Equation 6: [T x →T 2 ] = [Number → Number] -Apply step 1: T f, T sqrt are replaced by current substitution: [T x →T 2 ] = [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. EquationSubstitution 7. T x =T num4 T 1 :=[[Number → Number]*Number→T 0 ] T f :=[T x →T 2 ] T sqrt := [Number → Number] T num4 :=Number 8. T 2 =T 0 9. T x =Number 10. T 2 =Number Equation 6: -Apply step 1: T f, T sqrt T1 is replaced by current substitution: [T x →T 2 ]=[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. EquationSubstitution 7. T x =T num4 T 1 :=[[Number → Number]*Number→T 0 ] T f :=[T x →T 2 ] T sqrt := [Number → Number] T num4 :=Number 8. T 2 =T 0 9. T x =Number 10. T 2 =Number Equation 7: -Apply step 1: T num4 is replaced by current substitution: T x =Number -Eq7 is added to the substitution: The substitution is updated by substituting T x 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. EquationSubstitution 8. T 2 =T 0 T 1 :=[[Number → Number]*Number→T 0 ] T f :=[Number→T 2 ] T sqrt := [Number → Number] T num4 :=Number T x :=Number 9. T x =Number 10. T 2 =Number Equation 7: -Apply step 1: T num4 is replaced by current substitution: T x =Number -Eq7 is added to the substitution: The substitution is updated by substituting T x 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. EquationSubstitution 8. T 2 =T 0 T 1 :=[[Number → Number]*Number→T 0 ] T f :=[Number→T 2 ] T sqrt := [Number → Number] T num4 :=Number T x :=Number 9. T x =Number 10. T 2 =Number Equation 8: -Apply step 1: no change. -Eq8 is added to the substitution: The substitution is updated by substituting T 2 for T 0 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. EquationSubstitution 9. T x =Number T 1 :=[[Number → Number]*Number→T 0 ] T f :=[Number→T 0 ] T sqrt := [Number → Number] T num4 :=Number T x :=Number T 2 :=T T 2 =Number Equation 8: -Apply step 1: no change. -Eq8 is added to the substitution: The substitution is updated by substituting T 2 for T 0 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. EquationSubstitution 9. T x =Number T 1 :=[[Number → Number]*Number→T 0 ] T f :=[Number→T 0 ] T sqrt := [Number → Number] T num4 :=Number T x :=Number T 2 :=T T 2 =Number Equation 9: Number=Number -Apply step 1: T x 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. EquationSubstitution 10. T 2 =Number T 1 :=[[Number → Number]*Number→T 0 ] T f :=[Number→T 0 ] T sqrt := [Number → Number] T num4 :=Number T x :=Number T 2 :=T 0 Equation 10: -Apply step 1: no change. -Eq8 is added to the substitution: The substitution is updated by substituting T 2 for T 0. 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. EquationSubstitution T 1 :=[[Number → umber]*Number→Number] T f :=[Number→Number] T sqrt := [Number → Number] T num4 :=Number T x :=Number T 2 :=Number T 0 :=Number Equation 10: -Apply step 1: no change. -Eq8 is added to the substitution: The substitution is updated by substituting T 0 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.