/usr/share/acl2-6.3/books/add-ons/hash-stobjs.lisp is in acl2-books-source 6.3-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 | (in-package "ACL2")
;; Support for stobjs with hash table members.
;; To extend the example used in defstobj:
#|
(defstobj $st
(flag :type t :initially run)
(pctr :type (integer 0 255) :initially 128)
(mem :type (array (integer 0 255) (256)) :initially 0)
(tab :type (hash-table eql)))
(defstobj equalht
(equaltab :type (hash-table equal)))
(defstobj hons-equalht
(hons-equaltab :type (hash-table hons-equal)))
|#
;; Since array members are represented by lists, we'll represent hash
;; table members as alists, as illustrated below.
;; Is this sound? See the theorems proven below about the
;; interactions of the logical definitions of the access and update
;; functions. I argue that these theorems are exactly the contract of
;; a hash table (provided that the inputs are well-formed,
;; i.e. EQLABLE for an EQL table, etc). If this is the case, then
;; this is only unsound in the event that the underlying Lisp has a
;; bug in its hash table implementation.
;; We make guards on these functions as weak as possible since they
;; have nothing to do with the performance in raw Lisp, and arguably
;; we care more about ease of proving guard conjectures than we do
;; about how well they perform in the logic.
(defun hons-remove-assoc (x al)
(declare (xargs :guard t))
(if (atom al)
nil
(if (and (consp (car al))
(equal x (caar al)))
(hons-remove-assoc x (cdr al))
(cons (car al) (hons-remove-assoc x (cdr al))))))
(defthm hons-remove-assoc-acl2-count-weak
(<= (acl2-count (hons-remove-assoc x al)) (acl2-count al))
:rule-classes :linear)
(defun count-keys (al)
(declare (xargs :guard t))
(if (atom al)
0
(if (consp (car al))
(+ 1 (count-keys (hons-remove-assoc (caar al) (cdr al))))
(count-keys (cdr al)))))
(defthm not-assoc-hons-remove-assoc
(not (hons-assoc-equal k (hons-remove-assoc k al))))
(defthm assoc-hons-remove-assoc-diff
(implies (not (equal j k))
(equal (hons-assoc-equal k (hons-remove-assoc j al))
(hons-assoc-equal k al))))
(defthm hons-remove-assoc-repeat
(equal (hons-remove-assoc k (hons-remove-assoc k al))
(hons-remove-assoc k al)))
(defthm hons-remove-assoc-commutes
(equal (hons-remove-assoc j (hons-remove-assoc k al))
(hons-remove-assoc k (hons-remove-assoc j al))))
(local (include-book "arithmetic/top-with-meta" :dir :system))
(defthm count-keys-hons-remove-assoc
(equal (count-keys (hons-remove-assoc k al))
(if (consp (hons-assoc-equal k al))
(1- (count-keys al))
(count-keys al))))
(defthm count-keys-cons
(equal (count-keys (cons (cons k v) al))
(if (consp (hons-assoc-equal k al))
(count-keys al)
(+ 1 (count-keys al)))))
#||
;; Using this example stobj definition, we'll illustrate the logical
;; definitions of the functions used to access and update the table.
(defstobj htable
(tab :type (hash-table eql))) ;; or (hash-table equal)
(defun tabp
(declare (xargs :guard t))
;; Because we made the guards on hons-assoc-equal and hons-remove-assoc T, we
;; don't need to constrain what tabp is logically.
t)
(defun htablep (x)
(declare (xargs :guard t))
(true-listp x))
;; CREATE-HTABLE:
(defun create-htable ()
(declare (xargs :guard t))
(list nil))
;; GET, logic:
(defun tab-get (k htable)
(declare (xargs :guard (and (htablep htable)
;; eqlablep only in EQL version
(eqlablep k))))
(cdr (hons-assoc-equal k (nth 0 htable))))
;; BOUNDP, logic:
(defun tab-boundp (k htable)
(declare (xargs :guard (and (htablep htable)
;; eqlablep only in EQL version
(eqlablep k))))
(consp (hons-assoc-equal k (nth 0 htable))))
;; GET?, logic:
(defun tab-get? (k htable)
(declare (xargs :guard (and (htablep htable)
;; eqlablep only in EQL version
(eqlablep k))))
(mv (tab-get k htable)
(tab-boundp k htable)))
;; PUT, logic:
(defun tab-put (k v htable)
(declare (xargs :guard (and (htablep htable)
;; eqlablep only in EQL version
(eqlablep k))))
(update-nth 0 (cons (cons k v)
(nth 0 htable)) htable))
;; REM, logic:
(defun tab-rem (k htable)
(declare (xargs :guard (and (htablep htable)
;; eqlablep only in EQL version
(eqlablep k))))
(update-nth 0 (hons-remove-assoc k (nth 0 htable)) htable))
;; COUNT, logic:
(defun tab-count (htable)
(count-keys (nth 0 htable)))
;; CLEAR, logic:
(defun tab-clear (htable)
(declare (xargs :guard (htablep htable)))
(update-nth 0 nil htable))
(defun tab-init (size rehash-size rehash-threshold htable)
(declare (xargs :guard (and (htablep htable)
(or (natp size)
(not size))
(or (and (rationalp rehash-size)
(< 1 rehash-size))
(not rehash-size))
(or (and (rationalp rehash-threshold)
(<= 0 rehash-threshold)
(<= rehash-threshold 1))
(not rehash-threshold)))))
(declare (ignore size rehash-size rehash-threshold))
(update-nth 0 nil htable))
;; Theorems about the interactions of the functions above: Our
;; approach is sound if these theorems completely and accurately model
;; the functionality of a Common Lisp hash table, modulo assumptions
;; about what keys are allowed. We can argue that these are complete
;; since we can completely specify the values of any of the accessors
;; (tab-get, tab-boundp, tab-count) on any nesting of the updaters
;; (tab-put, tab-rem), by induction:
;; Base case 1: empty table; tab-get and tab-boundp both return nil.
;; Base case 2: (tab-put k v htable), where k is the key being
;; searched for: tab-get returns v, tab-boundp returns t.
;; Base case 3: (tab-rem k htable), where k is the key being searched
;; for: tab-get and tab-boundp again both return nil.
;; Base case 4: (tab-clear htable): both return nil.
;; Induction case 1: (tab-put j v htable), j not equal k, reduces to
;; access of htable,
;; Induction case 2: (tab-rem j htable), j not equal k, reduces to
;; access of htable.
(defthm tab-init-is-tab-clear
(equal (tab-init size rehash-size rehash-threshold htable)
(tab-clear htable)))
(defthm tab-get-tab-boundp
(implies (tab-get k htable)
(tab-boundp k htable)))
(defthm tab-boundp-start
(not (tab-boundp k (create-htable))))
(defthm tab-boundp-clear
(not (tab-boundp k (tab-clear htable))))
(defthm tab-boundp-tab-put-same
(tab-boundp k (tab-put k v htable)))
(defthm tab-boundp-tab-put-diff
(implies (not (equal j k))
(equal (tab-boundp k (tab-put j v htable))
(tab-boundp k htable))))
(defthm tab-get-tab-put-same
(equal (tab-get k (tab-put k v htable))
v))
(defthm tab-get-tab-put-diff
(implies (not (equal j k))
(equal (tab-get k (tab-put j v htable))
(tab-get k htable))))
(defthm tab-rem-tab-boundp-same
(not (tab-boundp k (tab-rem k htable))))
(defthm tab-rem-tab-boundp-diff
(implies (not (equal j k))
(equal (tab-boundp k (tab-rem j htable))
(tab-boundp k htable))))
(defthm tab-rem-tab-get-diff
(implies (not (equal j k))
(equal (tab-get k (tab-rem j htable))
(tab-get k htable))))
(defthm tab-count-start
(equal (tab-count (create-htable)) 0))
(defthm tab-count-put
(equal (tab-count (tab-put k v htable))
(if (tab-boundp k htable)
(tab-count htable)
(+ 1 (tab-count htable)))))
(defthm tab-count-rem
(equal (tab-count (tab-rem k htable))
(if (tab-boundp k htable)
(- (tab-count htable) 1)
(tab-count htable))))
(defthm tab-count-clear
(equal (tab-count (tab-clear htable)) 0))
;; CREATE-HTABLE, raw:
(defun create-htable ()
(vector (make-hash-table :test 'eql)))
;; GET, raw:
(defun tab-get (k htable)
;; Replace K with (HONS-COPY K) in HONS-EQUAL version
(values (gethash k
(svref htable 0))))
;; BOUNDP, raw:
(defun tab-boundp (k htable)
(multiple-value-bind (ans boundp)
;; Replace K with (HONS-COPY K) in HONS-EQUAL version
(gethash k (svref htable 0))
(declare (ignore ans))
boundp))
;; GET?, raw:
(defun tab-get? (k htable)
;; Replace K with (HONS-COPY K) in HONS-EQUAL version
(gethash k (svref htable 0)))
;; PUT, raw:
(defun tab-put (k v htable)
;; Replace K with (HONS-COPY K) in HONS-EQUAL version
(setf (gethash k (svref htable 0)) v)
htable)
;; REM, raw:
(defun tab-rem (k htable)
;; replace K with (HONS-COPY K) in HONS-EQUAL version
(remhash k (svref htable 0))
htable)
;; COUNT, raw:
(defun tab-count (htable)
(hash-table-count (svref htable 0)))
(defun tab-clear (htable)
(clrhash (svref htable 0))
htable)
(defun tab-init (size rehash-size rehash-threshold htable)
(setf (svref htable 0)
(make-hash-table
:size (or size 60)
:rehash-size (if rehash-size
(float rehash-size)
(float 17/10))
:rehash-threshold (if rehash-threshold
(float rehash-threshold)
(float 3/4))))
htable)
||#
(defttag hash-stobjs)
(program)
(set-state-ok t)
(redef+)
(defun defstobj-fnname (root key1 key2 renaming-alist)
; Warning: Keep this in sync with stobj-updater-guess-from-accessor.
; This has been moved from other-events.lisp, where other stobj-related
; functions are defined, because it is used in parse-with-local-stobj, which is
; used in translate11.
; This function generates the actual name we will use for a function generated
; by defstobj. Root and renaming-alist are, respectively, a symbol and an
; alist. Key1 describes which function name we are to generate and is one of
; :length, :resize, :recognizer, :accessor, :updater, or :creator. Key2
; describes the ``type'' of root. It is :top if root is the name of the live
; object (and hence, root starts with a $) and it is otherwise either :array or
; :non-array. Note that if renaming-alist is nil, then this function returns
; the ``default'' name used. If renaming-alist pairs some default name with an
; illegal name, the result is, of course, an illegal name.
(let* ((default-fnname
(case key1
(:recognizer
(case key2
(:top
(packn-pos
(list (coerce (append (coerce (symbol-name root) 'list)
'(#\P))
'string))
root))
(otherwise (packn-pos (list root "P") root))))
; This function can legitimately return nil for key1 values of :length
; and :resize. We are careful in the assoc-eq call below not to look
; for nil on the renaming-alist. That check is probably not
; necessary, but we include it for robustness.
(:length
(and (eq key2 :array)
(packn-pos (list root "-LENGTH") root)))
(:resize
(and (eq key2 :array)
(packn-pos (list "RESIZE-" root) root)))
(:accessor
(case key2
(:array (packn-pos (list root "I") root))
;---<
(:hash-table (packn-pos (list root "-GET") root))
; >---
(otherwise root)))
(:updater
(case key2
(:array (packn-pos (list "UPDATE-" root "I") root))
;---<
(:hash-table (packn-pos (list root "-PUT") root))
; >---
(otherwise (packn-pos (list "UPDATE-" root) root))))
(:creator
(packn-pos (list "CREATE-" root) root))
;---<
(:boundp
(and (eq key2 :hash-table)
(packn-pos (list root "-BOUNDP") root)))
(:accessor?
(and (eq key2 :hash-table)
(packn-pos (list root "-GET?") root)))
(:remove
(and (eq key2 :hash-table)
(packn-pos (list root "-REM") root)))
(:count
(and (eq key2 :hash-table)
(packn-pos (list root "-COUNT") root)))
(:init
(and (eq key2 :hash-table)
(packn-pos (list root "-INIT") root)))
(:clear
(and (eq key2 :hash-table)
(packn-pos (list root "-CLEAR") root)))
; >---
(otherwise
(er hard 'defstobj-fnname
"Implementation error (bad case); please contact ACL2 ~
implementors."))))
(temp (and default-fnname ; see comment above
(assoc-eq default-fnname renaming-alist))))
(if temp (cadr temp) default-fnname)))
(defun defstobj-fields-template (field-descriptors renaming wrld)
; Note: Wrld may be a world or nil. See fix-stobj-array-type.
(cond
((endp field-descriptors) nil)
(t
(let* ((field (if (atom (car field-descriptors))
(car field-descriptors)
(car (car field-descriptors))))
(type (if (consp (car field-descriptors))
(or (cadr (assoc-keyword :type
(cdr (car field-descriptors))))
t)
t))
(init (if (consp (car field-descriptors))
(cadr (assoc-keyword :initially
(cdr (car field-descriptors))))
nil))
(resizable (if (consp (car field-descriptors))
(cadr (assoc-keyword :resizable
(cdr (car field-descriptors))))
nil))
;---<
(key2 (if (consp type)
(case (car type)
(array :array)
(hash-table :hash-table)
(t :non-array))
:non-array))
; >---
(fieldp-name (defstobj-fnname field :recognizer key2 renaming))
(accessor-name (defstobj-fnname field :accessor key2 renaming))
(updater-name (defstobj-fnname field :updater key2 renaming))
;---<
(boundp-name (defstobj-fnname field :boundp key2 renaming))
(accessor?-name (defstobj-fnname field :accessor? key2
renaming))
(remove-name (defstobj-fnname field :remove key2 renaming))
(count-name (defstobj-fnname field :count key2 renaming))
(clear-name (defstobj-fnname field :clear key2 renaming))
(init-name (defstobj-fnname field :init key2 renaming))
; >---
(resize-name (defstobj-fnname field :resize key2 renaming))
(length-name (defstobj-fnname field :length key2 renaming)))
(cons (list fieldp-name
(cond ((and (consp type)
(eq (car type) 'array))
(fix-stobj-array-type type wrld))
(t type))
init
accessor-name
updater-name
length-name
resize-name
resizable
;---<
boundp-name
accessor?-name
remove-name
count-name
clear-name
init-name
; >---
)
(defstobj-fields-template
(cdr field-descriptors) renaming wrld))))))
(defun defstobj-raw-init-fields (ftemps)
; Keep this in sync with defstobj-axiomatic-init-fields.
(cond
((endp ftemps) nil)
(t (let* ((field-template (car ftemps))
(type (nth 1 field-template))
(arrayp (and (consp type) (eq (car type) 'array)))
;---<
(hashp (and (consp type) (eq (car type) 'hash-table)))
(hash-test (and hashp (cadr type)))
(hash-init-size (and hashp (if (cddr type)
(caddr type)
20)))
; >---
(array-etype (and arrayp (cadr type)))
(array-size (and arrayp (car (caddr type))))
(stobj-creator (get-stobj-creator (if arrayp array-etype type)
nil))
(init (nth 2 field-template)))
(cond
(arrayp
(cons (cond (stobj-creator
(assert$
(null init) ; checked by chk-stobj-field-descriptor
(assert$
; We expect array-size to be a natural number, as this is checked by
; chk-stobj-field-descriptor (using fix-stobj-array-type). It is important
; that array-size not be a Lisp form that references the variable AR, even
; after macroexpasion, in order to avoid capture by the binding of AR below.
(natp array-size)
`(let ((ar (make-array$ ,array-size
; Do not be tempted to use :initial-element (,stobj-creator) here, because that
; would presumably share structure among all the created stobjs.
:element-type ',array-etype)))
(loop for i from 0 to ,(1- array-size)
do
(setf (svref ar i) (,stobj-creator)))
ar))))
(t `(make-array$ ,array-size
:element-type ',array-etype
:initial-element ',init)))
(defstobj-raw-init-fields (cdr ftemps))))
;---<
(hashp
(cons `(make-hash-table
:test
,(case hash-test
(eql ''eql)
(equal
;; Is this safe?
''equal)
(t (er hard hash-test
"The hash test should be either EQL or EQUAL.~%")))
:size ,hash-init-size)
(defstobj-raw-init-fields (cdr ftemps))))
; >---
((eq type t)
(cons (kwote init) (defstobj-raw-init-fields (cdr ftemps))))
(stobj-creator
(cons `(,stobj-creator) (defstobj-raw-init-fields (cdr ftemps))))
(t (cons `(make-array$ 1
:element-type ',type
:initial-element ',init)
(defstobj-raw-init-fields (cdr ftemps)))))))))
(defun defstobj-component-recognizer-axiomatic-defs (name template ftemps wrld)
; Warning: See the guard remarks in the Essay on Defstobj Definitions.
; It is permissible for wrld to be nil, as this merely defeats additional
; checking by translate-declaration-to-guard.
; We return a list of defs (see defstobj-axiomatic-defs) for all the
; recognizers for the single-threaded resource named name with the given
; template. The answer contains the top-level recognizer as well as the
; definitions of all component recognizers. The answer contains defs for
; auxiliary functions used in array component recognizers. The defs are listed
; in an order suitable for processing (components first, then top-level).
(cond
((endp ftemps)
(let* ((recog-name (car template))
(field-templates (caddr template))
(n (length field-templates)))
; Rockwell Addition: See comment below.
; Note: The recognizer for a stobj must be Boolean! That is why we
; conclude the AND below with a final T. The individual field
; recognizers need not be Boolean and sometimes are not! For example,
; a field with :TYPE (MEMBER e1 ... ek) won't be Boolean, nor with
; certain :TYPE (OR ...) involving MEMBER. The reason we want the
; stobj recognizer to be Boolean is so that we can replace it by T in
; guard conjectures for functions that have been translated with the
; stobj syntactic restrictions. See optimize-stobj-recognizers.
(list `(,recog-name (,name)
(declare (xargs :guard t
:verify-guards t))
(and (true-listp ,name)
(= (length ,name) ,n)
,@(defstobj-component-recognizer-calls
field-templates 0 name nil)
t)))))
(t
(let ((recog-name (nth 0 (car ftemps)))
(type (nth 1 (car ftemps))))
; Below we simply append the def or defs for this field to those for
; the rest. We get two defs for each array field and one def for each
; of the others.
(cons (cond
((and (consp type)
(eq (car type) 'array))
(let ((etype (cadr type)))
`(,recog-name (x)
(declare (xargs :guard t
:verify-guards t))
(if (atom x)
(equal x nil)
(and ,(translate-stobj-type-to-guard
etype '(car x) wrld)
(,recog-name (cdr x)))))))
;---<
((and (consp type)
(eq (car type) 'hash-table))
`(,recog-name (x)
(declare (xargs :guard t
:verify-guards t)
(ignore x))
t))
; >---
(t (let ((type-term (translate-stobj-type-to-guard
type 'x wrld)))
; We may not use x in the type-term and so have to declare it ignorable.
`(,recog-name (x)
(declare (xargs :guard t
:verify-guards t)
(ignorable x))
,type-term))))
(defstobj-component-recognizer-axiomatic-defs
name template (cdr ftemps) wrld))))))
(defun defstobj-field-fns-axiomatic-defs (top-recog var n ftemps wrld)
; Wrld is normally a logical world, but it can be nil when calling this
; function from raw Lisp.
; Warning: Keep the formals in the definitions below in sync with corresponding
; formals defstobj-field-fns-raw-defs. Otherwise trace$ may not work
; correctly; we saw such a problem in Version_5.0 for a resize function.
; Warning: See the guard remarks in the Essay on Defstobj Definitions.
; We return a list of defs (see defstobj-axiomatic-defs) for all the accessors,
; updaters, and optionally, array resizing and length, of a single-threaded
; resource.
; Warning: Each updater definition should immediately follow the corresponding
; accessor definition, so that this is the case for the list of definitions
; returned by defstobj-axiomatic-defs. That list of definitions becomes the
; 'stobj property laid down by defstobj-fn, and function
; chk-stobj-let/updaters1 assumes that it will find each updater definition in
; that property immediately after the corresponding accessor definition.
(cond
((endp ftemps)
nil)
(t (let* ((field-template (car ftemps))
(type (nth 1 field-template))
(init (nth 2 field-template))
(arrayp (and (consp type) (eq (car type) 'array)))
;---<
(hashp (and (consp type) (eq (car type) 'hash-table)))
(hash-test (and hashp (cadr type)))
; >---
(type-term ; used in guard
(and (not arrayp) ; else type-term is not used
;---<
(not hashp)
; >---
(if (null wrld) ; called from raw Lisp, so guard is ignored
t
(translate-stobj-type-to-guard type 'v wrld))))
(array-etype (and arrayp (cadr type)))
(array-etype-term ; used in guard
(and arrayp ; else array-etype-term is not used
(if (null wrld) ; called from raw Lisp, so guard is ignored
t
(translate-stobj-type-to-guard array-etype 'v wrld))))
(array-length (and arrayp (car (caddr type))))
(accessor-name (nth 3 field-template))
(updater-name (nth 4 field-template))
(length-name (nth 5 field-template))
(resize-name (nth 6 field-template))
(resizable (nth 7 field-template))
;---<
(boundp-name (nth 8 field-template))
(accessor?-name (nth 9 field-template))
(remove-name (nth 10 field-template))
(count-name (nth 11 field-template))
(clear-name (nth 12 field-template))
(init-name (nth 13 field-template))
; >---
)
(cond
(arrayp
(append
`((,length-name (,var)
(declare (xargs :guard (,top-recog ,var)
:verify-guards t)
,@(and (not resizable)
`((ignore ,var))))
,(if resizable
`(len (nth ,n ,var))
`,array-length))
(,resize-name
(i ,var)
(declare (xargs :guard (,top-recog ,var)
:verify-guards t)
,@(and (not resizable)
'((ignore i))))
,(if resizable
`(update-nth ,n
(resize-list (nth ,n ,var) i ',init)
,var)
`(prog2$ (hard-error
',resize-name
"The array field corresponding to accessor ~x0 of ~
stobj ~x1 was not declared :resizable t. ~
Therefore, it is illegal to resize this array."
(list (cons #\0 ',accessor-name)
(cons #\1 ',var)))
,var)))
(,accessor-name (i ,var)
(declare (xargs :guard
(and (,top-recog ,var)
(integerp i)
(<= 0 i)
(< i (,length-name ,var)))
:verify-guards t))
(nth i (nth ,n ,var)))
(,updater-name (i v ,var)
(declare (xargs :guard
(and (,top-recog ,var)
(integerp i)
(<= 0 i)
(< i (,length-name ,var))
,@(if (eq array-etype-term
t)
nil
(list array-etype-term)))
:verify-guards t))
(update-nth-array ,n i v ,var)))
(defstobj-field-fns-axiomatic-defs
top-recog var (+ n 1) (cdr ftemps) wrld)))
;---<
(hashp
(append
`((,accessor-name
(k ,var)
(declare (xargs :guard ,(if (eq hash-test 'eql)
`(and (,top-recog ,var)
(eqlablep k))
`(,top-recog ,var))
:verify-guards t))
(cdr (hons-assoc-equal k (nth ,n ,var))))
(,updater-name
(k v ,var)
(declare (xargs :guard ,(if (eq hash-test 'eql)
`(and (,top-recog ,var)
(eqlablep k))
`(,top-recog ,var))
:verify-guards t))
(update-nth ,n (cons (cons k v) (nth ,n ,var)) ,var))
(,boundp-name
(k ,var)
(declare (xargs :guard ,(if (eq hash-test 'eql)
`(and (,top-recog ,var)
(eqlablep k))
`(,top-recog ,var))
:verify-guards t))
(consp (hons-assoc-equal k (nth ,n ,var))))
(,accessor?-name
(k ,var)
(declare (xargs :guard ,(if (eq hash-test 'eql)
`(and (,top-recog ,var)
(eqlablep k))
`(,top-recog ,var))
:verify-guards t))
(mv (,accessor-name k ,var)
(,boundp-name k ,var)))
(,remove-name
(k ,var)
(declare (xargs :guard ,(if (eq hash-test 'eql)
`(and (,top-recog ,var)
(eqlablep k))
`(,top-recog ,var))
:verify-guards t))
(update-nth ,n (hons-remove-assoc k (nth ,n ,var)) ,var))
(,count-name
(,var)
(declare (xargs :guard (,top-recog ,var)))
(count-keys (nth ,n ,var)))
(,clear-name
(,var)
(declare (xargs :guard (,top-recog ,var)))
(update-nth ,n nil ,var))
(,init-name
(size rehash-size rehash-threshold ,var)
(declare (xargs :guard (and (,top-recog ,var)
(or (natp size)
(not size))
(or (and (rationalp rehash-size)
(< 1 rehash-size))
(not rehash-size))
(or (and (rationalp rehash-threshold)
(<= 0 rehash-threshold)
(<= rehash-threshold 1))
(not rehash-threshold))))
(ignorable size rehash-size rehash-threshold))
(update-nth ,n nil ,var)))
(defstobj-field-fns-axiomatic-defs
top-recog var (+ n 1) (cdr ftemps) wrld)))
; >---
(t
(append
`((,accessor-name (,var)
(declare (xargs :guard (,top-recog ,var)
:verify-guards t))
(nth ,n ,var))
(,updater-name (v ,var)
(declare (xargs :guard
,(if (eq type-term t)
`(,top-recog ,var)
`(and ,type-term
(,top-recog ,var)))
:verify-guards t))
(update-nth ,n v ,var)))
(defstobj-field-fns-axiomatic-defs
top-recog var (+ n 1) (cdr ftemps) wrld))))))))
(defun defstobj-axiomatic-init-fields (ftemps wrld)
; Keep this in sync with defstobj-raw-init-fields.
(cond
((endp ftemps) nil)
(t (let* ((field-template (car ftemps))
(type (nth 1 field-template))
(arrayp (and (consp type) (eq (car type) 'array)))
(array-size (and arrayp (car (caddr type))))
;---<
(hashp (and (consp type) (eq (car type) 'hash-table)))
; >---
(init0 (nth 2 field-template))
(creator (get-stobj-creator (if arrayp (cadr type) type)
wrld))
(init (if creator
`(non-exec (,creator))
(kwote init0))))
(cond
(arrayp
(cons `(make-list ,array-size :initial-element ,init)
(defstobj-axiomatic-init-fields (cdr ftemps) wrld)))
;---<
(hashp
(cons nil
(defstobj-axiomatic-init-fields (cdr ftemps) wrld)))
; >---
(t ; whether the type is given or not is irrelevant
(cons init
(defstobj-axiomatic-init-fields (cdr ftemps) wrld))))))))
(defun defstobj-field-fns-raw-defs (var flush-var inline n ftemps)
; Warning: Keep the formals in the definitions below in sync with corresponding
; formals defstobj-field-fns-raw-defs. Otherwise trace$ may not work
; correctly; we saw such a problem in Version_5.0 for a resize function.
; Warning: See the guard remarks in the Essay on Defstobj Definitions.
#-hons (declare (ignorable flush-var)) ; irrelevant var without hons
(cond
((endp ftemps) nil)
(t
(append
(let* ((field-template (car ftemps))
(type (nth 1 field-template))
(init (nth 2 field-template))
(arrayp (and (consp type) (eq (car type) 'array)))
(array-etype0 (and arrayp (cadr type)))
(stobj-creator (get-stobj-creator (if arrayp array-etype0 type)
nil))
(scalar-type
(if stobj-creator t type)) ; only used when (not arrayp)
(array-etype (and arrayp
(if stobj-creator
; Stobj-creator is non-nil when array-etype is a stobj. The real element type,
; then, is simple-array rather than a simple-array-type, so we might say that
; the parent stobj array is not simple. But we will assume that the advantage
; of having a simple-vector for the parent stobj outweighs the advantage of
; having a simple-vector element type declaration.
t
array-etype0)))
(simple-type (and arrayp
(simple-array-type array-etype (caddr type))))
(array-length (and arrayp (car (caddr type))))
(vref (and arrayp
(if (eq (car simple-type) 'simple-vector)
'svref
'aref)))
(fix-vref (and arrayp
(if (array-etype-is-fixnum-type array-etype)
'fix-aref
vref)))
(accessor-name (nth 3 field-template))
(updater-name (nth 4 field-template))
(length-name (nth 5 field-template))
(resize-name (nth 6 field-template))
(resizable (nth 7 field-template))
;---<
(hashp (and (consp type) (eq (car type) 'hash-table)))
(hash-test (and hashp (cadr type)))
(boundp-name (nth 8 field-template))
(accessor?-name (nth 9 field-template))
(remove-name (nth 10 field-template))
(count-name (nth 11 field-template))
(clear-name (nth 12 field-template))
(init-name (nth 13 field-template))
; >---
)
(cond
;---<
(hashp
`((,accessor-name
(k ,var)
,@(and inline (list *stobj-inline-declare*))
(values (gethash ,(if (eq hash-test 'hons-equal)
`(hons-copy k)
'k)
(the hash-table (svref ,var ,n)))))
(,updater-name
(k v ,var)
,@(and inline (list *stobj-inline-declare*))
(progn
#+hons (memoize-flush ,var)
(setf (gethash ,(if (eq hash-test 'hons-equal)
`(hons-copy k)
'k)
(the hash-table (svref ,var ,n)))
v)
,var))
(,boundp-name
(k ,var)
,@(and inline (list *stobj-inline-declare*))
(multiple-value-bind (val boundp)
(gethash ,(if (eq hash-test 'hons-equal)
`(hons-copy k)
'k)
(the hash-table (svref ,var ,n)))
(declare (ignore val))
(if boundp t nil)))
(,accessor?-name
(k ,var)
,@(and inline (list *stobj-inline-declare*))
(multiple-value-bind
(val boundp)
(gethash ,(if (eq hash-test 'hons-equal)
`(hons-copy k)
'k)
(the hash-table (svref ,var ,n)))
(mv val (if boundp t nil))))
(,remove-name
(k ,var)
,@(and inline (list *stobj-inline-declare*))
(progn
#+(and hons (not acl2-loop-only))
(memoize-flush ,var)
(remhash ,(if (eq hash-test 'hons-equal)
`(hons-copy k)
'k)
(the hash-table (svref ,var ,n)))
,var))
(,count-name
(,var)
,@(and inline (list *stobj-inline-declare*))
(hash-table-count (svref ,var ,n)))
(,clear-name
(,var)
,@(and inline (list *stobj-inline-declare*))
(progn
#+(and hons (not acl2-loop-only))
(memoize-flush ,var)
(clrhash (svref ,var ,n))
,var))
(,init-name
(size rehash-size rehash-threshold ,var)
,@(and inline (list *stobj-inline-declare*))
(progn
#+(and hons (not acl2-loop-only))
(memoize-flush ,var)
(setf (svref ,var ,n)
(make-hash-table
:test ',(case hash-test
(eql 'eql)
(equal 'equal)
(t (er hard hash-test
"The hash test should be either ~
EQL or EQUAL.~%")))
:size (or size 60)
:rehash-size (if rehash-size
(float rehash-size)
(float 17/10))
:rehash-threshold (if rehash-threshold
(float rehash-threshold)
(float 3/4))))
,var))))
; >---
(arrayp
`((,length-name
(,var)
,@(and inline (list *stobj-inline-declare*))
,@(if (not resizable)
`((declare (ignore ,var))
,array-length)
`((the (and fixnum (integer 0 *))
(length (svref ,var ,n))))))
(,resize-name
(i ,var)
,@(if (not resizable)
`((declare (ignore i))
(prog2$
(er hard ',resize-name
"The array field corresponding to accessor ~x0 of ~
stobj ~x1 was not declared :resizable t. ~
Therefore, it is illegal to resize this array."
',accessor-name
',var)
,var))
`((if (not (and (integerp i)
(>= i 0)
(< i array-dimension-limit)))
(hard-error
',resize-name
"Attempted array resize failed because the requested ~
size ~x0 was not a nonnegative integer less than the ~
value of Common Lisp constant array-dimension-limit, ~
which is ~x1. These bounds on array sizes are fixed ~
by ACL2."
(list (cons #\0 i)
(cons #\1 array-dimension-limit)))
(let* ((var ,var)
(old (svref var ,n))
(min-index (min i (length old)))
(new (make-array$ i
; The :initial-element below is probably not necessary in the case
; that we are downsizing the array. At least, CLtL2 does not make any
; requirements about specifying an :initial-element, even when an
; :element-type is supplied. However, it seems harmless enough to go
; ahead and specify :initial-element even for downsizing: resizing is
; not expected to be fast, we save a case split here (at the expense
; of this comment!), and besides, we are protecting against the
; possibility that some Common Lisp will fail to respect the spec and
; will cause an error by trying to initialize a fixnum array (say)
; with NILs.
:initial-element
',init
:element-type
',array-etype)))
#+hons (memoize-flush ,flush-var)
(setf (svref var ,n)
(,(pack2 'stobj-copy-array- fix-vref)
old new 0 min-index))
,@(and stobj-creator
`((when (< (length old) i)
(loop for j from (length old) to (1- i)
do (setf (svref new j)
(,stobj-creator))))))
var)))))
(,accessor-name
(i ,var)
(declare (type (and fixnum (integer 0 *)) i))
,@(and inline (list *stobj-inline-declare*))
(the ,array-etype
(,vref (the ,simple-type (svref ,var ,n))
(the (and fixnum (integer 0 *)) i))))
(,updater-name
(i v ,var)
(declare (type (and fixnum (integer 0 *)) i)
(type ,array-etype v))
,@(and inline (list *stobj-inline-declare*))
(progn
#+hons (memoize-flush ,flush-var)
(setf (,vref (the ,simple-type (svref ,var ,n))
(the (and fixnum (integer 0 *)) i))
(the ,array-etype v))
,var))))
((eq scalar-type t)
`((,accessor-name (,var)
,@(and inline (list *stobj-inline-declare*))
(svref ,var ,n))
(,updater-name (v ,var)
,@(and inline (list *stobj-inline-declare*))
(progn
#+hons (memoize-flush ,flush-var)
; For the case of a stobj field, we considered causing an error here since the
; raw Lisp code for stobj-let avoids calling updaters because there is no need:
; updates for fields that are stobjs have already updated destructively.
; However, updaters can be called when evaluating forms at the top level, so we
; have decided not to cause such an error here.
;; !! Think about the comment above -- can we avoid this update. I mean, we'd
;; better be dealing only with live stobjs here, right? Otherwise don't we
;; have all sorts of problems using setf?
(setf (svref ,var ,n) v)
,var))))
(t
(assert$
(not stobj-creator) ; scalar-type is t for stobj-creator
`((,accessor-name (,var)
,@(and inline (list *stobj-inline-declare*))
(the ,scalar-type
(aref (the (simple-array ,scalar-type (1))
(svref ,var ,n))
0)))
(,updater-name (v ,var)
(declare (type ,scalar-type v))
,@(and inline (list *stobj-inline-declare*))
(progn
#+hons (memoize-flush ,flush-var)
(setf (aref (the (simple-array ,scalar-type (1))
(svref ,var ,n))
0)
(the ,scalar-type v))
,var)))))))
(defstobj-field-fns-raw-defs var flush-var inline (1+ n) (cdr ftemps))))))
(defun chk-stobj-field-descriptor (name field-descriptor ctx wrld state)
; See the comment just before chk-acceptable-defstobj1 for an explanation of
; our handling of Common Lisp compliance.
(cond
((symbolp field-descriptor) (value nil))
(t
(er-progn
(cond ((and (consp field-descriptor)
(symbolp (car field-descriptor))
(keyword-value-listp (cdr field-descriptor))
(member-equal (length field-descriptor) '(1 3 5 7))
(let ((keys (odds field-descriptor)))
(and (no-duplicatesp keys)
(subsetp-eq keys '(:type :initially :resizable)))))
(value nil))
(t (er soft ctx
"The field descriptors of a single-threaded object ~
definition must be a symbolic field-name or a list of the ~
form (field-name :type type :initially val), where ~
field-name is a symbol. The :type and :initially keyword ~
assignments are optional and their order is irrelevant. ~
The purported descriptor ~x0 for a field in ~x1 is not of ~
this form."
field-descriptor
name)))
(let* ((field (car field-descriptor))
(type (if (assoc-keyword :type (cdr field-descriptor))
(cadr (assoc-keyword :type (cdr field-descriptor)))
t))
(initp (assoc-keyword :initially (cdr field-descriptor)))
(init (if initp (cadr initp) nil))
(resizable (if (assoc-keyword :resizable (cdr field-descriptor))
(cadr (assoc-keyword :resizable
(cdr field-descriptor)))
nil)))
(cond
((and resizable (not (eq resizable t)))
(er soft ctx
"The :resizable value in the ~x0 field of ~x1 is illegal: ~x2. ~
The legal values are t and nil."
field name resizable))
((and (consp type)
(eq (car type) 'array))
(cond
((not (and (true-listp type)
(equal (length type) 3)
(true-listp (caddr type))
(equal (length (caddr type)) 1)))
(er soft ctx
"When a field descriptor specifies an ARRAY :type, the type ~
must be of the form (ARRAY etype (n)). Note that we only ~
support single-dimensional arrays. The purported ARRAY :type ~
~x0 for the ~x1 field of ~x2 is not of this form."
type field name))
(t (let* ((type0 (fix-stobj-array-type type wrld))
(etype (cadr type0))
(stobjp (stobjp etype t wrld))
(etype-term ; used only when (not stobjp)
(and (not stobjp) ; optimization
(translate-declaration-to-guard etype 'x wrld)))
(n (car (caddr type0))))
(cond
((not (natp n))
(er soft ctx
"An array dimension must be a non-negative integer or a ~
defined constant whose value is a non-negative integer. ~
~ The :type ~x0 for the ~x1 field of ~x2 is thus ~
illegal."
type0 field name))
(stobjp ; Defstobj-raw-init-fields depends on this check.
(cond ((null initp) (value nil))
(t (er soft ctx
"The :initially keyword must be omitted for a ~
:type specified as an array of stobjs. But ~
for :type ~x0, :initially is specified as ~x1 ~
for the ~x2 field of ~x3."
type init field name))))
((null etype-term)
(er soft ctx
"The element type specified for the ~x0 field of ~
~x1, namely ~x2, is not recognized by ACL2 as a ~
type-spec or a stobj name. See :DOC type-spec."
field name type))
(t
(er-let*
((pair (simple-translate-and-eval etype-term
(list (cons 'x init))
nil
(msg
"The type ~x0"
etype-term)
ctx
wrld
state
nil)))
; pair is (tterm . val), where tterm is a term and val is its value
; under x<-init.
(er-progn
(chk-common-lisp-compliant-subfunctions
nil (list field) (list (car pair))
wrld "auxiliary function" ctx state)
(chk-unrestricted-guards-for-user-fns
(all-fnnames (car pair))
wrld ctx state)
(cond
((not (cdr pair))
(er soft ctx
"The value specified by the :initially ~
keyword, namely ~x0, fails to satisfy the ~
declared type ~x1 in the array ~
specification for the ~x2 field of ~x3."
init etype field name))
(t (value nil)))))))))))
((assoc-keyword :resizable (cdr field-descriptor))
(er soft ctx
"The :resizable keyword is only legal for array types, hence is ~
illegal for the ~x0 field of ~x1."
field name))
;---<
((and (consp type)
(eq (car type) 'hash-table))
(cond ((or (atom (cdr type))
(not (member (cadr type)
'(EQL
EQUAL
#+(and hons (not acl2-loop-only))
HONS-EQUAL))))
(er soft ctx "A hash-table type must be specified as ~
(HASH-TABLE TEST), where test is EQL, ~
EQUAL, or (when built with the HONS ~
extension) HONS-EQUAL. The test given was ~
~x0.~%" (and (consp (cdr type))
(cadr type))))
(t (value nil))))
; >---
(t (let* ((stobjp (stobjp type t wrld))
(type-term ; used only when (not stobjp)
(and (not stobjp) ; optimization
(translate-declaration-to-guard type 'x wrld))))
(cond
(stobjp ; Defstobj-raw-init-fields depends on this check.
(cond ((null initp) (value nil))
(t (er soft ctx
"The :initially keyword must be omitted for a ~
:type specified as a stobj. But for :type ~x0, ~
:initially is specified as ~x1 for the ~x2 field ~
of ~x3."
type init field name))))
((null type-term)
(er soft ctx
"The :type specified for the ~x0 field of ~x1, namely ~x2, ~
is not recognized by ACL2 as a type-spec or a stobj name. ~
~ See :DOC type-spec."
field name type))
(t
(er-let* ((pair (simple-translate-and-eval type-term
(list (cons 'x init))
nil
(msg
"The type ~x0"
type-term)
ctx
wrld
state
nil)))
; pair is (tterm . val), where tterm is a term and val is its value
; under x<-init.
(er-progn
(chk-common-lisp-compliant-subfunctions
nil (list field) (list (car pair))
wrld "body" ctx state)
(chk-unrestricted-guards-for-user-fns
(all-fnnames (car pair))
wrld ctx state)
(cond
((not (cdr pair))
(er soft ctx
"The value specified by the :initially keyword, ~
namely ~x0, fails to satisfy the declared :type ~x1 ~
for the ~x2 field of ~x3."
init type field name))
(t (value nil)))))))))))))))
(defun chk-acceptable-defstobj1
(name field-descriptors ftemps renaming ctx wrld state names const-names)
; We check whether it is legal to define name as a single-threaded
; object with the description given in field-descriptors. We know
; name is a legal (and new) stobj name and we know that renaming is a
; symbol to symbol doublet-style alist. But we know nothing else. We
; either signal an error or return the world in which the event is to
; be processed (thus implementing redefinitions). Names is, in
; general, the actual set of names that the defstobj event will
; introduce. That is, it contains the images of the default names
; under the renaming alist. We accumulate the actual names into it as
; we go and check that it contains no duplicates at the termination of
; this function. All of the names in names are to be defined as
; functions with :VERIFY-GUARDS T. See the comment above about
; Common Lisp compliance.
(cond
((endp ftemps)
(let* ((recog-name (defstobj-fnname name :recognizer :top renaming))
(creator-name (defstobj-fnname name :creator :top renaming))
(names (list* recog-name creator-name names)))
(er-progn
(chk-all-but-new-name recog-name ctx 'function wrld state)
(chk-all-but-new-name creator-name ctx 'function wrld state)
(chk-acceptable-defstobj-renaming name field-descriptors renaming
ctx state nil)
; Note: We insist that all the names be new. In addition to the
; obvious necessity for something like this, we note that this does
; not permit us to have redundantly defined any of these names. For
; example, the user might have already defined a field recognizer,
; PCP, that is identically defined to what we will lay down. But we
; do not allow that. We basically insist that we have control over
; every one of these names.
(chk-just-new-names names 'function nil ctx wrld state)
(chk-just-new-names const-names 'const nil ctx wrld state))))
(t
; An element of field-descriptors (i.e., of ftemps) is either a symbolic field
; name, field, or else of the form (field :type type :initially val), where
; either or both of the keyword fields can be omitted. Val must be an evg,
; i.e., an unquoted constant like t, nil, 0 or undef (the latter meaning the
; symbol 'undef). :Type defaults to the unrestricted type t and :initially
; defaults to nil. Type is either a primitive type, as recognized by
; translate-declaration-to-guard, or a stobj name, or else is of the form
; (array ptype (n)), where ptype is a primitive type or stobj name and n is an
; positive integer constant. If type is a stobj name or an array of such, then
; :initially must be omitted.
(er-progn
(chk-stobj-field-descriptor name (car ftemps) ctx wrld state)
(let* ((field (if (atom (car ftemps))
(car ftemps)
(car (car ftemps))))
(type (if (consp (car ftemps))
(or (cadr (assoc-keyword :type
(cdr (car ftemps))))
t)
t))
;---<
(key2 (if (consp type)
(case (car type)
(array :array)
(hash-table :hash-table)
(t :non-array))
:non-array))
(boundp-name (defstobj-fnname field :boundp key2 renaming))
(accessor?-name (defstobj-fnname field :accessor? key2
renaming))
(remove-name (defstobj-fnname field :remove key2
renaming))
(count-name (defstobj-fnname field :count key2 renaming))
(clear-name (defstobj-fnname field :clear key2 renaming))
(init-name (defstobj-fnname field :init key2 renaming))
; >---
(fieldp-name (defstobj-fnname field :recognizer key2 renaming))
(accessor-name (defstobj-fnname field :accessor key2 renaming))
(accessor-const-name (defconst-name accessor-name))
(updater-name (defstobj-fnname field :updater key2 renaming))
(length-name (defstobj-fnname field :length key2 renaming))
(resize-name (defstobj-fnname field :resize key2 renaming)))
(er-progn
(chk-all-but-new-name fieldp-name ctx 'function wrld state)
(chk-all-but-new-name accessor-name ctx 'function wrld state)
(chk-all-but-new-name updater-name ctx 'function wrld state)
(chk-all-but-new-name accessor-const-name ctx 'const wrld state)
(if (eq key2 :array)
(er-progn (chk-all-but-new-name length-name ctx 'function wrld state)
(chk-all-but-new-name resize-name ctx 'function wrld state))
;---<
(if (eq key2 :hash-table)
(er-progn (chk-all-but-new-name boundp-name ctx
'function wrld state)
(chk-all-but-new-name accessor?-name ctx
'function wrld state)
(chk-all-but-new-name remove-name ctx
'function wrld state))
(value nil)
; >---
))
(chk-acceptable-defstobj1 name field-descriptors (cdr ftemps)
renaming ctx wrld state
(list* fieldp-name
accessor-name
updater-name
(if (eq key2 :array)
(list* length-name
resize-name
names)
;---<
(if (eq key2 :hash-table)
(list* boundp-name
accessor?-name
remove-name
count-name
clear-name
init-name
names)
names)
; >---
))
(cons accessor-const-name
const-names))))))))
(defun put-stobjs-in-and-outs1 (name ftemps wrld)
; See put-stobjs-in-and-outs for a table that explains what we're doing.
(cond
((endp ftemps) wrld)
(t (let ((type (nth 1 (car ftemps)))
(acc-fn (nth 3 (car ftemps)))
(upd-fn (nth 4 (car ftemps)))
(length-fn (nth 5 (car ftemps)))
(resize-fn (nth 6 (car ftemps)))
;;---<
(boundp-fn (nth 8 (car ftemps)))
(accessor?-fn (nth 9 (car ftemps)))
(remove-fn (nth 10 (car ftemps)))
(count-fn (nth 11 (car ftemps)))
(clear-fn (nth 12 (car ftemps)))
(init-fn (nth 13 (car ftemps)))
;; >---
)
(put-stobjs-in-and-outs1
name
(cdr ftemps)
(cond
((and (consp type)
(eq (car type) 'array))
(let* ((etype (cadr type))
(stobj-flg (and (stobjp etype t wrld)
etype)))
(putprop
length-fn 'stobjs-in (list name)
(putprop
resize-fn 'stobjs-in (list nil name)
(putprop
resize-fn 'stobjs-out (list name)
(putprop
acc-fn 'stobjs-in (list nil name)
(putprop-unless
acc-fn 'stobjs-out (list stobj-flg) '(nil)
(putprop
upd-fn 'stobjs-in (list nil stobj-flg name)
(putprop
upd-fn 'stobjs-out (list name) wrld)))))))))
;;---<
((and (consp type)
(eq (car type) 'hash-table))
(putprop
init-fn 'stobjs-in (list nil nil nil name)
(putprop
init-fn 'stobjs-out (list name)
(putprop
clear-fn 'stobjs-in (list name)
(putprop
clear-fn 'stobjs-out (list name)
(putprop
count-fn 'stobjs-in (list name)
(putprop
remove-fn 'stobjs-in (list nil name)
(putprop
remove-fn 'stobjs-out (list name)
(putprop
accessor?-fn 'stobjs-in (list nil name)
(putprop
boundp-fn 'stobjs-in (list nil name)
(putprop
acc-fn 'stobjs-in (list nil name)
(putprop
upd-fn 'stobjs-in (list nil nil name)
(putprop
upd-fn 'stobjs-out (list name) wrld)))))))))))))
;; >---
(t
(let ((stobj-flg (and (stobjp type t wrld)
type)))
(putprop
acc-fn 'stobjs-in (list name)
(putprop-unless
acc-fn 'stobjs-out (list stobj-flg) '(nil)
(putprop
upd-fn 'stobjs-in (list stobj-flg name)
(putprop
upd-fn 'stobjs-out (list name) wrld))))))))))))
(redef-)
;; Macro for proving theorems like the ones above about a hash field:
(defmacro prove-ht-theorems (field stobj &optional renaming)
(let ((get (defstobj-fnname field :accessor :hash-table renaming))
(boundp (defstobj-fnname field :boundp :hash-table renaming))
(put (defstobj-fnname field :updater :hash-table renaming))
(rem (defstobj-fnname field :remove :hash-table renaming))
(count (defstobj-fnname field :count :hash-table renaming))
(clear (defstobj-fnname field :clear :hash-table renaming))
(init (defstobj-fnname field :init :hash-table renaming))
(make (defstobj-fnname stobj :creator :hash-table renaming)))
`(progn
(defthm ,(packn-pos (list field "-INIT-IS-CLEAR") field)
(equal (,init size rehash-size rehash-threshold ,stobj)
(,clear ,stobj)))
(defthm ,(packn-pos (list field "-GET-BOUNDP") field)
(implies (,get k ,stobj)
(,boundp k ,stobj)))
(defthm ,(packn-pos (list field "-BOUNDP-START") field)
(not (,boundp k (,make))))
(defthm ,(packn-pos (list field "-BOUNDP-CLEAR") field)
(not (,boundp k (,clear ,stobj))))
(defthm ,(packn-pos (list field "-BOUNDP-PUT-SAME") field)
(,boundp k (,put k v ,stobj)))
(defthm ,(packn-pos (list field "-BOUNDP-PUT-DIFF") field)
(implies (not (equal j k))
(equal (,boundp k (,put j v ,stobj))
(,boundp k ,stobj))))
(defthm ,(packn-pos (list field "-GET-PUT-SAME") field)
(equal (,get k (,put k v ,stobj))
v))
(defthm ,(packn-pos (list field "-GET-PUT-DIFF") field)
(implies (not (equal j k))
(equal (,get k (,put j v ,stobj))
(,get k ,stobj))))
(defthm ,(packn-pos (list field "-REM-BOUNDP-SAME") field)
(not (,boundp k (,rem k ,stobj))))
(defthm ,(packn-pos (list field "-REM-BOUNDP-DIFF") field)
(implies (not (equal j k))
(equal (,boundp k (,rem j ,stobj))
(,boundp k ,stobj))))
(defthm ,(packn-pos (list field "-REM-GET-DIFF") field)
(implies (not (equal j k))
(equal (,get k (,rem j ,stobj))
(,get k ,stobj))))
(defthm ,(packn-pos (list field "-COUNT-START") field)
(equal (,count (,make)) 0))
(defthm ,(packn-pos (list field "-COUNT-PUT") field)
(equal (,count (,put k v ,stobj))
(if (,boundp k ,stobj)
(,count ,stobj)
(+ 1 (,count ,stobj)))))
(defthm ,(packn-pos (list field "-COUNT-REM") field)
(equal (,count (,rem k ,stobj))
(if (,boundp k ,stobj)
(- (,count ,stobj) 1)
(,count ,stobj))))
(defthm ,(packn-pos (list field "-COUNT-CLEAR") field)
(equal (,count (,clear ,stobj))
0)))))
(local
(progn
(defstobj bigstobj
(bigarray :type (array (unsigned-byte 16) (100))
:initially 0)
(bighash :type (hash-table eql))
(slowhash :type (hash-table equal))
)
(make-event
(let* ((bigstobj (bighash-put 0 0 bigstobj))
(bigstobj (slowhash-put (list 0) 0 bigstobj)))
(mv nil '(value-triple :invisible) state bigstobj)))
(include-book "misc/assert" :dir :system)
(assert! (equal (bighash-get 0 bigstobj) 0))
(assert! (equal (slowhash-get '(0) bigstobj) 0))
(defun init-stuff (n bigstobj state)
(declare (xargs :stobjs (bigstobj state)
:verify-guards nil
:guard (natp n)))
(if (zp n)
(mv bigstobj state)
(mv-let (rnd state) (random$ 10000 state)
(let* ((bigstobj (update-bigarrayi n rnd bigstobj))
(bigstobj (bighash-put n rnd bigstobj))
(bigstobj (slowhash-put (list n) rnd bigstobj)))
(init-stuff (- n 1) bigstobj state)))))
(make-event
(mv-let (bigstobj state)
(init-stuff 99 bigstobj state)
(mv nil '(value-triple :invisible) state bigstobj)))
(assert! (equal (bighash-count bigstobj) 100))
(assert! (equal (slowhash-count bigstobj) 100))
(make-event
(let* ((bigstobj (slowhash-put (cons 1 2) 3 bigstobj))
(bigstobj (slowhash-put (cons 1 2) 4 bigstobj)))
(mv nil '(value-triple :invisible) state bigstobj)))
(assert! (equal (slowhash-get (cons 1 2) bigstobj) 4))
(assert! (equal (slowhash-count bigstobj) 101))
(defun check-same (n bigstobj)
(declare (xargs :stobjs (bigstobj)
:verify-guards nil
:guard (natp n)))
(if (zp n)
t
(let ((expect (bigarrayi n bigstobj)))
(and (equal (bighash-get n bigstobj) expect)
(equal (slowhash-get (list n) bigstobj) expect)
(equal (bighash-boundp n bigstobj) t)
(equal (slowhash-boundp (list n) bigstobj) t)
(equal (mv-list 2 (bighash-get? n bigstobj)) (list expect t))
(equal (mv-list 2 (slowhash-get? (list n) bigstobj)) (list expect
t))
(check-same (- n 1) bigstobj)))))
(assert! (check-same 99 bigstobj))
(assert! (not (bighash-boundp 101 bigstobj)))
(assert! (equal (mv-list 2 (bighash-get? 101 bigstobj)) (list nil nil)))
(assert! (not (slowhash-boundp 101 bigstobj)))
(assert! (equal (mv-list 2 (slowhash-get? 101 bigstobj)) (list nil nil)))
(assert! (not (slowhash-boundp (list 101) bigstobj)))
(assert! (equal (mv-list 2 (slowhash-get? (list 101) bigstobj)) (list nil nil)))
(make-event
(let* ((bigstobj (bighash-rem 3 bigstobj))
(bigstobj (slowhash-rem (list 3) bigstobj)))
(mv nil '(value-triple :invisible) state bigstobj)))
(assert! (not (bighash-boundp 3 bigstobj)))
(assert! (not (slowhash-boundp (list 3) bigstobj)))
(assert! (equal (slowhash-count bigstobj) 100))
(assert! (equal (bighash-count bigstobj) 99))
(make-event
(let* ((bigstobj (slowhash-clear bigstobj))
(bigstobj (bighash-init 10000 nil nil bigstobj)))
(mv nil '(value-triple :invisible) state bigstobj)))
(assert! (equal (bighash-count bigstobj) 0))
(assert! (equal (slowhash-count bigstobj) 0))
(assert! (equal (bighash-get 5 bigstobj) nil))
(assert! (equal (slowhash-get (list 5) bigstobj) nil))
))
|