Journal of Automated Reasoning 23: 25–42, 1999. © 1999 Kluwer Academic Publishers. Printed in the Netherlands.
25
Completion for Multiple Reduction Orderings MASAHITO KURIHARA Department of Electrical Engineering, Hokkaido Institute of Technology, Sapporo 006-8585, Japan e-mail:
[email protected]
HISASHI KONDO Department of Systems Engineering, Ibaraki University, Hitachi 316-8511, Japan e-mail:
[email protected] (Received: 22 February 1996) Abstract. We present a completion procedure (called MKB) that works for multiple reduction orderings. Given equations and a set of reduction orderings, the procedure simulates a computation performed by the parallel processes each of which executes the standard completion procedure (KB) with one of the given orderings. To gain efficiency, however, we develop new inference rules working on objects called nodes, which are data structures consisting of a pair s : t of terms associated with the information to show which processes contain the rule s → t (or t → s) and which processes contain the equation s ↔ t. The idea is based on the observation that some inferences made in different processes are often closely related, so we can design inference rules that simulate these inferences all in a single operation. Our experiments show that MKB is significantly more efficient than the naive simulation of parallel execution of KB procedures, when the number of reduction orderings is large enough. We also present an extension of this technique to the unfailing completion for multiple reduction orderings, which is useful in various areas of automated reasoning, including equational theorem proving. Key words: completion, term rewriting, equational reasoning.
1. Introduction Given a system of equations and a reduction ordering on a set of terms, the standard completion procedure (KB) [11] uses the ordering for orienting equations (from left to right, or from right to left) and tries to compute a set of rewrite rules that is convergent (i.e., terminating and confluent) and equationally equivalent to the original system. As a result, it may either succeed (with a finite, convergent set of rules that may be used for deciding equational consequences of the original system), or fail (because of unorientable equations), or diverge (in a looping process trying to generate an infinite set of rules). The practical interest of the procedure is limited by the possibility of the failure and divergence. The success of the procedure heavily depends on the choice of the reduction ordering. Thus, the simplest (but often effective) way of trying to avoid the failure or divergence is to change the orderings. Actually, in many existing implementations
VTEX(VR) PIPS No: 189513 (jarskap:mathfam) v.1.15 JARSLW91.tex; 9/06/1999; 10:57; p.1
26
MASAHITO KURIHARA AND HISASHI KONDO
the user can interactively change (or extend) the orderings. Note, however, that this kind of implementation necessarily requires that the users have enough knowledge of the appropriate class of reduction orderings as well as intuition that one hopes is correct. From the viewpoint of interface with software designers and/or AI researchers who are not familiar with termination proof techniques, automatic change of (or search for) the orderings is desired. However, the problem is that since the completion process can diverge (and we can never decide the divergence in general), it is inappropriate to search for a correct ordering by just sequentially scanning possible orderings provided by the implementation; the process may fail to find a solution by sticking to an incorrect ordering indefinitely. This means that we have to consider, more or less, parallel execution of completion procedures each working with one of the possible orderings. This may be achieved by simply running multiple copies of the same sequential procedure on different workstations with different orderings in the input. Actually, such parallel or distributed implementations do exist. In this paper, however, we consider the situations in which the number of possible orderings is far larger than that of workstations. For example, if we take total lexicographic path orderings on a set of n function symbols, the number of orderings we have to consider is n!, which could easily exceed the number of workstations available. In such situations, it is necessary that each workstation take care of multiple orderings. However, it is clear that naive implementation, which might run multiple processes in parallel on a single machine, could result in serious inefficiency. In this paper, we present a single completion procedure that works for multiple reduction orderings. Basically, given equations and a set of reduction orderings, the procedure simulates a computation performed by the parallel processes each of which executes the standard completion with one of the given orderings. To gain efficiency, however, we develop new inference rules working on objects called nodes, which are data structures consisting of a pair s : t of terms associated with the information to show which processes contain the rule s → t (or t → s) and which processes contain the equation s ↔ t. The idea is based on the observation that some inferences made in different processes are often closely related, so we can design inference rules that simulate multiple inferences in several processes all in a single operation. Our experiments show that the proposed procedure is significantly more efficient than the naive simulation of parallel execution of the standard procedures, when the number of reduction orderings is large enough. We also present an extension of this technique to the unfailing completion for multiple reduction orderings, which is useful in various areas of automated reasoning, including equational theorem proving.
JARSLW91.tex; 9/06/1999; 10:57; p.2
COMPLETION FOR MULTIPLE REDUCTION ORDERINGS
27
2. Completion for Multiple Reduction Orderings 2.1.
STANDARD COMPLETION
We assume that the reader is familiar with the general idea of term rewriting systems [1, 6, 10, 13], reduction systems defined by a set of rewrite rules on first-order terms. We begin this section with a brief review of the standard completion [2, 4, 5]. Given a set E of equations and a reduction ordering , the standard completion procedure tries to generate a convergent (canonical) set R of rewrite rules that is contained in and that induces the same equational theory as E. It is defined in terms of the inference system KB, which consists of the following six inference rules: D ELETE : C OMPOSE : S IMPLIFY: O RIENT: C OLLAPSE :
(E ∪ {s ↔ s}; R}) ` (E; R) (E; R ∪ {s → t}) ` (E; R ∪ {s → u}) (E ∪ {s ↔ t}; R) ` (E ∪ {s ↔ u}; R) (E ∪ {s ↔ t}; R) ` (E; R ∪ {s → t}) (E; R ∪ {t → s}) ` (E ∪ {u ↔ s}; R)
D EDUCE :
(E; R) ` (E ∪ {s ↔ t}; R)
if t →R u if t →R u if s t if l → r ∈ R, t →{l→r} u, and t l · if s ←R u →R t
where · is a well-founded ordering on terms. In this paper, we use as · the encompassment ordering: s t · if a subterm of s is an instance of t, but not vice versa. It follows from the Critical Pair lemma that only critical pairs need to be considered in the DEDUCE rule. We write (E; R) `KB (E 0 ; R 0 ) if the latter may be obtained from the former by one application of the rule in KB. We usually leave implicit and write `KB . The results of a possibly infinite completion sequence (E0 ; R0 ) ` (E1 ; R1 ) ` · · · with E0 = E and R0 = ∅ are the sets E∞ and R∞ of persisting equations and rules. The sequence is successful if E∞ is empty and R∞ is convergent. The sequence fails if E∞ is not empty. The fairness conditions require that all critical pairs of persisting parents be considered in the DEDUCE rule. If a completion sequence is fair, then the limit system R∞ is convergent. A completion procedure is correct if it generates only fair or failing sequences. Let us define a completion procedure for multiple reduction orderings. Let O = {1 , . . . , m } be a finite set of reduction orderings and I = {1, . . . , m} be its index set. (Actually, we may allow O to be a multiset in order to avoid the problem of deciding the identity of orderings.) Given O and an initial set E of equations, the procedure simulates a computation performed by the parallel processes {P1 , . . . , Pm }, with Pi executing KB for the reduction ordering i and the common input E. Thus, we may regard I as indices of the parallel processes. Naive implementation of this scheme could result in serious inefficiency because many inferences made by different processes are related so closely (or even are virtually the same) that they could cause much waste of time and space. We can be smarter by exploiting this observation to design inference rules that simulate the
JARSLW91.tex; 9/06/1999; 10:57; p.3
28
MASAHITO KURIHARA AND HISASHI KONDO
D ELETE : O RIENT :
R EWRITE -1:
R EWRITE -2:
N ∪ {hs : s, ∅, ∅, Li} ` N if L 6= ∅ N ∪ {hs : t, L1 , L2 , L3 ∪ Li} ` N ∪ {hs : t, L1 ∪ L, L2 , L3 i} if L 6= ∅, L3 ∩ L = ∅, and s i t for all i ∈ L ( ) hs : t, L1 \L, L2 , L3 \Li, N ∪ {hs : t, L1 , L2 , L3 i} ` N ∪ hs : u, L1 ∩ L, ∅, L3 ∩ Li . if hl : r, L, . . . , . . .i ∈ N, t →{l→r} u, t = l, and (L1 ∪ L3 ) ∩ L 6= ∅ ( ) hs : t, L1 \L, L2 \L, L3 \Li, N ∪ {hs : t, L1 , L2 , L3 i} ` N ∪ hs : u, L1 ∩ L, ∅, (L2 ∪ L3 ) ∩ Li if hl : r, L, . . . , . . .i ∈ N, t →{l→r} u, t · l, and (L1 ∪ L2 ∪ L3 ) ∩ L 6= ∅ N ` N ∪ {hs : t, ∅, ∅, L ∩ L0 i} if hl : r, L, . . . , . . .i ∈ N, hl 0 : r 0 , L0 , . . . , . . .i ∈ N, L ∩ L0 6= ∅, and s ←{l→r} u →{l 0 →r 0 } t
D EDUCE :
Figure 1. MKB: Standard completion for multiple reduction orderings.
related KB inferences all in a single operation. The key idea is the data structure defined below. DEFINITION 1. A node is a tuple hs : t, L1 , L2 , L3 i, where s : t is an ordered pair of terms s and t, and L1 , L2 , L3 are subsets of I such that – L1 , L2 , and L3 are mutually disjoint, and – i ∈ L1 implies s i t, and i ∈ L2 implies t i s. The pair s : t is called a datum, and L1 , L2 , L3 are called labels of the node. The conditions above are called the label conditions. The node hs : t, L1 , L2 , L3 i is considered to be identical with the node ht : s, L2 , L1 , L3 i. Intuitively, L1 (L2 ) denotes the set of indices of processes (executing the KB) in which the current set of rules, R, contains a rule s → t (t → s); L3 denotes the set of indices of processes in which the current set of equations, E, contains an equation s ↔ t (or t ↔ s). The proposed system MKB consists of inference rules working on a set N of . nodes, as given in Figure 1. In the condition of REWRITE -1 the equation t = l denotes that t is an instance of l and vice versa: t and l are syntactically the same up · being the encompassment to renaming variables. Note that given t →{l→r} u and . ordering, the negation of t l, · appearing in REWRITE -2, is equivalent to t = l, appearing in REWRITE -1. As we have already assumed, we do not distinguish a node hs : t, L1 , L2 , L3 i from ht : s, L2 , L1 , L3 i. This implies that some inference rules implicitly specify the symmetric cases. In the following, we make informal comments about the inference rules one by one.
JARSLW91.tex; 9/06/1999; 10:57; p.4
COMPLETION FOR MULTIPLE REDUCTION ORDERINGS
29
The D ELETE rule removes a node containing a trivial equation s ↔ s. It simulates, in a single operation, applications of the DELETE operation (of KB) performed in all processes Pi with i ∈ L. O RIENT simulates processes Pi in which an equation s ↔ t is ORIENTed into the rule s → t. This is achieved by moving the indices i from the third label to the first one (or second, in the symmetrical case). The condition L 6= ∅ ensures that the operation is really effective. The other conditions are needed to preserve the label conditions. R EWRITE -1 and REWRITE -2 rewrite a term t to u by the rule l → r. The result is the modification of the labels of the node hs : t, . . .i and creation of a node hs : u, . . .i. We will see that REWRITE -1 simulates COMPOSE in the processes Pi with i ∈ L1 ∩ L, and SIMPLIFY in the processes Pi with i ∈ L3 ∩ L. All these KB operations in the processes Pi , i ∈ (L1 ∪ L3 ) ∩ L, are simulated by a single application of this MKB operation. Moreover, REWRITE -2 additionally simulates COLLAPSE in the processes Pi with i ∈ L2 ∩ L. To see this, consider the following two nodes: hl : r, L, . . . , . . .i . . . (1)
hs : t, L1 , L2 , L3 i . . . (2)
We assume that t →{l→r} u. By our interpretation of nodes, the current set of rules, R, contains both l → r and s → t in every process Pi with i ∈ L1 ∩ L. Therefore, COMPOSE may be applied. As a result, the rule s → t is replaced by a new rule s → u. This can be simulated by the modification of node (2) to (2a) and creation of node (3a): hs : t, L1 \L, L2 , L3 i . . . (2a)
hs : u, L1 ∩ L, ∅, ∅i . . . (3a)
Similarly, in every process Pi with i ∈ L3 ∩ L, R contains l → r and the current set of equations, E, contains s ↔ t. Therefore, the SIMPLIFY operation replaces s ↔ t by a new equation s ↔ u. This can be simulated by the modification of node (2) to (2b) and creation of node (3b): hs : t, L1 , L2 , L3 \Li . . . (2b)
hs : u, ∅, ∅, L3 ∩ Li . . . (3b)
It follows that the combination of COMPOSE and SIMPLIFY can be simulated by REWRITE -1, which modifies node (2) to (2c) and creates node (3c). hs : t, L1 \L, L2 , L3 \Li . . . (2c)
hs : u, L1 ∩ L, ∅, L3 ∩ Li . . . (3c)
To make this inference effective, we naturally require that (L1 ∪ L3 ) ∩ L 6= ∅. If t l · holds, we can combine more. Actually, it is easy to see that the combination of COMPOSE, SIMPLIFY, and COLLAPSE can be simulated by REWRITE -2. The last inference rule DEDUCE simulates the inference rule with the same name in KB. The critical node hs : t, ∅, ∅, L ∩ L0 i created here captures all the occurrences of the critical pair s ↔ t.
JARSLW91.tex; 9/06/1999; 10:57; p.5
30
MASAHITO KURIHARA AND HISASHI KONDO
It is possible to add the following two optional inference rules to MKB. They do not necessarily simulate effective inference of KB, but can affect efficiency of the completion procedure. GC: S UBSUME :
N ∪ {hs : t, ∅, ∅, ∅i} ` N ( ) hs : t, L1 , L2 , L3 i, N∪ ` N ∪ {hs : t, L1 ∪ L01 , L2 ∪ L02 , Li} hs 0 : t 0 , L01 , L02 , L03 i if s : t and s 0 : t 0 are the same (up to renaming of variables), where L = (L3 ∪ L03 )\(L1 ∪ L2 ∪ L01 ∪ L02 ).
G C (garbage-collect) effectively reduces the size of the current node database by one, by removing a node with three empty labels, because no processes contain the corresponding rule or equation. S UBSUME combines two nodes into a single one when they contain the same datum (up to renaming of variables). Note that we remove duplicate indices from the third label to preserve the label conditions. We use the notation `MKB in the same way as `KB . An MKB completion procedure is any program that takes as input a finite set E of equations and a set O of reduction orderings, and computes a sequence of inferences from the initial set of nodes, N0 = {hs : t, ∅, ∅, I i | s ↔ t ∈ E}, where I = {1, . . . , |O|}. Clearly, all initial nodes satisfy the label conditions. In general, if all the nodes of N satisfy the label conditions, then all the nodes of N 0 also satisfy them, whenever N `MKB N 0 . Therefore, the label conditions are always satisfied. 2.2.
FORMAL TREATMENT
The model based on the parallel processes discussed in the preceding section provides an informal semantics of MKB. In this section, we treat this idea rigorously and discuss the soundness, completeness, and fairness of MKB in terms of its formal semantics. Given the current set N of nodes, the following definition formally introduces the current sets (E[N, i]; R[N, i]) of equations and rules in a process Pi . DEFINITION 2. Let n = hs : t, L1 , L2 , L3 i be a node and i ∈ I be an index. The E-projection E[n, i] of n onto i is a (singleton or empty) set of equations defined by {s ↔ t}, if i ∈ L3 , E[n, i] = ∅, otherwise. Similarly, the R-projection R[n, i] of n onto i is a set of rules defined by {s → t}, if i ∈ L1 , R[n, i] = {t → s}, if i ∈ L2 , ∅, otherwise.
JARSLW91.tex; 9/06/1999; 10:57; p.6
COMPLETION FOR MULTIPLE REDUCTION ORDERINGS
31
These notions are extended for a set N of nodes, as follows: [ [ E[n, i], R[N, i] = R[n, i]. E[N, i] = n∈N
n∈N
Clearly, for the initial set N0 of nodes, we have (E[N0 , i]; R[N0 , i]) = (E0 ; ∅) for all i ∈ I , where E0 is the initial set of equations given by the user. In the rest of this section, we will discuss how `MKB and `KB are related by E- and Rprojections. = Let `= KB denote the reflexive closure of `KB . In other words, `KB means either `KB or =. Moreover, `KB is an abbreviation for `KBi . The following proposition formally states that MKB actually simulates KB. The easy proof is omitted. PROPOSITION 3 (Soundness). If N `MKB N 0 , then for all i ∈ I , 0 0 (E[N, i]; R[N, i]) `= KB (E[N , i]; R[N , i]),
where the strict part, `KB , holds for at least one i if the employed rule is not optional. The usefulness of MKB comes from the observation that the strict part, `KB , often holds for many i’s. The following proposition shows that MKB is as powerful as KB in its ability of inference. The easy proof is omitted. PROPOSITION 4 (Completeness). If (E[N, i]; R[N, i]) `KB (E 0 ; R 0 ), then there exists a set N 0 of nodes such that E 0 = E[N 0 , i], R 0 = R[N 0 , i], and N `MKB N 0 . Let S be a completion sequence N0 `MKB N1 `MKB · · ·. By projecting each = node set onto i ∈ I , we have a sequence (E0 ; R0 ) `= KB (E1 ; R1 ) `KB · · ·, where Ej = E[Nj , i] and Rj = R[Nj , i] for j ≥ 0. By removing all equivalent steps (Ej ; Rj ) = (Ej +1 ; Rj +1 ) we have a proper completion sequence (Ej0 ; Rj0 ) `KB (Ej1 ; Rj1 ) `KB · · ·, where 0 ≤ j0 < j1 < · · ·. We denote this sequence by KB[S, i]. The notions of success and failure of KB sequences are naturally extended to MKB sequences in terms of KB[S, i]: S is successful if KB[S, i] is successful for some i; and S is failing if KB[S, i] is failing for all i. As for fairness of S, however, it requires some subtleties to introduce a useful definition. A naive definition such as ‘S is fair if KB[S, i] is fair for all i’ is too strong to be useful in practice, because then S cannot fail for any i. Another possibility, ‘S is fair if KB[S, i] is fair for some i’, would not capture the main idea of MKB well when S is infinite. The fairness of KB[S, i] ensures a fair creation of (and selection from) critical pairs only in process i. For MKB to be really useful, we need a stronger notion of fairness which ensures a fair selection from processes so that no nonfailing process is ignored indefinitely. Of course, there is an exception when S is finite: when the process i has succeeded in completion in a finite amount of time, the overall MKB completion procedure will stop, with all
JARSLW91.tex; 9/06/1999; 10:57; p.7
32
MASAHITO KURIHARA AND HISASHI KONDO
the other processes ignored indefinitely. With this scheme, we would have a greater possibility of success. This motivates the following definition. DEFINITION 5 (Fairness). An MKB completion sequence S is fair if it satisfies the following conditions: – KB[S, i] is fair for some i ∈ I , and – if S is infinite, then for each i ∈ I , KB[S, i] is either fair or failing. Note that if S is infinite, some KB[S, i]’s may be finite failing sequences, but at least one KB[S, i] should be an (infinite) fair sequence. Given an MKB sequence S TN0 `MKB N1 `MKB · · ·, the set of persisting nodes is defined by N∞ = k≥0 j ≥k Nj . For a finite sequence ending with Nn , we let N∞ = Nn . An MKB completion procedure is correct if it generates only fair or failing MKB sequences. If a correct MKB completion procedure generates a nonfailing completion sequence S, then there exists an index i ∈ I such that KB[S, i] is fair; thus the limit rewrite system R[N∞ , i] is convergent. 2.3.
UNFAILING COMPLETION
A problem with the standard completion is that, even if an appropriate ordering is chosen, the procedure may fail to find any canonical system, though one exists. The unfailing completion (or ordered completion) addresses this problem and has turned out to be useful for many purposes, including equational theorem proving. In this section, we briefly review the unfailing completion [3] and apply our techniques to its extended version for multiple reduction orderings. Let be a reduction ordering. A rewrite rule uσ → vσ (with σ a substitution) is called an orientable instance of the equation u ↔ v (or v ↔ u) if and only if uσ vσ . The set of all orientable instances of equations in E is denoted by E . The unfailing completion is defined as the inference system UC consisting of the nine inference rules obtained from KB by replacing DEDUCE by a more general, ‘ordered’ version O RD D EDUCE :
(E; R) ` (E ∪ {s ↔ t}; R) if s ↔E∪R u ↔E∪R t, s 6 u, and t 6 u
and adding the following rules that use ordered rewriting for simplification: O RD C OMPOSE : O RD S IMPLIFY: O RD C OLLAPSE :
(E; R ∪ {s → t}) ` (E; R ∪ {s → u}) if lσ → rσ ∈ E and t →{lσ →rσ } u (E ∪ {s ↔ t}; R) ` (E ∪ {s ↔ u}; R) · if lσ → rσ ∈ E , t →{lσ →rσ } u and t l (E; R ∪ {t → s}) ` (E ∪ {s ↔ u}; R) · if lσ → rσ ∈ E , t →{lσ →rσ } u and t l.
JARSLW91.tex; 9/06/1999; 10:57; p.8
COMPLETION FOR MULTIPLE REDUCTION ORDERINGS
33
Actually, there are other versions of unfailing completion, as in [4, 6]. A theoretically simpler version never distinguishes E and R explicitly, and by regarding R as a part of E it redefines UC as inference rules operating only on E. In fact, however, it makes sense to distinguish them, because once an equation is moved from E to R, one never needs to check its orientation each time its instances are generated. In another version, the inference rule ORD D EDUCE is replaced by a stronger version based on the notion of ordered critical pairs, restricting the generation of equations more strongly. Computation of such critical pairs involves a decision problem that can be solved only for a restricted class of reduction orderings. Since we are interested in implementation as well as theory, we have decided to adopt the current version. However, it should be easy to extend our framework to such versions. We define the unfailing completion for multiple reduction orderings as the inference system MUC obtained from MKB by replacing the three inference rules REWRITE -1, REWRITE -2, and DEDUCE by their ordered versions given in Figure 2. The intuitive, informal idea and its formal treatment should be almost clear. O RD R EWRITE -1:
) hs : t, L1 \L0 , L2 , L3 \L01 i, N ∪ {hs : t, L1 , L2 , L3 i} ` N ∪ hs : u, L1 ∩ L0 , ∅, L3 ∩ L01 i 0 0 0 if hl : r, L1 , . . . , L3 ∪ Li ∈ N, L3 ∩ L = ∅, L0 = L01 ∪ L, . lσ i rσ for all i ∈ L, t →{lσ →rσ } u, t = l, and (L1 ∩ L0 ) ∪ (L3 ∩ L01 ) 6= ∅ O RD R EWRITE -2: ( ) hs : t, L1 \L0 , L2 \L0 , L3 \L0 i, N ∪ {hs : t, L1 , L2 , L3 i} ` N ∪ hs : u, L1 ∩ L0 , ∅, (L2 ∪ L3 ) ∩ L0 i if hl : r, L01 , . . . , L03 ∪ Li ∈ N, L03 ∩ L = ∅, L0 = L01 ∪ L, lσ i rσ for all i ∈ L, t →{lσ →rσ } u, t l, · and (L1 ∪ L2 ∪ L3 ) ∩ L0 6= ∅ O RD D EDUCE: N ` N ∪ {hs : t, ∅, ∅, Li} if hl : r, L1 , . . . , L3 i ∈ N, hl 0 : r 0 , L01 , . . . , L03 i ∈ N, ∅ 6= L ⊆ (L1 ∪ L3 ) ∩ (L01 ∪ L03 ), s ←{l→r} u →{l 0 →r 0 } t, and s 6i u 6≺i t for all i ∈ L (
Figure 2. Some inference rules for MUC.
JARSLW91.tex; 9/06/1999; 10:57; p.9
34
MASAHITO KURIHARA AND HISASHI KONDO
3. Implementation and Experiments 3.1.
COMPLETION PROCEDURE
Here are some notes on the implementation of data structures and procedures. Labels are best implemented as bit vectors (in which the ith bit is 1 if and only if i belongs to the label); then their union, intersection, and difference can be computed very quickly. This scheme requires three bits (per node) for each index i in order to distinguish i ∈ L1 , i ∈ L2 , i ∈ L3 , and i 6∈ L1 ∪ L2 ∪ L3 . Since exactly one of the four cases can occur, one could also encode this information in two bits at the cost of some extra computation time for decoding. Let us refer to the nodes hs : t, L1 , . . .i, hs : t, L1 \L, . . .i, and hs : u, L1 ∩ L, . . .i in REWRITE rules as original, updated, and created nodes, respectively. The inference rules remove an original node and introduce updated and created nodes. In actual implementation, however, we could directly modify the labels of the original node in order to put the updated node on the same memory location as occupied by the original node. This scheme is also applied to ORIENT. The informal account of REWRITE and ORIENT in the preceding section was already based on this scheme. In practical implementations of ORIENT, the set L of indices to be moved from the third label should be the maximal subset (of the third label) such that s i t for all i ∈ L. The subset may be trivially obtained by scanning the indices in the third label one by one, collecting i if and only if s i t; but for some classes of reduction orderings, this may be obtained more quickly. See [7] for an implementation using such a technique. For example, if O is a set of recursive path orderings and if we are trying to turn an equation into f (x) → g(x), then L is the intersection of the third label and the set of indices of orderings containing the precedence f g. A simple but practically most effective case is where O is a set of simplification orderings and the right-hand side of the rule is homeomorphically embedded in the left-hand side; then L is identical with the third label, because then the lefthand side is greater than the right-hand side in every simplification ordering. In particular, this scheme should be executed almost for free, when the right-hand side is a variable occurring in the left-hand side. Naive implementation of the optional rule SUBSUME can make the program slower, because it requires the search in the large node database for the nodes with virtually the same datum s : t. Hence, this should be implemented with appropriate hashing or indexing schemes which would find those nodes efficiently by using the (part of) datum s : t as a key. For example, you might want to develop a simple, but effective scheme in which the tuple (f, g, |s|, |t|) is used as a key for a hash table of nodes, when f and g are the leftmost function symbols of s = f (. . .) and t = g(. . .), and |s| and |t| are the size (the number of function symbols) of the corresponding terms. A possible MKB completion procedure, named mkb, is given in Figure 3. It accepts as input a set E of equations and a set O of reduction orderings, and returns
JARSLW91.tex; 9/06/1999; 10:57; p.10
COMPLETION FOR MULTIPLE REDUCTION ORDERINGS
35
procedure mkb(E, O); begin No := {hs : t, ∅, ∅, I i | s ↔ t ∈ E} where I = {1, . . . , |O|}; Nc := ∅; while success(No , Nc ) = false do if No = ∅ then return(fail) else n := choose(No ); No := union(No − {n}, delete(rewrite({n}, Nc ))); if n 6= h. . . , ∅, ∅, ∅i then n := orient(n); if n 6= h. . . , ∅, ∅, . . .i then No := union(No , delete(rewrite(Nc , {n}))); Nc := gc(Nc ); No := union(No , delete(deduce(n, Nc ))) end; Nc := union(Nc , {n}) end end end; return(R[Nc , i]) where i = success(No , Nc ) end mkb.
Figure 3. A completion procedure for multiple reduction orderings.
as output a convergent set of rewrite rules if it successfully halts. The procedure is based on the open/closed (set-of-support/have-been-given) lists algorithm, which is well known in the literature of search and automated reasoning for artificial intelligence. The sets No and Nc of nodes play the role of the open and closed lists, respectively. In the sequel, the elements of No (Nc ) will be called open (closed) nodes. Initially, Nc is empty and No consists of all the initial nodes. The union No ∪ Nc of the current sets of nodes defines a current set Nj of nodes in an MKB completion sequence. Although we have seen the inference rules as working on a whole node database, we can also see them as working on a single node or two nodes explicitly referred to in the rules. More precisely, let us refer to DELETE, ORIENT, and REMOVE as the single-node operations, and REWRITE(-1,-2), DEDUCE, and SUBSUME as the double-node operations. Then the former is applied to a single node, while the latter to a pair of nodes.
JARSLW91.tex; 9/06/1999; 10:57; p.11
36
MASAHITO KURIHARA AND HISASHI KONDO
The heart of mkb is the while loop. We assert (as a loop invariant) that, before and after each iteration of the body of the loop, – every closed node has been fully considered for application of the single-node operations, and – every pair of closed nodes has been fully considered for the double-node operations. The implication is that all we have to do is apply the single-node operations to open nodes, and the double-node operations to pairs of nodes that include at least one open node. Mkb implements a natural way of doing this procedure. It selects an open node one by one, to which all possible single-node operations are applied. Then the node is combined with each closed node to form a pair on which all possible double-node operations are performed. Finally, the open node is closed. The procedure repeats these steps either indefinitely or until it succeeds or fails. The procedure success(No , Nc ) checks if the completion process has succeeded. More precisely, it is successful if there exists an index i ∈ I such that i is not contained in any labels of No nodes and any L3 labels of Nc nodes. Then E[No ∪Nc , i] is the empty set of equations, and R[Nc , i] is a convergent set of rules contained in i . We assume that the procedure returns the index i as above if it is successful, and returns false, otherwise. Actually, we need not scan all the nodes every time success is invoked; this decision could be made more efficiently if we introduce integer variables ci (i = 1, . . . , |I |) for counting the occurrences of the index i in the labels as above, increasing and/or decreasing them every time a node is created, updated, or removed. The procedure choose(No ) selects a node from No . We assume that this selection is fair and that every node in No will be eventually selected if mkb does not fail. It may be a heuristically good strategy to prefer a node with the ‘smallest’ datum (in a sense, say, measured by its size), as long as it is fair. The procedure union(N, N 0 ) computes the union of N and N 0 . If the optional SUBSUME operation is employed, the operation is applied to a suitable pair of nodes of which at least one is from N 0 . The procedure delete(N) applies (if possible) the DELETE operation to N and returns the set of the remaining nodes. Note that only the nodes created by REWRITE and DEDUCE need to be submitted to this operation. The procedure rewrite(N, N 0 ) repeatedly applies REWRITE(-1,-2) rules (zero or more times) to N ∪ N 0 , rewriting the data of N by the rules of N 0 until no more rewriting is possible. It returns the set of nodes created in this process. We assume that this process contains mutation operations in which the labels of original nodes of N are directly modified in order to have the updated nodes stored in the same memory location as the original node. This means that the procedure implicitly executes the assignment such as N := N − {original nodes} ∪ {updated nodes}.
JARSLW91.tex; 9/06/1999; 10:57; p.12
COMPLETION FOR MULTIPLE REDUCTION ORDERINGS
37
The procedure orient(n) repeatedly applies ORIENT (zero or more times) to the node n until no more application is possible, and returns the resultant node n, which is the original object but whose labels may have been modified. The procedure gc(N) applies (if possible) the optional GC operation to N and returns the set of the remaining nodes. This operation is also implemented implicitly by the second if statement. Note that the nodes that can be potentially removed by this procedure are exactly those updated in the REWRITE operations. The procedure deduce(n, N) applies DEDUCE to {n} ∪ N and returns the set of all critical nodes between n and a node from {n} ∪ N. The loop invariant, together with the fair selection by choose, ensures the correctness of the procedure, as follows. The easy proof is omitted. PROPOSITION 6 (Correctness). Mkb is a correct MKB completion procedure. Note how the correctness of mkb depends on the loop invariant, independent of the specific arrangement of inference operations in the body of the loop. Hence, one can rearrange the order of application of the inference operations without losing the correctness as long as the loop invariant is preserved. Now let us briefly discuss about the implementation of unfailing completion for multiple reduction orderings. A possible MUC completion procedure, named muc, should be the same as mkb except the following points. – The procedure is successful if and only if there exists some index i ∈ I that is not contained in any labels of No nodes, because this means that all possible inferences have been made in process i. In that case, muc should return the system R[Nc , i] ∪ E[Nc , i], which is ground convergent with respect to i . In particular, unlike mkb, muc returns successfully if No = ∅. – The procedures rewrite and deduce should be replaced by their ordered versions ordrewrite and orddeduce. 3.2.
EXAMPLE
Let us illustrate the completion procedure mkb by a very simple problem. Consider an equational system consisting of the two equations (x + y) + z = x + (y + z) and f (x) + f (y) = f (x + y). Let 1 and 2 be the lexicographic path orderings (or recursive path orderings with left-to-right status) induced by the precedence + f and f +, respectively. It is known [1] that if we use 1 in the standard completion, the procedure will loop, generating an infinite canonical system, while the use of 2 will lead almost immediately to the finite canonical system R∞ = {(x + y) + z → x + (y + z), f (x + y) → f (x) + f (y)}. Let us apply the mkb procedure to this problem with O = {1, 2 } and I = {1, 2}. The initial sets of nodes are h(x + y) + z : x + (y + z), ∅, ∅, {1, 2}i, (1) No = hf (x) + f (y) : f (x + y), ∅, ∅, {1, 2}i (2)
JARSLW91.tex; 9/06/1999; 10:57; p.13
38
MASAHITO KURIHARA AND HISASHI KONDO
and Nc = ∅. We assume that node (1) is selected by choose. Then by orient the node is modified to h(x + y) + z : x + (y + z), {1, 2}, ∅, ∅i,
(10 )
and a critical node h(w + x) + (y + z) : (w + (x + y)) + z, ∅, ∅, {1, 2}i
(3)
between itself is created. After (10 ) is moved from No into Nc , we have No = {(2), (3)} and Nc = {(10 )}. Suppose node (2) is selected and oriented to (20 )
hf (x) + f (y) : f (x + y), {1}, {2}, ∅i.
Two critical nodes (4) and (5) given below are created between (10 ) and (20 ). At this point, we have No = {(3), (4), (5)} and Nc = {(10 ), (20 )}. hf (x + y) + z : f (x) + (f (y) + z), ∅, ∅, {1}i,
(4)
hf (x + (y + z)) : f (x + y) + f (z), ∅, ∅, {2}i.
(5)
By fairness the nodes (3) and (5) will be eventually selected. To make a long story short, let us suppose that they are selected successively from now on. Then by rewrite both are reduced to a node with a trivial equation and thus deleted. All the intermediate nodes (including the original nodes), which contain only empty labels, are removed by GC. Now we have No = {(4)} and Nc = {(10 ), (20 )}. Since the third labels of (10 ) and (20 ) are empty (thus not containing index 2) and since index 2 is not contained in any labels of (4), the success procedure reports the success of completion by returning 2, and the procedure is finished. This means that the completion has succeeded under the reduction ordering 2 , yielding the finite canonical set R∞ = R[Nc , 2] of rules described before. Although this simple example shows almost nothing but mkb’s ability of simulating parallel execution of KB, its effect on performance should be clear if we think of the extension of this example with more equations on a larger set of function symbols. For example, let O be a larger set of lexicographic path orderings and I the corresponding set of indices. Nevertheless, by orient we can expect to have a node h(x + y) + z : x + (y + z), I, ∅, ∅i
(10 )
at almost the same cost as before, because (x + y) + z is greater than x + (y + z) in every ordering in O and an appropriate implementation of orient could determine and exploit this fact very quickly. Similarly, we would have a node hf (x) + f (y) : f (x + y), I1 , I2 , ∅i,
(20 )
JARSLW91.tex; 9/06/1999; 10:57; p.14
COMPLETION FOR MULTIPLE REDUCTION ORDERINGS
39
where I1 and I2 , both being subsets of I , correspond to the sets of all the orderings (in O) containing + f and f +, respectively. Then by deduce we would have the following critical nodes: hf (x + y) + z : f (x) + (f (y) + z), ∅, ∅, I1i,
(4)
hf (x + (y + z)) : f (x + y) + f (z), ∅, ∅, I2i.
(5)
This simulates the creation of critical pairs in all processes Pi , i ∈ I1 ∪ I2 , at virtually the same cost as the simple example. 3.3.
EXPERIMENTS
We have implemented the procedures mkb and muc in Lisp and made some experiments. It has turned out that both procedures show virtually the same performance. Rather than presenting similar results almost in duplicate, we will restrict ourselves here to presentation of the performance of mkb more in detail. The sample problems have been taken from [14]. Mkb was compared with a standard completion procedure kb and also with a procedure (named pkb) that simply simulates parallel execution of kb on a sequential machine. Kb is implemented in the framework proposed by [12], and pkb is implemented on the multitasking facility of Lucid Common LispTM . The results show that because of the overhead for node manipulation, mkb is slightly slower than pkb when the number of reduction orderings, m = |O|, is relatively small, but when m is large enough, mkb is significantly faster than pkb. The same conclusion applies to muc. We start with the results for the following typical problem (Example 3.14 of [14]) in order to get an overall view of how the efficiency depends on m. s(s(x)) = x f (0, y) = y f (s(x), y) = s(f (x, y)) f (f (g(x, y), 0), 0) = g(x, y)
g(0, y) = y g(s(x), y) = f (g(x, y), 0) h(0) = s(0).
The problem is to complete the system of these equations. (In [14] the problem is to complete the system of rewrite rules defined by orienting these equations from left to right.) The completion succeeds if we specify as a reduction ordering a recursive path ordering induced by a precedence satisfying g f s, g 0, and h s. Given a node n = hs : t, L1 , L2 , L3 i, let |n| = |L1 | + |L2 | + |L3 | (where each |Li | denotes the cardinality of the set Li ) be the number of processes sharing the datum s : t in our formal semantics. For example, if n = hs : t, {1, 5}, { }, {2}i, then |n| = 3. Clearly, mkb and muc are expected to be useful only if |n| is large enough on the average. In the experiment, we have considered 5! = 120 recursive path orderings induced by all total precedences on the set {f, g, h, s, 0}, and computed |n| for each
JARSLW91.tex; 9/06/1999; 10:57; p.15
40
MASAHITO KURIHARA AND HISASHI KONDO
Table I. Distribution of |n|. |n|
6 12 15 20 21 25 28 30 40 50 74 90 120 Total
frequency 2
1
6
2
2
5
1
7
1
1
1
3
2
34
Table II. Effects by the number of orderings. m
1
5
10
20
40
120
m
1
5
10
20
40
120
kb pkb mkb
1 1.1 1.2
8.4 7.1
17 9.2
35 13
72 17
99 18
kb pkb mkb |n|
1 1 1.4 1
5.8 4.5 1.9
13.6 4.5 3.2
24 6.6 6.0
38 5.5 11.0
102 4.1 36.3
(a) Execution time
(b) The number of equations/nodes
node n in the node database when the procedure successfully finished. Actually, we have got 34 nodes and found that |n| = 36.3 on the average. This means that each datum is shared by about 30 % of the processes. The distribution of |n| is shown in Table I. To see how the efficiency depends on the number of reduction orderings, we have selected m (= 1, 5, 10, 20, 40, 120) distinct orderings from the 5! orderings given above. The selection was made at random, except that the ordering g f h s 0 has been always included to ensure a success. The statistics are given in Table II. Table (a) shows the average execution time. Table (b) shows the average number of equations (for pkb) or nodes (for mkb) required in the computation, i.e., the number of initial equations (nodes) plus the number of equations (nodes) created in the processes. (The number of initial equations for pkb is m times larger than that for kb.) This table also shows the average number of |n|, which turns out to be almost linear for m ≥ 10. In both tables the entries for kb are normalized to 1. We see that mkb is less efficient than pkb when m is less than about 5, but when m is greater than 10, mkb is significantly more efficient. Table III shows the results for other problems. From [14] we selected ten problems which contained five or more function symbols and were solved with recursive path orderings of the left-to-right status. (The problem number xx denotes Example 3.xx of [14].) The number of reduction orderings to be considered was fixed to m = 40. We see that in most problems (except problem 22) the number of nodes for mkb was smaller than that of equations for pkb and that in all the problems mkb was faster than pkb on the average. Note that for the exceptional problem, the average of |n| is only 1.1, which is far smaller than the other problems. In an additional experiment, however, we have found that problem 22 is not an exception any more when we increase the number of reduction orderings to m = 60.
JARSLW91.tex; 9/06/1999; 10:57; p.16
41
COMPLETION FOR MULTIPLE REDUCTION ORDERINGS
Table III. Experimental results for ten problems (m = 40). No.
13
14
19
20
21
22
23
29
30
31
pkb mkb
38 8.4
72 17
70 16
48 8.7
1.6 1.2
45 30
15 6.1
48 8.8
93 8.5
2 1.8
(a) Execution time
No.
13
14
19
20
21
22
23
29
30
31
pkb mkb |n|
38 3.8 16.8
38 5.5 11.0
52 6.5 9.8
51 12 17.2
8.6 3.4 13.6
49 62 1.1
24 3.3 8.6
47 4.4 8.6
65 4.7 12.6
24 3.8 30.2
(b) The number of equations/nodes
4. Conclusion We have presented the standard and unfailing completion procedures for multiple reduction orderings. Basically, they simulate a parallel execution of the basic procedures, except that they share information via node structures consisting of a pair of terms and three labels. This makes it possible to simulate multiple basic inferences in different processes all in a single operation. We have formally discussed the soundness, completeness, and fairness. We have proposed a possible correct implementation and made some experiments to show that the proposed procedures are significantly more efficient than the naive simulation of parallel execution of the basic procedures, when the number of reduction orderings is large enough. The future work includes the attempt to increase the significance of the proposed framework by showing it can find quick/short proofs for some challenge problems. Acknowledgments This work is partially supported by the Education Ministry of Japan, under the grants No. 09650444 (for the first author) and No. 09780231 (for the second author). The first author is also supported by the special grants from the Hokkaido Institute of Technology. References 1. 2.
Avenhaus, J. and Madlener, K.: Term rewriting and equational reasoning, in R. B. Banerji (ed.), Formal Techniques in Artificial Intelligence: A Sourcebook, North-Holland, 1990, pp. 1–44. Bachmair, L., Dershowitz, N., and Hsiang, J.: Orderings for equational proofs, in Proc. IEEE Symposium on Logic in Computer Science, 1986, pp. 346–357.
JARSLW91.tex; 9/06/1999; 10:57; p.17
42
MASAHITO KURIHARA AND HISASHI KONDO
3.
Bachmair, L., Dershowitz, N., and Plaisted, D. A.: Completion without failure, in H. AïtKaci and M. Nivat (eds.), Resolution of Equations in Algebraic Structures, vol. 2: Rewriting Techniques, Academic Press, 1989, pp. 1–30. 4. Bachmair, L.: Canonical Equational Proofs, Birkhäuser, 1991. 5. Dershowitz, N.: Completion and its applications, in H. Aït-Kaci and M. Nivat (eds.), Resolution of Equations in Algebraic Structures, vol. 2: Rewriting Techniques, Academic Press, 1989, pp. 31–85. 6. Dershowitz, N. and Jouannaud, J.-P.: Rewrite systems, in J. van Leeuwen (ed.), Handbook of Theoretical Computer Science, vol. B, North-Holland, 1990, pp. 243–320. 7. Detlefs, D. and Forgaad, R.: A procedure for automatically proving the termination of a set of rewrite rules, in Proc. 1st Conf. on Rewriting Techniques and Applications, Lecture Notes in Comput. Sci. 202, 1985, pp. 255–270. 8. Huet, G. and Oppen, D. C.: Equations and rewrite rules: A survey, in R. Book (ed.), Formal Language Theory: Perspectives and Open Problems, Academic Press, 1980, pp. 349–405. 9. Huet, G.: A complete proof of correctness of the Knuth and Bendix completion algorithm, J. Comput. System Sci. 23 (1981), 11–21. 10. Klop, J. W.: Term rewriting systems, in S. Abramsky et al. (eds.), Handbook of Logic in Computer Science, vol. 2, Oxford Univ. Press, 1992, pp. 1–116. 11. Knuth, D. E. and Bendix, P. B.: Simple word problems in universal algebras, in J. Leech (ed.), Computational Problems in Abstract Algebra, Pargamon Press, 1970, pp. 263–297. 12. Lescanne, P.: Completion procedures as transition rules + control, in Proc. TAPSOFT, vol. 1, Lecture Notes in Comput. Sci. 351, 1989, pp. 28–41. 13. Plaisted, D. A.: Equational reasoning and term rewriting systems, in D. M. Gabbay et al. (eds.), Handbook of Logic in Artificial Intelligence and Logic Programming, vol. 1, Oxford Univ. Press, 1993, pp. 274–367. 14. Steinbach, J. and Kühler, U.: Check your ordering – termination proofs and open problems, SEKI report SR-90-25 (SFB), Fachbereich Informatik, Universität Kaiserslautern, Germany, 1990.
JARSLW91.tex; 9/06/1999; 10:57; p.18