~ubuntu-branches/ubuntu/gutsy/acl2/gutsy

« back to all changes in this revision

Viewing changes to books/rtl/rel6/support/support/bits-proofs.lisp

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2006-12-04 10:35:42 UTC
  • mfrom: (1.1.5 upstream) (3.1.1 etch)
  • Revision ID: james.westby@ubuntu.com-20061204103542-68nf4pkilci0018n
Tags: 3.1-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
(in-package "ACL2")
 
2
 
 
3
(include-book "ground-zero")
 
4
(include-book "../../arithmetic/negative-syntaxp")
 
5
(include-book "../../arithmetic/power2p")
 
6
 
 
7
(local (include-book "../../arithmetic/top"))
 
8
(local (include-book "bvecp"))
 
9
 
 
10
(defund bvecp (x k)
 
11
  (declare (xargs :guard (integerp k)))
 
12
  (and (integerp x)
 
13
       (<= 0 x)
 
14
       (< x (expt 2 k))))
 
15
 
 
16
 
 
17
(defun expo-measure (x)
 
18
;  (declare (xargs :guard (and (real/rationalp x) (not (equal x 0)))))
 
19
  (cond ((not (rationalp x)) 0)
 
20
        ((< x 0) '(2 . 0))
 
21
        ((< x 1) (cons 1 (fl (/ x))))
 
22
        (t (fl x))))
 
23
 
 
24
(defund expo (x)
 
25
  (declare (xargs :guard t
 
26
                  :measure (expo-measure x)))
 
27
  (cond ((or (not (rationalp x)) (equal x 0)) 0)
 
28
        ((< x 0) (expo (- x)))
 
29
        ((< x 1) (1- (expo (* 2 x))))
 
30
        ((< x 2) 0)
 
31
        (t (1+ (expo (/ x 2))))))
 
32
 
 
33
(defun natp (x)
 
34
  (declare (xargs :guard t))
 
35
  (and (integerp x)
 
36
       (<= 0 x)))
 
37
 
 
38
 
 
39
;this book is still a mess
 
40
 
 
41
(defun fl (x)
 
42
  (declare (xargs :guard (real/rationalp x)))
 
43
  (floor x 1))
 
44
 
 
45
#|
 
46
 
 
47
The new version of bits is like Russinoff's "bits" but uses mod instead of rem.
 
48
The use of "mod" seems to allow nicer results to be proved.
 
49
 
 
50
For example, bits now *always* returns a non-negative integer.  Many hyps of other lemmas require
 
51
expressions to be non-negative integers, and with the old bits, this requires further checking of the
 
52
arguments (at worst, checking all the way to the leaves of the expression tree each time).
 
53
 
 
54
Add case-split to all hyps about i and j (indices to bits must be integers and j must be <= i or else weird
 
55
stuff may happen (but we can easily handle these cases).
 
56
 
 
57
|#
 
58
 
 
59
;In proofs about RTL terms, i and j should always be natural number constants
 
60
 
 
61
(defund bits (x i j)
 
62
  (declare (xargs :guard (and (natp x)
 
63
                              (natp i)
 
64
                              (natp j))
 
65
                  :verify-guards nil))
 
66
  (mbe :logic (if (or (not (integerp i))
 
67
                      (not (integerp j)))
 
68
                  0
 
69
                (fl (/ (mod x (expt 2 (1+ i))) (expt 2 j))))
 
70
       :exec  (if (< i j)
 
71
                  0
 
72
                (logand (ash x (- j)) (1- (ash 1 (1+ (- i j))))))))
 
73
 
 
74
(defthm bits-nonnegative-integerp-type
 
75
  (and (<= 0 (bits x i j))
 
76
       (integerp (bits x i j)))
 
77
  :hints (("Goal" :in-theory (enable bits)))
 
78
  :rule-classes (:type-prescription))
 
79
 
 
80
;this rule is no better than bits-nonnegative-integer-type and might be worse
 
81
(in-theory (disable (:type-prescription bits)))
 
82
 
 
83
;dup with bits-0!
 
84
(defthm bits-with-x-0
 
85
  (equal (bits 0 i j)
 
86
         0)
 
87
  :hints (("Goal" :in-theory (enable bits))))
 
88
 
 
89
(defthm bits-with-i-not-an-integer
 
90
  (implies (not (integerp i))
 
91
           (equal (bits x i j)
 
92
                  0))
 
93
  :hints (("Goal" :in-theory (enable bits))))
 
94
 
 
95
(defthm bits-with-j-not-an-integer
 
96
  (implies (not (integerp j))
 
97
           (equal (bits x i j)
 
98
                  0))
 
99
  :hints (("Goal" :in-theory (enable bits))))
 
100
 
 
101
(defthm bits-with-indices-in-the-wrong-order
 
102
  (implies (< i j)
 
103
           (equal (bits x i j)
 
104
                  0))
 
105
  :hints (("Goal" :in-theory (enable bits))))
 
106
 
 
107
(defthm bits-upper-bound
 
108
  (< (bits x i j) (expt 2 (+ 1 i (- j))))
 
109
  :rule-classes (:rewrite (:linear :trigger-terms ((bits x i j))))
 
110
  :hints (("Goal" :in-theory (enable bits 
 
111
                                     expt-minus
 
112
                                     expt-split))))
 
113
 
 
114
;has the right pattern to rewrite stuff like this: (<= (EXPT 2 J) (BITS Y (+ -1 J) 0)) to nil
 
115
(defthm bits-upper-bound-special
 
116
  (< (BITS x (1- i) 0) (EXPT 2 i))
 
117
  :hints (("Goal" :use (:instance bits-upper-bound (i (1- i)) (j 0))))
 
118
  )
 
119
 
 
120
;tigher bound
 
121
(defthm bits-upper-bound-tighter
 
122
  (implies (case-split (<= j i))
 
123
           (<= (bits x i j) (1- (expt 2 (+ i 1 (- j))))))
 
124
  :rule-classes (:rewrite (:linear :trigger-terms ((bits x i j))))
 
125
  :hints (("Goal" :cases ((rationalp x) (not (acl2-numberp x)))
 
126
           :in-theory (enable bits 
 
127
                              expt-minus
 
128
                              expt-split))))
 
129
 
 
130
  
 
131
;like  mod-upper-bound-3
 
132
(defthm bits-upper-bound-2
 
133
  (implies (and (<= (expt 2 (+ 1 i (- j))) z) ;backchain-limit?
 
134
                ;(case-split (integerp i))
 
135
                ;(case-split (integerp j))
 
136
                )
 
137
           (< (bits x i j) z)))
 
138
 
 
139
 
 
140
 
 
141
#|
 
142
;I have many theorems dealing with the simplification of bits of a sum
 
143
 
 
144
(include-book
 
145
 "lowbits")
 
146
 
 
147
 
 
148
;taking sort of a long time (3-4 secs)
 
149
(defthm bits-sum-lowbits
 
150
  (implies (and (rationalp x)
 
151
                (rationalp y)
 
152
                (integerp i)
 
153
                (integerp j))
 
154
           (equal (bits (+ x             y) i j)
 
155
                  (bits (+ (lowbits x i) y) i j)))
 
156
  :hints (("Goal" :in-theory (enable ;mod-cancel
 
157
                              bits
 
158
                              lowbits))))
 
159
(in-theory (disable bits-sum-lowbits))
 
160
 
 
161
;special case of the above -- helps rewrite the constant to a unique (positive) value
 
162
;make another rule to handle negative constants
 
163
(defthm bits-sum-reduce-leading-constant
 
164
  (implies (and (syntaxp (and (quotep x) (>= (cadr x) (expt 2 (+ (cadr i) 1)))))
 
165
                (rationalp x)
 
166
                (rationalp y)
 
167
                (integerp i)
 
168
                (integerp j))
 
169
           (equal (bits (+ x             y) i j)
 
170
                  (bits (+ (lowbits x i) y) i j)))
 
171
  :hints (("Goal" :use bits-sum-lowbits )))
 
172
 
 
173
(defun oldbits (x i j)
 
174
  (fl (/ (rem x (expt 2 (1+ i))) (expt 2 j))))
 
175
 
 
176
(in-theory (disable bits)) ;move up
 
177
 
 
178
;(in-theory (disable INTEGER-HAS-DENOM-1-OTHER-WAY))
 
179
 
 
180
(in-theory (disable rem))
 
181
 
 
182
 
 
183
|#
 
184
 
 
185
;a is a free var
 
186
(defthm bits-force
 
187
  (implies (and (<= (* a (expt 2 (+ i 1))) x)
 
188
                (< x (* (1+ a) (expt 2 (+ i 1))))
 
189
                (integerp x)
 
190
                (integerp i)
 
191
;                (<= 0 i)
 
192
                (integerp a)
 
193
                )
 
194
           (equal (bits x i 0) (- x (* a (expt 2 (+ i 1))))))
 
195
  :rule-classes nil
 
196
  :hints (("Goal" :in-theory (enable bits)
 
197
           :use (:instance  mod-force-eric (y (expt 2 (+ i 1)))))))
 
198
 
 
199
(defthm bits-force-with-a-chosen-neg
 
200
  (implies (and (< x 0)
 
201
                (<= (* -1 (expt 2 (+ i 1))) x)
 
202
;                (<= 0 i)
 
203
                (integerp x)
 
204
                (integerp i)
 
205
                )
 
206
           (equal (bits x i 0) (- x (* -1 (expt 2 (+ i 1))))))
 
207
  :hints (("Goal" 
 
208
           :use (:instance bits-force (a -1)))))
 
209
                                      
 
210
;remove:?
 
211
;(in-theory (disable bits-force))
 
212
 
 
213
;expensive?
 
214
;make a corollary?
 
215
(defthm bits-shift
 
216
  (implies (and ;(rationalp x)
 
217
            (case-split (integerp n))
 
218
            (case-split (integerp i))
 
219
            (case-split (integerp j))
 
220
            )
 
221
           (and (equal (bits (* (expt 2 n) x) i j)
 
222
                       (bits x (- i n) (- j n)))
 
223
                (equal (bits (* x (expt 2 n)) i j)
 
224
                       (bits x (- i n) (- j n)))))
 
225
  :hints (("Goal" :cases ((and (acl2-numberp n) (not (rationalp n)))
 
226
                          (and (rationalp n) (not (integerp n)))
 
227
                          (integerp n))
 
228
           :in-theory (e/d (expt-minus
 
229
                              mod-cancel
 
230
                              bits 
 
231
                              expt-split
 
232
                            
 
233
                              )
 
234
                           (  ;these are disabled to speed up he proof:
 
235
                            INTEGERP-PROD-OF-3-FIRST-AND-LAST
 
236
                              INTEGERP-PROD-OF-3-LAST-TWO
 
237
                              a10
 
238
                              a13)))))
 
239
 
 
240
; Basically a restatement of bits-shift:
 
241
(defthm bits-shift-up-1
 
242
  (implies (and (integerp k)
 
243
                (integerp i)
 
244
                (integerp j))
 
245
           (equal (bits (* (expt 2 k) x) i j)
 
246
                  (bits x (- i k) (- j k))))
 
247
  :hints (("Goal" :use ((:instance bits-shift (n k)))))
 
248
  :rule-classes ())
 
249
 
 
250
#| original:
 
251
(defthm bits-shift
 
252
  (implies (and ;(rationalp x)
 
253
            (case-split (integerp n))
 
254
            (case-split (integerp i))
 
255
            (case-split (integerp j))
 
256
            )
 
257
           (and (equal (bits (* (expt 2 n) x) i j)
 
258
                       (bits x (- i n) (- j n)))
 
259
                (equal (bits (* x (expt 2 n)) i j)
 
260
                       (bits x (- i n) (- j n)))))
 
261
  :hints (("Goal" :cases ((and (acl2-numberp n) (not (rationalp n)))
 
262
                          (and (rationalp n) (not (integerp n)))
 
263
                          (integerp n))
 
264
           :in-theory (enable expt-minus
 
265
                              mod-cancel
 
266
                              bits 
 
267
                              expt-split))))
 
268
|#
 
269
 
 
270
(local (in-theory (enable mod-cancel)))
 
271
  
 
272
;allows you to split bit vectors into two parts
 
273
;free var n (where to split)
 
274
;resembles doc's bits-plus-bits-1 and another rule in merge
 
275
(defthm bits-plus-bits2
 
276
  (implies (and ;(rationalp x)
 
277
                (integerp i) 
 
278
                (integerp j)
 
279
                (integerp n)
 
280
                (<= j n)
 
281
                (<= n i)
 
282
                )
 
283
           (equal (bits x i j)
 
284
                  (+ (* (bits x i n) (expt 2 (- n j)))
 
285
                     (bits x (1- n) j))))
 
286
  :rule-classes nil
 
287
  :hints (("Goal" :in-theory (enable bits mod expt-split expt-minus))))
 
288
 
 
289
;a hint that worked for this before I made such nice rules is in junk.lisp under bits-plus-bits-hint
 
290
 
 
291
;(in-theory (disable bits-plus-bits-1)) ;keep disabled
 
292
 
 
293
 
 
294
#|
 
295
;use mbitn?
 
296
;could use even-odd phrasing?
 
297
(defthm bits-down-to-1-case-split
 
298
  (implies (and (integerp x)
 
299
                (<= 0 x)
 
300
                (integerp i)
 
301
                (<= 1 i))
 
302
           (equal (bits x i 1)
 
303
                  (if (equal (bitn x 0) 0)
 
304
                      (/ (bits x i 0) 2) ;use the fact that we know bit 0 is 0?
 
305
                    (/ (1- (bits x i 0)) 2) ;use the fact that we know bit 0 is 1?
 
306
                    )))
 
307
  :otf-flg t
 
308
  :hints (("Goal" :in-theory (enable bits mod bitn))))
 
309
|#
 
310
 
 
311
;this really has two separate cases
 
312
;generalize with j not 0?
 
313
(defthm bits-split-around-zero
 
314
  (implies (and (>= x (- (expt 2 (+ i 1))))
 
315
                (< x (expt 2 (+ i 1)))
 
316
                (integerp x)
 
317
                (case-split (integerp i))
 
318
                (case-split (<= 0 i))
 
319
                )
 
320
           (equal (bits x i 0)
 
321
                  (if (<= 0 x)
 
322
                      x
 
323
                    (+ x (expt 2 (+ i 1))))))
 
324
 :hints (("Goal" :in-theory (enable bits)
 
325
          :use ((:instance bits-force (a 0))
 
326
                (:instance bits-force (a -1))))))
 
327
 
 
328
 
 
329
;(local (in-theory (disable expt-inverse)))
 
330
 
 
331
(defthm bits-bvecp-simple
 
332
  (implies (equal k (+ 1 i (* -1 j)))
 
333
           (bvecp (bits x i j) k))
 
334
  :hints (("Goal" :in-theory (enable 
 
335
                              bvecp))))
 
336
 
 
337
 
 
338
 
 
339
(defthm bits-bvecp
 
340
  (implies (and (<= (+ 1 i (- j)) k)
 
341
                (case-split (integerp k))
 
342
                )
 
343
           (bvecp (bits x i j) k))
 
344
  :hints (("Goal" :use ((:instance bits-bvecp-simple
 
345
                                   (k (+ 1 i (* -1 j)))))
 
346
           :in-theory (disable bits-bvecp-simple))))
 
347
 
 
348
(in-theory (disable bits-bvecp-simple))
 
349
 
 
350
;do we want this rule enabled?
 
351
(defthm bits-bvecp-fw
 
352
  (implies (equal n (- (1+ i) j)) ; note equal here to help with the fw chaining
 
353
           (bvecp (bits x i j) n))
 
354
  :rule-classes
 
355
  ((:forward-chaining :trigger-terms ((bits x i j)))))
 
356
 
 
357
;these may be made moot by my method of using lowbits with bits of a sum
 
358
 
 
359
#|
 
360
(defthm bits-sum-simplify-first-term
 
361
  (implies (and (>= (abs x) (expt 2 (+ j 1))) ;prevents loop
 
362
                (rationalp x)
 
363
                (rationalp y)
 
364
                (integerp j)
 
365
                (<= 0 j)
 
366
                )
 
367
           (equal (bits (+ x y) j 0)
 
368
                  (bits (+ (lowbits x j) y) j 0)))
 
369
  :hints (("Goal" :in-theory (enable lowbits
 
370
                                     bits))))
 
371
 
 
372
(defthm bits-sum-simplify-second-term
 
373
  (implies (and (>= (abs x) (expt 2 (+ j 1))) ;prevents loop
 
374
                (rationalp x)
 
375
                (rationalp y)
 
376
                (integerp j)
 
377
                (<= 0 j)
 
378
                )
 
379
           (equal (bits (+ y x) j 0)
 
380
                  (bits (+ (lowbits x j) y) j 0)))
 
381
  :hints (("Goal" :in-theory (enable lowbits
 
382
                                     bits))))
 
383
 
 
384
|#
 
385
 
 
386
 
 
387
(local (in-theory (disable mod-cancel)))
 
388
 
 
389
;better names: make the dropped term x, the others a,b,c,...
 
390
;;; more bits thms like this!
 
391
 
 
392
(defthm bits-sum-drop-irrelevant-term-2-of-2
 
393
  (implies (integerp (/ y (expt 2 (+ 1 i))))
 
394
           (equal (bits (+ x y) i j)
 
395
                  (bits x i j)))
 
396
  :hints (("Goal" :cases ((rationalp y) (not (acl2-numberp y)))
 
397
           :in-theory (enable bits))))
 
398
 
 
399
(defthm bits-sum-drop-irrelevant-term-1-of-2
 
400
  (implies (integerp (/ y (expt 2 (+ 1 i))))
 
401
           (equal (bits (+ y x) i j)
 
402
                  (bits x i j)))
 
403
  :hints (("Goal"  :cases ((rationalp y) (not (acl2-numberp y)))
 
404
           :in-theory (enable bits))))
 
405
 
 
406
(defthm bits-sum-drop-irrelevant-term-3-of-3
 
407
  (implies (integerp (/ y (expt 2 (+ 1 i))))
 
408
           (equal (bits (+ w x y) i j)
 
409
                  (bits (+ w x) i j)))
 
410
  :hints (("Goal" :in-theory (disable bits-sum-drop-irrelevant-term-1-of-2
 
411
                                      bits-sum-drop-irrelevant-term-2-of-2)
 
412
           :use (:instance bits-sum-drop-irrelevant-term-1-of-2 (x (+ w x))))))
 
413
 
 
414
(defthm bits-sum-drop-irrelevant-term-2-of-3
 
415
  (implies (integerp (/ y (expt 2 (+ 1 i))))
 
416
           (equal (bits (+ w y x) i j)
 
417
                  (bits (+ w x) i j)))
 
418
  :hints (("Goal" :in-theory (disable bits-sum-drop-irrelevant-term-3-of-3
 
419
                                      bits-sum-drop-irrelevant-term-1-of-2
 
420
                                      bits-sum-drop-irrelevant-term-2-of-2)
 
421
           :use (:instance bits-sum-drop-irrelevant-term-1-of-2 (x (+ w x))))))
 
422
#|
 
423
(defthm bits-sum-drop-irrelevant-term-1-of-3
 
424
  (implies (and (integerp x)
 
425
                (integerp y)
 
426
                (integerp w)
 
427
 
 
428
                (integerp i)
 
429
                (<= 0 i)
 
430
                (integerp j)
 
431
                (<= 0 j)
 
432
 
 
433
                (integerp (/ y (expt 2 (+ 1 i))))
 
434
                )
 
435
           (equal (bits (+ y w x) i j)
 
436
                  (bits (+ w x) i j)))
 
437
  :hints (("Goal" :in-theory (disable bits-sum-drop-irrelevant-term-2-of-3
 
438
                                      bits-sum-drop-irrelevant-term-3-of-3
 
439
                                      bits-sum-drop-irrelevant-term-1-of-2
 
440
                                      bits-sum-drop-irrelevant-term-2-of-2)
 
441
           :use (:instance bits-sum-drop-irrelevant-term-1-of-2 (x (+ w x))))))
 
442
|#
 
443
 
 
444
 
 
445
#|
 
446
 
 
447
This series of events deals with simplifying expressions like
 
448
(equal (bits x 8 0) 
 
449
       (+ (bits x 6 0) k))
 
450
Intuitively, bits 6 down-to 0 appear on both sides of the sum and should be "cancelled".  
 
451
The remaining bits will need to be "shifted" back into place.
 
452
 
 
453
More rules are probably needed to make the theory complete.
 
454
|#
 
455
 
 
456
#|
 
457
(defthm bits-cancel-lowbits-in-comparison
 
458
  (implies (and (> i+ i)
 
459
                (rationalp k)
 
460
                (rationalp x)
 
461
                (integerp i)
 
462
                (integerp j)
 
463
                (<= j i)
 
464
                (integerp i+)
 
465
                )
 
466
           (equal (equal (+ k (bits x i j))
 
467
                         (bits x i+ j))
 
468
                  (equal (* (expt 2 (+ 1 i (- j))) 
 
469
                            (BITS X I+ (+ 1 I))) 
 
470
                         k)))
 
471
  :hints (("Goal"          
 
472
           :use (:instance bits-plus-bits-1 (n (+ i 1)) (i i+))
 
473
           :in-theory (enable expt-split)
 
474
           )))
 
475
 
 
476
(defthm bits-cancel-lowbits-in-comparison-alt-2
 
477
  (implies (and (> i+ i)
 
478
                (rationalp k)
 
479
                (rationalp x)
 
480
                (integerp i)
 
481
                (integerp j)
 
482
                (<= j i)
 
483
                (integerp i+)
 
484
                )
 
485
           (equal (equal (+ (bits x i j) k)
 
486
                         (bits x i+ j))
 
487
                  (equal (* (expt 2 (+ 1 i (- j))) 
 
488
                            (BITS X I+ (+ 1 I))) 
 
489
                         k)))
 
490
  :hints (("Goal"          
 
491
           :use (:instance bits-plus-bits-1 (n (+ i 1)) (i i+))
 
492
           :in-theory (enable expt-split)
 
493
           )))
 
494
|#
 
495
 
 
496
#|
 
497
;todo: rephrase the conclusion of the above by moving the "constant" (* 2 (EXPT 2 I) (/ (EXPT 2 J))) to the other
 
498
;side
 
499
;a good idea since k is usually divisible by that quantity (with the rule above, we often end up with an
 
500
equality in which each side should have a power of 2 divided out of it
 
501
; not needed if we have good meta rules for cancelling factors from an inequality
 
502
 
 
503
 
 
504
(defthm bits-cancel-lowbits-in-comparison-2
 
505
  (implies (and (> i+ i)
 
506
                (rationalp k)
 
507
                (rationalp x)
 
508
                (integerp i)
 
509
                (integerp j)
 
510
                (<= j i)
 
511
                (integerp i+)
 
512
 
 
513
                )
 
514
           (equal (equal (+ k (bits x i j))
 
515
                         (bits x i+ j))
 
516
                  (equal (*  
 
517
                            (BITS X I+ (+ 1 I))) 
 
518
                         (/ k (expt 2 (+ 1 i (- j)))))))
 
519
  :hints (("Goal"          
 
520
           :use (:instance bits-cancel-lowbits-in-comparison)
 
521
           :in-theory (set-difference-theories
 
522
                       (enable expt-split)
 
523
                       '(bits-cancel-lowbits-in-comparison))
 
524
           )))
 
525
|#
 
526
 
 
527
#|
 
528
 
 
529
(defthm bits-cancel-lowbits-in-comparison-3
 
530
  (implies (and (> i+ i)
 
531
                (rationalp k)
 
532
                (rationalp x)
 
533
                (integerp i)
 
534
                (integerp j)
 
535
                (<= j i)
 
536
                (integerp i+)
 
537
                (rationalp y)
 
538
                )
 
539
           (equal (equal (+ (bits x i+ j) k)
 
540
                         (+ y (bits x i j)))
 
541
                  (equal (* (expt 2 (+ 1 i (- j )))
 
542
                            (BITS X I+ (+ 1 I))) 
 
543
                         (- y k))))
 
544
  :hints (("Goal"          
 
545
           :use (:instance bits-plus-bits-1 (n (+ i 1)) (i i+))
 
546
           :in-theory (set-difference-theories
 
547
                       (enable expt-split)
 
548
                       '(
 
549
                         BITS-CANCEL-LOWBITS-IN-COMPARISON-ALT-2))
 
550
           )))
 
551
 
 
552
;better conclusion?
 
553
(defthm bits-cancel-lowbits-in-comparison-no-constant
 
554
  (implies (and (> i+ i)
 
555
                (rationalp x)
 
556
                (integerp i)
 
557
                (integerp j)
 
558
                (<= j i)
 
559
                (integerp i+)
 
560
                )
 
561
           (equal (equal (bits x i j)
 
562
                         (bits x i+ j))
 
563
                  (equal (* 2 (EXPT 2 I)
 
564
                           (/ (EXPT 2 J))
 
565
                           (BITS X I+ (+ 1 I))) 
 
566
                         0)))
 
567
  :hints (("Goal"          
 
568
           :use (:instance bits-plus-bits-1 (n (+ i 1)) (i i+))
 
569
           :in-theory (set-difference-theories
 
570
                       (enable expt-split)
 
571
                       '( BITS-CANCEL-LOWBITS-IN-COMPARISON-ALT-2
 
572
                          ))
 
573
           )))
 
574
 
 
575
(defthm bits-cancel-lowbits-in-comparison-no-constant-2
 
576
  (implies (and (> i+ i)
 
577
                (rationalp x)
 
578
                (integerp i)
 
579
                (integerp j)
 
580
                (<= j i)
 
581
                (integerp i+)
 
582
                )
 
583
           (equal (equal (bits x i+ j)
 
584
                         (bits x i j))
 
585
                  (equal (* 2 (EXPT 2 I)
 
586
                           (/ (EXPT 2 J))
 
587
                           (BITS X I+ (+ 1 I))) 
 
588
                         0)))
 
589
  :hints (("Goal"          
 
590
           :use (:instance bits-plus-bits-1 (n (+ i 1)) (i i+))
 
591
           :in-theory (set-difference-theories
 
592
                       (enable expt-split)
 
593
                       '( BITS-CANCEL-LOWBITS-IN-COMPARISON-ALT-2
 
594
))
 
595
           )))
 
596
 
 
597
;the theory above (cancelling bits in comparisons) is not complete
 
598
;it should also deal with bitn
 
599
;perhaps a bind-free rule would be a good idea here?
 
600
 
 
601
|#
 
602
 
 
603
 
 
604
;kind of yucky...
 
605
(defthm bits-minus
 
606
  (implies (and (case-split (rationalp x))
 
607
                (case-split (integerp i))
 
608
                (case-split (<= j i))
 
609
                (case-split (integerp j))
 
610
                )
 
611
           (equal (bits (* -1 x) i j)
 
612
                  (if (integerp (* 1/2 x (/ (expt 2 i))))
 
613
                      0
 
614
                    (if (integerp (* x (/ (expt 2 j))))
 
615
                        (+ (* 2 (expt 2 i) (/ (expt 2 j))) (- (bits x i j)))
 
616
                      (+ -1 (* 2 (expt 2 i) (/ (expt 2 j))) (- (bits x i j)))))))
 
617
  :hints (("goal" :in-theory (enable mod-mult-of-n
 
618
                                     bits
 
619
                                     expt-split)))
 
620
  )
 
621
 
 
622
 
 
623
 
 
624
 
 
625
 
 
626
;expensive?
 
627
(defthm bits-minus-alt
 
628
  (implies (and (syntaxp (negative-syntaxp x))
 
629
                (case-split (rationalp x))
 
630
                (case-split (integerp i))
 
631
                (case-split (<= j i))
 
632
                (case-split (integerp j))
 
633
                )
 
634
           (equal (bits x i j)
 
635
                  (if (integerp (* 1/2 (- X) (/ (EXPT 2 I))))
 
636
                    0
 
637
                    (if (INTEGERP (* (- X) (/ (EXPT 2 J))))
 
638
                        (+ (* 2 (EXPT 2 I) (/ (EXPT 2 J))) (- (bits (- x) i j)))
 
639
                      (+ -1 (* 2 (EXPT 2 I) (/ (EXPT 2 J))) (- (bits (- x) i j)))))))
 
640
  :hints (("Goal" :in-theory (disable bits-minus)
 
641
           :use (:instance bits-minus (x (- x)))))
 
642
  )
 
643
 
 
644
 
 
645
 
 
646
 
 
647
#|
 
648
(defthm bits-shift-alt
 
649
  (implies (and (syntaxp (should-have-a-2-factor-divided-out x))
 
650
                (> j 0) ;restricts application 
 
651
                (rationalp x)
 
652
;                (integerp n)
 
653
                (integerp i)
 
654
                (integerp j)
 
655
                )
 
656
           (equal (bits x i j)
 
657
                  (bits (/ x 2) (- i 1) (- j 1))))
 
658
  :hints (("Goal" :in-theory (set-difference-theories
 
659
                              (enable bits)
 
660
                              '(bits-shift))
 
661
           :use (:instance bits-shift (x (/ x 2)) (n 1)))))
 
662
 
 
663
|#
 
664
 
 
665
 
 
666
;drops hyps like this: (<= (BITS x 30 24) 253)
 
667
;Recall that <= gets rewritten to < during proofs
 
668
(defthm bits-drop-silly-upper-bound
 
669
  (implies (and (syntaxp (quotep k))
 
670
                (>= k (1- (expt 2 (+ 1 i (- j)))))
 
671
                (case-split (<= j i))
 
672
;                (case-split (rationalp x))
 
673
                (case-split (integerp i))
 
674
                (case-split (integerp j))
 
675
;                (case-split (rationalp k))
 
676
                )
 
677
           (equal (< k (bits x i j))
 
678
                  nil)))
 
679
 
 
680
;rewrite things like (<= 4096 (BITS x 23 12)) to false
 
681
;Recall that <= gets rewritten to < during proofs
 
682
(defthm bits-drop-silly-lower-bound
 
683
  (implies (and (syntaxp (quotep k))
 
684
                (> k (1- (expt 2 (+ 1 i (- j)))))
 
685
                (case-split (<= j i))
 
686
;                (case-split (rationalp x))
 
687
                (case-split (integerp i))
 
688
                (case-split (integerp j))
 
689
;                (case-split (rationalp k))
 
690
                )
 
691
  (equal (< (bits x i j) k)
 
692
         t)))
 
693
 
 
694
;rewrite (< -64 (BITS <signal> 64 59)) to t
 
695
(defthm bits-drop-silly-bound-3
 
696
  (implies (and (syntaxp (quotep k))
 
697
                (< k 0)
 
698
                )
 
699
  (equal (< k (bits x i j))
 
700
         t)))
 
701
 
 
702
(defthm bits-drop-silly-bound-4
 
703
  (implies (and (syntaxp (quotep k))
 
704
                (<= k 0)
 
705
                )
 
706
  (equal (< (bits x i j) k)
 
707
         nil)))
 
708
 
 
709
(defthm bits-<-1
 
710
  (equal (< (bits x i j) 1)
 
711
         (equal (bits x i j) 0)))
 
712
 
 
713
;put bits-cancel- in the name?
 
714
(defthm bits-at-least-zeros
 
715
  (implies (and (syntaxp (quotep k))
 
716
                (equal k (expt 2 (- j2 j)))
 
717
                (<= j j2)
 
718
                (case-split (rationalp x))
 
719
                (case-split (integerp i))
 
720
                (case-split (integerp j))
 
721
                (case-split (integerp j2))
 
722
                )
 
723
  (equal (< (BITS x i j)
 
724
             (* k (BITS x i j2)))
 
725
         nil))
 
726
  :hints (("Goal" :use (:instance bits-plus-bits2 (n j2)))))
 
727
 
 
728
(defthm bits-upper-with-subrange
 
729
  (implies (and (syntaxp (quotep k))
 
730
                (<= j j2)
 
731
                (equal k (expt 2 (- j2 j)))
 
732
                (case-split (<= j2 i)) ;drop?
 
733
                (case-split (rationalp x))
 
734
                (case-split (integerp i))
 
735
                (case-split (integerp j))
 
736
                (case-split (integerp j2))
 
737
                )
 
738
           (< (BITS x i j) 
 
739
              (BINARY-+ k (BINARY-* k (BITS x i j2)))))
 
740
  :hints (("Goal" :use (:instance bits-plus-bits2 (j j) (n j2)))))
 
741
 
 
742
(defthm bits-upper-with-subrange-alt
 
743
  (implies (and (syntaxp (quotep k))
 
744
                (<= j j2)
 
745
                (equal k (expt 2 (- j2 j)))
 
746
                (case-split (<= j2 i)) ;drop?
 
747
                (case-split (rationalp x))
 
748
                (case-split (integerp i))
 
749
                (case-split (integerp j))
 
750
                (case-split (integerp j2))
 
751
                )
 
752
           (equal (< (BINARY-+ k (BINARY-* k (BITS x i j2))) 
 
753
                     (BITS x i j))
 
754
                  nil))
 
755
  :hints (("Goal" :use (:instance bits-plus-bits2 (j j) (n j2)))))
 
756
 
 
757
 
 
758
;make another version for k negative?
 
759
(defthm bits-equal-impossible-constant
 
760
  (implies (and (syntaxp (quotep k))
 
761
                (<= (expt 2 (+ 1 i (- j))) k)
 
762
                )
 
763
           (not (equal (bits x i j) k))))
 
764
 
 
765
 
 
766
 
 
767
 
 
768
#|
 
769
;degenerate case
 
770
;rename?
 
771
;expensive
 
772
(defthm bits-sum-drop-irrelevant-term-1-of-1
 
773
  (implies (and (rationalp x) ;(integerp x)
 
774
 
 
775
                (integerp i)
 
776
                (<= 0 i)
 
777
                (integerp j)
 
778
                (<= 0 j)
 
779
 
 
780
                (integerp (/ x (expt 2 (+ 1 i))))
 
781
                )
 
782
           (equal (bits x i j)
 
783
                  0))
 
784
  :hints (("Goal" :in-theory (enable bits bits)))
 
785
  :rule-classes ((:rewrite :backchain-limit-lst (nil nil nil nil nil 1)))
 
786
)
 
787
|#
 
788
 
 
789
(defthm bits-with-x-not-rational
 
790
  (implies (not (rationalp x))
 
791
           (equal (bits x i j)
 
792
                  0))
 
793
  :hints (("Goal" :in-theory (set-difference-theories
 
794
                              (enable bits)
 
795
                              '( ;REARRANGE-FRACTIONAL-COEFS-<
 
796
                                )))))
 
797
 
 
798
 
 
799
 
 
800
(defthm bits-compare-to-zero
 
801
  (implies (and (case-split (rationalp x))
 
802
                (case-split (integerp i))
 
803
                (case-split (integerp j))
 
804
                )
 
805
           (equal (not (< 0 (bits x i j)))
 
806
                  (equal 0 (bits x i j)))))
 
807
 
 
808
;expensive?
 
809
 
 
810
(encapsulate
 
811
 ()
 
812
 
 
813
 (defthmd bits-ignore-negative-bits-of-integer-main-case
 
814
   (implies (and (<= j 0)
 
815
                 (integerp x) 
 
816
                 (case-split (integerp j))
 
817
                 (case-split (<= j i))
 
818
                 )
 
819
            (equal (bits x i j)
 
820
                   (* (expt 2 (- j)) (bits x i 0))))
 
821
   :hints (("Goal" :cases ((<= j i))
 
822
            :in-theory (enable bits)))
 
823
   )
 
824
 
 
825
 (defthm bits-ignore-negative-bits-of-integer
 
826
   (implies (and (and (syntaxp (not (and (quotep j) (equal 0 (cadr j)))))) ;prevents loops
 
827
                 (<= j 0)
 
828
                 (integerp x)
 
829
                 (case-split (integerp j))
 
830
                 )
 
831
            (equal (bits x i j)
 
832
                   (* (expt 2 (- j)) (bits x i 0))))
 
833
   :hints (("Goal"; :cases ((<= j i))
 
834
            :use bits-ignore-negative-bits-of-integer-main-case
 
835
            :in-theory (enable)))
 
836
   )
 
837
 )
 
838
 
 
839
 
 
840
;disable since it can be bad to leave "naked" signals?
 
841
(defthmd bits-does-nothing-2
 
842
  (implies (and (<= j 0) ;a bit strange (j will usually be zero?)
 
843
                (bvecp x (+ i 1)) ;expand?
 
844
                (case-split (integerp i))
 
845
                (case-split (integerp j))
 
846
                )
 
847
           (equal (bits x i j)
 
848
                  (* (expt 2 (- j)) x)))
 
849
  :hints (("Goal" :cases ((<= j 0))
 
850
           :in-theory (enable bits bvecp))))
 
851
 
 
852
 
 
853
 
 
854
 
 
855
 
 
856
 
 
857
#|
 
858
;(include-book "factor-out")
 
859
 
 
860
(defthm bits-shift-better
 
861
  (implies (and (bind-free (can-take-out-numeric-power-of-2 x) (c))
 
862
                (force (power2p c))
 
863
                (case-split (integerp i))
 
864
                (case-split (integerp j))
 
865
                )
 
866
           (equal (bits x i j)
 
867
                  (bits (/ x c) (- i (expo c)) (- j (expo c)))))
 
868
  :hints (("Goal" :in-theory (set-difference-theories
 
869
                              (enable power2p)
 
870
                              '(bits-shift))
 
871
           :use  (:instance bits-shift 
 
872
                           (x (/ x c))
 
873
                           (n (expo c))
 
874
                           ))))
 
875
 
 
876
|#
 
877
 
 
878
 
 
879
 
 
880
 
 
881
(defthm bits-does-nothing
 
882
  (implies (and (bvecp x (1+ i))
 
883
                (case-split (integerp i))
 
884
;                (case-split (integerp j))
 
885
                )
 
886
           (equal (bits x i 0)
 
887
                  x))
 
888
  :hints (("Goal" :in-theory (enable bits-does-nothing-2)
 
889
           :cases ((<= i -1))))
 
890
  )
 
891
 
 
892
(in-theory (disable bits-does-nothing))
 
893
 
 
894
 
 
895
 
 
896
(defthm bits-with-bad-index-2
 
897
  (IMPLIES (NOT (INTEGERP i))
 
898
           (EQUAL (BITS x (1- i) 0)
 
899
                  0))
 
900
  :hints (("Goal" :in-theory (enable bits))))
 
901
 
 
902
(local (defthm bvecp-bits-0-aux
 
903
   (implies (and (case-split (natp i)) ;(natp i)
 
904
                 (case-split (<= j i))
 
905
                 (bvecp x j))
 
906
            (equal (bits x i j) 0))
 
907
   :hints (("Goal" :in-theory (enable bits bvecp)
 
908
            :use (;(:instance mod-equal (m x) (n (expt 2 (1+ i))))
 
909
                  (:instance expt-weak-monotone (n j) (m (1+ i))))))))
 
910
 
 
911
(defthmd bvecp-bits-0
 
912
   (implies (bvecp x j)
 
913
            (equal (bits x i j) 0))
 
914
   :hints (("Goal" :cases ((< i j))
 
915
            :in-theory (set-difference-theories
 
916
                        (enable natp)
 
917
                        '( bvecp )))))
 
918
 
 
919
;add case-split to hyps?
 
920
(local
 
921
 (defthm bits-drop-from-minus-original
 
922
   (implies (and (<= y x)
 
923
                 (bvecp x n)
 
924
                 (bvecp y n)
 
925
                 )
 
926
            (equal (bits (+ x (* -1 y)) (1- n) 0)
 
927
                   (+ x (* -1 y))
 
928
                   ))
 
929
   :hints (("Goal" :cases ((integerp n))
 
930
            :in-theory (enable bvecp)))
 
931
   ))
 
932
 
 
933
;add case-split to hyps?
 
934
(defthm bits-drop-from-minus
 
935
  (implies (and (bvecp x (1+ i))
 
936
                (bvecp y (1+ i))
 
937
                (<= y x)
 
938
                (case-split (acl2-numberp i)))
 
939
           (equal (bits (+ x (* -1 y)) i 0)
 
940
                  (+ x (* -1 y))))
 
941
  :hints (("Goal" :use ((:instance bits-drop-from-minus-original
 
942
                                   (n (1+ i))))
 
943
           :in-theory (disable bits-drop-from-minus-original))))
 
944
 
 
945
(defthm bits-tail
 
946
   (implies (and (bvecp x (1+ i))
 
947
                 (case-split (acl2-numberp i)))
 
948
            (equal (bits x i 0)
 
949
                   x))
 
950
   :hints (("Goal" :in-theory (enable bvecp bits-does-nothing))))
 
951
 
 
952
(defthm bits-tail-special
 
953
   (implies (bvecp x i)
 
954
            (equal (bits x (1- i) 0)
 
955
                   x))
 
956
   :hints (("Goal" :cases ((acl2-numberp i)))))
 
957
 
 
958
 
 
959
#|
 
960
 
 
961
comments from bits.lisp (these may duplicate comments in this book)
 
962
 
 
963
;why have this?
 
964
(defthm bits-shift-out-even
 
965
  (implies (and (integerp x)
 
966
                (evenp x)
 
967
                (integerp i)
 
968
                (integerp j)
 
969
                (>= i j))
 
970
           (equal (bits x i j)
 
971
                  (bits (/ x 2) (- i 1) (- j 1) )))
 
972
  :hints (("Goal" :in-theory (enable expt-minus
 
973
                                     bits 
 
974
                                     mod-cancel 
 
975
                                     expt-split-rewrite)))
 
976
  )
 
977
 
 
978
 
 
979
 
 
980
 
 
981
;could use even-odd phrasing?
 
982
(defthm bits-down-to-1-case-split
 
983
  (implies (and (integerp x)
 
984
                (>= x 0)
 
985
                (integerp i)
 
986
                (>= i 1))
 
987
           (equal (bits x i 1)
 
988
                  (if (equal (bitn x 0) 0)
 
989
                      (/ (bits x i 0) 2) ;use the fact that we know bit 0 is 0?
 
990
                    (/ (1- (bits x i 0)) 2) ;use the fact that we know bit 0 is 1?
 
991
                    ))))
 
992
 
 
993
 
 
994
;would like these
 
995
;x<2^n
 
996
(defthm test2
 
997
  (IMPLIES (AND (INTEGERP N) (<= 0 N) (rationalP X) (<= 0 x))
 
998
         (EQUAL (FLOOR (* 1/2 x) 1)
 
999
                (FLOOR (* 1/2 (FLOOR x 1))
 
1000
                       1)))
 
1001
  :otf-flg t
 
1002
  :hints (("Goal" :in-theory (enable floor))))
 
1003
 
 
1004
  :otf-flg t
 
1005
  :hints (("Goal" :in-theory (set-difference-theories
 
1006
                              (enable fl)
 
1007
                              '())))
 
1008
)
 
1009
 
 
1010
 
 
1011
(defthm test
 
1012
  (IMPLIES (AND (INTEGERP N) (<= 0 N) (INTEGERP X))
 
1013
         (EQUAL (FL (* 1/2 X (/ (EXPT 2 N))))
 
1014
                (FL (* 1/2 (FL (* X (/ (EXPT 2 N))))))))
 
1015
  :otf-flg t
 
1016
  :hints (("Goal" :in-theory (set-difference-theories
 
1017
                              (enable fl)
 
1018
                              '()))))
 
1019
)
 
1020
 
 
1021
 
 
1022
;these may be made moot by my method of using lowbits with bits of a sum
 
1023
 
 
1024
(defthm bits-sum-simplify-first-term
 
1025
  (implies (and (>= (abs x) (expt 2 (+ j 1))) ;prevents loop
 
1026
                (rationalp x)
 
1027
                (rationalp y)
 
1028
                (integerp j)
 
1029
                (<= 0 j)
 
1030
                )
 
1031
           (equal (bits (+ x y) j 0)
 
1032
                  (bits (+ (lowbits x j) y) j 0))))
 
1033
 
 
1034
(defthm bits-sum-simplify-second-term
 
1035
  (implies (and (>= (abs x) (expt 2 (+ j 1))) ;prevents loop
 
1036
                (rationalp x)
 
1037
                (rationalp y)
 
1038
                (integerp j)
 
1039
                (<= 0 j)
 
1040
                )
 
1041
           (equal (bits (+ y x) j 0)
 
1042
                  (bits (+ (lowbits x j) y) j 0))))
 
1043
 
 
1044
 
 
1045
 
 
1046
 
 
1047
This series of events deals with simplifying expressions like
 
1048
(equal (bits x 8 0) 
 
1049
       (+ (bits x 6 0) k))
 
1050
Intuitively, bits 6 down-to 0 appear on both sides of the sum and should be "cancelled".  
 
1051
The remaining bits will need to be "shifted" back into place.
 
1052
 
 
1053
More rules are probably needed to make the theory complete.
 
1054
 
 
1055
 
 
1056
(defthm bits-cancel-lowbits-in-comparison
 
1057
  (implies (and (> i+ i)
 
1058
                (rationalp k)
 
1059
                (rationalp x)
 
1060
                (integerp i)
 
1061
                (integerp j)
 
1062
                (<= j i)
 
1063
                (integerp i+)
 
1064
                )
 
1065
           (equal (equal (+ k (bits x i j))
 
1066
                         (bits x i+ j))
 
1067
                  (equal (* (expt 2 (+ 1 i (- j))) 
 
1068
                            (BITS X I+ (+ 1 I))) 
 
1069
                         k))))
 
1070
 
 
1071
(defthm bits-cancel-lowbits-in-comparison-alt-2
 
1072
  (implies (and (> i+ i)
 
1073
                (rationalp k)
 
1074
                (rationalp x)
 
1075
                (integerp i)
 
1076
                (integerp j)
 
1077
                (<= j i)
 
1078
                (integerp i+)
 
1079
                )
 
1080
           (equal (equal (+ (bits x i j) k)
 
1081
                         (bits x i+ j))
 
1082
                  (equal (* (expt 2 (+ 1 i (- j))) 
 
1083
                            (BITS X I+ (+ 1 I))) 
 
1084
                         k))))
 
1085
 
 
1086
 
 
1087
 
 
1088
;todo: rephrase the conclusion of the above by moving the "constant" (* 2 (EXPT 2 I) (/ (EXPT 2 J))) to the other
 
1089
;side
 
1090
;a good idea since k is usually divisible by that quantity (with the rule above, we often end up with an
 
1091
equality in which each side should have a power of 2 divided out of it
 
1092
; not needed if we have good meta rules for cancelling factors from an inequality
 
1093
 
 
1094
 
 
1095
(defthm bits-cancel-lowbits-in-comparison-2
 
1096
  (implies (and (> i+ i)
 
1097
                (rationalp k)
 
1098
                (rationalp x)
 
1099
                (integerp i)
 
1100
                (integerp j)
 
1101
                (<= j i)
 
1102
                (integerp i+)
 
1103
 
 
1104
                )
 
1105
           (equal (equal (+ k (bits x i j))
 
1106
                         (bits x i+ j))
 
1107
                  (equal (*  
 
1108
                            (BITS X I+ (+ 1 I))) 
 
1109
                         (/ k (expt 2 (+ 1 i (- j))))))))
 
1110
 
 
1111
 
 
1112
 
 
1113
 
 
1114
(defthm bits-cancel-lowbits-in-comparison-3
 
1115
  (implies (and (> i+ i)
 
1116
                (rationalp k)
 
1117
                (rationalp x)
 
1118
                (integerp i)
 
1119
                (integerp j)
 
1120
                (<= j i)
 
1121
                (integerp i+)
 
1122
                (rationalp y)
 
1123
                )
 
1124
           (equal (equal (+ (bits x i+ j) k)
 
1125
                         (+ y (bits x i j)))
 
1126
                  (equal (* (expt 2 (+ 1 i (- j )))
 
1127
                            (BITS X I+ (+ 1 I))) 
 
1128
                         (- y k)))))
 
1129
 
 
1130
;better conclusion?
 
1131
(defthm bits-cancel-lowbits-in-comparison-no-constant
 
1132
  (implies (and (> i+ i)
 
1133
                (rationalp x)
 
1134
                (integerp i)
 
1135
                (integerp j)
 
1136
                (<= j i)
 
1137
                (integerp i+)
 
1138
                )
 
1139
           (equal (equal (bits x i j)
 
1140
                         (bits x i+ j))
 
1141
                  (equal (* 2 (EXPT 2 I)
 
1142
                           (/ (EXPT 2 J))
 
1143
                           (BITS X I+ (+ 1 I))) 
 
1144
                         0))))
 
1145
 
 
1146
(defthm bits-cancel-lowbits-in-comparison-no-constant-2
 
1147
  (implies (and (> i+ i)
 
1148
                (rationalp x)
 
1149
                (integerp i)
 
1150
                (integerp j)
 
1151
                (<= j i)
 
1152
                (integerp i+)
 
1153
                )
 
1154
           (equal (equal (bits x i+ j)
 
1155
                         (bits x i j))
 
1156
                  (equal (* 2 (EXPT 2 I)
 
1157
                           (/ (EXPT 2 J))
 
1158
                           (BITS X I+ (+ 1 I))) 
 
1159
                         0))))
 
1160
;the theory above (cancelling bits in comparisons) is not complete
 
1161
;it should also deal with bitn
 
1162
;perhaps a meta rule would be a good idea here?
 
1163
 
 
1164
 
 
1165
(defthm bits-shift-alt
 
1166
  (implies (and (syntaxp (should-have-a-2-factor-divided-out x))
 
1167
                (> j 0) ;restricts application 
 
1168
                (rationalp x)
 
1169
                (integerp i)
 
1170
                (integerp j)
 
1171
                )
 
1172
           (equal (bits x i j)
 
1173
                  (bits (/ x 2) (- i 1) (- j 1)))))
 
1174
 
 
1175
(defthm bits-turn-bound-into-equal
 
1176
  (implies (and (syntaxp (quotep k))
 
1177
                (equal k (+ -2 (expt 2 (+ 1 i (- j)))))
 
1178
                (case-split (<= j i))
 
1179
                (case-split (rationalp x))
 
1180
                (case-split (integerp i))
 
1181
                (case-split (integerp j))
 
1182
                (case-split (rationalp k))
 
1183
                )
 
1184
           (equal (< k (bits x i j))
 
1185
                  (equal (bits x i j) (+ k 1))))
 
1186
  :hints (("Goal" :in-theory (disable  bits-upper-bound-tighter
 
1187
                                        bits-upper-bound
 
1188
                                         bits-upper-bound-2)
 
1189
           :use  bits-upper-bound-tighter))
 
1190
)
 
1191
 
 
1192
 
 
1193
 
 
1194
 
 
1195
 
 
1196
 
 
1197
 
 
1198
;(in-theory (disable nbits-shift-out-even))
 
1199
 
 
1200
 
 
1201
;...
 
1202
 
 
1203
 
 
1204
;degenerate case
 
1205
;rename?
 
1206
;expensive
 
1207
(defthm bits-sum-drop-irrelevant-term-1-of-1
 
1208
  (implies (and (rationalp x) ;(integerp x)
 
1209
 
 
1210
                (integerp i)
 
1211
                (<= 0 i)
 
1212
                (integerp j)
 
1213
                (<= 0 j)
 
1214
 
 
1215
                (integerp (/ x (expt 2 (+ 1 i))))
 
1216
                )
 
1217
           (equal (bits x i j)
 
1218
                  0))
 
1219
  :hints (("Goal" :in-theory (enable bits bits)))
 
1220
  :rule-classes ((:rewrite :backchain-limit-lst (nil nil nil nil nil 1)))
 
1221
)
 
1222
 
 
1223
 
 
1224
(defthm bits-shift-better
 
1225
  (implies (and (bind-free (can-take-out-numeric-power-of-2 x) (c))
 
1226
                (force (power2p c))
 
1227
                (case-split (integerp i))
 
1228
                (case-split (integerp j))
 
1229
                )
 
1230
           (equal (bits x i j)
 
1231
                  (bits (/ x c) (- i (expo c)) (- j (expo c))))))
 
1232
 
 
1233
;high bits don't matter - add syntaxp hyp that one addend is a constant with high bits still present
 
1234
;huh?
 
1235
 
 
1236
 
 
1237
|#
 
1238
 
 
1239
 
 
1240
;It may be easier to reason about bits if we use this rule instead of expanding/enabling bits.
 
1241
(defthmd bits-alt-def
 
1242
  (equal (bits x i j)
 
1243
         (if (or (not (integerp i))
 
1244
                 (not (integerp j)))
 
1245
             0
 
1246
           (mod (fl (/ x (expt 2 j))) (expt 2 (+ 1 i (- j))))))
 
1247
  :hints (("Goal" :in-theory (enable bits))))
 
1248
 
 
1249
 
 
1250
 
 
1251
 
 
1252
 
 
1253
 
 
1254
(defthm bits-bvecp-simple-2
 
1255
  (bvecp (bits x (1- i) 0) i)
 
1256
  :hints (("Goal" :cases ((acl2-numberp i)))))
 
1257
 
 
1258
 
 
1259
;Follows from BITS-SUM-DROP-IRRELEVANT-TERM-2-OF-2.
 
1260
;change param names
 
1261
(defthmd bits-plus-mult-2
 
1262
   (implies (and (< n k)
 
1263
                 (integerp y)
 
1264
                 (integerp k)
 
1265
                 )
 
1266
            (equal (bits (+ x (* y (expt 2 k))) n m)
 
1267
                   (bits x n m)))
 
1268
   :hints (("Goal" :cases ((integerp n))))) ;why cases hint needed?
 
1269
 
 
1270
(defthmd bits-plus-mult-2-rewrite
 
1271
   (implies (and (syntaxp (quotep c))
 
1272
                 (equal (mod c (expt 2 (1+ n))) 0))
 
1273
            (equal (bits (+ c x) n m)
 
1274
                   (bits x n m)))
 
1275
    :hints (("Goal" :use ((:instance bits-plus-mult-2
 
1276
                                     (x x)
 
1277
                                     (y (/ c (expt 2 (1+ n))))
 
1278
                                     (k (1+ n))
 
1279
                                     (n n)))
 
1280
             :in-theory (enable mod))))
 
1281
 
 
1282
(defthm bits-plus-bits
 
1283
   (implies (and (integerp m)
 
1284
                 (integerp p)
 
1285
                 (integerp n)
 
1286
                 (<= m p)
 
1287
                 (<= p n))
 
1288
            (= (bits x n m)
 
1289
               (+ (bits x (1- p) m)
 
1290
                  (* (expt 2 (- p m)) (bits x n p)))))
 
1291
   :rule-classes ()
 
1292
   :hints (("Goal" :use ((:instance bits-plus-bits2 (i n) (j m) (n p))))))
 
1293
 
 
1294
(defthm bits-less-than-x
 
1295
  (implies (<= 0 x)
 
1296
           (<= (bits x i 0) x))
 
1297
  :rule-classes (:rewrite :linear)
 
1298
  :hints (("goal" :in-theory (enable bits))))
 
1299
 
 
1300
(defthm bits-less-than-x-gen
 
1301
  (implies (and (<= 0 x)
 
1302
                (case-split (<= 0 j))
 
1303
                (case-split (not (complex-rationalp x)))
 
1304
                )
 
1305
           (<= (bits x i j) x))
 
1306
  :rule-classes (:rewrite :linear)
 
1307
  :hints (("goal" :in-theory (enable bits x-times-something>=1))))
 
1308
 
 
1309
(defthm bits-bvecp-when-x-is    
 
1310
  (implies (and (bvecp x k)     
 
1311
                (case-split (<= 0 j))
 
1312
                )
 
1313
           (bvecp (bits x i j) k))
 
1314
  :hints (("Goal" :in-theory (enable  bvecp))))
 
1315
 
 
1316
(defthmd bits-bits-1
 
1317
  (implies (and (<= k (- i j))
 
1318
                (case-split (<= 0 l))
 
1319
                (case-split (integerp i))
 
1320
                (case-split (integerp j))
 
1321
                (case-split (integerp k))
 
1322
                (case-split (integerp l))
 
1323
                )
 
1324
           (equal (bits (bits x i j) k l)
 
1325
                  (bits x (+ k j) (+ l j))))
 
1326
  :hints (("Goal" :in-theory (enable bits expt-split))))
 
1327
 
 
1328
(defthmd bits-bits-2
 
1329
  (implies (and (> k (- i j))
 
1330
                (case-split (<= 0 l))
 
1331
;                (case-split (integerp i))
 
1332
                (case-split (integerp j))
 
1333
                (case-split (integerp k))
 
1334
                (case-split (integerp l))
 
1335
                )
 
1336
           (equal (bits (bits x i j) k l)
 
1337
                  (bits x i (+ l j))))
 
1338
  :hints (("Goal" :in-theory (enable bits expt-split))))
 
1339
 
 
1340
(defthm bits-bits
 
1341
  (implies (and (case-split (<= 0 l))
 
1342
                (case-split (integerp i))
 
1343
                (case-split (integerp j))
 
1344
                (case-split (integerp k))
 
1345
                (case-split (integerp l))
 
1346
                )
 
1347
           (equal (bits (bits x i j) k l)
 
1348
                  (if (<= k (- i j))
 
1349
                      (bits x (+ k j) (+ l j))
 
1350
                    (bits x i (+ l j)))))
 
1351
:hints (("Goal" :in-theory (enable bits-bits-1 bits-bits-2))))
 
1352
 
 
1353
 
 
1354
;The following trivial corollary of bits-bits is worth keeping enabled.
 
1355
 
 
1356
(defthm bits-bits-constants
 
1357
  (implies (and (syntaxp (quotep i))
 
1358
                (syntaxp (quotep j))
 
1359
                (syntaxp (quotep k))
 
1360
                (<= 0 l)
 
1361
                (integerp i)
 
1362
                (integerp j)
 
1363
                (integerp k)
 
1364
                (integerp l))
 
1365
           (equal (bits (bits x i j) k l)
 
1366
                  (if (<= k (- i j))
 
1367
                      (bits x (+ k j) (+ l j))
 
1368
                    (bits x i (+ l j))))))
 
1369
 
 
1370
 
 
1371
(defthm bits-reduce
 
1372
  (implies (and (< x (expt 2 (+ 1 i)))
 
1373
                (case-split (integerp x))
 
1374
                (case-split (<= 0 x))
 
1375
                (case-split (integerp i))
 
1376
                )
 
1377
           (equal (bits x i 0) x)))
 
1378
 
 
1379
(defthm bits-0
 
1380
  (equal (bits 0 i j) 0))
 
1381
 
 
1382
 
 
1383
 
 
1384
 
 
1385
 
 
1386
;could prove a version where we drop bits from both args?
 
1387
(defthm bits-sum-drop-bits-around-arg-2
 
1388
  (implies (and (<= i i+)
 
1389
                (integerp y)
 
1390
                (case-split (integerp i+))
 
1391
                )
 
1392
           (equal (bits (+ x (bits y i+ 0)) i j)
 
1393
                  (bits (+ x y) i j)))
 
1394
  :hints (("Goal" :in-theory (enable bits))))
 
1395
 
 
1396
;Follows from BITS-SUM-DROP-BITS-AROUND-ARG-2.
 
1397
(defthm bits-sum-drop-bits-around-arg-1
 
1398
  (implies (and (<= i i+)
 
1399
                (integerp x)
 
1400
                (case-split (integerp i+))
 
1401
                )
 
1402
           (equal (bits (+ (bits x i+ 0) y) i j)
 
1403
                  (bits (+ x y) i j))))
 
1404
 
 
1405
(defthm bits-sum-drop-bits-around-arg-2-special-case
 
1406
  (implies (integerp y) ;case-split?
 
1407
           (equal (bits (+ x (bits y i 0)) i j)
 
1408
                  (bits (+ x y) i j))))
 
1409
 
 
1410
(defthm bits-sum-drop-bits-around-arg-1-special-case
 
1411
  (implies (integerp x) ;case-split?
 
1412
           (equal (bits (+ (bits x i 0) y) i j)
 
1413
                  (bits (+ x y) i j))))
 
1414
 
 
1415
;rename
 
1416
;Follows from BVECP-SUM-OF-BVECPS.
 
1417
(defthm bits-sum-1
 
1418
  (equal (bits (+ (bits x (1- i) 0)
 
1419
                  (bits y (1- i) 0))
 
1420
               i ;actually, this could be anything >= i ??
 
1421
               0)
 
1422
         (+ (bits x (1- i) 0)
 
1423
            (bits y (1- i) 0)))
 
1424
  :hints (("Goal" :in-theory (enable bits-tail))))
 
1425
 
 
1426
 
 
1427
;export!! enable?
 
1428
;gen?
 
1429
(defthmd bits-of-non-integer-special
 
1430
  (implies (case-split (not (integerp i)))
 
1431
           (equal (BITS X (1- i) 0)
 
1432
                  0)))
 
1433
 
 
1434
(local
 
1435
 (defthmd bits-fl-helper
 
1436
   (implies (and (<= 0 j)
 
1437
                 (<= -1 i)
 
1438
                 )
 
1439
            (equal (bits (fl x) i j)
 
1440
                   (bits x i j)))
 
1441
   :hints (("Goal" :in-theory (enable bits mod-fl-eric)))))
 
1442
 
 
1443
(defthm bits-fl
 
1444
  (implies (<= 0 j)
 
1445
           (equal (bits (fl x) i j)
 
1446
                  (bits x i j)))
 
1447
  :hints (("Goal" :use  bits-fl-helper)))
 
1448
 
 
1449
(defthmd bits-shift-down-eric
 
1450
  (implies (and (<= 0 j)
 
1451
                (integerp i)
 
1452
                (integerp j)
 
1453
                (integerp k)
 
1454
                )
 
1455
           (equal (bits (* x (/ (expt 2 k)))
 
1456
                        i
 
1457
                        j)
 
1458
                  (bits x (+ i k) (+ j k))))
 
1459
  :hints (("Goal" :in-theory (e/d (expt-minus) (bits-shift))
 
1460
           :use (:instance bits-shift (n (- k))))))
 
1461
 
 
1462
(defthmd bits-shift-down-1
 
1463
    (implies (and (<= 0 j)
 
1464
                  (integerp i)
 
1465
                  (integerp j)
 
1466
                  (integerp k)
 
1467
                  )
 
1468
             (equal (bits (fl (/ x (expt 2 k)))
 
1469
                          i
 
1470
                          j)
 
1471
                    (bits x (+ i k) (+ j k))))
 
1472
  :hints (("Goal" :in-theory (enable bits-fl bits-shift-down-eric))))
 
1473
 
 
1474
(local
 
1475
 (defthm bits-fl-by-2-helper
 
1476
   (implies (and (integerp i)
 
1477
                 (<= 0 i) 
 
1478
                 )
 
1479
            (equal (fl (* 1/2 (bits x i 0))) ;gen 0 to j?
 
1480
                   (bits x i 1)))
 
1481
   :rule-classes ()
 
1482
   :hints (("Goal"  :in-theory (disable LESS-THAN-MULTIPLY-THROUGH-BY-INVERTED-FACTOR-FROM-LEFT-HAND-SIDE)
 
1483
            :use ((:instance bits-shift-down-1 (k 1) (i (1- i)) (j 0))
 
1484
                  (:instance bits-plus-bits (n i) (m 0) (p 1))
 
1485
                  (:instance fl-unique (x (/ (bits x i 0) 2)) (n (bits x i 1))))))))
 
1486
;rename?
 
1487
(defthmd bits-fl-by-2
 
1488
  (equal (fl (* 1/2 (bits x i 0)))
 
1489
         (bits x i 1))
 
1490
  :hints (("Goal" :use (:instance  bits-fl-by-2-helper))))
 
1491
 
 
1492
(defthm mod-bits-by-2
 
1493
  (implies (and (integerp x)
 
1494
                (<= 0 i)
 
1495
                (integerp i)
 
1496
                )
 
1497
           (equal (mod (bits x i 0) 2)
 
1498
                  (mod x 2)))
 
1499
  :hints (("Goal" :in-theory (enable bits))))
 
1500
 
 
1501
 
 
1502
#|
 
1503
 
 
1504
;BOZO challenge:
 
1505
 
 
1506
(defthm bits-sum-drop-bits-around-arg-2-really-special-case
 
1507
  (implies (rationalp y)
 
1508
           (equal (bits (+ x (bits y i 0)) i 0)
 
1509
                  (bits (+ x y) i 0)))
 
1510
  :hints (("Goal" :in-theory (enable bits)))))
 
1511
 
 
1512
(defthm bits-sum-drop-bits-around-arg-1-really-special-case
 
1513
  (implies (integerp x)
 
1514
           (equal (bits (+ (bits x i 0) y) i 0)
 
1515
                  (bits (+ x y) i 0))))
 
1516
 
 
1517
|#
 
1518
 
 
1519
 
 
1520
(defthm bits-shift-by-constant-power-of-2
 
1521
  (implies (and (syntaxp (quotep k))
 
1522
                (power2p k)
 
1523
                (case-split (integerp i))
 
1524
                (case-split (integerp j))
 
1525
                )
 
1526
           (equal (bits (* k x) i j)
 
1527
                  (bits x (- i (expo k)) (- j (expo k)))))
 
1528
  :hints (("Goal" :in-theory (enable power2p-rewrite))))
 
1529
 
 
1530
 
 
1531
(defthm bits-shift-second-with-more
 
1532
  (implies (and ;(rationalp x)
 
1533
            (case-split (integerp n))
 
1534
            (case-split (integerp i))
 
1535
            (case-split (integerp j))
 
1536
            )
 
1537
           (equal (bits (* x (expt 2 n) y) i j)
 
1538
                  (bits (* x y) (- i n) (- j n))))
 
1539
  :hints (("Goal" :in-theory (disable bits-shift)
 
1540
           :use (:instance bits-shift (x (* x y))))))
 
1541
 
 
1542
(defthmd bits-times-2
 
1543
  (implies (and (acl2-numberp i)
 
1544
                (acl2-numberp j)
 
1545
                )
 
1546
           (equal (bits (* 2 x) i j)
 
1547
                  (bits x (1- i) (1- j))))
 
1548
  :hints (("Goal" :use ((:instance bits-shift (n 1))))))
 
1549
 
 
1550
(defthmd bits+2**k-2
 
1551
  (implies (and (< x (expt 2 k))
 
1552
                (<= 0 x)
 
1553
                (rationalp x) ;(integerp x)
 
1554
                (<= k m)
 
1555
                (integerp y)
 
1556
                (case-split (integerp m))
 
1557
                (case-split (integerp n))
 
1558
                (case-split (integerp k))
 
1559
                )
 
1560
           (equal (bits (+ x (* y (expt 2 k))) n m)
 
1561
                  (bits y (- n k) (- m k))))
 
1562
  :hints (("goal" :in-theory (disable FL-EQUAL-0)
 
1563
           :use ((:instance bits-shift-down-1
 
1564
                            (x (+ x (* y (expt 2 k))))
 
1565
                            (i (- n k))
 
1566
                            (j (- m k)))
 
1567
                 (:instance fl-unique (x (/ (+ x (* y (expt 2 k))) (expt 2 k))) (n y))))))
 
1568
 
 
1569
(defthm bits+2**k-2-alt
 
1570
  (implies (and (< x (expt 2 k))
 
1571
                (<= 0 x)
 
1572
                (rationalp x) ;(integerp x)
 
1573
                (<= k m)
 
1574
                (integerp y)
 
1575
                (case-split (integerp m))
 
1576
                (case-split (integerp n))
 
1577
                (case-split (integerp k))
 
1578
                )
 
1579
           (equal (bits (+ x (* (expt 2 k) y)) n m)
 
1580
                  (bits y (- n k) (- m k))))
 
1581
 
 
1582
  :hints (("Goal" :in-theory (disable bits+2**k-2)
 
1583
           :use (:instance bits+2**k-2))))
 
1584
 
 
1585
;basically the same as bits+2**k-2; drop one?
 
1586
;move
 
1587
(defthmd bits-plus-mult-1
 
1588
  (implies (and (bvecp x k) ;actually, x need not be an integer...
 
1589
                (<= k m)
 
1590
                (integerp y)
 
1591
                (case-split (integerp m))
 
1592
                (case-split (integerp n))
 
1593
                (case-split (integerp k))
 
1594
                )
 
1595
           (equal (bits (+ x (* y (expt 2 k))) n m)
 
1596
                  (bits y (- n k) (- m k))))
 
1597
  :hints (("Goal" :in-theory (enable bvecp)
 
1598
           :use (bits+2**k-2))))
 
1599
 
 
1600
(defthm bits-mod-0
 
1601
  (implies (and (integerp x)
 
1602
                (>= x 0)
 
1603
                (integerp m)
 
1604
                (>= m 0)
 
1605
                (integerp n)
 
1606
                (>= n 0))
 
1607
           (iff (= (mod x (expt 2 (+ m n 1))) 0)
 
1608
                (and (= (bits x (+ m n) n) 0)
 
1609
                     (= (mod x (expt 2 n)) 0))))
 
1610
  :rule-classes ()
 
1611
  :hints (("goal" :in-theory (e/d (bits expt-split) (BITS-FL)) 
 
1612
           :use ((:instance mod-0-0 (m x) (n (expt 2 n)) (p (expt 2 (1+ m))))
 
1613
                 (:instance bits-shift-down-1 (k n) (i m) (j 0))))))
 
1614
 
 
1615
;this is silly? just open up bits!
 
1616
(defthm mod-bits-equal
 
1617
  (implies (= (mod x (expt 2 (1+ i))) (mod y (expt 2 (1+ i))))
 
1618
           (= (bits x i j) (bits y i j)))
 
1619
  :rule-classes ()
 
1620
  :hints (("Goal" :in-theory (enable bits))))
 
1621
 
 
1622
(defthmd mod-bits-equal-cor
 
1623
    (implies (and (integerp x)
 
1624
                  (integerp n)
 
1625
                  (integerp i)
 
1626
                  (integerp j)
 
1627
                  (< i n))
 
1628
             (equal (bits (mod x (expt 2 n)) i j)
 
1629
                    (bits x i j)))
 
1630
    :hints (("Goal" :use ((:instance mod-bits-equal
 
1631
                                     (x (mod x (expt 2 n)))
 
1632
                                     (y x))))))
 
1633
 
 
1634
;not needed?  just expand bits?
 
1635
(defthmd bits-mod
 
1636
  (implies (and (case-split (integerp x))
 
1637
                (case-split (integerp i)) ;gen?
 
1638
                ;(case-split (<= 0 i))
 
1639
                )
 
1640
           (equal (bits x i 0)
 
1641
                  (mod x (expt 2 (1+ i)))))
 
1642
  :hints (("Goal" 
 
1643
           :cases ((rationalp i) (complex-rationalp i))
 
1644
           :in-theory (e/d (bits) ( EXPT-SPLIT)))))
 
1645
 
 
1646
(defthmd bits-bits-sum
 
1647
  (implies (and (integerp x)
 
1648
                (integerp y)
 
1649
                (integerp i))
 
1650
           (equal (bits (+ (bits x i 0) y) i 0)
 
1651
                  (bits (+ x y) i 0)))
 
1652
  :hints (("Goal" :in-theory (enable bits mod-sum))))
 
1653
 
 
1654
;reorder? make rewrite?
 
1655
(defthm bits-shift-up-2
 
1656
  (implies (and (integerp x)
 
1657
                (integerp k)
 
1658
                (<= 0 k)
 
1659
                (integerp i)
 
1660
                )
 
1661
           (equal (* (expt 2 k) (bits x i 0))
 
1662
                  (bits (* (expt 2 k) x) (+ i k) 0)))
 
1663
  :rule-classes ()
 
1664
;:hints (("Goal" :cases ((equal 0 k))))
 
1665
  )
 
1666
 
 
1667
 
 
1668
;export!
 
1669
(defthm bits-expt
 
1670
  (implies (and (case-split (integerp i))
 
1671
                (case-split (integerp j))
 
1672
                (case-split (integerp k))
 
1673
                )
 
1674
           (equal (bits (expt 2 k) i j)
 
1675
                  (if (or (< i j)
 
1676
                          (< k j)
 
1677
                          (< i k))
 
1678
                      0
 
1679
                    (expt 2 (- k j))))
 
1680
           )
 
1681
  :hints (("Goal" :use (:instance bits-shift (x 1) (n k))))
 
1682
  )
 
1683
 
 
1684
(defthm bits-natp
 
1685
  (natp (bits x i j)))
 
1686
 
 
1687
 
 
1688
(defthmd bits-shift-down-eric
 
1689
  (implies (and (<= 0 j)
 
1690
                (integerp i)
 
1691
                (integerp j)
 
1692
                (integerp k)
 
1693
                )
 
1694
           (equal (bits (* x (/ (expt 2 k)))
 
1695
                        i
 
1696
                        j)
 
1697
                  (bits x (+ i k) (+ j k))))
 
1698
  :hints (("Goal" :use (:instance bits-shift-down-1)))
 
1699
  )
 
1700
 
 
1701
;gen the 0?
 
1702
(defthm mod-bits
 
1703
  (implies (and (<= 0 i)
 
1704
                (<= 0 j)
 
1705
                (integerp j)
 
1706
                (integerp i))
 
1707
           (equal (mod (bits x i 0) (expt 2 j))
 
1708
                  (bits x (min i (1- j)) 0)))
 
1709
  :hints (("Goal" :in-theory (enable bits)))
 
1710
  )
 
1711
 
 
1712
(defthm bits-expt-constant
 
1713
  (implies (and (syntaxp (and (quotep k) (power2p (cadr k))))
 
1714
                (force (power2p k))
 
1715
                (case-split (integerp k)) ;gen?
 
1716
                (case-split (integerp i))
 
1717
                (case-split (integerp j))
 
1718
                )
 
1719
           (equal (bits k i j)
 
1720
                  (if (or (< i j)
 
1721
                          (< (expo k) j)
 
1722
                          (< i (expo k)))
 
1723
                      0
 
1724
                    (expt 2 (- (expo k) j)))))
 
1725
  :hints (("Goal" :in-theory (enable power2p-rewrite))))
 
1726
 
 
1727
#|
 
1728
 
 
1729
(defthm bits-minus-better-helper-1
 
1730
  (implies (and (integerp x)
 
1731
                (integerp i))
 
1732
           (equal (equal 0 (bits x i 0))
 
1733
                  (integerp (* 1/2 x (/ (expt 2 i))))
 
1734
                  ))
 
1735
  :hints (("Goal" :in-theory (enable expt-split mod-equal-0)
 
1736
           :expand  (BITS X I 0)))
 
1737
  )
 
1738
 
 
1739
(defthm bits-minus-better-helper-2
 
1740
  (implies (and (integerp x)
 
1741
                (integerp i))
 
1742
           (equal (equal 0 (bits x (1- j) 0))
 
1743
                  (integerp (* x (/ (expt 2 j))))
 
1744
                  ))
 
1745
  :hints (("Goal" :in-theory (enable expt-split mod-equal-0)
 
1746
           :expand   (bits x (1- j) 0)))
 
1747
  )
 
1748
 
 
1749
;note that although the RHS looks slightly gross, 
 
1750
;gen the (integerp x) hyp!!
 
1751
(defthm bits-minus-better
 
1752
  (implies (and (case-split (integerp x)) ;gen!
 
1753
                (case-split (integerp i))
 
1754
                (case-split (<= j i)) ;drop?
 
1755
                (case-split (integerp j))
 
1756
                )
 
1757
           (equal (bits (* -1 x) i j)
 
1758
                  (if (equal 0 (bits x i 0))
 
1759
                      0
 
1760
                    (if (equal 0 (bits x (1- j) 0))
 
1761
                        (+ (* 2 (expt 2 i) (/ (expt 2 j))) (- (bits x i j)))
 
1762
                      (+ -1 (* 2 (expt 2 i) (/ (expt 2 j))) (- (bits x i j)))))))
 
1763
        :hints (("Goal" :use bits-minus
 
1764
        :in-theory (enable mod-does-nothing expt-split mod-cancel)))
 
1765
)
 
1766
 
 
1767
|#
 
1768
 
 
1769
 
 
1770
;Unlike bits-tail, this allows j to be non-zero.
 
1771
;Note that the conclusion is (bits x ...), not just x.
 
1772
;i is a free variable
 
1773
;watch out for loops with this rule
 
1774
;BOZO export in lib/ or user/!
 
1775
(defthm bits-tighten
 
1776
  (implies (and (bvecp x i)
 
1777
                (<= i n) 
 
1778
                (case-split (integerp n))
 
1779
                )
 
1780
           (equal (bits x n j)
 
1781
                  (bits x (1- i) j)))
 
1782
  :rule-classes ((:rewrite :match-free :all))
 
1783
  :hints (("goal" :use (:instance expt-weak-monotone (n i) (m (+ 1 n)))
 
1784
           :in-theory (e/d (bits bvecp) (expt-compare)))))
 
1785
 
 
1786
(defthmd bits-mod-2
 
1787
  (implies (and (integerp x)
 
1788
                (integerp i)
 
1789
                (integerp j)
 
1790
                (>= i j))
 
1791
           (equal (bits x (1- i) j)
 
1792
                  (- (fl (/ x (expt 2 j)))
 
1793
                     (* (expt 2 (- i j))
 
1794
                        (fl (/ x (expt 2 i)))))))
 
1795
  :hints (("Goal" :in-theory (enable bits mod))))
 
1796
 
 
1797
(defthmd bits-neg
 
1798
  (implies (and (< i 0)
 
1799
                (integerp x))
 
1800
           (equal (bits x i j) 0))
 
1801
  :hints (("Goal" :in-theory (enable bits))))
 
1802
 
 
1803
(defthmd bits-shift-down-2
 
1804
  (implies (and (natp x)
 
1805
                (natp i)
 
1806
                (natp k))
 
1807
           (equal (fl (/ (bits x (+ i k) 0) (expt 2 k)))
 
1808
                  (bits (fl (/ x (expt 2 k))) i 0)))
 
1809
  :hints (("Goal" :use ((:instance bits-plus-bits (n (+ i k)) (m 0) (p k)))
 
1810
           :in-theory (enable bits-shift-down-1))))