Multiset Relations: A Tool for Proving Termination
Full text
Multiset Relations: a To ol for Proving Termination ? J.-L. Ruiz-Reina, J.-A. Alonso, M.-J. Hidalgo and F.-J. Martn f jruiz,jalonso,mjoseh,fjesus g ia.es Departamento de Cienias de la Computaion e Inteligenia Artiial. Faultad de Informatia y Estadstia, Universidad de Sevilla Avda. Reina Meredes, s/n. 41012 Sevilla, Spain Abstrat. We present in this pap er a formalization of multiset relations in ACL2, and we show how multisets an b e used to prove non-trivial termination prop erties in ACL2. Intuitively, multisets are sets that admit multiple o urrenes of elements. Every relation on a set A indues a relation on nite multisets over A and it an be shown that the multiset relation indued by a well-founded relation is also well-founded. We proved this prop erty in the ACL2 logi, and used it by funtional instantiation in order to provide well-founded relations to the admissibility test of reursive funtions. We also develop ed a maro defmul , to dene well-founded multiset relations in an easy way. Finally, we present three ase studies where multisets are used to prove non-trivial termination prop erties: a tail-reursive version of Akermann's funtion, a denition of MCarthy's 91 funtion and a pro of of Newman's lemma for abstrat redution relations. Intro dution We present in this pap er a formalization of multiset relations in ACL2, and we show how these relations an b e used to prove non-trivial termination prop erties, providing a to ol for dening relations on nite multisets and showing that these relations are well-founded. Suh well-founded relations are used in the admissibility test for reursive funtions, allowing the user to provide a partiular multiset measure in order to prove termination of reursively dened funtions. Multisets are usually dened in an informal way as \sets with rep eated elements". Dershowitz and Manna [4℄ proved that every well-founded relation on a set A indues a well-founded relation on the set of nite multisets of elements taken from A . We have formalized this theorem using ACL2, and stated it in an abstrat way. This allows to instantiate the theorem to show well-foundedness of onrete multiset relations. We have also develop ed a maro defmul in order to easily make denitions of indued multiset relations. Besides dening the multiset relation indued by a given relation, this maro proves, by funtional instantiation, well-foundedness of the de- ned multiset relation, provided that the original relation is well-founded. The rst setion of this pap er presents how we have formalized and proved wellfoundedness of multiset relations indued by well-founded relations. The seond se- tion presents the maro defmul and it is shown how it an b e used to dene multiset well-founded relations. In the three setions after that, three ase studies of inreasing omplexity are presented, showing how multisets an b e used to prove non-trivial termination prop erties. The rst one is a tail-reursive denition of Akermann's funtion. The seond one shows admissibility of an iterative version of MCarthy's 91 funtion. The third one is a pro of of Newman's lemma ab out abstrat redution relations: terminating and lo ally onuent redution relations are onuent. ? This work has been supp orted by DGES/MEC: Pro jets PB96-0098-C04-04 and PB96-1345
We will assume the reader has a working knowledge of ACL2. The b est intro du- tion to ACL2 is [5℄. Due to the lak of spae, we will skip details of the mehanial pro ofs and we will not mention guards or guards veriation. The omplete b o oks are available on the web in http://www-s.us.es/~jruiz/al2-mul/ . 1 Formalization of multiset relations in ACL2 1.1 Multisets: denitions and prop erties A multiset M over a set A is a funtion from A to the set of natural numb ers. This is a formal way to dene \sets with rep eated elements". Intuitively, M ( x ) is the numb er of opies of x 2 A in M . This multiset is nite if there are nitely many x suh that M ( x ) > 0. The set of all nite multisets over A is denoted as M ( A ). We will use standard set notation to represent multisets. For example, if A = f a; b; g , an example of multiset over A is M = f a; b; b; b g , an abbreviation of the funtion M ( a ) = 1, M ( b ) = 3 and M ( ) = 0. Thus, f a; b; b; b g is idential to the multiset f b; b; a; b g , but distint from the multiset f a; b; b g . Basi op erations on multisets are dened to generalize the same op erations on sets, taking into aount multiple o urrenes of elements: x 2 M means M ( x ) > 0, M N means M ( x ) N ( x ), for all x 2 A , M [ N is the funtion M + N and M n N is the funtion M : N (where x : y is x y if x y and 0 otherwise). For example, f a; b; b; a g [ f ; ; a; b g is the multiset f a; a; a; b; b; b; ; g and f a; b; b; a g n f ; ; a; b g is the multiset f a; b g . Any ordering dened on a set A indues an ordering on multisets over A : given a multiset, a smaller multiset an b e obtained by removing a non-empty subset X and adding elements whih are smaller than some element in X . This onstrution an b e generalized to binary relations in general, not only for partial orderings. This is the formal denition: DEFINITION 1. Given a relation < on a set A , the multiset relation indued by < on M ( A ), denoted as < mul , is dened as N < mul M i there exist X ; Y 2 M ( A ) suh that ; 6 = X M ; N = ( M n X ) [ Y and 8 y 2 Y 9 x 2 X ; y < x . For example, if A = f a; b; ; d; e g and b < a , d < , then f a; b; b; b; b; d; d; d; d; d; e g < mul f a; a; b; ; d; e g by replaing X = f a; g by Y = f b; b; b; d; d; d; d g . It an b e easily shown that if < is a strit ordering, then so is < mul . In suh ase we talk ab out multiset orderings . A relation < on a set A is terminating if there is no innite dereasing 1 sequene x 0 > x 1 > x 2 ::: . An imp ortant prop erty of multiset relations on nite multisets is that they are terminating when the original relation is terminating, as stated by the following theorem: THEOREM 1. Let < b e a terminating relation on a set A , and < mul the multiset relation indued by < on M ( A ). Then < mul is terminating. The ab ove theorem provides a to ol for showing termination of reursive funtion denitions, by using multisets: show that some multiset measure dereases in eah reursive all omparing multisets with resp et to the relation indued by a given terminating relation. In the following subsetion, we explain how we formalized theorem 1 in the ACL2 logi. 1 Although not expliitly, we will supp ose that the relations given here represent some kind of \smaller than" relation.
1.2 Formalization of well-founded multiset relations in ACL2 Let us deal with formalization of terminating relations in ACL2. A restrited notion of terminating relations is built into ACL2 based on the following meta-theorem (axiom of hoie needed): a relation < on a set A is terminating i there exists a funtion F : A ! O r d suh that x < y ) F ( x ) < F ( y ), where O r d is the lass of all ordinals. In this ase, we also say that the relation is wel l-founded . Note that we are denoting the relation on A and the ordering b etween ordinals using the same symb ol < . An arbitrary well-founded relation rel dened on a set of ob jets satisfying a prop erty mp an b e dened in ACL2 as shown b elow (dots are used to omit tehnial details, as in the rest of the pap er): (enapsulate ((mp (x) booleanp) (rel (x y) booleanp) (fn (x) e0-ordinalp)) ... (defthm rel-well-founded-relation-on-mp (and (implies (mp x) (e0-ordinalp (fn x))) (implies (and (mp x) (mp y) (rel x y)) (e0-ord-< (fn x) (fn y)))) :rule-lasses :well-founded-relation)) The prediate mp reognizes the kind of ob jets (alled measures ) that are ordered in a well-founded way by rel . The embedding funtion fn is an order-preserving funtion mapping every measure to an ordinal. One a relation is proved to satisfy these prop erties and the theorem is stored as a well-founded relation rule, it an b e used in the admissibility test for reursive funtions. We all the theorem rel-well- -founded-relation-on-mp ab ove the wel l-foundedness theorem for rel , mp and fn . In ACL2, every partiular well-founded relation has to b e given by means of three funtions (a binary relation, a measure prediate and an emb edding funtion) and the orresp onding well-foundedness theorem for suh funtions. As a partiular ase, when mp is t we an omit any referene to mp in the statement of the orresp onding well-foundedness theorem. See well-founded-relation in the ACL2 manual [6℄. The ab ove notion of termination is restrited: sine only ordinals up to " 0 are formalized in the ACL2 logi, a limitation is imp osed on the maximal order typ e of well-founded relations that an b e formalized. Consequently, our formalization suers from the same restrition (nevertheless, our pro ofs do not dep end on partiular prop erties of " 0 , exept well-foundedness). Let us now deal with formalization of multisets relations. We represent multisets in ACL2 as true lists. Given a prediate (mp x) desribing a set A , nite multisets over A are desrib ed by the following funtion: (defun mp-true-listp (l) (if (atom l) (equal l nil) (and (mp (ar l)) (mp-true-listp (dr l))))) Note that this funtion dep ends on the partiular denition of the prediate mp . With this representation, dierent true lists an represent the same multiset: two true lists represent the same multiset i one is a p ermutation of the other. Thus,
the order in whih the elements app ear in a list is not relevant, but the numb er of o urrenes of an element is imp ortant. This must b e taken into aount, for example, when dening multiset dierene in ACL2 (the funtion remove-one , omitted here, deletes one o urrene of an element from a list, whenever p ossible): (defun multiset-diff (m n) (if (atom n) m (multiset-diff (remove-one (ar n) m) (dr n)))) The denition of < mul given in the preeding subsetion is quite intuitive but, due to its many quantiers, diÆult to implement. Instead, we will use a somewhat restrited denition, based on the following theorem: THEOREM 2. Let < b e a strit ordering on a set A , and M ; N two nite multisets over A . Then N < mul M i M n N 6 = ; and 8 n 2 N n M ; 9 m 2 M n N ; suh that n < m . From the omputational p oint of view, the main advantage of this alternative denition is that the we do not have to searh the multisets X and Y of the original denition b eause we an take M n N and N n M , resp etively. It should b e remarked that this equivalene is true only when < is a strit partial ordering. Take for example, the relation S dened on A = f a; b; ; d g , suh that dS , S b and bS a . In this ase we have f b; ; d g S mul f a; b; g , taking X = f a; b; g and Y = f b; ; d g . Nevertheless, d 2 f b; ; d g n f a; b; g , f a g = f a; b; g n f b; ; d g and it is not true that dS a . Anyway, this is not a severe restrition. Moreover, well-foundedness of < mul also holds when this restrited denition is used, even if the relation < is not transitive, as we will see. Thus, given a dened (or onstrained) binary relation rel , we dene the indued relation on multisets based on this alternative denition: (defun exists-rel-bigger (x l) (ond ((atom l) nil) ((rel x (ar l)) t) (t (exists-rel-bigger x (dr l))))) (defun forall-exists-rel-bigger (l m) (if (atom l) t (and (exists-rel-bigger (ar l) m) (forall-exists-rel-bigger (dr l) m)))) (defun mul-rel (n m) (let ((m-n (multiset-diff m n)) (n-m (multiset-diff n m))) (and (onsp m-n) (forall-exists-rel-bigger n-m m-n)))) Finally, let us see how we an formalize in the ACL2 logi the theorem 1 ab ove, whih states well-foundedness of the relation mul-rel . As said b efore, in order to establish well-foundedness of a relation in ACL2, in addition to the relation ( mul-rel in this ase), we have to give the measure prediate and the emb edding funtion, and then prove the orresp onding well-foundedness theorem. Sine mul-rel is intended to b e dened on multisets of elements satisfying mp , then mp-true-listp is the measure prediate in this ase. Let us supp ose we have dened a suitable emb edding funtion alled map-fn-e0-ord . Then theorem 1 is formalized as follows:
(defthm multiset-extension-of-rel-well-founded (and (implies (mp-true-listp x) (e0-ordinalp (map-fn-e0-ord x))) (implies (and (mp-true-listp x) (mp-true-listp y) (mul-rel x y)) (e0-ord-< (map-fn-e0-ord x) (map-fn-e0-ord y)))) :rule-lasses :well-founded-relation) In the next subsetion we show a suitable denition of map-fn-e0-ord and desrib e some asp ets of the ACL2 pro of of this theorem. 1.3 A pro of of well-foundedness of the multiset relation In the literature [1℄, theorem 1 is usually proved using Konig's lemma: every innite and nitely branhed tree has an innite path. Nevertheless, we have to nd a dierent pro of in ACL2, dening an order-preserving emb edding funtion map-fn-e0-ord from mp-true-listp ob jets to e0-ordinalp ob jets. Thus, our pro of is based on the following result from ordinal theory: given an ordinal , the set M ( ) of nite multisets of elements of (ordinals less than ), ordered by the multiset relation indued by the order b etween ordinals, is order-isomorphi to the ordinal ! and the isomorphism is given by the funtion H where H ( f 1 ;::: ; n g ) = ! 1 + ::: + ! n . This result an b e proved using Cantor's normal form of ordinals and its prop erties. As a by-pro dut, an interesting prop erty ab out multiset well-founded relations an b e dedued. Sine " 0 implies ! ! " 0 = " 0 , this means that one an always prove, in the ACL2 logi, well-foundedness of the multiset relation indued by a given well-founded ACL2 relation (i.e., using emb eddings in the ordinal " 0 ). This is not the ase, for example, of lexiographi pro duts, sine the maximal ordinal typ e of a lexiographi pro dut of two ACL2 well-founded relations may b e greater than " 0 . The isomorphism H ab ove suggests the following denition of the emb edding funtion map-fn-e0-ord : given a multiset of elements satisfying mp , apply fn to every element to obtain a multiset of ordinals. Then apply H to obtain an ordinal less than " 0 . If ordinals are represented in ACL2 notation, then the funtion H an b e easily dened, provided that the funtion fn returns always a non-zero ordinal: the funtion H simply has to sort the ordinals in the multiset and add 0 as the nal dr . These onsiderations lead us to the following denition of the emb edding funtion map-fn-e0-ord . Note that the non-zero restrition on fn is easily overome, dening (the maro) fn1 equal to fn exept for integers, where 1 is added. In this way fn1 returns non-zero ordinals for every measure ob jet and it is order-preserving if and only if fn is. (defun insert-e0-ord-< (x l) (ond ((atom l) (ons x l)) ((not (e0-ord-< x (ar l))) (ons x l)) (t (ons (ar l) (insert-e0-ord-< x (dr l)))))) (defun add1-if-integer (x) (if (integerp x) (1+ x) x))
(defmaro fn1 (x) `(add1-if-integer (fn ,x))) (defun map-fn-e0-ord (l) (if (onsp l) (insert-e0-ord-< (fn1 (ar l)) (map-fn-e0-ord (dr l))) 0)) One map-fn-e0-ord has b een dened, let us now deal with the ACL2 me- hanial pro of of the well-foundedness theorem for mul-rel , mp-true-listp and map-fn-e0-ord as stated at the end of subsetion 1.2 by multiset-extension-of- -rel-well-founded . The rst part of the theorem, whih establishes that (map-fn- -e0-ord x) is an ordinal when (mp-true-listp x) , it is not diÆult, and an b e proved in ACL2 with minor help form the user. The hard part of the theorem is to show that map-fn-e0-ord is order-preserving. Here is an informal pro of sketh: Pro of sketh: Let us denote, for simpliity, the funtions fn1 and map-fn-e0-ord , as f and f mul , and the relation rel , mul-rel and e0-ord-< as < r el , < mul and < , resp etively. Let M and N b e two multisets of mp elements suh that N < mul M . We have to prove that f mul ( N ) < f mul ( M ). We an apply indution on the numb er of elements of N . Note that M an not b e empty, and if N is empty the result trivially holds. So let us supp ose that M and N are not empty. Let f ( x ), f ( y ) b e the biggest elements of f [ N ℄ and f [ M ℄, resp etively. Note that f ( x ) and f ( y ) are the ar elements of f mul ( N ) and f mul ( M ), resp etively. Sine f ( x ) and f ( y ) are ordinals, three ases may arise: 1. f ( x ) < f ( y ). Then, by denition of < , we have f mul ( N ) < f mul ( M ). 2. f ( x ) > f ( y ). This is not p ossible: in that ase x is in N n M and by the multiset relation denition, exists z in M n N suh that x < r el z . Consequently f ( z ) > f ( x ) > f ( y ). This ontradits the fat that f ( y ) is the biggest element of f [ M ℄. 3. f ( x ) = f ( y ). In that ase, x 2 M , sine otherwise it would exist z 2 M n N suh that x < r el z and the same ontradition as in the previous ase app ears. Let M 0 = M n f x g and N 0 = N n f x g . We have N 0 < mul M 0 and, in addition, f mul ( N 0 ) and f mul ( M 0 ) are the dr of f mul ( N ) and f mul ( M ), resp etively. Indution hyp othesis an b e applied here to onlude that f mul ( N 0 ) < f mul ( M 0 ) and therefore f mul ( N ) < f mul ( M ) : ut To lead ACL2 to the ab ove informal pro of sketh, an indution sheme must b e supplied as hint. This is a funtion suggesting suh indution sheme (the funtion max-fn1-list , omitted here, returns the element of a given list with the greatest value of fn1 ): (defun indution-multiset (n m) (delare (xargs :measure (al2::len n))) (ond ((atom n) (if (atom m) 1 2)) ((atom m) 3) (t (let* ((max-m (max-fn1-list m)) (max-n (max-fn1-list n)) (fn1-max-m (fn1 max-m)) (fn1-max-n (fn1 max-n)))
(ond ((equal fn1-max-m fn1-max-n) (if (member max-n m) (indution-multiset (remove-one max-n n) (remove-one max-n m)) 5)) ((e0-ord-< fn1-max-n fn1-max-m) 6) ((e0-ord-< fn1-max-m fn1-max-n) 7) (t 8)))))) Using this indution sheme we proved the following theorem, whih is the hard part of the theorem multiset-extension-of-rel-well-founded . (defthm map-fn-e0-ord-order-preserving (implies (and (mp-true-listp n) (mp-true-listp m) (mul-rel n m)) (e0-ord-< (map-fn-e0-ord n) (map-fn-e0-ord m))) :hints (("Goal" :indut (indution-multiset n m))))) The pro of of this result was not easy: lemmas to handle eah of the ases generated by the ab ove indution sheme have to b e proved, obtaining a mehanial pro of very lose to the previous pro of sketh. See the b o ok multiset.lisp in the web page for details ab out the mehanial pro of. Well-foundedness of mul-rel has b een proved in an abstrat framework, without assuming any partiular prop erties of rel , mp and fn , exept those onerning well-foundedness. This allows us to funtionally instantiate the theorem in order to establish well-foundedness of the multiset relation indued by any given well-founded ACL2 relation. We develop ed a maro named defmul in order to mehanize this pro ess of funtional instantiation. The following setion desrib es the maro. 2 The defmul maro and the multiset b o ok We dened a maro defmul in order to provide a onvenient way to dene the multiset relation indued by a well-founded relation, and to delare the orresp onding wellfounded relation rule. We explain now how defmul is used. Let us supp ose we have a previously dened (or onstrained) relation my-rel , whih is known to b e well-founded on a set of ob jets satisfying the measure prop erty my-mp and justied by the emb edding funtion my-fn . That is to say, the following theorem, using variables x and y , has b een proved (and stored as a well-founded relation rule): (defthm theorem-name (and (implies ( my-mp x ) (e0-ordinalp ( my-fn x ))) (implies (and ( my-mp x ) ( my-mp y ) ( my-rel x y )) (e0-ord-< ( my-fn x ) ( my-fn y )))) :rule-lasses :well-founded-relation)) In order to dene the (well-founded) multiset relation indued by my-rel , we write the following maro all: (defmul ( my-rel theorem-name my-mp my-fn x y ))
The expansion of this maro generates a numb er of ACL2 forms. You may use the ACL2 trans1 ommand in order to view the translated form of a defmul all. The main non-lo al events generated by this maro all are: { the denitions needed for the multiset relation indued by my-rel : funtions existsmy-rel -bigger , forall-existsmy-rel -bigger , and mulmy-rel analogous to the funtions given in subsetion 1.2. { the denition of the multiset measure prop erty, my-mp -true-listp . { the denition of mapmy-fn -e0-ord , the emb edding funtion from multisets to ordinals. { the well-foundedness theorem for mulmy-rel , my-mp -true-listp and mapmy- -fn -e0-ord . This theorem is proved by funtional instantiation from multiset- -extension-of-rel-well-founded and is named multiset-extension-ofmy- -rel -well-founded We exp et defmul to work without assistane from the user. After the ab ove all to defmul , the funtion mulmy-rel is dened as a well-founded relation on multisets of elements satisfying the prop erty my-mp , indued by the well-founded relation my-rel . From this moment on, mulmy-rel an b e used in the admissibility test for reursive funtions to show that the reursion terminates. To know the list of names we need to supply to a defmul all, we have develop ed a to ol to extrat the information from the ACL2 world and print it. This maro is simply alled in this way: (defmul-omponents my-rel ) This is only an informative to ol, not a event. This maro lo oks up the ACL2 world, and returns the list of names that are needed in the defmul all for my-rel . We have divided the results and to ols ab out multisets into two b o oks. The b o ok multiset.lisp ontains the pro of of the theorem multiset-extension-of-rel- -well-founded shown in subsetion 1.3. Names in this b o ok are dened in the pakage MUL . The b o ok defmul.lisp ontains the maro denitions of defmul and defmul- -omponents and inludes the multiset b o ok. See the web page for details. We have also inluded some non-lo al rules in multiset.lisp , whih help ed us to prove the three examples presented in this pap er, and we think they are general enough to assist in other ases. Two relevant examples of these additional results and to ols are: { We have dened the funtion equal-set as an equivalene relation. This funtion implements equality for sets, not for multisets, but it turned out useful in our ase studies b eause it an b e proved to b e a ongruene with resp et to b oth arguments of forall-existsmy-rel -bigger : (defun equal-set (x y) (and (subsetp x y) (subsetp y x))) (defequiv equal-set) (defong equal-set iff forall-existsmy-rel -bigger l m 1) (defong equal-set iff forall-existsmy-rel -bigger l m 2) Sine the latter two ongruene rules dep end on the partiular denition of my-rel , they are generated by every partiular all to defmul .
{ We also dene a meta rule to deal with dierene of multisets represented by lists with nal ommon suÆx. This rule rewrites expressions of the form (multiset-diff (list* x 1 x 2 : : : x m l) (list* y 1 y 2 : : : y k l)) to the following equivalent expression (with resp et to equal-set ): (multiset-diff (list x 1 x 2 : : : x m ) (list y 1 y 2 : : : y k )) This meta rule is very useful 2 when proving that a partiular multiset measure dereases in every reursive all of a funtion: it is \usual" that the multiset obtained measuring the arguments of a reursive all is a list with the same nal part than the multiset obtained measuring the arguments in the original all. 3 Case studies using multiset relations In the next subsetions, we show three examples where well-founded multiset relations play an imp ortant role in the ACL2 pro of of non-trivial termination prop erties. The rst example is taken from [4℄. We use a multiset relation to show termination of a tail-reursive version of Akermann's funtion. In the seond example, also taken from [4℄, we use a multiset relation to admit an iterative version of MCarthy's 91 funtion. The third example is a pro of of Newman's lemma for abstrat redution systems: every terminating and lo ally onuent redution relation has the ChurhRosser prop erty. This last example is part of a larger pro jet develop ed by the authors in order to formalize some asp ets of equational reasoning using ACL2 [8, 9℄. All the examples show one funtion whose termination is proved using a wellfounded multiset relation and a multiset measure funtion. When the funtion is presented for the rst time, its o de is ommented (using semiolons), to emphasize that a suitable measure has still to b e given in order to pass the admissibility test. 3.1 A tail-reursive version of Akermann's funtion The following is the standard denition of Akermann's funtion in ACL2: (defun ak (m n) (delare (xargs :measure (ons (+ (nfix m) 1) (nfix n)))) (ond ((zp m) (+ n 1)) ((zp n) (ak (- m 1) 1)) (t (ak (- m 1) (ak m (- n 1)))))) We now try to dene the following iterative program to ompute Akerman's funtion: ; (defun ak-it-aux (S z) ; (if (endp S) ; z ; (let ((head (first S)) 2 Due to a bug in ACL2 version 2.5, this meta rule fails to b e applied. This is not the ase in version 2.4, where we originally develop ed this work. The problem in version 2.5 an b e xed by inluding a path that will b e inluded in Version 2.6. Thanks to Matt Kaufmann for the path.
;;; (a) A well-founded partial order: (enapsulate ((rel (x y) t) (fn (x) t)) ... (defthm rel-well-founded-relation (and (e0-ordinalp (fn x)) (implies (rel x y) (e0-ord-< (fn x) (fn y)))) :rule-lasses (:well-founded-relation :rewrite)) (defthm rel-transitive (implies (and (rel x y) (rel y z)) (rel x z)))) ;;; (b) A terminating and loally onfluent redution relation: (enapsulate ((legal (x u) boolean) (redue-one-step (x u) element) (reduible (x) boolean) (transform-loal-peak (x) proof)) .... (defun proof-step-p (s) (let ((elt1 (elt1 s)) (elt2 (elt2 s)) (operator (operator s)) (diret (diret s))) (and (r-step-p s) (implies diret (and (legal elt1 operator) (equal (redue-one-step elt1 operator) elt2))) (implies (not diret) (and (legal elt2 operator) (equal (redue-one-step elt2 operator) elt1)))))) (defun equiv-p (x y p) (if (endp p) (equal x y) (and (proof-step-p (ar p)) (equal x (elt1 (ar p))) (equiv-p (elt2 (ar p)) y (dr p))))) (defthm terminating (implies (legal x op) (rel (redue-one-step x op) x))) (defthm loally-onfluent (let ((valley (transform-loal-peak p))) (implies (and (equiv-p x y p) (loal-peak-p p)) (and (steps-valley valley) (equiv-p x y valley)))))) Fig. 1. Assumptions of Newman's lemma partiular shap es ( val leys and loal peaks ): loal-peak-p reognizes pro ofs of the form v x ! u and steps-valley reognizes pro ofs of the form v ! x u . To deal with the assumption of lo al onuene, note that a redution is lo ally onuent i for every lo al p eak pro of there is an equivalent valley pro of. Therefore, in order to state lo al onuene of the general redution relation dened, we assume the existene of a funtion transform-loal-peak whih returns a valley pro of for every lo al p eak pro of. See again item (b) in gure 1 for a statement of this assumed prop erty.
Having established the assumptions, in order to prove Newman's lemma we must show onuene of this general redution relation assumed to b e terminating and lo ally onuent. Instead of onuene, we prove the Churh-Rosser prop erty, whih is equivalent. Therefore, we must prove that for every pro of there exists an equivalent valley pro of, i.e., we have to dene a funtion transform-to-valley and prove that (transform-to-valley p) is a valley pro of equivalent to p . This is the statement of Newman's lemma: (defthm Newman-lemma (let ((valley (transform-to-valley p))) (implies (equiv-p x y p) (and (steps-valley valley) (equiv-p x y valley))))) A suitable denition of transform-to-valley and a pro of of this theorem in ACL2 is shown in the following subsetion. The hard part of the pro of is to show termination of transform-to-valley . It will b e done with the help of a well-founded multiset relation. An ACL2 pro of of Newman's lemma: The pro of ommonly found in the literature [1℄, is done by well-founded indution on the terminating redution relation. Our approah is more onstrutive and is based on a pro of given in [7℄. We have to dene a funtion transform-to-valley whih transforms every pro of in a equivalent valley pro of. For that purp ose, we an use the funtion transform-loal-peak , assumed to transform every lo al p eak pro of in a equivalent valley pro of. Thus, the funtion we need is dened to iteratively apply replae-loal-peak , (whih replaes the rst lo- al p eak subpro of by the equivalent subpro of given by transform-loal-peak ) until there are no lo al p eaks (heked by exists-loal-peak ). The following is the de- nition of transform-to-valley (we omit here the denition of replae-loal-peak and exists-loal-peak ): ;(defun transform-to-valley (p) ; (if (not (exists-loal-peak p)) ; p ; (transform-to-valley (replae-loal-peak p)))) This funtion is not admitted without help from the user. The reason is that when a lo al p eak in a pro of is replaed by an equivalent valley subpro of, the length of the pro of obtained may b e larger than the length of the original pro of. Nevertheless, the key p oint here is that every element of the new subpro of is smaller (w.r.t. the well-founded relation rel ) than the greatest element of the lo al p eak. If we measure a pro of as the multiset of the elements involved in it, then replaing a lo al p eak subpro of by an equivalent valley subpro of, we obtain a pro of with smaller measure with resp et to the well-founded multiset relation indued by rel . The funtion proof-measure returns this measure for a given pro of: it ollets the elt1 elements of every pro of step in a pro of. (defun proof-measure (p) (if (endp p) nil (ons (elt1 (ar p)) (proof-measure (dr p)))))
Using defmul , we dene the well-founded relation mul-rel , indued by the wellfounded relation rel intro dued in the previous subsetion: (defmul (rel rel-well-founded-relation-on-mp t fn x y)) The main result we proved states that the pro of measure dereases (with resp et to the well-founded relation mul-rel ) if a lo al-p eak is replaed by an equivalent valley subpro of: (defthm transform-to-valley-admission (implies (exists-loal-peak p) (mul-rel (proof-measure (replae-loal-peak p)) (proof-measure p))) :rule-lasses nil) With this theorem, admission of the funtion transform-to-valley is now p ossible, giving a suitable hint: (defun transform-to-valley (p) (delare (xargs :measure (proof-measure p) :well-founded-relation mul-rel :hints (("Goal" :use (:instane transform-to-valley-admission))))) (if (not (exists-loal-peak p)) p (transform-to-valley (replae-loal-peak p)))) One transform-to-valley is admitted (whih is the hard part of the theorem), the following two theorems are proved, and this trivially implies Newman's lemma as stated at the end of subsetion 3.3. (defthm equiv-p-x-y-transform-to-valley (implies (equiv-p x y p) (equiv-p x y (transform-to-valley p)))) (defthm valley-transform-to-valley (implies (equiv-p x y p) (steps-valley (transform-to-valley p)))) The mehanial pro of of Newman's lemma is the most diÆult of the three examples presented here. Lemmas have to b e proved to simplify the multiset dierenes app earing in the onjeture generated by the termination pro of of transform-to- -valley . See the le newman.lisp in the web page for details. We also provide b o oks proving deidability of the equivalene relation generated by a terminating and lo ally onuent redution relation. To see how this result an b e exp orted to the study of equational theories, see [9℄.
4 Conlusions We have presented a formalization of multiset relations in ACL2, showing how they an b e used as a to ol for proving non-trivial termination prop erties of reursive fun- tions in ACL2. We have dened the multiset relation indued by a given relation and proved a theorem establishing well-foundedness of the multiset relation indued by a well-founded relation. This theorem is formulated in an abstrat way, so that funtional instantiation an b e used to prove well-foundedness of onrete multiset relations. We have presented also a maro named defmul , implemented to provide a onvenient to ol to dene well-founded multiset relations indued by well-founded relations. This maro allows the denition of these multiset relations in a single step. Three ase studies are presented, to show how this to ol an b e useful in obtaining pro ofs of non-trivial termination prop erties of funtions dened in ACL2. The rst ase study is the denition of a tail-reursive version of Akermann's funtion. The seond is the admissibility of a denition of MCarthy's 91 funtion, and a study of its prop erties. The third is a pro of of Newman's lemma for abstrat redution relations. This work arose as part of a larger pro jet, trying to formalize prop erties of abstrat redution relations, equational theories and term rewriting systems [8, 9℄. In that work, ACL2 is used as a meta-logi to study prop erties of a formal pro of system, namely equational logi. Newman's lemma is a key result needed to prove deidability of equational theories given by omplete term rewriting systems [1℄. One formalized multiset relations and used in the pro of of Newman's lemma, we deided to make a to ol ( defmul ) whih allowed to exp ort the results on multisets to other ontexts. To test this implementation, we applied it to two examples desrib ed in [4℄: Akermann's funtion and MCarthy's 91 funtion. Further work has to b e done to provide a go o d library of lemmas to handle multisets and their op erations. We plan also to improve the use of defmul , in order to provide only the name of the well-founded relation, avoiding to give the funtions, variables and event asso iated with it. Up dated versions of the b o oks will b e in the web page. The examples presented here are all of a theoretial nature. Nevertheless, a remark given at the end of setion I I I in [4℄, p ointing an heuristi pro edure for proving termination of lo ops using multisets, suggests that this kind of orderings ould b e applied to a wider lass of termination problems and that the searh for a suitable multiset measure ould b e mehanized to some extent. Another appliation of multisets orderings is to provide the basis for some pro ofs of termination of term rewriting systems [1℄. We intend to make further researh following these two lines. Referenes 1. Baader, F., and Nipkow, T. Term rewriting and al l that . Cambridge University Press, 1998. 2. Brok, B. defstruture for ACL2 version 2.0. Tehnial Rep ort, 1997. 3. Cowles, J. Knuth's generalization of MCarthy's 91 funtion. In Computer-Aided Reasoning: ACL2 Case Studies , M. Kaufmann, P. Manolios, and J S. Mo ore, Eds. Kluwer Aademi Publishers, 2000, h. 17. 4. Dershowitz, N., and Manna, Z. Proving termination with multiset orderings. In Annual International Col loquium on Automata, Languages and Programming (1979), H. Maurer, Ed., no. 71 in LNCS, Springer-Verlag, pp. 188{202.
5. Kaufmann, M., Manolios, P., and Moore, J S. Computer-Aided Reasoning: An Approah . Kluwer Aademi Publishers, 2000. 6. Kaufmann, M., and Moore, J S. ACL2 version 2.5. http://www.s.utexas.edu/users/mo ore- /al2/al2-do .html, 2000. 7. Klop, J. Term rewriting systems. Handbook of Logi in Computer Siene (1992). 8. Ruiz-Reina, J., Alonso, J., Hidalgo, M., and Mart n, F. http://www-s.us.es/ ~ jruiz/al2rewr. Formalizing equational reasoning in the ACL2 theorem prover, 2000. 9. Ruiz-Reina, J., Alonso, J., Hidalgo, M., and Mart n, F. Formalizing rewriting in the ACL2 theorem prover. In Proeedings of AISC'2000 (Fifth International Conferene Artiial Intel ligene and Symboli Computation) (to app ear), LNCS, Springer Verlag.