File: clrat-parser.lisp

package info (click to toggle)
acl2 8.6%2Bdfsg-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 1,138,276 kB
  • sloc: lisp: 17,818,294; java: 125,359; python: 28,122; javascript: 23,458; cpp: 18,851; ansic: 11,569; perl: 7,678; xml: 5,591; sh: 3,978; makefile: 3,840; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (2042 lines) | stat: -rw-r--r-- 66,914 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
; Copyright (C) 2016, Regents of the University of Texas
; Marijn Heule, Warren A. Hunt, Jr., and Matt Kaufmann
; License: A 3-clause BSD license.  See the LICENSE file distributed with ACL2.

; Reader for compressed LRAT ("clrat") files

; The description of the binary format is in the DRAT-trim public
; repository. You can get it at:

; git clone https://github.com/marijnheule/drat-trim.git

; The description is in the README.md file.  It is also here:
; https://github.com/marijnheule/drat-trim

; Work was originally started by Nathan Wetzler, but has been modified
; extensively by Warren Hunt and Matt Kaufmannn so as to produce a
; faster, lrat proof checker.

; We read two files: a formula file and a proof file.  A proof step in
; the proof file is either a deletion step

;  d i1 ... ik 0

; or else an addition step

;  j l1 l2 ... lk 0 d1 d2 ... dm -e1 f11 ... f1{m_1} -e2 f21 ... f2{m_2} ... 0

; where:

; - j is the index of a clause C = {l1, l2, ..., lk} to be added redundantly;
; - d1 ... dm are indices of unit clauses for RUP from the negation of C;
; - e1 < e2 < ... are indices of existing (non-deleted) clauses; and
; - ei is a RAT clause Ci, and fi1 ... fi{m_i} are indices of unit clauses for
;   RUP from the union of the negation of C with the negation of Ci \ {-l1}.

; Note that each index j must exceed all preceding clause indices.

; See the function LRAT-GUARD (below) for a description of our
; string/file reading invariant.

; See the function CHECK-LRAT-LINE-TO-LF (below) for a description of
; our coding approach.

; (in-package "ACL2")
(in-package "LRAT")

(include-book "std/util/bstar" :dir :system)
(include-book "limits")

; (include-book "misc/disassemble" :dir :system)


; Some miscellaneous definitions.

(defmacro with-arithmetic-help-5 (&rest forms)
  `(encapsulate
    ()
    (local (include-book "arithmetic-5/top" :dir :system))
    (set-default-hints #!acl2'((nonlinearp-default-hint
                                stable-under-simplificationp
                                hist
                                pspv)))
    ,@forms))


; Reverse function

(defun my-rev1 (x a)
  (declare (xargs :guard (true-listp a)))
  (if (atom x)
      a
    (my-rev1 (cdr x) (cons (car x) a))))

(defthm true-listp-my-rev1
  (implies (true-listp a)
           (true-listp (my-rev1 x a))))

(defthm nat-listp-my-rev1
  (implies (and (nat-listp x)
                (nat-listp a))
           (nat-listp (my-rev1 x a))))

(defthm integer-listp-my-rev1
  (implies (and (integer-listp x)
                (integer-listp a))
           (integer-listp (my-rev1 x a))))

(defthm alistp-my-rev1
  (implies (and (alistp x)
                (alistp a))
           (alistp (my-rev1 x a))))

(defthm character-listp-my-rev1
  (implies (and (character-listp x)
                (character-listp a))
           (character-listp (my-rev1 x a))))

(in-theory (disable my-rev1))


(defun my-rev (x)
  (declare (xargs :guard t))
  (if (atom x)
      nil
    (my-rev1 x nil)))

(defthm true-listp-my-rev
  (true-listp (my-rev x)))

(defthm nat-listp-my-rev
  (implies (nat-listp x)
           (nat-listp (my-rev x))))

(defthm integer-listp-my-rev
  (implies (integer-listp x)
           (integer-listp (my-rev x))))

(defthm alistp-my-rev
  (implies (alistp x)
           (alistp (my-rev x))))

(defthm character-listp-my-rev
  (implies (character-listp x)
           (character-listp (my-rev x))))

(in-theory (disable my-rev))


(defun my-app (x y)
  (declare (xargs :guard t))
  (if (atom x)
      y
    (cons (car x)
          (my-app (cdr x) y))))

(defthm true-listp-my-app
  (implies (true-listp y)
           (true-listp (my-app x y))))

(defthm integer-listp-app
  (implies (and (integer-listp x)
                (integer-listp y))
           (integer-listp (my-app x y))))

(in-theory (disable my-app))


(with-arithmetic-help-5
 (defthm logand-less-than-or-equal
   (implies (natp x)
            (and (<= (logand x y) x)
                 (<= (logand y x) x)))
   :hints (("Goal" :cases ((equal x 0))))
   :rule-classes :linear))

(with-arithmetic-help-5
 (defthm logand-greater-or-equal-to-zero
   (implies (or (natp x) (natp y))
            (and (integerp (logand x y))
                 (<= 0 (logand x y))
                 ;; (integerp (logand y x))
                 ;; (<= 0 (logand y x))
                 ))
   :hints (("Goal" :cases ((equal x 0))))
   :rule-classes :type-prescription))

; ASH rules

(with-arithmetic-help-5
 (defthm ash-negative-shift-makes-input-smaller
   (implies (and (integerp x)
                 (< 0 x)
                 (integerp shift)
                 (< shift 0))
            (< (ash x shift) x))
   :rule-classes :linear))

(with-arithmetic-help-5
 (defthm helper
   (implies (< 127 nat)
            (< (ash nat -7)
               nat))
   :rule-classes (:rewrite :linear)))

(with-arithmetic-help-5
 (defthm positivep-ash
   (implies (<= 0 x)
            (<= 0 (ash x n)))
   :rule-classes :type-prescription))

(in-theory (disable ash))

(with-arithmetic-help-5
 (defthm nat-to-clrat-num-helper
   (implies (and (integerp nat) ; needed for ACL2(r) proof
                 (< 127 nat))
            (< (integer-abs (ash nat -7))
               (acl2-count nat)))))

(defun list-of-integer-listp (x)
  (declare (xargs :guard t))
  (if (atom x)
      (null x)
    (and (integer-listp (car x))
         (list-of-integer-listp (cdr x)))))


(defun-inline char-to-nat (ch)

; Convert character decimal digit to natural number.  If CH is '0' to
; '9', return corresponding natural number; otherwise NIL

  (declare (xargs :guard (characterp ch)))
  (let ((code (u59 (char-code ch))))
    (and (>= code 48)
         (<= code 57)
         (- code 48))))

(defthm natp-or-null-char-to-nat
  (or (natp (char-to-nat ch))
      (null (char-to-nat ch)))
  :rule-classes :type-prescription)

(defthm natp-char-to-nat
  (implies (char-to-nat ch)
           (natp (char-to-nat ch))))

(defthm char-to-nat-is-less-than-10
  (implies (char-to-nat ch)
           (< (char-to-nat ch) 10))
  :rule-classes :linear)

(in-theory (disable char-to-nat))


; Our CLRAT reader processes the contents of a file and makes it ready
; for our SAT-checking algorithm..  This proof processing is done
; incrementally.  We process a SAT-proof file by reading some of it,
; processing what we read, and then, reading some more of the file and
; follow that with more processing, and so on.  We repeat this process
; until an entire file has been processed.


(defun lrat-guard (str len pos)

; Guard for string processing functions.  The requirement that LEN is
; less than 2^56 ensures that keeping track of our position can be
; done with FIXNUMs.  The diagram below is meant to represent a string.

; POS is a natural number <= LEN.
; LEN is the length of STR.
; "_ _ _ _ _ _ _ _ _ _ _ _ _ ... _ _ _ _" is some string STR.
;            ^                           ^
;           POS                          LEN

; Functions that accept a STR, POS, and LEN, and return a position,
; POS, are proven to return a position that is in bounds.

  (declare (xargs :guard t))
  (and (stringp str)
       (natp len)
       (natp pos)
       (equal len (length str))
       (<= pos len)
       (< len *2^56*)))


(defun check-lrat-line-to-lf (str len pos)  ; This function unused.

; Check STR up to a #\LineFeed for allowable characters; this is a
; general syntatic check that a file only contains characters that can
; be converted into a proof.  This function shows our idiomatic
; approach to functions that are guarded by our recognizer LRAT-GUARD
; (defined just above).  We we reach the end, we return LEN.  We also
; return LEN when we recognize an error condition.

; We use a certain idiomatic style here, which we follow in later
; functions.

  (declare (xargs :guard (lrat-guard str len pos)
                  :measure (nfix (- len pos))))
  ;; Declare LEN and POS to be 56-bit natural (fixnum) numbers.
  (let* ((len (u56 len))
         (pos (u56 pos)))

    ;; LEN is returned whenever we reach `the end'.
    (if (mbe :logic (zp (- len pos))
             :exec  (>= pos len))
        len ; At end

      (let ((ch  (char str pos)))
        (if (eql ch #\Newline)
            pos ; At end of line, return POS.

          (if (not (or (eql ch #\Space)
                       (eql ch #\Tab)
                       (eql ch #\-)
                       (eql ch #\d) ; Part of deletion command
                       (char-to-nat ch)))

              (prog2$
               (er hard?
                   "check-lrat-line-to-lf:  Check byte at position ~x0.~%"
                   pos)
               len) ; If unrecognized character, report at end

            (let ((pos+1 (u59 (1+ pos))))
              (check-lrat-line-to-lf str len pos+1))))))))


(defun find-next-space (str len pos)

; Starting at POS, find next space character.

  (declare (xargs :guard (lrat-guard str len pos)
                  :measure (nfix (- len pos))))
  (b* ((pos (u56 pos))
       (len (u56 len))
       ((if (mbe :logic (zp (- len pos))
                 :exec  (>= pos len)))
            len) ; File end
       (ch (char str pos))
       ((if (eql ch #\Space)) pos)
       (pos+1 (u56 (1+ pos))))
    (find-next-space str len pos+1)))

(defthm natp-find-next-space
  (implies (and (natp len) (natp pos))
           (natp (find-next-space str len pos)))
  :rule-classes :type-prescription)

(defthm bound-find-next-space
  (implies (and (natp len) (natp pos) (<= pos len))
           (and (<= pos (find-next-space str len pos))
                (<= (find-next-space str len pos) len)))
  :rule-classes (:rewrite :linear))

(in-theory (disable find-next-space))


(defun find-lf (str len pos)

; Find next #\Newline character

  (declare (xargs :guard (lrat-guard str len pos)
                  :measure (nfix (- len pos)))
           (type string str))
  (let ((len (u56 len))
        (pos (u56 pos)))
    (if (mbe :logic (zp (- len pos))
             :exec  (>= pos len))
        len
      (let ((ch (char str pos)))
        (if (eql ch #\Newline)
            pos
          (let ((pos+1 (u59 (1+ pos))))
            (find-lf str len pos+1)))))))

(defthm integerp-find-lf
  (implies (and (natp pos) (natp len))
           (integerp (find-lf str len pos)))
  :rule-classes :type-prescription)

(defthm natp-find-lf
  (implies (and (natp pos) (<= pos len))
           (and (<= 0 (find-lf str len pos))
                (<= (find-lf str len pos) len)))
  :rule-classes :linear)

(defthm pos-<=-find-lf
  (implies (<= pos len)
           (<= pos (find-lf str len pos)))
  :rule-classes (:linear :rewrite))

(in-theory (disable find-lf))


(defun lrat-pos-skip-spaces (str len pos)

; Skip space characters, leave POS at first non-space character

  (declare (xargs :guard (lrat-guard str len pos)
                  :measure (nfix (- len pos))))
  (b* ((len (u56 len))
       (pos (u56 pos))
       ((if (mbe :logic (zp (- len pos))
                 :exec  (>= pos len)))
        len)
       (ch (char str pos))
       ((when (not (eql ch #\Space))) pos)
       (pos+1 (u56 (1+ pos))))
    (lrat-pos-skip-spaces str len pos+1)))

(defthm natp-lrat-pos-skip-spaces
  (implies (and (natp len) (natp pos))
           (natp (lrat-pos-skip-spaces str len pos)))
  :rule-classes :type-prescription)

(defthm pos-fact-lrat-pos-skip-spaces
  (implies (and (natp len) (natp pos) (<= pos len)) ; (lrat-guard str len pos)
           (and (<= pos (lrat-pos-skip-spaces str len pos))
                (<= (lrat-pos-skip-spaces str len pos) len)))
  :hints
  (("Goal" :induct (lrat-pos-skip-spaces str len pos)))
  :rule-classes (:rewrite :linear))

(in-theory (disable lrat-pos-skip-spaces))


(with-arithmetic-help-5
 (defun lrat-nat1 (str len pos n)

; Natural number reader.  Can read (approximately) 18-decimal-digit
; numbers; otherwise, it will fail.  Returns natural number (fixnum).

   (declare (xargs :guard (and (lrat-guard str len pos)
                               (natp n)
                               (< n *2^60*))
                   :measure (nfix (- len pos))))
   (let* ((len (u56 len))
          (pos (u56 pos))
          (n   (u60 n)))
     (if (mbe :logic (zp (- len pos))
              :exec  (>= pos len))
         0 ; File end

       (let* ((ch (char str pos))
              (digit (char-to-nat ch)))

         (if (null digit)
             n ; At end of base-10 numerals, return answer

           (if (not (< n *2^56*))
               (prog2$ (er hard? 'lrat-nat1
                           "lrat-nat1:  Number too large; position: ~x0.~%"
                           pos)
                       0)
             (let* ((digit (u04 digit))
                    (n   (u56 n))
                    (2n  (u57 (+ n n)))
                    (8n  (u59 (mbe :logic (* 8 n) :exec (ash n 3))))
                    (10n (u60 (+ 8n 2n)))

                    ;; (10n (u60 (* n 10))) creates a big-num call
                    ;; And this code generated for 10n is
                    ;; unnecessarily large

                    ;; I find this strange -- that adding something
                    ;; (anything positive) to a u60 is still thought
                    ;; to be a u60.  Possibly, it is known that 10n
                    ;; leaves 6n left of a u60, where n is 2*56*.

                    (10n+digit (u60 (+ digit 10n)))
                    (pos+1 (u59 (1+ pos))))
               (lrat-nat1 str len pos+1 10n+digit)))))))))

(defthm natp-lrat-nat1
  (implies (natp n)
           (natp (lrat-nat1 str len pos n)))
  :hints
  (("Goal"
    :in-theory (disable length)
    :induct (lrat-nat1 str len pos n)))
  :rule-classes :type-prescription)

(defthm bound-lrat-nat1
   (implies (and (natp n) (< n *2^60*))
            (and (<= 0 (lrat-nat1 str len pos n))
                 (< (lrat-nat1 str len pos n) *2^60*)))
   :hints
   (("Goal"
     :in-theory (disable length)
     :induct (lrat-nat1 str len pos n)))
   :rule-classes :linear)

(in-theory (disable lrat-nat1))


(defun lrat-nat (str len pos)

; Read natural number starting at POS

  (declare (xargs :guard (lrat-guard str len pos)))
  (b* ((pos (u56 pos))
       (len (u56 len))
       ((unless (< pos len)) 0)
       (ch (char str pos))
       (val (char-to-nat ch))
       ((unless val) 0)
       ((if (= val 0)) 0))
    (lrat-nat1 str len pos 0)))

(defthm natp-lrat-nat
  (natp (lrat-nat str len pos))
  :rule-classes :type-prescription)

(defthm bound-lrat-nat
   (and (<= 0 (lrat-nat str len pos))
        (< (lrat-nat str len pos) *2^60*))
  :rule-classes :linear)

(in-theory (disable lrat-nat))


(defun lrat-int (str len pos)

; Read integer starting at POS

  (declare (xargs :guard (lrat-guard str len pos)))
  (b* ((pos (u56 pos))
       (len (u56 len))
       ((unless (< pos len)) 0)
       (ch  (char str pos))
       ((if (not (eql ch #\-)))
        (lrat-nat str len pos))
       (pos+1 (u56 (1+ pos)))
       ((unless (< pos+1 len)) 0))
    (- (lrat-nat str len pos+1))))

(defthm integerp-lrat-int
  (integerp (lrat-int str len pos))
  :rule-classes :type-prescription)

(defthm bound-lrat-int
   (and (< (- *2^60*) (lrat-int str len pos))
        (< (lrat-int str len pos) *2^60*))
  :rule-classes :linear)

(in-theory (disable lrat-int))


(defun lrat-flg-int (str len pos)

; Read an integer and return (mv <end-of-integer-pos> <integer>)

  (declare (xargs :guard (lrat-guard str len pos)))
  (b* ((pos (u56 pos))
       (len (u56 len))
       (space-pos (u56 (find-next-space str len pos)))
       ((unless (< pos space-pos)) (mv nil 0))
       (lrat-int (s61 (lrat-int str len pos))))
    (mv space-pos lrat-int)))

(defthm natp-pos-lrat-flg-int
  (implies (and (natp pos) (natp len)
                (car (lrat-flg-int str len pos)))
           (natp (car (lrat-flg-int str len pos))))
  :hints
  (("Goal"
    :in-theory (disable length))))

(defthm bound-lrat-flg-int
  (implies (and (natp pos) (natp len) (<= pos len)
                (car (lrat-flg-int str len pos)))
           (and (<= 0 (car (lrat-flg-int str len pos)))
                (<= (car (lrat-flg-int str len pos)) len)))
  :hints
  (("Goal"
    :in-theory (disable length)))
  :rule-classes :linear)

(defthm pos-has-increased-lrat-flg-int-1
  (implies (and (natp pos) (natp len)
                (<= pos pos1)
                (car (lrat-flg-int str len pos1)))
           (< pos (car (lrat-flg-int str len pos1))))
  :rule-classes :linear)

(defthm car-lrat-flag-int-less-than-len
  (implies (and (lrat-guard str len pos)
                (or (car (lrat-flg-int str len pos))
                    (integerp (lrat-flg-int str len pos))))
           (<= (car (lrat-flg-int str len pos)) len))
  :rule-classes :linear)

(defthm mv-nth-1-natp-lrat-flg-int
  (implies (car (lrat-flg-int str len pos))
           (integerp (mv-nth 1 (lrat-flg-int str len pos))))
  :rule-classes (:rewrite :type-prescription))

(encapsulate
  ()
  (local
   (defthm one-time-math-fact
     ;; Note, this is also true without the INTEGERP hypotheses
     (implies (and (integerp i)
                   (integerp n))
              (iff (< (- i) (- n))
                   (< n i)))
     ;; By using IFF (instead of EQUAL), linear arithmetic is given
     ;; the chance to work -- which it does; otherwise, it fails.
     :rule-classes ((:rewrite :corollary
                              (implies (and (integerp i)
                                            (integerp n))
                                       (equal (< (- i) (- n))
                                              (< n i)))))))

  (defthm mv-nth1-lrat-flg-int-abs-less-than-*2^60*
    (implies (car (lrat-flg-int str len pos))
             (and (< (- *2^60*) (mv-nth 1 (lrat-flg-int str len pos)))
                  (< (mv-nth 1 (lrat-flg-int str len pos)) *2^60*)))
    :hints
    (("Goal" :do-not-induct t))))

(in-theory (disable lrat-flg-int))


(defun lrat-flg-nat-list-until-0 (str len pos lst)

; Read list of natural numbers until a stand-alone "0" is found.
; Return (mv <pos-after-0> <nat-list>)

  (declare (xargs :guard (and (lrat-guard str len pos)
                              (nat-listp lst))
                  :measure (nfix (- len pos))))
  (b* ((pos (u56 pos))
       (len (u56 len))
       ((if (mbe :logic (zp (- len pos))
                 :exec  (>= pos len)))
        (mv len nil))

       ;; Skip spaces
       (pos-after-spaces (u56 (lrat-pos-skip-spaces str len pos)))
       ((unless (< pos-after-spaces len)) (mv len nil))

       ;; Read natural number
       (nat-read (u60 (lrat-nat str len pos-after-spaces)))

       ;; If zero, stop
       ((if (= nat-read 0)) (mv (1+ pos-after-spaces) (my-rev lst)))

       ;; Locate space after number
       (pos-after-nat (find-next-space str len pos-after-spaces))

       ;; For measure
       ((unless (< pos pos-after-nat)) (mv len lst)))

    (lrat-flg-nat-list-until-0 str len pos-after-nat (cons nat-read lst))))

(defthm natp-lrat-flg-nat-list-until-0
  (implies (and (natp len) (natp pos))
           (natp (car (lrat-flg-nat-list-until-0 str len pos lst))))
  :rule-classes :type-prescription)

(defthm bound-lrat-flg-nat-list-until-0
  (implies (and (natp len) (natp pos) (<= pos len))
           (and (<= pos (car (lrat-flg-nat-list-until-0 str len pos lst)))
                (<= (car (lrat-flg-nat-list-until-0 str len pos lst)) len)))
  :rule-classes :linear)

(defthm nat-listp-lrat-flg-nat-list-until-0
  (implies (nat-listp lst)
           (nat-listp (mv-nth 1 (lrat-flg-nat-list-until-0 str len pos lst)))))

(in-theory (disable lrat-flg-nat-list-until-0))


(defun lrat-flg-int-list-until-0 (str len pos lst)

; Read list of INTEGERs until "0" found.
; Return (mv <pos-after-0> <int-list>);  When Error or End, then (= pos len)

  (declare (xargs :guard (and (lrat-guard str len pos)
                              (integer-listp lst))
                  :measure (nfix (- len pos))))
  (b* ((pos (u56 pos))
       (len (u56 len))
       ((if (mbe :logic (zp (- len pos))
                 :exec  (>= pos len)))
        (mv len nil))

       ;; Skip spaces
       (pos-after-spaces (u56 (lrat-pos-skip-spaces str len pos)))
       ((unless (< pos-after-spaces len)) (mv len nil))

       ;; Read integer
       (int-read (s61 (lrat-int str len pos-after-spaces)))

       ;; If zero, stop
       ((if (= int-read 0)) (mv (1+ pos-after-spaces) (my-rev lst)))

       ;; Locate space after number
       (pos-after-int (find-next-space str len pos-after-spaces))

       ;; For measure; !!! This is an error return.
       ((unless (< pos pos-after-int)) (mv len lst)))

    (lrat-flg-int-list-until-0 str len pos-after-int (cons int-read lst))))

(defthm natp-lrat-flg-int-list-until-0
  (implies (and (natp len) (natp pos))
           (natp (car (lrat-flg-int-list-until-0 str len pos lst))))
  :rule-classes :type-prescription)

(defthm bound-lrat-flg-int-list-until-0
  (implies (and (natp len) (natp pos) (<= pos len))
           (and (<= pos (car (lrat-flg-int-list-until-0 str len pos lst)))
                (<= (car (lrat-flg-int-list-until-0 str len pos lst)) len)))
  :rule-classes :linear)

(defthm integer-listp-lrat-flg-int-list-until-0
  (implies (integer-listp lst)
           (integer-listp (mv-nth 1 (lrat-flg-int-list-until-0 str len pos lst)))))

(in-theory (disable lrat-flg-int-list-until-0))



(defun lrat-flg-nat-list-until-0-or-- (str len pos lst)

; Read list of NATs until "0" or "-" found
; Return (mv pos-after-0 nat-list); Finished when (= pos len)

  (declare (xargs :guard (and (lrat-guard str len pos)
                              (nat-listp lst))
                  :measure (nfix (- len pos))))
  (b* ((pos (u56 pos))
       (len (u56 len))
       ((if (mbe :logic (zp (- len pos))
                 :exec  (>= pos len)))
        (mv len nil))

       ;; Skip spaces
       (pos-after-spaces (u56 (lrat-pos-skip-spaces str len pos)))
       ((unless (< pos-after-spaces len)) (mv pos lst))

       ;; If "-", then advance POS and return list of naturals
       (ch (char str pos-after-spaces))
       ((if (eql ch #\-)) (mv (1+ pos-after-spaces) (my-rev lst)))

       ;; Read natural number
       (nat-read (u60 (lrat-nat str len pos-after-spaces)))

       ;; If zero, stop, and return list of naturals
       ((if (= nat-read 0)) (mv pos-after-spaces (my-rev lst)))

       ;; Locate space after number
       (pos-after-nat (find-next-space str len pos-after-spaces))

       ;; For measure
       ((unless (< pos pos-after-nat)) (mv len lst)))

    (lrat-flg-nat-list-until-0-or-- str len pos-after-nat (cons nat-read lst))))

(defthm natp-lrat-flg-nat-list-until-0-or--
  (implies (and (natp len) (natp pos))
           (natp (car (lrat-flg-nat-list-until-0-or-- str len pos lst))))
  :rule-classes :type-prescription)

(defthm bound-lrat-flg-nat-list-until-0-or--
  (implies (and (natp len) (natp pos) (<= pos len))
           (and (<= pos (car (lrat-flg-nat-list-until-0-or-- str len pos lst)))
                (<= (car (lrat-flg-nat-list-until-0-or-- str len pos lst)) len)))
  :rule-classes :linear)

(defthm nat-listp-lrat-flg-nat-list-until-0-or--
  (implies (nat-listp lst)
           (nat-listp (mv-nth 1 (lrat-flg-nat-list-until-0-or-- str len pos lst)))))

(in-theory (disable lrat-flg-nat-list-until-0-or--))


(defun lrat-flg-nat-rev-list-of-lists-until-0-or-- (str len pos lst)

; Read sequence of integer lists separated with "-" (which makes it
; look like a negative number)

; Returns (mv pos list-of-nat-list)

  (declare (xargs :guard (and (lrat-guard str len pos)
                              (list-of-integer-listp lst))
                  :measure (nfix (- len pos))))
  (b* ((pos (u56 pos))
       (len (u56 len))
       ((if (mbe :logic (zp (- len pos))
                 :exec  (>= pos len)))
        (mv len nil))

       ;; At #\0 char?
       (ch (char str pos))
       ((if (eql ch #\0)) (mv (1+ pos) lst))

       ;; Gather next list of natural numbers until "-" or 0 (at end of line)
       ((mv pos-after-int-list int-list)
        (lrat-flg-nat-list-until-0-or-- str len pos nil))

       ;; End-of-File?
       (pos-after-int-list (u56 pos-after-int-list))
       ((unless (< pos pos-after-int-list)) (mv len nil)))

      (lrat-flg-nat-rev-list-of-lists-until-0-or--
       str len pos-after-int-list (cons int-list lst))))

(defthm natp-lrat-flg-nat-rev-list-of-lists-until-0-or--
  (implies
   (and (natp len) (natp pos))
   (natp (car (lrat-flg-nat-rev-list-of-lists-until-0-or-- str len pos lst))))
  :rule-classes :type-prescription)

(defthm bound-lrat-flg-nat-rev-list-of-lists-until-0-or--
  (implies
   (and (natp len) (natp pos) (<= pos len))
   (and (<= pos (car (lrat-flg-nat-rev-list-of-lists-until-0-or-- str len pos lst)))
        (<= (car (lrat-flg-nat-rev-list-of-lists-until-0-or-- str len pos lst)) len)))
  :rule-classes :linear)

(defthm list-integer-listp-lrat-flg-nat-rev-list-of-lists-until-0-or--
  (implies (list-of-integer-listp lst)
           (list-of-integer-listp
            (mv-nth 1 (lrat-flg-nat-rev-list-of-lists-until-0-or-- str len pos lst)))))

(in-theory (disable lrat-flg-nat-rev-list-of-lists-until-0-or--))



(defun lrat-line-mv (str len pos)
  (declare (xargs :guard (lrat-guard str len pos)))

; Expects POS at beginning of line
; Leaves  POS at #\Newline or :eof
; Reads the next line or returns NIL.

  (b* ((pos (u56 pos))
       (len (u56 len))

       ;; Read proof step
       (proof-step (lrat-nat str len pos))
       (pos-after-proof-step (u56 (find-next-space str len pos)))
       ((unless (< pos-after-proof-step len)) (mv len nil))

       ;; Check for exactly one space character
       (ch (char str pos-after-proof-step))
       ((unless (eql ch #\Space)) (mv len nil))
       (pos-after-proof-step+1 (u56 (1+ pos-after-proof-step)))

       ;; Deletion command if ch is #\d
       ((unless (< pos-after-proof-step+1 len)) (mv len nil))
       (ch (char str pos-after-proof-step+1)))

    (if (eql ch #\d)

        ;; proof-step d <nat>* 0
        ;; Deletion:  (cons T <nat>*)
        (b* ((pos-after-proof-step+2 (u56 (1+ pos-after-proof-step+1)))
             ((unless (< pos-after-proof-step+2 len)) (mv len nil))

             ;; Check for space
             (ch (char str pos-after-proof-step+2))
             ((unless (eql ch #\Space)) (mv len nil))

             ;; Read list of naturals until 0
             ((mv pos-after-nat-list nat-list)
              (lrat-flg-nat-list-until-0 str len pos-after-proof-step+2 nil))

             ;; POS-AFTER-NAT-LIST should now be pointing at #\Newline

             ((unless (< pos pos-after-nat-list)) (mv len nat-list))
             ((unless (< pos-after-nat-list len)) (mv len nat-list)))

          (mv pos-after-nat-list (cons t nat-list)))

      ;; proof-step int* 0 <nats-until-0-or-first-neg-number> <negative-int nat*|0>*
      ;; Addition:  (list proof-step <list-of-ints> (rev <list-of-ints>))

      (b* (((mv pos-after-int-list-1 int-list-1)
            (lrat-flg-int-list-until-0 str len pos-after-proof-step+1 nil))
           ((unless (< pos-after-int-list-1 len)) (mv len (cons proof-step int-list-1)))

           ((mv pos-after-int-list-2 int-list-2)
            (lrat-flg-nat-list-until-0-or-- str len pos-after-int-list-1 nil))
           ((unless (< pos-after-int-list-2 len)) (mv len int-list-2))

           ((mv pos-after-int-list-3 int-list-3)
            (lrat-flg-nat-rev-list-of-lists-until-0-or--
             str len pos-after-int-list-2 nil))

           ;; POS-AFTER-INT-LIST-3 should now be pointing at #\Newline

           ((unless (< pos pos-after-int-list-3)) (mv len int-list-3))
           ((unless (< pos-after-int-list-3 len)) (mv len int-list-3))

           ;; Put the answer together
           (ans (cons (cons proof-step int-list-1)
                      (cons int-list-2 int-list-3))))

        (mv pos-after-int-list-3 ans)))))

(defthm natp-lrat-line-mv
  (implies (and (natp len) (natp pos))
           (natp (car (lrat-line-mv str len pos))))
  :rule-classes :type-prescription)

(defthm bound-1-lrat-line-mv
  (implies (and (natp len) (natp pos) (< pos len))
           (and (< pos (car (lrat-line-mv str len pos)))
                (<= (car (lrat-line-mv str len pos)) len)))
  :rule-classes :linear)

(defthm bound-2-lrat-line-mv
  (implies (and (natp len) (natp pos) (<= pos len))
           (and (<= pos (car (lrat-line-mv str len pos)))
                (<= (car (lrat-line-mv str len pos)) len)))
  :rule-classes :linear)

(in-theory (disable lrat-line-mv))


(defun lrat-str-mv (str len pos rev-lines)
  (declare (xargs :guard (lrat-guard str len pos)
                  :measure (nfix (- len pos))))

  (b* ((pos (u56 pos))
       (len (u56 len))
       ((if (mbe :logic (zp (- len pos))
                 :exec  (>= pos len)))
        (mv len rev-lines))

       ;; Process line
       ((mv pos-after-line line) (lrat-line-mv str len pos))
       (more-rev-lines (cons line rev-lines))

       ;; Advance pointer
       ((unless (< pos-after-line len)) (mv len more-rev-lines))
       (pos-after-line+1 (u56 (1+ pos-after-line)))

       ;; Check that POS(ition) advanced -- for measure
       ((unless (< pos pos-after-line+1)) (mv len rev-lines)))

    (lrat-str-mv str len pos-after-line+1 more-rev-lines)))


(defun lrat-str (str len pos rev-lines)
  (declare (xargs :guard (lrat-guard str len pos)
                  :measure (nfix (- len pos))))

  (b* ((pos (u56 pos))
       (len (u56 len))
       ((if (mbe :logic (zp (- len pos))
                 :exec  (>= pos len)))
        rev-lines)

       ;; Process line
       ((mv pos-after-line line) (lrat-line-mv str len pos))
       (more-rev-lines (cons line rev-lines))

       ;; Advance pointer
       ((unless (< pos-after-line len)) more-rev-lines)
       (pos-after-line+1 (u56 (1+ pos-after-line)))

       ;; Check that POS(ition) advanced -- for measure
       ((unless (< pos pos-after-line+1)) rev-lines))

    (lrat-str str len pos-after-line+1 more-rev-lines)))


(defun pos-at-eol (str len pos)

; Find position at end of line.

  (declare (xargs :guard (lrat-guard str len pos)
                  :measure (nfix (- len pos)))
           (type string str))
  (let ((len (u56 len))
        (pos (u56 pos)))
    (if (mbe :logic (zp (- len pos))
             :exec  (>= pos len))
        len
      (if (eql (char str pos) #\Newline)
          pos
        (pos-at-eol str len (u56 (1+ pos)))))))

(defthm natp-pos-at-eol
  (implies (and (natp pos) (natp len))
           (natp (pos-at-eol str len pos)))
  :hints
  (("Goal" :induct (pos-at-eol str len pos)))
  :rule-classes :type-prescription)

(defthm bound-pos-at-eol
  (implies (lrat-guard str len pos)
           (and (<= pos (pos-at-eol str len pos))
                (<= (pos-at-eol str len pos) len)))
  :hints
  (("Goal"
    :induct (pos-at-eol str len pos)
    :in-theory (disable not length)))
  :rule-classes :linear)

(in-theory (disable pos-at-eol))


(defun pos-at-next-digit-or-minus-char (str len pos)

; Find position of next integer -- skipping blank and comment lines.
; Return LEN if none.  LEN is often return when we discover an error.
; Assuming the LEN is (length STR), (char STR LEN) will not pass a
; guard check.

  (declare (xargs :guard (lrat-guard str len pos)
                  :measure (nfix (- len pos))))
  (let ((len (u56 len))
        (pos (u56 pos)))
    (if (mbe :logic (zp (- len pos))
             :exec  (>= pos len))
        len
      (let ((ch (char str pos)))

        ;; !!! Investigate removing this test...
        (if (or (eql ch #\c)  ; CNF-style comments
                (eql ch #\C))

            ;; Skip comment lines...
            (let ((pos-at-eol (pos-at-eol str len pos)))
              (if (< pos (u56 pos-at-eol))
                  (pos-at-next-digit-or-minus-char str len pos-at-eol)
                len))

          (let* ((digit? (char-to-nat ch))
                 (minus-ch (eql ch #\-)))
            (if (or digit? minus-ch)
                pos
              (pos-at-next-digit-or-minus-char str len (u56 (1+ pos))))))))))

(defthm natp-pos-at-next-digit-or-minus-char
  (implies (and (natp pos) (natp len))
           (natp (pos-at-next-digit-or-minus-char str len pos)))
  :hints
  (("Goal" :induct (pos-at-next-digit-or-minus-char str len pos)))
  :rule-classes :type-prescription)

(defthm bound-pos-at-next-digit-or-minus-char
  (implies (lrat-guard str len pos)
           (and (<= pos (pos-at-next-digit-or-minus-char str len pos))
                (<= (pos-at-next-digit-or-minus-char str len pos) len)))
  :hints
  (("Goal"
    :induct (pos-at-next-digit-or-minus-char str len pos)
    :in-theory (disable not length)))
  :rule-classes :linear)

(in-theory (disable pos-at-next-digit-or-minus-char))


(defun lrat-read-file (file-name state)

; Returns a formula data structure, i.e., a list of (index . clause) pairs.
; See formula-p in list-based/lrat-checker.lisp.  Note that the formula is
; _not_ a fast-alist.

  (declare (xargs :guard (stringp file-name)
                  :guard-hints
                  (("Goal" :in-theory (disable acl2::read-file-into-string2)))
                  :stobjs (state)))
  (b* ((str (read-file-into-string file-name))
       ((unless (stringp str))
        (prog2$ (er hard? 'lrat-read-file
                    "lrat-read-file: (read-file-into-string ~x0 .~%" file-name)
                nil))
       (len (length str))
       ((unless (< len *2^56*))
        (prog2$ (er hard? 'lrat-read-file
                    "lrat-read-file:  file ~x0 is too long.~%" file-name)
                nil))
       ;; Skip forward until the beginning of an integer
       (start (pos-at-next-digit-or-minus-char str len 0))
       ((unless (lrat-guard str len start))
        (prog2$ (er hard? 'cnf-read-file
                    "lrat-read-file:  LRAT-GUARD fails.~%")
                nil))
       (rev-lines (lrat-str str len start nil)))
    rev-lines))


(defun cnf-str (str len pos line-num rev-lines)

; Read a string containing a CNF formula, returns a list of
; adddition/deletion commands

; !!! For GCL, check whether a DECLARATION is required for U56, etc.
; !!! Have a look at: ../books/projects/sat/dimacs-reader/reader.lisp
; !!! Write formal spec and make it available on the Web (after
; !!! 4/10/17).

  (declare (xargs :guard (and (lrat-guard str len pos)
                              (natp line-num))
                  :measure (nfix (- len pos))))
  (b* ((len (u56 len))
       (pos (u56 pos))

       ;; End of file?
       ((if (mbe :logic (zp (- len pos))
                 :exec  (>= pos len)))
        (mv len rev-lines))

       ;; Read list of integers until 0
       ((mv pos-after-int-list int-list)
        (lrat-flg-int-list-until-0 str len pos nil))

       ;; If nothing read or at EOF, return what there is
       ((unless (and (< pos pos-after-int-list)
                     (< pos-after-int-list len)))
        (mv len rev-lines))

       ;; Find the next digit, possibly skipping blank lines
       (pos-at-next-digit-or-minus-char
        (pos-at-next-digit-or-minus-char str len pos-after-int-list))
       ((unless (< pos-after-int-list
                   pos-at-next-digit-or-minus-char))
        (mv len rev-lines))

       ;; Collect this line, and continue...
       (line (cons line-num int-list)))
    (cnf-str str len pos-at-next-digit-or-minus-char (1+ line-num)
             (cons line rev-lines))))

(defthm natp-cnf-str
  (implies (and (natp len) (natp pos))
           (natp (car (cnf-str str len pos line-num rev-lines))))
  :rule-classes :type-prescription)

(defthm bound-cnf-str
  (implies (and (natp len) (natp pos) (<= pos len))
           (and (<= pos (car (cnf-str str len pos line-num rev-lines)))
                (<= (car (cnf-str str len pos line-num rev-lines)) len)))
  :hints
  (("Goal"
    :induct (cnf-str str len pos line-num rev-lines)
    :in-theory (disable not length)))
  :rule-classes :linear)

(in-theory (disable cnf-str))


(defun cnf-read-file (file-name state)

; Returns a formula data structure, i.e., a list of (index . clause) pairs.
; See formula-p in list-based/lrat-checker.lisp.  Note that the formula is
; _not_ a fast-alist.

  (declare (xargs :guard (stringp file-name)
                  :guard-hints
                  (("Goal" :in-theory (disable acl2::read-file-into-string2)))
                  :stobjs (state)))
  (b* ((str (read-file-into-string file-name))
       ((unless (stringp str))
        (prog2$ (er hard? 'cnf-read-file
                    "Unable to read file ~x0." file-name)
                nil))
       (len (length str))
       ((unless (< len *2^56*))
        (prog2$ (er hard? 'cnf-read-file
                    "File ~x0 has length ~x1, which exceeds the maximum of ~
                     2^56-1 (~x2)."
                    file-name len (1- *2^56*))
                nil))
       ;; Skip forward until the beginning of an integer
       (start (pos-at-next-digit-or-minus-char str len 0))
       ((unless (lrat-guard str len start))
        (prog2$ (er hard? 'cnf-read-file
                    "Possible implementation error: LRAT-GUARD failed.~%")
                nil))
       ((mv & rev-lines)
         (cnf-str str len start 1 nil)))
    rev-lines))


; CLRAT Parser

; We now turn our attention to reading Compressed LRAT (CLRAT) files.
; CLRAT files are binary files and must be processed character by
; character.  CLRAT files are broken into lines by reading until one 0
; is found (when reading a deletion command) or until a second 0 is
; found (when reading an addition command).


(defun pos-after-clrat-nums (str len pos)

; Return POS after reading compressed numbers.  The last byte of a
; command is 0; thus we find a byte containing 0 -- and then, we
; finally return the POSition just after the zero byte.

  (declare (xargs :guard (lrat-guard str len pos)
                  :measure (nfix (- len pos))))
  ;; Declare LEN and POS to be56-bit natural (fixnum) numbers.
  (b*  ((pos (u56 pos))
        (len (u56 len))

        ;; If at end of string, then stop
        ((if (mbe :logic (zp (- len pos))
                  :exec  (>= pos len)))
         len) ; Report at end of STR.

        (ch (char str pos))
        (byte (u08 (char-code ch)))
        (zero (= byte 0))
        (pos+1 (u56 (1+ pos)))
        ((if zero) pos+1))
     (pos-after-clrat-nums str len pos+1)))

(defthm natp-pos-after-clrat-nums
  (implies (and (natp len) (natp pos))
           (natp (pos-after-clrat-nums str len pos)))
  :rule-classes :type-prescription)

(defthm bound-pos-after-clrat-nums
  (implies (and (natp len) (natp pos) (<= pos len))
           (and (<= pos (pos-after-clrat-nums str len pos))
                (<= (pos-after-clrat-nums str len pos) len)))
  :rule-classes :linear)

(in-theory (disable pos-after-clrat-nums))


(with-arithmetic-help-5
 (defun clrat-nat (str len pos n shift)

; Read compressed natural number from string; return number read.

   (declare (xargs :guard (and (lrat-guard str len pos)
                               (natp n)
                               (< n *2^60*)
                               (natp shift)
                               (< shift 53))
                   :measure (nfix (- len pos))))
   ;; Expecting little-endian numbers
   (b* ((pos (u56 pos))
        (len (u56 len))
        (n   (u60 n))

        ;; Maximum clause number permitted
        ((unless (< n *2^52*)) 0)
        (n (u52 n))

        ;; End of string?  Terminate read
        ((if (mbe :logic (zp (- len pos))
                  :exec  (>= pos len))) n) ; At end of str

        (ch (char str pos))
        (byte (u08 (char-code ch)))

        (7-bit-num (u07 (logand byte 127)))
        ;; MATT, math issue
        (placed-7-bit-num (u60 (n60 (ash 7-bit-num shift))))

        ;; End of natural number?
        (n (u60 (n60 (+ placed-7-bit-num n)))) ;; MATT, math issue
        (more (= (logand byte 128) 128))
        ((unless more) n)

        (pos+1 (u56 (1+ pos)))
        (next-shift (+ shift 7))

        ;; If number collect so far is too big, stop read
        ((if (not (< next-shift 53))) n))
     (clrat-nat str len pos+1 n (+ shift 7)))))

(defthm natp-clrat-nat
  (implies (natp n)
           (natp (clrat-nat str len pos n shift)))
  :rule-classes :type-prescription)

(defthm bound-clrat-nat
  (implies (and (natp n)
                (< n *2^60*)
                (< shift 53))
           (and (<= 0 (clrat-nat str len pos n shift))
                (< (clrat-nat str len pos n shift) *2^60*)))
  :rule-classes :linear)

(in-theory (disable clrat-nat))


(defun clrat-int (str len pos)

; Read compressed integer from STR starting at position POS.  Return
; integer read.

  (declare (xargs :guard (lrat-guard str len pos)))
  (b* ((pos (u56 pos))
       (len (u56 len))

       ;; If already at end, return 0.
       ((unless (< pos len)) 0)
       (ch (char str pos))
       (byte (u08 (char-code ch)))

       ((if (= byte 0)) 0)  ; MATT:  Check not needed; guard proof easier
       (sign (= (logand byte 1) 1))
       (more (= (logand byte 128) 128))
       (6-bit-num (u06 (logand (ash byte -1) 63)))

       ((unless (mbt (<= 6-bit-num 63))) 0) ; MATT:  Another guard issue
       ((unless more) (s07 (if sign (- 6-bit-num) 6-bit-num)))

       ;; Prematurely at end?
       (pos+1 (u56 (1+ pos)))
       ((unless (< pos+1 len)) 0)

       (all-of-nat (u60 (clrat-nat str len pos+1 6-bit-num 6))))
    (s61 (if sign (- all-of-nat) all-of-nat))))

(defthm integerp-clrat-int
  (integerp (clrat-int str len pos))
  :rule-classes :type-prescription)

(defthm bound-clrat-int
  (and (<= (- *2^60*) (clrat-int str len pos))
       (< (clrat-int str len pos) *2^60*))
  :rule-classes :linear)

(in-theory (disable clrat-int))


(defun clrat-zero (str len pos)

; Find next zero (starting at POS) in STR, where STR contains
; compressed list of numbers.  Return position of that next zero.

  (declare (xargs :guard (lrat-guard str len pos)
                  :measure (nfix (- len pos))))
  (b* ((pos (u56 pos))
       (len (u56 len))

       ;; At End of STR?  If so, return position at end of STR.
       ((if (mbe :logic (zp (- len pos))
                 :exec  (>= pos len)))
        len)

       (ch (char str pos))
       (byte (u08 (char-code ch)))
       (zero (= byte 0))
       ((if zero) pos)
       (pos+1 (u56 (1+ pos))))
    (clrat-zero str len pos+1)))

(defthm natp-clrat-zero
  (implies (and (natp len) (natp pos))
           (natp (clrat-zero str len pos)))
  :rule-classes :type-prescription)

(defthm bound-clrat-zero
  (implies (and (natp pos) (< pos len))
           (and (<= pos (clrat-zero str len pos))
                (<= (clrat-zero str len pos) len)))
  :rule-classes :linear)

(in-theory (disable clrat-zero))


(defun-inline clrat-zero+1 (str len pos)
  (declare (xargs :guard (lrat-guard str len pos)))
  (b* ((clrat-zero (clrat-zero str len pos))
       ((unless (< clrat-zero len)) len))
    (1+ clrat-zero)))

(defthm natp-clrat-zero+1
  (implies (and (natp len) (natp pos))
           (natp (clrat-zero+1 str len pos)))
  :rule-classes :type-prescription)

(defthm bound-clrat-zero+1
  (implies (and (natp pos) (natp len) (< pos len))
           (and (< pos (clrat-zero+1 str len pos))
                (<= (clrat-zero+1 str len pos) len)))
  :rule-classes :linear)

(in-theory (disable clrat-zero+1))


(defun clrat-int-end+1 (str len pos)

; Return the position in STR just after the end of a compressed integer.

  (declare (xargs :guard (lrat-guard str len pos)
                  :measure (nfix (- len pos))))
  (b* ((pos (u56 pos))
       (len (u56 len))

       ;; At end of STR?  If so, return end position
       ((if (mbe :logic (zp (- len pos))
                 :exec  (>= pos len)))
        len) ; At end of str

       (ch (char str pos))
       (byte (u08 (char-code ch)))
       (more (<= 128 byte))
       (pos+1 (u56 (1+ pos)))

       ;; If POS points to last byte of compressed number, quit
       ((if (not more)) pos+1))
    (clrat-int-end+1 str len pos+1)))

(defthm natp-clrat-int-end+1
  (implies (and (natp len) (natp pos))
           (natp (clrat-int-end+1 str len pos)))
  :rule-classes :type-prescription)

(defthm bound-clrat-int-end+1
  (implies (< pos len)
           (and (< pos (clrat-int-end+1 str len pos))
                (<= (clrat-int-end+1 str len pos) len)))
  :rule-classes :linear)

(in-theory (disable clrat-int-end+1))


(with-arithmetic-help-5
 (defun clrat-ints (str len pos ints)

; Read list of compressed integers from STR, until a 0 is discovered

   (declare (xargs :guard (lrat-guard str len pos)
                   :measure (nfix (- len pos))))
   (b* ((pos (u56 pos))
        (len (u56 len))

        ;; If at end of string, quit
        ((if (mbe :logic (zp (- len pos))
                  :exec  (>= pos len)))
         nil)

        (ch (char str pos))
        (byte (u08 (char-code ch)))

        ;; If zero discovered, return list of integers
        ((if (= byte 0)) (my-rev ints))

        (int (s61 (clrat-int str len pos)))
        (pos-nx-int (clrat-int-end+1 str len pos))

        ;; MATT -- A strange requirement
        ((unless (mbt (< pos pos-nx-int))) nil))

     (clrat-ints str len pos-nx-int (cons int ints)))))

(defthm integer-listp-clrat-ints
  (implies (integer-listp ints)
           (integer-listp (clrat-ints str len pos ints))))

(in-theory (disable clrat-ints))


(defun cut-at-first-negative-integer (lst)

; Collect natural numbers from integer list until first negative
; number encountered.

  (declare (xargs :guard (integer-listp lst)))
  (if (atom lst)
      nil
    (let ((int (car lst)))
      (if (< int 0)
          nil
        (cons int
              (cut-at-first-negative-integer (cdr lst)))))))

(defthm nat-listp-cut-at-first-negative-integer
  (implies (integer-listp lst)
           (nat-listp (cut-at-first-negative-integer lst))))

(in-theory (disable cut-at-first-negative-integer))


(defun delete-until-first-negative-integer (lst)

; Eliminate natural numbers in LST until first negative number; return
; remainder of LST (including negative number discovered).

  (declare (xargs :guard (integer-listp lst)))
  (if (atom lst)
      nil
    (let ((int (car lst)))
      (if (< int 0)
          lst
        (delete-until-first-negative-integer (cdr lst))))))

(defthm integer-listp-delete-until-first-negative-integer
  (implies (integer-listp x)
           (integer-listp (delete-until-first-negative-integer x))))

(defthm acl2-count-delete-until-first-negative-integer
  (<= (acl2-count (delete-until-first-negative-integer lst))
      (acl2-count lst))
  :rule-classes :linear)

(in-theory (disable delete-until-first-negative-integer))


(defun break-into-lists-at-negative-integers (lst rat-list)

; When "standing" at negative integer (in LST); negate first integer
; and collect up to next negative integer or to end of LST.  Finally,
; return result being collected in RAT-LIST.

  (declare (xargs :guard (and (integer-listp lst)
                              (list-of-integer-listp rat-list))))
  (declare (xargs :guard (integer-listp lst)))
  (if (atom lst)
      rat-list
    (b* ((rest-of-rat (cut-at-first-negative-integer (cdr lst)))
         (rat-clause (cons (- (car lst)) rest-of-rat))
         (rest-of-rats (delete-until-first-negative-integer (cdr lst))))
      (break-into-lists-at-negative-integers rest-of-rats (cons rat-clause rat-list)))))


(defun clrat-read-a-line (str len pos)

; Read addition command.  Returns (MV <new-pos> <command>) where
; <new-pos> is the position in the string after this <command>.

  (declare (xargs :guard (lrat-guard str len pos)))
  (b* ((pos (u56 pos))
       (len (u56 len))

       ;; Read until first zero
       (line-clause-ints (clrat-ints str len pos nil))
       (pos-after-first-zero (pos-after-clrat-nums str len pos))

       ;; If first zero isn't discovered before end of string
       ((unless (< pos-after-first-zero len)) (mv len nil))

       ;; Read until second zero
       (rup-rat-ints (clrat-ints str len pos-after-first-zero nil))
       (pos-after-second-zero (pos-after-clrat-nums str len pos-after-first-zero))

       ;; If second zero isn't discovered before end of string
       ((unless (<= pos-after-second-zero len)) (mv len nil))

       ;;If nothing read, then short-circuit end
       ((unless (consp line-clause-ints)) (mv len nil))

       ;; Cut second part of line
       (rup (cut-at-first-negative-integer rup-rat-ints))
       (rat-list (delete-until-first-negative-integer rup-rat-ints))

       ;; Reverse RAT clauses
       (rev-rat-lists (break-into-lists-at-negative-integers rat-list nil)))

    (mv pos-after-second-zero
        ;; Create return result
        (cons line-clause-ints
              (cons rup rev-rat-lists)))))

(defthm natp-car-clrat-read-a-line
  (implies (and (natp len) (natp pos))
           (natp (car (clrat-read-a-line str len pos))))
  :rule-classes :type-prescription)

(defthm bound-car-clrat-read-a-line
  (implies (and (natp len) (natp pos) (<= pos len))
           (and (<= pos (car (clrat-read-a-line str len pos)))
                (<= (car (clrat-read-a-line str len pos)) len)))
  :rule-classes :linear)

(in-theory (disable clrat-read-a-line))


(defun clrat-line-mv (str len pos)

; Read either an addition or a deletion line of a LRAT proof.  Returns
; (MV <new-pos> <command>) where <new-pos> is the position in the
; string after the addition/deletion <command>.

  (declare (xargs :guard (lrat-guard str len pos)))
  (b* ((pos (u56 pos))
       (len (u56 len))

       ;; Read character
       ((unless (< pos len)) (mv len nil))
       (ch (char str pos))

       ;; Bump read position
       (pos+1 (u56 (1+ pos)))
       ((unless (< pos+1 len)) (mv len nil)))

    (if (eql ch #\d) ; Deletion or Addition line

        ;; proof-step d <nat>* 0
        (b* ((dlst (clrat-ints str len pos+1 nil))
             (pos-after-dlst (u56 (clrat-zero+1 str len pos+1))))
          (mv pos-after-dlst (cons t dlst)))

      ;; proof-step a <nat>* 0 nat* {-int nat*}*
      (clrat-read-a-line str len pos+1))))

(defthm natp-car-clrat-line-mv
  (implies (and (natp len) (natp pos))
           (natp (car (clrat-line-mv str len pos))))
  :rule-classes :type-prescription)

(defthm bound-car-clrat-line-mv
  (implies (and (natp len) (natp pos) (<= pos len))
           (and (<= pos (car (clrat-line-mv str len pos)))
                (<= (car (clrat-line-mv str len pos)) len)))
  :rule-classes :linear)

(in-theory (disable clrat-line-mv))


(defun skip-clrat-command (str len pos)
  (declare (xargs :guard (lrat-guard str len pos)))
  (b* ((pos (u56 pos))
       (len (u56 len))

       ;; Read character
       ((unless (< pos len)) len)
       (ch (char str pos))

       ;; Bump read position
       (pos+1 (u56 (1+ pos)))
       ((unless (< pos+1 len)) len)

       ;; Position after first zero
       (pos-after-zero (clrat-zero+1 str len pos+1))
       ((unless (< pos-after-zero len)) len))

    (if (eql ch #\d) ; Deletion or Addition line
        pos-after-zero
      (clrat-zero+1 str len pos-after-zero))))

(defthm natp-skip-clrat-command
  (implies (and (natp pos) (natp len))
           (natp (skip-clrat-command str len pos)))
  :rule-classes :type-prescription)

(defthm bound-skip-clrat-command
  (implies (and (natp pos) (natp len) (<= pos len))
           (and (<= pos (skip-clrat-command str len pos))
                (<= (skip-clrat-command str len pos) len)))
  :rule-classes :linear)

(in-theory (disable skip-clrat-command))


(defun skip-clrat-commands (str len pos)
  (declare (xargs :guard (lrat-guard str len pos)
                   :measure (nfix (- len pos))))
   (b* ((pos (u56 pos))
        (len (u56 len))

        ;; If at end of string, quit
        ((if (mbe :logic (zp (- len pos))
                  :exec  (>= pos len)))
         len)

        (next-cmd-pos (skip-clrat-command str len pos))
        ((unless (< next-cmd-pos len)) pos)

        ;; If SKIP-CLRAT-COMMAND finishes at end -- not supposed to, then stop
        ((unless (< pos next-cmd-pos)) len))
    (skip-clrat-commands str len next-cmd-pos)))

(defthm natp-skip-clrat-commands
  (implies (and (natp pos) (natp len))
           (natp (skip-clrat-commands str len pos)))
  :rule-classes :type-prescription)

(defthm bound-skip-clrat-commands
  (implies (and (natp pos) (natp len) (<= pos len))
           (and (<= pos (skip-clrat-commands str len pos))
                (<= (skip-clrat-commands str len pos) len)))
  :rule-classes :linear)

(in-theory (disable skip-clrat-commands))



(defun clrat-read-some-lines
    (str       ; STRing to be read
     len       ; LENgth of STRing
     pos       ; POSition where processing STRing
     commands) ; Commands collected for processing

; Finally returns:
;    suffix    ; STRing remainder, if any; cut might be between commands
;  commands    ; Addition and Deletion commands

  (declare (xargs :guard (and (lrat-guard str len pos)
                              (true-listp commands))
                  :measure (nfix (- len pos))))
  (b* ((pos (u56 pos))
       (len (u56 len))

       ;; End of STRing?  If so, return what commands we have
       ((if (mbe :logic (zp (- len pos))
                 :exec  (>= pos len)))
        (mv "" (my-rev commands)))

       ;; Insufficient characters for command?  Return STRing fragment
       ;; and commands.
       (ch (char str pos))
       (next-zero (clrat-zero str len pos))
       (insufficient (if (eql ch #\d)
                         (>= next-zero len)
                       (or (>= (1+ next-zero) len)
                           (>= (clrat-zero str len (1+ next-zero)) len))))
       ((if insufficient)
        (mv (subseq str pos len) (my-rev commands)))

       ;; Read a line, either an addition or deletion command
       ((mv new-pos command)
        (clrat-line-mv str len pos))

       ;; No progress?  Error!  Return what we have.
       (new-pos (u56 new-pos))
       ((unless (< pos new-pos)) (mv "" (my-rev commands))))

    (clrat-read-some-lines str len new-pos
                           (cons command commands))))

(defthm true-listp-clrat-read-some-lines
  (implies (true-listp commands)
           (true-listp (mv-nth 1 (clrat-read-some-lines str len pos commands)))))

(in-theory (disable clrat-read-some-lines))


(defun matts-incremental-proof-command-checker (commands)
  (declare (xargs :guard (true-listp commands)))
  (if (atom commands)
      T
    (and t ; (proof-command-ok (car commands))
         (matts-incremental-proof-command-checker (cdr commands)))))

(in-theory (disable matts-incremental-proof-command-checker))


(defmacro clrat-read-next-er (str &rest args)
  `(mv (er hard? 'clrat-read-next
           ,str
           ,@args)
       ""
       position))

(defun clrat-read-next-1 (filename    ; Filename
                          position    ; Position in file
                          read-amount ; Amount to read from file
                          suffix      ; Left over from previous chunck
                          file-length ; Length of filename contents
                          state)

; First read the contents of filename starting from position,
; returning a string of length read-amount (or less if end-of-file is
; reached).  Return (mv proof suffix), where proof is a list of
; add-step records parsed from that string and suffix is the final
; portion of that string that is unused in that parse.  However, in
; the case of error return (mv nil nil) instead.

  (declare (xargs :guard (and (stringp filename)
                              (natp position)
                              (< position *2^56*)
                              (posp read-amount)
                              (stringp suffix)
                              (natp file-length))
                  :stobjs (state)
                  :guard-hints
                  (("Goal" :in-theory (disable acl2::read-file-into-string2)))))
  (b* (((unless (mbt (and (stringp filename)
                          (natp position)
                          (< position *2^56*)
                          (posp read-amount)
                          (stringp suffix)
                          (natp file-length))))
        ;; Guard finesse
        (clrat-read-next-er "Illegal arguments.~%"))

       (input-chars (read-file-into-string
                     filename
                     :start position
                     :bytes read-amount))

       ((unless input-chars) ; !! Don't we know it's a string?
        (clrat-read-next-er "Input file problem.~%"))

       ((unless (stringp input-chars)) ; !! Don't we know it's a string?
        (clrat-read-next-er "Input from file not a string.~%"))

       (new-position (+ position read-amount))
       (final
        (and (= new-position file-length)

             ;; Then close the stream.  (As of 9/2022 we could use :close t,
             ;; but the following still works.)

             (read-file-into-string
              filename
              :start file-length
              :bytes 1)))

       ((if (and final (not (equal final ""))))
        (clrat-read-next-er "Implementation error: expected final read to ~
                             yield \"\".~%"))

       (str (if (equal suffix "") ; optimization
                input-chars
              (string-append suffix input-chars)))
       (len (length str))

       ((unless (< len *2^56*)) ; !! presumably we know len <= read-amount
        (clrat-read-next-er "String too long to process."))

       ;; If both SUFFIX and INPUT-CHARS empty, then finished:

       ((if (= len 0))
        (mv nil "" position))
       ((mv suffix commands)
        (clrat-read-some-lines str len 0 nil))

       ((unless (stringp suffix))
        (clrat-read-next-er "Bad suffix returned from CLRAT-READ-SOME-LINES"))
       ((unless (< (+ position read-amount) *2^56*))
        (clrat-read-next-er "Proof file too big")))

    (mv commands suffix new-position)))


(defun clrat-read-next (filename    ; Filename
                        position    ; Position in file
                        read-amount ; Amount to read from file
                        suffix      ; Left over from previous chunck
                        file-length ; Length of filename contents
                        state)
  (declare (xargs :guard (and (stringp filename)
                              (natp position)
                              (< position *2^56*)
                              (posp read-amount)
                              (stringp suffix)
                              (natp file-length))
                  :measure (nfix (- file-length position))
                  :hints ; not necessary but greatly reduces processing time
                  (("Goal" :in-theory (disable acl2::read-file-into-string2)))
                  :guard-hints ; also unnecessary, but also greatly reduces time
                  (("Goal" :in-theory
                    (disable open-input-channel
                             open-input-channels
                             acl2::read-file-into-string2)))
                  :stobjs (state)))
  (mv-let (proof new-suffix new-posn)
    (clrat-read-next-1 filename position read-amount suffix file-length state)
    (cond
     ((or proof
          (equal new-suffix "")
          (not (mbt (natp file-length))))
      (mv proof new-suffix new-posn))
     (t (cond
         ((>= new-posn *2^56*) ; extremely unlikely
          (mv proof new-suffix new-posn))
         ((>= new-posn file-length) ; Don't recur; we need to terminate!
          (clrat-read-next-1 filename new-posn read-amount new-suffix file-length
                             state))
         (t
          (clrat-read-next filename new-posn read-amount new-suffix file-length
                           state)))))))

; The rest of this file defines function CLRAT-READ-FILE, which can be
; used to read the entire file into an alleged proof before it is checked.

(defun clrat-read-all-lines-rev (str len pos lines state)

; Read each "line" in a CLRAT-style file.  Returns a reversed list of
; addition and deletion commands.

  (declare (xargs :guard (lrat-guard str len pos)
                  :guard-hints
                  (("Goal" :in-theory (disable length)))
                  :stobjs (state)
                  :measure (nfix (- len pos))))
  (b* ((pos (u56 pos))
       (len (u56 len))

       ;; End of file?  If so, return collected lines
       ((if (mbe :logic (zp (- len pos))
                 :exec  (>= pos len)))
        (mv lines state))

       ;; Read a line, either an addition or deletion instruction
       ((mv new-pos line)
        (clrat-line-mv str len pos))

       ;; Progress?  If not, return what we have
       (new-pos (u56 new-pos))
       ((unless (< pos new-pos))
        (mv lines state)))

    (clrat-read-all-lines-rev str len new-pos (cons line lines) state)))


(defun clrat-read-all-lines (str len pos state)

; Just reverses the lines...

  (declare (xargs :guard (lrat-guard str len pos)
                  :stobjs (state)))
  (b* (((mv lines-rev state)
        (clrat-read-all-lines-rev str len pos nil state)))
    (mv (my-rev lines-rev) state)))


(defun clrat-read-file (file-name state)

; Reads a Compressed-LRAT file nameed FILE-NAME.  Returns a formula
; data structure, i.e., a list of (index . clause) pairs.  See
; formula-p in list-based/lrat-checker.lisp.  Note that the formula is
; _not_ a fast-alist.

  (declare (xargs :guard (stringp file-name)
                  :guard-hints
                  (("Goal" :in-theory (disable acl2::read-file-into-string2)))
                  :stobjs (state)))
  (b* ((str (read-file-into-string file-name))
       (state (increment-file-clock state))
       ((unless (stringp str))
        (mv (er hard? 'clrat-read-file
                    "clrat-read-file: (read-file-into-string ~x0 .~%" file-name)
            state))
       (len (length str))
       ((unless (< len *2^56*))
        (mv (er hard? 'clrat-read-file
                "clrat-read-file:  file ~x0 is too long.~%" file-name)
            state))
       (start 0)
       ((unless (lrat-guard str len start))
        (mv (er hard? 'clrat-read-file
                "clrat-read-file:  LRAT-GUARD fails.~%")
            state))
       (pos start))
    (clrat-read-all-lines str len pos state)))



; For debugging:

(defrec add-step
  ((index . clause)
   .
   (rup-indices . drat-hints))
  t)

(defun show-proof (proof acc)
  (cond ((endp proof) (reverse acc))
        (t (show-proof
            (cdr proof)
            (let ((step (if (eq (caar proof) t)
                            (list :delete (cdar proof))
                          (let* ((x (car proof))
                                 (index (access add-step x :index))
                                 (clause (access add-step x :clause))
                                 (rup-indices (access add-step x :rup-indices))
                                 (drat-hints (access add-step x :drat-hints)))
                            (list :index index
                                  :clause clause
                                  :rup-indices rup-indices
                                  :drat-hints drat-hints)))))
              (cons step acc))))))


; No need to reason about these printing and debugging functions...

(program)
(set-state-ok t)

(defun print-integers (lst end chan state)

; Lst is a list of integers.  Print each element of the list to the given
; channel, following each with a space.  If end is not nil, then print it with
; princ$ too.

  (cond ((endp lst)
         (if end
             (princ$ end chan state)
           state))
        (t (pprogn (princ$ (car lst) chan state)
                   (princ$ #\Space chan state)
                   (print-integers (cdr lst) end chan state)))))

(defun print-drat-hints (drat-hints chan state)
  (cond ((endp drat-hints) state)
        (t (pprogn (princ$ (- (caar drat-hints)) chan state)
                   (princ$ #\Space chan state)
                   (print-integers (cdar drat-hints) nil chan state)
                   (print-drat-hints (cdr drat-hints) chan state)))))


(defun clrat-to-lrat-add-step (add-step chan state)

; Finally, we produce a converter from clrat to lrat, which can be useful for
; viewing clrat files.  For this first cut we read the entire file, but it
; should be easy to deal with chunks if need be.

;  j l1 l2 ... lk 0 d1 d2 ... dm -e1 f11 ... f1{m_1} -e2 f21 ... f2{m_2} ... 0

  (let ((index (access add-step add-step :index))
        (clause (access add-step add-step :clause))
        (rup-indices (access add-step add-step :rup-indices))
        (drat-hints (access add-step add-step :drat-hints)))
    (pprogn (princ$ index chan state)
            (princ$ #\Space chan state)
            (print-integers clause 0 chan state)
            (princ$ #\Space chan state)
            (print-integers rup-indices nil chan state)
            (print-drat-hints (reverse drat-hints) chan state)
            (princ$ 0 chan state)
            (newline chan state))))

(defun clrat-to-lrat-rec (lines last-index chan state)
  (cond ((endp lines) state)
        (t (pprogn (if (eq (caar lines) t) ; deletion
                       (pprogn (princ$ last-index chan state)
                               (princ$ #\Space chan state)
                               (princ$ #\d chan state)
                               (princ$ #\Space chan state)
                               (print-integers (cdar lines) 0 chan state)
                               (newline chan state))
                     (clrat-to-lrat-add-step (car lines) chan state))
                   (clrat-to-lrat-rec (cdr lines)
                                      (access add-step (car lines) :index)
                                      chan
                                      state)))))

(defun clrat-to-lrat (clrat-in-file lrat-out-file state)

; Clrat-in-file is a .clrat input file.  We print out a corresponding .lrat
; file, either to lrat-out-file if that is a string, else to (standard-co
; state).

  (mv-let (lines state)
    (clrat-read-file clrat-in-file state) ; returns a proofp
    (let ((last-index (or (and (consp lines)
                               (eq (car (car lines)) t) ; deletion
                               (consp (cdr lines))
                               (not (eq (car (cadr lines)) t))
                               (1- (access add-step (cadr lines) :index)))
                          0)))
      (pprogn
       (cond ((not (stringp lrat-out-file))
              (clrat-to-lrat-rec lines last-index (standard-co state) state))
             (t (mv-let
                  (chan state)
                  (open-output-channel lrat-out-file :character state)
                  (pprogn (clrat-to-lrat-rec lines last-index chan state)
                          (close-output-channel chan state)))))
       (value :invisible)))))