File: aux-function-lemmas.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 (1447 lines) | stat: -rw-r--r-- 48,010 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
; 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 "../aux-functions")

(make-flag include-fnc-fn :defthm-macro-name defthm-include-fnc)

(make-flag rp-termp :defthm-macro-name defthm-rp-termp)
(make-flag beta-search-reduce :defthm-macro-name defthm-beta-search-reduce)
#|(make-flag all-falist-consistent :defthm-macro-name
defthm-all-falist-consistent)||#
#|(make-flag rp-syntaxp :defthm-macro-name defthm-rp-syntaxp)||#
(make-flag lambda-exp-free-p :defthm-macro-name defthm-lambda-exp-free-p)

(make-flag get-lambda-free-vars :defthm-macro-name defthm-get-lambda-free-vars)

(make-flag ex-from-rp-all2 :defthm-macro-name defthm-ex-from-rp-all2)

(local
 (make-flag get-lambda-free-vars :defthm-macro-name
            defthm-get-lambda-free-vars))

(local
 (in-theory (enable falist-consistent)))

(make-event
 `(defthm is-lambda-implies
    (implies (is-lambda term)
             ,(beta-search-reduce (caddar (caddr (meta-extract-formula
                                                  'is-lambda state)))
                                  1000))
    :rule-classes :forward-chaining
    :hints (("Goal"
             :in-theory (e/d (is-lambda) ())))))

(make-event
 `(defthm is-lambda-strict-implies
    (implies (is-lambda-strict x)
             ,(beta-search-reduce (caddar (caddr (meta-extract-formula
                                                  'is-lambda-strict state)))
                                  1000))
    :rule-classes :forward-chaining
    :hints (("Goal"
             :in-theory (e/d (is-lambda is-lambda-strict) ())))))

(defthm get-lambda-free-vars-implies
  (implies
   (and (mv-nth 0 (get-lambda-free-vars term))
        (is-lambda term))
   (b* (((mv valid sub-vars) (get-lambda-free-vars (caddr (car term))))
        (lambda-vars (cadr (car term)))
        ((mv valid-2 &) (get-lambda-free-vars-lst (cdr term))))
     (and valid
          valid-2
          (equal (remove-vars sub-vars lambda-vars) nil))))
  :rule-classes :forward-chaining
  :hints (("Goal"
           :in-theory (e/d (is-lambda is-lambda-strict get-lambda-free-vars)
                           ()))))

(set-ignore-ok t)

(make-event
 `(defthm rp-termp-implies
    (implies (rp-termp term)
             ,(caddr (meta-extract-formula
                      'rp-termp state)))
    :rule-classes :forward-chaining
    :hints (("Goal"
             :in-theory (e/d (is-lambda is-lambda-strict) ())))))

(make-event
 `(defthm lambda-exp-free-p-implies
    (implies (lambda-exp-free-p term)
             ,(caddr (meta-extract-formula
                      'lambda-exp-free-p state)))
    :rule-classes :forward-chaining
    :hints (("Goal"
             :in-theory (e/d (is-lambda is-lambda-strict lambda-exp-free-p )
                             ())))))

(make-event
 `(defthm LAMBDA-EXP-FREE-LISTP-implies
    (implies (LAMBDA-EXP-FREE-LISTP subterms)
             ,(caddr (meta-extract-formula
                      'LAMBDA-EXP-FREE-LISTP state)))
    :rule-classes :forward-chaining
    :hints (("Goal"
             :in-theory (e/d (is-lambda is-lambda-strict lambda-exp-free-p LAMBDA-EXP-FREE-LISTP )
                             ())))))

(defret rp-termp-dumb-negate-lit2
  (implies (rp-termp term)
           (rp-termp new-term))
  :fn dumb-negate-lit2
  :hints (("goal" :in-theory (enable rp-term-listp
                                     dumb-negate-lit2))))

#|(defthm-remove-return-last
(defthm rp-termp-remove-return-last
(implies (rp-termp term)
(rp-termp (remove-return-last term)))
:flag remove-return-last)
(defthm rp-term-listp-remove-return-last-subterms
(implies (rp-term-listp subterms)
(rp-term-listp (remove-return-last-subterms subterms)))
:flag remove-return-last-subterms)
:hints (("Goal" :in-theory (enable rp-term-listp
rp-termp))))||#

(defthm-rp-termp
  (defthm rp-termp-implies-pseudo-termp
    (implies (rp-termp term)
             (pseudo-termp term))
    :flag rp-termp)

  (defthm rp-term-list-implies-pseudo-term-listp
    (implies (rp-term-listp lst)
             (pseudo-term-listp lst))
    :flag rp-term-listp)
  :hints (("Goal"
           :in-theory (e/d (is-rp
                            falist-consistent)
                           ()))))

(defthm strip-cdrs-pseudo-termlistp2
  (implies
   (and (rp-term-listp (strip-cdrs alist))
        (cdr (assoc-equal key alist)))
   (rp-termp (cdr (assoc-equal key alist)))))

(defthm rp-termp-bindings-lemma1
  (implies (and (rp-term-listp (strip-cdrs bindings))
                (consp (assoc-equal term bindings)))
           (cdr (assoc-equal term bindings))))

(defthm rp-term-listp-is-true-listp
  (implies (rp-term-listp lst)
           (true-listp lst))
  :hints (("Goal" :expand (rp-term-listp lst))))

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

  ;; Removed in July 2021 by Matt Kaufmann since the right-hand sides of the
  ;; generated rules are now all constants, due to enhancements to
  ;; remove-guard-holders.
  #||
  (defthm is-synp-implies
  (implies (is-synp term)
  (CASE-MATCH TERM (('SYNP & & &) T)
  (& NIL)))
  :hints (("Goal" :in-theory (enable is-synp))))
  ||#

  (defthm pseudo-termlistp-extract-from-rp
    (implies (and (rp-termp term)
                  (not (quotep (ex-from-rp term))))
             (rp-term-listp (cdr (ex-from-rp term))))
    :hints (("Goal"
             :Expand (rp-termp term)
             :in-theory (e/d (is-rp) ()))))

  (defthm rp-termp-extract-from-rp
    (implies (and (rp-termp term))
             (rp-termp (ex-from-rp term))))

  (defthm extract-from-synp-to-ex-from-rp
    (equal (extract-from-synp term)
           (ex-from-synp term))
    :hints (("Goal" :in-theory (enable is-synp ex-from-synp extract-from-synp))))

  (defthm pseudo-termp-extract-from-synp
    (implies (rp-termp term)
             (rp-termp (ex-from-synp term)))
    :hints (("Goal" :in-theory (enable extract-from-synp))))

  (defthm atom-rp-termp-is-symbolp
    (implies (and (atom x) (rp-termp x)) (symbolp x)))

  (defthm rp-termp-context-from-rp
    (implies (and (rp-termp term)
                  (rp-term-listp context))
             (rp-term-listp (context-from-rp term context)))
    :hints (("goal"
             :in-theory (e/d (context-from-rp ex-from-rp is-rp) ())
             :expand (rp-termp term))))

  (defthm put-term-in-cons-is-pseudo-termp
    (rp-termp (put-term-in-cons term))
    :hints (("goal" :in-theory (enable is-rp put-term-in-cons))))

  (defthm extract-from-rp-with-context-to-no-context
    (equal (mv-nth 1 (extract-from-rp-with-context term context))
           (ex-from-rp term))
    :hints (("goal" :in-theory (enable extract-from-rp-with-context
                                       is-rp
                                       ex-from-rp))))

  (defthm extract-from-rp-with-context-context
    (equal (mv-nth 0 (extract-from-rp-with-context term context))
           (context-from-rp term context))
    :hints (("goal" :in-theory (enable extract-from-rp-with-context
                                       is-rp
                                       context-from-rp
                                       ex-from-rp))))

  (defthm cons-consp-context-from-rp
    (implies (cons-consp context)
             (cons-consp (context-from-rp term context)))
    :hints (("Goal"
             :induct (context-from-rp term context)
             :in-theory (enable cons-consp is-rp context-from-rp))))

  (defthm symbolp-ex-from-rp
    (implies (and (rp-termp term)
                  (not (consp (ex-from-rp term))))
             (symbolp (ex-from-rp term))))

  (defthm ex-from-rp-x2
    (equal (ex-from-rp (ex-from-rp term))
           (ex-from-rp term))
    :hints (("Goal" :in-theory (enable ex-from-rp is-rp))))

  (defthm ex-from-synp-ex-from-rpx2
    (equal (EX-FROM-SYNP
            (EX-FROM-RP (EX-FROM-SYNP (EX-FROM-RP term))))
           (EX-FROM-SYNP (EX-FROM-RP term)))
    :hints (("Goal" :in-theory (enable ex-from-rp
                                       ex-from-synp
                                       is-rp
                                       is-synp)))))

(defthm append-of-two-context
  (implies (and (context-syntaxp c1)
                (context-syntaxp c2))
           (context-syntaxp (append c1 c2)))
  :hints (("Goal" :in-theory (enable rp-term-listp
                                     context-syntaxp))))

;; acl2::beta-eval-to-beta-reduce-lambda-expr

(defthm-beta-search-reduce
  (defthm eval-of-beta-search-reduce
    (implies (pseudo-termp term)
             (equal (acl2::beta-eval (beta-search-reduce term limit) a)
                    (acl2::beta-eval term a)))
    :flag beta-search-reduce)

  (defthm eval-of-beta-search-reduce-subterms
    (implies (pseudo-term-listp subterms)
             (equal (acl2::beta-eval-list (beta-search-reduce-subterms subterms limit) a)
                    (acl2::beta-eval-list subterms a)))
    :flag beta-search-reduce-subterms)
  :hints (("Goal" :in-theory (enable acl2::beta-eval-constraint-0))))

(defthm eval-of-beta-search-reduce-fixed-lim
  (implies (pseudo-termp (car cl))
           (equal (acl2::beta-eval (beta-search-reduce (car cl) *big-number*) a)
                  (acl2::beta-eval (car cl) a))))

(defthm falist-consistent-implies-falist-syntaxp
  (implies (and (rp-termp term)
                (falist-consistent-aux falist term))
           (falist-syntaxp falist)))

(defthm extract-from-rp-with-context-to-context-from-rp
  (equal (mv-nth 0 (extract-from-rp-with-context term context))
         (context-from-rp term context))
  :hints (("Goal" :in-theory (enable context-from-rp is-rp))))

(defthm remove-from-alist-returns-alistp
  (implies (alistp x)
           (alistp (remove-from-alist x key))))

;; (defthm rp-stat-add-to-rules-used-returns-rp-stat-p
;;   (implies (rp-stat-p stat)
;;            (rp-stat-p (rp-stat-add-to-rules-used rule stat failed)))
;;   :hints (("Goal" :in-theory (enable rp-stat-p
;;                                      rp-stat-add-to-rules-used))))

;; (defthm rp-stat-add-to-rules-used-returns-rp-stat-p2
;;   (implies (rp-stat-p stat)
;;            (rp-stat-p (rp-stat-add-to-rules-used-ex-cnt rule stat)))
;;   :hints (("Goal" :in-theory (enable rp-stat-p
;;                                      rp-stat-add-to-rules-used-ex-cnt))))

#|(defthm all-falist-consistent-bindings-lemma1
(implies (all-falist-consistent-bindings bindings)
(all-falist-consistent (cdr (assoc-equal term bindings)))))||#

#|(encapsulate
nil

(local
(defthm is-rpl-is-not-is-falist
(implies (is-rp term)
(not (is-falist term)))
:hints (("Goal" :in-theory (enable is-rp is-falist)))))

(local
(defthm lemma2
(implies (is-rp term)
(and (not (EQUAL (CAR TERM) 'QUOTE))
(CONSP TERM)))
:hints (("Goal" :in-theory (enable is-rp is-falist)))))

(defthm all-falist-consistent-ex-from-rp
(implies (all-falist-consistent term)
(all-falist-consistent (ex-from-rp term)))
:hints (("Goal"
:induct (ex-from-rp term)
:expand ((all-falist-consistent term))
:in-theory (e/d (ex-from-rp)
(IS-FALIST all-falist-consistent)))))

(defthm all-falist-consistent-put-term-in-cons
(implies (all-falist-consistent term)
(all-falist-consistent (put-term-in-cons term)))
:hints (("Goal" :in-theory (enable put-term-in-cons))))

(defthm all-falist-consistent-lst-context-from-rp
(implies (and (all-falist-consistent-lst context)
(all-falist-consistent term))
(all-falist-consistent-lst (context-from-rp term context)))
:hints (("Goal"
:induct (context-from-rp term context)
:expand ((ALL-FALIST-CONSISTENT-LST (CDDR TERM))
(ALL-FALIST-CONSISTENT-LST (CDR TERM)))
:in-theory (e/d (context-from-rp is-rp)))))

(defthm cdr-term-is-all-falist-consistent-lst
(implies (and (not (quotep term))
(all-falist-consistent term))
(all-falist-consistent-lst (cdr term)))
:hints (("Goal"
:expand ((ALL-FALIST-CONSISTENT TERM)
(ALL-FALIST-CONSISTENT-LST (CDR TERM)))
:in-theory (enable all-falist-consistent
all-falist-consistent-lst
is-falist)))))||#

#|(defthm all-falist-consistent-lst-cdr-term-lemma
(implies (and (all-falist-consistent term)
(not (equal (car term) 'quote)))
(all-falist-consistent-lst (cdr term)))
:hints (("Goal"
:do-not-induct t
:expand ((all-falist-consistent term)
(FALIST-CONSISTENT TERM)
(ALL-FALIST-CONSISTENT (CADR TERM))
(ALL-FALIST-CONSISTENT-LST (CDR TERM)))
:in-theory (enable is-falist all-falist-consistent-lst))))||#

#|(defthm-all-falist-consistent
(defthm not-include-falist-all-falist-consistent
(implies (not (include-fnc term 'falist))
(all-falist-consistent term))
:flag all-falist-consistent)
(defthm not-include-falist-all-falist-consistent-lst
(implies (not (include-fnc-subterms lst 'falist))
(all-falist-consistent-lst lst))
:flag all-falist-consistent-lst)
:hints (("Goal"
:in-theory (e/d (is-falist
all-falist-consistent-lst
all-falist-consistent)
(falist-consistent)))))||#

(encapsulate
  nil

  (local
   (defthm lemma1
     (implies (and (is-lambda (cons (car term) subterms))
                   (rp-termp term))
              (is-lambda-strict term))
     :rule-classes :forward-chaining
     :hints (("Goal"
              :in-theory (e/d (is-lambda-strict is-lambda) ())))))

  (defthm get-lambda-free-vars-of-cons-car-term-subterms
    (implies (and (rp-termp term)
                  (mv-nth 0 (get-lambda-free-vars-lst subterms)))
             (mv-nth 0 (get-lambda-free-vars (cons (car term)
                                                   subterms))))
    :otf-flg t
    :hints (("Goal"
             :expand ((get-lambda-free-vars (cons (car term)
                                                  subterms)))
             :in-theory (e/d () ())))))

(defthm-rp-termp
  (defthm rp-termp-implies-get-lambda-free-vars
    (implies (rp-termp term)
             (mv-nth 0
                     (get-lambda-free-vars term)))
    :flag rp-termp)
  (defthm rp-term-listp-implies-get-lambda-free-vars-lst
    (implies (rp-term-listp lst)
             (mv-nth 0
                     (get-lambda-free-vars-lst lst)))
    :flag rp-term-listp)
  :hints (("Goal"
           :in-theory (e/d (is-rp falist-consistent) ()))))

(defthm get-lambda-free-vars-of-bindings
  (implies (and (rp-term-listp (strip-cdrs bindings))
                (consp (assoc-equal term bindings)))
           (mv-nth 0
                   (get-lambda-free-vars (cdr (assoc-equal term bindings))))))

(encapsulate
  nil

  #|(local
  (defthm lemma1
  (implies (and (is-lambda term)
  (mv-nth 0 (get-lambda-free-vars term)))
  (equal (mv-nth 1 (get-lambda-free-vars term))
  (mv-nth 1 (get-lambda-free-vars-lst (cdr term)))))
  :hints (("Goal"
  :expand ((get-lambda-free-vars term))
  :in-theory (e/d () ())))))
  (local
  (defthm lemma2
  (implies (and (booleanp x)
  x)
  (equal (equal t x) t))))

  #|(skip-proofs
  (local
  (defthm lemma3
  (booleanp (mv-nth 0 (get-lambda-free-vars-lst lst))))))||#

  (local
  (defthm-get-lambda-free-vars
  (defthm booleanp-get-lambda-free-vars
  (booleanp (mv-nth 0 (get-lambda-free-vars term)))
  :flag get-lambda-free-vars)
  (defthm booleanp-get-lambda-free-vars-lst
  (booleanp (mv-nth 0 (get-lambda-free-vars-lst lst)))
  :flag get-lambda-free-vars-lst)))

  (local
  (defthm lemma4
  (implies (and (is-lambda term)
  (mv-nth 0 (get-lambda-free-vars term)))
  (equal (mv-nth 0 (get-lambda-free-vars term))
  (mv-nth 0 (get-lambda-free-vars-lst (cdr term)))))
  :hints (("Goal"
  :expand ((get-lambda-free-vars term))
  :in-theory (e/d () ())))))

  (local
  (defthm lemma5
  (implies (and (mv-nth 0 (GET-LAMBDA-FREE-VARS-LST (CDR TERM)))
  (EQUAL (GET-LAMBDA-FREE-VARS-LST SUBTERMS)
  (GET-LAMBDA-FREE-VARS-LST (CDR TERM))))
  (MV-NTH 0 (GET-LAMBDA-FREE-VARS-LST SUBTERMS)))))||#

  #|(local
  ;; this was used when lambda expressions were allowed
  (defthm rp-termp-cons-car-term-subterms-lemma1
  (implies (and (rp-termp term)
  (consp term)
  (consp (car term))
  (equal (len subterms)
  (len (cdr term)))
;(mv-nth 0 (get-lambda-free-vars-lst subterms)) ; ;
  #|(lambda-exp-free-listp subterms)||#
  (rp-term-listp subterms))
  (rp-termp (cons (car term) subterms)))
  :otf-flg t
  :hints (("Goal"
  :do-not-induct t
  :expand ((rp-termp term)
  (GET-LAMBDA-FREE-VARS (CONS (CAR TERM) SUBTERMS))
  (is-lambda-strict (cons (car term) subterms))
  (IS-LAMBDA (CONS (CAR TERM) SUBTERMS)))
  :in-theory (e/d () (get-lambda-free-vars))))))||#

  (defthm rp-termp-cons-car-term-subterms
    (implies (and (rp-termp term)
                  (consp term)
                  #|(or ;(atom (car term))
                  #|(and (equal (len subterms)
                  (len (cdr term)))
                  #|(lambda-exp-free-listp subterms)||#)||#)||#
                  (not (equal (car term) 'quote))
                  (force (or (not (equal (car term) 'rp))
                             (is-rp (cons (car term) subterms))))
                  (force (or (not (equal (car term) 'falist))
                             (falist-consistent (cons (car term) subterms))))
                  (rp-term-listp subterms))
             (rp-termp (cons (car term) subterms)))
    :hints (("Goal"
             :in-theory (e/d (is-rp
                              falist-consistent) ())))
    ))

(defthm rp-term-listp-append
  (implies (and (rp-term-listp lst1)
                (rp-term-listp lst2))
           (rp-term-listp (append lst1 lst2)))
  :hints (("Goal" :in-theory (enable append rp-term-listp))))

#|(defthm ALL-FALIST-CONSISTENT-LST-append
(implies (and (ALL-FALIST-CONSISTENT-LST lst1)
(ALL-FALIST-CONSISTENT-LST lst2))
(ALL-FALIST-CONSISTENT-LST (append lst1 lst2)))
:hints (("Goal" :in-theory (enable append ALL-FALIST-CONSISTENT-LST))))||#

(encapsulate
  nil

  (local
   (defthm is-falist-remove-return-last-subterms
     (implies (consp term)
              (equal (is-falist (cons (car term)
                                      (remove-return-last-subterms (cdr term))))
                     (is-falist term)))
     :hints (("Goal"
; :in-theory (disable is-falist)
              :Expand ((REMOVE-RETURN-LAST-SUBTERMS (CDR TERM))
                       (REMOVE-RETURN-LAST-SUBTERMS (CDDDR TERM))
                       (REMOVE-RETURN-LAST-SUBTERMS (CDDR TERM)))))))

  (local
   (defthm not-is-falist-cons-quote-return-last
     (not (is-falist (cons 'return-last x)))))

  (local
   (defthm lemma1
     (implies (is-falist term)
              (equal (car term) 'falist))))
  #|
  ;; needed for the guards of rp-rw-aux
  (defthm-remove-return-last
  (defthm all-falist-consistent-remove-return-last
  (implies (all-falist-consistent term)
  (all-falist-consistent (remove-return-last term)))
  :flag remove-return-last)

  (defthm all-falist-consistent-remove-return-last-subterms
  (implies (all-falist-consistent-lst subterms)
  (all-falist-consistent-lst (remove-return-last-subterms subterms)))
  :flag remove-return-last-subterms)
  :hints (("Goal"
  :expand
  ((ALL-FALIST-CONSISTENT TERM)
  (ALL-FALIST-CONSISTENT (CONS (CAR TERM)
  (REMOVE-RETURN-LAST-SUBTERMS (CDR TERM)))))
  :in-theory (e/d () (is-falist falist-consistent-aux
  falist-consistent)))))||#)

(defthm dont-rw-syntaxp-dont-rw-if-fix
  (implies (and (dont-rw-syntaxp dont-rw))
           (and (dont-rw-syntaxp (dont-rw-if-fix dont-rw))
                (dont-rw-syntaxp (cadr (dont-rw-if-fix dont-rw)))
                (dont-rw-syntaxp (caddr (dont-rw-if-fix dont-rw)))
                (dont-rw-syntaxp (cadddr (dont-rw-if-fix dont-rw)))))
  :hints (("Goal"
           :in-theory (e/d (dont-rw-syntaxp
                            dont-rw-if-fix)
                           ()))))

#|(encapsulate
nil
(local
(defthm lemma1
(implies (is-rp term)
(equal (REMOVE-RETURN-LAST (CADR TERM))
(cadr term)))
:hints (("Goal"
:in-theory (e/d (is-rp) ())))))
(local
(DEFTHM IS-RP-IMPLIES-lemma
(IMPLIES (IS-RP TERM)
(CASE-MATCH TERM
(('RP ('QUOTE TYPE) &)
(AND (SYMBOLP TYPE)
(NOT (EQUAL TYPE 'QUOTE))))
(& NIL)))
:hints (("Goal"
:in-theory (e/d (is-rp) ())))
:rule-classes :forward-chaining))

||#

;; (defthm RP-SYNTAXP-EX-FROM-RP-TERM
;;   (implies (rp-syntaxp term)
;;            (rp-syntaxp (ex-from-rp term)))
;;   :hints (("Goal"
;;            :induct (ex-from-rp term)
;;            :in-theory (e/d (is-rp
;;                             ex-from-rp) ()))))

;; (defthm-rp-syntaxp
;;   (defthm not-include-rp-means-rp-syntaxp
;;     (implies (not (include-fnc term 'rp))
;;              (rp-syntaxp term))
;;     :flag rp-syntaxp)
;;   (defthm not-include-rp-means-rp-syntaxp-lst
;;     (implies (not (include-fnc-subterms lst 'rp))
;;              (rp-syntaxp-lst lst))
;;     :flag rp-syntaxp-lst))

#|(defthm-remove-return-last
(defthm not-include-rp-of-remove-return-last
(implies (not (include-fnc term 'rp))
(not (include-fnc (remove-return-last term) 'rp)))
:flag remove-return-last)
(defthm not-include-rp-of-remove-return-last-subterms
(implies (not (include-fnc-subterms subterms 'rp))
(Not (include-fnc-subterms (remove-return-last-subterms subterms) 'rp)))
:flag remove-return-last-subterms))||#

#|(defthm rp-syntaxp-ex-from-rp
(implies (rp-syntaxp term)
(rp-syntaxp (ex-from-rp term)))
:hints (("Goal"
:in-theory (e/d (ex-from-rp
is-rp) ()))))||#
#|
(defthm rp-syntaxp-ex-from-rp-lemma
(implies (and (rp-syntaxp term)
(not (equal (car (ex-from-rp term))
'quote)))
(rp-syntaxp-lst (cdr (ex-from-rp term))))
:hints (("Goal"
:in-theory (e/d (ex-from-rp
is-rp) ()))))

(defthm rp-syntaxp-lst-context-from-rp
(implies (and (rp-syntaxp term)
(rp-syntaxp-lst context))
(rp-syntaxp-lst (context-from-rp term context)))
:hints (("Goal"
:in-theory (e/d (context-from-rp
is-rp) ()))))

(defthm rp-syntaxp-put-term-in-cons
(implies
(should-term-be-in-cons rule-lhs term)
(and (rp-syntaxp (put-term-in-cons term))
(RP-SYNTAXP-LST (CDR (PUT-TERM-IN-CONS term)))))
:hints (("Goal"
:in-theory (e/d (put-term-in-cons
should-term-be-in-cons) ()))))

(encapsulate
nil
(local
(defthm lemma1
(IMPLIES
(AND
(CONSP TERM)
(NOT (EQUAL (CAR TERM) 'QUOTE))
(NOT (EQUAL (CAR TERM) 'RP))
(RP-SYNTAXP-LST
(REMOVE-RETURN-LAST-SUBTERMS (CDR TERM)))
(RP-SYNTAXP-LST (CDR TERM)))
(RP-SYNTAXP (REMOVE-RETURN-LAST TERM)))
:hints (("Goal"
:cases ((is-return-last term))
:in-theory (e/d () ())))))

#|(defthm-rp-syntaxp
(defthm rp-syntaxp-remove-return-last
(implies (rp-syntaxp term)
(rp-syntaxp (remove-return-last term)))
:flag rp-syntaxp)
(defthm rp-syntaxp-remove-return-last-subterms
(implies
(rp-syntaxp-lst lst)
(rp-syntaxp-lst (remove-return-last-subterms lst)))
:flag rp-syntaxp-lst)
:hints (("Goal"
:in-theory (e/d (is-rp) ()))))||#)

(defthmd context-syntaxp-implies
(implies (context-syntaxp context)
(AND (RP-TERM-LISTP CONTEXT)
(RP-SYNTAXP-LST CONTEXT)
(ALL-FALIST-CONSISTENT-LST CONTEXT)))
:hints (("Goal"
:in-theory (e/d (context-syntaxp) ()))))

(defthm rp-syntaxp-assoc-equal
(IMPLIES
(AND
(RP-SYNTAXP-LST (STRIP-CDRS BINDINGS))
(CONSP (ASSOC-EQUAL TERM BINDINGS)))
(and
(RP-SYNTAXP (CDR (ASSOC-EQUAL TERM BINDINGS)))
(RP-SYNTAXP (CDR (hons-ASSOC-EQUAL TERM BINDINGS))))))

(defthm rp-syntaxp-lst-append
(equal (rp-syntaxp-lst (append x y))
(and (rp-syntaxp-lst x)
(rp-syntaxp-lst y)))
:hints (("Goal"
:in-theory (e/d (rp-syntaxp-lst
append)
(rp-syntaxp)))))||#

(defthm is-if-implies
  (implies (is-if term)
           (CASE-MATCH TERM (('IF & & &) T)
             (& NIL)))
  :hints (("Goal"
           :in-theory (e/d (is-if) ())))
  :rule-classes :forward-chaining)

(defthmd is-rp-implies-fc
  (implies (is-rp term)
           (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)))
  :rule-classes :forward-chaining
  :hints (("Goal"
           :in-theory (e/d (is-rp) ()))))

(defthm is-rp-of-rp
  (implies (AND (SYMBOLP TYPE)
                (NOT (BOOLEANP TYPE))
                (NOT (EQUAL TYPE 'QUOTE))
                (NOT (EQUAL TYPE 'RP))
                (NOT (EQUAL TYPE 'LIST))
                (NOT (EQUAL TYPE 'FALIST)))
           (is-rp `(rp ',type ,x)))
  :hints (("Goal"
           :in-theory (e/d (is-rp) ()))))

(defthmd rule-syntaxp-implies
  (implies (and (rule-syntaxp rule :warning warning)
                (not (rp-rule-metap rule)))
           (and
            (weak-custom-rewrite-rule-p rule)
            (rp-term-listp (rp-hyp rule))
            (rp-termp (rp-lhs rule))
            (rp-termp (rp-rhs rule))
            ;; (rp-syntaxp (rp-lhs rule))
            (not (include-fnc (rp-lhs rule) 'rp))
            (not (include-fnc-subterms (rp-hyp rule) 'rp))
            ;;(rp-syntaxp (rp-rhs rule))
            (not (include-fnc (rp-rhs rule) 'falist))
            (not (include-fnc-subterms (rp-hyp rule) 'falist))
            ;;(not (include-fnc (rp-lhs rule) 'if))
            (not (include-fnc (rp-lhs rule) 'synp))
            #|(no-free-variablep rule)|#
            (not (include-fnc (rp-lhs rule) 'list))
            (not (include-fnc-subterms (rp-hyp rule) 'list))
            (not (include-fnc (rp-rhs rule) 'list))

            (not (include-fnc (rp-lhs rule) 'equals))
            (not (include-fnc-subterms (rp-hyp rule) 'equals))
            (not (include-fnc (rp-rhs rule) 'equals))))
  :rule-classes (:rewrite :forward-chaining)
  :hints (("Goal" :in-theory (enable rule-syntaxp))))

(defthmd rule-syntaxp-implies-2
  (implies (and (rule-syntaxp rule :warning warning)
                (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)))
  :rule-classes (:rewrite :forward-chaining)
  :hints (("Goal" :in-theory (enable rule-syntaxp))))

(defthm ex-from-rp-loose-of-ex-from-rp-loose
  (equal (ex-from-rp-loose (ex-from-rp-loose term))
         (ex-from-rp-loose term))
  :hints (("Goal"
           :in-theory (e/d (ex-from-rp-loose
                            is-rp-loose) ()))))

;; (defthm all-falist-consistent-of-ex-from-rp-loose
;;   (implies (all-falist-consistent term)
;;            (all-falist-consistent (ex-from-rp-loose term)))
;;   :hints (("Goal"
;;            :in-theory (e/d (ex-from-rp-loose
;;                             is-rp-loose) ()))))

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

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

;; (defthm all-falist-consistent-of-ex-from-rp
;;   (implies (all-falist-consistent term)
;;            (all-falist-consistent (ex-from-rp  term)))
;;   :hints (("Goal"
;;            :in-theory (e/d (ex-from-rp
;;                             is-rp ) ()))))

(defthm
  rp-termp-ex-from-rp
  (implies (rp-termp term)
           (and (rp-termp (ex-from-rp term))
                (rp-termp (ex-from-rp-loose term))))
  :hints
  (("goal" :in-theory
    (e/d (ex-from-rp-loose ex-from-rp is-rp-loose is-rp)
         nil))))

;; (defthm all-falist-consistent-ex-from-rp-loose
;;   (implies (all-falist-consistent term)
;;            (all-falist-consistent (ex-from-rp-loose term)))
;;   :hints (("Goal"
;;            :in-theory (e/d (all-falist-consistent
;;                             is-falist
;;                             ex-from-rp-loose
;;                             is-rp-loose) ()))))

(defthm rp-termp-cadr
  (implies (and (rp-termp term)
                (consp term)
                (not (quotep term))
                (consp (cdr term)))
           (rp-termp (cadr term)))
  :hints (("Goal"
           :in-theory (e/d (rp-termp
                            is-rp
                            ex-from-rp-loose
                            is-rp-loose) ()))))

#|(defthm rp-syntaxp-cadr
(implies (and (rp-syntaxp term)
(consp term)
(not (quotep term))
(not (equal (car term) 'rp))
(consp (cdr term)))
(rp-syntaxp (cadr term)))
:hints (("Goal"
:in-theory (e/d (rp-termp
ex-from-rp-loose
is-rp-loose) ()))))||#

#|(defthm all-falist-consistent-cadr
(implies (and (all-falist-consistent term)
(consp term)
(not (quotep term))
(consp (cdr term)))
(all-falist-consistent (cadr term)))
:hints (("Goal"
:in-theory (e/d (all-falist-consistent
ex-from-rp-loose
is-rp-loose) ()))))||#

#|(defthm all-falist-consistent-caddr
(implies (and (all-falist-consistent term)
(consp term)
(not (quotep term))
(consp (cdr term))
(consp (cddr term)))
(all-falist-consistent (caddr term)))
:hints (("Goal"
:in-theory (e/d (all-falist-consistent
ex-from-rp-loose
is-rp-loose) ()))))||#

#|(defthm all-falist-consistent-cadddr
(implies (and (all-falist-consistent term)
(consp term)
(not (quotep term))
(consp (cdr term))
(consp (cddr term))
(consp (cdddr term)))
(all-falist-consistent (cadddr term)))
:hints (("Goal"
:in-theory (e/d (all-falist-consistent
ex-from-rp-loose
is-rp-loose) ()))))||#

(defthm rp-termp-caddr
  (implies (and (rp-termp term)
                (consp term)
                (not (quotep term))
                (consp (cdr term))
                (consp (cddr term)))
           (rp-termp (caddr term)))
  :hints (("Goal"
           :in-theory (e/d (rp-termp
                            is-rp
                            ex-from-rp-loose
                            is-rp-loose) ()))))

(defthm rp-termp-cadddr
  (implies (and (rp-termp term)
                (consp term)
                (not (quotep term))
                (consp (cdr term))
                (consp (cddr term))
                (consp (cdddr term)))
           (rp-termp (cadddr term)))
  :hints (("Goal"
           :in-theory (e/d (rp-termp
                            is-rp
                            ex-from-rp-loose
                            is-rp-loose) ()))))

(defthm rp-termp-caddddr
  (implies (and (rp-termp term)
                (consp term)
                (not (quotep term))
                (consp (cdr term))
                (consp (cddr term))
                (consp (cdddr term))
                (consp (cddddr term)))
           (rp-termp (car (cddddr term))))
  :hints (("Goal"
           :in-theory (e/d (rp-termp
                            is-rp
                            ex-from-rp-loose
                            is-rp-loose) ()))))

#|(defthm rp-syntaxp-caddr
(implies (and (rp-syntaxp term)
(consp term)
(not (quotep term))
                ;(not (equal (car term) 'rp)) ;
(consp (cdr term))
(consp (cddr term)))
(rp-syntaxp (caddr term)))
:hints (("Goal"
:in-theory (e/d (rp-termp
ex-from-rp-loose
is-rp-loose) ()))))||#

#|(defthm rp-syntaxp-cadddr
(implies (and (rp-syntaxp term)
(consp term)
(not (quotep term))
(consp (cdr term))
(consp (cddr term))
(consp (cdddr term)))
(rp-syntaxp (cadddr term)))
:hints (("Goal"
:expand ((rp-syntaxp term))
:in-theory (e/d (rp-termp
ex-from-rp-loose
rp-syntaxp
is-rp
is-rp-loose) ()))))||#

(defthmd ex-from-rp-loose-is-ex-from-rp
  (implies (rp-termp term)
           (equal (ex-from-rp-loose term)
                  (ex-from-rp term)))
  :hints (("Goal"
           :in-theory (e/d (is-rp-loose
                            is-rp
                            ex-from-rp
                            ex-from-rp-loose) ()))))

(defthm car-of-ex-from-rp-is-not-rp
  (implies (rp-termp term)
           (not (equal (car (ex-from-rp term))
                       'rp)))
  :hints (("Goal"
           :in-theory (e/d (ex-from-rp
                            is-rp) ()))))

(defthm dont-rw-syntaxp-dont-rw-syntax-fix
  (dont-rw-syntaxp (dont-rw-syntax-fix dont-rw))
  :hints (("Goal"
           :in-theory (e/d (dont-rw-syntax-fix)
                           (DONT-RW-SYNTAXP)))))

(defthm rp-termp-implies-dont-rw-syntaxp
  (implies (rp-termp term)
           (dont-rw-syntaxp term)))

(defthm rp-termp-ex-from-falist
  (implies (rp-termp x)
           (rp-termp (ex-from-falist x)))
  :rule-classes (:type-prescription :rewrite)
  :hints (("Goal"
           :expand (ex-from-falist x)
           :in-theory (e/d () ()))))

(defthm is-falist-strict-to-is-falist
  (implies (rp-termp term)
           (equal (is-falist-strict term)
                  (is-falist term))))

(defthm rp-termp-trans*-list
  (implies (and (rp-term-listp lst)
                (consp lst))
           (rp-termp (trans-list lst))))

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

(defthm is-rp-rp-trans-lst
  (IMPLIES (AND (RP-TERMP TERM)
                (is-rp term))
           (IS-RP (CONS 'RP (RP-TRANS-LST (CDR TERM)))))
  :hints (("Goal"
           :in-theory (e/d (is-rp) ()))))

(defthm-rp-trans
  (defthm rp-termp-of-rp-trans
    (implies (rp-termp term)
             (rp-termp (rp-trans term)))
    :flag rp-trans)
  (defthm rp-term-listp-of-rp-trans-lst
    (implies (rp-term-listp lst)
             (rp-term-listp (rp-trans-lst lst)))
    :flag rp-trans-lst)
  :otf-flg t
  :hints (("Goal"
           :expand ((:free (x y)
                           (rp-termp (cons x y)))
                    (RP-TERMP TERM))
           :in-theory (e/d ()
                           (FALIST-CONSISTENT
                            RP-TERMP-CONS-CAR-TERM-SUBTERMS)))))

(defthm-rp-trans
  (defthm rp-trans-not-include-fnc-list
    (not (include-fnc (rp-trans term) 'list))
    :flag rp-trans)
  (defthm rp-trans-lst-not-include-fnc-subterms-list
    (not (include-fnc-subterms (rp-trans-lst lst) 'list))
    :flag rp-trans-lst)
  :otf-flg t
  :hints (("Goal"
           :in-theory (e/d ()
                           ()))))

(defthm rp-trans-of-trans-list
  (equal (rp-trans (trans-list lst))
         (trans-list (rp-trans-lst lst))))

(defthm-rp-trans
  (defthm rp-trans-of-rp-trans
    (equal (rp-trans (rp-trans term))
           (rp-trans term))
    :flag rp-trans)
  (defthm rp-trans-lst-of-rp-trans-lst
    (equal (rp-trans-lst (rp-trans-lst lst))
           (rp-trans-lst lst))
    :flag rp-trans-lst)
  :otf-flg t
  :hints (("goal"
           :expand ((is-falist (list (car term)))
                    (rp-trans-lst (cdr term))
                    (rp-trans-lst (cddr term))
                    (rp-trans-lst (cdddr term))
                    (rp-trans (cons (car term)
                                    (rp-trans-lst (cdr term)))))
           :in-theory (e/d ()
                           ()))))

#|(defthm-rp-trans
(defthm rp-trans-is-term-when-list-is-absent
(implies (not (include-fnc term 'list))
(equal (rp-trans term) term))
:flag rp-trans)
(defthm rp-trans-lst-is-lst-when-list-is-absent
(implies (not (include-fnc-subterms lst 'list))
(equal (rp-trans-lst lst) lst))
:flag rp-trans-lst))||#

(defthm rp-state-new-run-returns-rp-statep
  (implies (rp-statep rp-state)
           (rp-statep (rp-state-new-run rp-state)))
  :hints (("Goal"
           :in-theory (e/d (rp-state-new-run
                            rp-statep)
                           ()))))

(defthm RP-TERM-LISTP-of-append
  (implies (and (rp-term-listp lst1)
                (rp-term-listp lst2))
           (rp-term-listp (append lst1 lst2))))

(defthm rp-state-preservedp-of-the-same
  (implies (rp-statep rp-state)
           (rp-state-preservedp rp-state rp-state))
  :hints (("Goal"
           :in-theory (e/d (rp-state-preservedp) ()))))

(defthm rp-state-preservedp-implies-rp-statep
  (implies (and (rp-statep rp-state)
                (rp-state-preservedp rp-state new-rp-state))
           (rp-statep new-rp-state))
  :hints (("Goal"
           :in-theory (e/d (rp-state-preservedp) ()))))

(defthm rp-state-preservedp-implies-valid-rp-state-syntaxp
  (implies (and (valid-rp-state-syntaxp rp-state)
                (rp-state-preservedp rp-state new-rp-state))
           (valid-rp-state-syntaxp new-rp-state))
  :hints (("Goal"
           :do-not-induct t
           :use ((:instance RP-STATE-PRESERVEDP-SK-necc
                            (key (VALID-RP-STATE-SYNTAXP-AUX-WITNESS
                                  NEW-RP-STATE))
                            (OLD-RP-STATE rp-state))
                 (:instance VALID-RP-STATE-SYNTAXP-AUX-necc
                            (key (VALID-RP-STATE-SYNTAXP-AUX-WITNESS NEW-RP-STATE))))
           :expand (VALID-RP-STATE-SYNTAXP-AUX NEW-RP-STATE)
           :in-theory (e/d (rp-state-preservedp
                            valid-rp-state-syntaxp)
                           (RP-STATE-PRESERVEDP-SK
                            VALID-RP-STATE-SYNTAXP-AUX
                            RP-STATEP)))))


#|(local
(defthmd rp-termp-ex-from-rp-all2-lemma
(implies (syntaxp (not (include-fnc term 'ex-from-rp)))
(equal (rp-termp term)
(and (hide (rp-termp term))
(rp-termp (ex-from-rp term)))))
:hints
(("goal"
:expand (hide (rp-termp term))
:in-theory
(e/d (ex-from-rp-loose ex-from-rp is-rp-loose is-rp)
nil)))))|#

(defthm-ex-from-rp-all2
  (defthm rp-termp-ex-from-rp-all2
    (implies (rp-termp term)
             (rp-termp (ex-from-rp-all2 term)))
    :flag ex-from-rp-all2)
  (defthm rp-term-listp-ex-from-rp-all2-lst
    (implies (rp-term-listp lst)
             (rp-term-listp (ex-from-rp-all2-lst lst)))
    :flag ex-from-rp-all2-lst)
  :hints (("Goal"
           :do-not-induct t
           :expand (RP-TERMP (EX-FROM-RP TERM))
           :in-theory (e/d (
                            EX-FROM-RP-ALL2
                            IS-RP-LOOSE
                            is-rp
                            ;;rp-termp-ex-from-rp-all2-lemma
                            ex-from-rp-all2-lst
                            )
                           (ex-from-rp
                            ;;FALIST-CONSISTENT
                            RP-TERMP-EX-FROM-RP
                            RP-TERMP-CONS-CAR-TERM-SUBTERMS
                            )))))

(defthm-ex-from-rp-all2
  (defthm ex-from-rp-all2-not-include-rp
    (implies (rp-termp term)
             (not (include-fnc (ex-from-rp-all2 term) 'rp)))
    :flag ex-from-rp-all2)
  (defthm ex-from-rp-all2-lst-not-include-rp
    (implies (rp-term-listp lst)
             (not (include-fnc-subterms (ex-from-rp-all2-lst lst) 'rp)))
    :flag ex-from-rp-all2-lst)
  :hints (("Goal"
           :do-not-induct t
           :expand (RP-TERMP (EX-FROM-RP TERM))
           :in-theory (e/d (
                            EX-FROM-RP-ALL2
                            IS-RP-LOOSE
                            is-rp
                            ;;rp-termp-ex-from-rp-all2-lemma
                            ex-from-rp-all2-lst
                            )
                           (ex-from-rp
                            ;;FALIST-CONSISTENT
                            RP-TERMP-EX-FROM-RP
                            RP-TERMP-CONS-CAR-TERM-SUBTERMS
                            )))))

(defthmd equal-of-len-with-constant
  (implies (and (syntaxp (quotep x))
                (posp x))
           (equal (equal (len lst) x)
                  (and (consp lst)
                       (equal (len (cdr lst)) (1- x)))))
  :hints (("Goal"
           :in-theory (e/d (len) ()))))

(defthm equal-of-len-with-0
  (equal (equal (len lst) 0)
         (atom lst))
  :hints (("Goal"
           :in-theory (e/d (len) ()))))

(defthm len-of-ex-from-rp-all2-lst
  (equal (len (ex-from-rp-all2-lst lst))
         (len lst))
  :hints (("Goal"
           :induct (len lst)
           :in-theory (e/d (ex-from-rp-all2-lst len) ()))))

(defthm-ex-from-rp-all2
  (defthm ex-from-rp-all2-not-include-equals
    (implies (rp-termp term)
             (not (include-fnc (ex-from-rp-all2 term) 'equals 2)))
    :flag ex-from-rp-all2)
  (defthm ex-from-rp-all2-lst-not-include-equals
    (implies (rp-term-listp lst)
             (not (include-fnc-subterms (ex-from-rp-all2-lst lst) 'equals 2)))
    :flag ex-from-rp-all2-lst)
  :hints (("Goal"
           :do-not-induct t
           :expand (RP-TERMP (EX-FROM-RP TERM))
           :in-theory (e/d (equal-of-len-with-constant
                            is-equals
                            EX-FROM-RP-ALL2
                            IS-RP-LOOSE
                            is-rp
                            ;;rp-termp-ex-from-rp-all2-lemma
                            ex-from-rp-all2-lst
                            )
                           (ex-from-rp
                            ;;FALIST-CONSISTENT
                            RP-TERMP-EX-FROM-RP
                            RP-TERMP-CONS-CAR-TERM-SUBTERMS
                            )))))

(defthmd rp-termp-single-step
  (implies (is-rp term)
           (and (equal (rp-termp (list (car term)
                                       (cadr term)
                                       rest))
                       (rp-termp rest))
                (equal (rp-termp term)
                       (rp-termp (caddr term)))))
  :hints (("Goal"
           :in-theory (e/d (is-rp) ()))))

(defthm rp-termp-single-step-2
  (implies (is-rp term)
           (equal (rp-termp (list (car term)
                                  (cadr term)
                                  rest))
                  (rp-termp rest)))
  :hints (("Goal"
           :in-theory (e/d (is-rp) ()))))

(defthm rp-termp-single-step-3
  (implies (and (is-rp term)
                (rp-termp term))
           (rp-termp (caddr term)))
  :hints (("Goal"
           :in-theory (e/d (is-rp
                            rp-termp-single-step) ()))))

(defthm rp-term-listp-of-rev
  (implies (rp-term-listp x)
           (rp-term-listp (rev x)))
  :hints (("Goal"
           :in-theory (e/d (rev) ()))))

(progn
  (define binary-or**/and**-guard-meta-aux (term)
    (cond
     ((atom term)
      term)
     ((atom (cdr term))
      term)
     ((atom (cddr term))
      (case-match term
        (('hide x)
         (binary-or**/and**-guard-meta-aux x))
        (& term)))
     ((atom (cdddr term))
      (case-match term
        (('acl2::binary-or* x y)
         `(binary-or** ,(binary-or**/and**-guard-meta-aux x)
                       ,(binary-or**/and**-guard-meta-aux y)))
        (('acl2::binary-and* x y)
         `(binary-and** ,(binary-or**/and**-guard-meta-aux x)
                        ,(binary-or**/and**-guard-meta-aux y)))
        (('binary-or** x y)
         `(binary-or** ,(binary-or**/and**-guard-meta-aux x)
                       ,(binary-or**/and**-guard-meta-aux y)))
        (('binary-and** x y)
         `(binary-and** ,(binary-or**/and**-guard-meta-aux x)
                        ,(binary-or**/and**-guard-meta-aux y)))
        (('equal x y)
         (b* ((x (binary-or**/and**-guard-meta-aux x))
              (y (binary-or**/and**-guard-meta-aux y)))
           (if (equal x y)
               ''t
             `(equal ,x ,y))))
        (& term)))
     ((atom (cddddr term))
      (case-match term
        (('RETURN-LAST & & x)
         (binary-or**/and**-guard-meta-aux x))

        (('if x x y)
         `(binary-or** ,(binary-or**/and**-guard-meta-aux x)
                       ,(binary-or**/and**-guard-meta-aux y)))
        (('if x y ''nil)
         `(binary-and** ,(binary-or**/and**-guard-meta-aux x)
                        ,(binary-or**/and**-guard-meta-aux y)))
        (& term)))
     (t term)))

  (define binary-or**/and**-guard-meta (term)
    (case-match term
      (('equal & ('hide &))
       (binary-or**/and**-guard-meta-aux
        (beta-search-reduce term *big-number*)))
      (('equal ('hide &) &)
       (binary-or**/and**-guard-meta-aux
        (beta-search-reduce term *big-number*)))
      (& term)))

  (defevaluator binary-or**/and**-guard-meta-eval
    binary-or**/and**-guard-meta-eval-lst
    ((acl2::binary-or* x y)
     (binary-or** x y)
     (acl2::binary-and* x y)
     (binary-and** x y)
     (hide x)
     (equal x y)
     (if x y z)
     (RETURN-LAST x y z))
    :namedp t)

  (defthm binary-or**/and**-guard-meta-eval-beta-search-reduce
    (implies
     (pseudo-termp term)
     (equal (binary-or**/and**-guard-meta-eval (beta-search-reduce term limit) a)
            (binary-or**/and**-guard-meta-eval term a)))
    :hints (("Goal"
             :use ((:functional-instance
                    eval-of-beta-search-reduce
                    (acl2::beta-eval binary-or**/and**-guard-meta-eval)
                    (acl2::beta-eval-list binary-or**/and**-guard-meta-eval-lst)))
             :in-theory (e/d (binary-or**/and**-guard-meta-eval-of-fncall-args) ()))))

  (defthm binary-or**/and**-guard-meta-aux-correct
    (implies t
             (equal (binary-or**/and**-guard-meta-eval
                     (binary-or**/and**-guard-meta-aux term) a)
                    (binary-or**/and**-guard-meta-eval
                     term a)))
    :hints (("Goal"
             :do-not-induct t
             :expand ((:free (x) (hide x)))
             :induct (binary-or**/and**-guard-meta-aux term)
             :in-theory (e/d (binary-or**/and**-guard-meta-aux
                              BINARY-AND**
                              acl2::BINARY-AND*
                              BINARY-or**
                              acl2::BINARY-or*)
                             ()))))

  (defthm binary-or**/and**-guard-meta-correct
    (implies (pseudo-termp term)
             (equal (binary-or**/and**-guard-meta-eval term a)
                    (binary-or**/and**-guard-meta-eval
                     (binary-or**/and**-guard-meta term) a)))
    :rule-classes ((:meta :trigger-fns (equal)))
    :hints (("Goal"
             :do-not-induct t
             :in-theory (e/d (binary-or**/and**-guard-meta)
                             ())))))

#|(defthm binary-and**-of-nil/t
(and (equal (binary-and** nil a)
nil)
(equal (binary-and** t a)
a))
:hints (("Goal"
:in-theory (e/d (binary-and**) ()))))|#


(defthm-include-fnc
  (defthmd include-fnc-without-size-implies-with-size
    (implies (not (include-fnc term fnc nil))
             (not (include-fnc term fnc arg-size)))
    :flag include-fnc-fn)
  (defthmd include-fnc-subterms-without-size-implies-with-size
    (implies (not (include-fnc-subterms subterms fnc nil))
             (not (include-fnc-subterms subterms fnc arg-size)))
    :flag include-fnc-subterms-fn))