Docsity

CS421 Lecture 13: Unification - Pattern Matching & Type Checking in Programming - Prof. El, Study notes of Computer Science

Toggle sidebar
bg1
Outline
Objectives
The Problem
The Algorithm
Implementation
Activity
CS421 Lecture 13: Unification1
Mark Hills
University of Illinois at Urbana-Champaign
June 26, 2008
1Based on slides by Mattox Beckman, as updated by Vikram Adve, Gul
Agha, and Elsa Gunter
Mark Hills CS421 Lecture 13: Unification 1 / 19
Outline
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
Objectives
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 Matching
Type Checking
Type Inference
The Domain
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 Matching
Type Checking
Type Inference
The Domain, cont.
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)
Mark Hills CS421 Lecture 13: Unification 5 / 19
Outline
Objectives
The Problem
The Algorithm
Implementation
Activity
The Problem Domain
The Problem
Pattern Matching
Type Checking
Type Inference
The Problem
◮Given terms sand t, try to find a substitution σsuch that
σ(s) = σ(t).
◮If such a substitution exists, it is said that sand tunify.
◮Aunification problem is a set of equations
S={s1=t1,s2=t2,...}.
◮A unification problem S={x1=t1,x2=t2,...}is in solved
form if
◮the terms xiare 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.
Mark Hills CS421 Lecture 13: Unification 6 / 19
pf3
pf4
pf5

Partial preview of the text

Download CS421 Lecture 13: Unification - Pattern Matching & Type Checking in Programming - Prof. El and more Study notes Computer Science in PDF only on Docsity!

Objectives The Problem The Algorithm Implementation Activity

CS421 Lecture 13: Unification^1

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

Objectives

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

The Domain

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

The Domain, cont.

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

The Problem

◮ (^) 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

Example — Pattern Matching

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

Example — Type Checking

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 = 3 # (ListAssoc [(1,2); (3,5)] 3) < 1.0;; (* ERROR ) 4 # (ListAssoc [(1,2.0); (3,5.0)] 3) < 1.0;; ( bool *) ◮ (^) Let γ be the type of the return value of ListAssoc in last step ◮ (^) Function application tells us: list(α, β) = list(int, float) and α = int ◮ (^) The comparison requires γ = float. The result tells us γ = β. ◮ (^) S = {list(α, β) = list(int, float); γ = β; γ = float} ◮ (^) Let σ = {α 7 → int; β 7 → float; γ 7 → float} Mark Hills CS421 Lecture 13: Unification 8 / 19

Outline Objectives The Problem The Algorithm Implementation Activity

The Problem Domain The Problem Pattern MatchingType Checking Type Inference

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

Four Operations

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

Example 1

(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

Example 1

(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

Example 2

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

Example 2

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

Example 2

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

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

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

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

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

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

How to do it....

◮ (^) 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

The Rules

◮ (^) 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

The Rules

◮ (^) 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

Decomposing

◮ (^) 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"])]