











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!












Dr. Mattox Beckman
University of Illinois at Urbana-Champaign Department of Computer Science
You should be able to ...
Unification is a third major topic that will appear many times in this course. It is used in languages such as Haskell and Prolog, and also in theoretical discussions. I (^) Describe the problem that unification solves. I (^) Solve a unification problem. I (^) Implement unification in Haskell. I (^) Describe some use cases for unification.
I (^) Given terms s and t , try to find a substitution σ such that σ( s ) = σ( t ). I (^) If such a substitution exists, it is said that s and t unify. I (^) A unification problem is a set of equations S = { s 1 = t 1 , s 2 = t 2 ,.. .}. I (^) A unification problem S = { x 1 = t 1 , x 2 = t 2 ,.. .} is in solved form if I (^) The terms xi are distinct variables. I (^) 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.
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 }. 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.
(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.
(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. {α = f ( x ), g ( f ( x ), f ( x )) = g ( f ( x ), β)} We can use the decompose method, and get rid of the g functions.
(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. {α = f ( x ), g ( f ( x ), f ( x )) = g ( f ( x ), β)} We can use the decompose method, and get rid of the g functions. {α = f ( x ), f ( x ) = f ( x ), f ( x ) = β} We can delete the f ( x ) = f ( x ) equation. {α = f ( x ), f ( x ) = β} Now we can reorient to make the variables show up on the left side.
(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. {α = f ( x ), g ( f ( x ), f ( x )) = g ( f ( x ), β)} We can use the decompose method, and get rid of the g functions. {α = f ( x ), f ( x ) = f ( x ), f ( x ) = β} We can delete the f ( x ) = f ( x ) equation. {α = f ( x ), f ( x ) = β} Now we can reorient to make the variables show up on the left side. {α = f ( x ), β = f ( x )} Now we are done .... S = {α 7 → f ( x ), β 7 → f ( x )}
There are two situations that can cause unification to fail:
f ( x ) = g (α), h ( y ) = h ( z )
f (α) = f ( f (α))
To implement this in a programming language: I (^) Keep two lists: one for the incoming equations, one for the solved variables. I (^) Remove the first element of the incoming list. I (^) Decompose and delete manipulate the incoming list. I (^) Orient and eliminate can be handled in one case. I (^) Your solution list contains the result once the incoming list is empty.
Type checking is also a form of unification.
map :: (a -> b) -> [a] -> [b] inc :: Int -> Int foo :: [Int]
Will map(inc)(foo) work?
S = {(α ⇒ β) = (Int ⇒ Int), List[α] = List[Int]}
S = {(α ⇒ β) = (Int ⇒ Int), List[α] = List[Int]}
I (^) Decompose: {α = Int, β = Int, List[α] = List[Int]} I (^) Substitute: {α = Int, β = Int, List[Int] = List[Int]} I (^) Delete: {α = Int, β = Int}
The original type of map was (α ⇒ β) ⇒ List[α] ⇒ List[β]. We can use our pattern to get the output type: S (List[β]) ≡ List[Int].
S = {(α ⇒ β) = (String ⇒ Int), List[α] = List[Int]}
I (^) Decompose: {α = String, β = Int, List[α] = List[Int]} I (^) Substitute: {α = string, β = Int, List[String] = List[Int]} I (^) Error: List[string] 6 = List[Int]!