Variadic equational matching in associative and commutative theories

In this paper we study matching in equational theories that specify counterparts of associativity and commutativity for variadic function symbols. We design a procedure to solve a system of matching equations and prove its termination, soundness, completeness, and minimality. The minimal complete set of matchers for such a sys-tem can be inﬁnite, but our algorithm computes its ﬁnite representation in the form of solved set. From the practical side, we identify two ﬁnitary cases and impose restrictions on the procedure to get an incomplete algorithm, which, based on our experiments, describes the input-output behavior and properties of Mathematica’s ﬂat and orderless pattern matching. © 2021 The Authors. Published by Elsevier Ltd. This is an open access article under the CC BY license (http://creativecommons.org/licenses/by/4.0/).


Introduction
In variadic languages, function symbols do not have a fixed arity.They can take an arbitrary number of arguments.In the literature, such symbols are known by different names: flexary, of flexible arity, polyadic, multi-ary, unranked.They are a convenient and useful tool for formalizing mathematical texts, representing symbolic computation data structures, modeling XML documents, expressing patterns in declarative programming, etc. Usually, variadic languages contain variables not only for individual terms, but also for finite sequences of terms, which help to take the full advantage of the flexibility of such languages.
On the other hand, the increased expressiveness of variadic languages has its price, from the computational perspective.Solving equations with sequence variables is a nontrivial task (Kutsia, 2002b(Kutsia, , 2004(Kutsia, , 2007) ) and pattern matching, a very common operation in the above-mentioned applications, becomes pretty involved.
In this paper we address the problem of pattern matching in variadic languages, where some function symbols satisfy (the variadic counterparts of) the commutativity (C) and associativity (A) properties.Equational matching in these theories has been intensively studied in languages with ranked alphabets, see, e.g., (Benanav et al., 1987;Eker, 2002Eker, , 2003;;Hullot, 1979).Variadic equational matching so far attracted less attention.
We try to address this shortcoming, approaching the problem both from the theoretical and application points of view.From the theoretical side, we propose a modular rule-based system for solving matching equations with A, C, and AC function symbols.The major problem is that variadic A-and AC-matching problems might have infinitely many incomparable solutions.(They are examples of so called infinitary matching theories.)Hence, any procedure that aims at enumerating the minimal complete set of matchers in these theories is nonterminating.Therefore, we propose a finite representation of this infinite set.From this representation, there is an "easy way" to obtain any solution of the problem.It applies to both A-and AC-matching.Our rules give algorithms for such a finite representation in the corresponding theories.We prove termination, soundness and completeness of the algorithms.
Our focus was not on coming up with an optimized, efficient procedure.Rather, we chose a declarative, modular approach, which makes proving properties easier.From the application perspective, we show how some intuitive modifications of the rules can lead to finitary cases.Two such special cases: bounded fragment and strict variant, are studied in the paper.
The final part of the paper is devoted to the analysis of the behavior of the equational variadic matching algorithm implemented in the symbolic computation system Mathematica (Wolfram, 2003).Its programming language, called Wolfram, has a powerful matching engine.It uses variadic symbols, individual and sequence variables, and can work modulo A and C theories, called there flat and orderless theories, respectively.The matching mechanism is explained in tutorials and help files, but to the best of our knowledge, its formal description has never been published.We try to fill this gap, proposing rules which are a further restriction of the above-mentioned strict variant and, in our opinion, describe the input-output behavior and properties of Mathematica's flat and orderless pattern matching.In particular, our analysis suggest that Mathematica's matching corresponds to an incomplete strict variant of a fragment normalized with respect to the given equational theory.The algorithm is not complete.We suppose that incompleteness is a deliberate decision motivated by efficiency reasons.
This paper is an extended and improved version of (Dundua et al., 2019).

Related work
In (Kutsia, 2008), a procedure for flat (A) matching was described and its relation to the correspondent algorithm in Mathematica was discussed.The current work builds on it and extends the results from that paper.Namely, for the variadic A-theory, Kutsia (2008) presented a nonterminating complete procedure and terminating incomplete algorithms, while we propose here a special representation of substitutions, which allows us to design a terminating complete algorithm.The bounded fragment in the current paper generalizes the bounded fragment in (Kutsia, 2008).The strict variant that we consider here has not been studied in (Kutsia, 2008), but the F NE algorithm there uses a similar idea.Moreover, in the current paper we consider also variadic C-and AC-theories.
They were not treated in (Kutsia, 2008).Finally, the comparison with Mathematica's behavior is more comprehensive in the current paper than in (Kutsia, 2008).It covers more theories, since in addition to flat, we also discuss matching with the orderless and flat-orderless attributes, as the Mathematica counterparts of variadic commutative and variadic associative-commutative matching.Also, this comparison reflects some changes that, as it turned out, meanwhile happened in the system.
Pattern matching in Mathematica is informally described in (Wolfram, 2003;Trott, 2004) and in other sources on Mathematica programming.We are not aware of any formal treatment of this mechanism and its semantics.
Recently, a library to extend Python with variadic matching and sequence variables has been developed (Krebber et al., 2017).Pattern matching compiler Tom (Cirstea et al., 2010) supports associative matching.Matching in the combination of order-sorted associative, commutative, and unit element theories is implemented in Maude (Clavel et al., 2007).In Kutsia (2007), it was shown how variadic syntactic matching with sequence variables can be encoded as a special form of order-sorted associative-unit matching.Usefulness of variadic operators and sequence variables in logical frameworks has been discussed in (Horozal et al., 2014;Horozal, 2014).

Preliminaries
We assume some familiarity with the standard notions of unification theory (Baader and Snyder, 2001).We consider four pairwise disjoint sets: function symbols F , individual variables V Ind , sequence variables V Seq , and function variables V Fun .All the symbols in F are variadic, i.e., their arity is not fixed.We will use x, y, z for individual variables, x, y, z for sequence variables, X, Y , Z for function variables, and a, b, c, f , g, h for function symbols.The set of variables V Ind ∪ V Seq ∪ V Fun is denoted by V. Terms t and sequence elements s are defined by the grammar: When it is not ambiguous, we write f for the term f () where f ∈ F .In particular, we will always write a, b, c for a(), b(), c().Terms are denoted by t, r and sequence elements by s, q.Finite, possibly empty sequences of terms are denoted by t, r, while s, q are used to denote sequences of sequence elements.
The set of variables of a term t is denoted by V(t).We will use the subscripts Ind, Seq, and Fun to indicate the sets of individual, sequence, and function variables of a term, respectively.A ground term is a term without variables.The size of a term t, denoted size(t), is the number of symbols in it.These definitions are generalized for any syntactic object throughout the paper.The head of a term is its root symbol.The head of a variable is the variable itself.
A substitution is a mapping from individual variables to individual terms, from sequence variables to finite sequences of sequence elements, and from function variables to function symbols or function variables, such that all but finitely many variables are mapped to themselves.(We do not distinguish between a singleton term sequence and its sole element.)We will use lower case Greek letters for substitutions, with ε reserved for the identity substitution.
For a substitution σ , the domain is the set of variables dom(σ For readability, we put term sequences in parentheses.For instance, the set {x → f (a, y), x → (), y → (a, X( f (b)), x), X → g} is such a representation of the substitution, which maps x to the term f (a, y), x to the empty sequence, y to the sequence of three elements (a, X( f (b)), x), and X to g.
Instances of a sequence element s and a sequence s under a substitution σ , denoted, respectively, by sσ and sσ , are defined as follows: ).One can also see that nested sequences are not allowed: they are immediately flattened.
Composition of two substitutions σ and ϑ , written σ ϑ , is a substitution defined by An equation is a pair of individual terms.Given a set E of equations over F and V, we denote by .= E the least congruence relation on the set of finite sequences of sequence elements (over F and V) that is closed under substitution application and contains E. The set .= E is called an equational theory defined by E. Slightly abusing the terminology, we will also call the set E an equational theory or an E-theory.When s .= E q, we say that s and q are equal modulo E. The signature of E, denoted sig(E), is the set of all function symbols occurring in E. A function symbol is called free with respect to E if it does not occur in sig(E).
A substitution σ is more general than a substitution ϑ on a set of variables V modulo an equational theory E, denoted σ ≤ • V E ϑ , if there exists a substitution ϕ such that χσ ϕ .
= E χ ϑ for all individual and sequence variables χ ∈ V , and X()σ ϕ .= E X()ϑ for all function variables X ∈ V .
Two substitutions σ and ϑ are called equigeneral on a set of variables V modulo an equational theory Solving equations in an equational theory E is called E-unification.If one of the sides of an equation is ground, then it is called a matching equation, and solving such equations in a theory E is called E-matching.We write E-matching equations as s E t, where t is ground.If E = ∅ (i.e., if all involved function symbols are free), we talk about syntactic matching.An E-matching problem over F is a finite set of E-matching equations over F and V, which is usually denoted by : = {s 1 E t 1 , . . ., s n E t n }.
An E-matcher of is a substitution σ such that A complete set of E-matchers of is a set S of substitutions with the following properties: The set S is a minimal complete set of E-matchers of with respect to V( ) if it is a complete set of E-matchers satisfying the minimality property, which states that two distinct elements of S are incomparable with respect to ≤ • V( ) Now we briefly discuss the matching type of equational theories.Its definition is based on the existence and cardinality of the minimal complete set of E-matchers.A matching problem is of type unitary (resp.finitary, infinitary) if its minimal complete set of matchers exists and is a singleton (resp., a finite non-singleton set, an infinite set).It is of type zero if the minimal complete set of matchers does not exist.These types are denoted respectively by 1, ω, ∞, 0, and are ordered as 1 < ω < ∞ < 0.
An equational theory E has matching of type unitary (resp.finitary, infinitary, zero) if the <-maximal type of E-matching problems in this theory is 1 (resp.ω, ∞, 0).See (Baader and Snyder, 2001) for more information about unification/matching types.
Below, in termination proofs, we will need a well-founded ordering on multisets of natural numbers.For this, the Dershowitz-Manna ordering on multisets (Dershowitz and Manna, 1979) will be used, which we denote by > DM .It is defined as follows: Definition 1 (Multiset Ordering > DM ).Let S be a partially ordered set by an ordering >, and M(S) be the set of all finite multisets with elements taken from S. Then for M, M ∈ M(S), we say that M > DM M if for some multisets X, Y ∈ M(S) where ∅ = X ⊆ M, we have M = (M \ X) ∪ Y and for all y ∈ Y there exists x ∈ X such that x > y.Dershowitz and Manna (1979) explain this definition also in words: "a multiset is reduced by the removal of at least one element (those in X ) and their replacement with any finite number -possibly zero -of elements (those in Y ), each of which is smaller than one of the elements that have been removed." In our proofs, we will have N in the role of S with the standard ordering on natural numbers.

Variadic equational matching problems
In this paper we consider equational theories that specify pretty common properties of variadic function symbols: counterparts of associativity and commutativity.They are defined by the axioms below (for a function symbol f ): The A( f ) axiom asserts that nested occurrences of f can be flattened out.The C( f ) axiom says that the order of arguments of f does not matter.Below we often omit the word "variadic" and write associativity and commutativity instead of variadic associativity and variadic commutativity.We also say f is A, C, or AC if, respectively, only A( f ), only C( f ), or both A( f ) and C( f ) hold for f .
Example 2. We can illustrate the notions of equality modulo A( f ) and/or C( f ) for some f with the following examples: An associative normal form (A-normal form) of a term or a sequence is obtained by rewriting it with the associativity axiom from left to right as long as possible.In Example 2, for instance, f (a We introduce a strict total order on function symbols and extend it to ground terms and term sequences so that the obtained ordering is also total.A commutative normal form (C-normal form) of a ground term is obtained by rearranging arguments of commutative function symbols to obtain the minimal term with respect to the defined ordering.An associative-commutative normal form (ACnormal form) of a ground term t is the C-normal form of the A-normal form of t.
A C-normal form (resp. an AC-normal form) of a sequence of ground terms (t 1 , . . ., t n ) is a sequence of ground terms (t 1 , . . ., t n ) where t i is a C-normal form (resp. AC-normal form) of t i for all 1 ≤ i ≤ n.
Similarly, a C-normal form (resp. an AC-normal form) of a multiset of ground terms { {t 1 , . . ., t n } } is a multiset of ground terms { {t 1 , . . ., t n } } where t i is a C-normal form (resp. AC-normal form) of t i for all 1 ≤ i ≤ n.
A ground term, sequence of ground terms, or a multiset of ground terms is in C-(resp.AC-) normal form if it is its own C-(resp.AC-) normal form.All these normal forms are unique.
The notion of normal form extends to substitutions straightforwardly: σ is in A-(C-, AC-) normal form if χσ is in A-(C-, AC-) normal form for all individual and sequence variables χ .
In A-and AC-theories there exist matching problems whose minimal complete set of matchers is infinite.This is related to the flatness property of variadic associative symbols, and originates from flat matching (Kutsia, 2002a(Kutsia, , 2008)).The simplest such problem is f It, naturally, implies that any matching procedure that directly enumerates a complete set of A-or AC-matchers is non-terminating.
In general, our matching problems are formulated in a theory that may contain several A, C, or AC-symbols.
Below we design variadic A-,C-, and AC-matching algorithms first for a special case when no variable occurs more than once in matching problems.Such matching problems are called linear.The general, nonlinear case will be considered after that.To solve a nonlinear problem, we first linearize it, solve the obtained linear problem by the corresponding algorithm, and then combine the computed solutions into matchers of the original problem.In the next two sections we discuss these algorithms in detail.

Matching algorithm for linear problems
We formulate our matching algorithm in a rule-based manner for linear problems.The rules operate on a matching equation and return a set of matching equations.They also produce a set of equations, which we call a solved set, denoted in the rules by S.
Before continuing with the rules, we need to define notions related to solved sets.
Definition 2 (Solved Set of Equations).Solved sets are sets of equations, whose elements are called solved equations and have one of six possible forms: where the symbol { {•} } denotes multisets.In equations f is associative or associative-commutative and no term in t has f as its head.Besides, each variable that appears in a solved set, appears there only once, in the left hand side of an equation.The right hand sides are ground and in (A-, C-, AC-) normal form.

Definition 3 (Set of Substitutions Generated by a Solved Equation)
. Each solved equation eq generates a set of substitutions, denoted by (eq), as follows: where n ≥ 0.
To explain in words, each element of (x ) is a substitution that maps x either to (t 1 , . . ., t n ) or to a sequence obtained from (t 1 , . . ., t n ) by applying f to some of its subsequence(s).That can be, in particular, the empty subsequence, i.e., f ()'s may get inserted.Similarly, each element of (x ) is a substitution that maps x to a permutation of (t 1 , . . ., t n ) or to a sequence obtained from a permutation of (t 1 , . . ., t n ) by applying f to some of its subsequence(s) (which can be also empty).The AC-normal form requirement in the definition guarantees that if f is applied to Example 3.Each of the last two cases in Definition 3 actually defines an infinite set of pairwise ≤ • {x} Here * is the Kleene star: Assume that the function symbol names are ordered alphabetically (for C-ordering).
Note that f (b, a) does not appear in the range of the substitutions due to the AC-normal form requirement in the definition of ( respect to the given ordering on function symbols).

S∈S (S).
The matching algorithm defined below will be based on applications of transformation rules.It is important to note that terms in the left hand sides of the rules are kept in normal forms with respect to associativity, and those in the right hand side (ground terms) are kept in normal forms with respect to associativity and commutativity.The transformation rules are divided into three groups: the common rules, rules for associative symbols, and the rules that deal with commutativity.
The rules have the form s E t S , where s E t is an E-matching equation, S is a solved set (which is either empty or consists of a single solved equation), and is a finite set of E-matching equations.Intuitively, such a rule transforms s E t into , and also records information in S how a particular variable is supposed to be instantiated in this transformation.

Common rules
The common rules apply in any theory.
FVE: Function variable elimination Rules for free symbols These rules apply when f is free.
Dec-F: Decomposition under free head where f is free and s / ∈ V Seq .

SVE-F:
Sequence variable elimination under free head where f is free and S = {x ≈ t1 }.
Rules for commutative symbols These rules apply when f is commutative but not associative.
Dec-C: Decomposition under commutative head where f is commutative and non-associative and s / ∈ V Seq .
Rules for associative symbols These rules apply when f is associative but not commutative.
Dec-A: Decomposition under associative head where f is associative and non-commutative and s / ∈ V Seq .

SVE-A:
Sequence variable elimination under associative head where f is associative and non-commutative and S = {x ≈ ( t1 )[ f ]}.
FVE-A: Function variable elimination under associative head where f is associative and non-commutative and S = {X ≈ f }.
IVE-A: Individual variable elimination under associative head where f is associative and non-commutative and S = {x ≈ f ( t1 )}.
Rules for associative-commutative symbols These rules apply when f is both associative and commutative.
Dec-AC: Decomposition under AC head where f is associative-commutative and s / ∈ V Seq .

SVE-AC:
Sequence variable elimination under AC head where n ≥ 0, f is associative-commutative and where f is associative-commutative and S = {X ≈ f }.
The matching algorithm The form and the conditions of the rules guarantee that no two rules apply to the same equation.However, it is possible that a rule transforms the same equation in multiple ways.
All sequence variable elimination and individual variable elimination rules are like that, depending on the choice of the subsequence of the right hand side they match to.Also, Dec-C and Dec-AC rules may choose the t from the right hand side in different ways.Given a matching problem , we create the initial system ; ∅ and start applying the rules.If a derivation reaches a terminal system ∅; S, then it is called a successful derivation and ∅; S is called a success leaf.If a derivation cannot be extended further from a system of the form ; S where = ∅, then it is called failed.
The set S at the success leaf ∅; S of a derivation ; ∅ * ∅; S is called an answer of computed by LM, or just a computed answer of .The set of answers of computed by the algorithm LM is denoted by LM( ).
Example 4. Some linear matching problems and the corresponding computed answers: Recall that our matching problems are formulated in a theory that may contain one or more A, C, or AC-symbols.In the theorems proven below, the equational theory E refers to such a theory.The considered matching problems are linear.
Theorem 1 (Computed solved set).For a linear E-matching problem , every element of LM( ) is a solved set.
Proof.Let S ∈ LM( ).Since is linear, by inspection of the rules it is clear that no variable will occur more than once in S.Moreover, the right hand sides of equations in S are E-normalized.Also, in the equations of the form x ≈ { { t} }[ f ] and x ≈ t[ f ], f is not the head of any of the terms in t.Hence, S is a solved set.
Theorem 2 (Termination of LM).The algorithm LM terminates for every input.
Proof.Let the complexity measure of a linear E-matching problem be the pair (N, M), where N is the number of variables in , and M is the multiset of the sizes of the right hand sides of equations in .Measures are compared lexicographically.The obtained ordering is well-founded, as a lexicographic combination of two well-founded orderings: > on natural numbers and > DM on multisets of natural numbers.The rules strictly reduce this measure.It is easy to see since each variable elimination rule reduces N and the other rules reduce M without changing N.
For the soundness theorem, we will need the following lemma: Proof.We prove the lemma only for the rule SVE-AC.For the other rules the proof is simpler.If the step is performed by SVE-AC, then for some and an AC symbol f we have (1) Then dom(s) = {x}.Since we work with linear problems, x / ∈ V( f (s)).Therefore from (1) we get (2) Since remains unchanged during the rule application, from (2) we get that ϕ ∈ match E ( 1 σ ).This proves match E ( 2 ) ⊆ match E ( 1 σ ).The other direction can be proved analogously.
Corollary 1.For each n, if ; ∅ n ∅; S is a sequence of rule applications in LM, then (S) ⊆ match E ( ), where is linear.
Proof.By induction on n, using Lemma 1 to make the step.
Proof.We prove the theorem by constructing the derivation that starts from ; ∅ and ends with ∅; S such that σ | V( ) ∈ (S).
Assume that we have already constructed We show that we can make the next step n ; S n n+1 ; S n+1 such that σ ∈ match E ( n+1 ), and (3) Let us select an equation s E t ∈ n , assume n = {s E t} , and show how to make the inference step.If head(s) = head(t), then head(s) should be a variable.Otherwise n would not be solvable, which contradicts the assumption that σ is a matcher of n .
If s is a variable x, then we have σ (x) = t, we extend the derivation by the IVE rule, which In a similar manner, we can make the step when s has a form X(s). Now assume head(s) = head(t).If s and t are the same, then the step is made by the T rule and the conditions (3) and (4) are satisfied.
The reasoning is similar when head(s) is commutative.Now we consider the case when it is associative-commutative and s 0 is a sequence variable.The other remaining cases are similar or simpler.We have s = f (x, s 1 , . . ., s n ) and f is AC.Let r = σ (x).Let t1 be obtained from r by normalization and removing all occurrences of f () as elements of r.Then t ≈ AC f ( t1 , t2 ).We make the step with SVE-AC, choosing t 1 , . . ., t n from the arguments of t so that { {t 1 , . . ., and the condition (3) holds.As for S n+1 , we have and by the definition of , the condition (4) is also satisfied.
By iterating this process, we obtain a final state ∅; which finishes the proof.

Theorem 5 (Minimality). (LM( )) is minimal for any linear E-matching problem .
Proof.First, note that the rules in LM( ) do not introduce any fresh variable, and every variable from V( ) is eventually moved to the solved set (unless the algorithm fails and there is no solution).It implies that all substitutions in (LM( )) have the same domain, which is V( ).
Assume now by contradiction that there exists σ , ϑ ∈ (LM( ) , σ and ϑ are E-equigeneral (on V( )).Since we kept the right hand sides E-normalized, the transformation rules guarantee that the terms in each computed solved form in LM( ) are in E-normal form.From this fact and the definition of , the ranges of substitutions in (LM( )) are E-normalized.Therefore, since the normal forms for our equational theories are unique, equigenerality of σ (v) and ϑ(v) imply that σ (v) = ϑ(v) for any v ∈ V( ).Together with dom(σ ) = dom(ϑ), it implies that σ = ϑ , which is a contradiction.Hence, (LM( )) is minimal.
Example 5. Note that if we did not require normalization of the right hand sides in matching problems, LM( ) may contain elements that are not solved sets.This can be illustrated, e.g., by the solutions to the and its elements are not solved sets, since the right hand sides are not normalized.
Moreover, it would have the consequence that (LM( )) would not be minimal, since we would With the normalized right hand sides, LM( Example 6.For minimality of (LM( )), normalization of x → r in the definitions of (x • The type of variadic linear syntactic and C-matching problems is at most finitary.
• The type of variadic linear A-and AC-matching problems is at most infinitary.
Proof.By Theorem 5, every linear variadic E-matching problem admits a minimal complete set of E-matchers, which is (LM( )).It implies that not is of type zero.By construction, the set ) for some x, t, and f .Inspecting the rules of LM, one can see that such equations are in LM( ) iff is an A-or an AC-matching problem.
For syntactic and C-matching problems this is not the case.Hence, if is a syntactic or a C-matching problem, (LM( )) is finite.For some such 's this set may contain more than one element.For instance, (LM( It implies that variadic linear syntactic and C-matching problems have at most finitary type. On the other hand, when is a linear A-or AC-matching problem, if it is solvable, then (LM( )) can be either a singleton, a finite non-singleton, or an infinite set.As examples illustrating each of these possibilities we could take the following problems, where It implies that variadic linear A-or AC-matching problems have at most infinitary type.

Nonlinear matching
To solve nonlinear variadic equational matching problems, we first linearize them, replacing multiple occurrences of the same variable by fresh variables.Then solve the obtained problem by the matching algorithm LM described in the previous section.The last step is to check the obtained solutions for consistency.We describe this step in this section.
We assume that the input is normalized with respect to associativity (the left hand sides) and associativity and commutativity (the right hand sides), whenever such symbols appear there.
Let S lin be an answer computed by LM for the linearized version lin of a matching problem .For each variable v in we have variables v 1 , . . ., v n in lin , which correspond to different occurrences of v in .We call them copied variables.The algorithm RS (for "reconstruct solutions") described below produces (nondeterministically) from S lin a set S which solves .We initialize S by S lin .
2. Replace all copies of individual, function, and sequence variables in S with their original names.
For instance, if S contains v 1 x ≈ t 1 and v 2 x ≈ t 2 , replace them respectively by x ≈ t 1 and x ≈ t 2 .If t 1 = t 2 , this operation will collapse multiple occurrences of the same equation into one occurrence, because S is a set.3.If S contains two equations for the same individual or function variable (i.e., two equations of the form x ≈ t 1 and x ≈ t 2 with t 1 = t 2 , or X ≈ f 1 and X ≈ f 2 with f 1 = f 2 ), stop with failure.4. If S contains two equations for the same sequence variable of the form x ≈ r, x ≈ t, or of the form , where r = t, stop with failure. 5.If S contains two equations for the same sequence variable of the form where the multisets { {r} } and { { t} } are not equal, stop with failure.6.Otherwise, if S contains two equations for the same sequence variable with f = g, then create the pair R 1 R 2 ; R 0 , where R 0 = () in the cases (a)-(c) and R 0 = ∅ in the case (d), and apply the reduction rules in Group 1-4 below as long as possible.If the process ends with ; R, then update S with S := S \ {x ≈ R 1 , x ≈ R 2 } ∪ {x ≈ R}, and repeat Step 6.If the process terminates without reaching ; R, stop with failure.

Return S.
The reduction rules operate on pairs of the form R 1 R 2 ; I , where R 1 and R 2 originate from Step 6 of the RS algorithm above.(Remember that they are in normal form.)The goal is to compute a sequence (or a multiset) that "is common" between R 1 and R 2 , i.e., some kind of "intersection" between them.More precisely, we aim at computing an I such that (I) = (R 1 ) ∩ (R 2 ).We compute it in the second argument of the pair R 1 R 2 ; I , which serves like an accumulator for the result computed so far during execution.
Due to the nature of R 1 and R 2 , we have four groups of rules: when both R 1 and R 2 are sequences, when one of them is a sequence and the other one a multiset, and when both are multisets.In each group, there are four types of rules: success, common term/intersection, relax left, relax right.This gives 16 types of rules.Within each type, we have two, three, or four rules depending on whether the function symbols are attached to sequence/multisets or not.
The reduction rules are the following (in all rules f = g, and ∪, ∩, and \ are operations on multisets): Group 1, sequences in both sides This group contains 10 rules: (Succ-S) Success, sequences: (RL-S) Relax the left hand side, sequences: (RR-S) Relax the right hand side, sequences: Example 7. We illustrate four rules: RL-S.2,RR-S.2, CFT.3, and Succ-S.3: Note that both RL-S.2 and RR-S.2 can apply to some problem.Therefore, to get all solutions, we need to try both alternatives: Alternative 1, leading to the result (g(), f ()): Alternative 2, leading to the result ( f (), g()): (RL-MS) Relax the left hand side, multiset-sequence: (RR-MS) Relax the right hand side, multiset-sequence: Example 8. We illustrate some of the rules in Group 2: .
Note that to the initial problem, we could also apply the RL-MS.1 differently, which would lead to a different derivation: , g(a, b), c).
Group 3, left sequence, right multiset These rules are symmetric to those in the previous group, obtained by swapping the arguments of .We do not list them explicitly.Their names are similar to the names of the rules in Group 2, with the difference that MS (multiset, sequence) is changed into SM (sequence, multiset). ; These derivations give four solved forms as results of application of RS to S lin .It is easy to see that they give solutions of : After linearizing it (taking x 1 , x 2 as copies of x and y 1 , y 2 as copies of y) and using the algorithm LM, we get solved sets: Because of Step 4 of the algorithm, we get RS(S 1 Step 6 gives , which is the final solution.The solved equation for x has been computed by the following reduction: Theorem 7 (Termination of RS).The procedure RS terminates for every input.
Let S( ) be the set of all solved forms obtained for an E-matching problem by LM and RS: S( ) := {RS(S lin ) | S lin ∈ LM( lin ), lin is the linearization of }.

Theorem 9. (S( )) is a minimal complete set of E-matchers of an E-matching problem .
Proof.By Theorems 4 and 8, (S) is a complete set of matchers for C-, A-, and AC-theories.For minimality, we reason as follows: First, our equational theories are regular, meaning that for each axiom s .
= t defining them we have V(s) = V(t).Second, according to (Fages and Huet, 1986, Proposition 4.1), for a regular theory E, any complete set of different E-matchers of a term to a ground term is minimal.Redundancies in (S( )) can be caused only by substitutions that are equigeneral to each other (modulo C-, A-or AC, with respect to the variables of the input matching problem).
This is because all input matching variables appear in the domain of each matcher in (S( )) (due to regularity), and the range of each matcher in (S( )) is ground (since matching problems have ground right hand sides).However, since our substitutions are in normal forms and the normal forms are unique, their equigenerality actually means their equality.Therefore, in (S( )) no two distinct substitutions are ≤ • E -comparable.Hence, (S( )) is minimal.
In Theorem 6 we showed what are the maximal matching types for linear C-, A-, and AC-problems.Now we get a more general result about matching types in these theories:

Theorem 10 (Matching types of variadic C-, A-, and AC-theories).
• The variadic C-theory has the finitary matching type.
• The variadic A-and AC-theories have the infinitary matching type.
Proof.Follows from Theorem 6 and Theorem 9.
Proof.First, we show that if an associative, commutative, or associative-commutative matching problem is solvable, then it has a solution whose size is polynomially bounded by the size of .For a linear , this follows from the existence of a solution that just "fills the missing gaps" in the non-ground side to fit it to the ground one, without inserting extra terms of the form f () in the instantiations of sequence variables.The exact construction of such a solution is given by algorithm LM.It shows that each occurrence of a non-associative symbol in the ground side of (denoted by For a nonlinear , to show the existence of a polynomially bounded matcher, we can follow the construction of algorithm RS.The algorithm shows how to build solutions of starting from the representation of solutions for its linearized version lin .For each function or individual variable in , by this construction we have that any solution of retains the representation of the instantiation of that variable in the solution of lin .For a sequence variable x ∈ V( ), the size of its instantiation in a solution of can be, in the worst case, the sum of the instantiation sizes for its copies in lin .The worst case is achieved for A-or AC-matching problems, e.g., of the form ))}, where {A(g), A(h)} ⊆ E and h() n stands for the n-element sequence (h(), . . ., h()).(Same for g() m .)Then for lin , by LM we get a solution {v 1 Hence, we showed that a solvable has a matcher whose size is bounded polynomially in the size of , by ) is the number of occurrences of v in .Now membership in NP follows from the fact that checking whether a substitution is a solution of any of stated matching problems can be done in polynomial time.The check involves the normalization of a term with respect to associativity and commutativity (according to the given ordering on function symbols) and then checking two terms for equality.
Hardness follows from the hardness of the syntactic variadic matching problem.The latter can be shown by a reduction from positive 1-in-3-SAT (Schaefer, 1978).In positive 1-in-3-SAT problems, clauses contain three positive literals.One looks for a truth assignment which makes exactly one literal true in each clause.In the reduction, we associate a term variable to each literal and encode a clause p 1 ∨ p 2 ∨ p 3 as a matching problem where the variable x i corresponds to p i , 1 ≤ i ≤ 3, the symbol t corresponds to true, and the symbol f to false.Each solution to such a matching problem corresponds to an 1-in-3 truth assignment.For instance, {y 1 → (), } corresponds to the truth assignment that maps p 1 to true and p 2 and p 3 to false.This encoding is similar to the one used in proving NP-hardness of regular expression order-sorting matching (Kutsia and Marin, 2015).
To encode a SAT problem, i.e., a set of clauses, we take matching equations s 1 E t 1 , . . ., s k E t k for the clauses and form a single matching equation as usual, using a free function symbol: The encoding is polynomial and preserves solvability in both directions: for each matcher, there exists the corresponding truth assignment that solves the given positive 1-in-3 SAT problem and vice versa.It implies that syntactic variadic matching problem is NP-hard.
We conclude this section with a remark on a potential application of our finite representation of matchers' sets in rule-based transformations modulo variadic equational theories.To make such transformations work, one could enrich rules with constraints, which represent solved set equations.At each step, matching will need to take into account the existing constraints.It goes beyond the scope of this paper and can be a subject of a potential future work.

Finitary fragment and variant
A fragment of equational matching is obtained by restricting the form of the input, while variants require computing solutions of some special form without restricting the input.The algorithms LM and RS provide a finite representation of potentially infinite complete sets of E-matchers.An interesting question is to identify special cases when the set itself is finite.In this section we discuss two such special cases: the bounded fragment and the strict variant.

Bounded fragment
We start with a fragment that restricts occurrences of sequence variables.
Definition 5. Let be a matching problem.A sequence variable x is called bounded in if • it occurs as an argument of at least two different function symbols or • it is not an argument of any associative and associative-commutative symbol in .
The problem is called bounded if all its sequence variables are bounded in it.
The following matching problems are bounded: An important property of bounded matching problems is the existence of a bound on the size of their solutions.More precisely, the following lemma holds: Lemma 2. Let be a bounded matching problem and σ be its solution.Assume that the terms in the range of σ are A-normalized.Then for every variable v ∈ V( ), we have size(vσ ) ≤ s E t∈ size(t).
Proof.Since is bounded, there will be an occurrence of v in some s E t ∈ such that vσ is not flattened in sσ .If the inequality does not hold, we will have size(sσ ) ≥ size(vσ ) > size(t), contradicting the assumption that σ solves .
For bounded problems, RS returns a solution in which solved equations for sequence variables . This is easy to see: if a sequence variable occurs only under free or commutative symbol, then LM never applies the rules SVE-A and SVE-AC, which is the only source of generating solved equations of the form sequence variable, say x, appears under two distinct function symbols, say f and g, it can be that those symbols are, e.g., associative and after LM we have two equations of the form y ≈ s[ f ] and z ≈ t[g], where y and z are copies of x.However, application of RS to s[ f ] t[g] produces r without a function symbol attached to it, which gives a solved equation x ≈ r. (The same reasoning applies to the associative-commutative case.) Hence, for bounded problems, no solution computed by LM and RS contains an equation of the It implies that if S is a solution of a bounded problem , the set (S) is finite.Moreover, the number of distinct solutions is always finite for any matching problem.Therefore, the set of all solutions S of is finite, and (S) is finite.

Strict variant
Infinitely many solutions to our matching problems are caused by sequences of f ()'s in the matchers, where f is an A or AC function symbol.But one might be interested in solutions, which do not introduce such extra f ()'s.
For a precise characterization, we modify the variadic associativity axiom into variadic strict associativity: For an f , this axiom is denoted by A s ( f ) and we use A s for the corresponding equational theory.
Obviously, any solution of a matching problem modulo A s or A s C is also a solution modulo A or AC.Hence, we can say that we are aiming at solving a variant of A or AC-matching.We call it the strict variant and adapt our algorithms to compute matchers for it.Actually, the adaptation is pretty small.It concerns the definition of and an extra condition for the A and AC rules in LM and for relaxing rules in RS.They are discussed below.The notions of A s -normal form and A s C-normal form are defined analogously to their A-and AC-counterparts.
Adapting the definition of to A s In solved sets, the equations of the form x ≈ (t 1 , . . ., t n )[ f ] and x ≈ { {t 1 , . . ., t n } }[ f ] now will have f as a strict associative symbol (instead of associative).Consequently, in the definition of , ≈ A is changed into ≈ A s and A-and AC-normal forms into A s -and A s C-normal forms, respectively.This modified definition defines a finite set of pairwise ≤ • {x} E -incomparable substitutions.For instance, (x Adapting the associative and associative-commutative rules to A s We replace the associative and associative-commutative rules by their strict counterparts, requiring strict associativity in the conditions.In addition, in the FVE-A-strict and FVE-AC-strict rules we require s = (), in the IVE-A-strict rule t1 = (), and in the IVE-AC-strict rule n > 0. For SVE-A-strict and SVE-AC-strict, we do not need any extra condition, because the modified definition of does the job.We call the modified algorithm LM S .
Adapting the reduction rules to A s This change is motivated by the fact that in the relaxing rules, one cannot relax the sides by pretending that terms like f () or g() stand in the corresponding sequences or multisets.We need to impose an extra condition on those rules.Namely, we require t1 = () in each of the relaxing rules.We call the modified algorithm RS S .
It is assumed that the equations these algorithms work on are normalized: LM S works on equations with A s -normal left hand sides and A s C-normal right hand sides, and RS S on A s C-normal forms.
Theorem 12.The algorithm LM S is terminating, sound, complete, and minimal.
Proof.Termination is obvious, since LM is terminating (Theorem 2).Soundness means that the algorithm only computes strict solutions.That it computes solutions, follows from Theorem 3. Strictness of the computed solutions follows from the fact that no rule invents terms like f () for the matchers.
Completeness follows from Theorem 4 based on the observation that the new definition of and the new conditions in the rules do not discard strict solutions.Hence, LM S computes all linear strict solutions and, therefore, is complete.Minimality means the minimality of the set (LM S ( )) for any , which follows from the minimality of (LM( )) (Theorem 5).
Similarly, RS remains terminating, sound, and complete for theories involving A s : Theorem 13.The algorithm RS S is terminating, sound, and complete.
Proof.RS S terminates, since RS terminates (Theorem 7).Soundness and completeness can be shown similarly to soundness and completeness of RS.We need to take into account the definition of adapted to A s and the new conditions imposed over the relaxing reduction rules, which do not allow to introduce extra subsequences of the form ( f (), . . ., f ()) in solutions, preventing non-strict results.
Example 13.If E = {A( f )}, the matching problem f (x) E f (a, a) has infinitely many solutions.If Note that the size of matchers is bounded by the size of the right-hand side of matching equations both for the bounded fragment and for the strict variant.

Data and observations
The programming language of Mathematica, called Wolfram, supports equational variadic matching in A, C, AC theories with individual and sequence variables.The terminology is a bit different, though.Variadic associative symbols there are called flat and commutative ones orderless.Individual variables correspond to patterns like x_, and sequence variables to patterns like y_ _ _.
The matching variants used in Mathematica are efficiently implemented, but the algorithm is not public.In this section we first show Mathematica's behavior on some selected characteristic examples and then will try to imitate it by variants of our rules.In the experiments we used the Mathematica built-in function ReplaceList[expr,rules], which attempts to transform the entire expression expr by applying a rule or list of rules in all possible ways, and returns a list of the results obtained.In transformation, Mathematica tries to match rules to expr, exhibiting the behavior of the built-in matching mechanism.The equational theories can be specified by setting attributes (flat, orderless) to symbols.
The examples below are used to illustrate the behavior of Mathematica, but we prefer to write those examples in the notation of this paper.We compare it to our strict variant, because it also does not compute extra f ()'s in the answer.However, they are not the same, as the examples below show.
We report only sets of matchers, ignoring their order and how many times the same (syntactically or modulo an equational theory) matcher was computed.The strict variant treats associativity of input symbols as strict associativity.Mathematica treats them as symbols with the attribute Flat. Problem: Strict matchers: {x → f (a)}.
In (6), strictness does not allow one to flatten the left hand side, but Mathematica does not have this restriction and transforms the term into f ().
Interestingly, the behavior of Mathematica's matching changed from Version 6.0 to Version 11.2.As reported in (Kutsia, 2008), for Problem (9), Mathematica 6.0 would return three out of four substitutions reported above.It would not compute {x → a, y → (b, c)}.
For problems like (7), Mathematica does not compute a matcher in which one individual variable is mapped to a subterm from the right hand side, and the other one is instantiated by f applied to a single subterm from the right hand side.(The same is true when f is A.) Examining more examples, e.g.f (x, y, z) E f (a, b, c, d), for f being AC, confirms this observation.One can see there matchers and many more, the solution set consists of 72 matchers), but not {x → a, y → f (b), z → f (c, d)} or similar.
However, this concerns only those individual variables which are arguments of the same occurrence of an associative symbol, as in f (x, y) E f (a, b).For problems like g( f (x), f (y)) E g( f (a), f (b)), where g is free and f is associative or associative-commutative, Mathematica computes mixed solutions: It is interesting to see how sequence variables behave in such a situation.Example (8) shows that in Mathematica matchers, f is not applied to terms from the right hand side.Besides, when a sequence variable is instantiated by a sequence, the order of elements in that sequence coincide with their relative order in the canonical form of the right hand side of the equation.For instance, in (8) Mathematica does not return {x → (b, a), y → ()}.Note that if f were only C in (8), Mathematica would still compute exactly the same set of matchers.
Imitating the behavior of function variables Equations of the form X(s) E f ( t) are transformed by a new rule, which we denote by FVE-M (M for modified): X(s) E f ( t) ϑ {g(s) E g( t)}, where ϑ = {X → f } and g is free.This rule replaces FVE.Besides, FVE-A-strict and FVE-AC-strict are dropped.

Imitating the behavior of individual and sequence variables under commutative symbols
The rule SVE-C is dropped.Instead, SVE-F is allowed to be used.(As we will see below, SVE-F can be used actually without any restriction to the head of the involved terms and, therefore, we can rename it to SVE.) On the other hand, Dec-C stays.Hence, commutative terms with individual variables can still be decomposed by the Dec-C rule.
Imitating the behavior of sequence variables under A s and A s C symbols For equations f (s) E f ( t), where A s ( f ) ∈ E and a sequence variable x appears in its solution σ , no element of the sequence xσ should have f as its head.For this, to make things simple, we just drop SVE-A-strict and SVE-AC-strict.Instead, SVE-F may apply also to strict associative and strict associative-commutative heads.We said above that this rule applies also to terms with a commutative head.We rename this modified rule into SVE, to reflect that it is not restricted to free symbols only and applies to any head.It implies that for the algorithm that reconstructs nonlinear solutions from the linear ones, we will not have the reduction rules anymore (i.e., Step 6 of RS S does not apply).To imitate this behavior, we introduce markings for equations of the form s E t, where head(s) = head(t) and A s (head(s)) ∈ E. Initially, they are not marked.First, assume that f is not commutative.

Imitating the behavior of individual variables under
If for an unmarked equation s E t, the first argument of s is not an individual variable, then no marking takes place.Now assume that an unmarked equation has a form f (x, s) E f (t, t), where f is A s .There are only two rules that apply to this equation: Dec-A-strict and IVE-A-strict.Then marking works in the following way: • If the unmarked equation is transformed by the Dec-A-strict rule into {x E t, f (s) E f ( t)}, then f (s) E f ( t) is marked by 0. • If the unmarked equation is transformed by the IVE-A-strict rule into {x E f (t), f (s) E f ( t)}, then f (s) E f ( t) is marked by 1. • Otherwise, if the unmarked equation is transformed by the IVE-A-strict rule into {x E f (t, t1 ), f (s) E f ( t2 )}, where t1 = (), then f (s) E f ( t2 ) remains unmarked.
After introducing markings, they are used in rule applications.Remember that for each marked equation s E t we have head(s) = head(t) and {A s (head(s))} = E. Therefore, the applicable rules are T, SVE, Dec-A-strict, or IVE-A-strict.
• If T applies, the equation is removed.
• If SVE applies (i.e., if the first argument of s is a sequence variable), then the obtained equation retains the marking of s E t.
• Otherwise, if the first argument of s is not an individual variable, the only applicable rule is Dec-A-strict.It produced two new equations.The first one, obtained from the first arguments of s and t, is unmarked.The second one retains the marking of s E t.
• Otherwise, the first argument of s is an individual variable.We have two cases depending on the marking of s E t: -s E t is marked by 1. Then it will not be transformed by the Dec-A-strict rule.Only IVE-A-strict applies and the obtained equation retains the mark 1. Linearization transforms it into g(X 1 (x 1 , y 1 ), X 2 (y 2 , x 2 )) E g( f (a, b), f (a, b)).
The first one gives the solution σ 1 = {X 1 → f , x 1 → a, y 1 → b}.The second one is solved by σ 2 = {X 2 → f , x 2 → b, y 2 → a}.Since x 1 σ 1 = x 2 σ 2 , the solutions are inconsistent and, hence, M Mma cannot solve the problem.This is also how Mathematica behaves.
At the end of this section, we note that Mathematica has matching with yet another equational theory, called OneIdentity.It can be characterized by the axiom f (x) ≈ x.It is an example of a collapse theory, where a term is equal to its proper subterm (Siekmann, 1989).It implies that the equivalence class of a variable does not consist of that variable only.Such theories need a special treatment and we have not considered it in our work.It can be a subject of future investigations.

Discussion and conclusion
We studied matching in variadic equational theories for associativity, commutativity, and their combination.Variadic A-matching and AC-matching in the presence of sequence variables are infinitary.Therefore, any procedure that tries to directly enumerate their complete set of matchers is nonterminating.However, looking at those matchers closer, one can see that there is a pretty regular way to obtain them from finitely many basic ones.This observation allowed us to develop a terminating matching algorithm for the mentioned variadic equational theories.The algorithm computes a finite representation of a possibly infinite complete set of matchers.It works in two steps: first, the given matching problem is linearized by replacing multiple occurrences of the same variable with fresh variables.Next, the obtained linear problem is solved by an algorithm called LM.It gives a finite representation of potentially infinite set of matchers in the form of solved equations of certain kind.Finally, from the solutions of the linear problem we reconstruct solutions of the original one by another algorithm, which is called RS. Termination, soundness, and completeness of LM and RS are proved.
After that, we looked into special matching problems for which the complete set of matchers is finite.It requires certain restrictions on occurrences of sequence variables in the problem or in the solutions.We identified two such cases: a bounded fragment and a strict variant.In the bounded fragment, each sequence variable either is an argument of two different function symbols, or never appears as an argument of any associative and associative-commutative symbol.This condition implies that solutions have a bound on their size and, hence, there are finitely many matchers.
The strictness property imposes a restriction on the way nested associative function symbols are flattened: The inner one should have at least one argument.The strict variant requires us to compute matchers modulo the strictness property.There are finitely many such matchers.Our algorithms need only a minor modification to compute them.We called the algorithms adapted to the strict variant LM S and RS S .
Further, we made an attempt to understand the behavior of the powerful variadic equational matching algorithm of the Mathematica system.Our analysis suggests that it corresponds to an incomplete strict variant of a fragment normalized with respect to the given equational theory, where incompleteness stems from the goal of making it efficient.To model its behavior, we restricted LM S and RS S and obtained an algorithm which we called M Mma .Its evaluation showed that for those examples we experimented with, M Mma computes the same set of answers as Mathematica's equational (flat, orderless) matching algorithm.

Declaration of competing interest
The authors declare that they have no known competing financial interests or personal relationships that could have appeared to influence the work reported in this paper.
and the range is the set of sequences range(σ ) = {σ (v) | v ∈ dom(σ )}.A substitution can be represented explicitly as a function by a finite set of bindings of variables in its domain: {v the algorithm LM gives the solved set { {a, b} }[ f ], and (LM( )) contains the substitutions {x → f (a, b)} and {x → f (b, a)}, among others.Theorem 6 (Types of variadic linear C-, A-, and AC-matching problems).
A-normalized left hand sides and AC-normalized right hand sides.It selects an equation from and transforms it by the applicable rule, if such a rule exists.Assume a rule R transforms s E t S .One step of LM is then performed as {s S ∪ S , where is the disjoint union symbol.A sequence of rule applications is called a derivation.If the chosen rule can be applied in multiple ways, one alternative is chosen nondeterministically.Such a nondeterminism introduces branching in the derivation tree.
The matching algorithm LM ("linear matching" to indicate that it works on linear matching problems) works on pairs ; S, where is a linear matching problem consisting of equations in t} ; S R ∪ ; A s and A s C symbols This concerns equations of the form f (s) E f ( t), where A s ( f ) ∈ E. As we observed, if individual variables x and y occur as arguments of f (s), and t 1 and t 2 are two terms among t, then for the same matcher σ it cannot happen that xσ = t 1 and yσ = f (t 2 ): Either xσ should also have the head f , or yσ should have more arguments.This is what Mathematica does.