Optimization of Rewrite Theories by Equational Partial Evaluation (cid:63)

. In this paper, we develop an automated optimization framework for rewrite theories that supports sorts, subsort overloading, equations and algebraic axioms with free/non-free constructors, and rewrite rules modeling concurrent system transitions whose state structure is deﬁned by means of the equations. The main idea of the framework is to make the system computations more efﬁcient by partially evaluating the equations to the speciﬁc calls that are required by the transition rules. This can be particularly useful for automatically optimizing rewrite theories that contain overly general equational theories which perform unnecessary and costly computations involving pattern matching and/or uniﬁca-tion modulo equations and axioms. The transformation is based on a suitable unfolding operator parameter that relies on the symbolic operational engine of Maude’s equational theories, called folding variant narrowing , together with a generic abstraction operator. Depending on the properties of the rewrite theory, the unfolding and abstraction operators must be ﬁne-tuned to achieve the biggest optimization possible while ensuring termination and total correctness of the transformation. We formalize two instances of our scheme for the case when the rewrite theory either has an inﬁnite number of most general variants or a ﬁnite number of most general variants. Finally, we discuss some experimental results which demonstrate that the proposed optimization technique pays off in practice.


Introduction
Rewriting Logic (RWL) is a logic of change that extends equational logic by adding rewrite rules that are used to describe non-deterministic transitions of concurrent systems. Rewriting Logic is efficiently implemented in the high-performance system Maude [23]. Roughly speaking, a rewrite theory R = (Σ, E B, R) seamlessly combines a term rewriting system (TRS) R, which specifies the system dynamics, with an equational theory E that defines the static structure of the system states as terms of an algebraic datatype. Given a signature Σ of program operators together with their type definition, the equational theory E = (Σ, E B) combines, in turn, a set E of equations (that are implicitly oriented from left to right and operationally used as simplification rules) on Σ and a set B of commonly occurring axioms such as associativity, commutativity, and identity that are essentially used for B-matching 3 (and are implicitly expressed in Maude as operator attributes).
Partial evaluation (PE) is a program optimization technique (also known as program specialization) that, given a program and some of its input data, produces a residual or specialized program. Running the residual program on the remaining data is generally faster and yields the same result as running the original program on all of its input data [34]. PE has been widely applied to a variety of programming paradigms, including functional programming [34] and logic programming [36], where it is usually called partial deduction (PD). In contrast to classical PE, partial deduction allows to not only instantiate input variables with constant values but also with terms that may contain variables, thus providing extra capabilities for specialization [36,37].
Narrowing is a symbolic mechanism that extends term rewriting by replacing pattern matching with unification [32,51]. The Equational Narrowing-driven Partial Evaluation (EQNPE) scheme of [6] extends PD to the specialization of order-sorted equational theories with respect to a set of input terms by making use of folding variant narrowing (also called FV-narrowing [31]). Thanks to the combined (logic and functional) capabilities of narrowing, the achieved transformation is strictly more powerful than the PE of both logic programs and functional programs [17]. In the EQNPE scheme, the key ingredients of PD get generalized to an order-sorted typed setting modulo axioms by formalizing: 1) a narrowing-based unfolding operator that ensures correction of the transformation; 2) order-sorted equational homeomorphic embedding for local termination (i.e., finiteness of unfolding); 3) order-sorted equational closedness (a recursive notion ensuring that all possible calls that may arise during the execution of the residual program are covered by the specialization) for completeness; and 4) term abstraction (based on order-sorted equational anti-unification) for global termination of the whole specialization process.
While the EqNPE scheme of [6] only applies to deterministic and terminating equational theories, partial evaluation has never been investigated in the context of non-deterministic and non-terminating rewrite theories. This paper addresses the specialization of such rewrite theories R = (Σ, E B, R), whose rewrite rules R are defined on top of an underlying equational theory E = (Σ, E B). Altogether, the rewrite theory R models a concurrent system that evolves by rewriting the system states by means of equational rewriting, i.e., rewriting with the rewrite rules of R modulo the equations and axioms of E [38]. To be executable in Maude, the equational theory E is required to be convergent (i.e., the equations of E are confluent, terminating, and sort-decreasing) and coherent modulo B. This ensures that every input expression t has one (and only one) canonical form t↓ E,B up to B-equality. On the other hand, the rules of R are required to be coherent w.r.t. E , which allows the rewrite steps with R to always be postponed in favor of deterministically rewriting with E .
In Maude, rewrite theories can also be symbolically executed by narrowing at two levels: (i) narrowing with the (typically non-confluent and non-terminating) rules of R modulo E = (Σ, E B); and (ii) narrowing with the (explicitly) oriented equations E modulo the axioms B. They both have practical applications: (i) narrowing with R modulo E = (Σ, E B) is useful for solving reachability goals [43] and logical model checking [29]; and (ii) narrowing with E modulo B is useful for E -unification and variant computation 4 [31]. Both levels of narrowing should meet some conditions: (i) narrowing with R modulo E is performed in a "topmost" way (i.e., the rules in R rewrite the global system state) and there must be a finitary equational unification algorithm for E ; and (ii) narrowing with E modulo B requires that B is a theory with a finitary unification algorithm and that E is convergent. When (Σ, E B) additionally has the property that a finite complete set of most general variants exists for each term, known as the finite variant property (FVP), E -unification is finitary and topmost narrowing with R modulo the equations and axioms can be effectively performed. For variant computation and (variantbased) E -unification, the folding variant narrowing 5 (or FV-narrowing) strategy of [31] is used in Maude, whose termination is guaranteed for theories that satisfy the FVP (also known as finite variant theories). Many relevant theories have the FVP, including theories of interest for Boolean satisfiability and theories that give algebraic axiomatizations of cryptographic functions used in communication protocols.
Partial evaluation techniques typically remove some computation states by performing as much program computation as possible, hence contracting the search space because some transitions are removed. However, narrowing-based analysis of rewrite theories generally requires the whole search space of a rewrite theory to be analyzed (i.e., all system states and transitions). Given the rewrite theory R = (Σ, E B, R), to avoid hindering said analysis, our method proceeds by specializing the underlying order-sorted equational theory E = (Σ, E B) to the precise use that the rules of R make of the functions that are defined in E . This is done by partially evaluating E with respect to the maximal (or outermost) function calls that can be retrieved from the rules of R, in such a way that E gets rid of any possible overgenerality. Actually, while the transformation highly contracts the system states and, more generally, the functional computations given by E are greatly compacted, no system state disappears. Moreover, in many cases we transform a rewrite theory whose operators obey structural, algebraic axioms such as associativity, commutativity, and unity into a much simpler rewrite theory whose operators obey no axioms. This makes it possible to run such theories into an independent rewriting infrastructure that does not support rewriting modulo axioms. Furthermore, some costly analyses that may require significant (or even unaffordable) resources, both in time and space, can now be effectively performed after the transformation.
A preliminary version of this work was presented in [5].
Our Contribution The main contributions of this paper are as follows.
1. We formalize a specialization scheme for rewrite theories that extends the equational, narrowingdriven partial evaluation approach of [6] to the specialization of rewrite theories.
(i.e., the overloading of operators that are related in the subsort ordering) for constructor symbols. To deal with it, in this article we naturally extend some key notions of the EqNPE framework, including the definitions of equational closedness and equational abstraction. This allows us to deal with non-free constructors as overloaded function symbols that may behave as a constructor operator for some typing, while behaving as a defined function symbol for a higher typing. In line with the extended definitions, we have correspondingly extended our technical results in [6] for dealing with sorts and subsorts in a finer way.
4. Similarly to [6], for the equational component of the rewrite theory, our specialization algorithm follows the classic control strategy of logic specializers [37], with two separate control levels: 1) local control (managed by a generic unfolding operator) that avoids infinite evaluations and is responsible for the construction of the residual equations for each specialized call; and 2) global control or control of polyvariance (managed by a generic abstraction operator) that avoids infinite iterations of the partial evaluation algorithm and decides which specialized functions appear in the transformed theory. To further optimize both rules and equations, we introduce a final, post-processing compression transformation that highly contracts the system states and the functional computations occurring in the specialized rewrite theory. 5. We provide two different implementations of the unfolding operator based on FV-narrowing that adapt the generic technique to the FVP behavior of the equational theory E by distinguishing two cases: (a) E fulfills the finite variant property: since FV-narrowing trees are always finite in finite variant theories for any input term, the unfolding strategy is formulated as a process of total evaluation where the defined functions that have the FVP which appear in righthand sides of rules are completely evaluated by computing a complete set of most general variants.
(b) E does not satisfy the finite variant property: in this case, a subsumption check is performed at each FV-narrowing step that compares (under order-sorted equational homeomorphic embedding [6]) the current term with all previous narrowing redexes in the same derivation so that all infinite FV-narrowing computations are safely stopped. 6. We have implemented an experimental prototype system called Presto, and we provide an empirical evaluation of the system on a set of benchmark problems that test the speedups achieved for both rewriting and narrowing computations.
Plan of the paper The paper is organized as follows. In Section 2, we recall some preliminary notions and we provide the specification of a client-server communication protocol which is used as running example throughout the paper. The generic specialization scheme for rewrite theories is described in Section 3. After introducing the folding variant narrowing strategy in Section 4, in Section 5 we instantiate the specialization scheme for the two classes of equational theories already mentioned: theories that fulfill the FVP and theories that do not fulfill the FVP. The proposed scheme instantiations come with some non-trivial examples that highlight the power of our specialization methodology. Section 6 provides an experimental evaluation in the Presto system, which implements the proposed specialization framework. Our benchmarks demonstrate the program optimization that is achieved for narrowing as well as for rewriting computations. In Section 7, we discuss some related work and we conclude. Proofs of the main results are given in Appendix A, while Appendix B provides the full specification of the clientserver communication protocol.

Preliminaries
Let Σ be a signature that includes typed operators (also called function symbols) of the form f : s 1 . . . s m → s, where s i , for i = 1, . . . n, and s are sorts in a poset (S, <) that models subsort relations (e.g., s < s means that sort s is a subsort of s ). Σ is assumed to be preregular, so each term t has a unique least sort under <, denoted ls(t). The connected components of (S, <) are the equivalence classes [s] corresponding to the least equivalence relation ≡ < containing <. For technical reasons, it is useful to assume that Σ has no ad-hoc overloading. 6 However, this assumption entails no real loss of generality: any Σ can be transformed into a semantically equivalent signature with no ad-hoc overloading (by symbol renaming). Note that avoiding adhoc overloading ensures that Σ is sensible, in the sense that for any two typings f : s 1 . . . s n → s and f : s 1 . . . s n → s of a n-ary function symbol f , if s i and s i are in the same connected component of (S, <) for 1 ≤ i ≤ n, then s and s are also in the same connected component; this provides the right notion of unambiguous signature at the order-sorted level. Binary operators in Σ may have attached an axiom declaration that specifies any combinations of algebraic laws such as associativity (assoc), commutativity (comm), and identity (id). By ax( f ), we denote the set of algebraic axioms for the operator f . By T Σ (X ), we denote the usual non-ground term algebra built over Σ and the set of (typed) variables X . By T Σ , we denote the ground term algebra over Σ. By notation x : s, we denote a variable x with sort s. Any expression t n denotes a finite sequence of terms t 1 . . .t n , n ≥ 0. A position w in a term t is represented by a sequence of natural numbers that addresses a subterm of t (Λ denotes the empty sequence, i.e., the root position). Given a term t, we let Pos(t) denote the set of positions of t. We denote the usual prefix preorder over positions by ≤. By t |w , we denote the subterm of t at position w. By root(t), we denote the operator of t at position Λ.
A substitution σ is a sorted mapping from a finite subset of X to T Σ (X ). Substitutions are written as σ = {X 1 → t 1 , . . . , X n → t n }. The identity substitution is denoted by id. Substitutions are homomorphically extended to T Σ (X ). The application of a substitution σ to a term t is denoted by tσ . The restriction of σ to a set of variables V ⊂ X is denoted σ |V . Composition of two substitutions is denoted by σ σ so that t(σ σ ) = (tσ )σ .
A Σ-equation (or simply equation, where Σ is clear from the context) is an unoriented pair λ = ρ, where λ , ρ ∈ T Σ,s (X ) for some sort s ∈ S, where T Σ,s (X ) is the set of terms of sort s built over Σ and X . An equational theory E is a pair (Σ, E B) that consists of a signature Σ, a set E of Σ-equations, and a set B of equational axioms (e.g., associativity, commutativity, and/or identity) declared for some binary operators in Σ. The equational theory E induces a congruence relation = E on T Σ (X ).
A term t is more general than (or at least as general as) t modulo E , denoted by t ≤ E t , if there is a substitution γ such that t = E tγ. We also define t E t iff t ≤ E t and t ≤ E t. By abuse of notation, we write ≤ B and B when B is an axiom set.
A substitution θ is more general than (or at least as general as) σ modulo E , denoted by θ ≤ E σ , if there is a substitution γ such that σ = E θ γ, i.e., for all x ∈ X , xσ = E xθ γ. Also, θ ≤ E σ [V ] iff there is a substitution γ such that, for all x ∈ V, xσ = E xθ γ.
An E -unifier for a Σ-equation t = t is a substitution σ such that tσ = E t σ . By CSU E (t = t ), we denote a complete set of E -unifiers for the equation t = t so that any E -unifier of t = t is less general modulo E than (at least) one element in the set.
A rewrite rule (or simply rule) is an expression of the form is an equational theory and R is a set of rewrite rules. A rewrite theory (Σ, E B, R) is called topmost if there is a sort State such that: (i) for each rule λ ⇒ ρ, ls(λ ) ≡ < State and ls(ρ) ≡ < State; and (ii) there is no symbol f : t 1 . . .t n → s ∈ Σ and i ∈ {1, . . . , n} such that s ≡ < State and t i ≡ < State. Topmost rewrite theories provide a natural computation model for concurrent systems as shown in the following example. Example 1. Let us consider a topmost rewrite theory R = (Σ, E B, R) that encodes a clientserver communication protocol. The code snippet in Figure 1 shows a fragment of the theory signature Σ that includes the most relevant sorts and operators of the considered protocol specification.
Specifically, the signature Σ includes several operators and sorts that model the protocol entities. Names of the sorts are self-explanatory: for example, servers are typed with sort Serv, clients with sort Cli, and messages with sort Message.
Messages are encoded as non-empty, associative sequences t 1 . . . t n , where, for the sake of simplicity, each t i is a term of sort Symbol in the alphabet {a,b,c}. We assume that the sort Symbol is a subsort of Message; hence, any symbol is also a (one-symbol) message. Clients are represented as 5-tuples of the form [C,S,Q,K,V] of sort Cli, where C is the client's name, S is the name of the server that C wants to communicate with, Q is a message encoding a client request, K is a natural number (specified in Peano's notation) that determines an encryption/decryption key for messages, and V is a constant value that models the client status. Initially, the status is set to the initial default value mt, and it changes to success whenever a server acknowledges message reception. Servers are simply modeled by means of pairs of the form [S,K] of sort Serv, where S is a server name, and K is an encryption/decryption key. All network packets are represented as pairs of the form Host <-CNT of sort Packet, where Host is a client or server recipient and CNT specifies the packet content. Specifically, CNT is a term {H,M}, with H being the sender's name and M being a message that represents either a client request or a server response. System states are formalized as multisets < t 1 &. . . & t m > of clients, servers, and network packets via the associative and commutative operator & whose unity element is the constant null. System states have sort State.
The protocol dynamics is specified by the term rewriting system R in R that consists of the following three rewrite rules, where clients and servers agree on a shared key K. More specifically, the rule req allows a client C to initiate a transmission request with a server S by sending a message Q that is encrypted by function enc(Q,K) using the client's key K. The rule reply lets the server S consume a client request packet S <-{C,M} by first decrypting the incoming message M with the server key and then sending a response packet back to C that   includes the decrypted request message. The rule rec successfully completes the data transmission between C and S whenever the server response packet C <-{S,Q} includes a message Q that is equal to the initial client request message. In this case, the status of the client is changed from mt to success. Note that the transmission succeeds when the client and server use the same key K. ---Function enc(m,k) (resp. dec(m,k)) takes a message m and a ---natural number k as input and returns the corresponding encrypted ---(resp. decrypted) message using the Caesar cipher with key k.  Encryption and decryption functionality is implemented by two functions (namely, enc(M,K) and dec(M,K)) that are specified by the equational theory E in R. The equational theory E implements a Caesar cipher with key K, which is a simple substitution ciphering where each symbol in the plaintext message is replaced by the symbol that appears K positions later in the alphabet (handled as the list a,b,c). The cipher is circular, i.e., it works modulo the cardinality of the alphabet. For instance, enc(a b,s(0)) delivers (b c), and dec(a b,s(0)) yields the message (c a). The equational theory E includes the equations 7 in Figure 2. In the specification, the equational attribute variant is used to identify the equations to be considered in the folding variant narrowing strategy, while any equations without the variant attribute are disregarded and are only considered for rewriting.
The complete Maude specification of the client-server communication protocol can be found in Appendix B.

Computing in Rewrite Theories
Given a rewrite theory (Σ, E B, R), with E = (Σ, E B), the rewriting relation modulo E (in symbols, → R/E ) can be defined by lifting the usual rewrite relation on terms to the E Bcongruence classes [t] E B on the term algebra T Σ (X ) that are induced by = E ; in other words, [t] E B is the class of all terms that are equal to t modulo E B. This means → R/E is defined as We say that the relation → R/E is confluent if whenever t → * R/E u and t → * R/E v, then u and v can be rewritten to some w up to E -equality. A rewrite theory (Σ, E , R) is convergent if the rules R are sortdecreasing and the relation → R/E is confluent and terminating.
In a convergent order-sorted rewrite theory, for each term t ∈ T Σ (X ), there is a unique (up to E -equivalence) R/E -irreducible term t that can be obtained by rewriting t to R/E -irreducible or normal form, which is denoted by t → ! R/E t , or t↓ R/E when t is not relevant. For each x ∈ Dom(σ ), σ ↓ R/E is defined as (σ ↓ R/E )(x) = σ (x)↓ R/E . A substitution σ is R/E -irreducible (normalized) iff xσ is so for each x ∈ Dom(σ ). For a set Q of terms, we denote by Q↓ R/E the set of normal forms of the terms in Q.
Since E -congruence classes can be infinite, → R/E -reducibility is undecidable in general because any rewrite step t → R/E t involves searching through the possibly infinite equivalence classes [t] E B and [t ] E B . Therefore, R/E -rewriting is usually implemented by R,E -rewriting. We define the relation → R,E on T Σ (X ) by t → p,R,E t (or simply t → R,E t ) iff there is a nonvariable position p ∈ Pos Σ (t), a rule λ → ρ in R, and a substitution σ such that t |p = E λ σ and t = t[ρσ ] p . To ensure completeness of R,E -rewriting w.r.t. R/E -rewriting, we require Ecoherence: for any Σ-terms u, u , v if u = E u and u → R,E v, then there exists a term v such that u → R,E v and v = E v . If E -coherence holds for a set of rewrite rules R, we say that R is E -coherent. Note that, assuming E -matching is decidable, → R,E is decidable and notions such as confluence, termination, irreducible term, and normalized substitution are defined for → R,E straightforwardly [40].

Equational Theories as Rewrite Theories
Algebraic structures often involve axioms like associativity and/or commutativity of function symbols, which cannot be handled by ordinary term rewriting but are instead handled implicitly by working with congruence classes of terms. This is why the equation set of an equational theory is often decomposed into a disjoint union E = E B, where B is a set of algebraic axioms (which are implicitly expressed in Maude as attributes of their corresponding operator using the assoc, comm, and id: keywords) that are used for B-matching, and E is a set of equations that are implicitly oriented from left to right as a set E of rewrite rules (and operationally used as simplification rules modulo B). By doing this, a (well-behaved) rewrite theory (Σ, B, E ) can be defined, with E = {t ⇒ t | t = t ∈ E }, which satisfies all of the conditions that we need. This is formalized by the notion of decomposition E = (Σ, B, E) of an equational theory E = (Σ, E B).
Definition 1 (Decomposition [30]). Let E = (Σ, E B) be an order-sorted equational theory. We call (Σ, B, E) a decomposition of E if (Σ, B, E) is an order-sorted rewrite theory satisfying the following properties: 1. B is regular, i.e., for each t = t in B, we have Var(t) = Var(t ), and linear, i.e., for each t = t in B, each variable occurs only once in t and in t . 2. B is sort-preserving, i.e., for each t = t in B and substitution σ , we have tσ ∈ T Σ,s (X ) iff t σ ∈ T Σ,s (X ), for each sort s in the signature. Furthermore, for each equation t = t in B, all variables in Var(t) and Var(t ) have a common top sort. 3. B has a finitary and complete unification algorithm, which implies that B-matching is decidable. 4. The rewrite rules in E are convergent (i.e., confluent, terminating, and sort-decreasing), and B-coherent.
Normal forms t ↓ E,B in a decomposition (Σ, B, E) are also called canonical forms. Note that the mild requirements listed in Definition 1 are satisfied by most of the equational theories that are used in practice. In particular, the axiom set of commonly occurring equational theories only includes combinations of associativity (A), commutativity (C) and identity (U) axioms, and all the three axioms are regular, linear and sort-preserving. Furthermore, there exist finitary and complete unification algorithms for modular axiom combinations such as ACU, AC, CU, C, and U. The remaining cases (namely, A and AU) are partially supported by Maude (hence by our framework) because unification modulo A and AU are generally infinitary, yet Maude generates a complete and finitary set of equational unifiers for many A and AU unification problems [26].
It is worth noting that the Maude system automatically provides B-coherence completion w.r.t. rewriting, for rules and equations, for any combination of associativity and/or commutativity and/or identity axioms. That is, the specified rules and equations are automatically completed with no need for user intervention. Note that, for narrowing derivations, B-coherence of rules and equations must be explicitly ensured by the user (see [41]). We often abuse notation and say that (Σ, B, E ) is a decomposition of an order-sorted equational theory (Σ, E B), where E is the explicitly extended B-coherent completion of E.
The Maude interpreter implements rewriting modulo E B by means of two much simpler relations than → R/E and → R,E , namely → R,B and → E,B , so that rules and (oriented) equations can be intermixed in the rewriting process by simply using an algorithm of matching modulo B.
Then, an (R, E B)-rewrite step → R, E B on a term t in the rewrite theory R = (Σ, E B, R) can be implemented, without loss of completeness, by applying the following rewrite strategy: rewrite steps and → R,B rewrite steps following the strategy mentioned above. Note that, following this strategy, after each rewrite step using → R,B , generally the resulting term t i , i = 1, . . . , n, is not in canonical normal form and is thus normalized before the subsequent rewrite step using → R,B is performed. Also, in the precise strategy adopted by Maude, the last term of a finite computation is finally normalized before the result is delivered.
Then, there exists the following one-step rewrite sequence The considered rewrite sequence specifies an initial communication request from client Cli-A to server Srv-A using the key K=s(s(0)) and an initial message a.
where the req rule is applied. More specifically, the rewrite sequence t 0 → + E,B t 0 ↓ E,B equationally simplifies the initial state t 0 into its canonical form t 0 ↓ E,B by completely evaluating the function call shift(s(0)) into the term s(s(0)). Then the req rule is applied to t 0 ↓ E,B and a new term is yielded that is further simplified into the canonical form t 1 ↓ E,B by normalizing the function call enc(a,s(s(0))) with the oriented equations in E.

Symbolic Computation in Rewrite Theories
Similarly to rewriting modulo an equational theory E , where syntactic pattern-matching is replaced with matching modulo E (or E -matching), in narrowing modulo an equational theory (i.e., narrowing with the rules in R modulo the equations and axioms in E), syntactic unification is replaced by equational unification (or E -unification). More precisely, we define the equational narrowing relation ; R,E on T Σ (X ) by t ; σ ,p,R,E t (or simply t ; σ ,R,E t or even t ; σ t ) iff there is a non-variable position p ∈ Pos Σ (t), a rule λ ⇒ ρ in R, and a substitution σ such that t |p σ = E λ σ and t = t[ρ] p σ . A term t is called (R, E )-strongly irreducible (also called a rigid normal form [11]) iff there is no term u such that t ; σ ,p,R,E u for any position p, which amounts to say that no subterm of t unifies modulo B with the left-hand side of any equation of E.
In a topmost rewrite theory R = (Σ, E B, R), with E = (Σ, E B), ; R,E is implemented in Maude by means of a three-layer narrowing relation ; R, E B [22]: 1. An (R, E B)-narrowing step from s to t with a rule l ⇒ r in R can be performed iff there is a E -unifier θ of the Σ-equation s = l such that t = rθ . 2. In turn, each E -unification problem s = ? E l of Point 1 is solved by using folding variant narrowing in the equational theory E that computes a finite, minimal and complete set of E -unifiers for s = l under suitable requirements [31]. Following [44], this is done by equationally narrowing the term s =?= l (that encodes the unification problem s = ? E l) to an extra constant tt for denoting success in the rewrite theory where the extra 8 rewrite rule ε = (X =?= X ⇒ tt) has been added to E in order to mimic unification of two terms (modulo B) as a narrowing step 9 that uses ε. 3. For each folding variant narrowing step using a rule in E modulo B in Point 2, B-unification algorithms are employed.
The search space of topmost narrowing computations in (Σ, E B, R) (respectively, FVnarrowing computations in (Σ, B, E)) can be represented as a tree-like structure that we call topmost narrowing (respectively, FV-narrowing) tree.
Equational, (R, E B)-narrowing computations are natively supported by Maude version 3.0 for unconditional rewrite theories.

Example 3. Consider the (partial) specification of integer numbers defined by the equations
where variables X, Y are of sort Int, operators p and s respectively stand for the predecessor and successor functions, and B contains the commutativity axiom X + Y = Y + X. Also consider that the program signature Σ contains a binary state constructor operator || _, _ ||: Int Int → State for a new sort State that models a simple network of processes that are either performing a common task (denoted by the first component of the state) or have finished the task (denoted by the second component). The system state t = || s(0), s(0) + p(0) || can be rewritten to || 0, s(0) || (modulo the equations of E and the commutativity of +) using the following rule that specifies the system dynamics: Also, a narrowing reachability goal from||V+V, 0+V||to||p(0), s(0)||succeeds (in one step) with computed answer substitution 10 {V → 0}, which might signal a possible programming error in rule (1) since the number of processes in the first component of the state becomes negative.

Specialization of Rewrite Theories
In this section, we present the specialization procedure NPER U A , which allows a rewrite theory R = (Σ, E B, R) to be optimized by specializing the underlying equational theory E = (Σ, E B) with respect to the calls in the rewrite rules of R. The procedure NPER U A extends the equational, narrowing-driven partial evaluation algorithm EQNPE U A of [6], which applies to equational theories and is parametric on an unfolding operator U that is used to construct finite narrowing trees for any given expression and an abstraction operator A that guarantees global termination.

Partial Evaluation of Equational Theories
Given a convergent equational theory E = (Σ, E B) and a set Q of terms (henceforth called specialized calls), we define a transformation EQNPE U A that derives a new equational theory E which computes the same answers (and values) as E for any input term t that is a recursive instance (modulo B) of the specialized calls in Q. This means that all of the subterms of t (including itself) are a substitution instance of some term in Q. The transformation EQNPE U A has two parameters, an unfolding operator U and an abstraction operator A , whose precise meaning is clarified below.
The algorithm requires that the input equational theory E to be specialized is decomposed as a rewrite theory E = (Σ, B, E), whose only equations are the equational axioms in B and where the equations in E are explicitly oriented from left to right as the set E of rewrite rules.
The transformation consists of iterating two consecutive actions: (i) Symbolic execution (Unfolding). A finite, possibly partial folding variant narrowing tree for each input term t of Q is generated. 11 This is done by using the unfolding operator U (Q, E ) that determines when and how to stop the derivations in the FV-narrowing tree.
(ii) Search for regularities (Abstraction). In order to guarantee that all calls that may occur at runtime are covered by the specialization, every (sub-)term in any leaf of the tree must be equationally closed w.r.t. Q. This notion extends the classical PD closedness by: 1) considering B-equivalence of terms; 2) considering a natural partition of the signature as Σ = D Ω, where Ω are the constructor symbols, which are used to define the (irreducible) values of the theory (also called constructor terms), and D are the defined symbols, which are evaluated away by equational rewriting; and 3) recursing over the term structure to handle nested function calls. Roughly speaking, a term u is equationally closed modulo B w.r.t. Q iff either: (i) it does not contain defined function symbols of D, or (ii) there exists a substitution θ and a (possibly renamed) q ∈ Q such that u = B qθ and the terms in θ are recursively Q-closed. For instance, given a defined binary symbol • (i.e., • ∈ D) that does not obey any structural axioms, Steps (i) and (ii) of the transformation are iterated as long as new terms are generated, and the considered abstraction operator A is used to guarantee that only finitely many expressions are evaluated, thus ensuring global termination.
For simplicity, the formulation of the EqNPE framework in [6] does not explicitly deal with subsort overloading of symbols, which could introduce subtle issues in the partial evaluation process, e.g., when a given function symbol is both a defined function symbol and a constructor operator. The new framework in this article does deal with subsort overloading via the following definition that naturally extends the equational closedness of [6] by introducing a least sort check to precisely identify constructor-rooted terms (i.e., terms whose top symbol is a constructor operator).
Definition 2 ((Extended) Equational Closedness). Let (Σ, B, E) be an equational theory decomposition and Q be a finite set of Σ-terms, i.e., terms that are built from Σ and a countably infinite set of variables X . Assume the signature Σ splits into a set D of defined function symbols and a set Ω of constructor operators so that Σ = D Ω. We say that a Σ-term t is closed modulo B, or simply B-closed, (w.r.t. Q and Σ) if closed B (Q,t) holds, where the predicate closed B is defined as follows: if the set that can be formed by taking the right-hand sides of all of the rules in R is also closed modulo B. We often omit Σ when no confusion can arise.
The main difference of Definition 2 with respect to [6] is in the case when t = c(t n ) because, due to subsort overloading, we might have an overloaded symbol in Σ with two different typings: a constructor typing and a defined typing. To cope with this, we need to search the very specific constructor declaration that matches the input term t; i.e., c : s n → s ∈ Σ, n ≥ 0, with c ∈ Ω and ls(t) = s. More precisely, we adopt the natural assumption that any constructor term is still constructor under instantiation (preregular-below condition in [50]). In other words, the preregular-below condition states that every overloaded symbol in Σ cannot have a defined typing that lies below any constructor typing for the same symbol in the sort poset.
Given a term t, its least-sort ls(t) provides the information that is required to establish whether the top symbol of t is (or is not) a constructor operator and allows us to deal with non-free 12 constructor operators (whose behavior is defined through equations), which could not be handled by our previous framework in [6]. The [ctor] attribute is the Maude syntax that is used to label constructor operators. Note that the program is preregular-below since the constructor typing c : A -> A for the overloaded operator c and its defined typing are incomparable in the sort poset. In particular, c : B -> B does not lie below c : A -> A.
The term c(a) is constructor and its least sort matches the declaration op c : A -> A whereas the term c(b) is not constructor and its least sort matches the declaration of the defined symbol op c : B -> B.
In contrast, our previous framework in [6] would have erroneously classified c(b) as / 0closed w.r.t. Q because we did not support overloaded symbols that can be constructor or defined depending on their typing. Actually, the top symbol of c(b) would simply have been considered to be constructor (because of the constructor typing op c : A -> A) and thus the closedness check for c(b) w.r.t. Q would have succeeded since its argument, b, is also constructor.
Example 5. Consider the rewrite theory in Example 1 whose complete specification appears in Appendix B. The rewrite theory is trivially preregular-below since it does not include any overloaded symbol.
Given the set L of leaves in the FV-narrowing trees for the partially evaluated calls in Q, in order to properly add to Q the non-closed (sub-)terms occurring in the terms of L , the abstraction operator A (Q, L , B) is applied, which yields a new set of terms which may need further evaluation. The abstraction operator A (Q, L , B) ensures that the resulting set of terms "covers" (modulo B) the calls previously specialized and that equational closedness modulo B is preserved throughout successive abstractions.
More formally, for the correctness of the equational partial evaluation, any instance of the generic abstraction operator A (Q, L , B) must agree with the following definition.
Definition 3 (Equational Abstraction [6]). Given the finite set of terms T and the already evaluated set of terms Q, A (Q, L , B) returns a new set Q such that: for some position p and substitution θ 2. for all t ∈ (Q ∪ T ), t is closed with respect to Q modulo B.
A concrete implementation of an abstract operator that meets the requirements of Definition 3 is shown in Section 5.1.
Note that the equational partial evaluation procedure does not explicitly compute a partially evaluated equational theory. It does so implicitly, by computing a (generally augmented) set Q of partially evaluated terms that unambiguously determine the desired partial evaluation of the equations E. The partial evaluation of E basically consists of the set E of resultants tσ = t that are associated with the derivations in the FV-narrowing tree from a root t ∈ Q to a leaf t with computed substitution σ (i.e., the accumulated substitution along the narrowing derivation to the leaf). Note that the closedness condition modulo B w.r.t. Q is satisfied for all function calls that appear in the right-hand sides of the equations in E . We assume the existence of a function GENTHEORY(Q , (Σ, E B)) that delivers the partially evaluated equational theory E = (Σ , E B ) univocally determined by Q and the original equational theory E = (Σ, E B), with Σ = Σ and B = B. Formally,

The NPER U A Scheme for the Specialization of Rewrite Theories
The specialization of the rewrite theory R = (Σ, E B, R) is achieved by partially evaluating the hosted equational theory E = (Σ, E B) w.r.t. the rules of R, which is done by using the partial evaluation procedure EQNPE U A of Section 3.1. By providing suitable unfolding and abstraction operators, different instances of the specialization scheme can be defined.
The NPER U A procedure is outlined in Algorithm 1 and it consists of two phases.

Algorithm 1 Symbolic Specialization of Rewrite Theories NPER U A (R)
Require: Phase 1 (Partial Evaluation). It applies the EQNPE U A algorithm to specialize the equational theory E = (Σ, E B) w.r.t. a set Q of specialized calls that consists of all of the maximal function calls that appear in the ( E, B)-normalized version R of the rewrite rules of R.
Given Σ = (D Ω), a maximal function call in a term t is a subterm t |w of t, with w ∈ Pos(t), such that (i) root(t |w ) ∈ D, and (ii) there does not exist w ∈ Pos(t), such that w < w and root(t |w ) ∈ D. In other words, a maximal function call in a term t is any outermost subterm of t that is rooted by a defined function symbol of E. By mcalls(R), we denote the set of all maximal calls in the rules of R.
, c}, and g and c are associative and commutative operators. Then, the maximal function calls for the term c( f (g(a, a)), c(g (g(b, b), a), a)) are f (g(a, a)) and g(g(b, b), a). Example 7. Consider the set of rewrite rules R of the rewrite theory of Example 1 that specifies the dynamics of our client-server communication protocol. Then, mcalls(R) ={enc(Q,K), dec(M,K)}.
This phase produces the new set of specialized calls Q from which the partial evaluation Phase 2 (Compression). It consists of a refactoring transformation that takes as input the rewrite theory R = (Σ, E B, R ), the computed partially evaluated theory E = (Σ , E B ), and the final set of specialized calls Q from which E derives. Roughly speaking, the transformation computes a new, much more compact equational theory E = (Σ , E B ) where unused symbols and unnecessary repetitions of variables are removed and equations of E are simplified by recursively renaming all expressions that are Q -closed modulo B by using an independent (i.e., overlap-free) renaming function that is derived from the set of specialized calls Q .
Formally, an independent renaming ρ for Q is a mapping from terms to terms that is defined as follows. For each t of sort s in Q with root(t) = f , we define ρ(t) = f t (x n : s n ), where x n are the distinct variables in t in the order of their first occurrence and f t : s n → s is a new function symbol that does not occur in Σ or Q , and is different from the root symbol of any other ρ(t ), with t ∈ Q and t t.
By abuse, we let ρ(T ) denote the set T = {ρ(t) | t ∈ T } for a given set of terms T . Compression is performed by the COMPRESS function given in Algorithm 2 that relies on the notion of best fitting calls (BFC), which is used in the renaming process for selecting the specialized calls from Q that best cover a given call t. Formally, given t and a set U of terms, let Anti B (t,U) = {u ∈ U | t = B uθ } be the subset of U whose elements are more general (modulo B) than t (i.e., the anti-instances of t modulo B occurring in U). Then, we define the best fitting calls BFC B (t,U) for t in U w.r.t. B as the subset of minimally general elements of Anti B (t,U) (i.e., if u ∈ BFC B (t,U), then BFC B (t,U) does not contain any term that is strictly more general than u modulo B). In symbols, Note that BFC B (t,U) may contain more than one element, and hence multiple best fitting terms are possible for a given term t. Let us see an example.
Example 9. Consider a signature that contains an associative binary symbol ⊕ and the constant operators a, b and c. Let X and Y be variables. Let t = a⊕b⊕c and U = {a⊕X,Y ⊕b⊕c}. Then, BFC B (t,U) = U, because both a ⊕ X and Y ⊕ b ⊕ c are anti-instances of t modulo associativity and neither a ⊕ X is more general than Y ⊕ b ⊕ c modulo A nor vice versa.

Algorithm 2 Compression algorithm
Require: Let ρ be an independent renaming for Q in 3: Essentially, the COMPRESS function recursively computes, by means of the function RN ρ , a new equation set E by replacing each call t in E by a call to the corresponding renamed function according to ρ and the best fitting calls for t in Q, i.e., BFC B (t, Q). 13 Furthermore, a new rewrite rule set R is also produced by consistently applying RN ρ to the rewrite rules of R . Formally, each rewrite rule l ⇒ r in R is transformed into the rewrite rule RN ρ (l) ⇒ RN ρ (r), in which every maximal function call t in the rewrite rule is recursively renamed according to the independent renaming ρ and BFC B (t, Q). The algorithm also computes the specialized signature Σ and restricts the set B to those axioms obeyed by the function symbols in Σ ∩ Σ . Finally, the rewrite theory R = (Σ , E B , R ) is delivered as the final outcome.
Note that, while the independent renaming suffices to rename the left-hand sides of the equations in E (since they are mere instances of the specialized calls), the right-hand sides are renamed by means of the auxiliary function RN ρ , which recursively replaces each call in the given expression by a call to the corresponding renamed function (according to ρ).
Also, observe that compression does not reduce the number of equations and rules of a rewrite theory, it just replaces (possibly) textually-large expressions in equations and rules with simpler ones that are obtained via the recursive renaming procedure. However, the effect of compression is much more striking than a mere reduction of the program size. This is because of two reasons. On the one hand, the set of axioms may be cut down whenever any operator that is equipped with some axioms and does not occur in the partially evaluated equations is taken out from the theory E , as shown in Example 17. On the other hand, potential overlaps among the specialized calls in the final set Q are removed by applying the independent renaming ρ, and so is any spurious non-determinism that might have been introduced by the specialization process before compression. This is illustrated in the following example. that can be obtained by partially evaluating the well-know append function for list concatenation w.r.t. the input call append(append(L1:List,L2:List),L3:List) that concatenates three lists by applying the append function twice.
The first two equations in the specification above reproduce the original definition of the append function where nil and _._ are the usual list constructors, while the remaining three equations provide the intended specialization for the double append call. However, note that a given term append(append(l1,l2),l3)) could be possibly narrowed by using equations 3, 4, and 5, but also using equations 1 and 2, which is certainly unintended, wastes the optimized function and has more indeterminism than the original definition.
The compression phase is able to eliminate this extra, spurious non-determinism by producing the following independent set of renamed equations where double append applications can now be reduced only by f1 equations: The following technical result holds for the specialization of the rewrite theories. Let u ∈ T Σ (X ) be B-closed w.r.t. Q and Σ and u = RN ρ (u) ∈ T Σ (X ).
2. If E satisfies the FVP, then for any ( E, B)-irreducible computed substitution σ , (u ; * Proofs of the (strong) correctness of the NPER U A specialization algorithm and of the preservation of the executability conditions of the specialized theories are given in Appendix A.

FV-narrowing for Specializing Rewrite Theories
Given a rewrite theory R = (Σ, E B, R), with E = (Σ, B, E) being a decomposition of (Σ, E B), the equational theory E in R may or may not meet the finite variant property (FVP). In this section, we formalize the notion of finite variant property and the related narrowing strategy that is called folded variant narrowing, which are the key ingredients to partially evaluate finite variant as well as non-finite vartiant equational theories.
Intuitively, given an equational theory E = (Σ, E B), the ( E,B)-variants (or simply variants) (tσ ↓ E,B , σ ) of t are the "irreducible patterns" (tσ ) ↓ E,B to which t can be narrowed, with An equational theory has the finite variant property (FVP) (or it is called a finite variant theory) iff there is a finite and complete set of most general variants for each term. It is generally undecidable whether an equational theory has the FVP [20]; a semi-decision procedure is given in [39] (and implemented in [8]) that works well in practice. The procedure in [39] works by computing the variants of all flat terms f (X 1 , . . . , X n ) for any n-ary operator f in the theory and pairwise-distinct variables X 1 , . . . , X n (of the corresponding sort); the theory does have the FVP iff there is a finite number of most general variants for every such term.
For example, the theory of Example 11 satisfies the FVP since the flat term (X and Y) has only five most general variants. In contrast, the equational theory of Example 1 does not have the finite variant property; for instance, the term d(X,Y) has an infinite number of most general variants (X, {Y →0}), (unshift(X), {Y →s(0)}), . . ., (unshift k (X), {Y → s k (0)}).
In [31], folding variant narrowing was proved to be complete, minimal, and finitary for variant generation and variant E B-unification w.r.t. ( E,B)-normalized substitutions, provided that the theory has the FVP.
FV-narrowing derivations correspond to sequences t 0 ; σ 0 , e 0 ,B t 1 ; σ 1 , e 1 ,B . . . ; σ n , e n−1 ,B t n , where t ; σ , e,B t (or simply t ; σ t when no confusion can arise) denotes a transition (modulo the axioms in B) from term t to t via the variant equation e (i.e., an oriented equation e that is enabled to be used for FV-narrowing thanks to the attribute variant) using the equational unifier σ . The composition σ 0 σ 1 σ n−1 of all the unifiers along a narrowing sequence leading to t n (restricted to the variables of t 0 ) is the computed substitution of this sequence. By notation t ; σ , E,B t (or also t ; E,B t ), we denote a FV-narrowing step which is performed using some oriented equation in E. Also, notation t ; n σ , E,B t (or simply t ; n E,B t ) denotes a FV-narrowing derivation of exactly n FV-narrowing steps. The set of all FV-narrowing computations for a term t in E can be represented as a tree-like structure, denoted by VN E (t), which we call the FV-narrowing tree of t in E .
Assuming that the initial term t is normalized, each (variant narrowing) step t ; σ , e,B t is followed by the simplification of the term into its normal form by using all of the equations in the theory, which may include not only the variant equations in the theory but also (non-variant) equations (i.e., equations without the variant attribute). More precisely, given a rewrite theory R = (Σ, E G B, R), where B is the axiom set, E is the set of variant equations, and G is the set of non-variant equations, Maude does not perform narrowing with R modulo E G B, but only modulo E B, while equational simplification is carried out modulo the whole equational set E G B. This gives a more flexible narrowing relation for rewrite rules, which is particularly useful when only the equational theory fragment (Σ, E ∪ B) has the FVP (while equations in G would break it) so that variant E B-unification is finitary, whereas variant E G B-unification would be infinitary and undecidable. Therefore, in this scenario, each narrowing step with a rewrite rule r of the form t ; σ ,R, E B t is followed by simplification using the rewrite relation → !

G E,B
) is defined as t ; σ ,p,R, E B ; → ! G E,B t iff t ; σ ,p,R, E B t , t → * G E,B t , and t = t ↓ G E,B . An important number of verification tools and techniques rely on narrowing-based variant generation: for example, protocol analyzers, proofs of coherence and local confluence, termination provers, variant-based satisfiability checkers, and different applications of symbolic reachability analyses (references can be found in [8]).

Instantiating the Specialization Scheme for FVP and non-FVP Theories
Recall that the parameterized NPER U A algorithm of Section 3.2 relies on two generic operators: an unfolding operator U that defines the unfolding rule used to determine when and how to terminate the construction of the narrowing trees; and an abstraction operator A that is used to guarantee that the set of terms obtained during partial evaluation (i.e., the set of deployed narrowing trees) is kept finite and progressively covers (modulo B) all of the specialized calls. The instantiation of the scheme requires particularizing these two parameters in order to specify a terminating, correct, and complete partial evaluation for E . We provide two different implementations for the unfolding operator U , namely, U fvp and U fvp . Both implementations exploit the folding variant narrowing strategy outlined in Section 4. Furthermore, we will resort to a single concrete definition of the abstraction operator A that works for both cases and is based on an equational generalization algorithm.

Abstraction Operator via Least General Generalizations
In general, there is no guarantee that the leaves L of the FV-narrowing trees are B-closed w.r.t. the specialized calls in Q. Indeed, the chosen unfolding operator U might deliver uncovered function calls that should be subsequently considered for specialization, while avoiding the set of partially evaluated calls from growing infinitely. In the following we introduce an abstraction operator A Elgg (Q, L , B) that returns a set Q of specialized calls that abstracts the set Q ∪ L by using the generalization process formalized in [6] that ensures that Q is B-closed w.r.t. Q ∪ L .
The abstraction operator A Elgg (Q, L , B) relies on an equational order sorted extension of the pure, syntactical least general generalization algorithm [9] so that not too much precision is lost despite the abstraction. Roughly speaking, the syntactic generalization problem for two or more expressions, in a pure syntactic and untyped setting, means finding their least general generalization (lgg), i.e., the least general expression t such that all of the given expressions are instances of t under appropriate substitutions. For instance, the expression sibling(X,Y) is a generalizer of both sibling(john,sam) and sibling(tom,sam), but their least general generalizer is sibling(X,sam).
In [9], the notion of least general generalization is extended to the order-sorted modulo axioms setting, where function symbols can obey any combination of associativity, commutativity, and identity axioms (including the empty set of such axioms). For instance, the least general generalizer of sibling(sam,john) and sibling(tom,sam) is still sibling(X,sam), when sibling is a commutative symbol. In general, there is no unique lgg in the framework of [9], due to both the order-sortedness and to the equational axioms. Nonetheless, for the case of modular combinations of associativity and commutativity axioms, there is always a finite, minimal, and complete set of equational lggs (E-lggs) so that any other generalizer has at least one of them as a B-instance. This result is fully extended in [14] to any modular combinations of associativity, commutativity, and identity axioms.
Formally, given an order-sorted signature Σ and a set of algebraic axioms B, a generalization modulo B of the nonempty set of terms {t 1 , . . . ,t n } is a pair t, Θ , where Θ = {θ 1 , . . . , θ n } is a set of substitutions, such that, for all i = 1, . . . , n, tθ i = B t i . The pair t, Θ is the least general generalization modulo B of a set of terms S, written lgg B (S), if (1) t, Θ is a generalization of S and (2) for every other generalization t , Θ of S, t is more general than t modulo B.
Let us introduce the notion of best matching set that is aimed at avoiding loss of specialization due to generalization. This notion is a proper, equational extension of [1] that we use to select the more appropriate terms in a given set U that cover a new call t. Roughly speaking, we determine the best matching set for t in a set U of terms w.r.t. B, BMS B (U,t), as follows: for each u i in U, we compute the set W i = lgg B ({u i ,t}) and select the subset M of minimal upper bounds of the union W = i W i . Then, the term u k belongs to BMS B (U,t) if at least one element in the corresponding W k belongs to M.
Let us introduce a simple motivating example, with B = / 0. d(a, b))). To compute the best matching set for t in Q, we first consider the set Now, the minimally general elements of W are f (g(x)) and f (g(y)), and thus we have BMS B (Q,t) = { f (g(a)), f (g(b))}. Set modulo B). Let E = (Σ, E B) be an order-sorted equational theory. Let U = {u 1 , . . . , u n } be a set of terms and t be a term. Given the decomposition (Σ, B, E) of (Σ, E B), consider the sets of terms W i = {w | w, {θ 1 , θ 2 } ∈ lgg B ({u i ,t})}, for i = 1, .., n, and W = n i=1 W i . The best matching set BMS B (U,t) for t in U modulo B is the set of those terms u k ∈ U such that the corresponding W k contains a minimally general element w of W under ≤ B , i.e., there is no different element w in W (modulo the relation B induced by ≤ B ) such that w < B w .

Definition 4 (Best Matching
The following example illustrates the above definition. (1), X ⊗Y }, and consider B to consist of the associativity and commutativity axioms for the function symbol ⊗. To compute the best matching set for t in U, we first compute the sets of lgg B 's of t with each of the terms in U: Now, we are ready to instantiate the abstraction parameter A of our specialization procedure with the following function A Elgg (Q, T, B) that relies on the notions of best matching set modulo B and equational least general generalization. Given the current set Q of already specialized calls, in order to augment Q with a new set T of terms, the best matching set is used when selecting the most appropriate terms of Q to be used for generalizing T , in the sense of providing appropriate least general generalizations.
Definition 5 (Abstraction operator). Let E = (Σ, E B) be an order-sorted equational theory, with Σ = D Ω. Let U = {u 1 , . . . , u n } be a set of terms and t be a term. Given the decomposition (Σ, B, E) of (Σ, E B), Let Q, T be two sets of terms. We define A Elgg (Q, T, B) = abs˘ B (Q, T ), where: if T = {c(t n : s n )} and ∃c : s n → s ∈ Σ s.t. c ∈ Ω, ls(t) = s, n ≥ 0 where Q = {t ∈ Q | root(t) = root(t ) and t B t}. The function generalize is defined as follows: It is worth noting that Definition 5 slightly generalizes the original formulation in [6] in order to fully deal with subsort overloading (including the overloading of constructor symbols). The new formalization of A Elgg (Q, T, B) searches for the very specific constructor declaration that matches the input term t, similarly to the extended notion of equational closedness of Definition 2; i.e., c : s n → s ∈ Σ, n ≥ 0, with c ∈ Ω and ls(t) = s. The following example illustrates the improved specialization power that we achieve by this extension.  This module introduces two subsorts, namely, Zero and One, whose aim is to respectively type the two values 0 and s(0). Furthermore, operator overloading is used to provide two versions of the successor operator s so that for one typing is a constructor symbol and for another typing is a defined symbol. More specifically, s : Zero → One specifies a constructor symbol that builds the canonical form s(0), while s : Nat → Nat is a defined symbol that is used to simplify any natural number s k (0), with k ≥ 2, to either the value 0 or s(0) via the equation s(s(0)) = 0.
The term s(0) is constructor and matches the declaration op s : Zero -> One whereas the term s(s(0)) is not and it matches the declaration of the defined symbol op s : Nat -> Nat.
On the contrary, our previous framework in [6] would have erroneously classified s(s(0)) as a constructor term and, as a result, the uncovered call s(s(0)) could not have been specialized.
Theorem 3. The operator A Elgg (Q, L , B) terminates and is an abstraction operator in the sense of Definition 3.

Unfolding operators
Let us provide two possible implementations of the unfolding operator U that are respectively able to deal with: (a) equational theories that do not satisfy the FVP; and (b) equational theories that satisfy the FVP. Since (Σ, B, E) is a decomposition of (Σ, E B), both implementations adopt the folding variant narrowing strategy to build the narrowing trees that are needed to specialize the input theory.
(a) When E does not meet the finite variant property, folding variant narrowing may lead to the creation of an infinite FV-narrowing tree for some specialized calls in Q. In this case, the unfolding rule must implement a form of local control that stops the expansion of infinite derivations in the FV-narrowing tree. A solution to this problem has already been provided in [6] by means of an unfolding operator that computes a finite (possibly partial) FV-narrowing tree fragment for every specialized call t in Q. Narrowing derivations in the tree are stopped when no further FV-narrowing step can be performed or potential non-termination is detected by applying a subsumption check at each FV-narrowing step. The subsumption check is based on an equational order-sorted extension˘ B [7] of the classical homeomorphic embedding relation that is commonly used to ensure termination of symbolic methods and program optimization techniques. Roughly speaking, a homeomorphic embedding relation is a structural preorder under which a term t is greater than (i.e., it embeds) another term t , written as t t , if t can be obtained from t by deleting some parts, e.g., s(s(X +Y ) * (s(X)+Y )) embeds s(Y * (X +Y ))). Embedding relations have become very popular to ensure termination of symbolic transformations because, provided the signature is finite, for every infinite sequence of terms t 1 ,t 2 , . . . , there exists i < j such that t i t j . In other words, the embedding relation is a well-quasi order (wqo) [35]. Therefore, when iteratively computing a sequence t 1 ,t 2 , . . . ,t n , finiteness of the sequence can be guaranteed by using the embedding as a whistle: whenever a new expression t n+1 is to be added to the sequence, we first check whether t n+1 embeds any of the expressions already in the sequence. If that is the case, we say that whistles, i.e., it has detected (potential) non-termination and the computation has to be stopped in t n+1 . Otherwise, t n+1 is added to the sequence and the computation can proceed. The U fvp (Q, E ) operator implements an unfolding rule that is based on the homeomorphic relation˘ B , whose full formalization relies on the following auxiliary notion. We say that a FV-narrowing derivation D is Definition 6 (Unfolding function). Given the equational theory E = (Σ, E B) with a decomposition E = (Σ, B, E), and a term t 0 to be specialized in E , we define Unfold(t 0 , E ) as the set of terms given by which eliminates prolix nested calls and redundant arguments in E computations. It is worth noting that the resulting specialization E provides a highly optimized version of E for an arbitrarily fixed key K=s(s(0)), where both functional and structural compression are achieved. Specifically, data structures in E for natural numbers and their associated operations for message encryption and decryption are totally removed from E . Note that the _+_ operator, together with its associative and commutative axioms, disappears from E , thereby avoiding expensive matching operations modulo axioms. This transformation eq dec(a,s(s(0))) = b [variant] .
eq dec(b,s(s(0))) = c [variant] . eq dec(c,s(s(0))) = a [variant] . eq dec(S:Symbol M:Message,s(s(0))) = toSym(unshift(unshift(toNat(S:Symbol)))) dec (  power cannot be achieved by existing, functional, logic or functional logic partial evaluators. Encryption (resp., decryption) in E is now the direct mapping f0 (resp., f1) that associates messages to their corresponding encrypted (resp. decrypted) counterparts, avoiding a huge amount of computation in the profuse domain of natural numbers. Finally, the com-  puted renaming is also applied to R by respectively replacing the maximal function calls enc(M,s(s(0)) and dec(M,s(s(0)) with f0(M) and f1(M) into the rewrite rules of R. This allows the (renamed) rewrite rules to be able to access the new specialized encryption and decryption functionality provided by E .
Example 1 shows that a high degree of simplification can be achieved by the specialization technique of Presto for theories that do not have the FVP. Furthermore, in many cases, the specialization algorithm is also able to transform an equational theory that does not meet the FVP into a specialized one that does. This typically happens when the function calls to be specialized can only be unfolded a finite number of times. Let us see an example.
Example 17. Consider a slight variant of the protocol theory of Example 16 in which messages consist of one single symbol instead of arbitrarily long sequences of symbols. This variant can be obtained by simply modifying the sort of the messages from Message to Symbol in the protocol rewrite rules. In this scenario, the set of maximal function calls becomes Q={enc(S,s(s(0))↓ E,B ,dec(S,s(s(0)↓ E,B )}, where S is a variable of sort Symbol. Note that the calls in Q subsume a finite number of more specific calls that correspond to the encryption and decryption of the symbols a, b and c w.r.t. the key s(s(0)). The rewrite theory can be automatically specialized by Presto for this use case by using NPER U A (R), instantiated with U = U f vp and A = A Elgg . The final outcome produced is a specialized rewrite theory R whose underlying, transformed equational theory is shown in Figure 5. This theory clearly meets the FVP since it specifies four non-recursive functions that all work over the finite domain {a,b,c}. Additionally, the obtained specialization gets rid of the associative data structure needed to build messages of arbitrary size since only one-symbol messages are allowed in the specialized program.  Finally, note that the satisfaction of the FVP allows narrowing-based reachability problems to be effectively solved within the specialized rewrite theory, while they were unfeasible in the original rewrite theory. 14 For instance, the following reachability goal succeeds, proving that it is possible to establish a successful communication from an initial state in which client Cli-A sends a request containing the crypted message c to server Srv-A Also the computed substitution {K/s(s(0)),Q/a} provides the required key K and the plain message Q that the server sends back to the client.
(b) When the equational theory E does satisfy the FVP, FV-narrowing trees are always finite objects that can be effectively constructed in finite time. Therefore, in this specific case, we define the following unfolding operator that constructs the complete FV-narrowing tree for any possible call. where t ; ! E,B t denotes a FV-narrowing derivation from t to the term t to which no FVnarrowing steps can be applied.
Note that, when an equational theory has the FVP, both unfolding operators U fvp and U fvp can be used to specialize a rewrite theory. Nonetheless, the advantage of using U fvp instead of U fvp is twofold. First, U fvp disregards any embedding check, which can be extremely time-consuming when E includes several operators that obey complex modular combinations of algebraic axioms. 15 Second, U fvp exhaustively explores the whole FV-narrowing tree of a term, while U fvp does not. This leads to a lower degree of specialization when U fvp is applied to a finite variant theory, as shown in the following (pathological) example. The equational theory specifies the encoding for natural numbers in Presburger's style 16 and uses this encoding to define the function mkEven(X,Y) that makes X even (if X is odd) by "moving" one unit from Y to X. Otherwise, if X is even, X is left unchanged. The partial evaluation of the given theory w.r.t. the call mkEven(X,Y) yields different outcomes that depend on the chosen unfolding operator. On the one hand, by using U fvp , the output is the very same input theory, thus no "real" specialization is achieved. On the other hand, the unfolding operator U fvp produces the specialized definition of mkEven given by eq mkEven(X + X,0) = X + X [variant] . eq mkEven(1 + X + X, 1) = 1 + 1 + X + X [variant] .
Termination of the symbolic specialization Algorithm NPER U A (R) for finite variant theories and non-finite variant theories follows from the global termination of the EQNPE U A (E ) algorithm proved in [6] for the unfolding function U fvp (Q, E ) and the equational least general abstraction function A Elgg (Q, L , B), and from the local termination of U fvp (Q, E ).

Theorem 4 (Termination of NPER U
A (R)). Let R = (Σ, E B, R) be a rewrite theory such that E = (Σ, E B), and E = (Σ, B, E) is a decomposition of E . Let U be an unfolding operator and let A be an abstract operator. Algorithm NPER U A (R) terminates when E satisfies the FVP (respectively, E does not satisfy the FVP) with the tandem U fvp /A Elgg (respectively, the tandem U fvp /A Elgg ). Table 1 contains the experiments that we have performed with the Presto system [47], which is a prototype implementation of the proposed specialization framework for rewrite theories. The experimental evaluation has been conducted on an Intel Xeon E5-1660 3.3GHz CPU with 64 GB RAM running Maude v3. 1 We have considered the following benchmark programs: Crypto Protocol, a variant of the client server communication protocol of Example 1 where we introduce extra functions (e.g., the Fibonacci function and the mod function that computes the remainder of the division between natural numbers) in the underlying equational theory to make key generation heavier; FM-Account, a rewrite theory that specifies a bank account system with managed accounts which automates a simple investment model; tkEven, a simple rewrite theory that tuples a couple of calls to the mkEven function of Example 18; Handshake-KMP, a handshake network protocol in which a client sends an arbitrary long and noisy message M to a server. The handshake succeeds if the server can recognize a secret handshake sequence P inside the client message M by matching P against M via the well-known KMP string matching algorithm. These experiments plus several others are also publicly available at Presto's website. For each experiment, we publish the source code of all of the examples to make the experiments easily reproducible. In many cases, we transform a rewrite theory whose operators obey algebraic axioms, such as associativity, commutativity, and unity, into a much simpler rewrite theory with no structural axioms.

Experimental Evaluation
The experiments have been divided into two classes. The first class, which is identified by the Rewriting section in Table 1, aims at measuring the speedup of the obtained specialization w.r.t. the rewriting evaluation mechanism; the second class corresponds to the Narrowing section in Table 1 and measures the speedup w.r.t. narrowing computations. Column Rls R /Eqs R (resp. Rls R /Eqs R ) shows the size of the original (resp. specialized) theories measured as the number of rules and equations they contain. We do not benchmark the specialization times since they are almost negligible (<100 ms for most cases).
To evaluate rewriting performance, we considered rewrite sequences of increasing sizes that range from 1 million to 10 million rewrite steps. Column Rewrites in Table 1 indicates the number of rewrites for each benchmark program and experiment.
For each rewriting experiment, we executed the original specification R and the specialized R on the very same input states. Specialization was performed according to the nature of each benchmark program. Therefore, programs that do not meet the FVP have been specialized using the unfolding operator U fvp , while programs that include a finite variant theory have been specialized using the U fvp operator. We recorded the following experimental data: the execution times (in ms) for R (Column T → R ) and for R (Column T → R ), and the speedup that is computed as the ratio T → R /T → R (Column Speedup → ). To reduce the noise, we considered the average time of ten runs for each experiment.
Regarding rewriting times, our figures show that the specialized rewrite theories achieve a significant improvement when compared to the original theory, with an average speedup for these benchmarks of 60.34. In other words, the specialized program is, on average, more than 60 times faster than the original program on the considered inputs. Particularly remarkable is the performance improvement of the Handshake-KMP, which reaches two orders of magnitude for the case when the specialized maximal calls within the rewrite theory partially instantiate the input pattern and input message. This is basically due to the huge simplification that is achieved by Presto by specializing the KMP string matching function. The smallest speedup for rewriting executions is obtained by tkEven. In this case, we specialize the very general call tkEven(X:Nat,Y:Nat), which offers less opportunities for optimization since no argument in tkEven has been sufficiently instantiated. For the narrowing experiments, we considered a narrowing-based reachability goal for each benchmark program, which has been used to search for solutions in a narrowing tree fragment of an increasing number of levels from 1 to 150 levels. This means that our experiments consider huge search spaces that consist of complete narrowing trees of depth up to 150 levels. It is worth recalling that narrowing-based reachability goals cannot be solved in those Maude programs with an equational theory E that does not meet the FVP since E -unification may not terminate. Thus, to be able to execute narrowing-based reachability goals in any benchmark programs R = (Σ, E B, R) regardless of their FVP behavior, we only performed narrowing steps with R modulo E B, where E = (Σ, E B) is the maximal FVP-fragment of the equational theory (Σ, E B) included in R. The rest of the equations (E \ E ) are only used for normalization as described in Section 4.
In this case, we recorded the search time in the corresponding narrowing tree fragment for the original program (Column T ; R ) as well as for the specialized program (Column T ; R ), together with the achieved speedup (Column Speedup ; ). In all narrowing experiments, the specialized program outperforms the original one by greatly reducing the time required to solve the considered narrowing-based reachability goals. The average speedup is 96.82. For some example programs, we note that the speedup for narrowing exponentially grows with the size of the narrowing tree. In particular, this happens for programs FM-Account and tkEven where the obtained specializations greatly reduce the branching factor of the narrowing trees (both at the level of rules and equations) associated with the considered reachability goals, thereby enabling a faster exploration of the search space in the specialized programs that is more noticeable the larger the size of the trees. This means that some costly analyses that might require inordinate resources, both in time and space, could be effectively performed after the transformation. For the case of Handshake-KMP, the performance gain that is obtained for narrowing with specialized rules and equations is much lower than for equational rewriting computations. This is actually expected, since in order to select the maximal FVP-fragment of the KMP equational theory (so that we can run the three-level narrowing modulo equations and axioms mechanism), we must remove the variant attribute from most of the KMP equations, which dramatically reduces the opportunities for optimizing the narrowing computations.
The original EqNPE framework of [6] was implemented in our earlier partial evaluator Victoria. Since Victoria cannot handle rewrite theories but only equational theories, the specialization of rewrite theories supported by Presto could not be previously achieved unless a complex hack is introduced not only at the level of the theory signature but also by providing a suitable program infrastructure that simulates rewrite rule nondeterminism through equations. For the case when we simply specialize an equational theory, both systems perform similarly. For instance, we benchmarked the total specialization time for a loop consisting of 1000 specializations of the KMP program that is classically used to compare program specializers, and then we divided the accumulated specialization time by 1000. This modus operandi allowed the noise on the small time for a single specialization to be reduced. The resulting specialization times of Victoria and Presto are comparable (6.6 ms and 5.6 ms, respectively). This is noteworthy since the implementation of Presto is considerably more ambitious and it provides much greater coverage of the Maude language compared to the simpler approach of Victoria.
Presto includes a FVP checker for equational theories that is based on the checking procedure described in [39]. It also implements a strong irreducibility checker that determines whether there is any redex within the left-hand sides of the rewrite rules (w.r.t. the hosted equational theory).

Related work and Conclusion
The generic specialization framework proposed in this work represents our most ambitious automated optimization scheme for rewrite theories. To efficiently achieve aggressive specialization that scales to real-world problems, the key components of the EQNPE scheme needed to be thoroughly investigated, extended, and highly optimized over the years. This is because equational problems such as order-sorted equational homeomorphic embedding and order-sorted equational least general anti-unification are much more costly than their corresponding "syntactic" counterparts and achieving proper formalizations and efficient implementations has required years [12,13,9,10,4,6,14].
Our specialization technique can have a decisive impact on the symbolic analysis of concurrent systems that are modeled as rewrite theories in Maude. The main reason why our technique is so effective in this area is that it not only achieves huge speedup for relevant classes of rewrite theories, but it can also cut down an infinite folding variant narrowing space to a finite one for the underlying equational theory E . By doing this, any E -unification problem can be finitely solved, and symbolic, narrowing-based analysis with R modulo E can be effectively performed.
Among the wide literature on logic program specialization, the partial evaluation of functional logic programs [17,2,33] is the closest to our work. The narrowing-driven Partial Eval-uation (NPE) algorithm of [17] extends to narrowing the classical PD scheme of [37] and was proved to be strictly more powerful than the PE of both logic programs and functional programs [17], with a potential for specialization that is comparable to conjunctive partial deduction (CPD) and positive supercompilation [25]. Early instances of this framework implemented partial evaluation algorithms for different narrowing strategies, including lazy narrowing [15], innermost narrowing [17], and needed narrowing [3,18].
NPE was extended in [6] to the specialization of order-sorted equational theories and implemented in the partial evaluator for equational theories Victoria. For a detailed discussion of the literature related to narrowing-driven partial evaluation, we refer to [6].
A narrowing-based partial evaluator for the lazy functional logic language Curry is described in [33,46]. Its implementation can be seen as an instance of the generic narrowing-based partial evaluation framework of [17]. This system improves the former prototype of [2] by taking into account (mutually recursive) let expressions and non-deterministic operations, while the PE system of [2] was restricted to confluent programs. Obviously, the protocol benchmarks in this paper cannot be directly specialized by using Curry's partial evaluator since neither evaluation modulo algebraic axioms nor concurrency are supported by Curry's partial evaluator; this would require artificially rewriting the program code so that any comparison would be meaningless. In the opposite direction, Presto cannot manage the specialization of higher-order functions that is achieved by [33].
Our specialization technique falls into the category of the semantic-preserving program transformations. There are very few semantic-preserving transformations for rewrite theories in the related literature. An important example is explicit coherence completion [52] between rules, equations and axioms, which is necessary for symbolic execution in rewrite theories and relies on semantically-equivalent theory transformations [41]. Also the semantic K-framework [48] and the model transformations of [49] are based on sophisticated program transformations and both preserve the reduction semantics of the original theory. Nonetheless, they do not aim to program optimization. Furthermore, Maude tools usually rely on weaker theory transformations that preserve only specific properties such as invariants or termination behavior (Full-Maude [22], Real-time Maude [45], MTT [27], and Maude-NPA [28] are prominent examples). Other transformations focus on reducing the size of the search space; for instance, equational abstraction [42,19] reduces an infinite state system to a finite quotient of the original system algebra by introducing some extra equations that preserve certain temporal logic properties.
Finally, we would like to discuss the limitations of the current specialization framework (and its associated implementation) along with possible lines of future work. Although a wide class of Maude rewrite theories can be already partially evaluated, there are still some Maude features that cannot be handled by Presto.
First, there is no support for user-defined rewrite strategies and object-oriented programming. Since these are important Maude features, as future work we plan to investigate possible extensions of the framework that can deal with them.
Second, rewriting logic is parameterized by an equational logic that, in the case of Maude, is membership equational logic (MEL). MEL allows sorts of terms to be asserted through suitable (conditional) membership axioms. Nonetheless, our specialization technique only handles order-sorted equational logic which is strictly less expressive than MEL. This is because the computation of term variants is currently formulated and implemented in Maude only for ordersorted specifications. Hence, the extension of our partial evaluation framework to deal with MEL specifications is far from trivial since it requires to extend both folding variant narrowing and variant computation to MEL specifications.
Third, we plan to generalize our specialization scheme so that it can cope with rewrite theories that are not strongly irreducible. Strong irreducibility is a reasonable requirement that is much more practical and less demanding than a constructor discipline forbidding that arguments of the left-hand side of rules contain any defined symbols. Nonetheless, we do believe that more relaxed conditions can be found to specialize rewrite theories without jeopardizing correctness of the specialization.

A Proofs of Technical Results
In this appendix, we demonstrate the main technical results of the paper, together with other important results for equational partial evaluation.
Theorem 5 (Preservation of executability conditions by EQNPE U A (E , Q) [6]). Let E = (Σ, E B) be an equational theory such that E = (Σ, B, E) is a decomposition of E , let u be a Σ-term, and let Q be a finite set of Σ-terms.
Given an unfolding operator U and an abstract operator A , let Q =EQNPE U A (E , Q) and let E = (Σ , E B ) =GENTHEORY(Q , (Σ, E B)) be the partial evaluation of E w.r.t Q. Then, E is a decomposition of E .
Theorem 3. The operator A Elgg (Q, T, B) terminates and is an abstraction operator in the sense of Definition 3.
Proof. (Proof sketch) The proof is a slight modification of the proof of Proposition 18 of [6] that considers subsort overloading. Note that Condition (1) of Definition 3 is trivially fulfilled, since abs˘ B only applies the lgg B operator, which cannot introduce function symbols not appearing in Q or T . Condition (2) is proved by well-founded, structural induction on M Q∪T , which is the multiset of all the depths of the terms in Q ∪ T . The induction entirely resembles the proof in [6] with a minor change in the treatment of the constructor case abs˘ B (Q, c(t n )), where c is a constructor symbol. In the original proof, there is no need to check the least sort of c(t n : s n ) since no overloaded operator c could have both a constructor and a defined typing. Here, we relaxed this condition in favor of the milder preregularity-below property and thus the constructor case abs˘ B (Q, c(t n : s n )) is applied if and only if the following refined check is fulfilled: c : s n → s ∈ Σ s.t. c ∈ Ω, ls(t) = s.
Similarly, termination of the abstract operator computation can be proved as in the proof of Theorem 2 of [6]. The proof scheme again exploits a well-founded, structural induction on M Q∪T .
First, we ensure that executability conditions are preserved for rewrite theories that are specialized by using the NPER U A algorithm. Proof. By Theorem 5.
Lemma 2 (Preservation of topmost condition). Let R = (Σ, E B, R) be a topmost rewrite theory where E = (Σ, B, E) is a decomposition of E = (Σ, E B). Given an unfolding operator U and an abstract operator A , let R = (Σ , E B , R ) = NPER U A (R) be a specialization of R under the independent renaming ρ. Then, R is topmost.
Proof. Immediate, since the topmost property ensures that no function symbol of the signature Σ admits a subterm of the topmost sort State. Then, the property follows straightforwardly because no extra sorts are introduced by the independent renaming ρ: given ρ(t) = f t (x n : s n ), where t has sort s, we have f t : s n → s. Given an unfolding operator U and an abstract operator A , let R = (Σ , E B , R ) = NPER U A (R) be a specialization of R under the independent renaming ρ. Then, R is E -coherent with E = (Σ , E B ).
Proof. Immediate by the condition of ( E, B)-strong irreducibility since no equation can be applied to the left-hand side of a rule of R.
It is very important that equations do not interfere with the topmost property, as shown in the following example.  However, this theory is no longer coherent, and moreover the term f(b,b) is reducible in the original rewrite theory but the corresponding renamed term f0(b) cannot be reduced in the specialized rewrite theory. Now we are ready to prove that the specialization of a rewrite theory preserves the executability conditions of the original rewrite theory. Then, E = (Σ , B , E ) is a decomposition of E , R is E -coherent, and the left-hand sides of the rules in R are ( E , B )-strongly irreducible.
Proof. The result follows immediately from Lemmata 1, 2 and 3 In order to prove our main result, we first prove the following auxiliary lemma. Let t be a term that is B-closed w.r.t. Q and Σ and let l be the left-hand side of a rule in R. Then, σ is an E -unifier of t and l if and only if RN ρ (σ ) is an E -unifier of RN ρ (t) and RN ρ (l).
The following result establishes the strong correctness of the NPER U A specialization Algorithm 1, which states that the specialized rewrite theory R =NPER U A (R) and the original theory R are equivalent in the very strong sense that all computations in R are preserved in R .
Theorem 2. (Strong correctness of NPER U A (R)). Let R = (Σ, E B, R) be a topmost rewrite theory such that E = (Σ, E B) and R is E -coherent. Let E = (Σ, B, E) be a decomposition of E , and let the left-hand sides of the rules in R be ( E, B)-strongly irreducible. Let U be an unfolding operator and let A be an abstract operator. Given the set Q of the maximal calls in the normalized rules of R, let R = (Σ , E B , R ) =NPER U A (R) be the specialization of R, with Q =EQNPE U A (E , Q) and E = (Σ , E B ) =GENTHEORY(Q , (Σ, E B)) being the partial evaluation of E w.r.t Q. (under a given independent renaming ρ for Q ).
Let u ∈ T Σ (X ) be B-closed w.r.t. Q and Σ and u = RN ρ (u) ∈ T Σ (X ). Proof. Immediate by Lemma 4 which works for: (i) matching with only axioms B, and (ii) unification with equations E and axioms B.
Global termination of the EQNPE U A (E ) algorithm was proved in [6] for a version of U fvp (Q, E ) and A Elgg (Q, L , B)) that do not deal with subsort overloading for constructor operators. By replacing these notions with the extended definitions of equational closedness and equational generalization given in this article, the proof of global termination extends with no changes to theories that fully cope with subsort overloading and that are either finite variant or non-finite variant.
More precisely, for the equational theories that do not satisfy the FVP, the equational homeomorphic embedding integrated in U fvp (Q, E ), which is used to prevent infinite narrowing derivations, is not affected by the extension, and termination of A Elgg (Q, L , B) is not affected by the extension either. For equational theories that have the FVP, U fvp (Q, E ) terminates by definition, and hence the tandem U fvp /A Elgg cannot introduce any nontermination issues.

Theorem 7 (Termination of EQNPE U
A (E , Q)). Let E = (Σ, E B) be an equational theory, and E = (Σ, B, E) be a decomposition of E . Let Q be a set of Σ-terms. Let U be an unfolding operator and let A be an abstract operator. Then, Algorithm EQNPE U A (E , Q) terminates both for the tandem U fvp /A Elgg and the tandem U fvp /A Elgg .
Proof. The result follows from: 1) the fact that U fvp (Q, E ) terminates for FVP theories; 2) the local termination of EQNPE for U fvp (Q, E ) (Theorem 1 in [6]); 3) termination of the abstraction operator A Elgg (Q, L , B)) (Theorem 2 in [6]); and 4) the fact that the proof of the global termination of EQNPE in (Theorem 3 in [6]) relies on: a) termination of the applied unfolding operator and termination of the abstraction operator A Elgg (Q, L , B)). Now we can prove the termination result for the symbolic specialization of rewrite theories that are defined on top of both, finite variant and non-finite variant equational theories.

Theorem 4. (Termination of NPER U
A (R)). Let R = (Σ, E B, R) be a rewrite theory such that E = (Σ, E B), and E = (Σ, B, E) is a decomposition of E . Let U be an unfolding operator and let A be an abstract operator. Algorithm NPER U A (R) terminates when E satisfies the FVP (respectively, E does not satisfy the FVP) with the tandem U fvp /A Elgg (respectively, the tandem U fvp /A Elgg ).
Proof. Termination of the Phase 1 follows straightforwardly from Theorem 7. Termination of the Phase 2 is immediate since function RN ρ trivially terminates (structural induction over terms).