# A Unification Algorithm for Second-Order Linear Terms

Gilles Dowek

► To cite this version:

Gilles Dowek. A Unification Algorithm for Second-Order Linear Terms. 1993. [hal-04195982](https://HAL Id: hal-04195982)

Preprint submitted on 5 Sep 2023

HAL is a multi-disciplinary open access
archive for the deposit and dissemination of sci-
entific research documents, whether they are pub-
lished or not. The documents may come from
teaching and research institutions in France or
abroad, or from public or private research centers.

L'archive ouverte pluridisciplinaire HAL, est
destinée au dépôt et à la diffusion de documents
scientifiques de niveau recherche, publiés ou non,
émanant des établissements d'enseignement et de
recherche français ou étrangers, des laboratoires
publics ou privés.

<figure><img src="image_1.png" /></figure>

Distributed under a Creative Commons CC BY 4.0 - Attribution - International License
# A Unification Algorithm for Second-Order Linear Terms

Gilles Dowek

INRIA*

We give an algorithm for the class of second order unification problems in which second order variables have at most one occurrence. As an application we give another algorithm that generalizes both first order unification and second order matching.

## 1 An Algorithm for Second Order Linear Unification Problems

We consider simply typed lambda-calculus with $\alpha, \beta$ and $\eta$ rule. Variables and constants are restricted to be at most second order. A unification problem $N$ is a set of equations $a = b$ such that $a$ and $b$ have the same type $T$ and this type is first order. A solution of a unification problem $N$ is a substitution $\sigma$ such that for each equation $a = b$ of $N$, $\sigma a = \alpha\beta\eta \sigma b$.

**Remark:** Normal terms of a first order type are atomic, i.e. they have the form ($f a_1 ... a_n$), where $f$ is a variable or a constant. Such a term is said to be flexible when $f$ is a variable, it is said to be rigid when $f$ is a constant.

A second order unification problem is said to be linear when each second order variable has at most one occurrence in the problem (without any restriction on the number of occurrences of constants and first order variables).

A second order unification problem is said to be superficial if all the equations of this problem have the form ($F a_1 ... a_n$) = ($G b_1 ... b_p$) where all the variables that occur in the terms $a_1, ..., a_n, b_1, ..., b_p$ are first order, i.e. second order variables can only occur as head symbols.

**Proposition:** There exists an algorithm that transforms each second order unification problem $N$ into a second order superficial unification problem which is equivalent to $N$.

While there is an equation of the form

$$ (F ... a ...) = t $$

where $F$ is a second order variable or constant, $t$ a term, and $a$ a term which is not a variable, replace this equation by

$$ (F ... x ...) = t $$

$$ x = a $$

where $x$ is a fresh first order variable. This process obviously terminates.

**Corollary:** (Amiot [1]) There is no decision algorithm for second order superficial problems.

**Remark:** If the problem we start with is linear then so is the superficial problem built from it.

**Proposition:** There exists an algorithm that solves second order superficial linear unification problems.

Let us consider equations between a flexible term and a rigid term of a second order superficial problem. If the head variable of the flexible term is first order then the equation has the form

$$ x = b $$

where $x$ is a first order variable and all the variables occurring in the term $b$ are first order. If the head variable of the flexible term is second order then the equation has the form

$$ (f a_1 ... a_n) = b $$

<footnote>*B.P. 105, 78153 Le Chesnay CEDEX, France. Gilles.Dowek@inria.fr</footnote>
where f is a second order variable and all the variables occurring in the terms $a_i$ and b are first order.

We consider a variant of the unification algorithm of [2] [3] in which we consider an equation on the form $x = b$ such that all the variables occurring in the term b are first order, if the variable $x$ does not occurs in b then one substitutes b for $x$ and otherwise one fails. When restricted to second order superficial unification problems, the projection and imitation substitutions are considered in this algorithm only when the head variable of the flexible term is second order.

**Lemma:** The unification tree of a second order superficial linear unification problem is finite.

Let $N$ be a problem. Let $v(N)$ be the number of distinct first order variables occurring in $N$. Let $W(N) = \{t_1, ..., t_n\}$ be the multiset of rigid terms of equations between a flexible and a rigid term such that the head variable of the flexible term is second order. Let $w(N) = |t_1| + ... + |t_n|$, where $|t|$ is the number of variable occurrences in the term $t$.

Let $N$ be a second order superficial linear unification problem and $N'$ a son of this problem in the unification tree. Then $(v(N'), w(N')) < (v(N), w(N))$ for the lexicographic order on $\omega^2$.

* When we instantiate a first order variable $v(N') < v(N)$.

* When we perform a projection substitution, a term gets out from $W(N)$, $w(N') < w(N)$ and $v(N') = v(N)$.

* When we perform an imitation substitution, we replace the equation

$$ (f a_1 ... a_n) = (\Phi b_1 ... b_p) $$

by

$$ (h_1 a_1 ... a_n) = b_1 $$

...

$$ (h_p a_1 ... a_n) = b_p $$

The term $(\Phi b_1 ... b_p)$ gets out from $W(N)$ and the terms $b_1, ..., b_p$ get in, $w(N') < w(N)$ and $v(N') = v(N)$.

**Proposition:** There exists an algorithm that solves second order linear unification problems.

**Remark:** In the algorithm above, both variables and constants are restricted to be at most second order and we consider only equations between terms of a first order type. A similar algorithm can be designed for the case in which constants and bound variables are at most third order and equations relate terms of an arbitrary type.

The main problem is one cannot transform the problem $[x_1 : T_1]...[x_n : T_n](F ... a ... ) = t$ into the problem $[x_1 : T_1]...[x_n : T_n](F ... x ... ) = t$ and $x = a$ because the variables $x_1, ..., x_n$ may occur into the term $a$. One way to avoid this problem to use mixed prefixes [5]. In order that flexible-flexible equation have a solution we consider only unification problems such that the prefix contains a constant in each first order type in the left of all the declaration of existential variables.

So we use a variant of the unification algorithm of [5] such that when one considers an equation $x = b$ of a unification problem in a prefix $Q$, if $b$ is a term in which all the variables are first order then if $b$ contains a constant that is declared on the right to $x$ or $b$ contains the variable $x$ one fails otherwise one substitutes to $x$ the term $b'$ obtained by replacing in $b$ all the variables by fresh variables. Then the problems simplifies in a sequence of equations $x = y$ where $x$ and $y$ are first order and one substitutes the rightmost for the leftmost.

In the formalism of mixed prefixes, in the algorithm that transforms a problem into a superficial problem one replaces an equation $(F ... a ... ) = t$ into the equations $(F ... x ... ) = t$ and $x = a$ and the prefix $Q$ into the prefix $Q[\exists x : T]$.
## 2 An Algorithm that Generalizes First Order Unification and Second Order Matching

**Remark:** The flexible-flexible equations that remain in success node have either the form $x = t$ (if one of the head variables is first order) or $(f a_1 ... a_n) = (g b_1 ... b_p)$ (if both head variables are second order). For equations of the first type, one can substitute the term $t$ for $x$. If after this substitutions all the success nodes are empty then one gets a finite complete set of unifiers.

**Remark:** The algorithm above can be used to construct a unification algorithm for second order terms that either gives a (possibly empty) complete set of unifiers or fails to give any information.

Take a second order unification problem, replace the $n$ occurrences of each second order variable by new variables $f_1, ..., f_n$, apply the algorithm above. If

* each success nodes of the unification tree is empty,

* for each success node and each second order variable $f$ of the linear problem, $f$ is either substituted by a closed term or left unbound

then select the success nodes such that all the closed terms substituted to the variables $f_1, ..., f_n$ taking place of a variable $f$ of the initial problem are equal, else fail to give any information.

**Remark:** The algorithm above generalizes both first order unification [7] and second order matching [3] [4] as for problems in these classes it does not fail and gives a complete set of unifiers.

## References

[1] G. Amiot, The Undecidability of the Second Order Predicate Unification Problem.

[2] G. Huet, A Unification Algorithm for Typed $\lambda$-calculus, *Theoretical Computer Science*, 1, 1975, pp. 27-57.

[3] G. Huet, Résolution d'Équations dans les Langages d'Ordre 1,2, ..., $\omega$, *Thèse de Doctorat d'État*, Université de Paris VII, 1976.

[4] G. Huet, B. Lang, Proving and Applying Program Transformations Expressed with Second Order Patterns, *Acta Informatica*, 11, 1978, pp. 31-55.

[5] D. A. Miller, Unification Under a Mixed Prefix, *Journal of Symbolic Computation*, 14, 1992, pp. 321-358.

[6] Ch. Prehofer, Decidable Higher-Order Unification Problems, Manuscript, 1993.

[7] J. A. Robinson, A Machine-Oriented Logic Based on the Resolution Principle, *Journal of the Association for Computing Machinery*, 12, 1, 1965, pp. 23-41.