Docsity

Unification: A Topic in CS 440 for Solving Constraints - Prof. Alan Mattox Beckman, Study notes of Programming Languages

Toggle sidebar
bg1
CS 440 — Unification March 4, 2009
1 Introduction and Objectives
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.
•Be able to describe the problem of unification.
•Be able to solve a unification problem.
•Know how to use unification to implement pattern matching.
•Know how to use unification to check types of functions.
2 The Problem
The problem of unification is to find a single solution that satisfies multiple sets of constraints. A
constraint is a way of specifying a solution to a problem, where some of the elements of the solution
are specified precisely, but other elements may be less precise.
For example, suppose you have two advisors. Your first advisor wants you to take a theory
course and also CS 440. There are several theory courses you could take. Register for one of those,
and register for CS 440, and you have satisfied the problem, and your advisor.
Now suppose your second advisor wants you to take CS 430 and a language course. There are
several language courses that you could take. Take one of those, and take CS 430, and your second
advisor is happy.
The problem of unification comes in when we ask, ā€œIs there a way to register for classes that
make both advisors happy?ā€. We want one solution that simultaneously solves both constraints. In
this case, we do have a solution: take CS 430 and CS 440.
2.1 The Domain
Of course, we need to do this more formally. We will use a notation that will look something like
function applications. In actuality, they are patterns, much like the ones you use when you write a
function in Haskell. There are two kinds of patterns: terms and variables.
Terms have names, to be written using the Roman alphabet, and arity, a fancy way of saying
ā€œnumber of argumentsā€. That number may turn out to be zero. Such terms will look like variables
in algebra, but it’s important to remember that they are patterns.
Examples of terms include x,z,f(x,y), and x(y,f,z).
We write variables using the Greek alphabet, and we may subscript them if necessary. Examples
include α, β12,and γ7.
Using terms and variables, we can specify constraints. For example, we could say that sis the
schedule function. The first argument is a theory course, and the second argument is a language
course. The first advisor’s constraint might look like s(α, c440). The second advisor’s constraint
would look like s(c430, β).
If we want to specify a solution to the constraint, we need to provide a mapping from variables
to terms. The solution to the first advisor’s constraint would be σ1={α7→ c430 }. This solution
σ1is also called a substitution. A substitution could involve multiple variables, such as σ={α7→
f(x, β), β 7→ y}.
Notationally, these substitutions are applied like functions. So, σ1(s(α, c440 )) = s(c430, c440 ).
Mattox Beckman Page 1 Illinois Institute of Technology
pf3
pf4
pf5

Partial preview of the text

Download Unification: A Topic in CS 440 for Solving Constraints - Prof. Alan Mattox Beckman and more Study notes Programming Languages in PDF only on Docsity!

1 Introduction and Objectives

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.

  • Be able to describe the problem of unification.
  • Be able to solve a unification problem.
  • Know how to use unification to implement pattern matching.
  • Know how to use unification to check types of functions.

2 The Problem

The problem of unification is to find a single solution that satisfies multiple sets of constraints. A constraint is a way of specifying a solution to a problem, where some of the elements of the solution are specified precisely, but other elements may be less precise. For example, suppose you have two advisors. Your first advisor wants you to take a theory course and also CS 440. There are several theory courses you could take. Register for one of those, and register for CS 440, and you have satisfied the problem, and your advisor. Now suppose your second advisor wants you to take CS 430 and a language course. There are several language courses that you could take. Take one of those, and take CS 430, and your second advisor is happy. The problem of unification comes in when we ask, ā€œIs there a way to register for classes that make both advisors happy?ā€. We want one solution that simultaneously solves both constraints. In this case, we do have a solution: take CS 430 and CS 440.

2.1 The Domain

Of course, we need to do this more formally. We will use a notation that will look something like function applications. In actuality, they are patterns, much like the ones you use when you write a function in Haskell. There are two kinds of patterns: terms and variables. Terms have names, to be written using the Roman alphabet, and arity, a fancy way of saying ā€œnumber of argumentsā€. That number may turn out to be zero. Such terms will look like variables in algebra, but it’s important to remember that they are patterns. Examples of terms include x, z, f(x,y), and x(y,f,z). We write variables using the Greek alphabet, and we may subscript them if necessary. Examples include α, β 12 , and γ 7. Using terms and variables, we can specify constraints. For example, we could say that s is the schedule function. The first argument is a theory course, and the second argument is a language course. The first advisor’s constraint might look like s(α, c 440 ). The second advisor’s constraint would look like s(c 430 , β). If we want to specify a solution to the constraint, we need to provide a mapping from variables to terms. The solution to the first advisor’s constraint would be σ 1 = {α 7 → c 430 }. This solution σ 1 is also called a substitution. A substitution could involve multiple variables, such as σ = {α 7 → f(x, β), β 7 → y}. Notationally, these substitutions are applied like functions. So, σ 1 (s(α, c 440 )) = s(c 430 , c 440 ).

2.2 The Problem

The problem is, given constraints S and T , is 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 terms 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.

3 The Algorithm

Start with a unification problem S = {s 1 =?^ t 1 , s 2 =?^ t 2 ,.. .} and apply the following transforma- tions 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.

3.1 Example 1

(Stolen from ā€œTerm Rewriting and All Thatā€) {α =?^ f (x), g(α, α) =?^ g(α, β)}

4 Activity

Try to unify the following:

  • {f (α, y) = f (x, β)}
  • {f (α, y) = f (x, α)}
  • {f (α, β) = γ, γ = f (x, Ī“), β = g(y)}
  • {f (α, β) = γ, γ = f (x, Ī“)}

5 Implementation

To build a unifier, you need:

  • a way to represent unification problems... i.e., a type,
  • a way to decide which unification step is appropriate,
    • (and a way to tell when we are done)
  • and functions to perform the various unification steps.

How should we represent things?

5.1 Strategy for Writing the Function

You need three lists:

  • One is the list of solved-form equations.
  • Two form a queue of elements in progress.

You need functions to perform the transformations we need.

  • Substitute
  • Deconstruct
  • Reorient (easy)
  • Drop (very easy)

You may need a flag to indicate completion. Time to start coding...!