1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
open! Import
module Array = Array0
module Either = Either0
[%%template
[@@@kind_set.define all_ks_non_value = base_non_value]
[@@@kind_set.define all_ks = (all_ks_non_value, value_or_null)]
include List0
include Constructors
module%template Derived = struct
include Container.Derived [@kind.explicit value_or_null]
include Indexed_container.Derived [@kind.explicit value_or_null]
end
let invalid_argf = Printf.invalid_argf
[%%rederive.portable
type ('a : value_or_null) t = 'a list
[@@deriving globalize, sexp ~stackify, sexp_grammar]]
module Or_unequal_lengths = struct
type ('a : value_or_null) t =
| Ok of 'a
| Unequal_lengths
[@@deriving compare ~localize, sexp_of ~stackify]
end
let invariant f t = iter t ~f
let%template of_list t = t [@@alloc __ = (heap, stack)]
let%template singleton x = [ x ] [@exclave_if_stack a] [@@alloc a = (heap, stack)]
let range' ~compare ~stride ?(start = `inclusive) ?(stop = `exclusive) start_i stop_i =
let next_i = stride start_i in
let order x y = Ordering.of_int (compare x y) in
let raise_stride_cannot_return_same_value () =
invalid_arg "List.range': stride function cannot return the same value"
in
let initial_stride_order =
match order start_i next_i with
| Equal -> raise_stride_cannot_return_same_value ()
| Less -> `Less
| Greater -> `Greater
in
let[@tail_mod_cons] rec local_ range_loop i =
let i_to_stop_order = order i stop_i in
match i_to_stop_order, initial_stride_order with
| Less, `Less | Greater, `Greater ->
let next_i = stride i in
(match order i next_i, initial_stride_order with
| Equal, _ -> (raise_stride_cannot_return_same_value [@tailcall false]) ()
| Less, `Greater | Greater, `Less ->
invalid_arg "List.range': stride function cannot change direction"
| Less, `Less | Greater, `Greater -> i :: range_loop next_i)
| Less, `Greater | Greater, `Less ->
[]
| Equal, _ ->
(match stop with
| `inclusive -> [ i ]
| `exclusive -> [])
in
let start_i =
match start with
| `inclusive -> start_i
| `exclusive -> next_i
in
range_loop start_i [@nontail]
;;
let compare_int = Replace_polymorphic_compare.Int_replace_polymorphic_compare.compare
let range ?(stride = 1) ?(start = `inclusive) ?(stop = `exclusive) start_i stop_i =
if stride = 0 then invalid_arg "List.range: stride must be non-zero";
range' ~compare:compare_int ~stride:(fun x -> x + stride) ~start ~stop start_i stop_i
;;
[%%template
[@@@mode.default l = (global, local)]
let hd t =
match[@exclave_if_local l ~reasons:[ May_return_regional; Will_return_unboxed ]] t with
| [] -> None
| x :: _ -> Some x
;;
let[@zero_alloc] hd_or_null t =
match t with
| [] -> Null
| x :: _ -> This x
;;
let tl t =
match[@exclave_if_local l ~reasons:[ May_return_regional; Will_return_unboxed ]] t with
| [] -> None
| _ :: t' -> Some t'
;;
let[@zero_alloc] tl_or_null t =
match t with
| [] -> Null
| _ :: t' -> This t'
;;]
[@@@warning "-incompatible-with-upstream"]
[%%template
[@@@kind k = all_ks]
open struct
type nonrec ('a : any) t = ('a t[@kind.explicit k]) =
| []
| ( :: ) of 'a * ('a t[@kind.explicit k])
end
[@@@kind.default k]
let nth t n : (_ Option0.t[@kind k]) =
if n < 0
then None
else (
let rec nth_aux t n : (_ Option0.t[@kind k]) =
match t with
| [] -> None
| a :: t ->
if n = 0
then Some a [@exclave_if_local l]
else (
let n = n - 1 in
nth_aux t n [@exclave_if_local l])
in
nth_aux t n [@exclave_if_local l])
[@@zero_alloc_if_local l] [@@mode l = (local, global)]
;;
let nth_exn t n =
match[@exclave_if_local l ~reasons:[ May_return_regional ]]
(nth [@kind k] [@mode l]) t n
with
| None ->
(match
(invalid_argf
"List.nth_exn %d called on list of length %d"
n
((length [@kind k]) t)
()
: Nothing0.t)
with
| _ -> .)
| Some a -> a
[@@zero_alloc_if_local l] [@@mode l = (local, global)]
;;
let init_internal n ~f ~name =
if n < 0 then invalid_argf "%s %d" name n ();
(let rec init_loop i accum =
assert (i >= 0);
if i = 0 then accum else init_loop (i - 1) (f (i - 1) :: accum) [@exclave_if_stack a]
in
init_loop n [] [@nontail])
[@exclave_if_stack a]
[@@alloc a = (heap, stack)]
;;
let init n ~f =
(init_internal [@kind k] [@alloc a]) n ~f ~name:"List.init" [@exclave_if_stack a]
[@@alloc a = (heap, stack)]
;;
let init_i64 n ~f =
let open struct
type t = int64#
external ( - ) : t -> t -> t @@ portable = "%int64#_sub"
external ( < ) : t -> t -> bool @@ portable = "%int64#_lessthan"
end in
(let rec init_i64_loop n acc =
let n = n - #1L in
if n < #0L
then acc
else (init_i64_loop [@tailcall]) n (f n :: acc) [@exclave_if_stack a]
in
init_i64_loop n [] [@nontail])
[@exclave_if_stack a]
[@@alloc a = (heap, stack)]
;;
let iteri l ~f =
ignore
((fold [@kind k value_or_null] [@mode l global]) l ~init:0 ~f:(fun i x ->
f i x;
i + 1)
: int)
[@@mode l = (global, local)]
;;
let mem t a ~equal =
let rec mem_loop equal a = function
| [] -> false
| b :: bs -> equal a b || mem_loop equal a bs
in
mem_loop equal a t
[@@mode l = (global, local)]
;;]
let%template[@zero_alloc] nth_or_null t n =
if n < 0
then Null
else (
let rec nth_aux t n : 'a or_null =
match t with
| [] -> Null
| a :: t ->
if n = 0
then This a
else (
let n = n - 1 in
nth_aux t n [@exclave_if_local l])
in
nth_aux t n [@exclave_if_local l])
[@@mode l = (local, global)]
;;
let unordered_append l1 l2 =
match l1, l2 with
| [], l | l, [] -> l
| _ -> rev_append l1 l2
;;
module Check_length2 = struct
type ('a : value_or_null, 'b : value_or_null) t =
| Same_length of int
| Unequal_lengths of
{ shared_length : int
; tail_of_a : 'a list
; tail_of_b : 'b list
}
let%template of_lists l1 l2 =
let rec of_lists2_loop a b shared_length =
match[@exclave_if_local l ~reasons:[ May_return_regional; Will_return_unboxed ]]
a, b
with
| [], [] -> Same_length shared_length
| _ :: a, _ :: b -> of_lists2_loop a b (shared_length + 1)
| [], _ | _, [] -> Unequal_lengths { shared_length; tail_of_a = a; tail_of_b = b }
in
of_lists2_loop l1 l2 0 [@exclave_if_local l]
[@@mode l = (global, local)]
;;
end
let%template check_length2_exn name l1 l2 =
match (Check_length2.of_lists [@mode l]) l1 l2 with
| Same_length _ -> ()
| Unequal_lengths { shared_length; tail_of_a; tail_of_b } ->
invalid_argf
"length mismatch in %s: %d <> %d"
name
(shared_length + length tail_of_a)
(shared_length + length tail_of_b)
()
[@@mode l = (global, local)]
;;
let%template check_length2 (l1 @ li) (l2 @ li) ~f @ lo =
(match[@exclave_if_local lo ~reasons:[ May_return_local ]]
(Check_length2.of_lists [@mode li]) l1 l2
with
| Same_length _ -> Or_unequal_lengths.Ok (f l1 l2)
| Unequal_lengths _ -> Unequal_lengths)
[@@mode li = (global, local), lo = (global, local)]
;;
module Check_length3 = struct
type ('a : value_or_null, 'b : value_or_null, 'c : value_or_null) t =
| Same_length of int
| Unequal_lengths of
{ shared_length : int
; tail_of_a : 'a list
; tail_of_b : 'b list
; tail_of_c : 'c list
}
let of_lists l1 l2 l3 =
let rec of_lists3_loop a b c shared_length =
match a, b, c with
| [], [], [] -> Same_length shared_length
| _ :: a, _ :: b, _ :: c -> of_lists3_loop a b c (shared_length + 1)
| [], _, _ | _, [], _ | _, _, [] ->
Unequal_lengths { shared_length; tail_of_a = a; tail_of_b = b; tail_of_c = c }
in
of_lists3_loop l1 l2 l3 0
;;
end
let check_length3_exn name l1 l2 l3 =
match Check_length3.of_lists l1 l2 l3 with
| Same_length _ -> ()
| Unequal_lengths { shared_length; tail_of_a; tail_of_b; tail_of_c } ->
let n1 = shared_length + length tail_of_a in
let n2 = shared_length + length tail_of_b in
let n3 = shared_length + length tail_of_c in
invalid_argf "length mismatch in %s: %d <> %d || %d <> %d" name n1 n2 n2 n3 ()
;;
let check_length3 l1 l2 l3 ~f =
match Check_length3.of_lists l1 l2 l3 with
| Same_length _ -> Or_unequal_lengths.Ok (f l1 l2 l3)
| Unequal_lengths _ -> Unequal_lengths
;;
[%%template
[@@@mode.default l = (global, local)]
let iter2 l1 l2 ~f =
(check_length2 [@mode l l])
l1
l2
~f:((iter2_ok [@mode l]) ~f)
[@nontail] [@exclave_if_local l ~reasons:[ Will_return_unboxed ]]
;;
let iter2_exn l1 l2 ~f =
(check_length2_exn [@mode l]) "iter2_exn" l1 l2;
(iter2_ok [@mode l]) l1 l2 ~f
;;]
let rev_map2 l1 l2 ~f = check_length2 l1 l2 ~f:(rev_map2_ok ~f) [@nontail]
let rev_map2_exn l1 l2 ~f =
check_length2_exn "rev_map2_exn" l1 l2;
rev_map2_ok l1 l2 ~f
;;
let fold2 l1 l2 ~init ~f = check_length2 l1 l2 ~f:(fold2_ok ~init ~f) [@nontail]
let fold2_exn l1 l2 ~init ~f =
check_length2_exn "fold2_exn" l1 l2;
fold2_ok l1 l2 ~init ~f
;;
let fold_right2 l1 l2 ~f ~init =
check_length2 l1 l2 ~f:(fold_right2_ok ~f ~init) [@nontail]
;;
let fold_right2_exn l1 l2 ~f ~init =
check_length2_exn "fold_right2_exn" l1 l2;
fold_right2_ok l1 l2 ~f ~init
;;
let for_all2 l1 l2 ~f = check_length2 l1 l2 ~f:(for_all2_ok ~f) [@nontail]
let for_all2_exn l1 l2 ~f =
check_length2_exn "for_all2_exn" l1 l2;
for_all2_ok l1 l2 ~f
;;
let exists2 l1 l2 ~f = check_length2 l1 l2 ~f:(exists2_ok ~f) [@nontail]
let exists2_exn l1 l2 ~f =
check_length2_exn "exists2_exn" l1 l2;
exists2_ok l1 l2 ~f
;;
let rev_filter t ~f =
let rec find ~f accu = function
| [] -> accu
| x :: l -> if f x then find ~f (x :: accu) l else find ~f accu l
in
find ~f [] t
;;
let filteri l ~f =
let[@tail_mod_cons] rec local_ filteri_loop pos l =
match l with
| [] -> []
| hd :: tl ->
if f pos hd then hd :: filteri_loop (pos + 1) tl else filteri_loop (pos + 1) tl
in
filteri_loop 0 l [@nontail]
;;
let%template[@tail_mod_cons] rec filter l ~f =
match l with
| [] -> []
| hd :: tl -> if f hd then hd :: filter tl ~f else filter tl ~f
[@@alloc a = heap]
;;
[%%template
[@@@mode.default li = (global, local), lo = (global, local)]
let find_map t ~f =
(let rec find_map_loop = function
| [] -> None
| x :: l ->
(match[@exclave_if_local lo ~reasons:[ May_return_local ]] f x with
| None -> find_map_loop l
| Some _ as r -> r)
in
find_map_loop t [@nontail])
[@exclave_if_local lo ~reasons:[ May_return_local ]]
;;]
let%template find_map_or_null t ~f =
(let rec find_map_or_null_loop = function
| [] -> Null
| x :: l ->
(match[@exclave_if_local lo ~reasons:[ May_return_local ]] f x with
| Null -> find_map_or_null_loop l
| This _ as r -> r)
in
find_map_or_null_loop t [@nontail])
[@exclave_if_local lo ~reasons:[ May_return_local ]]
[@@mode li = (global, local), lo = (global, local)]
;;
let find_map_or_null_exn =
let not_found = Not_found_s (Atom "List.find_map_or_null_exn: not found") in
let find_map_or_null_exn t ~f =
match find_map_or_null t ~f with
| Null ->
raise (Portability_hacks.magic_uncontended__promise_deeply_immutable not_found)
| This x -> x
in
find_map_or_null_exn
;;
let find_map_exn =
let not_found = Not_found_s (Atom "List.find_map_exn: not found") in
let find_map_exn t ~f =
match find_map t ~f with
| None ->
raise (Portability_hacks.magic_uncontended__promise_deeply_immutable not_found)
| Some x -> x
in
find_map_exn
;;
[%%template
[@@@mode.default l = (global, local)]
let find t ~f =
(let rec find_loop = function
| [] -> None
| x :: l ->
if f x then Some x [@exclave_if_local l] else find_loop l [@exclave_if_local l]
in
find_loop t [@nontail])
[@exclave_if_local l ~reasons:[ May_return_regional; Will_return_unboxed ]]
;;
let find_or_null t ~f =
(let rec find_or_null_loop = function
| [] -> Null
| x :: l -> if f x then This x else find_or_null_loop l [@exclave_if_local l]
in
find_or_null_loop t [@nontail])
[@exclave_if_local l ~reasons:[ May_return_regional ]]
;;
let find_exn =
let not_found = Not_found_s (Atom "List.find_exn: not found") in
let rec find_exn t ~f =
match[@exclave_if_local l ~reasons:[ May_return_regional ]] t with
| [] ->
raise (Portability_hacks.magic_uncontended__promise_deeply_immutable not_found)
| x :: t -> if f x then x else find_exn t ~f
in
find_exn
;;
let findi t ~f =
(let rec findi_loop i t =
match[@exclave_if_local l ~reasons:[ May_return_regional; Will_return_unboxed ]]
t
with
| [] -> None
| x :: l -> if f i x then Some (i, x) else findi_loop (i + 1) l
in
findi_loop 0 t [@nontail])
[@exclave_if_local l ~reasons:[ May_return_regional; Will_return_unboxed ]]
;;
let findi_exn =
let not_found = Not_found_s (Atom "List.findi_exn: not found") in
let findi_exn t ~f =
match[@exclave_if_local l ~reasons:[ May_return_regional; Will_return_unboxed ]]
(findi [@mode l]) t ~f
with
| None ->
raise (Portability_hacks.magic_uncontended__promise_deeply_immutable not_found)
| Some x -> x
in
findi_exn
;;]
[%%template
[@@@mode.default li = (global, local), lo = (global, local)]
let find_mapi t ~f =
(let rec find_mapi_loop i t =
match[@exclave_if_local lo ~reasons:[ May_return_local ]] t with
| [] -> None
| x :: l ->
(match f i x with
| Some _ as result -> result
| None ->
let next = i + 1 in
find_mapi_loop next l)
in
find_mapi_loop 0 t [@nontail])
[@exclave_if_local lo ~reasons:[ May_return_local ]]
;;
let find_mapi_or_null t ~f =
(let rec find_mapi_or_null_loop i t =
match[@exclave_if_local lo ~reasons:[ May_return_local ]] t with
| [] -> Null
| x :: l ->
(match f i x with
| This _ as result -> result
| Null ->
let next = i + 1 in
find_mapi_or_null_loop next l)
in
find_mapi_or_null_loop 0 t [@nontail])
[@exclave_if_local lo ~reasons:[ May_return_local ]]
;;]
let find_mapi_or_null_exn =
let not_found = Not_found_s (Atom "List.find_mapi_or_null_exn: not found") in
let find_mapi_or_null_exn t ~f =
match find_mapi_or_null t ~f with
| Null ->
raise (Portability_hacks.magic_uncontended__promise_deeply_immutable not_found)
| This x -> x
in
find_mapi_or_null_exn
;;
let find_mapi_exn =
let not_found = Not_found_s (Atom "List.find_mapi_exn: not found") in
let find_mapi_exn t ~f =
match find_mapi t ~f with
| None ->
raise (Portability_hacks.magic_uncontended__promise_deeply_immutable not_found)
| Some x -> x
in
find_mapi_exn
;;
let%template for_alli t ~f =
let rec for_alli_loop i t =
match t with
| [] -> true
| hd :: tl -> f i hd && for_alli_loop (i + 1) tl
in
for_alli_loop 0 t [@nontail]
[@@mode l = (global, local)]
;;
let%template existsi t ~f =
let rec existsi_loop i t =
match t with
| [] -> false
| hd :: tl -> f i hd || existsi_loop (i + 1) tl
in
existsi_loop 0 t [@nontail]
[@@mode l = (global, local)]
;;
let fold_left = fold
let%template of_array = (Array.to_list [@alloc a]) [@@alloc a = (heap, stack)]
let to_array = Array.of_list
let%template to_list t = t [@@alloc __ = (heap, stack)]
(** Tail recursive versions of standard [List] module *)
let[@tail_mod_cons] rec append_loop l1 l2 =
match l1 with
| [] -> l2
| [ x1 ] -> x1 :: l2
| [ x1; x2 ] -> x1 :: x2 :: l2
| [ x1; x2; x3 ] -> x1 :: x2 :: x3 :: l2
| [ x1; x2; x3; x4 ] -> x1 :: x2 :: x3 :: x4 :: l2
| x1 :: x2 :: x3 :: x4 :: x5 :: tl ->
x1 :: x2 :: x3 :: x4 :: x5 :: (append_loop [@tailcall]) tl l2
;;
let%template append l1 l2 =
match l2 with
| [] -> l1
| _ :: _ -> (append_loop [@alloc a]) l1 l2 [@exclave_if_stack a]
[@@alloc a = heap]
;;
let%template append l1 l2 =
match (l2 : (_ t[@kind k])) with
| [] -> l1
| _ :: _ -> (rev [@kind k]) ((rev_append [@kind k]) l2 ((rev [@kind k]) l1))
[@@kind k = all_ks_non_value] [@@alloc a = heap]
;;
let%template append =
let append_stack_tail l1 l2 = exclave_
(rev_append [@kind k] [@alloc a]) ((rev [@kind k] [@alloc a]) l1) l2
in
let rec append_stack_loop l1 l2 ~depth =
match (l1 : (_ t[@kind k])) with
| [] -> l2
| x :: xs ->
exclave_
let xs =
if depth <= max_non_tailcall
then append_stack_loop xs l2 ~depth:(depth + 1)
else (append_stack_tail [@inlined never]) xs l2
in
x :: xs
in
fun [@zero_alloc] l1 l2 ->
match (l2 : (_ t[@kind k])) with
| [] -> l1
| _ :: _ -> exclave_ append_stack_loop l1 l2 ~depth:0
[@@kind k = all_ks] [@@alloc a = stack]
;;
let%template filteri_internal t ~f = exclave_
let rec filteri_stack_tail i xs ~acc = exclave_
match (xs : (_ t[@kind k])) with
| [] -> (rev [@kind k] [@alloc stack]) acc
| x :: xs ->
let acc : (_ t[@kind k]) = if f #(i, x) then x :: acc else acc in
(filteri_stack_tail [@tailcall]) (i + 1) xs ~acc
in
let rec filteri_stack_loop i xs : (_ t[@kind k]) =
match (xs : (_ t[@kind k])) with
| [] -> []
| x :: xs ->
exclave_
let res = f #(i, x) in
let i = i + 1 in
(match res with
| false -> (filteri_stack_loop [@tailcall]) i xs
| true ->
let xs =
if i <= max_non_tailcall
then filteri_stack_loop i xs
else (filteri_stack_tail [@inlined never]) i xs ~acc:[]
in
x :: xs)
in
filteri_stack_loop 0 t
[@@kind k = all_ks] [@@alloc stack]
;;
let%template filteri t ~f = exclave_
(filteri_internal [@kind k] [@alloc stack] [@inlined]) t ~f:(fun #(i, x) -> f i x)
[@@kind k = all_ks] [@@alloc stack]
;;
let%template[@inline] filter t ~f = exclave_
(filteri_internal [@kind k] [@alloc a] [@inlined]) t ~f:(fun #(_, x) -> f x)
[@@kind k = all_ks] [@@alloc a = stack]
;;
[%%template
[@@@kind.default ka = all_ks, kb = all_ks]
[@@@mode.default li = (global, local)]
let rev_mapi l ~f =
(let rec rev_mapi_loop i acc : (_ t[@kind ka]) @ li -> (_ t[@kind kb]) @ lo = function
| [] -> acc
| h :: t -> rev_mapi_loop (i + 1) (f i h :: acc) t [@exclave_if_stack a]
in
rev_mapi_loop 0 [] l [@nontail])
[@exclave_if_stack a]
[@@alloc a @ lo = (heap_global, stack_local)]
;;
let mapi_internal l ~f = exclave_
let rec mapi_stack_tail i xs ~acc : (_ t[@kind kb]) = exclave_
match (xs : (_ t[@kind ka])) with
| [] -> (rev [@kind kb] [@alloc a]) acc
| x :: xs -> (mapi_stack_tail [@tailcall]) (i + 1) xs ~acc:(f #(i, x) :: acc)
in
let rec mapi_stack_loop i xs : (_ t[@kind kb]) =
match (xs : (_ t[@kind ka])) with
| [] -> []
| x :: xs ->
exclave_
let y = f #(i, x) in
let i = i + 1 in
let ys =
if i <= max_non_tailcall
then mapi_stack_loop i xs
else (mapi_stack_tail [@inlined never]) i xs ~acc:[]
in
y :: ys
in
mapi_stack_loop 0 l
[@@alloc a = stack]
;;
let mapi l ~f = exclave_
(mapi_internal [@kind ka kb] [@mode li] [@alloc stack] [@inlined])
l
~f:(fun #(i, x) -> exclave_ f i x)
[@@alloc a = stack]
;;
let[@inline] map t ~f = exclave_
(mapi_internal [@kind ka kb] [@mode li] [@alloc a] [@inlined])
t
~f:(fun #(_, x) -> exclave_ f x)
[@@alloc a = stack]
;;
let filter_mapi_internal t ~f = exclave_
let rec filter_mapi_stack_tail i (xs @ li) ~acc = exclave_
match (xs : (_ t[@kind ka])) with
| [] -> (rev [@kind kb] [@alloc a]) acc
| x :: xs ->
let acc =
match (f #(i, x) : (_ Option0.t[@kind kb])) with
| None -> acc
| Some y -> y :: acc
in
(filter_mapi_stack_tail [@tailcall]) (i + 1) xs ~acc
in
let rec filter_mapi_stack_loop i (xs @ li) : (_ t[@kind kb]) =
match (xs : (_ t[@kind ka])) with
| [] -> []
| x :: xs ->
exclave_
let y = f #(i, x) in
let i = i + 1 in
(match (y : (_ Option0.t[@kind kb])) with
| None -> (filter_mapi_stack_loop [@tailcall]) i xs
| Some y ->
let ys =
if i <= max_non_tailcall
then filter_mapi_stack_loop i xs
else (filter_mapi_stack_tail [@inlined never]) i xs ~acc:[]
in
y :: ys)
in
filter_mapi_stack_loop 0 t
[@@alloc a = stack]
;;
let filter_mapi t ~f = exclave_
(filter_mapi_internal [@kind ka kb] [@mode li] [@alloc stack] [@inlined])
t
~f:(fun #(i, x) -> exclave_ f i x)
[@@alloc a = stack]
;;
let filter_map t ~f = exclave_
(filter_mapi_internal [@kind ka kb] [@mode li] [@alloc a] [@inlined])
t
~f:(fun #(_, x) -> exclave_ f x)
[@@alloc a = stack]
;;
[@@@alloc.default a @ lo = stack_local]
let concat_mapi_internal t ~f = exclave_
let concat_mapi_stack_tail (t : (_ t[@kind ka])) ~i = exclave_
let #(_, expanded) =
(fold [@kind ka (value_or_null & value_or_null)] [@mode li lo])
t
~init:#(i, ([] : ('b t[@kind kb])))
~f:(fun #(i, acc) x -> exclave_
#(i + 1, (rev_append [@kind kb] [@alloc a]) (f #(i, x)) acc))
in
(rev [@kind kb] [@alloc a]) expanded
in
let rec concat_mapi_stack_loop xs ~i =
match (xs : (_ t[@kind ka])) with
| [] -> ([] : (_ t[@kind kb]))
| x :: xs ->
exclave_
let expanded_hd = f #(i, x) in
let expanded_tl =
if i <= max_non_tailcall
then concat_mapi_stack_loop xs ~i:(i + 1)
else (concat_mapi_stack_tail [@inlined never]) xs ~i:(i + 1)
in
(append [@kind kb] [@alloc a]) expanded_hd expanded_tl
in
concat_mapi_stack_loop t ~i:0
;;
let concat_mapi t ~f = exclave_
(concat_mapi_internal [@kind ka kb] [@mode li] [@alloc a] [@inlined])
t
~f:(fun #(i, x) -> exclave_ f i x)
;;
let concat_map l ~f = exclave_
(concat_mapi_internal [@kind ka kb] [@mode li] [@alloc a] [@inlined])
l
~f:(fun #(_, x) -> exclave_ f x)
;;]
[%%template
[@@@kind.default ka = all_ks, kb = all_ks]
let foldi t ~init ~f =
let #(_, r) =
(fold [@kind ka (value_or_null & kb)] [@mode li lo])
t
~init:#(0, init)
~f:(fun #(i, acc) v -> #(i + 1, f i acc v))
in
r
[@@mode li = (global, local), lo = global]
;;
let foldi t ~init ~f = exclave_
let #(_, r) =
(fold [@kind ka (value_or_null & kb)] [@mode li lo])
t
~init:#(0, init)
~f:(fun #(i, acc) v -> exclave_ #(i + 1, f i acc v))
in
r
[@@mode li = (global, local), lo = local]
;;]
[%%template
[@@@kind.default ka = all_ks]
[@@@mode.default li = (global, local)]
let filter_mapi_or_null_internal t ~f = exclave_
let rec filter_mapi_or_null_stack_tail i (xs @ li) ~acc = exclave_
match (xs : (_ t[@kind ka])) with
| [] -> (rev [@alloc a]) acc
| x :: xs ->
let acc =
match (f #(i, x) : _ or_null @ local) with
| Null -> acc
| This y -> y :: acc
in
(filter_mapi_or_null_stack_tail [@tailcall]) (i + 1) xs ~acc
in
let rec filter_mapi_or_null_stack_loop i (xs @ li) : _ t =
match (xs : (_ t[@kind ka])) with
| [] -> []
| x :: xs ->
exclave_
let y = f #(i, x) in
let i = i + 1 in
(match (y : _ or_null @ local) with
| Null -> (filter_mapi_or_null_stack_loop [@tailcall]) i xs
| This y ->
let ys =
if i <= max_non_tailcall
then filter_mapi_or_null_stack_loop i xs
else (filter_mapi_or_null_stack_tail [@inlined never]) i xs ~acc:[]
in
y :: ys)
in
filter_mapi_or_null_stack_loop 0 t
[@@alloc a = stack]
;;
let filter_mapi_or_null t ~f = exclave_
(filter_mapi_or_null_internal [@kind ka] [@mode li] [@alloc stack] [@inlined])
t
~f:(fun #(i, x) -> exclave_ f i x)
[@@alloc a = stack]
;;
let[@inline] filter_map_or_null t ~f = exclave_
(filter_mapi_or_null_internal [@kind ka] [@mode li] [@alloc a] [@inlined])
t
~f:(fun #(_, x) -> exclave_ f x)
[@@alloc a = stack]
;;]
[%%template
[@@@kind.default ka = all_ks, kb = value_or_null]
open struct
type nonrec ('a : any) t = ('a t[@kind ka]) =
| []
| ( :: ) of 'a * ('a t[@kind ka])
[@@kind ka]
end
[@@@mode.default li = (global, local)]
[@@@alloc.default a = heap]
let mapi l ~f =
let[@tail_mod_cons] rec local_ mapi_loop i = function
| [] -> ([] : (_ t[@kind kb]))
| h :: t -> f i h :: mapi_loop (i + 1) t
in
mapi_loop 0 l [@nontail]
;;
let map l ~f =
let[@tail_mod_cons] rec map_loop l : (_ t[@kind kb]) =
match l with
| [] -> []
| x :: tl -> f x :: (map_loop [@tailcall]) tl
in
map_loop l [@nontail]
;;
let filter_mapi l ~f =
let[@tail_mod_cons] rec local_ filter_mapi_loop pos l : (_ t[@kind kb]) =
match l with
| [] -> []
| hd :: tl ->
(match (f pos hd : (_ Option0.t[@kind kb])) with
| None -> filter_mapi_loop (pos + 1) tl
| Some x -> x :: filter_mapi_loop (pos + 1) tl)
in
filter_mapi_loop 0 l [@nontail]
;;
let filter_map l ~f =
let[@tail_mod_cons] rec filter_map_loop l : (_ t[@kind kb]) =
match l with
| [] -> []
| hd :: tl ->
(match (f hd : (_ Option0.t[@kind kb])) with
| None -> filter_map_loop tl
| Some x -> x :: filter_map_loop tl)
in
filter_map_loop l [@nontail]
;;
let concat_mapi l ~(local_ f) =
let[@tail_mod_cons] rec local_ concat_mapi_outer_loop pos = function
| [] -> ([] : (_ t[@kind kb]))
| [ hd ] -> (f [@tailcall false]) pos hd
| hd :: (_ :: _ as tl) -> concat_mapi_inner_loop (pos + 1) (f pos hd) tl
and[@tail_mod_cons] local_ concat_mapi_inner_loop pos l1 l2 =
match l1 with
| [] -> concat_mapi_outer_loop pos l2
| [ x1 ] -> x1 :: concat_mapi_outer_loop pos l2
| [ x1; x2 ] -> x1 :: x2 :: concat_mapi_outer_loop pos l2
| [ x1; x2; x3 ] -> x1 :: x2 :: x3 :: concat_mapi_outer_loop pos l2
| [ x1; x2; x3; x4 ] -> x1 :: x2 :: x3 :: x4 :: concat_mapi_outer_loop pos l2
| x1 :: x2 :: x3 :: x4 :: x5 :: tl ->
x1 :: x2 :: x3 :: x4 :: x5 :: concat_mapi_inner_loop pos tl l2
in
concat_mapi_outer_loop 0 l [@nontail]
;;
let concat_map l ~(f @ local) =
(concat_mapi [@kind ka kb] [@mode li] [@alloc a]) l ~f:(fun _ x ->
f x [@exclave_if_stack a])
[@nontail] [@exclave_if_stack a]
;;]
[%%template
[@@@kind.default ka = all_ks]
[@@@mode.default li = (global, local)]
let filter_mapi_or_null l ~f =
let[@tail_mod_cons] rec local_ filter_mapi_or_null_loop pos (l : (_ t[@kind ka])) : _ t =
match l with
| [] -> []
| hd :: tl ->
(match (f pos hd : _ or_null) with
| Null -> filter_mapi_or_null_loop (pos + 1) tl
| This x -> x :: filter_mapi_or_null_loop (pos + 1) tl)
in
filter_mapi_or_null_loop 0 l [@nontail]
[@@alloc a = heap]
;;
let filter_map_or_null l ~f =
let[@tail_mod_cons] rec filter_map_or_null_loop (l : (_ t[@kind ka])) ~f : _ t =
match l with
| [] -> []
| hd :: tl ->
(match (f hd : _ or_null) with
| Null -> filter_map_or_null_loop tl ~f
| This x -> x :: filter_map_or_null_loop tl ~f)
in
filter_map_or_null_loop l ~f
[@@alloc a = heap]
;;]
[%%template
[@@@kind.default k = all_ks_non_value]
let sexp_of_t sexp_of__a t : Sexplib0.Sexp.t =
List ((map [@kind k value_or_null]) ~f:sexp_of__a t)
;;
let sexp_of_t : _ -> (_ t[@kind k]) @ local -> Sexplib0.Sexp.t @ local =
fun sexp_of__a t ->
let map ~f lst = exclave_
let rec rev lst acc = exclave_
match lst with
| [] -> acc
| hd :: tl -> rev tl (hd :: acc)
in
let rec rev_map (lst : (_ t[@kind k])) acc = exclave_
match lst with
| [] -> acc
| hd :: tl -> rev_map tl (f hd :: acc)
in
rev (rev_map lst []) []
in
exclave_ List (map ~f:sexp_of__a t)
[@@mode __ = stack]
;;]
[%%template
[@@@kind.default k = all_ks_non_value]
open struct
type nonrec ('a : any) t = ('a t[@kind k]) =
| []
| ( :: ) of 'a * ('a t[@kind k])
[@@kind k]
end
let filter l ~f =
let rec local_ filter_loop ~acc l =
match l with
| [] -> acc
| hd :: tl ->
let acc = if f hd then hd :: acc else acc in
(filter_loop [@tailcall]) ~acc tl
in
filter_loop ~acc:[] l |> (rev [@kind k])
;;
let filteri l ~f =
let rec local_ filteri_loop ~acc pos l =
match l with
| [] -> acc
| hd :: tl ->
let acc = if f pos hd then hd :: acc else acc in
(filteri_loop [@tailcall]) ~acc (pos + 1) tl
in
(filteri_loop ~acc:[] 0 l |> (rev [@kind k])) [@nontail]
;;]
[%%template
[@@@kind.default ka = all_ks, kb = all_ks_non_value]
[@@@mode.default li = (global, local)]
[@@@alloc.default a = heap]
open struct
type nonrec ('a : any) t = ('a t[@kind ka]) =
| []
| ( :: ) of 'a * ('a t[@kind ka])
[@@kind ka]
end
let map (l : (_ t[@kind ka]) @ li) ~(f @ local) : (_ t[@kind kb]) =
match l with
| [] -> []
| [ x ] -> [ f x ]
| _ :: _ ->
((rev_map [@kind ka kb] [@mode li] [@alloc a]) l ~f |> (rev [@kind kb])) [@nontail]
;;
let mapi (l : (_ t[@kind ka])) ~(local_ f) : (_ t[@kind kb]) =
match l with
| [] -> []
| [ x ] -> [ f 0 x ]
| _ :: _ ->
((rev_mapi [@kind ka kb] [@mode li] [@alloc a]) l ~f |> (rev [@kind kb])) [@nontail]
;;
let filter_map l ~f =
let rec local_ filter_map_loop ~acc : (_ t[@kind ka]) @ li -> (_ t[@kind kb]) = function
| [] -> acc
| hd :: tl ->
(match (f hd : (_ Option0.t[@kind kb])) with
| Some hd -> (filter_map_loop [@tailcall]) ~acc:(hd :: acc) tl
| None -> (filter_map_loop [@tailcall]) ~acc tl)
in
(filter_map_loop ~acc:[] l |> (rev [@kind kb])) [@nontail]
;;
let filter_mapi l ~f =
let rec local_ filter_mapi_loop ~acc pos : (_ t[@kind ka]) @ li -> (_ t[@kind kb])
= function
| [] -> acc
| hd :: tl ->
(match (f pos hd : (_ Option0.t[@kind kb])) with
| Some hd -> (filter_mapi_loop [@tailcall]) ~acc:(hd :: acc) (pos + 1) tl
| None -> (filter_mapi_loop [@tailcall]) ~acc (pos + 1) tl)
in
(filter_mapi_loop ~acc:[] 0 l |> (rev [@kind kb])) [@nontail]
;;
let concat_mapi l ~(local_ f) =
let rec local_ concat_mapi_loop ~acc pos : (_ t[@kind ka]) @ li -> (_ t[@kind kb])
= function
| [] -> []
| [ hd ] -> (rev_append [@kind kb]) acc (f pos hd)
| hd :: (_ :: _ as tl) ->
let acc = (rev_append [@kind kb]) (f pos hd) acc in
(concat_mapi_loop [@tailcall]) ~acc (pos + 1) tl
in
concat_mapi_loop ~acc:[] 0 l [@nontail]
;;
let concat_map l ~(local_ f) =
let rec local_ concat_map_loop ~acc : (_ t[@kind ka]) @ li -> (_ t[@kind kb]) = function
| [] -> []
| [ hd ] -> (rev_append [@kind kb]) acc (f hd)
| hd :: (_ :: _ as tl) ->
let acc = (rev_append [@kind kb]) (f hd) acc in
(concat_map_loop [@tailcall]) ~acc tl
in
concat_map_loop ~acc:[] l [@nontail]
;;]
let folding_map t ~init ~f =
let acc = ref init in
map t ~f:(fun x ->
let new_acc, y = f !acc x in
acc := new_acc;
y)
[@nontail]
;;
let%template fold_map t ~init ~f =
let acc = ref init in
let result =
(map [@mode li] [@alloc heap]) t ~f:(fun x ->
let new_acc, y = f !acc x in
acc := new_acc;
y)
in
!acc, result
[@@mode li = (global, local)] [@@alloc a = heap]
;;
let%template fold_map t ~init ~f = exclave_
let accum, rev_list_accum =
(fold [@mode li local])
t
~init:(init, [])
~f:(fun (accum, rev_list_accum) x -> exclave_
let accum, x = f accum x in
accum, x :: rev_list_accum)
in
accum, (rev [@alloc stack]) rev_list_accum
[@@mode li = (global, local)] [@@alloc a = stack]
;;
let ( >>| ) l f = map l ~f
[%%template
[@@@mode.default li = (global, local)]
let[@tail_mod_cons] rec map2_ok l1 l2 ~f =
match l1, l2 with
| [], [] -> []
| x1 :: l1, x2 :: l2 -> f x1 x2 :: (map2_ok [@mode li] [@alloc a]) l1 l2 ~f
| _, _ -> invalid_arg "List.map2"
[@@alloc a = heap]
;;
let map2_ok l1 l2 ~f = exclave_
let rec map2_ok_stack_tail l1 l2 ~acc = exclave_
match l1, l2 with
| [], [] -> (rev [@alloc stack]) acc
| x1 :: l1, x2 :: l2 -> (map2_ok_stack_tail [@tailcall]) l1 l2 ~acc:(f x1 x2 :: acc)
| _, _ -> invalid_arg "List.map2"
in
let rec map2_ok_stack_loop l1 l2 ~depth =
match l1, l2 with
| [], [] -> []
| x1 :: l1, x2 :: l2 ->
exclave_
let x = f x1 x2 in
let l =
if depth <= max_non_tailcall
then map2_ok_stack_loop l1 l2 ~depth:(depth + 1)
else (map2_ok_stack_tail [@inlined never]) l1 l2 ~acc:[]
in
x :: l
| _, _ -> invalid_arg "List.map2"
in
map2_ok_stack_loop l1 l2 ~depth:0
[@@alloc a = stack]
;;
[@@@alloc.default a @ lo = (heap_global, stack_local)]
let map2 l1 l2 ~f =
(check_length2 [@mode li lo])
l1
l2
~f:((map2_ok [@mode li] [@alloc a]) ~f) [@nontail] [@exclave_if_stack a]
;;
let map2_exn l1 l2 ~f =
(check_length2_exn [@mode li]) "map2_exn" l1 l2;
(map2_ok [@mode li] [@alloc a]) l1 l2 ~f [@exclave_if_stack a]
;;]
let rev_map3_ok l1 l2 l3 ~f =
let rec rev_map3_ok_loop l1 l2 l3 ac =
match l1, l2, l3 with
| [], [], [] -> ac
| x1 :: l1, x2 :: l2, x3 :: l3 -> rev_map3_ok_loop l1 l2 l3 (f x1 x2 x3 :: ac)
| _ -> assert false
in
rev_map3_ok_loop l1 l2 l3 [] [@nontail]
;;
let rev_map3 l1 l2 l3 ~f = check_length3 l1 l2 l3 ~f:(rev_map3_ok ~f) [@nontail]
let rev_map3_exn l1 l2 l3 ~f =
check_length3_exn "rev_map3_exn" l1 l2 l3;
rev_map3_ok l1 l2 l3 ~f
;;
let[@tail_mod_cons] rec map3_ok l1 l2 l3 ~f =
match l1, l2, l3 with
| [], [], [] -> []
| x1 :: l1, x2 :: l2, x3 :: l3 -> f x1 x2 x3 :: map3_ok l1 l2 l3 ~f
| _, _, _ -> invalid_arg "List.map3"
;;
let map3 l1 l2 l3 ~f = check_length3 l1 l2 l3 ~f:(map3_ok ~f) [@nontail]
let map3_exn l1 l2 l3 ~f =
check_length3_exn "map3_exn" l1 l2 l3;
map3_ok l1 l2 l3 ~f
;;
let rec rev_map_append l1 l2 ~f =
match l1 with
| [] -> l2
| h :: t -> rev_map_append ~f t (f h :: l2)
;;
let%template unzip list =
let rec unzip_loop list l1 l2 =
match list with
| [] -> l1, l2
| (x, y) :: tl -> unzip_loop tl (x :: l1) (y :: l2)
in
unzip_loop (rev list) [] []
[@@alloc a = heap]
;;
let%template unzip list = exclave_
let rec unzip_stack_tail ~xs ~ys = function
| [] -> exclave_ #((rev [@alloc stack]) xs, (rev [@alloc stack]) ys)
| (x, y) :: rest ->
exclave_ (unzip_stack_tail [@tailcall]) ~xs:(x :: xs) ~ys:(y :: ys) rest
[@@zero_alloc]
in
let rec unzip_stack_loop ~depth = function
| [] -> #([], [])
| (x, y) :: rest ->
exclave_
let #(xs, ys) =
if depth <= max_non_tailcall
then unzip_stack_loop ~depth:(depth + 1) rest
else (unzip_stack_tail [@inlined never]) ~xs:[] ~ys:[] rest
in
#(x :: xs, y :: ys)
[@@zero_alloc]
in
let #(xs, ys) = unzip_stack_loop ~depth:0 list in
xs, ys
[@@zero_alloc] [@@alloc a = stack]
;;
let unzip3 list =
let rec unzip3_loop list l1 l2 l3 =
match list with
| [] -> l1, l2, l3
| (x, y, z) :: tl -> unzip3_loop tl (x :: l1) (y :: l2) (z :: l3)
in
unzip3_loop (rev list) [] [] []
;;
[%%template
[@@@alloc.default a @ l = (heap_global, stack_local)]
let zip_exn l1 l2 =
try[@exclave_if_stack a]
(map2_ok [@mode l] [@alloc a]) ~f:(fun a b -> (a, b) [@exclave_if_stack a]) l1 l2
with
| _ -> invalid_argf "length mismatch in zip_exn: %d <> %d" (length l1) (length l2) ()
;;
let zip l1 l2 =
(map2 [@mode l] [@alloc a])
~f:(fun a b -> (a, b) [@exclave_if_stack a])
l1
l2 [@exclave_if_stack a]
;;]
let zip3_exn l1 l2 l3 =
check_length3_exn "zip3_exn" l1 l2 l3;
map3_ok l1 l2 l3 ~f:(fun a b c -> a, b, c)
;;
let zip3 l1 l2 l3 = map3 l1 l2 l3 ~f:(fun a b c -> a, b, c)
(** Additional list operations *)
let folding_mapi t ~init ~f =
let acc = ref init in
mapi t ~f:(fun i x ->
let new_acc, y = f i !acc x in
acc := new_acc;
y)
[@nontail]
;;
let%template fold_mapi t ~init ~f =
let acc = ref init in
let result =
(mapi [@mode li] [@alloc heap]) t ~f:(fun i x ->
let new_acc, y = f i !acc x in
acc := new_acc;
y)
in
!acc, result
[@@mode li = (global, local)] [@@alloc a = heap]
;;
let%template fold_mapi t ~init ~f = exclave_
let accum, rev_list_accum =
(foldi [@mode li local])
t
~init:(init, [])
~f:(fun i (accum, rev_list_accum) x -> exclave_
let accum, x = f i accum x in
accum, x :: rev_list_accum)
in
accum, (rev [@alloc stack]) rev_list_accum
[@@mode li = (global, local)] [@@alloc a = stack]
;;
[%%template
[@@@mode.default l = (global, local)]
let reduce l ~f =
match[@exclave_if_local l ~reasons:[ May_return_local ]] l with
| [] -> None
| hd :: tl -> Some ((fold [@mode l l]) ~init:hd ~f tl)
;;
let reduce_exn l ~f =
match[@exclave_if_local l ~reasons:[ May_return_local ]] l with
| [] -> invalid_arg "List.reduce_exn"
| hd :: tl -> (fold [@mode l l]) ~init:hd ~f tl
;;]
let reduce_balanced l ~f =
let rec step_accum num acc x =
if num land 1 = 0
then x :: acc
else (
match acc with
| [] -> assert false
| y :: ys -> step_accum (num asr 1) ys (f y x))
in
match foldi l ~init:[] ~f:step_accum with
| [] -> None
| x :: xs -> Some (fold xs ~init:x ~f:(fun x y -> f y x))
;;
let reduce_balanced_exn l ~f =
match reduce_balanced l ~f with
| None -> invalid_arg "List.reduce_balanced_exn"
| Some v -> v
;;
let%template groupi l ~break =
let local_ pos = ref 0 in
let local_ l = ref l in
let[@tail_mod_cons] rec local_ take_group () =
match !l with
| ([] | [ _ ]) as group ->
l := [];
group
| x :: (y :: _ as tl) ->
pos := !pos + 1;
l := tl;
if break !pos x y then [ x ] else x :: take_group ()
in
let[@tail_mod_cons] rec local_ groups () =
if is_empty !l
then []
else (
let group = take_group () in
group :: groups ())
in
groups () [@nontail]
[@@alloc a = heap]
;;
let%template groupi l ~break = exclave_
let groups =
(foldi [@mode local local]) l ~init:[] ~f:(fun i acc x -> exclave_
match acc with
| [] -> [ [ x ] ]
| (current_hd :: current_tl) :: tl ->
if break i current_hd x
then [ x ] :: (current_hd :: current_tl) :: tl
else (x :: current_hd :: current_tl) :: tl
| [] :: _ -> assert false)
in
match groups with
| [] -> []
| l ->
(map [@mode local] [@alloc stack]) l ~f:(rev [@alloc stack]) |> (rev [@alloc stack])
[@@alloc a = stack]
;;
let%template group l ~break =
(groupi [@alloc a]) l ~break:(fun _ x y -> break x y) [@nontail] [@exclave_if_stack a]
[@@alloc a = (heap, stack)]
;;
let[@tail_mod_cons] rec merge l1 l2 ~compare =
match l1, l2 with
| [], l2 -> l2
| l1, [] -> l1
| h1 :: t1, h2 :: t2 ->
if compare h1 h2 <= 0 then h1 :: merge t1 l2 ~compare else h2 :: merge l1 t2 ~compare
;;
let merge_all lists ~compare =
let[@tail_mod_cons] rec merge_pairs ~compare = function
| [] -> []
| [ l ] -> [ l ]
| l1 :: l2 :: rest ->
(merge [@tailcall false]) l1 l2 ~compare :: (merge_pairs [@tailcall]) ~compare rest
in
let rec loop ~compare = function
| [] -> []
| [ l ] -> l
| lists -> loop ~compare (merge_pairs ~compare lists)
in
loop ~compare lists
;;
let%template stable_sort l ~compare:cmp =
let rec rev_merge cmp (l1 @ l) (l2 @ l) accu =
match[@exclave_if_stack a] l1, l2 with
| [], l2 -> (rev_append [@alloc a]) l2 accu
| l1, [] -> (rev_append [@alloc a]) l1 accu
| h1 :: t1, h2 :: t2 ->
if (cmp [@zero_alloc assume]) h1 h2 <= 0
then rev_merge cmp t1 l2 (h1 :: accu)
else rev_merge cmp l1 t2 (h2 :: accu)
in
let rec rev_merge_rev cmp (l1 @ l) (l2 @ l) accu =
match[@exclave_if_stack a] l1, l2 with
| [], l2 -> (rev_append [@alloc a]) l2 accu
| l1, [] -> (rev_append [@alloc a]) l1 accu
| h1 :: t1, h2 :: t2 ->
if (cmp [@zero_alloc assume]) h1 h2 > 0
then rev_merge_rev cmp t1 l2 (h1 :: accu)
else rev_merge_rev cmp l1 t2 (h2 :: accu)
in
let rec sort cmp n (l @ l) =
match[@exclave_if_stack a] n, l with
| 2, x1 :: x2 :: tl ->
let s = if (cmp [@zero_alloc assume]) x1 x2 <= 0 then [ x1; x2 ] else [ x2; x1 ] in
s, tl
| 3, x1 :: x2 :: x3 :: tl ->
let s =
if (cmp [@zero_alloc assume]) x1 x2 <= 0
then
if (cmp [@zero_alloc assume]) x2 x3 <= 0
then [ x1; x2; x3 ]
else if (cmp [@zero_alloc assume]) x1 x3 <= 0
then [ x1; x3; x2 ]
else [ x3; x1; x2 ]
else if (cmp [@zero_alloc assume]) x1 x3 <= 0
then [ x2; x1; x3 ]
else if (cmp [@zero_alloc assume]) x2 x3 <= 0
then [ x2; x3; x1 ]
else [ x3; x2; x1 ]
in
s, tl
| n, l ->
let n1 = n asr 1 in
let n2 = n - n1 in
let s1, l2 = rev_sort cmp n1 l in
let s2, tl = rev_sort cmp n2 l2 in
rev_merge_rev cmp s1 s2 [], tl
and rev_sort cmp n (l @ l) =
match[@exclave_if_stack a] n, l with
| 2, x1 :: x2 :: tl ->
let s = if (cmp [@zero_alloc assume]) x1 x2 > 0 then [ x1; x2 ] else [ x2; x1 ] in
s, tl
| 3, x1 :: x2 :: x3 :: tl ->
let s =
if (cmp [@zero_alloc assume]) x1 x2 > 0
then
if (cmp [@zero_alloc assume]) x2 x3 > 0
then [ x1; x2; x3 ]
else if (cmp [@zero_alloc assume]) x1 x3 > 0
then [ x1; x3; x2 ]
else [ x3; x1; x2 ]
else if (cmp [@zero_alloc assume]) x1 x3 > 0
then [ x2; x1; x3 ]
else if (cmp [@zero_alloc assume]) x2 x3 > 0
then [ x2; x3; x1 ]
else [ x3; x2; x1 ]
in
s, tl
| n, l ->
let n1 = n asr 1 in
let n2 = n - n1 in
let s1, l2 = sort cmp n1 l in
let s2, tl = sort cmp n2 l2 in
rev_merge cmp s1 s2 [], tl
in
let len = length l in
if [@exclave_if_stack a] len < 2 then l else fst (sort cmp len l)
[@@zero_alloc_if_stack a] [@@alloc a @ l = (heap_global, stack_local)]
;;
let%template sort = (stable_sort [@alloc a]) [@@alloc a = (heap, stack)]
let sort_and_group l ~compare =
(l |> stable_sort ~compare |> group ~break:(fun x y -> compare x y <> 0)) [@nontail]
;;
let%template dedup_and_sort l ~compare:cmp =
let rec rev_merge cmp (l1 @ l) (l2 @ l) accu =
match[@exclave_if_stack a] l1, l2 with
| [], l2 -> (rev_append [@alloc a]) l2 accu
| l1, [] -> (rev_append [@alloc a]) l1 accu
| h1 :: t1, h2 :: t2 ->
(match (cmp [@zero_alloc assume]) h1 h2 with
| c when c < 0 -> rev_merge cmp t1 l2 (h1 :: accu)
| c when c > 0 -> rev_merge cmp l1 t2 (h2 :: accu)
| _ -> rev_merge cmp t1 l2 accu)
in
let rec rev_merge_rev cmp (l1 @ l) (l2 @ l) accu =
match[@exclave_if_stack a] l1, l2 with
| [], l2 -> (rev_append [@alloc a]) l2 accu
| l1, [] -> (rev_append [@alloc a]) l1 accu
| h1 :: t1, h2 :: t2 ->
(match (cmp [@zero_alloc assume]) h1 h2 with
| c when c > 0 -> rev_merge_rev cmp t1 l2 (h1 :: accu)
| c when c < 0 -> rev_merge_rev cmp l1 t2 (h2 :: accu)
| _ -> rev_merge_rev cmp t1 l2 accu)
in
let rec sort cmp n (l @ l) =
match[@exclave_if_stack a] n, l with
| 2, x1 :: x2 :: tl ->
let s =
match (cmp [@zero_alloc assume]) x1 x2 with
| c when c < 0 -> [ x1; x2 ]
| c when c > 0 -> [ x2; x1 ]
| _ -> [ x2 ]
in
s, tl
| 3, x1 :: x2 :: x3 :: tl ->
let s =
match (cmp [@zero_alloc assume]) x1 x2 with
| c when c < 0 ->
(match (cmp [@zero_alloc assume]) x2 x3 with
| c when c < 0 -> [ x1; x2; x3 ]
| c when c > 0 ->
(match (cmp [@zero_alloc assume]) x1 x3 with
| c when c < 0 -> [ x1; x3; x2 ]
| c when c > 0 -> [ x3; x1; x2 ]
| _ -> [ x3; x2 ])
| _ -> [ x1; x3 ])
| c when c > 0 ->
(match (cmp [@zero_alloc assume]) x1 x3 with
| c when c < 0 -> [ x2; x1; x3 ]
| c when c > 0 ->
(match (cmp [@zero_alloc assume]) x2 x3 with
| c when c < 0 -> [ x2; x3; x1 ]
| c when c > 0 -> [ x3; x2; x1 ]
| _ -> [ x3; x1 ])
| _ -> [ x2; x3 ])
| _ ->
(match (cmp [@zero_alloc assume]) x2 x3 with
| c when c < 0 -> [ x2; x3 ]
| c when c > 0 -> [ x3; x2 ]
| _ -> [ x3 ])
in
s, tl
| n, l ->
let n1 = n asr 1 in
let n2 = n - n1 in
let s1, l2 = rev_sort cmp n1 l in
let s2, tl = rev_sort cmp n2 l2 in
rev_merge_rev cmp s1 s2 [], tl
and rev_sort cmp n (l @ l) =
match[@exclave_if_stack a] n, l with
| 2, x1 :: x2 :: tl ->
let s =
match (cmp [@zero_alloc assume]) x1 x2 with
| c when c > 0 -> [ x1; x2 ]
| c when c < 0 -> [ x2; x1 ]
| _ -> [ x2 ]
in
s, tl
| 3, x1 :: x2 :: x3 :: tl ->
let s =
match (cmp [@zero_alloc assume]) x1 x2 with
| c when c > 0 ->
(match (cmp [@zero_alloc assume]) x2 x3 with
| c when c > 0 -> [ x1; x2; x3 ]
| c when c < 0 ->
(match (cmp [@zero_alloc assume]) x1 x3 with
| c when c > 0 -> [ x1; x3; x2 ]
| c when c < 0 -> [ x3; x1; x2 ]
| _ -> [ x3; x2 ])
| _ -> [ x1; x3 ])
| c when c < 0 ->
(match (cmp [@zero_alloc assume]) x1 x3 with
| c when c > 0 -> [ x2; x1; x3 ]
| c when c < 0 ->
(match (cmp [@zero_alloc assume]) x2 x3 with
| c when c > 0 -> [ x2; x3; x1 ]
| c when c < 0 -> [ x3; x2; x1 ]
| _ -> [ x3; x1 ])
| _ -> [ x2; x3 ])
| _ ->
(match (cmp [@zero_alloc assume]) x2 x3 with
| c when c > 0 -> [ x2; x3 ]
| c when c < 0 -> [ x3; x2 ]
| _ -> [ x3 ])
in
s, tl
| n, l ->
let n1 = n asr 1 in
let n2 = n - n1 in
let s1, l2 = sort cmp n1 l in
let s2, tl = sort cmp n2 l2 in
rev_merge cmp s1 s2 [], tl
in
let len = length l in
if len < 2 then l else fst (sort cmp len l) [@exclave_if_stack a]
[@@zero_alloc_if_stack a] [@@alloc a @ l = (heap_global, stack_local)]
;;
let stable_dedup list ~compare =
match list with
| [] | [ _ ] -> list
| _ :: _ :: _ ->
let open struct
type ('a : value_or_null) dedup =
{ elt : 'a
; mutable dup : bool
}
end in
let dedups = rev_map list ~f:(fun elt -> { elt; dup = true }) in
let unique = dedup_and_sort dedups ~compare:(fun x y -> compare x.elt y.elt) in
iter unique ~f:(fun dedup -> dedup.dup <- false);
fold dedups ~init:[] ~f:(fun acc dedup -> if dedup.dup then acc else dedup.elt :: acc)
;;
module Cartesian_product = struct
let bind = concat_map
let map = map
let map2 a b ~f =
concat_map a ~f:(fun x -> map b ~f:(fun y -> f x y) [@nontail]) [@nontail]
;;
let return = singleton
let ( >>| ) = ( >>| )
let ( >>= ) t (local_ f) = bind t ~f
open struct
module%template Applicative =
Applicative.Make_using_map2
[@kind value_or_null mod maybe_null]
[@mode local]
[@modality portable]
(struct
type ('a : value_or_null) t = 'a list
let return = return
let map = `Custom map
let map2 = map2
end)
module%template Monad =
Monad.Make [@kind value_or_null mod maybe_null] [@mode local] [@modality portable] (struct
type ('a : value_or_null) t = 'a list
let return = return
let map = `Custom map
let bind = bind
end)
end
let all = Monad.all
let all_unit = Monad.all_unit
let ignore_m = Monad.ignore_m
let join = Monad.join
module Monad_infix = struct
let ( >>| ) = ( >>| )
let ( >>= ) = ( >>= )
end
let apply = Applicative.apply
let both = Applicative.both
let map3 = Applicative.map3
let ( <*> ) = Applicative.( <*> )
let ( *> ) = Applicative.( *> )
let ( <* ) = Applicative.( <* )
module Applicative_infix = struct
let ( >>| ) = ( >>| )
let ( <*> ) = Applicative.( <*> )
let ( *> ) = Applicative.( *> )
let ( <* ) = Applicative.( <* )
end
module Let_syntax = struct
let return = return
let ( >>| ) = ( >>| )
let ( >>= ) = ( >>= )
module Let_syntax = struct
let return = return
let bind = bind
let map = map
let both = both
module Open_on_rhs = struct end
end
end
end
include (
Cartesian_product :
sig
@@ portable
include%template
Monad.S
[@kind value_or_null mod maybe_null] [@mode local]
with type ('a : value_or_null) t := 'a t
end)
(** returns final element of list *)
let rec last_exn list =
match list with
| [ x ] -> x
| _ :: tl -> last_exn tl
| [] -> invalid_arg "List.last"
;;
(** optionally returns final element of list *)
let rec last list =
match list with
| [ x ] -> Some x
| _ :: tl -> last tl
| [] -> None
;;
let rec is_prefix list ~prefix ~equal =
match prefix with
| [] -> true
| hd :: tl ->
(match list with
| [] -> false
| hd' :: tl' -> equal hd hd' && is_prefix tl' ~prefix:tl ~equal)
;;
let find_consecutive_duplicate t ~equal =
match t with
| [] -> None
| a1 :: t ->
let rec find_consecutive_duplicate_loop a1 t =
match t with
| [] -> None
| a2 :: t ->
if equal a1 a2 then Some (a1, a2) else find_consecutive_duplicate_loop a2 t
in
find_consecutive_duplicate_loop a1 t [@nontail]
;;
let remove_consecutive_duplicates ?(which_to_keep = `Last) list ~equal =
let rec remove_consecutive_duplicates_loop to_keep accum = function
| [] -> to_keep :: accum
| hd :: tl ->
if equal hd to_keep
then (
let to_keep =
match which_to_keep with
| `First -> to_keep
| `Last -> hd
in
remove_consecutive_duplicates_loop to_keep accum tl)
else remove_consecutive_duplicates_loop hd (to_keep :: accum) tl
in
match list with
| [] -> []
| hd :: tl -> rev (remove_consecutive_duplicates_loop hd [] tl)
;;
let find_a_dup l ~compare =
let sorted = sort l ~compare in
let rec find_a_dup_loop l =
match l with
| [] | [ _ ] -> None
| hd1 :: (hd2 :: _ as tl) ->
if compare hd1 hd2 = 0 then Some hd1 else find_a_dup_loop tl
in
find_a_dup_loop sorted [@nontail]
;;
let contains_dup lst ~compare =
match find_a_dup lst ~compare with
| Some _ -> true
| None -> false
;;
let find_all_dups l ~compare =
let sorted = sort ~compare l in
let[@tail_mod_cons] rec local_ find_all_dups_loop sorted prev ~already_recorded =
match sorted with
| [] -> []
| hd :: tl ->
if compare prev hd <> 0
then find_all_dups_loop tl hd ~already_recorded:false
else if already_recorded
then find_all_dups_loop tl hd ~already_recorded:true
else hd :: find_all_dups_loop tl hd ~already_recorded:true
in
match sorted with
| [] -> []
| hd :: tl -> find_all_dups_loop tl hd ~already_recorded:false [@nontail]
;;
[%%template
[@@@mode.default l = (global, local)]
let rec all_equal_to t v ~equal =
match t with
| [] -> true
| x :: xs -> equal x v && (all_equal_to [@mode l]) xs v ~equal
;;
let all_equal t ~equal =
match t with
| [] -> None
| x :: xs ->
if (all_equal_to [@mode l]) xs x ~equal
then
Some x
[@exclave_if_local l ~reasons:[ May_return_regional; Will_return_unboxed ]]
else None
;;]
let%template sum m t ~f =
(Derived.sum [@mode li lo]) ~fold:(fold [@mode li lo]) m t ~f [@exclave_if_local lo]
[@@mode li = (global, local), lo = (global, local)]
;;
[%%template
[@@@mode.default l = (global, local)]
let min_elt t ~compare =
match t with
| [] -> None
| elt :: t ->
Some
((fold [@mode l l]) t ~init:elt ~f:(fun acc elt ->
if compare acc elt > 0 then elt else acc))
[@exclave_if_local l ~reasons:[ May_return_regional; Will_return_unboxed ]]
;;
let max_elt t ~compare =
match t with
| [] -> None
| elt :: t ->
Some
((fold [@mode l l]) t ~init:elt ~f:(fun acc elt ->
if compare acc elt < 0 then elt else acc))
[@exclave_if_local l ~reasons:[ May_return_regional; Will_return_unboxed ]]
;;
let count t ~f = (Derived.count [@mode l]) ~fold:(fold [@mode l global]) t ~f
let counti t ~f =
(foldi [@mode l global]) t ~init:0 ~f:(fun idx count a ->
if f idx a then count + 1 else count)
[@nontail]
;;]
let create ~len x =
init_internal len ~f:(local_ fun _ -> x) ~name:"List.create" [@nontail]
;;
[%%template
[@@@alloc.default a @ lo = (heap_global, stack_local)]
let filter_opt l = (filter_map [@mode lo] [@alloc a]) l ~f:Fn.id [@exclave_if_stack a]
let filter_or_null l =
(filter_map_or_null [@mode lo] [@alloc a]) l ~f:Fn.id [@exclave_if_stack a]
;;
[@@@mode.default li = (global, local)]
let rev_filter_map l ~f =
(let rec rev_filter_map_loop l accum =
match[@exclave_if_stack a] l with
| [] -> accum
| hd :: tl ->
(match f hd with
| Some x -> rev_filter_map_loop tl (x :: accum)
| None -> rev_filter_map_loop tl accum)
in
rev_filter_map_loop l [] [@nontail])
[@exclave_if_stack a]
;;
let rev_filter_mapi l ~f =
(let rec rev_filter_mapi_loop i l accum =
match[@exclave_if_stack a] l with
| [] -> accum
| hd :: tl ->
(match f i hd with
| Some x -> rev_filter_mapi_loop (i + 1) tl (x :: accum)
| None -> rev_filter_mapi_loop (i + 1) tl accum)
in
rev_filter_mapi_loop 0 l [] [@nontail])
[@exclave_if_stack a]
;;
let rev_filter_map_or_null l ~f =
(let rec rev_filter_map_or_null_loop l accum =
match[@exclave_if_stack a] l with
| [] -> accum
| hd :: tl ->
(match f hd with
| This x -> rev_filter_map_or_null_loop tl (x :: accum)
| Null -> rev_filter_map_or_null_loop tl accum)
in
rev_filter_map_or_null_loop l [] [@nontail])
[@exclave_if_stack a]
;;
let rev_filter_mapi_or_null l ~f =
(let rec rev_filter_mapi_or_null_loop i l accum =
match[@exclave_if_stack a] l with
| [] -> accum
| hd :: tl ->
(match f i hd with
| This x -> rev_filter_mapi_or_null_loop (i + 1) tl (x :: accum)
| Null -> rev_filter_mapi_or_null_loop (i + 1) tl accum)
in
rev_filter_mapi_or_null_loop 0 l [] [@nontail])
[@exclave_if_stack a]
;;]
let partition3_map t ~f =
let rec partition3_map_loop t fst snd trd =
match t with
| [] -> rev fst, rev snd, rev trd
| x :: t ->
(match f x with
| `Fst y -> partition3_map_loop t (y :: fst) snd trd
| `Snd y -> partition3_map_loop t fst (y :: snd) trd
| `Trd y -> partition3_map_loop t fst snd (y :: trd))
in
partition3_map_loop t [] [] [] [@nontail]
;;
let%template partition_tf t ~f =
(let f x : _ Either.t = (if f x then First x else Second x) [@exclave_if_stack a] in
(partition_map [@mode l] [@alloc a]) t ~f [@nontail])
[@exclave_if_stack a]
[@@alloc a @ l = (heap_global, stack_local)]
;;
let%template partition_result (t @ l) =
(partition_map [@mode l] [@alloc a])
t
~f:(Result.to_either [@mode l]) [@exclave_if_stack a]
[@@alloc a @ l = (heap_global, stack_local)]
;;
let%template partition_mapi t ~f =
(let rec partition_mapi_loop i t fst snd =
match[@exclave_if_stack a] t with
| [] -> (rev [@alloc a]) fst, (rev [@alloc a]) snd
| x :: t ->
(match (f i x : _ Either0.t) with
| First y -> partition_mapi_loop (i + 1) t (y :: fst) snd
| Second y -> partition_mapi_loop (i + 1) t fst (y :: snd))
in
partition_mapi_loop 0 t [] [] [@nontail])
[@exclave_if_stack a]
[@@mode l = (global, local)] [@@alloc a = (heap, stack)]
;;
let%template partitioni_tf t ~f =
(let f i x : _ Either.t = (if f i x then First x else Second x) [@exclave_if_stack a] in
(partition_mapi [@mode l] [@alloc a]) t ~f [@nontail])
[@exclave_if_stack a]
[@@alloc a @ l = (heap_global, stack_local)]
;;
let of_iter ~iter =
let local_ acc = ref [] in
iter ~f:(fun x -> acc := x :: !acc);
rev !acc
;;
module Assoc = struct
type ('a : value_or_null) key = ('a[@tag Sexplib0.Sexp_grammar.assoc_key_tag = List []])
[@@deriving sexp ~stackify, sexp_grammar]
type ('a : value_or_null) value =
('a[@tag Sexplib0.Sexp_grammar.assoc_value_tag = List []])
[@@deriving sexp ~stackify, sexp_grammar]
type ('a : value_or_null, 'b : value_or_null) t =
(('a key * 'b value) list[@tag Sexplib0.Sexp_grammar.assoc_tag = List []])
[@@deriving sexp ~stackify, sexp_grammar]
let pair_of_group = function
| [] -> assert false
| (k, _) :: _ as list -> k, map list ~f:snd
;;
let group alist ~equal =
group alist ~break:(fun (x, _) (y, _) -> not (equal x y)) |> map ~f:pair_of_group
;;
let sort_and_group alist ~compare =
sort_and_group alist ~compare:(fun (x, _) (y, _) -> compare x y)
|> map ~f:pair_of_group
;;
[%%template
let find (t @ l) ~equal key =
match[@exclave_if_local l ~reasons:[ May_return_regional; Will_return_unboxed ]]
(find [@mode l]) t ~f:(fun (key', _) -> equal key key')
with
| None -> None
| Some x -> Some (snd x)
[@@mode l = (local, global)]
;;
let find_or_null t ~equal key =
match[@exclave_if_local l ~reasons:[ May_return_regional ]]
(find_or_null [@mode l]) t ~f:(stack_ fun (key', _) -> equal key key')
with
| Null -> Null
| This x -> This (snd x)
[@@mode l = (local, global)]
;;]
let%template find_exn =
let not_found = Not_found_s (Atom "List.Assoc.find_exn: not found") in
let rec find_exn t ~equal key =
match t with
| [] ->
raise (Portability_hacks.magic_uncontended__promise_deeply_immutable not_found)
| (key', value) :: t ->
if equal key key' then value else find_exn t ~equal key [@exclave_if_local l]
in
find_exn
[@@mode l = (local, global)]
;;
let mem t ~equal key =
match find t ~equal key with
| None -> false
| Some _ -> true
;;
let remove t ~equal key = filter t ~f:(fun (key', _) -> not (equal key key')) [@nontail]
let add t ~equal key value =
(key, value) :: remove t ~equal key
;;
let inverse t = map t ~f:(fun (x, y) -> y, x)
let map t ~f = map t ~f:(fun (key, value) -> key, f value) [@nontail]
end
let sub l ~pos ~len =
if pos < 0 || len < 0 || pos > length l - len then invalid_arg "List.sub";
let stop = pos + len in
let[@tail_mod_cons] rec local_ sub_loop i l =
match l with
| [] -> []
| hd :: tl ->
if i < pos
then sub_loop (i + 1) tl
else if i < stop
then hd :: sub_loop (i + 1) tl
else []
in
sub_loop 0 l [@nontail]
;;
let%template split_n t_orig n =
if n <= 0
then #([], t_orig)
else (
let rec split_n_loop n t accum =
match t with
| [] -> #(t_orig, [])
| hd :: tl ->
if n = 0 then #(rev accum, t) else split_n_loop (n - 1) tl (hd :: accum)
in
split_n_loop n t_orig [])
[@@alloc a = heap]
;;
let%template[@zero_alloc] [@inline] split_n xs n = exclave_
let rec split_n_stack_tail ~orig ~n ~acc = function
| [] -> #(orig, [])
| hd :: tl when n > 0 ->
exclave_ (split_n_stack_tail [@tailcall]) ~orig ~n:(n - 1) ~acc:(hd :: acc) tl
| t -> exclave_ #((rev [@alloc stack]) acc, t)
[@@zero_alloc]
in
let rec split_n_stack_loop ~depth ~n = function
| hd :: tl when depth < n ->
exclave_
let depth = depth + 1 in
let #(fst, snd) =
if depth <= max_non_tailcall
then split_n_stack_loop ~depth ~n tl
else (split_n_stack_tail [@inlined never]) ~orig:tl ~n:(n - depth) ~acc:[] tl
in
#(hd :: fst, snd)
| t -> #([], t)
[@@zero_alloc]
in
split_n_stack_loop ~depth:0 ~n xs
[@@alloc a = stack]
;;
let%template take t_orig n =
if n <= 0
then []
else (
let rec take_loop n t accum =
match t with
| [] -> t_orig
| hd :: tl -> if n = 0 then rev accum else take_loop (n - 1) tl (hd :: accum)
in
take_loop n t_orig [])
[@@alloc a = heap]
;;
let%template take =
let rec take_stack_tail ~orig ~n ~acc = function
| [] -> orig
| hd :: tl when n > 0 ->
exclave_ (take_stack_tail [@tailcall]) ~orig ~n:(n - 1) ~acc:(hd :: acc) tl
| _ -> exclave_ (rev [@alloc stack]) acc
in
let rec take_stack_loop ~depth ~n = function
| hd :: tl when depth < n ->
exclave_
let depth = depth + 1 in
let tl =
if depth <= max_non_tailcall
then take_stack_loop ~depth ~n tl
else (take_stack_tail [@inlined never]) ~orig:tl ~n:(n - depth) ~acc:[] tl
in
hd :: tl
| _ -> []
in
fun [@zero_alloc] t n -> exclave_ take_stack_loop t ~depth:0 ~n
[@@alloc a = stack]
;;
let rec drop t n =
match t with
| _ :: tl when n > 0 -> drop tl (n - 1)
| t -> t
;;
let[@cold] chunks_of_invalid_length ~length =
invalid_argf "List.chunks_of: Expected length > 0, got %d" length ()
;;
let%template chunks_of l ~length =
if length <= 0 then chunks_of_invalid_length ~length;
let rec aux length acc l =
match l with
| [] -> rev acc
| _ :: _ ->
let #(sublist, l) = split_n l length in
aux length (sublist :: acc) l
in
aux length [] l
[@@alloc a = heap]
;;
let%template[@inline] [@zero_alloc] chunks_of t ~length = exclave_
let[@cold] rec chunks_of_tail t ~length acc = exclave_
if is_empty t
then (rev [@alloc stack]) acc
else (
let #(sublist, t) = (split_n [@alloc stack]) t length in
chunks_of_tail t ~length (sublist :: acc))
in
let rec chunks_of_loop t ~length ~depth = exclave_
if is_empty t
then []
else if depth > max_non_tailcall
then chunks_of_tail t ~length []
else (
let depth = depth + 1 in
let #(x, xs) = (split_n [@alloc stack]) t length in
x :: chunks_of_loop xs ~length ~depth)
in
if length <= 0
then chunks_of_invalid_length ~length
else chunks_of_loop t ~length ~depth:0
[@@alloc a = stack]
;;
let chunk_evenly t ~into =
if into <= 0 then invalid_argf "List.chunk_evenly: Expected [into] > 0, got %d" into ();
let num_items = length t in
let smaller_bucket_size = num_items / into in
let num_smaller_into = into - (num_items mod into) in
let[@tail_mod_cons] rec (chunk_evenly_loop @ local) t ~into =
match into with
| 0 -> []
| _ ->
let bucket_size =
if into <= num_smaller_into then smaller_bucket_size else smaller_bucket_size + 1
in
let #(this_bucket, remaining) = split_n t bucket_size in
this_bucket :: chunk_evenly_loop remaining ~into:(into - 1)
in
chunk_evenly_loop t ~into [@nontail]
;;
let split_while =
let[@tail_mod_cons] rec split_while_loop ~f ~rest t =
match t with
| hd :: tl when f hd -> hd :: (split_while_loop [@tailcall]) ~f ~rest tl
| _ ->
rest := t;
[]
in
fun xs ~f ->
let rest = stack_ (ref []) in
let first = split_while_loop ~f ~rest xs in
first, !rest
;;
let[@tail_mod_cons] rec take_while xs ~f =
match xs with
| hd :: tl when f hd -> hd :: (take_while [@tailcall]) tl ~f
| _ -> []
;;
let rec drop_while t ~f =
match t with
| hd :: tl when f hd -> drop_while tl ~f
| t -> t
;;
let drop_last =
let[@tail_mod_cons] rec loop ~hd:hd1 ~tl =
match tl with
| [] -> []
| hd2 :: tl -> hd1 :: (loop [@tailcall]) ~hd:hd2 ~tl
in
function
| [] -> None
| hd :: tl -> Some (loop ~hd ~tl)
;;
let drop_last_exn t =
match drop_last t with
| None -> failwith "List.drop_last_exn: empty list"
| Some lst -> lst
;;
let%template cartesian_product list1 list2 =
if is_empty list2
then []
else (
let[@tail_mod_cons] rec local_ cartesian_product_outer_loop l1 =
match l1 with
| [] -> []
| x1 :: l1 -> cartesian_product_inner_loop x1 l1 list2
and[@tail_mod_cons] local_ cartesian_product_inner_loop x1 l1 l2 =
match l2 with
| [] -> cartesian_product_outer_loop l1
| x2 :: l2 -> (x1, x2) :: cartesian_product_inner_loop x1 l1 l2
in
cartesian_product_outer_loop list1 [@nontail])
[@@alloc a = heap]
;;
let%template cartesian_product =
let rec cartesian_product_stack_tail ~acc l2 = function
| [] -> exclave_ (rev [@alloc stack]) acc
| x :: xs -> exclave_ (cartesian_product_stack_inner_tail [@tailcall]) ~acc l2 x xs l2
and cartesian_product_stack_inner_tail ~acc l2 x xs = function
| [] -> exclave_ (cartesian_product_stack_tail [@tailcall]) ~acc l2 xs
| y :: ys ->
exclave_
(cartesian_product_stack_inner_tail [@tailcall]) ~acc:((x, y) :: acc) l2 x xs ys
in
let rec cartesian_product_stack_loop ~depth l2 = function
| [] -> exclave_ []
| x :: xs ->
exclave_ (cartesian_product_stack_loop_inner [@tailcall]) ~depth l2 x xs l2
and cartesian_product_stack_loop_inner ~depth l2 x xs = function
| [] -> exclave_ (cartesian_product_stack_loop [@tailcall]) ~depth l2 xs
| y :: ys ->
exclave_
let rest =
if depth <= max_non_tailcall
then cartesian_product_stack_loop_inner ~depth:(depth + 1) l2 x xs ys
else (cartesian_product_stack_inner_tail [@inlined never]) ~acc:[] l2 x xs ys
in
(x, y) :: rest
in
fun [@zero_alloc] l1 l2 ->
if is_empty l2 then [] else exclave_ cartesian_product_stack_loop ~depth:0 l2 l1
[@@alloc a = stack]
;;
let%template concat l =
(fold_right [@mode l l]) l ~init:[] ~f:(append [@alloc a]) [@exclave_if_stack a]
[@@alloc a @ l = (heap_global, stack_local)]
;;
let concat_no_order l = fold l ~init:[] ~f:(fun acc l -> rev_append l acc)
let cons x l = x :: l
let is_sorted l ~compare =
let rec is_sorted_loop l =
match l with
| [] | [ _ ] -> true
| x1 :: (x2 :: _ as rest) -> compare x1 x2 <= 0 && is_sorted_loop rest
in
is_sorted_loop l [@nontail]
;;
let is_sorted_strictly l ~compare =
let rec is_sorted_strictly_loop l =
match l with
| [] | [ _ ] -> true
| x1 :: (x2 :: _ as rest) -> compare x1 x2 < 0 && is_sorted_strictly_loop rest
in
is_sorted_strictly_loop l [@nontail]
;;
module Infix = struct
let ( @ ) = append
end
let permute ?(random_state = Random.State.get_default ()) list =
match list with
| [] | [ _ ] -> list
| [ x; y ] -> if Random.State.bool random_state then [ y; x ] else list
| _ ->
let arr = Array.of_list list in
Array_permute.permute arr ~random_state;
Array.to_list arr
;;
let random_element_exn ?(random_state = Random.State.get_default ()) list =
if is_empty list
then failwith "List.random_element_exn: empty list"
else nth_exn list (Random.State.int random_state (length list))
;;
let random_element ?random_state list =
try Some (random_element_exn ?random_state list) with
| _ -> None
;;
let rec compare cmp a b =
match a, b with
| [], [] -> 0
| [], _ -> -1
| _, [] -> 1
| x :: xs, y :: ys ->
let n = cmp x y in
if n = 0 then compare cmp xs ys else n
;;
let%template[@mode local] rec compare cmp a b =
match a, b with
| [], [] -> 0
| [], _ -> -1
| _, [] -> 1
| x :: xs, y :: ys ->
let n = cmp x y in
if n = 0 then (compare [@mode local]) cmp xs ys else n
;;
let hash_fold_t = hash_fold_list
let equal_with_local_closure (local_ (equal : _ -> _ -> _)) t1 t2 =
let rec equal_with_local_closure_loop t1 t2 =
match t1, t2 with
| [], [] -> true
| x1 :: t1, x2 :: t2 -> equal x1 x2 && equal_with_local_closure_loop t1 t2
| _ -> false
in
equal_with_local_closure_loop t1 t2 [@nontail]
;;
let equal : ('a : value_or_null). ('a -> 'a -> bool) -> 'a t -> 'a t -> bool =
fun f x y -> equal_with_local_closure f x y
;;
let%template[@mode local] equal equal_a t1 t2 =
let rec equal_local_loop t1 t2 =
match t1, t2 with
| [], [] -> true
| x1 :: t1, x2 :: t2 -> equal_a x1 x2 && equal_local_loop t1 t2
| _ -> false
in
equal_local_loop t1 t2 [@nontail]
;;
let%template transpose =
let rec split_off_first_column t column_acc trimmed found_empty =
match[@exclave_if_stack a] t with
| [] -> column_acc, trimmed, found_empty
| [] :: tl -> split_off_first_column tl column_acc trimmed true
| (x :: xs) :: tl ->
split_off_first_column tl (x :: column_acc) (xs :: trimmed) found_empty
in
let split_off_first_column rows =
split_off_first_column rows [] [] false [@exclave_if_stack a]
in
let rec transpose_loop rows columns do_rev =
match[@exclave_if_stack a] split_off_first_column rows with
| [], [], _ -> Some ((rev [@alloc a]) columns)
| column, trimmed_rows, found_empty ->
if found_empty
then None
else (
let column = if do_rev then (rev [@alloc a]) column else column in
transpose_loop trimmed_rows (column :: columns) (not do_rev))
in
fun [@zero_alloc_if_stack a] t -> transpose_loop t [] true [@exclave_if_stack a]
[@@alloc a = (heap, stack)]
;;
exception Transpose_got_lists_of_different_lengths of int list [@@deriving sexp]
let%template transpose_exn l =
match[@exclave_if_stack a] (transpose [@alloc a]) l with
| Some l -> l
| None ->
raise
(Transpose_got_lists_of_different_lengths
((map [@mode l] [@alloc heap]) l ~f:length))
[@@alloc a @ l = (heap_global, stack_local)]
;;
let intersperse t ~sep =
match t with
| [] -> []
| x :: xs -> x :: fold_right xs ~init:[] ~f:(fun y acc -> sep :: y :: acc)
;;
let%template fold_until t ~init ~f ~finish =
(let rec fold_until_loop t ~init =
match[@exclave_if_local lo ~reasons:[ May_return_local ]] t with
| [] -> finish init
| a :: xs ->
(match f init a with
| Container.Continue_or_stop.Continue x ->
(fold_until_loop [@tailcall]) xs ~init:x
| Stop x -> x)
in
fold_until_loop t ~init [@nontail])
[@exclave_if_local lo ~reasons:[ May_return_local ]]
[@@mode li = (global, local), lo = (global, local)]
;;
let%template fold_result t ~init ~f =
(Derived.fold_result [@mode li lo])
~fold_until:(fold_until [@mode li lo])
~init
~f
t [@exclave_if_local lo]
[@@mode li = (global, local), lo = (global, local)]
;;
let%template foldi_until t ~init ~f ~finish =
(Derived.foldi_until [@mode li lo])
~fold_until:(fold_until [@mode li lo])
~init
~f
t
~finish [@exclave_if_local lo]
[@@mode li = (global, local), lo = (global, local)]
;;
let%template iter_until t ~f ~finish =
(Derived.iter_until [@mode li lo])
~fold_until:(fold_until [@mode li lo])
t
~f
~finish [@exclave_if_local lo]
[@@mode li = (global, local), lo = (global, local)]
;;
let%template iteri_until t ~f ~finish =
(Derived.iteri_until [@mode li lo])
~foldi_until:(foldi_until [@mode li lo])
t
~f
~finish [@exclave_if_local lo]
[@@mode li = (global, local), lo = (global, local)]
;;
let is_suffix list ~suffix ~equal:(local_ (equal_elt : _ -> _ -> _)) =
let list_len = length list in
let suffix_len = length suffix in
list_len >= suffix_len
&& equal_with_local_closure equal_elt (drop list (list_len - suffix_len)) suffix
;;
module%template Local = struct
module Let_syntax = struct
let return = (singleton [@alloc stack])
let ( >>| ) x f = exclave_ (map [@mode local] [@alloc stack]) x ~f
let ( >>= ) x f = exclave_ (concat_map [@mode local] [@alloc stack]) x ~f
module Let_syntax = struct
let return = (singleton [@alloc stack])
let map = (map [@mode local] [@alloc stack])
let bind = (concat_map [@mode local] [@alloc stack])
let both = (cartesian_product [@alloc stack])
module Open_on_rhs = struct end
end
end
end
module Private = struct
let max_non_tailcall = max_non_tailcall
end]