3/20/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,

Slides:



Advertisements
Similar presentations
Functional Programming Lecture 10 - type checking.
Advertisements

Patterns in ML functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the.
5/24/20151 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
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.
Logic Programming Part 2: Semantics James Cheney CS 411.
3.5 Solving systems of equations in 3 variables
Bell Work2/12/15 Solve the system by elimination..
Solving Systems of Equations: Elimination Method.
9/18/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
3x – 5y = 11 x = 3y + 1 Do Now. Homework Solutions 2)2x – 2y = – 6 y = – 2x 2x – 2(– 2x) = – 6 2x + 4x = – 6 6x = – 6 x = – 1y = – 2x y = – 2(– 1) y =
Inverses and Systems Section Warm – up:
10/14/20151 Programming Languages and Compilers (CS 421) Grigore Rosu 2110 SC, UIUC Slides by Elsa Gunter, based.
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
CS321 Functional Programming 2 © JAS Type Checking Polymorphism in Haskell is implicit. ie the system can derive the types of all objects. This.
10/25/20151 Programming Languages and Compilers (CS 421) Grigore Rosu 2110 SC, UIUC Slides by Elsa Gunter, based.
Another method for solving systems of equations is elimination
The AI War LISP and Prolog Basic Concepts of Logic Programming
5.2: Solving Systems of Equations using Substitution
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Solving Systems of Equations by Elimination (Addition) Section 3.2, Part II.
Solving by Elimination Example 1: STEP 2: Look for opposite terms. STEP 1: Write both equations in Standard Form to line up like variables. STEP 5: Solve.
Elimination Method: Solve the linear system. -8x + 3y=12 8x - 9y=12.
Do Now (3x + y) – (2x + y) 4(2x + 3y) – (8x – y)
10/16/081 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
2/6/20161 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Solving systems of equations with three variables January 13, 2010.
3/8/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman, as updated.
Objective solve systems of equations using elimination.
6/21/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,
Elimination Method - Systems. Elimination Method  With the elimination method, you create like terms that add to zero.
7/7/20161 Programming Languages and Compilers (CS 421) Dennis Griffith 0207 SC, UIUC Based in part on slides by Mattox.
Programming Languages and Compilers (CS 421)
Solving Systems of Equations using Substitution
Equations Quadratic in form factorable equations
Programming Languages and Compilers (CS 421)
Solving Systems of Equations
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
3-2: Solving Systems of Equations using Substitution
Solve a system of linear equation in two variables
Programming Languages and Compilers (CS 421)
Solving Systems Equations in Three Variables
3.5 Solving systems of equations in 3 variables
Programming Languages and Compilers (CS 421)
SYSTMES OF EQUATIONS SUBSTITUTION.
3-2: Solving Systems of Equations using Substitution
Solving Systems of Equations using Substitution
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
3-2: Solving Systems of Equations using Substitution
If you can easily isolate one of the variables,
Madhusudan Parthasarathy
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Madhusudan Parthasarathy (madhu) 3112 Siebel Center
Solving systems using substitution
Programming Languages and Compilers (CS 421)
3-2: Solving Systems of Equations using Substitution
Solve the linear system.
Equations Quadratic in form factorable equations
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
Step 1: Put the equations in Standard Form. Standard Form: Ax + By = C
Presentation transcript:

3/20/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul Agha

3/20/ Background for Unification Terms made from constructors and variables (for the simple first order case) Constructors may be applied to arguments (other terms) to make new terms Variables and constructors with no arguments are base cases Constructors applied to different number of arguments (arity) considered different Substitution of terms for variables

3/20/ Simple Implementation Background type term = Variable of string | Const of (string * term list) let rec subst var_name residue term = match term with Variable name -> if var_name = name then residue else term | Const (c, tys) -> Const (c, List.map (subst var_name residue) tys);;

3/20/ Unification Problem Given a set of pairs of terms (“equations”) {(s 1, t 1 ), (s 2, t 2 ), …, (s n, t n )} (the unification problem) does there exist a substitution  (the unification solution) of terms for variables such that  (s i ) =  (t i ), for all i = 1, …, n?

3/20/ Uses for Unification Type Inference and type checking Pattern matching as in OCAML Can use a simplified version of algorithm Logic Programming - Prolog Simple parsing

3/20/ Unification Algorithm Let S = {(s 1, t 1 ), (s 2, t 2 ), …, (s n, t n )} be a unification problem. Case S = { }: Unif(S) = Identity function (ie no substitution) Case S = {(s, t)}  S’ : Four main steps

3/20/ Unification Algorithm Delete: if s = t (they are the same term) then Unif(S) = Unif(S’) Decompose: if s = f(q 1, …, q m ) and t =f(r 1, …, r m ) (same f, same m!), then Unif(S) = Unif({(q 1, r 1 ), …, (q m, r m )}  S’) Orient: if t = x is a variable, and s is not a variable, Unif(S) = Unif ({(x,s)}  S’)

3/20/ Unification Algorithm Eliminate: if s = x is a variable, and x does not occur in t (the occurs check), then Let  = x |  t Let  = Unif(  (S’)) Unif(S) = {x |   (t)} o  Note: {x |  a} o {y |  b} = {y |  {x |  a}(b)} o {x |  a} if y not in a

3/20/ Tricks for Efficient Unification Don’t return substitution, rather do it incrementally Make substitution be constant time Requires implementation of terms to use mutable structures (or possibly lazy structures) We haven’t discussed these yet

3/20/ Example x,y,z variables, f,g constructors S = { ( f(x), f(g(y,z)) ), ( g(y,f(y)), x ) }

3/20/ Example x,y,z variables, f,g constructors Pick a pair: ( g(y,f(y)), x ) S = { ( f(x), f(g(y,z)) ), ( g(y,f(y)), x ) }

3/20/ Example x,y,z variables, f,g constructors Pick a pair: ( g(y,f(y))), x ) Orient is first rule that applies S = { ( f(x), f(g(y,z)) ), ( g(y,f(y)), x ) }

3/20/ Example x,y,z variables, f,g constructors S -> {(f(x), f(g(y,z))), (x, g(y,f(y)))}

3/20/ Example x,y,z variables, f,g constructors Pick a pair: (f(x), f(g(y,z))) S -> {(f(x), f(g(y,z))), (x, g(y,f(y)))}

3/20/ Example x,y,z variables, f,g constructors Pick a pair: (f(x), f(g(y,z))) Decompose it: (x, g(y,z)) S -> {(x, g(y,z)), (x, g(y,f(y)))}

3/20/ Example x,y,z variables, f,g constructors Pick a pair: (x, g(y,f(y))) S -> {(x, g(y,z)), (x, g(y,f(y)))}

3/20/ Example x,y,z variables, f,g constructors Pick a pair: (x, g(y,f(y))) Substitute: S -> {(g(y,f(y)), g(y,z))} With {x |  g(y,f(y))}

3/20/ Example x,y,z variables, f,g constructors Pick a pair: (g(y,f(y)), g(y,z)) S -> {(g(y,f(y)), g(y,z))} With {x |  g(y,f(y))}

3/20/ Example x,y,z variables, f,g constructors Pick a pair: (g(y,f(y)), g(y,z)) Decompose: (y, y) and (f(y), z) S -> {(y, y), (f(y), z)} With {x |  g(y,f(y))}

3/20/ Example x,y,z variables, f,g constructors Pick a pair: (y, y) S -> {(y, y), (f(y), z)} With {x |  g(y,f(y))}

3/20/ Example x,y,z variables, f,g constructors Pick a pair: (y, y) Delete S -> {(f(y), z)} With {x |  g(y,f(y))}

3/20/ Example x,y,z variables, f,g constructors Pick a pair: (f(y), z) S -> {(f(y), z)} With {x |  g(y,f(y))}

3/20/ Example x,y,z variables, f,g constructors Pick a pair: (f(y), z) Orient S -> {(z, f(y))} With {x |  g(y,f(y))}

3/20/ Example x,y,z variables, f,g constructors Pick a pair: (z, f(y)) S -> {(z, f(y))} With {x |  g(y,f(y))}

3/20/ Example x,y,z variables, f,g constructors Pick a pair: (z,f(y)) Substitute S -> { } With {x |  {z |  f(y)} (g(y,f(y)) } o {z |  f(y)}

3/20/ Example x,y,z variables, f,g constructors Pick a pair: (z,f(y)) Substitute S -> { } With {x |  g(y,f(y))} o {(z |  f(y))}

3/20/ Example S = {(f(x), f(g(y,z))), (g(y,f(y)),x)} Solved by {x |  g(y,f(y))} o {(z |  f(y))} f(g(y,f(y))) = f(g(y,f(y))) x z and g(y,f(y)) = g(y,f(y)) x

3/20/ Example of Failure S = {(f(x,g(y)), f(h(y),x))} Decompose S -> {(x,h(y)), (g(y),x)} Orient S -> {(x,h(y)), (x,g(y))} Substitute S -> {(h(y), g(y))} with {x |  h(y)} No rule to apply! Decompose fails!