Finitary Matching For Constructor Based Theories Hui Shi, Burkhart Wol FB 3 Mathematik-Informatik, Universitat Bremen fshi,
[email protected]
Abstract
Acyclic Constructor Based (ACB ) theories are induced by a restricted class of equations, which can be treated as convergent rewrite systems. We claim that solving matching problems with such theories presents an interesting subclass of matching-modulo-E problems. In this paper we present a constructive matching algorithm for ACB theories, and show its termination and completeness. The comparison to the second-order matching and the rewriting modulo commutativity sheds some light on the power of ACB -matching. The easy combination of ACB matching problems is also an advantage of our approach.
1 Introduction \Extended uni cation" (see [3]) has been investigated for many theories. Well known positive results for associativity, commutativity [21], for boolean rings [14], or for convergent rewrite systems [10, 4] demonstrate the importance of extended uni cation in many application elds, like theorem proving, logic programming, etc. The congruence relations induced by equational theories can be interpreted as syntactical similarity that makes them also very attractive to be applied in the eld of program transformations [13]. In this paper we present a mathematical framework of a re ned \matching language" for program transformations that can cope with complex program schemata, such as \sequences of statements containing a special one", \contexts of programs", and so on. Namely, we put forward a restricted class of theories that de ne most useful syntactical similarities of programs. Additionally, they are easy to combine. This class of theories, closely related to recursive functional programs, such as in functional language ML, is called constructor based theories, which can be treated as convergent rewrite systems. A restriction of constructor based theories is given to get a class of acyclic constructor based theories, for which the matching problem is nitary. The concatenation function `++' on lists, which has two constructors `nil' (empty list) and `:' (taking an element and a list to make a new list), is an example of a recursive function that can be de ned by the following equations, which induce an acyclic constructor This work is partially supported by the Graduate Programme \Safe and Secure Software-Hardware Systems" at Universitat Bremen and as part of the project \Korrekte Software (KORSO)" by the German Ministry of Research and Technology (BMFT).
1
based theory.
nil ++ys = ys (x : xs) ++ys = x : (xs ++ys): With the help of `++' it is possible to express some program schemata, such as a sequence of statements that contains a special one, etc. A matching problem consists of nding a substitution , such that (x ++[c] ++y) = [a; c; b; ], here we use `[' and `]' to express lists shortly. An obstacle to nd such a solution is the fact that `++' is not a free symbol, but de ned by a set of equations. Solving the equation can be considered as an matching-modulo-E problem. Narrowing technique, which is widely employed as a universal procedure solving equations in theories ([5, 10], etc.), is also adopted in our paper with some variations. The contribution of this paper is not so much the result itself but to present a constructive matching algorithm for matching problems with respect to a special class of theories, which not only can be proved to be both terminating and complete (i.e., nitary), but also oers a possibility of ecient implementation (see [18]). The asymmetric nature of matching and the introduction of non-ground matching problems through narrowing lead to some special diculties in our work. Some interesting constructor based theories will be given to compare the matching problem with Huet and Lang's second-order matching [9] and the rewriting modulo commutativity ([16] and [11]). Without any change, the algorithm can be used to solve matching problems with respect to disjoin combinations of acyclic constructor based theories. The rest of this paper is organized as follows. Section 2 introduces the basic de nitions and notations that will be used in this paper. Section 3 de nes acyclic constructor based theories and gives a uniform matching algorithm to solve ACB matching problems at a fairly abstract level; its correctness is proved in section 4. Section 5 discusses the applications of ACB theories. We conclude in section 6.
2 Preliminaries In this section we brie y review the relevant basic notations, terminology that will be used in this paper. For surveys of this area, see [19] and [12]. A speci cation is given by (, E ), where is a signature and E a set of equations. can be presented as a triple hS ; C ; Fi, S introduces a set of sorts, C a set of constructors generating terms and F a set of de ned functions, or matching combinators (short: combinators), de ned by E . Every function symbol f has a type s1 sn ! s, where n S is its arity, s1; ; sn; s 2 S . V = s2S Vs is a set of variables. u, v, l and r range over Ts (V ) and T (V ), which denote terms of sort s and all terms over , respectively. A term is called linear if no variables in it occur more than once. The depth of a term is the length of the longest path in its tree presentation. A context is a term containing holes, denoted by C [21; ; 2n ]. If terms u1, , un are substituted in n holes simultaneously, the result is C [u1; ; un]. Let = hfListg; fnil; :g; f++gi, and E = f nil ++ys = ys, (x : xs) ++ys = x : (xs ++ys)g, then (, E ) is the speci cation of our example above. Note that `++' and `:' are used as in x operators. The set of variables in a term u is denoted by V (u). Substitutions are functions from V to T (V ), which are represented as nite sets of ordered pairs of the form = 2
f(x1; u1); ; (xn; un)g, where xi and ui have same sort (1 i n). The domain and range of are de ned as dom() = fx1; ; xng and ran() = V (u1) [ [ V (un). The composition of two substitutions and , denoted as , and the restriction of a substitution to a set of variables W , written as [W ], are de ned as usual. (x) = ( (x)); [W ] (x) = (x) if x 2 W , x otherwise. Without loss of generality we assume any substitution to be idempotent, i.e. dom() \ V (ran()) = .
In the sequel stands for signatures, s stands for sorts, f , g for matching combinators, u, v and t for arbitrary terms, a, b for ground terms, x, y, z for variables, c for constructors. , and are reserved for substitutions. To distinguish syntactic equality of terms from a number of other equalities, the former is often denoted by . The set of equations E in a speci cation induces an equational theory =E , which is the closure of E under the laws of equational logic. In this paper we consider only sets of equations that can be treated as rewrite systems by taking equations as rewrite rules from left to right. So if we say that E is terminating, it means that the associated rewrite system is terminating; u is a E -normal form if u is a normal form with respect to the rewrite system, and so on. An equation l = r is called variable-preserving if V (l) = V (r); it is variable-dropping if V (r) V (l). Let (, E ) be a speci cation, a matching pair is an ordered pair of terms (u; v) with the same sort. A matching problem ? is a set of matching pairs: f(u1; v1), , (un; vn)g, LHS(?) = fu1, , ung, RHS(?) = fv1, , vng and V (?) = V (u1) [ [ V (un). If the RHS of a matching problem is in T (), then it is called a ground matching problem. ? will be used to denote matching problems. As an abbreviation, we represent lists of terms using a \vector" notation, so that f (u1; ; um) will be represented as f (um ), and we shall even sometimes represent a matching problem as f(u; v)ng. Two substitutions and are E -equal, written as =E if, and only if, (x) =E (x) for all x; is an instance of , written as E if, and only if, there exists a such that =E . The set of E -matches of a matching problem ? over a speci cation (, E ) is de ned as: M(?) = f j 8(u; v) 2 ?: (u) =E v ^ dom() V (?)g: CSM(?) is said to be a complete set of minimal E -matches of a matching problem ?, if CSM(?) M(?); for any 2 M(?), there exists a 2 CSM(?) such that, E ; and if 1 and 2 are two matches in CSM(?), then 1 6E 2.
3 Matching Algorithm for Constructor Based Theories 3.1 Constructor based theories
The E in a speci cation can be any set of equations, such as associativity or commutativity, which are widely studied in uni cation and matching theory. It can also induce collapse-free or regular theories, which are rather interesting for combining uni cation problems, see [15]. We are interested in a special class of so called constructor based 3
theories, in their matching problems and in their combinations. This class of theories is a variant of a class of left-linear systems discussed in [4], whose semantic matching problems are decidable. At rst we represent two auxiliary de nitions (see [4, 2]).
De nition 1 (Suitable Property) A suitable property P is a measure associated with ground terms, along with a partial-ordering that compares values of P , such that P is strictly larger, under , for terms than for its subterms. De nition 2 (Non-Decreasing) Let P be a suitable property. A function symbol f is de ned to be non-decreasing (with respect to P ) if whenever f (s1; ; sn) =E t, where si and t are all ground E -normal forms, then P (si) P (t) does not hold. Any function which does not have this property is said to be a decreasing function (with respect to P ).
It is not possible to always decide whether a function de ned by a given theory is non-decreasing with respect to a property P , see [4]. For functions that are de ned only by variable-preserving equations, such as `++', this de nition is suitable. But for functions containing also variable-dropping equations, such as the function `len' de ned by the following equations, the de nition is not proper, since some parameters of such functions do not in uence the results. len(nil) ! 0 len(x : xs) ! s(len(xs)) `0' and `s' are constructors of natural numbers. `len' is decreasing with respect to depth, since len(s(s(0)) : nil) =E s(0), but depth(s(s(0))) > depth(s(0)). De nition 3 (Proper Non-Decreasing) Let P be a suitable property. A function symbol f is de ned to be proper non-decreasing (with respect to P ) if whenever (f (x1, ; xn)) =E a, where (xi) and a are all ground E -normal forms, and there exists no 0 < such that 0(f (x1, ; xn)) =E a, then P ((xi)) P (a) does not hold by assuming P (b) P (x) for any ground term b and variable x. Any function which does not have this property is said to be proper decreasing (with respect to P ). Then it can be shown that the function `len' is proper non-decreasing with respect to depth. Now we can de ne the classes of theories concerned in this paper.
De nition 4 (Constructor Based Theories) Let E be a set of equations, which is convergent by treating equations as rewrite rules (from left to right). We say that E induces a constructor based theory , if l = r 2 E , then l is linear and has the form f (un ), where f is a de ned function, each ui (1 i n) contains only variables and
constructors.
Well known results (e.g. [5, 10]) ensure that complete sets of matches can always be found for such kind of theories, but not necessarily nite. It is not dicult to see that f(x; s(0)); (y; 0)g, f(x; 0); (y; s(0))g are two possible matches of f(x + y; s(0))g. But the matching problem f(x ? y, s(0))g may have in nite matches: f(x; s(0)); (y; 0)g, f(x; s(s(0))), (y; s(0))g, , where `+' and `?' are de ned for natural numbers in the usual way: fx + 0 = x, x + s(y) = s(x + y)g, fx ? 0 = x, 0 ? y = 0, s(x) ? s(y) = x ? yg. In order to get a nitary algorithm, we restrict constructor based theories to a class of acyclic constructor based theories. 4
De nition 5 (Acyclic Constructor Based Theories) Let P be a suitable property. A constructor based theory E is called acyclic (short: ACB theory), if l = r 2 E satis es the following conditions: the top-level symbol of r is a variable or a constructor; for any other l0 = r0 in E , l and l0 are not uni able, i.e., E is non-overlopping; and there is no de ned function in r that is nested below any function proper decreasing with respect to P . The non-overlopping of E is necessary in the termination proof, because it ensures that the set of matches produced by our matching algorithm is minimal, which makes the use of the proper non-decreasing property possible, we will show it latter. Instead of using an existing narrowing strategy, such as basic narrowing or innermost narrowing, we introduce a new narrowing strategy { structural narrowing. It is particularly appropriate to solve the matching problems for constructor based theories and makes implementation easy and ecient.
De nition 6 (Structural Narrowing) A non-variable term u is structurally narrowable into u0 with respect to E , if there is a renaming l = r of an equation in E such that, u and l have no common variables and are uni able, u0 mgu(u; l)(r). It is denoted as u ;l=r u0. A term that is not structurally narrowable is called structurally normalized. For example, s(x + y) is not structurally narrowable with x + s(y) = s(x + y), but x ? y is structurally narrowable with s(x) ? s(y) = x ? y. Here is a useful fact. Fact If u is not structurally narrowable with l = r and is any substitution, then (u) is also not structurally narrowable with the equation.
To make the structural narrowing work, we should abstract a term into a context containing only one occurrence of a combinator. A combinator abstraction of a term u is de ned as C [v1; ; vk ], where C is a context containing one occurrence of a combinator, the top-level symbol of each vi (1 i k) is a combinator. C [x1; ; xk ] is denoted as [u], where each xi is a new variable. For example x ++(y ++z) can be abstracted as (x ++2) [y ++z] and [x ++(y ++z)] = x ++x1. We write t ; t0 if there is a structural narrowing derivation
t = t1; [t1] ;l1=r1 t2; [t2] ;l2=r2 t3; ; [tn?1] ;ln?1=rn?1 tn = t0: The rst condition in the de nition of ACB theories ensures the termination of any structural narrowing derivation. Proposition 1 Let E induce an acyclic constructor based theory, t be a term and [t] ;l=r t0, then [t0] is structurally normalized. The dierences between the class of rewrite systems in [4] and the set of equations inducing ACB theories are discussed in section 6. In the examples above, `++', `+' `and len' are de ned by acyclic theories, but `-' not. 5
3.2 A Matching Algorithm
The matching process will be given as a set of rules for transforming a ground matching problem into an explicit representation of its solution. A pair (x; u) in a matching problem ? is in solved form and x is called a solved variable, if x is a variable which occurs neither in u nor anywhere else in ?. A matching problem is in solved form if all its pairs are in solved form. We write l = r 2 E to denote a renaming l = r of an equation in E , whose variables do not occur in the other related terms. Let (, E ) be a speci cation. In the sequel, if we write f (un ), it means that each ui does not contain any combinator, otherwise it is presented in its abstraction form. And it is assumed that every term is automatically normalized with respect to E . At rst we give the following six transformation rules on matching problems, starting with a given ground matching problem ?, terminating with ?0, where ?0 is in solved form if ? has solutions. (Rem1) removes a trivial matching pair.
f(u; u)g [ ? =) ?:
(Rem2) removes a matching pair playing no role in solutions. f(u; x)g [ ? =) ?; where if u is not a variable, then x 62 V (u0) for any (u0; v0) 2 ?; otherwise x does not occur anywhere else in ?. (Var) nds a partial solution for a variable.
f(x; u)g [ ? =) f(x; u)g [ f(x; u)g(?); where x 2 V (?):
(Dec) breaks a matching pair down into simpler ones. f(c(un); c(vn))g [ ? =) f(u1; v1); ; (un; vn)g [ ?; where c 2 C [ F : (Abs) abstracts a matching pair. f(C [uk]; u)g [ ? =) f(C [xk]; u); (u; x)kg [ ?; where fxk g \ (V (u) [ V (?) [ V (uk)) = : (S-Narr) realizes structural narrowing of a pair. f(f (un); u)g [ ? =) [ f((r); u)g [ (?); where l = r 2 E , = mgu(f (un); l) and ran() \ (V (u) [ V (?)) = . As usual, =) denotes the re exive-transitive closure of =). (Rem2), (S-Narr) and
(Abs) make the transformation rules dierent from those of general rst-order uni cation [7]. Rule (S-Narr) uses structural narrowing technique to search possible solutions. Rule (Rem2) throws away matching pairs playing no role in nal solutions, which could be 6
produced by (Abs) and (S-Narr). The following is a transformation sequence that begins with the matching problem f(len((x + y) :: xs), s(s(0)))g.
f(len((x + y) :: xs); s(s(0)))g =)(Abs) f(len(z :: xs); s(s(0))); (x + y; z)g =)(S-Narr) f(z; x1); (xs; xs1); (s(len(xs1)); s(s(0))); (x + y; x1)g =)(Rem2) f(z; x1); (xs; xs1); (s(len(xs1)); s(s(0)))g =)(Rem2) f(xs; xs1); (s(len(xs1)); s(s(0)))g =)(Dec) f(xs; xs1); (len(xs1); s(0))g =)(S-Narr) f(xs; x2 :: xs2); (xs1; x2 :: xs2); (s(len(xs2)); s(0))g =)(Dec) f(xs; x2 :: xs2); (xs1; x2 :: xs2); (len(xs2); 0)g =)(S-Narr) f(xs; x2 :: nil); (xs1; x2 :: nil); (xs2; nil); (0; 0)g =)(Rem1) f(xs; x2 :: nil); (xs1; x2 :: nil); (xs2; nil) The match is f(xs; x2 :: nil)g. The equation used in the rst (S-Narr) transformation is (x : xs) ++ys) = x : (xs ++ys). In the rst application of (Rem2), x + y is not a variable and x1 does not occur in z, xs and s(len(xs1)); in the second application, z is a variable and x1 does not occur anywhere else. The transformation rules given above are rather simple and abstract but can be very inecient, because of the random selection of pairs to be applied, especially, the use of (S-Narr) and (Abs) may transform ground matching problems into non-ground problems.
f(x1 ++(x2 ++x3); [3; 2])g =)(Abs) f(x1 ++z; [3; 2]); (x2 ++x3; z)g (1) f(x1 ++Z; [3; 2])g =)(S-Narr) f(x1; x : xs); (z; y); (x : (xs ++y); [3; 2])g (2) The right hand sides of both (1) and (2) are not ground matching problems, they contain newly introduced variables z, y, x and xs. It is not reasonable to say that there exists a substitution , such that (x2 ++x3) = z with the variable z that occurs also in some other matching pair. Finally, solving (x2 ++x3; z) at rst without knowing z could be very inecient through many useless narrowing steps. Instead, the pairs whose right hand sides contain no variables that are dependent upon other pairs, like (x1++ z,[3; 2]), (x1; x : xs) and (x : (xs ++y), [3; 2]), should be considered rst.
De nition 7 (Dependence and Solvable Matching Pair) A matching pair (u; v) is called dependent on pair (u0; v0), if V (v) \ V (u0) 6= , denoted as (u; v) (u0; v0). A matching pair (u; v) 2 ? is solvable, if it is not in solved form and there is no (u0; v0) 2 ? such that (u; v) (u0; v0). For these reasons, we will give a new version of the matching algorithm. Let ? be a rep matching problem, the representation of ? is denoted as ? = ?0 & ?1 & ?2 such that, ?0 [ ?1 [ ?2 = ?, ?0 contains all solved pairs, ?1 all solvable pairs and ?2 the rest. If ? is a ground matching problem or a solved matching problem, then it will be represented as fg & ? & fg or ? & fg & fg respectively. With representations, rules (Var), (Abs) and (S-Narr) could be represented as: 7
(Var0) nds a partial solution for a variable. ?0 & (f(x; u)g [ ?1 ) & ?2 =) ( ?0) & ((?1) [ (?2;1)) & (?2;2); where = f(x; u)g, ?2;1 [ ?2;2 = ?2 . (Abs0) abstracts a solvable matching pair. ?0 & (f(C [uk]; u)g [ ?1) & ?2 =) ?0 & (f(C [xk]; u)g [ ?1) & (f(u; x)k g [ ?2); where fxk g \ (V (u) [ V (?) [ V (uk )) = . (S-Narr0) realizes structural narrowing of a solvable pair. ?0 & (f(f (un); u)g [ ?1) & ?2 =) ( ?0) & (f((r); u)g [ (?1) [ (?2;1)) & (?2;2); where ?2;1 [ ?2;2 = ?2 , l = r 2 E , = mgu(f (un ); l) and ran() \ (V (u) [ V (?)) = .
Let R and R0 denote the transformation relations using the original rules and the new ones respectively. Obviously, if ? rep = ?0 & ?1 & ?2 =)R0 ?00 & ?01 & ?02 rep = ?0 ;
then there exists a transformation: ? =)R ?0. In the following section we will discuss the correctness of R0 .
4 The Correctness of the Algorithm The correctness of the matching algorithm will be presented in two pieces: a proof of partial correctness (theorem 1) and a proof of termination (theorem 4). We always present the global results, which will be proved through a sequences of lemmata and theorems.
Theorem 1 The matching algorithm is sound and complete, i.e., 2 M(?) if, and only if, there exists a sequence of transformations ? =) 0 such that 0 [V (?)]. 2 The central point of the correctness proof lies in the fact that the algorithm always chooses a solvable pair to obtain some intermediate solutions, through the applications of (Var0) and (S-Narr0) some new solvable pairs may be produced, trying then to obtain some new solutions, until all pairs are in solved form. In the sequel, we sketches the proof of the theorem. It is already mentioned that a ground matching problem could be transformed to a non-ground problem by using (Abs0) and (S-Narr0), so we should extendrep the set of matchs 0 M into M to cover the case of non-ground matching problems. Let ? = ?0 & ?1 & ?2, 2 M0(?) means that there exist 1 2 M(?1) and 2 2 M(1(?2)), such that = 2 1 ?0. We have said, the algorithm begins with a ground matching problem ?repand terminates with arepmatching problem ?0 in solved form, if it is matchable. Hence, ? = fg & ? & fg and ?0 = ?0 & fg & f g. M0(?) = M(?) and M0(?0) = M(?0), since uni ers of a ground matching problem are also its matches. In our case, a transformation = ?0 ? rep = ?0 & ?1 & ?2 =) ?00 & ?01 & ?02 rep 8
is called sound, if 2 M0(?0) , then there exists 2 M0 (?) and = [V (?)]. A transformation is called complete, if 2 M0(?), then there exists 2 M0 (?0) and [V (?)]. A transformation is called correct, if it is both sound and complete. A transformation rule is correct, if each transformation issued by the rule is correct. At rst we give two lemmata that are the basis of our proofs.
Lemma 1 Let u be any term and l a linear term, i.e., every variable in l occurs only once, such that V (u) \ V (l) = . If = mgu(u; l), then for any two dierent variables x1 and x2 2 V (u), V ((x1)) \ V ((x2)) = . 2 Using structural induction over l, we can prove the lemma. As a consequence of the fact that each CB theory is left linear, the following lemma can be shown by the induction over transformations.
Lemma 2 Let ? be a ground matching problem and ? =) ?0. If (u1; v1), (u2; v2) 2 ?0, then V (v1) \ V (v2) = . 2 Theorem 2 ensures that the representations of matching problems is meaningful.
Theorem 2 Let ? be a ground matching problem and ? =) ?0 rep = ?0 & ?1 & ?2 , ?1 0 6= as long as ? is not in solved form, otherwise ? is unsolvable. 2 Since the following lemma can be proved by the induction over transformations and by using lemma 3. Lemma 3 Let ? be a ground matching problem and fg & ? & fg =) ?0 rep = ?0 & ?1 & ?2, then dependency relation is a well-founded partial ordering over ?2. 2 Without \left linear", this lemma does not always holds. The following is a counterexample. Let f and g be two matching problems. One of the equations associated with f is f (c(x); x) = a with a a constant. We have the matching problem f(f (g(y); y),a)g and the following transformations: fg & f(f (g(y); y); a)g & fg =)(Abs0) fg & f(f (y1; y); a)g & f(g(y); y1)g =)(S-Narr0) f(y1; c(x)); (y; x)g & f(a; a)g & f(g(x); c(x))g =)(Rem1) f(y1; c(x)); (y; x)g & fg & f(g(x); c(x))g; although the problem is not in solved form, there is no solvable pair. Following lemmata present the correctness of (Var0), (S-Narr0) and (Abs0), their proofs are omitted. The correctness of (Rem1), (Rem2) and (Dec) is obvious. Using these lemmata and theorem 2, theorem 1 can be proved.
Lemma 4 (Var) (Var0) is a correct transformation rule. Lemma 5 (Abs) (Abs0) is a correct transformation rule. Lemma 6 (S-Narr) (S-Narr0) is a correct transformation rule.
2 2 2
The following theorem 3 will be used to prove the termination of the matching algorithm, since it makes the use of the proper non-decreasing property possible. 9
Theorem 3 Let ? be a ground matching problem and fg & ? & fg =) & fg & fg, then is a minimal match of ?.
Proof. We present only an outline hier. The only two rules to be considered are (Var0) and (S-Narr0). Any application of the rest rules does not in uence the matches. Let (x; v) be a solvable matching pair in a matching problem, then the minimal match of it is f(x; v)g. That is to say, the (Var0) transformation produces a minimal partial solution. Let (f (un); v) be a solvable matching pair in a matching problem. l1 = r1 and l2 = r2 are two equations used to narrow it. If 1 = mgu(f (un); l1) and 2 = mgu(f (un); l2), then 1 6 2 [V (f (un))], since l1 and l2 are not uni able. 2
Theorem 4 (Termination) For any matching problem ? over an ACB theory, every transformation sequence terminates. 2 The central point of the proof is the de nition an ordering over matching problems. Let t be any term, jtj denotes the depth of de ned functions in t and is de ned as:
jaj = 0; a is a variable or a constant c is a constructor jc(un )j = max1in juij; jf (un )j = max1in juij + 1; f is a de ned function: Let P be a partial ordering on matching pairs such that (u; v) P (u0; v0) if, and only if, P (v) P (v0), or P (v) P (v0) and ju0j < juj, where < is the less than function on natural numbers. Obviously, P is a well-founded partial-ordering over matching pairs. rep
Now werepcan further extend the ordering to matching problems. Let ? = ?0 & ?1 & ?2 and ?0 = ?00 & ?01 & ?02 be two matching problems, ? ? ?0 if, and only if, ?1 ? ?01 if, and only if, ?01 ?1 or ?01 6= ?1 and for every (u0i; vi0) 2 ?01 and 62 ?, there exists (uj ; vj ) 2 ?1 and 62 ?01 such that (uj ; vj ) P (u0i; vi0). It is not dicult to see that ? is the multiset ordering of P on set of matching pairs, so it is a well-founded partial-ordering.
Theorem 5 (Finite Invertibility) Let f(u; a)g be a matching pair, where u is a term containing no de ned function nested below any proper decreasing function and a is a ground normal form, then any transformation sequence beginning with f(u; a)g terminates.
Proof: The interesting case is the one in which u has the form f (un ) and f is a de ned function. Let C [vk] be the combinator abstraction of u.
fg & f(u; a)g & fg =)(Abs0) fg & f(C [xk]; a)g & f(v; x)kg =)(S-Narr0) & (f((r); a)g [ (?2;1)) & (?2;2); where ?2;1 [ ?2;2 = f(v; x)k g, f (ln ) ! r is the rule applied, = mgu(C [xk]; f (ln)). We show, at rst, (u; a) P ((r); a). There are two cases to be considered. (r) has a variable at the top-level. Then only (Var0) can be used to it and transforms it to a solved pair. 10
(r) has a constructor at the top-level. Then after using (Dec), we get a set of pairs: f(r1; a1), , (rm; am)g with each ai a proper subterm of a, so ((r); a) P (ri; ai), 1 i m. Thus, by applying the inductive hypothesis we can assume that all matches for ((r); a) can be found in nite time. Let be one of these matches, we have:
((f (un)) =E a: Then we show that for any (vi; xi) 2 ?2;1, (u; a) P ((vi); (xi)). The following two cases should be considered:
Function f is decreasing. By assumption there is no de ned function below it, that is, the combinator abstraction of u is u itself. So ?2 = .
Function f is non-decreasing. So P (a) P ((xi)) or P (a) P ((xi)), 1 i k, because of the de nition of proper non-decreasing functions and the fact that is a minimal match such that ((f (un))) =E t. Additionally, j(vi)j < juj, since each li contains no occurrence of de ned function. Therefore, we have: (u; a) P ((vi); (xi)). 2 Combining these results, we obtain the following theorem for our matching algorithm.
Theorem 6 (Finitary) For any matching problem ? over an ACB theory, the matching algorithm terminates with a complete set of minimal matches of ?. 2 Corollary 1 Let ? be a ground matching problem, ? =)R ?0, ?0 is in solved form, then there exists a transformation sequence fg & ? & fg =)R0 ?00 & fg & fg, such that ?00 ?0 [V (?)]. 2
5 Applications One major advantage of our approach is the easy combination of ACB matching problems. The comparison to second-order matching and rewriting modulo commutativity through examples shows the power of ACB matching.
5.1 The Combination of Constructor Based Theories
One all-pervasive concern in computing science is compositionality: the ability of composing the solution to a problem from the solutions to its subproblems. Usually, the combinations of uni cation and matching problems are rather complex (see [17, 8, 15]). But the combination of matching problems with respect to ACB theories are rather easy.
De nition 8 (Disjoint Combination) Let (hS ; C ; F1i, E1) and (hS ; C ; F2i, E2) be two speci cations. Then the disjoint combination (hS ; C ; F1 F2i,E1E2) is obtained by renaming F1 and F2 such that they are disjoint, then taking the union of their de ned functions and equations.
11
It is known that convergent is a `modular' property for left linear rewrite systems (see [12]). That is to say, the disjoint combination of two constructor based theories are still a constructor based theory. It is also not dicult to show that the disjoint combination of two ACB theories is still an ACB theory. So without any changes, our matching algorithm can be applied to solve matching problems with respect to disjoint combinations of ACB theories.
5.2 Example: Contexts and Terms
Contexts are now de ned as `terms' containing one occurrence of a special symbol 2, denoted by C []. If t is substituted in 2, the result is C [t]; t is said to be a subterm of C [t]. The signature is given as follows. For the clarity, the types of operator symbols are also given in the signature. = hf T; C g; f v : T; c : T; ap : T T ! T; 2 : C; v : C; c : C; apc : C C ! C g; f ": C T ! T gi: Without loss of generality, we assume that there is only one variable and one constant in T and in C . The set of equations de ning `"' is 2"u = u; v " u = v; c " u = c; apc(v1; v2) " u = ap(v1 " u; v2 " u): It is not dicult to see that this set of equations induces an ACB theory. There are some matching problems associated with contexts, like \ nd a context C , such that if t is substituted in 2, then it becomes to u" and \ nd all contexts and subterms of a given term u". Solving this kind of matching problems is very important in program transformations. Using the matching algorithm, the matching problem f(x " v), ap(c; v)g has the following solutions f(x; apc(c; 2))g, f(x; apc(c; v))g. Comparing the matching combinator `"' with general function applications, we can nd that matching problems with `"' simulate some second-order matching problems. In the second-order case, the matching problem f(x(v); c(v))g has the solutions f(x; y:c(y))g and f(x; y:c(v))g.
5.3 Example: Commutative Matching Combinator
In our framework we can de ne a binary permutation function: Boolean pair ! pair as follows: (true; (x; y)) = (x; y); (false; (x; y)) = (y; x): With , the commutativity of functions can be easily simulated, such as the commutativity of the natural number operator . Instead of having rules (+(x; y); z) = +((x; z); (y; z)) (z; +(x; y)) = +((z; x); (z; y)); 12
we only need
((xb; (+(x; y); z))) = +(((xb; (x; z))); ((xb; (y; z)))):
If xb is true, it is the rst rule, otherwise the second one. Such kind of applications makes program transformations very abstract. A more interesting application of is to present rewriting modulo commutativity. If R is a set of rewriting rules: x+0 ! x x + (?x) ! 0 ?(?x) ! x ?(x + y) ! (?x) + (?y) In rewriting modulo commutativity, see Peterson and Stickel [16], Jouannaud and Kirchner [11], the system RC contains all rules u ! v, such that u $C (u0) and v = (v0), for some rule u0!v0 in R. Using we can extend R to R0: +((xb; (x; 0)) ! x +((xb; (x; ?x)) ! 0 ?(?x) ! x ?(x + y) ! (?x) + (?y) It is not dicult to see that R0 is equivalent to RC . It seems that for a quite large class of rewriting modulo a congruence can be simulated by matching combinators. We feel that this is another aspect in which ACB theories turn out to be practically useful and theoretically interesting.
6 Conclusion Matching problems associated with constructor based theories are a special class of matching-modulo-E problems, which are particularly interesting in functional programming and program transformations. We obtained a nitary matching algorithm through the restriction of constructor based theories to a class of acyclic constructor based theories. The comparison to the second-order matching and the rewriting modulo commutativity shows the power of ACB matchings. The easy combination of constructor based theories is also an advantage of our approach. The dierences between the class of rewrite systems given by Dershowitz, et al. in [4], and the set of equations inducing ACB theories are the following: the concept of non-decreasing is generalized to proper non-decreasing, it is suitable for both variable-preserving and variable-dropping equations; but we require that the left-hand side of each equation contains only one occurrence of de ned function, i.e., as its top-level symbol. Furthermore, the transformation system presented in this paper is more constructive than that in [4]. Particularly, we have introduced structural narrowing to solve equations during matching process, instead of using the general \Mutate" transformation as in [4]. (Mutate) f(f (t1; ; tn); t)g [ ? =) f(t1; l1); ; (tn; ln); (r; t)g [ ?; 13
where f (l1 ; ln) ! r is a renamed rule. It is not dicult to see that using structural narrowing is more ecient, because only rules whose left-hand sides are uni able with the term will be considered. The goal of our work is not only to get the theoretical result { a decidable semantic matching, but also its implementation and application. The central point of the matching algorithm is the introduction of structural narrowing, which is dierent from the existing narrowing strategies, such as basic narrowing [10], normal narrowing [5], innermost narrowing [6] and lazy narrowing [22]. In our case, structural narrowing helps to nd impossible top-level symbols of s in a matching pair (s; t) with t a ground term as early as possible and to avoid unnecessary narrowing steps. In addition, structural narrowing is easy to implement. We only compare here structural narrowing with basic narrowing that is widely studied. In a basic narrowing derivation, narrowing is never applied to a subterm introduced by a previous narrowing substitution. Because of its completeness for convergent term rewrite systems, only basic narrowing needs to be considered. In structural narrowing derivation, we just apply narrowing to a term as a whole, not to its subterms. It is obviously simpler than basic narrowing. In addition, we have proved that structural narrowing together with function abstraction is complete for constructor based theories. The other good point of constructor based theories is the possibility to have an ecient matching processes. As an example, let (u; v) be a matching pair, if the rst symbols of u and v are not matchable, there is no need to narrow any subterm of u. In addition, the dependent relation over matching pairs leads to ecient implementations. Furthermore, because of the asymmetrical property of matching, the correctness proof of the matching algorithm is quite dierent from that of the uni cation algorithm, especially, if non-ground matching problems can be introduced by narrowing (S-Narr) and abstraction (Abs). In [15], a combining matching algorithm for the regular case is presented. The main consideration of the regular case is that all matches returned from each algorithm are ground. So the constant abstraction rule, called (M), will not produce non-ground matching problems. Our approach gives a concrete treatment of the special problems associated with non-ground matching problems. We believe that our work could be generalized for more matching-modulo-E problems. There remain some other interesting directions to explore. For example, it is important to investigate the possibility for extension of our approach to higher-order constructor based theories, functions like `map' will be included in our framework. There are two reasons to extend ACB theories to the second-order case. On the one hand, it is known that the second-order matching is nitary (see Huet and Lang [9]); on the other hand, most of the important program transformation rules are covered by the secondorder case. One can expect that there exists a complete and terminating algorithm for extended second-order matching with ACB equational theories, of course under some additional restrictions. Finally, the number of matches of a given problem may be very large. Usually only a few of them, or even only one of them, are interesting in practice. It would be meaningful to consider context information or additional restrictions associated with given problems during the matching process. 14
References [1] Bachmair, L.: Canonical Equational Proofs. Ronald V. Book (eds), Birkhauser, 1991. [2] Dershowitz, N.: Termination of Rewriting. In: J. of Symbolic Computation, 3, 1987. [3] Dershowitz, N. and Jouannaud, J.-P.: Rewrite System. In: Handbook of Theoretical Computer Science, Elsevier, 1990. [4] Dershowitz, N., Mitra, S. and Sivakumar, G.: Decidable Matching for Convergent Systems. In: Proc. of 11th International Conference on Automated Deduction, LNCS 607, 1992. [5] Fay, M.: First Order Uni cation in an Equational Theory. In Proc. 4th Workshop on Automated Deduction (1979), 161-167. [6] Fribourg, L.: SLOG: A Logic Programming Language Interpreter Based on Clausal Superposition and Rewriting. In Proc. of Symposium on Logic Programming, (1985), 172-184. [7] Gallier, J. H., Snyder, W.: Complete Sets of Transformations for General EUni cation. LNCS 256 (1987), 256-273. [8] Herold, A.: Combination of Uni cation Algorithms. In Proc. 8th International Conference on Automated Deduction, LNCS 230, 450-469, (1986). [9] Huet, G. and Lang, B.: Proving and Applying Program Transformations Expressed with Second-Order Patterns. Acta Informatica 11, (1978). [10] Hullot, J.-M.: Canonical Forms and Uni cation. In Proc. 5th Workshop on Automated Deduction (1980), 318-334. [11] Jouannaud, J.-P. and Kirchner, H.: Completion of a Set of Rules Modulo a Set of Equations. In: SIAM Journal of Comput. 15, 1155-1194, (1986). [12] Klop, J. W.: Term Rewriting Systems. In Abramski, S., Gabbay, Dov. M. and Maibaum, T.S.E. (eds): Handbook of Logic in Computer Science, Chapter 1. Oxford Science Publications, (1992). [13] Krieg-Bruckner, B., Liu, J., Shi, H. and Wol, B.: Towards Correct, Ecient and Reusable Transformational Developments. In Broy, M., Jahnichen, S. (eds): KORSO, Correct Software by Formal Methods,, LNCS (1994), to appear. [14] Martin, U. and Nipkow, T.: Uni cation in Boolean Rings. Proc. 8th International Conference on Automated Deduction, LNCS 230, 506-513, (1986). [15] Nipkow, T.: Combining Matching Algorithms: The Regular Case. In N. Dershowitz, editor, Proc. 3rd Int. Conf. on Rewriting Techniques and Applications, 343-358, LNCS 355, 1989. [16] Peterson, G. E. and Stickel, M. E.: Complete Sets of Reductions for some Equational Theories. In: Journal of ACM 28, 233-264, (1981). 15
[17] Schmidt-Schauss, M. : Uni cation in a Combination of Arbitrary Disjoint Equational Theories. Journal of Symbolic Computation, (1989) 8, 51-99. [18] Shi, H.: An Ecient Matching Algorithm for Convergent Rewrite Systems. Under preparation. [19] Siekmann, J.H.: Uni cation Theory. Journal of Symbolic Computation, (1989) 7, 207-274. [20] Snyder, W.: Complete Sets of Transformations for General Uni cation. Ph.D. Dissertation, Dept. of Computer and Information Science, University of Pennsylvania, 1988. [21] Stickel, M.E.: A Uni cation Algorithm for Associative Commutative Functions. Journal of the ACM, Vol.28, No. 3, 423-434, (1981). [22] You, Y.H.: Enumerating Outer Narrowing Derivations for Constructor Based Term rewriting Systems. In Journal of Symbolic Computation 7, 391-343, 1989.
16