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 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500
|
; 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/
;
; extra-operations.lisp - additional bdd operations (q-and, q-or, etc.)
(in-package "ACL2")
(include-book "core")
(local (defun q-binop-induct (x y vals)
(declare (xargs :verify-guards nil))
(if (atom x)
(list x y vals)
(if (atom y)
nil
(if (car vals)
(q-binop-induct (car x) (car y) (cdr vals))
(q-binop-induct (cdr x) (cdr y) (cdr vals)))))))
(defsection cheap-and-expensive-arguments
:parents (ubdd-constructors)
:short "Sort arguments into those that we think are definitely cheap to evaluate
versus those that may be expensive."
:long "<p>This is the same idea as in @(see q-ite). Variables and quoted
constants are cheap to evaluate, so in associative/commutative operations like
@(see q-and) we prefer to evaluate them first.</p>"
(defun cheap-and-expensive-arguments-aux (x cheap expensive)
(declare (xargs :mode :program))
(if (atom x)
(mv cheap expensive)
(if (or (quotep (car x))
(atom (car x)))
(cheap-and-expensive-arguments-aux (cdr x) (cons (car x) cheap) expensive)
(cheap-and-expensive-arguments-aux (cdr x) cheap (cons (car x) expensive)))))
(defun cheap-and-expensive-arguments (x)
(declare (xargs :mode :program))
(mv-let (cheap expensive)
(cheap-and-expensive-arguments-aux x nil nil)
(mv (reverse cheap) (reverse expensive)))))
(defsection q-and
:parents (ubdd-constructors)
:short "@(call q-and) constructs a UBDD representing the conjunction of its
arguments."
:long "<p>In the logic,</p>
@({
(Q-AND) = T
(Q-AND X) = X
(Q-AND X Y) = (Q-BINARY-AND X Y)
(Q-AND X Y Z) = (Q-BINARY-AND X (Q-BINARY-AND Y Z))
... etc ...
})
<p>But as a special optimization, in the execution, when there is more than one
argument, we can sometimes \"short-circuit\" the computation and avoid
evaluating some arguments. In particular, we classify the arguments as cheap
or expensive using @(see cheap-and-expensive-arguments). We then arrange
things so that the cheap arguments are considered first. If any cheap argument
is @('nil'), we can stop before any expensive arguments even need to be
computed.</p>"
(defund q-binary-and (x y)
(declare (xargs :guard t))
(cond ((atom x)
(if x
;; [Jared hack]: normalize in the atom case for fewer hyps
(if (atom y) (if y t nil) y)
nil))
((atom y)
;; We know x is not an atom, so no need to normalize
(if y x nil))
((hons-equal x y)
x)
(t
(qcons (q-binary-and (car x) (car y))
(q-binary-and (cdr x) (cdr y))))))
(add-bdd-fn q-binary-and)
(defun q-and-macro-logic-part (args)
;; Generates the :logic part for a q-and MBE call.
(declare (xargs :mode :program))
(cond ((atom args)
t)
((atom (cdr args))
(car args))
(t
`(q-binary-and ,(car args) ,(q-and-macro-logic-part (cdr args))))))
(defun q-and-macro-exec-part (args)
;; Generates the :exec part for a q-and MBE call.
(declare (xargs :mode :program))
(cond ((atom args)
t)
((atom (cdr args))
(car args))
(t
`(let ((q-and-x-do-not-use-elsewhere ,(car args)))
(and q-and-x-do-not-use-elsewhere
(prog2$
(last-chance-wash-memory)
(q-binary-and q-and-x-do-not-use-elsewhere
,(q-and-macro-exec-part (cdr args)))))))))
(defun q-and-macro-fn (args)
(declare (xargs :mode :program))
(cond ((atom args)
t)
((atom (cdr args))
(car args))
(t
(mv-let (cheap expensive)
(cheap-and-expensive-arguments args)
(if (not expensive)
;; If everything is cheap, there's no reason to bother
;; with all this MBE stuff.
(q-and-macro-logic-part cheap)
;; Otherwise, try to process the cheap arguments first in
;; hopes of avoiding as many expensive computations as
;; possible. It would be nice if the :logic part below did
;; not have to have the arguments reordered, but to do that
;; we need a hyp-free associativity rule for q-and (for our
;; guard theorems), and that's hard. Oh well.
(let ((reordered-args (append cheap expensive)))
`(mbe :logic ,(q-and-macro-logic-part reordered-args)
:exec ,(q-and-macro-exec-part reordered-args))))))))
(defmacro q-and (&rest args)
(q-and-macro-fn args))
(add-macro-alias q-and q-binary-and)
(add-binop q-and q-binary-and)
(local (in-theory (enable q-and)))
(defthm ubddp-of-q-and
(implies (and (force (ubddp x))
(force (ubddp y)))
(equal (ubddp (q-and x y))
t))
:hints(("Goal" :in-theory (enable ubddp))))
(defthm eval-bdd-of-q-and
(equal (eval-bdd (q-and x y) values)
(and (eval-bdd x values)
(eval-bdd y values)))
:hints(("Goal"
:in-theory (enable eval-bdd)
:induct (q-binop-induct x y values)
:expand (q-and x y))))
(defthm canonicalize-q-and
(implies (and (force (ubddp x))
(force (ubddp y)))
(equal (q-and x y)
(q-ite x y nil))))
(add-to-ruleset canonicalize-to-q-ite '(canonicalize-q-and))
;; One strategy is to canonicalize away q-and into q-ite whenever it is seen,
;; using canonicalize-q-and. But if this strategy is not being followed,
;; weak-boolean-listp may find the following theorems useful. It is
;; important that these theorems come after canonicalize-q-and, since in
;; particular q-and-of-nil is to verify the guards when the q-and macro is
;; being used.
(defthm q-and-of-nil
(and (equal (q-and nil x) nil)
(equal (q-and x nil) nil))
:hints(("Goal" :in-theory (disable canonicalize-q-and))))
(defthm q-and-of-t-slow
(and (equal (q-and t x) (if (consp x) x (if x t nil)))
(equal (q-and x t) (if (consp x) x (if x t nil))))
:hints(("Goal" :in-theory (disable canonicalize-q-and))))
(defthm q-and-of-t-aggressive
(implies (force (ubddp x))
(and (equal (q-and t x) x)
(equal (q-and x t) x))))
(defthm q-and-symmetric
(equal (q-and x y)
(q-and y x))
:hints(("Goal" :in-theory (disable equal-by-eval-bdds
canonicalize-q-and))))
(memoize 'q-binary-and
:condition '(and (consp x) (consp y))
:commutative 'q-and-symmetric)
(defthm q-and-of-self-slow
(equal (q-and x x) (if (consp x) x (if x t nil))))
(defthm q-and-of-self-aggressive
(implies (force (ubddp x))
(equal (q-and x x) x)))
;; The following ensures that a trivial usage of our Q-AND macro
;; will succeed in guard verification.
(local (in-theory (disable q-and)))
(local (defun test-q-and (a b c x y z)
(declare (xargs :guard t
:guard-hints
(("Goal" :in-theory (disable (force))))))
(list (q-and x y)
(q-and x (q-not y))
(q-and (q-not x) y)
(q-and x y (q-not x) a b (q-ite y z c))))))
(defsection q-or
:parents (ubdd-constructors)
:short "@(call q-or) constructs a UBDD representing the disjunction of its
arguments."
:long "<p>In the logic,</p>
@({
(Q-OR) = NIL
(Q-OR X) = X
(Q-OR X Y) = (Q-BINARY-OR X Y)
(Q-OR X Y Z) = (Q-BINARY-OR X (Q-BINARY-OR Y Z))
})
<p>In the execution, we use the same sort of optimization as in @(see
q-and).</p>"
(defund q-binary-or (x y)
(declare (xargs :guard t))
(cond ((atom x)
(if x
t
;; [Jared hack]: normalize atoms
(if (atom y) (if y t nil) y)))
((atom y)
;; We know x is not an atom, so no need to normalize.
(if y t x))
((hons-equal x y)
x)
(t
(let ((l (q-binary-or (car x) (car y)))
(r (q-binary-or (cdr x) (cdr y))))
(qcons l r)))))
(add-bdd-fn q-binary-or)
(defun q-or-macro-logic-part (args)
(declare (xargs :mode :program))
(cond ((atom args)
nil)
((atom (cdr args))
(car args))
(t
`(q-binary-or ,(car args) ,(q-or-macro-logic-part (cdr args))))))
(defun q-or-macro-exec-part (args)
(declare (xargs :mode :program))
(cond ((atom args)
nil)
((atom (cdr args))
(car args))
(t
`(let ((q-or-x-do-not-use-elsewhere ,(car args)))
;; We could be slightly more permissive and just check
;; for any non-nil atom here. But it's probably faster
;; to check equality with t and we probably don't care
;; about performance on non-ubddp bdds?
(if (eq t q-or-x-do-not-use-elsewhere)
t
(prog2$
(last-chance-wash-memory)
(q-binary-or q-or-x-do-not-use-elsewhere
,(q-or-macro-exec-part (cdr args)))))))))
(defun q-or-macro-fn (args)
(declare (xargs :mode :program))
(cond ((atom args)
nil)
((atom (cdr args))
(car args))
(t
(mv-let (cheap expensive)
(cheap-and-expensive-arguments args)
(if (not expensive)
(q-or-macro-logic-part cheap)
(let ((reordered-args (append cheap expensive)))
`(mbe :logic ,(q-or-macro-logic-part reordered-args)
:exec ,(q-or-macro-exec-part reordered-args))))))))
(defmacro q-or (&rest args)
(q-or-macro-fn args))
(add-macro-alias q-or q-binary-or)
(add-binop q-or q-binary-or)
(local (in-theory (enable q-or)))
(defthm ubddp-of-q-or
(implies (and (force (ubddp x))
(force (ubddp y)))
(equal (ubddp (q-or x y))
t))
:hints(("Goal" :in-theory (enable ubddp))))
(defthm eval-bdd-of-q-or
(equal (eval-bdd (q-or x y) values)
(or (eval-bdd x values)
(eval-bdd y values)))
:hints(("Goal"
:in-theory (enable eval-bdd)
:induct (q-binop-induct x y values)
:expand (q-or x y))))
(defthm canonicalize-q-or
(implies (and (force (ubddp x))
(force (ubddp y)))
(equal (q-or x y)
(q-ite x t y))))
(add-to-ruleset canonicalize-to-q-ite '(canonicalize-q-or))
(defthm q-or-of-nil-slow
(and (equal (q-or nil x) (if (consp x) x (if x t nil)))
(equal (q-or x nil) (if (consp x) x (if x t nil)))))
(defthm q-or-of-nil-aggressive
(implies (force (ubddp x))
(and (equal (q-or nil x) x)
(equal (q-or x nil) x))))
(defthm q-or-of-t
(and (equal (q-or t x) t)
(equal (q-or x t) t))
:hints(("Goal" :in-theory (disable canonicalize-q-or))))
(defthm q-or-symmetric
(equal (q-or x y)
(q-or y x))
:hints(("Goal" :in-theory (disable canonicalize-q-or
q-or-of-nil-aggressive
equal-by-eval-bdds))))
(memoize 'q-binary-or
:condition '(and (consp x) (consp y))
:commutative 'q-or-symmetric)
(defthm q-or-of-self-slow
(equal (q-or x x)
(if (consp x) x (if x t nil))))
(defthm q-or-of-self-aggressive
(implies (force (ubddp x))
(equal (q-or x x) x)))
(local (in-theory (disable q-or)))
(local (defun test-q-or (a b c x y z)
(declare (xargs :guard t))
(list (q-or x y)
(q-or x (q-not y))
(q-or (q-not x) y)
(q-or x y (q-not x) a b (q-ite y z c))))))
(defsection q-implies
:parents (ubdd-constructors)
:short "@(call q-implies) constructs a UBDD representing @('(implies x y)')."
(defund q-implies-fn (x y)
(declare (xargs :guard t))
(cond ((atom x)
(if x
;; [Jared hack]: changing this base case to get
;; always-boolean behavior on atoms. Previously this
;; was just "y".
(if (atom y)
(if y t nil)
y)
t))
((atom y)
(if y t (q-not x)))
((hons-equal x y)
t)
(t
(qcons (q-implies-fn (car x) (car y))
(q-implies-fn (cdr x) (cdr y))))))
(memoize 'q-implies-fn :condition '(and (consp x) (consp y)))
(add-bdd-fn q-implies-fn)
(defun q-implies-macro-fn (x y)
(declare (xargs :mode :program))
(cond ((and (or (quotep x) (atom x))
(or (quotep y) (atom y)))
;; X and Y both look cheap. Don't optimize anything.
`(q-implies-fn ,x ,y))
((or (quotep y) (atom y))
;; Y looks cheap. Try to avoid computing X.
`(mbe :logic (q-implies-fn ,x ,y)
:exec (let ((q-implies-y-do-not-use-elsewhere ,y))
(if (eq t q-implies-y-do-not-use-elsewhere)
t
(prog2$
(last-chance-wash-memory)
(q-implies-fn ,x q-implies-y-do-not-use-elsewhere))))))
(t
;; Try to avoid computing Y.
`(mbe :logic (q-implies-fn ,x ,y)
:exec (let ((q-implies-x-do-not-use-elsewhere ,x))
(if (not q-implies-x-do-not-use-elsewhere)
t
(prog2$
(last-chance-wash-memory)
(q-implies-fn q-implies-x-do-not-use-elsewhere
,y))))))))
(defmacro q-implies (x y)
(q-implies-macro-fn x y))
(add-macro-alias q-implies q-implies-fn)
(local (in-theory (enable q-implies)))
(defthm ubddp-of-q-implies
(implies (and (force (ubddp x))
(force (ubddp y)))
(equal (ubddp (q-implies x y))
t))
:hints(("Goal" :in-theory (e/d (ubddp)
(canonicalize-q-not)))))
(defthm eval-bdd-of-q-implies
(equal (eval-bdd (q-implies x y) values)
(implies (eval-bdd x values)
(eval-bdd y values)))
:hints(("Goal"
:in-theory (e/d (eval-bdd)
(canonicalize-q-not))
:induct (q-binop-induct x y values)
:expand (q-implies x y))))
(defthm canonicalize-q-implies
(implies (and (force (ubddp x))
(force (ubddp y)))
(equal (q-implies x y)
(q-ite x y t))))
(add-to-ruleset canonicalize-to-q-ite '(canonicalize-q-implies))
(defthm q-implies-of-nil
(and (equal (q-implies nil x) t)
(equal (q-implies x nil) (q-not x)))
:hints(("Goal" :in-theory (e/d (q-not)
(canonicalize-q-not
canonicalize-q-implies)))))
(defthm q-implies-of-t-left-slow
(equal (q-implies t x) (if (consp x) x (if x t nil)))
:hints(("Goal" :in-theory (disable canonicalize-q-implies))))
(defthm q-implies-of-t-left-aggressive
(implies (force (ubddp x))
(equal (q-implies t x) x))
:hints(("Goal" :in-theory (disable canonicalize-q-implies))))
(defthm q-implies-of-t-right
(equal (q-implies x t) t)
:hints(("Goal" :in-theory (disable canonicalize-q-implies))))
(defthm q-implies-of-self
(equal (q-implies x x) t)
:hints(("Goal" :in-theory (disable canonicalize-q-implies))))
(local (in-theory (disable q-implies)))
(local (defun test-q-implies (a b)
(declare (xargs :guard t
:guard-hints (("Goal" :in-theory (disable (force))))))
(list (q-implies a b)
(q-implies a (q-not b))
(q-implies (q-not b) a)
(q-implies (q-not a) (q-not b))))))
(defsection q-or-c2
:parents (ubdd-constructors)
:short "@(call q-or-c2) constructs a UBDD representing @('(or x (not y))')."
(defund q-or-c2-fn (x y)
(declare (xargs :guard t))
(cond ((atom y)
(if y x t))
((atom x)
(if x t (q-not y)))
((hons-equal x y)
t)
(t (qcons (q-or-c2-fn (car x) (car y))
(q-or-c2-fn (cdr x) (cdr y))))))
(add-bdd-fn q-or-c2-fn)
(defun q-or-c2-macro-fn (x y)
(declare (xargs :mode :program))
(cond ((and (or (quotep x) (atom x))
(or (quotep y) (atom y)))
;; Both look cheap. Don't try to avoid anything.
`(q-or-c2-fn ,x ,y))
((or (quotep y) (atom y))
;; Y looks cheap. Try to avoid computing X.
`(mbe :logic (q-or-c2-fn ,x ,y)
:exec (let ((q-or-c2-y-do-not-use-elsewhere ,y))
(if (not q-or-c2-y-do-not-use-elsewhere)
t
(prog2$
(last-chance-wash-memory)
(q-or-c2-fn ,x q-or-c2-y-do-not-use-elsewhere))))))
(t
;; Try to avoid computing Y.
`(mbe :logic (q-or-c2-fn ,x ,y)
:exec (let ((q-or-c2-x-do-not-use-elsewhere ,x))
(if (eq q-or-c2-x-do-not-use-elsewhere t)
t
(prog2$
(last-chance-wash-memory)
(q-or-c2-fn q-or-c2-x-do-not-use-elsewhere ,y))))))))
(defmacro q-or-c2 (x y)
(q-or-c2-macro-fn x y))
(add-macro-alias q-or-c2 q-or-c2-fn)
(local (in-theory (enable q-or-c2)))
(memoize 'q-or-c2 :condition '(and (consp x) (consp y)))
(defthm ubddp-of-q-or-c2
(implies (and (force (ubddp x))
(force (ubddp y)))
(equal (ubddp (q-or-c2 x y))
t))
:hints(("Goal" :in-theory (enable ubddp))))
(defthm eval-bdd-of-q-or-c2
(equal (eval-bdd (q-or-c2 x y) values)
(implies (eval-bdd y values)
(eval-bdd x values)))
:hints(("Goal"
:in-theory (e/d (eval-bdd)
(canonicalize-q-not))
:induct (q-binop-induct x y values)
:expand (q-or-c2 x y))))
(defthm canonicalize-q-or-c2
(implies (and (force (ubddp x))
(force (ubddp y)))
(equal (q-or-c2 x y)
(q-ite y x t))))
(add-to-ruleset canonicalize-to-q-ite '(canonicalize-q-or-c2))
(defthm q-or-c2-of-t
(and (equal (q-or-c2 t x) t)
(equal (q-or-c2 x t) x))
:hints(("Goal" :in-theory (disable canonicalize-q-or-c2))))
(defthm q-or-c2-of-nil
(and (equal (q-or-c2 nil x) (q-not x))
(equal (q-or-c2 x nil) t))
:hints(("Goal" :in-theory (e/d (q-not)
(canonicalize-q-not
canonicalize-q-or-c2)))))
(local (in-theory (disable q-or-c2)))
(local (defun test-q-or-c2 (a b)
(declare (xargs :guard t))
(list (q-or-c2 a b)
(q-or-c2 a (q-not b))
(q-or-c2 (q-not a) b)
(q-or-c2 (q-not a) (q-not b))))))
(defsection q-and-c1
:parents (ubdd-constructors)
:short "@(call q-and-c1) constructs a UBDD representing @('(and (not x) y)')."
(defund q-and-c1-fn (x y)
(declare (xargs :guard t))
(cond ((atom x)
(if x nil y))
((atom y)
(if y (q-not x) nil))
((hons-equal x y)
nil)
(t
(qcons (q-and-c1-fn (car x) (car y))
(q-and-c1-fn (cdr x) (cdr y))))))
(memoize 'q-and-c1-fn :condition '(and (consp x) (consp y)))
(add-bdd-fn q-and-c1-fn)
(defun q-and-c1-macro-fn (x y)
(declare (xargs :mode :program))
(cond ((and (or (quotep x) (atom x))
(or (quotep y) (atom y)))
;; Both look cheap. Don't try to avoid anything.
`(q-and-c1-fn ,x ,y))
((or (quotep y) (atom y))
;; Y looks cheap. Try to avoid computing X.
`(mbe :logic (q-and-c1-fn ,x ,y)
:exec (let ((q-and-c1-y-do-not-use-elsewhere ,y))
(if (not q-and-c1-y-do-not-use-elsewhere)
nil
(prog2$
(last-chance-wash-memory)
(q-and-c1-fn ,x q-and-c1-y-do-not-use-elsewhere))))))
(t
;; Try to avoid computing Y.
`(mbe :logic (q-and-c1-fn ,x ,y)
:exec (let ((q-and-c1-x-do-not-use-elsewhere ,x))
(if (eq t q-and-c1-x-do-not-use-elsewhere)
nil
(prog2$
(last-chance-wash-memory)
(q-and-c1-fn q-and-c1-x-do-not-use-elsewhere ,y))))))))
(defmacro q-and-c1 (x y)
(q-and-c1-macro-fn x y))
(add-macro-alias q-and-c1 q-and-c1-fn)
(local (in-theory (enable q-and-c1)))
(defthm ubddp-of-q-and-c1
(implies (and (force (ubddp x))
(force (ubddp y)))
(equal (ubddp (q-and-c1 x y))
t))
:hints(("Goal" :in-theory (enable ubddp))))
(defthm eval-bdd-of-q-and-c1
(equal (eval-bdd (q-and-c1 x y) values)
(if (eval-bdd x values)
nil
(eval-bdd y values)))
:hints(("Goal"
:in-theory (e/d (eval-bdd)
(canonicalize-q-not))
:induct (q-binop-induct x y values)
:expand (q-and-c1 x y))))
(defthm canonicalize-q-and-c1
(implies (and (force (ubddp x))
(force (ubddp y)))
(equal (q-and-c1 x y)
(q-ite x nil y))))
(add-to-ruleset canonicalize-to-q-ite '(canonicalize-q-and-c1))
(defthm q-and-c1-of-nil
(and (equal (q-and-c1 nil x) x)
(equal (q-and-c1 x nil) nil))
:hints(("Goal" :in-theory (disable canonicalize-q-and-c1))))
(defthm q-and-c1-of-t
(and (equal (q-and-c1 t x) nil)
(equal (q-and-c1 x t) (q-not x)))
:hints(("Goal" :in-theory (e/d (q-not)
(canonicalize-q-and-c1
canonicalize-q-not)))))
(local (in-theory (disable q-and-c1)))
(local (defun test-q-and-c1 (a b)
(declare (xargs :guard t))
(list (q-and-c1 a b)
(q-and-c1 a (q-not b))
(q-and-c1 (q-not a) b)
(q-and-c1 (q-not a) (q-not b))))))
(defsection q-and-c2
:parents (ubdd-constructors)
:short "@(call q-and-c2) constructs a UBDD representing @('(and x (not y))')."
(defund q-and-c2-fn (x y)
(declare (xargs :guard t))
(cond ((atom x)
(if x (q-not y) nil))
((atom y)
(if y nil x))
((hons-equal x y)
nil)
(t
(qcons (q-and-c2-fn (car x) (car y))
(q-and-c2-fn (cdr x) (cdr y))))))
(memoize 'q-and-c2-fn :condition '(and (consp x) (consp y)))
(add-bdd-fn q-and-c2-fn)
(defun q-and-c2-macro-fn (x y)
(declare (xargs :mode :program))
(cond ((and (or (quotep x) (atom x))
(or (quotep y) (atom y)))
;; Both look cheap. Don't try to avoid anything.
`(q-and-c2-fn ,x ,y))
((or (quotep y) (atom y))
;; Y looks cheap. Try to avoid computing X.
`(mbe :logic (q-and-c2-fn ,x ,y)
:exec (let ((q-and-c2-y-do-not-use-elsewhere ,y))
(if (eq t q-and-c2-y-do-not-use-elsewhere)
nil
(prog2$
(last-chance-wash-memory)
(q-and-c2-fn ,x q-and-c2-y-do-not-use-elsewhere))))))
(t
;; Try to avoid computing Y.
`(mbe :logic (q-and-c2-fn ,x ,y)
:exec (let ((q-and-c2-x-do-not-use-elsewhere ,x))
(if (not q-and-c2-x-do-not-use-elsewhere)
nil
(prog2$
(last-chance-wash-memory)
(q-and-c2-fn q-and-c2-x-do-not-use-elsewhere ,y))))))))
(defmacro q-and-c2 (x y)
(q-and-c2-macro-fn x y))
(add-macro-alias q-and-c2 q-and-c2-fn)
(local (in-theory (enable q-and-c2)))
(defthm ubddp-of-q-and-c2
(implies (and (force (ubddp x))
(force (ubddp y)))
(equal (ubddp (q-and-c2 x y))
t))
:hints(("Goal" :in-theory (enable ubddp))))
(defthm eval-bdd-of-q-and-c2
(equal (eval-bdd (q-and-c2 x y) values)
(if (eval-bdd y values)
nil
(eval-bdd x values)))
:hints(("Goal"
:in-theory (e/d (eval-bdd)
(canonicalize-q-not))
:induct (q-binop-induct x y values)
:expand (q-and-c2 x y))))
(defthm canonicalize-q-and-c2
(implies (and (force (ubddp x))
(force (ubddp y)))
(equal (q-and-c2 x y)
(q-ite y nil x))))
(add-to-ruleset canonicalize-to-q-ite '(canonicalize-q-and-c2))
(defthm q-and-c2-of-nil-left
(equal (q-and-c2 nil x) nil)
:hints(("Goal" :in-theory (disable (force)))))
(defthm q-and-c2-of-nil-right-slow
(equal (q-and-c2 x nil) (if (consp x) x (if x t nil))))
(defthm q-and-c2-of-nil-right-aggressive
(implies (force (ubddp x))
(equal (q-and-c2 x nil) x)))
(defthm q-and-c2-of-t
(and (equal (q-and-c2 t x) (q-not x))
(equal (q-and-c2 x t) nil))
:hints(("Goal" :in-theory (disable (force)))))
(local (in-theory (disable q-and-c2)))
(local (defun test-q-and-c2 (a b)
(declare (xargs :guard t
:guard-hints (("Goal" :in-theory (disable (force))))))
(list (q-and-c2 a b)
(q-and-c2 a (q-not b))
(q-and-c2 (q-not a) b)
(q-and-c2 (q-not a) (q-not b))))))
(defsection q-iff
:parents (ubdd-constructors)
:short "@(call q-iff) constructs a UBDD representing an equality/iff-equivalence."
:long "<p>In the logic, for instance,</p>
@({
(q-iff x1 x2 x3 x4) = (q-and (q-binary-iff x1 x2)
(q-binary-iff x1 x3)
(q-binary-iff x1 x4))
})
<p>We do not see how to use short-circuiting to improve upon @('(q-binary-iff x
y)'), since the answer depends upon both inputs in all cases. But when we
implement the multiple-input @('q-iff') macro, we can at least take advantage
of the short-circuiting provided by @(see q-and). We also reorder the inputs
so that cheap ones come first, hoping that we can avoid evaluating expensive
arguments.</p>"
(defund q-binary-iff (x y)
(declare (xargs :guard t))
(cond ((atom x)
(if x y (q-not y)))
((atom y)
(if y x (q-not x)))
((hons-equal x y)
t)
(t
(qcons (q-binary-iff (car x) (car y))
(q-binary-iff (cdr x) (cdr y))))))
(memoize 'q-binary-iff :condition '(and (consp x) (consp y)))
(add-bdd-fn q-binary-iff)
(defun q-iff-macro-guts (a x)
;; Produces (LIST (Q-BINARY-IFF A X1) (Q-BINARY-IFF A X2) ...)
(declare (xargs :mode :program))
(if (consp x)
(cons `(q-binary-iff ,a ,(car x))
(q-iff-macro-guts a (cdr x)))
nil))
(defun q-iff-macro-fn (args)
(declare (xargs :mode :program))
(if (equal (len args) 2)
`(prog2$
(last-chance-wash-memory)
(q-binary-iff ,(car args) ,(cadr args)))
(mv-let (cheap expensive)
(cheap-and-expensive-arguments args)
(let ((reordered-args (append cheap expensive)))
`(let ((a-for-q-iff-do-not-use-elsewhere ,(car reordered-args)))
(prog2$
(last-chance-wash-memory)
(Q-AND . ,(q-iff-macro-guts 'a-for-q-iff-do-not-use-elsewhere
(cdr reordered-args)))))))))
(defmacro q-iff (a b &rest args)
(declare (xargs :guard (true-listp args)))
(q-iff-macro-fn (list* a b args)))
(add-macro-alias q-iff q-binary-iff)
(local (in-theory (enable q-iff)))
(defthm ubddp-of-q-iff
(implies (and (force (ubddp x))
(force (ubddp y)))
(equal (ubddp (q-iff x y))
t))
:hints(("Goal" :in-theory (enable ubddp))))
(defthm eval-bdd-of-q-iff
(equal (eval-bdd (q-iff x y) values)
(iff (eval-bdd x values)
(eval-bdd y values)))
:hints(("Goal"
:in-theory (e/d (eval-bdd)
(canonicalize-q-not))
:induct (q-binop-induct x y values)
:expand (q-iff x y))))
(defthm canonicalize-q-iff
(implies (and (force (ubddp x))
(force (ubddp y)))
(equal (q-iff x y)
(q-ite x y (q-ite y nil t)))))
(add-to-ruleset canonicalize-to-q-ite '(canonicalize-q-iff))
(local (in-theory (disable q-iff)))
(local (defun test-q-iff (a b c d)
(declare (xargs :guard t
:guard-hints (("Goal" :in-theory (disable (force))))))
(list (q-iff a b)
(q-iff a b c (q-not b))
(q-iff (q-not a) (q-not b) c d)))))
(defsection q-nand
:parents (ubdd-constructors)
:short "@(call q-nand) constructs a UBDD representing the NAND of its arguments."
:long "<p>For instance:</p>
@({
(q-nand) = nil
(q-nand a) = (q-not a)
(q-nand a b ...) = (q-not (q-and a b ...))
})
@(def q-nand)"
(defmacro q-nand (&rest args)
`(q-not (q-and . ,args))))
(defsection q-nor
:parents (ubdd-constructors)
:short "@(call q-nor) constructs a UBDD representing the NOR of its arguments."
:long "<p>For instance:</p>
@({
(q-nor) = t
(q-nor a) = (q-not a)
(q-nor a b ...) = (q-not (q-or a b ...))
})
@(def q-nor)"
(defmacro q-nor (&rest args)
`(q-not (q-or . ,args))))
(defsection q-xor
:parents (ubdd-constructors)
:short "@(call q-xor) constructs a UBDD representing the exclusive OR of its
arguments."
(defund q-binary-xor (x y)
(declare (xargs :guard t))
(cond ((atom x)
(if x (q-not y) y))
((atom y)
(if y (q-not x) x))
((hons-equal x y)
nil)
(t
(qcons (q-binary-xor (car x) (car y))
(q-binary-xor (cdr x) (cdr y))))))
(memoize 'q-binary-xor :condition '(and (consp x) (consp y)))
(add-bdd-fn q-binary-xor)
;; It needs all the bits, so I don't see how to make a lazy version.
(defun q-xor-macro-fn (args)
(declare (xargs :guard (consp args)))
(if (atom (cdr args))
(car args)
`(prog2$
(last-chance-wash-memory)
(q-binary-xor ,(car args)
,(q-xor-macro-fn (cdr args))))))
(defmacro q-xor (a b &rest args)
(q-xor-macro-fn (cons a (cons b args))))
(add-macro-alias q-xor q-binary-xor)
(add-binop q-xor q-binary-xor)
(local (in-theory (enable q-xor)))
(defthm ubddp-of-q-xor
(implies (and (force (ubddp x))
(force (ubddp y)))
(equal (ubddp (q-xor x y))
t))
:hints(("Goal" :in-theory (enable ubddp))))
(defthm eval-bdd-of-q-xor
(equal (eval-bdd (q-xor x y) values)
(if (eval-bdd x values)
(not (eval-bdd y values))
(eval-bdd y values)))
:hints(("Goal"
:in-theory (e/d (eval-bdd)
(canonicalize-q-not))
:induct (q-binop-induct x y values)
:expand (q-xor x y))))
(defthm q-xor-of-self
(equal (q-xor x x)
nil)
:hints(("Goal" :in-theory (e/d (q-not)
(equal-by-eval-bdds
canonicalize-q-not)))))
(defthm canonicalize-q-xor
(implies (and (force (ubddp x))
(force (ubddp y)))
(equal (q-xor x y)
(q-ite x (q-not y) y))))
(add-to-ruleset canonicalize-to-q-ite '(canonicalize-q-xor)))
(defsection qv
:parents (ubdd-constructors)
:short "@(call qv) constructs a UBDD which evaluates to true exactly when the
@('i')th BDD variable is true."
(defund qv (i)
(declare (xargs :guard t))
(if (posp i)
(let ((prev (qv (1- i))))
(hons prev prev))
(hons t nil)))
(memoize 'qv)
(add-bdd-fn qv)
(local (in-theory (enable qv)))
(defthm ubddp-qv
(ubddp (qv i))
:hints(("goal" :in-theory (enable ubddp))))
(defthm eval-bdd-of-qv-and-nil
(not (eval-bdd (qv i) nil))
:hints(("Goal" :in-theory (enable eval-bdd))))
(defthm eval-bdd-of-qv
(equal (eval-bdd (qv i) lst)
(if (nth i lst) t nil))
:hints(("Goal" :in-theory (enable eval-bdd))))
(local (defun qvs-ind (i j)
(if (or (zp i) (zp j))
i
(qvs-ind (1- i) (1- j)))))
(defthm qvs-equal
(equal (equal (qv i) (qv j))
(equal (nfix i) (nfix j)))
:hints(("Goal" :induct (qvs-ind i j)))))
(defsection eval-bdd-list
:parents (ubdds)
:short "Apply @(see eval-bdd) to a list of UBDDs."
:long "<p>The resulting list is @(see hons)ed together.</p>
<p>BOZO why do we hons the list, I suspect it makes no sense to do so...</p>"
(defund eval-bdd-list (x values)
(declare (xargs :guard t))
(if (consp x)
(hons (eval-bdd (car x) values)
(eval-bdd-list (cdr x) values))
nil))
(local (in-theory (enable eval-bdd-list)))
(defthm eval-bdd-list-when-not-consp
(implies (not (consp x))
(equal (eval-bdd-list x values)
nil)))
(defthm eval-bdd-list-of-cons
(equal (eval-bdd-list (cons a x) values)
(cons (eval-bdd a values)
(eval-bdd-list x values))))
(defthmd consp-eval-bdd-list
(equal (consp (eval-bdd-list x env))
(consp x))))
(defsection q-compose
:parents (ubdd-constructors)
:short "@(call q-compose) composes the UBDD @('x') with the list of UBDDs
@('l')."
:long "<p>BOZO document what this is doing. Is it like sexpr compose?</p>"
(defund q-compose (x l)
(declare (xargs :guard t))
(cond ((atom x)
x)
((atom l)
(q-compose (cdr x) nil))
((hons-equal (car x) (cdr x))
(q-compose (car x) (cdr l)))
(t
(q-ite (car l)
(q-compose (car x) (cdr l))
(q-compose (cdr x) (cdr l))))))
(add-bdd-fn q-compose)
(local (in-theory (enable q-compose)))
(defthm ubddp-of-q-compose
(implies (and (force (ubddp x))
(force (ubdd-listp l)))
(equal (ubddp (q-compose x l))
t))
:hints(("Goal" :in-theory (enable ubddp)))))
(defsection q-compose-list
:parents (ubdd-constructors)
:short "@(call q-compose-list) composes each UBDD in @('xs') with the list of
UBDDs @('l'), i.e., it maps @(see q-compose) across @('xs')."
(defund q-compose-list (xs l)
(declare (xargs :guard t))
(if (atom xs)
nil
(cons (q-compose (car xs) l)
(q-compose-list (cdr xs) l))))
(local (in-theory (enable q-compose-list)))
(defthm q-compose-list-when-not-consp
(implies (not (consp xs))
(equal (q-compose-list xs l)
nil)))
(defthm q-compose-list-of-cons
(equal (q-compose-list (cons x xs) l)
(cons (q-compose x l)
(q-compose-list xs l))))
(defthm ubdd-listp-of-q-compose-list
(implies (and (force (ubdd-listp xs))
(force (ubdd-listp l)))
(equal (ubdd-listp (q-compose-list xs l))
t))))
(defsection q-and-is-nil
:parents (ubdd-constructors)
:short "@(call q-and-is-nil) determines whether @('(q-and x y)') is
always false."
:long "<p>You could ask the same question in other ways, say for instance</p>
@({
(not (q-and x y))
})
<p>But @('q-and-is-nil') is especially efficient and doesn't need to construct
an intermediate UBDD.</p>"
(defund q-and-is-nil (x y)
(declare (xargs :guard t))
(cond ((eq x t) (eq y nil))
((atom x) t)
((eq y t) nil)
((atom y) t)
(t (and (q-and-is-nil (qcar x) (qcar y))
(q-and-is-nil (qcdr x) (qcdr y))))))
(memoize 'q-and-is-nil :condition '(and (consp x) (consp y)))
(local (in-theory (enable q-and-is-nil)))
(defthm q-and-is-nil-removal
(implies (and (force (ubddp x))
(force (ubddp y)))
(equal (q-and-is-nil x y)
(not (q-and x y))))
:hints(("Goal"
:induct (q-and-is-nil x y)
:in-theory (e/d (q-and ubddp)
(canonicalize-q-and
equal-by-eval-bdds))))))
(defsection q-and-is-nilc2
:parents (ubdd-constructors)
:short "@(call q-and-is-nilc2) determines whether @('(q-and x (q-not y))') is
always false."
:long "<p>This is like @(see q-and-is-nil).</p>"
(defund q-and-is-nilc2 (x y)
(declare (xargs :guard t))
(cond ((eq x t) (eq y t))
((atom x) t)
((eq y t) t)
((atom y) nil)
(t (and (q-and-is-nilc2 (qcar x) (qcar y))
(q-and-is-nilc2 (qcdr x) (qcdr y))))))
(local (in-theory (enable q-and-is-nilc2)))
(defthm q-and-is-nilc2-removal
(implies (and (force (ubddp x))
(force (ubddp y)))
(equal (q-and-is-nilc2 x y)
(not (q-and x (q-not y)))))
:hints(("Goal"
:induct (q-and-is-nilc2 x y)
:in-theory (e/d (q-and-c2 q-and-is-nilc2 ubddp)
(canonicalize-q-and-c2 equal-by-eval-bdds
; Matt K. mod, 6/2020: Disable the following rule, which causes the proof to
; fail now that ACL2 is loosening its "being-openedp" heuristic restriction.
canonicalize-q-not))))))
(defund q-is-atomic (x)
(declare (xargs :guard t))
(if (atom x)
(if x t nil)
'not-atomic))
(defund q-not-is-atomic (x)
(declare (xargs :guard t))
;; returns T, NIL, or NOT-ATOMIC
(if (atom x)
(not x)
'not-atomic))
(defthm q-not-is-atomic-removal
(implies (ubddp x)
(equal (q-not-is-atomic x)
(q-is-atomic (q-not x))))
:hints(("Goal"
:in-theory (enable q-not-is-atomic
q-is-atomic
q-ite q-not ubddp))))
(defabbrev atom-fix (y)
(if (atom y) y 'not-atomic))
(defund q-ite-is-atomic (x y z)
;; returns T, NIL, or NOT-ATOMIC
(declare (xargs :guard t))
(cond ((null x) (atom-fix z))
((atom x) (atom-fix y))
(t (let ((y (if (hons-equal x y) t y))
(z (if (hons-equal x z) nil z)))
(cond ((hons-equal y z) (atom-fix y))
((and (eq y t) (eq z nil)) (atom-fix x))
((and (eq y nil) (eq z t)) (q-not-is-atomic x))
(t (let ((a (q-ite-is-atomic (car x) (qcar y) (qcar z)))
(d (q-ite-is-atomic (cdr x) (qcdr y) (qcdr z))))
(cond ((or (eq a 'not-atomic)
(eq d 'not-atomic))
'not-atomic)
((equal a d) a)
(t 'not-atomic)))))))))
(encapsulate
()
(local (defthm lemma
(implies (ubddp x)
(equal (consp x)
(if (equal x t)
nil
(if (equal x nil)
nil
t))))
:hints(("Goal" :in-theory (enable ubddp)))))
(local
(defthm consp-q-ite
(implies (and (ubddp x) (ubddp y) (ubddp z))
(equal (consp (q-ite x y z))
(and (not (equal (q-ite x y z) t))
(not (equal (q-ite x y z) nil)))))))
(local (defthm ubddp-consp-rws
(implies (and (ubddp x) (consp x))
(and (ubddp (car x))
(ubddp (cdr x))))
:hints(("Goal" :in-theory (enable ubddp)))))
(with-output
:off (prove warning) :gag-mode nil
(defthm q-ite-is-atomic-removal
(implies (and (ubddp x)
(ubddp y)
(ubddp z))
(equal (q-ite-is-atomic x y z)
(q-is-atomic (q-ite x y z))))
:hints(("Goal"
:induct (q-ite-is-atomic x y z)
:in-theory (e/d (q-is-atomic
; ubddp
(:induction q-ite-is-atomic))
(canonicalize-q-not
;; q-ite ubddp
equal-by-eval-bdds
equal-of-booleans-rewrite
|(q-ite non-nil y z)|
default-car
default-cdr
(:type-prescription booleanp)
(:type-prescription ubddp)
(:type-prescription q-ite-is-atomic)
(:type-prescription q-ite-fn)
(:type-prescription q-is-atomic)
q-not not lemma))
:expand ((:free (y z) (q-ite x y z))
(:free (y z) (q-ite t y z))
(:free (y z) (q-ite nil y z))
(:free (y z) (q-ite-is-atomic x y z))
(:free (y z) (q-ite-is-atomic t y z))
(:free (y z) (q-ite-is-atomic nil y z)))
:do-not-induct t
:do-not '(generalize fertilize eliminate-destructors))
))))
(defun q-buf (x)
;; !!! BOZO this is stupid. We should get rid of it.
(declare (xargs :guard t))
x)
;; JARED HACK. I think we should turn off canonicalize-q-fns by
;; default given the new guard stuff.
(in-theory (disable* (:ruleset canonicalize-to-q-ite)))
;; !!! COMPATIBILITY WRAPPER. I'd like to get rid of this stuff.
; We define additional BDD-operations that can sometime provide better
; efficiency than only using Q-ITE through the use of specific function
; memoization and other tricks.
;; COMPATIBILITY WRAPPER --- Trying to get rid of ITE functions entirely.
(defmacro q-and-ite (x y)
`(q-and ,x ,y))
(defmacro q-or-ite (x y)
`(q-or ,x ,y))
(defmacro q-not-ite (x)
`(q-not ,x))
(defmacro q-implies-ite (x y)
`(q-implies ,x ,y))
(defmacro q-or-c2-ite (x y)
`(q-or-c2 ,x ,y))
(defmacro q-and-c1-ite (x y)
`(q-and-c1 ,x ,y))
(defmacro q-and-c2-ite (x y)
`(q-and-c2 ,x ,y))
(defmacro q-iff-ite (x y)
`(q-iff ,x ,y))
(defmacro q-xor-ite (x y)
`(q-xor ,x ,y))
; Finds a satisfying assignment for a BDD.
(defsection q-sat
:parents (ubdds)
:short "@(call q-sat) finds a satisfying assignment for the UBDD @('x'), if
one exists."
:long "<p>Assuming @('x') is a well-formed UBDD, the only time there isn't
a satisfying assignment is when @('x') represents the constant false function,
i.e., when @('x') is @('nil'). In any other case it's easy to construct some
list of values for which @(see eval-bdd) returns true.</p>
<p>BOZO naming. This shouldn't start with @('q-') since it's constructing a
list of values instead of a UBDD.</p>"
(defund q-sat (x)
(declare (xargs :guard t))
(if (atom x)
nil
(if (eq (cdr x) nil)
(cons t (q-sat (car x)))
(cons nil (q-sat (cdr x))))))
(local (in-theory (enable q-sat)))
(defthm q-sat-correct
(implies (and (ubddp x) x)
(eval-bdd x (q-sat x)))
:hints(("Goal" :in-theory (enable ubddp
eval-bdd)))))
(defsection bdd-sat-dfs
:parents (ubdds)
:short "@(call bdd-sat-dfs) finds a satisfying assignment for the UBDD @('x'), if
one exists. It works even if @('x') is not a well-formed UBDD, but it might be
slow in that case."
(defund bdd-sat-dfs (x)
(declare (xargs :guard t))
(if (atom x)
nil
(let* ((a (car x))
(d (cdr x)))
(cond ((and (atom a) a) '(t))
((and (atom d) d) '(nil))
(t (let ((rec1 (bdd-sat-dfs a)))
(if rec1 (cons t rec1)
(let ((rec2 (bdd-sat-dfs d)))
(and rec2 (cons nil rec2))))))))))
(local (in-theory (enable bdd-sat-dfs)))
(defthmd bdd-sat-dfs-produces-satisfying-assign
(implies (bdd-sat-dfs x)
(eval-bdd x (bdd-sat-dfs x)))
:hints(("Goal" :in-theory (enable eval-bdd))))
(local (in-theory (enable bdd-sat-dfs-produces-satisfying-assign)))
(defthmd bdd-sat-dfs-not-satisfying-when-nil
(implies (and (consp x)
(not (bdd-sat-dfs x)))
(not (eval-bdd x vars)))
:hints(("Goal" :in-theory (enable eval-bdd))))
(local (in-theory (enable bdd-sat-dfs-not-satisfying-when-nil)))
(defthm bdd-sat-dfs-correct
(implies (eval-bdd x vars)
(eval-bdd x (bdd-sat-dfs x)))
:hints(("Goal" :in-theory (enable eval-bdd)))))
(defsection q-sat-any
:parents (ubdds)
:short "@(call q-sat-any) finds an assignment that satisfies at least one
UBDD in the list of UBDDs @('x')."
:long "<p>BOZO naming. This shouldn't start with @('q-') since it's
constructing a list of values instead of a UBDD.</p>"
(defund q-sat-any (a)
(declare (xargs :guard t))
(if (atom a)
nil
(if (eq (car a) nil)
(q-sat-any (cdr a))
(q-sat (car a))))))
(defsection ubdd-fix
:parents (ubdds)
:short "@(call ubdd-fix) constructs a valid @(see ubddp) that is treated
identically to @('x') under @(see eval-bdd)."
:long "<p>This fixes up the tips of @('x') to be Booleans and handles any
collapsing of @('(t . t)') and @('(nil . nil)') nodes. It is occasionally
useful in theorems to avoid @(see ubddp) hypotheses.</p>"
(defund ubdd-fix (x)
(declare (xargs :guard t))
(if (atom x)
(if x t nil)
(if (and (atom (car x)) (atom (cdr x))
(iff (car x) (cdr x)))
(if (car x) t nil)
(qcons (ubdd-fix (car x))
(ubdd-fix (cdr x))))))
(local (in-theory (enable ubdd-fix)))
(memoize 'ubdd-fix :condition '(consp x))
(defthm ubddp-ubdd-fix
(ubddp (ubdd-fix x))
:hints(("Goal" :in-theory (enable ubddp))))
(defthm eval-bdd-ubdd-fix
(equal (eval-bdd (ubdd-fix x) env)
(eval-bdd x env))
:hints(("Goal" :in-theory (enable eval-bdd))))
(defthm ubdd-fix-x-is-x
(implies (ubddp x)
(equal (ubdd-fix x) x))
:hints(("Goal" :in-theory (enable ubddp)))))
|