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 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
|
; UBDD Library
; Copyright (C) 2008-2011 Warren Hunt and Bob Boyer
; License: A 3-clause BSD license. See the LICENSE file distributed with ACL2.
; Significantly revised in 2008 by Jared Davis and Sol Swords.
; Now maintained by Centaur Technology.
;
; Contact:
; Centaur Technology Formal Verification Group
; 7600-C N. Capital of Texas Highway, Suite 300, Austin, TX 78731, USA.
; http://www.centtech.com/
; core.lisp - fundamental operations, e.g., ubddp, eval-bdd, q-not, and q-ite.
(in-package "ACL2")
(include-book "misc/definline" :dir :system)
(include-book "../misc/memory-mgmt-logic")
(include-book "misc/computed-hint-rewrite" :dir :system)
(include-book "tools/rulesets" :dir :system)
(include-book "xdoc/top" :dir :system)
(defxdoc ubdds
:parents (boolean-reasoning)
:short "A @(see hons)-based, unlabeled <a
href='http://en.wikipedia.org/wiki/Binary_decision_diagram'>Binary Decision
Diagram</a> (bdd) representation of Boolean functions."
:long "<p>UBDDs (\"unlabeled bdds\") are a concise and efficient
implementation of binary decision diagrams. Unlike most BDD packages, our
internal nodes are not labelled; instead, each node's depth from the root
determines its variable.</p>
<p>The valid UBDDs are recognized by @(see ubddp). Structurally, UBDDs are a
subset of purely Boolean cons-trees, i.e., trees where all the tips are @('t')
or @('nil'). For instance, the valid one-variable UBDDs are:</p>
<ul>
<li>@('t') or @('nil') for \"var is irrelevant to this decision\", or</li>
<li>@('(t . nil)') for \"if var then @('t') else @('nil'), or</li>
<li>@('(nil . t)') for \"if var then @('nil') else @('t')</li>
</ul>
<p>But @('(t . t)') and @('(nil . nil)') are not permitted—we reduce them
to @('t') and @('nil'), respectively. This restriction leads to a key property
of UBDDs, viz. two UBDDs are semantically equivalent exactly when they are
structurally @(see equal).</p>
<p>The semantics of a UBDD are given by the interpreter function @(see
eval-bdd). Operations that construct UBDDs, such as @(see q-not) and @(see
q-and), are proven correct w.r.t. this interpreter. We also provide various
theorems to reason about UBDD operations, including the important theorem @(see
equal-by-eval-bdds), and implement some @(see computed-hints) for automating
the use of this theorem.</p>
<p>Our UBDD implementation is made efficient via @(see hons-and-memoization).
We construct UBDDs with @(see hons) and @(see memoize) UBDD constructors such
as @(see q-not) and @(see q-and).</p>")
;;; THE CORE UBDD OPERATIONS
(defsection ubddp
:parents (ubdds)
:short "Recognizer for well-formed @(see ubdds)."
:long "<p>Note: memoized for efficiency.</p>"
(defund ubddp (x)
(declare (xargs :guard t))
(mbe :logic (if (atom x)
(booleanp x)
(and (ubddp (car x))
(ubddp (cdr x))
(if (atom (car x))
(not (equal (car x) (cdr x)))
t)))
:exec (cond ((eq x t) t)
((eq x nil) t)
((atom x) nil)
(t (let ((a (car x))
(d (cdr x)))
(cond ((eq a t)
(cond ((eq d nil) t)
((eq d t) nil)
(t (ubddp d))))
((eq a nil)
(cond ((eq d nil) nil)
((eq d t) t)
(t (ubddp d))))
(t (and (ubddp a) (ubddp d)))))))))
(memoize 'ubddp :condition '(consp x))
(defthm ubddp-compound-recognizer
(implies (ubddp x)
(or (consp x)
(booleanp x)))
:hints(("Goal" :in-theory (enable ubddp)))
:rule-classes :compound-recognizer))
(defsection ubdd-listp
:parents (ubdds)
:short "Recognizer for a list of @(see ubdds)."
(defund ubdd-listp (l)
(declare (xargs :guard t))
(if (atom l)
(null l)
(and (ubddp (car l))
(ubdd-listp (cdr l)))))
(local (in-theory (enable ubdd-listp)))
(defthm ubdd-listp-when-atom
(implies (atom x)
(equal (ubdd-listp x)
(not x))))
(defthm ubdd-listp-of-cons
(equal (ubdd-listp (cons a x))
(and (ubddp a)
(ubdd-listp x)))))
(defsection eval-bdd
:parents (ubdds)
:short "Sematics of a UBDD."
:long "<p>@(call eval-bdd) evaluates the UBDD @('x') with respect to a list
of Boolean @('values'), returning a Boolean value which we think of as the
<i>interpretation</i> of @('x') for this particular set of values.</p>
<p>Typically @('x') should be a @(see ubddp) and @('values') should be a @(see
boolean-listp), but we do not enforce this in our @(see guard).</p>
<p>When @('x') is @('t') or @('nil'), it represents a constant function and
always evaluates to itself no matter what @('values') are given for the
variables.</p>
<p>When @('x') is any other tree, we use @('(car values)') to guide us down the
@('car') (the \"true branch\") or @('cdr') (the \"false branch\") of @('x').
In case we run out of @('values'), we pretend that @('values') contains an
infinite list of @('nil')s at the end and just follow the false branches of
@('x') all the way down.</p>"
(defund eval-bdd (x values)
(declare (xargs :guard t))
(cond ((atom x)
;; This "if" fixes the result to a Boolean.
(if x t nil))
((atom values)
(eval-bdd (cdr x) nil))
((car values)
(eval-bdd (car x) (cdr values)))
(t
(eval-bdd (cdr x) (cdr values))))))
(defsection qcar
:parents (ubdds)
:short "The \"true branch\" of a UBDD."
:long "<p>@(call qcar) returns the true branch of the UBDD @('x').</p>
<p>For a compound UBDD, i.e., @('(a . b)'), we simply return @('a'). But for
the atomic UBDDs @('t') and @('nil'), which represent the constant functions
@('t') and @('nil'), this is the identity function.</p>"
(definline qcar (x)
(declare (xargs :guard t))
(if (consp x)
(car x)
x)))
(defsection qcdr
:parents (ubdds)
:short "The \"false branch\" of a UBDD."
:long "<p>@(call qcdr) returns the false branch of the UBDD @('x').</p>
<p>For a compound UBDD, i.e., @('a . b)'), we simply return @('b'). But for
the atomic UBDDs @('t') and @('nil'), which represent the constant functions
@('t') and @('nil'), this is the identity function.</p>"
(definline qcdr (x)
(declare (xargs :guard t))
(if (consp x)
(cdr x)
x)))
(defsection qcons
:parents (ubdds)
:short "Raw construtor for UBDDs."
:long "<p>@(call qcons) constructs the UBDD whose true branch is @('x') and
whose false branch is @('y'). It handles any collapsing which needs to
occur.</p>"
(definline qcons (x y)
(declare (xargs :guard t))
(if (if (eq x t)
(eq y t)
(and (eq x nil) (eq y nil)))
x
(hons x y))))
(defsection ubdd-constructors
:parents (ubdds)
:short "Basic functions for constructing UBDDs.")
(defsection q-not
:parents (ubdd-constructors)
:short "Negate a UBDD."
:long "<p>@(call q-not) builds a new UBDD representing @('(not x)'). That
is, the the following is a theorem:</p>
@({
(equal (eval-bdd (q-not x) values)
(not (eval-bdd x values)))
})
<p>Note: memoized for efficiency.</p>"
(defun q-not (x)
(declare (xargs :guard t))
(if (atom x)
(if x nil t)
(hons (q-not (car x))
(q-not (cdr x)))))
(memoize 'q-not :condition
'(and (consp x)
(or (consp (car x))
(consp (cdr x))))))
(defsection q-ite-fn
:parents (ubdd-constructors)
:short "Basic way to construct a new if-then-else UBDD."
:long "<p>@(call q-ite-fn) builds a new UBDD representing @('(if x y z)').
That is, the following is a theorem:</p>
@({
(equal (eval-bdd (q-ite-fn x y z) values)
(if (eval-bdd x values)
(eval-bdd y values)
(eval-bdd z values)))
})
<p>Normally you will want to use @(see q-ite) instead, which is a macro that is
logically the same as @('q-ite-fn'), but expands into a lazy call of
@('q-ite-fn') and can sometimes avoid the need to evaluate y and z.</p>
<p>Note: memoized for efficiency.</p>"
(defun q-ite-fn (x y z)
(declare (xargs :guard t))
(cond ((null x) z)
((atom x) y)
(t (let ((y (if (hons-equal x y) t y))
(z (if (hons-equal x z) nil z)))
(cond ((hons-equal y z) y)
((and (eq y t) (eq z nil)) x)
;; Calling Q-NOT is meant to improve efficiency.
((and (eq y nil) (eq z t)) (q-not x))
(t (qcons (q-ite-fn (car x) (qcar y) (qcar z))
(q-ite-fn (cdr x) (qcdr y) (qcdr z)))))))))
#||
(memoize 'q-ite-fn :condition
'(and (consp x)
(consp (car x))
(consp (cdr x))
(or (and (consp y) (consp (car y)) (consp (cdr y)))
(and (consp z) (consp (car z)) (consp (cdr z))))))
||#
; The above condition doesn't memoize Q-ITE-FN calls that could have
; an enormous test structure, but with the test structure being
; (HONS <atom> <huge-structure>) or (HONS <huge-structure> <atom>).
(memoize 'q-ite-fn :condition ;; Changed to this on 9 July 2009
'(and (consp x)
(or (consp (car x))
(consp (cdr x))))))
(defsection q-ite
:parents (ubdd-constructors)
:short "Optimized way to construct a new if-then-else UBDD."
:long "<p>@(call q-ite) is a macro that is logically equal to @(call
q-ite-fn). But in the execution, special measures are taken so that we can
sometimes avoid evaluating @('y') or @('z').</p>
<p>At a first approximation, @(call q-ite) expands into</p>
@({
(let ((<x> x))
(cond ((null <x>) z)
((atom <x>) y)
(t
(let* ((<y> y)
(<z> z))
(q-ite-fn <x> <y> <z>)))))
})
<p>The advantage of this over a raw call of @(see q-ite-fn) is that when the
expression for @('x') evaluates to an atom, then we can avoid evaluating @('y')
or @('z'). Hence, when @('y') or @('z') is expensive to evaluate (e.g.,
perhaps they are terms involving calls to UBDD-building operations like @(see
q-not)), we can save some time.</p>
<p>Of course, if neither @('y') nor @('z') is expensive to evaluate, then the
above just adds some minor overhead to each call of @(see q-ite-fn). Sometimes
we can avoid this overhead by recognizing that they are cheap to evaluate. In
particular, we claim that constants and variable symbols are always cheap to
evaluate, and so, if both @('y') and @('z') are either constants or variables,
then we do not bother to introduce the @(see let) statement above and instead
we just lay down a raw call of @(see q-ite-fn).</p>"
(defmacro q-ite (x y z)
(cond ((and (or (quotep y) (atom y))
(or (quotep z) (atom z)))
`(q-ite-fn ,x ,y ,z))
(t
`(mbe :logic (q-ite-fn ,x ,y ,z)
:exec (let ((q-ite-x-do-not-use-elsewhere ,x))
(cond
((null q-ite-x-do-not-use-elsewhere) ,z)
((atom q-ite-x-do-not-use-elsewhere) ,y)
(t
(let* ((q-ite-y-do-not-use-elsewhere ,y)
(q-ite-z-do-not-use-elsewhere ,z))
(prog2$
(last-chance-wash-memory)
(q-ite-fn q-ite-x-do-not-use-elsewhere
q-ite-y-do-not-use-elsewhere
q-ite-z-do-not-use-elsewhere)))))))))))
;;; REASONING ABOUT THE CORE OPERATIONS
(defthm equal-of-booleans-rewrite ; !!! Why is this rule here?
(implies (and (booleanp x)
(booleanp y))
(equal (equal x y)
(iff x y)))
:rule-classes ((:rewrite :backchain-limit-lst 1)))
(defsection eval-bdd
:extension eval-bdd
(local (in-theory (enable eval-bdd)))
(defthm eval-bdd-when-not-consp
(implies (not (consp x))
(equal (eval-bdd x values)
(if x t nil))))
(defthm eval-bdd-of-non-consp-cheap
;; BOZO rescued from below, blah, bad to have both...?
(implies (not (consp x))
(equal (eval-bdd x vals)
(if x t nil)))
:rule-classes ((:rewrite :backchain-limit-lst 0)))
(defthm eval-bdd-of-nil
(equal (eval-bdd nil values)
nil))
(defthm eval-bdd-of-t
(equal (eval-bdd t values)
t))
(defthm eval-bdd-when-non-consp-values
;; !!! Could probably be better expressed as a congruence
;; rule for a LIST-EQUIV operation.
(implies (and (syntaxp (not (equal vals *nil*)))
(atom vals))
(equal (eval-bdd x vals)
(eval-bdd x nil)))))
(defsection max-depth
:parents (equal-by-eval-bdds) ;; bozo this is very general...
:short "Depth of a cons tree."
(defund max-depth (x)
(declare (xargs :guard t))
(if (atom x)
0
(1+ (max (max-depth (car x))
(max-depth (cdr x))))))
(memoize 'max-depth :condition '(consp x)))
(defsection equal-by-eval-bdds
:parents (ubdds)
:short "Reasoning strategy: reduce equality of @(see ubdds) to equality of an
arbitrary evaluation."
:long "<p>Suppose we know that @('x') and @('y') are @(see ubddp) objects,
and we are trying to prove they are equal. The @('equal-by-eval-bdds')
approach involves trying to establish this equality by proving the equivalent
fact, roughly:</p>
@({
(forall values
(equal (eval-bdd x values)
(eval-bdd y values)))
})
<p>Actually, we can even assume that @('values') has a certain length and
contains only Booleans, but usually these extra facts are not needed.</p>
<p>It is somewhat tricky to implement this sort of reduction in ACL2. Our
approach is very similar to the \"pick a point\" strategy for proving that sets
are equal, described in the following paper:</p>
<p>Jared Davis. <a
href='http://www.cs.utexas.edu/~jared/publications/2004-acl2-osets/set-theory.pdf'>
Finite Set Theory based on Fully Ordered Lists.</a> In <a
href='http://www.cs.utexas.edu/users/moore/acl2/workshop-2004/'>ACL2 2004</a>.
November, 2004, Austin, TX, USA.</p>
<p>The computed hint @(see equal-by-eval-bdds-hint) automates the strategy.
That is, when this hint is active, ACL2 will automatically try to use
@('equal-by-eval-bdds') to prove equalities when we can determine (with
rewriting) that @('x') and @('y') are @(see ubddp)s.</p>
<p>At any rate, if you want to prove that @('(foo ...)') and @('(bar ...)') are
equal BDDs, you might start by proving the rules:</p>
@({
(equal (eval-bdd (foo ...) values)
(... expression 1 ...))
(equal (eval-bdd (bar ...) values)
(... expression 2 ...))
})
<p>Then you will be able to prove @('(equal (foo ...) (bar ...))') by arguing
that expression 1 and expression 2 are equal.</p>"
;; We want to write the rule:
;;
;; (ubddp x)
;; (ubddp y)
;; -------------
;; (equal x y) = (forall values
;; (equal (eval-bdd x values)
;; (eval-bdd y values)))
;;
;; But this is not possible to do with a regular rewrite rule due to the
;; nested quantifier. So we instead we begin by expressing our hypotheses as
;; a constraint about uninterpreted function symbols.
(encapsulate
(((bdd-hyps) => *)
((bdd-lhs) => *)
((bdd-rhs) => *))
(local (defun bdd-hyps () nil))
(local (defun bdd-lhs () nil))
(local (defun bdd-rhs () nil))
(defthmd bdd-equivalence-constraint
(implies (and (bdd-hyps)
(equal (len arbitrary-values)
(max (max-depth (bdd-lhs))
(max-depth (bdd-rhs))))
(boolean-listp arbitrary-values)
(ubddp (bdd-lhs))
(ubddp (bdd-rhs)))
(equal (eval-bdd (bdd-lhs) arbitrary-values)
(eval-bdd (bdd-rhs) arbitrary-values)))))
;; Now we use this constraint to prove a generic theorem that says (bdd-lhs)
;; and (bdd-rhs) are equal when the (bdd-hyps) hold. This takes a bit of
;; work because of the weaker constraint we chose above.
(local (defund find-conflicting-values (x y)
(declare (xargs :measure (+ (acl2-count x) (acl2-count y))))
(if (or (consp x)
(consp y))
;; At least one of them is a cons. We descend both trees and try
;; to discover a path that will break their equality.
(mv-let (car-successp car-path)
(find-conflicting-values (qcar x) (qcar y))
(if car-successp
;; We took the "true" branch.
(mv t (cons t car-path))
(mv-let (cdr-successp cdr-path)
(find-conflicting-values (qcdr x) (qcdr y))
(if cdr-successp
(mv t (cons nil cdr-path))
(mv nil nil)))))
;; Otherwise, both are atoms. If they are equal, then we have failed to
;; find a conflicting path. But if they are not equal, then this path
;; violates their success.
(mv (not (equal x y)) nil))))
(local (defthm mv-nth-1
(equal (mv-nth 1 x)
(cadr x))
:hints(("Goal" :in-theory (enable mv-nth)))))
(local (defthm lemma1
(implies (and (ubddp x)
(ubddp y)
(car (find-conflicting-values x y)))
(equal (equal (eval-bdd x (cadr (find-conflicting-values x y)))
(eval-bdd y (cadr (find-conflicting-values x y))))
nil))
:hints(("Goal" :in-theory (enable (:induction find-conflicting-values)
eval-bdd ubddp)
:induct (find-conflicting-values x y)
:expand ((find-conflicting-values x y))))))
(local (defthm lemma2
(implies (and (ubddp x)
(ubddp y))
(equal (car (find-conflicting-values x y))
(not (equal x y))))
:hints(("Goal" :in-theory (enable (:induction find-conflicting-values)
ubddp)
:induct (find-conflicting-values x y)
:expand ((find-conflicting-values x y))))))
(local (defthm lemma3
(<= (len (cadr (find-conflicting-values x y)))
(max (max-depth x) (max-depth y)))
:hints(("Goal" :in-theory (enable (:induction find-conflicting-values)
ubddp)
:induct (find-conflicting-values x y)
:expand ((find-conflicting-values x y)
(max-depth x) (max-depth y))))
:rule-classes (:linear :rewrite)))
(local (defthm lemma4
(boolean-listp (cadr (find-conflicting-values x y)))
:hints(("Goal" :in-theory (enable find-conflicting-values)))))
(local (defun extend-to-len (lst n)
(declare (xargs :guard t))
(if (zp (nfix n))
lst
(if (atom lst)
(cons nil (extend-to-len lst (1- n)))
(cons (car lst) (extend-to-len (cdr lst) (1- n)))))))
(local (defthm extend-to-len-of-cons
(equal (extend-to-len (cons a b) n)
(cons a (extend-to-len b (1- n))))))
(local (defun eval-extend-induct (x lst n)
(if (zp (nfix n))
(cons lst x)
(if (atom lst)
(list (eval-extend-induct (car x) lst (1- n))
(eval-extend-induct (cdr x) lst (1- n)))
(list (eval-extend-induct (car x) (cdr lst) (1- n))
(eval-extend-induct (cdr x) (cdr lst) (1- n)))))))
(local (defthm eval-bdd-of-extend
(equal (eval-bdd x (extend-to-len lst n))
(eval-bdd x lst))
:hints (("goal" :in-theory (enable eval-bdd)
:induct (eval-extend-induct x lst n)))))
(local (defthm len-of-extend
(equal (len (extend-to-len lst n))
(max (len lst) (nfix n)))))
(local (defthm boolean-listp-of-extend
(implies (boolean-listp lst)
(boolean-listp (extend-to-len lst n)))))
;; Finally, here is the theorem we are going to functionally instantiate.
(defthm equal-by-eval-bdds
(implies (and (bdd-hyps)
(ubddp (bdd-lhs))
(ubddp (bdd-rhs)))
(equal (equal (bdd-lhs) (bdd-rhs))
t))
:hints(("Goal"
:use ((:instance bdd-equivalence-constraint
(arbitrary-values (extend-to-len
(cadr (find-conflicting-values
(bdd-lhs) (bdd-rhs)))
(max (max-depth (bdd-lhs))
(max-depth (bdd-rhs)))))))))))
(defsection term-is-bdd
:parents (equal-by-eval-bdds)
:short "Heuristic for deciding which terms are UBDDs."
:long "<p>The computed hint @(see equal-by-eval-bdds-hint) is supposed to
automatically apply @(see equal-by-eval-bdds) when we are trying to prove that
two @(see ubdds) are @(see equal). But a basic prerequisite to being able to
do this is to determine whether the terms on either side of an equality are
actually UBDDs or not.</p>
<p>We use the function @('term-is-bdd') to decide if a particular term is a
BDD. For variables, we ask the rewriter if the variable is known to be a
@('ubddp'). For quoted constants, we just run @('ubddp') to see if it's a
UBDD.</p>
<p>For function calls, we set up a @('bdd-fns') table that lists functions that
produce BDDs, and we just check whether the function being called is mentioned
in this table. This is a pretty dumb heuristic, and we may eventually want a
more flexible notion here that allows us to pattern match against @(see
mv-nth)s and so on.</p>
<p>When you add your own BDD producing functions, you may wish to use @(call
add-bdd-fn) to add them to the @('bdd-fns') table.</p>"
(defmacro bdd-fns ()
'(cdr (assoc 'bdd-fns (table-alist 'bdd-fns world))))
(defmacro set-bdd-fns (val)
`(table bdd-fns 'bdd-fns ,val))
(defmacro add-bdd-fn (fnname)
`(set-bdd-fns (cons ',fnname (bdd-fns))))
(defmacro add-bdd-fns (fnnames)
`(set-bdd-fns (append ',fnnames (bdd-fns))))
(set-bdd-fns nil)
;; What follows is nasty ACL2 magic. You probably don't want to try to
;; understand it unless you need to.
(defun term-is-bdd (term hyps whole hist pspv world ctx state)
;; This function decides whether we think that TERM is a ubddp. If it is a
;; variable, we ask the rewriter if it's known to be a ubddp. If it's a
;; quoted constant, we see if its value is a ubddp. And if it's a function
;; call, we see if it is one of the functions we explicitly mentioned in
;; bdd-fns.
(declare (xargs :mode :program :stobjs state))
(cond ((atom term)
(let ((term-and-ttree
(computed-hint-rewrite
`(ubddp ,term) hyps t whole hist pspv world ctx state)))
(equal (car term-and-ttree) *t*)))
((eq (car term) 'quote)
;; !!! Note to Sol: Jared changed this to ubddp from booleanp.
(ubddp (cadr term)))
(t
(member-eq (car term) (bdd-fns))))))
(defsection equal-by-eval-bdds-hint
:parents (equal-by-eval-bdds)
:short "Basic automation of @(see equal-by-eval-bdds)."
:long "<p>This is a computed hint in the spirits of the ordered sets
library's pick-a-point automation. It reduces questions of BDD equality to
questions of BDD evaluation.</p>"
(defun equal-by-eval-bdds-hint-fn (id clause hist pspv ctx stable world state)
(declare (xargs :mode :program :stobjs state)
(ignorable id))
(if (not stable)
;; Don't suggest anything until the goal is stable. This gives other
;; rules a chance to fire.
nil
(let* ((rcnst (access prove-spec-var pspv :rewrite-constant))
(ens (access rewrite-constant rcnst :current-enabled-structure)))
(if (not (enabled-numep (fnume '(:rewrite equal-by-eval-bdds) world) ens))
;; Don't suggest anything if equal-by-eval-bdds is
;; disabled. This gives the user an easy way to turn off
;; the strategy.
nil
(let ((conclusion (car (last clause))))
(if (not (and (consp conclusion)
(or (eq (car conclusion) 'equal)
(eq (car conclusion) 'not))))
nil
;; We look for conclusions of the form (equal lhs rhs)
;; or (not lhs). We think of (not lhs) as the same as
;; (equal lhs nil).
(let ((lhs (second conclusion))
(rhs (or (third conclusion) *nil*))
(hyps (dumb-negate-lit-lst (butlast clause 1))))
;; We only want to apply the hint if we believe lhs
;; and rhs are ubddp objects.
(let ((lhs-okp
(term-is-bdd lhs hyps clause hist pspv world ctx state)))
(if (not lhs-okp)
nil
(let ((rhs-okp
(term-is-bdd rhs hyps clause hist pspv world ctx state)))
(if (not rhs-okp)
nil
;; We think LHS and RHS are ubddp's. Go ahead
;; and give the hint.
(let ((hint `(:use (:functional-instance
equal-by-eval-bdds
(bdd-hyps (lambda () (and ,@hyps)))
(bdd-lhs (lambda () ,lhs))
(bdd-rhs (lambda () ,rhs))))))
(prog2$
;; And tell the user what's happening.
(cw "~|~%We now appeal to ~s3 in an ~
attempt to show that ~x0 and ~x1 ~
are equal because all of their ~
evaluations under ~s2 are the ~
same. (You can disable ~s3 to ~
avoid this. See :doc ~s3 for more ~
details.)~|~%"
(untranslate lhs nil world)
(untranslate rhs nil world)
'eval-bdd
'equal-by-eval-bdds)
hint)))))))))))))
(defmacro equal-by-eval-bdds-hint ()
`(equal-by-eval-bdds-hint-fn id clause hist pspv ctx
stable-under-simplificationp world state))
(add-default-hints!
'((equal-by-eval-bdds-hint))))
(defsection equal-by-eval-bdds-hint-heavy
:parents (equal-by-eval-bdds)
:short "Experimental \"heavyweight\" hint."
:long "<p>Sometimes it is difficult to make use of equalities between BDDs
when carrying out the pick-a-point proof. QS-SUBSET-MODE is one way to
approach this. And the HEAVY hint below is another. The idea is to replace
other equalities between BDDs with what they mean in terms of the
arbitrary-values which have just been picked.</p>
<p>BOZO this was a precursor to the defwitness stuff, right? Should we port
this to use defwitness, etc.?</p>"
(defun lit-find-equality (lit)
(case-match lit
(('not ('ubddp . &) . &)
(mv nil nil nil))
(('ubddp . &)
(mv nil nil nil))
(('not ('equal a b . &) . &)
(mv 'ineq a b))
(('not a . &)
(mv 'eq a *nil*))
(('equal a b . &)
(mv 'eq a b))
(& (mv 'ineq lit *nil*))))
(defun collect-ubddp-eq-and-ineqs (clause whole hyps hist pspv ctx world state)
(declare (xargs :mode :program :stobjs state))
(if (atom clause)
(mv nil nil nil state)
(mv-let (eqs ineqs remhyps state)
(collect-ubddp-eq-and-ineqs (cdr clause) whole hyps hist pspv ctx world
state)
(let ((conclusion (car clause)))
(mv-let (sense lhs rhs)
(lit-find-equality conclusion)
(let ((lhs-okp
(term-is-bdd lhs hyps whole hist pspv world ctx state)))
(if (not lhs-okp)
(mv eqs ineqs (cons (dumb-negate-lit conclusion) remhyps) state)
(let ((rhs-okp
(term-is-bdd rhs hyps whole hist pspv world ctx state)))
(if (not rhs-okp)
(mv eqs ineqs (cons (dumb-negate-lit conclusion) remhyps) state)
(case sense
(eq (mv (cons (cons lhs rhs) eqs) ineqs
(cons (dumb-negate-lit conclusion) remhyps)
state))
(ineq (mv eqs (cons (cons lhs rhs) ineqs) remhyps state))
(t (mv eqs ineqs
(cons (dumb-negate-lit conclusion) remhyps)
state))))))))))))
(defun collect-eq-hyps (eq-hyps acc)
(declare (xargs :mode :program))
(if (atom eq-hyps)
acc
(collect-eq-hyps
(cdr eq-hyps)
(cons `(equal (eval-bdd ,(caar eq-hyps) arbitrary-values)
(eval-bdd ,(cdar eq-hyps) arbitrary-values))
acc))))
(defun collect-hints (eqs hyps)
(declare (xargs :mode :program))
(if (atom eqs)
nil
(cons `(:use (:functional-instance equal-by-eval-bdds
(bdd-hyps (lambda () (and . ,hyps)))
(bdd-lhs (lambda () ,(caar eqs)))
(bdd-rhs (lambda () ,(cdar eqs)))))
(collect-hints (cdr eqs) hyps))))
(defun equal-by-eval-bdds-hint-heavy-fn (id clause hist pspv ctx stable world state)
(declare (xargs :mode :program :stobjs state)
(ignorable id))
(if (not stable)
;; Don't suggest anything until the goal is stable. This
;; gives other rules a chance to fire.
(value nil)
(let* ((rcnst (access prove-spec-var pspv :rewrite-constant))
(ens (access rewrite-constant rcnst :current-enabled-structure)))
(if (not (enabled-numep (fnume '(:rewrite equal-by-eval-bdds) world) ens))
;; Don't suggest anything if equal-by-eval-bdds is
;; disabled. This gives the user an easy way to turn off
;; the strategy.
(value nil)
(let ((hyps (dumb-negate-lit-lst clause)))
(mv-let (eqs ineqs new-hyps state)
(collect-ubddp-eq-and-ineqs clause clause hyps
hist pspv ctx world state)
(if (not eqs)
(value nil)
(let* ((new-hyps (collect-eq-hyps ineqs new-hyps))
(hints (collect-hints eqs new-hyps)))
(prog2$ nil ;; (cw "hyps: ~x0~%hints: ~x1~%" hyps hints)
(value (list :or hints)))))))))))
(defmacro equal-by-eval-bdds-hint-heavy ()
`(equal-by-eval-bdds-hint-heavy-fn
id clause hist pspv ctx stable-under-simplificationp world state))
(defun equal-by-eval-bdds-inst-fn (lhs rhs concl add-hyps
instantiate-hyps clause hist pspv ctx
world state)
(declare (xargs :mode :program :stobjs state))
(let ((clhyps (dumb-negate-lit-lst clause)))
(mv-let (cleqs clineqs cl-new-hyps state)
(collect-ubddp-eq-and-ineqs
clause clause clhyps hist pspv ctx world state)
(mv-let (lhs rhs state)
(cond ((and lhs rhs)
(mv (car lhs) (car rhs) state))
(concl
(er-let* ((concl (translate (car concl) t t t
'equal-by-eval-bdds-inst world state)))
(mv-let (sense lhs rhs)
(lit-find-equality (car concl))
(declare (ignore sense))
(mv lhs rhs state))))
(t (mv (caar (last cleqs)) (cdar (last cleqs)) state)))
(let* ((hyps
(if (and instantiate-hyps (car instantiate-hyps))
(collect-eq-hyps clineqs cl-new-hyps)
clhyps))
(hyps
(if add-hyps
(append (car add-hyps) hyps)
hyps)))
(let ((res `(:use (:functional-instance
equal-by-eval-bdds
(bdd-hyps (lambda () (and . ,hyps)))
(bdd-lhs (lambda () ,lhs))
(bdd-rhs (lambda () ,rhs))))))
(prog2$ (cw "res: ~x0~%" res)
(value res))))))))
(defmacro equal-by-eval-bdds-inst (&key (lhs 'nil lhsp)
(rhs 'nil rhsp)
(concl 'nil conclp)
(add-hyps 'nil add-hypsp)
(instantiate-hyps 'nil instantiate-hypsp))
`(equal-by-eval-bdds-inst-fn ,(and lhsp `(list ,lhs))
,(and rhsp `(list ,rhs))
,(and conclp `(list ,concl))
,(and add-hypsp `(list ,add-hyps))
,(and instantiate-hypsp `(list ,instantiate-hyps))
clause hist pspv ctx world state)))
(defsection q-not
:extension q-not
(add-bdd-fn q-not)
(local (in-theory (enable q-not)))
(defthm consp-of-q-not
(equal (consp (q-not x))
(consp x))
:hints(("Goal" :in-theory (enable q-not))))
(defthm equal-t-and-q-not
(equal (equal t (q-not x))
(not x)))
(defthm equal-nil-and-q-not
(equal (equal nil (q-not x))
(and (atom x)
(if x t nil))))
(defthm q-not-under-iff
(iff (q-not x)
(if (consp x)
t
(if x nil t))))
(defthm ubddp-of-q-not
(implies (force (ubddp x))
(equal (ubddp (q-not x))
t))
:hints(("Goal" :in-theory (enable ubddp))))
(defthm eval-bdd-of-q-not
(equal (eval-bdd (q-not x) values)
(not (eval-bdd x values)))
:hints(("Goal" :in-theory (enable eval-bdd)))))
(defsection q-ite
:extension q-ite
(add-bdd-fn q-ite-fn)
(add-macro-alias q-ite q-ite-fn)
(local (in-theory (disable (:type-prescription q-ite-fn)
equal-of-booleans-rewrite
eval-bdd-when-not-consp
eval-bdd-when-non-consp-values)))
(local (defun q-ite-induct (x y z)
(declare (ignorable y z))
(cond ((null x) nil)
((atom x) nil)
(t (let ((y (if (equal x y) t y))
(z (if (equal x z) nil z)))
(list (q-ite-induct (car x) (qcar y) (qcar z))
(q-ite-induct (cdr x) (qcdr y) (qcdr z))))))))
(defthm ubddp-of-q-ite
(implies (and (force (ubddp x))
(force (ubddp y))
(force (ubddp z)))
(equal (ubddp (q-ite x y z))
t))
:hints(("Goal"
:in-theory (e/d (ubddp) (default-car default-cdr))
:induct (q-ite-induct x y z)
:expand ((:free (y z) (q-ite-fn x y z))
(:free (y z) (q-ite-fn nil y z))
(:free (y z) (q-ite-fn t y z))))))
(local (defun q-ite-induct-vals (x y z vals)
(declare (ignorable y z))
(cond ((null x) nil)
((atom x) nil)
(t (let ((y (if (equal x y) t y))
(z (if (equal x z) nil z)))
(cond ((car vals)
(q-ite-induct-vals (car x) (qcar y) (qcar z) (cdr vals)))
(t
(q-ite-induct-vals (cdr x) (qcdr y) (qcdr z) (cdr vals)))))))))
(with-output
:off prove
(defthm eval-bdd-of-q-ite
(equal (eval-bdd (q-ite x y z) values)
(if (eval-bdd x values)
(eval-bdd y values)
(eval-bdd z values)))
:hints(("Goal"
:in-theory (enable eval-bdd)
:induct (q-ite-induct-vals x y z values)
:expand ((:free (y z) (q-ite-fn x y z))
(:free (y z) (q-ite-fn nil y z))
(:free (y z) (q-ite-fn t y z))))))))
(defsection canonicalize-q-not
:extension q-not
(defthm canonicalize-q-not
(implies (force (ubddp x))
(equal (q-not x)
(q-ite x nil t)))
:rule-classes :definition))
(defsection canonicalize-to-q-ite
:parents (ubdds q-ite)
:short "Reasoning strategy: rewrite all BDD-building functions into calls of
@(see q-ite)."
:long "<p>This isn't an especially good reasoning strategy, and we generally
prefer @(see equal-by-eval-bdds) instead.</p>
<p>@('canonicalize-to-q-ite') is a <see topic='@(url rulesets)'>ruleset</see>
that holds theorems for rewriting other BDD-constructing functions into
@('q-ite') form.</p>"
(def-ruleset canonicalize-to-q-ite '(canonicalize-q-not)))
(defsection q-ite-reductions
:parents (q-ite)
:short "Basic rewriting of @(see q-ite) expressions."
:long "<p>When using the @(see canonicalize-to-q-ite) strategy, you generally
end up with big nests of @(see q-ite) calls. Afterward, these basic sorts of
reductions can be useful.</p>"
;; !!! I think we should maybe be able to get some more of these
;; hypothesis free if we change q-ite-fn around a bit so that it
;; coerces atoms into booleans. Would performance be okay?
(defthm |(q-ite x (q-ite y nil t) z)|
(implies (and (syntaxp (not (equal z ''t))) ;; Prevents loops (see next rule)
(force (ubddp x))
(force (ubddp y))
(force (ubddp z)))
(equal (q-ite x (q-ite y nil t) z)
(q-ite y
(q-ite x nil z)
(q-ite x t z)))))
(defthm |(q-ite x (q-ite y nil t) t)|
;; ACL2's loop-stopper works.
(implies (and (force (ubddp x))
(force (ubddp y))
(force (ubddp z)))
(equal (q-ite x (q-ite y nil t) t)
(q-ite y (q-ite x nil t) t))))
(defthm |(q-ite (q-ite a b c) x y)|
(implies (and (force (ubddp a))
(force (ubddp b))
(force (ubddp c))
(force (ubddp x))
(force (ubddp y)))
(equal (q-ite (q-ite a b c) x y)
(q-ite a
(q-ite b x y)
(q-ite c x y)))))
(defthm |(q-ite x y y)|
(equal (q-ite x y y)
y)
:hints(("Goal" :in-theory (enable q-ite))))
(defthm |(q-ite x x y)|
(implies (and (force (ubddp x))
(force (ubddp y)))
(equal (q-ite x x y)
(q-ite x t y))))
(defthm |(q-ite x y x)|
(equal (q-ite x y x)
(q-ite x y nil))
:hints(("Goal" :in-theory (e/d (q-ite) ((force))))))
(defthm |(q-ite x t nil)|
(implies (force (ubddp x))
(equal (q-ite x t nil)
x)))
(defthm |(q-ite non-nil y z)|
(implies (and (atom x)
x)
(equal (q-ite x y z)
y))
:hints(("Goal" :in-theory (enable q-ite))))
(defthm |(q-ite t x y)|
(equal (q-ite t x y)
x)
:hints(("Goal" :in-theory (enable q-ite))))
(defthm |(q-ite nil x y)|
(equal (q-ite nil x y)
y)
:hints(("Goal" :in-theory (enable q-ite)))))
|