aboutsummaryrefslogtreecommitdiff
path: root/src/RewriterInterpProofs1.v
blob: 145a3d388aa7e79ead004d580d99c4b9219dacc6 (plain)
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
Require Import Coq.ZArith.ZArith.
Require Import Coq.micromega.Lia.
Require Import Coq.Lists.SetoidList.
Require Import Coq.Lists.List.
Require Import Coq.Classes.Morphisms.
Require Import Coq.MSets.MSetPositive.
Require Import Coq.FSets.FMapPositive.
Require Import Coq.Logic.FunctionalExtensionality.
Require Import Crypto.Language.
Require Import Crypto.LanguageInversion.
Require Import Crypto.LanguageWf.
Require Import Crypto.UnderLetsProofs.
Require Import Crypto.GENERATEDIdentifiersWithoutTypesProofs.
Require Import Crypto.Rewriter.
Require Import Crypto.RewriterWf1.
Require Import Crypto.Util.MSetPositive.Facts.
Require Import Crypto.Util.Tactics.BreakMatch.
Require Import Crypto.Util.Tactics.SplitInContext.
Require Import Crypto.Util.Tactics.SpecializeAllWays.
Require Import Crypto.Util.Tactics.SpecializeBy.
Require Import Crypto.Util.Tactics.RewriteHyp.
Require Import Crypto.Util.Tactics.Head.
Require Import Crypto.Util.Tactics.CPSId.
Require Import Crypto.Util.Tactics.SetEvars.
Require Import Crypto.Util.Tactics.SubstEvars.
Require Import Crypto.Util.Tactics.TransparentAssert.
Require Import Crypto.Util.Prod.
Require Import Crypto.Util.Sigma.Related.
Require Import Crypto.Util.ListUtil.
Require Import Crypto.Util.ListUtil.SetoidList.
Require Import Crypto.Util.Option.
Require Import Crypto.Util.CPSNotations.
Require Import Crypto.Util.HProp.
Require Import Crypto.Util.Decidable.
Require Import Crypto.Util.Notations.
Import ListNotations. Local Open Scope bool_scope. Local Open Scope Z_scope.

Import EqNotations.
Module Compilers.
  Import Language.Compilers.
  Import LanguageInversion.Compilers.
  Import LanguageWf.Compilers.
  Import UnderLetsProofs.Compilers.
  Import GENERATEDIdentifiersWithoutTypesProofs.Compilers.
  Import Rewriter.Compilers.
  Import RewriterWf1.Compilers.
  Import expr.Notations.
  Import defaults.
  Import Rewriter.Compilers.RewriteRules.
  Import RewriterWf1.Compilers.RewriteRules.

  Module Import RewriteRules.
    Module Compile.
      Import Rewriter.Compilers.RewriteRules.Compile.
      Import RewriterWf1.Compilers.RewriteRules.Compile.

      Section with_var.
        Local Notation type_of_list
          := (fold_right (fun a b => prod a b) unit).
        Local Notation type_of_list_cps
          := (fold_right (fun a K => a -> K)).
        Context {ident var : type.type base.type -> Type}
                (eta_ident_cps : forall {T : type.type base.type -> Type} {t} (idc : ident t)
                                        (f : forall t', ident t' -> T t'),
                    T t)
                {pident : type.type pattern.base.type -> Type}
                (pident_arg_types : forall t, pident t -> list Type)
                (pident_unify pident_unify_unknown : forall t t' (idc : pident t) (idc' : ident t'), option (type_of_list (pident_arg_types t idc)))
                {raw_pident : Type}
                (strip_types : forall t, pident t -> raw_pident)
                (raw_pident_beq : raw_pident -> raw_pident -> bool)

                (full_types : raw_pident -> Type)
                (invert_bind_args invert_bind_args_unknown : forall t (idc : ident t) (pidc : raw_pident), option (full_types pidc))
                (type_of_raw_pident : forall (pidc : raw_pident), full_types pidc -> type.type base.type)
                (raw_pident_to_typed : forall (pidc : raw_pident) (args : full_types pidc), ident (type_of_raw_pident pidc args))
                (raw_pident_is_simple : raw_pident -> bool)
                (pident_unify_unknown_correct
                 : forall t t' idc idc', pident_unify_unknown t t' idc idc' = pident_unify t t' idc idc')
                (invert_bind_args_unknown_correct
                 : forall t idc pidc, invert_bind_args_unknown t idc pidc = invert_bind_args t idc pidc)
                (eta_ident_cps_correct : forall T t idc f, @eta_ident_cps T t idc f = f _ idc)
                (raw_pident_to_typed_invert_bind_args_type
                 : forall t idc p f, invert_bind_args t idc p = Some f -> t = type_of_raw_pident p f)
                (raw_pident_to_typed_invert_bind_args
                 : forall t idc p f (pf : invert_bind_args t idc p = Some f),
                    raw_pident_to_typed p f = rew [ident] raw_pident_to_typed_invert_bind_args_type t idc p f pf in idc).

        Local Notation type := (type.type base.type).
        Local Notation expr := (@expr.expr base.type ident var).
        Local Notation pattern := (@pattern.pattern pident).
        Local Notation UnderLets := (@UnderLets.UnderLets base.type ident var).
        Local Notation ptype := (type.type pattern.base.type).
        Local Notation value' := (@value' base.type ident var).
        Local Notation value := (@value base.type ident var).
        Local Notation value_with_lets := (@value_with_lets base.type ident var).
        Local Notation Base_value := (@Base_value base.type ident var).
        Local Notation splice_under_lets_with_value := (@splice_under_lets_with_value base.type ident var).
        Local Notation splice_value_with_lets := (@splice_value_with_lets base.type ident var).
        Local Notation rawexpr := (@rawexpr ident var).
        Local Notation eval_decision_tree := (@eval_decision_tree ident var raw_pident full_types invert_bind_args invert_bind_args_unknown type_of_raw_pident raw_pident_to_typed raw_pident_is_simple).
        Local Notation reveal_rawexpr_cps_gen := (@reveal_rawexpr_cps_gen ident var).
        Local Notation reveal_rawexpr_cps := (@reveal_rawexpr_cps ident var).
        Local Notation eval_rewrite_rules := (@eval_rewrite_rules ident var pident pident_arg_types pident_unify pident_unify_unknown raw_pident full_types invert_bind_args invert_bind_args_unknown type_of_raw_pident raw_pident_to_typed raw_pident_is_simple).
        Local Notation rewrite_rulesT := (@rewrite_rulesT ident var pident pident_arg_types).
        Local Notation rewrite_with_rule := (@rewrite_with_rule ident var pident pident_arg_types pident_unify pident_unify_unknown).
        Let type_base (t : base.type) : type := type.base t.
        Coercion type_base : base.type >-> type.

        Context (reify_and_let_binds_base_cps : forall (t : base.type), expr t -> forall T, (expr t -> UnderLets T) -> UnderLets T).

        Local Notation "e <---- e' ; f" := (splice_value_with_lets e' (fun e => f%under_lets)) : under_lets_scope.
        Local Notation "e <----- e' ; f" := (splice_under_lets_with_value e' (fun e => f%under_lets)) : under_lets_scope.
        Local Notation "e1 === e2" := (existT expr _ e1 = existT expr _ e2) : type_scope.

        Local Existing Instance SetoidList.eqlistA_equiv.

        Local Ltac rew_swap_list_step :=
          match goal with
          | [ H : swap_list ?a ?b ?ls1 = Some ?ls2, H' : context[swap_list ?a ?b ?ls2] |- _ ]
            => rewrite (swap_swap_list H) in H'
          | [ H : swap_list ?a ?b ?ls1 = Some ?ls2 |- context[swap_list ?a ?b ?ls2] ]
            => rewrite (swap_swap_list H)
          | [ H : swap_list ?a ?b ?ls1 = Some ?ls2 |- context[swap_list ?a ?b ?ls1] ]
            => rewrite H
          | [ H : swap_list ?a ?b ?ls1 = Some ?ls2,
                  H' : swap_list ?a ?b ?ls3 = Some ?ls4,
                       Hl : eqlistA ?R ?ls2 ?ls3
              |- _ ]
            => unique pose proof (swap_swap_list_eqlistA H H' Hl)
          end.

        Local Ltac rew_eval_decision_tree_step :=
          match goal with
          | [ H : (forall ctx' cont', eval_decision_tree ctx' ?d cont' = None \/ _)
              |- context[eval_decision_tree ?ctx ?d ?cont] ]
            => let H' := fresh in
               destruct (H ctx cont) as [H' | [? [? [H' ?] ] ] ]; rewrite H'
          end.

        Local Hint Constructors eqlistA.
        Local Hint Unfold rawexpr_equiv.
        Local Hint Unfold rawexpr_equiv_expr.

        Lemma eval_decision_tree_correct_Switch_cons
              {T} ctx icase icases app_case d cont
              (res := @eval_decision_tree T ctx (Switch (icase :: icases) app_case d) cont)
          : (exists b,
                res = match ctx with
                      | r :: ctx
                        => eval_decision_tree ctx (snd icase) (fun k ctx' => cont k (reveal_rawexpr_cps_gen (Some b) r _ id :: ctx'))
                      | _ => None
                      end)
            \/ res = eval_decision_tree ctx (Switch icases app_case d) cont
            \/ res = match app_case with
                     | Some app_case => eval_decision_tree ctx app_case cont
                     | None => None
                     end
            \/ res = eval_decision_tree ctx d cont.
        Proof using raw_pident_to_typed_invert_bind_args invert_bind_args_unknown_correct.
          destruct icase as [p icase]; subst res; cbn [fst snd].
          destruct ctx as [|r ctx]; [ now cbn; auto | ].
          destruct r.
          all: cbn [eval_decision_tree fold_right].
          all: destruct app_case as [app_case|].
          Set Ltac Profiling.
          Reset Ltac Profile.
          all: repeat first [ match goal with
                              | [ |- context[?x = ?x \/ _] ] => solve [ auto ]
                              | [ |- context[_ \/ ?x = ?x] ] => solve [ auto ]
                              | [ H : ?x = ?y |- context[?y = ?x \/ _] ] => solve [ auto ]
                              | [ H : ?y = ?x |- context[?x = ?y \/ _] ] => solve [ auto ]
                              | [ H : _ = ?v |- (exists b, ?v = _) \/ _ ]
                                => left; eexists; (idtac + symmetry); eassumption
                              | [ H : ?v = _ |- (exists b, ?v = _) \/ _ ]
                                => left; eexists; (idtac + symmetry); eassumption
                              | [ H : context[invert_bind_args_unknown ?a ?b ?c] |- _ ] => rewrite invert_bind_args_unknown_correct in H
                              | [ |- context[invert_bind_args_unknown ?a ?b ?c] ] => rewrite invert_bind_args_unknown_correct
                              | [ H : context[eval_decision_tree _ _ (fun _ _ => None)] |- _ ]
                                => rewrite eval_decision_tree_cont_None in H
                              | [ |- context[eval_decision_tree _ _ (fun _ _ => None)] ]
                                => rewrite eval_decision_tree_cont_None
                              | [ |- (exists b, ?f _ = ?f _) \/ _ ]
                                => left; eexists; reflexivity
                              end
                            | progress subst
                            | progress inversion_sigma
                            | progress inversion_option
                            | progress cbv [reveal_rawexpr_cps reveal_rawexpr_cps_gen value] in *
                            | progress cbn [value'] in *
                            | progress expr.invert_match
                            | break_match_hyps_step ltac:(fun v => is_var v; let t := type of v in unify t type)
                            | break_match_step ltac:(fun v => is_var v; let t := type of v in unify t type)
                            | match goal with
                              | [ |- context[match ?d with Failure => _ | _ => _ end] ] => is_var d; destruct d
                              end
                            | progress cbn [eq_rect Option.sequence Option.sequence_return] in *
                            | progress cbv [id option_bind' Option.bind Option.sequence Option.sequence_return] in *
                            | match goal with
                              | [ H : invert_bind_args _ _ _ = Some _ |- _ ]
                                => pose proof (@raw_pident_to_typed_invert_bind_args _ _ _ _ H);
                                   generalize dependent (@raw_pident_to_typed_invert_bind_args_type _ _ _ _ H); clear H; intros
                              | [ |- context[rew [?P] ?pf in ?v] ]
                                => lazymatch type of pf with
                                   | ?t1 = ?t2
                                     => generalize dependent v; generalize dependent pf;
                                        (generalize dependent t1 || generalize dependent t2);
                                        intros; subst
                                   end
                              | [ H : ?t = rew [?P] ?pf in ?v |- _ ]
                                => generalize dependent t; intros; subst
                              | [ H : context[rew [?P] ?pf in ?v] |- _ ]
                                => lazymatch type of pf with
                                   | ?t1 = ?t2
                                     => generalize dependent v; generalize dependent pf;
                                        (generalize dependent t1 || generalize dependent t2);
                                        intros; subst
                                   end
                              | [ |- context[match @fold_right ?A ?B ?f ?v ?ls with Some _ => _ | _ => _ end] ]
                                => destruct (@fold_right A B f v ls) eqn:?
                              end
                            | break_innermost_match_step ].
        Qed.

        Fixpoint eval_decision_tree_correct {T} d ctx cont
                 (res := @eval_decision_tree T ctx d cont)
                 {struct d}
          : res = None
            \/ exists n ctx',
              res = cont n ctx'
              /\ SetoidList.eqlistA rawexpr_equiv ctx ctx'.
        Proof using raw_pident_to_typed_invert_bind_args invert_bind_args_unknown_correct.
          specialize (eval_decision_tree_correct T).
          subst res; cbv zeta in *; destruct d.
          all: [ > specialize (eval_decision_tree_correct ltac:(assumption))
               | clear eval_decision_tree_correct
               |
               | specialize (eval_decision_tree_correct ltac:(assumption)) ].
          { cbn [eval_decision_tree]; cbv [Option.sequence]; break_innermost_match; eauto.
            all: right; repeat esplit; (idtac + symmetry); (eassumption + reflexivity). }
          { cbn; eauto. }
          { let d := match goal with d : decision_tree |- _ => d end in
            pose proof (eval_decision_tree_correct d) as eval_decision_tree_correct_default.
            let app_case := match goal with app_case : option decision_tree |- _ => app_case end in
            pose proof (match app_case return match app_case with Some _ => _ | _ => _ end with
                        | Some app_case => eval_decision_tree_correct app_case
                        | None => I
                        end) as eval_decision_tree_correct_app_case.
            let icases := match goal with icases : list (_ * decision_tree) |- _ => icases end in
            induction icases as [|icase icases IHicases];
              [ clear eval_decision_tree_correct | specialize (eval_decision_tree_correct (snd icase)) ].
            (* now that we have set up guardedness correctly, we can
               stop worrying so much about what order we destruct
               things in *)
            2: destruct (@eval_decision_tree_correct_Switch_cons _ ctx icase icases app_case d cont)
              as [ [? H] | [H| [H|H] ] ];
              rewrite H; try assumption.
            all: destruct app_case as [app_case|]; try (left; reflexivity).
            all: destruct ctx as [|ctx0 ctx]; [ try (left; reflexivity) | ].
            all: try destruct ctx0.
            all: cbn [eval_decision_tree fold_right]; cbv [reveal_rawexpr_cps reveal_rawexpr_cps_gen Option.sequence Option.sequence_return].
            all: repeat first [ rew_eval_decision_tree_step
                              | progress cbv [value id] in *
                              | progress cbn [value'] in *
                              | assumption
                              | reflexivity
                              | progress subst
                              | progress inversion_option
                              | expr.invert_match_step
                              | match goal with
                                | [ |- ?x = ?x \/ _ ] => left; reflexivity
                                | [ |- Some _ = None \/ _ ] => right
                                | [ |- None = Some _ \/ _ ] => right
                                | [ |- ?v = None \/ _ ]
                                  => lazymatch v with
                                     | match ?d with Failure => None | TryLeaf _ _ => None | _ => _ end
                                       => let H := fresh in
                                          assert (H : v = None) by (destruct d; auto); rewrite H
                                     | match ?d with Failure => ?x | TryLeaf _ _ => ?y | _ => _ end
                                       => let H := fresh in
                                          assert (H : v = x \/ v = y) by (destruct d; auto);
                                          destruct H as [H|H]; rewrite H
                                     end
                                | [ |- context[match ?x with nil => None | cons _ _ => _ end] ]
                                  => is_var x; destruct x
                                | [ |- match match ?x with nil => None | cons _ _ => _ end with None => None | Some _ => _ end = None \/ _ ]
                                  => is_var x; destruct x; [ left; reflexivity | ]
                                | [ |- _ \/ exists n ctx', ?f ?a ?b = ?f n ctx' /\ _ ]
                                  => right; exists a, b; split; [ reflexivity | ]
                                | [ |- exists n ctx', ?f ?a ?b = ?f n ctx' /\ _ ]
                                  => right; exists a, b; split; [ reflexivity | ]
                                | [ H : ?f ?a ?b = Some ?v |- exists n ctx', Some ?v = ?f n ctx' /\ _ ]
                                  => exists a, b; split; [ symmetry; assumption | ]
                                end
                              | break_match_hyps_step ltac:(fun v => is_var v; let t := type of v in unify t type)
                              | match goal with
                                | [ H : rawexpr_equiv ?a ?b |- eqlistA _ _ _ ] => unique assert (rawexpr_equiv b a) by (symmetry; exact H)
                                | [ H : eqlistA _ (_ :: _) _ |- eqlistA _ _ _ ] => inversion H; clear H; subst
                                | [ H : eqlistA _ nil _ |- eqlistA _ _ _ ] => inversion H; clear H; subst
                                | [ |- eqlistA _ (cons _ _) (cons _ _) ] => constructor
                                | [ |- eqlistA _ nil nil ] => constructor
                                | [ |- rawexpr_equiv _ _ ] => solve [ auto ]
                                | [ |- rawexpr_equiv (rValue ?v) ?r ] => change (rawexpr_equiv (rExpr v) r)
                                end
                              | break_innermost_match_step
                              | break_innermost_match_hyps_step ]. }
          { cbn [eval_decision_tree];
              repeat first [ rew_eval_decision_tree_step
                           | rew_swap_list_step
                           | solve [ eauto ]
                           | break_innermost_match_step ]. }
        Qed.

        Lemma eval_rewrite_rules_correct
              (do_again : forall t : base.type, @expr.expr base.type ident value t -> UnderLets (expr t))
              (maybe_do_again
               := fun (should_do_again : bool) (t : base.type)
                  => if should_do_again return ((@expr.expr base.type ident (if should_do_again then value else var) t) -> UnderLets (expr t))
                     then do_again t
                     else UnderLets.Base)
              (d : decision_tree)
              (rew_rules : rewrite_rulesT)
              (e : rawexpr)
              (res := @eval_rewrite_rules do_again d rew_rules e)
          : res = UnderLets.Base (expr_of_rawexpr e)
            \/ exists n pf e',
              nth_error rew_rules n = Some pf
              /\ Some res
                 = rewrite_with_rule do_again e' pf
              /\ rawexpr_equiv e e'.
        Proof using raw_pident_to_typed_invert_bind_args invert_bind_args_unknown_correct.
          subst res; cbv [eval_rewrite_rules].
          refine (let H := eval_decision_tree_correct d [e] _ in _).
          destruct H as [H| [? [? [H ?] ] ] ]; rewrite H; cbn [Option.sequence Option.sequence_return];
            [ left; reflexivity | ]; clear H.
          inversion_head' eqlistA.
          unfold Option.bind at 1.
          break_innermost_match_step; [ | left; reflexivity ].
          cbn [Option.bind Option.sequence Option.sequence_return].
          match goal with
          | [ |- (Option.sequence_return ?x ?y) = _ \/ _ ]
            => destruct x eqn:?
          end; [ | left; reflexivity ]; cbn [Option.sequence_return].
          right; repeat esplit; try eassumption; auto.
        Qed.
      End with_var.

      Section with_interp.
        Local Notation type_of_list
          := (fold_right (fun a b => prod a b) unit).
        Local Notation type_of_list_cps
          := (fold_right (fun a K => a -> K)).
        Context {ident : type.type base.type -> Type}
                {ident_interp : forall t, ident t -> type.interp base.interp t}
                (eta_ident_cps : forall {T : type.type base.type -> Type} {t} (idc : ident t)
                                        (f : forall t', ident t' -> T t'),
                    T t)
                {pident : type.type pattern.base.type -> Type}
                (pident_arg_types : forall t, pident t -> list Type)
                (pident_unify pident_unify_unknown : forall t t' (idc : pident t) (idc' : ident t'), option (type_of_list (pident_arg_types t idc)))
                {raw_pident : Type}
                (strip_types : forall t, pident t -> raw_pident)
                (raw_pident_beq : raw_pident -> raw_pident -> bool)

                (full_types : raw_pident -> Type)
                (invert_bind_args invert_bind_args_unknown : forall t (idc : ident t) (pidc : raw_pident), option (full_types pidc))
                (type_of_raw_pident : forall (pidc : raw_pident), full_types pidc -> type.type base.type)
                (raw_pident_to_typed : forall (pidc : raw_pident) (args : full_types pidc), ident (type_of_raw_pident pidc args))
                (raw_pident_is_simple : raw_pident -> bool)
                (pident_unify_unknown_correct
                 : forall t t' idc idc', pident_unify_unknown t t' idc idc' = pident_unify t t' idc idc')
                (invert_bind_args_unknown_correct
                 : forall t idc pidc, invert_bind_args_unknown t idc pidc = invert_bind_args t idc pidc)
                (eta_ident_cps_correct : forall T t idc f, @eta_ident_cps T t idc f = f _ idc)
                (raw_pident_to_typed_invert_bind_args_type
                 : forall t idc p f, invert_bind_args t idc p = Some f -> t = type_of_raw_pident p f)
                (raw_pident_to_typed_invert_bind_args
                 : forall t idc p f (pf : invert_bind_args t idc p = Some f),
                    raw_pident_to_typed p f = rew [ident] raw_pident_to_typed_invert_bind_args_type t idc p f pf in idc)
                (pident_to_typed
                 : forall t idc (evm : EvarMap),
                    type_of_list (pident_arg_types t idc) -> ident (pattern.type.subst_default t evm))
                (ident_interp_Proper : forall t, Proper (eq ==> type.eqv) (@ident_interp t))
                (pident_unify_to_typed
                 : forall t t' idc idc' v,
                    pident_unify t t' idc idc' = Some v
                    -> forall evm pf,
                      rew [ident] pf in @pident_to_typed t idc evm v = idc').

        Local Notation var := (type.interp base.interp) (only parsing).
        Local Notation type := (type.type base.type).
        Local Notation expr := (@expr.expr base.type ident var).
        Local Notation pattern := (@pattern.pattern pident).
        Local Notation UnderLets := (@UnderLets.UnderLets base.type ident var).
        Local Notation ptype := (type.type pattern.base.type).
        Local Notation value' := (@value' base.type ident var).
        Local Notation value := (@value base.type ident var).
        Local Notation value_with_lets := (@value_with_lets base.type ident var).
        Local Notation Base_value := (@Base_value base.type ident var).
        Local Notation splice_under_lets_with_value := (@splice_under_lets_with_value base.type ident var).
        Local Notation splice_value_with_lets := (@splice_value_with_lets base.type ident var).
        Local Notation rawexpr := (@rawexpr ident var).
        Local Notation eval_decision_tree := (@eval_decision_tree ident var raw_pident full_types invert_bind_args invert_bind_args_unknown type_of_raw_pident raw_pident_to_typed raw_pident_is_simple).
        Local Notation eval_rewrite_rules := (@eval_rewrite_rules ident var pident pident_arg_types pident_unify pident_unify_unknown raw_pident full_types invert_bind_args invert_bind_args_unknown type_of_raw_pident raw_pident_to_typed raw_pident_is_simple).
        Local Notation rewrite_rulesT := (@rewrite_rulesT ident var pident pident_arg_types).
        Local Notation rewrite_with_rule := (@rewrite_with_rule ident var pident pident_arg_types pident_unify pident_unify_unknown).
        Local Notation reify := (@reify ident var).
        Local Notation reflect := (@reflect ident var).
        (*Local Notation rawexpr_equiv_expr := (@rawexpr_equiv_expr ident var).*)
        Local Notation rewrite_rule_data_interp_goodT := (@rewrite_rule_data_interp_goodT ident pident pident_arg_types pident_to_typed ident_interp).
        Local Notation rewrite_rules_interp_goodT := (@rewrite_rules_interp_goodT ident pident pident_arg_types pident_to_typed ident_interp).
        Local Notation rewrite_ruleTP := (@rewrite_ruleTP ident var pident pident_arg_types).
        Local Notation rewrite_ruleT := (@rewrite_ruleT ident var pident pident_arg_types).
        Local Notation unify_pattern := (@unify_pattern ident var pident pident_arg_types pident_unify pident_unify_unknown).
        Local Notation unify_pattern' := (@unify_pattern' ident var pident pident_arg_types pident_unify pident_unify_unknown).
        Local Notation under_with_unification_resultT_relation_hetero := (@under_with_unification_resultT_relation_hetero ident var pident pident_arg_types).
        Local Notation under_with_unification_resultT'_relation_hetero := (@under_with_unification_resultT'_relation_hetero ident var pident pident_arg_types).
        Local Notation assemble_identifier_rewriters := (@assemble_identifier_rewriters ident var eta_ident_cps pident pident_arg_types pident_unify pident_unify_unknown raw_pident full_types invert_bind_args invert_bind_args_unknown type_of_raw_pident raw_pident_to_typed raw_pident_is_simple).
        Local Notation assemble_identifier_rewriters' := (@assemble_identifier_rewriters' ident var pident pident_arg_types pident_unify pident_unify_unknown raw_pident full_types invert_bind_args invert_bind_args_unknown type_of_raw_pident raw_pident_to_typed raw_pident_is_simple).
        Local Notation pattern_default_interp' := (@pattern_default_interp' ident pident pident_arg_types pident_to_typed (@ident_interp)).
        Local Notation pattern_default_interp := (@pattern_default_interp ident pident pident_arg_types pident_to_typed (@ident_interp)).
        Local Notation pattern_collect_vars := (@pattern.collect_vars pident).
        Local Notation app_with_unification_resultT_cps := (@app_with_unification_resultT_cps pident pident_arg_types).
        Local Notation app_transport_with_unification_resultT'_cps := (@app_transport_with_unification_resultT'_cps pident pident_arg_types).
        Local Notation with_unification_resultT' := (@with_unification_resultT' pident pident_arg_types).
        Local Notation value'_interp := (@value'_interp ident ident_interp).
        Local Notation eval_decision_tree_correct := (@eval_decision_tree_correct ident var raw_pident full_types invert_bind_args invert_bind_args_unknown type_of_raw_pident raw_pident_to_typed raw_pident_is_simple invert_bind_args_unknown_correct raw_pident_to_typed_invert_bind_args_type raw_pident_to_typed_invert_bind_args).
        Local Notation expr_interp_related := (@expr.interp_related _ ident _ ident_interp).
        Local Notation UnderLets_interp_related := (@UnderLets.interp_related _ ident _ ident_interp _ _ expr_interp_related).
        Local Notation rawexpr_interp_related := (@rawexpr_interp_related ident ident_interp).
        Local Notation value_interp_related := (@value_interp_related ident ident_interp).
        Local Notation unification_resultT'_interp_related := (@unification_resultT'_interp_related ident pident pident_arg_types ident_interp).
        Local Notation unification_resultT_interp_related := (@unification_resultT_interp_related ident pident pident_arg_types ident_interp).
        Let type_base (t : base.type) : type := type.base t.
        Coercion type_base : base.type >-> type.

        Context (reify_and_let_binds_base_cps : forall (t : base.type), expr t -> forall T, (expr t -> UnderLets T) -> UnderLets T)
                (interp_reify_and_let_binds_base
                 : forall t e v,
                    expr_interp_related e v
                    -> UnderLets_interp_related (@reify_and_let_binds_base_cps t e _ UnderLets.Base) v).

        Local Notation reify_and_let_binds_cps := (@reify_and_let_binds_cps ident var reify_and_let_binds_base_cps).
        Local Notation "e <---- e' ; f" := (splice_value_with_lets e' (fun e => f%under_lets)) : under_lets_scope.
        Local Notation "e <----- e' ; f" := (splice_under_lets_with_value e' (fun e => f%under_lets)) : under_lets_scope.

        Local Lemma pident_unify_to_typed'
          : forall t t' idc idc' v,
            pident_unify t t' idc idc' = Some v
            -> forall evm pf,
              @pident_to_typed t idc evm v = rew [ident] pf in idc'.
        Proof using pident_unify_to_typed.
          intros t t' idc idc' v H evm pf.
          pose proof (@pident_unify_to_typed t t' idc idc' v H evm (eq_sym pf)).
          subst; reflexivity.
        Qed.

        Lemma interp_reify_and_let_binds {with_lets t v1 v}
          : value_interp_related v1 v
            -> UnderLets_interp_related (@reify_and_let_binds_cps with_lets t v1 _ UnderLets.Base) v.
        Proof using interp_reify_and_let_binds_base.
          cbv [reify_and_let_binds_cps]; break_innermost_match;
            repeat first [ progress intros
                         | progress destruct_head'_ex
                         | progress destruct_head'_and
                         | progress subst
                         | solve [ eauto ]
                         | apply reify_interp_related
                         | eapply @UnderLets.splice_interp_related_of_ex with (RA:=expr_interp_related);
                           eexists (fun x => x), _; repeat apply conj;
                           [ eassumption | | reflexivity ] ].
        Qed.

        (*Local Infix "===" := expr_interp_related : type_scope.
        Local Infix "====" := value_interp_related : type_scope.
        Local Infix "=====" := rawexpr_interp_related : type_scope.*)

        Fixpoint types_match_with (evm : EvarMap) {t} (e : rawexpr) (p : pattern t) {struct p} : Prop
          := match p, e with
             | pattern.Wildcard t, e
               => pattern.type.subst t evm = Some (type_of_rawexpr e)
             | pattern.Ident t idc, rIdent known t' _ _ _
               => pattern.type.subst t evm = Some t'
             | pattern.App s d f x, rApp f' x' _ _
               => @types_match_with evm _ f' f
                  /\ @types_match_with evm _ x' x
             | pattern.Ident _ _, _
             | pattern.App _ _ _ _, _
               => False
             end.

        Lemma preunify_types_to_match_with {t re p evm}
          : match @preunify_types ident var pident t re p with
            | Some None => True
            | Some (Some (pt, t')) => pattern.type.subst pt evm = Some t'
            | None => False
            end
            -> types_match_with evm re p.
        Proof using Type.
          revert re; induction p; intro; cbn [preunify_types types_match_with];
            break_innermost_match; try exact id.
          all: repeat first [ progress Bool.split_andb
                            | progress type_beq_to_eq
                            | progress inversion_option
                            | progress Reflect.beq_to_eq
                                       (@type.type_beq pattern.base.type pattern.base.type.type_beq)
                                       (@type.internal_type_dec_bl pattern.base.type pattern.base.type.type_beq pattern.base.type.internal_type_dec_bl)
                                       (@type.internal_type_dec_lb pattern.base.type pattern.base.type.type_beq pattern.base.type.internal_type_dec_lb)
                            | progress subst
                            | reflexivity
                            | progress cbn [Option.bind pattern.type.subst_default pattern.type.subst]
                            | rewrite pattern.type.eq_subst_default_relax
                            | rewrite pattern.type.subst_relax
                            | match goal with
                              | [ H : (forall re, match preunify_types re ?p with _ => _ end -> _)
                                  |- context[preunify_types ?re' ?p] ]
                                => specialize (H re')
                              end
                            | break_innermost_match_hyps_step
                            | progress intros
                            | solve [ auto ]
                            | exfalso; assumption
                            | progress type.inversion_type
                            | progress cbv [Option.bind] in * ].
        Qed.

        Lemma unify_types_match_with {t re p evm}
          : @unify_types ident var pident t re p _ id = Some evm
            -> types_match_with evm re p.
        Proof using Type.
          intro H; apply preunify_types_to_match_with; revert H.
          cbv [unify_types id].
          break_innermost_match; intros; inversion_option; try exact I.
          RewriteRules.pattern.type.add_var_types_cps_id.
          cbv [option_type_type_beq] in *; break_innermost_match_hyps; type_beq_to_eq; inversion_option.
          let H := match goal with H : option_beq _ _ _ = true |- _ => H end in
          apply internal_option_dec_bl in H;
            [ | intros; type_beq_to_eq; assumption ].
          subst.
          assumption.
        Qed.

        Local Notation mk_new_evm0 evm ls
          := (fold_right
                (fun i k evm'
                 => k match PositiveMap.find i evm with
                      | Some v => PositiveMap.add i v evm'
                      | None => evm'
                      end) (fun evm' => evm')
                (List.rev ls)) (only parsing).

        Local Notation mk_new_evm' evm ps
          := (mk_new_evm0
                evm
                (PositiveSet.elements ps)) (only parsing).

        Local Notation mk_new_evm evm ps
          := (mk_new_evm' evm ps (PositiveMap.empty _)) (only parsing).

        Lemma types_match_with_Proper_evm {t p evm evm' re}
              (Hevm : forall k, PositiveSet.mem k (pattern_collect_vars p) = true -> PositiveMap.find k evm = PositiveMap.find k evm')
          : @types_match_with evm t re p <-> @types_match_with evm' t re p.
        Proof using Type.
          revert re; induction p, re; cbn [types_match_with pattern_collect_vars] in *.
          all: repeat first [ progress cbn [type_of_rawexpr] in *
                            | match goal with
                              | [ H : context[PositiveSet.mem _ (PositiveSet.union _ _)] |- _ ]
                                => setoid_rewrite PositiveSetFacts.union_b in H
                              | [ H : context[orb _ _ = true] |- _ ]
                                => setoid_rewrite Bool.orb_true_iff in H
                              end
                            | reflexivity
                            | progress split_contravariant_or
                            | progress specialize_by_assumption
                            | erewrite pattern.type.subst_eq_if_mem by eassumption
                            | match goal with
                              | [ H : _ |- _ ] => rewrite H by assumption
                              | [ |- (?x = Some ?y) <-> (?x' = Some ?y) ]
                                => cut (x = x'); [ let H := fresh in intro H; rewrite H; reflexivity | ]
                              end
                            | apply pattern.type.subst_eq_if_mem ].
        Qed.

        Lemma mem_pattern_collect_vars_types_match_with {evm t re p x}
              (H : @types_match_with evm t re p)
              (Hmem : PositiveSet.mem x (pattern_collect_vars p) = true)
          : PositiveMap.find x evm <> None.
        Proof using Type.
          revert re H; induction p; intros.
          all: repeat first [ progress cbn [pattern_collect_vars types_match_with] in *
                            | eapply pattern.type.mem_collect_vars_subst_Some_find; eassumption
                            | progress destruct_head'_and
                            | progress specialize_by_assumption
                            | assumption
                            | exfalso; assumption
                            | rewrite PositiveSetFacts.union_b, Bool.orb_true_iff in *
                            | progress destruct_head'_or
                            | break_innermost_match_hyps_step
                            | match goal with
                              | [ H : forall re, types_match_with ?evm re ?p -> _, H' : types_match_with ?evm _ ?p |- _ ] => specialize (H _ H')
                              end ].
        Qed.

        Lemma mem_collect_vars_pattern_to_type {t p k}
          : PositiveSet.mem k (pattern.type.collect_vars t) = true
            -> PositiveSet.mem k (@pattern_collect_vars t p) = true.
        Proof using Type.
          induction p.
          all: repeat first [ progress intros
                            | assumption
                            | progress cbn [pattern_collect_vars pattern.type.collect_vars] in *
                            | progress split_contravariant_or
                            | progress specialize_by_assumption
                            | rewrite PositiveSetFacts.union_b, Bool.orb_true_iff in *
                            | solve [ auto ] ].
        Qed.

        Lemma eq_subst_types_pattern_collect_vars t0 pt t evm evm0
              (evm' := mk_new_evm' evm (@pattern_collect_vars t0 pt) evm0)
              (Hty : pattern.type.subst t0 evm = Some t)
          : pattern.type.subst t0 evm' = Some t.
        Proof using Type.
          rewrite <- Hty; symmetry; apply pattern.type.subst_eq_Some_if_mem; subst evm'; intros; try congruence; [].
          rewrite pattern.base.fold_right_evar_map_find_In.
          erewrite mem_collect_vars_pattern_to_type by eassumption.
          break_innermost_match; congruence.
        Qed.

        Lemma eq_subst_types_pattern_collect_vars_empty_iff t0 pt evm (evm0:=PositiveMap.empty _)
              (evm' := mk_new_evm' evm (@pattern_collect_vars t0 pt) evm0)
          : pattern.type.subst t0 evm = pattern.type.subst t0 evm'.
        Proof using Type.
          apply pattern.type.subst_eq_if_mem; subst evm' evm0; intro.
          intro H; rewrite pattern.base.fold_right_evar_map_find_In, PositiveMap.gempty, mem_collect_vars_pattern_to_type by assumption.
          break_innermost_match; reflexivity.
        Qed.

        Lemma types_match_with_new_evm_iff {t re p evm}
              (evm' := mk_new_evm evm (pattern_collect_vars p))
          : @types_match_with evm' t re p <-> @types_match_with evm t re p.
        Proof using Type.
          clear -type_base; subst evm'; apply types_match_with_Proper_evm.
          intro k; rewrite pattern.base.fold_right_evar_map_find_In.
          intro H; rewrite H.
          rewrite PositiveMap.gempty.
          break_innermost_match; reflexivity.
        Qed.

        Lemma eq_type_of_rawexpr_of_types_match_with {t re p evm}
              (Ht : @types_match_with evm t re p)
              (Ht' : rawexpr_types_ok re (type_of_rawexpr re))
              (evm' := mk_new_evm evm (pattern_collect_vars p))
          : pattern.type.subst t evm' = Some (type_of_rawexpr re).
        Proof using Type.
          clear -Ht Ht' type_base.
          subst evm'.
          apply eq_subst_types_pattern_collect_vars.
          revert re Ht Ht'; induction p.
          all: repeat first [ progress intros
                            | progress cbn [type_of_rawexpr types_match_with pattern.type.subst rawexpr_types_ok] in *
                            | progress cbv [Option.bind] in *
                            | progress inversion_option
                            | progress specialize_by_assumption
                            | progress specialize_by eauto using rawexpr_types_ok_for_type_of_rawexpr
                            | progress subst
                            | assumption
                            | reflexivity
                            | exfalso; assumption
                            | progress destruct_head'_and
                            | break_innermost_match_hyps_step
                            | match goal with
                              | [ H : forall re, types_match_with ?evm re ?p -> _, H' : types_match_with ?evm _ ?p |- _ ]
                                => specialize (H _ H')
                              | [ H : rawexpr_types_ok _ _ |- _ ] => apply eq_type_of_rawexpr_of_rawexpr_types_ok in H
                              | [ H : context[type_of_rawexpr ?re] |- _ ]
                                => generalize dependent (type_of_rawexpr re)
                              | [ H : type.arrow _ _ = type.arrow _ _ |- _ ]
                                => inversion_clear H
                              end ].
        Qed.

        Lemma eq_type_of_rawexpr_of_types_match_with' {t re p evm}
              (Ht : @types_match_with evm t re p)
              (Ht' : rawexpr_types_ok re (type_of_rawexpr re))
              (evm' := mk_new_evm evm (pattern_collect_vars p))
          : type_of_rawexpr re = pattern.type.subst_default t evm'.
        Proof using Type.
          symmetry; eapply pattern.type.subst_Some_subst_default, eq_type_of_rawexpr_of_types_match_with; eassumption.
        Qed.

        Lemma app_transport_with_unification_resultT'_pattern_default_interp'_cps_id
              {K t p evm1 evm2 args k T k'}
          : @app_transport_with_unification_resultT'_cps
              _ t p evm1 evm2 _
              (@pattern_default_interp' K t p _ k) args
              T k'
            = (v0 <- (@app_transport_with_unification_resultT'_cps
                        _ t p evm1 evm2 _
                        (@pattern_default_interp' _ t p _ id) args
                        _ (@Some _));
                 k' (k v0))%option.
        Proof using Type.
          revert K evm1 evm2 args k T k'; induction p.
          all: repeat first [ progress intros
                            | progress cbn [pattern_default_interp' unification_resultT' app_transport_with_unification_resultT'_cps eq_rect] in *
                            | reflexivity
                            | progress inversion_option
                            | progress subst
                            | progress cbv [id Option.bind option_map] in *
                            | progress fold (@with_unification_resultT')
                            | rewrite app_lam_type_of_list
                            | break_innermost_match_step
                            | break_innermost_match_hyps_step
                            | progress rewrite_type_transport_correct
                            | progress type_beq_to_eq
                            | progress cps_id'_with_option app_transport_with_unification_resultT'_cps_id
                            | match goal with
                              | [ H : forall K evm1 evm2 args k T k', _ = _, H' : context[app_transport_with_unification_resultT'_cps (pattern_default_interp' _ ?evm1v ?kv) ?argsv ?Tv ?k'v] |- _ ]
                                => lazymatch kv with
                                   | @id _ => fail
                                   | (fun x => x) => fail
                                   | _ => idtac
                                   end;
                                   rewrite (H _ _ _ _ kv Tv k'v) in H'
                              | [ H : forall K evm1 evm2 args k T k', _ = _ |- context[app_transport_with_unification_resultT'_cps (pattern_default_interp' _ ?evm1v ?kv) ?argsv ?Tv ?k'v] ]
                                => lazymatch kv with
                                   | @id _ => fail
                                   | (fun x => x) => fail
                                   | _ => idtac
                                   end;
                                   rewrite (H _ _ _ _ kv Tv k'v)
                              end ].
        Qed.

        Local Ltac solve_side_condition_equations :=
          repeat
            repeat
            first [ progress intros
                  | assumption
                  | progress subst
                  | progress cbv [Option.bind] in *
                  | progress inversion_option
                  | match goal with
                    | [ |- ?x = ?x ] => reflexivity
                    | [ H : ?x = ?x |- _ ] => clear H
                    | [ H : expr_interp_related _ _ |- type_of_rawexpr _ = _ ] => clear H
                    | [ H : rawexpr_interp_related _ _ |- type_of_rawexpr _ = _ ] => clear H
                    | [ H : types_match_with _ _ _ |- type_of_rawexpr _ = _ ] => apply eq_type_of_rawexpr_of_types_match_with in H; [ | eapply rawexpr_types_ok_for_type_of_rawexpr; eassumption ]
                    | [ H : rawexpr_types_ok _ _ |- type_of_rawexpr _ = _ ] => apply eq_type_of_rawexpr_of_rawexpr_types_ok in H
                    | [ H : context[pattern.type.subst ?t (fold_right (fun i k evm' => k match PositiveMap.find i ?evm with _ => _ end) _ _ (PositiveMap.empty _))] |- _ ]
                      => rewrite <- eq_subst_types_pattern_collect_vars_empty_iff in H
                    | [ H : type.arrow _ _ = type.arrow _ _ |- _ = _ :> type ]
                      => inversion H; clear H
                    | [ |- type.arrow _ _ = type.arrow _ _ ] => apply f_equal2
                    | [ |- _ = _ :> type ]
                      => match goal with
                         | [ |- context[type_of_rawexpr ?re] ]
                           => is_var re; generalize dependent (type_of_rawexpr re)
                         | [ H : context[type_of_rawexpr ?re] |- _ ]
                           => is_var re; generalize dependent (type_of_rawexpr re)
                         end
                    end
                  | progress cbn [pattern.type.subst] in *
                  | progress break_match_hyps
                  | match goal with
                    | [ |- pattern.type.subst_default ?t _ = pattern.type.subst_default ?t _ ] => reflexivity
                    | [ |- ?t = pattern.type.subst_default _ _ ]
                      => is_var t; symmetry; apply pattern.type.subst_Some_subst_default
                    end ].

        Lemma interp_unify_pattern' {t re p evm res v}
              (Hre : rawexpr_interp_related re v)
              (H : @unify_pattern' t re p evm _ (@Some _) = Some res)
              (Ht : @types_match_with evm t re p)
              (Ht' : rawexpr_types_ok re (type_of_rawexpr re))
              (evm' := mk_new_evm evm (pattern_collect_vars p))
              (*Hty : type_of_rawexpr re = pattern.type.subst_default t evm'
               := eq_type_of_rawexpr_of_types_match_with' Ht Ht'*)
              (Hty : type_of_rawexpr re = pattern.type.subst_default t evm'
               := eq_type_of_rawexpr_of_types_match_with' Ht Ht')
          : exists resv : _,
              unification_resultT'_interp_related res resv
              /\ app_transport_with_unification_resultT'_cps
                   (pattern_default_interp' p evm' id) resv _ (@Some _)
                 = Some (rew Hty in v).
        Proof using pident_unify_unknown_correct pident_unify_to_typed.
          clear -pident_unify_unknown_correct pident_unify_to_typed Hre H Ht Ht' Hty.
          clearbody Hty.
          (*assert (Ht : @types_match_with evm t re p) by (eapply types_match_with_of_unify_pattern'; eassumption).*)
          assert (Hevm' : @types_match_with evm' t re p) by now apply types_match_with_new_evm_iff.
          clearbody evm'; cbv [unification_resultT'_interp_related].
          revert re res v evm' H Hre Hty Ht' Ht Hevm'; induction p; cbn [unify_pattern' related_unification_resultT' unification_resultT' rawexpr_interp_related app_transport_with_unification_resultT'_cps pattern_default_interp'] in *.
          all: repeat
                 ((unshelve
                     (repeat first [ progress intros
                                   | rewrite pident_unify_unknown_correct in *
                                   | progress cbv [Option.bind option_bind'] in *
                                   | progress cbn [fst snd rawexpr_interp_related eq_rect rawexpr_types_ok] in *
                                   | progress inversion_option
                                   | progress destruct_head'_ex
                                   | progress destruct_head'_and
                                   | progress inversion_sigma
                                   | progress subst
                                   | progress eliminate_hprop_eq
                                   | progress specialize_by_assumption
                                   | progress specialize_by eauto using rawexpr_types_ok_for_type_of_rawexpr
                                   | exfalso; assumption
                                   | assumption
                                   | match goal with
                                     | [ |- ?x = ?x ] => reflexivity
                                     | [ |- { x : _ | _ = x } ] => eexists; reflexivity
                                     | [ |- exists x, _ = x /\ _ ] => eexists; split; [ reflexivity | ]
                                     | [ |- exists x, _ /\ Some x = Some _ ] => eexists; split; [ | reflexivity ]
                                     end
                                   | progress cps_id'_with_option unify_pattern'_cps_id
                                   | progress cps_id'_with_option app_transport_with_unification_resultT'_cps_id
                                   | progress rewrite_type_transport_correct
                                   | progress type_beq_to_eq
                                   | rewrite app_lam_type_of_list
                                   | break_innermost_match_hyps_step
                                   | break_innermost_match_step
                                   | match goal with
                                     | [ |- exists x : _ * _, (_ /\ _) /\ _ ] => eexists (_, _); split; [ split; eassumption | ]
                                     | [ |- exists res, value_interp_related (value_of_rawexpr _) res ]
                                       => eexists; eapply value_of_rawexpr_interp_related; eassumption
                                     | [ |- value_interp_related (value_of_rawexpr _) _ ]
                                       => eapply value_of_rawexpr_interp_related; eassumption
                                     | [ |- Some _ = Some _ ] => apply f_equal
                                     | [ H : context[eq_type_of_rawexpr_of_types_match_with' ?H1 ?H2] |- _ ]
                                       => generalize dependent (eq_type_of_rawexpr_of_types_match_with' H1 H2)
                                     | [ H : context[rew ?pf in _] |- _ ]
                                       => tryif is_var pf then destruct pf else generalize dependent pf
                                     | [ |- context[rew ?pf in _] ]
                                       => tryif is_var pf then destruct pf else generalize dependent pf
                                     | [ |- context[rew _ in rew _ in _] ]
                                       => rewrite <- eq_trans_rew_distr
                                     | [ |- (rew ?pf1 in ?f) (rew ?pf2 in ?x) = ?f ?x ]
                                       => clear; cbv [eq_rect]
                                     end
                                   | progress cbn [type_of_rawexpr expr.interp types_match_with pattern.type.subst pattern.type.subst_default] in *
                                   | erewrite pident_unify_to_typed' with (pf:=eq_refl) by eassumption
                                   | break_match_step ltac:(fun _ => idtac)
                                   | progress fold (@with_unification_resultT') in *
                                   | match goal with
                                     | [ H : context[app_transport_with_unification_resultT'_cps (pattern_default_interp' _ ?evm1v ?kv) ?argsv ?Tv ?k'v] |- _ ]
                                       => lazymatch kv with
                                          | @id _ => fail
                                          | (fun x => x) => fail
                                          | _ => idtac
                                          end;
                                          rewrite (@app_transport_with_unification_resultT'_pattern_default_interp'_cps_id _ _ _ _ _ argsv kv Tv k'v) in H
                                     | [ |- context[app_transport_with_unification_resultT'_cps (pattern_default_interp' _ ?evm1v ?kv) ?argsv ?Tv ?k'v] ]
                                       => lazymatch kv with
                                          | @id _ => fail
                                          | (fun x => x) => fail
                                          | _ => idtac
                                          end;
                                          setoid_rewrite (@app_transport_with_unification_resultT'_pattern_default_interp'_cps_id _ _ _ _ _ argsv kv Tv k'v)
                                     | [ H : app_transport_with_unification_resultT'_cps ?f ?x _ (@Some _) = ?a,
                                             H' : app_transport_with_unification_resultT'_cps ?f' ?x _ (@Some _) = ?a'
                                         |- _ ]
                                       => unify f f';
                                          assert (a = a') by (etransitivity; (idtac + symmetry); eassumption);
                                          clear H'
                                     | [ H : context[ex _] |- _ ]
                                       => unshelve edestruct H; clear H;
                                          lazymatch goal with
                                          | [ |- rawexpr_types_ok _ _ ] => eapply rawexpr_types_ok_for_type_of_rawexpr; eassumption
                                          | [ |- unify_pattern' _ _ _ _ _ = Some _ ] => eassumption
                                          | [ |- types_match_with ?evm _ _ ] => tryif is_evar evm then idtac else assumption
                                          | [ |- rawexpr_interp_related _ _ ] => eassumption
                                          | _ => idtac
                                          end; shelve_unifiable;
                                          [ shelve.. | ]
                                     | [ |- type_of_rawexpr _ = _ ] => solve [ solve_side_condition_equations ]
                                     | [ |- types_match_with _ _ _ ] => solve [ solve_side_condition_equations ]
                                     end ]));
                  shelve_unifiable).
          1-2:match goal with
              | [ H : ?x == ?y |- ?x = ?y ]
                => apply (type.eqv_iff_eq_of_funext (fun _ _ => functional_extensionality)), H
              end.
        Qed.

        Lemma interp_unify_pattern {t re p v res}
              (Hre : rawexpr_interp_related re v)
              (Ht' : rawexpr_types_ok re (type_of_rawexpr re))
              (H : @unify_pattern t re p _ (@Some _) = Some res)
              (evm' := mk_new_evm (projT1 res) (pattern_collect_vars p))
          : exists resv,
            unification_resultT_interp_related res resv
            /\ exists Hty, (app_with_unification_resultT_cps (@pattern_default_interp t p) resv _ (@Some _) = Some (existT (fun evm => type.interp base.interp (pattern.type.subst_default t evm)) evm' (rew Hty in v))).
        Proof using pident_unify_unknown_correct pident_unify_to_typed.
          subst evm'; cbv [unify_pattern unification_resultT_interp_related unification_resultT related_unification_resultT app_with_unification_resultT_cps pattern_default_interp] in *.
          repeat
            (unshelve
               (repeat first [ progress cbv [Option.bind related_sigT_by_eq] in *
                             | progress cbn [projT1 projT2 eq_rect] in *
                             | progress destruct_head'_ex
                             | progress destruct_head'_and
                             | progress inversion_option
                             | progress subst
                             | exfalso; assumption
                             | eassumption
                             | match goal with
                               | [ H : unify_types _ _ _ _ = Some _ |- _ ] => apply unify_types_match_with in H
                               | [ H : unify_pattern' _ _ _ _ _ = Some _, H'' : rawexpr_types_ok _ _ |- _ ]
                                 => let T := type of H in
                                    unique pose proof (H : id T) (* save an extra copy *);
                                    epose proof (interp_unify_pattern' _ H _ H'')
                               | [ H : pattern.type.app_forall_vars (pattern.type.lam_forall_vars _) _ = Some _ |- _ ] => pose proof (pattern.type.app_forall_vars_lam_forall_vars H); clear H
                               | [ H : pattern.type.app_forall_vars (pattern.type.lam_forall_vars _) _ = None |- None = Some _ ]
                                 => exfalso; revert H;
                                    lazymatch goal with
                                    | [ |- ?x = None -> False ]
                                      => change (x <> None)
                                    end;
                                    rewrite app_lam_forall_vars_not_None_iff
                               end
                             | progress cps_id'_with_option unify_types_cps_id
                             | progress cps_id'_with_option unify_pattern'_cps_id
                             | progress cps_id'_with_option app_transport_with_unification_resultT'_cps_id
                             | break_innermost_match_hyps_step
                             | break_innermost_match_step
                             | match goal with
                               | [ |- exists x : sigT _, _ ] => eexists (existT _ _ _)
                               | [ |- { pf : _ = _ | _ } ] => exists eq_refl
                               | [ |- { pf : _ = _ & _ } ] => exists eq_refl
                               | [ |- _ /\ _ ] => split
                               | [ |- Some _ = Some _ ] => apply f_equal
                               | [ |- existT _ _ _ = existT _ _ _ ] => apply Sigma.path_sigT_uncurried
                               | [ H : forall x : rawexpr_types_ok ?a ?b, _, H' : rawexpr_types_ok ?a ?b |- _ ] => specialize (H H')
                               end
                             | break_match_step ltac:(fun _ => idtac)
                             | reflexivity
                             | progress intros
                             | eapply mem_pattern_collect_vars_types_match_with; eassumption
                             | exists (eq_type_of_rawexpr_of_types_match_with' ltac:(eassumption) ltac:(eassumption))
                             | match goal with
                               | [ |- rew ?pf in _ = rew ?pf' in _ ]
                                 => cut (pf = pf'); generalize pf pf'; [ intros; subst; reflexivity | clear; cbv beta zeta; intros ];
                                    lazymatch goal with
                                    | [ |- ?a = ?b :> (?x = ?y) ]
                                      => generalize dependent x; generalize dependent y; intros; subst; eliminate_hprop_eq; reflexivity
                                    end
                               end ])).
          (* For 8.7 compatibility *)
          Grab Existential Variables.
          all: assumption.
        Qed.

        Lemma interp_maybe_do_again
              (do_again : forall t : base.type, @expr.expr base.type ident value t -> UnderLets (expr t))
              (Hdo_again : forall t e v,
                  expr.interp_related_gen ident_interp (fun t => value_interp_related) e v
                  -> UnderLets_interp_related (do_again t e) v)
              {should_do_again : bool} {t e v}
              (He : (if should_do_again return @expr.expr _ _ (if should_do_again then _ else _) _ -> _
                     then expr.interp_related_gen ident_interp (fun t => value_interp_related)
                     else expr_interp_related) e v)
          : UnderLets_interp_related (@maybe_do_again _ _ do_again should_do_again t e) v.
        Proof using Type.
          cbv [maybe_do_again]; break_innermost_match; [ apply Hdo_again | cbn [UnderLets.interp_related] ];
            assumption.
        Qed.

        Lemma interp_rewrite_with_rule
              (do_again : forall t : base.type, @expr.expr base.type ident value t -> UnderLets (expr t))
              (Hdo_again : forall t e v,
                  expr.interp_related_gen ident_interp (fun t => value_interp_related) e v
                  -> UnderLets_interp_related (do_again t e) v)
              (rewr : rewrite_ruleT)
              (Hrewr : rewrite_rule_data_interp_goodT (projT2 rewr))
              t re v1 v2
              (Ht : t = type_of_rawexpr re)
              (Ht' : rawexpr_types_ok re (type_of_rawexpr re))
          : @rewrite_with_rule do_again t re rewr = Some v1
            -> rawexpr_interp_related re (rew Ht in v2)
            -> UnderLets_interp_related v1 v2.
        Proof using pident_unify_to_typed pident_unify_unknown_correct.
          destruct rewr as [p r].
          cbv [rewrite_with_rule].
          repeat first [ match goal with
                         | [ |- Option.bind ?x _ = Some _ -> _ ]
                           => destruct x eqn:?; cbn [Option.bind]; [ | intros; solve [ inversion_option ] ]
                         end
                       | progress cps_id'_with_option unify_pattern_cps_id
                       | progress cps_id'_with_option app_with_unification_resultT_cps_id ].
          repeat first [ break_match_step ltac:(fun v => match v with Sumbool.sumbool_of_bool _ => idtac end)
                       | progress rewrite_type_transport_correct
                       | progress type_beq_to_eq
                       | progress cbv [option_bind'] in *
                       | progress cbn [Option.bind projT1 projT2 UnderLets.interp eq_rect UnderLets_interp_related] in *
                       | progress destruct_head'_sigT
                       | progress destruct_head'_sig
                       | progress inversion_option
                       | progress subst
                       | solve [ intros; inversion_option ]
                       | rewrite UnderLets_interp_related_splice_iff
                       | match goal with
                         | [ H : Option.bind ?x _ = Some _ |- _ ]
                           => destruct x eqn:?; cbn [Option.bind] in H; [ | solve [ inversion_option ] ]
                         | [ |- expr.interp _ (UnderLets.interp _ (maybe_do_again _ _ _ _)) == _ ]
                           => apply interp_maybe_do_again_gen; [ assumption | ]
                         | [ |- context[rew ?pf in _] ] => is_var pf; destruct pf
                         end ].
          repeat first [ progress destruct_head'_ex
                       | progress destruct_head'_sig
                       | progress destruct_head'_and
                       | exfalso; assumption
                       | progress inversion_option
                       | progress subst
                       | progress cbv [related_sigT_by_eq] in *
                       | progress cbn [projT1 projT2 eq_rect] in *
                       | match goal with
                         | [ H : unify_pattern _ _ _ _ = Some _ |- _ ] => eapply interp_unify_pattern in H; [ | eassumption | eassumption ]
                         | [ H : unification_resultT_interp_related _ _, Hrewr : rewrite_rule_data_interp_goodT _ |- _ ]
                           => specialize (Hrewr _ _ H)
                         | [ H : option_eq _ ?x ?y, H' : ?x' = Some _ |- _ ]
                           => change x with x' in H; rewrite H' in H;
                              destruct y eqn:?; cbn [option_eq] in H
                         | [ H : ?x = Some _, H' : context[?x] |- _ ] => rewrite H in H'
                         | [ H : app_with_unification_resultT_cps _ _ _ (@Some _) = Some (existT _ ?evm _) |- _ ]
                           => is_var evm;
                              let H' := fresh in
                              pose proof (projT1_app_with_unification_resultT _ H) as H';
                              cbn [projT1] in H'; subst evm
                         end
                       | progress cbv [deep_rewrite_ruleTP_gen_good_relation] in *
                       | unshelve (eapply UnderLets.splice_interp_related_of_ex; eexists (fun x => rew _ in x), _; repeat apply conj;
                                   [ eassumption | intros | ]);
                         [ etransitivity; eassumption | .. ]
                       | match goal with
                         | [ H : ?R ?xv ?v
                             |- UnderLets_interp_related (fv <-- maybe_do_again _ _ _ ((rew _ in fun x => x) ?xv); _) _ ]
                           => unshelve (eapply UnderLets.splice_interp_related_of_ex;
                                        eexists (fun x => rew _ in x), (rew _ in v); repeat apply conj;
                                        [ eapply interp_maybe_do_again; try eassumption | | ])
                         end ].
          all: repeat first [ assumption
                            | progress intros
                            | reflexivity
                            | progress eliminate_hprop_eq
                            | progress cbn [UnderLets.interp_related eq_rect] in *
                            | match goal with
                              | [ |- context[rew _ in rew _ in _] ]
                                => rewrite <- eq_trans_rew_distr
                              | [ |- context[rew ?pf in _] ]
                                => tryif is_var pf then destruct pf else generalize pf
                              end ].
        Qed.

        Lemma interp_eval_rewrite_rules
              (do_again : forall t : base.type, @expr.expr base.type ident value t -> UnderLets (expr t))
              (d : decision_tree)
              (rew_rules : rewrite_rulesT)
              (re : rawexpr) v
              (Hre : rawexpr_types_ok re (type_of_rawexpr re))
              (res := @eval_rewrite_rules do_again d rew_rules re)
              (Hdo_again : forall t e v,
                  expr.interp_related_gen ident_interp (fun t => value_interp_related) e v
                  -> UnderLets_interp_related (do_again t e) v)
              (Hr : rawexpr_interp_related re v)
              (Hrew_rules : rewrite_rules_interp_goodT rew_rules)
          : UnderLets_interp_related res v.
        Proof using raw_pident_to_typed_invert_bind_args invert_bind_args_unknown_correct pident_unify_unknown_correct pident_unify_to_typed.
          subst res; cbv [eval_rewrite_rules].
          refine (let H := eval_decision_tree_correct d [re] _ in _).
          destruct H as [H| [? [? [H ?] ] ] ]; rewrite H; cbn [Option.sequence Option.sequence_return UnderLets_interp_related];
            [ now apply expr_of_rawexpr_interp_related | ]; clear H.
          inversion_head' eqlistA.
          unfold Option.bind at 1.
          break_innermost_match_step; [ | cbn [Option.sequence_return UnderLets_interp_related]; now apply expr_of_rawexpr_interp_related ].
          cbn [Option.bind Option.sequence Option.sequence_return UnderLets_interp_related].
          match goal with
          | [ |- ?R (Option.sequence_return ?x ?y) _ ]
            => destruct x eqn:Hinterp
          end; cbn [Option.sequence_return UnderLets.interp]; [ | now apply expr_of_rawexpr_interp_related ].
          unshelve (eapply interp_rewrite_with_rule; [ | | | eassumption | ]; try eassumption).
          { apply eq_type_of_rawexpr_equiv; assumption. }
          { eapply Hrew_rules, nth_error_In; rewrite <- sigT_eta; eassumption. }
          { erewrite <- rawexpr_types_ok_iff_of_rawexpr_equiv, <- eq_type_of_rawexpr_equiv by eassumption; assumption. }
          { apply rawexpr_interp_related_Proper_rawexpr_equiv; assumption. }
        Qed.

        Lemma interp_assemble_identifier_rewriters'
              (do_again : forall t : base.type, @expr.expr base.type ident value t -> UnderLets (expr t))
              (dt : decision_tree)
              (rew_rules : rewrite_rulesT)
              t re K
              (res := @assemble_identifier_rewriters' dt rew_rules do_again t re K)
              (Hre : rawexpr_types_ok re (type_of_rawexpr re))
              (Ht : type_of_rawexpr re = t)
              v
              (HK : K = (fun P v => rew [P] Ht in v))(*
                      /\ rew pf in value_of_rawexpr re = ev })*)
              (Hdo_again : forall t e v,
                  expr.interp_related_gen ident_interp (fun t => value_interp_related) e v
                  -> UnderLets_interp_related (do_again t e) v)
              (Hrew_rules : rewrite_rules_interp_goodT rew_rules)
              (Hr : rawexpr_interp_related re v)
          : value_interp_related res (rew Ht in v).
        Proof using raw_pident_to_typed_invert_bind_args_type raw_pident_to_typed_invert_bind_args invert_bind_args_unknown_correct pident_unify_unknown_correct pident_unify_to_typed.
          subst K res.
          revert dependent re; induction t as [t|s IHs d IHd]; cbn [assemble_identifier_rewriters' value'_interp];
            intros; fold (@type.interp).
          { cbn [value_interp_related].
            destruct Ht; cbn [eq_rect].
            apply interp_eval_rewrite_rules; [ | exact Hdo_again | | ]; assumption. }
          { cbn [value_interp_related].
            intros x1 x2 Hx.
            lazymatch goal with
            | [ |- context[assemble_identifier_rewriters' _ _ _ _ ?re ?K] ] => apply (IHd re eq_refl); clear IHd
            end.
            { cbv [rValueOrExpr2]; destruct s; cbn.
              all: repeat apply conj; try reflexivity.
              all: repeat match goal with
                          | [ H : _ = _ |- _ ] => revert H
                          | [ H : rawexpr_types_ok _ _ |- _ ] => revert H
                          end.
              all: clear.
              all: generalize dependent (type_of_rawexpr re); intros; subst; assumption. }
            cbn [rawexpr_interp_related type.interp type_of_rawexpr].
            do 2 eexists.
            exists (eq_sym Ht).
            unshelve eexists.
            { clear; cbv [rValueOrExpr2 type_of_rawexpr]; destruct s; reflexivity. }
            repeat apply conj.
            all: repeat first [ instantiate (1:=ltac:(eassumption))
                              | match goal with
                                | [ |- expr_interp_related (rew [?P] ?H in ?v) ?ev ]
                                  => is_evar ev;
                                     refine (_ : expr_interp_related (rew [P] H in v) (rew [type.interp base.interp] H in _))
                                end
                              | assumption
                              | progress cbv [eq_sym eq_rect]
                              | break_innermost_match_step
                              | reflexivity
                              | apply expr_of_rawexpr_interp_related
                              | apply reify_interp_related ]. }
        Qed.

        Lemma interp_assemble_identifier_rewriters
              (do_again : forall t : base.type, @expr.expr base.type ident value t -> UnderLets (expr t))
              (d : decision_tree)
              (rew_rules : rewrite_rulesT)
              t idc v
              (res := @assemble_identifier_rewriters d rew_rules do_again t idc)
              (Hdo_again : forall t e v,
                  expr.interp_related_gen ident_interp (fun t => value_interp_related) e v
                  -> UnderLets_interp_related (do_again t e) v)
              (Hrew_rules : rewrite_rules_interp_goodT rew_rules)
              (Hv : ident_interp t idc == v)
          : value_interp_related res v.
        Proof using eta_ident_cps_correct raw_pident_to_typed_invert_bind_args_type raw_pident_to_typed_invert_bind_args invert_bind_args_unknown_correct pident_unify_unknown_correct pident_unify_to_typed.
          subst res; cbv [assemble_identifier_rewriters].
          rewrite eta_ident_cps_correct.
          match goal with
          | [ |- ?R (assemble_identifier_rewriters' ?d ?rew_rules ?do_again ?t ?re' ?K) _ ]
            => apply interp_assemble_identifier_rewriters' with (re:=re') (Ht:=eq_refl)
          end.
          all: cbn [rawexpr_interp_related expr.interp rawexpr_types_ok type_of_rawexpr].
          all: try solve [ reflexivity
                         | assumption
                         | auto ].
        Qed.
      End with_interp.

      Section with_cast.
        Context (cast_outside_of_range : ZRange.zrange -> Z -> Z).
        Local Notation var := (type.interp base.interp).
        Local Notation ident_interp := (@ident.gen_interp cast_outside_of_range).
        Local Notation value_interp_related := (@value_interp_related ident (@ident_interp)).
        Local Notation expr_interp_related := (@expr.interp_related _ ident _ (@ident_interp)).

        Section with_rewrite_head.
          Context (rewrite_head : forall t (idc : ident t), value_with_lets t)
                  (interp_rewrite_head : forall t idc v, ident_interp idc == v -> value_interp_related (rewrite_head t idc) v).

          Lemma interp_rewrite_bottomup {t e v}
                (He : expr.interp_related_gen (@ident_interp) (fun t => value_interp_related) e v)
            : value_interp_related (@rewrite_bottomup var rewrite_head t e) v.
          Proof using interp_rewrite_head.
            induction e; cbn [rewrite_bottomup value_interp_related expr.interp_related_gen] in *; auto.
            all: repeat first [ apply interp_Base_value
                              | eassumption
                              | progress cbv beta
                              | progress intros
                              | progress destruct_head'_ex
                              | progress destruct_head'_and
                              | progress subst
                              | match goal with
                                | [ IH : forall v, expr.interp_related_gen _ _ ?e v -> _, H' : expr.interp_related_gen _ _ ?e _ |- _ ]
                                  => specialize (IH _ H')
                                end
                              | apply reflect_interp_related
                              | eapply interp_splice_value_with_lets_of_ex;
                                do 2 eexists; repeat apply conj; [ eassumption | | reflexivity ]
                              | eapply @interp_splice_under_lets_with_value_of_ex with (R:=expr_interp_related);
                                do 2 eexists; repeat apply conj
                              | apply interp_reify_and_let_binds
                              | apply UnderLets.reify_and_let_binds_base_interp_related
                              | match goal with
                                | [ H : _ |- _ ] => eapply H; clear H
                                | [ |- ?f ?x = ?f ?y ] => is_evar x; reflexivity
                                | [ |- ?x = ?x ] => reflexivity
                                end ].
          Qed.
        End with_rewrite_head.

        Local Notation nbe := (@rewrite_bottomup var (fun t idc => reflect (expr.Ident idc))).

        Lemma interp_nbe {t e v}
              (He : expr.interp_related_gen (@ident_interp) (fun t => value_interp_related) e v)
          : value_interp_related (@nbe t e) v.
        Proof using Type.
          eapply interp_rewrite_bottomup; try eassumption.
          intros; apply reflect_interp_related; cbv [expr.interp_related]; cbn [expr.interp_related_gen]; assumption.
        Qed.

        Lemma interp_repeat_rewrite
              {rewrite_head fuel t e v}
              (retT := value_interp_related (@repeat_rewrite _ rewrite_head fuel t e) v)
              (Hrewrite_head
               : forall do_again
                        (Hdo_again : forall t e v,
                            expr.interp_related_gen (@ident_interp) (fun t => value_interp_related) e v
                            -> UnderLets.interp_related (@ident_interp) (expr.interp_related (@ident_interp)) (do_again t e) v)
                        t idc v,
                  ident_interp idc == v
                  -> value_interp_related (@rewrite_head do_again t idc) v)
              (He : expr.interp_related_gen (@ident_interp) (fun t => value_interp_related) e v)
          : retT.
        Proof using Type.
          subst retT.
          revert rewrite_head t e v Hrewrite_head He.
          induction fuel as [|fuel IH]; cbn [repeat_rewrite]; intros;
            apply interp_rewrite_bottomup; auto; intros;
              apply Hrewrite_head; auto; intros.
          { refine (@interp_nbe (type.base _) _ _ _); assumption. }
          { refine (IH _ (type.base _) _ _ _ _); auto. }
        Qed.

        Lemma interp_related_rewrite
              {rewrite_head fuel t e v}
              (retT := expr.interp_related (@ident_interp) (@rewrite _ rewrite_head fuel t e) v)
              (Hrewrite_head
               : forall do_again
                        (Hdo_again : forall t e v,
                            expr.interp_related_gen (@ident_interp) (fun t => value_interp_related) e v
                            -> UnderLets.interp_related (@ident_interp) (expr.interp_related (@ident_interp)) (do_again t e) v)
                        t idc v,
                  ident_interp idc == v
                  -> value_interp_related (@rewrite_head do_again t idc) v)
              (He : expr.interp_related_gen (@ident_interp) (fun t => value_interp_related) e v)
          : retT.
        Proof using Type.
          subst retT; cbv [rewrite].
          apply reify_interp_related, interp_repeat_rewrite; auto.
        Qed.

        Lemma interp_rewrite
              {rewrite_head fuel G t e1 e2}
              (retT := expr.interp (@ident_interp) (@rewrite _ rewrite_head fuel t e1) == expr.interp (@ident_interp) e2)
              (Hrewrite_head
               : forall do_again
                        (Hdo_again : forall t e v,
                            expr.interp_related_gen (@ident_interp) (fun t => value_interp_related) e v
                            -> UnderLets.interp_related (@ident_interp) (expr.interp_related (@ident_interp)) (do_again t e) v)
                        t idc v,
                  ident_interp idc == v
                  -> value_interp_related (@rewrite_head do_again t idc) v)
              (HG : forall t v1 v2, List.In (existT _ t (v1, v2)) G -> value_interp_related v1 v2)
              (Hwf : expr.wf G e1 e2)
          : retT.
        Proof using Type.
          apply expr.eqv_of_interp_related, interp_related_rewrite; try assumption; [].
          eapply expr.interp_related_gen_of_wf; eassumption.
        Qed.

        Lemma InterpRewrite
              {rewrite_head fuel t e}
              (retT := expr.Interp (@ident_interp) (@Rewrite rewrite_head fuel t e) == expr.Interp (@ident_interp) e)
              (Hrewrite_head
               : forall do_again
                        (Hdo_again : forall t e v,
                            expr.interp_related_gen (@ident_interp) (fun t => value_interp_related) e v
                            -> UnderLets.interp_related (@ident_interp) (expr.interp_related (@ident_interp)) (do_again t e) v)
                        t idc v,
                  ident_interp idc == v
                  -> value_interp_related (@rewrite_head _ do_again t idc) v)
              (Hwf : expr.Wf e)
          : retT.
        Proof using Type.
          subst retT; cbv [Rewrite expr.Interp].
          eapply interp_rewrite; eauto; cbn [List.In]; tauto.
        Qed.
      End with_cast.
    End Compile.
  End RewriteRules.
End Compilers.