Type Inference II David Walker COS 441. Type Inference Goal: Given unannotated program, find its type or report it does not type check Overview: generate.

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Substitution & Evaluation Order cos 441 David Walker.
The simplex algorithm The simplex algorithm is the classical method for solving linear programs. Its running time is not polynomial in the worst case.
Type Inference David Walker COS 320. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Homework Answers.
SLD-resolution Introduction Most general unifiers SLD-resolution
10 October 2006 Foundations of Logic and Constraint Programming 1 Unification ­An overview Need for Unification Ranked alfabeths and terms. Substitutions.
The Solution of a Difference Equation for Simple Interest Account
Introduction to Computability Theory
CS 611 Advanced Programming Languages Andrew Myers Cornell University Lecture 27 Type inference 2 Nov 05.
Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction.
Type Inference David Walker COS 441. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Type Inference David Walker CS 510, Fall Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs.
Solve an equation with variables on both sides
Table of Contents Solving Linear Systems of Equations - Substitution Method Recall that to solve the linear system of equations in two variables... we.
Chapter 8. Section 8. 1 Section Summary Introduction Modeling with Recurrence Relations Fibonacci Numbers The Tower of Hanoi Counting Problems Algorithms.
Solve Equations with Variables on Both Sides
Inverses and Systems Section Warm – up:
Chapter 4.1 Solving Systems of Linear Equations in two variables.
Chapter 8 With Question/Answer Animations 1. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
EXAMPLE 1 Solve an equation with variables on both sides 7 – 8x = 4x – 17 7 – 8x + 8x = 4x – x 7 = 12x – = 12x Write original equation. Add.
3-2 Solving Equations by Using Addition and Subtraction Objective: Students will be able to solve equations by using addition and subtraction.
Type Inference II David Walker COS 441. Type Inference Goal: Given unannotated program, find its type or report it does not type check Overview: generate.
Solving Linear Systems of Equations - Substitution Method Recall that to solve the linear system of equations in two variables... we need to find the value.
Systems of Equations 7-4 Learn to solve systems of equations.
1 Solving Linear Equations. 2 Like Terms Like terms contain the same variables raised to the same powers. To combine like terms, add or subtract the numerical.
Martin-Gay, Beginning Algebra, 5ed Using Both Properties Divide both sides by 3. Example: 3z – 1 = 26 3z = 27 Simplify both sides. z = 9 Simplify.
5.3: Solving Addition Equations Goal #1: Solving Addition Problems Goal #2: Writing Addition Equations.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
3.2 Solving Equations by Using Addition and Subtraction Addition Property of Equality –If the same number is added to each side of an equation, the resulting.
(Finite) Mathematical Induction In our first lesson on sequences and series, you were told that How can we be certain that this will be true for all counting.
§ 2.2 The Addition Property of Equality. Angel, Elementary Algebra, 7ed 2 Linear Equations A linear equation in one variable is an equation that can be.
Lesson 1-8 Solving Addition and Subtraction Equations.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.2: Pushdown Automata) Prof. Karen Daniels, Fall 2010 with acknowledgement.
Solving Radical Inequalities. Solving radical inequalities is similar to solving rational equations, but there is one extra step since we must make sure.
3.4 Solving Equations with Variables on Both Sides Objective: Solve equations that have variables on both sides.
Types and Programming Languages Lecture 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
1 2/21/2016 MATH 224 – Discrete Mathematics Sequences and Sums A sequence of the form ar 0, ar 1, ar 2, ar 3, ar 4, …, ar n, is called a geometric sequence.
CS383 Programming Languages Quiz Which one is a term schema? a.‘a b.s :: = ‘a| int | bool c.fun f (x: int) : bool = e d.fun f (x: s1) : s2 = e.
2.4 Equations with Variables on Both Sides To solve an equation with variables on both sides, you must use the Addition or Subtraction Properties of Equality.
Process Algebra (2IF45) Basic Process Algebra Dr. Suzana Andova.
Using Number Sense to Solve One-Step Equations Lesson 2-5.
Solving equations with variable on both sides Part 1.
11.3 Solving Radical Equations Definitions & Rules Simplifying Radicals Practice Problems.
Lesson 8.1. » A statement where two mathematical expressions are. » Think of an equation as a balance scale or teeter-totter. The left side must always.
COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
§ 2.3 Solving Linear Equations. Martin-Gay, Beginning and Intermediate Algebra, 4ed 22 Solving Linear Equations Solving Linear Equations in One Variable.
Lesson 10 Type Reconstruction
Lecture 11: Proof by Reflection
Manuel Fahndrich Jakob Rehof Manuvir Das
MATH 224 – Discrete Mathematics
Using Number Sense to Solve One-Step Equations
Example: Solve the equation. Multiply both sides by 5. Simplify both sides. Add –3y to both sides. Simplify both sides. Add –30 to both sides. Simplify.
Using Number Sense to Solve One-Step Equations
Solving Equations with the Variable on Each Side
Solving Equations by Adding or Subtracting
Solving Radical Equations
Typed Arithmetic Expressions
Solving Systems of Equations using Substitution
Do Now 1) t + 3 = – 2 2) 18 – 4v = 42.
Equations with Variables on Both Sides Day 2
2 Understanding Variables and Solving Equations.
4.6 Cramer’s Rule System of 2 Equations System of 3 Equations
2. A System of Equations is a pair of equations with two variables
2. A System of Equations is a pair of equations with two variables
9.7 Solving Systems of Equations Algebraically
Algebra 1 Section 2.7.
2-3 Equations With Variables on Both Sides
2-5 Solving Equations with the Variable on Each Side
Equations Reducible to Quadratic
Presentation transcript:

Type Inference II David Walker COS 441

Type Inference Goal: Given unannotated program, find its type or report it does not type check Overview: generate type constraints (equations) from unannotated programs solve equations

Constraint Generation Typing rules for annotated programs: G |-- e : t Note: given G and e, at most one type t Typing rules for unannotated programs: G |-- u => e : t, q Note: given G and u, there may be many possible types for u; the many possibilities represented using type schemes u has a type t if q has a solution Remember: fun f (x) = f x

Rule comparison G |-- u1 ==> e1 : t1, q1 G |-- u2 ==> e2 : t2, q2 G |-- u3 ==> e3 : t3, q G |-- if u1 then u2 else u3 ==> if e1 then e2 else e3 : a, q1 U q2 U q3 U {t1 = bool, a = t2, a = t3} G |-- e1 : bool G |-- e2 : t G |-- e3 : t G |-- if e1 then e2 else e3 : t implicit equality easy to implement because t can’t contain variables equality harder to implement because t2, t3 can contain variables that may be further constrained elsewhere

Non-local Constraints fun f (x) = fun g (y) = (if true then x else y, x + y) fun f (x) = fun g (y) = (if true then x else y, x + (if y then 3 else 4)) Does this type check? fun f (x) = fun g (y) = (if true then x else y,...) It depends:

Non-local Constraints fun f (x:int) = fun g (y:int) = (if true then x else y, x + y) fun f (x:int) = fun g (y:bool) = (if true then x else y, x + (if y then 3 else 4)) But remember, it was easy to check when types were declared in advance:

Solving Constraints A solution to a system of type constraints is a substitution S S |= q iff applying S makes left- & right-hand sides of each equation equal A solution S is better than T if it is “more general” intuition: S makes fewer or less-defined substitutions (leaves more variables alone) T <= S if and only if T = U o S for some U

Most General Solutions S is the principal (most general) solution of a constraint q if S |= q (it is a solution) if T |= q then T <= S (it is the most general one) Lemma: If q has a solution, then it has a most general one We care about principal solutions since they will give us the most general types for terms

principal solutions principal solutions give rise to most general reconstruction of typing information for a term: fun f(x:a):a = x is a most general reconstruction fun f(x:int):int = x is not

Unification Unification: An algorithm that provides the principal solution to a set of constraints (if one exists) If one exists, it will be principal

Unification Unification: Unification systematically simplifies a set of constraints, yielding a substitution during simplification, we maintain (S,q) S is the solution so far q are the constraints left to simplify Starting state of unification process: (I,q) Final state of unification process: (S, { }) identity substitution is most general

Unification Machine We can specify unification as a transition system: (S,q) -> (S’,q’) Base types & simple variables: (S,{int=int} U q) -> (S, q) (S,{bool=bool} U q) -> (S, q) (S,{a=a} U q) -> (S, q)

Unification Machine Functions: Variable definitions (S,{s11 -> s12= s21 -> s22} U q) -> (S, {s11 = s21, s12 = s22} U q) (a not in FV(s)) (S,{a=s} U q) -> ([a=s] o S, [s/a]q) (a not in FV(s)) (S,{s=a} U q) -> ([a=s] o S, [s/a]q)

Occurs Check What is the solution to {a = a -> a}?

Occurs Check What is the solution to {a = a -> a}? There is none! (Remember your homework) The occurs check detects this situation (a not in FV(s)) (S,{s=a} U q) -> ([a=s] o S, [s/a]q) occurs check

Irreducible States Recall: final states have the form (S, { }) Stuck states (S,q) are such that every equation in q has the form: int = bool s1 -> s2 = s (s not function type) a = s (s contains a) or is symmetric to one of the above Stuck states arise when constraints are unsolvable

Termination We want unification to terminate (to give us a type reconstruction algorithm) In other words, we want to show that there is no infinite sequence of states (S1,q1) -> (S2,q2) ->...

Termination We associate an ordering with constraints q < q’ if and only if q contains fewer variables than q’ q contains the same number of variables as q’ but fewer type constructors (ie: fewer occurrences of int, bool, or “->”) This is a lexicographic ordering There is no infinite decreasing sequence of constraints To prove termination, we must demonstrate that every step of the algorithm reduces the size of q according to this ordering

Termination Lemma: Every step reduces the size of q Proof: By cases (ie: induction) on the definition of the reduction relation (S,{int=int} U q) -> (S, q) (S,{bool=bool} U q) -> (S, q) (S,{a=a} U q) -> (S, q) (S,{s11 -> s12= s21 -> s22} U q) -> (S, {s11 = s21, s12 = s22} U q) (a not in FV(s)) (S,{a=s} U q) -> ([a=s] o S, [s/a]q)

Complete Solutions A complete solution for (S,q) is a substitution T such that 1. T <= S 2. T |= q intuition: T extends S and solves q A principal solution T for (S,q) is complete for (S,q) and 3. for all T’ such that 1. and 2. hold, T’ <= T

Properties of Solutions Lemma 1: Every final state (S, { }) has a complete solution. It is S: S <= S S |= { }

Properties of Solutions Lemma 2 No stuck state has a complete solution (or any solution at all) it is impossible for a substitution to make the necessary equations equal int  bool int  t1 -> t2...

Properties of Solutions Lemma 3 If (S,q) -> (S’,q’) then T is complete for (S,q) iff T is complete for (S’,q’) T is principal for (S,q) iff T is principal for (S’,q’) in the forward direction, this is the preservation theorem for the unification machine!

Summary: Unification By termination, (I,q) ->* (S,q’) where (S,q’) is irreducible. Moreover: If q’ = { } then (S,q’) is final (by definition) S is a principal solution for q. Why? S is principal for (S,q’) (by lemma 1) S is principal for (I,q) (by lemma 3) Since S is principal for (I,q), since all possible solutions T <= I, S is a principal solution for q.

Summary: Unification (cont.)... Moreover: If q’ is not { } (and (I,q) ->* (S,q’) where (S,q’) is irreducible) then (S,q) is stuck. Consequently, (S,q) has no complete solution. By lemma 3, even (I,q) has no complete solution and therefore q has no solution at all.

Summary: Type Inference Type inference algorithm. Given a context G, and untyped term u: Find e, t, q such that G |- u ==> e : t, q Find principal solution S of q via unification if no solution exists, there is no reconstruction Apply S to e, ie our solution is S(e) S(e) contains schematic type variables a,b,c, etc that may be instantiated with any type Since S is principal, S(e) characterizes all reconstructions.

End