File: sbitsets.lisp

package info (click to toggle)
acl2 7.2dfsg-3
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 198,968 kB
  • ctags: 182,300
  • sloc: lisp: 2,415,261; ansic: 5,675; perl: 5,577; xml: 3,576; sh: 3,255; cpp: 2,835; makefile: 2,440; ruby: 2,402; python: 778; ml: 763; yacc: 709; csh: 355; php: 171; lex: 162; tcl: 44; java: 24; asm: 23; haskell: 17
file content (2111 lines) | stat: -rw-r--r-- 79,507 bytes parent folder | download | duplicates (2)
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
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
; Centaur Bitops Library
; Copyright (C) 2010-2011 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/
;
; License: (An MIT/X11-style license)
;
;   Permission is hereby granted, free of charge, to any person obtaining a
;   copy of this software and associated documentation files (the "Software"),
;   to deal in the Software without restriction, including without limitation
;   the rights to use, copy, modify, merge, publish, distribute, sublicense,
;   and/or sell copies of the Software, and to permit persons to whom the
;   Software is furnished to do so, subject to the following conditions:
;
;   The above copyright notice and this permission notice shall be included in
;   all copies or substantial portions of the Software.
;
;   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
;   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
;   DEALINGS IN THE SOFTWARE.
;
; Original author: Jared Davis <jared@centtech.com>

(in-package "BITSETS")
(include-book "bits-between")
(local (include-book "centaur/bitops/equal-by-logbitp" :dir :system))
(local (include-book "centaur/bitops/ihs-extensions" :dir :system))
(local (include-book "centaur/misc/equal-sets" :dir :system))
(local (include-book "std/lists/revappend" :dir :system))
(local (include-book "std/lists/rev" :dir :system))
(local (include-book "std/lists/append" :dir :system))
(local (include-book "ihs/quotient-remainder-lemmas" :dir :system))
(local (include-book "misc/assert" :dir :system))

(local (in-theory (acl2::enable* set::definitions set::expensive-rules
                                 bitops::ash-1-removal)))

(defxdoc sbitsets
  :parents (std/bitsets)
  :short "Sparse bitsets library: an alternative to @(see bitsets) for
representing finite sets of natural numbers."

  :long "<h3>Introduction</h3>

<p>In the ordinary @(see bitsets) library, sets of natural numbers are encoded
as bignums.  This is perfectly fine when the set elements are relatively small,
but it is not an efficient way to deal with sets of large numbers.  For
instance, trying to insert a number like 2^30 into an ordinary bitset will
probably kill your Lisp.</p>

<p>Sparse bitsets are a more forgiving alternative.  They can handle very large
set elements, but still achieve bitset-like efficiencies in many cases.</p>

<p>Sparse bitsets are represented as ordered lists of @('(offset . block)')
pairs.  Loosely speaking, each such pair in the set @('X') means that:</p>

@({
  offset*blocksize + 0             \\in X  iff  (logbitp 0 block)
  offset*blocksize + 1             \\in X  iff  (logbitp 1 block)
    ...
  offset*blocksize + (blocksize-1) \\in X  iff  (logbitp (blocksize-1) block)
})

<p>The @('blocksize') is a some constant determined by @(see
*sbitset-block-size*).  We'll assume it is 60 (which makes each @('block') a
fixnum on 64-bit CCL).</p>

<h4>Efficiency Considerations</h4>

<p>Sparse bitsets are essentially a hybrid of ordered sets and bitsets, and
their efficiency characteristics are very data dependent.</p>

<p>Sparse bitsets perform best on sets that have \"clusters\" of nearby
members, perhaps separated by wide gaps.  Here, sparse bitsets give you some of
the same benefits of bitsets, viz. word-at-a-time operations like union and the
space efficiency of using bit masks to represent the set members.</p>

<p>For completely dense sets, e.g., all integers in @('[0, 1000]'), sparse
bitsets are:</p>

<ul>

<li>Better than ordered sets, because they get to compress nearby elements into
 bitmasks and use word-at-a-time operations.</li>

<li>Somewhat worse than ordinary bitsets.  They are less space efficient due to
 the conses and offset numbers.  They are also more expensive for membership
 testing: regular bitsets can basically use O(1) array indexing via @(see
 logbitp), while sparse bitsets must use an O(n) search for the right
 pair.</li>

</ul>

<p>For \"cluster-free\" sets where the elements are so far apart that they
never fall into the same block, sparse bitsets are:</p>

<ul>

<li>Better than bitsets, which perform badly here since they can have large
segments of 0 bits that waste space and take time to process during set
operations.</li>

<li>Somewhat worse than ordered sets.  For instance, a set like {0, 60, 120}
would be encoded as @('((0 . 1) (1 . 1) (2 . 1))'), which is similar to its
ordered set representation except for the additional overhead of using @('(1
. 1)') to represent 60, etc.</li>

</ul>


<h3>Sparse Bitset Operations</h3>

<p>Convention:</p>

<ul>
<li>@('a, b, ...') denote set elements</li>
<li>@('X, Y, ...') denote sets.</li>
</ul>

<p>Valid sparse bitsets are recognized by @(see sbitsetp), and there is a
standard fixing function, @(see sbitset-fix).</p>

<p>We intend to eventually implement sparse analogues for all of the functions
in the @(see bitsets) library, and analogues for the bitset @(see reasoning)
techniques.  For now we have only finished a few of these operations:</p>

<h5>Sparse Bitset Construtors</h5>

<dl>

<dt>@('(sbitset-singleton a)')</dt>
<dd>Constructs the set @('{a}')</dd>

<dt>@('(sbitset-union X Y ...)')</dt>
<dd>Constructs the set @('X U Y U ...')</dd>

<dt>@('(sbitset-intersect X Y ...)')</dt>
<dd>Constructs the set @('X \\intersect Y \\intersect ...')</dd>

<dt>@('(sbitset-difference X Y)')</dt>
<dd>Constructs the set @('X - Y')</dd>

</dl>

<h5>Inspecting Sparse Bitsets</h5>

<dl>
<dt>Todo</dt>
</dl>

<h5>Enumerating Sparse Bitset Members</h5>

<dl>
<dt>@('(sbitset-members X)')</dt>
<dd>Constructs an ordinary ordered set with the elements of X.</dd>
<dd>Important in @(see reasoning) about sparse bitsets.</dd>
</dl>")

(defsection *sbitset-block-size*
  :parents (sbitset-pairp)
  :short "Size of each block in an sparse bitset."

  :long "<p>The block size can be any positive number, but for good performance
you probably want to use the maximal number such that:</p>

@({
 (fixnump (1- (expt 2 *sbitset-block-size*))) = T
})

<p>On CCL and SBCL, 60 is the right number.  For other Lisps, 60 is logically
fine, but other numbers may give you better performance.</p>

<p>Note: if you change this, you may wish to also write a corresponding,
optimized version of @(see sbitset-pair-members).</p>"

  (defconst *sbitset-block-size* 60))

(local (defthm floor-is-nonnegative-integer-quotient
         (implies (and (natp a) (posp b))
                  (equal (floor a b)
                         (nonnegative-integer-quotient a b)))))

(local (in-theory (disable truncate)))

(local (defthm truncate-is-nonnegative-integer-quotient
         (implies (and (natp a) (posp b))
                  (equal (truncate a b)
                         (nonnegative-integer-quotient a b)))))


(local
 (encapsulate
   ()
   (local (defun niq-ind (a b c)
            (declare (xargs :measure (nonnegative-integer-quotient c a)))
            (if (or (= (nfix a) 0) (< (ifix c) a))
                b
              (niq-ind a (- b 1) (- c a)))))

   (defthm niq-lemma
     (implies (and (posp a)
                   (natp b)
                   (natp c))
              (equal (equal (nonnegative-integer-quotient c a)
                            b)
                     (and (<= (* a b) c)
                          (< c (+ a (* a b))))))
     :hints (("goal" :induct (niq-ind a b c))
             (and stable-under-simplificationp
                  '(:nonlinearp t))))

   (local (include-book "arithmetic-3/bind-free/top" :dir :system))
   (local (in-theory (disable acl2::cancel-mod-+-basic)))

   (local (defthm l0
            (implies (and (integerp a)
                          (natp size)
                          (<= (* size offset) a)
                          (< a (+ size (* size offset)))
                          (natp offset))
                     (equal (+ a (* (- size) offset))
                            (mod a size)))
            :hints(("Goal" :in-theory (enable mod)
                    :do-not-induct t))))

   (make-event
    `(defthm mod-blocksize-nonsense
       (implies (and (integerp a)
                     (<= (* ,*sbitset-block-size* offset) a)
                     (< a (+ ,*sbitset-block-size* (* ,*sbitset-block-size* offset)))
                     (natp offset))
                (equal (+ a (* ,(- *sbitset-block-size*) offset))
                       (mod a ,*sbitset-block-size*)))))))



(local (in-theory (disable bitops::logbitp-when-too-small
                           ;expt-between-one-and-two
                           ;; arith-3 acl2::expt-is-increasing-for-base->-1
                           bitops::expt-2-monotonic
                           acl2::associativity-of-append)))

(local (defthm integer-listp-of-append
         (implies (and (integer-listp x)
                       (integer-listp y))
                  (integer-listp (append x y)))))

(local (defthm rassoc-append
         (equal (append x (append y z))
                (append (append x y) z))))

(local (defun dec-induct (n)
         (if (zp n)
             n
           (dec-induct (1- n)))))

(local (defthm integerp-of-car-when-integer-listp
         (implies (integer-listp x)
                  (equal (integerp (car x))
                         (consp x)))))

(local (defthmd member-equal-is-in
         (implies (set::setp x)
                  (iff (member-equal a x)
                       (set::in a x)))
         :hints(("Goal" :in-theory (enable set::in-to-member)))))

(local
 (make-event
  `(defthm expt-2-monotonic-specialized-for-block-size
     (implies (and (< a ,*sbitset-block-size*)
                   (natp a))
              (< (expt 2 a) ,(expt 2 *sbitset-block-size*)))
     :hints(("Goal" :use ((:instance bitops::expt-2-monotonic
                                     (a a)
                                     (b ,*sbitset-block-size*))))))))


(local
 (defsection add-to-each

   (defthm consp-of-add-to-each
     (equal (consp (add-to-each offset x))
            (consp x))
     :hints(("Goal" :in-theory (enable add-to-each))))

   (defthm integer-listp-of-add-to-each
     (implies (and (integerp offset)
                   (integer-listp x))
              (integer-listp (add-to-each offset x)))
     :hints(("Goal" :in-theory (enable add-to-each))))

   (local (defthm l0
            (implies (and (member-equal a (add-to-each offset x))
                          (integerp offset)
                          (integer-listp x))
                     (integerp a))))

   (defthm member-equal-add-to-each
     (implies (and (force (integerp offset))
                   (force (integer-listp x)))
              (iff (member-equal a (add-to-each offset x))
                   (and (integerp a)
                        (member-equal (- a offset) x)))))

   (defthm no-duplicatesp-equal-of-add-to-each
     (implies (and (no-duplicatesp-equal x)
                   (force (integer-listp x))
                   (force (integerp offset)))
              (no-duplicatesp-equal (add-to-each offset x))))

   (local (defthm l1
            (implies (and (integerp a)
                          (integerp b))
                     (equal (set::<< a b)
                            (< a b)))
            :hints(("Goal" :in-theory (enable set::<< lexorder alphorder)))))

   (defthm setp-of-add-to-each
     (implies (and (integerp offset)
                   (integer-listp x)
                   (set::setp x))
              (set::setp (add-to-each offset x)))
     :hints(("Goal" :in-theory (enable* (:ruleset set::primitive-rules)
                                        add-to-each))))))



(local
 (defsection no-duplicatesp-equal-when-setp

   (local (defthm l0
            (implies (and (member-equal a x)
                          (set::<< a (car x)))
                     (not (set::setp x)))
            :hints(("Goal"
                    :induct (len x)
                    :in-theory (enable* (:ruleset set::primitive-rules)
                                        (:ruleset set::order-rules))))))

   (defthm no-duplicatesp-equal-when-setp
     (implies (set::setp x)
              (no-duplicatesp-equal x))
     :hints(("Goal" :in-theory (enable* (:ruleset set::primitive-rules)))))))


(local
 (defsection append-is-union

   (defun max-int (x)
     (cond ((atom x)
            nil)
           ((atom (cdr x))
            (car x))
           (t
            (max (car x)
                 (max-int (cdr x))))))

   (defun min-int (x)
     (cond ((atom x)
            nil)
           ((atom (cdr x))
            (car x))
           (t
            (min (car x)
                 (min-int (cdr x))))))

   (local (defthm l1
            (implies (and (case-split (integerp a))
                          (case-split (integerp b)))
                     (equal (set::<< a b)
                            (< a b)))
            :hints(("Goal" :in-theory (enable set::<< lexorder alphorder)))))

   (local (defthm l2
            (implies (and (integer-listp x)
                          (set::setp x))
                     (equal (min-int x)
                            (if (consp x)
                                (car x)
                              nil)))
            :hints(("Goal" :in-theory (enable* (:ruleset set::primitive-rules))))))

   (defthmd setp-of-append
     (implies (and (force (or (< (max-int x) (min-int y))
                              (atom x)
                              (atom y)))
                   (force (integer-listp x))
                   (force (integer-listp y))
                   (force (set::setp x))
                   (force (set::setp y)))
              (set::setp (append x y)))
     :hints(("Goal" :in-theory (enable* (:ruleset set::primitive-rules)))))

   (local (in-theory (enable setp-of-append)))

   (local (defthm in-of-append
            (implies (and (force (or (< (max-int x) (min-int y))
                                     (atom x)
                                     (atom y)))
                          (force (integer-listp x))
                          (force (integer-listp y))
                          (force (set::setp x))
                          (force (set::setp y)))
                     (equal (set::in a (append x y))
                            (or (set::in a x)
                                (set::in a y))))
            :hints(("goal"
                    :use ((:instance member-equal-is-in (x (append x y)))
                          (:instance member-equal-is-in (x x))
                          (:instance member-equal-is-in (x y)))))))

   (defthmd append-is-union
     (implies (and (force (or (< (max-int x) (min-int y))
                              (atom x)
                              (atom y)))
                   (force (integer-listp x))
                   (force (integer-listp y))
                   (force (set::setp x))
                   (force (set::setp y)))
              (equal (append x y)
                     (set::union x y))))))



(local
 (defsection consp-of-bits-between

   (local (defthm l1
            (implies (posp x)
                     (logbitp (logbitp-mismatch x 0) x))
            :hints(("Goal"
                    :in-theory (disable acl2::logbitp-mismatch-correct)
                    :use ((:instance acl2::logbitp-mismatch-correct
                                     (a x)
                                     (b 0)))))))

   (local (defthm l2
            (implies (posp x)
                     (< (logbitp-mismatch x 0) (integer-length x)))
            :rule-classes ((:rewrite) (:linear))))

   (local (defthm l3
            (implies (and (< x (expt 2 n))
                          (posp x)
                          (natp n))
                     (<= (logbitp-mismatch x 0)
                         n))
            :rule-classes ((:rewrite) (:linear))
            :hints(("Goal"
                    :in-theory (disable bitops::integer-length-bounded-by-expt)
                    :use ((:instance bitops::integer-length-bounded-by-expt (a x)))))))

   (local (defthm l4
            (implies (posp x)
                     (natp (logbitp-mismatch x 0)))
            :hints(("Goal" :in-theory (enable natp)))
            :rule-classes ((:rewrite) (:type-prescription))))

   (defthm consp-of-bits-between
     (implies (and (< block (expt 2 n))
                   (posp block)
                   (natp n))
              (consp (bits-between 0 n block)))
     :hints(("Goal"
             :in-theory (disable member-equal-of-bits-between)
             :use ((:instance member-equal-of-bits-between
                              (a (logbitp-mismatch block 0))
                              (n 0)
                              (x block)
                              (m n))))))))



(defmacro the-sbitset-block (x)
  `(the (unsigned-byte ,*sbitset-block-size*) ,x))

(make-event
 `(define sbitset-blockp (x)
    :returns (bool booleanp :rule-classes :type-prescription)
    :parents (sbitset-pairp)
    :short "@(call sbitset-blockp) recognizes numbers that are valid as the
<i>block</i> component of an @(see sbitset-pairp)."

    :long "<p>To ensure that sparse bitsets are canonical, we don't allow 0 as
a valid block.  The idea is that any block whose value is 0 should just be
omitted from the set.</p>"
    :inline t
    (mbe :logic (and (posp x)
                     (< x (expt 2 *sbitset-block-size*)))
         :exec (and (integerp x)
                    (<= 1 x)
                    (<= x ,(1- (expt 2 *sbitset-block-size*)))))
    ///
    (defthm sbitset-block-type
      (implies (sbitset-blockp x)
               (posp x))
      :rule-classes :compound-recognizer)

    (defthm sbitset-block-upper-bound
      ;; BOZO should this be linear, too?
      (implies (sbitset-blockp x)
               (< x (expt 2 *sbitset-block-size*))))))


(define sbitset-pairp (x)
  :parents (sbitsets)
  :returns (pairp booleanp :rule-classes :type-prescription)
  :short "@(call sbitset-pairp) recognizes a valid @('(offset . block)') pair
for sparse bitsets."
  :inline t
  (and (consp x)
       (natp (car x))
       (sbitset-blockp (cdr x)))
  ///
  (defthm consp-when-sbitset-pairp
    (implies (sbitset-pairp x)
             (consp x))
    :rule-classes :compound-recognizer))

(define sbitset-pair ((offset natp :type unsigned-byte)
                      (block sbitset-blockp))
  :returns pair
  :parents (sbitset-pairp)
  :short "@(call sbitset-pair) constructs a @('(offset . block)') pair."
  :inline t
  (cons offset block))

(define sbitset-pair-offset ((x sbitset-pairp))
  :parents (sbitset-pairp)
  :returns offset
  :short "@(call sbitset-pair-offset) gets the @('offset') from an @('(offset
  . block)') pair."
  :inline t
  (car x))

(define sbitset-pair-block ((x sbitset-pairp))
  :parents (sbitset-pairp)
  :returns block
  :short "@(call sbitset-pair-block) gets the @('block') from an @('(offset
  . block)') pair."
  :inline t
  (cdr x))

(defsection sbitset-pairp-basics
  :extension sbitset-pairp
  (local (in-theory (enable sbitset-pairp
                            sbitset-pair
                            sbitset-pair-offset
                            sbitset-pair-block)))

  (defthm sbitset-pair-offset-of-sbitset-pair
    (equal (sbitset-pair-offset (sbitset-pair offset block))
           offset))

  (defthm sbitset-pair-block-of-sbitset-pair
    (equal (sbitset-pair-block (sbitset-pair offset block))
           block))

  (defthm sbitset-pair-offset-type
    (implies (force (sbitset-pairp x))
             (natp (sbitset-pair-offset x)))
    :rule-classes :type-prescription)

  (defthm sbitset-pair-block-type
    (implies (force (sbitset-pairp x))
             (and (integerp (sbitset-pair-block x))
                  (< 0 (sbitset-pair-block x))))
    :rule-classes :type-prescription)

  (defthm sbitset-pair-block-upper-bound
    (implies (force (sbitset-pairp x))
             (< (sbitset-pair-block x) (expt 2 *sbitset-block-size*)))
    :rule-classes ((:rewrite) (:linear))
    :hints(("Goal" :use ((:instance sbitset-block-upper-bound
                                    (x (cdr x)))))))

  (defthm sbitset-pairp-of-sbitset-pair
    (implies (and (force (natp offset))
                  (force (sbitset-blockp block)))
             (sbitset-pairp (sbitset-pair offset block))))

  (defthm sbitset-blockp-of-sbitset-pair-block
    (implies (force (sbitset-pairp x))
             (sbitset-blockp (sbitset-pair-block x)))))



(define sbitset-pair-members ((x sbitset-pairp))
  :returns set
  :parents (sbitset-members sbitset-pairp)
  :short "@(call sbitset-pair-members) extracts the members of a single @(see
sbitset-pairp)."

  :long "<p>For instance, if the pair is @('(0 . 7)'), we produce the set
@('{0, 1, 2}'); if the set is @('(1 . 7)'), we produce @('{60, 61, 62}').</p>

<p>This serves as the logical definition we use to reason about member
extraction.</p>

<p>Normally this function isn't executed.  When the block size is 32 or 60, we
can instead use the optimized @(see bits-0-31) or @(see 60bits-0-59) routines
instead.  But, if you change the block size to something else, this function
<b>will</b> be executed and its performance will probably be bad.  In this
case, you might want to write a custom @('bits-0-32') style routine for your
block size.</p>"

  (add-to-each (* (sbitset-pair-offset x) *sbitset-block-size*)
               (bits-between 0 *sbitset-block-size* (sbitset-pair-block x)))

  ///

  (defthm sbitset-pair-members-of-nil
    (equal (sbitset-pair-members nil)
           nil))

  (defthm true-listp-of-sbitset-pair-members
    ;; Under the guard it always produces a consp as well, but it seems nice to
    ;; prove this unconditionally, separately.
    (true-listp (sbitset-pair-members x))
    :rule-classes :type-prescription)

  (defthm integer-listp-of-sbitset-pair-members
    (implies (force (sbitset-pairp x))
             (integer-listp (sbitset-pair-members x))))

  (local (include-book "arithmetic-3/bind-free/top" :dir :system))

  (defthm member-equal-sbitset-pair-members
    (implies (force (sbitset-pairp x))
             (iff (member-equal a (sbitset-pair-members x))
                  (and (natp a)
                       (equal (floor a *sbitset-block-size*)
                              (sbitset-pair-offset x))
                       (logbitp (mod a *sbitset-block-size*)
                                (sbitset-pair-block x))))))

  (defthm no-duplicatesp-equal-of-sbitset-pair-members
    (implies (force (sbitset-pairp x))
             (no-duplicatesp-equal (sbitset-pair-members x))))

  (defthm offsets-must-agree-when-sbitset-pair-members-intersect
    (implies (and (force (sbitset-pairp x))
                  (force (sbitset-pairp y))
                  (intersectp-equal (sbitset-pair-members x)
                                    (sbitset-pair-members y))
                  (syntaxp (term-order y x)))
             (equal (sbitset-pair-offset x)
                    (sbitset-pair-offset y)))
    :hints((acl2::set-reasoning)))

  (defthm consp-of-sbitset-pair-members
    (implies (force (sbitset-pairp x))
             (and (true-listp (sbitset-pair-members x))
                  (consp (sbitset-pair-members x))))
    :rule-classes :type-prescription)

  (defthm setp-of-sbitset-pair-members
    (implies (force (sbitset-pairp a))
             (set::setp (sbitset-pair-members a))))

  (defthm in-of-sbitset-pair-members
    (implies (force (sbitset-pairp x))
             (equal (set::in a (sbitset-pair-members x))
                    (and (natp a)
                         (equal (floor a *sbitset-block-size*)
                                (sbitset-pair-offset x))
                         (logbitp (mod a *sbitset-block-size*)
                                  (sbitset-pair-block x)))))
    :hints(("Goal" :use ((:instance member-equal-is-in
                                    (x (sbitset-pair-members x)))))))

  (defthm empty-of-sbitset-pair-members
    (implies (force (sbitset-pairp x))
             (not (set::empty (sbitset-pair-members x))))
    :hints(("Goal" :in-theory (e/d* ((:ruleset set::primitive-rules))
                                    (sbitset-pair-members))))))


(local
 (defsection sbitset-pair-members-bounds

  (local (in-theory (enable sbitset-pair-members)))

  (local (defthm max-int-of-add-to-each
           (implies (and (force (integerp offset))
                         (force (integer-listp x)))
                    (equal (max-int (add-to-each offset x))
                           (if (consp x)
                               (+ offset (max-int x))
                             nil)))
           :hints(("Goal"
                   :induct (add-to-each offset x)
                   :in-theory (enable add-to-each max-int)))))

  (local (defthm min-int-of-add-to-each
           (implies (and (force (integerp offset))
                         (force (integer-listp x)))
                    (equal (min-int (add-to-each offset x))
                           (if (consp x)
                               (+ offset (min-int x))
                             nil)))
           :hints(("Goal"
                   :induct (add-to-each offset x)
                   :in-theory (enable add-to-each min-int)))))

  (local (defthm max-int-of-bits-between
           (implies (and (consp (bits-between n m x))
                         (force (natp n))
                         (force (natp m))
                         (force (<= n m)))
                    (< (max-int (bits-between n m x))
                       m))
           :rule-classes ((:rewrite) (:linear))
           :hints(("Goal"
                   :induct (bits-between n m x)
                   :in-theory (enable bits-between)))))

  (local (defthm min-int-of-bits-between
           (implies (and (consp (bits-between n m x))
                         (force (natp n))
                         (force (natp m))
                         (force (<= n m)))
                    (<= n (min-int (bits-between n m x))))
           :rule-classes ((:rewrite) (:linear))
           :hints(("Goal"
                   :induct (bits-between n m x)
                   :in-theory (enable bits-between)))))

  (defthm sbitset-pair-members-upper-bound
    (implies (force (sbitset-pairp x))
             (< (max-int (sbitset-pair-members x))
                (* *sbitset-block-size* (+ (sbitset-pair-offset x) 1))))
    :rule-classes ((:rewrite) (:linear)))

  (defthm sbitset-pair-members-lower-bound
    (implies (force (sbitset-pairp x))
             (<= (* *sbitset-block-size* (sbitset-pair-offset x))
                 (min-int (sbitset-pair-members x))))
    :rule-classes ((:rewrite) (:linear)))))


(define sbitsetp (x)
  :returns bool
  :parents (sbitsets)
  :short "@(call sbitsetp) determines whether @('X') is a well-formed sparse
bitset."

  (if (atom x)
      (not x)
    (and (sbitset-pairp (first x))
         (if (atom (cdr x))
             (not (cdr x))
           (and (sbitset-pairp (second x))
                (< (sbitset-pair-offset (first x))
                   (sbitset-pair-offset (second x)))))
         (sbitsetp (cdr x))))
  ///

  (defthm true-listp-when-sbitsetp
    (implies (sbitsetp x)
             (true-listp x))
    :rule-classes :compound-recognizer)

  (defthm sbitsetp-when-atom
    (implies (atom x)
             (equal (sbitsetp x)
                    (not x))))

  (defthm sbitsetp-of-cons
    (equal (sbitsetp (cons a x))
           (and (sbitset-pairp a)
                (sbitsetp x)
                (or (atom x)
                    (< (sbitset-pair-offset a)
                       (sbitset-pair-offset (first x)))))))

  (defthm sbitset-pairp-of-car
    (implies (sbitsetp x)
             (equal (sbitset-pairp (car x))
                    (consp x))))

  (defthm sbitsetp-of-cdr
    (implies (sbitsetp x)
             (sbitsetp (cdr x))))

  (defthm sbitset-pairp-when-member-equal-of-sbitsetp
    (implies (and (member-equal a x)
                  (sbitsetp x))
             (sbitset-pairp a))
    :rule-classes ((:rewrite)
                   (:rewrite :corollary
                             (implies (and (sbitsetp x)
                                           (member-equal a x))
                                      (sbitset-pairp a))))))


(define sbitset-fix ((x sbitsetp))
  :parents (sbitsets)
  :short "@(call sbitset-fix) is a basic fixing function for sparse bitsets."
  :inline t
  (mbe :logic (if (sbitsetp x) x nil)
       :exec x)
  ///
  (defthm true-listp-of-sbitset-fix
    (true-listp (sbitset-fix x))
    :rule-classes :type-prescription)

  (defthm sbitsetp-of-sbitset-fix
    (sbitsetp (sbitset-fix x)))

  (defthm sbitset-fix-when-sbitsetp
    (implies (sbitsetp x)
             (equal (sbitset-fix x)
                    x))))



; We optimize the definition of sbitset-members if the block-size is 60 or 32
; bits.

(defsection sbitset-members
  :parents (sbitsets)
  :short "@(call sbitset-members) converts a bitset into an ordinary, ordered
set."

  :long "<p>In the logic, we just call @(see sbitset-pair-members) on each
block to get the members.</p>

<p>However, this function should typically be left disabled and you should not
reason about the underlying representation of sparse bitsets.</p>

<p>For faster execution, when the block size is 32 or 60 we use a partially
unrolled inner loop for extracting the bits from each block.  This optimization
is not performed for other block sizes.</p>"

  (make-event
   (b* ((fn (case *sbitset-block-size*
              (60 '60bits-0-59)
              (32 'bits-0-31)
              (otherwise nil)))
        (exec-def
         (if fn
             `(defun sbitset-members-exec (x acc)
                (declare (xargs :guard (sbitsetp x)))
                (if (atom x)
                    acc
                  (let* ((pair1   (car x))
                         (offset1 (sbitset-pair-offset pair1))
                         (block1  (sbitset-pair-block pair1)))
                    ;; BOZO should probably use ash 5 for 32-bit case
                    (,fn (* offset1 ,*sbitset-block-size*)
                         block1
                         (sbitset-members-exec (cdr x) acc)))))
           `(value-triple :invisible)))
        (logic-body
         `(if (not (sbitsetp x))
              nil
            (if (atom x)
                nil
              (append (sbitset-pair-members (car x))
                      (sbitset-members-aux (cdr x))))))
        (real-body
         (if fn
             `(mbe :logic ,logic-body
                   :exec (sbitset-members-exec x nil))
           logic-body))
        (remove-thm
         (if fn
             `(defthm sbitset-members-exec-removal
                (implies (force (sbitsetp x))
                         (equal (sbitset-members-exec x acc)
                                (append (sbitset-members-aux x) acc)))
                :hints(("Goal" :in-theory (enable sbitset-pair-members))))
           `(value-triple :invisible))))
     `(encapsulate
        ()
        ,exec-def
        (acl2::defund-inline sbitset-members-aux (x)
          (declare (xargs :guard (sbitsetp x)
                          :verify-guards nil))
          ,real-body)
        (local (in-theory (enable sbitset-members-aux)))
        ,remove-thm
        (verify-guards sbitset-members-aux$inline))))

  (define sbitset-members ((x sbitsetp))
    :inline t
    :verify-guards nil
    (mbe :logic (if (or (not (sbitsetp x))
                        (atom x))
                    nil
                  (set::union (sbitset-pair-members (car x))
                               (sbitset-members (cdr x))))
         :exec (sbitset-members-exec x nil)))

  (local (in-theory (enable sbitset-members sbitset-members-aux)))

  (defsection guard-verification

    (local (defthm l0
             (integer-listp (sbitset-members-aux x))))

    (local (defthm l1
             (equal (consp (sbitset-members-aux x))
                    (and (sbitsetp x)
                         (consp x)))))

    (local (defthm l2
             (implies (set::setp (append x y))
                      (and (set::setp (acl2::list-fix x))
                           (set::setp y)))
             :hints(("Goal" :in-theory (enable* (:ruleset set::primitive-rules))))))

    (local (defthm l3
             (equal (min-int (append x y))
                    (if (consp x)
                        (if (consp y)
                            (min (min-int x) (min-int y))
                          (min-int x))
                      (if (consp y)
                          (min-int y)
                        nil)))
             :hints(("Goal" :in-theory (enable min-int append)))))

    (local (defthm l4
             (set::setp (sbitset-members-aux x))
             :hints(("Goal" :in-theory (enable setp-of-append)))))

    (local (defthm l5
             (equal (sbitset-members-aux x)
                    (if (or (atom x)
                            (not (sbitsetp x)))
                        nil
                      (set::union (sbitset-pair-members (car x))
                                   (sbitset-members-aux (cdr x)))))
             :rule-classes :definition
             :hints(("Goal" :in-theory (e/d (append-is-union)
                                            (set::double-containment))))))

    (defthm sbitset-members-aux-removal
      (equal (sbitset-members-aux x)
             (sbitset-members x)))

    (verify-guards+ sbitset-members))


  (defthm sbitset-members-default
    (implies (not (sbitsetp a))
             (equal (sbitset-members a)
                    nil)))

  (defthm sbitset-members-when-atom
    (implies (atom x)
             (equal (sbitset-members x)
                    nil)))

  (local (defthm l0
           (implies (atom x)
                    (equal (set::setp x)
                           (not x)))
           :hints(("Goal" :in-theory (enable set::setp)))))

  (local (defthm union-under-iff
           (iff (set::union x y)
                (or (not (set::empty x))
                    (not (set::empty y))))
           :hints(("Goal" :in-theory (enable* (:ruleset set::primitive-rules))))))

  (defthm sbitset-members-of-cons
    (implies (force (sbitsetp (cons a x)))
             (equal (sbitset-members (cons a x))
                    (set::union (sbitset-pair-members a)
                                 (sbitset-members x)))))

  (defthm sbitset-members-of-sbitset-fix
    (equal (sbitset-members (sbitset-fix b))
           (sbitset-members b))
    :hints(("Goal" :in-theory (enable sbitset-fix))))

  (defthm true-listp-of-sbitset-members
    (true-listp (sbitset-members x))
    :rule-classes :type-prescription)

  (defthm setp-of-sbitset-members
    (set::setp (sbitset-members x)))

  (defthm no-duplicatesp-equal-of-sbitset-members
    (no-duplicatesp-equal (sbitset-members x))))



(define sbitset-find ((offset natp)
                      (x      sbitsetp))
  :parents (in-of-sbitset-members)
  :short "Helper definition; shouldn't be needed by users."
  (cond ((atom x)
         nil)
        ((equal (sbitset-pair-offset (car x)) offset)
         (car x))
        (t
         (sbitset-find offset (cdr x))))
  ///
  (defthm sbitset-find-when-atom
    (implies (atom x)
             (equal (sbitset-find offset x)
                    nil)))

  (defthm sbitset-find-of-cons
    (equal (sbitset-find offset (cons a x))
           (if (equal (sbitset-pair-offset a) offset)
               a
             (sbitset-find offset x))))

  (defthm member-equal-of-sbitset-find
    (implies (sbitset-find offset x)
             (member-equal (sbitset-find offset x) x)))

  (defthm sbitset-pairp-of-sbitset-find
    (implies (force (sbitsetp x))
             (equal (sbitset-pairp (sbitset-find offset x))
                    (if (sbitset-find offset x)
                        t
                      nil))))

  (defthm sbitset-pair-offset-of-sbitset-find
    (implies (sbitsetp x)
             (equal (sbitset-pair-offset (sbitset-find offset x))
                    (if (sbitset-find offset x)
                        offset
                      nil))))

  (defthm natp-when-sbitset-find
    (implies (and (sbitset-find offset x)
                  (sbitsetp x))
             (natp offset))
    :rule-classes (:forward-chaining)))



(defsection in-of-sbitset-members
  :parents (sbitset-members)
  :short "Low-level lemma to rewrite @('(in a (sbitset-members x))') into a
more structural form."

  :long "<p>This theorem is disabled by default and most users shouldn't enable
it.  It's only needed to prove properties about the basic sbitset
constructors.</p>

<p>This is a little more complicated than in the basic bitset library, because
we have to go to the proper pair.</p>"

  (local (defthm l0
           (implies (and (sbitsetp x)
                         (set::in a (sbitset-members x)))
                    (sbitset-find (floor a *sbitset-block-size*) x))
           :hints(("Goal"
                   :in-theory (disable mod-blocksize-nonsense)
                   :induct (len x)
                   :expand ((sbitset-members x))))))

  (local (defthm l1
           (implies (and (sbitsetp x)
                         (< offset (sbitset-pair-offset (car x))))
                    (not (sbitset-find offset x)))
           :hints(("Goal" :in-theory (enable sbitset-find)))))

  (local (defthm l2
           (implies (and (sbitset-pairp x)
                         (set::in a (sbitset-pair-members x)))
                    (equal (sbitset-pair-offset x)
                           (floor a *sbitset-block-size*)))))

  (local (defthm l3
           (implies (sbitsetp x)
                    (equal (set::in a (sbitset-members x))
                           (let ((pair (sbitset-find (floor a *sbitset-block-size*) x)))
                             (and pair
                                  (set::in a (sbitset-pair-members pair))))))
           :hints(("Goal"
                   :induct (len x)
                   :expand ((sbitset-members x)
                            (:free (n) (sbitset-find n x)))
                   :in-theory (disable in-of-sbitset-pair-members)
                   :do-not '(generalize fertilize)))))

  (defthmd in-of-sbitset-members
    (equal (set::in a (sbitset-members x))
           (and (sbitsetp x)
                (let ((pair (sbitset-find (floor a *sbitset-block-size*) x)))
                  (and pair
                       (set::in a (sbitset-pair-members pair))))))
    :hints(("Goal" :use ((:instance l3))))))


(local (in-theory (enable in-of-sbitset-members)))



; -----------------------------------------------------------------------------
;
;                           BASIC SBITSET OPERATIONS
;
; -----------------------------------------------------------------------------

(make-event
 `(define sbitset-singleton-pair ((a natp :type unsigned-byte))
    :returns (pair sbitset-pairp)
    :parents (sbitset-singleton)
    :short "@(call sbitset-singleton-pair) creates a @(see sbitset-pairp) whose
only member is @('a')."
    :guard-hints(("Goal" :in-theory (enable sbitset-blockp)))
    :split-types t
    :prepwork ((local (in-theory (enable sbitset-blockp)))
               (local (include-book "arithmetic-3/bind-free/top" :dir :system)))
    :inline t
    (mbe :logic
         (let ((a (nfix a)))
           (sbitset-pair (floor a *sbitset-block-size*)
                         (expt 2 (mod a *sbitset-block-size*))))
         :exec
         (sbitset-pair (truncate a ,*sbitset-block-size*)
                       (ash 1 (rem a ,*sbitset-block-size*))))
    ///
    (defthm sbitset-pair-offset-of-sbitset-singleton-pair
      (equal (sbitset-pair-offset (sbitset-singleton-pair a))
             (floor (nfix a) *sbitset-block-size*)))

    (defthm sbiset-pair-block-of-sbitset-singleton-pair
      (equal (sbitset-pair-block (sbitset-singleton-pair a))
             (expt 2 (mod (nfix a) *sbitset-block-size*))))))

(define sbitset-singleton ((a natp :type unsigned-byte))
  :parents (sbitsets)
  :short "@(call sbitset-singleton) constructs the singleton set @('{a}')."
  :split-types t
  :inline t
  :returns (sbitset sbitsetp)
  (list (sbitset-singleton-pair a))
  ///
  (local (include-book "arithmetic-3/bind-free/top" :dir :system))

  (local (defthm l0
           (equal (set::in a (sbitset-members (sbitset-singleton b)))
                  (equal a (nfix b)))
           :hints (("goal" :use ((:instance niq-lemma
                                  (c (nfix b)) (a 60) (b a)))
                    :in-theory (e/d (mod) (niq-lemma mod-blocksize-nonsense))))))

  (defthm sbitset-members-of-sbitset-singleton
    (equal (sbitset-members (sbitset-singleton a))
           (set::insert (nfix a) nil))
    :hints(("Goal" :in-theory (disable sbitset-singleton)))))


(local (defthm sbitset-blockp-of-logior
         (implies (and (force (sbitset-blockp x))
                       (force (sbitset-blockp y)))
                  (sbitset-blockp (logior x y)))
         :hints(("Goal"
                 :in-theory (e/d (sbitset-blockp)
                                 (bitops::upper-bound-of-logior-for-naturals))
                 :use ((:instance bitops::upper-bound-of-logior-for-naturals
                                  (x x)
                                  (y y)
                                  (n *sbitset-block-size*)))))))

(local (defthm equal-of-sbitset-pair
         (implies (force (sbitset-pairp x))
                  (equal (equal (sbitset-pair offset block) x)
                         (and (equal offset (sbitset-pair-offset x))
                              (equal block (sbitset-pair-block x)))))
         :hints(("Goal" :in-theory (enable sbitset-pair
                                           sbitset-pairp
                                           sbitset-pair-offset
                                           sbitset-pair-block)))))

(local (defthm lower-bound-of-first-offset-of-sbitset-find
         (implies (and (sbitset-find b x)
                       (<= b a)
                       (sbitsetp x))
                  (<= (sbitset-pair-offset (car x)) a))
         :rule-classes ((:rewrite) (:linear))
         :hints(("goal"
                 :in-theory (enable sbitset-find)
                 :do-not '(generalize fertilize)))))


(defsection sbitset-union
  :parents (sbitsets)
  :short "@('(sbitset-union X Y ...) constructs the set @('X U Y U ...')."
  :long "@(def sbitset-union)"

  (define sbitset-union-exec ((x sbitsetp)
                              (y sbitsetp))
    :measure (+ (len x) (len y))
    (b* (((when (atom x)) y)
         ((when (atom y)) x)
         (x1 (car x))
         (y1 (car y))
         ((the unsigned-byte x1.offset) (sbitset-pair-offset x1))
         ((the unsigned-byte y1.offset) (sbitset-pair-offset y1))
         ((when (< x1.offset y1.offset))
          (cons x1 (sbitset-union-exec (cdr x) y)))
         ((unless (eql x1.offset y1.offset))
          (cons y1 (sbitset-union-exec x (cdr y))))
         (x1.block (sbitset-pair-block x1))
         (y1.block (sbitset-pair-block y1))
         (new-block (the-sbitset-block
                     (logior (the-sbitset-block x1.block)
                             (the-sbitset-block y1.block))))
         (new-pair (sbitset-pair x1.offset new-block)))
      (cons new-pair (sbitset-union-exec (cdr x) (cdr y)))))

  (define sbitset-binary-union ((x sbitsetp)
                                (y sbitsetp))
    :inline t
    (mbe :logic (sbitset-union-exec (sbitset-fix x)
                                    (sbitset-fix y))
         :exec (sbitset-union-exec x y)))

  (defmacro sbitset-union (&rest args)
    (cond ((atom args)
           nil)
          ((atom (cdr args))
           (car args))
          (t
           (xxxjoin 'sbitset-binary-union args))))

  (local (assert! (sbitsetp (sbitset-union))))

  (add-macro-alias sbitset-union sbitset-binary-union$inline)
  (add-macro-fn    sbitset-union sbitset-binary-union$inline t)

  (local (in-theory (enable sbitset-union-exec
                            sbitset-union)))

  (local (defthm c0
           (implies (and (< a (sbitset-pair-offset x1))
                         (< a (sbitset-pair-offset (car y2)))
                         (sbitset-pairp x1)
                         (sbitsetp y2))
                    (< a (sbitset-pair-offset (car (sbitset-union-exec (cons x1 x2) y2)))))
           :rule-classes ((:rewrite) (:linear))))

  (local (defthm c1
           (implies (and (< a (sbitset-pair-offset y1))
                         (< a (sbitset-pair-offset (car x2)))
                         (sbitsetp x2)
                         (sbitset-pairp y1))
                    (< a (sbitset-pair-offset (car (sbitset-union-exec x2 (cons y1 y2))))))
           :rule-classes ((:rewrite) (:linear))))

  (local (defthm c2
           (implies (and (< a (sbitset-pair-offset (car x2)))
                         (< a (sbitset-pair-offset (car y2)))
                         (sbitset-pairp x1)
                         (sbitsetp y2))
                    (< a (sbitset-pair-offset (car (sbitset-union-exec x2 y2)))))
           :rule-classes ((:rewrite) (:linear))))

  (defthm sbitsetp-of-sbitset-union-exec
    (implies (and (sbitsetp x)
                  (sbitsetp y))
             (sbitsetp (sbitset-union-exec x y)))
    :hints(("Goal"
            :induct (sbitset-union-exec x y)
            :do-not '(generalize fertilize))))

  (local (defthm l0
           (implies (and (sbitsetp x)
                         (sbitsetp y))
                    (iff (sbitset-find offset (sbitset-union-exec x y))
                         (or (sbitset-find offset x)
                             (sbitset-find offset y))))
           :hints(("Goal"
                   :induct (sbitset-union-exec x y)
                   :in-theory (enable sbitset-union-exec)))))

  (local (defthm m0
           (implies (and (sbitsetp x)
                         (sbitsetp y))
                    (equal (sbitset-find offset (sbitset-union-exec x y))
                           (let* ((x[offset] (sbitset-find offset x))
                                  (y[offset] (sbitset-find offset y)))
                             (cond ((and x[offset] y[offset])
                                    (sbitset-pair offset
                                                  (logior (sbitset-pair-block x[offset])
                                                          (sbitset-pair-block y[offset]))))
                                   (x[offset])
                                   (y[offset])
                                   (t
                                    nil)))))
           :hints(("Goal"
                   :induct (sbitset-union-exec x y)
                   :in-theory (enable sbitset-union-exec sbitset-find)
                   :do-not '(generalize fertilize)))))

  (in-theory (disable sbitset-union-exec))

  (local (defthm m1
           (implies (and (sbitsetp x)
                         (sbitsetp y))
                    (equal (set::in a (sbitset-members (sbitset-union-exec x y)))
                           (or (set::in a (sbitset-members x))
                               (set::in a (sbitset-members y)))))))

  (local (defthm sbitset-members-of-sbitset-union-exec
           ;; Just by double-containment.
           (implies (and (sbitsetp x)
                         (sbitsetp y))
                    (equal (sbitset-members (sbitset-union-exec x y))
                           (set::union (sbitset-members x)
                                        (sbitset-members y))))))

  ;; And now we just get rid of the hyps, with the fixing function.

  (local (in-theory (enable sbitset-union)))

  (defthm sbitset-of-sbitset-union
    (sbitsetp (sbitset-union x y)))

  (defthm sbitset-members-of-sbitset-union
    (equal (sbitset-members (sbitset-union x y))
           (set::union (sbitset-members x)
                        (sbitset-members y)))))


(local (defthm sbitset-blockp-of-logand
         (implies (and (force (sbitset-blockp x))
                       (force (sbitset-blockp y)))
                  (equal (sbitset-blockp (logand x y))
                         (not (zp (logand x y)))))
         :hints(("Goal"
                 :in-theory (e/d (sbitset-blockp)
                                 (bitops::unsigned-byte-p-of-logand-1
                                  bitops::unsigned-byte-p-of-logand-2
                                  ))
                 :use ((:instance bitops::unsigned-byte-p-of-logand-1
                                  (x x)
                                  (y y)
                                  (n *sbitset-block-size*)))))))

(defsection sbitset-intersect
  :parents (sbitsets)
  :short "@('(sbitset-intersect X Y ...) constructs the set @('X \\intersect Y
\\intersect ...')."
  :long "@(def sbitset-intersect)"

  (define sbitset-intersect-exec ((x sbitsetp)
                                  (y sbitsetp))
    :measure (+ (len x) (len y))
    (b* (((when (atom x)) nil)
         ((when (atom y)) nil)
         (x1 (car x))
         (y1 (car y))
         ((the unsigned-byte x1.offset) (sbitset-pair-offset x1))
         ((the unsigned-byte y1.offset) (sbitset-pair-offset y1))
         ((when (< x1.offset y1.offset))
          (sbitset-intersect-exec (cdr x) y))
         ((unless (eql x1.offset y1.offset))
          (sbitset-intersect-exec x (cdr y)))
         (x1.block (sbitset-pair-block x1))
         (y1.block (sbitset-pair-block y1))
         (new-block (the-sbitset-block
                     (logand (the-sbitset-block x1.block)
                             (the-sbitset-block y1.block))))
         ((when (eql 0 (the-sbitset-block new-block)))
          (sbitset-intersect-exec (cdr x) (cdr y)))
         (new-pair (sbitset-pair x1.offset new-block)))
      (cons new-pair (sbitset-intersect-exec (cdr x) (cdr y)))))

  (define sbitset-binary-intersect ((x sbitsetp)
                                    (y sbitsetp))
    :inline t
    (mbe :logic (sbitset-intersect-exec (sbitset-fix x)
                                        (sbitset-fix y))
         :exec (sbitset-intersect-exec x y)))

  (defmacro sbitset-intersect (&rest args)
    (cond ((atom args)
           nil)
          ((atom (cdr args))
           (car args))
          (t
           (xxxjoin 'sbitset-binary-intersect args))))

  (add-macro-alias sbitset-intersect sbitset-binary-intersect$inline)
  (add-macro-fn    sbitset-intersect sbitset-binary-intersect$inline t)

  (local (assert! (sbitsetp (sbitset-intersect))))

  (local (in-theory (enable sbitset-intersect-exec
                            sbitset-intersect)))

  (local (defthm c0
           (implies (and (sbitset-intersect-exec x2 y2)
                         (< (sbitset-pair-offset x1)
                            (sbitset-pair-offset (car y2)))
                         (sbitset-pairp x1)
                         (sbitsetp x2)
                         (sbitset-pairp y1)
                         (sbitsetp y2))
                    (< (sbitset-pair-offset x1)
                       (sbitset-pair-offset (car (sbitset-intersect-exec x2 y2)))))))

  (defthm sbitsetp-of-sbitset-intersect-exec
    (implies (and (sbitsetp x)
                  (sbitsetp y))
             (sbitsetp (sbitset-intersect-exec x y)))
    :hints(("Goal"
            :induct (sbitset-intersect-exec x y)
            :do-not '(generalize fertilize))))

  (local (defthm l0
           (implies (and (sbitsetp x)
                         (sbitsetp y))
                    (iff (sbitset-find offset (sbitset-intersect-exec x y))
                         (let ((a (sbitset-find offset x))
                               (b (sbitset-find offset y)))
                           (and a
                                b
                                (not (zp (logand (sbitset-pair-block a)
                                                 (sbitset-pair-block b))))))))
           :hints(("Goal"
                   :induct (sbitset-intersect-exec x y)
                   :in-theory (enable sbitset-intersect-exec)))))

  (local (defthm m0
           (implies (and (sbitsetp x)
                         (sbitsetp y))
                    (equal (sbitset-find offset (sbitset-intersect-exec x y))
                           (let* ((a         (sbitset-find offset x))
                                  (b         (sbitset-find offset y))
                                  (new-block (logand (sbitset-pair-block a)
                                                     (sbitset-pair-block b))))
                             (and a
                                  b
                                  (not (zp new-block))
                                  (sbitset-pair offset new-block)))))
           :hints(("Goal"
                   :induct (sbitset-intersect-exec x y)
                   :in-theory (enable sbitset-intersect-exec sbitset-find)
                   :do-not '(generalize fertilize)))))

  (in-theory (disable sbitset-intersect-exec))

  (local (defthm n0
           (implies (and (logbitp a x)
                         (equal (logand x y) 0))
                    (not (logbitp a y)))
           :hints(("Goal"
                   :in-theory (disable bitops::logbitp-of-logand)
                   :use ((:instance bitops::logbitp-of-logand
                                    (a a)
                                    (x x)
                                    (y y)))))))

  (local (defthm m1
           (implies (and (sbitsetp x)
                         (sbitsetp y))
                    (equal (set::in a (sbitset-members (sbitset-intersect-exec x y)))
                           (and (set::in a (sbitset-members x))
                                (set::in a (sbitset-members y)))))))

  (local (defthm sbitset-members-of-sbitset-intersect-exec
           ;; Just by double-containment.
           (implies (and (sbitsetp x)
                         (sbitsetp y))
                    (equal (sbitset-members (sbitset-intersect-exec x y))
                           (set::intersect (sbitset-members x)
                                            (sbitset-members y))))
           :hints(("Goal" :in-theory (disable (force))))))

  ;; And now we just get rid of the hyps, with the fixing function.

  (local (in-theory (enable sbitset-intersect)))

  (defthm sbitset-of-sbitset-intersect
    (sbitsetp (sbitset-intersect x y)))

  (defthm sbitset-members-of-sbitset-intersect
    (equal (sbitset-members (sbitset-intersect x y))
           (set::intersect (sbitset-members x)
                            (sbitset-members y)))))




(local (defthm sbitset-blockp-of-logandc2
         (implies (and (force (sbitset-blockp x))
                       (force (sbitset-blockp y)))
                  (equal (sbitset-blockp (logand x (lognot y)))
                         (not (zp (logand x (lognot y))))))
         :hints(("Goal"
                 :in-theory (e/d (sbitset-blockp)
                                 (bitops::unsigned-byte-p-of-logand-1
                                  bitops::unsigned-byte-p-of-logand-2
                                  ))
                 :use ((:instance bitops::unsigned-byte-p-of-logand-1
                                  (x x)
                                  (y (lognot y))
                                  (n *sbitset-block-size*)))))))


(define sbitset-difference ((x sbitsetp)
                            (y sbitsetp))
  :returns sbitset
  :parents (sbitsets)
  :short "@('(sbitset-difference X Y)') constructs the set @('X - Y')."

  :prepwork
  ((define sbitset-difference-exec ((x sbitsetp)
                                    (y sbitsetp))
     :measure (+ (len x) (len y))
     (b* (((when (atom x)) nil)
          ((when (atom y)) x)
          (x1 (car x))
          (y1 (car y))
          ((the unsigned-byte x1.offset) (sbitset-pair-offset x1))
          ((the unsigned-byte y1.offset) (sbitset-pair-offset y1))
          ((when (< x1.offset y1.offset))
           (cons (car x) (sbitset-difference-exec (cdr x) y)))
          ((unless (eql x1.offset y1.offset))
           (sbitset-difference-exec x (cdr y)))
          (x1.block (sbitset-pair-block x1))
          (y1.block (sbitset-pair-block y1))
          (new-block (the-sbitset-block
                      (logandc2 (the-sbitset-block x1.block)
                                (the-sbitset-block y1.block))))
          ((when (eql 0 (the-sbitset-block new-block)))
           (sbitset-difference-exec (cdr x) (cdr y)))
          (new-pair (sbitset-pair x1.offset new-block)))
       (cons new-pair (sbitset-difference-exec (cdr x) (cdr y))))))

  :inline t
  (mbe :logic (sbitset-difference-exec (sbitset-fix x)
                                       (sbitset-fix y))
       :exec (sbitset-difference-exec x y))
  ///
  (local (in-theory (enable sbitset-difference-exec)))

  (local (defthm c1
           (IMPLIES (AND (< a (SBITSET-PAIR-OFFSET (CAR X2)))
                         (< a (SBITSET-PAIR-OFFSET (CAR Y2)))
                         (SBITSET-DIFFERENCE-EXEC X2 Y2)
                         (SBITSETP X2)
                         (SBITSETP Y2))
                    (< a (SBITSET-PAIR-OFFSET (CAR (SBITSET-DIFFERENCE-EXEC X2 Y2)))))))

  (defthm sbitsetp-of-sbitset-difference-exec
    (implies (and (sbitsetp x)
                  (sbitsetp y))
             (sbitsetp (sbitset-difference-exec x y)))
    :hints(("Goal"
            :induct (sbitset-difference-exec x y)
            :do-not '(generalize fertilize))))

  (local (defthm l0
           (implies (and (sbitsetp x)
                         (sbitsetp y))
                    (iff (sbitset-find offset (sbitset-difference-exec x y))
                         (let* ((a (sbitset-find offset x))
                                (b (sbitset-find offset y))
                                (new-block (logandc2 (sbitset-pair-block a)
                                                     (sbitset-pair-block b))))
                           (and a
                                (or (not b)
                                    (not (zp new-block)))))))
           :hints(("Goal"
                   :induct (sbitset-difference-exec x y)
                   :in-theory (enable sbitset-difference-exec)))))

  (local (defthm m0
           (implies (and (sbitsetp x)
                         (sbitsetp y))
                    (equal (sbitset-find offset (sbitset-difference-exec x y))
                           (let* ((a         (sbitset-find offset x))
                                  (b         (sbitset-find offset y))
                                  (new-block (logandc2 (sbitset-pair-block a)
                                                       (sbitset-pair-block b))))
                             (and a
                                  (if (not b)
                                      a
                                    (and (not (zp new-block))
                                         (sbitset-pair offset new-block)))))))
           :hints(("Goal"
                   :induct (sbitset-difference-exec x y)
                   :in-theory (enable sbitset-difference-exec sbitset-find)
                   :do-not '(generalize fertilize)))))

  (in-theory (disable sbitset-difference-exec))

  (local (defthm n0
           (implies (and (equal (logand x (lognot y)) 0)
                         (logbitp a x))
                    (logbitp a y))
           :hints(("Goal"
                   :in-theory (disable bitops::logbitp-of-logand)
                   :use ((:instance bitops::logbitp-of-logand
                                    (a a)
                                    (x x)
                                    (y (lognot y))))))))

  (local (defthm m1
           (implies (and (sbitsetp x)
                         (sbitsetp y))
                    (equal (set::in a (sbitset-members (sbitset-difference-exec x y)))
                           (and (set::in a (sbitset-members x))
                                (not (set::in a (sbitset-members y))))))))

  (local (defthm sbitset-members-of-sbitset-difference-exec
           ;; Just by double-containment.
           (implies (and (sbitsetp x)
                         (sbitsetp y))
                    (equal (sbitset-members (sbitset-difference-exec x y))
                           (set::difference (sbitset-members x)
                                             (sbitset-members y))))
           :hints(("Goal" :in-theory (disable (force))))))

  ;; And now we just get rid of the hyps, with the fixing function.

  (local (in-theory (enable sbitset-difference)))

  (defthm sbitset-of-sbitset-difference
    (sbitsetp (sbitset-difference x y)))

  (defthm sbitset-members-of-sbitset-difference
    (equal (sbitset-members (sbitset-difference x y))
           (set::difference (sbitset-members x)
                             (sbitset-members y)))))


#||


; -----------------------------------------------------------------------------
;
;                         WITNESS STYLE REASONING
;
; -----------------------------------------------------------------------------

; In ordinary bitsets, we could just use equal-by-logbitp to quickly prove
; equal-bitsets-by-membership.  Here we don't have a badguy already ready
; already, so we have to make one.

;; (local
;;  (define sbitset-badguy (x y)
;;    ;; Returns the offset for the first difference, or nil if they're identical.
;;    :verify-guards nil
;;    (b* (((when (atom x))
;;          (if (atom y)
;;              nil
;;            (sbitset-pair-offset (car y))))
;;         ((when (atom y))
;;          (sbitset-pair-offset (car x)))
;;         (x-off (sbitset-pair-offset (car x)))
;;         (y-off (sbitset-pair-offset (car y)))
;;         ((when (< x-off y-off)) x-off)
;;         ((when (< y-off x-off)) y-off)
;;         ((unless (equal (sbitset-pair-block (car x))
;;                         (sbitset-pair-block (car y))))
;;          x-off))
;;      (sbitset-badguy (cdr x) (cdr y)))
;;    ///
;;    (local (defthm equal-of-sbitset-pairs
;;             (implies (and (sbitset-pairp x)
;;                           (sbitset-pairp y))
;;                      (equal (equal x y)
;;                             (and (equal (sbitset-pair-offset x)
;;                                         (sbitset-pair-offset y))
;;                                  (equal (sbitset-pair-block x)
;;                                         (sbitset-pair-block y)))))
;;             :hints(("Goal" :in-theory (enable sbitset-pairp
;;                                               sbitset-pair-offset
;;                                               sbitset-pair-block)))))

;;    (defthm sbitset-badguy-under-iff
;;      (implies (and (sbitsetp x)
;;                    (sbitsetp y))
;;               (iff (sbitset-badguy x y)
;;                    (not (equal x y))))
;;      :hints(("Goal" :in-theory (enable sbitsetp))))

;;    (local (defthm l0
;;             (IMPLIES (AND (EQUAL (SBITSET-BADGUY X2 Y2) offset)
;;                           (< offset (SBITSET-PAIR-OFFSET (CAR X2)))
;;                           offset
;;                           (SBITSETP X2)
;;                           (SBITSETP Y2))
;;                      (<= (SBITSET-PAIR-OFFSET (CAR Y2))
;;                          offset))))

;;    (local (defthm l1
;;             (IMPLIES (AND (EQUAL (SBITSET-BADGUY X2 Y2) 0)
;;                           (< 0 (SBITSET-PAIR-OFFSET (CAR X2)))
;;                           (SBITSETP X2)
;;                           (SBITSETP Y2))
;;                      (equal (SBITSET-PAIR-OFFSET (CAR Y2))
;;                             0))))

;;    (defthm sbitset-badguy-correct
;;      (let ((offset (sbitset-badguy x y)))
;;        (implies (and (sbitsetp x)
;;                      (sbitsetp y)
;;                      offset)
;;                 (not (equal (sbitset-find offset x)
;;                             (sbitset-find offset y))))))))


; if we've found some block that's in one but not the other, then we have a member.

(define sbitset-pair-badguy (x y)
  :verify-guards nil
  (b* (((when (equal x y))
        nil)
       (x-off   (sbitset-pair-offset x))
       (y-off   (sbitset-pair-offset y))
       (x-block (sbitset-pair-block x))
       (y-block (sbitset-pair-block y))
       ((when (equal x-off y-off))
        (+ (* x-off *sbitset-block-size*)
           (logbitp-mismatch x-block y-block))))
    (car (sbitset-pair-members x))))

(define sbitset-badguy (x y)
  :verify-guards nil
  (b* (((when (atom x))
        (if (atom y)
            nil
          (car (sbitset-pair-members (car y)))))
       ((when (atom y))
        (car (sbitset-pair-members (car x))))
       ((when (equal (car x) (car y)))
        (sbitset-badguy (cdr x) (cdr y))))
    (sbitset-pair-badguy (car x) (car y)))))

(local (in-theory (enable sbitset-pair-badguy)))

(local (defthm l1
           (implies (nat-listp x)
                    (iff (car x)
                         (consp x)))))

(local (defthm l2
           (implies (and (nat-listp x)
                         (natp offset))
                    (nat-listp (add-to-each offset x)))))

(local (defthm l3
           (implies (sbitset-pairp x)
                    (nat-listp (sbitset-pair-members x)))
           :hints(("Goal" :in-theory (enable sbitset-pair-members)))))

(defthm sbitset-pair-badguy-under-iff
    (implies (and (sbitset-pairp x)
                  (sbitset-pairp y))
             (iff (sbitset-pair-badguy x y)
                  (not (equal x y))))
    :hints(("Goal" :in-theory (enable sbitset-pair-members))))


(local (defthm c0
           (iff (member (car x) x)
                (consp x))))

(local (defthm c1-upper
           (implies (and (member elem (sbitset-pair-members x))
                         (sbitset-pairp x))
                    (< elem (+ *sbitset-block-size*
                               (* *sbitset-block-size* (sbitset-pair-offset x)))))))

(local (defthm c1-lower
           (implies (and (member elem (sbitset-pair-members x))
                         (sbitset-pairp x))
                    (<= (* *sbitset-block-size* (sbitset-pair-offset x))
                        elem))))

(local (defthm c1-upper-special
           (implies (sbitset-pairp x)
                    (< (car (sbitset-pair-members x))
                       (+ *sbitset-block-size*
                          (* *sbitset-block-size* (sbitset-pair-offset x)))))
           :rule-classes ((:rewrite) (:linear))))

(local (defthm c1-lower-special
           (implies (sbitset-pairp x)
                    (<= (* *sbitset-block-size* (sbitset-pair-offset x))
                        (car (sbitset-pair-members x))))
           :rule-classes ((:rewrite) (:linear))))

(local (defthm c2
           (implies (sbitset-pairp x)
                    (not (empty (sbitset-pair-members x))))))

(local (defthm equal-of-sbitset-pairs
           (implies (and (sbitset-pairp x)
                         (sbitset-pairp y))
                    (equal (equal x y)
                           (and (equal (sbitset-pair-offset x)
                                       (sbitset-pair-offset y))
                                (equal (sbitset-pair-block x)
                                       (sbitset-pair-block y)))))
           :hints(("Goal" :in-theory (enable sbitset-pairp
                                             sbitset-pair-offset
                                             sbitset-pair-block)))))

(local (defthm correct-when-offsets-differ
           (let ((elem (sbitset-pair-badguy x y)))
             (implies (and (not (equal (sbitset-pair-offset x)
                                       (sbitset-pair-offset y)))
                           (sbitset-pairp x)
                           (sbitset-pairp y))
                      (not (iff (member elem (sbitset-pair-members x))
                                (member elem (sbitset-pair-members y))))))))

(local (include-book "arithmetic-3/bind-free/top" :dir :system))

(local (defthm d0
           (iff (natp (logbitp-mismatch x y))
                (logbitp-mismatch x y))))

(local (defthm d0b
           (iff (rationalp (logbitp-mismatch x y))
                (logbitp-mismatch x y))))

(local (defthm d1
           (implies (and (natp x)
                         (syntaxp (not (quotep x))))
                    (equal (< (+ 0 x) y)
                           (< x y)))))

(local (defthm d2-help1
           (equal (logbitp-mismatch x x)
                  nil)))

(local (defthm d2-help2
           (implies (sbitset-pairp x)
                    (<= (integer-length (sbitset-pair-block x))
                        60))
           :rule-classes ((:rewrite) (:linear))
           :hints(("goal"
                   :in-theory (disable acl2::integer-length-monotonic)
                   :use ((:instance acl2::integer-length-monotonic
                                    (i (sbitset-pair-block x))
                                    (j (1- (expt 2 *sbitset-block-size*)))))))))

(local (defthm d2
           (implies (and (sbitset-pairp x)
                         (sbitset-pairp y))
                    (< (logbitp-mismatch (sbitset-pair-block x)
                                         (sbitset-pair-block y))
                       60))
           :hints(("Goal"
                   :in-theory (disable acl2::logbitp-mismatch-upper-bound)
                   :use ((:instance acl2::logbitp-mismatch-upper-bound
                                    (a (sbitset-pair-block x))
                                    (b (sbitset-pair-block y))))))))

(local (defthm d3
           (implies (and (not (logbitp (logbitp-mismatch x y) x))
                         (not (equal x y))
                         (natp x)
                         (natp y))
                    (logbitp (logbitp-mismatch x y) y))
           :hints(("goal" :use ((:instance acl2::logbitp-mismatch-correct
                                           (a x) (b y)))))))

(local (defthm d4
           (implies (and (logbitp (logbitp-mismatch x y) x)
                         (not (equal x y))
                         (natp x)
                         (natp y))
                    (not (logbitp (logbitp-mismatch x y) y)))
           :hints(("goal" :use ((:instance acl2::logbitp-mismatch-correct
                                           (a x) (b y)))))))

(local (defthm correct-when-offsets-same
           (let ((elem (sbitset-pair-badguy x y)))
             (implies (and (equal (sbitset-pair-offset x)
                                  (sbitset-pair-offset y))
                           (not (equal x y))
                           (sbitset-pairp x)
                           (sbitset-pairp y))
                      (not (iff (member elem (sbitset-pair-members x))
                                (member elem (sbitset-pair-members y))))))))

(defthm sbitset-pair-badguy-correct
    (let ((elem (sbitset-pair-badguy x y)))
      (implies (and (not (equal x y))
                    (sbitset-pairp x)
                    (sbitset-pairp y))
               (not (iff (member elem (sbitset-pair-members x))
                         (member elem (sbitset-pair-members y))))))
    :hints(("Goal"
            :in-theory (disable sbitset-pair-badguy
                                sbitset-pair-members
                                member-equal-sbitset-pair-members
                                correct-when-offsets-same
                                correct-when-offsets-differ)
            :use ((:instance correct-when-offsets-same)
                  (:instance correct-when-offsets-differ)))))

(local (in-theory (disable sbitset-pair-badguy)))
(local (in-theory (enable sbitset-badguy)))

(defthm sbitset-badguy-under-iff
  (implies (and (sbitsetp x)
                (sbitsetp y))
           (iff (sbitset-badguy x y)
                (not (equal x y))))
  :hints(("Goal" :in-theory (enable sbitsetp))))


(local (defthm m0
         (implies (and (sbitset-pairp x)
                       (sbitset-pairp y))
                  (iff (natp (sbitset-pair-badguy x y))
                       (sbitset-pair-badguy x y)))
         :hints(("Goal" :in-theory (enable sbitset-pair-badguy)))))

(local (defthm m1
         (let ((elem (sbitset-pair-badguy x y)))
           (implies (and (not (equal x y))
                         (sbitset-pairp x)
                         (sbitset-pairp y)
                         (member elem (sbitset-pair-members x)))
                    (not (member elem (sbitset-pair-members y)))))
         :hints(("Goal"
                 :in-theory (disable sbitset-pair-badguy-correct
                                     member-equal-sbitset-pair-members
                                     equal-of-sbitset-pairs)
                 :use ((:instance sbitset-pair-badguy-correct))))))

(local (defthm m2
         (let ((elem (sbitset-pair-badguy x y)))
           (implies (and (not (equal x y))
                         (sbitset-pairp x)
                         (sbitset-pairp y)
                         (not (member elem (sbitset-pair-members x))))
                    (member elem (sbitset-pair-members y))))
         :hints(("Goal"
                 :in-theory (disable sbitset-pair-badguy-correct
                                     member-equal-sbitset-pair-members
                                     equal-of-sbitset-pairs)
                 :use ((:instance sbitset-pair-badguy-correct))))))

(local (defthm m4
         (implies (and (< (sbitset-pair-offset a) (sbitset-pair-offset (car x)))
                       (member elem (sbitset-pair-members a))
                       (sbitsetp x)
                       (sbitset-pairp a))
                  (not (member elem (sbitset-members x))))
         :hints(("Goal" :in-theory (enable sbitset-members)))))

(local (defthm m5
         (IMPLIES (AND (< (sbitset-pair-offset a) (sbitset-pair-offset (car x)))
                       (not (member elem (sbitset-pair-members a)))
                       (sbitsetp x)
                       (sbitset-pairp a))
                  (not (member elem (sbitset-members x))))
         :hints(("Goal" :in-theory (enable sbitset-members)))))

(defthm m6
  (implies (and (member elem (sbitset-pair-members a))
                (member a x)
                (sbitset-pairp a)
                (sbitsetp x))
           (member elem (sbitset-members x)))
  :hints(("Goal" :in-theory (e/d (sbitset-members)
                                 (sbitset-pair-members
                                  in-of-sbitset-pair-members
                                  MEMBER-EQUAL-SBITSET-PAIR-MEMBERS
                                  car-cdr-elim
                                  )))))

(defthm m7
  (IMPLIES (AND (< (SBITSET-PAIR-OFFSET X1)
                   (SBITSET-PAIR-OFFSET (CAR X2)))
                (NOT (EQUAL X1 Y1))
                (SBITSET-PAIRP X1)
                (SBITSETP X2)
                (SBITSET-PAIRP Y1))
           (not (MEMBER-EQUAL (SBITSET-PAIR-BADGUY X1 Y1)
                              (SBITSET-MEMBERS X2))))
  :hints(("Goal"
          :use ((:instance m4
                           (elem (SBITSET-PAIR-BADGUY X1 Y1))
                           (a x1)
                           (x x2)))
          :in-theory (e/d (;sbitset-members
                                  )
                                 (;sbitset-pair-members
                                  m4
                                  in-of-sbitset-pair-members
                                  equal-of-sbitset-pairs
                                  MEMBER-EQUAL-SBITSET-PAIR-MEMBERS
                                  car-cdr-elim
                                  )))))



Subgoal *1/5.1.3'5'
(IMPLIES (AND (SBITSET-PAIRP A)
              (SBITSET-PAIRP B)
              (SBITSETP X)
              (NOT (EQUAL A B))
              (< (SBITSET-PAIR-OFFSET A)
                 (SBITSET-PAIR-OFFSET (CAR X)))
              (MEMBER-EQUAL (SBITSET-PAIR-BADGUY A B)
                            (SBITSET-MEMBERS X)))
         (NOT (MEMBER-EQUAL (SBITSET-PAIR-BADGUY A B)
                            (SBITSET-PAIR-MEMBERS B))))

Subgoal *1/5.1.2.3''
(IMPLIES (AND (NOT (EQUAL X1 Y1))
              (SBITSET-PAIRP X1)
              (SBITSET-PAIRP Y1)
              (SBITSETP Y2)
              (< (SBITSET-PAIR-OFFSET Y1)
                 (SBITSET-PAIR-OFFSET (CAR Y2)))
              (MEMBER-EQUAL (SBITSET-PAIR-BADGUY X1 Y1)
                            (SBITSET-PAIR-MEMBERS X1)))
         (NOT (MEMBER-EQUAL (SBITSET-PAIR-BADGUY X1 Y1)
                            (SBITSET-MEMBERS Y2))))

Subgoal *1/5.1.2.2
(IMPLIES (AND (NOT (EQUAL X1 Y1))
              (SBITSET-PAIRP X1)
              (SBITSETP X2)
              (< (SBITSET-PAIR-OFFSET X1)
                 (SBITSET-PAIR-OFFSET (CAR X2)))
              (SBITSET-PAIRP Y1)
              (SBITSETP Y2)
              (< (SBITSET-PAIR-OFFSET Y1)
                 (SBITSET-PAIR-OFFSET (CAR Y2)))
              (MEMBER-EQUAL (SBITSET-PAIR-BADGUY X1 Y1)
                            (SBITSET-MEMBERS X2)))
         (NOT (MEMBER-EQUAL (SBITSET-PAIR-BADGUY X1 Y1)
                            (SBITSET-MEMBERS Y2))))

(defthm sbitset-badguy-correct
  (let ((elem (sbitset-badguy x y)))
    (implies (and (sbitsetp x)
                  (sbitsetp y)
                  elem)
             (not (iff (member elem (sbitset-members x))
                       (member elem (sbitset-members y))))))
  :hints(("goal"
          :induct (sbitset-badguy x y)
          :in-theory (disable member-equal-sbitset-pair-members
                              equal-of-sbitset-pairs)
          :do-not '(generalize fertilize)
          :do-not-induct t))
  :otf-flg t)



(defund sbitset-

(local (in-theory (disable double-containment)))

(defthm c0
  (IMPLIES (AND (NOT (EQUAL (SBITSET-FIND offset X)
                            (SBITSET-FIND offset Y)))
                (SBITSETP X)
                (SBITSETP Y)
                (NOT (EQUAL X Y)))
           (not (equal (sbitset-members x)
                       (sbitset-members y))))
  :hints(("Goal" :in-theory (enable sbitset-members))))

(defthm crux
  (implies (and (sbitsetp x)
                (sbitsetp y)
                (not (equal x y)))
           (not (equal (sbitset-members x)
                       (sbitset-members y))))
  :hints(("Goal"
          :in-theory (disable sbitset-badguy-correct)
          :use ((:instance sbitset-badguy-correct)))))
           in-of-sbitset-members

(defthmd equal-sbitsets-by-membership
  (implies (and (sbitsetp x)
                (sbitsetp y))
           (equal (equal x y)
                  (equal (sbitset-members x)
                         (sbitset-members y))))
  :hints(("Goal"
          :in-theory (disable set::double-containment))))
          :use ((:instance sbitset-badguy-correct
                           (x x) (y y)))
                 equal-by-logbitp
                 (logbitp-hyp (lambda ()
                                (and (natp x)
                                     (natp y)
                                     (equal (sbitset-members x)
                                            (sbitset-members y)))))
                 (logbitp-lhs (lambda () x))
                 (logbitp-rhs (lambda () y))))))))


||#