File: expt.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 (1096 lines) | stat: -rw-r--r-- 30,311 bytes parent folder | download | duplicates (3)
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
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
; Arithmetic-5 Library
; Written by Robert Krug
; Copyright/License:
; See the LICENSE file at the top level of the arithmetic-5 library.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; expt.lisp
;;;
;;; This book contains rules for reasoning about expt.
;;;
;;; It contains the following sections:
;;;
;;; 1. Type-prescription rules for expt.
;;; 2. Simple rules about expt.
;;; 3. Normalizing expt expressions
;;; 4. Some miscelaneous rules about expt.
;;; 5. Linear rules about expt.
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

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

(in-package "ACL2")

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

(include-book "building-blocks")

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

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

(local
 (include-book "types"))

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

;;; 1. Type-prescription rules for expt.

(defthm expt-type-prescription-rationalp-base
  (implies (rationalp x)
           (rationalp (expt x n)))
  :rule-classes (:type-prescription :generalize))

#+non-standard-analysis
(defthm expt-type-prescription-realp-base
  (implies (real/rationalp x)
           (real/rationalp (expt x n)))
  :rule-classes (:type-prescription :generalize))

(defthm expt-type-prescription-integerp-base
  (implies (and (<= 0 n)
                (integerp x))
           (integerp (expt x n)))
  :rule-classes (:type-prescription :generalize))

(defthm expt-type-prescription-integerp-base-a
  (implies (and (integerp n)
		(< n 0)
                (integerp x)
		(< 1 x))
           (and (rationalp (expt x n))
		(not (integerp (expt x n)))))
  :rule-classes (:type-prescription :generalize))

(defthm expt-type-prescription-integerp-base-b
  (implies (and (integerp n)
		(< n 0)
                (integerp x)
		(< x -1))
           (and (rationalp (expt x n))
		(not (integerp (expt x n)))))
  :rule-classes (:type-prescription :generalize))

#|
;;; I would really like to not need the following rewrite rule.
;;; However, type-reasoning is not particularly good at
;;; determining the truth of inequalities.

;;; Type reasoning should now (v2-8) be a little better at determining
;;; the truth of inequalities and I believe that the following rule is
;;; no longer neccesary.  I keep it around, but commented out, just in
;;; case this is wrong.

(defthm integerp-expt
    (implies (and (integerp x)
                  (<= 0 n))
             (integerp (expt x n))))
|#

;;; Note the form of the conclusion of these rules.  It is important
;;; to write type-prescription rules such that their conclusions
;;; actually specify a type-set.  Due to the presence of complex
;;; numbers and the fact that they are linearly ordered,
;;; (< 0 (expt x n)) does not encode a type-set.  This makes me
;;; unhappy at times.

;;; NOTE: Should the next 3 rules be :linear rules also?
;;; Since they compare to zero, probably not.  On the other hand, as
;;; noted above, type-reasoning is not always as good at
;;; determining the truth of inequalities as one might desire.  This is
;;; still true even with the improvement to type-set mentioned
;;; above.

(defthm expt-type-prescription-non-0-base
  (implies (and (acl2-numberp x)
                (not (equal x 0)))
           (and (acl2-numberp (expt x n))
		(not (equal (expt x n) 0))))
  :rule-classes (:type-prescription :generalize))

(defthm expt-type-prescription-positive-base
  (implies (and (< 0 x)
                (rationalp x))
           (and (rationalp (expt x n))
		(< 0 (expt x n))))
  :rule-classes (:type-prescription :generalize))

#+non-standard-analysis
(defthm expt-type-prescription-positive-base-real-case
  (implies (and (< 0 x)
                (real/rationalp x))
           (and (real/rationalp (expt x n))
		(< 0 (expt x n))))
  :rule-classes (:type-prescription :generalize))

(defthm expt-type-prescription-nonnegative-base
  (implies (and (<= 0 x)
                (rationalp x))
	   (and (rationalp (expt x n))
		(<= 0 (expt x n))))
  :rule-classes (:type-prescription :generalize))

#+non-standard-analysis
(defthm expt-type-prescription-nonnegative-base-real-case
  (implies (and (<= 0 x)
                (real/rationalp x))
	   (and (real/rationalp (expt x n))
		(<= 0 (expt x n))))
  :rule-classes (:type-prescription :generalize))

(defthm integerp-/-expt-1
  (implies (and (integerp x)
		(< 1 x)
		(integerp n))
	   (equal (integerp (/ (expt x n)))
		  (<= n 0)))
  :rule-classes (:rewrite
		 (:type-prescription
		  :corollary
		  (implies (and (integerp x)
				(< 1 x)
				(integerp n)
				(<= n 0))
			   (integerp (/ (expt x n)))))
		 (:generalize
		  :corollary
		  (implies (and (integerp x)
				(< 1 x)
				(integerp n)
				(<= n 0))
			   (integerp (/ (expt x n)))))))

(defthm integerp-/-expt-2
  (implies (and (integerp x)
		(< x -1)
		(integerp n))
	   (equal (integerp (/ (expt x n)))
		  (<= n 0)))
  :rule-classes (:rewrite
		 (:type-prescription
		  :corollary
		  (implies (and (integerp x)
				(< x -1)
				(integerp n)
				(<= n 0))
			   (integerp (/ (expt x n)))))
		 (:generalize
		  :corollary
		  (implies (and (integerp x)
				(< x -1)
				(integerp n)
				(<= n 0))
			   (integerp (/ (expt x n)))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; 2. Simple rules about expt

;;; Since expt will be disabled, I provide some rules to take care of
;;; the ``simple'' cases.

(defthm |(expt x 0)|
 (equal (expt x 0)
        1))

(defthm |(expt 0 n)|
    (equal (expt 0 n)
           (if (zip n)
               1
             0)))

(defthm |(expt x 1)|
  (implies (acl2-numberp x)
	   (equal (expt x 1)
		  x)))

(defthm |(expt 1 n)|
    (equal (expt 1 n)
           1))

(defthm |(expt x -1)|
  (equal (expt x -1)
	 (/ x)))

;;; Do we want a rule like the following?  I have neither tried to
;;; prove it, nor tested its effects.
#|
(defthm |equal (expt x n) -c|
  (implies (and (syntaxp (negative-numeric-constant-p c))
		(integerp c)
		(integerp n)
		(rationalp x))
	   (equal (equal (expt x n) c)
		  (and (equal (expt (- x) n) (- c))
		       (oddp n)))))
|#
;;; There would be issues with |(expt (- x) n)|, at the least.
;;; Maybe a forward-chaining rule with concl (oddp n)?

(defthm |(equal (expt x n) -1)|
  (implies (and (integerp n)
		(real/rationalp x))
	   (equal (equal (expt x n) -1)
		  (and (equal x -1)
		       (oddp n)))))

(defthm |(equal (expt x n) 0)|
  (implies (and (integerp n)
		(real/rationalp x))
	   (equal (equal (expt x n) 0)
		  (and (equal x 0)
		       (not (equal n 0))))))

;;; Should we restrict this to present-in-goal?  Introducing case-splits
;;; like the below can be expensive.

(defthm |(equal (expt x n) 1)|
  (implies  (and (integerp n)
		 (real/rationalp x)
		 (syntaxp (rewriting-goal-literal x mfc state)))
	   (equal (equal (expt x n) 1)
		  (or (zip n)
		      (equal x 1)
		      (and (equal x -1)
			   (evenp n))))))

;;; Do we want something like this?  I have not tried to prove it yet,
;;; but I think it will require reasoning about prime numbers and
;;; factorization.  Given that, should we generalize it to any prime,
;;; not just 2?
#|
(defthm |(equal (expt x n) 2)|
  (implies (syntaxp (rewriting-goal-literal x mfc state))
	   (equal (equal (expt x n) 2)
		  (or (and (equal x 1/2)
			   (equal n -1))
		      (and (equal x 2)
			   (equal n 1))))))
|#

;;; Could we generalize this to other bases than two easily?

;;; Two is an important number

(defun p-o-2-g-fn (c)
  (let ((x (power-of-2-generalized c)))
    (if x
	(list (cons 'x (kwote x)))
      nil)))

(defthm |(equal (expt 2 n) c)|
  (implies (and (bind-free (p-o-2-g-fn c) (x))
		(integerp x)
		(equal (expt 2 x) c)
		(integerp n))
	   (equal (equal (expt 2 n) c)
		  (equal n x))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Should we expand (expt (+ c x) d), whenever c and d are constants?
;;; What about (expt (+ x y) 256)?  Where should we draw the line?

(defthm |(expt (+ x y) 2)|
    (implies (syntaxp (rewriting-goal-literal x mfc state))
             (equal (expt (+ x y) 2)
                    (+ (expt x 2)
                       (* 2 x y)
                       (expt y 2))))
  :hints (("Goal" :expand (expt (+ x y) 2))))

(defthm |(expt (+ x y) 3)|
    (implies (syntaxp (rewriting-goal-literal x mfc state))
             (equal (expt (+ x y) 3)
                    (+ (expt x 3)
                       (* 3 (expt x 2) y)
                       (* 3 x (expt y 2))
                       (expt y 3))))
  :hints (("Goal" :expand ((expt (+ x y) 3)
			   (expt (+ x y) 2)))))

(defthm |(expt c (* d n))|
  (implies (and (syntaxp (quotep c))
                (integerp c)
		(syntaxp (quotep d))
                (integerp d)
		(integerp n))
	   (equal (expt c (* d n))
		  (expt (expt c d) n))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; 3. Normalizing expt expressions

;;; In these next sections we define a couple of rules for
;;; normalizing expressions involving expt.  See top.lisp for a couple
;;; of theories which use some or all of these.

;;; The next six rules come in three pairs, one for general terms, one
;;; for constants.  If you change or disable any of these rules, you
;;; may break the assumptions in collect.lisp for collect-* rules.
;;; Also, see invert-match.

;;; I keep going back and forth on what is the proper treatment of
;;; expt and unary-/.  I originally preferred (expt (/ x) n),
;;; next I tried the below, and now I am trying (expt x (- n)).

#|
;;; I used to push / inside expt, but I now believe that was wrong.

;;; Note that the use of negative-addends-p means that we are not
;;; introducing negation into the exponent below, rather we are
;;; undoing it.

(defthm |(expt x (- n))|
    (implies (syntaxp (mostly-negative-addends-p n mfc state))
             (equal (expt x n)
                    (/ (expt x (- n))))))

(defthm |(expt x (- c))|
    (implies (syntaxp (numeric-constant-p c))
             (equal (expt x (- c))
                    (/ (expt x c)))))

;;; If you change |(expt (/ x) n)| below, see nintergerp-extra in
;;; integerp.lisp.

(defthm |(expt (/ x) n)|
  (equal (expt (/ x) n)
	 (/ (expt x n))))
|#

(defthm |(expt (/ x) n)|
  (equal (expt (/ x) n)
	 (expt x (- n))))

(defthm |(/ (expt x n))|
  (equal (/ (expt x n))
	 (expt x (- n))))

(defthm |(expt 1/c n)|
    (implies (and (syntaxp (quotep x))
                  (syntaxp (rationalp (unquote x)))
                  (syntaxp (not (integerp (unquote x))))
                  ;(syntaxp (equal (numerator x) 1))
		  (syntaxp (< (abs x) 1))
		  )
             (equal (expt x n)
                    (expt (/ x) (- n)))))

(defthm |(expt (- x) n)|
    (implies (and (syntaxp (rewriting-goal-literal x mfc state))
		  (syntaxp (mostly-negative-addends-p x mfc state))
                  (integerp n))
             (equal (expt x n)
                    (if (evenp n)
                        (expt (- x) n)
                      (- (expt (- x) n)))))
  :hints (("Goal" :use ((:instance expt-negative-base-even-exponent-a
                                   (i n)
                                   (r x))
                        (:instance expt-negative-base-odd-exponent-a
                                   (i n)
                                   (r x))))))

(defthm |(expt (- c) n)|
    (implies (and (syntaxp (rewriting-goal-literal c mfc state))
		  (syntaxp (rational-constant-p c))
                  (integerp n))
             (equal (expt (- c) n)
                    (if (evenp n)
                        (expt c n)
                      (- (expt c n))))))

(theory-invariant (if (active-runep '(:definition arith-5-active-flag))
                      (and ;(active-runep '(:rewrite |(expt x (- n))|))
                           ;(active-runep '(:rewrite |(expt x (- c))|))
                           (active-runep '(:rewrite |(expt (/ x) n)|))
                           (active-runep '(:rewrite |(/ (expt x n))|))
                           (active-runep '(:rewrite |(expt 1/c n)|))
                           (active-runep '(:rewrite |(expt (- x) n)|))
                           (active-runep '(:rewrite |(expt (- c) n)|)))
                    t)
                  :error nil)

(defthm |(expt (* x y) n)|
  (equal (expt (* x y) n)
         (* (expt x n)
            (expt y n))))

(defthm |(expt (expt x m) n)|
  (implies (and (integerp m)
                (integerp n))
           (equal (expt (expt x m) n)
                  (expt x (* m n)))))

;;; The following will be disabled for gather-exponents.

;;; Force the scattering of the exponents even at the cost of introducing a
;;; case-split only when we are back-chaining.

(defthm |(expt x (+ m n))|
  (implies (and (syntaxp (rewriting-goal-literal x mfc state))
		(integerp m)
		(integerp n))
	   (equal (expt x (+ m n))
		  (if (equal (+ m n) 0)
		      1
		      (* (expt x m)
			 (expt x n))))))

;;; The following will be disabled for gather-exponents.

(defthm |(expt x (+ m n)) non-zero (+ m n)|
  (implies (and (integerp m)
		(integerp n)
		(not (equal (+ m n) 0)))
	   (equal (expt x (+ m n))
		  (* (expt x m)
		     (expt x n)))))

;;; The following will be disabled for gather-exponents.

(defthm |(expt x (+ m n)) non-zero x|
  (implies (and (acl2-numberp x)
		(not (equal x 0))
		(integerp m)
		(integerp n))
	   (equal (expt x (+ m n))
		  (* (expt x m)
		     (expt x n)))))
#|
;;; I don't think we want these next two.  I leave them here for
;;; referance purposes only.  If you reinstate them, be sure to
;;; uncomment any references to them in top.

(defthm |(expt x (+ m n)) non-pos m and n|
  (implies (and (<= m 0)
		(<= n 0)
		(integerp m)
		(integerp n))
	   (equal (expt x (+ m n))
		  (* (expt x m)
		     (expt x n)))))

(defthm |(expt x (+ m n))) non-neg m and n|
  (implies (and (<= 0 m)
		(<= 0 n)
		(integerp m)
		(integerp n))
	   (equal (expt x (+ m n))
		  (* (expt x m)
		     (expt x n)))))
|#

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; 4. Some miscelaneous rules about expt.

;;; NOTE: There are several rules in this book which have (< 1 x)
;;; as a hypothesis.  There probably should be rules with
;;; (< 0 x) (< x 1) also.

(defthm |(integerp (expt x n))|
  (implies (and (integerp n)
		(integerp x)
		(< 1 x))
	   (equal (integerp (expt x n))
		  (<= 0 n))))

(defthm |(< (expt x n) (expt x m))|
   (implies (and (real/rationalp x)
		 (< 1 x)
		 (integerp m)
		 (integerp n))
	    (equal (< (expt x m) (expt x n))
		   (< m n))))

 (defthm |(equal (expt x m) (expt x n))|
   (implies (and (real/rationalp x)
		 (not (equal x -1))
		 (not (equal x 0))
		 (not (equal x 1))
		 (integerp m)
		 (integerp n))
	    (equal (equal (expt x m) (expt x n))
		   (equal m n))))

;;; I do not particularly like the form of the next rule, but I do
;;; not see how to do better.  Also, something like this would
;;; be a nifty linear rule if we could guess the correct m or n but,
;;; again, I do not see how.

(defthm expt-exceeds-another-by-more-than-y
  (implies (and (real/rationalp x)
		(< 1 x)
                (integerp m)
                (integerp n)
		(<= 0 m)
                (<= 0 n)
                (< m n)
		(real/rationalp y)
		(< (+ y 1) x))
	   (< (+ y (expt x m)) (expt x n))))

(defthm expt-2-n-is-even
  (implies (and (integerp n)
		(integerp m))
	   (equal (equal (expt 2 n)
			 (+ 1 (expt 2 m)))
		  (and (equal n 1)
		       (equal m 0)))))

(defthm odd-expt-thm
  (implies (and (integerp man)
		(integerp exp)
		(<= exp 0))
	   (equal (< man (expt 2 exp))
		  (<= man 0))))

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

;;; 5. Linear rules about expt.

;;; We include two sets of linear rules for expt.  The first set
;;; consists of rules which are both linear and rewrite rules.  Both
;;; types are needed because of the free variable problem.  The second
;;; set are linear rules only.

(defthm expt-x->-x
  (implies (and (< 1 x)
		(< 1 n)
		(real/rationalp x)
		(integerp n))
	   (< x (expt x n)))
  :rule-classes (:rewrite :linear))

(defthm expt-x->=-x
  (implies (and (<= 1 x)
		(< 1 n)
		(real/rationalp x)
		(integerp n))
	   (<= x (expt x n)))
  :rule-classes (:rewrite :linear))

(defthm expt-is-increasing-for-base->-1
  (implies (and (< m n)
		(< 1 x)
		(integerp m)
		(integerp n)
		(real/rationalp x))
	   (< (expt x m)
	      (expt x n)))
  :rule-classes ((:rewrite)
                 (:linear :match-free :once)))

(defthm expt-is-decreasing-for-pos-base-<-1
  (implies (and (< m n)
                (< 0 x)
                (< x 1)
                (integerp m)
                (integerp n)
                (real/rationalp x))
           (< (expt x n)
              (expt x m)))
  :rule-classes ((:rewrite)
                 (:linear :match-free :once)))

(defthm expt-is-weakly-increasing-for-base->-1
  (implies (and (<= m n)
                (<= 1 x)
                (integerp m)
                (integerp n)
                (real/rationalp x))
           (<= (expt x m)
               (expt x n)))
  :rule-classes ((:rewrite)
                 (:linear :match-free :once)))

(defthm expt-is-weakly-decreasing-for-pos-base-<-1
  (implies (and (<= m n)
                (< 0 x)
                (<= x 1)
                (integerp m)
                (integerp n)
                (real/rationalp x))
           (<= (expt x n)
               (expt x m)))
  :rule-classes ((:rewrite)
                 (:linear :match-free :once)))

;; Should these be rewrite rules also? Probably not.

(defthm expt->-1-one
  (implies (and (< 1 x)
		(< 0 n)
		(real/rationalp x)
		(integerp n))
	   (< 1 (expt x n)))
  :rule-classes :linear)

(defthm expt->=-1-one
  (implies (and (<= 1 x)
		(<= 0 n)
		(real/rationalp x)
		(integerp n))
	   (<= 1 (expt x n)))
  :rule-classes :linear)

(defthm expt->-1-two
  (implies (and (< 0 x)
		(< x 1)
		(< n 0)
		(real/rationalp x)
		(integerp n))
	   (< 1 (expt x n)))
  :rule-classes :linear)

(defthm expt->=-1-two
  (implies (and (< 0 x)
		(<= x 1)
		(<= n 0)
		(real/rationalp x)
		(integerp n))
	   (<= 1 (expt x n)))
  :rule-classes :linear)

(defthm expt-<-1-one
  (implies (and (< 0 x)
		(< x 1)
		(< 0 n)
		(real/rationalp x)
		(integerp n))
	   (< (expt x n) 1))
  :rule-classes :linear)

(defthm expt-<=-1-one
  (implies (and (<= 0 x)
		(<= x 1)
		(<= 0 n)
		(real/rationalp x)
		(integerp n))
	   (<= (expt x n) 1))
  :hints (("Goal" :cases ((equal x 0)
			  (equal x 1))))
  :rule-classes :linear)

(defthm expt-<-1-two
  (implies (and (< 1 x)
		(< n 0)
		(real/rationalp x)
		(integerp n))
	   (< (expt x n) 1))
  :rule-classes :linear)

(defthm expt-<=-1-two
  (implies (and (<= 1 x)
		(<= n 0)
		(real/rationalp x)
		(integerp n))
	   (<= (expt x n) 1))
  :rule-classes :linear)


;;; So, we now try replacing the six rules below with four rules using
;;; bind-free.  I really wish ACL2 would let me do the work once, and
;;; to somehow construct the (either one or two) inequalities (including
;;; the relation) on the fly.
#|
;;; RBK: Maybe use bind-free to find the best match for d in the six
;;; rules below?  This could also eliminate the neccesity for -aaa and
;;; -bbb.

;;; Note that I limit these to when c and d are constants.  Thus,
;;; (expt c d) or (expt c (+ 1 d)) are constants being fed into
;;; linear arithemtic as bounds.

(defthm expt-linear-a
  (implies (and (syntaxp (rational-constant-p c))
		(< d n)
		(syntaxp (rational-constant-p d))
		(integerp d)
		(rationalp c)
		(< 1 c)
		(integerp n))
	   (<= (expt c (+ 1 d)) (expt c n)))
  :hints (("Goal" :in-theory (disable expt
				      EXPONENTS-ADD-1
				      EXPONENTS-ADD-2
				      |(expt x (+ m n))|
				      |(expt x (+ m n)) non-zero x|)))
  :rule-classes ((:linear :trigger-terms ((expt c n)))))

(defthm expt-linear-aa
  (implies (and (syntaxp (rational-constant-p c))
		(<= d n)
		(syntaxp (rational-constant-p d))
		(integerp d)
		(real/rationalp c)
		(< 1 c)
		(integerp n))
	   (<= (expt c d) (expt c n)))
  :rule-classes ((:linear :trigger-terms ((expt c n)))))

;;; We need this one because of weaknesses in free variable matching.

(defthm expt-linear-aaa
  (implies (and (syntaxp (rational-constant-p c))
		(<= d n)
		(syntaxp (rational-constant-p d))
		(not (equal n d))
		(integerp d)
		(real/rationalp c)
		(< 1 c)
		(integerp n))
	   (<= (expt c (+ 1 d)) (expt c n)))
  :hints (("Goal" :in-theory (disable expt
				      EXPONENTS-ADD-1
				      EXPONENTS-ADD-2
				      |(expt x (+ m n))|
				      |(expt x (+ m n)) non-zero x|)))
  :rule-classes ((:linear :trigger-terms ((expt c n)))))

(defthm expt-linear--b
  (implies (and (syntaxp (rational-constant-p c))
		(< n d)
		(syntaxp (rational-constant-p d))
		(integerp d)
		(real/rationalp c)
		(< 1 c)
		(integerp n))
	   (<= (expt c n) (expt c (+ -1 d))))
  :hints (("Goal" :in-theory (disable expt
				      EXPONENTS-ADD-1
				      EXPONENTS-ADD-2
				      |(expt x (+ m n))|
				      |(expt x (+ m n)) non-zero x|)))
  :rule-classes ((:linear :trigger-terms ((expt c n)))))

(defthm expt-linear-bb
  (implies (and (syntaxp (rational-constant-p c))
		(<= n d)
		(syntaxp (rational-constant-p d))
		(integerp d)
		(real/rationalp c)
		(< 1 c)
		(integerp n))
	   (<= (expt c n) (expt c d)))
  :rule-classes ((:linear :trigger-terms ((expt c n)))))

(defthm expt-linear-bbb
  (implies (and (syntaxp (rational-constant-p c))
		(<= n d)
		(syntaxp (rational-constant-p d))
		(not (equal n d))
		(integerp d)
		(real/rationalp c)
		(< 1 c)
		(integerp n))
	   (<= (expt c n) (expt c (+ -1 d))))
  :hints (("Goal" :in-theory (disable expt
				      EXPONENTS-ADD-1
				      EXPONENTS-ADD-2
				      |(expt x (+ m n))|
				      |(expt x (+ m n)) non-zero x|)))
  :rule-classes ((:linear :trigger-terms ((expt c n)))))
|#

;;; This should really be put in axioms.lisp.
(defun mfc-pot-lst (mfc)
  (car (cddddr (cddddr mfc))))

;;; This should really be put in axioms.lisp.
(defun mfc-pt (mfc)
  (let ((rcnst (cadr (cddddr (cddddr mfc)))))
    (caaddr rcnst)))

(defun bounds-for-expt-linear-fns-1 (n mfc state)
  ;; See extract-bounds in non-linear for details.
  (declare (xargs :mode :program))
  (declare (ignore state))
  (if (equal (fn-symb n) 'UNARY--)
      (let ((bounds-polys (bounds-polys-with-var n
						 (mfc-pot-lst mfc)
						 (mfc-pt mfc))))
	(mv-let (lbd lbd-rel lbd-ttree
		 ubd ubd-rel ubd-ttree)
	  (extract-bounds bounds-polys)
	  (declare (ignore lbd-ttree ubd-ttree))
	  (mv lbd lbd-rel ubd ubd-rel)))
    (let ((bounds-polys (bounds-polys-with-var n
					       (mfc-pot-lst mfc)
					       (mfc-pt mfc))))
      (mv-let (lbd lbd-rel lbd-ttree
	       ubd ubd-rel ubd-ttree)
	(extract-bounds bounds-polys)
	(declare (ignore lbd-ttree ubd-ttree))
	(mv lbd lbd-rel ubd ubd-rel)))))

;;; RBK: I am pretty sure that this is what I want, but I really should
;;; do some proper testing.
(defun bounds-for-expt-linear-fns (n mfc state)
  ;; We are about to try to extract bounds for n from the linear-pot.
  ;; So, we make sure waht we pass on is a legitimate pot-label
  (declare (xargs :mode :program))
  (cond ((quotep n)
	 ;; Shouldn't happen, raise error or warning?
	 (mv nil nil nil nil))
	((symbolp n)
	 (bounds-for-expt-linear-fns-1 n mfc state))
	((equal (fn-symb n) 'BINARY-+)
	 ;; Shouldn't happen (given the use of arithmetic-theory),
	 ;; raise error or warning?
	 (mv nil nil nil nil))
	((equal (fn-symb n) 'UNARY--)
	 (mv-let (lbd lbd-rel ubd ubd-rel)
	   (bounds-for-expt-linear-fns-1 (arg1 n) mfc state)
	   (mv (if ubd
		   (- ubd)
		 nil)
	       ubd-rel
	       (if lbd
		   (- lbd)
		 nil)
	       lbd-rel)))
	((and (equal (fn-symb n) 'BINARY-*)
	      (quotep (arg1 n))
	      (equal (unquote (arg1 n)) 0))
	 ;; Shouldn't happen, raise error or warning?
	 (mv nil nil nil nil))
	((and (equal (fn-symb n) 'BINARY-*)
	      (quotep (arg1 n))
	      (rationalp (unquote (arg1 n)))
	      (< 0 (unquote (arg1 n))))
	 (mv-let (lbd lbd-rel ubd ubd-rel)
	   (bounds-for-expt-linear-fns-1 (arg2 n) mfc state)
	   (let ((x (/ (unquote (arg1 n)))))
	     (mv (if lbd
		     (/ lbd x)
		   nil)
		 lbd-rel
		 (if ubd
		     (/ ubd x)
		   nil)
		 ubd-rel))))
	((and (equal (fn-symb n) 'BINARY-*)
	      (quotep (arg1 n))
	      (rationalp (unquote (arg1 n)))
	      (< (unquote (arg1 n)) 0))
	 (mv-let (lbd lbd-rel ubd ubd-rel)
	   (bounds-for-expt-linear-fns-1 (arg2 n) mfc state)
	   (let ((x (/ (unquote (arg1 n)))))
	     (mv (if ubd
		     (- (/ ubd x))
		   nil)
		 ubd-rel
		 (if lbd
		     (- (/ lbd x))
		   nil)
		 lbd-rel))))
	(t
	 (bounds-for-expt-linear-fns-1 n mfc state))))

;;; RBK: I always get horribly confused when I try to think abut the
;;; various permutations of relations and constants with respect to
;;; the strength of the final concls in the linear rules.  It is very
;;; likely that this could be done better.

(defun expt-linear-upper-<-fn (n mfc state)
  (declare (xargs :mode :program))
  (mv-let (lbd lbd-rel ubd ubd-rel)
    (bounds-for-expt-linear-fns n mfc state)
    (declare (ignore lbd lbd-rel))
    (cond ((and (equal ubd-rel '<)
		(integerp ubd))
	   (list (cons 'd (kwote ubd))))
	  (t
	   nil))))

(defun expt-linear-upper-<=-fn (n mfc state)
  (declare (xargs :mode :program))
  (mv-let (lbd lbd-rel ubd ubd-rel)
    (bounds-for-expt-linear-fns n mfc state)
    (declare (ignore lbd lbd-rel))
    (cond ((and (equal ubd-rel '<=)
		(integerp ubd))
	   (list (cons 'd (kwote ubd))))
	  ((and (equal ubd-rel '<)
		(integerp ubd))
	   ;; Caught in expt-linear-upper-<-fn
	   nil)
	  ((real/rationalp ubd)
	   (list (cons 'd (kwote (floor ubd 1)))))
	  (t
	   nil))))

(defun expt-linear-lower-<-fn (n mfc state)
  (declare (xargs :mode :program))
  (mv-let (lbd lbd-rel ubd ubd-rel)
    (bounds-for-expt-linear-fns n mfc state)
    (declare (ignore ubd ubd-rel))
    (cond ((and (equal lbd-rel '<)
		(integerp lbd))
	   (list (cons 'd (kwote lbd))))
	  (t
	   nil))))

(defun expt-linear-lower-<=-fn (n mfc state)
  (declare (xargs :mode :program))
  (mv-let (lbd lbd-rel ubd ubd-rel)
    (bounds-for-expt-linear-fns n mfc state)
    (declare (ignore ubd ubd-rel))
    (cond ((and (equal lbd-rel '<=)
		(integerp lbd))
	   (list (cons 'd (kwote lbd))))
	  ((and (equal lbd-rel '<)
		(integerp lbd))
	   nil)
	  ((real/rationalp lbd)
	   (list (cons 'd (kwote (+ 1 (floor lbd 1))))))
	  (t
	   nil))))

(defthm expt-linear-upper-<
  (implies (and (syntaxp (rational-constant-p c))
		(real/rationalp c)
		(< 1 c)
		(bind-free (expt-linear-upper-<-fn n mfc state)
			   (d))
		(integerp d)
		(< n d)
		(integerp n))
	   (< (expt c n) (expt c d)))
  :rule-classes ((:linear :trigger-terms ((expt c n)))))

(defthm expt-linear-upper-<=
  (implies (and (syntaxp (rational-constant-p c))
		(real/rationalp c)
		(< 1 c)
		(bind-free (expt-linear-upper-<=-fn n mfc state)
			   (d))
		(integerp d)
		(<= n d)
		(integerp n))
	   (<= (expt c n) (expt c d)))
  :rule-classes ((:linear :trigger-terms ((expt c n)))))

(defthm expt-linear-lower-<
  (implies (and (syntaxp (rational-constant-p c))
		(real/rationalp c)
		(< 1 c)
		(bind-free (expt-linear-lower-<-fn n mfc state)
			   (d))
		(integerp d)
		(< d n)
		(integerp n))
	   (< (expt c d) (expt c n)))
  :rule-classes ((:linear :trigger-terms ((expt c n)))))

(defthm expt-linear-lower-<=
  (implies (and (syntaxp (rational-constant-p c))
		(real/rationalp c)
		(< 1 c)
		(bind-free (expt-linear-lower-<=-fn n mfc state)
			   (d))
		(integerp d)
		(<= d n)
		(integerp n))
	   (<= (expt c d) (expt c n)))
  :rule-classes ((:linear :trigger-terms ((expt c n)))))


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

;;; Some rules about expt with a negative base.

(defthm expt-type-prescription-negative-base-even-exponent
  (implies (and (< x 0)
		(rationalp x)
		(integerp n)
		(integerp (* 1/2 n)))
	   (and (rationalp (expt x n))
		(< 0 (expt x n))))
  :rule-classes (:type-prescription :generalize))

#+non-standard-analysis
(defthm expt-type-prescription-negative-base-even-exponent-real-case
  (implies (and (< x 0)
		(real/rationalp x)
		(integerp n)
		(integerp (* 1/2 n)))
	   (and (real/rationalp (expt x n))
		(< 0 (expt x n))))
  :rule-classes (:type-prescription :generalize))

(defthm expt-type-prescription-negative-base-odd-exponent
  (implies (and (< x 0)
		(rationalp x)
		(integerp n)
		(not (integerp (* 1/2 n))))
	   (and (rationalp (expt x n))
		(< (expt x n) 0)))
  :rule-classes (:type-prescription :generalize))

#+non-standard-analysis
(defthm expt-type-prescription-negative-base-odd-exponent-real-case
  (implies (and (< x 0)
		(real/rationalp x)
		(integerp n)
		(not (integerp (* 1/2 n))))
	   (and (real/rationalp (expt x n))
		(< (expt x n) 0)))
  :rule-classes (:type-prescription :generalize))

(defthm expt-type-prescription-nonpositive-base-even-exponent
  (implies (and (<= x 0)
                (rationalp x)
		(integerp n)
		(integerp (* 1/2 n)))
	   (and (rationalp (expt x n))
		(<= 0 (expt x n))))
  :rule-classes (:type-prescription :generalize)
  :hints (("Goal" :use ((:instance
			 expt-type-prescription-negative-base-even-exponent-a)))))

#+non-standard-analysis
(defthm expt-type-prescription-nonpositive-base-even-exponent-real-case
  (implies (and (<= x 0)
                (real/rationalp x)
		(integerp n)
		(integerp (* 1/2 n)))
	   (and (real/rationalp (expt x n))
		(<= 0 (expt x n))))
  :rule-classes (:type-prescription :generalize)
  :hints (("Goal" :use ((:instance
			 expt-type-prescription-negative-base-even-exponent-a)))))

(defthm expt-type-prescription-nonpositive-base-odd-exponent
  (implies (and (<= x 0)
                (rationalp x)
		(integerp n)
		(not (integerp (* 1/2 n))))
	   (and (rationalp (expt x n))
		(<= (expt x n) 0)))
  :rule-classes (:type-prescription :generalize)
  :hints (("Goal" :use ((:instance
			 expt-type-prescription-negative-base-odd-exponent-a)))))

#+non-standard-analysis
(defthm expt-type-prescription-nonpositive-base-odd-exponent-real-case
  (implies (and (<= x 0)
                (real/rationalp x)
		(integerp n)
		(not (integerp (* 1/2 n))))
	   (and (real/rationalp (expt x n))
		(<= (expt x n) 0)))
  :rule-classes (:type-prescription :generalize)
  :hints (("Goal" :use ((:instance
			 expt-type-prescription-negative-base-odd-exponent-a)))))