



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Objectives The Problem The Algorithm Implementation Activity
Mark Hills [email protected]
University of Illinois at Urbana-Champaign
June 26, 2008
(^1) Based on slides by Mattox Beckman, as updated by Vikram Adve, Gul Agha, and Elsa Gunter Mark Hills CS421 Lecture 13: Unification 1 / 19
Objectives The Problem The Algorithm Implementation Activity
Objectives
The Problem
The Algorithm
Implementation
Activity
Mark Hills CS421 Lecture 13: Unification 2 / 19
Outline Objectives The Problem The Algorithm Implementation Activity
Unification is an important technique in programming languages. It is used to implement several important languages features in languages such as OCaml (where it is used for pattern-matching and polymorphic type inference) and Prolog (where it used as the core evaluation process for logic rules). It is also used for type inference in other languages, many modern pointer analysis algorithms, and in theoretical discussions. After this lecture (and MP5!), you should ◮ (^) be able to describe the problem of unification; ◮ (^) know how to use unification to implement pattern matching; ◮ (^) know how to use unification to check types of functions.
Mark Hills CS421 Lecture 13: Unification 3 / 19
Outline Objectives The Problem The Algorithm Implementation Activity
The Problem Domain The Problem Pattern MatchingType Checking Type Inference
Terms Have name and arity ◮ (^) The name will be one or more characters (f, sum, ...) ◮ (^) Arity = “number of arguments” — may be zero ◮ (^) Terms used as constructors to build other terms ◮ (^) Constructors with different arities considered different ◮ (^) Examples: x, z, f(x,y), x(y,f,z)
Note: arguments to terms may have non-zero arity, or may be variables.
Mark Hills CS421 Lecture 13: Unification 4 / 19
Outline Objectives The Problem The Algorithm Implementation Activity
The Problem Domain The Problem Pattern MatchingType Checking Type Inference
Variables Written using Greek alphabet, may be subscripted ◮ (^) Represent a target for substitution ◮ (^) Examples: α, β 12 , γ 7 Substitutions Mappings from Variables to Terms ◮ (^) Examples: σ = {α 7 → f(x, β), β 7 → y} ◮ (^) Substitutions are applied: σ(g(β)) → g(y)
Outline Objectives The Problem The Algorithm Implementation Activity
The Problem Domain The Problem Pattern MatchingType Checking Type Inference
◮ (^) Given terms s and t, try to find a substitution σ such that σ(s) = σ(t). ◮ (^) If such a substitution exists, it is said that s and t unify. ◮ (^) A unification problem is a set of equations S = {s 1 = t 1 , s 2 = t 2 ,.. .}. ◮ (^) A unification problem S = {x 1 = t 1 , x 2 = t 2 ,.. .} is in solved form if ◮ (^) the terms xi are distinct variables ◮ (^) none of them occur in ti. Our approach: given a unification problem S, we want to find the most general unifier σ that solves it. We will do this by transforming the equations.
Objectives The Problem The Algorithm Implementation Activity
The Problem Domain The Problem Pattern Matching Type Checking Type Inference
Pattern Matching can be implemented via unification
1 let lst = 3::4::5::[];; 2 match lst with 3 | [] -> ... 4 | x::xs -> ....
◮ (^) We want to unify lst with [] or x :: xs. ◮ (^) We cannot unify lst with []. ◮ (^) Let S = {αx :: αxs = lst; lst = 3::4::5::[]} ◮ (^) σ(αxs ) = 4::5::[] ◮ (^) σ(αx ) = 3.
Mark Hills CS421 Lecture 13: Unification 7 / 19
Objectives The Problem The Algorithm Implementation Activity
The Problem Domain The Problem Pattern Matching Type Checking Type Inference
Unification is widely used for type checking and type inference 1 # let rec ListAssoc lst2 n = match lst2 with ... 2 val ListAssoc : (’a * ’b) list -> ’a -> ’b =
Outline Objectives The Problem The Algorithm Implementation Activity
The Problem Domain The Problem Pattern MatchingType Checking Type Inference
MP5 will focus on type inference. Overall, we will: ◮ (^) begin by assigning a type variable as the type of the whole expression ◮ (^) decompose the expression into component expressions ◮ (^) use typing rules to generate constraints on the components and the entire expression ◮ (^) recursively gather additional constraints over the structure of the term ◮ (^) solve (unify) the system of constraints to generate a substitution ◮ (^) apply this substitution to the type variable assigned to the whole expression to get the expression type
Mark Hills CS421 Lecture 13: Unification 9 / 19
Outline Objectives The Problem The Algorithm Implementation Activity
Operations Example 1 Example 2 Example 3
Start with a unification problem S = {s 1 = t 1 , s 2 = t 2 ,.. .} and apply the following transformations as necessary: Delete A trivial equation t = t can be deleted. Decompose An equation f (tn) = f (un) can be replaced by the set {t 1 = u 1 ,... , tn = un}, with f and n same in both Orient An equation t = x can be replaced by x = t if x is a variable and t is not. Eliminate an equation x = t can be used to substitute all occurrences of x in the remainder of S. x must not occur in t!
Mark Hills CS421 Lecture 13: Unification 10 / 19
Outline Objectives The Problem The Algorithm Implementation Activity
Operations Example 1 Example 2 Example 3
(Stolen from “Term Rewriting and All That”) {α = f (x), g (α, α) = g (α, β)}
Outline Objectives The Problem The Algorithm Implementation Activity
Operations Example 1 Example 2 Example 3
(Stolen from “Term Rewriting and All That”) {α = f (x), g (α, α) = g (α, β)} We can use the Eliminate method, replace α with f (x) on the right sides of the equations.
Objectives The Problem The Algorithm Implementation Activity
Operations Example 1 Example 2 Example 3
Start with {f (α, g (β)) = f (g (x), α)} {f (α, g (β)) = f (g (x), α)} Use the decompose method to get rid of f. {α = g (x), g (β) = α} Now we can substitute g (x) for α.
Mark Hills CS421 Lecture 13: Unification 12 / 19
Objectives The Problem The Algorithm Implementation Activity
Operations Example 1 Example 2 Example 3
Start with {f (α, g (β)) = f (g (x), α)} {f (α, g (β)) = f (g (x), α)} Use the decompose method to get rid of f. {α = g (x), g (β) = α} Now we can substitute g (x) for α. {α = g (x), g (β) = g (x)} Use decomposition to eliminate g.
Mark Hills CS421 Lecture 13: Unification 12 / 19
Outline Objectives The Problem The Algorithm Implementation Activity
Operations Example 1 Example 2 Example 3
Start with {f (α, g (β)) = f (g (x), α)} {f (α, g (β)) = f (g (x), α)} Use the decompose method to get rid of f. {α = g (x), g (β) = α} Now we can substitute g (x) for α. {α = g (x), g (β) = g (x)} Use decomposition to eliminate g. {α = g (x), β = x} We are done.
Mark Hills CS421 Lecture 13: Unification 12 / 19
Outline Objectives The Problem The Algorithm Implementation Activity
Operations Example 1 Example 2 Example 3
Start with {f (α, g (y )) = f (h(y ), α)}
Mark Hills CS421 Lecture 13: Unification 13 / 19
Outline Objectives The Problem The Algorithm Implementation Activity
Operations Example 1 Example 2 Example 3
Start with {f (α, g (y )) = f (h(y ), α)} {f (α, g (y )) = f (h(y ), α)} Use the decompose method to get rid of f.
Outline Objectives The Problem The Algorithm Implementation Activity
Operations Example 1 Example 2 Example 3
Start with {f (α, g (y )) = f (h(y ), α)} {f (α, g (y )) = f (h(y ), α)} Use the decompose method to get rid of f. {α = h(y ), g (y ) = α} Now orient g (y ) = α
Objectives The Problem The Algorithm Implementation Activity
Operations Example 1 Example 2 Example 3
Start with {f (α, g (y )) = f (h(y ), α)} {f (α, g (y )) = f (h(y ), α)} Use the decompose method to get rid of f. {α = h(y ), g (y ) = α} Now orient g (y ) = α {α = h(y ), α = g (y )} Use substitution for α.
Mark Hills CS421 Lecture 13: Unification 13 / 19
Objectives The Problem The Algorithm Implementation Activity
Operations Example 1 Example 2 Example 3
Start with {f (α, g (y )) = f (h(y ), α)} {f (α, g (y )) = f (h(y ), α)} Use the decompose method to get rid of f. {α = h(y ), g (y ) = α} Now orient g (y ) = α {α = h(y ), α = g (y )} Use substitution for α. {α = h(y ), h(y ) = g (y )} We’re stuck – no rules apply, but we haven’t unified.
Mark Hills CS421 Lecture 13: Unification 13 / 19
Outline Objectives The Problem The Algorithm Implementation Activity
◮ (^) There is a simple algorithm that works well most of the time. 1 type exp = Var of string 2 | Term of string * exp list
◮ (^) A unification equation is a pair, a unification problem is a list of pairs. ◮ (^) Example: {α = f (x), g (α, α) = g (α, β)} 1 let problem = [ 2 Var "alpha", Term("f",[Term("x",[])]); 3 Term("g",[Var "alpha"; Var "alpha"]), 4 Term("g",[Var "alpha"; Var "beta"]) ]
Mark Hills CS421 Lecture 13: Unification 14 / 19
Outline Objectives The Problem The Algorithm Implementation Activity
◮ (^) To unify a problem p... ◮ (^) If p is [], then you are done. ◮ (^) Otherwise, decompose p into e::es. ◮ (^) If e is Var n, m, then substitute m for Var n in es. Then “emit” e::unify es ◮ (^) If the second part contains a variable, you need to put it at the end of es rather than emitting it. unify [Var "alpha", Term("f",[Term("x",[])]); Term("g",[Var "alpha"; Var "alpha"]), Term("g",[Var "alpha"; Var "beta"]) ] will become...
Mark Hills CS421 Lecture 13: Unification 15 / 19
Outline Objectives The Problem The Algorithm Implementation Activity
◮ (^) To unify a problem p... ◮ (^) If p is [], then you are done. ◮ (^) Otherwise, decompose p into e::es. ◮ (^) If e is Var n, m, then substitute m for Var n in es. Then “emit” e::unify es ◮ If the second part contains a variable, you need to put it at the end of es rather than emitting it. (Var "alpha", Term("f",[Term("x",[])])) :: unify [Term("g",[Term("f",[Term("x",[])]); Term("f",[Term("x",[])])]); Term("g",[Term("f",[Term("x",[])]); Var "beta"])]
Outline Objectives The Problem The Algorithm Implementation Activity
◮ (^) If e consists of a pair Term(f,[x 1 ; x 2 ; · · · ; xn]), Term(f,[y 1 ; y 2 ; · · · ; yn])... ◮ (^) Note, term names and arities are the same. ◮ (^) Then the problem becomes
unify((
⋃^ n
i=
[(xi, yi)]) ∪ es)
(Var "alpha", Term("f",[Term("x",[])])) :: unify [Term("f",[Term("x",[])]), Term("f",[Term("x",[])])] @ unify [Term("f",[Term("x",[])]); Var "beta"])]