1
; Port land0 theorems to land. The original definition of land (in rel4) was
2
; that of land0 in the current release. So the port is to keep all the lemmas
3
; about land0 and then use equality of land0 with land to port them to land.
8
(local (include-book "top1")) ; for land0-bits-1 and land0-bits-2
10
(defun binary-land (x y n)
11
(declare (xargs :guard (and (natp x)
21
(if (and (equal (bitn x 0) 1)
25
(t (+ (* 2 (binary-land (fl (/ x 2)) (fl (/ y 2)) (1- n)))
26
(binary-land (mod x 2) (mod y 2) 1))))
28
(logand (bits x (1- n) 0)
31
(defmacro land (&rest x)
32
(declare (xargs :guard (and (consp x)
35
(cond ((endp (cdddr x)) ;(land x y n) -- the base case
38
`(binary-land ,(car x)
42
; We attempt to derive all land results from corresponding land0 results.
53
(defthm p0-holds-inductive-step
54
(implies (and (not (zp n))
59
(p0 (mod x 2) (mod y 2) 1))
61
:hints (("Goal" :use (land0-def binary-land)))))
64
(defthm p0-holds-base-1
66
:hints (("Goal" :in-theory (enable bitn)
67
:expand ((binary-land0 x y 1))))))
70
(defthm p0-holds-base-0
73
:hints (("Goal" :expand ((binary-land0 x y n))))))
78
:hints (("Goal" :induct (land x y n)
79
:in-theory (disable p0)))
82
(defthmd land-is-land0
85
:hints (("Goal" :use p0-holds))))
87
(local (in-theory (e/d (land-is-land0) (binary-land))))
89
;Allows things like (in-theory (disable land)) to refer to binary-land.
90
(add-macro-alias land binary-land)
92
(defthm land-nonnegative-integer-type
93
(and (integerp (land x y n))
95
:rule-classes (:type-prescription))
97
;(:type-prescription land) is no better than land-nonnegative-integer-type and might be worse:
98
(in-theory (disable (:type-prescription binary-land)))
100
;drop this if we plan to keep natp enabled?
104
;BOZO split into 2 rules?
105
(defthm land-with-n-not-a-natp
106
(implies (not (natp n))
110
(defthmd land-bvecp-simple
111
(bvecp (land x y n) n)
112
:hints (("Goal" :use land0-bvecp-simple)))
115
(implies (and (<= n k)
116
(case-split (integerp k)))
117
(bvecp (land x y n) k)))
121
;; Rules to normalize land terms (recall that LAND is a macro for BINARY-LAND):
124
;This guarantees that the n parameters to nested LAND calls match.
125
;Note the MIN in the conclusion.
126
;BOZO do we expect MIN to be enabled? Maybe we should use IF instead for this and other rules?
127
(defthm land-nest-tighten
128
(implies (and (syntaxp (not (equal m n)))
129
(case-split (integerp m))
130
(case-split (integerp n))
132
(equal (land x (land y z m) n)
133
(land x (land y z (min m n)) (min m n)))))
135
; allow the n's to differ on this?
136
(defthm land-associative
137
(equal (land (land x y n) z n)
138
(land x (land y z n) n)))
140
(defthm land-commutative
144
; allow the n's to differ on this?
145
(defthm land-commutative-2
146
(equal (land y (land x z n) n)
147
(land x (land y z n) n)))
149
; allow the n's to differ on this?
150
(defthm land-combine-constants
151
(implies (syntaxp (and (quotep x)
154
(equal (land x (land y z n) n)
155
(land (land x y n) z n))))
161
;nicer than the analogous rule for logand? is it really?
162
;BOZO gen the second 1 in the lhs?
171
;perhaps use only the main rule, bits-land?
173
(implies (and (< i n)
174
(case-split (<= 0 j))
175
(case-split (integerp n))
177
(equal (bits (land x y n) i j)
181
:hints (("Goal" :use bits-land0-1)))
183
;perhaps use only the main rule, bits-land?
185
(implies (and (<= n i)
186
(case-split (<= 0 j))
187
(case-split (integerp n))
189
(equal (bits (land x y n) i j)
193
:hints (("Goal" :use bits-land0-2)))
195
;Notice the call to MIN in the conclusion.
197
(implies (and (case-split (<= 0 j))
198
(case-split (integerp n))
199
(case-split (integerp i))
201
(equal (bits (land x y n) i j)
204
(+ (min n (+ 1 i)) (- j))))))
207
(implies (and (< m n)
208
(case-split (<= 0 m))
209
(case-split (integerp n))
211
(equal (bitn (land x y n) m)
216
(implies (and (<= n m)
217
(case-split (<= 0 m))
218
(case-split (integerp n))
220
(equal (bitn (land x y n) m)
223
;notice the IF in the conclusion
224
;we expect this to cause case splits only rarely, since m and n will usually be constants
226
(implies (and (case-split (<= 0 k))
227
(case-split (integerp n))
229
(equal (bitn (land x y n) k)
236
;BOZO see land-equal-0
237
;drop bvecp hyps and put bitn in conclusion?
238
(defthm land-of-single-bits-equal-0
239
(implies (and (case-split (bvecp x 1))
240
(case-split (bvecp y 1))
242
(equal (equal 0 (land x y 1))
246
(defthm land-of-single-bits-equal-1
247
(implies (and (case-split (bvecp x 1))
248
(case-split (bvecp y 1))
250
(equal (equal 1 (land x y 1))
255
(equal (land (1- (expt 2 n)) x n)
257
:hints (("Goal" :use land0-ones))
260
;land-with-all-ones will rewrite (land x n) [note there's only one value being ANDed], because (land x n)
261
;expands to (BINARY-LAND X (ALL-ONES N) N) - now moot???
262
;BOZO drop bvecp hyp and move to conclusion?
263
(defthm land-with-all-ones
264
(implies (case-split (bvecp x n))
265
(equal (land (all-ones n) x n)
268
(defthmd land-ones-rewrite
269
(implies (and (syntaxp (and (quotep k) (quotep n)))
270
(equal k (1- (expt 2 n))) ;this computes on constants...
274
:hints (("Goal" :use land0-ones-rewrite)))
276
(defthm land-def-original
277
(implies (and (integerp x)
283
(+ (* 2 (land (fl (/ x 2)) (fl (/ y 2)) (1- n)))
284
(land (mod x 2) (mod y 2) 1))))
285
:hints (("Goal" :use land0-def))
289
(implies (and (natp x)
293
(equal (mod (land x y n) 2)
294
(land (mod x 2) (mod y 2) 1)))
295
:hints (("Goal" :use land0-mod-2)))
297
;BOZO RHS isn't simplified...
299
(implies (and (natp x)
303
(equal (fl (/ (land x y n) 2))
304
(land (fl (/ x 2)) (fl (/ y 2)) (1- n))))
305
:hints (("Goal" :use land0-fl-2)))
307
;BOZO rename to land-with-n-0
308
;what if n is negative? or not an integer?
310
(equal (land x y 0) 0))
312
;actually, maybe only either x or y must be a bvecp of length n
315
(implies (and (bvecp x n)
323
;deceptive name; this only works for single bits!
325
(implies (and (bvecp i 1)
327
(equal (equal 0 (land i j 1))
333
(implies (case-split (<= 0 x))
335
:rule-classes (:rewrite :linear))
337
;enable? make an alt version??
338
(defthmd land-ignores-bits
339
(equal (land (bits x (1- n) 0) y n)
341
:hints (("Goal" :use land0-ignores-bits)))
343
(defthmd land-with-shifted-arg
344
(implies (and (integerp x) ;gen?
350
(equal (land (* (expt 2 m) x) y n)
351
(* (expt 2 m) (land x (bits y (1- n) m) (+ n (- m))))))
352
:hints (("Goal" :use land0-with-shifted-arg)))
355
(implies (and (integerp x)
358
(= (land (* (expt 2 k) x)
361
(* (expt 2 k) (land x y (- n k)))))
362
:hints (("Goal" :use land0-shift))
366
(implies (and (natp n)
369
(equal (land x (expt 2 k) n)
370
(* (expt 2 k) (bitn x k))))
371
:hints (("Goal" :use land0-expt)))
374
(implies (and (<= j i) ;drop? or not?
381
(equal (land x (- (expt 2 i) (expt 2 j)) n)
382
(* (expt 2 j) (bits x (1- i) j))))
383
:hints (("Goal" :use land0-slice))
387
(implies (and (natp n)
392
(equal (land (- (expt 2 n) (1+ (expt 2 l)))
393
(- (expt 2 n) (expt 2 k))
396
(- (expt 2 n) (expt 2 (1+ k)))
397
(- (expt 2 n) (expt 2 k)))))
398
:hints (("Goal" :use land0-slices)))
400
(defthm land-upper-bound
401
(implies (and (integerp n)
403
(< (land x y n) (expt 2 n)))
404
:rule-classes (:rewrite :linear))
406
(defthm land-upper-bound-tight
407
(implies (and (integerp n)
409
(<= (land x y n) (1- (expt 2 n)))))
412
(equal (land (fl x) y n)
415
(defthm land-fl-2-eric ;BOZO name conflicted...
416
(equal (land x (fl y) n)
419
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
420
; Added in move to rel5 (should perhaps be in a -proofs file):
421
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
424
(implies (and (bvecp x m)
426
(bvecp (land x y n) m))
427
:hints (("Goal" :in-theory (enable bvecp))))
429
; Start proof of fl-land.
432
(defun fl-land-induction (k n)
435
(fl-land-induction (1- k) (1+ n)))))
439
(defthmd fl-land-induction-step-1
440
(implies (not (zp k))
441
(equal (land (fl (* x (/ (expt 2 k))))
442
(fl (* y (/ (expt 2 k))))
444
(land (fl (/ (fl (* x (/ (expt 2 (1- k))))) 2))
445
(fl (/ (fl (* y (/ (expt 2 (1- k))))) 2))
447
:hints (("Goal" :in-theory (disable land-fl-1
452
:use ((:instance fl/int-rewrite
453
(x (* x (/ (expt 2 (1- k)))))
455
(:instance fl/int-rewrite
456
(x (* y (/ (expt 2 (1- k)))))
460
(defthmd fl-land-induction-step-2
461
(equal (land (fl (/ (fl (* x (/ (expt 2 (1- k))))) 2))
462
(fl (/ (fl (* y (/ (expt 2 (1- k))))) 2))
464
(fl (/ (land (fl (* x (/ (expt 2 (1- k)))))
465
(fl (* y (/ (expt 2 (1- k)))))
468
:hints (("Goal" :in-theory (disable land-fl-1
472
:expand ((land (fl (* x (/ (expt 2 (1- k)))))
473
(fl (* y (/ (expt 2 (1- k)))))
477
(defthmd fl-land-induction-step-3
478
(implies (and (not (zp k))
479
(equal (fl (* (/ (expt 2 (+ -1 k)))
481
(land (fl (* x (/ (expt 2 (+ -1 k)))))
482
(fl (* y (/ (expt 2 (+ -1 k)))))
487
(equal (fl (/ (land (fl (* x (/ (expt 2 (1- k)))))
488
(fl (* y (/ (expt 2 (1- k)))))
491
(fl (/ (fl (* (/ (expt 2 (+ -1 k)))
496
(defthmd fl-land-induction-step-4
497
(implies (and (not (zp k))
498
(equal (fl (* (/ (expt 2 (+ -1 k)))
500
(land (fl (* x (/ (expt 2 (+ -1 k)))))
501
(fl (* y (/ (expt 2 (+ -1 k)))))
506
(equal (fl (/ (fl (* (/ (expt 2 (+ -1 k)))
509
(fl (* (/ (expt 2 k))
510
(land x y (+ k n))))))
511
:hints (("Goal" :expand ((expt 2 k))))))
514
(defthm fl-land-induction-step
515
(implies (and (not (zp k))
516
(equal (fl (* (/ (expt 2 (+ -1 k)))
518
(land (fl (* x (/ (expt 2 (+ -1 k)))))
519
(fl (* y (/ (expt 2 (+ -1 k)))))
524
(equal (fl (* (/ (expt 2 k))
526
(land (fl (* x (/ (expt 2 k))))
527
(fl (* y (/ (expt 2 k))))
529
:hints (("Goal" :use (fl-land-induction-step-1
530
fl-land-induction-step-2
531
fl-land-induction-step-3
532
fl-land-induction-step-4)))))
535
(implies (and (natp x)
539
(equal (fl (/ (land x y (+ n k)) (expt 2 k)))
540
(land (fl (/ x (expt 2 k))) (fl (/ y (expt 2 k))) n)))
541
:hints (("Goal" :induct (fl-land-induction k n)
542
:in-theory (disable land-is-land0 land-fl-1 land-fl-2-eric))))
546
(implies (and (integerp x)
551
(+ (* 2 (land (fl (/ x 2)) (fl (/ y 2)) (1- n)))
552
(land (bitn x 0) (bitn y 0) 1))))
553
:hints (("Goal" :in-theory (enable bitn-rec-0)
554
:use land-def-original)))
556
; Proof of mod-land as derived from bits-land:
559
; = {by land-bvecp and bits-tail}
560
; (bits (land x y k) (1- k) 0)
561
; = {by (:instance bits-land (x x) (y y) (n k) (i (1- k)) (j 0))}
562
; (land (bits x (1- k) 0) (bits y (1- k) 0) k)
563
; = {by (:instance bits-land (x x) (y y) (n n) (i (1- k)) (j 0))}
564
; (bits (land x y n) (1- k) 0)
566
; (bits (land x y n) (min (1- n) (1- k)) 0)
567
; = {by (:instance mod-bits (x (land x y n)) (i (1- n)) (j k))}
568
; (mod (bits (land x y n) (1- n) 0) (expt 2 k))
570
; (mod (land x y n) (expt 2 k))
573
(implies (and (integerp n)
576
(equal (mod (land x y n) (expt 2 k))
580
((:instance bits-land (x x) (y y) (n k) (i (1- k)) (j 0))
581
(:instance mod-bits (x (land x y n)) (i (1- n)) (j k))))))
584
(equal (land (bits x (1- n) 0)
588
:hints (("Goal" :use land0-bits-1)))
595
:hints (("Goal" :use land0-bits-2)))
599
(if (and (equal (bitn x 0) 1)
600
(equal (bitn y 0) 1))
603
:hints (("Goal" :use land0-base))