File: aux-functions.lisp

package info (click to toggle)
acl2 8.6%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 1,111,420 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,976; makefile: 3,833; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (2073 lines) | stat: -rw-r--r-- 67,535 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
; RP-REWRITER

; Note: The license below is based on the template at:
; http://opensource.org/licenses/BSD-3-Clause

; Copyright (C) 2019, Regents of the University of Texas
; All rights reserved.
; Copyright (C) 2022 Intel Corporation

; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions are
; met:

; o Redistributions of source code must retain the above copyright
;   notice, this list of conditions and the following disclaimer.

; o Redistributions in binary form must reproduce the above copyright
;   notice, this list of conditions and the following disclaimer in the
;   documentation and/or other materials provided with the distribution.

; o Neither the name of the copyright holders nor the names of its
;   contributors may be used to endorse or promote products derived
;   from this software without specific prior written permission.

; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
; HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

; Original Author(s):
; Mertcan Temel         <mert@utexas.edu>

(in-package "RP")

(include-book "std/lists/remove-duplicates" :dir :system)
(include-book "misc/beta-reduce" :dir :system)
(include-book "tools/flag" :dir :system)
(include-book "std/util/defines" :dir :system)
(include-book "centaur/misc/starlogic" :dir :system)
;; Functions and lemmas used by both correctness proofs (rp-correct.lisp) and
;; guards (rp-rewriter.lisp)

(defun rp (prop term)
  (declare (ignorable prop))
  term)

(defun equals (term1 term2)
  (declare (ignorable term1 term2))
  term1)

(defun falist (fast-list term)
  (declare (ignorable fast-list))
  term)

(defconst *big-number*
  (1- (expt 2 60)))

(defun is-nonnil-fix (term)
  (declare (xargs :guard t))
  (case-match term (('nonnil-fix &) t) (& nil)))

(defun nonnil-p (term)
  (declare (xargs :guard t))
  (or (and (quotep term)
           (consp (cdr term)) ;; so that it is not (list 'quote)
           (not (equal (cadr term) 'nil)))
      (case-match term
        (('cons & &)
         t)
        (& nil))
;(is-nonnil-fix term)
      ))

(defun nonnil-listp (lst)
  (declare (xargs :guard t))
  (if (atom lst)
      (equal lst nil)
    (and (nonnil-p (car lst))
         (nonnil-listp (cdr lst)))))

(defun nonnil-fix (x)
  (if x x t))

(defthm not-nonnil-fix
  (equal (not (nonnil-fix x))
         nil))

(encapsulate
  nil

  (defun beta-reduce-lamdas (term limit)
    (declare (xargs :measure (acl2-count limit)
                    :guard (and (natp limit))))
    ;; gets a term that could be a cascade of lambda expressions and turns it into
    ;; a regular expression.
    (if (zp limit)
        term
      (if (atom term)
          term
        (if (and (acl2::flambda-applicationp term)
                 (pseudo-termp term))
            (beta-reduce-lamdas (acl2::beta-reduce-lambda-expr term)
                                (1- limit))
          term))))

  (mutual-recursion
   ;; searchs all the lambda terms and performs beta reduction on them.

   (defun beta-search-reduce (term limit)
     (declare (xargs :measure  (nfix limit)
                     :guard (and (natp limit))))
     (if (or (atom term)
             (quotep term)
             (zp limit))
         term
       (if (and (acl2::lambda-expr-p term)
                (pseudo-termp term))
           ;; !!!! PSEUDO-TERMP IS FOR THE GUARD. PROBABLY  BAD FOR RUNTIME!!!
           ;; it is ok for the time being because this function is not intended
           ;; for big terms.
           (beta-search-reduce (acl2::beta-reduce-lambda-expr term)
                               (1- limit))
         (cons (car term)
               (beta-search-reduce-subterms (cdr term) (1- limit))))))

   (defun beta-search-reduce-subterms (subterms limit)
     (declare (xargs :measure  (nfix limit)
                     :guard (and (natp limit))))
     (if (or (zp limit)
             (atom subterms))
         subterms
       (cons (beta-search-reduce (car subterms) (1- limit))
             (beta-search-reduce-subterms (cdr subterms) (1- limit)))))))



(define is-rp (term)
  :inline t
  (case-match term (('rp ('quote type) &)
                    (and (symbolp type)
                         (not (booleanp type))
                         (not (equal type 'quote))
                         (not (equal type 'rp))
                         (not (equal type 'list))
                         (not (equal type 'falist))))
    (& nil))
  ///
  (defthmd is-rp-implies
    (implies (is-rp term)
             (case-match term
               (('rp ('quote type) &)
                (and (symbolp type)
                     (not (equal type 'falist))
                     (not (booleanp type))
                     (not (equal type 'quote))))
               (& nil)))))

(define is-equals (term)
  :inline t
  (case-match term (('equals & &)
                    t))
  ///
  (defthm is-equals-implies
    (implies (is-equals term)
             (case-match term (('equals & &) t)))
    :rule-classes :forward-chaining))

(define is-if (term)
  :inline t
  (case-match term (('if & & &) t)
    (& nil)))

(define is-return-last (term)
  :enabled t
  :inline t
  (case-match term (('return-last & & &) t)
    (& nil)))

(define is-rp-soft (term)
  :enabled t
  :inline t
  (case-match term (('rp & &) t)
    (& nil)))

(define is-lambda (term)
  (case-match term
    ((('lambda & &) .  &)
     t)
    (& nil)))

(defun is-member (e lst)
  (declare (xargs :guard t))
  (if (atom lst)
      nil
    (or (equal e (car lst))
        (is-member e (cdr lst)))))

(defun union-equal2 (lst1 lst2)
  (declare (xargs :guard t))
  (cond ((atom lst1)
         lst2)
        ((is-member (car lst1) lst2)
         (union-equal2 (cdr lst1) lst2))
        (t
         (cons (car lst1)
               (union-equal2 (cdr lst1) lst2)))))

(defun remove-vars (big small)
  (declare (xargs :guard t))
  (if (atom big)
      nil
    (if (is-member (car big) small)
        (remove-vars (cdr big) small)
      (cons (car big)
            (remove-vars (cdr big) small)))))

(mutual-recursion
 (defun get-lambda-free-vars (term)
   (declare (xargs :guard t
                   :guard-hints (("Goal"
                                  :in-theory (e/d (is-lambda) ())))))
   (cond
    ((atom term) (mv t (list term)))
    ((quotep term) (mv t nil))
    ((is-lambda term)
     (b* (((mv valid sub-vars) (get-lambda-free-vars (caddr (car term))))
          (lambda-vars (cadr (car term)))
          ((mv valid-2 global-vars) (get-lambda-free-vars-lst (cdr term))))
       (mv (and valid
                valid-2
                (equal (remove-vars sub-vars lambda-vars) nil))
           global-vars)))
    (t (get-lambda-free-vars-lst (cdr term)))))

 (defun get-lambda-free-vars-lst (lst)
   (if (atom lst)
       (mv t nil)
     (b* (((mv valid-1 vars-1)
           (get-lambda-free-vars (car lst)))
          ((mv valid-2 vars-2)
           (get-lambda-free-vars-lst (cdr lst))))
       (mv (and valid-1 valid-2)
           (union-equal2 vars-1 vars-2))))))

#|(encapsulate
  nil
  (local
   (make-flag get-lambda-free-vars :defthm-macro-name
              defthm-get-lambda-free-vars))
  (local
   (defthm-get-lambda-free-vars
     (defthm true-listp-get-lambda-free-vars
       (true-listp (get-lambda-free-vars term))
       :flag get-lambda-free-vars)

     (defthm true-listp-get-lambda-free-vars-lst
       (true-listp (get-lambda-free-vars-lst lst))
       :flag get-lambda-free-vars-lst)))

  (verify-guards get-lambda-free-vars-lst))||#

(mutual-recursion
 (defun lambda-exp-free-p (term)
   (declare (xargs :guard t :mode :logic))
   (cond ((atom term) t)
         ((eq (car term) 'quote)
          t)
         (t (and (atom (car term))
                 (lambda-exp-free-listp (cdr term))))))

 (defun lambda-exp-free-listp (subterms)
   (if (atom subterms)
       (eq subterms nil)
     (and (lambda-exp-free-p (car subterms))
          (lambda-exp-free-listp (cdr subterms))))))

(encapsulate
  nil

  (defun falist-consistent-aux (falist term)
    ;; given an unquoted falist (a fast alist from (falist & &)), compares it
    ;; with the term and makes sure that they're consistent.
    (declare (xargs :guard t))
    (if (atom falist)
        (and (equal falist nil)
             (equal term ''nil))
      (b* ((cf (car falist))
           (cf-key (if (consp cf) (car cf) nil))
           (cf-val (if (consp cf) (cdr cf) nil)))
        (and
         (consp cf)
         (case-match term
           (('cons ('cons ('quote key1) val1) rest1)
            (and (equal cf-key key1)
                 (equal cf-val val1)
                 (falist-consistent-aux (cdr falist)
                                        rest1)))
           (('cons ('quote (key1 . val1)) rest1)
            (and #|(if (equal key1 nil)
             (equal cf-key (list 'quote))
             nil)||#
             (equal cf-key key1)
             (equal cf-val (list 'quote val1))
             (falist-consistent-aux (cdr falist)
                                    rest1)))
           (('quote ((key1 . val1) . rest1))
            (and (equal cf-key key1)
                 (equal cf-val (list 'quote val1))
                 (falist-consistent-aux (cdr falist)
                                        `',rest1)))
           (& nil))))))

  (define falist-consistent (falist-term)
    :parents (rp-other-utilities)
    :short "Given a falist term \(falist \* \*\), checks consistence of arguments."
    (case-match falist-term
      (('falist ('quote falist) term)
       (falist-consistent-aux falist term))
      (('falist ''nil ''nil)
       t)
      (& nil)))

  (defun is-falist (term)
    ;; checks if it is a falist statement?
    (declare (xargs :guard t))
    (case-match term (('falist & &) t) (& nil)))

  (defun is-falist-strict (term)
    ;; checks if it is a falist statement?
    (declare (xargs :guard t))
    (case-match term (('falist ('quote &) &) t) (& nil)))

  #|(mutual-recursion
  (defun all-falist-consistent (term)
  ;; searches the term for (falist & &) and if found, checkes whether
  ;; they're consistent.
  (declare (xargs :guard t #|(rp-termp term)||#))
  (cond
  ((or (atom term)
  (quotep term))
  t)
  ((is-falist term)
  (and (falist-consistent term)
  (all-falist-consistent (caddr term))))
  (t (all-falist-consistent-lst (cdr term)))))

  (defun all-falist-consistent-lst (lst)
  (declare (xargs :guard t #|(rp-term-listp lst)||#))
  (if (atom lst)
  t
  (and (all-falist-consistent (car lst))
  (all-falist-consistent-lst (cdr lst))))))||#

  #|(defun all-falist-consistent-bindings (bindings)
  ;; input is var-bindings;
  ;; checks if the values are falist-consistent
  (if (atom bindings)
  t
  (and (consp (car bindings))
  (all-falist-consistent (cdar bindings))
  (all-falist-consistent-bindings (cdr bindings)))))||#)

(encapsulate
  nil

  (define is-lambda-strict (x)
    :prepwork ((local (in-theory (enable is-lambda))))
    (and (is-lambda x)
         (symbol-listp (cadr (car x)))
         (equal (len (cadr (car x)))
                (len (cdr x)))
;(lambda-exp-free-listp (cdr x)) ;; variables should not have lambda expressions
         (b* (((mv valid &)
               (get-lambda-free-vars x)))
           valid)))

  (local
   (in-theory (enable is-rp
                      is-lambda
                      falist-consistent
                      is-lambda-strict
                      is-rp-soft)))

  (acl2::defines
   rp-termp
   (define rp-termp (term)
     ;; same as pseudo-termp but does not allow nil as a symbol
     :enabled t
     :parents (rp-utilities)
     :short "Similarly to pseudo-termp, defines the syntax for terms. "
     (cond ((atom term) (and (symbolp term) term))
           ((eq (car term) 'quote)
            (and (consp (cdr term))
                 (null (cdr (cdr term)))))
           ((eq (car term) 'rp)
            (and (is-rp term)
                 (rp-termp (caddr term))))
           ((eq (car term) 'falist)
            (and (falist-consistent term)
                 (rp-termp (caddr term))))
           (t (and (symbolp (car term))
                   (car term)
                   (rp-term-listp (cdr term))))))

   (define rp-term-listp (lst)
     :enabled t
     (cond ((atom lst) (eq lst nil))
           (t (and (rp-termp (car lst))
                   (rp-term-listp (cdr lst)))))))

  (define rp-term-fix (term)
    (if (rp-termp term)
        term
      ''nil)
    ///
    (defthm rp-termp-rp-term-fix
      (rp-termp (rp-term-fix term))
      :rule-classes :type-prescription))

  (define rp-term-list-fix (term)
    (if (rp-term-listp term)
        term
      nil)
    ///
    (defthm rp-term-listp-rp-term-list-fix
      (rp-term-listp (rp-term-list-fix term))
      :rule-classes :type-prescription))
  
  (defun rp-term-list-listp (lst)
    (declare (xargs :guard t))
    (if (atom lst)
        (equal lst nil)
      (and (rp-term-listp (car lst))
           (rp-term-list-listp (cdr lst))))))

(defun falist-syntaxp (unquoted-falist)
  ;; on the unquoted fast-alist (which is the first parameter of (falist & &)
  ;; but unquoted), checks the syntacial correctness
  (declare (xargs :guard t))
  (and (alistp unquoted-falist)
       (rp-term-listp
        (strip-cdrs unquoted-falist))))

;; (encapsulate
;;   nil
;;   (local
;;    (in-theory (enable is-rp)))
;;   (mutual-recursion
;;    ;; checks if all the terms with a function symbol
;;    ;; "rp" satisfies the is-rp condition.
;;    (defun rp-syntaxp (term)
;;      (declare (xargs :guard t))
;;      (cond
;;       ((atom term) t)
;;       ((eq (car term) 'quote) t)
;;       ((eq (car term) 'rp)
;;        (and (is-rp term)
;;             (rp-syntaxp (caddr term))))
;;       (t (rp-syntaxp-lst (cdr term)))))
;;    (defun rp-syntaxp-lst (lst)
;;      (cond
;;       ((atom lst) t)
;;       (t (and (rp-syntaxp (car lst))
;;               (rp-syntaxp-lst (cdr lst))))))))

;; (defun rp-syntaxp-bindings (bindings)
;;   (rp-syntaxp-lst (strip-cdrs bindings)))

(defthm rp-termp-implies-cdr-listp
  (implies (and (consp term)
                (rp-termp term)
                (not (equal (car term) 'quote)))
           (rp-term-listp (cdr term)))
  :hints (("Goal"
           :Expand ((RP-TERMP TERM)
                    (RP-TERM-LISTP (CDR TERM))
                    (RP-TERM-LISTP (CdDR TERM)))
           :in-theory (e/d (is-rp falist-consistent) ()))))

(encapsulate
  nil
  (define fnc-alistp (fnc-alist)
    :enabled t
    (if (atom fnc-alist)
        (equal fnc-alist nil)
      (and (consp (car fnc-alist))
           (symbolp (caar fnc-alist))
           (natp (cdar fnc-alist))
           (fnc-alistp (cdr fnc-alist)))))

  (defmacro bindings-alistp (bindings)
    `(and (alistp ,bindings)
;(symbol-listp (strip-cars ,bindings))
          (rp-term-listp (strip-cdrs ,bindings)))))

(defun cons-count (x)
  (declare (xargs :guard t))
  (cond ((atom x)
         1)
        (t
         (+ (cons-count (car x))
            (cons-count (cdr x))))))

(progn
  (defun cons-count-compare-aux (x count)
    (declare (xargs :guard (natp count)))
    (cond ((zp count)
           (mv 0 t))
          ((atom x)
           (mv (1- count) nil))
          ((quotep x)
           (mv (1- count) nil))
          (t (b* (((mv count count-reached)
                   (cons-count-compare-aux (car x) count))
                  ((when count-reached)
                   (mv 0 t)))
               (cons-count-compare-aux (cdr x) count)))))

  (defun cons-count-compare (x count)
    (declare (xargs :guard (natp count)))
    (b* (((mv & count-reached)
          (cons-count-compare-aux x count)))
      count-reached)))

(mutual-recursion
 (defun count-lambdas (x)
   (declare (xargs :guard t
                   :guard-hints (("Goal"
                                  :in-theory (e/d (is-lambda is-lambda-strict) ())))))
   (cond ((atom x) 0)
         ((eq (car x) 'quote) 0)
         ((is-lambda-strict x)
          (+ 1
             (count-lambdas (caddr (car x)))))
         (t (count-lambdas-lst (cdr x)))))

 (defun count-lambdas-lst (lst)
   (if (atom lst)
       0
     (+ (count-lambdas (car lst))
        (count-lambdas-lst (cdr lst))))))

(defun cons-consp (lst)
  (declare (xargs :guard t))
  ;;  all the elements should be conses and not quoteps
  (if (atom lst)
      (equal lst nil)
    (and (consp (car lst))
         (not (quotep (car lst)))
         (cons-consp (cdr lst)))))



(define maybe-natp (x)
  :enabled t
  (or (not x)
      (natp x))) 

(acl2::defines
 include-fnc
 (define include-fnc (term fnc &optional arg-size)
   :enabled t
   :guard (and (symbolp fnc)
               (maybe-natp arg-size))
   :parents (rp-utilities)
   :short "Searches a term for an instance of fnc. Returns t or nil."
   (if (or (atom term)
           (quotep term))
       nil
     (if (and (eq (car term) fnc)
              (implies arg-size
                       (equal (len (cdr term)) arg-size)))
         t
       (include-fnc-subterms (cdr term) fnc arg-size))))

 (define include-fnc-subterms (subterms fnc &optional arg-size)
   :guard (and (symbolp fnc)
               (maybe-natp arg-size))
   :enabled t
   :parents (include-fnc)
   :short "Searches a list of terms for an instance of fnc. Returns t or nil."
   (if (atom subterms)
       nil
     (or (include-fnc (car subterms) fnc arg-size)
         (include-fnc-subterms (cdr subterms) fnc arg-size)))))

(defun is-honsed-assoc-eq-values (term)
  (declare (xargs :guard t))
  (case-match term
    (('assoc-eq-vals ('quote &) ('falist ('quote &) &))
     t)
    (& nil)))

(encapsulate
  nil

  (local
   (in-theory (enable is-rp)))

  (defun-inline is-synp (term)
    (declare (xargs :guard t #|(and (rp-termp term))||#))
    (case-match term (('synp & & &) t) (& nil)))

  (defund-inline is-rp-loose (term)
    (declare (xargs :guard t #|(and (rp-termp term))||#))
    (case-match term (('rp & &) t) (& nil)))

  (define ex-from-falist (term)
    (case-match term
      (('falist & x)
       x)
      (& term)))

  (define ex-from-rp (term)
    :enabled t
    :parents (rp-utilities)
    :short "Extracts a term if it is wrapped in an rp instance."
    (if (is-rp term)
        (ex-from-rp (caddr term))
      term))

  (local
   (in-theory (enable IS-RP-LOOSE)))

  (define ex-from-rp-loose (term)
    :parents (ex-from-rp)
    :short "Same as @(see rp::ex-from-rp) when term is @(see rp::rp-termp) but
    a little faster."
    (mbe :logic (if (is-rp-loose term)
                    (ex-from-rp-loose (caddr term))
                  term)
         :exec (case-match term (('rp & x)
                                 (ex-from-rp-loose x))
                 (& term))))

  (local
   (in-theory (enable ex-from-rp-loose)))


  (defun-inline ex-from-rp$ (term)
    (declare (xargs :guard (rp-termp term)))
    (mbe :exec (ex-from-rp-loose term)
         :logic (ex-from-rp term)))

  (defun-inline is-rp$ (term)
    (declare (xargs :guard (rp-termp term)))
    (mbe :exec (is-rp-loose term)
         :logic (is-rp term)))
    
  
  (defun extract-from-rp-with-context (term context)
    (declare (xargs :guard (rp-termp term)))
    (if (mbe :logic (is-rp term)
             :exec (is-rp-loose term))
        (b* ((type (cadr (cadr term)))
             ((mv rcontext rterm)
              (extract-from-rp-with-context (caddr term) context)))
          (mv (cons `(,type ,(ex-from-rp$ (caddr term))) rcontext) rterm))
      (mv context term)))

  (defun extract-from-synp (term)
    (declare (xargs :guard t #|(rp-termp term)||#))
    (case-match term
      (('synp & & &) ''t)
      (& term)))

  (defun ex-from-synp (term)
    (if (is-synp term)
        ''t
      term))

  (defun-inline is-cons (term)
    (declare (xargs :guard (and t)))
    (case-match term (('cons & &) t) (& nil)))

  (defun-inline is-quoted-pair (term)
    (declare (xargs :guard (and t)))
    (and #|(quotep term)||#
     (consp term)
     (eq (car term) 'quote)
     (consp (cdr term))
     (consp (unquote term))))

  (defun-inline should-term-be-in-cons (rule-lhs term)
    (declare (xargs :guard t #|(and (rp-termp term)
                    (rp-termp rule-lhs))||#))
    (and (is-quoted-pair term) ;(quotep term)
         ;;(consp (unquote term))
         (is-cons rule-lhs);;(case-match rule-lhs (('cons & &) t) (& nil))
         ))

  (defun-inline put-term-in-cons (term)
    (declare (xargs :guard (and #|(rp-termp term)||#
                            (should-term-be-in-cons '(cons x y) term))))
    `(cons ',(car (unquote term))
           ',(cdr (unquote term))))

  (define context-from-rp (term context)
    :short "Expands a given context list with the side-conditions from the term"
    :parents (ex-from-rp)
    (if (is-rp term)
        (let ((type (car (cdr (car (cdr term)))))
              (x (car (cdr (cdr term)))))
          (b* ((rcontext (context-from-rp x context)))
            (cons (cons type (cons (ex-from-rp x) 'nil))
                  rcontext)))
      context)))

(define dumb-negate-lit2 (term)
  :enabled t
  :inline t
  :returns (mv new-term dont-rw)
  (cond ((atom term)
         (mv (acl2::fcons-term* 'not term)
             `(nil t)))
        ((acl2::fquotep term)
         (mv (cond ((equal term ''nil)
                    ''t)
                   (t ''nil))
             t))
        ((case-match term (('not &) t))
         (mv (acl2::fargn term 1) t))
        ((case-match term (('if & then ''nil) (nonnil-p then)))
         (mv `(if ,(acl2::fargn term 1) 'nil 't)
             `(nil t t t)))
        ((case-match term (('if & ''nil else) (nonnil-p else)))
         (mv (acl2::fargn term 1) t))
        ((and (case-match term (('equal & &) t))
              (or (equal (acl2::fargn term 2)
                         ''nil)
                  (equal (acl2::fargn term 1)
                         ''nil)))
         (mv (if (equal (acl2::fargn term 2)
                        ''nil)
                 (acl2::fargn term 1)
               (acl2::fargn term 2))
             t))
        ((nonnil-p term) (mv ''nil t))
        (t (mv `(if ,term 'nil 't) `(nil t t t)))))

(encapsulate
  nil

  (mutual-recursion
   (defun get-vars1 (q acc)
     (declare (xargs :guard (and (true-listp acc)
                                 #|(rp-termp q)||#)
                     :verify-guards nil))
     (if (quotep q)
         acc
       (if (atom q)
           (if (member-equal q acc) acc (cons q acc))
         (get-vars-subterms (cdr q) acc))))

   (defun get-vars-subterms (subterms acc)
     (declare (xargs :guard (and (true-listp acc)
                                 #|(rp-term-listp subterms)||#)
                     :verify-guards nil))
     (if (atom subterms)
         acc
       (get-vars-subterms (cdr subterms)
                          (get-vars1 (car subterms) acc)))))

  (make-flag get-vars1 :defthm-macro-name defthm-get-vars1)

  (defthm-get-vars1
    (defthm true-listp-get-vars1
      (implies (true-listp acc)
               (true-listp (get-vars1 q acc)))
      :flag get-vars1)
    (defthm true-listp-get-vars-subterms
      (implies (true-listp acc)
               (true-listp (get-vars-subterms subterms acc)))
      :flag get-vars-subterms))

  (verify-guards get-vars1)

  (defun get-vars (term)
    (declare (xargs :guard t #|(rp-termp term)||#))
    (get-vars1 term nil)))

(encapsulate
  nil
  (defrec custom-rewrite-rule
    ((meta-rulep . lhs/trig-fnc) (flg . hyp) rhs/meta-fnc . rune)
    t) ; t when we are confident that the code is OK

  (defun weak-custom-rewrite-rule-listp (rules)
    (declare (xargs :guard t))
    (if (atom rules)
        (eq rules nil)
      (and (weak-custom-rewrite-rule-p (car rules))
           (weak-custom-rewrite-rule-listp (cdr rules)))))

  (defun-inline rp-rule-metap (rule)
    ;; return hyps from a given rule
    (declare (xargs :guard (weak-custom-rewrite-rule-p rule)))
    (access custom-rewrite-rule rule :meta-rulep))

  (defun-inline rp-rule-rwp (rule)
    ;; return hyps from a given rule
    (declare (xargs :guard (weak-custom-rewrite-rule-p rule)))
    (not (rp-rule-metap rule)))

  (defun rp-rule-meta-listp (rules)
    (declare (xargs :guard (weak-custom-rewrite-rule-listp rules)))
    (if (atom rules)
        (eq rules nil)
      (and (rp-rule-metap (car rules))
           (rp-rule-meta-listp (cdr rules)))))

  (defun rp-rule-rw-listp (rules)
    (declare (xargs :guard (weak-custom-rewrite-rule-listp rules)))
    (if (atom rules)
        (eq rules nil)
      (and (rp-rule-rwp (car rules))
           (rp-rule-rw-listp (cdr rules)))))
  
  (defun-inline rp-hyp (rule)
    ;; return hyps from a given rule
    (declare (xargs :guard (and (weak-custom-rewrite-rule-p rule)
                                (not (rp-rule-metap rule)))))
    (access custom-rewrite-rule rule :hyp))

  (defun-inline rp-lhs (rule)
    ;; return hyps from a given rule
    (declare (xargs :guard (and (weak-custom-rewrite-rule-p rule)
                                (not (rp-rule-metap rule)))))
    (access custom-rewrite-rule rule :lhs/trig-fnc))
  
  (defun-inline rp-rule-trig-fnc (rule)
    ;; return hyps from a given rule
    (declare (xargs :guard (and (weak-custom-rewrite-rule-p rule)
                                (rp-rule-metap rule))))
    (access custom-rewrite-rule rule :lhs/trig-fnc))

  (defun-inline rp-rhs (rule)
    ;; return hyps from a given rule
    (declare (xargs :guard (and (weak-custom-rewrite-rule-p rule)
                                (not (rp-rule-metap rule)))))
    (access custom-rewrite-rule rule :rhs/meta-fnc))

  (defun-inline rp-rule-meta-fnc (rule)
    ;; return hyps from a given rule
    (declare (xargs :guard (and (weak-custom-rewrite-rule-p rule)
                                (rp-rule-metap rule))))
    (access custom-rewrite-rule rule :rhs/meta-fnc))

  (defun-inline rp-rune (rule)
    ;; return hyps from a given rule
    (declare (xargs :guard (and (weak-custom-rewrite-rule-p rule))))
    (access custom-rewrite-rule rule :rune))

  (defun-inline rp-iff-flag (rule)
    (declare (xargs :guard (and (weak-custom-rewrite-rule-p rule)
                                (not (rp-rule-metap rule)))))
    (access custom-rewrite-rule rule :flg))

  (defun meta-runep (rune)
    (declare (xargs :guard t))
    (case-match rune
      ((':meta meta-fnc . trig-fnc)
       (and (symbolp meta-fnc)
            meta-fnc
            (symbolp trig-fnc)
            trig-fnc))))

  (defun-inline meta-rune-fnc (rune)
    (declare (xargs :guard (meta-runep rune)))
    (cadr rune))
  
  (defun-inline meta-rune-trig-fnc (rune)
    (declare (xargs :guard (meta-runep rune)))
    (cddr rune))

  #|(defun-inline rp-rule-fnc (rule)
    (declare (xargs :guard (weak-custom-rewrite-rule-p rule)))
    (access custom-rewrite-rule rule :rule-fnc))||#)

(encapsulate
  nil

  (defmacro rp-hypm (rule)
    ;; return hyps from a given rule

    `(access custom-rewrite-rule ,rule :hyp))

  (defmacro rp-lhsm (rule)
    ;; return hyps from a given rule
    `(access custom-rewrite-rule ,rule :lhs/trig-fnc))

  (defmacro rp-rhsm (rule)
    ;; return hyps from a given rule
    `(access custom-rewrite-rule ,rule :rhs/meta-fnc))

  (defmacro rp-runem (rule)
    ;; return hyps from a given rule
    `(access custom-rewrite-rule ,rule :rune))

  (defmacro rp-iff-flagm (rule)
    `(access custom-rewrite-rule ,rule :flg)))

(defun remove-from-alist (alist key)
  (declare (xargs :guard t))
  (if (atom alist)
      alist
    (if (not (consp (car alist)))
        alist
      (if (equal (caar alist) key)
          (remove-from-alist (cdr alist) key)
        (cons-with-hint (car alist)
                        (remove-from-alist (cdr alist) key)
                        alist)))))

(encapsulate
  nil

  (define dont-rw-if-fix (dont-rw)
    (case-match
      dont-rw
      ((& & & &)
       dont-rw)
      (& '(nil nil nil nil)))
    ///
    (local
     (defthmd dont-rw-if-fix-type
       (let ((res (dont-rw-if-fix dont-rw)))
         (and (consp res)
              (consp (cdr res))
              (consp (cddr res))
              (consp (cdddr res))
              (equal (cddddr res) nil))))))

  (define strict-quotep (term)
    :enabled t
    (and (consp term)
         (eq (car term) 'quote)
         (consp (cdr term))
         (not (cddr term))))

  (defun dont-rw-syntaxp-aux (dont-rw)
    (declare (xargs :guard t))
    (if (atom dont-rw)
        t
      (and (or (atom (car dont-rw))
               (dont-rw-syntaxp-aux (car dont-rw)))
           (dont-rw-syntaxp-aux (cdr dont-rw)))))

  (defund dont-rw-syntaxp (dont-rw)
    (declare (xargs :guard t))
    (or (atom dont-rw)
        (dont-rw-syntaxp-aux dont-rw)))

  (define should-not-rw (dont-rw)
    :inline t
    (and (atom dont-rw)
         dont-rw))

  (defund dont-rw-syntax-fix (dont-rw)
    (declare (xargs :guard t))
    (if (dont-rw-syntaxp dont-rw)
        dont-rw
      (progn$ (hard-error 'dont-rw-syntax-fix
                          "this dont'rw is being fixed. This should have
    not happened... ~p0 ~%"
                          (list (cons #\0 dont-rw)))
              t))))

(defun context-syntaxp (context)
  (declare (xargs :guard t))
  (rp-term-listp context))

(mutual-recursion

 (defun remove-return-last (term)
   (declare (xargs :guard t))
   (cond
    ((or (atom term)
         (quotep term)
         (is-falist term))
     term)
    ((is-return-last term)
     (remove-return-last (cadddr term)))
    (t (cons (car term)
             (remove-return-last-subterms (cdr term))))))

 (defun remove-return-last-subterms (subterms)
   (declare (xargs :guard t #|(rp-term-listp subterms)||#))
   (if (atom subterms)
       subterms
     (cons (remove-return-last (car subterms))
           (remove-return-last-subterms (cdr subterms))))))

(defund light-remove-return-last (term)
  (declare (xargs :guard t))
  (if (is-return-last term)
      (light-remove-return-last (cadddr term))
    term))

(define is-hide (term)
  (declare (xargs :guard t))
  (case-match term
    (('hide &) t)
    (& nil))
  ///
  (defthm is-hide-implies
    (implies (is-hide x)
             (case-match x
               (('hide &) t)))
    :rule-classes :forward-chaining))

(in-theory (disable extract-from-rp-with-context))

(mutual-recursion
 (defun search-term (term seq)
   ;; case insensitive search on the term
   (cond ((atom term)
          (search seq (symbol-name term)  :test 'char-equal))
         ((quotep term)
          nil)
         ((consp (car term))
          (or (search-subterms (car term) seq)
              (search-subterms (cdr term) seq)))
         (t
          (or (search seq (symbol-name (car term)) :test 'char-equal)
              (search-subterms (cdr term) seq)))))
 (defun search-subterms (subterms seq)
   (if (atom subterms)
       nil
     (or (search-term (car subterms) seq)
         (search-subterms (cdr subterms) seq)))))

(encapsulate
  nil

  (local
   (defthm consp-extract-from-rp
     (implies (consp (ex-from-rp term))
              (consp term))))

  (local
   (defthm consp-ex-from-rp-loose
     (implies (consp (ex-from-rp-loose term))
              (consp term))
     :hints (("Goal"
              :in-theory (e/d (ex-from-rp-loose
                               is-rp-loose)
                              ())))))

  (local
   (defthm extract-from-rp-acl2-count
     (implies (consp term)
              (< (acl2-count (cdr (ex-from-rp term)))
                 (acl2-count term)))))

  (local
   (defthm ex-from-rp-loose-acl2-count
     (implies (consp term)
              (< (acl2-count (cdr (ex-from-rp-loose term)))
                 (acl2-count term)))
     :hints (("Goal"
              :in-theory (e/d (ex-from-rp-loose
                               is-rp-loose) ())))))

  (acl2::defines
   rp-equal
   :parents (rp-utilities)
   :short "Check if two terms are equivalent by discarding rp terms"
   (define rp-equal (term1 term2)
     :enabled t
     (declare (xargs :mode :logic
                     :verify-guards nil
                     :guard t))
     "Check syntactic equivalance of two terms by ignoring all the rp terms"
     (let* ((term1 (ex-from-rp term1))
            (term2 (ex-from-rp term2)))
       (cond
        ((or (atom term1)
             (atom term2)
             (acl2::fquotep term1)
             (acl2::fquotep term2))
         (equal term1 term2))
        (t (and (equal (car term1) (car term2))
                (rp-equal-subterms (cdr term1) (cdr term2)))))))

   (define rp-equal-subterms (subterm1 subterm2)
     :enabled t
     (declare (xargs :mode :logic
                     :verify-guards nil
                     :guard t #|(and (rp-term-listp subterm1)
                     (rp-term-listp subterm2))||#))
     (if (or (atom subterm1)
             (atom subterm2))
         (equal subterm1 subterm2)
       (and (rp-equal (car subterm1) (car subterm2))
            (rp-equal-subterms (cdr subterm1) (cdr subterm2))))))

  (acl2::defines
   rp-equal-cw
   :parents (rp-equal)
   :short "Same as @(see rp::rp-equal) but prints a mismatch."
   (define rp-equal-cw (term1 term2)
     :enabled t
     (declare (xargs :mode :logic
                     :guard t #|(and (rp-termp term1)
                     (rp-termp term2))||#))
     "Check syntactic equivalance of two terms by ignoring all the rp terms"
     (let* ((term1 (ex-from-rp term1))
            (term2 (ex-from-rp term2)))
       (cond
        ((or (atom term1)
             (atom term2)
             (acl2::fquotep term1)
             (acl2::fquotep term2))
         (or (equal term1 term2)
             (cw "Mismatch: term1=~p0, term2=~p1 ~%" term1 term2)))
        (t (and (or (equal (car term1) (car term2))
                    (cw "Mismatch: term1=~p0, term2=~p1 ~%" term1 term2))
                (rp-equal-cw-subterms (cdr term1) (cdr term2)))))))

   (define rp-equal-cw-subterms (subterm1 subterm2)
     :enabled t
     (declare (xargs :mode :logic
                     :guard t #|(and (rp-term-listp subterm1)
                     (rp-term-listp subterm2))||#))
     (if (or (atom subterm1)
             (atom subterm2))
         (or (equal subterm1 subterm2)
             (cw "Mismatch: subterm1=~p0, sunterm2=~p1 ~%" subterm1 subterm2))
       (and (rp-equal-cw (car subterm1) (car subterm2))
            (rp-equal-cw-subterms (cdr subterm1) (cdr subterm2))))))

  (mutual-recursion
   ;; check if two terms are equivalent by discarding rp terms
   (defun rp-equal-loose (term1 term2)
     (declare (xargs :mode :logic
                     :verify-guards nil
;           :measure (+ (cons-count term1)
;                      (cons-count term2))
                     :guard t #|(and (rp-termp term1)
                     (rp-termp term2))||#))
     "Check syntactic equivalance of two terms by ignoring all the rp terms"
     (let* ((term1 (ex-from-rp-loose term1))
            (term2 (ex-from-rp-loose term2)))
       (cond
        ((or (atom term1) (atom term2)
             (acl2::fquotep term1) (acl2::fquotep term2))
         (equal term1 term2))
        (t (and (equal (car term1) (car term2))
                (rp-equal-loose-subterms (cdr term1) (cdr term2)))))))

   (defun rp-equal-loose-subterms (subterm1 subterm2)
     (declare (xargs :mode :logic
                     :verify-guards nil
                     :guard t #|(and (rp-term-listp subterm1)
                     (rp-term-listp subterm2))||#))
     (if (or (atom subterm1)
             (atom subterm2))
         (equal subterm1 subterm2)
       (and (rp-equal-loose (car subterm1) (car subterm2))
            (rp-equal-loose-subterms (cdr subterm1) (cdr subterm2))))))

  (acl2::defines
   rp-equal-cnt
   :parents (rp-equal)
   :short "Same as @(see rp::rp-equal) but when counts down from cnt and starts ~
   using 'equal' when it hits 0."
   ;; check if two terms are equivalent by discarding rp terms
   (define rp-equal-cnt (term1 term2 cnt)
     :enabled t
     (declare (xargs :mode :logic
                     ;;:verify-guards nil
                     :guard (and (natp cnt)
                                 #|(rp-termp term1)||#
                                 #|(rp-termp term2)||#)))
     "Same as rp-equal but also runs equal after counter goes below 0."
     (or (if (and (zp cnt))
             (equal term1 term2)
           nil)
         (let* ((term1 (ex-from-rp term1))
                (term2 (ex-from-rp term2)))
           (cond
            ((or (atom term1) (atom term2)
                 (acl2::fquotep term1)
                 (acl2::fquotep term2))
             (equal term1 term2))
            (t ;(or (if (< cnt 0) (equal term1 term2) nil)
             (and (equal (car term1) (car term2))
                  (rp-equal-cnt-subterms (cdr term1) (cdr term2) (nfix (1- cnt)))))))))

   (define rp-equal-cnt-subterms (subterm1 subterm2 cnt)
     :enabled t
     (declare (xargs :mode :logic
                     ;;:verify-guards nil
                     :guard (and (natp cnt)
                                 #|(rp-term-listp subterm1)||#
                                 #|(rp-term-listp subterm2)||#)))
     (if (or (atom subterm1)
             (atom subterm2))
         (equal subterm1 subterm2)
       (and (rp-equal-cnt (car subterm1) (car subterm2) cnt)
            (rp-equal-cnt-subterms (cdr subterm1) (cdr subterm2) cnt)))))

  (mutual-recursion
   ;; check if two terms are equivalent by discarding rp terms
   (defun p-rp-equal-cnt (term1 term2 cnt)
     (declare (xargs :mode :program))
     "Same as rp-equal but also runs equal after counter goes below 0."
     (or (if (and (< cnt 0))
             (equal term1 term2)
           nil)
         (let* ((term1 (ex-from-rp-loose term1))
                (term2 (ex-from-rp-loose term2)))
           (cond
            ((or (atom term1) (atom term2)
                 (acl2::fquotep term1)
                 (acl2::fquotep term2))
             (equal term1 term2))
            (t ;(or (if (< cnt 0) (equal term1 term2) nil)
             (and (equal (car term1) (car term2))
                  (p-rp-equal-cnt-subterms (cdr term1) (cdr term2) (1- cnt))))))))

   (defun p-rp-equal-cnt-subterms (subterm1 subterm2 cnt)
     (if (or (atom subterm1)
             (atom subterm2))
         (equal subterm1 subterm2)
       (and (p-rp-equal-cnt (car subterm1) (car subterm2) cnt)
            (p-rp-equal-cnt-subterms (cdr subterm1) (cdr subterm2) cnt))))))

(encapsulate
  nil

  (local
   (in-theory (disable rp-hyp rp-lhs rp-rhs)))

  (define no-free-variablep (rule)
    (declare (xargs :guard (and (weak-custom-rewrite-rule-p rule)
                                 (NOT (RP-RULE-METAP RULE))
                                #|(rp-termp (rp-hyp rule))||#
                                #|(rp-termp (rp-lhs rule))||#
                                #|(rp-termp (rp-rhs rule))||#)))
    (let ((vars (get-vars (rp-lhs rule))))
      (and (subsetp (get-vars-subterms (rp-hyp rule) nil)
                    vars
                    :test 'equal)
           (subsetp (get-vars (rp-rhs rule))
                    vars
                    :test 'equal)))
    ///
    (in-theory (disable (:type-prescription no-free-variablep))))

  (define rule-syntaxp (rule &key warning)
    :parents (rp-other-utilities)
    :short "Syntax check for a 'rule' defined with rp::custom-rewrite-rule. If
    warning key is set to non-nil, a warning is issued for failures. "
    (and
     (or (weak-custom-rewrite-rule-p rule)
         (and warning
              (hard-error
               'rule-syntaxp
               "ATTENTION! weak-custom-rewrite-rule-p failed! ~p0 ~%"
               (list (cons #\0 rule)))))
     (if (rp-rule-metap rule)
         (and (symbolp (rp-rule-trig-fnc rule))
              (rp-rule-trig-fnc rule)
              (symbolp (rp-rule-meta-fnc rule))
              (rp-rule-meta-fnc rule))
       (and
        (or (not (include-fnc (rp-lhs rule) 'rp))
            (and warning
                 (cw "ATTENTION! (not (include-fnc (rp-lhs rule) 'rp))
    failed! LHS cannot contain an instance of rp. ~%")))
        (or (not (include-fnc (rp-lhs rule) 'equals))
            (and warning
                 (cw "ATTENTION! (not (include-fnc (rp-lhs rule) 'equals))
    failed! LHS cannot contain an instance of equals. ~%")))
        (or (not (include-fnc-subterms (rp-hyp rule) 'rp))
            (and warning
                 (cw "ATTENTION! (not (include-fnc-subterms (rp-hyp rule) 'rp))
    failed! HYP cannot contain an instance of rp. ~%")))
        (or (not (include-fnc-subterms (rp-hyp rule) 'equals))
            (and warning
                 (cw "ATTENTION! (not (include-fnc-subterms (rp-hyp rule) 'equals))
    failed! HYP cannot contain an instance of equals ~%")))
        (or (not (include-fnc (rp-rhs rule) 'falist))
            (and warning
                 (cw "ATTENTION! (not (include-fnc (rp-rhs rule) 'falist))
    failed! RHS cannot contain an instance of falist ~%")))
        (or (not (include-fnc (rp-rhs rule) 'equals))
            (and warning
                 (cw "ATTENTION! (not (include-fnc (rp-rhs rule) 'equals))
    failed! RHS cannot contain an instance of equals ~%")))
        (or (not (include-fnc-subterms (rp-hyp rule) 'falist))
            (and warning
                 (cw "ATTENTION! (not (include-fnc (rp-hyp rule) 'falist))
    failed! HYP cannot contain an instance of falist ~%")))
        (or (and
             (or (rp-term-listp (rp-hyp rule))
                 (and warning
                      (cw "ATTENTION! (rp-term-listp (rp-hyp rule)) failed! Hyp of the ~
    rule does not satisfy rp::rp-termp. ~%")))
             (or (rp-termp (rp-lhs rule))
                 (and warning
                      (cw "ATTENTION! (rp-termp (rp-lhs rule)) failed! LSH of the ~
    rule does not satisfy rp::rp-termp. ~%")))
             (or (rp-termp (rp-rhs rule))
                 (and warning
                      (cw "ATTENTION! (rp-termp (rp-rhs rule)) failed! RHS of the ~
    rule does not satisfy rp::rp-termp. ~%")))

             #|(or (not (include-fnc (rp-lhs rule) 'if))
                 (and warning
                      (cw "ATTENTION! (not (include-fnc (rp-lhs rule) 'if))
    failed! LHS cannot contain an instance of 'if'. ~%")))|#
             (or (consp (rp-lhs rule))
                 (and warning
                      (cw "ATTENTION! (consp (rp-lhs rule)) failed! LHS cannot
    be a variable. ~%")))
             (or (not (acl2::fquotep (rp-lhs rule)))
                 (and warning
                      (cw "ATTENTION! (not (acl2::fquotep (rp-lhs rule))) failed!
    LHS cannot be a quoted value ~%")))

             #|(or (no-free-variablep rule)
                 (and warning
                      (cw "Warning! This rule (~%) has the following free variables: ~
In the hyps: ~p0, in the rhs :~p1. ~%")))|#
             
             (or (not (include-fnc (rp-lhs rule) 'synp))
                 (and warning
                      (cw "ATTENTION! (not (include-fnc (rp-lhs rule) 'synp))
    failed! LHS cannot contain an instance of synp ~%")))
             (not (include-fnc (rp-lhs rule) 'list))
             (not (include-fnc-subterms (rp-hyp rule) 'list))
             (not (include-fnc (rp-rhs rule) 'list)))
            (and (equal warning ':err)
                 (hard-error
                  'rule-syntaxp
                  "Read the above messages. Error  is issued for: ~%
 rp-rune: ~p0 ~% rp-hyp: ~p1 ~% rp-lhs: ~p2 ~% rp-rhs ~p3 ~%"
                  (list (cons #\0 (rp-rune rule))
                        (cons #\1 (rp-hyp rule))
                        (cons #\2 (rp-lhs rule))
                        (cons #\3 (rp-rhs rule)))))
            (and warning
                 (cw "Read the above messages. Warning in rp::rule-syntaxp is issued for: ~%
 rp-rune: ~p0 ~% rp-hyp: ~p1 ~% rp-lhs: ~p2 ~% rp-rhs ~p3 ~%"
                     (rp-rune rule)
                     (rp-hyp rule)
                     (rp-lhs rule)
                     (rp-rhs rule))))))))

  (defun rule-list-syntaxp (rules)
    (declare (xargs :guard t))
    (if (atom rules)
        (equal rules nil)
      (and (rule-syntaxp (car rules))
           (rule-list-syntaxp (cdr rules)))))

  (defun rule-list-list-syntaxp (rules)
    (declare (xargs :guard t))
    (if (atom rules)
        (equal rules nil)
      (and (rule-list-syntaxp (car rules))
           (rule-list-list-syntaxp (cdr rules)))))

  (defun rules-alistp (rules)
    (declare (xargs :guard t))
    (and (alistp rules)
         (symbol-listp (strip-cars rules))
         (rule-list-list-syntaxp (strip-cdrs rules)))))

(defun conjecture-syntaxp (term)
  (declare (xargs :guard t))
  (and (not (include-fnc term 'rp))
       (not (include-fnc term 'falist))
       (rp-termp term)))

(acl2::defines
 ex-from-rp-all
 :parents (ex-from-rp)
 :short "Removes all instances of 'rp' from a term"
 (define ex-from-rp-all (term)
   :returns (res )
   (b* ((term (ex-from-rp term)))
     (cond ((atom term)
            term)
           ((quotep term)
            term)
           (t
            (cons-with-hint (car term)
                            (ex-from-rp-all-lst (cdr term))
                            term)))))

 (define ex-from-rp-all-lst (lst)
   :returns (res-lst)
   (if (atom lst)
       lst
     (cons-with-hint (ex-from-rp-all (car lst))
                     (ex-from-rp-all-lst (cdr lst))
                     lst))))

(acl2::defines
    ex-from-rp-all2
    :parents (ex-from-rp)
    :short "Removes all instances of 'rp' from a term, and remove falist instances along
    the way"
    :prepwork ((local
                (in-theory (enable is-rp
                                   is-rp-loose))))
    (define ex-from-rp-all2 (term)
      :returns (res)
      (cond ((atom term)
             term)
            ((quotep term)
             term)
            ((is-falist term)
             (ex-from-rp-all2 (caddr term)))
            ((is-rp-loose term)
             (ex-from-rp-all2 (caddr term)))
            ((is-equals term)
             (ex-from-rp-all2 (cadr term)))
            (t
             (cons-with-hint (car term)
                             (ex-from-rp-all2-lst (cdr term))
                             term))))

    (define ex-from-rp-all2-lst (lst)
      :returns (res-lst)
      (if (atom lst)
          lst
          (cons-with-hint (ex-from-rp-all2 (car lst))
                          (ex-from-rp-all2-lst (cdr lst))
                          lst))))

#|(encapsulate
  nil

  (defrec meta-rule-rec
    (trig-fnc ;; trigger function name
     fnc ;; function name that meta rule executes
     dont-rw ;; if meta rule also returns a structure for dont-rw
     valid-syntax ;; if meta rule returns valid-syntax (rp-valid-termp)
     outside-in ;; rewriting direction outside-in, inside-out or both
     args ;; arguments of "fnc"
     ret-vals ;; return vals of "fnc"
     )
    t)

  (defun rp-meta-fnc (rule)
    (declare (xargs :guard (weak-rp-meta-rule-rec-p rule)))
    (access meta-rule-rec rule :fnc))

  (defun rp-meta-trig-fnc (rule)
    (declare (xargs :guard (weak-rp-meta-rule-rec-p rule)))
    (access meta-rule-rec rule :trig-fnc))

  (defun rp-meta-dont-rw (rule)
    (declare (xargs :guard (weak-rp-meta-rule-rec-p rule)))
    (access meta-rule-rec rule :dont-rw))

  (defun rp-meta-syntax-verified (rule)
    (declare (xargs :guard (weak-rp-meta-rule-rec-p rule)))
    (access meta-rule-rec rule :valid-syntax))

  (defun rp-meta-outside-in (rule)
    (declare (xargs :guard (weak-rp-meta-rule-rec-p rule)))
    (access meta-rule-rec rule :outside-in))

  #|(defun rp-meta-rule-syntaxp (term)
  "Returned term from meta rule functin should meet this syntax."
  (rp-valid-termp term))||#

  (defun rp-meta-rule-rec-p (rule state)
    (declare (xargs :guard t
                    :stobjs (state)))
    (and (weak-rp-meta-rule-rec-p rule)
         (symbolp (rp-meta-fnc rule))
         (acl2::logicp (rp-meta-fnc rule) (w state))
         (symbolp (rp-meta-trig-fnc rule))
         (booleanp (rp-meta-dont-rw rule))
         (booleanp (rp-meta-syntax-verified rule))))

  (defun weak-rp-meta-rule-recs-p (xs)
    (declare (xargs :guard t))
    (if (atom xs)
        (eq xs nil)
      (and (weak-rp-meta-rule-rec-p (car xs))
           (weak-rp-meta-rule-recs-p (cdr xs)))))

  (defun rp-meta-rule-recs-p (rules state)
    (declare (xargs :guard t
                    :stobjs (state)))
    (if (atom rules)
        (eq rules nil)
      (and (rp-meta-rule-rec-p (car rules) state)
           (rp-meta-rule-recs-p (cdr rules) state))))

  (in-theory (disable weak-rp-meta-rule-rec-p
                      rp-meta-syntax-verified
                      rp-meta-dont-rw
                      rp-meta-trig-fnc
                      rp-meta-fnc))

  (defund rp-meta-valid-syntaxp (meta-rule term state)
    (declare (xargs :guard (rp-meta-rule-rec-p meta-rule state)
                    :stobjs (state)))
    (b* (((mv error res)
          (magic-ev-fncall (rp-meta-fnc meta-rule)
                           (list term)
                           state
                           t nil)))
      (implies
       (and (not error)
            (acl2::logicp (rp-meta-fnc meta-rule) (w state)))
       (and (if (rp-meta-dont-rw meta-rule)
                (and
                 (dont-rw-syntaxp (mv-nth 1 res))
                 (if (rp-meta-syntax-verified meta-rule)
                     (implies (rp-termp term)
                              (rp-termp (mv-nth 0 res)))
                   t))
              (and (if (rp-meta-syntax-verified meta-rule)
                       (implies (rp-termp term)
                                (rp-termp res))
                     t)))))))

  (defun-sk rp-meta-valid-syntaxp-sk (meta-rule state-)
    (declare (xargs :guard (and (STATE-P1 STATE-))
                    :verify-guards nil))
    (forall term
            (rp-meta-valid-syntaxp meta-rule term state-)))

  (defund rp-meta-valid-syntax-listp (meta-rules state)
    (declare (xargs :guard (rp-meta-rule-recs-p meta-rules state)
                    :verify-guards nil
                    :stobjs (state)))
    (if (atom meta-rules)
        (eq meta-rules nil)
      (and (rp-meta-valid-syntaxp-sk (car meta-rules) state)
           (rp-meta-valid-syntax-listp (cdr meta-rules) state))))


  (define simple-meta-rule-alistp (meta-rules)
    :short "A simple trig-fnc and meta-fnc pair list recognizer."
    :parents (rp-utilities)
    (declare (xargs :Guard t))
    (if (atom meta-rules)
        (eq meta-rules nil)
      (and (consp (car meta-rules))
           (symbolp (cdar meta-rules))
           (symbolp (caar meta-rules))
           (simple-meta-rule-alistp (cdr meta-rules)))))

  (define simple-meta-rule-alist-fix (x)
    :returns (res simple-meta-rule-alistp)
    (if (simple-meta-rule-alistp x)
        x
      nil))

  #|(defmacro rp-meta-rulesp (meta-rules &optional (state 'state))
  (declare (xargs :guard t)
  (ignorable state))
  `(and (weak-rp-meta-rule-recs-p ,meta-rules)
  ;;(rp-meta-valid-syntax-listp ,meta-rules ,state)
  ))||#)||#

(mutual-recursion
 (defun subtermp (term subterm)
   (declare (xargs :guard t))
   (cond ((atom term)
          (equal term subterm))
         ((quotep term)
          (equal term subterm))
         (t
          (or (equal term subterm)
              (equal (car term) subterm)
              (subtermp-lst (cdr term) subterm)))))
 (defun subtermp-lst (lst subterm)
   (if (atom lst)
       nil
     (or (subtermp (car lst) subterm)
         (subtermp-lst (cdr lst) subterm)))))

(encapsulate
  nil

  (defun rp-beta-reduce-get-val (key keys vals)
    (declare (xargs :guard t))
    (cond ((atom keys)
           (progn$ (cw "warning binding problem! ~p0 ~%" key)
                   key))
          ((equal key (car keys))
           (if (consp vals) (car vals) key))
          (t (rp-beta-reduce-get-val key (cdr keys)
                                     (if (consp vals) (cdr vals) nil)))))

  (mutual-recursion
   (defun rp-beta-reduce (term keys vals)
     (declare (xargs :guard t))
     (cond ((atom term)
            (rp-beta-reduce-get-val term keys vals))
           ((acl2::fquotep term) term)
           (t (cons-with-hint (car term)
                              (rp-beta-reduce-subterms (cdr term) keys vals)
                              term))))

   (defun rp-beta-reduce-subterms (subterms keys vals)
     (cond ((atom subterms) subterms)
           (t (cons-with-hint (rp-beta-reduce (car subterms) keys vals)
                              (rp-beta-reduce-subterms (cdr subterms) keys vals)
                              subterms)))))

  (defund rp-beta-reduce-main (term)
    (declare (xargs :guard t
                    :guard-hints (("Goal"
                                   :in-theory (e/d (is-lambda) ())))))
    (if (is-lambda term)
        (rp-beta-reduce (caddr (car term)) (cadar term) (cdr term))
      term)))

#|(encapsulate
  nil

  (local
   (defthm lemma1
     (implies (and (consp x)
                   (consp (cdr x)))
              (< (len (evens x))
                 (len x)))))

  (local
   (defthm lemma2
     (implies (and (consp x)
                   )
              (< (len (evens x))
                 (1+ (len x))))))

  (local
   (defthm lemma3
     (IMPLIES (AND (CONSP (CDR L)) (CONSP L))
              (< (LEN (EVENS L)) (+ 1 (LEN (CDR L)))))))

  (defun merge-comperator (l1 l2 acc comperator)
    (declare (xargs :guard (and (true-listp l1)
                                (true-listp l2)
                                (true-listp acc)
                                (symbolp comperator ))
                    :measure (+ (len l1) (len l2))))
    (cond
     ((endp l1)
      (revappend acc l2))
     ((endp l2)
      (revappend acc l1))
     ((apply$ comperator (list (car l1) (car l2)))
      (merge-comperator  (cdr l1)
                         l2
                         (cons (car l1) acc)
                         comperator))
     (t (merge-comperator  l1 (cdr l2)
                           (cons (car l2) acc) comperator))))

  (defun merge-comperator-sort (l comperator)
    (declare (xargs :guard (and (true-listp l)
                                (symbolp comperator))
                    :measure (len l)
                    :verify-guards nil))
    (cond ((endp (cdr l)) l)
          (t (merge-comperator
              (merge-comperator-sort (evens l) comperator)
              (merge-comperator-sort (odds l) comperator)
              nil
              comperator))))

  (local
   (defthm true-listp-of-merge-comprerator
     (implies (and (true-listp l1)
                   (true-listp l2)
                   (true-listp acc))
              (true-listp (merge-comperator l1 l2 acc comperator)))))

  (local
   (defthm true-listp-of-merge-sort
     (implies (true-listp l)
              (true-listp (merge-comperator-sort l comperator)))))

  (verify-guards merge-comperator-sort))|#

#|(define remove-disabled-meta-rules ((meta-rules weak-rp-meta-rule-recs-p)
                                    (disabled-meta-rules ))
  :guard-hints (("Goal"
                 :in-theory (e/d (weak-rp-meta-rule-rec-p) ())))
  (cond ((atom disabled-meta-rules)
         meta-rules)
        ((atom meta-rules)
         meta-rules)
        (t (b* ((entry (hons-assoc-equal (rp-meta-fnc (car meta-rules))
                                         disabled-meta-rules)))
             (if (and (consp entry)
                      (cdr entry))
                 (remove-disabled-meta-rules (cdr meta-rules)
                                             disabled-meta-rules)
               (cons (car meta-rules)
                     (remove-disabled-meta-rules (cdr meta-rules)
                                                 disabled-meta-rules)))))))||#


(defund get-rune-name (fn state)
    (declare (xargs :guard (and (symbolp fn))
                    :stobjs (state)
                    :verify-guards t))
    (b* ((mappings
          (getpropc fn 'acl2::runic-mapping-pairs
                    nil (w state)))
         ((when (atom mappings))
          (progn$ (and (equal (meta-extract-formula fn state) ''t)
                       (hard-error 'get-rune-name
                                   " ~p0 does not seem to have a rune nor an associated formula. ~%"
                                   (list (cons #\0 fn))))
                  `(:unknown ,fn)))
         (mapping (car mappings)))
      (if (consp mapping)
          (cdr mapping)
        fn)))




(defun trans-list (lst)
  (declare (xargs :guard t))
  (if (atom lst)
      ''nil
    (if (atom (cdr lst))
        `(cons ,(car lst) 'nil)
      `(cons ,(car lst) ,(trans-list (cdr lst))))))

(progn
  (mutual-recursion
   (defun rp-trans (term)
     (cond ((atom term)
            term)
           ((quotep term)
            term)
           ((and (equal (car term) 'list))
            (trans-list (rp-trans-lst (cdr term))))
           ((and (equal (car term) 'falist) ;; not using is-falist so that I
                 ;; can prove (equal (rp-trans (rp-trans x)) (rp-trans x)). 
                 (consp (cdr term))
                 (consp (cddr term)))
            (rp-trans (caddr term)))
           (t (cons-with-hint (car term)
                              (rp-trans-lst (cdr term))
                              term))))
   (defun rp-trans-lst (lst)
     (if (atom lst)
         nil
       (cons-with-hint (rp-trans (car lst))
                       (rp-trans-lst (cdr lst))
                       lst))))

  (make-flag rp-trans :defthm-macro-name defthm-rp-trans)

  (local
   (defthm rp-trans-lst-consp
     (equal (consp (rp-trans-lst lst))
            (consp lst))
     :hints (("Goal"
              :induct (len lst)
              :in-theory (e/d () ())))))

  (verify-guards rp-trans)

  (memoize 'rp-trans))

(mutual-recursion
 (defun rp-untrans (term)
   (declare (xargs :guard t))
   (cond ((atom term)
          term)
         ((quotep term)
          term)
         ((is-cons term)
          (b* ((a (rp-untrans (cadr term)))
               (b (rp-untrans (caddr term))))
            (case-match b
              (('list . rest)
               `(list ,a . ,rest))
              (&
               `(list ,a ,b)))))
         (t (cons (car term)
                  (rp-untrans-lst (cdr term))))))
 (defun rp-untrans-lst (lst)
   (if (atom lst)
       nil
     (cons (rp-untrans (car lst))
           (rp-untrans-lst (cdr lst))))))


(defund force$ (term rule-name hyp)
  ;; When rules are processed, force instances in the hyps will be replaced by
  ;; this function. rule-name will be the name of the rule, and hyp will be the
  ;; main hypothesis from the rule that the force is originated from.
  ;; when force fails, an error will be thrown and a message will be printed
  ;; the rule that causes this error is in user-lemmas.lisp
  (declare (ignorable rule-name hyp))
  term)


(defconst *rw-recent-terms-size-width*
  6)
(defconst *rw-recent-terms-size*
   ;; the last value will be used for the pointer.
  (1+ (expt 2 *rw-recent-terms-size-width*)))

(defstobj rp-state

  
  ;;(context :type (satisfies rp-term-listp)  :initially nil)
  ;;(iff-flg :type (satisfies booleanp) :initially nil)
  ;;(outside-in-flg :type (satisfies booleanp) :initially nil)

  (rules-alist-inside-out :type (hash-table eq) :initially nil)
  (rules-alist-outside-in :type (hash-table eq) :initially nil)
  (disabled-exc-rules :type (hash-table eq) :initially nil) 
  
  (show-used-rules-flg :type (satisfies booleanp) :initially t)
  (count-used-rules-flg :type (satisfies booleanp) :initially nil)
  (rules-used :type (hash-table equal) :initially nil)

  (rp-brr :type (satisfies booleanp) :initially nil)
  (rw-stack-size :type (satisfies integerp) :initially 0)
  (rw-stack :type (satisfies alistp) :initially nil)
  (rule-frame-cnts :type (satisfies alistp) :initially nil)

  (rw-step-limit :type (unsigned-byte 58) :initially 100000)
  (rw-backchain-limit :type (unsigned-byte 58) :initially 1000)
  (rw-backchain-limit-throws-error :type (satisfies booleanp) :initially t) ;; to be set outside and read internally when starting to rewrite a hyp.
  (rw-limit-throws-error :type (satisfies booleanp) :initially t) ;; to be used only internally.
  (backchaining-rule :type t :initially nil)
  
  (rw-context-disabled :type (satisfies booleanp) :initially nil) 

  (not-simplified-action :type (satisfies symbolp) :initially :error)
  (suppress-not-simplified-error :type (satisfies booleanp) :initially nil) ;; only used internally

  (casesplitter-cases :type (satisfies rp-term-listp) :initially nil)

  (orig-conjecture :type (satisfies rp-termp) :initially ''nil)


  (rw-recent-terms :type (array t (*rw-recent-terms-size*)))
  ;; the last value will be the pointer.

  :inline t)

(encapsulate
  nil
  (local
   (include-book "arithmetic-3/top" :dir :system))
  (defund push-to-rw-recent-terms (term rp-state)
    (declare (xargs :stobjs (rp-state)))
    (b* ((length (mbe :exec *rw-recent-terms-size*
                      :logic (rw-recent-terms-length rp-state)))
         (ptr (nfix (rw-recent-termsI (1- length) rp-state)))
         (rp-state (update-rw-recent-termsI (1- length)
                                            (1+ ptr)
                                            rp-state))
         (rp-state (update-rw-recent-termsI (mod ptr (1- length))
                                            term
                                            rp-state)))
      rp-state))

  (defund get-from-rw-recent-terms (cnt rp-state)
    (declare (xargs :stobjs (rp-state)
                    :guard (integerp cnt)))
    (b* ((ptr (nfix (rw-recent-termsI (1- *rw-recent-terms-size*) rp-state)))
         (ptr (- ptr cnt))
         (term (rw-recent-termsI (mod ptr (1- *rw-recent-terms-size*))
                                 rp-state)))
      term)))


(in-theory (disable rules-alist-inside-out-put
                    rules-alist-outside-in-put
                    disabled-exc-rules-put

                    rules-alist-inside-out-get
                    rules-alist-outside-in-get
                    disabled-exc-rules-get

                    rules-alist-inside-out-boundp
                    rules-alist-outside-in-boundp
                    disabled-exc-rules-boundp

                    disabled-exc-rules-init
                    rules-alist-inside-out-init
                    rules-alist-outside-in-init
                    ))

(defund rp-state-new-run (rp-state)
  (declare (xargs :stobjs (rp-state)))
  (b* ((rp-state (update-rw-context-disabled nil rp-state))
       (rp-state (update-rw-limit-throws-error t rp-state))
       (rp-state (update-backchaining-rule nil rp-state))
       (rp-state (update-casesplitter-cases nil rp-state))
       (rp-state (rules-used-clear rp-state))
       (rp-state (update-rw-stack-size 0 rp-state))
       (rp-state (update-rw-stack nil rp-state))
       (rp-state (update-rule-frame-cnts nil rp-state)))
    rp-state))

;; (defmacro set-outside-in-flg ()
;;   `(update-outside-in-flg t rp-state))

;; (defmacro unset-outside-in-flg ()
;;   `(update-outside-in-flg nil rp-state))


;; (defmacro set-iff-flg ()
;;   `(update-iff-flg t rp-state))

;; (defmacro unset-iff-flg ()
;;   `(update-iff-flg nil rp-state))


(defun-sk valid-rp-state-syntaxp-aux (rp-state)
  (declare (xargs :stobjs (rp-state)))
  (forall key
          (or 
           (not (symbolp key))
           (and  (rule-list-syntaxp
                  (rules-alist-outside-in-get key rp-state))
                 (rule-list-syntaxp
                  (rules-alist-inside-out-get key rp-state))))))

(verify-guards valid-rp-state-syntaxp-aux
  :hints (("Goal"
           :in-theory (e/d () (rp-statep
                               rule-list-syntaxp)))))



;; :i-am-here

;; (define valid-rp-state-syntaxp-exec (rp-state)
;;   (b* ((

(define valid-rp-state-syntaxp (rp-state)
  (and (rp-statep rp-state)
       (valid-rp-state-syntaxp-aux rp-state))
  ///
  (defthm VALID-RP-STATE-SYNTAXP-implies
    (implies (VALID-RP-STATE-SYNTAXP rp-state)
             (AND (RP-STATEP RP-STATE)
                  (VALID-RP-STATE-SYNTAXP-AUX RP-STATE)))
    :rule-classes (:forward-chaining)
    :hints (("Goal"
             :in-theory (e/d (VALID-RP-STATE-SYNTAXP) ())))))

(defun-sk rp-state-preservedp-sk (old-rp-state new-rp-state)
  (declare (xargs :verify-guards nil))
  (forall key
          (or (not (symbolp key))
              (and  (equal (rules-alist-outside-in-get key old-rp-state)
                           (rules-alist-outside-in-get key new-rp-state))
                    (equal (rules-alist-inside-out-get key old-rp-state)
                           (rules-alist-inside-out-get key new-rp-state))))))
              

(define rp-state-preservedp (old-rp-state new-rp-state)
  :verify-guards nil
  (and (rp-statep new-rp-state)
       (rp-state-preservedp-sk old-rp-state
                               new-rp-state)))     
       

(define not* (x)
  (not x)
  ///
  (defthm not*-forward
    (implies (not* x)
             (not x))
    :rule-classes :forward-chaining)
  (defthm not-of-not*-forward
    (implies (not (not* a))
             a)
    :rule-classes :forward-chaining))


(defund casesplit-from-context-trig (x)
  (declare (xargs :guard t))
  x)

(defund dont-rw-context (x)
  (declare (xargs :guard t))
  x)

(progn
  (defund is-dont-rw-context (x)
    (declare (xargs :guard t))
    (case-match x
      (('dont-rw-context &)
       t)))
  (defthm is-dont-rw-context-implies
    (implies (is-dont-rw-context x)
             (case-match x
               (('dont-rw-context &)
                t)))
    :rule-classes :forward-chaining
    :hints (("Goal"
             :in-theory (e/d (is-dont-rw-context) ())))))

(progn
  (defund binary-and** (x y)
    (declare (xargs :guard t))
    (and x y))

  (DEFUN AND**-MACRO (ACL2::LST)
    (IF (ATOM ACL2::LST)
        T
        (IF (ATOM (CDR ACL2::LST))
            (CAR ACL2::LST)
            (LIST 'BINARY-AND**
                  (CAR ACL2::LST)
                  (AND**-MACRO (CDR ACL2::LST))))))

  (defmacro and** (&rest lst)
    `(mbe :logic ,(and**-macro lst)
          :exec (hide ,(and-macro lst))))

  (defmacro and*e (&rest lst)
    `(mbe :logic ,(acl2::and*-macro lst)
          :exec (hide ,(and-macro lst))))
  
  (defund binary-or** (x y)
    (declare (xargs :guard t))
    (or x y))

  (DEFUN OR**-MACRO (ACL2::LST)
    (IF (ATOM ACL2::LST)
        T
        (IF (ATOM (CDR ACL2::LST))
            (CAR ACL2::LST)
            (LIST 'BINARY-OR**
                  (CAR ACL2::LST)
                  (OR**-MACRO (CDR ACL2::LST))))))

  (defmacro or** (&rest lst)
    `(mbe :logic ,(or**-macro lst)
          :exec (hide ,(or-macro lst))))

  (defmacro or*e (&rest lst)
    `(mbe :logic ,(acl2::or*-macro lst)
          :exec (hide ,(or-macro lst))))

  (defund not** (x)
    (declare (xargs :guard t))
    (not x)))

(defund if** (x y z)
  (declare (xargs :guard t))
  (if x y z))