Unifying Nominal Uniﬁcation ∗ Christophe Calvès

Nominal uniﬁcation is proven to be quadratic in time and space. It was so by two diﬀerent approaches, both inspired by the Paterson-Wegman linear uniﬁcation algorithm, but dramatically diﬀerent in the way nominal and ﬁrst-order constraints are dealt with. To handle nominal constraints, Levy and Villaret introduced the notion of replacing while Calvès and Fernández use permutations and sets of atoms. To deal with structural constraints, the former use multi-equations in a way similar to the Martelli-Montanari algorithm while the later mimic Paterson-Wegman. In this paper we abstract over these two approaches and genralize them into the notion of modality, highlighting the general ideas behind nominal uniﬁcation. We show that replacings and environments are in fact isomorphic. This isomorphism is of prime importance to prove intricate properties on both sides and a step further to the real complexity of nominal uniﬁcation.


Introduction
Operators with binding are ubiquitous in computer science.Programs, logic formulas, types and proofs are some examples of systems that involve binding.Program transformations, optimisations and compilation, for instance, are defined as operations on programs and should work uniformly on α-equivalence classes.Manipulating terms up to α-equivalence is not easy [8].Gabbay and Pitts introduced nominal syntax [7,11] to represent, in a simple and natural way, systems that include binders by extending first-order syntax to provide support for binding operators.
The nominal approach to the representation of systems with binders is characterised by the distinction, at the syntactical level, between atoms (or object-level variables), which can be abstracted (we use the notation a.t, where a is an atom and t is a term), and meta-variables (or just variables), which behave like first-order variables but may be decorated with atom permutations.Permutations are generated using swappings (e.g., (a b)•t means swap a and b everywhere in t).For instance, (a b)•λa.a= λb.b,and (a b)•λa.X = λb.(ab)•X where λ is here a function symbol (we will introduce the notation formally in the next section).As shown in this example, permutations suspend on variables.The idea is that when a substitution is applied to X in (a b)•X, the permutation will be applied to the term that instantiates X. Permutations of atoms are one of the main ingredients in the definition of α-equivalence for nominal terms.
Urban, Pitts and Gabbay [13] showed that nominal unification, i.e. unification up to α-equivalence, is decidable.They gave an algorithm to find the most general solution to a nominal unification problem, if one exists.A naive implementation, representing terms as trees, is exponential.Cheney proved that a more general form, called equivariant unification, is NP-complete [5].Fortunately nominal unification was proven to be polynomial [5] and even quadratic [9] using reduction to higher-order patterns [12].
Levy and Villaret [10], and Calvès and Fernández [1,2] presented, independently, two very different algorithms to solve nominal unification in quadratic time and space.Both were inspired by the Paterson-Wegman first-order unification algorithm.But they dramatically differ in the way nominal constraints and equations are dealt with: Levy and Villaret use replacings (i.e.sequences of abstractions) and multi-equations rewriting system while Calvès and Fernández use environments (i.e.permutations and sets of atoms).While being two very different structures, environments and replacings share the same goal: representing constraints generated by abstractions.The actual complexity of nominal unification is still unknown.Could another representation for nominal or first-order constraints lead to a more efficient algorithm?To answer this question we need to abstract over technical details such as representation of these constraints.
Our constributions are: We show that the algorithms in [10] and [1,2] can be unified.The result is a general abstract nominal-unification algorithm that unifies any algorithm based on Paterson-Wegman first-order linear unification and nominal constraints.We develop a general notion of nominal modality to enable reasoning about any data structure representing nominal constraints.We showed that the unification algorithm actually relies on four modality operations, so does its complexity.We prove that these strutures are isomorphic.So in particular, environments and replacings are.This also means that any representation can be used to establish properties on any modality.We used this to exchange properties between replacings and environments.
The paper is structured as follows.Section 2 presents nominal terms.Section 3 introduces the notion of nominal modality as an abstraction of any data structure used to represent nominal constraints.Section 4 defines operations on modalities.Section 5 establishes properties on any modality.Then section 7 presents the unification algorithm on any modality.Section 8 is about the complexity of modalities' operations.Section 9 discusses related and future work.Finally section 10 concludes.

Background
Let Σ be a denumerable set of function symbols f , g, . . .; X a denumerable set of variables X, Y, . ..; and A a denumerable set of atoms a, b, c, d . . .We assume that these sets are pairwise disjoint.In the intended applications, variables will be used to denote metalevel variables (unknowns), and atoms will be used to represent object-level variables, which can be bound (for instance, atoms may be used to represent the variables of a programming language on which we want to reason).Let Π denote the set of finite permutations over A. Its elements are written π,π i ,π ,. . .In this paper, we represent permutations by lists of swappings ((a 1 b 1 ) . . .(a n b n )).The empty list is written id, inversion and composition are written respectively π −1 and π where a.s is an abstraction and π•X a suspension.
The action of a permutation π on a term s, written π•s, is defined by Definition 2 (Subterms).Let s and t be two nominal terms.s N t means that s is a subterm of t (possibly t itself).s ≺ N t means that s is a strict subterm of t.
Nominal constraints have the form a#t and s ≈ t (read "a fresh for t" and "s α-equivalent to t", respectively).A freshness context γ is a set of freshness constraints of the form a#X. We define the validity of constraints under a freshness context γ inductively, by a system of axioms and rules, using # in the definition of ≈ (see figure 1).In this figure, we write ds(π, π )#X as an abbreviation for {a#X | a ∈ ds(π, π )}, where ds(π, π ) = {a | π•a = π •a} is the set of atoms where π and π differ (i.e., their difference set).a, b are any pair of distinct atoms.The relation ≈ is indeed an equivalence relation (see [13] for more details).
Let R = P(N 2 ) be the set of binary relations on N and R α ∈ R be the α-equivalence relation on N .Let ∆ be the set of finite sets of atoms.Let δ,δ i ,δ ,. . .denote elements of ∆.
Substitutions are mappings from variables X to nominal terms, written σ,σ ,. . . .Composition of substitutions is written σ • σ .tσ represent the term where every variable X in t has been replaced by σ(X).

Nominal Modality
We can express the relation f (s This section aims to express any α-equivalence between two nominal terms in terms of (modal) relations between their subterms.The unification algorithm relies on the ability to compute (efficiently) such relations.More precisely, these relations form a family and can be indexed.This section studies those indexes.

Nominal Pre-Modality
..n} be a finite sequence of pair of atoms : For example, R provided with Φ R = id is a pre-modality.
Remark.In the rest of this paper Φ M (m) will be written R m .Furthermore, for any nominal terms s and t, (s, t) ∈ R m will be written s ≈ m t.Definition 5. Let M be a pre-modality.Let ∼ be the kernel pair of Φ M , i.e. the equivalence relation on M defined by: ∀m By abuse of notation, for any pre-modalities M and M , we say that (m In the rest of the paper, when it is not ambiguous, e will denote the neutral element of the considered pre-modality.
Corollary 9. Let M be a pre-modality.For any m ∈ [M ] there exists a finite sequence of pair of atoms Proposition 3.For any pre-modality M , the restriction Φ

Nominal Modality
Definition 10.A nominal modality M , or modality for short, is a set provided with a bijection Φ Corollary 11.Let M be a modality, then M is a pre-modality and [M ] ∼ = M .Conversely, let M be a pre-modality, then [M ] is a modality.Proposition 4. Let M and M be two modalities.There exist a unique bijection φ : M → M such that: φ(e M ) = e M where e M (resp.e M ) is the neutral element of Such a bijection is called a nominal-modality isomorphism, or modality isomorphism for short, and is writen Φ M →M .

Environments
This section shows that the set of environments [3] is a pre-modality.Thus, by isomorhism, properties on environments can be transposed to any modality.Definition 12.An environment is a pair (π, δ) of a permutation and a set of atoms.The set of environments (Π × ∆) is written E. Proposition 5. E is a pre-modality provided with the function Φ E : E → R : for any atoms a and b: Proposition 6.Let ξ = (π, δ) and ξ = (π , δ ) be two environments: Let π be a permutation and δ = {a 1 , . . ., a n } ∈ ∆ (atoms a i are considered, without loss of generality, distinct), a i = π(a i ) and b ∈ δ: Let π be a permutation, there exists a finite sequence of pair of atoms Thus, for any (pre-)modality M and m ∈ M , we can define the freshness set of m using the isomorphism between environments and M .Precisely: Definition 13.For any modality M and any m ∈ M , let ξ(m) ∈ E /∼ be the environment equivalence class defined by ξ(m) = Φ M →E /∼ (m).We write δ(m) for the finite set of atoms and Π(m) for the set of permutations such that ξ(m) = Π(m) × {δ(m)}.

Simple Replacings
Simple replacings [10], or replacings for short, were introduced by Levy and Villaret as a draft for generalized replacings.Using them to handle nominal constraints is inefficient in practice but being sequences of abstraction they are useful on the theoritical side.Definition 14.A replacing is a finite sequence of pair of atoms written (a i ← b i ) i∈{1...n} .The set of replacings is written L.

Operations
This section presents all the operations on modal relations (and their modality conterparts) used in the unification algorithms.

Transposition
Definition 17.Let R ∈ R be a relation.The transpose of R, written t R, is defined by For any modality M and m ∈ M we can define the transpose of m, written t m,

Monoid
Definition 21 (Environment composition).Let ξ = (π, δ) and ξ = (π , δ ) be two environments.The composition of ξ and ξ , written ξ • ξ , is defined as Proposition 16.Let ξ ∈ E be an environment and e = (id, ∅) the neutral element of E: Definition 22 (Relation composition).Let R, R ∈ R be two relations.We define the composition of R and R , written R • R , as Corollary 23.Let R and R be two relations in [R].The following propositions hold: provided with R α as its neutral element and the relation composition (•) as its internal law is a monoid.
Definition 24.Any modality M can be given a monoid structure whose neutral element is e M and whose internal law (•) :

Properties
This section presents basic properties on modalities used in the unification algorithm.In the following M denotes a modality and m, m , . . .denote elements of M .

Decomposition
These properties are proven by the isomorphism between M and E /∼ .

Resolution
Decomposition propositions are able to express relations on nominal terms into relations on their subterms but they are stuck faced to relations such as X ≈ m X or (s ≈ m t ∧ s ≈ m t).This subsection shows how to deal with such cases.

Modal Problems
Definition 27 (Equation).A solution of the equation s ≈ ?m t is a pair (σ, γ) where σ is a substitution and γ freshness context such that γ sσ ≈ m tσ holds.Two equations are said to be equivalent if they have the same set of solutions.

Definition 28 (Modal Probem).
A nominal modal problem P (or modal problem for short) is a set of equations of the form s ≈ ?m t .(σ, γ) is a solution of P if it is a solution for any equation s ≈ ?m t in P. Two problems are said to be equivalent if they have the same set of solutions.
Remark.Note that s ≈ m t is equivalent to t ≈t m s.So, in the following, every predicate, pattern, etc involving an equation s ≈ ?m t also matches the equation t ≈ ?t m s.For example, a ≈ ?(c←d)•e b ∈ {b ≈ ?(d←c)•e a}.
Definition 29 (Substitutions).We write X → m s for the elementary substitution defined, when δ(m) # X, by Definition 30 (Freshness constraints).We write m # X, when id ∈ Π(m), for the equation The unification algorithm of section 7.2 produces elementary substitution and freshness constraints.They are elementary parts of the incrementatly computed solution.To ease the reading of the paper we have chosen to write them as part of the problem but the reader should keep in mind that they are not inputs but outputs.The "problem" {X → m s, m # Y, s ≈ ?m t} actually represents the modal problem {s ≈ ?m t} where the algorithm has already output the elementary substitution X → m s and the freshness constraint m # Y .
Definition 31 (Fail).fail is to be considered, in the algorithm, as an exception.It means that the problem does not have any solution.

Graph Representation
Definition 32.The graph representation of a problem P, written G P , is the directed graph whose vertex (also called node) set, written G P V is the set of all nominal terms appearing in the problem: Remark.Let s = (a b)•t be a term appearing in P.This relation is not only a term edge but also an equivalence relation so it is also represented as an equivalence edge.These relations form the set {s ≈ ?(a←b)•(b←a)•e t | s, t ∈ G P V s = (a b)•t} which is contained in G P ≈ .Remark.Several structurally equal subterms can be represented as several nodes but there must be exactly one node per variable in the problem.
Remark.Note that equivalence of equations s ≈ ?m t and t ≈ ?t m s implies that an edge from s to t labelled by m is also an edge from t so s labelled by t m.Both are considered to be the same edge.
Term edges are good at representing term sharing, but they have the drawback that t is considered as a subterm of (a b)•t.So a clycle involving of term edge does not implies that the problem have no solution.For example the cycle is valid.We need a notion of term edges such that an edge from s to t means that t is a subterm of s but s and t can not be equivalent up to a modality: Definition 33 (Strict subterm).A strict term edge is an edge s π t from s to t such that either s = a.((a If the graph representation of a problem has a cycle involving a strict term edge, then the problem has no solution. Definition 34 (Path).Let Path be a predicate on pairs of nodes.Path(s, t) = if there exists a path from s to t.Let SPath be a predicate on pairs of nodes.SPath(s, t) = if there exists a strict path (a path involving a strict term edge) from s to t.
Definition 35 (Term Root).Given a problem P, a term root is a node in G P with no parent.A strict term root is a node with no strict parent.Definition 36 (Occurences).Given a problem P, the occurence of a node n, written occ(n) is defined as the number of its parents and equivalence arrows involing n: Definition 37. Given a graph representation G P .A ≈-connected component is a connected component for the graph (G P V , G P ≈ ) (same vertices but only equivalence edges).

Unifying Nominal Unification
Remark.≈-connected components represents classes of α-eqvuivalent terms up to a modality.
Remark (Garbage collection).Note that, as a representation of a problem, when a term diseappear from the problem, its node in the graph and all the edges involved disapear too.More precisely, when a term root does not appear in any equivalence edges, this node is garbage collected.All of its children become term roots and may be garbage collected to.

Unification Algorithm
Though [10] and [2] both rely on the Paterson-Wegnam first-oder linear algorithm, they use very different approaches.This section shows that even this part of the algorithm can be unified.In addition, thanks to the support operation on modalities, the present algorithm can stay general even when dealing with freshness constraints (see propositions 23 and 26).

Rules
The following rules never create nominal terms.Instead they rewrite edges of the graph representation of the problem and create elementaty substitutions/freshness constaints.
Definition 40 (Top to Bottom rules).
This is clear that all these rules preserve the set of solution as their left-hand sides are equivalent to their right-hand ones as shown in section 5.

The Paterson-Wegman Strategy
In this section we unify [10] and [2] as a strategy for the rules of section 7.1.

The Strategy
The strategy explores nodes by traversing ≈-connected components and assigning to each node n a component representative (repr(n)).A component is reduced if all of its nodes are term roots (top to bottom).
Definition 42.Let P be a problem.For any node n, repr(n) represents, if defined (⊥ otherwise), an equivalence edge n ≈ ?m r where r is the representative of the ≈-connected component of n.Initially repr(n) = ⊥ for every n.This defines a function repr on nodes treated as a global variable.
We want to reduce first ≈-connected components whose nodes are all term roots but looking for one would be inefficient.Instead we process a component until we find a node s which is not a term root.Let p be one of its parents.We need to reduce the component of p first to make s a term root.Any reduction performed on the component of s must wait for the one p to be resolved.We implement this priority system as a representative stack S.Only reduction involving the top element of S are allowed.This garantees that reductions are performed in the correct order.Definition 43.Let S be a node stack, treated as a global variable.We define two operations on S: push(n) pushes the node n on the stack and top represents its top element (if it exists).Note that when one node disappear from the problem, it is also removed from S.
This strategy performs stateful computations.The output (written O), the representative function repr and the representative stack S are global variables so we need to consider a state as a tupple composed of a problem and all of the global variables involved in the strategy.The state generated by Paterson-Wegman Strategy rules verifies some helpful properties so we only consider values of these variables that can be generated by the rules.Definition 44.An ouput, written O, is a set of elementary substitutions and freshness constraints generated by the Paterson-Wegman Strategy rules.
Definition 45 (State).The set of states S is defined as the smallet set of 4-tupple (P, O, repr, S) (where P is a problem, O is an output, repr is a repesentative function and S is a representative stack) such that : for any problem P, the initial state is (P, ∅, (_ → ⊥), ∅) ∈ S. if a state st ∈ S, and st is obtained by applying one of the Paterson-Wegman Strategy rules on st, then st ∈ S.
Definition 46 (Unification Algorithm).Let P be a problem (the input of the algorithm), take (P, ∅, (_ → ⊥), ∅) as the initial state.Then rewrite it using the following rules until a normal form is reached.If fail is raised, the problem is considered to have no solution.Otherwise consider the output of the normal state as the most-general unifier of the input problem.
Note that the following rules do work on states, but, in order to ease the reading of the paper, patterns use the expressions s ≈ ?m t and repr(s) to represent respectively the equivalence edge from s to t labelled by m and the image of s by the representative function.Similarly, right-hand sides use the expressions X → m r and m # X to represent respectively the addition to the output of the elementary substition and freshness constraint.The expression repr(s) := s ≈ ?m r means that the image of s by the representative function is set to to s ≈ ?m r.Failure rules are applied at every edge creation.This is done in constant time.Normalisation rules are applied mostly on repr at edge creation: Proposition 39.The algorithm computes the most-general unfier of a problem P if it exists or raise fail in a linear number of rewriting steps.

Modal Complexity
The complexity of every rewriting steps depends on the complexity of modal operations which itself depends on the modality used.
Let P be a problem and A P be the set of atoms appearing in P. The unification algorithm does not introduce any atom.So any modal operation computed by any rewriting step only involves atoms in A P .If any modal operation can be computed in at most θ(|A P |), the complexity of the unification algorithm is at most θ(|A P | × µ(G P )).
As proven in [3], modal operation on environments can be computed in θ(|A P |) using intergers as atoms and arrays as permutations and freshness sets.Thus using environments, the algorithm is quadratic in time.

Replacings
Computing eagerly replacings would be terribly inefficient.Levy and Villaret avoid this complexity by introducing generalized replacings which can be seen as a formulae of modal operations.Using subterm sharing, they get a directed acycling graphs of modal-operation formulas for which they compute the sets For(g) and Rew(g) to determine whether a ≈ ?g b is true or not.Remark.The definition in [10] does not contain supp(g).Instead, multiple occurences of the same variables are kept in multi-equations.
Proposition 40.GL is a pre-modality.

Proposition 41. GL /∼ = [GL]
The size of the acyclic graph representing modal-operation formulae is linear in the size of the input problem P because a linear number of Paterson-Wegman Strategy rules lead to a normal form and each rule involved a bounded number of modal operations.As proven in [10] we can check in quadratic time if the problem has a solution.

9
Related and Future Work Cheney [4] proved that higher-order pattern unification reduces to nominal unification.Levy and Villaret [9] proved the opposite side.These two results prove that higher-order pattern and nominal unification are equivalent.It would be interesting to adapt modalities to higher-order patterns.
Permissive nominal syntax [6] is a modification to the original syntax to ease the writing of proofs.Given a term t, we know that there are infinitely many fresh atoms for t, but we need freshness constraints to set them.Permissive nominal syntax encodes directly in a term (on variables) which atoms may occur free or not inside.It would be interesting to investigate if our approach can be adapted to take into account this modification.

Conclusion
The unique isomorphism between modalities is a powerful tool to establish properties on a representation.Most of the propositions of section 5 where established by proving them on environments and then transposing to any modality by isomorhpism.Furthermore, the algorithm completely isolates nominal constraints from first-order ones.Even when dealing with freshness constraints thanks to Proposition 26.

Proposition 18 . 4 . 4
For any modalities M and M ∀m, m ∈ M, Φ M →M (m • m ) = Φ M →M (m) • Φ M →M (m ) Replacings operations Definition 25 (For).Let M be a modality and m ∈ M , we can define the set of forbidden atoms of m by For(m) = {a ∈ A | ¬(a ≈ m a)}.Definition 26 (Rew).Let M be a modality and m ∈ M , we can define the renaming function of m by Rew(m) = {(a ← b) ∈ A × A | a = b ∧ a ≈ m b}.
Proposition 36.µ(G P ) ≤ 3|G P | Proposition 37. The strategy reduces G P to a normal form in at most µ(G P ) steps of rewriting.Definition 55.A state (P, O, repr, S) is said to be in solved form either if P = ∅ or if fail has been raised.Proposition 38.Normal forms are solved form.

Definition 56 (
Generalized Replacings).The set GL of generalized replacings is the set of terms generated by the grammar:GL = e | (a ← b)•GL | t GL | GL • GL | supp(GL).
1 , . . ., s n ) ≈ f (t 1 , . . ., t n ) in terms of relations s i ≈ t i .But we can not express a.s ≈ b.t in terms of s ≈ t where a and b are two different atoms.The relation between s and t is not the α-equivalence but another relation, written s ≈ (a←b)•e t.
P}. edge set, written G P E , is G P ≺ N ∪ G P ≈ where G P ≺ N is the set of termgraph edges.Let s N t be such an edge.It means that t is a direct proper subterm of of s, i.e. that either s = a.t, s = f (. . ., t, . . . ) or s = (a b)•t.s is called a parent of t and t is called a child of s.In the algorithm, permutation actions π•s are not applied but considered as syntactic constructions.For example, let t be the term (a b)(c d)•X.Its child is (c d)•X whose child is X.G P ≈ is the set of equations s ≈ ?m t considered as edges from s to t labelled by m and called equivalence edge: