File: rp-equal-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 (1294 lines) | stat: -rw-r--r-- 44,571 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
; 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.

; 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 "../rp-rewriter")
(include-book "local-lemmas")
(include-book "aux-function-lemmas")

(include-book "proof-function-lemmas")

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

  (local
   (defthm extract-from-rp-acl2-count
     (implies (consp term)
              (< (acl2-count (cdr (ex-from-rp term)))
                 (acl2-count 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 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) ())))))

  (make-flag rp-equal-cnt :defthm-macro-name defthm-rp-equal-cnt)
  (make-flag rp-equal :defthm-macro-name defthm-rp-equal)
  (make-flag rp-equal-loose :defthm-macro-name defthm-rp-equal-loose))

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

(local
 (in-theory (disable is-synp  extract-from-synp
                     should-term-be-in-cons put-term-in-cons)))

(local
 (defthm consp-extract-from-rp
   (implies (consp (ex-from-rp term))
            (consp term))
   :hints (("Goal" :in-theory (enable ex-from-rp)))))

(local
 (defthm consp-extract-from-synp
   (implies (consp (ex-from-synp term))
            (consp term))
   :hints (("Goal" :in-theory (enable is-synp extract-from-synp)))))

(defthm-rp-equal
  (defthm rp-equal-reflexive
    (implies (equal term1 term2)
             (rp-equal term1 term2))
    :flag rp-equal)

  (defthm rp-equal-subterms-reflexive
    (implies (equal subterm1 subterm2)
             (rp-equal-subterms subterm1 subterm2))
    :flag rp-equal-subterms))

(defthm evl-of-extract-from-rp
  (equal (rp-evl (ex-from-rp term) a)
         (rp-evl term a))
  :hints (("Goal"
           :in-theory (enable is-rp ex-from-rp))))

(defthm evl-of-extract-from-rp-loose
  (equal (rp-evl (ex-from-rp-loose term) a)
         (rp-evl term a))
  :hints (("Goal"
           :in-theory (enable is-rp-loose ex-from-rp-loose))))

(defthmd evl-of-extract-from-rp-loose-reverse
  (implies (syntaxp (atom term))
           (equal (rp-evl term a)
                  (rp-evl (ex-from-rp-loose term) a)))
  :hints (("Goal"
           :use ((:instance evl-of-extract-from-rp-loose))
           :in-theory (disable evl-of-extract-from-rp-loose ))))

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

(local
 (defthm extract-from-rp-equal-to-evl
   (implies (equal (ex-from-rp term1)
                   (ex-from-rp term2))
            (equal (equal (rp-evl term1 a)
                          (rp-evl term2 a))
                   t))
   :hints (("goal"
            :induct (ex-from-rp term2)
            :in-theory (enable is-rp ex-from-rp)))))

(local
 (defthm extract-from-rp-equal-to-evl-loose
   (implies (equal (ex-from-rp-loose term1)
                   (ex-from-rp-loose term2))
            (equal (equal (rp-evl term1 a)
                          (rp-evl term2 a))
                   t))
   :hints (("goal"
            :induct (ex-from-rp-loose term2)
            :in-theory (enable is-rp-loose ex-from-rp-loose)))))

(local
 (defthm extract-from-rp-equal-to-evl-2
   (implies (EQUAL (rp-evl (EX-FROM-RP TERM1) a)
                   (rp-evl (EX-FROM-RP TERM2) a))
            (equal (EQUAL (RP-EVL TERM1 A)
                          (RP-EVL TERM2 A))
                   t))
   :hints (("Goal"
            :in-theory (e/d (is-rp ex-from-rp)
                            (NOT-INCLUDE-RP))))))

(local
 (defthm extract-from-rp-equal-to-evl-2-loose
   (implies (EQUAL (rp-evl (ex-from-rp-loose TERM1) a)
                   (rp-evl (ex-from-rp-loose TERM2) a))
            (equal (EQUAL (RP-EVL TERM1 A)
                          (RP-EVL TERM2 A))
                   t))
   :hints (("Goal"
            :in-theory (e/d (is-rp-loose ex-from-rp-loose)
                            (NOT-INCLUDE-RP))))))

(local
 (defthm extract-from-synp-equal-to-evl-1
   (implies (EQUAL (EXTRACT-FROM-SYNP TERM1)
                   (EXTRACT-FROM-SYNP TERM2))
            (equal (EQUAL (RP-EVL TERM1 A)
                          (RP-EVL TERM2 A))
                   t))
   :hints (("Goal"
            :in-theory (enable is-synp extract-from-synp)))))

(local
 (defthm evl-of-is-synp
   (IMPLIES  (IS-SYNP TERM1)
             (EQUAL (RP-EVL TERM1 A) T))
   :hints (("Goal" :in-theory (enable is-synp)))))

(local
 (defthm evl-of-is-synp-and-is-rp
   (IMPLIES  (and (or (is-rp term1)
                      (is-rp-loose term1))
                  (IS-SYNP (caddr term1)))
             (EQUAL (RP-EVL TERM1 A) T))
   :hints (("Goal" :in-theory (enable is-synp
                                      is-rp
                                      IS-RP-LOOSE)))))

(local
 (defthm extract-from-synp-and-rp-equal-to-evl-1-lemma1
   (IMPLIES (AND (or (IS-RP TERM1)
                     (is-rp-loose TERM1))
                 (IS-SYNP (CADDR TERM1))
                 (IS-RP TERM2)
                 (EQUAL ''T (CADDR TERM2)))
            (equal (EQUAL T (RP-EVL TERM2 A))
                   t))
   :hints (("Goal" :in-theory (enable is-rp
                                      is-rp-loose
                                      is-synp)))))

(local
 (defthm extract-from-synp-and-rp-equal-to-evl-1
   (implies (EQUAL (EX-FROM-SYNP (EX-FROM-RP TERM1))
                   (EX-FROM-SYNP (EX-FROM-RP TERM2)))
            (equal (EQUAL (RP-EVL TERM1 A)
                          (RP-EVL TERM2 A))
                   t))
   :hints (("Goal"
            :cases (;(is-synp (EX-FROM-RP TERM1))
;(is-synp (EX-FROM-RP TERM2))
                    (is-rp term1)
                    (is-rp term2)
                    )
            :in-theory (disable is-rp is-synp)))))

(local
 (defthm extract-from-synp-equal-to-evl-2
   (implies (EQUAL (rp-evl (ex-from-synp TERM1) a)
                   (rp-evl (ex-from-synp TERM2) a))
            (equal (EQUAL (RP-EVL TERM1 A)
                          (RP-EVL TERM2 A))
                   t))
   :hints (("Goal"
            :in-theory (enable extract-from-synp)))))

(defthm ex-from-rp-lemma1
  (implies (or (is-rp term)
               (is-rp-loose term))
           (equal (rp-evl (caddr term) a)
                  (rp-evl term a)))
  :hints (("Goal" :in-theory (enable is-rp
                                     is-rp-loose))))

(defthm evl-of-extract-from-rp-2
  (implies (and (or (is-rp term)
                    (is-rp-loose term))
                (rp-evl (caddr term) a))
           (rp-evl term a))
  :hints (("Goal"
           :use (ex-from-rp-lemma1)
           :in-theory '())))

(defthm ex-from-synp-lemma1
  (implies (is-synp term)
           (equal (rp-evl term a)
                  t))
  :hints (("Goal" :in-theory (enable is-synp))))

(defthm is-rp-pseudo-termp
  (implies (and (rp-termp term)
                (or (is-rp term)
                    (is-rp-loose term)))
           (rp-termp (caddr term)))
  :hints (("Goal" :in-theory (enable is-rp
                                     is-rp-loose))))

(defthm rp-termp-should-term-be-in-cons-lhs
  (implies (and (should-term-be-in-cons lhs term)
                (rp-termp lhs))
           (and (rp-termp (cadr lhs))
                (rp-termp (car lhs))
                (rp-termp (caddr lhs))))
  :hints (("Goal" :in-theory (enable should-term-be-in-cons))))

(defthm rp-termp-should-term-be-in-cons-term
  (implies (and (rp-termp term))
           (and (rp-termp (cadr (put-term-in-cons term)))
                (rp-termp (car (put-term-in-cons term)))
                (rp-termp (caddr (put-term-in-cons term)))))
  :hints (("Goal" :in-theory (enable put-term-in-cons))))

(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) ()))))

(encapsulate
  nil
  (local
   (defthm pseudo-termp-and-rp-evl-list
     (implies (and (rp-termp term1)
                   (rp-termp term2)
                   (consp term1)
                   (consp term2)
                   (not (quotep term1))
                   (equal (car term1) (car term2)) ;;same function symbols
                   (equal (rp-evl-lst (cdr term1) a) ;;params evaluate to the same
                          (rp-evl-lst (cdr term2) a)))
              (equal (equal (rp-evl term1 a)
                            (rp-evl term2 a))
                     t))
     :hints (("Goal" :in-theory (enable rp-evl-of-fncall-args)))))

  (local
   (defthm lemma1
     (implies (and (equal (rp-evl-lst (cdr term1) a)
                          (rp-evl-lst (cdr term2) a))
                   (equal (car term1) (car term2))
                   (consp term1)
                   (not (equal (car term1) 'quote))
                   (not (equal (car term2) 'quote))
                   (syntaxp (lexorder term1 term2))
                   (consp term2))
              (equal (rp-evl term1 a)
                     (rp-evl term2 a)))
     :hints (("goal" :in-theory (enable rp-evl-of-fncall-args)))))

  (local
   (defthmd lemma2
     (implies (quotep term)
              (equal (unquote term)
                     (rp-evl term a)))))
  #|(local
  (defthm lemma3
  (implies (and (not (equal (caddr term2) term2))
  (not (is-rp-loose (caddr term2)))
  (is-rp-loose term2)
  (consp (caddr term2))
  (equal (car (caddr term2)) 'quote)
  (consp (cdr (caddr term2)))
  (not (cddr (caddr term2)))
  (rp-termp term2))
  (equal (cadr (caddr term2))
  (rp-evl term2 a)))
  :instructions (:promote (:dive 1)
  (:rewrite lemma2)
  :top
  :bash :s)))||#

  (local
   (in-theory (disable rp-evl-of-variable)))

  (defthm-rp-equal
    (defthm rp-evl-of-rp-equal
      (implies (and (rp-equal term1 term2))
               (equal (equal (rp-evl term1 a)
                             (rp-evl term2 a))
                      t))
      :flag rp-equal)

    (defthm rp-evl-of-rp-equal-subterms
      (implies (and (rp-equal-subterms subterm1 subterm2))
               (equal (equal (rp-evl-lst subterm1 a)
                             (rp-evl-lst subterm2 a))
                      t))
      :flag rp-equal-subterms))

  (defthm-rp-equal-loose
    (defthm rp-evl-of-rp-equal-loose
      (implies (and (rp-termp term1)
                    (rp-termp term2)
                    (rp-equal-loose term1 term2))
               (equal (equal (rp-evl term1 a)
                             (rp-evl term2 a))
                      t))
      :flag rp-equal-loose)

    (defthm rp-evl-of-rp-equal-loosesubterms
      (implies (and (rp-equal-loose-subterms subterm1 subterm2)
                    (rp-term-listp subterm1)
                    (rp-term-listp subterm2))
               (equal (equal (rp-evl-lst subterm1 a)
                             (rp-evl-lst subterm2 a))
                      t))
      :flag rp-equal-loose-subterms)))


(encapsulate
  nil


  ;; (local
  ;;  (defthm lemma1
  ;;    (IMPLIES (AND (CONSP (EX-FROM-RP TERM1))
  ;;                  ;(CONSP (EX-FROM-RP TERM2))
  ;;                  )
  ;;             (O< 1
  ;;                 (CONS-COUNT TERM1)))
  ;;    :hints (("Goal"
  ;;             :in-theory (e/d (cons-count
  ;;                              ex-from-rp) ())))))

  ;; (local
  ;;  (defthm lemma2
  ;;    (IMPLIES (AND (consp term2)
  ;;                  (<=  (cons-count term2)
  ;;                   (cons-count term1)))
  ;;        (O< (CONS-COUNT (CADDR term2))
  ;;            (CONS-COUNT TERM1)))
  ;;    :hints (("Goal"
  ;;             :in-theory (e/d (cons-count) ())))))

  #|(local
  (mutual-recursion
  ;; check if two terms are equivalent by discarding rp terms
  (defun rp-equal-induct (term1 term2)
  (declare (xargs :measure (+ (cons-count term1)

;(cons-count term2) ;
  )
  :hints (("Goal"
  :in-theory (e/d (measure-lemmas)
  ((:DEFINITION INCLUDE-FNC)
  ex-from-rp

  (:LINEAR
  ACL2::APPLY$-BADGEP-PROPERTIES . 1)
  (:DEFINITION
  ACL2::APPLY$-BADGEP)
  ))))))
  "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))
  (mv (equal term1 term2) term1 term2))
  ((or (is-falist term1)
  (is-falist term2))
  (b* (((mv rest-equal t1 t2)
  (rp-equal-induct (caddr term1) (caddr term2))))
  (mv (and (equal (car term1) (car term2))
  (equal (cadr term1) (cadr term2))
  rest-equal)
  t1 t2)))
  ((or (and (equal (car term1) 'list)
  (consp (cdr term1)))
  (and (equal (car term2) 'list)
  (consp (cdr term2))))
  (b* (((mv rest-equal t1 t2)
  (rp-equal-induct-subterms (cdr term1) (cdr term2)))
  (t1 (trans-list t1))
  (t2 (trans-list t2)))
  (mv (and (and (equal (car term1) 'list)
  (consp (cdr term1)))
  (and (equal (car term2) 'list)
  (consp (cdr term2)))
  rest-equal)
  t1 t2)))
  (t (b* (((mv rest-equal t1 t2)
  (rp-equal-induct-subterms (cdr term1) (cdr term2))))
  (mv  (and (equal (car term1) (car term2))
  rest-equal)
  (cons (car term1) t1)
  (cons (car term2) t2)))))))

  (defun rp-equal-induct-subterms (subterm1 subterm2)
  (declare (xargs :mode :logic
  :measure (+ (cons-count subterm1)
                                  ;(cons-count subterm2) ;
  )))
  (if (or (atom subterm1)
  (atom subterm2))
  (equal subterm1 subterm2)
  (and (rp-equal-induct (car subterm1) (car subterm2))
  (rp-equal-induct-subterms (cdr subterm1) (cdr subterm2)))))))||#

  #|(mutual-recursion
  (defun rp-trans-induct (term)
  (let* ((term (ex-from-rp term)))
  (cond ((atom term) term)
  ((quotep term) term)
  ((and (equal (car term) 'list)
  (consp (cdr term)))
  (trans-list (rp-trans-induct-lst (cdr term))))
  ((and (is-falist term))
  (rp-trans-induct (caddr term)))
  (t (cons (car term)
  (rp-trans-lst (cdr term)))))))
  (defun rp-trans-induct-lst (lst)
  (if (atom lst)
  lst
  (cons (rp-trans-induct (car lst))
  (rp-trans-induct-lst (cdr lst))))))||#
  #|(make-flag rp-trans-induct :defthm-macro-name defthm-rp-trans-induct )||#

  #|(local
  (make-flag rp-equal-induct :defthm-macro-name defthm-rp-equal-induct
  :hints (("Goal"
  :in-theory (e/d (measure-lemmas)
  ((:definition include-fnc)
  ex-from-rp
  (:linear acl2::apply$-badgep-properties . 1)
  (:definition acl2::apply$-badgep)))))))||#

  (mutual-recursion
   (defun remove-rp (term)
     (let* ((term (ex-from-rp term)))
       (cond ((or (atom term)
                  (acl2::fquotep term))
              term)
             (t (cons (car term)
                      (remove-rp-lst (cdr term)))))))
   (defun remove-rp-lst (lst)
     (if (atom lst)
         lst
       (cons (remove-rp (car lst))
             (remove-rp-lst (cdr lst))))))

  (defthm-rp-equal
    (defthmd rp-equal-of-remove-rp
      (implies (and (rp-equal term1 term2))
               (equal (remove-rp term1)
                      (remove-rp term2)))
      :flag rp-equal)
    (defthmd rp-equal-subterms-of-remove-rp-lst
      (implies (and (rp-equal-subterms subterm1 subterm2))
               (equal (remove-rp-lst subterm1)
                      (remove-rp-lst subterm2)))
      :flag rp-equal-subterms))


  (local
   (mutual-recursion
    (defun rp-trans-induct (term)
      (let* ((term (ex-from-rp term)))
        (cond ((atom term) term)
              ((quotep term) term)
              ((and (equal (car term) 'list)
                    (consp (cdr term)))
               (trans-list (rp-trans-induct-lst (cdr term))))
              ((and (is-falist term))
               (rp-trans-induct (caddr term)))
              (t (cons (car term)
                       (rp-trans-induct-lst (cdr term)))))))
    (defun rp-trans-induct-lst (lst)
      (if (atom lst)
          lst
        (cons (rp-trans-induct (car lst))
              (rp-trans-induct-lst (cdr lst)))))))

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

  (local
   (defthm lemma10
     (equal (ex-from-rp (cons 'falist x))
            (cons 'falist x))
     :hints (("Goal"
              :in-theory (e/d (ex-from-rp
                               is-rp) ())))))

  (Local
   (defthmd insert-ex-from-rp-rp-trans
     (implies (syntaxp (or (atom term)))
              (equal (rp-evlt term a)
                     (rp-evlt (ex-from-rp term) a)))))


  (local
   (defthm-rp-trans-induct
     (defthmd rp-evlt-of-remove-rp
       (implies (syntaxp (or (atom term)
                             (not (equal (car term) 'remove-rp))))
                (equal (rp-evlt term a)
                       (rp-evlt (remove-rp term) a)))
       :flag rp-trans-induct)
     (defthmd rp-evlt-lst-of-remove-rp-lst
       (implies (syntaxp (or (atom lst)
                             (not (equal (car lst) 'remove-rp-lst))))
                (equal (rp-evlt-lst lst a)
                       (rp-evlt-lst (remove-rp-lst lst) a)))
       :flag rp-trans-induct-lst)
     :hints (("Goal"
              :do-not-induct t
              :in-theory (e/d (rp-evl-of-fncall-args
                               insert-ex-from-rp-rp-trans)
                              (ex-from-rp
                               trans-list

                               RP-EVLT-OF-EX-FROM-RP))))))

  (defthm rp-evlt-of-rp-equal
    (implies (and (rp-equal term1 term2))
             (equal (equal (rp-evlt term1 a)
                           (rp-evlt term2 a))
                    t))
    :hints (("Goal"
             :in-theory (e/d (rp-evlt-of-remove-rp
                              rp-equal-of-remove-rp)
                             (rp-trans
                              rp-equal)))))

  (defthm rp-evlt-lst-of-rp-equal-lst
    (implies (and (rp-equal-subterms subterm1 subterm2))
             (equal (equal (rp-evlt-lst subterm1 a)
                           (rp-evlt-lst subterm2 a))
                    t))
    :hints (("Goal"
             :in-theory (e/d (rp-evlt-lst-of-remove-rp-lst
                              rp-equal-subterms-of-remove-rp-lst)
                             (rp-trans-lst
                              rp-equal-subterms)))))

  #|(defthm-rp-trans-induct
  (defthm rp-evlt-of-rp-equal
  (implies (and (rp-equal term term2))
  (equal (equal (rp-evlt term a)
  (rp-evlt term2 a))
  t))
  :flag rp-trans-induct)

  (defthm rp-evlt-of-rp-equal-subterms
  (implies (and (rp-equal-subterms lst subterm2))
  (equal (equal (rp-evlt-lst lst a)
  (rp-evlt-lst subterm2 a))
  t))
  :flag rp-trans-induct-lst)
  :hints (("Goal"
  :do-not-induct t
  :in-theory (e/d (;;ex-from-rp
  is-rp
  is-falist
  rp-equal-subterms
  rp-equal)
  (cons-equal)))))||#)

(defthm-rp-equal
  (defthm rp-equal-is-symmetric
    (equal (rp-equal term2 term1)
           (rp-equal term1 term2))
    :flag rp-equal)
  (defthm rp-equal-subterms-is-symmetric
    (equal (rp-equal-subterms subterm2 subterm1)
           (rp-equal-subterms subterm1 subterm2))
    :flag rp-equal-subterms))

(encapsulate
  nil
  (local
   (in-theory (disable ex-from-rp ex-from-synp )))

  (local
   (defthm lemma1
     (implies (rp-equal2 term term2)
              (iff (consp (ex-from-synp (ex-from-rp term)))
                   (consp (ex-from-synp (ex-from-rp term2)))))
     :hints (("goal"
              :in-theory (enable ex-from-rp ex-from-synp)))))

  (local
   (defthm lemma1-2
     (implies (rp-equal term1 term2)
              (and #|(iff (consp (ex-from-rp-loose term1))
               (consp (ex-from-rp-loose term2)))||#
               (iff (consp (ex-from-rp term1))
                    (consp (ex-from-rp term2)))
               (iff (consp (ex-from-synp (ex-from-rp term1)))
                    (consp (ex-from-synp (ex-from-rp term2))))))
     :rule-classes :forward-chaining
     :hints (("goal" :in-theory (enable ex-from-rp
                                        ex-from-rp-loose
                                        is-rp
                                        is-rp-loose
                                        ex-from-synp)))))

  (local
   (defthm lemma3 (implies (and (acl2::flag-is 'rp-equal2)
                                (not (consp (ex-from-synp (ex-from-rp term2))))
                                (rp-equal term1 term2))
                           (equal (equal (ex-from-synp (ex-from-rp term1))
                                         (ex-from-synp (ex-from-rp term2)))
                                  t))
     :hints (("goal" :expand (rp-equal term1 term2)))))
  (local
   (defthm lemma4
     (implies (and (not (consp (ex-from-synp (ex-from-rp term1))))
                   (rp-equal term1 term2))
              (equal (ex-from-synp (ex-from-rp term1))
                     (ex-from-synp (ex-from-rp term2))))
     :hints (("goal"
              :use (:instance lemma1-2)
              :expand (rp-equal term1 term2)
              :in-theory (disable lemma1-2)))))
  (local
   (defthm lemma5
     (implies
      (and (consp (ex-from-synp (ex-from-rp term1)))
           (not (should-term-be-in-cons (ex-from-synp (ex-from-rp term2))
                                        (ex-from-synp (ex-from-rp term1))))
           (not (equal (car (ex-from-synp (ex-from-rp term1)))
                       'quote))
           (not (equal (car (ex-from-synp (ex-from-rp term2)))
                       'quote))
           (not (equal (car (ex-from-synp (ex-from-rp term1)))
                       (car (ex-from-synp (ex-from-rp term2))))))
      (not (rp-equal term1 term2)))
     :hints (("goal" :expand (rp-equal term1 term2)
              :in-theory (enable ex-from-synp)))))
  (local
   (defthm lemma6
     (implies (not (iff (is-synp (ex-from-rp term1))
                        (is-synp (ex-from-rp term2))))
              (not (rp-equal term1 term2)))
     :hints (("goal" :in-theory (enable is-synp)
              :expand ((rp-equal term1 term2)
                       (rp-equal-subterms (cdr (ex-from-rp term1))
                                          (cdr (ex-from-rp term2)))
                       (rp-equal-subterms (cddr (ex-from-rp term1))
                                          (cddr (ex-from-rp term2)))
                       (rp-equal-subterms (cdddr (ex-from-rp term1))
                                          (cdddr (ex-from-rp term2))))))))
  (local
   (defthm lemma7
     (implies (rp-equal term1 term2)
              (rp-equal-subterms (cdr (ex-from-synp (ex-from-rp term1)))
                                 (cdr (ex-from-synp (ex-from-rp term2)))))
     :hints (("goal" :in-theory (enable  ex-from-synp)))))

  (local
   (defthm lemma8
     (implies (not (equal (car (ex-from-synp (ex-from-rp term1)))
                          (car (ex-from-synp (ex-from-rp term2)))))
              (not (rp-equal term1 term2)))
     :hints (("goal" :in-theory (enable ex-from-synp)
              :expand ((rp-equal term1 term2)
                       (rp-equal-subterms (cdr (ex-from-rp term1))
                                          (cdr (ex-from-rp term2))))))))

  (local
   (defthm lemma9-lemma1
     (implies (and (equal (car a) (car b))
                   (consp b)
                   (equal (cdr a) (cdr b))
                   (consp a))
              (equal a b))
     :rule-classes :forward-chaining))

  (local
   (defthm lemma9-lemma2
     (implies (and (equal a b)
                   (is-synp a))
              (is-synp b))))

  (local
   (defthm lemma9-lemma
     (implies (and (consp b)
                   (equal (car a) (car b))
                   (equal (cdr a) (cdr b))
                   (is-synp a))
              (is-synp b))
; Removed by Matt K. 7/2021 (is-synp-implies removed in aux-function-lemmas.lisp)
#||
     :hints (("goal" :in-theory (disable is-synp-implies)
              :use ((:instance is-synp-implies (term a)))))
||#
     ))

  (local
   (defthm lemma9-lemma3
     (implies (and (consp (ex-from-rp term2))
                   (not (equal (car (ex-from-rp term1)) 'quote))
                   (equal (car (ex-from-rp term1))
                          (car (ex-from-rp term2)))
                   (consp (cdr (ex-from-rp term1)))
                   (consp (cdr (ex-from-rp term2)))
                   (rp-equal (cadr (ex-from-rp term1))
                             (cadr (ex-from-rp term2)))
                   (rp-equal-subterms (cddr (ex-from-rp term1))
                                      (cddr (ex-from-rp term2)))
                   (is-synp (ex-from-rp term1)))
              (is-synp (ex-from-rp term2)))
     :hints (("goal" :in-theory (enable is-synp)
              :expand
              ((rp-equal-subterms (cddr (ex-from-rp term1))
                                  (cddr (ex-from-rp term2)))
               (rp-equal-subterms (cdddr (ex-from-rp term1))
                                  (cdddr (ex-from-rp term2))))))))

  (local
   (defthm lemma9
     (implies (and (rp-equal term1 term2)
                   (consp (ex-from-synp (ex-from-rp term1)))
                   (equal (car (ex-from-synp (ex-from-rp term1)))
                          'quote))
              (equal (equal (ex-from-synp (ex-from-rp term1))
                            (ex-from-synp (ex-from-rp term2)))
                     t))
     :hints (("goal"
              :in-theory (enable ex-from-synp)
              :expand ((rp-equal term1 term2)
                       (rp-equal-subterms (cdr (ex-from-rp term1))
                                          (cdr (ex-from-rp term2))))))))

  (local
   (defthm lemma10
     (implies (should-term-be-in-cons (ex-from-synp (ex-from-rp term2))
                                      (ex-from-synp (ex-from-rp term1)))
              (not (rp-equal term1 term2)))
     :hints (("goal" :in-theory (enable should-term-be-in-cons
                                        ex-from-synp)))))

  (local
   (defthm lemma11
     (implies (should-term-be-in-cons (ex-from-synp (ex-from-rp term1))
                                      (ex-from-synp (ex-from-rp term2)))
              (not (rp-equal term1 term2)))
     :hints
     (("goal" :in-theory (enable is-synp should-term-be-in-cons)
       :expand (;(ex-from-synp (ex-from-rp term1))
                (rp-equal term1 term2))))))

  #|(defthm lemma4
  (implies (or (and (not (is-synp (ex-from-rp term1)))
  (is-synp (ex-from-rp term2)))
  (and  (is-synp (ex-from-rp term1))
  (not (is-synp (ex-from-rp term2)))))
  (not (rp-equal term1 term2)))
  :hints (("goal" :in-theory (enable )
  :expand (rp-equal term1 term2))))||#

  (defthm-rp-equal2
    (defthm rp-equal-implies-rp-equal2
      (implies (rp-equal term1 term2)
               (rp-equal2 term1 term2))
      :flag rp-equal2)
    (defthm rp-equal-subterms-implies-rp-equal2-subterms
      (implies (rp-equal-subterms subterm1 subterm2)
               (rp-equal2-subterms subterm1 subterm2))
      :flag rp-equal2-subterms)))

(encapsulate
  nil

  (local
   (in-theory (disable ex-from-synp ex-from-rp)))

  (local
   (in-theory (disable should-term-be-in-cons-lemma4
                       should-term-be-in-cons-lemma2)))

  (local
   (defthm lemma1
     (implies (rp-equal2 term1 term2)
              (iff (consp (ex-from-synp (ex-from-rp term1)))
                   (consp (ex-from-synp (ex-from-rp term2)))))
     :hints (("Goal" :in-theory (enable ex-from-rp ex-from-synp)))))

  (local
   (defthm lemma2
     (implies (and (EQUAL (RP-EVL-LST (CDR (EX-FROM-SYNP (EX-FROM-RP TERM1)))
                                      A)
                          (RP-EVL-LST (CDR (EX-FROM-SYNP (EX-FROM-RP TERM2)))
                                      A))
                   (CONSP (EX-FROM-SYNP (EX-FROM-RP TERM1)))
                   (CONSP (EX-FROM-SYNP (EX-FROM-RP TERM2)))
                   (NOT (EQUAL (CAR (EX-FROM-SYNP (EX-FROM-RP TERM2)))
                               'QUOTE))
                   (NOT (EQUAL (CAR (EX-FROM-SYNP (EX-FROM-RP TERM1)))
                               'QUOTE))
                   (syntaxp (and (equal term1 'term1)
                                 (equal term2 'term2)))
                   (EQUAL (CAR (EX-FROM-SYNP (EX-FROM-RP TERM1)))
                          (CAR (EX-FROM-SYNP (EX-FROM-RP TERM2)))))
              (equal (rp-evl (EX-FROM-SYNP (EX-FROM-RP TERM1)) a)
                     (rp-evl (EX-FROM-SYNP (EX-FROM-RP TERM2)) a)))
     :rule-classes :rewrite
     :hints (("Goal" :in-theory (e/d (rp-evl-of-fncall-args)
                                     (EVL-OF-EXTRACT-FROM-RP
                                      EVL-OF-EXTRACT-FROM-SYNP))))))

; Matt K. mod 7/2021: The following lemma is no longer accepted due to a
; strengthening of remove-guard-holders.
#||
  (local
   (defthm lemma4
     (implies (SHOULD-TERM-BE-IN-CONS rule-lhs term)
              (AND (QUOTEP TERM)
                   (CONSP (UNQUOTE TERM))
                   (CASE-MATCH RULE-LHS (('CONS & &) T)
                     (& NIL))
                   (equal (car (put-term-in-cons term))
                          (car rule-lhs))))
     :hints (("Goal" :in-theory (enable put-term-in-cons should-term-be-in-cons)))))
||#

  (local
   (defthm lemma5
     (implies (and (rp-termp term1)
                   (syntaxp (and (equal term1 'term1)
                                 (equal term2 'term2)))
                   (rp-termp term2))
              (equal (equal (rp-evl term1 a)
                            (rp-evl term2 a))
                     (equal (rp-evl (EX-FROM-SYNP (EX-FROM-RP TERM1)) a)
                            (rp-evl (EX-FROM-SYNP (EX-FROM-RP TERM2)) a))))))

  (local
   (defthm lemma6
     (implies (and (quotep term)
                   (consp (unquote term)))
              (and (equal (rp-evl (put-term-in-cons term) a)
                          (rp-evl term a))))
     :hints (("Goal" :in-theory (enable put-term-in-cons)))))

  (local
   (defthm lemma7
     (implies (and (rp-termp term)
                   (quotep term)
                   (consp (unquote term)))
              (rp-term-listp (cdr (put-term-in-cons term))))
     :hints (("Goal" :in-theory (enable put-term-in-cons)))))

  (local
   (in-theory (disable EVL-OF-EXTRACT-FROM-RP
                       EVL-OF-EXTRACT-FROM-SYNP)))

  (local
   (defthm lemma8-lemma0
     (equal (equal (list a b)
                   (list c d))
            (and (equal a c)
                 (equal b d)))))

  (local
   (defthm lemma8-lemma1
     (equal (equal (list a b)
                   (list c d))
            (equal (cons a b)
                   (cons c d)))))

  (local
   (defthm lemma8
     (implies (and (should-term-be-in-cons lhs term)
                   (equal (rp-evl-lst (cdr (put-term-in-cons term)) a)
                          (rp-evl-lst (cdr lhs) a))
                   (syntaxp (or
                             (and (equal term '(EX-FROM-SYNP (EX-FROM-RP TERM1)))
                                  (equal lhs '(EX-FROM-SYNP (EX-FROM-RP
                                                             TERM2))))
                             (and (equal term '(EX-FROM-SYNP (EX-FROM-RP TERM2)))
                                  (equal lhs '(EX-FROM-SYNP (EX-FROM-RP TERM1)))))))
              (and (equal (car (put-term-in-cons term))
                          (car lhs))
                   (equal (rp-evl term a)
                          (rp-evl lhs a))))
     :hints (("Goal" :in-theory (enable
                                 rp-evl-of-fncall-args
                                 should-term-be-in-cons
                                 put-term-in-cons)))))

  (local
   (defthm lemma9
     (implies (and (should-term-be-in-cons lhs term)
                   (rp-termp lhs))
              (rp-term-listp (cdr lhs)))
     :hints (("Goal" :in-theory (enable should-term-be-in-cons)))))

  (local
   (defthm lemma10
     (implies (and (consp term)
                   (not (EQUAL (CAR TERM) 'QUOTE))
                   (rp-termp term))
              (rp-term-listp (cdr term)))))

  (local
   (defthm lemma11
     (implies (and (should-term-be-in-cons term1 term2)
                   (equal  (rp-evl (cadr term1) a)
                           (rp-evl (cadr (put-term-in-cons term2)) a))
                   (equal  (rp-evl (caddr term1) a)
                           (rp-evl (caddr (put-term-in-cons term2)) a)))
              (equal (equal (rp-evl term1 a)
                            (rp-evl term2 a))
                     t))
     :hints (("Goal" :in-theory (enable put-term-in-cons
                                        should-term-be-in-cons)))))

  (local
   (defthm lemma11-2
     (implies (and (should-term-be-in-cons term1 term2)
                   (equal  (rp-evl (cadr term1) a)
                           (rp-evl (cadr (put-term-in-cons term2)) a))
                   (equal  (rp-evl (caddr term1) a)
                           (rp-evl (caddr (put-term-in-cons term2)) a)))
              (equal (equal (rp-evl term2 a)
                            (rp-evl term1 a))
                     t))
     :hints (("Goal" :in-theory (enable put-term-in-cons
                                        should-term-be-in-cons)))))

  (local
   (in-theory (disable is-cons)))

  (local
   (defthm lemma12-1
     (implies (and
               (RP-TERMP term1)
               (is-cons term1))
              (RP-TERMP (CADDR term1)))
     :hints (("Goal" :in-theory (enable is-cons  )))))

  (local
   (defthm lemma12-2
     (implies (and
               (RP-TERMP term)
               (QUOTEP TERM)
               (CONSP (UNQUOTE TERM)))
              (RP-TERMP (CADDR (PUT-TERM-IN-CONS term))))
     :hints (("Goal"  :in-theory (enable PUT-TERM-IN-CONS  )))))

  (local
   (defthm lemma13
     (implies (and (SHOULD-TERM-BE-IN-CONS rule-lhs term)
                   t)
              (AND (QUOTEP TERM)
                   (CONSP (UNQUOTE TERM))
                   (is-cons rule-lhs)))
     :hints (("Goal" :in-theory (enable is-cons should-term-be-in-cons)))))

  (defthm-rp-equal2
    (defthm rp-evl-of-rp-equal2
      (implies (and (rp-termp term1)
                    (rp-termp term2)
                    (rp-equal2 term1 term2))
               (equal (equal (rp-evl term1 a)
                             (rp-evl term2 a))
                      t))
      :flag rp-equal2)

    (defthm rp-evl-of-rp-equal2-subterms
      (implies (and (rp-equal2-subterms subterm1 subterm2)
                    (rp-term-listp subterm1)
                    (rp-term-listp subterm2))
               (equal (equal (rp-evl-lst subterm1 a)
                             (rp-evl-lst subterm2 a))
                      t))
      :flag rp-equal2-subterms)))

(defthm rp-equal2-of-ex-from-rp
  (implies (rp-termp term)
           (equal (rp-equal2 term1 (ex-from-rp term2))
                  (rp-equal2 term1 term2)))
  :hints (("Goal" :in-theory (disable ex-from-synp)
           :do-not-induct t
           :expand ((rp-equal2 term1 (ex-from-rp term2))
                    (rp-equal2 term1 term2)))))

(defthm-rp-equal2
  (defthm rp-equal2-reflexive
    (implies (equal term1 term2)
             (rp-equal2 term1 term2))
    :flag rp-equal2)

  (defthm rp-equal2-subterms-reflexive
    (implies (equal subterm1 subterm2)
             (rp-equal2-subterms subterm1 subterm2))
    :flag rp-equal2-subterms))

(defthm-rp-equal2
  (defthm rp-equal2-booleanp
    (booleanp (rp-equal2 term1 term2))
    :flag rp-equal2)
  (defthm rp-equal2-subterms-booleanp
    (booleanp (rp-equal2-subterms subterm1 subterm2))
    :flag rp-equal2-subterms))

#|(encapsulate
  nil
  (local
   (include-book "measure-lemmas"))
  (defun
    flag2-rp-equal2
    (flag term1 term2 subterm1 subterm2)
    (declare (xargs :non-executable t :mode :logic))
    (declare
     (xargs :verify-guards nil
            :normalize nil
            :measure (case flag (rp-equal2 (cons-count term2))
                       (otherwise (cons-count subterm2)))
            :hints nil
            :well-founded-relation o<
            :mode :logic)
     (ignorable term1 term2 subterm1 subterm2))
    (prog2$
     (acl2::throw-nonexec-error
      'flag2-rp-equal2
      (list flag term1 term2 subterm1 subterm2))
     (cond
      ((equal flag 'rp-equal2)
       ((lambda
         (term1 term2)
         ((lambda
           (term1 term2)
           ((lambda
             (term2 term1)
             ((lambda
               (term2 term1)
               (if
                (consp term1)
                (if
                 (consp term2)
                 (if
                  (should-term-be-in-cons term2 term1)
                  ((lambda
                    (nterm term2)
                    (if (flag2-rp-equal2 'rp-equal2
                                         (car (cdr nterm))
                                         (car (cdr term2))
                                         nil nil)
                        (flag2-rp-equal2 'rp-equal2
                                         (car (cdr (cdr nterm)))
                                         (car (cdr (cdr term2)))
                                         nil nil)
                        'nil))
                   (put-term-in-cons term1)
                   term2)
                  (if
                   (should-term-be-in-cons term1 term2)
                   ((lambda
                     (nterm term1)
                     (if (flag2-rp-equal2 'rp-equal2
                                          (car (cdr term1))
                                          (car (cdr nterm))
                                          nil nil)
                         (flag2-rp-equal2 'rp-equal2
                                          (car (cdr (cdr term1)))
                                          (car (cdr (cdr nterm)))
                                          nil nil)
                         'nil))
                    (put-term-in-cons term2)
                    term1)
                   (if
                    (quotep term1)
                    (equal term1 term2)
                    (if (quotep term2)
                        (equal term1 term2)
                        (if (equal (car term1) (car term2))
                            (flag2-rp-equal2 'rp-equal2-subterms
                                             nil nil (cdr term1)
                                             (cdr term2))
                            'nil)))))
                 'nil)
                (equal term1 term2)))
              (extract-from-synp term2)
              term1))
            (ex-from-rp term2)
            term1))
          (extract-from-synp term1)
          term2))
        (ex-from-rp term1)
        term2))
      (t (if (consp subterm1)
             (if (consp subterm2)
                 (if (flag2-rp-equal2 'rp-equal2
                                      (car subterm1)
                                      (car subterm2)
                                      nil nil)
                     (flag2-rp-equal2 'rp-equal2-subterms
                                      nil nil (cdr subterm1)
                                      (cdr subterm2))
                     'nil)
                 'nil)
             (equal subterm1 subterm2)))))))||#

(encapsulate
  nil

  (local
   (in-theory (disable ex-from-synp)))

  (local
   (defthm rp-equal2-lemma1
     (implies (should-term-be-in-cons term1 term2)
              (not (should-term-be-in-cons term2 term1)))
     :hints (("Goal" :in-theory (enable should-term-be-in-cons)))))

  (defthm-rp-equal2
    (defthm rp-equal2-is-symmetric
      (equal (rp-equal2 term2 term1)
             (rp-equal2 term1 term2))
      :flag rp-equal2)
    (defthm rp-equal2-subterms-is-symmetric
      (equal (rp-equal2-subterms subterm2 subterm1)
             (rp-equal2-subterms subterm1 subterm2))
      :flag rp-equal2-subterms)
    :hints (("Goal" :expand ((rp-equal2 term2 term1)
                             (rp-equal2-subterms subterm2 subterm1)))))

  #|(skip-proofs
  ;; necessary for def-equiv
  (defthm-rp-equal2
  (defthm rp-equal2-transitive
  (implies (and (rp-equal2 term0 term1)
  (rp-equal2 term1 term2))
  (rp-equal2 term0 term2))
  :flag rp-equal2)

  (defthm rp-equal2-subterms2-transitive
  (implies (and (rp-equal2-subterms subterm0 subterm1)
  (rp-equal2-subterms subterm1 subterm2))
  (rp-equal2-subterms subterm0 subterm2))
  :flag rp-equal2-subterms)

;:hints (("Goal" :induct (flag2-rp-equal2 flag term0 term1 term2 subterm0 ; ; ; ; ; ; ; ; ; ; ; ; ; ;
;                                        subterm1 subterm2))) ; ; ; ; ; ; ; ; ; ; ; ; ; ;
  ))||#

  )

#|(local
(defequiv rp-equal2))||#

#|(local
(defequiv rp-equal2-subterms))||#

(defthm-rp-equal-cnt
  (defthm rp-equal-cnt-is-rp-equal
    (equal (rp-equal-cnt term1 term2 cnt)
           (rp-equal term1 term2 ))
    :flag rp-equal-cnt)
  (defthm rp-equal-cnt-subterms-is-rp-equal-subterms
    (equal (rp-equal-cnt-subterms subterm1 subterm2 cnt)
           (rp-equal-subterms subterm1 subterm2 ))
    :flag rp-equal-cnt-subterms))





(defthm-rp-equal
  (defthmd rp-equal-alt-def
    (equal (rp-equal term1 term2)
           (equal (ex-from-rp-all term1)
                  (ex-from-rp-all term2)))
    :flag rp-equal)
  (defthmd rp-equal-subterms-alt-def
    (equal (rp-equal-subterms subterm1 subterm2)
           (equal (ex-from-rp-all-lst subterm1)
                  (ex-from-rp-all-lst subterm2)))
    :flag rp-equal-subterms)
  :hints (("Goal"
           :expand ((EX-FROM-RP-ALL TERM1)
                    (EX-FROM-RP-ALL-LST SUBTERM1)
                    (EX-FROM-RP-ALL-LST SUBTERM2)
                    (EX-FROM-RP-ALL TERM2))
           :in-theory (e/d (;is-rp
                            ex-from-rp-all
                            )
                           (ex-from-rp)))))



(defequiv rp-equal
  :hints (("Goal"
           :in-theory (e/d (rp-equal-alt-def) ()))))

(defequiv rp-equal-subterms
  :hints (("Goal"
           :in-theory (e/d (rp-equal-subterms-alt-def) ()))))

(defcong rp-equal equal (rp-evlt term a) 1)
(defcong rp-equal equal (rp-evl term a) 1)

(defcong rp-equal-subterms equal (rp-evlt-lst lst a) 1)


(defthm rp-equal-of-rp
  (implies (is-rp (cons 'rp x))
           (and (equal (rp-equal y (cons 'rp x))
                       (rp-equal y (cadr x)))
                (equal (rp-equal (cons 'rp x) y)
                       (rp-equal (cadr x) y))))
  :hints (("Goal"
           :Expand ((EX-FROM-RP-ALL (CONS 'RP X))
                    (EX-FROM-RP (CONS 'RP X)))
           :in-theory (e/d (rp-equal-alt-def
                            ex-from-rp-all)
                           ()))))