File: cat-proofs.lisp

package info (click to toggle)
acl2 8.6%2Bdfsg-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 1,138,276 kB
  • sloc: lisp: 17,818,294; java: 125,359; python: 28,122; javascript: 23,458; cpp: 18,851; ansic: 11,569; perl: 7,678; xml: 5,591; sh: 3,978; makefile: 3,840; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (1193 lines) | stat: -rw-r--r-- 41,435 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
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
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
(in-package "ACL2")

(defund bvecp (x k)
  (declare (xargs :guard (integerp k)))
  (and (integerp x)
       (<= 0 x)
       (< x (expt 2 k))))

(local ; ACL2 primitive
 (defun natp (x)
   (declare (xargs :guard t))
   (and (integerp x)
        (<= 0 x))))

(defund fl (x)
  (declare (xargs :guard (real/rationalp x)))
  (floor x 1))

(defund bits (x i j)
  (declare (xargs :guard (and (natp x)
                              (natp i)
                              (natp j))
                  :verify-guards nil))
  (mbe :logic (if (or (not (integerp i))
                      (not (integerp j)))
                  0
                (fl (/ (mod x (expt 2 (1+ i))) (expt 2 j))))
       :exec  (if (< i j)
                  0
                (logand (ash x (- j)) (1- (ash 1 (1+ (- i j))))))))

(defund bitn (x n)
  (declare (xargs :guard (and (natp x)
                              (natp n))
                  :verify-guards nil))
  (mbe :logic (bits x n n)
       :exec  (if (evenp (ash x (- n))) 0 1)))

;(defund ocat (x y n)
;  (declare (xargs :guard t))
;  (+ (* (expt 2 (nfix n)) (nfix x)) (nfix y)))

(defund all-ones (n)
  (declare (xargs :guard (and (integerp n) (<= 0 n))))
  (if (zp n)
      0 ;degenerate case
    (1- (expt 2 n))))

(include-book "cat-def")
(local (include-book "../arithmetic/top")) ;try
(local (include-book "bits"))
(local (include-book "bvecp"))

(local (in-theory (enable expt-minus)))

#|
Concatenate the M-bit value X onto the N-bit value Y.  X will occupy the high bits of the result.

(cat x m y n) is well-defined only when the following predicate is true:

(and (natp m)
     (bvecp x m)
     (natp n)
     (bvecp y n))

|#

(defthm cat-nonnegative-integer-type
  (and (integerp (cat x m y n))
       (<= 0 (cat x m y n)))
  :hints (("Goal" :in-theory (enable cat)))
  :rule-classes (:type-prescription)
  )

;this rule is no better than cat-nonnegative-integer-type and might be worse:
(in-theory (disable (:type-prescription binary-cat)))

;just a rewrite rule
(defthm cat-natp
  (natp (cat x m y n)))

;disable?
(defthm cat-0
  (implies (and (case-split (bvecp y n))
                (case-split (integerp n))
                (case-split (integerp m))
                (case-split (<= 0 m))
                )
           (equal (cat 0 m y n) y))
  :hints (("Goal" :in-theory (enable cat bits-tail))))

;BOZO just use this one??
(defthm cat-0-alt
  (implies (and ;(case-split (bvecp y n))
                (case-split (integerp n))
                (case-split (integerp m))
                (case-split (<= 0 m))
                )
           (equal (cat 0 m y n) (bits y (1- n) 0)))
  :hints (("Goal" :in-theory (enable cat bits-tail))))

;We can rely on bits-tail to complete the simplification down to x if desired.
(defthm cat-with-n-0
  (equal (binary-cat x m y 0)
         (bits x (1- m) 0))
  :hints (("goal" :in-theory (enable cat))))

(local (in-theory (enable bits-tail)))

(defthm cat-with-n-0-alt
 (implies (case-split (bvecp x m))
           (equal (cat x m y 0)
                  x)))

;We can rely on bits-tail to complete the simplification down to y if desired.
(defthm cat-with-m-0
  (equal (binary-cat x 0 y n)
         (bits y (1- n) 0))
  :hints (("goal" :in-theory (enable cat))))

(defthm cat-with-m-0-alt
  (implies (case-split (bvecp y n))
           (equal (cat x 0 y n)
                  y)))

;change this behavior??
(defthm cat-with-n-not-a-natural
  (implies (or (not (integerp n))
               (< n 0))
           (equal (cat x m y n)
                  0))
  :hints (("Goal" :in-theory (enable cat))))

(defthm cat-with-m-not-a-natural
  (implies (or (not (integerp m))
               (< m 0))
           (equal (cat x m y n)
                  0))
  :hints (("Goal" :in-theory (enable cat))))

#|
;used to transfer theorems about ocat to theorems about cat
(defthmd cat-ocat
    (equal (binary-cat x m y n)
	   (if (or (not (integerp m))
		   (< m 0)
		   (not (integerp n))
		   (< n 0)
		   )
	       0
	     (ocat (bits x (1- m) 0)
		   (bits y (1- n) 0)
		   n)))
  :hints (("Goal" :in-theory (enable ocat cat))))
|#

(local (defthm hack-10
    (implies (and (integerp x)
		  (integerp y)
		  (< x y)
                  )
	     (<= x (1- y)))
  :rule-classes ()))

(defthm cat-bvecp-simple
  (bvecp (cat x m y n) (+ m n))
  :hints (("Goal" :in-theory (set-difference-theories
                              (enable bvecp cat expt-split)
                              '(expt-compare EXPT-COMPARE-EQUAL ))
		  :use ((:instance expt-split (i m) (j n) (r 2))
			(:instance hack-10 (x (BITS X (1- M) 0)) (y (expt 2 m)))
			(:instance expt-weak-monotone (m p))
			(:instance expt-weak-monotone (m k) (n (+ m n)))))))


(defthm cat-bvecp
  (implies (and (<= (+ m n) k)
                (case-split (integerp k)))
           (bvecp (cat x m y n) k))
  :hints (("Goal" :in-theory (disable cat-bvecp-simple)
           :use cat-bvecp-simple)))

#|

these aren't right any more?

(defthm cat-with-x-not-a-natural
  (implies (or (not (integerp x))
               (< x 0))
           (equal (cat x m y n)
                  0))
  :hints (("Goal" :in-theory (enable cat))))

(defthm cat-with-y-not-a-natural
  (implies (or (not (integerp y))
               (< y 0))
           (equal (cat x y n)
                  (* (nfix x) (expt 2 (nfix n)))))
  :hints (("Goal" :in-theory (enable cat-ocat))))

|#

;move
(defthm hack11
 (implies (and (< x z)
               (integerp x)
               (integerp z)
               (<= 0 y)
               (< y 1))
          (< (+ x y) z)))

;move!
(defthm expt-bound-hack
 (implies (and (< y (expt 2 i))
               (< x (expt 2 (- k i)))
               (<= 0 x)
               (<= 0 y)
               (<= i k)
               (integerp k)
               (integerp i)
               (integerp x)
               (integerp y)
               )
          (< (+ y (* (expt 2 i) x)) (expt 2 k)))
 :hints (("Goal" :in-theory (e/d ( expt-split expt-minus)
                                 (LESS-THAN-MULTIPLY-THROUGH-BY-INVERTED-FACTOR-FROM-LEFT-HAND-SIDE
                                  LESS-THAN-MULTIPLY-THROUGH-BY-INVERTED-FACTOR-FROM-RIGHT-HAND-SIDE))
          :use (:instance  mult-both-sides-of-<-by-positive (a (+ y (* x (expt 2 i)))) (b (expt 2 k))
                           (c (/ (expt 2 i))))))
 )



(defthm cat-associative
  (implies (and (case-split (<= (+ m n) p)) ;gen?
                (case-split (<= 0 m))
                (case-split (<= 0 n))
                (case-split (<= 0 q))
                (case-split (integerp m))
                (case-split (integerp n))
                (case-split (integerp p))
                (case-split (integerp q))
                )
           (equal (cat (cat x m y n) p z q)
                  (cat x m (cat y n z q) (+ n q))))
  :hints (("Goal" :in-theory (enable cat))))

;prove from something more general (cat-equal-split??)
;BOZO move hyps to conclusion?
(defthm cat-equal-0
  (implies (and (case-split (bvecp x m))
                (case-split (bvecp y n))
                (case-split (natp n))
                (case-split (natp m))
                )
           (equal (equal (cat x m y n) 0)
                  (and (equal x 0)
                       (equal y 0))))
  :hints (("Goal" :in-theory (enable cat bits-tail bvecp)))
  )

(defthm cat-combine-constants
  (implies (and (syntaxp (and (quotep x)
                              (quotep m)
                              (quotep y)
                              (quotep n)))
                (equal (+ n p) r)
                (case-split (<= 0 m))
                (case-split (<= 0 n))
                (case-split (<= 0 p))
                (case-split (integerp m))
                (case-split (integerp n))
                (case-split (integerp p))
                )
           (equal (cat x m (cat y n z p) r)
                  (cat (cat x m y n) (+ m n) z p))))

;allows r to be > n+p
(defthm cat-combine-constants-gen
  (implies (and (syntaxp (and (quotep x)
                              (quotep m)
                              (quotep y)
                              (quotep r)
                              (quotep p)))
                (case-split (<= (+ n p) r))
                (case-split (bvecp y n))
                (case-split (<= 0 m))
                (case-split (<= 0 n))
                (case-split (<= 0 p))
                (case-split (integerp m))
                (case-split (integerp n))
                (case-split (integerp p))
                (case-split (integerp r))
                )
           (equal (cat x m (cat y n z p) r)
                  (cat (cat x m y (+ r (- p))) (+ m r (- p)) z p)))
  :hints (("goal" :in-theory (enable bits-tail)
           :expand ((binary-cat y n z p)
                           (binary-cat y (+ r (* -1 p)) z p)))))

(defthm cat-constant-equal-constant-hack
  (implies (and (syntaxp (and (quotep k1) (quotep k2)))
                (case-split (bvecp x n))
                (case-split (bvecp k1 m))
                (case-split (rationalp k2))
                (case-split (natp n))
                (case-split (natp m))
                )
           (equal (equal (cat k1 m x n) k2)
                  (equal x (- k2 (* (expt 2 n) k1)))))
  :hints (("Goal" :in-theory (enable cat bvecp))))

(defthm cat-upper-bound
  (< (cat x m y n)
     (expt 2 (+ m n)))
  :rule-classes (:rewrite :linear)
  :hints (("goal"
           :in-theory (set-difference-theories
                       (enable cat)
                       '()))))

;perhaps the :linear rule cat-upper-bound is enough, but this may help stupid hyps be rewritten away
(defthm cat-compare-to-constant-1
  (implies (and (syntaxp (quotep k))
                (<= (expt 2 (+ m n)) k))
           (< (cat x m y n) k))
  :hints (("goal" :in-theory (disable cat-upper-bound)
           :use cat-upper-bound)))

;provides a tighter bound
(defthm cat-upper-bound-tight
  (implies (and (case-split (<= 0 m))
                (case-split (<= 0 n))
                (case-split (integerp m))
                (case-split (integerp n))
                )
           (<= (cat x m y n)
               (1- (expt 2 (+ n m)))))
  :hints (("goal" :use cat-upper-bound
           :in-theory (set-difference-theories
                       (enable)
                       '(cat-upper-bound)))))


(defthm cat-compare-to-constant-2
  (implies (and (syntaxp (quotep k))
                (<= (1- (expt 2 (+ m n))) k)
                (case-split (<= 0 m))
                (case-split (<= 0 n))
                (case-split (integerp m))
                (case-split (integerp n))
                )
           (not (< k (cat x m y n))))
  :hints (("goal" :in-theory (disable cat-upper-bound)
           :use cat-upper-bound)))

;BOZO consider adding?
;problem if we case-split something that turns out to be false?
(defthm bits-with-i-not-an-integer-2
  (implies (case-split (not (integerp i)))
           (equal (bits x i j)
                  0)))

(defthm bits-with-j-not-an-integer-2
  (implies (case-split (not (integerp j)))
           (equal (bits x i j)
                  0)))

;also case-split that i>=j in any call to bits?


;loops with bits-<-1
;BOZO add theory invariant!
;BOZO ask matt about parity..
(defthmd bits-equal-0-to-bound
  (equal (equal 0 (bits x i j))
         (< (bits x i j) 1)))

;we had a special case where j was 0, but I think this is better (it's certainly more general):
;better name?
;think about whether this can be proved without opening bits (including bits-plus-bits??)
;prove bvecp-bits from this??
;the regular bits-bvecp should fire first...
(defthm bits-slice-zero-gen
  (implies (and (case-split (<= 0 k))
                (case-split (integerp k))
                (case-split (integerp j))
                )
           (equal (bvecp (bits x i j) k)
                  (equal 0 (bits x i (+ k j)))))
  :otf-flg t
  :hints (("Goal" :use (:instance bits-plus-bits (n i) (m j) (p (+ k j)))
           :in-theory (e/d (bits-equal-0-to-bound bvecp expt-strong-monotone-linear) (bits-<-1)))))

(encapsulate
 ()

 (local (defthm cat-bvecp-rewrite-case-1-a
          (implies (and (<= n k) ;this case
                        (< k (+ m n))
                        (<= 0 n)
                        (integerp k)
                        (integerp n)
                        )
                   (implies (bvecp (cat x m y n) k)
                            (bvecp (bits x (1- m) 0) (+ k (* -1 n))) ;the BITS call around x is weird but necessary
                            ))
          :rule-classes nil
          :otf-flg t
          :hints (("Goal" :in-theory (e/d (cat bvecp expt-split) ( expt-inverse  bits-slice-zero-gen))))))

 (local (defthm cat-bvecp-rewrite-case-1-a-better
          (implies (and (<= n k) ;this case
                        (< k (+ m n))
                        (<= 0 n)
                        (integerp k)
                        (integerp n)
                        )
                   (implies (bvecp (cat x m y n) k)
                            (equal 0 (bits x (1- m) (+ k (* -1 n))))
                            ))
          :rule-classes nil
          :otf-flg t
          :hints (("Goal" :use  cat-bvecp-rewrite-case-1-a ))))

;move!
;this can help, especially when we aren't multiplying through by inverted factors
 (defthm bits-upper-bound-new
   (< (* (/ (expt 2 i)) (bits x (1- i) 0)) 1)
   :hints (("Goal" :in-theory (disable expt-inverse)))
   :rule-classes (:rewrite :linear)
   )

;make a lemma about bvecp of (+ (BITS Y (+ -1 N) 0) (* (EXPT 2 N) (BITS X (+ -1 M) 0))) ?
 (local (defthm cat-bvecp-rewrite-case-1-b
          (implies (and (<= n k) ;this case
                        (< k (+ m n))
                        (integerp k)
                        )
                   (implies (bvecp (bits x (1- m) 0) (- k n))
                            (bvecp (cat x m y n) k))) ;the BITS call around x is weird but necessary?
          :rule-classes nil
          :hints (("Goal" :in-theory (e/d (cat bvecp expt-split
                                               EXPT-minus
                                               )
                                          ( expt-inverse
;these must be disabled, or  bits-upper-bound-new fails to do its job
                                               LESS-THAN-MULTIPLY-THROUGH-BY-inverted-factor-FROM-LEFT-HAND-SIDE
                                               LESS-THAN-MULTIPLY-THROUGH-BY-inverted-factor-FROM-RIGHT-HAND-SIDE
                                               ))))))



 (local (defthm cat-bvecp-rewrite-case-1-b-better
          (implies (and (<= n k) ;this case
                        (< k (+ m n))
                        (integerp k)
                        )
                   (implies (equal 0 (bits x (1- m) (+ k (* -1 n))))
                            (bvecp (cat x m y n) k))) ;the BITS call around x is weird but necessary?
          :rule-classes nil
          :hints (("Goal":use  cat-bvecp-rewrite-case-1-b))))


 (local (defthm cat-bvecp-rewrite-case-1
          (implies (and (<= n k) ;this case
                        (< k (+ m n))
                        (case-split (<= 0 n))
                        (case-split (integerp k))
                        (case-split (integerp n))
                        )
                   (equal (bvecp (cat x m y n) k)
                          (equal 0 (bits x (1- m) (+ k (* -1 n)))))) ;the BITS call around x is weird but necessary?
          :hints (("Goal" :use ( cat-bvecp-rewrite-case-1-b-better  cat-bvecp-rewrite-case-1-a-better)))
          ))






 (local (defthm cat-bvecp-rewrite-case-2-a
          (implies (and (< k n) ;this case
                        (<= 0 k)
                        (natp m) (natp n) (natp k)
                        (integerp n)
                        (integerp k)
                        )
                   (implies (bvecp (cat x m y n) k)
                            (and (equal (bits x (1- m) 0) 0)
                                 (bvecp (bits y (1- n) 0) k))))
          :rule-classes nil
          :otf-flg t
          :hints (("Goal"
                   :use (
                         )
                   :in-theory (e/d (cat bvecp bits-equal-0-to-bound expt-strong-monotone-linear) (bits-<-1
                                                                                                  BITS-SLICE-ZERO-GEN
                                                                                                  ))))))


 (local (defthm cat-bvecp-rewrite-case-2-a-better
          (implies (and (< k n) ;this case
                        (<= 0 k)
                        (<= 0 m)
                        (integerp m)
                        (integerp n)
                        (integerp k)
                        )
                   (implies (bvecp (cat x m y n) k)
                            (and (equal 0 (bits x (1- m) 0))
                                 (equal 0 (bits y (1- n) k)))))
          :rule-classes nil
          :otf-flg t
          :hints (("Goal"
                   :use (cat-bvecp-rewrite-case-2-a)))))

 (local (defthm cat-bvecp-rewrite-case-2-b
          (implies (and (< k n) ;this case
                        (<= 0 k)
                        (integerp n)
                        (integerp k)
                        )
                   (implies (and (equal (bits x (1- m) 0) 0)
                                 (bvecp (bits y (1- n) 0) k))
                            (bvecp (cat x m y n) k)))
          :rule-classes nil
          :otf-flg t
          :hints (("Goal"
                   :use (
                         )
                   :in-theory (e/d (cat
                                    bits-equal-0-to-bound
                                    expt-strong-monotone-linear
                                    bvecp expt-split)
                                   (;CANCEL-IN-PRODS-<-1-OF-2-WITH-1-OF-1
                                    ;CANCEL-TIMES-<-ERIC-1-BETTER-ALT
                                    bits-<-1

                                    expt-inverse
                                    BITS-SLICE-ZERO-GEN))))))

 (local (defthm cat-bvecp-rewrite-case-2-b-better
          (implies (and (< k n) ;this case
                        (<= 0 k)
                        (integerp n)
                        (integerp k)
                        )
                   (implies (and (equal (bits x (1- m) 0) 0)
                                 (equal 0 (bits y (1- n) k)))
                            (bvecp (cat x m y n) k)))
          :rule-classes nil
          :otf-flg t
          :hints (("Goal"
                   :use (cat-bvecp-rewrite-case-2-b)))))

 (local (defthm cat-bvecp-rewrite-case-2
          (implies (and (< k n) ;this case
                        (<= 0 k)
                        (<= 0 m)
                        (integerp m)
                        (integerp n)
                        (integerp k)
                        )
                   (equal (bvecp (cat x m y n) k)
                          (and (equal 0 (bits x (1- m) 0))
                               (equal 0 (bits y (1- n) k)))))
          :otf-flg t
          :hints (("Goal"
                   :use (cat-bvecp-rewrite-case-2-a-better
                         cat-bvecp-rewrite-case-2-b-better
                         )
                   :in-theory (e/d () ())))))

 (defthmd cat-bvecp-rewrite
   (implies (and (case-split (<= 0 k))
                 (case-split (<= 0 n))
                 (case-split (<= 0 m))
                 (case-split (integerp n))
                 (case-split (integerp m))
                 (case-split (integerp k))
                 )
            (equal (bvecp (cat x m y n) k)
                   (if (<= (+ m n) k)
                       t
                     (if (<= n k)
                         (equal 0 (bits x (1- m) (+ k (* -1 n))))
                       (and (equal 0 (bits x (1- m) 0))
                            (equal 0 (bits y (1- n) k))))))))

 ) ;end the encapsulate

(defthm cat-bvecp-rewrite-constants
  (implies (and (syntaxp (and (quotep k) (quotep m) (quotep n)))
                (case-split (<= 0 k))
                (case-split (<= 0 n))
                (case-split (<= 0 m))
                (case-split (integerp n))
                (case-split (integerp m))
                (case-split (integerp k))
                )
           (equal (bvecp (cat x m y n) k)
                  (if (<= (+ m n) k)
                      t
                    (if (<= n k)
                        (equal 0 (bits x (1- m) (+ k (* -1 n))))
                      (and (equal 0 (bits x (1- m) 0))
                           (equal 0 (bits y (1- n) k)))))))
  :hints (("Goal" :by cat-bvecp-rewrite)))


#|
Art's example:

(thm (implies (and (natp i) (natp m) (natp n) (bvecp x i))
              (bvecp (cat x m y n) (+ i n)))
     )
|#

;k is a free variable.
;There is no real analogue of this for y (that is, we can't change n to something smaller).
(defthm cat-tighten-x
  (implies (and (bvecp x k) ;k becomes bound here
                (< k m) ;if k=m, this rule can loop
                (case-split (<= 0 k))
                (case-split (integerp k))
                (case-split (integerp m))
                )
           (equal (cat x m y n)
                  (cat x k y n)))
  :hints (("Goal" :in-theory (enable cat))))














(defthm cat-equal-y
  (implies (and (bvecp y (+ m n))
                (case-split (integerp m))
                (case-split (<= 0 m))
                (case-split (integerp n))
                (case-split (<= 0 n)))
           (equal (equal y (binary-cat x m y n))
                  (equal (bits y (+ -1 m n) n)
                         (bits x (1- m) 0))))
  :hints (("Goal" :in-theory (enable cat a15)
           :use (:instance bits-plus-bits (x y) (n  (+ -1 m n)) (m 0) (p n))))
  )

(defthm cat-equal-y-alt
  (implies (and (case-split (integerp m))
                (case-split (<= 0 m))
                (case-split (integerp n))
                (case-split (<= 0 n)))
           (equal (equal y (binary-cat x m y n))
                  (if (bvecp y (+ m n))
                      (equal (bits y (+ -1 m n) n)
                             (bits x (1- m) 0))
                    nil)))
  :hints (("Goal" :in-theory (disable  cat-equal-y)
           :use  cat-equal-y))
)

(defthm cat-equal-bits-of-y
  (implies (and; (case-split (bvecp y n))
;                (case-split (bvecp x m))
                (case-split (integerp m))
                (case-split (<= 0 m))
                (case-split (integerp n))
                (case-split (<= 0 n)))
           (equal (equal (bits y (1- n) 0) (binary-cat x m y n))
                  (equal (bits x (1- m) 0) 0)))
  :hints (("goal" :in-theory (enable binary-cat)))
   )

;requires y to be a bvecp of length n
;drop this one?
(defthm cat-equal-y-special
  (implies (and (case-split (bvecp y n))
                (case-split (integerp m))
                (case-split (<= 0 m)) ;gen!
                (case-split (integerp n))
                (case-split (<= 0 n)))
           (equal (equal y (binary-cat x m y n))
                  (equal 0 (bits x (1- m) 0))))
  :hints (("Goal" :use cat-equal-bits-of-y)) ;drop this hint
  )

;enable?
;make into 2 separate lemmas (can drop the bits from x or from y)
(defthmd cat-ignores-bits
  (equal (cat (bits x (1- m) 0)
              m (bits y (1- n) 0)
              n)
         (cat x m y n))
  :hints (("goal" :in-theory (enable cat))))

(defthmd bits-cat-1
  (implies (and (< i n)
                (case-split (<= 0 j))
                (case-split (integerp n))
                (case-split (integerp m))
                (case-split (<= 0 m))
                )
           (equal (bits (cat x m y n) i j)
                  (bits y i j)))
  :hints (("Goal" :in-theory (enable cat))))

;move!


(defthmd bits-cat-2-1
  (implies (and (<= n j) ;case 2
                (< i (+ m n))  ;case 2-1
                (case-split (natp n))
                (case-split (integerp i))
                (case-split (integerp j))
                (case-split (natp m))
                )
           (equal (bits (cat x m y n) i j)
                  (bits x (- i n) (- j n))))
  :hints (("Goal" :in-theory (enable cat))))

(defthmd bits-cat-2-2
  (implies (and (<= n j)  ;case 2
                (<= (+ m n) i)  ;case 2-1
                (case-split (natp n))
                (case-split (integerp i))
                (case-split (integerp j))
                (case-split (natp m))
                )
           (equal (bits (cat x m y n) i j)
                  (bits x (+ m -1) (- j n))))
  :hints (("Goal" :in-theory (enable cat)))
  )

;note the IF in the conclusion
(defthmd bits-cat-2
  (implies (and (<= n j)  ;case 2
                (case-split (natp n))
                (case-split (integerp i))
                (case-split (integerp j))
                (case-split (natp m))
                )
           (equal (bits (cat x m y n) i j)
                  (bits x (if (< i (+ m n))
                              (- i n)
                            (1- m))
                        (- j n))))
  :hints (("Goal" :in-theory (enable cat)))
  )


;Note the IF in the conclusion
(defthmd bits-cat-3
  (implies (and (>= i n)
                (< j n)
                (case-split (natp n))
                (case-split (natp m))
                (case-split (natp i))
                (case-split (natp j))
                )
           (equal (bits (cat x m y n) i j)
                  (cat (bits x (if (< i (+ m n))
                                    (- i n)
                                  (1- m))
                              0)
                        (+ 1 (- i n))
                        (bits y (1- n) j)
                        (- n j))))
  :hints (("Goal" :use (:instance bits-plus-bits (x (cat x m y n)) (p n) (n i) (m j))
           :in-theory (enable cat   bits-plus-mult-1))))

;includes both bits-cat-1, bits-cat-2, and bits-cat-3
;we expect the indices to be constants, so this won't cause case-splits
;gen
(defthm bits-cat
  (implies (and (case-split (natp n))
                (case-split (natp m))
                (case-split (natp i))
                (case-split (natp j)))
           (equal (bits (cat x m y n) i j)
                  (if (< i n)
                      (bits y i j)
                    (if (>= j n)
                        (bits x (if (< i (+ m n))
                                    (- i n)
                                  (1- m))
                              (- j n))
                      (cat (bits x (if (< i (+ m n))
                                        (- i n)
                                      (1- m)) 0)
                            (+ 1 (- i n))
                            (bits y (1- n) j)
                            (- n j))))))
  :hints (("Goal" :in-theory (enable bits-cat-1
                                     bits-cat-2
                                     bits-cat-3))))

;The following trivial corollary of bits-cat is worth keeping enabled.

(defthm bits-cat-constants
  (implies (and (syntaxp (quotep n))
                (syntaxp (quotep m))
                (syntaxp (quotep i))
                (syntaxp (quotep j))
                (natp n)
                (natp m)
                (natp i)
                (natp j))
           (equal (bits (cat x m y n) i j)
                  (if (< i n)
                      (bits y i j)
                    (if (>= j n)
                        (bits x (if (< i (+ m n))
                                    (- i n)
                                  (1- m))
                              (- j n))
                      (cat (bits x (if (< i (+ m n))
                                       (- i n)
                                     (1- m)) 0)
                           (+ 1 (- i n))
                           (bits y (1- n) j)
                           (- n j)))))))

;bitn-cat should be all we need for simplifying (bitn (cat...))
(defthmd bitn-cat-1
  (implies (and (< i n)
                (case-split (natp n))
                (case-split (natp m))
                (case-split (natp i))
                )
           (equal (bitn (cat x m y n) i)
                  (bitn y i)))
  :hints (("Goal" :in-theory (set-difference-theories
                              (enable bitn bits-cat-1)
                              '()))))

;bitn-cat should be all we need for simplifying (bitn (cat...))
(defthmd bitn-cat-2
  (implies (and (>= i n)
                (case-split (natp n))
                (case-split (natp m))
                (case-split (integerp i))
                )
           (equal (bitn (cat x m y n) i)
                  (if (< i (+ m n))
                      (bitn x (- i n))
                    0)))
  :hints (("Goal" :in-theory (set-difference-theories
                              (enable bitn)
                              '()))))

;includes both bitn-cat-1 and bitn-cat-2
(defthm bitn-cat
  (implies (and (case-split (natp n))
                (case-split (natp m))
                (case-split (natp i)))
           (equal (bitn (cat x m y n) i)
                  (if (< i n)
                      (bitn y i)
                    (if (< i (+ m n))
                      (bitn x (- i n))
                    0))))
  :hints (("Goal" :in-theory (enable bitn-cat-1
                                     bitn-cat-2))))

;The following trivial corollary of bitn-cat is worth keeping enabled.

(defthm bitn-cat-constants
  (implies (and (syntaxp (quotep n))
                (syntaxp (quotep m))
                (syntaxp (quotep i))
                (natp n)
                (natp m)
                (natp i))
           (equal (bitn (cat x m y n) i)
                  (if (< i n)
                      (bitn y i)
                    (if (< i (+ m n))
                      (bitn x (- i n))
                    0)))))

(defthm cat-bits-bits
  (implies (and (equal j (1+ k))
                (equal n (+ 1 (- l) k))
                (case-split (<= (+ 1 (- j) i) m))
                (case-split (<= j i))
                (case-split (<= l k))
                (case-split (integerp i))
                (case-split (integerp k))
                (case-split (integerp l))
                (case-split (integerp m))
                )
           (equal (cat (bits x i j) m (bits x k l) n)
                  (bits x i l)))
  :hints (("Goal" :in-theory (enable cat)
           :use ((:instance bits-plus-bits (n i) (p j) (m l))))))

(defthm cat-bitn-bits
    (implies (and (equal j (+ 1 k))
		  (equal n (+ 1 (- l) k))
                  (case-split (<= 1 m))
		  (case-split (<= l k))
		  (case-split (integerp j))
                  (case-split (integerp k))
                  (case-split (integerp l))
                  (case-split (integerp m))
		  )
	     (equal (cat (bitn x j) m (bits x k l) n)
		    (bits x j l)))
    :hints (("Goal" :in-theory (enable bitn))))

(defthm cat-bits-bitn
  (implies (and (equal j (+ 1 k))
                (case-split (<= (+ 1 (- j) i) m))
                (case-split (<= j i))
                (case-split (integerp i))
                (case-split (integerp j))
                (case-split (integerp k))
                (case-split (integerp m))
                )
           (equal (cat (bits x i j) m (bitn x k) 1)
                  (bits x i k)))
  :hints (("Goal" :in-theory (enable bitn))))

(defthm cat-bitn-bitn
  (implies (and (equal i (+ 1 j))
                (case-split (integerp i))
                (case-split (integerp j)))
           (equal (cat (bitn x i) 1 (bitn x j) 1)
                  (bits x i j)))
  :hints (("Goal" :in-theory (enable bitn))))




;may not want this enabled (but probably do want CAT-EQUAL-CONSTANT enabled)
;the BITS calls around X and Y in the conclusion allow us to drop the hyps that X and Y are bvecps.
(defthmd cat-split-equality
  (implies (and (case-split (bvecp k (+ m n))) ;if not, K can't be equal to the CAT expression
                (case-split (integerp m))
                (case-split (<= 0 m))
                (case-split (integerp n))
                (case-split (<= 0 n))
                )
           (equal (equal k (cat x m y n))
                  (and (equal (bits y (1- n) 0) (bits k (1- n) 0))
                       (equal (bits x (1- m) 0) (bits k (+ -1 m n) n)))))
  :hints (("Goal" :in-theory (enable cat-ignores-bits)
           :use ((:instance cat-bits-bits (x  (cat x m y n)) (i (+ -1 m n)) (j n) (k (+ -1 n)) (l 0))))))



;generalize this by dropping the bvecp-hyps and wrapping bits around x and y in the conclusion?
;follows trivially from   cat-split-equality
;prove a version of this without the bvecp hyps?
(defthm cat-equal-constant
  (implies (and (syntaxp (and (quotep k)
                              (quotep m)
                              (quotep n)))
                (case-split (bvecp y n))
                (case-split (bvecp x m))
                (case-split (< k (expt 2 (+ m n)))) ;drop!
                (case-split (integerp k))
                (case-split (<= 0 k))
                (case-split (integerp m))
                (case-split (<= 0 m))
                (case-split (integerp n))
                (case-split (<= 0 n))
                )
           (equal (equal k (cat x m y n))
                  (and (equal y (bits k (1- n) 0))
                       (equal x (bits k (+ -1 m n) n)))))
  :otf-flg t
  :hints (("Goal" :in-theory (enable cat-split-equality))))

;lacks the bvecp hyps.  do we want this or cat-equal-rewrite?
(defthm cat-equal-rewrite-alt
  (implies (and (case-split (natp n))
                (case-split (natp m))
                )
           (equal (equal (cat x1 m y1 n)
                         (cat x2 m y2 n))
                  (and (equal (bits x1 (1- m) 0) (bits x2 (1- m) 0))
                       (equal (bits y1 (1- n) 0) (bits y2 (1- n) 0)))))
  :hints (("Goal" :in-theory (enable cat-split-equality))))

;move hyps to conclusion?
(defthm cat-equal-rewrite
  (implies (and (case-split (bvecp x1 m))
                (case-split (bvecp y1 n))
                (case-split (bvecp x2 m))
                (case-split (bvecp y2 n))
                (case-split (integerp n))
                (case-split (<= 0 n))
                (case-split (integerp m))
                (case-split (<= 0 m))
                )
           (equal (equal (cat x1 m y1 n)
                         (cat x2 m y2 n))
                  (and (equal x1 x2)
                       (equal y1 y2))))
  :hints (("Goal" :in-theory (enable cat-split-equality))))

(defthm cat-bits-bits-bits
  (implies (and (<= k i)
                (<= l k)
                (<= j l)
                (integerp i)
                (integerp j)
                (integerp k)
                (integerp l)
                )
           (equal (cat (bits x i (+ 1 k))
                       (+ 2 i (- k))
                       (cat (bits x k l)
                            (+ 1 k (- l))
                            (bits x (1- l) j)
                            (+ l (- j)))
                       (+ 1 (- j) k))
                  (bits x i j)))
  :rule-classes nil)

#|
bits-dont-match can prove things like this:
(thm (IMPLIES (EQUAL 7 (BITS x 8 6))
              (NOT (EQUAL 3 (BITS x 15 6)))))
|#

(defthm bits-dont-match
  (implies (and (syntaxp (and (quotep i)
                              (quotep j)
                              (quotep k)))
                (equal (bits x i2 j2) k2) ;i2, j2, and k2 are free vars
                (syntaxp (and (quotep i2)
                              (quotep j2)
                              (quotep k2)))
                (<= j2 j) (<= j i) (<= i i2)
                (not (equal k (bits k2 (+ i (- j2)) (+ (- j2) j))))
                (<= 0 i) (<= 0 j) (<= 0 k) (<= 0 i2) (<= 0 j2) (<= 0 k2)
                (integerp i) (integerp j)  (integerp k) (integerp i2) (integerp j2) (integerp k2)
                )
           (equal (equal k (bits x i j))
                  nil))
  :otf-flg t
  :hints (("Goal" :in-theory ( set-difference-theories
                               (enable)
                              '( cat-bits-bits))
           :use (:instance cat-bits-bits-bits
                           (i i2)
                           (j j2)
                           (k (+ i (- j2)))
                           (l (+ j (- j2)))))))

(defthm bits-match
  (implies (and (syntaxp (and (quotep i)
                              (quotep j)
                              (quotep k)))
                (equal (bits x i2 j2) k2) ;i2, j2, and k2 are free vars
                (syntaxp (and (quotep i2)
                              (quotep j2)
                              (quotep k2)))
                (<= j2 j) (<= j i) (<= i i2)
                (equal k (bits k2 (+ i (- j2)) (+ (- j2) j)))
                (<= 0 i) (<= 0 j) (<= 0 k) (<= 0 i2) (<= 0 j2) (<= 0 k2)
                (integerp i) (integerp j)  (integerp k) (integerp i2) (integerp j2) (integerp k2)
                )
           (equal (equal k (bits x i j))
                  t))
  :otf-flg t
  :hints (("Goal" :in-theory ( set-difference-theories
                               (enable)
                              '( cat-bits-bits))
           :use (:instance cat-bits-bits-bits
                           (i i2)
                           (j j2)
                           (k (+ i (- j2)))
                           (l (+ j (- j2)))))))


;make into a rewrite rule
(defthm cat-with-slice-of-x-equal-x
  (implies (and (bvecp x (+ m n))
                (case-split (bvecp y n))
                (case-split (<= 0 m))
                (case-split (<= 0 n))
                (case-split (integerp m))
                (case-split (integerp n))
                )
           (equal (equal x (cat (bits x (+ -1 m n) n) m y n))
                  (equal (bits x (1- n) 0) y))
           )
  :hints (("Goal" :in-theory (disable CAT-BITS-BITS
                                      BITS-SPLIT-AROUND-ZERO)
           :use (:instance cat-bits-bits (i (+ -1 m n)) (l 0) (j n) (k (+ -1 n))))))

;cat-with-slice-of-x-equal-x won't match, so we use kk here
;add a syntaxp hyp?
(defthm cat-with-slice-of-x-equal-x-rewrite
  (implies (and (equal kk (+ -1 m n))
                (bvecp x (+ m n))
                (case-split (bvecp y n))
                (case-split (<= 0 m))
                (case-split (<= 0 n))
                (case-split (integerp m))
                (case-split (integerp n))
                )
           (equal (equal x (cat (bits x kk n) m y n))
                  (equal (bits x (1- n) 0) y))
           )
  :hints (("Goal" :in-theory (disable CAT-BITS-BITS
                                      BITS-SPLIT-AROUND-ZERO)
           :use (:instance cat-bits-bits (i (+ -1 m n)) (l 0) (j n) (k (+ -1 n))))))

;If X and Y have identical bits in the range [i..j], then they also match on any subrange [k..l] of [i..j]
(defthmd bits-equal-implies-subranges-equal-helper
  (implies (and (equal (bits x i j) (bits y i j))
                (<= j l)
                (<= k i)
                (case-split (integerp i))
                (case-split (integerp j))
                (case-split (integerp k))
                (case-split (integerp l))
                )
           (equal (equal (bits x k l) (bits y k l))
                  t))
  :rule-classes ((:rewrite :match-free :all))
  :hints (("Goal" :in-theory (disable cat-bits-bits cat-equal-rewrite cat-equal-rewrite-alt)
           :use ((:instance cat-equal-rewrite
                            (x1 (BITS X K L))
                            (y1 (BITS X (+ -1 L) J))
                            (x2 (BITS Y K L))
                            (y2 (BITS Y (+ -1 L) J))
                            (m (+ 1 k (* -1 l)))
                            (n (+ L (* -1 J))))
                 (:instance cat-equal-rewrite
                            (x1 (BITS X I (+ 1 K)))
                            (y1 (CAT (BITS X K L)
                                     (+ 1 k (* -1 l))
                                     (BITS X (+ -1 L) J)
                                     (+ L (* -1 J))))
                            (x2 (BITS Y I (+ 1 K)))
                            (y2 (CAT (BITS Y K L)
                                      (+ 1 k (* -1 l))
                                     (BITS Y (+ -1 L) J)
                                     (+ L (* -1 J))))
                            (m (+ 2 i (* -1 k)))
                            (n (+ 1 K (* -1 J))))
                 (:instance cat-bits-bits-bits (x x))
                 (:instance cat-bits-bits-bits (x y)))
           )))

(defthm bits-equal-implies-subranges-equal
  (implies (and (equal (bits x i j) (bits y i j))
                (<= j l)
                (<= k i)
                (case-split (integerp i))
                (case-split (integerp j))
                )
           (equal (equal (bits x k l) (bits y k l))
                  t))
  :rule-classes ((:rewrite :match-free :all))
  :hints (("Goal" :use ( bits-equal-implies-subranges-equal-helper)
           )))


#|

(thm
 (implies (and (integerp m) (integerp n) (<= 0 m) (<= 0 n) (bvecp x m)
                (bvecp y n))
          (equal (EQUAL (BINARY-CAT X M Y N) X)
                 (or (equal y 0) (equal n 0))))
 :hints (("Goal" :in-theory (enable binary-cat bvecp))))

;keep disabled?
(defthm cat-when-bits-of-x-are-0
  (implies (and (EQUAL (BITS X (1- M) 0) 0)
                (integerp m)
                (<= 0 m))
           (equal (cat x m y n)
                  (bits y (+ -1 n) 0))))
|# ; |