Introduction The Problem The Algorithm Use Cases Introduction The Problem The Algorithm Use Cases Objectives You should be able to ... Unifjcation Unifjcation 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. Dr. Mattox Beckman ◮ Describe the problem that unifjcation solves. ◮ Solve a unifjcation problem. University of Illinois at Urbana-Champaign Department of Computer Science ◮ Implement unifjcation in Haskell . ◮ Describe some use cases for unifjcation. Introduction The Problem The Algorithm Use Cases Introduction The Problem The Algorithm Use Cases The Domain The Problem Terms Have name and arity ◮ The name will be in western alphabet. ◮ Given terms s and t , try to fjnd a substitution σ such that σ ( s ) = σ ( t ) . ◮ Arity = “number of arguments” – may be zero ◮ If such a substitution exists, it is said that s and t unify. ◮ Examples: x , z , f(x,y) , x(y,f,z) ◮ A unifjcation problem is a set of equations S = { s 1 = t 1 , s 2 = t 2 , . . . } . Variables Written using Greek alphabet, may be subscripted ◮ A unifjcation problem S = { x 1 = t 1 , x 2 = t 2 , . . . } is in solved form if ◮ Represent a target for substitution ◮ The terms x i are distinct variables. ◮ Examples: α, β 12 , γ 7 ◮ None of them occur in t i . Substitutions Mappings from variables to terms Our approach: given a unifjcation problem S , we want to fjnd the most general unifjer σ that ◮ Examples: σ = { α �→ f ( x , β ) , β �→ y } solves it. We will do this by transforming the equations. ◮ Substitutions are applied : σ ( g ( β )) → g ( y ) Note: arguments to terms may have non-zero arity, or may be variables.
Introduction The Problem The Algorithm Use Cases Introduction The Problem The Algorithm Use Cases Four Operations Example Start with a unifjcation 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. (Stolen from “Term Rewriting and All That”) { α = f ( x ) , g ( α, α ) = g ( α, β ) } Decompose An equation f ( t n ) = f ( u n ) can be replaced by the set { t 1 = u 1 , . . . , t n = u n } . 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 . Introduction The Problem The Algorithm Use Cases Introduction The Problem The Algorithm Use Cases Example Example (Stolen from “Term Rewriting and All That”) (Stolen from “Term Rewriting and All That”) { α = f ( x ) , g ( α, α ) = g ( α, β ) } { α = f ( x ) , g ( α, α ) = g ( α, β ) } We can use the eliminate method, replace α with f ( x ) on the right sides of the equations. 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.
2. Failing the “occurs check” f f f Introduction The Problem The Algorithm Use Cases Introduction The Problem The Algorithm Use Cases Example Example (Stolen from “Term Rewriting and All That”) (Stolen from “Term Rewriting and All That”) { α = f ( x ) , g ( α, α ) = g ( α, β ) } { α = f ( x ) , g ( α, α ) = g ( α, β ) } We can use the eliminate method, replace α with f ( x ) on the right sides of the equations. 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 ) , β ) } { α = f ( x ) , g ( f ( x ) , f ( x )) = g ( f ( x ) , β ) } We can use the decompose method, and get rid of the g functions. We can use the decompose method, and get rid of the g functions. { α = f ( x ) , f ( x ) = f ( x ) , f ( x ) = β } { α = f ( x ) , f ( x ) = f ( x ) , f ( x ) = β } We can delete the f ( x ) = f ( x ) equation. 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. Introduction The Problem The Algorithm Use Cases Introduction The Problem The Algorithm Use Cases Example Unifjcation Failures (Stolen from “Term Rewriting and All That”) { α = f ( x ) , g ( α, α ) = g ( α, β ) } There are two situations that can cause unifjcation to fail: We can use the eliminate method, replace α with f ( x ) on the right sides of the equations. 1. A pattern mismatch { α = 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 ) = g ( α ) , h ( y ) = h ( z ) { α = 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 = { α �→ f ( x ) , β �→ f ( x ) }
inc :: Int -> Int map :: (a -> b) -> [a] -> [b] foo :: [Int] Introduction The Problem The Algorithm Use Cases Introduction The Problem The Algorithm Use Cases Unifjcation Failures Implementation There are two situations that can cause unifjcation to fail: To implement this in a programming language: 1. A pattern mismatch ◮ Keep two lists: one for the incoming equations, one for the solved variables. ◮ Remove the fjrst element of the incoming list. f ( x ) = g ( α ) , h ( y ) = h ( z ) ◮ Decompose and delete manipulate the incoming list. 2. Failing the “occurs check” ◮ Orient and eliminate can be handled in one case. ◮ Your solution list contains the result once the incoming list is empty. f ( α ) = f ( f ( α )) Introduction The Problem The Algorithm Use Cases Introduction The Problem The Algorithm Use Cases Example – Compatibility Example – Types ◮ Your advisor wants you to take CS 421 and some theory class. Type checking is also a form of unifjcation. ◮ Your mom wants you to take CS 374 and some language class. ◮ Can both your advisor and your mom be happy? This is a problem we can solve using unifjcation: ◮ Let f be a “schedule function,” the fjrst argument is a language class, the second argument is a theory class. Will map(inc)(foo) work? ◮ s = f ( cs 421 , β ) (where β is a theory class) S = { ( α ⇒ β ) = ( Int ⇒ Int ) , List [ α ] = List [ Int ] } ◮ t = f ( α, cs 374) (where α is a language class) ◮ Let σ = { α �→ cs 421 , β �→ cs 374 }
inc : String -> Int foo : [Int] map :: (a->b) -> [a] -> [b] Introduction The Problem The Algorithm Use Cases Introduction The Problem The Algorithm Use Cases Type Checking Solution Example 2 – Types Here’s an example that fails. S = { ( α ⇒ β ) = ( Int ⇒ Int ) , List [ α ] = List [ Int ] } ◮ Decompose: { α = Int , β = Int , List [ α ] = List [ Int ] } ◮ Substitute: { α = Int , β = Int , List [ Int ] = List [ Int ] } ◮ Delete: { α = Int , β = Int } Will map(inc)(foo) work? The original type of map was ( α ⇒ β ) ⇒ List [ α ] ⇒ List [ β ] . S = { ( α ⇒ β ) = ( String ⇒ Int ) , List [ α ] = List [ Int ] } We can use our pattern to get the output type: S ( List [ β ]) ≡ List [ Int ] . Introduction The Problem The Algorithm Use Cases Type Checking 2 Solution S = { ( α ⇒ β ) = ( String ⇒ Int ) , List [ α ] = List [ Int ] } ◮ Decompose: { α = String , β = Int , List [ α ] = List [ Int ] } ◮ Substitute: { α = string , β = Int , List [ String ] = List [ Int ] } ◮ Error: List [ string ] � = List [ Int ] !
Recommend
More recommend