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
|
(in-package "ACL2")
#|
This book is about LXOR, a nice version of LOGXOR. LXOR takes an extra size parameter, N, and always returns
a bit vector of length N.
todo: ;add analogs of the thms in land.lisp past bitn-land
|#
;add macro alias
(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 lnot (x n)
(declare (xargs :guard (and (natp x)
(integerp n)
(< 0 n))
:verify-guards nil))
(if (natp n)
(+ -1 (expt 2 n) (- (bits x (1- n) 0)))
0))
(defund bvecp (x k)
(declare (xargs :guard (integerp k)))
(and (integerp x)
(<= 0 x)
(< x (expt 2 k))))
(defund all-ones (n)
(declare (xargs :guard (and (integerp n) (<= 0 n))))
(if (zp n)
0 ;degenerate case
(1- (expt 2 n))))
(local (include-book "all-ones"))
(local (include-book "merge"))
(local (include-book "bvecp"))
(local (include-book "logand"))
(local (include-book "bits"))
(local (include-book "bitn"))
(local (include-book "../arithmetic/top"))
(defund binary-lxor (x y n)
(declare (xargs :guard (and (natp x)
(natp y)
(integerp n)
(< 0 n))
:verify-guards nil))
(logxor (bits x (1- n) 0)
(bits y (1- n) 0)))
(defun formal-+ (x y)
(declare (xargs :guard t))
(if (and (acl2-numberp x) (acl2-numberp y))
(+ x y)
(list '+ x y)))
(defmacro lxor (&rest x)
(declare (xargs :guard (consp x)))
(cond ((endp (cdddr x)) ;(lxor x y n) -- the base case
`(binary-lxor ,@x))
(t
`(binary-lxor ,(car x)
(lxor ,@(cdr x))
,(car (last x))))))
;Allows things like (in-theory (disable lxor)) to refer to binary-lxor.
(add-macro-alias lxor binary-lxor)
(defthm lxor-nonnegative-integer-type
(and (integerp (lxor x y n))
(<= 0 (lxor x y n)))
:rule-classes (:type-prescription))
;(:type-prescription lxor) is no better than lxor-nonnegative-integer-type and might be worse:
(in-theory (disable (:type-prescription binary-lxor)))
;drop this if we plan to keep natp enabled?
(defthm lxor-natp
(natp (lxor x y n)))
(defthm lxor-with-n-not-a-natp
(implies (not (natp n))
(equal (lxor x y n)
0))
:hints (("Goal" :cases ((acl2-numberp n))
:in-theory (enable lxor)))
)
(defthmd lxor-bvecp-simple
(bvecp (lxor x y n) n)
:hints (("Goal" :cases ((natp n))
:in-theory (enable lxor))))
(defthm lxor-bvecp
(implies (and (<= n k)
(case-split (integerp k)))
(bvecp (lxor x y n) k))
:hints (("Goal" :in-theory (disable lxor-bvecp-simple)
:use lxor-bvecp-simple)))
;;
;; Rules to normalize lxor terms (recall that LXOR is a macro for BINARY-LXOR):
;;
;; allow sizes to differ on these?
(defthm lxor-associative
(equal (lxor (lxor x y n) z n)
(lxor x (lxor y z n) n))
:hints (("Goal" :cases ((natp n))
:in-theory (enable lxor bits-tail))))
(defthm lxor-commutative
(equal (lxor y x n)
(lxor x y n))
:hints (("Goal" :in-theory (enable lxor))))
(defthm lxor-commutative-2
(equal (lxor y (lxor x z n) n)
(lxor x (lxor y z n) n))
:hints (("Goal" :cases ((natp n))
:in-theory (enable lxor bits-tail))))
(defthm lxor-combine-constants
(implies (syntaxp (and (quotep x)
(quotep y)
(quotep n)))
(equal (lxor x (lxor y z n) n)
(lxor (lxor x y n) z n))))
(defthm lxor-0
(implies (case-split (bvecp y n))
(equal (lxor 0 y n)
y))
:hints (("Goal" :in-theory (enable lxor bits-tail))))
;nicer than the analogous rule for logand?
(defthm lxor-1
(implies (case-split (bvecp y 1))
(equal (lxor 1 y 1)
(lnot y 1)))
:hints (("Goal" :in-theory (enable bvecp-1-rewrite))))
(defthm lxor-self
(implies (case-split (bvecp x n))
(equal (lxor x x n)
0))
:hints (("Goal" :in-theory (enable lxor bits-tail))))
(defthmd bits-lxor-1
(implies (and (< i n)
(case-split (<= 0 j))
(case-split (integerp n))
)
(equal (bits (lxor x y n) i j)
(lxor (bits x i j)
(bits y i j)
(+ 1 i (- j)))))
:otf-flg t
:hints (("Goal" :in-theory (enable lxor bits-logand))))
(defthmd bits-lxor-2
(implies (and (<= n i)
(case-split (<= 0 j))
(case-split (integerp n))
)
(equal (bits (lxor x y n) i j)
(lxor (bits x i j)
(bits y i j)
(+ n (- j)))))
:otf-flg t
:hints (("Goal" :in-theory (enable lxor bits-logand))))
;notice the call to MIN in the conclusion
(defthm bits-lxor
(implies (and (case-split (<= 0 j))
(case-split (integerp n))
(case-split (integerp i))
)
(equal (bits (lxor x y n) i j)
(lxor (bits x i j)
(bits y i j)
(+ (min n (+ 1 i)) (- j)))))
:hints (("Goal" :in-theory (enable bits-lxor-1 bits-lxor-2))))
(defthmd bitn-lxor-1
(implies (and (< m n)
(case-split (<= 0 m))
(case-split (integerp n))
)
(equal (bitn (lxor x y n) m)
(lxor (bitn x m)
(bitn y m)
1)))
:hints (("Goal" :in-theory (set-difference-theories
(enable bitn)
'(BITS-N-N-REWRITE)))))
(defthmd bitn-lxor-2
(implies (and (<= n m)
(case-split (<= 0 m))
(case-split (integerp n))
)
(equal (bitn (lxor x y n) m)
0))
:hints (("Goal" :in-theory (enable BVECP-BITN-0))))
;notice the IF in the conclusion
;we expect this to cause case splits only rarely, since m and n will usually be constants
(defthm bitn-lxor
(implies (and (case-split (<= 0 m))
(case-split (integerp n))
)
(equal (bitn (lxor x y n) m)
(if (< m n)
(lxor (bitn x m)
(bitn y m)
1)
0)))
:hints (("Goal" :in-theory (enable bitn-lxor-1 bitn-lxor-2))))
(defthm lxor-ones
(implies (case-split (bvecp x n))
(equal (lxor (1- (expt 2 n)) x n)
(lnot x n)))
:rule-classes ()
:hints
(("subgoal 1" :use logxor-ones)
("goal" :cases ((natp n))
:in-theory (enable lxor bits-tail)
)))
;lxor-with-all-ones will rewrite (lxor x n) [note there's only one value being ANDed], because (lxor x n)
;expands to (BINARY-LXOR X (ALL-ONES N) N) - now moot???
(defthm lxor-with-all-ones
(implies (case-split (bvecp x n))
(equal (lxor (all-ones n) x n)
(lnot x n)))
:hints
(("goal" :use lxor-ones
:in-theory (enable all-ones))))
(defthm lxor-ones-rewrite
(implies (and (syntaxp (and (quotep k)
(quotep n)
(equal (cadr k) (1- (expt 2 (cadr n))))))
(force (equal k (1- (expt 2 n))))
(case-split (bvecp x n)))
(equal (lxor k x n)
(lnot x n)))
:hints (("Goal" :use lxor-ones)))
(local (in-theory (disable mod-by-2-rewrite-to-even mod-mult-of-n mod-equal-0)))
(defthm lxor-def
(implies (and (integerp x)
(integerp y)
(< 0 n)
(integerp n)
)
(equal (lxor x y n)
(+ (* 2 (lxor (fl (/ x 2)) (fl (/ y 2)) (1- n)))
(lxor (mod x 2) (mod y 2) 1))))
:rule-classes ()
:hints (("Goal" :in-theory (e/d (lxor bits-fl-by-2) ())
:use ((:instance logxor-def (i (bits x (1- n) 0)) (j (bits y (1- n) 0)))
mod012
(:instance mod012 (x y))))))
(defthm lxor-mod-2
(implies (and (natp x)
(natp y)
(natp n)
(> n 0))
(equal (mod (lxor x y n) 2)
(lxor (mod x 2) (mod y 2) 1)))
:hints (("Goal" :use (lxor-def
mod012
(:instance mod012 (x y))
(:instance quot-mod (m (lxor x y n)) (n 2))))))
(defthm lxor-fl-2
(implies (and (natp x)
(natp y)
(natp n)
(> n 0))
(equal (fl (/ (lxor x y n) 2))
(lxor (fl (/ x 2)) (fl (/ y 2)) (1- n))))
:hints (("Goal" :use (lxor-def
mod012
(:instance mod012 (x y))
(:instance quot-mod (m (lxor x y n)) (n 2))))))
(in-theory (disable lxor-mod-2 lxor-fl-2))
(defthm bitn-lxor-0
(implies (and (integerp x)
(integerp y)
(not (zp n))
)
(= (bitn (lxor x y n) 0)
(bitn (+ x y) 0)))
:rule-classes ()
:hints (("Goal" :use ((:instance bitn-logxor-0 (a (bits x (1- n) 0)) (b (bits y (1- n) 0)))
(:instance mod-mod-sum (n (expt 2 n)) (a x) (b y))
(:instance mod-of-mod (a n) (b 1) (x (+ x y)))
(:instance mod-of-mod (a n) (b 1) (x (+ (mod x (expt 2 n)) (mod y (expt 2 n))))))
:in-theory (enable lxor bitn-rec-0 bits-mod bitn-bits))))
(defthm lxor-x-y-0
(equal (lxor x y 0) 0)
:hints (("Goal" :in-theory (enable lxor))))
;N is a free variable
(defthm lxor-reduce
(implies (and (bvecp x n)
(bvecp y n)
(< n m)
(case-split (integerp m))
)
(equal (lxor x y m)
(lxor x y n)))
:hints (("Goal" :in-theory (enable lxor))))
;move
(defthm lxor-upper-bound
(implies (and (integerp n)
(<= 0 n))
(< (lxor x y n) (expt 2 n)))
:rule-classes (:rewrite :linear)
:hints (("Goal" :in-theory (enable lxor))))
;move
(defthm lxor-upper-bound-tight
(implies (and (integerp n)
(<= 0 n))
(<= (lxor x y n) (1- (expt 2 n)))))
|