aboutsummaryrefslogtreecommitdiff
path: root/src/Experiments/NewPipeline/Rewriter.v
blob: dd898bc3e1e2fa37d635dd86946496a414142e96 (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
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
Require Import Coq.ZArith.ZArith.
Require Import Crypto.Util.ListUtil Coq.Lists.List Crypto.Util.ListUtil.FoldBool.
Require Import Crypto.Util.Option.
Require Import Crypto.Util.OptionList.
Require Import Crypto.Util.CPSNotations.
Require Crypto.Util.PrimitiveProd.
Require Crypto.Util.PrimitiveHList.
Require Import Crypto.Experiments.NewPipeline.Language.
Require Import Crypto.Experiments.NewPipeline.UnderLets.
Require Import Crypto.Experiments.NewPipeline.GENERATEDIdentifiersWithoutTypes.
Require Import Crypto.Util.LetIn.
Require Import Crypto.Util.Notations.
Import ListNotations. Local Open Scope bool_scope. Local Open Scope Z_scope.

Module Compilers.
  Export Language.Compilers.
  Export UnderLets.Compilers.
  Export GENERATEDIdentifiersWithoutTypes.Compilers.
  Import invert_expr.

  Module pattern.
    Export GENERATEDIdentifiersWithoutTypes.Compilers.pattern.

    Module base.
      Local Notation einterp := type.interp.
      Module type.
        Inductive type := any | type_base (t : Compilers.base.type.base) | prod (A B : type) | list (A : type).
      End type.
      Notation type := type.type.

      Module Notations.
        Global Coercion type.type_base : Compilers.base.type.base >-> type.type.
        Bind Scope pbtype_scope with type.type.
        (*Bind Scope ptype_scope with Compilers.type.type type.type.*) (* COQBUG(https://github.com/coq/coq/issues/7699) *)
        Delimit Scope ptype_scope with ptype.
        Delimit Scope pbtype_scope with pbtype.
        Notation "A * B" := (type.prod A%ptype B%ptype) : ptype_scope.
        Notation "A * B" := (type.prod A%pbtype B%pbtype) : pbtype_scope.
        Notation "()" := (type.type_base base.type.unit) : pbtype_scope.
        Notation "()" := (type.base (type.type_base base.type.unit)) : ptype_scope.
        Notation "A -> B" := (type.arrow A%ptype B%ptype) : ptype_scope.
        Notation "??" := type.any : pbtype_scope.
        Notation "??" := (type.base type.any) : ptype_scope.
      End Notations.
    End base.
    Notation type := (type.type base.type).
    Export base.Notations.

    Inductive pattern {ident : Type} :=
    | Wildcard (t : type)
    | Ident (idc : ident)
    | App (f x : pattern).

    Global Arguments Wildcard {ident%type} t%ptype.

    Notation ident := ident.ident.

    Module Export Notations.
      Export base.Notations.
      Delimit Scope pattern_scope with pattern.
      Bind Scope pattern_scope with pattern.
      Local Open Scope pattern_scope.
      Notation "#?()" := (Ident ident.LiteralUnit) : pattern_scope.
      Notation "#?N" := (Ident ident.LiteralNat) : pattern_scope.
      Notation "#?ℕ" := (Ident ident.LiteralNat) : pattern_scope.
      Notation "#?Z" := (Ident ident.LiteralZ) : pattern_scope.
      Notation "#?ℤ" := (Ident ident.LiteralZ) : pattern_scope.
      Notation "#?B" := (Ident ident.LiteralBool) : pattern_scope.
      Notation "#?𝔹" := (Ident ident.LiteralBool) : pattern_scope.
      Notation "??{ t }" := (Wildcard t) (format "??{ t }") : pattern_scope.
      Notation "??" := (??{??})%pattern : pattern_scope.
      Notation "# idc" := (Ident idc) : pattern_scope.
      Infix "@" := App : pattern_scope.
      Notation "( x , y , .. , z )" := (#ident.pair @ .. (#ident.pair @ x @ y) .. @ z) : pattern_scope.
      Notation "x :: xs" := (#ident.cons @ x @ xs) : pattern_scope.
      Notation "xs ++ ys" := (#ident.List_app @ xs @ ys) : pattern_scope.
      Notation "[ ]" := (#ident.nil) : pattern_scope.
      Notation "[ x ]" := (x :: []) : pattern_scope.
      Notation "[ x ; y ; .. ; z ]" :=  (x :: (y :: .. (z :: []) ..)) : pattern_scope.
      Notation "x - y" := (#ident.Z_sub @ x @ y) : pattern_scope.
      Notation "x + y" := (#ident.Z_add @ x @ y) : pattern_scope.
      Notation "x / y" := (#ident.Z_div @ x @ y) : pattern_scope.
      Notation "x * y" := (#ident.Z_mul @ x @ y) : pattern_scope.
      Notation "x >> y" := (#ident.Z_shiftr @ x @ y) : pattern_scope.
      Notation "x << y" := (#ident.Z_shiftl @ x @ y) : pattern_scope.
      Notation "x &' y" := (#ident.Z_land @ x @ y) : pattern_scope.
      Notation "x 'mod' y" := (#ident.Z_modulo @ x @ y)%pattern : pattern_scope.
      Notation "- x" := (#ident.Z_opp @ x) : pattern_scope.
    End Notations.
  End pattern.
  Export pattern.Notations.
  Notation pattern := (@pattern.pattern pattern.ident).

  Module RewriteRules.
    Module Import AnyExpr.
      Record anyexpr {base_type} {ident var : type.type base_type -> Type}
        := wrap { anyexpr_ty : base_type ; unwrap :> @expr.expr base_type ident var (type.base anyexpr_ty) }.
      Global Arguments wrap {base_type ident var _} _.
    End AnyExpr.

    Module Compile.
      Section with_var0.
        Context {base_type} {ident var : type.type base_type -> Type}.
        Local Notation type := (type.type base_type).
        Local Notation expr := (@expr.expr base_type ident var).
        Local Notation UnderLets := (@UnderLets.UnderLets base_type ident var).
        Let type_base (t : base_type) : type := type.base t.
        Coercion type_base : base_type >-> type.

        Fixpoint value' (with_lets : bool) (t : type)
          := match t with
             | type.base t
               => if with_lets then UnderLets (expr t) else expr t
             | type.arrow s d
               => value' false s -> value' true d
             end.
        Definition value := value' false.
        Definition value_with_lets := value' true.

        Definition Base_value {t} : value t -> value_with_lets t
          := match t with
             | type.base t => fun v => UnderLets.Base v
             | type.arrow _ _ => fun v => v
             end.

        Fixpoint splice_under_lets_with_value {T t} (x : UnderLets T) : (T -> value_with_lets t) -> value_with_lets t
          := match t return (T -> value_with_lets t) -> value_with_lets t with
             | type.arrow s d
               => fun k v => @splice_under_lets_with_value T d x (fun x' => k x' v)
             | type.base _ => fun k => x <-- x; k x
             end%under_lets.
        Local Notation "x <--- v ; f" := (splice_under_lets_with_value x (fun v => f%under_lets)) : under_lets_scope.
        Definition splice_value_with_lets {t t'} : value_with_lets t -> (value t -> value_with_lets t') -> value_with_lets t'
          := match t return value_with_lets t -> (value t -> value_with_lets t') -> value_with_lets t' with
             | type.arrow _ _
               => fun e k => k e
             | type.base _ => fun e k => e <--- e; k e
             end%under_lets.
      End with_var0.
      Section with_var.
        Context {ident var : type.type base.type -> Type}
                {pident : Type}
                (*(invert_Literal_cps : forall t, ident t ~> option (type.interp base.interp t))*)
                (*(beq_typed : forall t (X : pident) (Y : ident t), bool)*)
                (full_types : pident -> Type)
                (invert_bind_args : forall t (idc : ident t) (pidc : pident), option (full_types pidc))
                (type_of_pident : forall (pidc : pident), full_types pidc -> type.type base.type)
                (pident_to_typed : forall (pidc : pident) (args : full_types pidc), ident (type_of_pident pidc args))
                (eta_ident_cps : forall {T : type.type base.type -> Type} {t} (idc : ident t)
                                        (f : forall t', ident t' -> T t'),
                    T t)
                (of_typed_ident : forall {t}, ident t -> pident)
                (arg_types : pident -> option Type)
                (bind_args : forall {t} (idc : ident t), match arg_types (of_typed_ident idc) return Type with Some t => t | None => unit end)
                (pident_beq : pident -> pident -> bool)
                (try_make_transport_ident_cps : forall (P : pident -> Type) (idc1 idc2 : pident), ~> option (P idc1 -> P idc2)).
        Local Notation type := (type.type base.type).
        Local Notation expr := (@expr.expr base.type ident var).
        Local Notation anyexpr := (@anyexpr 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).
        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.

        Fixpoint reify {with_lets} {t} : value' with_lets t -> expr t
          := match t, with_lets return value' with_lets t -> expr t with
             | type.base _, false => fun v => v
             | type.base _, true => fun v => UnderLets.to_expr v
             | type.arrow s d, _
               => fun f
                  => λ x , @reify _ d (f (@reflect _ s ($x)))
             end%expr%under_lets%cps
        with reflect {with_lets} {t} : expr t -> value' with_lets t
             := match t, with_lets return expr t -> value' with_lets t with
                | type.base _, false => fun v => v
                | type.base _, true => fun v => UnderLets.Base v
                | type.arrow s d, _
                  => fun f (x : value' _ _) => @reflect _ d (f @ (@reify _ s x))
                end%expr%under_lets.

        Definition reify_and_let_binds_cps {with_lets} {t} : value' with_lets t -> forall T, (expr t -> UnderLets T) -> UnderLets T
          := match t, with_lets return value' with_lets t -> forall T, (expr t -> UnderLets T) -> UnderLets T with
             | type.base _, false => reify_and_let_binds_base_cps _
             | type.base _, true => fun v => fun T k => v' <-- v; reify_and_let_binds_base_cps _ v' T k
             | type.arrow s d, _
               => fun f T k => k (reify f)
             end%expr%under_lets%cps.

        Inductive rawexpr : Type :=
        | rIdent {t} (idc : ident t) {t'} (alt : expr t')
        | rApp (f x : rawexpr) {t} (alt : expr t)
        | rExpr {t} (e : expr t)
        | rValue {t} (e : value t).

        Definition type_of_rawexpr (e : rawexpr) : type
          := match e with
             | rIdent t idc t' alt => t'
             | rApp f x t alt => t
             | rExpr t e => t
             | rValue t e => t
             end.
        Definition expr_of_rawexpr (e : rawexpr) : expr (type_of_rawexpr e)
          := match e with
             | rIdent t idc t' alt => alt
             | rApp f x t alt => alt
             | rExpr t e => e
             | rValue t e => reify e
             end.
        Definition value_of_rawexpr (e : rawexpr) : value (type_of_rawexpr e)
          := Eval cbv [expr_of_rawexpr] in
              match e with
              | rValue t e => e
              | e => reflect (expr_of_rawexpr e)
              end.
        Definition rValueOrExpr {t} : value t -> rawexpr
          := match t with
             | type.base _ => @rExpr _
             | type.arrow _ _ => @rValue _
             end.
        Definition rValueOrExpr2 {t} : value t -> expr t -> rawexpr
          := match t with
             | type.base _ => fun v e => @rExpr _ e
             | type.arrow _ _ => fun v e => @rValue _ v
             end.

        Definition try_rExpr_cps {T t} (k : option rawexpr -> T) : expr t -> T
          := match t with
             | type.base _ => fun e => k (Some (rExpr e))
             | type.arrow _ _ => fun _ => k None
             end.

        Definition reveal_rawexpr_cps (e : rawexpr) : ~> rawexpr
          := fun T k
             => match e with
               | rExpr _ e as r
               | rValue (type.base _) e as r
                 => match e with
                   | expr.Ident t idc => k (rIdent idc e)
                   | expr.App s d f x => k (rApp (rExpr f) (rExpr x) e)
                   | _ => k r
                   end
               | e' => k e'
               end.

        Inductive quant_type := qforall | qexists.

        (* p for pattern *)
        Fixpoint pbase_type_interp_cps (quant : quant_type) (t : pattern.base.type) (K : base.type -> Type) : Type
          := match t with
             | pattern.base.type.any
               => match quant with
                 | qforall => forall t : base.type, K t
                 | qexists => { t : base.type & K t }
                 end
             | pattern.base.type.type_base t => K t
             | pattern.base.type.prod A B
               => @pbase_type_interp_cps
                   quant A
                   (fun A'
                    => @pbase_type_interp_cps
                        quant B (fun B' => K (A' * B')%etype))
             | pattern.base.type.list A
               => @pbase_type_interp_cps
                   quant A (fun A' => K (base.type.list A'))
             end.

        Fixpoint ptype_interp_cps (quant : quant_type) (t : ptype) (K : type -> Type) {struct t} : Type
          := match t with
             | type.base t
               => pbase_type_interp_cps quant t (fun t => K (type.base t))
             | type.arrow s d
               => @ptype_interp_cps
                   quant s
                   (fun s => @ptype_interp_cps
                            quant d (fun d => K (type.arrow s d)))
             end.

        Definition ptype_interp (quant : quant_type) (t : ptype) (K : Type -> Type) : Type
          := ptype_interp_cps quant t (fun t => K (value t)).

        Fixpoint binding_dataT (p : pattern) : Type
          := match p return Type with
             | pattern.Wildcard t => ptype_interp qexists t id
             | pattern.Ident idc => match arg_types idc return Type with
                                   | Some t => t
                                   | None => unit
                                   end
             | pattern.App f x => binding_dataT f * binding_dataT x
             end%type.

        Fixpoint bind_base_cps {t1 t2}
                 (K : base.type -> Type)
                 (v : K t2)
                 {struct t1}
          : ~> option (pbase_type_interp_cps qexists t1 K)
          := match t1 return ~> option (pbase_type_interp_cps qexists t1 K) with
             | pattern.base.type.any
               => (return (Some (existT K t2 v)))
             | pattern.base.type.type_base t
               => (tr <-- base.try_make_transport_cps _ _ _;
                  return (Some (tr v)))
             | pattern.base.type.prod A B
               => fun T k
                 => match t2 return K t2 -> T with
                   | base.type.prod A' B'
                     => fun v
                       => (v' <-- @bind_base_cps B B' (fun B' => K (A' * B')%etype) v;
                            v'' <-- @bind_base_cps A A' (fun A' => pbase_type_interp_cps qexists B (fun B' => K (A' * B')%etype)) v';
                          return (Some v''))
                           T k
                   | _ => fun _ => k None
                   end v
             | pattern.base.type.list A
               => fun T k
                 => match t2 return K t2 -> T with
                   | base.type.list A'
                     => fun v => @bind_base_cps A A' (fun A' => K (base.type.list A')) v T k
                   | _ => fun _ => k None
                   end v
             end%cps.

        Fixpoint bind_value_cps {t1 t2}
                 (K : type -> Type)
                 (v : K t2)
                 {struct t1}
          : ~> option (ptype_interp_cps qexists t1 K)
          := match t1 return ~> option (ptype_interp_cps qexists t1 K) with
             | type.base t1
               => fun T k
                 => match t2 return K t2 -> T with
                   | type.base t2
                     => fun v => bind_base_cps (fun t => K (type.base t)) v T k
                   | _ => fun _ => k None
                   end v
             | type.arrow A B
               => fun T k
                 => match t2 return K t2 -> T with
                   | type.arrow A' B'
                     => fun v
                       => (v' <-- @bind_value_cps B B' (fun B' => K (A' -> B')%etype) v;
                            v'' <-- @bind_value_cps A A' (fun A' => ptype_interp_cps qexists B (fun B' => K (A' -> B')%etype)) v';
                          return (Some v''))
                           T k
                   | _ => fun _ => k None
                   end v
             end%cps.

        Fixpoint bind_data_cps (e : rawexpr) (p : pattern)
          : ~> option (binding_dataT p)
          := match p, e return ~> option (binding_dataT p) with
             | pattern.Wildcard t, _
               => bind_value_cps value (value_of_rawexpr e)
             | pattern.Ident pidc, rIdent _ idc _ _
               => (tr <-- (try_make_transport_ident_cps
                             (fun idc => match arg_types idc with
                                         | Some t1 => t1
                                         | None => unit
                                         end) _ _);
                     return (Some (tr (bind_args _ idc))))
             | pattern.App pf px, rApp f x _ _
               => (f' <-- bind_data_cps f pf;
                     x' <-- bind_data_cps x px;
                   return (Some (f', x')))
             | pattern.Ident _, _
             | pattern.App _ _, _
               => (return None)
             end%cps.

        (** We follow
            http://moscova.inria.fr/~maranget/papers/ml05e-maranget.pdf,
            "Compiling Pattern Matching to Good Decision Trees" by Luc
            Maranget.  A [decision_tree] describes how to match a
            vector (or list) of patterns against a vector of
            expressions. The cases of a [decision_tree] are:

            - [TryLeaf k onfailure]: Try the kth rewrite rule; if it
              fails, keep going with [onfailure]

            - [Failure]: Abort; nothing left to try

            - [Switch icases app_case default]: With the first element
              of the vector, match on its kind; if it is an identifier
              matching something in [icases], remove the first element
              of the vector run that decision tree; if it is an
              application and [app_case] is not [None], try the
              [app_case] decision_tree, replacing the first element of
              each vector with the two elements of the function and
              the argument its applied to; otherwise, don't modify the
              vectors, and use the [default] decision tree.

            - [Swap i cont]: Swap the first element of the vector with
              the ith element, and keep going with [cont] *)
        Inductive decision_tree :=
        | TryLeaf (k : nat) (onfailure : decision_tree)
        | Failure
        | Switch (icases : list (pident * decision_tree))
                 (app_case : option decision_tree)
                 (default : decision_tree)
        | Swap (i : nat) (cont : decision_tree).

        Definition swap_list {A} (i j : nat) (ls : list A) : option (list A)
          := match nth_error ls i, nth_error ls j with
             | Some vi, Some vj => Some (set_nth i vj (set_nth j vi ls))
             | _, _ => None
             end.

        Fixpoint eval_decision_tree {T} (ctx : list rawexpr) (d : decision_tree) (cont : option nat -> list rawexpr -> option (unit -> T) -> T) {struct d} : T
          := match d with
             | TryLeaf k onfailure
               => cont (Some k) ctx
                      (Some (fun 'tt => @eval_decision_tree T ctx onfailure cont))
             | Failure => cont None ctx None
             | Switch icases app_case default_case
               => match ctx with
                  | nil => cont None ctx None
                  | ctx0 :: ctx'
                    => let default := fun 'tt => @eval_decision_tree T ctx default_case cont in
                       let bind_default_in f
                           := match default_case with
                              | Failure => f default
                              | _ => (dlet default := default in f default)
                              end in
                       bind_default_in
                         (fun default
                          => reveal_rawexpr_cps
                               ctx0 _
                               (fun ctx0'
                                => match ctx0' with
                                   | rIdent t idc t' alt
                                     => fold_right
                                          (fun '(pidc, icase) default 'tt
                                           => match invert_bind_args _ idc pidc with
                                              | Some args
                                                => @eval_decision_tree
                                                     T ctx' icase
                                                     (fun k ctx''
                                                      => cont k (rIdent (pident_to_typed pidc args) alt :: ctx''))
                                              | None => default tt
                                              end)
                                          default
                                          icases
                                          tt
                                   | rApp f x t alt
                                     => match app_case with
                                        | Some app_case
                                          => @eval_decision_tree
                                               T (f :: x :: ctx') app_case
                                               (fun k ctx''
                                                => match ctx'' with
                                                   | f' :: x' :: ctx'''
                                                     => cont k (rApp f' x' alt :: ctx''')
                                                   | _ => cont None ctx
                                                   end)
                                        | None => default tt
                                        end
                                   | rExpr t e
                                   | rValue t e
                                     => default tt
                                   end))
                  end
             | Swap i d'
               => match swap_list 0 i ctx with
                 | Some ctx'
                   => @eval_decision_tree
                       T ctx' d'
                       (fun k ctx''
                        => match swap_list 0 i ctx'' with
                          | Some ctx''' => cont k ctx'''
                          | None => cont None ctx
                          end)
                 | None => cont None ctx None
                 end
             end.

        Local Notation opt_anyexprP ivar
          := (fun should_do_again : bool => UnderLets (@AnyExpr.anyexpr base.type ident (if should_do_again then ivar else var)))
               (only parsing).
        Local Notation opt_anyexpr ivar
          := (option (sigT (opt_anyexprP ivar))) (only parsing).

        Definition rewrite_ruleTP
          := (fun p : pattern => binding_dataT p -> forall T, (opt_anyexpr value -> T) -> T).
        Definition rewrite_ruleT := sigT rewrite_ruleTP.
        Definition rewrite_rulesT
          := (list rewrite_ruleT).

        Definition eval_rewrite_rules
                   (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 : rewrite_rulesT)
                   (e : rawexpr)
          : UnderLets (expr (type_of_rawexpr e))
          := dlet default := UnderLets.Base (expr_of_rawexpr e) in
             eval_decision_tree
               (e::nil) d
               (fun k ctx default_on_rewrite_failure
                => match k, ctx return UnderLets (expr (type_of_rawexpr e)) with
                  | Some k', e'::nil
                    => match nth_error rew k' return UnderLets (expr (type_of_rawexpr e)) with
                      | Some (existT p f)
                        => bind_data_cps
                            e' p _
                            (fun v
                             => match v with
                               | Some v
                                 => f v _
                                     (fun fv
                                      => match fv return UnderLets (expr (type_of_rawexpr e)) with
                                        | Some (existT should_do_again fv)
                                          => (fv <-- fv;
                                               fv <-- maybe_do_again should_do_again _ fv;
                                               type.try_transport_cps
                                                 base.try_make_transport_cps _ _ _ fv _
                                                 (fun fv'
                                                  => match fv', default_on_rewrite_failure with
                                                    | Some fv'', _ => UnderLets.Base fv''
                                                    | None, Some default => default tt
                                                    | None, None => default
                                                    end))%under_lets
                                        | None => match default_on_rewrite_failure with
                                                 | Some default => default tt
                                                 | None => default
                                                 end
                                        end)
                               | None => default
                               end)
                      | None => default
                      end
                  | _, _ => default
                   end).

        Local Notation enumerate ls
          := (List.combine (List.seq 0 (List.length ls)) ls).

        Fixpoint first_satisfying_helper {A B} (f : A -> option B) (ls : list A) : option B
          := match ls with
             | nil => None
             | cons x xs
               => match f x with
                 | Some v => Some v
                 | None => first_satisfying_helper f xs
                 end
             end.

        Definition get_index_of_first_non_wildcard (p : list pattern) : option nat
          := first_satisfying_helper
               (fun '(n, x) => match x with
                            | pattern.Wildcard _ => None
                            | _ => Some n
                            end)
               (enumerate p).

        Definition starts_with_wildcard : nat * list pattern -> bool
          := fun '(_, p) => match p with
                            | pattern.Wildcard _::_ => true
                            | _ => false
                            end.

        Definition not_starts_with_wildcard : nat * list pattern -> bool
          := fun p => negb (starts_with_wildcard p).

        Definition filter_pattern_wildcard (p : list (nat * list pattern)) : list (nat * list pattern)
          := filter starts_with_wildcard p.

        Definition split_at_first_pattern_wildcard (p : list (nat * list pattern)) : list (nat * list pattern) * list (nat * list pattern)
          := (take_while not_starts_with_wildcard p, drop_while not_starts_with_wildcard p).

        Fixpoint get_unique_pattern_ident' (p : list (nat * list pattern)) (so_far : list pident) : list pident
          := match p with
             | nil => List.rev so_far
             | (_, pattern.Ident pidc :: _) :: ps
               => let so_far' := if existsb (pident_beq pidc) so_far
                                 then so_far
                                 else pidc :: so_far in
                  get_unique_pattern_ident' ps so_far'
             | _ :: ps => get_unique_pattern_ident' ps so_far
             end.

        Definition get_unique_pattern_ident p : list pident := get_unique_pattern_ident' p nil.

        Definition contains_pattern_pident (pidc : pident) (p : list (nat * list pattern)) : bool
          := existsb (fun '(n, p) => match p with
                                  | pattern.Ident pidc'::_ => pident_beq pidc pidc'
                                  | _ => false
                                  end)
                     p.

        Definition contains_pattern_app (p : list (nat * list pattern)) : bool
          := existsb (fun '(n, p) => match p with
                                  | pattern.App _ _::_ => true
                                  | _ => false
                                  end)
                     p.

        Definition filter_pattern_app (p : nat * list pattern) : option (nat * list pattern)
          := match p with
             | (n, pattern.App f x :: ps)
               => Some (n, f :: x :: ps)
             | (_, pattern.Ident _::_)
             | (_, pattern.Wildcard _::_)
             | (_, nil)
               => None
             end.

        Definition filter_pattern_pident (pidc : pident) (p : nat * list pattern) : option (nat * list pattern)
          := match p with
             | (n, pattern.Ident pidc'::ps)
               => if pident_beq pidc pidc'
                 then Some (n, ps)
                 else None
             | (_, pattern.Wildcard _::_)
             | (_, pattern.App _ _::_)
             | (_, nil)
               => None
             end.

        Definition compile_rewrites_step
                   (compile_rewrites : list (nat * list pattern) -> option decision_tree)
                   (pattern_matrix : list (nat * list pattern))
          : option decision_tree
          := match pattern_matrix with
             | nil => Some Failure
             | (n1, p1) :: ps
               => match get_index_of_first_non_wildcard p1 with
                 | None (* p1 is all wildcards *)
                   => (onfailure <- compile_rewrites ps;
                        Some (TryLeaf n1 onfailure))
                 | Some Datatypes.O
                   => let '(pattern_matrix, default_pattern_matrix) := split_at_first_pattern_wildcard pattern_matrix in
                      default_case <- compile_rewrites default_pattern_matrix;
                        app_case <- (if contains_pattern_app pattern_matrix
                                     then option_map Some (compile_rewrites (Option.List.map filter_pattern_app pattern_matrix))
                                     else Some None);
                        let pidcs := get_unique_pattern_ident pattern_matrix in
                        let icases := Option.List.map
                                        (fun pidc => option_map (pair pidc) (compile_rewrites (Option.List.map (filter_pattern_pident pidc) pattern_matrix)))
                                        pidcs in
                        Some (Switch icases app_case default_case)
                 | Some i
                   => let pattern_matrix'
                         := List.map
                              (fun '(n, ps)
                               => (n,
                                  match swap_list 0 i ps with
                                  | Some ps' => ps'
                                  | None => nil (* should be impossible *)
                                  end))
                              pattern_matrix in
                     d <- compile_rewrites pattern_matrix';
                       Some (Swap i d)
                 end
             end%option.

        Fixpoint compile_rewrites' (fuel : nat) (pattern_matrix : list (nat * list pattern))
          : option decision_tree
          := match fuel with
             | Datatypes.O => None
             | Datatypes.S fuel' => compile_rewrites_step (@compile_rewrites' fuel') pattern_matrix
             end.

        Definition compile_rewrites (fuel : nat) (ps : rewrite_rulesT)
          := compile_rewrites' fuel (enumerate (List.map (fun p => projT1 p :: nil) ps)).


        Fixpoint with_bindingsT (p : pattern) (T : Type)
          := match p return Type with
             | pattern.Wildcard t => ptype_interp qforall t (fun eT => eT -> T)
             | pattern.Ident idc
               => match arg_types idc with
                 | Some t => t -> T
                 | None => T
                 end
             | pattern.App f x => with_bindingsT f (with_bindingsT x T)
             end.

        Fixpoint lift_pbase_type_interp_cps {K1 K2} {quant} (F : forall t : base.type, K1 t -> K2 t) {t}
          : pbase_type_interp_cps quant t K1
            -> pbase_type_interp_cps quant t K2
          := match t, quant return pbase_type_interp_cps quant t K1
                                   -> pbase_type_interp_cps quant t K2 with
             | pattern.base.type.any, qforall
               => fun f t => F t (f t)
             | pattern.base.type.any, qexists
               => fun tf => existT _ _ (F _ (projT2 tf))
             | pattern.base.type.type_base t, _
               => F _
             | pattern.base.type.prod A B, _
               => @lift_pbase_type_interp_cps
                   _ _ quant
                   (fun A'
                    => @lift_pbase_type_interp_cps
                        _ _ quant (fun _ => F _) B)
                   A
             | pattern.base.type.list A, _
               => @lift_pbase_type_interp_cps
                   _ _ quant (fun _ => F _) A
             end.

        Fixpoint lift_ptype_interp_cps {K1 K2} {quant} (F : forall t : type.type base.type, K1 t -> K2 t) {t}
          : ptype_interp_cps quant t K1
            -> ptype_interp_cps quant t K2
          := match t return ptype_interp_cps quant t K1
                                   -> ptype_interp_cps quant t K2 with
             | type.base t
               => lift_pbase_type_interp_cps F
             | type.arrow A B
               => @lift_ptype_interp_cps
                   _ _ quant
                   (fun A'
                    => @lift_ptype_interp_cps
                        _ _ quant (fun _ => F _) B)
                   A
             end.

        Fixpoint lift_with_bindings {p} {A B : Type} (F : A -> B) {struct p} : with_bindingsT p A -> with_bindingsT p B
          := match p return with_bindingsT p A -> with_bindingsT p B with
             | pattern.Wildcard t
               => lift_ptype_interp_cps
                   (K1:=fun t => value t -> A)
                   (K2:=fun t => value t -> B)
                   (fun _ f v => F (f v))
             | pattern.Ident idc
               => match arg_types idc as ty
                       return match ty with
                              | Some t => t -> A
                              | None => A
                              end -> match ty with
                                    | Some t => t -> B
                                    | None => B
                                    end
                 with
                 | Some _ => fun f v => F (f v)
                 | None => F
                 end
             | pattern.App f x
               => @lift_with_bindings
                   f _ _
                   (@lift_with_bindings x _ _ F)
             end.

        Fixpoint app_pbase_type_interp_cps {T : Type} {K1 K2 : base.type -> Type}
                 (F : forall t, K1 t -> K2 t -> T)
                 {t}
          : pbase_type_interp_cps qforall t K1
            -> pbase_type_interp_cps qexists t K2 -> T
          := match t return pbase_type_interp_cps qforall t K1
                            -> pbase_type_interp_cps qexists t K2 -> T with
             | pattern.base.type.any
               => fun f tv => F _ (f _) (projT2 tv)
             | pattern.base.type.type_base t
               => fun f v => F _ f v
             | pattern.base.type.prod A B
               => @app_pbase_type_interp_cps
                   _
                   (fun A' => pbase_type_interp_cps qforall B (fun B' => K1 (A' * B')%etype))
                   (fun A' => pbase_type_interp_cps qexists B (fun B' => K2 (A' * B')%etype))
                   (fun A'
                    => @app_pbase_type_interp_cps
                        _
                        (fun B' => K1 (A' * B')%etype)
                        (fun B' => K2 (A' * B')%etype)
                        (fun _ => F _)
                        B)
                   A
             | pattern.base.type.list A
               => @app_pbase_type_interp_cps T (fun A' => K1 (base.type.list A')) (fun A' => K2 (base.type.list A')) (fun _ => F _) A
             end.

        Fixpoint app_ptype_interp_cps {T : Type} {K1 K2 : type -> Type}
                 (F : forall t, K1 t -> K2 t -> T)
                 {t}
          : ptype_interp_cps qforall t K1
            -> ptype_interp_cps qexists t K2 -> T
          := match t return ptype_interp_cps qforall t K1
                            -> ptype_interp_cps qexists t K2 -> T with
             | type.base t => app_pbase_type_interp_cps F
             | type.arrow A B
               => @app_ptype_interp_cps
                   _
                   (fun A' => ptype_interp_cps qforall B (fun B' => K1 (A' -> B')%etype))
                   (fun A' => ptype_interp_cps qexists B (fun B' => K2 (A' -> B')%etype))
                   (fun A'
                    => @app_ptype_interp_cps
                        _
                        (fun B' => K1 (A' -> B')%etype)
                        (fun B' => K2 (A' -> B')%etype)
                        (fun _ => F _)
                        B)
                   A
             end.

        Fixpoint app_binding_data {T p} : forall (f : with_bindingsT p T) (v : binding_dataT p), T
          := match p return forall (f : with_bindingsT p T) (v : binding_dataT p), T with
             | pattern.Wildcard t
               => app_ptype_interp_cps
                   (K1:=fun t => value t -> T)
                   (K2:=fun t => value t)
                   (fun _ f v => f v)
             | pattern.Ident idc
               => match arg_types idc as ty
                       return match ty with
                              | Some t => t -> T
                              | None => T
                              end -> match ty return Type with
                                    | Some t => t
                                    | None => unit
                                    end -> T
                 with
                 | Some t => fun f x => f x
                 | None => fun v 'tt => v
                 end
             | pattern.App f x
               => fun F '(vf, vx)
                 => @app_binding_data _ x (@app_binding_data _ f F vf) vx
             end.

        (** XXX MOVEME? *)
        Definition mkcast {P : type -> Type} {t1 t2 : type} : ~> (option (P t1 -> P t2))
          := fun T k => type.try_make_transport_cps base.try_make_transport_cps P t1 t2 _ k.
        Definition cast {P : type -> Type} {t1 t2 : type} (v : P t1) : ~> (option (P t2))
          := fun T k => type.try_transport_cps base.try_make_transport_cps P t1 t2 v _ k.
        Definition castb {P : base.type -> Type} {t1 t2 : base.type} (v : P t1) : ~> (option (P t2))
          := fun T k => base.try_transport_cps P t1 t2 v _ k.
        Definition castbe {t1 t2 : base.type} (v : expr t1) : ~> (option (expr t2))
          := @castb expr t1 t2 v.
        Definition castv {t1 t2} (v : value t1) : ~> (option (value t2))
          := fun T k => type.try_transport_cps base.try_make_transport_cps value t1 t2 v _ k.

        Section with_do_again.
          Context (dtree : decision_tree)
                  (rewrite_rules : rewrite_rulesT)
                  (default_fuel : nat)
                  (do_again : forall t : base.type, @expr.expr base.type ident value t -> UnderLets (expr t)).

          Let dorewrite1 (e : rawexpr) : UnderLets (expr (type_of_rawexpr e))
            := eval_rewrite_rules do_again dtree rewrite_rules e.

          Fixpoint assemble_identifier_rewriters' (t : type) : forall e : rawexpr, (forall P, P (type_of_rawexpr e) -> P t) -> value_with_lets t
            := match t return forall e : rawexpr, (forall P, P (type_of_rawexpr e) -> P t) -> value_with_lets t with
               | type.base _
                 => fun e k => k (fun t => UnderLets (expr t)) (dorewrite1 e)
               | type.arrow s d
                 => fun f k (x : value' _ _)
                    => let x' := reify x in
                       @assemble_identifier_rewriters' d (rApp f (rValueOrExpr2 x x') (k _ (expr_of_rawexpr f) @ x'))%expr (fun _ => id)
               end%under_lets.

          Definition assemble_identifier_rewriters {t} (idc : ident t) : value_with_lets t
            := eta_ident_cps _ _ idc (fun t' idc' => assemble_identifier_rewriters' t' (rIdent idc' #idc') (fun _ => id)).
        End with_do_again.
      End with_var.

      Section full.
        Context {var : type.type base.type -> Type}.
        Local Notation expr := (@expr base.type ident).
        Local Notation value := (@Compile.value base.type ident var).
        Local Notation value_with_lets := (@Compile.value_with_lets base.type ident var).
        Local Notation UnderLets := (UnderLets.UnderLets base.type ident var).
        Local Notation reify_and_let_binds_cps := (@Compile.reify_and_let_binds_cps ident var (@UnderLets.reify_and_let_binds_base_cps var)).
        Local Notation reflect := (@Compile.reflect ident var).
        Section with_rewrite_head.
          Context (rewrite_head : forall t (idc : ident t), value_with_lets t).

          Local Notation "e <---- e' ; f" := (Compile.splice_value_with_lets e' (fun e => f%under_lets)) : under_lets_scope.
          Local Notation "e <----- e' ; f" := (Compile.splice_under_lets_with_value e' (fun e => f%under_lets)) : under_lets_scope.

          Fixpoint rewrite_bottomup {t} (e : @expr value t) : value_with_lets t
            := match e in expr.expr t return value_with_lets t with
               | expr.Ident t idc
                 => rewrite_head _ idc
               | expr.App s d f x => let f : value s -> value_with_lets d := @rewrite_bottomup _ f in x <---- @rewrite_bottomup _ x; f x
               | expr.LetIn A B x f => x <---- @rewrite_bottomup A x;
                                         xv <----- reify_and_let_binds_cps x _ UnderLets.Base;
                                         @rewrite_bottomup B (f (reflect xv))
               | expr.Var t v => Compile.Base_value v
               | expr.Abs s d f => fun x : value s => @rewrite_bottomup d (f x)
               end%under_lets.
        End with_rewrite_head.

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

        Fixpoint repeat_rewrite
                 (rewrite_head : forall (do_again : forall t : base.type, @expr value (type.base t) -> UnderLets (@expr var (type.base t)))
                                            t (idc : ident t), value_with_lets t)
                 (fuel : nat) {t} e : value_with_lets t
          := @rewrite_bottomup
               (rewrite_head
                  (fun t' e'
                   => match fuel with
                      | Datatypes.O => nbe e'
                      | Datatypes.S fuel' => @repeat_rewrite rewrite_head fuel' (type.base t') e'
                      end%under_lets))
               t e.

        Definition rewrite rewrite_head fuel {t} e : expr t
          := reify (@repeat_rewrite rewrite_head fuel t e).
      End full.

      Definition Rewrite rewrite_head fuel {t} (e : expr.Expr (ident:=ident) t) : expr.Expr (ident:=ident) t
        := fun var => @rewrite var (rewrite_head var) fuel t (e _).
    End Compile.

    Module pident := pattern.ident.

    Module Make.
      Section make_rewrite_rules.
        Import Compile.
        Context {var : type.type base.type -> Type}.
        Local Notation type := (type.type base.type).
        Local Notation expr := (@expr.expr base.type ident var).
        Local Notation value := (@value base.type ident var).
        Local Notation anyexpr := (@anyexpr ident var).
        Local Notation pattern := (@pattern.pattern pattern.ident).
        Local Notation UnderLets := (@UnderLets.UnderLets base.type ident var).
        Local Notation ptype := (type.type pattern.base.type).
        Let type_base (t : base.type) : type := type.base t.
        Let ptype_base (t : pattern.base.type) : ptype := type.base t.
        Let ptype_base' (t : base.type.base) : ptype := @type.base pattern.base.type t.
        Coercion ptype_base' : base.type.base >-> ptype.
        Coercion type_base : base.type >-> type.
        Coercion ptype_base : pattern.base.type >-> ptype.
        Local Notation opt_anyexprP ivar
          := (fun should_do_again : bool => UnderLets (@AnyExpr.anyexpr base.type ident (if should_do_again then ivar else var))).
        Local Notation opt_anyexpr ivar
          := (option (sigT (opt_anyexprP ivar))).
        Local Notation binding_dataT := (@binding_dataT ident var pattern.ident pattern.ident.arg_types).
        Local Notation lift_with_bindings := (@lift_with_bindings ident var pattern.ident pattern.ident.arg_types).
        Local Notation app_binding_data := (@app_binding_data ident var pattern.ident pattern.ident.arg_types).
        Local Notation rewrite_rulesT := (@rewrite_rulesT ident var pattern.ident pattern.ident.arg_types).
        Local Notation rewrite_ruleT := (@rewrite_ruleT ident var pattern.ident pattern.ident.arg_types).
        Local Notation castv := (@castv ident var).

        Definition make_base_Literal_pattern (t : base.type.base) : pattern
          := Eval cbv [pident.of_typed_ident] in
              pattern.Ident (pident.of_typed_ident (@ident.Literal t DefaultValue.type.base.default)).

        Definition bind_base_Literal_pattern (t : base.type.base) : binding_dataT (make_base_Literal_pattern t) ~> base.interp t
          := match t return binding_dataT (make_base_Literal_pattern t) ~> base.interp t with
             | base.type.unit
             | base.type.Z
             | base.type.bool
             | base.type.nat
               => fun v => (return v)
             end%cps.

        Fixpoint make_Literal_pattern (t : base.type) : option { p : pattern & binding_dataT p ~> base.interp t }
          := match t return option { p : pattern & binding_dataT p ~> base.interp t } with
             | base.type.type_base t => Some (existT _ (make_base_Literal_pattern t) (bind_base_Literal_pattern t))
             | base.type.prod A B
               => (a <- make_Literal_pattern A;
                    b <- make_Literal_pattern B;
                    Some (existT
                            (fun p : pattern => binding_dataT p ~> base.interp (A * B))
                            (#pident.pair @ (projT1 a) @ (projT1 b))%pattern
                            (fun '(args : unit * binding_dataT (projT1 a) * binding_dataT (projT1 b))
                             => (av <--- projT2 a (snd (fst args));
                                  bv <--- projT2 b (snd args);
                                return (av, bv)))))
             | base.type.list A => None
             end%option%cps.

        Fixpoint make_interp_rewrite' (t : type) (p : pattern) (rew : binding_dataT p ~> type.interp base.interp t) {struct t}
          : option rewrite_ruleT
          := match t return (_ ~> type.interp base.interp t) -> _ with
             | type.base t
               => fun rew
                 => Some (existT _ p (fun args => v <--- rew args;
                                     return (Some (existT _ false (UnderLets.Base (AnyExpr.wrap (ident.smart_Literal v)))))))
             | type.arrow (type.base s) d
               => fun rew
                 => (lit_s <- make_Literal_pattern s;
                      @make_interp_rewrite'
                        d
                        (pattern.App p (projT1 lit_s))
                        (fun (args : binding_dataT p * binding_dataT (projT1 lit_s))
                         => (rewp <--- rew (fst args);
                              sv <--- projT2 lit_s (snd args);
                            return (rewp sv))))
             | type.arrow _ _ => fun _ => None
             end%option%cps rew.

        Definition make_interp_rewrite'' {t} (idc : ident t) : option rewrite_ruleT
          := make_interp_rewrite'
               t
               (pattern.Ident (pident.of_typed_ident idc))
               (fun iargs => return (ident.interp (pident.retype_ident idc iargs)))%cps.
        (*
        Definition make_interp_rewrite {t} (idc : ident t)
          := invert_Some (make_interp_rewrite'' idc).
         *)

        Local Ltac get_all_valid_interp_rules_from body so_far :=
          let next := match body with
                      | context[@Some (sigT (fun x : pattern => binding_dataT x ~> opt_anyexpr value)) ?rew]
                        => lazymatch so_far with
                          | context[cons rew _] => constr:(I : I)
                          | _ => lazymatch rew with
                                | existT _ _ _ => constr:(Some rew)
                                | _ => constr:(I : I)
                                end
                          end
                      | _ => constr:(@None unit)
                      end in
          lazymatch next with
          | Some ?rew => get_all_valid_interp_rules_from body (cons rew so_far)
          | None => (eval cbv [List.rev List.app] in (List.rev so_far))
          end.
        Local Ltac make_valid_interp_rules :=
          let body := constr:(fun t idc => @pident.eta_ident_cps _ t idc (@make_interp_rewrite'')) in
          let body := (eval cbv [pident.eta_ident_cps make_interp_rewrite'' make_interp_rewrite' make_Literal_pattern pident.of_typed_ident Option.bind projT1 projT2 cpsbind cpsreturn cpscall ident.interp pident.retype_ident ident.gen_interp bind_base_Literal_pattern make_base_Literal_pattern] in body) in
          let body := (eval cbn [base.interp binding_dataT pattern.ident.arg_types base.base_interp ident.smart_Literal fold_right map] in body) in
          let retv := get_all_valid_interp_rules_from body (@nil rewrite_ruleT) in
          exact retv.
        Definition interp_rewrite_rules : rewrite_rulesT
          := ltac:(make_valid_interp_rules).
      End make_rewrite_rules.
    End Make.

    Section with_var.
      Import Compile.
      Context {var : type.type base.type -> Type}.
      Local Notation type := (type.type base.type).
      Local Notation expr := (@expr.expr base.type ident var).
      Local Notation value := (@value base.type ident var).
      Local Notation anyexpr := (@anyexpr ident var).
      Local Notation pattern := (@pattern.pattern pattern.ident).
      Local Notation UnderLets := (@UnderLets.UnderLets base.type ident var).
      Local Notation ptype := (type.type pattern.base.type).
      Let type_base (t : base.type) : type := type.base t.
      Let ptype_base (t : pattern.base.type) : ptype := type.base t.
      Let ptype_base' (t : base.type.base) : ptype := @type.base pattern.base.type t.
      Coercion ptype_base' : base.type.base >-> ptype.
      Coercion type_base : base.type >-> type.
      Coercion ptype_base : pattern.base.type >-> ptype.
      Local Notation opt_anyexprP ivar
        := (fun should_do_again : bool => UnderLets (@AnyExpr.anyexpr base.type ident (if should_do_again then ivar else var))).
      Local Notation opt_anyexpr ivar
        := (option (sigT (opt_anyexprP ivar))).
      Local Notation binding_dataT := (@binding_dataT ident var pattern.ident pattern.ident.arg_types).
      Local Notation lift_with_bindings := (@lift_with_bindings ident var pattern.ident pattern.ident.arg_types).
      Local Notation app_binding_data := (@app_binding_data ident var pattern.ident pattern.ident.arg_types).
      Local Notation rewrite_ruleTP := (@rewrite_ruleTP ident var pattern.ident pattern.ident.arg_types).
      Local Notation rewrite_rulesT := (@rewrite_rulesT ident var pattern.ident pattern.ident.arg_types).
      Local Notation castv := (@castv ident var).
      Local Notation assemble_identifier_rewriters := (@assemble_identifier_rewriters ident var pattern.ident pattern.ident.full_types (@pattern.ident.invert_bind_args) pattern.ident.type_of pattern.ident.to_typed (@pattern.ident.eta_ident_cps) (@pattern.ident.of_typed_ident) pattern.ident.arg_types (@pattern.ident.bind_args) pattern.ident.try_make_transport_ident_cps).

      Let UnderLetsExpr {btype bident ivar} t := @UnderLets.UnderLets base.type ident var (@expr.expr btype bident ivar t).
      Let UnderLetsAnyExpr {btype ident ivar} := @UnderLets.UnderLets btype ident ivar (@AnyExpr.anyexpr btype ident ivar).
      Let UnderLetsAnyExprCpsOpt {btype bident ivar} := ~> option (@UnderLets.UnderLets base.type ident var (@AnyExpr.anyexpr btype bident ivar)).
      (*Let UnderLetsAnyAnyExpr {btype ident ivar} := @UnderLets.UnderLets btype ident ivar (@AnyAnyExpr.anyexpr btype ident ivar).*)
      Let BaseWrapUnderLetsAnyExpr {btype bident ivar t} : @UnderLetsExpr btype bident ivar t -> @UnderLetsAnyExprCpsOpt btype bident ivar
        := fun e T k
           => k (match t return @UnderLets.UnderLets _ _ _ (@expr.expr _ _ _ t) -> _ with
                | type.base _ => fun e => Some (e <-- e; UnderLets.Base (AnyExpr.wrap e))%under_lets
                | type.arrow _ _ => fun _ => None
                end e)%cps.
      Let BaseExpr {btype ident ivar t} : @expr.expr btype ident ivar t -> @UnderLetsExpr btype ident ivar t := UnderLets.Base.
      (*Let BaseAnyAnyExpr {btype ident ivar t} : @expr.expr btype ident ivar t -> @UnderLets.UnderLets btype ident ivar (@expr.expr btype ident ivar t) := UnderLets.Base.*)
      Coercion BaseWrapUnderLetsAnyExpr : UnderLetsExpr >-> UnderLetsAnyExprCpsOpt.
      Coercion BaseExpr : expr >-> UnderLetsExpr.
      Notation ret v := ((v : UnderLetsExpr _) : UnderLetsAnyExprCpsOpt).
      Notation oret v := (fun T k => k (Some v)).
      (*Coercion BaseExpr : expr >-> UnderLets.*)
      Notation make_rewrite'_cps p f
        := (existT
              (fun p' : pattern => binding_dataT p' ~> (opt_anyexpr value))
              p%pattern
              (fun v T (k : opt_anyexpr value -> T)
               => @app_binding_data _ p%pattern f%expr v T k)).
      Notation make_rewrite' p f
        := (existT
              (fun p' : pattern => binding_dataT p' ~> (opt_anyexpr value))
              p%pattern
              (fun v T (k : opt_anyexpr value -> T)
               => k (@app_binding_data _ p%pattern f%expr v))).
      Notation make_rewrite p f
        := (let f' := (@lift_with_bindings p _ _ (fun x:@UnderLetsAnyExprCpsOpt base.type ident var => (x' <-- x; oret (existT (opt_anyexprP value) false x'))%cps) f%expr) in
            make_rewrite'_cps p f').
      Notation make_rewrite_step p f
        := (let f' := (@lift_with_bindings p _ _ (fun x:@UnderLetsAnyExprCpsOpt base.type ident value => (x' <-- x; oret (existT (opt_anyexprP value) true x'))%cps) f%expr) in
            make_rewrite'_cps p f').

      Local Notation "x' <- v ; C" := (fun T k => v%cps T (fun x' => match x' with Some x' => (C%cps : UnderLetsAnyExprCpsOpt) T k | None => k None end)) : cps_scope.
      Local Notation "x <-- y ; f" := (UnderLets.splice y (fun x => (f%cps : UnderLetsExpr _))) : cps_scope.
      Local Notation "x <--- y ; f" := (UnderLets.splice_list y (fun x => (f%cps : UnderLetsExpr _))) : cps_scope.
      Local Notation "x <---- y ; f" := (fun T k => match y with Some x => (f%cps : UnderLetsAnyExprCpsOpt) T k | None => k None end) : cps_scope.

      Definition rlist_rect {A P}
                 {ivar}
                 (Pnil : @UnderLetsExpr base.type ident ivar (type.base P))
                 (Pcons : expr (type.base A) -> list (expr (type.base A)) -> @expr.expr base.type ident ivar (type.base P) -> @UnderLetsExpr base.type ident ivar (type.base P))
                 (e : expr (type.base (base.type.list A)))
        : @UnderLetsAnyExprCpsOpt base.type ident ivar
        := (ls <- reflect_list_cps e;
              list_rect
                (fun _ => UnderLetsExpr (type.base P))
                Pnil
                (fun x xs rec => rec' <-- rec; Pcons x xs rec')
                ls)%cps.

      Definition rlist_rect_cast {A A' P}
                 {ivar}
                 (Pnil : @UnderLetsExpr base.type ident ivar (type.base P))
                 (Pcons : expr (type.base A) -> list (expr (type.base A)) -> @expr.expr base.type ident ivar (type.base P) -> @UnderLetsExpr base.type ident ivar (type.base P))
                 (e : expr (type.base A'))
        : @UnderLetsAnyExprCpsOpt base.type ident ivar
        := (e <- castbe e; rlist_rect Pnil Pcons e)%cps.

      Definition rwhen {ivar} (v : @UnderLetsAnyExprCpsOpt base.type ident ivar) (cond : bool)
        : @UnderLetsAnyExprCpsOpt base.type ident ivar
        := fun T k => if cond then v T k else k None.

      Local Notation "e 'when' cond" := (rwhen e%cps cond) (only parsing, at level 100).

      Local Notation ℤ := base.type.Z.
      Local Notation ℕ := base.type.nat.
      Local Notation bool := base.type.bool.
      Local Notation list := pattern.base.type.list.

      Local Arguments Make.interp_rewrite_rules / .

      (**
         The follow are rules for rewriting expressions. On the left is a pattern to match:
           ??: any expression whose type contains no arrows.
           ??{x}: any expression whose type is x.
           ??{pattern.base.type.list ??}: for example, a list with elements of a captured type. (The captured type does not match a type with arrows.)
           x @ y: x applied to y.
           #?x: a value, know at compile time, with type x. (Where x is one of {ℕ or N (nat), 𝔹 or B (bool), ℤ or Z (integers)}.)
           #x: the identifer x.

         A matched expression is replaced with the right-hand-side, which is a function that returns a syntax tree, or None to indicate that the match didn't really match. The syntax tree is under three monads: continuation, option, and custom UnderLets monad.

         The function takes the elements that where matched on the LHS as arguments. The arguments are given in the same order as on the LHS, but where wildcards in a type appear before the outer wildcard for that element. So ??{??} results in two arguments, the second wildcard comes first, and ??{?? -> ??} gives arguments in the order 2, 3, 1.

         Sometimes matching an identifer will also result in arguments. Depends on the identifer. Good luck!

In the RHS, the follow notation applies:
           ##x: the literal value x
           #x: the identifier x
           x @ y: x applied to y
           $x: PHOAS variable named x
           λ: PHOAS abstraction / functions

         On the RHS, since we're returning a value under three monads, there's some fun notion for dealing with different levels of the monad stack in a single expression:
           ret: return something of type [UnderLets expr]
           <-: bind, under the CPS+Option monad.
           <--: bind, under the UnderLets monad
           <---: bind, under the UnderLets+List monad
           <----: bind, under the Option monad.

         If you have an expression of type expr or UnderLetsExpr or UnderLetsAnyExprCpsOpt, coercions will handle it; if you have an expression of type [UnderLets expr], you will need [ret].

         If stuck, email Jason.
       *)
      Definition nbe_rewrite_rules : rewrite_rulesT
        := Eval cbn [Make.interp_rewrite_rules List.app] in
            Make.interp_rewrite_rules
              ++ [
                make_rewrite (#pident.fst @ (??, ??)) (fun _ x _ y => x)
                ; make_rewrite (#pident.snd @ (??, ??)) (fun _ x _ y => y)
                ; make_rewrite (#pident.List_repeat @ ?? @ #?ℕ) (fun _ x n => reify_list (repeat x n))
                ; make_rewrite
                    (#pident.bool_rect @ ??{() -> ??} @ ??{() -> ??} @ #?𝔹)
                    (fun _ t _ f b
                     => if b return UnderLetsExpr (type.base (if b then _ else _))
                        then t ##tt
                        else f ##tt)
                ; make_rewrite
                    (#pident.prod_rect @ ??{?? -> ?? -> ??} @ (??, ??))
                    (fun _ _ _ f _ x _ y
                     => x <- castbe x; y <- castbe y; ret (f x y))
                ; make_rewrite
                    (??{list ??} ++ ??{list ??})
                    (fun _ xs _ ys => rlist_rect_cast ys (fun x _ xs_ys => x :: xs_ys) xs)
                ; make_rewrite
                    (#pident.List_firstn @ #?ℕ @ ??{list ??})
                    (fun n _ xs
                     => xs <- reflect_list_cps xs;
                          reify_list (List.firstn n xs))
                ; make_rewrite
                    (#pident.List_skipn @ #?ℕ @ ??{list ??})
                    (fun n _ xs
                     => xs <- reflect_list_cps xs;
                          reify_list (List.skipn n xs))
                ; make_rewrite
                    (#pident.List_rev @ ??{list ??})
                    (fun _ xs
                     => xs <- reflect_list_cps xs;
                          reify_list (List.rev xs))
                ; make_rewrite_step
                    (#pident.List_flat_map @ ??{?? -> list ??} @ ??{list ??})
                    (fun _ B f _ xs
                     => rlist_rect_cast
                          []
                          (fun x _ flat_map_tl => fx <-- f x; UnderLets.Base ($fx ++ flat_map_tl))
                          xs)
                ; make_rewrite_step
                    (#pident.List_partition @ ??{?? -> base.type.bool} @ ??{list ??})
                    (fun _ f _ xs
                     => rlist_rect_cast
                          ([], [])
                          (fun x tl partition_tl
                           => fx <-- f x;
                                (#ident.prod_rect
                                  @ (λ g d, #ident.bool_rect
                                             @ (λ _, ($x :: $g, $d))
                                             @ (λ _, ($g, $x :: $d))
                                             @ $fx)
                                  @ partition_tl))
                          xs)
                ; make_rewrite
                    (#pident.List_fold_right @ ??{?? -> ?? -> ??} @ ?? @ ??{list ??})
                    (fun _ _ _ f B init A xs
                     => f <- @castv _ (A -> B -> B)%etype f;
                          rlist_rect
                            init
                            (fun x _ y => f x y)
                            xs)
                ; make_rewrite
                    (#pident.list_rect @ ??{() -> ??} @ ??{?? -> ?? -> ?? -> ??} @ ??{list ??})
                    (fun P Pnil _ _ _ _ Pcons A xs
                     => Pcons <- @castv _ (A -> base.type.list A -> P -> P) Pcons;
                          rlist_rect
                            (Pnil ##tt)
                            (fun x' xs' rec => Pcons x' (reify_list xs') rec)
                            xs)
                ; make_rewrite
                    (#pident.list_case @ ??{() -> ??} @ ??{?? -> ?? -> ??} @ []) (fun _ Pnil _ _ _ Pcons => ret (Pnil ##tt))
                ; make_rewrite
                    (#pident.list_case @ ??{() -> ??} @ ??{?? -> ?? -> ??} @ (?? :: ??))
                    (fun _ Pnil _ _ _ Pcons _ x _ xs
                     => x <- castbe x; xs <- castbe xs; ret (Pcons x xs))
                ; make_rewrite
                    (#pident.List_map @ ??{?? -> ??} @ ??{list ??})
                    (fun _ _ f _ xs
                     => rlist_rect_cast
                          []
                          (fun x _ fxs => fx <-- f x; fx :: fxs)
                          xs)
                ; make_rewrite
                    (#pident.List_nth_default @ ?? @ ??{list ??} @ #?ℕ)
                    (fun _ default _ ls n
                     => default <- castbe default;
                          ls <- reflect_list_cps ls;
                          nth_default default ls n)
                ; make_rewrite
                    (#pident.nat_rect @ ??{() -> ??} @ ??{base.type.nat -> ?? -> ??} @ #?ℕ)
                    (fun P O_case _ _ S_case n
                     => S_case <- @castv _ (@type.base base.type base.type.nat -> type.base P -> type.base P) S_case;
                          ret (nat_rect _ (O_case ##tt) (fun n' rec => rec <-- rec; S_case ##n' rec) n))
                ; make_rewrite
                    (#pident.nat_rect_arrow @ ??{?? -> ??} @ ??{base.type.nat -> (?? -> ??) -> (?? -> ??)} @ #?ℕ @ ??)
                    (fun P Q O_case _ _ _ _ S_case n _ v
                     => S_case <- @castv _ (@type.base base.type base.type.nat -> (type.base P -> type.base Q) -> (type.base P -> type.base Q)) S_case;
                          v <- castbe v;
                          ret (nat_rect _ O_case (fun n' rec v => S_case ##n' rec v) n v))
                ; make_rewrite
                    (#pident.List_length @ ??{list ??})
                    (fun _ xs => xs <- reflect_list_cps xs; ##(List.length xs))
                ; make_rewrite
                    (#pident.List_combine @ ??{list ??} @ ??{list ??})
                    (fun _ xs _ ys
                     => xs <- reflect_list_cps xs;
                          ys <- reflect_list_cps ys;
                          reify_list (List.map (fun '((x, y)%core) => (x, y)) (List.combine xs ys)))
                ; make_rewrite
                    (#pident.List_update_nth @ #?ℕ @ ??{?? -> ??} @ ??{list ??})
                    (fun n _ _ f A ls
                     => f <- @castv _ (A -> A) f;
                          ls <- reflect_list_cps ls;
                          ret
                            (retv <--- (update_nth
                                          n
                                          (fun x => x <-- x; f x)
                                          (List.map UnderLets.Base ls));
                               reify_list retv))
              ]%list%pattern%cps%option%under_lets%Z%bool.

      Definition arith_rewrite_rules : rewrite_rulesT
        := [make_rewrite (#pident.fst @ (??, ??)) (fun _ x _ y => x)
            ; make_rewrite (#pident.snd @ (??, ??)) (fun _ x _ y => y)
            ; make_rewrite (#?ℤ   + ??{ℤ}) (fun z v => v  when  Z.eqb z 0)
            ; make_rewrite (??{ℤ} + #?ℤ  ) (fun v z => v  when  Z.eqb z 0)
            ; make_rewrite (#?ℤ   + (-??{ℤ})) (fun z v => ##z - v  when  Z.gtb z 0)
            ; make_rewrite ((-??{ℤ}) + #?ℤ  ) (fun v z => ##z - v  when  Z.gtb z 0)
            ; make_rewrite (#?ℤ   + (-??{ℤ})) (fun z v => -(##((-z)%Z) + v)  when  Z.ltb z 0)
            ; make_rewrite ((-??{ℤ}) + #?ℤ  ) (fun v z => -(v + ##((-z)%Z))  when  Z.ltb z 0)
            ; make_rewrite ((-??{ℤ}) + (-??{ℤ})) (fun x y => -(x + y))
            ; make_rewrite ((-??{ℤ}) +   ??{ℤ} ) (fun x y => y - x)
            ; make_rewrite (  ??{ℤ}  + (-??{ℤ})) (fun x y => x - y)

            ; make_rewrite (#?ℤ   - (-??{ℤ})) (fun z v =>  v  when  Z.eqb z 0)
            ; make_rewrite (#?ℤ   -   ??{ℤ} ) (fun z v => -v  when  Z.eqb z 0)
            ; make_rewrite (??{ℤ} - #?ℤ     ) (fun v z =>  v  when  Z.eqb z 0)
            ; make_rewrite (#?ℤ   - (-??{ℤ})) (fun z v => ##z + v  when  Z.gtb z 0)
            ; make_rewrite (#?ℤ   - (-??{ℤ})) (fun z v => v - ##((-z)%Z)     when  Z.ltb z 0)
            ; make_rewrite (#?ℤ   -   ??{ℤ} ) (fun z v => -(##((-z)%Z) + v)  when  Z.ltb z 0)
            ; make_rewrite ((-??{ℤ}) - #?ℤ  ) (fun v z => -(v + ##((-z)%Z))  when  Z.gtb z 0)
            ; make_rewrite ((-??{ℤ}) - #?ℤ  ) (fun v z => ##((-z)%Z) - v     when  Z.ltb z 0)
            ; make_rewrite (  ??{ℤ}  - #?ℤ  ) (fun v z => v + ##((-z)%Z)     when  Z.ltb z 0)
            ; make_rewrite ((-??{ℤ}) - (-??{ℤ})) (fun x y => y - x)
            ; make_rewrite ((-??{ℤ}) -   ??{ℤ} ) (fun x y => -(x + y))
            ; make_rewrite (  ??{ℤ}  - (-??{ℤ})) (fun x y => x + y)

            ; make_rewrite (#?ℤ   * ??{ℤ}) (fun z v => ##0  when  Z.eqb z 0)
            ; make_rewrite (??{ℤ} * #?ℤ  ) (fun v z => ##0  when  Z.eqb z 0)
            ; make_rewrite (#?ℤ   * ??{ℤ}) (fun z v => v  when  Z.eqb z 1)
            ; make_rewrite (??{ℤ} * #?ℤ  ) (fun v z => v  when  Z.eqb z 1)
            ; make_rewrite (#?ℤ      * (-??{ℤ})) (fun z v =>  v  when  Z.eqb z (-1))
            ; make_rewrite ((-??{ℤ}) * #?ℤ     ) (fun v z =>  v  when  Z.eqb z (-1))
            ; make_rewrite (#?ℤ      *   ??{ℤ} ) (fun z v => -v  when  Z.eqb z (-1))
            ; make_rewrite (??{ℤ}    * #?ℤ     ) (fun v z => -v  when  Z.eqb z (-1))
            ; make_rewrite (#?ℤ      * ??{ℤ}   ) (fun z v => -(##((-z)%Z) * v)  when  Z.ltb z 0)
            ; make_rewrite (??{ℤ}    * #?ℤ     ) (fun v z => -(v * ##((-z)%Z))  when  Z.ltb z 0)
            ; make_rewrite ((-??{ℤ}) * (-??{ℤ})) (fun x y => x * y)
            ; make_rewrite ((-??{ℤ}) *   ??{ℤ} ) (fun x y => -(x * y))
            ; make_rewrite (  ??{ℤ}  * (-??{ℤ})) (fun x y => -(x * y))

            ; make_rewrite (??{ℤ} &' #?ℤ) (fun x mask => ##(0)%Z  when  mask =? 0)
            ; make_rewrite (#?ℤ &' ??{ℤ}) (fun mask x => ##(0)%Z  when  mask =? 0)

            ; make_rewrite (??{ℤ} * #?ℤ)   (fun x y => x << ##(Z.log2 y)  when  (y =? (2^Z.log2 y)) && (negb (y =? 2)))
            ; make_rewrite (#?ℤ * ??{ℤ})   (fun y x => x << ##(Z.log2 y)  when  (y =? (2^Z.log2 y)) && (negb (y =? 2)))
            ; make_rewrite (??{ℤ} / #?ℤ)   (fun x y => x                  when  (y =? 1))
            ; make_rewrite (??{ℤ} mod #?ℤ) (fun x y => ##(0)%Z            when  (y =? 1))
            ; make_rewrite (??{ℤ} / #?ℤ)   (fun x y => x >> ##(Z.log2 y)  when  (y =? (2^Z.log2 y)))
            ; make_rewrite (??{ℤ} mod #?ℤ) (fun x y => x &' ##(y-1)%Z     when  (y =? (2^Z.log2 y)))
            ; make_rewrite (-(-??{ℤ})) (fun v => v)

            ; make_rewrite (#pident.Z_mul_split @ #?ℤ @ #?ℤ @ ??{ℤ}) (fun s xx y => (##0, ##0)%Z  when  Z.eqb xx 0)
            ; make_rewrite (#pident.Z_mul_split @ #?ℤ @ ??{ℤ} @ #?ℤ) (fun s y xx => (##0, ##0)%Z  when  Z.eqb xx 0)
            ; make_rewrite (#pident.Z_mul_split @ #?ℤ @ #?ℤ @ ??{ℤ}) (fun s xx y => (y, ##0)%Z  when  Z.eqb xx 1)
            ; make_rewrite (#pident.Z_mul_split @ #?ℤ @ ??{ℤ} @ #?ℤ) (fun s y xx => (y, ##0)%Z  when  Z.eqb xx 1)
            ; make_rewrite (#pident.Z_mul_split @ #?ℤ @ #?ℤ @ ??{ℤ}) (fun s xx y => (-y, ##0%Z)  when  Z.eqb xx (-1))
            ; make_rewrite (#pident.Z_mul_split @ #?ℤ @ ??{ℤ} @ #?ℤ) (fun s y xx => (-y, ##0%Z)  when  Z.eqb xx (-1))

            ; make_rewrite
                (#pident.Z_add_get_carry @ ??{ℤ} @ (-??{ℤ}) @ ??{ℤ})
                (fun s y x => ret (UnderLets.UnderLet
                                     (#ident.Z_sub_get_borrow @ s @ x @ y)
                                     (fun vc => UnderLets.Base (#ident.fst @ $vc, -(#ident.snd @ $vc)))))
            ; make_rewrite
                (#pident.Z_add_get_carry @ ??{ℤ} @ ??{ℤ} @ (-??{ℤ}))
                (fun s x y => ret (UnderLets.UnderLet
                                     (#ident.Z_sub_get_borrow @ s @ x @ y)
                                     (fun vc => UnderLets.Base (#ident.fst @ $vc, -(#ident.snd @ $vc)))))
            ; make_rewrite
                (#pident.Z_add_get_carry @ ??{ℤ} @ #?ℤ @ ??{ℤ})
                (fun s yy x => ret (UnderLets.UnderLet
                                      (#ident.Z_sub_get_borrow @ s @ x @ ##(-yy)%Z)
                                      (fun vc => UnderLets.Base (#ident.fst @ $vc, -(#ident.snd @ $vc))))
                                   when  yy <? 0)
            ; make_rewrite
                (#pident.Z_add_get_carry @ ??{ℤ} @ ??{ℤ} @ #?ℤ)
                (fun s x yy => ret (UnderLets.UnderLet
                                      (#ident.Z_sub_get_borrow @ s @ x @ ##(-yy)%Z)
                                      (fun vc => UnderLets.Base (#ident.fst @ $vc, -(#ident.snd @ $vc))))
                                   when  yy <? 0)


            ; make_rewrite
                (#pident.Z_add_with_get_carry @ ??{ℤ} @ (-??{ℤ}) @ (-??{ℤ}) @ ??{ℤ})
                (fun s c y x => ret (UnderLets.UnderLet
                                       (#ident.Z_sub_with_get_borrow @ s @ c @ x @ y)
                                       (fun vc => UnderLets.Base (#ident.fst @ $vc, -(#ident.snd @ $vc)))))
            ; make_rewrite
                (#pident.Z_add_with_get_carry @ ??{ℤ} @ (-??{ℤ}) @ ??{ℤ} @ (-??{ℤ}))
                (fun s c x y => ret (UnderLets.UnderLet
                                       (#ident.Z_sub_with_get_borrow @ s @ c @ x @ y)
                                       (fun vc => UnderLets.Base (#ident.fst @ $vc, -(#ident.snd @ $vc)))))
            ; make_rewrite
                (#pident.Z_add_with_get_carry @ ??{ℤ} @ #?ℤ @ (-??{ℤ}) @ ??{ℤ})
                (fun s cc y x => ret (UnderLets.UnderLet
                                        (#ident.Z_sub_get_borrow @ s @ x @ y)
                                        (fun vc => UnderLets.Base (#ident.fst @ $vc, -(#ident.snd @ $vc))))
                                     when  cc =? 0)
            ; make_rewrite
                (#pident.Z_add_with_get_carry @ ??{ℤ} @ #?ℤ @ (-??{ℤ}) @ ??{ℤ})
                (fun s cc y x => ret (UnderLets.UnderLet
                                        (#ident.Z_sub_with_get_borrow @ s @ ##(-cc)%Z @ x @ y)
                                        (fun vc => UnderLets.Base (#ident.fst @ $vc, -(#ident.snd @ $vc))))
                                     when  cc <? 0)
            ; make_rewrite
                (#pident.Z_add_with_get_carry @ ??{ℤ} @ #?ℤ @ ??{ℤ} @ (-??{ℤ}))
                (fun s cc x y => ret (UnderLets.UnderLet
                                        (#ident.Z_sub_get_borrow @ s @ x @ y)
                                        (fun vc => UnderLets.Base (#ident.fst @ $vc, -(#ident.snd @ $vc))))
                                     when  cc =? 0)
            ; make_rewrite
                (#pident.Z_add_with_get_carry @ ??{ℤ} @ #?ℤ @ ??{ℤ} @ (-??{ℤ}))
                (fun s cc x y => ret (UnderLets.UnderLet
                                        (#ident.Z_sub_with_get_borrow @ s @ ##(-cc)%Z @ x @ y)
                                        (fun vc => UnderLets.Base (#ident.fst @ $vc, -(#ident.snd @ $vc))))
                                     when  cc <? 0)
            ; make_rewrite
                (#pident.Z_add_with_get_carry @ ??{ℤ} @ (-??{ℤ}) @ #?ℤ @ ??{ℤ})
                (fun s c yy x => ret (UnderLets.UnderLet
                                        (#ident.Z_sub_with_get_borrow @ s @ c @ x @ ##(-yy)%Z)
                                        (fun vc => UnderLets.Base (#ident.fst @ $vc, -(#ident.snd @ $vc))))
                                     when  yy <=? 0)
            ; make_rewrite
                (#pident.Z_add_with_get_carry @ ??{ℤ} @ (-??{ℤ}) @ ??{ℤ} @ #?ℤ)
                (fun s c x yy => ret (UnderLets.UnderLet
                                        (#ident.Z_sub_with_get_borrow @ s @ c @ x @ ##(-yy)%Z)
                                        (fun vc => UnderLets.Base (#ident.fst @ $vc, -(#ident.snd @ $vc))))
                                     when  yy <=? 0)
            ; make_rewrite
                (#pident.Z_add_with_get_carry @ ??{ℤ} @ #?ℤ @ #?ℤ @ ??{ℤ})
                (fun s cc yy x => ret (UnderLets.UnderLet
                                         (#ident.Z_sub_with_get_borrow @ s @ ##(-cc)%Z @ x @ ##(-yy)%Z)
                                         (fun vc => UnderLets.Base (#ident.fst @ $vc, -(#ident.snd @ $vc))))
                                      when  (yy <=? 0) && (cc <=? 0) && ((yy + cc) <? 0)) (* at least one must be strictly negative *)
            ; make_rewrite
                (#pident.Z_add_with_get_carry @ ??{ℤ} @ #?ℤ @ ??{ℤ} @ #?ℤ)
                (fun s cc x yy => ret (UnderLets.UnderLet
                                         (#ident.Z_sub_with_get_borrow @ s @ ##(-cc)%Z @ x @ ##(-yy)%Z)
                                         (fun vc => UnderLets.Base (#ident.fst @ $vc, -(#ident.snd @ $vc))))
                                      when  (yy <=? 0) && (cc <=? 0) && ((yy + cc) <? 0)) (* at least one must be strictly negative *)


            ; make_rewrite (#pident.Z_add_get_carry @ ??{ℤ} @ #?ℤ @ ??{ℤ}) (fun s xx y => (y, ##0)  when  xx =? 0)
            ; make_rewrite (#pident.Z_add_get_carry @ ??{ℤ} @ ??{ℤ} @ #?ℤ) (fun s y xx => (y, ##0)  when  xx =? 0)

            ; make_rewrite (#pident.Z_add_with_carry @ #?ℤ @ ??{ℤ} @ ??{ℤ}) (fun cc x y => x + y  when  cc =? 0)
            (*; make_rewrite_step (#pident.Z_add_with_carry @ ??{ℤ} @ ??{ℤ} @ ??{ℤ}) (fun x y z => $x + $y + $z)*)

            ; make_rewrite
                (#pident.Z_add_with_get_carry @ #?ℤ @ #?ℤ @ #?ℤ @ ??{ℤ}) (fun s cc xx y => (y, ##0)   when   (cc =? 0) && (xx =? 0))
            ; make_rewrite
                (#pident.Z_add_with_get_carry @ #?ℤ @ #?ℤ @ ??{ℤ} @ #?ℤ) (fun s cc y xx => (y, ##0)   when   (cc =? 0) && (xx =? 0))
            (*; make_rewrite
                (#pident.Z_add_with_get_carry @ ??{ℤ} @ ??{ℤ} @ #?ℤ @ #?ℤ) (fun s c xx yy => (c, ##0) when   (xx =? 0) && (yy =? 0))*)
            ; make_rewrite (* carry = 0: ADC x y -> ADD x y *)
                (#pident.Z_add_with_get_carry @ ??{ℤ} @ #?ℤ @ ??{ℤ} @ ??{ℤ})
                (fun s cc x y => ret (UnderLets.UnderLet
                                        (#ident.Z_add_get_carry @ s @ x @ y)
                                        (fun vc => UnderLets.Base (#ident.fst @ $vc, #ident.snd @ $vc)))
                                     when  cc =? 0)
            ; make_rewrite (* ADC 0 0 -> (ADX 0 0, 0) *) (* except we don't do ADX, because C stringification doesn't handle it *)
                (#pident.Z_add_with_get_carry @ ??{ℤ} @ ??{ℤ} @ #?ℤ @ #?ℤ)
                (fun s c xx yy => ret (UnderLets.UnderLet
                                         (#ident.Z_add_with_get_carry @ s @ c @ ##xx @ ##yy)
                                         (fun vc => UnderLets.Base (#ident.fst @ $vc, ##0)))
                                      when  (xx =? 0) && (yy =? 0))


            (* let-bind any adc/sbb/mulx *)
            ; make_rewrite
                (#pident.Z_add_with_get_carry @ ??{ℤ} @ ??{ℤ} @ ??{ℤ} @ ??{ℤ})
                (fun s c x y => ret (UnderLets.UnderLet (#ident.Z_add_with_get_carry @ s @ c @ x @ y)
                                                        (fun vc => UnderLets.Base (#ident.fst @ $vc, #ident.snd @ $vc))))
            ; make_rewrite
                (#pident.Z_add_with_carry @ ??{ℤ} @ ??{ℤ} @ ??{ℤ})
                (fun c x y => ret (UnderLets.UnderLet (#ident.Z_add_with_carry @ c @ x @ y)
                                                      (fun vc => UnderLets.Base ($vc))))
            ; make_rewrite
                (#pident.Z_add_get_carry @ ??{ℤ} @ ??{ℤ} @ ??{ℤ})
                (fun s x y => ret (UnderLets.UnderLet (#ident.Z_add_get_carry @ s @ x @ y)
                                                      (fun vc => UnderLets.Base (#ident.fst @ $vc, #ident.snd @ $vc))))
            ; make_rewrite
                (#pident.Z_sub_with_get_borrow @ ??{ℤ} @ ??{ℤ} @ ??{ℤ} @ ??{ℤ})
                (fun s c x y => ret (UnderLets.UnderLet (#ident.Z_sub_with_get_borrow @ s @ c @ x @ y)
                                                        (fun vc => UnderLets.Base (#ident.fst @ $vc, #ident.snd @ $vc))))
            ; make_rewrite
                (#pident.Z_sub_get_borrow @ ??{ℤ} @ ??{ℤ} @ ??{ℤ})
                (fun s x y => ret (UnderLets.UnderLet (#ident.Z_sub_get_borrow @ s @ x @ y)
                                                      (fun vc => UnderLets.Base (#ident.fst @ $vc, #ident.snd @ $vc))))
            ; make_rewrite
                (#pident.Z_mul_split @ ??{ℤ} @ ??{ℤ} @ ??{ℤ})
                (fun s x y => ret (UnderLets.UnderLet (#ident.Z_mul_split @ s @ x @ y)
                                                      (fun v => UnderLets.Base (#ident.fst @ $v, #ident.snd @ $v))))


            ; make_rewrite_step (* _step, so that if one of the arguments is concrete, we automatically get the rewrite rule for [Z_cast] applying to it *)
                (#pident.Z_cast2 @ (??{ℤ}, ??{ℤ})) (fun r x y => (#(ident.Z_cast (fst r)) @ $x, #(ident.Z_cast (snd r)) @ $y))

            ; make_rewrite (-??{ℤ}) (fun e => ret (UnderLets.UnderLet e (fun v => UnderLets.Base (-$v)))  when  negb (SubstVarLike.is_var_fst_snd_pair_opp e)) (* inline negation when the rewriter wouldn't already inline it *)
           ]%list%pattern%cps%option%under_lets%Z%bool.

      Definition nbe_dtree'
        := Eval compute in @compile_rewrites ident var pattern.ident pattern.ident.arg_types pattern.ident.ident_beq 100 nbe_rewrite_rules.
      Definition arith_dtree'
        := Eval compute in @compile_rewrites ident var pattern.ident pattern.ident.arg_types pattern.ident.ident_beq 100 arith_rewrite_rules.
      Definition nbe_dtree : decision_tree
        := Eval compute in invert_Some nbe_dtree'.
      Definition arith_dtree : decision_tree
        := Eval compute in invert_Some arith_dtree'.
      Definition nbe_default_fuel := Eval compute in List.length nbe_rewrite_rules.
      Definition arith_default_fuel := Eval compute in List.length arith_rewrite_rules.

      Import PrimitiveHList.
      (* N.B. The [combine_hlist] call MUST eta-expand
         [pr2_rewrite_rules].  That is, it MUST NOT block reduction of
         the resulting list of cons cells on the pair-structure of
         [pr2_rewrite_rules].  This is required so that we can use
         [cbv -] to unfold the entire discrimination tree evaluation,
         including choosing which rewrite rule to apply and binding
         its arguments, without unfolding any of the identifiers used
         to define the replacement value.  (The symptom of messing
         this up is that the [cbv -] will run out of memory when
         trying to reduce things.)  We accomplish this by making
         [hlist] based on a primitive [prod] type with judgmental η,
         so that matching on its structure never blocks reduction. *)
      Definition nbe_split_rewrite_rules := Eval cbv [split_list projT1 projT2 nbe_rewrite_rules] in split_list nbe_rewrite_rules.
      Definition nbe_pr1_rewrite_rules := Eval hnf in projT1 nbe_split_rewrite_rules.
      Definition nbe_pr2_rewrite_rules := Eval hnf in projT2 nbe_split_rewrite_rules.
      Definition nbe_all_rewrite_rules := combine_hlist (P:=rewrite_ruleTP) nbe_pr1_rewrite_rules nbe_pr2_rewrite_rules.

      Definition arith_split_rewrite_rules := Eval cbv [split_list projT1 projT2 arith_rewrite_rules] in split_list arith_rewrite_rules.
      Definition arith_pr1_rewrite_rules := Eval hnf in projT1 arith_split_rewrite_rules.
      Definition arith_pr2_rewrite_rules := Eval hnf in projT2 arith_split_rewrite_rules.
      Definition arith_all_rewrite_rules := combine_hlist (P:=rewrite_ruleTP) arith_pr1_rewrite_rules arith_pr2_rewrite_rules.

      Definition nbe_rewrite_head0 do_again {t} (idc : ident t) : value_with_lets t
        := @assemble_identifier_rewriters nbe_dtree nbe_all_rewrite_rules do_again t idc.

      Definition arith_rewrite_head0 do_again {t} (idc : ident t) : value_with_lets t
        := @assemble_identifier_rewriters arith_dtree arith_all_rewrite_rules do_again t idc.

      Section fancy.
        Context (invert_low invert_high : Z (*log2wordmax*) -> Z -> option Z).
        Definition fancy_rewrite_rules : rewrite_rulesT
          := [
              (*
(Z.add_get_carry_concrete 2^256) @@ (?x, ?y << 128) --> (add 128) @@ (x, y)
(Z.add_get_carry_concrete 2^256) @@ (?x << 128, ?y) --> (add 128) @@ (y, x)
(Z.add_get_carry_concrete 2^256) @@ (?x, ?y >> 128) --> (add (- 128)) @@ (x, y)
(Z.add_get_carry_concrete 2^256) @@ (?x >> 128, ?y) --> (add (- 128)) @@ (y, x)
(Z.add_get_carry_concrete 2^256) @@ (?x, ?y)        --> (add 0) @@ (y, x)
*)
              make_rewrite
                (#pident.Z_add_get_carry @ #?ℤ @ ??{ℤ} @ (#pident.Z_shiftl @ ??{ℤ} @ #?ℤ))
                (fun s x y offset => #(ident.fancy_add (Z.log2 s) offset) @ (x, y)  when  s =? 2^Z.log2 s)
              ; make_rewrite
                  (#pident.Z_add_get_carry @ #?ℤ @ (#pident.Z_shiftl @ ??{ℤ} @ #?ℤ) @ ??{ℤ})
                  (fun s y offset x => #(ident.fancy_add (Z.log2 s) offset) @ (x, y)  when  s =? 2^Z.log2 s)
              ; make_rewrite
                  (#pident.Z_add_get_carry @ #?ℤ @ ??{ℤ} @ (#pident.Z_shiftr @ ??{ℤ} @ #?ℤ))
                  (fun s x y offset => #(ident.fancy_add (Z.log2 s) (-offset)) @ (x, y)  when  s =? 2^Z.log2 s)
              ; make_rewrite
                  (#pident.Z_add_get_carry @ #?ℤ @ (#pident.Z_shiftr @ ??{ℤ} @ #?ℤ) @ ??{ℤ})
                  (fun s y offset x => #(ident.fancy_add (Z.log2 s) (-offset)) @ (x, y)  when  s =? 2^Z.log2 s)
              ; make_rewrite
                  (#pident.Z_add_get_carry @ #?ℤ @ ??{ℤ} @ ??{ℤ})
                  (fun s x y => #(ident.fancy_add (Z.log2 s) 0) @ (x, y)  when  s =? 2^Z.log2 s)
(*
(Z.add_with_get_carry_concrete 2^256) @@ (?c, ?x, ?y << 128) --> (addc 128) @@ (c, x, y)
(Z.add_with_get_carry_concrete 2^256) @@ (?c, ?x << 128, ?y) --> (addc 128) @@ (c, y, x)
(Z.add_with_get_carry_concrete 2^256) @@ (?c, ?x, ?y >> 128) --> (addc (- 128)) @@ (c, x, y)
(Z.add_with_get_carry_concrete 2^256) @@ (?c, ?x >> 128, ?y) --> (addc (- 128)) @@ (c, y, x)
(Z.add_with_get_carry_concrete 2^256) @@ (?c, ?x, ?y)        --> (addc 0) @@ (c, y, x)
 *)
              ; make_rewrite
                  (#pident.Z_add_with_get_carry @ #?ℤ @ ??{ℤ} @ ??{ℤ} @ (#pident.Z_shiftl @ ??{ℤ} @ #?ℤ))
                  (fun s c x y offset => #(ident.fancy_addc (Z.log2 s) offset) @ (c, x, y)  when  s =? 2^Z.log2 s)
              ; make_rewrite
                  (#pident.Z_add_with_get_carry @ #?ℤ @ ??{ℤ} @ (#pident.Z_shiftl @ ??{ℤ} @ #?ℤ) @ ??{ℤ})
                  (fun s c y offset x => #(ident.fancy_addc (Z.log2 s) offset) @ (c, x, y)  when  s =? 2^Z.log2 s)
              ; make_rewrite
                  (#pident.Z_add_with_get_carry @ #?ℤ @ ??{ℤ} @ ??{ℤ} @ (#pident.Z_shiftr @ ??{ℤ} @ #?ℤ))
                  (fun s c x y offset => #(ident.fancy_addc (Z.log2 s) (-offset)) @ (c, x, y)  when  s =? 2^Z.log2 s)
              ; make_rewrite
                  (#pident.Z_add_with_get_carry @ #?ℤ @ ??{ℤ} @ (#pident.Z_shiftr @ ??{ℤ} @ #?ℤ) @ ??{ℤ})
                  (fun s c y offset x => #(ident.fancy_addc (Z.log2 s) (-offset)) @ (c, x, y)  when  s =? 2^Z.log2 s)
              ; make_rewrite
                  (#pident.Z_add_with_get_carry @ #?ℤ @ ??{ℤ} @ ??{ℤ} @ ??{ℤ})
                  (fun s c x y => #(ident.fancy_addc (Z.log2 s) 0) @ (c, x, y)  when  s =? 2^Z.log2 s)
(*
(Z.sub_get_borrow_concrete 2^256) @@ (?x, ?y << 128) --> (sub 128) @@ (x, y)
(Z.sub_get_borrow_concrete 2^256) @@ (?x, ?y >> 128) --> (sub (- 128)) @@ (x, y)
(Z.sub_get_borrow_concrete 2^256) @@ (?x, ?y)        --> (sub 0) @@ (y, x)
 *)
              ; make_rewrite
                  (#pident.Z_sub_get_borrow @ #?ℤ @ ??{ℤ} @ (#pident.Z_shiftl @ ??{ℤ} @ #?ℤ))
                  (fun s x y offset => #(ident.fancy_sub (Z.log2 s) offset) @ (x, y)  when  s =? 2^Z.log2 s)
              ; make_rewrite
                  (#pident.Z_sub_get_borrow @ #?ℤ @ ??{ℤ} @ (#pident.Z_shiftr @ ??{ℤ} @ #?ℤ))
                  (fun s x y offset => #(ident.fancy_sub (Z.log2 s) (-offset)) @ (x, y)  when  s =? 2^Z.log2 s)
              ; make_rewrite
                  (#pident.Z_sub_get_borrow @ #?ℤ @ ??{ℤ} @ ??{ℤ})
                  (fun s x y => #(ident.fancy_sub (Z.log2 s) 0) @ (x, y)  when  s =? 2^Z.log2 s)
(*
(Z.sub_with_get_borrow_concrete 2^256) @@ (?c, ?x, ?y << 128) --> (subb 128) @@ (c, x, y)
(Z.sub_with_get_borrow_concrete 2^256) @@ (?c, ?x, ?y >> 128) --> (subb (- 128)) @@ (c, x, y)
(Z.sub_with_get_borrow_concrete 2^256) @@ (?c, ?x, ?y)        --> (subb 0) @@ (c, y, x)
 *)
              ; make_rewrite
                  (#pident.Z_sub_with_get_borrow @ #?ℤ @ ??{ℤ} @ ??{ℤ} @ (#pident.Z_shiftl @ ??{ℤ} @ #?ℤ))
                  (fun s b x y offset => #(ident.fancy_subb (Z.log2 s) offset) @ (b, x, y)  when  s =? 2^Z.log2 s)
              ; make_rewrite
                  (#pident.Z_sub_with_get_borrow @ #?ℤ @ ??{ℤ} @ ??{ℤ} @ (#pident.Z_shiftr @ ??{ℤ} @ #?ℤ))
                  (fun s b x y offset => #(ident.fancy_subb (Z.log2 s) (-offset)) @ (b, x, y)  when  s =? 2^Z.log2 s)
              ; make_rewrite
                  (#pident.Z_sub_with_get_borrow @ #?ℤ @ ??{ℤ} @ ??{ℤ} @ ??{ℤ})
                  (fun s b x y => #(ident.fancy_subb (Z.log2 s) 0) @ (b, x, y)  when  s =? 2^Z.log2 s)
              (*(Z.rshi_concrete 2^256 ?n) @@ (?c, ?x, ?y) --> (rshi n) @@ (x, y)*)
              ; make_rewrite
                  (#pident.Z_rshi @ #?ℤ @ ??{ℤ} @ ??{ℤ} @ #?ℤ)
                  (fun s x y n => #(ident.fancy_rshi (Z.log2 s) n) @ (x, y)  when  s =? 2^Z.log2 s)
(*
Z.zselect @@ (Z.cc_m_concrete 2^256 ?c, ?x, ?y) --> selm @@ (c, x, y)
Z.zselect @@ (?c &' 1, ?x, ?y)                  --> sell @@ (c, x, y)
Z.zselect @@ (?c, ?x, ?y)                       --> selc @@ (c, x, y)
 *)
              ; make_rewrite
                  (#pident.Z_zselect @ (#pident.Z_cc_m @ #?ℤ @ ??{ℤ}) @ ??{ℤ} @ ??{ℤ})
                  (fun s c x y => #(ident.fancy_selm (Z.log2 s)) @ (c, x, y)  when  s =? 2^Z.log2 s)
              ; make_rewrite
                  (#pident.Z_zselect @ (#pident.Z_land @ #?ℤ @ ??{ℤ}) @ ??{ℤ} @ ??{ℤ})
                  (fun mask c x y => #ident.fancy_sell @ (c, x, y)  when  mask =? 1)
              ; make_rewrite
                  (#pident.Z_zselect @ (#pident.Z_land @ ??{ℤ} @ #?ℤ) @ ??{ℤ} @ ??{ℤ})
                  (fun c mask x y => #ident.fancy_sell @ (c, x, y)  when  mask =? 1)
              ; make_rewrite
                  (#pident.Z_zselect @ ??{ℤ} @ ??{ℤ} @ ??{ℤ})
                  (fun c x y => #ident.fancy_selc @ (c, x, y))
(*Z.add_modulo @@ (?x, ?y, ?m) --> addm @@ (x, y, m)*)
              ; make_rewrite
                  (#pident.Z_add_modulo @ ??{ℤ} @ ??{ℤ} @ ??{ℤ})
                  (fun x y m => #ident.fancy_addm @ (x, y, m))
(*
Z.mul @@ (?x &' (2^128-1), ?y &' (2^128-1)) --> mulll @@ (x, y)
Z.mul @@ (?x &' (2^128-1), ?y >> 128)       --> mullh @@ (x, y)
Z.mul @@ (?x >> 128, ?y &' (2^128-1))       --> mulhl @@ (x, y)
Z.mul @@ (?x >> 128, ?y >> 128)             --> mulhh @@ (x, y)
 *)
              (* literal on left *)
              ; make_rewrite
                  (#?ℤ * (#pident.Z_land @ ??{ℤ} @ #?ℤ))
                  (fun x y mask => let s := (2*Z.log2_up mask)%Z in x <---- invert_low s x; #(ident.fancy_mulll s) @ (##x, y)  when  (mask =? 2^(s/2)-1))
              ; make_rewrite
                  (#?ℤ * (#pident.Z_land @ #?ℤ @ ??{ℤ}))
                  (fun x mask y => let s := (2*Z.log2_up mask)%Z in x <---- invert_low s x; #(ident.fancy_mulll s) @ (##x, y)  when  (mask =? 2^(s/2)-1))
              ; make_rewrite
                  (#?ℤ * (#pident.Z_shiftr @ ??{ℤ} @ #?ℤ))
                  (fun x y offset => let s := (2*offset)%Z in x <---- invert_low s x; #(ident.fancy_mullh s) @ (##x, y))
              ; make_rewrite
                  (#?ℤ * (#pident.Z_land @ #?ℤ @ ??{ℤ}))
                  (fun x mask y => let s := (2*Z.log2_up mask)%Z in x <---- invert_high s x; #(ident.fancy_mulhl s) @ (##x, y)  when  mask =? 2^(s/2)-1)
              ; make_rewrite
                  (#?ℤ * (#pident.Z_land @ ??{ℤ} @ #?ℤ))
                  (fun x y mask => let s := (2*Z.log2_up mask)%Z in x <---- invert_high s x; #(ident.fancy_mulhl s) @ (##x, y)  when  mask =? 2^(s/2)-1)
              ; make_rewrite
                  (#?ℤ * (#pident.Z_shiftr @ ??{ℤ} @ #?ℤ))
                  (fun x y offset => let s := (2*offset)%Z in x <---- invert_high s x; #(ident.fancy_mulhh s) @ (##x, y))

              (* literal on right *)
              ; make_rewrite
                  ((#pident.Z_land @ #?ℤ @ ??{ℤ}) * #?ℤ)
                  (fun mask x y => let s := (2*Z.log2_up mask)%Z in y <---- invert_low s y; #(ident.fancy_mulll s) @ (x, ##y)  when  (mask =? 2^(s/2)-1))
              ; make_rewrite
                  ((#pident.Z_land @ ??{ℤ} @ #?ℤ) * #?ℤ)
                  (fun x mask y => let s := (2*Z.log2_up mask)%Z in y <---- invert_low s y; #(ident.fancy_mulll s) @ (x, ##y)  when  (mask =? 2^(s/2)-1))
              ; make_rewrite
                  ((#pident.Z_land @ #?ℤ @ ??{ℤ}) * #?ℤ)
                  (fun mask x y => let s := (2*Z.log2_up mask)%Z in y <---- invert_high s y; #(ident.fancy_mullh s) @ (x, ##y)  when  mask =? 2^(s/2)-1)
              ; make_rewrite
                  ((#pident.Z_land @ ??{ℤ} @ #?ℤ) * #?ℤ)
                  (fun x mask y => let s := (2*Z.log2_up mask)%Z in y <---- invert_high s y; #(ident.fancy_mullh s) @ (x, ##y)  when  mask =? 2^(s/2)-1)
              ; make_rewrite
                  ((#pident.Z_shiftr @ ??{ℤ} @ #?ℤ) * #?ℤ)
                  (fun x offset y => let s := (2*offset)%Z in y <---- invert_low s y; #(ident.fancy_mulhl s) @ (x, ##y))
              ; make_rewrite
                  ((#pident.Z_shiftr @ ??{ℤ} @ #?ℤ) * #?ℤ)
                  (fun x offset y => let s := (2*offset)%Z in y <---- invert_high s y; #(ident.fancy_mulhh s) @ (x, ##y))

              (* no literal *)
              ; make_rewrite
                  ((#pident.Z_land @ #?ℤ @ ??{ℤ}) * (#pident.Z_land @ #?ℤ @ ??{ℤ}))
                  (fun mask1 x mask2 y => let s := (2*Z.log2_up mask1)%Z in #(ident.fancy_mulll s) @ (x, y)  when  (mask1 =? 2^(s/2)-1) && (mask2 =? 2^(s/2)-1))
              ; make_rewrite
                  ((#pident.Z_land @ ??{ℤ} @ #?ℤ) * (#pident.Z_land @ #?ℤ @ ??{ℤ}))
                  (fun x mask1 mask2 y => let s := (2*Z.log2_up mask1)%Z in #(ident.fancy_mulll s) @ (x, y)  when  (mask1 =? 2^(s/2)-1) && (mask2 =? 2^(s/2)-1))
              ; make_rewrite
                  ((#pident.Z_land @ #?ℤ @ ??{ℤ}) * (#pident.Z_land @ ??{ℤ} @ #?ℤ))
                  (fun mask1 x y mask2 => let s := (2*Z.log2_up mask1)%Z in #(ident.fancy_mulll s) @ (x, y)  when  (mask1 =? 2^(s/2)-1) && (mask2 =? 2^(s/2)-1))
              ; make_rewrite
                  ((#pident.Z_land @ ??{ℤ} @ #?ℤ) * (#pident.Z_land @ ??{ℤ} @ #?ℤ))
                  (fun x mask1 y mask2 => let s := (2*Z.log2_up mask1)%Z in #(ident.fancy_mulll s) @ (x, y)  when  (mask1 =? 2^(s/2)-1) && (mask2 =? 2^(s/2)-1))
              ; make_rewrite
                  ((#pident.Z_land @ #?ℤ @ ??{ℤ}) * (#pident.Z_shiftr @ ??{ℤ} @ #?ℤ))
                  (fun mask x y offset => let s := (2*offset)%Z in #(ident.fancy_mullh s) @ (x, y)  when  mask =? 2^(s/2)-1)
              ; make_rewrite
                  ((#pident.Z_land @ ??{ℤ} @ #?ℤ) * (#pident.Z_shiftr @ ??{ℤ} @ #?ℤ))
                  (fun x mask y offset => let s := (2*offset)%Z in #(ident.fancy_mullh s) @ (x, y)  when  mask =? 2^(s/2)-1)
              ; make_rewrite
                  ((#pident.Z_shiftr @ ??{ℤ} @ #?ℤ) * (#pident.Z_land @ #?ℤ @ ??{ℤ}))
                  (fun x offset mask y => let s := (2*offset)%Z in #(ident.fancy_mulhl s) @ (x, y)  when  mask =? 2^(s/2)-1)
              ; make_rewrite
                  ((#pident.Z_shiftr @ ??{ℤ} @ #?ℤ) * (#pident.Z_land @ ??{ℤ} @ #?ℤ))
                  (fun x offset y mask => let s := (2*offset)%Z in #(ident.fancy_mulhl s) @ (x, y)  when  mask =? 2^(s/2)-1)
              ; make_rewrite
                  ((#pident.Z_shiftr @ ??{ℤ} @ #?ℤ) * (#pident.Z_shiftr @ ??{ℤ} @ #?ℤ))
                  (fun x offset1 y offset2 => let s := (2*offset1)%Z in #(ident.fancy_mulhh s) @ (x, y)  when  offset1 =? offset2)

            ]%list%pattern%cps%option%under_lets%Z%bool.

        Definition fancy_dtree'
          := Eval compute in @compile_rewrites ident var pattern.ident pattern.ident.arg_types pattern.ident.ident_beq 100 fancy_rewrite_rules.
        Definition fancy_dtree : decision_tree
          := Eval compute in invert_Some fancy_dtree'.
        Definition fancy_default_fuel := Eval compute in List.length fancy_rewrite_rules.

        Import PrimitiveHList.
        Definition fancy_split_rewrite_rules := Eval cbv [split_list projT1 projT2 fancy_rewrite_rules] in split_list fancy_rewrite_rules.
        Definition fancy_pr1_rewrite_rules := Eval hnf in projT1 fancy_split_rewrite_rules.
        Definition fancy_pr2_rewrite_rules := Eval hnf in projT2 fancy_split_rewrite_rules.
        Definition fancy_all_rewrite_rules := combine_hlist (P:=rewrite_ruleTP) fancy_pr1_rewrite_rules fancy_pr2_rewrite_rules.

        Definition fancy_rewrite_head0 do_again {t} (idc : ident t) : value_with_lets t
          := @assemble_identifier_rewriters fancy_dtree fancy_all_rewrite_rules do_again t idc.
      End fancy.
    End with_var.

    Section red_fancy.
      Context (invert_low invert_high : Z (*log2wordmax*) -> Z -> option Z)
              {var : type.type base.type -> Type}
              (do_again : forall t : base.type, @expr base.type ident (@Compile.value base.type ident var) (type.base t)
                                                -> UnderLets.UnderLets base.type ident var (@expr base.type ident var (type.base t)))
              {t} (idc : ident t).

      Time Let rewrite_head1
        := Eval cbv -[fancy_pr2_rewrite_rules
                        base.interp base.try_make_transport_cps
                        type.try_make_transport_cps type.try_transport_cps
                        Let_In
                        UnderLets.splice UnderLets.to_expr
                        Compile.reflect Compile.reify Compile.reify_and_let_binds_cps UnderLets.reify_and_let_binds_base_cps
                        Compile.value' SubstVarLike.is_var_fst_snd_pair_opp
                     ] in @fancy_rewrite_head0 var invert_low invert_high do_again t idc.
      (* Finished transaction in 1.434 secs (1.432u,0.s) (successful) *)

      Time Local Definition fancy_rewrite_head2
        := Eval cbv [id
                       rewrite_head1 fancy_pr2_rewrite_rules
                       projT1 projT2
                       cpsbind cpscall cps_option_bind cpsreturn
                       pattern.ident.arg_types
                       Compile.app_binding_data
                       Compile.app_pbase_type_interp_cps
                       Compile.app_ptype_interp_cps
                       Compile.bind_base_cps
                       Compile.bind_data_cps
                       Compile.binding_dataT
                       Compile.bind_value_cps
                       Compile.eval_decision_tree
                       Compile.eval_rewrite_rules
                       Compile.expr_of_rawexpr
                       Compile.lift_pbase_type_interp_cps
                       Compile.lift_ptype_interp_cps
                       Compile.lift_with_bindings
                       Compile.pbase_type_interp_cps
                       Compile.ptype_interp
                       Compile.ptype_interp_cps
                       (*Compile.reflect*)
                       (*Compile.reify*)
                       Compile.reveal_rawexpr_cps
                       Compile.rValueOrExpr
                       Compile.swap_list
                       Compile.type_of_rawexpr
                       Compile.value
                       (*Compile.value'*)
                       Compile.value_of_rawexpr
                       Compile.value_with_lets
                       Compile.with_bindingsT
                       ident.smart_Literal
                       type.try_transport_cps
                       rlist_rect rlist_rect_cast rwhen
                    ] in rewrite_head1.
      (* Finished transaction in 1.347 secs (1.343u,0.s) (successful) *)

      Local Arguments base.try_make_base_transport_cps _ !_ !_.
      Local Arguments base.try_make_transport_cps _ !_ !_.
      Local Arguments type.try_make_transport_cps _ _ _ !_ !_.
      Local Arguments fancy_rewrite_head2 / .

      Time Definition fancy_rewrite_head
        := Eval cbn [id
                       fancy_rewrite_head2
                       cpsbind cpscall cps_option_bind cpsreturn
                       Compile.reify Compile.reify_and_let_binds_cps Compile.reflect Compile.value'
                       UnderLets.reify_and_let_binds_base_cps
                       UnderLets.splice UnderLets.splice_list UnderLets.to_expr
                       base.interp base.base_interp
                       type.try_make_transport_cps base.try_make_transport_cps base.try_make_base_transport_cps
                       PrimitiveProd.Primitive.fst PrimitiveProd.Primitive.snd Datatypes.fst Datatypes.snd
                    ] in fancy_rewrite_head2.
      (* Finished transaction in 13.298 secs (13.283u,0.s) (successful) *)

      Local Set Printing Depth 1000000.
      Local Set Printing Width 200.
      Local Notation "'llet' x := v 'in' f" := (Let_In v (fun x => f)).
      Redirect "/tmp/fancy_rewrite_head" Print fancy_rewrite_head.
    End red_fancy.

    Section red_nbe.
      Context {var : type.type base.type -> Type}
              (do_again : forall t : base.type, @expr base.type ident (@Compile.value base.type ident var) (type.base t)
                                                -> UnderLets.UnderLets base.type ident var (@expr base.type ident var (type.base t)))
              {t} (idc : ident t).

      Time Let rewrite_head1
        := Eval cbv -[nbe_pr2_rewrite_rules
                        base.interp base.try_make_transport_cps
                        type.try_make_transport_cps type.try_transport_cps
                        Let_In
                        UnderLets.splice UnderLets.to_expr
                        Compile.reflect UnderLets.reify_and_let_binds_base_cps Compile.reify Compile.reify_and_let_binds_cps
                        Compile.value'
                        SubstVarLike.is_var_fst_snd_pair_opp
                     ] in @nbe_rewrite_head0 var do_again t idc.
      (* Finished transaction in 16.593 secs (16.567u,0.s) (successful) *)

      Time Local Definition nbe_rewrite_head2
        := Eval cbv [id
                       rewrite_head1 nbe_pr2_rewrite_rules
                       projT1 projT2
                       cpsbind cpscall cps_option_bind cpsreturn
                       pattern.ident.arg_types
                       Compile.app_binding_data
                       Compile.app_pbase_type_interp_cps
                       Compile.app_ptype_interp_cps
                       Compile.bind_base_cps
                       Compile.bind_data_cps
                       Compile.binding_dataT
                       Compile.bind_value_cps
                       Compile.eval_decision_tree
                       Compile.eval_rewrite_rules
                       Compile.expr_of_rawexpr
                       Compile.lift_pbase_type_interp_cps
                       Compile.lift_ptype_interp_cps
                       Compile.lift_with_bindings
                       Compile.pbase_type_interp_cps
                       Compile.ptype_interp
                       Compile.ptype_interp_cps
                       (*Compile.reflect*)
                       (*Compile.reify*)
                       Compile.reveal_rawexpr_cps
                       Compile.rValueOrExpr
                       Compile.swap_list
                       Compile.type_of_rawexpr
                       Compile.value
                       (*Compile.value'*)
                       Compile.value_of_rawexpr
                       Compile.value_with_lets
                       Compile.with_bindingsT
                       ident.smart_Literal
                       type.try_transport_cps
                       rlist_rect rlist_rect_cast rwhen
                    ] in rewrite_head1.
      (* Finished transaction in 29.683 secs (29.592u,0.048s) (successful) *)

      Local Arguments base.try_make_base_transport_cps _ !_ !_.
      Local Arguments base.try_make_transport_cps _ !_ !_.
      Local Arguments type.try_make_transport_cps _ _ _ !_ !_.
      Local Arguments nbe_rewrite_head2 / .

      Time Definition nbe_rewrite_head
        := Eval cbn [id
                       nbe_rewrite_head2
                       cpsbind cpscall cps_option_bind cpsreturn
                       Compile.reify Compile.reify_and_let_binds_cps Compile.reflect Compile.value'
                       UnderLets.reify_and_let_binds_base_cps
                       UnderLets.splice UnderLets.splice_list UnderLets.to_expr
                       base.interp base.base_interp
                       type.try_make_transport_cps base.try_make_transport_cps base.try_make_base_transport_cps
                       PrimitiveProd.Primitive.fst PrimitiveProd.Primitive.snd Datatypes.fst Datatypes.snd
                    ] in nbe_rewrite_head2.
      (* Finished transaction in 16.561 secs (16.54u,0.s) (successful) *)

      Local Set Printing Depth 1000000.
      Local Set Printing Width 200.
      Local Notation "'llet' x := v 'in' f" := (Let_In v (fun x => f)).
      Redirect "/tmp/nbe_rewrite_head" Print nbe_rewrite_head.
    End red_nbe.

    Section red_arith.
      Context {var : type.type base.type -> Type}
              (do_again : forall t : base.type, @expr base.type ident (@Compile.value base.type ident var) (type.base t)
                                                -> UnderLets.UnderLets base.type ident var (@expr base.type ident var (type.base t)))
              {t} (idc : ident t).

      Time Let rewrite_head1
        := Eval cbv -[arith_pr2_rewrite_rules
                        base.interp base.try_make_transport_cps
                        type.try_make_transport_cps type.try_transport_cps
                        Let_In
                        UnderLets.splice UnderLets.to_expr
                        Compile.reflect UnderLets.reify_and_let_binds_base_cps Compile.reify Compile.reify_and_let_binds_cps
                        Compile.value'
                        SubstVarLike.is_var_fst_snd_pair_opp
                     ] in @arith_rewrite_head0 var do_again t idc.
      (* Finished transaction in 16.593 secs (16.567u,0.s) (successful) *)

      Time Local Definition arith_rewrite_head2
        := Eval cbv [id
                       rewrite_head1 arith_pr2_rewrite_rules
                       projT1 projT2
                       cpsbind cpscall cps_option_bind cpsreturn
                       pattern.ident.arg_types
                       Compile.app_binding_data
                       Compile.app_pbase_type_interp_cps
                       Compile.app_ptype_interp_cps
                       Compile.bind_base_cps
                       Compile.bind_data_cps
                       Compile.binding_dataT
                       Compile.bind_value_cps
                       Compile.eval_decision_tree
                       Compile.eval_rewrite_rules
                       Compile.expr_of_rawexpr
                       Compile.lift_pbase_type_interp_cps
                       Compile.lift_ptype_interp_cps
                       Compile.lift_with_bindings
                       Compile.pbase_type_interp_cps
                       Compile.ptype_interp
                       Compile.ptype_interp_cps
                       (*Compile.reflect*)
                       (*Compile.reify*)
                       Compile.reveal_rawexpr_cps
                       Compile.rValueOrExpr
                       Compile.swap_list
                       Compile.type_of_rawexpr
                       Compile.value
                       (*Compile.value'*)
                       Compile.value_of_rawexpr
                       Compile.value_with_lets
                       Compile.with_bindingsT
                       ident.smart_Literal
                       type.try_transport_cps
                       rlist_rect rlist_rect_cast rwhen
                    ] in rewrite_head1.
      (* Finished transaction in 29.683 secs (29.592u,0.048s) (successful) *)

      Local Arguments base.try_make_base_transport_cps _ !_ !_.
      Local Arguments base.try_make_transport_cps _ !_ !_.
      Local Arguments type.try_make_transport_cps _ _ _ !_ !_.
      Local Arguments arith_rewrite_head2 / .

      Time Definition arith_rewrite_head
        := Eval cbn [id
                       arith_rewrite_head2
                       cpsbind cpscall cps_option_bind cpsreturn
                       Compile.reify Compile.reify_and_let_binds_cps Compile.reflect Compile.value'
                       UnderLets.reify_and_let_binds_base_cps
                       UnderLets.splice UnderLets.splice_list UnderLets.to_expr
                       base.interp base.base_interp
                       type.try_make_transport_cps base.try_make_transport_cps base.try_make_base_transport_cps
                       PrimitiveProd.Primitive.fst PrimitiveProd.Primitive.snd Datatypes.fst Datatypes.snd
                    ] in arith_rewrite_head2.
      (* Finished transaction in 16.561 secs (16.54u,0.s) (successful) *)

      Local Set Printing Depth 1000000.
      Local Set Printing Width 200.
      Local Notation "'llet' x := v 'in' f" := (Let_In v (fun x => f)).
      Redirect "/tmp/arith_rewrite_head" Print arith_rewrite_head.
    End red_arith.

    Definition RewriteNBE {t} (e : expr.Expr (ident:=ident) t) : expr.Expr (ident:=ident) t
      := @Compile.Rewrite (@nbe_rewrite_head) nbe_default_fuel t e.
    Definition RewriteArith {t} (e : expr.Expr (ident:=ident) t) : expr.Expr (ident:=ident) t
      := @Compile.Rewrite (@arith_rewrite_head) arith_default_fuel t e.
    Definition RewriteToFancy
               (invert_low invert_high : Z (*log2wordmax*) -> Z -> option Z)
               {t} (e : expr.Expr (ident:=ident) t) : expr.Expr (ident:=ident) t
      := @Compile.Rewrite (fun var _ => @fancy_rewrite_head invert_low invert_high var) fancy_default_fuel t e.
  End RewriteRules.

  Import defaults.

  Definition PartialEvaluate {t} (e : Expr t) : Expr t := RewriteRules.RewriteNBE e.
End Compilers.