Full text
ACUOS A System for Order-Sorted Modular ACU Generalization Master’s Thesis Presented by: Javier Espert Real Supervisors: Mar´ıa Alpuente Frasnedo Santiago Escobar Rom´an July 9th, 2012
Abstract Generalization, also called anti-unification, is the dual of unification. Given terms tand t0, a generalization is a term t00 of which tand t0are substitution instances. The dual of a most general unifier (mgu) is that of least general generalization (lgg). In this thesis, we extend the known untyped generalization algorithm to, first, an order-sorted typed setting with sorts, subsorts, and subtype polymorphism; second, we extend it to work modulo equational theories, where function symbols can obey any combination of associativity, commutativity, and identity axioms (including the empty set of such axioms); and third, to the combination of both, which results in a modular, order-sorted equational generalization algorithm. Unlike the untyped case, there is in general no single lgg in our framework, due to order-sortedness or to the equational axioms. Instead, there is a finite, minimal set of lggs, so that any other generalization has at least one of them as an instance. Our generalization algorithms are expressed by means of inference systems for which we give proofs of correctness. This opens up new applications to partial evaluation, program synthesis, data mining, and theorem proving for typed equational reasoning systems and typed rule-based languages such as ASF+SDF, Elan, OBJ, Cafe-OBJ, and Maude. This thesis also describes a tool for automatically computing the generalizers of a given set of structures in a typed language modulo a set of axioms. By supporting the modular combination of associative, commutative and unity (ACU) equational attributes for arbitrary function symbols, modular ACU generalization adds enough expressive power to ordinary generalization to reason about typed data structures such as lists, sets and multisets. The ACU generalization technique has been generally and efficiently implemented in the ACUOS system and can be easily integrated with third-party software. Keywords: least general generalization, rule–based languages, equational reasoning
iv
Resumen La generalizaci´on, tambi´en denominada anti-unificaci´on, es la operaci´on dual de la unificaci´on. Dados dos t´erminos tyt0, un generalizador es un t´ermino t00 del cual tyt0son instancias de sustituci´on. El concepto dual del unificador m´as general (mgu) es el de generalizador menos general (lgg). En esta tesina extendemos el conocido algoritmo de generalizaci´on sin tipos a, primero, una configuraci´on order-sorted con sorts,subsorts ypolimorfismo de subtipado; en segundo lugar, la extendemos para soportar generalizaci´on m´odulo teor´ıas ecuacionales, donde los s´ımbolos de funci´on pueden obedecer cualquier combinaci´on de axiomas de asociatividad, conmutatividad e identidad (incluyendo el conjunto vac´ıo de dichos axiomas); y, en tercer lugar, a la combinaci´on de ambos, que resulta en un algoritmo modular de generalizaci´on order-sorted ecuacional. A diferencia de las configuraciones sin tipos, en nuestro marco te´orico en general el lgg no es ´unico, que se debe tanto al tipado como a los axiomas ecuacionales. En su lugar, existe un conjunto finito y m´ınimo de lggs, tales que cualquier otra generalizaci´on tiene a alguno de ellos como instancia. Nuestros algoritmos de generalizaci´on se expresan mediante reglas de inferencia para las cuales damos demostraciones de correcci´on. Ello abre la puerta a nuevas aplicaciones en campos como la evaluaci´on parcial, la s´ıntesis de programas, la miner´ıa de datos y la demostraci´on de teoremas para sistemas de razonamiento ecuacional y lenguajes tipados basados en reglas tales como ASD+SDF, Elan, OBJ, CafeOBJ y Maude. Esta tesis tambi´en describe una herramienta para el c´omputo automatizado de los generalizadores de un conjunto dado de estructuras en un lenguaje tipado m´odulo un conjunto de axiomas dado. Al soportar la combinaci´on modular de atributos ecuacionales de asociatividad, conmutatividad y existencia de elemento neutro (ACU) para s´ımbolos de funci´on arbitrarios, la generalizaci´on ACU modular aporta suficiente poder expresivo a la generalizaci´on ordinaria para razonar sobre estructuras de datos tipadas tales como listas, conjuntos y multiconjuntos. La t´ecnica ha sido implementada con generalidad y eficiencia en el sistema ACUOS y puede ser f´acilmente integrada con software de terceros.
vi Palabras clave: generalizaci´on menos general, lenguajes basados en reglas, razonamiento ecuacional
Contents Introduction xi Our contribution and plan of the thesis ............. xv Related work ........................... xvii Preliminaries xxiii 1 Syntactic LGG 1 1.1 Termination and Confluence ................ 4 1.2 Correctness and Completeness ............... 8 2 Order-Sorted LGG 13 2.1 Termination and Confluence ................ 15 2.2 Computation by subsort specialization .......... 17 2.3 Correctness and Completeness ............... 19 3 LGG modulo E23 3.1 Recursively enumerating lggs modulo E.......... 23 3.2 Basic inference rules .................... 26 3.3 Least general generalization modulo C.......... 28 3.4 Least general generalization modulo A.......... 33 3.5 Least general generalization modulo AC ......... 40 3.6 Least general generalization modulo U.......... 44 3.7 A general ACU-generalization method .......... 50 4 Order-Sorted LGG modulo E51 4.1 Termination ......................... 51 4.2 Correctness and Completeness ............... 54 5 The ACUOS System 57 5.1 Algorithmic improvements ................. 57 5.2 Architecture ......................... 58 5.3 Interface ........................... 59
viii Contents 5.4 Generalizing data structures ................ 60 5.5 Experiments ......................... 62 6 Conclusions and future work 65 A A Generalization Session with ACUOS 69 Bibliography 73
List of Figures 1.1 Rules for least general generalization ........... 3 1.2 Computation trace for the (syntactic) generalization of terms f(g(a), g(y), a) and f(g(b), g(y), b)......... 5 2.1 Rules for order–sorted least general generalization. . . . . 14 2.2 Computation trace for the order–sorted generalization of terms f(x) and f(y).................... 15 2.3 Sort hierarchy ........................ 15 3.1 Basic inference rules for least general E–generalization . 27 3.2 Decomposition rule for a commutative function symbol f29 3.3 Decomposition rule for an associative (non–commutative) function symbol f...................... 34 3.4 Computation trace for the A–generalization of terms f(f(a, c), b) and f(c, b)). .................. 34 3.5 Decomposition rule for an associative–commutative function symbol f........................ 40 3.6 Computation trace for the AC–generalizations of terms f(a, f(a, b)) and f(f(b, b), a). ................ 41 3.7 Inference rule for expanding function symbol fwith identity element e........................ 45 3.8 Computation trace for the ACU–generalization of terms f(a, b, c, d) and f(a, c). ................... 46 4.1 Basic inference rules for least general E–generalization . 52 4.2 Decomposition rule for a commutative function symbol f52 4.3 Decomposition rule for an associative (non–commutative) function symbol f...................... 53 4.4 Decomposition rule for an associative–commutative function symbol f........................ 53 4.5 Inference rule for expanding function symbol fwith identity element e........................ 53
xvi List of Figures kind of equational axiom— but the overall algorithm is modular in the precise sense that the combination of different equational axioms for different function symbols is automatic and seamless: the inference rules can be applied to generalization problems involving each symbol with no need whatsoever for any changes or adaptations. This is similar to, but much simpler and easier than, modular methods for combining E-unification algorithms, e.g., (Baader and Snyder,1999). To the best of our knowledge, ours are the first equational least general generalization algorithms in the literature. An interesting result is that associative generalization is finitary, whereas associative unification is infinitary. 3. An order-sorted modular equational generalization algorithm (in Chapter 4), which combines and refines the inference rules given in Chapters 2and 3. 4. Formal correctness, completeness, and termination results for all the above generalization algorithms. 5. In Chapter 5, we present the ACUOS system, an implementation of the order-sorted, modular equational generalization algorithm described in Chapter 4. Working with the tool is further illustrated in the example work session described in the Appendix A. 6. Finally, we present some conclusions and directions for future work in Chapter 6. This paper is an extended and improved version of (Alpuente et al., 2009b,a) which unifies both, the order-sorted generalization of (Alpuente et al.,2009b) and the equational generalization of (Alpuente et al.,2009a) into a novel and more powerful, combined algorithm. The proposed algorithms should be of interest to developers of rule-based languages, theorem provers and equational reasoning programs, as well as program manipulation tools such as program analyzers, partial evaluators, test case generators, and machine learning tools, for (order-sorted) declarative languages and reasoning systems supporting commonly occurring equational axioms such as associativity, commutativity and identity in a built–in and efficient way. For instance, this includes many theorem provers, and a variety of rule-based languages such as ASF+SDF, OBJ,
List of Figures xvii CafeOBJ, Elan, and Maude. Since the many-sorted and unsorted settings are special instances of the order-sorted case, our algorithm applies a fortiori to those less expressive settings. Related work Generalization goes back to work of Plotkin (Plotkin,1970), Reynolds (Reynolds,1970), and Huet (Huet,1976) and has been studied in detail by other authors; see for example the survey (Lassez et al.,1988). Plotkin (Plotkin,1970) and Reynolds (Reynolds,1970) gave imperative–style algorithms for generalization, which are both essentially the same. Huet’s generalization algorithm (Huet,1976), formulated as a pair of recursive equations, cannot be understood as an automated calculus due to some implicit assumptions in the treatment of variables. A deterministic reconstruction of Huet’s algorithm is given in (Østvold,2004) which does not consider types either. A many-sorted generalization algorithm was presented in (Frisch and Jr.,1990) that is provided with the so-called S-sentences, which can be seen as a logical notation for encoding taxonomic (or ordering) information. Anti-unification for unranked terms, which differ from the standard ones by not having fixed arity for function symbols, and for finite sequences of such terms (called hedges) is investigated in (Kutsia et al.,2011); efficiency of the algorithm is improved by imposing a rigidity function that is a parameter of the improved algorithm. The algorithm for higher-order generalization in the calculus of constructions of (Pfenning,1991) does not consider order-sorted theories or equational axioms either, and for any two higher-order patterns, either there is no lgg (because the types are incomparable), or there is a unique lgg. The significance of equational generalization was already pointed out by Pfenning in (Pfenning,1991): “It appears that the intuitiveness of generalizations can be significantly improved if anti–unification takes into account additional equations which come from the object theory under consideration. It is conceivable that there is an interesting theory of equational anti–unification to be discovered”. However, to the best of our knowledge, we are not aware of any existing equational generalization algorithm modulo the combination of associativity, commutativity
xviii List of Figures and identity axioms. Actually, equational generalization has been absolutely neglected, except for the theory of associativity and commutativity (Pottier,1989) (in french) and for commutative theories (Baader,1991). For the commutative case, (Baader,1991) shows that all commutative theories are of generalization type ‘unitary’, but no generalization algorithm is provided. Pottier (Pottier,1989) provides (unsorted) inference rules which mechanize generalization in AC theories, but these rules do not apply to the separate cases of C or A alone, nor to arbitrary combinations of the C, A, and U axioms. Finally, (Burghardt,2005) presented a specially tailored algorithm that uses grammars to compute a finite representation of the (usually infinite) set of all E-generalizations of given terms, provided that E leads to regular congruence classes, which happens when E is the deductive closure of finitely many ground equations. However, as a natural consequence of representing equivalence classes of terms as regular tree grammars, the result of the E-generalization process is not a term, but a regular tree grammar of terms. Least general generalization in an order-sorted typed setting was first investigated in (A¨ıt-Kaci,1983). A generalization algorithm is proposed in (A¨ıt-Kaci,1983) for feature terms, which are sorted, possibly nested, attribute-based structures which extend algebraic terms by relaxing the fixed arity and fixed indexing constraints. This is done by adding features (or attribute labels) to a sort as argument indicators. Feature terms (previously known as indexed terms or Ψ-terms) were originally proposed as flexible record structures for logic programming and then used to describe different data models, including attributed typed objects, in rule-based languages which are oriented towards applications to knowledge representation and natural language processing. Since functor symbols of feature terms are ordered sorts, a feature term can be thought of as a type template which represents a set-denoting sort. By choosing to define types to be terms, and the type classification ordering to be term instantiation, the resulting type system is a lattice whose meet operation (i.e., greatest lower bound) w.r.t. the subsumption relation induced by the subset ordering (term instantiation) is first-order unification, and whose join operation (i.e., least upper bound) is firstorder generalization. This model is familiar to Prolog programmers but unlike any other type system available in typed languages. Moreover, by considering a partial order on functors, the set of sorts is also given
List of Figures xix a pre-order structure. Intuitively, a feature term Sis subsumed by a feature term Tif Scontains more information than T, or, equivalently, S denotes a subset of T. Under this subsumption order, the set of all feature terms is a prelattice provided the sort symbols are ordered as a lattice. Generalization is then defined as computing greater lower bounds in the prelattice of feature terms. The lgg of feature terms is also described in (Plaza,1995). We also refer to (Plaza,1995) for an account of several variants of feature descriptions, as used in computational linguistics and related areas, where generalization is recast as the retrieval of common structural similarity. A rich description level is achieved when types are viewed as constraints. In this context, terms can be thought of as “crystallized” sintaxes that dissolve into a semantically equivalent conjunction of elementary constraints, best defined as a “soup”, thanks to the conjuntion being associative and commutative. In the constraint setting, feature terms correspond to order-sorted feature (OSF) constraints in solved form (a normal form). Generalization in the OSF foundation is investigated in (A¨ıt- Kaci and Sasaki,2001), where an axiomatic definition of feature term generalization is provided, together with its operational realization. In the axiomatic definition, generalization is presented as an OSF-constraint construction process: the information conveyed by OSF terms is given an alternative, syntactic presentation by means of a constraint clause, and generalization is then defined by means of OSF clause generalization rules. The lattice of partially ordered type structures of (A¨ıt-Kaci,1983; A¨ıt-Kaci and Sasaki,2001) and the order-sorted equational setting of rewriting logic (Meseguer,1997) differ in several aspects and are incomparable, i.e., one is not subsumed into the other. The differences, explained below, are based on term representation, sort structure, and algebraic axioms. The order-sorted type structure is much simpler and typically finite, whereas the association of a type to each feature term makes the set of types infinite. In the much simpler order-sorted setting, only the subsort relations between basic sorts need to be explicitly considered, although implicitly each term with variables can be interpreted set-theoretically as the set of its substitution instances. Obviously, by an encoding of first order terms as feature-terms —the features simply being argument positions, e.g., the term f(t1, .., tn) if and only if the fea-
xx List of Figures ture term f(1 ⇒t1, .., , n ⇒tn)— the order-sorted syntactic algorithm presented in (Alpuente et al.,2009b) could be seen as a special case of (A¨ıt-Kaci,1983). However, feature types can also be expressed as algebraic types if we supply the missing constructors for attributes, which are called implicit constructors in (Smolka and A¨ıt-Kaci,1989). This encoding was used to develop a framework, based on equational constraint solving, where feature term unification and order-sorted term unification coexist. Thus, each term representations can be encoded into the other. On the other hand, as already hinted at above, the sort structure is different in both approaches and, thus, the algorithm presented in (A¨ıt- Kaci,2007) is different of what we present here. In (A¨ıt-Kaci and Sasaki, 2001), least upper bounds (lubs) are canonically represented as disjunctive sets of maximal terms: if one wants to specify that an element is of sort Aor Bwhen no explicit type symbol is known as their lub, then this element is induced to be of type A∨B. Instead, in an order-sorted setting (Goguen and Meseguer,1992;Meseguer,1998) the sort structure is much simpler, namely a (typically finite) poset as opposed to an infinite lattice. Yet, under the easily checkable assumption of preregularity (or E-preregularity for equational axioms Eof associativity and/or commutativity and/or identity), each term (resp. each E-equivalence class of terms) has a least sort possible, see (Goguen and Meseguer,1992), and (Clavel et al.,2007, 22.2.5). Furthermore, unlike in the feature term case, there is no global assumption of a top sort, although each connected component in the poset of sorts can be conservatively extended with a top sort for that component (the so-called kinds, see (Meseguer,1998; Clavel et al.,2007) and the Preliminaries). This means that certain generalization problems are regarded as incoherent and have no solution. For example, there is no generalization for the terms x:Bool, and y:Nat, assuming that the connected components of sorts for numbers (where Nat is one of the sorts) and truth values (where Bool is another sort) are disjoint. Thus, the sort structure contains different assumptions in each approach. Finally, even if the comma (conjunction) is handled in the OSF as an associative-commutative operator, the OSF does not support the definition of operators with combinations of algebraic properties such as commutativity, associativity and identity, while each operator in our order-sorted setting can have any desired combination of these algebraic
List of Figures xxi properties.
xxii List of Figures
Preliminaries We follow the classical notation and terminology from (TeReSe,2003) for term rewriting and from (Meseguer,1997;Goguen and Meseguer,1992) for order-sorted equational logic. We assume an order-sorted signature Σ with a finite poset of sorts (S,≤) and a finite number of function symbols. We furthermore assume akind-completed signature such that: (i) each connected component in the poset ordering has a top sort, and for each s∈Swe denote by [s] the top sort in the connected component of s, (i.e., if sand s0are sorts in the same connected component, then [s]=[s0]); and (ii) for each operator declaration f:s1×. . . ×sn→sin Σ, there is also a declaration f: [s1]×. . . ×[sn]→[s] in Σ. We assume pre-regularity of the signature Σ: for each operator declaration f:s1×. . . ×sn→s, and for the set Sfcontaining sorts s0 appearing in operator declarations of the form f:s0 1,...,s0 n→s0in Σ such that si≤s0 ifor 1 ≤i≤n, then the set Sfhas a least sort. The unique least sort of each Σ-term tis denoted by LS(t). Therefore, the top sort in the connected component of LS(t) is denoted by [LS(t)]. Since the poset (S,≤) is finite and each connected component has a top sort, given any two sorts sand s0in the same connected component, the set of least upper bound sorts of sand s0, although non necessarily a singleton set, always exists and is denoted by LUBS(s,s0). Throughout this paper, we assume that Σ has no ad-hoc operator overloading, i.e., any two operator declarations for the same symbol f with equal number of arguments, f:s1×. . .×sn→sand f:s0 1×. . .×s0 n→ s0, must necessarily have [s1] = [s0 1],...,[sn] = [s0n],[s] = [s0]. We assume an S-sorted family X={Xs}s∈Sof disjoint variable sets with each Xscountably infinite. We write the sort associated to a variable explicitly with a colon and the sort, i.e., x:Nat. A fresh variable is a variable that appears nowhere else. TΣ(X)sis the set of terms of sort s, and TΣ,sis the set of ground terms of sort s. We write TΣ(X) and TΣfor the corresponding term algebras. For a term t, we write Var(t) for the set of all variables in t. We assume that TΣ,s6=∅for every sort s.
xxiv List of Figures The set of positions of a term t, written Pos(t), is represented as a sequence of natural numbers, e.g. 1.2.1. The set of non-variable positions is written PosΣ(t). The root position of a term is Λ. The subterm of t at position pis t|pand t[u]pis the term twhere t|pis replaced by u. By root(t) we denote the symbol occurring at the root position of t. Asubstitution σis a mapping from a finite subset of X, written Dom(σ), to TΣ(X). The set of variables introduced by σis Ran(σ). The identity substitution is id. Substitutions are homomorphically extended to TΣ(X). The application of a substitution σto a term tis denoted by tσ. The restriction of σto a set of variables Vis σ|V. Composition of two substitutions is denoted by juxtaposition, i.e., σσ0(X) = σ0(σ(X)) for any variable X. We call a substitution σarenaming if there is another substitution σ−1such that (σσ−1)|Dom(σ)=id. Substitutions are sort– preserving, i.e., for any substitution σ, if X∈ Xs, then Xσ ∈ TΣ(X)s. We assume substitutions are idempotent, i.e., σ(X) = σ(σ(X)) for any variable X. A Σ-equation is an unoriented pair t=t0. An equational theory (Σ, E) is a set of Σ-equations. An equational theory (Σ, E) is regular if for each t=t0∈E, we have Var(t) = Var(t0). Given Σ and a set Eof Σ-equations, order-sorted equational logic induces a congruence relation =Eon terms t, t0∈ TΣ(X), see (Goguen and Meseguer,1992;Meseguer, 1997). The E-subsumption preorder ≤E(simply ≤when Eis empty) holds between t, t0∈ TΣ(X), denoted t≤Et0(meaning that tis more general than t0modulo E), if there is a substitution σsuch that tσ =Et0; such a substitution σis said to be an E-matcher for t0in t. The E-renaming equivalence t≃Et0(or ≃if Eis empty), holds if there is a renaming θ such that tθ =Et0. We write t <Et0(or <if Eis empty) if t≤Et0and t6≃Et0.
1 Syntactic Least General Generalization In order to better present our work, in this chapter we revisit untyped generalization (Plotkin,1970;Reynolds,1970;Huet,1976) and formalize the lgg computation by means of a new inference system that will be useful in our subsequent extension of this algorithm to the order– sorted setting given in Chapter 2and to the equational setting given in Chapter 3. Throughout this chapter, we assume unsorted terms, i.e., t∈ TΣ(X), with an unsorted signature Σ. This can be understood as the special case of having only one sort. Most general unification of a (unifiable) set Mof terms is the least upper bound (most general instance, mgi) of Munder the standard instantiation quasi-ordering ≤on terms given by the relation of being “more general” (i.e., sis an instance of t, written t≤s, iff there exists θsuch that tθ =s). Formally, instances(M) = {t0∈ TΣ(X)| ∀t∈M, t ≤t0} and mgi(M) = s∈instances(M) s.t. ∀t0∈instances(M), s ≤t0. Least general generalization, lgg, of Mcorresponds to the greatest lower bound, i.e., generalizations(M) = {t0∈ TΣ(X)| ∀t∈M, t0≤t} and lgg(M) = s∈generalizations(M) s.t. ∀t0∈generalizations(M), t0≤s.
8Chapter 1. Syntactic LGG •If t1=t0 1and t2=t0 2, then the application of the inference rule Solve to t1 y ,t2disables the application of the inference rule Solve to t0 1 y0 ,t0 2but enables the application of the inference rule Recover to t0 1 y0 ,t0 2. That is, given the two inference steps ht1 y ,t2∧t1 y0 ,t2∧CT |S|θi →Solve ht1 y0 ,t2∧CT |S∧t1 y ,t2|θi and ht1 y ,t2∧t1 y0 ,t2∧CT |S|θi →Solve ht1 y ,t2∧CT |S∧t1 y0 ,t2|θi, we have that ht1 y0 ,t2∧CT |S∧t1 y ,t2|θi →Recover hCT |S∧t1 y ,t2|θi and ht1 y ,t2∧CT |S∧t1 y0 ,t2|θi →Recover hCT |S∧t1 y0 ,t2|θi. Thus, hCT |S∧t1 y ,t2|θi≃hCT |S∧t1 y0 ,t2|θiand the conclusion follows. 1.2 Correctness and Completeness Before proving correctness and completeness of the above inference rules, we introduce the auxiliary concepts of a conflict position and of conflict pairs, and three auxiliary lemmas. Also, note that for a given constraint tx ,t0, the variable xis a valid generalization of tand t0, though generally not the least one. The first lemma states that the range of the substitutions partially computed at any stage of a generalization derivation coincides with the set of the index variables of the configuration.
1.2. Correctness and Completeness 9 Lemma 1.2.1 Given terms tand t0and a fresh variable xsuch that htx ,t0| ∅ | id i →∗hCT |S|θiusing the inference rules of Figure 1.1, then Index(S∪CT)⊆Ran(θ), and Ran(θ) = Var(xθ). Proof. Immediate by construction. The following lemma establishes an auxiliary property that is useful for defining the notion of a conflict pair of terms. Lemma 1.2.2 Given terms tand t0and a fresh variable x,htx ,t0|∅| id i →∗huy ,v∧CT |S|θiusing the inference rules of Figure 1.1 iff there exists a position pof tand t0such that t|p=u,t0|p=v, and ∀p0< p,root(t|p0) = root(t0|p0). Proof. Straightforward by successive application of the inference rule Decompose of Figure 1.1. The notion of a conflict pair is the key idea for our generalization proof schema. Definition 1.2.3 (Conflict Position/Pair) Given terms tand t0, a position p∈ Pos(t)∩ Pos(t0)is called a conflict position of tand t0if root(t|p)6=root(t0|p)and for all q < p,root(t|q) = root(t0|q). Given terms tand t0, the pair (u, v)is called a conflict pair of tand t0if there exists at least one conflict position pof tand t0such that u=t|pand v=t0|p. The following lemma states the appropriate connection between the constraints in a derivation and the conflict pairs of the initial configuration. Lemma 1.2.4 Given terms tand t0and a fresh variable x,htx ,t0|∅| id i →∗hCT |uy ,v∧S|θiusing the inference rules of Figure 1.1 iff there exists a conflict position pof tand t0such that t|p=uand t0|p=v.
10 Chapter 1. Syntactic LGG Proof. (⇒) If uy ,v∈S, then there must be two configurations huy ,v∧ CT1|S1|θ1i,hCT2|uy ,v∧S2|θ2isuch that htx ,t0|∅|id i →∗huy ,v∧CT1|S1|θ1i → hCT2|uy ,v∧S2|θ2i →∗h∅ | S|θi, uy ,v6∈ S1,uy ,v6∈ CT2, and root(u)6=root(v). By Lemma 1.2.2, there exists a position pof tand t0such that t|p=uand t0|p=v. Since root(u)6=root(v), pis a conflict position. (⇐) By Lemma 1.2.2, there is a configuration huy ,v∧CT1|S1|θ1i such that htx ,t0|∅|id i →∗huy ,v∧CT1|S1|θ1i,uy ,v6∈ S1, and root(u)6=root(v). Then, the inference rule Solve is applied, i.e., huy ,v∧CT1|S1|θ1i→hCT1|uy ,v∧S1|θ1iand the constraint uy ,v will be part of Sin the final configuration h∅ | S|θi. The following lemma establishes the link between the substitution component of a terminal configuration (simply called “computed substitution” from now on) and a proper generalization term. Lemma 1.2.5 Given terms tand t0and a fresh variable x,htx ,t0|∅| id i →∗hC|S|θiusing the inference rules of Figure 1.1 iff xθ is a generalization of tand t0. Proof. By structural induction on the term xθ. If xθ =x, then θ=id and the conclusion follows. If xθ =f(u1, . . . , uk), then the Decompose inference rule is applied and we have that t=f(t1, . . . , tk) and t0= f(t0 1, . . . , t0 k). By induction hypothesis, uiis a generalization of tiand t0 i, for each i. Now, if there is no variable shared between two different ui, then the conclusion follows. Otherwise, for each variable zshared between two different terms uiand uj, there is a constraint w1 z ,w2∈S and, by Lemma 1.2.4, there are conflict positions piin tiand t0 i, and pj
1.2. Correctness and Completeness 11 in tjand t0 jsuch that ti|pi=tj|pjand t0 i|pi=t0 j|pj. Thus, the conclusion follows. Finally, correctness and completeness are proved as follows. Theorem 1.2.6 (Correctness and Completeness) Given terms t and t0and a fresh variable x,uis the lgg of tand t0iff htx ,t0|∅| idi →∗h∅ | S|θiusing the inference rules of Figure 1.1 and u≃xθ. Proof. We rely on the already known existence and uniqueness of the lgg of tand t0(Lassez et al.,1988) and reason by contradiction. Consider the normalizing derivation htx ,t0| ∅ | idi →∗h∅ | S|θi. By Lemma 1.2.5,xθ is a generalization of tand t0. If xθ is not the lgg of tand t0 up to renaming, then there is a term uwhich is the lgg of tand t0and a substitution ρwhich is not a variable renaming such that xθρ =u. By Lemma 1.2.1,Ran(θ) = Var(xθ), hence we can choose ρwith Dom(ρ) = Var(xθ). Now, since ρis not a variable renaming, either: 1. there are variables y, y0∈Var(xθ) and a variable zsuch that yρ = y0ρ=z, or 2. there is a variable y∈Var(xθ) and a non-variable term vsuch that yρ =v. In case (1), there are two conflict positions p, p0for tand t0such that u|p=z=u|p0and xθ|p=yand xθ|p0=y0. In particular, this means that t|p=t|p0and t0|p=t0|p0. But this is impossible by Lemmas 1.2.4 and 1.2.1. In case (2), there is a position psuch that xθ|p=yand pis neither a conflict position of tand t0nor it is under a conflict position of tand t0. Since this is impossible by Lemmas 1.2.4 and 1.2.1, the claim is proved. Let us mention that the generalization algorithm can also be used to compute (thanks to associativity and commutativity of symbol ∧) the lgg of an arbitrary set of terms by successively computing the lgg of two elements of the set in the obvious way.
12 Chapter 1. Syntactic LGG
2 Order–sorted Least General Generalizations In this chapter, we generalize the unsorted generalization algorithm presented in Chapter 1to the order-sorted setting. We consider two terms tand t0having the same top sort, i.e., [LS(t)] = [LS(t0)]. Otherwise they are incomparable and no generalization exists. Starting from the initial configuration ht x:[s] ,t0| ∅ | idi where [s]=[LS(t)] = [LS(t0)], configurations are transformed until a terminal configuration h∅ | S|θiis reached. In the order–sorted setting, the lgg, in general, is not unique. Each terminal configuration h∅ | S|θi provides an lgg of tand t0given by (x:[s])θ. A substitution δis called downgrading if each binding is of the form x:s7→ x0:s0, where xand x0 are variables and s0≤s. The transition relation →is given by the smallest relation satisfying the rules in Figure 2.1. The meaning of these rules is as follows. •The rule Decompose is the syntactic decomposition generating new constraints to be solved. Fresh variables are initially assigned a top sort, which will be appropriately “downgraded” when necessary. •The rule Recover reuses a previously solved constraint, similarly to to the corresponding unsorted rule of Figure 1.1. •The rule Solve checks that a constraint ty ,t0∈C, with root(s)6= root(t), is not already solved. Then the solved constraint ty ,t0is added to the store S, and the substitution {x7→ z}is composed
14 Chapter 2. Order-Sorted LGG Decompose f∈(Σ ∪ X)∧f: [s1]×. . . ×[sn]→[s] hf(t1, . . . , tn) x:[s] ,f(s1, . . . , sn)∧C|S|θi → ht1 x1:[s1] ,s1∧. . . ∧tn xn:[sn] ,sn∧C|S|θσi where σ={x:[s]7→ f(x1:[s1], . . . , xn:[sn])},x1:[s1], . . . , xn:[sn] are fresh variables, and n≥0 Solve root(t)6=root(t0)∧s0∈LUBS(LS(t), LS(t0)) ∧@y@s00 :ty:s00 ,t0∈S ht x:[s] ,t0∧C|S|θi→hC|S∧tz:s0 ,t0|θσi where σ={x:[s]7→ z:s0}and z:s0is a fresh variable. Recover root(t)6=root(t0) ht x:[s] ,t0∧C|S∧ty:s0 ,t0|θi→hC|S∧ty:s0 ,t0|θσi where σ={x:[s]7→ y:s0} Figure 2.1: Rules for order–sorted least general generalization. with the substitution part, where zis a fresh variable with sort in the LUBS of the least sorts of both terms. Note that this is the only additional source of non-determinism (besides the choice of the constraint to work on) in our inference rules, in contrast to Figure 1.1. This extra non–determinism causes our rules to be non–confluent in general. However, this is essential for our algorithm to work, since different final configurations h∅ | S1|θ1i,...,h∅ | Sn|θni correspond to different (least general) generalizations xθ1, . . . , xθn. Example 2.0.7 Let t=f(x:A) and t0=f(y:B) be two terms where xand yare variables of sorts Aand B, respectively, and assume the sort hierarchy that is shown in Figure 2.3. The typed definition of fis f:E→E. Starting from the initial configuration hf(x:A)z:E ,f(y:B)| ∅ | id i, we apply the inference rules of Figure 2.1 and the substitutions obtained by the lgg algorithm are θ1={z:E7→ f(z2:C), z1:E7→ z2:C}and θ2={z:E7→
2.1. Termination and Confluence 15 lgg(f(x:A), f(y:B)) ↓Initial Configuration hf(x:A) z:E ,f(y:B)|∅|idi ↓Decompose hx:A z1:E ,y:B| ∅ | {z:E7→ f(z1:E)}i .Solve & h∅ | x:A z2:C ,y:B| {z:E7→ f(z2:C), z1:E7→ z2:C}i h∅ | x:A z3:D ,y:B| {z:E7→ f(z3:D), z1:E7→ z3:D}i Figure 2.2: Computation trace for the order–sorted generalization of terms f(x) and f(y) A C D E B Figure 2.3: Sort hierarchy f(z3:D), z1:E7→ z3:D}. Note that θ1and θ2are incomparable, so that we have two posible lggs where (z:E)θ1=f(z2:C) and (z:E)θ2=f(z3:D). The computation of both solutions is illustrated in Figure 2.2. 2.1 Termination and Confluence Termination of the transition system (Conf,→) is straightforward. Theorem 2.1.1 (Termination) Every derivation stemming from an initial configuration htx:s ,t0|∅|id iusing the inference rules of Figure 2.1 where s= [LS(t)] = [LS(t0)] terminates with a configuration h∅ | S|θi. Proof. Similar to the proof of Theorem 1.1.1.
16 Chapter 2. Order-Sorted LGG The transition system (Conf,→) is no longer confluent, as shown in Example 2.0.7. However, confluence can be recovered under appropriate conditions. Definition 2.1.2 (Top-sorted Constant) Given an order-sorted signature Σ, we say that a constant c:nil →sis top-sorted if s= [s]. Definition 2.1.3 (Top-sorted Variable) A variable x:sis called topsorted if s= [s]. Definition 2.1.4 (Top-sorted Term) A term tis called top-sorted if every variable and every constant in tare top-sorted. The following result uses the assumption of a kind-completed ordersorted signature described in the Preliminaries. Lemma 2.1.5 Given a top-sorted term t,LS(t) = [LS(t)]. Proof. By structural induction on t. The cases when tis a variable or a constant are straightforward. If t=f(t1, . . . , tn), then by induction hypothesis, LS(t1)=[LS(t1)], . . . , LS(tn)=[LS(tn)], and given that f: [s1]× · · · × [sn]→[s], we have that LS(t)=[LS(t)]. Lemma 2.1.6 Given two top-sorted terms t, t0, LUBS(LS(t), LS(t0)) = LS(t) = LS(t0) = [LS(t)] = [LS(t0)]. Proof. By Lemma 2.1.5,LS(t) = [LS(t)] and LS(t0) = [LS(t0)] and, since [LS(t)] is the top sort in the connected component, we conclude that LUBS(LS(t), LS(t0)) = {LS(t)}={LS(t0)}. Theorem 2.1.7 (Confluence) The set of derivations stemming from an initial configuration htx:s ,t0| ∅ | id iusing the inference rules of Figure 2.1 where tand t0are top-sorted terms and s= [LS(t)] = [LS(t0)], contain a unique solution h∅ | S|θiup to renaming. Proof. Similar to the proof of Theorem 1.1.3, but taking into account that Lemma 2.1.6 ensures that there is no non-determinism involved in the application of the inference rule Solve.
2.2. Computation by subsort specialization 17 2.2 Order-sorted lgg computation by subsort specialization Even if the set of least general generalizations of two terms is not generally a singleton, there is still a unique top-sorted generalization that can just be specialized into the appropriate subsorts. This enables a different approach to computing order-sorted least general generalizations by just removing sorts (i.e., upgrading variables to top sorts) in order to compute (unsorted) lgg’s, and then obtaining the right subsorts by a suitable post-processing. Obviously, the set of inference rules of Figure 2.1 has a better performance than this alternative method of first upgrading variables, computing the standard lggs, and then downgrading variables, since the inference rules of Figure 2.1 detect sort-based failures much earlier. Indeed, we do not use this approach in practice, but we only use it for the proofs of correctness and completeness of the inference rules given in Section 2.3 below. Note that this proof schema for correctness and completeness of inference rules is useful here for the order-sorted generalization and also for the order-sorted E-generalization of Chapter 4below. To simplify our notation, in the following we write t[u]p1,...,pninstead of ((t[u]p1)· · · )[u]pn. The notion of conflict pair of Definition 1.2.3 can be extended to the order-sorted case in the obvious way, since two variables of different sorts having the same name, e.g. x:s1and x:s2, are considered to be different. Definition 2.2.1 (Top-sorted Generalization) Given terms tand t0 such that [LS(t)] = [LS(t0)], let (u1, v1),...,(uk, vk)be the conflict pairs of t and t0, and for each such conflict pair (ui, vi), let pi 1, . . . , pi nibe the corresponding conflict positions (i.e., t|pi j=uiand t0|pi j=vifor 1≤j≤ni), and let si= [LS(ui)] = [LS(vi)]. The top-sorted generalization of tand t0is defined by tsg(t, t0) = ((t[x1:s1]p1 1,...,p1 n1)· · · )[xk:sk]pk 1,...,pk nk where x1:s1, . . . , xk:skare fresh variables.
24 Chapter 3. LGG modulo E theory Ehas the additional property that each E-equivalence class is finite and can be effectively generated, then the above process becomes a terminating algorithm, generating a finite complete set of generalizations of tand s. In any case, for any finite set of equations E, we can always mathematically characterize a minimal complete set of E-generalizations, namely the set lggE(t, s) defined as follows. Roughly speaking, the minimal and complete set lggE(t, s) is just the minimal set than can be obtained from a complete (generally non-minimal) set genE(t, s) by filtering only the maximal elements of the set with regard to the ordering <E, as also noted in (Pottier,1989). Definition 3.1.1 Let tand sbe terms and let Ebe an equational theory. A complete set of generalizations of tand smodulo E, denoted by genE(t, t0), is defined as follows: genE(t, t0) = {v| ∃u, u0, t =Eu, t0=Eu0, v ∈lgg(u, u0)}. The set of least general generalizations of tand smodulo Eis defined as follows: lggE(t, s) = maximal<E(genE(t, s)) where maximal<E(S) = {s∈S|@s0∈S:s <Es0}. Lggs are equivalent modulo renaming and, therefore, we remove from lggE(t, t0)renamed versions (modulo E) of terms. Our modular E-generalization algorithm defined below computes a complete set of generalizations, i.e., the set genE(t, t0), that must be filtered out to obtain the least general generalizations, i.e., the set lggE(t, t0). Let us prove that the set genE(t, t0) is a complete set of E-lggs. Lemma 3.1.2 Given terms tand t0in an equational theory E, if uis an lgg modulo Eof tand t0, then there exists u0∈genE(t, t0)such that u0≃Eu. Proof. By contradiction. Let ube a lgg of tand smodulo Eand assume that there is no u0∈genE(t, t0) such that u0≃Eu. Since u≤Etand u≤Es, there exist substitutions σtand σssuch that uσt=Etand uσs=Es. But then, u∈lgg(uσt, uσs) (i.e., without making use of the
3.1. Recursively enumerating lggs modulo E25 equations E) and, by definition, u∈genE(t, s), which contradicts the assumption. Now, the minimality and completeness result for lggE(t, t0) follows straightforwardly. Theorem 3.1.3 Given terms tand t0in an equational theory E, lggE(t, t0)is a minimal, correct, and complete set of lggs modulo Eof tand t0(up to renaming). Proof. Lemma 3.1.2 ensures that genE(t, t0) is a complete set of lggs. Minimality of the set lggE(t, t0) is ensured by maximality of the relation <E. However, note that in general the relation t <Et0is undecidable, so that the above set, although definable at the mathematical level, might not be effectively computed. Nevertheless, when: (i) each E-equivalence class is finite and can be effectively generated, and (ii) there is an E- matching algorithm, then we also have an effective algorithm for computing lggE(t, s), since the relation t≤Et0is precisely the E-matching relation. In summary, when Eis finite and satisfies conditions (i) and (ii), the above definitions give us a feasible, although horribly inefficient, procedure to compute a finite, minimal, and complete set of least general generalizations lggE(t, s), because the cardinality of the E-equivalence classes can be exponential in the size of their elements, as in the case of associative-commutative theories (Pottier,1989): for instance, if fis AC, then the class Efor f(a1, f(a2, ..., f(an−1, an)...)) has (2n−2)!!/((n−1)! elements. This naive algorithm could be used when Econsists of associativity and/or commutativity axioms for some functions symbols, because such theories (a special case of our proposed parametric family of theories) all satisfy conditions (i)–(ii). However, when we add identiy axioms, E-equivalence classes become infinite, so that the above approach no longer gives us a lgg algorithm modulo E. In the following sections, we do provide a modular, minimal, terminating, sound, and complete algorithm for equational theories containing different axioms such as associativity, commutativity, and identity (and
26 Chapter 3. LGG modulo E their combinations). This algorithm computes the set genE(t, t0) modulo Eand renaming. The set lggE(t, s) of least general E-generalizations can be computed as in Definition 3.1.1. That is: first a complete set of E-generalizations is computed by the inference rules given below, and then they are filtered to obtain lggE(t, s) by using the fact that, for all theories Ein the parametric family of theories we consider in this paper, there is a matching algorithm modulo Ethat provides the relation <E. We consider that a given function symbol fin the signature Σ obeys a subset of axioms ax(f)⊆ {Af, Cf, Uf}. In particular, fmay not satisfy any such axioms, i.e., ax(f) = ∅. Note that, technically, variables of the original terms are handled in our rules as constants, thus without any attribute, i.e., for any variable x∈X, we consider ax(x) = ∅. Let us provide our inference rules for equational generalization in a stepwise manner. First, ax(f) = ∅in Section 3.2, then, ax(f) = {Cf}in Section 3.3, then, ax(f) = {Af}in Section 3.4, then, ax(f) = {Af, Cf} in Section 3.5, and finally, Uf∈ax(f) in Section 3.6. In each section, proofs of correctness and completeness are very similar to the ones in Section 1.2 and, thus, we define a key notion of conflict pair for each equational property (i.e., commutative conflict pairs, associative conflict pairs, associative-commutative conflict pairs, and identity conflict pairs) which is the basis for our overall proof scheme. For readability, we have provided complete proofs, even if they are in several aspects similar and differ mainly in the different conflict pair notions, which make it impossible to structure the proof in a parametric way. 3.2 Basic inference rules for least general E– generalization Let us start with a set of basic rules that are the equational version of the syntactic generalization rules of Chapter 1. The rule DecomposeE applies to function symbols obeying no axioms, ax(f) = ∅. Specific rules for decomposing constraints involving terms that are rooted by symbols obeying equational axioms, such as ACU and their combinations, are given below. Concerning the rules SolveEand RecoverE, the main difference w.r.t. the corresponding syntactic generalization rules given in Chapter 1is in
3.2. Basic inference rules 27 DecomposeE f∈(Σ ∪ X)∧ax(f) = ∅ hf(t1, . . . , tn)x ,f(t0 1, . . . , t0 n)∧CT |S|θi → ht1 x1 ,t0 1∧. . . ∧tn xn ,t0 n∧CT |S|θσi where σ={x7→ f(x1, . . . , xn)},x1, . . . , xnare fresh variables, and n≥0 SolveE f=root(t)∧g=root(t0)∧f6=g∧Uf6∈ ax(f)∧Ug6∈ ax(g)∧@y:ty ,t0∈ES htx ,t0∧CT |S|θi→hCT |S∧tx ,t0|θi RecoverE root(t)6=root(t0)∧ ∃y:ty ,t0∈ES htx ,t0∧CT |S|θi→hCT |S|θσi where σ={x7→ y} Figure 3.1: Basic inference rules for least general E–generalization the fact that the checks to the store consider the constraints modulo E: in the rules below, we write (ty ,t0)∈ESto express that there exists uy ,u0∈Ssuch that t=Euand t0=Eu0. Finally, regarding the rule SolveE, note that this rule cannot be applied to any constraint tx ,ssuch that either tor sare rooted by a function symbol fwith Uf∈ax(f). For function symbols with an identity element, a specially–tailored rule ExpandUis given in Section 3.6 that gives us the opportunity to solve a constraint (conflict pair) f(t1, t2)x ,s, such that root(s)6=f, with a generalization f(y, z) more specific than x, by first introducing the constraint f(t1, t2)x ,f(s, e). Termination, correctness and completeness of the basic algorithm are straightforward by reasoning similarly to the syntactic case of Chapter 1. Theorem 3.2.1 (Termination) Given an equational theory (Σ, E),Σ- terms tand t0such that every symbol in tand t0is free, and a fresh variable x, every derivation stemming from an initial configuration htx ,t0|∅| idiusing the inference rules of Figure 3.1 terminates with a configuration
28 Chapter 3. LGG modulo E h∅ | S|θi. Proof. It follows directly from Theorem 1.1.1. Theorem 3.2.2 (Correctness and Completeness) Given an equational theory (Σ, E),Σ-terms tand t0such that every symbol in tand t0is free, and a fresh variable x, then u∈genE(t, t0)iff there is u0in {xθ | htx ,t0| ∅ | idi →∗h∅ | S|θi} using the inference rules of Figure 3.1 such that u≃u0. Proof. It follows directly from Theorem 1.2.6. Note that the basic inference rules of Figure 3.1 are confluent when E=∅, according to Theorem 1.1.3, but the inference system of Chapter 4 combining free, commutative, associative, and associative-commutative operators with and without an identity element is not generally confluent, and different final configurations h∅ | S1|θ1i,...,h∅ | Sn|θnicorrespond to different (least general) generalizations xθ1, . . . , xθn. 3.3 Least general generalization modulo C In this section we extend the basic set of equational generalization rules by adding a specific inference rule DecomposeC, given in Figure 3.2, for dealing with commutativity function symbols. This inference rule replaces the syntactic decomposition inference rule for the case of a binary commutative symbol f, i.e., the two possible rearrangements of the terms f(t1, t2) and f(t0 1, t0 2) are considered. Just notice that this rule is (don’t know) non-deterministic, hence all four combinations must be explored. Example 3.3.1 Let t=f(a, b) and s=f(b, a) be two terms where fis commutative, i.e., ax(f) = {Cf}. By applying the rules SolveE,RecoverE, and DecomposeCabove, we end in a terminal configuration h∅ | S|θi, where θ={x7→ f(b, a), x37→ b, x47→ a}, thus we conclude that the lgg modulo Cof tand sis xθ =f(b, a).
3.3. Least general generalization modulo C29 DecomposeC Cf∈ax(f)∧Af6∈ ax(f)∧i∈ {1,2} hf(t1, t2)x ,f(t0 1, t0 2)∧CT |S|θi→ht1 x1 ,t0 i∧t2 x2 ,t0 (i mod 2)+1 ∧CT |S|θσi where σ={x7→ f(x1, x2)}, and x1, x2are fresh variables Figure 3.2: Decomposition rule for a commutative function symbol f Termination is straightforward. Theorem 3.3.2 (Termination) Given an equational theory (Σ, E),Σ- terms tand t0such that every symbol in tand t0is free or commutative, and a fresh variable x, every derivation stemming from an initial configuration htx ,t0|∅|idiusing the inference rules of Figures 3.1 and 3.2 terminates with a configuration h∅ | S|θi. Proof. Similar to the proof of Theorem 1.1.1 by considering the two possible rearrangements of each term. In order to prove correctness and completeness of the lgg calculus modulo C, similarly to Definition 1.2.3 we introduce the auxiliary concept of commutative conflict pair, and prove some useful results for this case. First, we prove an auxiliary result stating that only (independently) fresh variables yappear in the index positions of the constraints in CT and Scomponents of lgg configurations. Lemma 3.3.3 (Uniqueness of Generalization Variables) Lemma 1.1.2 holds for tx ,t0when the symbols in t,t0are free or commutative, for the inference rules of Figures 3.1 and 3.2. The first lemma states that the range of the substitutions partially computed at any stage of a generalization derivation coincides with the set of the index variables of the configuration. Lemma 3.3.4 Given terms tand t0such that every symbol in tand t0is free or commutative, and a fresh variable xsuch that htx ,t0| ∅ | id i →∗hCT |S|θiusing the inference rules of Figures 3.1 and 3.2, then Index(S∪CT)⊆Ran(θ), and Ran(θ) = Var(xθ).
30 Chapter 3. LGG modulo E Proof. Immediate by construction. The following lemma establishes an auxiliary property that is useful for defining the notion of a commutative conflict pair of terms. The depth of a position is defined as depth(Λ) = 0 and depth(i.p) = 1 + depth(p); in other words, it is the length of the sequence p. Given a position p with depth n,p|kis the (prefix) position pat depth k≤n, i.e., p|0=Λ, (i.p)|k=i.(p|k−1) if k > 0. For instance, for p= 1.2.1.3, p|3= 1.2.1. Lemma 3.3.5 Given terms tand t0such that every symbol in tand t0is free or commutative, and a fresh variable x,htx ,t0|∅|id i →∗ huy ,v∧CT |S|θiusing the inference rules of Figures 3.1 and 3.2 iff there exist a position p∈Pos(t)and a position p0∈Pos(t0)such that t|p=u,t0|p0=v, depth(p) = depth(p0), and ∀1≤i≤depth(p), root(t|p|i) = root(t0|p0|i). Proof. Straightforward by successive application of the inference rule Decompose of Figure 1.1 and the inference rule DecomposeCof Figure 3.2. Definition 3.3.6 (Commutative Conflict Pair) Given terms tand t0such that every symbol in tand t0is free or commutative, the pair (u, v) is called a commutative conflict pair of tand t0iff u6=Evand there exist at least one position p∈Pos(t)and at least one position p0∈Pos(t0) such that t|p=u,t0|p0=v, depth(p) = depth(p0), and ∀1≤i≤depth(p), root(t|p|i) = root(t0|p0|i). The following lemma states the appropriate connection between the constraints in a derivation and the commutative conflict pairs of the initial configuration. Lemma 3.3.7 Given terms tand t0such that every symbol in tand t0 is free or commutative, and a fresh variable x,htx ,t0| ∅ | id i →∗hCT | uy ,v∧S|θiusing the inference rules of Figures 3.1 and 3.2 iff (u, v)is a commutative conflict pair of tand t0.
3.3. Least general generalization modulo C31 Proof. (⇒) If uy ,v∈S, then there must be two configurations huy ,v∧ CT1|S1|θ1i,hCT2|uy ,v∧S2|θ2isuch that htx ,t0|∅|id i →∗huy ,v∧CT1|S1|θ1i → hCT2|uy ,v∧S2|θ2i →∗h∅ | S|θi, uy ,v6∈ S1,uy ,v6∈ CT2, and root(u)6=root(v). By Lemma 3.3.5, there exists a position p∈Pos(t) and a position p0∈Pos(t0) such that t|p=u, t0|p0=v,depth(p) = depth(p0), and ∀1≤i≤depth(p), root(t|p|i) = root(t0|p0|i). Therefore, (u, v) is a commutative conflict pair. (⇐) By Lemma 3.3.5, there is a configuration huy ,v∧CT1|S1|θ1i such that htx ,t0|∅|id i →∗huy ,v∧CT1|S1|θ1i,uy ,v6∈ S1, and root(u)6=root(v). Then, the inference rule Solve is applied, i.e., huy ,v∧CT1|S1|θ1i→hCT1|uy ,v∧S1|θ1iand uy ,vwill be part of Sin the final configuration h∅ | S|θi. The following lemma establishes the link between the computed substitution and a proper generalization term. Lemma 3.3.8 Given terms tand t0such that every symbol in tand t0is free or commutative, and a fresh variable x,htx ,t0|∅|id i →∗ hC|S|θi, using the inference rules of Figures 3.1 and 3.2 iff xθ is a generalization of tand t0modulo commutativity. Proof. By structural induction on the term xθ. If xθ =x, then θ=id and the conclusion follows. If xθ =f(u1, . . . , uk) and fis free, then the inference rule DecomposeEof Figure 3.1 is applied and we have that t=f(t1, . . . , tk) and t0=f(t0 1, . . . , t0 k). If xθ =f(u1, . . . , uk) and f is commutative, then the inference rule DecomposeCof Figure 3.2 is applied and we have that either: (i) t=f(t1, t2) and t0=f(t0 1, t0 2), or (ii) t=f(t1, t2) and t0=f(t0 2, t0 1), or (iii) t=f(t2, t1) and t0=f(t0 1, t0 2), or (iv) t=f(t2, t1) and t0=f(t0 2, t0 1). By induction hypothesis, uiis
32 Chapter 3. LGG modulo E a generalization of tiand t0 i, for each i. Now, if for each pair of terms in u1, . . . , ukthere are no shared variables, then the conclusion follows. Otherwise, for each variable zshared between two different terms uiand uj, there is a constraint w1 z ,w2∈Sand, by Lemma 3.3.7, there is a commutative conflict pair (w1, w2) in tiand t0 i. Thus, the conclusion follows. Finally, correctness and completeness are proved as follows. Theorem 3.3.9 (Correctness and Completeness) Given an equational theory (Σ, E),Σ-terms tand t0such that every symbol in tand t0is free or commutative, and xa fresh variable, then u∈genE(t, t0)iff there is u0in {xθ | htx ,t0|∅|idi →∗h∅ | S|θi} using the inference rules of Figures 3.1 and 3.2 such that u≃Eu0. Proof. By contradiction. By Lemma 3.3.8,xθ is a generalization of t and t0. If xθ is not a generalization of tand t0up to renaming, then there is a term uwhich is a generalization of tand t0and a substitution ρ which is not a variable renaming such that xθρ =Eu. By Lemma 3.3.4, Ran(θ) = Var(xθ), hence we can choose ρwith Dom(ρ) = Var(xθ). Since ρis not a variable renaming, either: 1. there are variables y, y0∈Var(xθ) and a variable zsuch that yρ = y0ρ=z, or 2. there is a variable y∈Var(xθ) and a non-variable term vsuch that yρ =v. In case (1), there are two positions p, p0in usuch that u|p=z=u|p0. Moreover, there is a position qin xθ such that (xθ)|q=yand the pair (y, z) is a conflict pair of xθ and u. Similarly there is a position q0in xθ such that (xθ)|q0=y0and the pair (y0, z) is a conflict pair of xθ and u. But this also means that there is a position qtin tsuch that t|qt=w1 and the pair (w1, z) is a conflict pair of tand u; and there is a position q0 t0in t0such that t|q0 t0=w2and the pair (w2, z) is a conflict pair of t0 and u. But this is impossible by Lemmas 3.3.7 and 3.3.4. In case (2), there is a position psuch that (xθ)|p=yand, since yρ =vand vis a
3.4. Least general generalization modulo A33 non-variable term, then pis not involved in any conflict pair of tand t0. But this is again impossible by Lemmas 3.3.7 and 3.3.4. We recall again that in general the inference rules of Figures 3.1 and 3.2 together are not confluent, and different final configurations h∅ | S1| θ1i,...,h∅ | Sn|θnicorrespond to different generalizations xθ1, . . . , xθn. 3.4 Least general generalization modulo A In this section we provide a specific inference rule DecomposeAfor handling function symbols obeying the associativity axiom (but not the commutativity one). A specific set of rules for dealing with AC function symbols is given in the next section. The DecomposeArule is given in Figure 3.3. We use flattened versions of the terms which use poly-variadic versions of the associative symbols, i.e., being fan associative symbol, with narguments, and n≥2, flattened terms are canonical forms w.r.t. the set of rules given by the following rule schema f(x1, . . . , f(t1, . . . , tn), . . . , xm)→f(x1, . . . , t1, . . . , tn, . . . , xm)n, m ≥2 Given an associative symbol fand a term f(t1, . . . , tn) we call f-alien terms (or simply alien terms) to those terms among t1, . . . , tnthat are not rooted by f. In the following, being fan associative poly-varyadic symbol, by convention f(t) represents the term titself, since symbol f needs at least two arguments. The inference rule of Figure 3.3 replaces the syntactic decomposition inference rule for the case of an associative function symbol f, where all prefixes of t1, . . . , tnand t0 1, . . . , t0 mare considered. Note that this rule is (don’t know) non-deterministic, hence all possibilities must be explored. This inference rule for associativity is better than generating all terms in the corresponding equivalence class, as explained in Chapter 3, since we will eagerly stop the computation whenever we find a constraint tx ,f(t1, . . . , tn) such that root(t)6=fwithout considering all the combinations in the equivalence class of f(t1, . . . , tn). We give the rule DecomposeAfor the case when, in the generalization problem f(t1, ..., tn)x ,f(s1, ..., sm), we have that n≥m. For the other
40 Chapter 3. LGG modulo E DecomposeAC {Af, Cf} ⊆ ax(f)∧n≥m∧ {i1, . . . , im−1}¯ ]{im, . . . , in}={1, . . . , n} hf(t1, . . . , tn)x ,f(t0 1, . . . , t0 m)∧C|S|θi → hti1 x1 ,t0 1∧. . . ∧tim−1 xm−1 ,t0 m−1∧f(tim, . . . , tin)xm ,t0 m∧C|S|θσi where σ={x7→ f(x1, . . . , xm)}, and x1, . . . , xmare fresh variables Figure 3.5: Decomposition rule for an associative–commutative function symbol f 3.5 Least general generalization modulo AC In this section we provide a specific inference rule DecomposeAC for handling function symbols obeying both the associativity and commutativity axioms. Note that we use again flattened versions of the terms, as in the associative case of Section 3.4. Actually, by considering AC function symbols as varyadic functions with no ordering among the arguments, an AC term can be represented by a canonical representative (Hullot, 1980;Eker,2003) such that =AC is decidable. The new decomposition rule for the AC case is similar to the decompose inference rule for associative function symbols, except that all permutations of f(t1, . . . , tn) and f(s1, . . . , sm) are considered. As before, the AC generalization of tand sare the maximal elements w.r.t. <AC of the normal forms of tx ,t0w.r.t. the new extended generalization calculus. Just notice that this rule is (don’t know) non-deterministic, hence all possibilities must be explored. Similarly to the rule DecomposeA, we give the rule DecomposeAC for the case when, in the generalization problem f(t1, ..., tn)x ,f(s1, ..., sm), we have that n≥m. For the other way around, i.e., n < m, a similar rule would be needed, that we omit since it is entirely similar. To simplify, we write {i1, . . . , ik}¯ ]{ik+1, . . . , in}={1, . . . , n}to denote that the sequence {i1, . . . , in}is a permutation of the sequence {1, . . . , n}and, given an element k∈ {1, . . . , n}, we split the sequence {i1, . . . , in}in the two parts, {i1, . . . , ik}and {ik+1, . . . , in}.
3.5. Least general generalization modulo AC 41 lggE(f(a, f(a, b)), f(f(b, b), a)), with E ={Cf, Af} ↓Initial Configuration hf(a, a, b) x ,f(b, b, a)|∅|idi .DecomposeAC (Other permutations are not shown) & ha x1 ,b∧a x2 ,b∧b x3 ,a|∅|{x7→f(x1, x2, x3)}i ha x4 ,b∧a x5 ,a∧b x6 ,b|∅|{x7→f(x4, x5, x6)}i ↓Solve ↓Solve ha x2 ,b∧b x3 ,a|a x1 ,b| {x7→ f(x1, x2, x3)}i ha x5 ,a∧b x6 ,b|a x4 ,b| {x7→ f(x4, x5, x6)}i ↓Recover ↓Decompose hb x3 ,a|a x1 ,b| {x7→ f(x1, x1, x3), x27→ x1}i hb x6 ,b|a x4 ,b| {x7→ f(x4, a, x6), x57→ a}i ↓Solve ↓Decompose h∅ | a x1 ,b∧b x3 ,a| {x7→ f(x1, x1, x3), x27→ x1}i h∅ | a x4 ,b| {x7→ f(x4, a, b), x57→ a, x67→ b}i &maximal<AC . {x7→ f(x1, x1, x3), x27→ x1}and {x7→ f(x4, a, b), x57→ a, x67→ b} Figure 3.6: Computation trace for the AC–generalizations of terms f(a, f(a, b)) and f(f(b, b), a). Example 3.5.1 Let t=f(a, f(a, b)) and s=f(f(b, b), a) be two terms where fis associative and commutative, i.e., ax(f) = {Af, Cf}. By applying the rules SolveE,RecoverE, and DecomposeAC above, we end in two terminal configurations whose respective substitution components are θ1={x7→ f(x1, x1, x3), x27→ x1}and θ2={x7→ f(x4, a, b), x57→ a, x67→ b}, thus we compute that the lggs modulo AC of tand sare f(x1, x1, x3) and f(x4, a, b). The corresponding computation trace is shown in Figure 3.6. Termination is straightforward. Theorem 3.5.2 (Termination) Given an equational theory (Σ, E),Σ- terms tand t0such that every symbol in tand t0is free or associativecommutative, and xis a variable, every derivation stemming from an initial configuration htx ,t0|∅|idiusing the inference rules of Figures 3.1 and 3.5 terminates with a configuration h∅ | S|θi. Proof. Similar to the proof of Theorem 1.1.1. In order to prove correctness and completeness of the lgg calculus modulo AC, similarly to Definitions 1.2.3,3.3.6, and 3.4.7, we introduce the auxiliary concept of an associative-commutative conflict pair, and prove the appropriate auxiliary results.
42 Chapter 3. LGG modulo E First, we prove an auxiliary result stating that only (independently) fresh variables yappear in the index positions of the constraints in CT and Scomponents of lgg configurations. Lemma 3.5.3 (Uniqueness of Generalization Variables) Lemma 1.1.2 holds for tx ,t0when the symbols in t,t0are free or associativecommutative, for the inference rules of Figures 3.1 and 3.5. The lemma below states that the range of the substitutions partially computed at any stage of a generalization derivation coincides with the set of the index variables of the configuration. Lemma 3.5.4 Given terms tand t0such that every symbol in tand t0is free or associative-commutative, and a fresh variable xsuch that htx ,t0| ∅ | id i →∗hCT |S|θiusing the inference rules of Figures 3.1 and 3.5, then Index(S∪CT)⊆Ran(θ), and Ran(θ) = Var(xθ). Proof. Immediate by construction. The following lemma establishes an auxiliary property that is useful for defining the notion of an associative-commutative conflict pair of terms. Lemma 3.5.5 Given flattened terms tand t0such that every symbol in tand t0is free or associative-commutative, and a fresh variable x, htx ,t0| ∅ | id i →∗huy ,v∧CT |S|θiusing the inference rules of Figures 3.1 and 3.5 iff there exist a position p∈Pos(t)and a position p0∈Pos(t0)such that either: 1. t|p=u,t0|p0=v, depth(p) = depth(p0), and ∀1≤i≤depth(p), root(t|p|i) = root(t0|p0|i); or 2. t|p=u,v=f(v1, . . . , vn),t0|p0=f(w1, . . . , wm),fis associativecommutative, for each i∈ {1, . . . , n}there is j∈ {1, . . . , m}s.t. vi=Ewj, depth(p) = depth(p0)+1, and ∀1≤i≤depth(p0), root(t|p|i) = root(t0|p0|i); or
3.5. Least general generalization modulo AC 43 3. u=f(u1, . . . , un),t|p=f(w1, . . . , wm),t0|p0=v,fis associativecommutative, for each j∈ {1, . . . , m}there is i∈ {1, . . . , n}s.t. wj=Evi, depth(p0) = depth(p)+1, and ∀1≤i≤depth(p), root(t|p|i) = root(t0|p0|i). Proof. Straightforward by successive application of the inference rule Decompose of Figure 1.1 and the inference rule DecomposeAC of Figure 3.5. Definition 3.5.6 (Associative-commutative Conflict Pair) Given flattened terms tand t0such that every symbol in tand t0is free or associativ-com mutative, the pair (u, v)is called an associative conflict pair of tand t0 iff there exist at least one position p∈Pos(t)and at least one position p0∈Pos(t0)such that either: 1. t|p=u,t0|p0=v,u6=Ev, depth(p) = depth(p0), and ∀1≤i≤ depth(p),root(t|p|i) = root(t0|p0|i); or 2. t|p=u,v=f(v1, . . . , vn),t0|p0=f(w1, . . . , wm),fis associativecommutative, for each i∈ {1, . . . , n}there is j∈ {1, . . . , m}s.t. vi=Ewj,u6=Ev, depth(p) = depth(p0)+1, and ∀1≤i≤ depth(p0),root(t|p|i) = root(t0|p0|i); or 3. u=f(u1, . . . , un),t|p=f(w1, . . . , wm),t0|p0=v,fis associativecommutative, for each j∈ {1, . . . , m}there is i∈ {1, . . . , n}s.t. wj=Evi,u6=Ev, depth(p0) = depth(p)+1, and ∀1≤i≤depth(p), root(t|p|i) = root(t0|p0|i). The following lemma states the appropriate connection between the constraints in a derivation and the associative-commutative conflict pairs of the initial configuration. Lemma 3.5.7 Given flattened terms tand t0such that every symbol in tand t0is free or associative-commutative, and a fresh variable x, htx ,t0| ∅ | id i →∗hCT |uy ,v∧S|θiusing the inference rules of Figures 3.1 and 3.5 iff (u, v)is an associative-commutative conflict pair of tand t0.
44 Chapter 3. LGG modulo E Proof. Similar to the proof of Lemma 3.4.8 but using Lemma 3.5.5 instead of Lemma 3.4.6 and Definition 3.5.6 instead of Definition 3.4.7. The following lemma establishes the link between the computed substitution and a proper generalization term. Lemma 3.5.8 Given flattened terms tand t0such that every symbol in t and t0is free or associative-commutative, and a fresh variable x,htx ,t0| ∅ | id i →∗hC|S|θiusing the inference rules of Figures 3.1 and 3.5 iff xθ is a generalization of tand t0modulo associativity-commutativity. Proof. Similar to the proof of Lemma 3.4.9 but using Lemma 3.5.7 instead of Lemma 3.4.8 and Definition 3.5.6 instead of Definition 3.4.7. Finally, correctness and completeness are proved as follows. Theorem 3.5.9 (Correctness and Completeness) Given an equational theory (Σ, E), flattened Σ-terms tand t0such that every symbol in tand t0is free or associative-commutative, and a fresh variable x, then u∈genE(t, t0)iff there is u0in {xθ | htx ,t0| ∅ | idi →∗h∅ | S|θi} using the inference rules of Figures 3.1 and 3.5 such that u≃Eu0. Proof. Similar to Theorem 3.3.9. Recall that the inference rules of Figures 3.1 and 3.5 together are not confluent, so that different final configurations h∅ | S1|θ1i,...,h∅ | Sn| θnicorrespond to different generalizations xθ1, . . . , xθn. 3.6 Least general generalization modulo U Finally, let us introduce the inference rule of Figure 3.7 for handling function symbols fwhich have an identity element e. This rule considers the identity axioms in a rather lazy or on-demand manner to avoid infinite generation of all the elements in the equivalence class. The rule corresponds to the case when the root symbol fof the term tin the left–hand
3.6. Least general generalization modulo U45 ExpandU root(t)≡f∧Uf∈ax(f)∧root(t0)6=f∧t00 ∈ {f(e, t0), f(t0, e)} htx ,t0∧CT |S|θi→htx ,t00 ∧CT |S|θi Figure 3.7: Inference rule for expanding function symbol fwith identity element e side of the constraint tx ,shas eas an identity element. A companion rule for handling the case when the root symbol fof the term t0in the right–hand side has eas an identity element is omitted, since that is entirely similar. Example 3.6.1 Let t=f(a, b, c, d) and s=f(a, c) be two terms where ax(f) = {Af, Cf, Uf}. By applying the rules SolveE, RecoverE,DecomposeAC , and ExpandUabove, we end in a terminal configuration h∅ | S|θi, where θ={x7→ f(a, f(c, f(x5, x6))), x17→ a, x27→ f(c, f(x5, x6)), x37→ c, x47→ f(x5, x6)}, thus we compute that the lgg modulo ACU of tand sis f(a, c, x5, x6). The computation trace is shown in Figure 3.8. Note that in the example above there is a unique lgg modulo U, although this is not true for some generalization problems as witnessed by the following example. Example 3.6.2 Let t=f(f(a, a), f(b, a)) and t0=f(f(b, b), a) be two terms such that {Af, Uf} ⊆ ax(f). We end in two terminal configurations h∅ | S1|θ1i and h∅ | S2|θ2i, where θ1={x7→ f(f(x, x), f(y, a))}and θ2={x7→ f(y, f(b, a))}. Both are more general terms. Termination is slightly more difficult when there are symbols with identities. Theorem 3.6.3 (Termination) Given an equational theory (Σ, E),Σ- terms tand t0such that every symbol in tand t0is free or with identity element e, and a fresh variable x, every derivation stemming from an
46 Chapter 3. LGG modulo E lggE(f(a, b, c, d), f(a, c)), with E ={Cf, Af, Uf} ↓Initial Configuration hf(a, b, c, d) x ,f(a, c)|∅|idi ↓DecomposeAC (Other permutations are not shown) ha x1 ,a∧f(b, c, d) x2 ,c| ∅ | {x7→ f(x1, x2)}i ↓Decompose hf(b, c, d) x2 ,c|∅|{x7→ f(a, x2), x17→ a}i ↓ExpandU hf(b, c, d) x2 ,f(c, e)|∅|{x7→ f(a, x2), x17→ a}i ↓DecomposeAC (Other permutations are not shown) hc x3 ,c∧f(b, d) x4 ,e|∅|{x7→ f(a, f(x3, x4)), x17→ a, x27→ f(x3, x4)}i ↓Decompose hf(b, d) x4 ,e| ∅ | {x7→ f(a, f(c, x4)), x17→ a, x27→ f(c, x4), x37→ c}i ↓ExpandU hf(b, d) x4 ,f(e, e)|∅|{x7→ f(a, f(c, x4)), x17→ a, x27→ f(c, x4), x37→ c}i ↓DecomposeAC (Other permutations are not shown) hb x5 ,e∧d x6 ,e|∅|{x7→f(a, f(c, f(x5, x6))), x17→a, x27→f(c, f(x5, x6)), x37→c, x47→f(x5, x6)}i ↓Solve hd x6 ,e|b x5 ,e| {x7→ f(a, f(c, f(x5, x6))), x17→ a, x27→ f(c, f(x5, x6)), x37→c, x47→f(x5, x6)}i ↓Solve h∅|b x5 ,e∧d x6 ,e|{x7→f(a, f(c, f(x5, x6))), x17→a, x27→f(c, f(x5, x6)), x37→c, x47→f(x5, x6)}i ↓maximal<ACU {x7→ f(a, f(c, f(x5, x6))), x17→ a, x27→ f(c, f(x5, x6)), x37→ c, x47→ f(x5, x6)} Figure 3.8: Computation trace for the ACU–generalization of terms f(a, b, c, d) and f(a, c). initial configuration htx ,t0|∅|idiusing the inference rules of Figures 3.1 and 3.7 terminates with a configuration h∅ | S|θi. Proof. Let |u|be the number of symbol occurrences in the syntactic object u. Let kbe the minimum of |t|and |t0|.kis an upper bound to the number of times that the inference rule DecomposeEof Figure 3.1 can be applied. Let kbe the maximum of |t|and |t0|. Since the inference rule ExpandUadds a symbol fwith an identity to one side of a constraint only when the other side already has such a symbol, k−kis an upper bound to the number of times that the inference rule ExpandUfollowed by a decomposing rule of Figure 3.1 (or Figures 3.2,3.3, and 3.5) can be applied. Finally, the application of rules SolveEand RecoverEstrictly decreases the size |CT|of the CT component of the lgg configurations
3.6. Least general generalization modulo U47 at each step, hence the derivation terminates. In order to prove correctness and completeness, we introduce the auxiliary concepts of an identity conflict pair, similarly to Definitions 1.2.3, 3.3.6,3.4.7, and 3.5.6, plus some auxiliary results. First, we prove an auxiliary result stating that only (independently) fresh variables yappear in the index positions of the constraints in CT and Scomponents of lgg configurations. Lemma 3.6.4 (Uniqueness of Generalization Variables) Lemma 1.1.2 holds for tx ,t0when the symbols in t,t0are free or with identity element e, for the inference rules of Figures 3.1 and 3.7. The lemma below states that the range of the substitutions partially computed at any stage of a generalization derivation coincides with the set of the index variables of the configuration. Lemma 3.6.5 Given terms tand t0such that every symbol in tand t0is free or with identity element e, and a fresh variable xsuch that htx ,t0| ∅ | id i →∗hCT |S|θiusing the inference rules of Figures 3.1 and 3.7, then Index(S∪CT)⊆Ran(θ), and Ran(θ) = Var(xθ). Proof. Immediate by construction. The following lemma establishes an auxiliary property that is useful for defining the notion of an identity conflict pair of terms. Lemma 3.6.6 Given terms tand t0such that every symbol in tand t0 is free or with identity element e, and a fresh variable x, then htx ,t0|∅| id i →∗huy ,v∧CT |S|θiusing the inference rules of Figures 3.1 and 3.7 iff there exist a position p∈Pos(t)and a position p0∈Pos(t0)such that either: 1. t|p=u,t0|p0=v,∀1≤i≤min(depth(p),depth(p0)),root(t|p|i) = root(t0|p0|i), and
48 Chapter 3. LGG modulo E •if p≥p0, then ∀i∈ {depth(p0),...,depth(p)},root(t|p|i) = f s.t. fhas identity element e, or •if p0> p, then ∀i∈ {depth(p),...,depth(p0)},root(t0|p0|i) = f s.t. fhas identity element e; or 2. t|p=u,v=e,p0=p|depth(p)−1,root(t|p0) = fs.t. fhas identity element e, and ∀1≤i≤depth(p0),root(t|p|i) = root(t0|p0|i); or 3. u=e,t0|p0=v,p=p0|depth(p0)−1,root(t0|p) = fs.t. fhas identity element e, and ∀1≤i≤depth(p),root(t|p|i) = root(t0|p0|i). Proof. Straightforward by successive application of the inference rule Decompose of Figure 1.1 and the inference rule DecomposeUof Figure 3.7. Definition 3.6.7 (Identity Conflict Pair) Given terms tand t0such that every symbol in tand t0is free or with identity element e, the pair (u, v)is called an identity conflict pair of tand t0iff there exist at least one position p∈Pos(t)and at least one position p0∈Pos(t0)such that either: 1. t|p=u,t0|p0=v,u6=Ev,∀1≤i≤min(depth(p),depth(p0)), root(t|p|i) = root(t0|p0|i), and •if p≥p0, then ∀i∈ {depth(p0),...,depth(p)},root(t|p|i) = f s.t. fhas identity element e, or •if p0> p, then ∀i∈ {depth(p),...,depth(p0)},root(t0|p0|i) = f s.t. fhas identity element e; or 2. t|p=u,v=e,u6=Ee,p0=p|depth(p)−1,root(t|p0) = fs.t. fhas identity element e, and ∀1≤i≤depth(p0),root(t|p|i) = root(t0|p0|i); or 3. u=e,t0|p0=v,v6=Ee,p=p0|depth(p0)−1,root(t0|p) = fs.t. fhas identity element e, and ∀1≤i≤depth(p),root(t|p|i) = root(t0|p0|i). The following lemma states the appropriate connection between the constraints in a derivation and the identity conflict pairs of the initial configuration.
3.6. Least general generalization modulo U49 Lemma 3.6.8 Given terms tand t0such that every symbol in tand t0is free or has an identity element, and a fresh variable x,htx ,t0|∅|id i →∗ hCT |uy ,v∧S|θiusing the inference rules of Figures 3.1 and 3.7 iff (u, v)is an identity conflict pair of tand t0. Proof. Similar to the proof of Lemma 3.4.8 but using Lemma 3.6.6 instead of Lemma 3.4.6 and Definition 3.6.7 instead of Definition 3.4.7. The following lemma establishes the link between the computed substitution and a proper generalization term. Lemma 3.6.9 Given terms tand t0such that every symbol in tand t0is free or has an identity element, and a fresh variable x,htx ,t0|∅|id i →∗ hC|S|θi, using the inference rules of Figures 3.1 and 3.7 iff xθ is a generalization of tand t0modulo identity. Proof. Similar to the proof of Lemma 3.4.9 but using Lemma 3.6.8 instead of Lemma 3.4.8 and Definition 3.6.7 instead of Definition 3.4.7. Finally, correctness and completeness are proved as follows. Theorem 3.6.10 (Correctness and Completeness) Given an equational theory (Σ, E),Σ-terms tand t0such that every symbol in t and t0is free or has an identity element, and a fresh variable x, then u∈genE(t, t0)iff there is u0in {xθ | htx ,t0| ∅ | idi →∗h∅ | S|θi} using the inference rules of Figures 3.1 and 3.7 such that u≃Eu0. Proof. Similar to Theorem 3.3.9. Recall that the inference rules of Figures 3.1 and Figure 3.7 together are not confluent, hence different final configurations h∅ | S1|θ1i,...,h∅ | Sn|θnicorrespond to different generalizations xθ1, . . . , xθn. Note that if the symbol fhas an identity element eand is commutative or associativecommutative, then it is not necessary to consider both forms f(t0, e) and f(e, t0) in Figure 3.7.
56 Chapter 4. Order-Sorted LGG modulo E Now, we prove that sort-specialized E-generalizations are the same as order-sorted E-lggs. Theorem 4.2.5 Given a kind-completed, pre-regular, order-sorted equational theory (Σ, E)with the same equational attributes for overloaded symbols, and flattened Σ-terms tand t0such that [LS(t)] = [LS(t0)], tsgE(t, t0)is a order-sorted equational generalization of tand t0, and lggE(t, t0)provides a minimal complete set of order-sorted equational lggs. Proof. Similar to the proof of Theorem 2.2.5. Finally, we prove the correctness and completeness of the ordersorted, equational generalization algorithm. Theorem 4.2.6 (Correctness and Completeness) Given a kindcompleted, pre-regular, order-sorted equational theory (Σ, E)with the same equational attributes for overloaded symbols, flattened Σ-terms tand t0such that [s] = [LS(t)] = [LS(t0)], and a fresh variable x:[s],u∈lggE(t, t0)is an order-sorted equational lgg of tand t0iff ht x:[s] ,t0| ∅ | idi →∗h∅ | S|θi using the inference rules of Figures 4.1,4.2,4.3,4.4, and 4.5 for some Sand θand u≃E(x:[s])θ. Proof. Similar to the proof of Theorem 2.3.4.
5 The ACUOS System This chapter presents ACUOS, an optimized implementation of the ordersorted modular ACU least general generalization algorithm that we formalized in Chapter 4. The tool consists of about a thousand lines of code written in the high-performance Maude equational programming language (Meseguer,1992), taking advantage of its powerful reflective capabilities to express inference rules and encode terms. The ACUOS system integrates three different front-ends: (i) a Maude meta-level function, which provides direct access to the complete backend functionality; (ii) a Full Maude user level command, which liberates the user from ancillary low-level technicalities; and (iii) a Web interface that enables the use of the tool directly from a Web browser. Both the application source code and the ACUOS Web interface are publicly available at http://safe-tools.dsic.upv.es/acuos. 5.1 Algorithmic improvements In order to optimize the tool performance, we have introduced a number of significant improvements w.r.t. the original algorithm of (Alpuente et al.,2012). First, the inference rules have been carefully encoded as a memoization-based, recursive procedure that does not carry any configuration store and avoids unnecessary re-computations. We also identified the inference rules that are confluent and encoded them as Maude equations (instead of rules), highly reducing the search space as well as the memory usage due to the different treatment of rules and equations in Maude (Clavel et al.,2007). Finally, unlike the original algorithm, the sort information is not incrementally computed but is more efficiently
58 Chapter 5. The ACUOS System introduced into the solution by a suitable and inexpensive sorting postprocessing. 5.2 Architecture The tool takes as input two input structures (terms) and a Maude module that specifies the signature for the function symbols involved. The architecture of ACUOS, which is depicted in Figure 5.1, comprises the following modules: (i) Pre-processing. The input Maude specification and the input terms are lifted to the meta-level. Using their meta-level representation, an initial configuration for the generalization procedure is built that is lifted to the meta-level again so that Maude’s metalevel search operators can be used to find all ACU generalizers. (ii) Search for candidates. Maude’s metaSearch function is used to compute the final configurations of the recursive generalization procedure. (iii) Order-sorting addition. Unresolved conflict pairs are substituted by fresh variables. If there is more than one possible sort for a given variable, each of them gives rise to a different candidate. (iv) Normalization. A deterministic variable renaming algorithm that reduces term equality modulo renaming to syntactic equality is applied. As a result, a complete and finite (although not minimal) set of valid generalizers is delivered. (v) Minimization post-processing. The candidate set is minimized according to the instantiation ordering, producing the set of least general, order-sorted ACU generalizers of the input terms.
5.3. Interface 59 Figure 5.1: Architecture of the ACUOS system 5.3 Interface ACUOS comes with an intuitive Web user interface based on the AJAX technology, which allows the tool to be used through a Java Web application (see Appendix Afor details). The back-end of the tool has been implemented as a Maude meta-level function named metaGeneralize, which computes the set of generalizers of two terms in a given signature. For convenience, we also provide a Full Maude (Clavel et al.,2007) extension that offers a user-level command (get lggs), allowing the user to harness the full power of the tool while being liberated from ancillary meta-level technicalities. For instance, consider the following Full Maude1module: (mod fACU-OS is sorts E A B C D Empty . subsort Empty < A B . subsort A < C D . subsort B < C D . subsort C < E . subsort D < E . op a : -> A . op b : -> B . op c : -> C . op d : -> D . op e : -> Empty . op f : Empty Empty -> Empty [assoc comm id: e] . 1We refer the reader to (Clavel et al.,2007) for Maude and Full Maude syntax.
60 Chapter 5. The ACUOS System op f : A A -> A [assoc comm id: e] . op f : B B -> B [assoc comm id: e] . op f : E E -> E [assoc comm id: e] . endm) This module is automatically extended to its kind-complete version by Maude. It defines five constants a,b,c,d,eand four binary symbols sharing the same name fbut with different signatures corresponding to the subsort structure of Figure 2.3. All four versions of symbol f (plus its kind extension f: [E]→[E]) are associative-commutative and with identity symbol the constant e. Now, we can type the following generalization problem in Full Maude obtaining the six possible ordersorted E-lggs. (get lggs in fACU-OS : f(b,b,a) =? f(a,a,b) .) Lgg 1 f(X1:C,b,a) Lgg 2 f(X1:D,b,a) Lgg 3 f(X1:C,X1:C,X3:C) Lgg 4 f(X1:C,X1:C,X3:D) Lgg 5 f(X1:D,X1:D,X3:C) Lgg 6 f(X1:D,X1:D,X3:D) No more lgg. 5.4 Generalizing data structures As mentioned above, the generalization problem for two terms t1and t2consists in finding their least general generalization (lgg), i.e., the least general term tsuch that both t1and t2are instances of tunder appropriate substitutions. For instance, the term siblings(X,Y)
5.4. Generalizing data structures 61 is a generalization of siblings(john,sam) and siblings(tom,sam), but their least general generalizer is siblings(X,sam). We also recall that generalizer terms are not always linear: the generalization of siblings(john,john,sam) and siblings(tom,tom,sam) is siblings(X,X,sam), not siblings(X,Y,sam). While ordinary, syntactic generalization is useful for some applications, it suffers two important limitations. First, it cannot generalize common data structures such as records, lists, sets, and multisets. For instance, given a record structure r(field1:v1, field2:v2, . . . , fieldn:vn), we want to recognize the tuple (field1:v1, ..., fieldn:vn) of the record fields irrespectively of the ordering of the elements; or more formally, two record structures are equal if they are equal modulo the commutativity and associativity of the tuple constructor (,). The problem is similar for lists (concatenation is associative with unity nil), multisets (insertion is associative-commutative with unity ∅), or sets (insertion is associativecommutative-idempotent with unity ∅). Let us introduce the constants john,sam,peter,tom, and mary and consider a relation symbol siblings whose only argument is a tuple of such constants that we build by using the tuple constructor (,) as explained above. If we consider the symbol (,) to be commutative (C), the terms siblings(john,sam) and siblings(sam,tom) can be generalized modulo commutativity as siblings(X,sam); if the symbol (,) is associative2(A), the terms siblings(tom,sam,john) and siblings(john,sam,mary,peter) can be generalized as siblings(X,sam,Y); if the symbol (,) is both associative and commutative (AC), then the lgg is siblings(john,sam,Y); and if we additionally endow the symbol (,) with an identity element (ACU), so that two structures can be paired at any size by just introducing as many unity elements as needed into the structures, then the corresponding lgg would be siblings(john,sam,X,Y). The second problem with ordinary generalization is that it does not cope with types and subtypes, which can further increase the number of solutions. For instance, assume that the constants john,sam,peter, and tom belong to type Male and that mary,joan belong to type Female. Also assume that Male, Female are subtypes of the type Person that 2When we work modulo associativity, the term f(a,b,c) is a shorthand for f(f(a,b),c).
62 Chapter 5. The ACUOS System is used for the elements of the relation siblings. Then, for the case where (,) is AC, the terms siblings(john,sam,mary,peter,chris) and siblings(tom,sam,john,joan) have two least general generalizers instead of one: siblings(john,sam,X:Male,Y:People) and siblings(john,sam,X:Female,Y:People). As has been shown, order-sorted modular ACU least general generalization provides a solution for these two classical limitations of generalization while remaining decidable3. In consequence, the extension to generalization presented in this thesis offers a significant increase in the ability to reason about common data structures in a natural way, thus opening up new applications for typed equational reasoning systems and typed rule-based languages such as ASF+SDF, Elan, OBJ, Cafe-OBJ, and Maude, where some function symbols may be declared to obey given algebraic laws (the so-called equational attributes) of associativity and/or commutativity and/or identity. 5.5 Experiments We have tested our tool with several generalization problems that can be found at the ACUOS Web site and within the distributed package. This also includes the generalization of XML schemata, that can be naturally interpreted as terms built using associative-commutative operators with unbounded arity, which we modeled by using ACU constructor symbols in Maude as well. With regard to the time required to achieve the lggs, and considering the combinatorial complexity of the ACU generalization problem, our implementation is reasonably time efficient. For example, running the tool for the siblings example described in the previous section –with ACU rewrites– took less than 10−2second (480.000 rewrites per second on standard hardware, 2.26GHz Intel Core 2 Duo with 8Gb of RAM memory). The elapsed times for the Rutherford example described in Figure A.1 in Appendix Aare obviously higher but still cogent, due to the encoding of higher-order generalization as first order generalization that we used. 3As demonstrated by Theorem 4.1.1
5.5. Experiments 63 In order to facilitate the understanding, a typical generalization session is provided in Appendix A.
64 Chapter 5. The ACUOS System
6 Conclusions and future work Generalization is a formal reasoning component of many symbolic frameworks. Order-sorted modular ACU generalization extends ordinary generalization with subtypes and the capability to endow each function symbol with any combination of associativity, commutativity, and identity axioms, making it possible to reason about records, lists, sets and multisets of data elements, atoms or rules. In this thesis, we have presented an order-sorted, modular equational generalization algorithm that computes a minimal and complete set of least general generalizations for two terms modulo any combination of associativity, commutativity and identity axioms for the binary symbols in the theory. Our algorithm is directly applicable to any many-sorted and order-sorted declarative language and equational reasoning system (and also, a fortiori, to untyped languages and systems which have only one sort). As shown in the examples, the algorithms we propose are effective to compute E-generalizations, which would be unfeasible in a na¨ıve way. In our own work, we plan to use the proposed order-sorted equational generalization algorithm as a key component of a narrowing-based partial evaluator (PE) for programs in order-sorted rule-based languages such as OBJ, CafeOBJ, and Maude. This will make available for such languages useful narrowing–driven PE techniques developed for the untyped setting in, e.g., (Alpuente et al.,1998a,b,1999;Albert et al.,1999). We are also considering adding this generalization mechanism to an inductive theorem prover such a Maude’s ITP (Clavel and Palomino,2005) to support automatic conjecture of lemmas. This will provide a typed analogue of similar automatic lemma conjecture mechanisms in untyped inductive theorem provers such as Nqthm (Boyer and Moore,1980b) and its ACL2
72 Appendix A. A Generalization Session with ACUOS Figure A.2: The input form of the ACUOS generalization Web tool
Bibliography A¨ıt-Kaci, H. (1983). Outline of a calculus of type subsumption. Technical report, University of Pennsylvania. A¨ıt-Kaci, H. (2007). Data models as constraint systems: A key to the semantic web. Constraint Programming Letters, 1:33–88. A¨ıt-Kaci, H. and Sasaki, Y. (2001). An axiomatic approach to feature term generalization. In Raedt, L. D. and Flach, P. A., editors, Machine Learning: EMCL 2001, 12th European Conference on Machine Learning, Freiburg, Germany, September 5-7, 2001, Proceedings, volume 2167 of Lecture Notes in Computer Science, pages 1–12. Springer. Albert, E., Alpuente, M., Hanus, M., and Vidal, G. (1999). A partial evaluation framework for curry programs. In Ganzinger, H., McAllester, D. A., and Voronkov, A., editors, Logic Programming and Automated Reasoning, 6th International Conference, LPAR’99, Tbilisi, Georgia, September 6-10, 1999, Proceedings, volume 1705 of Lecture Notes in Computer Science, pages 376–395. Springer. Alpuente, M., Escobar, S., Espert, and J., Meseguer, J. (2012). A modular order-sorted equational generalization algorithm. Alpuente, M., Escobar, S., Meseguer, J., and Ojeda, P. (2009a). A Modular Equational Generalization Algorithm. In Proc. 18th Int’l Symp. on Logic-Based Program Synthesis and Transformation, LOPSTR 2008, Revised Selected Papers, volume 5438 of Lecture Notes in Computer Science, pages 24–39. Springer. Alpuente, M., Escobar, S., Meseguer, J., and Ojeda, P. (2009b). Order– Sorted Generalization. Electr. Notes Theor. Comput. Sci., 246:27–38. Alpuente, M., Falaschi, M., and Vidal, G. (1998a). Partial evaluation of functional logic programs. ACM Trans. Program. Lang. Syst., 20(4):768–844.
74 Bibliography Alpuente, M., Falaschi, M., and Vidal, G. (1998b). A unifying view of functional and logic program specialization. ACM Comput. Surv., 30(3es):9. Alpuente, M., Hanus, M., Lucas, S., and Vidal, G. (1999). Specialization of Inductively Sequential Functional Logic Programs. In Proc. 4th ACM SIGPLAN International Conference on Functional Programming (ICFP ’99), Paris, France, September 27-29, 1999, pages 273–283. Baader, F. (1991). Unification, weak unification, upper bound, lower bound, and generalization problems. In Book, R. V., editor, Rewriting Techniques and Applications, 4th International Conference, RTA-91, Como, Italy, April 10-12, 1991, Proceedings, volume 488 of Lecture Notes in Computer Science, pages 86–97. Springer. Baader, F. and Snyder, W. (1999). Unification theory. In Handbook of Automated Reasoning. Elsevier. Belli, F. and Jack, O. (1998). Declarative paradigm of test coverage. Softw. Test., Verif. Reliab., 8(1):15–47. Bergstra, J., Heering, J., and Klint, P. (1989). Algebraic Specification. ACM Press. Borovansk´y, P., Kirchner, C., Kirchner, H., and Moreau, P.-E. (2002). ELAN from a rewriting logic point of view. Theoretical Computer Science, 285:155–185. Boyer, R. and Moore, J. (1980a). A Computational Logic. Academic Press. Boyer, R. and Moore, J. (1980b). A Computational Logic. In (Boyer and Moore,1980a). Bulychev, P. E., Kostylev, E. V., and Zakharov, V. A. (2010). Antiunification algorithms and their applications in program analysis. Burghardt, J. (2005). E-generalization using Grammars. Artif. Intell., 165(1):1–35.
Bibliography 75 Clavel, M., Dur´an, F., Eker, S., Lincoln, P., Mart´ı-Oliet, N., Meseguer, J., and Talcott, C. (2007). All About Maude - A High-Performance Logical Framework. Springer-Verlag New York, Inc., Secaucus, NJ, USA. Clavel, M. and Palomino, M. (2005). The ITP tool’s manual. Universidad Complutense, Madrid, http://maude.sip.ucm.es/itp/. Diaconescu, R. and Futatsugi, K. (1998). CafeOBJ Report, volume 6 of AMAST Series in Computing. World Scientific, AMAST Series. Eker, S. (2003). Associative-commutative rewriting on large terms. In Nieuwenhuis, R., editor, Rewriting Techniques and Applications, 14th International Conference, RTA 2003, Valencia, Spain, June 9-11, 2003, Proceedings, volume 2706 of Lecture Notes in Computer Science, pages 14–29. Springer. Frisch, A. M. and Jr., C. D. P. (1990). Generalization with taxonomic information. In AAAI, pages 755–761. Gallagher, J. P. (1993). Tutorial on specialisation of logic programs. In PEPM ’93: Proceedings of the 1993 ACM SIGPLAN symposium on Partial evaluation and semantics-based program manipulation, pages 88–98, New York, NY, USA. ACM. Goguen, J. and Meseguer, J. (1992). Order-sorted algebra I: Equational deduction for multiple inheritance, overloading, exceptions and partial operations. Theoretical Computer Science, 105:217–273. Goguen, J., Winkler, T., Meseguer, J., Futatsugi, K., and Jouannaud, J.-P. (2000). Introducing OBJ. In Software Engineering with OBJ: Algebraic Specification in Action, pages 3–167. Kluwer. Huet, G. (1976). Resolution d’Equations dans des Langages d’Order 1, 2,.. . ,ω. PhD thesis, Univ. Paris VII. Hullot, J.-M. (1980). Canonical Forms and Unification. In 5th Int’l Conference on Automated Deduction CADE’80, volume 87 of LNCS, pages 318–334, Berlin. Springer-Verlag.
76 Bibliography Jouannaud, J.-P. and Kirchner, C. (1991). Solving Equations in Abstract Algebras: A Rule-Based Survey of Unification. In Computational Logic - Essays in Honor of Alan Robinson, pages 257–321. MIT Press. Kaufmann, M., Manolios, P., and Moore, J. (2000). Computer-Aided Reasoning: An Approach. Kluwer. Kitzelmann, E. and Schmid, U. (2006). Inductive synthesis of functional programs: An explanation based generalization approach. Journal of Machine Learning Research, 7:429–454. Kutsia, T., Levy, J., and Villaret, M. (2011). Anti-unification for unranked terms and hedges. In Schmidt-Schauß, M., editor, Proceedings of the 22nd International Conference on Rewriting Techniques and Applications, RTA 2011, May 30 - June 1, 2011, Novi Sad, Serbia, volume 10 of LIPIcs, pages 219–234. Schloss Dagstuhl - Leibniz-Zentrum fuer Informatik. Lassez, J.-L., Maher, M. J., and Marriott, K. (1988). Unification Revisited. In Minker, J., editor, Foundations of Deductive Databases and Logic Programming, pages 587–625. Morgan Kaufmann, Los Altos, Ca. Lu, J., Mylopoulos, J., Harao, M., and Hagiya, M. (2000). Higher order generalization and its application in program verification. Ann. Math. Artif. Intell., 28(1-4):107–126. Martelli, A. and Montanari, U. (1982). An efficient unification algorithm. Transactions on Programming Languages and Systems, 4(2):258– 282. Meseguer, J. (1992). Conditioned rewriting logic as a united model of concurrency. Theor. Comput. Sci., 96(1):73–155. Meseguer, J. (1997). Membership algebra as a logical framework for equational specification. In Proc. WADT’97, pages 18–61. Meseguer, J. (1998). Membership algebra as a logical framework for equational specification. In Parisi-Presicce, F., editor, Proc. WADT’97, pages 18–61. Springer LNCS 1376.
Bibliography 77 Meseguer, J., Goguen, J., and Smolka, G. (1989). Order-sorted unification. J. Symbolic Computation, 8:383–413. Mogensen, T. Æ. (2000). Glossary for partial evaluation and related topics. Higher-Order and Symbolic Computation, 13(4). Muggleton, S. (1999). Inductive Logic Programming: Issues, Results and the Challenge of Learning Language in Logic. Artif. Intell., 114(1- 2):283–296. Østvold, B. (2004). A functional reconstruction of anti-unification. Technical Report DART/04/04, Norwegian Computing Center. Available at http://publications.nr.no/nr-notat-dart-04-04.pdf. Pfenning, F. (1991). Unification and anti-unification in the calculus of constructions. In Proceedings, Sixth Annual IEEE Symposium on Logic in Computer Science, 15-18 July, 1991, Amsterdam, The Netherlands, pages 74–85. IEEE Computer Society. Plaza, E. (1995). Cases as terms: A feature term approach to the structured representation of cases. In Veloso, M. M. and Aamodt, A., editors, Case-Based Reasoning Research and Development, First International Conference, ICCBR-95, Sesimbra, Portugal, October 23-26, 1995, Proceedings, volume 1010 of Lecture Notes in Computer Science, pages 265–276. Springer. Plotkin, G. (1970). A note on inductive generalization. In Machine Intelligence, volume 5, pages 153–163. Edinburgh University Press. Plotkin, G. (2004). A structural approach to operational semantics. J. Log. Algebr. Program., 60-61:17–139. Popplestone, R. (1969). An experiment in automatic induction. In Machine Intelligence, volume 5, pages 203–215. Edinburgh University Press. Pottier, L. (1989). Generalisation de termes en theorie equationelle: Cas associatif-commutatif. Technical Report INRIA 1056, Norwegian Computing Center.
78 Bibliography Reynolds, J. (1970). Transformational systems and the algebraic structure of atomic formulas. Machine Intelligence, 5:135–151. Schmidt-Schauss, M. (1986). Unification in many-sorted equational theories. In Proceedings, 8th International Conference on Automated Deduction, pages 538–552. Springer-Verlag. LNCS, Volume 230. Siekmann, J. (1989). Unification Theory. Journal of Symbolic Computation, 7:207–274. Smolka, G. and A¨ıt-Kaci, H. (1989). Inheritance hierarchies: Semantics and unification. J. Symb. Comput., 7(3/4):343–370. Smolka, G., Nutt, W., Goguen, J., and Meseguer, J. (1989). Ordersorted equational computation. In Nivat, M. and A¨ıt-Kaci, H., editors, Resolution of Equations in Algebraic Structures, volume 2, pages 297– 367. Academic Press. TeReSe, editor (2003). Term Rewriting Systems. Cambridge University Press, Cambridge.