Docsity

Unification Slides lecture, Slides of Computer Science

Partial preview of the text

Download Unification Slides lecture and more Slides Computer Science in PDF only on Docsity!

Unification

Dr. Mattox Beckman

University of Illinois at Urbana-Champaign Department of Computer Science

Objectives

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.

The Problem

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.

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 }. 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.

Example

(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.

Example

(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.

Example

(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.

Example

(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 )}

Unification Failures

There are two situations that can cause unification to fail:

  1. A pattern mismatch

f ( x ) = g (α), h ( y ) = h ( z )

  1. Failing the “occurs check”

f (α) = f ( f (α))

Implementation

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.

Example – Types

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]}

Type Checking Solution

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].

Type Checking 2 Solution

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]!