File: logand.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 (359 lines) | stat: -rw-r--r-- 9,997 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
(in-package "ACL2")

#|
This book includes lemmas about LOGAND.  Note that LOGAND is a macro which expands to nested calls to
BINARY-LOGAND. Both LOGAND and BINARY-LOGAND are built into ACL2.

This book contains only results; all the proofs are done in the book logand-proofs.

Todo:
 use set-invisible-fns-alist - or find a better way?
 rules for logand x with lognot x anywhere in there?
 should logand-with-0 be both sides? what about logand-with-minus-one
 how order rules for efficiency? perhaps make a separate documentation book?
 any other log lemmas?
 are the 4 enough for assoc comm functions?

|#
(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))

(include-book "ground-zero")
(local (include-book "logand-proofs"))

(set-inhibit-warnings "theory") ; avoid warning in the next event
(local (in-theory nil))
;(set-inhibit-warnings) ; restore theory warnings (optional)


;;
;; Rules to normalize logand terms (recall that LOGAND is a macro for BINARY-LOGAND):
;;

(defthm logand-associative
  (equal (logand (logand i j) k)
         (logand i (logand j k))))

(defthm logand-commutative
  (equal (logand j i)
         (logand i j)))

(defthm logand-commutative-2
  (equal (logand j i k)
         (logand i j k)))

(defthm logand-combine-constants
  (implies (syntaxp (and (quotep i)
                         (quotep j)))
           (equal (logand i j k)
                  (logand (logand i j) k))))


;;
;; LOGAND with special values
;;

(defthm logand-with-non-integer-arg
  (implies (or (not (integerp i))
               (not (integerp j)))
           (equal (logand i j)
                  0)))

;0 should always be brought to the front of logand
;should we have a rule with the second arg being 0?
(defthm logand-with-zero
  (equal (logand 0 j) 0))

;-1 should always be brought to the front of logand
;should we have both cases or not?
(defthm logand-with-minus-one
  (implies (case-split (integerp i))
           (equal (logand -1 i) i)))



;;
;; Type facts
;;

;this goes through:
;(thm (integerp (logand i j)))


(defthm logand-integer-type-prescription
  (integerp (logand i j))
  :rule-classes (:type-prescription)
  :hints (("Goal" :in-theory (enable logand))))



;These three go together.

;logand is negative iff either arg is negative

;Didn't make this a rewrite rule to avoid backchaining on (integerp (logand i j)) -- should never happen, but
;just in case.
(defthm logand-non-negative-integer-type-prescription
  (implies (or (<= 0 i)
               (<= 0 j))
           (and (<= 0 (logand i j))
                (integerp (logand i j))))
  :rule-classes (:type-prescription))

(defthm logand-negative-integer-type-prescription
  (implies (and (< i 0)
                (< j 0)
                (case-split (integerp i))
                (case-split (integerp j)))
           (and (< (logand i j) 0)
                (integerp (logand i j))))
  :rule-classes (:type-prescription))

; rewrites (<= 0 (logand i j)) and (< (logand i j) 0)
;could this perhaps not fire (say, during backchaining) because of case-splitting of the conclusion, causing
;us to wish we had a simple rule that natp args imply logand is natp?
;maybe don't want this one?
(defthm logand-negative-rewrite
  (implies (and (case-split (integerp i))
                (case-split (integerp j)))
           (equal (< (logand i j) 0)
                  (and (< i 0)
                       (< j 0)))))

(defthm logand-non-negative
  (implies (or (<= 0 x)
               (<= 0 y)
               )
           (<= 0 (logand x y))))

;There's no nice logand-positive rule.  Nor is there a clear rewrite for (< 0 (logand i j))
;For logand to be positive, the arguments must have bits that overlap, and there's no way to state this.
(defthm logand-non-positive-integer-type-prescription
    (implies (and (<= i 0)
                  (<= j 0))
	     (and (<= (logand i j) 0)
		  (integerp (logand i j))))
  :rule-classes (:type-prescription))

(defthm logand-non-positive-rewrite
    (implies (and (<= i 0)
                  (<= j 0))
	     (<= (logand i j) 0)))

#| do we want this?
(defthm logand-negative
  (implies (and (< i 0)
                (< j 0)
                (case-split (integerp i))
                (case-split (integerp j))
                )
           (and (integerp (logand i j))
                (< (logand i j) 0)))
  :hints (("Goal" :in-theory (enable logand)))
  :rule-classes (:rewrite (:type-prescription)))
|#



; If logand is less than -1, then both i and j are <= -1, and at least one of them is strictly < -1.
(defthm logand-less-than-minus-one
  (implies (and (case-split (integerp i))
                (case-split (integerp j))
                )
           (equal (< (logand i j) -1)
                  (or (and (<= i -1) (< j -1))
                      (and (<= j -1) (< i -1))))))

;BOZO move!
;perhaps put on a backchain limit?
(defthm integer-tighten-bound
  (implies (integerp x)
           (equal (< -1 x)
                  (<= 0 x))))
#|
;rewrite < -1 to <= 0?
;simplify the conclusion?
(defthm logand-negative-5
  (implies (and (case-split (integerp i))
                (case-split (integerp j))
                )
           (equal (< -1 (logand i j))
                  (not (and (< i 0)
                            (< j 0))))))

:hints (("Goal" :cases ((equal j -1) (equal i -1))
         :in-theory (enable logand))))

|#



(defthm logand-self
  (implies (case-split (integerp i))
           (equal (logand i i) i)))

(defthm logand-equal-minus-one
  (equal (EQUAL (LOGAND i j) -1)
         (and (equal i -1)
              (equal j -1))))

(defthm logand-even
  (implies (and (case-split (integerp i))
                (case-split (integerp j))
                )
           (equal (INTEGERP (* 1/2 (logand i j)))
                  (or (INTEGERP (* 1/2 i))
                      (INTEGERP (* 1/2 j))))))

;weird?
(defthm logand-0-when-one-arg-is-odd
  (implies (and (not (integerp (* 1/2 j)))
                (case-split (integerp j))
                (case-split (integerp i))
                )
           (and (equal (equal (logand i j) 0)
                       (and (integerp (* 1/2 i))
                            (equal (logand (fl (* 1/2 i)) (fl (* 1/2 j))) 0)))
                (equal (equal (logand j i) 0)
                       (and (integerp (* 1/2 i))
                            (equal (logand (fl (* 1/2 i)) (fl (* 1/2 j))) 0))))))

(defthm logand-simp-1
  (implies (and (case-split (integerp i))
                (case-split (integerp j)))
           (equal (LOGAND (+ 1 (* 2 i))
                          (+ 1 (* 2 j)))
                  (+ 1 (* 2 (logand i j))))))

;add to this
;make linear?
(defthm logand-upper-bound-1
  (implies (<= 0 i)
           (<= (logand i j) i)))


;BOZO same as  logand-upper-bound-1
(defthm logand-bnd
   (implies (<= 0 x)
            (<= (logand x y) x))
   :rule-classes :linear
   )


;trying disabled...
(defthmd logand-with-1
  (implies (case-split (integerp i))
           (equal (logand 1 i)
                  (if (evenp i)
                      0
                    1))))

;trying disabled...
;rename
;BOZO make a nice rule for logand with 1?
(defthmd logand-special-value
  (implies (case-split (integerp j))
           (equal (equal (logand 1 j) j)
                  (or (equal j 0) (equal j 1)))))

(defthmd logand-def
  (implies (and (case-split (integerp i))
                (case-split (integerp j))
                )
           (equal (logand i j)
              (+ (* 2 (logand (fl (* 1/2 i)) (fl (* 1/2 j))))
                 (logand (mod i 2) (mod j 2)))))
  :rule-classes  ((:definition :controller-alist ((binary-logand t t)))))


(defthm fl-logand-by-2
  (implies (and (case-split (integerp i))
                (case-split (integerp j))
                )
           (equal (fl (* 1/2 (logand i j)))
                  (logand (fl (* 1/2 i)) (fl (* 1/2 j))))))

(defthm floor-logand-by-2
    (implies (and (case-split (integerp i))
		  (case-split (integerp j)))
	     (equal (floor (logand i j) 2)
                    (logand (floor i 2) (floor j 2)))))

(defthm mod-logand-by-2
  (equal (mod (logand i j) 2)
         (logand (mod i 2) (mod j 2))))

;allow them to occur in other orders (perhaps with intervening terms)?
;think about this
;make a version for logior
(defthm logand-i-lognot-i
  (implies (case-split (integerp i))
           (equal (LOGAND i (LOGNOT i))
                  0)))






;make a nice recognizer?
;handle negative case?
;rename?
(defthmd logand-ones
  (implies (and (< i (expt 2 n)) ;drop and wrap bits around i?
                (<= 0 i)
                (case-split (integerp i))
                )
           (equal (logand i (1- (expt 2 n)))
                  i)))

#|

;change name and param names eventually
(defthm AND-BITS-A
  (implies (and (integerp x); (>= x 0)
                (integerp k); (>= k 0)
                )
           (equal (logand x (expt 2 k))
                  (* (expt 2 k) (bitn x k))))
  :rule-classes ())

|#

(defthm AND-DIST-B
   (implies (and (integerp x) (>= x 0)
                 (integerp y) (>= y 0)
                 (integerp n) (>= n 0))
            (= (logand (* (expt 2 n) x) y)
               (* (expt 2 n) (logand x (fl (/ y (expt 2 n)))))))
   :rule-classes ())


;BOZO also have logand-with-zero
(defthm logand-0
  (equal (logand 0 j) 0))


(defthmd logand-rewrite
  (implies (and (case-split (integerp x))
                (case-split (integerp y))
                )
           (equal (logand x y)
                  (+ (* 2 (logand (fl (/ x 2)) (fl (/ y 2))))
                     (logand (mod x 2) (mod y 2)))))
  :rule-classes ((:definition :controller-alist ((binary-logand t t)))))

(defthm logand-even-2
  (implies (and (integerp i)
                (integerp j))
           (equal (or (= (mod i 2) 0)
                      (= (mod j 2) 0))
                  (= (mod (logand i j) 2) 0)))
  :rule-classes ())