File: simplify.lisp

package info (click to toggle)
acl2 8.5dfsg-5
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 991,452 kB
  • sloc: lisp: 15,567,759; javascript: 22,820; cpp: 13,929; ansic: 12,092; perl: 7,150; java: 4,405; xml: 3,884; makefile: 3,507; sh: 3,187; ruby: 2,633; ml: 763; python: 746; yacc: 723; awk: 295; csh: 186; php: 171; lex: 154; tcl: 49; asm: 23; haskell: 17
file content (849 lines) | stat: -rw-r--r-- 29,340 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
; Arithmetic-5 Library
; Written by Robert Krug
; Copyright/License:
; See the LICENSE file at the top level of the arithmetic-5 library.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; simplify.lisp
;;;
;;;
;;; This book contains three types of rules:
;;; 1. simplify-terms-such-as-ax+bx-rel-0
;;;
;;;    assuming a, b, and x are acl2-numberp
;;;    (equal (+ x (* b x)) 0) ==> (or (equal x 0) (equal (+ 1 b) 0))
;;;    (equal (+ (* a x) (* b x)) 0) ==> (or (equal x 0) (equal (+ a b) 0))
;;;
;;; 2. Cancel "like" terms on either side of an equality or inequality.
;;;
;;;    A couple of simple example of cancelling like terms:
;;;
;;;    (equal (+ a (* 2 c) d)
;;;           (+ b c d))
;;;    ===>
;;;    (equal (+ a c)
;;;           b)
;;;
;;;    Note that just as for normailze.liso, there are two distinct
;;;    behaviours for cancelling like factors:
;;;
;;;    Under the default theory, gather-exponents:
;;;
;;;    (equal (* a (expt b n))
;;;           (* c (expt b m)))
;;;    ===>
;;;    (equal (* a (expt b (- n m)))
;;;           c)
;;;
;;;    Under the other theory, scatter-exponents
;;;
;;;    (equal (* a (expt b n))
;;;           (* c (expt b m)))
;;;    ===>
;;;    no change
;;;
;;;    Under both theories:
;;;
;;;    (equal (* a (expt b (* 2 n)))
;;;           (* c (expt b n)))
;;;    ===>
;;;    (equal (* a (expt b n))
;;;           (* c))
;;;
;;;    (equal (* a (expt c 2) d)
;;;           (* b c d))
;;;    ===>
;;;    (equal (* a c)
;;;           b)
;;;
;;; 3. Move "negative" terms form one side of an equality or
;;;    inequality to the other.
;;;
;;;    A couple of simple example of moving a negative term to the
;;;    other side:
;;;
;;;    (< (+ a (- b) c)
;;;       (+ d e))
;;;    ===>
;;;    (< (+ a c)
;;;       (+ b d e))
;;;
;;;    Under the default theory, gather-exponents, we do not move
;;;    ``negative'' exponents to the other side.  It has proved too
;;;    dificult to prevent loops in the general case, and so we avoid
;;;    the issue entirely.  We could certainly catch the ``simple''
;;;    cases, but have not done so.
;;;
;;;    Under the other theory, scatter-exponents:
;;;
;;;    (equal (* a (/ b) c)
;;;           (* d e))
;;;    ===>
;;;    (equal (* a c)
;;;           (* b d e))
;;;
;;;    (equal (* a (expt b (- n)) c)
;;;           (* d (expt b m)))
;;;    ===>
;;;    (equal (* a c)
;;;           (* d (expt b m) (expt b n)))
;;;
;;;    Note that for multiplication, division or exponentiation to a negative
;;;    power is considered to be negative.
;;;
;;; See common.lisp for a short description of the general strategy
;;; used in the last two of these types.
;;;
;;; The certification of this book could probably be sped up a good
;;; bit.  It is rather slow.
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(in-package "ACL2")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(include-book "common")

(local
 (include-book "simplify-helper"))

(local
 (include-book "basic"))

(table acl2-defaults-table :state-ok t)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(local
 (defthm rewrite-equal-<-to-iff-<
     (equal (equal (< a b)
                   (< c d))
            (iff (< a b)
                 (< c d)))))

(local
 (encapsulate
  ()

  (local (include-book "../../support/top"))

  (defthm equal-*-/-1
      (equal (equal (* (/ x) y) z)
             (if (equal (fix x) 0)
                 (equal z 0)
               (and (acl2-numberp z)
                    (equal (fix y) (* x z))))))

  (defthm equal-*-/-2
      (equal (equal (* y (/ x)) z)
             (if (equal (fix x) 0)
                 (equal z 0)
               (and (acl2-numberp z)
                    (equal (fix y) (* z x))))))

  ))

(local
 (encapsulate
  ()

  (local (include-book "../../support/top"))

  (defthm equal-*-1
    (implies (not (equal (fix x) 0))
      (equal (equal (* x y) (* x z))
	     (equal (fix y) (fix z)))))

  (defthm equal-*-2
    (implies (not (equal (fix x) 0))
      (equal (equal (* y x) (* z x))
	     (equal (fix y) (fix z)))))

  ))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; simplify-terms-such-as-ax+bx-rel-0

;;; assuming a, b, and x are acl2-numberp
;;; (equal (+ x (* b x)) 0) ==> (or (equal x 0) (equal (+ 1 b) 0))
;;; (equal (+ (* a x) (* b x)) 0) ==> (or (equal x 0) (equal (+ a b) 0))

;;; We are, in affect, undoing distributivity when the term is being
;;; compared to zero.

;;; In the example
;;; (thm
;;;  (implies (and (acl2-numberp a)
;;;                (acl2-numberp b)
;;;                (acl2-numberp x))
;;;           (equal (+ (* a x) (* b x))
;;;                  0))
;;;  :otf-flg t)
;;; simplify-terms-such-as-ax+bx-rel-0-fn returns
;;; ((COMMON . X)
;;;  (REMAINDER BINARY-+ A B))

(defun simplify-terms-such-as-ax+bx-rel-0-fn (sum)
  (declare (xargs :guard t))

  ;; We look for any factors common to each addend of sum.  If we
  ;; find any, we return a binding list with common bound to the
  ;; product of these factors, and remainder bound to the original
  ;; sum but with the common factors removed from each addend.

  (if (eq (fn-symb sum) 'BINARY-+)
      (let ((common-factors (common-factors (factors (arg1 sum))
                                            (arg2 sum))))
        (if common-factors
            (let ((common (make-product common-factors))
                  (remainder (remainder common-factors sum)))
              (list (cons 'common common)
                    (cons 'remainder remainder)))
          nil))
    nil))

;;; assuming a, b, and x are acl2-numberp
;;; (equal (+ x (* b x)) 0) ==> (or (equal x 0) (equal (+ 1 b) 0))
;;; (equal (+ (* a x) (* b x)) 0) ==> (or (equal x 0) (equal (+ a b) 0))

(defthm simplify-terms-such-as-ax+bx-=-0
    (implies (and (bind-free
                   (simplify-terms-such-as-ax+bx-rel-0-fn sum)
                   (common remainder))
                  (acl2-numberp common)
                  (acl2-numberp remainder)
                  (equal sum
                         (* common remainder)))
             (equal (equal sum 0)
                    (or (equal common 0)
                        (equal remainder 0)))))

(defthm simplify-terms-such-as-ax+bx-<-0-rational-remainder
    (implies (and (bind-free
                   (simplify-terms-such-as-ax+bx-rel-0-fn sum)
                   (common remainder))
                  (acl2-numberp common)
                  (real/rationalp remainder)
                  (equal sum
                         (* common remainder)))
             (equal (< sum 0)
                    (cond ((< common 0)
                           (< 0 remainder))
                          ((< 0 common)
                           (< remainder 0))
                          (t
                           nil)))))

(defthm simplify-terms-such-as-ax+bx-<-0-rational-common
    (implies (and (bind-free
                   (simplify-terms-such-as-ax+bx-rel-0-fn sum)
                   (common remainder))
                  (real/rationalp common)
                  (acl2-numberp remainder)
                  (equal sum
                         (* common remainder)))
             (equal (< sum 0)
                    (cond ((< common 0)
                           (< 0 remainder))
                          ((< 0 common)
                           (< remainder 0))
                          (t
                           nil))))
    :hints (("Goal" :use (:instance simplify-terms-such-as-ax+bx-<-0-rational-remainder
				    (common remainder)
				    (remainder common)))))

(defthm simplify-terms-such-as-0-<-ax+bx-rational-remainder
    (implies (and (bind-free
                   (simplify-terms-such-as-ax+bx-rel-0-fn sum)
                   (common remainder))
                  (acl2-numberp common)
                  (real/rationalp remainder)
                  (equal sum
                         (* common remainder)))
             (equal (< 0 sum)
                    (cond ((< 0 common)
                           (< 0 remainder))
                          ((< common 0)
                           (< remainder 0))
                          (t
                           nil)))))

(defthm simplify-terms-such-as-0-<-ax+bx-rational-common
    (implies (and (bind-free
                   (simplify-terms-such-as-ax+bx-rel-0-fn sum)
                   (common remainder))
                  (real/rationalp common)
                  (acl2-numberp remainder)
                  (equal sum
                         (* common remainder)))
             (equal (< 0 sum)
                    (cond ((< 0 common)
                           (< 0 remainder))
                          ((< common 0)
                           (< remainder 0))
                          (t
                           nil))))
    :hints (("Goal" :use (:instance simplify-terms-such-as-0-<-ax+bx-rational-remainder
				    (common remainder)
				    (remainder common)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; simplify-sums-equal and simplify-sums-<

;;; We wish to cancel like addends from both sides of af an equality
;;; or inequality.  We scan the sums on either side of the relation,
;;; and construct a pair of addend-info-lists.  We then find the first
;;; match in these lists and cancel it.

(defun addend-val (addend)
  (declare (xargs :guard t))

  ;; We wish to subtract the ``smaller'' of two matching addends.
  ;;

  (cond ((variablep addend)
	 (list 0 1 0))
	((constant-p addend)
	 (let ((val (unquote addend)))
	   (if (rationalp val)		; OK, no constant reals
	       (list 0 0 (abs val))
	     (list 0 0 1))))
	((eq (ffn-symb addend) 'UNARY--)
	 (addend-val (arg1 addend)))
	((and (eq (ffn-symb addend) 'BINARY-*)
	      (constant-p (arg1 addend)))
	 (let ((val (unquote (arg1 addend))))
	   (if (rationalp val)
	       (list (abs val) 0 0)
	     (list 1 0 0))))
	(t
	 (list 0 1 0))))

(defun addend-info-entry (x)
  (declare (xargs :guard t))
  (list (addend-pattern x) (addend-val x) x))

(defun addend-info-list (x)
  (declare (xargs :guard t))
  (if (eq (fn-symb x) 'BINARY-+)
      (cons (addend-info-entry (arg1 x))
            (addend-info-list (arg2 x)))
    (list (addend-info-entry x))))

(local
 (encapsulate
  ()

  (local
   (defthm temp-1
     (good-val-triple-p (addend-val x))))

  (defthm addend-info-list-thm
    (info-list-p (addend-info-list x)))
  ))

(defun assoc-addend (x info-list)
  (declare (xargs :guard (info-list-p info-list)))
  (cond ((endp info-list)
         nil)
        ((matching-addend-patterns-p x (caar info-list))
         (car info-list))
        (t
         (assoc-addend x (cdr info-list)))))

(defun first-match-in-addend-info-lists (info-list1 info-list2 mfc state)
  (declare (xargs :guard (and (info-list-p info-list1)
                              (info-list-p info-list2))
		  :guard-hints (("Goal" :in-theory (disable good-val-triple-p
							    negate-match
							    val-<)))))
  (if (endp info-list1)
      nil
    (let ((temp (assoc-addend (car (car info-list1)) info-list2)))
      (if temp
	  (cond ((and ;; We want the ``smaller'' match
		      (val-< (cadr (car info-list1))
			     (cadr temp))
		      ;; prevent various odd loops
		      (stable-under-rewriting-sums (negate-match
						    (caddr (car info-list1)))
						   mfc state))
		 (list (cons 'x (negate-match (caddr (car info-list1))))))
		((stable-under-rewriting-sums (negate-match
					       (caddr temp))
					      mfc state)
		 (list (cons 'x (negate-match (caddr temp)))))
		(t
		 (first-match-in-addend-info-lists (cdr info-list1) info-list2
						   mfc state)))
        (first-match-in-addend-info-lists (cdr info-list1) info-list2
					  mfc state)))))

(defun find-matching-addends (lhs rhs mfc state)
  (declare (xargs :guard t
		  :verify-guards nil))
  (let* ((info-list1 (addend-info-list lhs))
         (info-list2 (if info-list1
			 (addend-info-list rhs)
		       nil)))
    (if info-list2
	(first-match-in-addend-info-lists info-list1 info-list2
					  mfc state)
      nil)))

 (verify-guards find-matching-addends)

(defthm simplify-sums-equal
    (implies (and (syntaxp (not (quotep lhs)))
                  (syntaxp (not (quotep rhs)))
		  (syntaxp (in-term-order-+ lhs mfc state))
		  (syntaxp (in-term-order-+ rhs mfc state))
                  (acl2-numberp lhs)
                  (acl2-numberp rhs)
                  (bind-free
                   (find-matching-addends lhs rhs mfc state)
                   (x)))
             (equal (equal lhs rhs)
                    (equal (+ x lhs) (+ x rhs)))))

(local
 (in-theory (disable simplify-sums-equal)))

(defthm simplify-sums-<
    (implies (and (syntaxp (not (quotep lhs)))
                  (syntaxp (not (quotep rhs)))
                  (syntaxp (in-term-order-+ lhs mfc state))
		  (syntaxp (in-term-order-+ rhs mfc state))
                  (acl2-numberp lhs)
                  (acl2-numberp rhs)
                  (bind-free
                   (find-matching-addends lhs rhs mfc state)
                   (x)))
             (equal (< lhs rhs)
                    (< (+ x lhs) (+ x rhs)))))

(local
 (in-theory (disable simplify-sums-<)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun negative-addend-p (x)
  (declare (xargs :guard t))
  (or (and (eq (fn-symb x) 'UNARY--)
           (or (variablep (arg1 x))
              (not (equal (ffn-symb (arg1 x)) 'UNARY--))))
      (and (eq (fn-symb x) 'BINARY-*)
           (rational-constant-p (arg1 x))
           (< (unquote (arg1 x)) 0))))

(defun find-negative-addend1 (x mfc state)
  (declare (xargs :guard t))
  (cond ((not (eq (fn-symb x) 'BINARY-+))
         (if (and (negative-addend-p x)
		  ;; prevent various odd loops
		  (stable-under-rewriting-sums (negate-match x)
					       mfc state))
             (list (cons 'x (negate-match x)))
           nil))
        ((and (negative-addend-p (arg1 x))
	      (stable-under-rewriting-sums (negate-match (arg1 x))
					   mfc state))
         (list (cons 'x (negate-match (arg1 x)))))
        ((eq (fn-symb (arg2 x)) 'BINARY-+)
         (find-negative-addend1 (arg2 x) mfc state))
        ((and (negative-addend-p (arg2 x))
	      (stable-under-rewriting-sums (negate-match (arg2 x))
					   mfc state))
         (list (cons 'x (negate-match (arg2 x)))))
        (t
         nil)))

(defun find-negative-addend (lhs rhs mfc state)
  (declare (xargs :guard t))
  (let ((temp1 (find-negative-addend1 lhs mfc state)))
    (if temp1
        temp1
      (let ((temp2 (find-negative-addend1 rhs mfc state)))
        (if temp2
            temp2
          nil)))))

(defthm prefer-positive-addends-equal
    (implies (and (acl2-numberp lhs)
                  (acl2-numberp rhs)
                  (syntaxp (in-term-order-+ lhs mfc state))
		  (syntaxp (in-term-order-+ rhs mfc state))
                  (syntaxp (or (equal (fn-symb lhs) 'BINARY-+)
                               (equal (fn-symb rhs) 'BINARY-+)))
                  (bind-free
                   (find-negative-addend lhs rhs mfc state)
                   (x)))
             (equal (equal lhs rhs)
                    (equal (+ x lhs) (+ x rhs)))))

(local
 (in-theory (disable prefer-positive-addends-equal)))


(defthm prefer-positive-addends-<
    (implies (and (acl2-numberp lhs)
                  (acl2-numberp rhs)
                  (syntaxp (in-term-order-+ lhs mfc state))
		  (syntaxp (in-term-order-+ rhs mfc state))
                  (syntaxp (or (equal (fn-symb lhs) 'BINARY-+)
                               (equal (fn-symb rhs) 'BINARY-+)))
                  (bind-free
                   (find-negative-addend lhs rhs mfc state)
                   (x)))
             (equal (< lhs rhs)
                    (< (+ x lhs) (+ x rhs)))))

(local
 (in-theory (disable prefer-positive-addends-<)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defthm simplify-products-gather-exponents-equal
    (implies (and (acl2-numberp lhs)
                  (acl2-numberp rhs)
                  (syntaxp (not (quotep lhs)))
                  (syntaxp (not (quotep rhs)))
                  (syntaxp (in-term-order-* lhs mfc state))
		  (syntaxp (in-term-order-* rhs mfc state))
                  (bind-free
                   (find-matching-factors-gather-exponents lhs rhs mfc state)
                   (x))
		  ;; Something is not right if x = +/-1.  This will
		  ;; presumably be rewritten away later.  We abort
		  ;; for now.
		  (syntaxp (not (equal x ''1)))
		  (syntaxp (not (equal x ''-1)))
                  (case-split (acl2-numberp x))
		  (case-split (not (equal x 0))))
             (equal (equal lhs rhs)
		    (equal (* x lhs) (* x rhs)))))

(local
 (in-theory (disable simplify-products-gather-exponents-equal)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defthm simplify-products-gather-exponents-<
    (implies (and (acl2-numberp lhs)
                  (acl2-numberp rhs)
                  (syntaxp (not (quotep lhs)))
                  (syntaxp (not (quotep rhs)))
                  (syntaxp (in-term-order-* lhs mfc state))
		  (syntaxp (in-term-order-* rhs mfc state))
                  (bind-free
                   (find-rational-matching-factors-gather-exponents lhs rhs
                                                                    mfc state)
                   (x))
		  ;; Something is not right if x = +/-1.  This will
		  ;; presumably be rewritten away later.  We abort
		  ;; for now.
		  (syntaxp (not (equal x ''1)))
		  (syntaxp (not (equal x ''-1)))
                  (case-split (real/rationalp x))
		  (case-split (not (equal x 0))))
             (equal (< lhs rhs)
                    (cond ((< 0 x)
                           (< (* x lhs) (* x rhs)))
                          (t
                           (< (* x rhs) (* x lhs)))))))

(local
 (in-theory (disable simplify-products-gather-exponents-<)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defthm simplify-products-scatter-exponents-equal
    (implies (and (acl2-numberp lhs)
                  (acl2-numberp rhs)
                  (syntaxp (not (quotep lhs)))
                  (syntaxp (not (quotep rhs)))
                  (syntaxp (in-term-order-* lhs mfc state))
		  (syntaxp (in-term-order-* rhs mfc state))
                  (bind-free
                   (find-matching-factors-scatter-exponents lhs rhs mfc state)
                   (x))
		  (syntaxp (not (equal x ''1)))
		  (syntaxp (not (equal x ''-1)))
                  (case-split (acl2-numberp x))
		  (case-split (not (equal x 0))))
             (equal (equal lhs rhs)
		    (equal (* x lhs) (* x rhs)))))

(local
 (in-theory (disable simplify-products-scatter-exponents-equal)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defthm simplify-products-scatter-exponents-<
    (implies (and (acl2-numberp lhs)
                  (acl2-numberp rhs)
                  (syntaxp (not (quotep lhs)))
                  (syntaxp (not (quotep rhs)))
                  (syntaxp (in-term-order-* lhs mfc state))
		  (syntaxp (in-term-order-* rhs mfc state))
                  (bind-free
                   (find-rational-matching-factors-scatter-exponents lhs rhs
                                                                    mfc state)
                   (x))
		  (syntaxp (not (equal x ''1)))
		  (syntaxp (not (equal x ''-1)))
                  (case-split (real/rationalp x))
		  (case-split (not (equal x 0))))
             (equal (< lhs rhs)
                    (cond ((< 0 x)
                           (< (* x lhs) (* x rhs)))
                          (t
                           (< (* x rhs) (* x lhs)))))))

(local
 (in-theory (disable simplify-products-scatter-exponents-<)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#|
;;; We do not include theorems such as the below
;;; prefer-positive-exponents-gather-exponents-equal because it is too
;;; difficult to prevent looping.

;;; Add an example here!!!

(defthm prefer-positive-exponents-gather-exponents-equal
    (implies (and (acl2-numberp lhs)
                  (acl2-numberp rhs)
                  (syntaxp (in-term-order-* lhs mfc state))
		  (syntaxp (in-term-order-* rhs mfc state))
                  (bind-free
                   (find-rational-negative-factor-gather-exponents lhs rhs
                                                                   mfc state)
                   (x))
                  (case-split (rationalp x))
		  (case-split (not (equal x 0))))
             (equal (equal lhs rhs)
		    (equal (* (/ x) lhs) (* (/ x) rhs)))))
|#

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; This should probably be refactored and cleaned up.  Do I have
;;; divisive-factor-p defined anywhere already?  Am I consistent in
;;; my notions?  Is it consistent with invert-match?

(defun find-divisive-factor-scatter-exponents2 (x mfc state)
  (declare (xargs :guard t))
  (cond ((or (variablep x)
             (fquotep x))
         nil)
	;; Is this correct?
        ((eq (ffn-symb x) 'UNARY--)
	 (find-divisive-factor-scatter-exponents2 (arg1 x) mfc state))
        ((eq (ffn-symb x) 'UNARY-/)
	 (if (stable-under-rewriting-products (invert-match x) mfc state)
	     (list (cons 'x (invert-match x)))
	   nil))
        ((eq (ffn-symb x) 'EXPT)
         (cond ((eq (fn-symb (arg1 x)) 'UNARY-/)
                (if (stable-under-rewriting-products (invert-match x) mfc state)
		    (list (cons 'x (invert-match x)))
		  nil))
               ((and (quotep (arg1 x))
		     (consp (cdr (arg1 x)))
                     (not (integerp (cadr (arg1 x))))
                     (rationalp (cadr (arg1 x))) ; OK, no realp constants
                     (eql (numerator (cadr (arg1 x))) 1))
                (if (stable-under-rewriting-products (invert-match x) mfc state)
		    (list (cons 'x (invert-match x)))
		  nil))
               ((eq (fn-symb (arg2 x)) 'UNARY--)
                (if (stable-under-rewriting-products (invert-match x) mfc state)
		    (list (cons 'x (invert-match x)))
		  nil))
               ((and (eq (fn-symb (arg2 x)) 'BINARY-*)
                     (rational-constant-p (arg1 (arg2 x)))
                     (< (unquote (arg1 (arg2 x))) 0))
                (if (stable-under-rewriting-products (invert-match x) mfc state)
		    (list (cons 'x (invert-match x)))
		  nil))
               (t
                nil)))
        ((eq (ffn-symb x) 'BINARY-*)
         (let ((temp (find-divisive-factor-scatter-exponents2 (arg1 x)
							      mfc state)))
           (if temp
               temp
             (find-divisive-factor-scatter-exponents2 (arg2 x)
						      mfc state))))
        (t
         nil)))

(defun find-divisive-factor-scatter-exponents1 (x mfc state)
  (declare (xargs :guard t))
  (cond ((or (variablep x)
             (fquotep x))
         nil)
        ((eq (ffn-symb x) 'BINARY-+)
         (let ((temp (find-divisive-factor-scatter-exponents2 (arg1 x)
							      mfc state)))
           (if temp
               temp
             (find-divisive-factor-scatter-exponents1 (arg2 x)
						      mfc state))))
        (t
         (find-divisive-factor-scatter-exponents2 x mfc state))))

(defun find-divisive-factor-scatter-exponents (lhs rhs mfc state)
  (declare (xargs :guard t))
  (let ((temp1 (find-divisive-factor-scatter-exponents1 lhs mfc state)))
    (if temp1
        temp1
      (let ((temp2 (find-divisive-factor-scatter-exponents1 rhs mfc state)))
        (if temp2
            temp2
          nil)))))

(defthm prefer-positive-exponents-scatter-exponents-equal
    (implies (and (acl2-numberp lhs)
                  (acl2-numberp rhs)
;                  (syntaxp (not (equal rhs ''0)))
;                  (syntaxp (not (equal lhs ''0)))
                  (syntaxp (in-term-order-* lhs mfc state))
		  (syntaxp (in-term-order-* rhs mfc state))
                  (bind-free
                   (find-divisive-factor-scatter-exponents lhs rhs
							   mfc state)
                   (x))
		  (syntaxp (not (equal x ''1)))
		  (syntaxp (not (equal x ''-1)))
                  (case-split (acl2-numberp x))
		  (case-split (not (equal x 0))))
             (equal (equal lhs rhs)
		    (equal (* x lhs) (* x rhs)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; This should probably be refactored and cleaned up.  Do I have
;;; divisive-factor-p defined anywhere already?

(defun find-rational-divisive-factor-scatter-exponents2 (x mfc state)
  (declare (xargs :guard t))
  (cond ((or (variablep x)
             (fquotep x))
         nil)
        ((eq (ffn-symb x) 'UNARY--)
	 (find-rational-divisive-factor-scatter-exponents2 (arg1 x) mfc state))
        ((eq (ffn-symb x) 'UNARY-/)
	 (if (and (proveably-real/rational 'x `((x . ,x)) mfc state)
		  (stable-under-rewriting-products (invert-match x) mfc state))
	     (list (cons 'x (invert-match x)))
	   nil))
        ((eq (ffn-symb x) 'EXPT)
         (cond ((eq (fn-symb (arg1 x)) 'UNARY-/)
                (if (and (proveably-real/rational 'x `((x . ,x)) mfc state)
			 (stable-under-rewriting-products (invert-match x) mfc state))
		    (list (cons 'x (invert-match x)))
		  nil))
               ((and (quotep (arg1 x))
		     (consp (cdr (arg1 x)))
                     (not (integerp (cadr (arg1 x))))
                     (rationalp (cadr (arg1 x))) ; OK, no realp constants
                     (eql (numerator (cadr (arg1 x))) 1))
                (if (and (proveably-real/rational 'x `((x . ,x)) mfc state)
			 (stable-under-rewriting-products (invert-match x) mfc state))
		    (list (cons 'x (invert-match x)))
		  nil))
               ((eq (fn-symb (arg2 x)) 'UNARY--)
                (if (and (proveably-real/rational 'x `((x . ,x)) mfc state)
			 (stable-under-rewriting-products (invert-match x) mfc state))
		    (list (cons 'x (invert-match x)))
		  nil))
               ((and (eq (fn-symb (arg2 x)) 'BINARY-*)
                     (rational-constant-p (arg1 (arg2 x)))
                     (< (unquote (arg1 (arg2 x))) 0))
                (if (and (proveably-real/rational 'x `((x . ,x)) mfc state)
			 (stable-under-rewriting-products (invert-match x) mfc state))
		    (list (cons 'x (invert-match x)))
		  nil))
               (t
                nil)))
        ((eq (ffn-symb x) 'BINARY-*)
         (let ((temp (find-rational-divisive-factor-scatter-exponents2 (arg1 x) mfc state)))
           (if temp
               temp
             (find-rational-divisive-factor-scatter-exponents2 (arg2 x) mfc state))))
        (t
         nil)))

(defun find-rational-divisive-factor-scatter-exponents1 (x mfc state)
  (declare (xargs :guard t))
  (cond ((or (variablep x)
             (fquotep x))
         nil)
        ((eq (ffn-symb x) 'BINARY-+)
         (let ((temp (find-rational-divisive-factor-scatter-exponents2 (arg1 x) mfc state)))
           (if temp
               temp
             (find-rational-divisive-factor-scatter-exponents1 (arg2 x) mfc state))))
        (t
         (find-rational-divisive-factor-scatter-exponents2 x mfc state))))

(defun find-rational-divisive-factor-scatter-exponents (lhs rhs mfc state)
  (declare (xargs :guard t))
  (let ((temp1 (find-rational-divisive-factor-scatter-exponents1 lhs mfc state)))
    (if temp1
        temp1
      (let ((temp2 (find-rational-divisive-factor-scatter-exponents1 rhs mfc state)))
        (if temp2
            temp2
          nil)))))

(defthm prefer-positive-exponents-scatter-exponents-<
    (implies (and (acl2-numberp lhs)
                  (acl2-numberp rhs)
;                  (syntaxp (not (equal rhs ''0)))
;                  (syntaxp (not (equal lhs ''0)))
                  (syntaxp (in-term-order-* lhs mfc state))
		  (syntaxp (in-term-order-* rhs mfc state))
                  (bind-free
                   (find-rational-divisive-factor-scatter-exponents lhs rhs
                                                                    mfc state)
                   (x))		  (syntaxp (not (equal x ''1)))
		  (syntaxp (not (equal x ''-1)))
                  (case-split (real/rationalp x))
		  (case-split (not (equal x 0))))
             (equal (< lhs rhs)
                    (cond ((< 0 x)
                           (< (* x lhs) (* x rhs)))
                          (t
                           (< (* x rhs) (* x lhs)))))))

(defthm prefer-positive-exponents-scatter-exponents-<-2
    (implies (and (real/rationalp lhs)
                  (real/rationalp rhs)
;                  (syntaxp (not (equal rhs ''0)))
;                  (syntaxp (not (equal lhs ''0)))
                  (syntaxp (in-term-order-* lhs mfc state))
		  (syntaxp (in-term-order-* rhs mfc state))
                  (bind-free
                   (find-divisive-factor-scatter-exponents lhs rhs
							   mfc state)
                   (x))
		  (syntaxp (not (equal x ''1)))
		  (syntaxp (not (equal x ''-1)))
                  (case-split (acl2-numberp x))
		  (case-split (not (equal x 0))))
             (equal (< lhs rhs)
                    (cond ((< 0 x)
                           (< (* x lhs) (* x rhs)))
                          (t
                           (< (* x rhs) (* x lhs))))))
    :hints (("Goal" :in-theory (enable big-helper-2))))