Volume IS, Number 2
INFORMATION
PROCESSING LETTERS
6 September 19b2
ON MULTISET IORDERINGS
Jean-Pierre JOUA,NNAUD Centre de Rechetce en Informatique de Nancy, CO 140, F54037 Nancy, FranLe
Pierre LESCANNE Centre de Recherche en Informatique de Nancy, CO 140, F54037 Nancy, France and Laboratory for Computer Science, Massachusetts Institute of Technology Square, Cambridge, MA, U.S.A. Received 29 September 1981; revised version received 18 June 1982 Abstract: We propose two-well founded orderings on multisets that extend the Dershowitz-Manna ordering. Unlike the Dershowitz-Manna ordering, ours do not have a natural monotonicity property. This lack of monotonicity suggests u&g monotonicity to provide a new characterization of the Dershowitz-Manna ordering. Section S proposes an efficient and correct implementation of that ordering. Rt!sum~: Darts cette note, on propose deux ordres biend fond&s qui etendent l‘ordre de Dershowitz et Manna. Ces ordres ne verifient pas une prop&t& natureile de monotonicite que nous definissons. Aussi cela suggere d‘utiliser la monotonicite comme une nouvelle caracterisation de l‘ordre de Dershowitx et Manna. La cinqui&ne section de cette note propose une implantion efficace et correcte de: cet ordre.
KeyworBr: Multiset ordering, well-founded ordering
1. Introduction The multiset ordering proposed by Dershowitz and Manna [2] is a basis for many orderings used for proving termination of programs and term rewriting systems [ 11,and it would be nice to have all efficient implementation of it. Often, deriving an algorithm directly from a mathematical definition is not the best way. Thus a more suitable definition and a proof that this new program implements the desired function must be found. Our approach was as follows. We tried two definitions, both having efficient implementations but both failing to be equivalent. In fact, they are stronger than the Dershowitz-Manna multiset ordering but do not have a monotonicity property. As an explanation of these facts, we give a new definition of the De.rshowitz-Manna ordering based on the main characterization of this ordering: no stronger monotonic ordering exists on 0020-O190/82/0000-0000/$02.75
multisets. In Section 5 of this paper, we propose a correct and efficient implementation of ti?e Dershowitz-Manna ordering.
2. The Dershowitz-Manna ordering Intuitively, a multiset M (or bag) on E is an unordered collection of elements of E, with possibly multiple occurrences of elements. More formally M is a mapping E + N, where IV is the set of natural numbers, associating with each value in E the number of times it occurs in the multiset. For example, x is in M if M(x) > 0. %(E) denotes the set of all the finite multisets on E, i.e., the multisets M with finite carrier {x E El M(x) # 0). The empty multiset { } has { j(x) = 0, for all x in E. A set is particular case of a multiset such that S(x) G 1 for each x in E.
0 1982 North-Holland
57
Volume 15,
~&&ion
Number2
INFORMATION
2.1. The sum of twc multisets I4 and N
is the multiset M + N such that M + N(x) = M(X) + N(x). M + N is an associative and commutative
operation on %(E) with neutral element ( ). If M,, M *, . . ., MP are multisets Cr= iMi is the multiset SU& tht;rti’f’= ,M,)(x) = Zp= iMi(X). M f N k 8 set only if M and N are disjoint sets; in this case + is the classical disjoint union or direct sum of sets. Definition 2.2. Multiset M is included in multiset N
(written M c N) if and only if Vx E E M(x) G N(x). Definition 2.3. If M c N, the dgference PJ - 34 is defined by (N - Id)(x) = N(x) - M(x).
In this paper, an ordering 4 on a set is a partial or total strict ordering, i.e., an irreflexive and transitive relation on E. We use the notation x# y or x=y or x~y). Assume to mean +y+x throughout that E is ordered by 4 . Defiiition 2.4. The Dershowitz-Manna ordering is defined as follows: M + N if there exist two multisets X and Y in S(E) satisfying (i) { )+XGN, (ii) M=(N--X)+Y, (iii) X dominatesy: VyEY BXEX y+x.
Using Definition 2.4, it may be difficult to prove that two multisets are not related by +. In [3], Huet and Oppen give a different and more tractable definition. Definition 2.5. The &et-Oppen
definition is de-
fined as follows: M+Niff
6 September 1982
PRCbCESSlNG LETTERS
M#N&
[M(y) > N(y) =)(3x E E y < x&M(x) c ‘N(x)].
follows: X(x) = max( N(x) - M(x),01 , Y(Y) = I==(M(Y)
- NtYhQ) *
Let us prove (i) of Definition 2.4. (a) X c N is clear by definition. (b) Be&use M # N there exist z such that M(z) # N(z). If M(z) < N(z), then z E X, if M(z) > N(z), by Definition 2.5 there exist x, such that z -c x and M(x) c N(x). Hence x E X. In both cases X # ( }# (ii) is true by construction. To prove (iii), let y E Y. By hypothesis, there exist x satisfying y < x and M(x) c N(x). Hence 3 x E X y < x. because X#( > SUppOSe M <‘ 0 *Y(x) = 0 &Y(x) > 0 v X(x) = 0. Otherwise X would be replaced by X-Z and Y by Y-Z where Z(x)= min(X(x), Y(x)}. Assume M(y) > N(y). By (ii) M(y) = N(y) + Y(y) - X(y). This implies Y(y) > X(y) > 0, which means y E Y. By (iii) there exist x E X, i.e., X(x) > 0, such that y *: x. The value of M in x is M(x) = (N(x) -X(x)) +Y(x) =N(x) X(x) because Y(x) = 0. Thus M(x) c N(x). Cl Another important property of the Dershowitz-Manna ordering is monotonicity. Definition 2.7. Let 4 be a partial ordering on E and T a mapping from E X E into %(E) X x(E). T(<) is said to be a monotonic extension of < iff
(1) 7(*) is an ordering, (2) 7 is monotonic, i.e., < C <=+I( I<) e r( 0. Lemma 2.8 (Monotonicity lemma). The Dershowitz-Manna ordering + is a motiotonic extension of + . Proof. Straightforward using the Huet-Oppen
inition.
def-
Cl
Lemma 2.6. ?%e Dershowitz-Manna definition is equivalent to the Huet-Oppen definition.
3. Partition based orderings
Let +Dh/b and qHO be the DershowitzManna and HuetOppen orderings, respectively. Assume M -HO N, and define X and Y as
We now define two multiset orderings based on partitioning a multiset. We sa;q that (M; 1i = I , -. . , p) is a partition of a multiset iff Mm=Zr! ,Mi.
h-f.
58
Volume 15, Number 2
INFORMATION PROCESSING LETTERS
Assume now, we are able to compare the Mi using an ordering 4 , and thus to sort them such that 4 M,. It is now easy to define a M,gMp-,Q new ordering
M+N
iff
l
l
M,M,...M,41exN,N,...N
9’
In practice, we have to define the basic ordering 4 and the method for constructing the partition of a given multiset. 3.~. The multiset ordering -+=z~ Assume that the partition &I= (Mi 1i = 1,. . . , p) of the multiset M satisfies l:he following properties: (1) X E Mi ~ M,(X) := M(X). (2) XEM, and yElMi~X and y are incomparable. (3) V iE[2..pJ xEMi*3 yEMi_1 X*y. Intuitively the partition is built by first computing the multiset M, of all the maximal elements and then recursively computing the partition of M-M,. Example 3.1. M=(a, a, 2, 2, b, 1, I} with a
iff
M=+NandVxEEM(x)GN(x) or3yENxly.
Let a’(E) be the subset of x(E) such that if ME%‘(E) and xEM and yEM, then x and y are incomparable.
6 September 1982
It is easy to see that -+c% is an ordering because lexicographical extension preserves the orderings. We will show that -+z% is more powerful than e. Lemma
3.4. M *
N knplies M -R% N.
Proof. Suppose M-N, and M=M,...M,, and N = N, . . . Nq. Then we have M +&I. The proof is by induction on p i- q. The result is obvious if M={ }. Assume that p#O and q#O. Three cases have to be distinguished. Case 1. M, 4% N,. The result is straightforward. +%N2...Nq Case2. M, =N,.ThusM,...M, and the result follows by the induction hypothesis. Qzse 3. Y, 4 m M, or M, and N, ali incomparable. There must exist an x E M, such that M,(x) > N,(x) and V y E N, x * y. This contradicts the hypothesis M + N. Cl We show that the converse is false by using the previous example. We see that M(a) = 1~ N(a) = 0. However, the only element in N greater than a, i.e., b has one occurrence in both M and N. Thus M and N are incomparable using the Dershowitz-Manna ordering. On the other hand, d<% has a serious drawback, which prevents it from being used in some usual cases requiring incremental orderings; it is not monotonic. Let us go back to Example 3.3 and assume now l( a < b. This increases the basic ordering by adding the new pair 14 a. We now get M,= (b},
M,=(a),
N, = (b),
N,=(U)
Mj= (l),
thus N ~4~ M. 3.2’. The multiset ordering (4s
Definition 3.2, Let M and N be multisets. We say that M +c% N iff a-<$#.
A different way to construct a partition of a multiset is to require that each multiset in the partition be a set. Thus the partition m = (Si 1i = 1. . . p) must satisfy the following properties:
Example 3.3. If a-< b and M = { 1, a, b}, then M,={l, b}, M2=(il}. If N={l, 1, b}, then N, = (1, 1, b) and M ‘
(1) Si is a set, that is, Si(x) G 1. andy areincomparable. (2) ~~S~andy~S~i~~~plies~
59
INFORMATION PROCESSING LE’ITERS
Volume 15, Number 2
(3) V i E [2. .p]
X E
Si implies 3 y E Si- 1 X 4 Ye
difference between the partitions created by +
only
Example3.5. M=(a, a, 2, 2, b, 1, 1) with a
iff
S#TandVxES3yETx+y.
In the following N = (Ti 1i z 1..q) will be the partition of N. If sets are considered as a particular case of multisets, -+ is N< on the sets of incomparable elements.
6 -ternbet
1982
Example 3.7. We get S, z=(b), S, = (b) and T, = (b}, T2 = (a). Thus N -1~~ M., Therefore +Q, is not a monotonic ordering Now, let us try to c’ompare +Q and +%, using two examples with a + b: (i) M = (1, a, b}, N = (1, 1, b},M “
Example 3.7. Suppose a and b incomparable. If N = (a, b}, then T, = {a, b); if M = (b, b), then S, = (b}, S,= {b} and M +zs N.
Proof. ]2] contains a proof of the well-founde&ess of ~4. Note that a proof of well-foundedness of
Once more, it is easer j to see that es is an ordering. Let us show that it is more powerful than +<.
4. A property of maximal@ of fhe DershowitzManna ordering
Definition 3.6. Let M and N be multisets. We say that M +
Lemma 3.8. M + N implies M <+, N. Roof. By induction on p and q. The result is straightforward if M = { }. Else, we have to distinguish three cases. Case 1. S, *
In the previous section, we showed two non-monotonic orderings containing the Dershowitz-Manna ordering +. A natural question arises: Do monotonic orderings exist on %(E) that contain +? The answer is negative and provides 5 new important characterization of +z, Let us first prove an important lemma. Lemma 4.1. Let -C be a partlial ordering on E and let M and N be two mu&sets clllE s1IcIIthat N 6 ti M that is -(N = M OPN * M). Then there exists a partial ordering c on E such that c 2 4 (that is, y
Volume 15, Number 2
INFORMATION PROCBSING
Basic case: Let D==0. Then NQ?M*M+c N. be not empty. Then either and the result is proved with < = < , or and there must exist a pair (x, y) such
General case: Let 1)
M *N N-M that
(1) M(x)>N(x)
andVzEEx
(2) M(y) < N(y) and V z E E y 4 z N M(z) < N(z). It follows from (1) and (2) that x $6~ an thus (x, y) e D. Let now <: be the transitive closure of the relation union of c( and the pair (x,y). e is clearly an ordering strictly containing I(. Therefore * is an ordering strictly containing +. Since x 4 y, either M 4: N and the result is true or M and N are incomparable according to a and the result follows from the induction hypothesis used with a new D, whose cardinal is strictly less than the previous one. Cl Theorem 4.2 (Maximality theorem). Lst N be a partial ordering on E and I(<<) a monotonic extension of 4 such that + C r( + ). l%en * and T( 4) are the same ordering. Proof. Assume first < is total. Then +c is total
on $X(E) and must coincide with ~(4). Assume now that 4 is partial on E. Then + is partial on s(E). Suppose that ~(4) > e. Then there must exist two multisets M and N such that Mr(+)N and M#N for the ordering 4<. Using Lemma 4.1 there exist < 2 4 such that N a M, which implies NT(c)M by hypothesis. Using now the monotonicity of the multiset extension I and the hypothesis M+)N, we get M+)N, which is a contradiction. 0 This main property of the Dershowitz-Manna ordering can be used to give a simple proof of the equivalence of Definitions 2.4 and 2.5. In the following, we use this technique to present and prove a new definition of %. If 4 is a total ordering on E, *“’ is a total ordering on the ordered lists on E which provides a simple definition of the Dersh!owitz--Manna ordering in that particular case: let kst(M)= (x,, xz,...,fc,] with j > i eoxi 4; xi be the ordered list representation of
LETTERS
multiset M. Then M + N iff &jM) 4 lcrtlist(N). Let us now define a new multiset ordering -+z, in the following way. DefWio8l43. Given a partial ering * 1etM +.Niffforall < that total or6 containing * , &z(M) ekx h&(N). It is ea5y to prove that this new exactly the Dershowitz-Manna orderin application of Theorem 4.2. Lemma 4.4. -%. is a monotonic ordering. Proof. Follows obviously from Definition 4.3.
Roof. Suppose M + N and --,(I+#W. N). There exists a total ordering < such that c 2 4 and +lisi(N) ekx list(M)). Thus there exist y such that M(Y)> N(Y) and Y c z - N(z) = M(z), which implies y 4 z *N(z) = M(z). Combining this result and Definition 2.5, we infer -,(M +c N), which is a contradiction. ct T&mrem 4.6. +. orderings.
and -+c are the same mu&set
proot. It follows from Theorem 4.2, Lemmas 4.4 and 4.5. @
pen defiition
but it is not
Volume 15, Number 2
lINFORMAT(ON PROCESSING LETI’ERS
Fig. 1.
process is repeated until it is possible to decide easily whether M is greater than or less than or equal to or incomparable to N. To decrease M, and Nk choose a pair (a, b) in Mk X N, and do the following:
Cu.re 1. If a = b and M,(a) = N,(b), a is removed from Mk and N,. Case 2. If a -
wIdIe “‘possible”do
ebuose a ww pair (a, b) in Maximal(M)X Maximal(N) if bda ar [a=b and M(a)>N(b)) then for ea& x fa succ*(b, N) do removefx, N) en!!Jfy e&if if a 4 b 8t [a=b and M(a)cN(b)] then for each x in m-cc%, M) & remove(x, M) endfor endif
if a=b and M(a)=N(b) thm for~xinsucc(a,M)~
.,
nb_a@, N) : - nb_ant(x, W)- 1 if nb_ant(x, N)-0 then ad@, Maximal(M)) end for
remove@, Maximal(M)) foresebxin~c(b,N)do
nb_ant(x, N): =nb_ant(x, N)- 1 if nb_ant(x, N)=O then ad(x, Maximal(N)) e&for remove (b, Maximal(N)) eQdti eadwhile ivh&xima~(M)=( }th&fMaximal(N)=(
]thenretum(“M=N”) dse return (“M -wN”) end id else H Maximal(N) =f{ } then return (“N +C M”) else return (“M;flN”) end iff
Fig, 2. An algorithm to implement the Dershowitz-Manna ordering. 62
6 September 1982
Volume 15, Number 2
&(y+x*[3
INFORMATION
PROCESSING LElTERS
zEsucc(x)]yiz).
In Case 1, M,,!, = M, - {a}, then Maximal(M,+,)
= Maximal
- (a}
+ (xix Esucc(a)&x Esucc(a’)for afa’). In Case 2, Mk+ I = M, - (xl x E succ*(a)} where succ+(a) = 2:=, s&(a), then Maximal(Mk+,) = Maximal
6 September 1982
a new pair in the body of the loop, then all the elements present in Maximal(M) and Maximal(N) are incomparable. Then easily M#%N and by Lemma 3.4, M# N. The only problem is to prove termination of the algorithm, but it is easy to see that, although Maximal(M) and Maximal(N) can increase, they remain included in M and N which do decrease. ‘Thus the algorithm terminates for any choice which computes a new pair (a, b) at each iteration.
- (a).
This suggests repres&.ing a multiset M as a directed acyclic graph representing the relation succ. Each node contains a triple: the value x of the element, M(x), the number of antecedents nb, ant(x, M) of x in M, i.e., card{y 1succ(y) = x}. If this last number is 0, x belongs to Maximal(M). The arrows deduced by transitivity are not netessary and the algorithm is more efficient if they are not present. Flor examples if the definition of succ is the following minimal one:
y E succ(x) ti y-
Acknowledgement
We would like to thank the referees for their helpful comments and Sriram Atreya for reading a version of the manuscript.
References N. Dershowitz, Ordering for term rewriting systems, Proc. 20th Symp. on Foundations of Computer Science (1979) pp. 123-131. N. Dershowitz and 2. Manna, Proving termination with multi& orderings, Comm. ACM 22 (1979) 465-476. G. Huet and D.C. Oppen, Equations and rewrite rules: a survey, in: R. Book, ed., Formal Languages Perspectives and Open Problems (Academic Press, New York, 1980). lj
63