1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
|
;; Copyright (C) 2017, Regents of the University of Texas
;; Written by Cuong Chau (derived from the FM9001 work of Brock and Hunt)
;; License: A 3-clause BSD license. See the LICENSE file distributed with
;; ACL2.
;; The ACL2 source code for the FM9001 work is available at
;; https://github.com/acl2/acl2/tree/master/books/projects/fm9001.
;; Cuong Chau <ckcuong@cs.utexas.edu>
;; January 2019
(in-package "ADE")
(include-book "../../hard-spec")
;; ======================================================================
;; This file defines a tree-based formalization of memory. This tree-based
;; memory offers advantages over a linear-list formalization. Specifically,
;; reading and writing the memory take O(log n) time and CONS operations
;; respectively, where n is the number of words in the memory. Also, we are
;; able to "stub-out", or leave unspecified, large sections of the memory.
;; Memory is modeled as a CONS tree, where the leaves of the tree are instances
;; of one of three parts: ROM tags read-only locations of the memory, while RAM
;; tags read-write locations and STUB represents ``unimplemented'' portions.
;; Each instance of the memory parts includes a value, which is returned when
;; that memory location is read. RAM cells may be overwritten, but writing to
;; a ROM or STUB cell does not change the memory. ROM and RAM cells may only
;; appear at the leaves of the tree, whereas STUB cells may appear anywhere.
;; Although our basic definitions restrict the types of data stored in memory
;; to be four-valued vectors, we assume throughout the specification of the
;; FM9001 (and enforce) the restriction that only bit-vectors are stored in
;; memory.
;; The bit-vector that specifies the address is used in an obvious way to
;; search the memory tree for the addressed location. Note, however, that the
;; address is reversed prior to the search. This allows for more compact
;; storage for sequences of data. If the address were not reversed, then the
;; memory trees would be subject to branching near the root of the tree. With
;; reversed addresses, the branching is localized near the leaves. This is an
;; especially important consideration for the main memory of the FM9001.
;; There, each path through the memory tree to a leaf cell is constructed from
;; 32 CONS cells.
(defun romp (mem)
(declare (xargs :guard t))
(and (true-listp mem)
(equal (len mem) 2)
(equal (car mem) 'rom)
(4v-listp (cadr mem))))
(defun rom-guts (mem)
(declare (xargs :guard t))
(if (equal (len mem) 2)
(cadr mem)
nil))
(defun ram (value)
(declare (xargs :guard t))
(list 'ram (v-fourfix value)))
(defun ramp (mem)
(declare (xargs :guard t))
(and (true-listp mem)
(equal (len mem) 2)
(equal (car mem) 'ram)
(4v-listp (cadr mem))))
(defun ram-guts (mem)
(declare (xargs :guard t))
(if (equal (len mem) 2)
(cadr mem)
nil))
(defun stubp (mem)
(declare (xargs :guard t))
(and (true-listp mem)
(equal (len mem) 2)
(equal (car mem) 'stub)
(4v-listp (cadr mem))))
(defun stub-guts (mem)
(declare (xargs :guard t))
(if (equal (len mem) 2)
(cadr mem)
nil))
(defthmd romp-is-not-ramp-nor-stubp
(implies (romp mem)
(and (not (ramp mem))
(not (stubp mem)))))
(defthm 4v-listp-rom-guts-of-romp
(implies (romp x)
(4v-listp (rom-guts x))))
(local
(defthm 4v-listp=>true-listp
(implies (4v-listp x) (true-listp x))
:rule-classes :forward-chaining))
(defthm true-listp-rom-guts-of-romp
(implies (romp x)
(true-listp (rom-guts x)))
:rule-classes :type-prescription)
(defthmd rom-guts-of-romp
(implies (romp x)
(equal (rom-guts x)
(cadr x))))
(defthm ramp-ram
(ramp (ram x)))
(defthm ram-guts-ram
(equal (ram-guts (ram value))
(v-fourfix value)))
(defthmd ramp-is-not-romp-nor-stubp
(implies (ramp mem)
(and (not (romp mem))
(not (stubp mem)))))
(defthm 4v-listp-ram-guts-of-ramp
(implies (ramp x)
(4v-listp (ram-guts x))))
(defthm true-listp-ram-guts-of-ramp
(implies (ramp x)
(true-listp (ram-guts x)))
:rule-classes :type-prescription)
(defthmd ram-guts-of-ramp
(implies (ramp x)
(equal (ram-guts x)
(cadr x))))
(defthmd stubp-is-not-romp-nor-ramp
(implies (stubp mem)
(and (not (romp mem))
(not (ramp mem)))))
(defthm 4v-listp-stub-guts-of-stubp
(implies (stubp x)
(4v-listp (stub-guts x))))
(defthm true-listp-stub-guts-of-stubp
(implies (stubp x)
(true-listp (stub-guts x)))
:rule-classes :type-prescription)
(defthmd stub-guts-of-stubp
(implies (stubp x)
(equal (stub-guts x)
(cadr x))))
(defun memp (mem)
(declare (xargs :guard t))
(or (romp mem)
(ramp mem)
(stubp mem)))
(defthm memp=>consp
(implies (memp x)
(consp x))
:rule-classes :forward-chaining)
(deftheory mem-theory
'(romp rom-guts ram ramp ram-guts stubp stub-guts))
;; MEMORY-PROPERP -- All memory cells are proper lists of length SIZE.
(defun memory-properp (n size mem)
(declare (xargs :guard (natp n)))
(if (stubp mem)
(and (true-listp (stub-guts mem))
(equal (len (stub-guts mem)) size))
(if (zp n)
(cond
((ramp mem) (and ;;(true-listp (ram-guts mem))
(equal (len (ram-guts mem)) size)))
((romp mem) (and ;;(true-listp (rom-guts mem))
(equal (len (rom-guts mem)) size)))
(t nil))
(and (not (romp mem))
(not (ramp mem))
(consp mem)
(memory-properp (1- n) size (car mem))
(memory-properp (1- n) size (cdr mem))))))
;; MEMORY-OKP -- All memory cells are BVP lists with length SIZE.
(defun memory-okp (n size mem)
(declare (xargs :guard (natp n)))
(if (stubp mem)
(and (bvp (stub-guts mem))
(equal (len (stub-guts mem)) size))
(if (zp n)
(cond
((ramp mem) (and (bvp (ram-guts mem))
(equal (len (ram-guts mem)) size)))
((romp mem) (and (bvp (rom-guts mem))
(equal (len (rom-guts mem)) size)))
(t nil))
(and (not (romp mem))
(not (ramp mem))
(consp mem)
(memory-okp (1- n) size (car mem))
(memory-okp (1- n) size (cdr mem))))))
;; READ-MEM
(defun read-mem1 (v-addr mem)
(declare (xargs :guard t))
(if (stubp mem)
(stub-guts mem)
(if (atom v-addr)
(cond ((ramp mem) (ram-guts mem))
((romp mem) (rom-guts mem))
(t nil))
(if (atom mem)
nil
(if (car v-addr)
(read-mem1 (cdr v-addr) (cdr mem))
(read-mem1 (cdr v-addr) (car mem)))))))
(defun read-mem (v-addr mem)
(declare (xargs :guard (true-listp v-addr)))
(read-mem1 (reverse v-addr) mem))
;; WRITE-MEM
(defun write-mem1 (v-addr mem value)
(declare (xargs :guard t))
(if (stubp mem)
mem
(if (atom v-addr)
(cond ((ramp mem) (ram value))
(t mem))
(if (atom mem)
mem
(if (car v-addr)
(cons (car mem)
(write-mem1 (cdr v-addr) (cdr mem) value))
(cons (write-mem1 (cdr v-addr) (car mem) value)
(cdr mem)))))))
(defthm true-listp-write-mem1
(implies (true-listp mem)
(true-listp (write-mem1 v-addr mem value)))
:rule-classes :type-prescription)
(defthm true-listp-write-mem1=>true-listp-mem
(implies (true-listp (write-mem1 v-addr mem value))
(true-listp mem))
:rule-classes :forward-chaining)
(defthm len-write-mem1
(equal (len (write-mem1 v-addr mem value))
(len mem)))
(defthm 4v-listp-of-write-mem1
(equal (4v-listp (write-mem1 v-addr mem value))
(4v-listp mem))
:hints (("Goal" :in-theory (e/d (4vp)
(romp-is-not-ramp-nor-stubp
ramp-is-not-romp-nor-stubp
stubp-is-not-romp-nor-ramp
romp
bv-is-true-list)))))
(defthm 4v-listp-of-car-write-mem1
(equal (4v-listp (car (write-mem1 v-addr mem value)))
(4v-listp (car mem))))
(defthm romp-write-mem1
(equal (romp (write-mem1 v-addr mem value))
(romp mem)))
(defthm romp-cons-write-mem1-1
(equal (romp (cons (car mem)
(write-mem1 v-addr (cdr mem) value)))
(romp mem)))
(defthm romp-cons-write-mem1-2
(equal (romp (cons (write-mem1 v-addr (car mem) value)
(cdr mem)))
(romp mem)))
(defthm ramp-write-mem1
(equal (ramp (write-mem1 v-addr mem value))
(ramp mem)))
(defthm ramp-cons-write-mem1-1
(equal (ramp (cons (car mem)
(write-mem1 v-addr (cdr mem) value)))
(ramp mem)))
(defthm ramp-cons-write-mem1-2
(equal (ramp (cons (write-mem1 v-addr (car mem) value)
(cdr mem)))
(ramp mem)))
(defthm stubp-write-mem1
(equal (stubp (write-mem1 v-addr mem value))
(stubp mem)))
(defthm stubp-cons-write-mem1-1
(equal (stubp (cons (car mem)
(write-mem1 v-addr (cdr mem) value)))
(stubp mem)))
(defthm stubp-cons-write-mem1-2
(equal (stubp (cons (write-mem1 v-addr (car mem) value)
(cdr mem)))
(stubp mem)))
(defun write-mem (v-addr mem value)
(declare (xargs :guard (true-listp v-addr)))
(write-mem1 (reverse v-addr) mem value))
(defthm true-listp-write-mem
(implies (true-listp mem)
(true-listp (write-mem v-addr mem value)))
:rule-classes :type-prescription)
(defthm true-listp-write-mem=>true-listp-mem
(implies (true-listp (write-mem v-addr mem value))
(true-listp mem))
:rule-classes :forward-chaining)
(defthm len-write-mem
(equal (len (write-mem v-addr mem value))
(len mem)))
(defthm romp-write-mem
(equal (romp (write-mem v-addr mem value))
(romp mem)))
(defthm romp-cons-write-mem-1
(equal (romp (cons (car mem)
(write-mem v-addr (cdr mem) value)))
(romp mem)))
(defthm romp-cons-write-mem-2
(equal (romp (cons (write-mem v-addr (car mem) value)
(cdr mem)))
(romp mem)))
(defthm ramp-write-mem
(equal (ramp (write-mem v-addr mem value))
(ramp mem)))
(defthm ramp-cons-write-mem-1
(equal (ramp (cons (car mem)
(write-mem v-addr (cdr mem) value)))
(ramp mem)))
(defthm ramp-cons-write-mem-2
(equal (ramp (cons (write-mem v-addr (car mem) value)
(cdr mem)))
(ramp mem)))
(defthm stubp-write-mem
(equal (stubp (write-mem v-addr mem value))
(stubp mem)))
(defthm stubp-cons-write-mem-1
(equal (stubp (cons (car mem)
(write-mem v-addr (cdr mem) value)))
(stubp mem)))
(defthm stubp-cons-write-mem-2
(equal (stubp (cons (write-mem v-addr (car mem) value)
(cdr mem)))
(stubp mem)))
;; RAMP-MEM -- A particular address is RAM.
(defun ramp-mem1 (v-addr mem)
(declare (xargs :guard t))
(if (stubp mem)
nil
(if (atom v-addr)
(ramp mem)
(if (atom mem)
nil
(if (car v-addr)
(ramp-mem1 (cdr v-addr) (cdr mem))
(ramp-mem1 (cdr v-addr) (car mem)))))))
(defun ramp-mem (v-addr mem)
(declare (xargs :guard (true-listp v-addr)))
(ramp-mem1 (reverse v-addr) mem))
;; ALL-RAMP-MEM -- The entire memory is RAM.
(defun all-ramp-mem (n mem)
(declare (xargs :guard (natp n)))
(if (stubp mem)
nil
(if (zp n)
(ramp mem)
(if (atom mem)
nil
(and (all-ramp-mem (1- n) (car mem))
(all-ramp-mem (1- n) (cdr mem)))))))
;; CONSTANT-RAM -- Sets all RAM cells to VALUE.
(defun constant-ram (mem value)
(declare (xargs :guard t))
(if (ramp mem)
(ram value)
(if (atom mem)
mem
(cons (constant-ram (car mem) value)
(constant-ram (cdr mem) value)))))
(defthm true-listp-constant-ram
(implies (true-listp mem)
(true-listp (constant-ram mem value)))
:rule-classes :type-prescription)
(defthm true-listp-constant-ram=>true-listp-mem
(implies (true-listp (constant-ram mem value))
(true-listp mem))
:rule-classes :forward-chaining)
(defthm len-constant-ram
(equal (len (constant-ram mem value))
(len mem)))
(local
(defthmd constant-ram-of-4vp
(implies (or (4vp mem)
(4vp (constant-ram mem value)))
(equal (constant-ram mem value)
mem))
:hints (("Goal" :in-theory (enable 4vp)))))
(defthm 4v-listp-of-constant-ram
(equal (4v-listp (constant-ram mem value))
(4v-listp mem))
:hints (("Subgoal *1/3"
:use (:instance constant-ram-of-4vp
(mem (car mem))))))
(defthm 4v-listp-of-car-constant-ram
(equal (4v-listp (car (constant-ram mem value)))
(4v-listp (car mem))))
(defthm romp-constant-ram
(equal (romp (constant-ram mem value))
(romp mem))
:hints (("Goal" :in-theory (disable stubp bv-is-true-list))))
(defthm ramp-constant-ram
(equal (ramp (constant-ram mem value))
(ramp mem))
:hints (("Goal" :in-theory (disable stubp bv-is-true-list))))
(defthm stubp-constant-ram
(equal (stubp (constant-ram mem value))
(stubp mem))
:hints (("Goal" :in-theory (disable bv-is-true-list))))
;; LEMMAS
(defthm memory-properp-if
(implies (and (memory-properp n size a)
(memory-properp n size b))
(memory-properp n size (if c a b))))
(defthm memory-okp-if
(implies (and (memory-okp n size a)
(memory-okp n size b))
(memory-okp n size (if c a b))))
(defthm memory-properp-constant-ram
(implies (and (memory-properp n size mem)
(true-listp value)
(equal size (len value)))
(memory-properp n size (constant-ram mem value))))
(defthm memory-properp-after-write-mem1
(implies (and (memory-properp n size mem)
(true-listp value)
(equal size (len value))
(equal n (len v-addr)))
(memory-properp n size (write-mem1 v-addr mem value))))
(defthm memory-properp-after-write-mem
(implies (and (memory-properp n size mem)
(true-listp value)
(equal size (len value))
(equal n (len v-addr)))
(memory-properp n size (write-mem v-addr mem value)))
:hints (("Goal"
:use (:instance memory-properp-after-write-mem1
(v-addr (reverse v-addr))))))
(defthm memory-okp-after-write-mem1
(implies (and (memory-okp n size mem)
(bvp value)
(equal size (len value))
(equal n (len v-addr)))
(memory-okp n size (write-mem1 v-addr mem value)))
:hints (("Goal" :in-theory (enable bvp))))
(defthm memory-okp-after-write-mem
(implies (and (memory-okp n size mem)
(bvp value)
(equal size (len value))
(equal n (len v-addr)))
(memory-okp n size (write-mem v-addr mem value)))
:hints (("Goal"
:use (:instance memory-okp-after-write-mem1
(v-addr (reverse v-addr))))))
(defthm v-iff-v-addr1-v-addr2-read-mem1-write-mem1
(implies (and (v-iff v-addr1 v-addr2)
(ramp-mem1 v-addr2 mem)
(equal (len v-addr1) (len v-addr2)))
(equal (read-mem1 v-addr1 (write-mem1 v-addr2 mem value))
(v-fourfix value)))
:hints (("Goal"
:in-theory (enable v-iff))))
(defthm v-iff-v-addr1-v-addr2-read-mem1-write-mem1-not-ram
(implies (and (not (ramp-mem1 v-addr2 mem))
(equal (len v-addr1) (len v-addr2)))
(equal (read-mem1 v-addr1 (write-mem1 v-addr2 mem value))
(read-mem1 v-addr1 mem))))
(defthm not-v-iff-v-addr1-v-addr2-read-mem1-write-mem1
(implies (not (v-iff v-addr1 v-addr2))
(equal (read-mem1 v-addr1 (write-mem1 v-addr2 mem value))
(read-mem1 v-addr1 mem)))
:hints (("Goal"
:in-theory (enable v-iff))))
(defthm read-mem-write-mem
(implies (equal (len v-addr1) (len v-addr2))
(equal (read-mem v-addr1 (write-mem v-addr2 mem value))
(if (and (v-iff v-addr1 v-addr2)
(ramp-mem v-addr2 mem))
(v-fourfix value)
(read-mem v-addr1 mem))))
:hints (("Goal"
:in-theory (enable v-iff))))
(defthm true-listp-read-mem1-of-memory-properp
(implies (memory-properp (len v-addr) size mem)
(true-listp (read-mem1 v-addr mem)))
:rule-classes (:rewrite :type-prescription))
(defthm len-read-mem1-of-memory-properp
(implies (memory-properp (len v-addr) size mem)
(equal (len (read-mem1 v-addr mem))
size)))
(defthm true-listp-read-mem-of-memory-properp
(implies (memory-properp (len v-addr) size mem)
(true-listp (read-mem v-addr mem)))
:hints (("Goal"
:use (:instance true-listp-read-mem1-of-memory-properp
(v-addr (reverse v-addr)))))
:rule-classes (:rewrite :type-prescription))
(defthm true-listp-read-mem-of-memory-properp-32
(implies (memory-properp (len v-addr) 32 mem)
(true-listp (read-mem v-addr mem)))
:rule-classes (:rewrite :type-prescription))
(defthm len-read-mem-of-memory-properp
(implies (memory-properp (len v-addr) size mem)
(equal (len (read-mem v-addr mem))
size))
:hints (("Goal"
:use (:instance len-read-mem1-of-memory-properp
(v-addr (reverse v-addr))))))
(defthm len-read-mem-of-memory-properp-32
(implies (memory-properp (len v-addr) 32 mem)
(equal (len (read-mem v-addr mem))
32))
:hints (("Goal" :in-theory (disable read-mem))))
(defthm bvp-read-mem1-of-memory-okp
(implies (memory-okp (len v-addr) size mem)
(bvp (read-mem1 v-addr mem)))
:rule-classes (:rewrite :type-prescription))
(defthm len-read-mem1-of-memory-okp
(implies (memory-okp (len v-addr) size mem)
(equal (len (read-mem1 v-addr mem))
size)))
(defthm bvp-read-mem-of-memory-okp
(implies (memory-okp (len v-addr) size mem)
(bvp (read-mem v-addr mem)))
:hints (("Goal"
:use (:instance bvp-read-mem1-of-memory-okp
(v-addr (reverse v-addr)))))
:rule-classes (:rewrite :type-prescription))
(defthm bvp-read-mem-of-memory-okp-32
(implies (memory-okp (len v-addr) 32 mem)
(bvp (read-mem v-addr mem)))
:rule-classes (:rewrite :type-prescription))
(defthm len-read-mem-of-memory-okp
(implies (memory-okp (len v-addr) size mem)
(equal (len (read-mem v-addr mem))
size))
:hints (("Goal"
:use (:instance len-read-mem1-of-memory-okp
(v-addr (reverse v-addr))))))
(defthm all-ramp-mem->ramp-mem1
(implies (all-ramp-mem (len v-addr) mem)
(ramp-mem1 v-addr mem)))
(defthm all-ramp-mem->ramp-mem
(implies (all-ramp-mem (len v-addr) mem)
(ramp-mem v-addr mem))
:hints (("Goal"
:use (:instance all-ramp-mem->ramp-mem1
(v-addr (reverse v-addr))))))
(defthm all-ramp-mem-after-write-mem1
(implies (and (all-ramp-mem n mem)
(equal n (len v-addr)))
(all-ramp-mem n (write-mem1 v-addr mem value))))
(defthm all-ramp-mem-after-write-mem
(implies (and (all-ramp-mem n mem)
(equal n (len v-addr)))
(all-ramp-mem n (write-mem v-addr mem value)))
:hints (("Goal"
:use (:instance all-ramp-mem-after-write-mem1
(v-addr (reverse v-addr))))))
(defthm all-ramp-mem-constant-ram
(equal (all-ramp-mem n (constant-ram mem value))
(all-ramp-mem n mem)))
(defthm memory-okp==>memory-properp
(implies (memory-okp n m mem)
(memory-properp n m mem)))
(in-theory (disable mem-theory
memory-properp memory-okp
read-mem1 read-mem
write-mem1 write-mem
ramp-mem1 ramp-mem
all-ramp-mem constant-ram))
|