package base

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file map_intf.ml

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
open! Import
open! T

module Or_duplicate = struct
  type 'a t =
    [ `Ok of 'a
    | `Duplicate
    ]
  [@@deriving_inline compare, equal, sexp_of]

  let compare : 'a. ('a -> 'a -> int) -> 'a t -> 'a t -> int =
    fun _cmp__a a__001_ b__002_ ->
    if Stdlib.( == ) a__001_ b__002_
    then 0
    else (
      match a__001_, b__002_ with
      | `Ok _left__003_, `Ok _right__004_ -> _cmp__a _left__003_ _right__004_
      | `Duplicate, `Duplicate -> 0
      | x, y -> Stdlib.compare x y)
  ;;

  let equal : 'a. ('a -> 'a -> bool) -> 'a t -> 'a t -> bool =
    fun _cmp__a a__005_ b__006_ ->
      if Stdlib.( == ) a__005_ b__006_
      then true
      else (
        match a__005_, b__006_ with
        | `Ok _left__007_, `Ok _right__008_ -> _cmp__a _left__007_ _right__008_
        | `Duplicate, `Duplicate -> true
        | x, y -> Stdlib.( = ) x y)
  ;;

  let sexp_of_t : 'a. ('a -> Sexplib0.Sexp.t) -> 'a t -> Sexplib0.Sexp.t =
    fun _of_a__009_ -> function
      | `Ok v__010_ -> Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "Ok"; _of_a__009_ v__010_ ]
      | `Duplicate -> Sexplib0.Sexp.Atom "Duplicate"
  ;;

  [@@@end]
end

module Without_comparator = struct
  type ('key, 'cmp, 'z) t = 'z
end

module With_comparator = struct
  type ('key, 'cmp, 'z) t = comparator:('key, 'cmp) Comparator.t -> 'z
end

module With_first_class_module = struct
  type ('key, 'cmp, 'z) t = ('key, 'cmp) Comparator.Module.t -> 'z
end

module Symmetric_diff_element = struct
  type ('k, 'v) t = 'k * [ `Left of 'v | `Right of 'v | `Unequal of 'v * 'v ]
  [@@deriving_inline compare, equal, sexp, sexp_grammar]

  let compare :
    'k 'v. ('k -> 'k -> int) -> ('v -> 'v -> int) -> ('k, 'v) t -> ('k, 'v) t -> int
    =
    fun _cmp__k _cmp__v a__011_ b__012_ ->
    let t__013_, t__014_ = a__011_ in
    let t__015_, t__016_ = b__012_ in
    match _cmp__k t__013_ t__015_ with
    | 0 ->
      if Stdlib.( == ) t__014_ t__016_
      then 0
      else (
        match t__014_, t__016_ with
        | `Left _left__017_, `Left _right__018_ -> _cmp__v _left__017_ _right__018_
        | `Right _left__019_, `Right _right__020_ -> _cmp__v _left__019_ _right__020_
        | `Unequal _left__021_, `Unequal _right__022_ ->
          let t__023_, t__024_ = _left__021_ in
          let t__025_, t__026_ = _right__022_ in
          (match _cmp__v t__023_ t__025_ with
           | 0 -> _cmp__v t__024_ t__026_
           | n -> n)
        | x, y -> Stdlib.compare x y)
    | n -> n
  ;;

  let equal :
    'k 'v.
    ('k -> 'k -> bool) -> ('v -> 'v -> bool) -> ('k, 'v) t -> ('k, 'v) t -> bool
    =
    fun _cmp__k _cmp__v a__027_ b__028_ ->
      let t__029_, t__030_ = a__027_ in
      let t__031_, t__032_ = b__028_ in
      Stdlib.( && )
        (_cmp__k t__029_ t__031_)
        (if Stdlib.( == ) t__030_ t__032_
         then true
         else (
           match t__030_, t__032_ with
           | `Left _left__033_, `Left _right__034_ -> _cmp__v _left__033_ _right__034_
           | `Right _left__035_, `Right _right__036_ -> _cmp__v _left__035_ _right__036_
           | `Unequal _left__037_, `Unequal _right__038_ ->
             let t__039_, t__040_ = _left__037_ in
             let t__041_, t__042_ = _right__038_ in
             Stdlib.( && ) (_cmp__v t__039_ t__041_) (_cmp__v t__040_ t__042_)
           | x, y -> Stdlib.( = ) x y))
  ;;

  let t_of_sexp :
    'k 'v.
    (Sexplib0.Sexp.t -> 'k)
    -> (Sexplib0.Sexp.t -> 'v)
    -> Sexplib0.Sexp.t
    -> ('k, 'v) t
    =
    let error_source__057_ = "map_intf.ml.Symmetric_diff_element.t" in
    fun _of_k__043_ _of_v__044_ -> function
      | Sexplib0.Sexp.List [ arg0__067_; arg1__068_ ] ->
        let res0__069_ = _of_k__043_ arg0__067_
        and res1__070_ =
          let sexp__066_ = arg1__068_ in
          try
            match sexp__066_ with
            | Sexplib0.Sexp.Atom atom__047_ as _sexp__049_ ->
              (match atom__047_ with
               | "Left" ->
                 Sexplib0.Sexp_conv_error.ptag_takes_args error_source__057_ _sexp__049_
               | "Right" ->
                 Sexplib0.Sexp_conv_error.ptag_takes_args error_source__057_ _sexp__049_
               | "Unequal" ->
                 Sexplib0.Sexp_conv_error.ptag_takes_args error_source__057_ _sexp__049_
               | _ -> Sexplib0.Sexp_conv_error.no_variant_match ())
            | Sexplib0.Sexp.List (Sexplib0.Sexp.Atom atom__047_ :: sexp_args__050_) as
              _sexp__049_ ->
              (match atom__047_ with
               | "Left" as _tag__063_ ->
                 (match sexp_args__050_ with
                  | [ arg0__064_ ] ->
                    let res0__065_ = _of_v__044_ arg0__064_ in
                    `Left res0__065_
                  | _ ->
                    Sexplib0.Sexp_conv_error.ptag_incorrect_n_args
                      error_source__057_
                      _tag__063_
                      _sexp__049_)
               | "Right" as _tag__060_ ->
                 (match sexp_args__050_ with
                  | [ arg0__061_ ] ->
                    let res0__062_ = _of_v__044_ arg0__061_ in
                    `Right res0__062_
                  | _ ->
                    Sexplib0.Sexp_conv_error.ptag_incorrect_n_args
                      error_source__057_
                      _tag__060_
                      _sexp__049_)
               | "Unequal" as _tag__051_ ->
                 (match sexp_args__050_ with
                  | [ arg0__058_ ] ->
                    let res0__059_ =
                      match arg0__058_ with
                      | Sexplib0.Sexp.List [ arg0__052_; arg1__053_ ] ->
                        let res0__054_ = _of_v__044_ arg0__052_
                        and res1__055_ = _of_v__044_ arg1__053_ in
                        res0__054_, res1__055_
                      | sexp__056_ ->
                        Sexplib0.Sexp_conv_error.tuple_of_size_n_expected
                          error_source__057_
                          2
                          sexp__056_
                    in
                    `Unequal res0__059_
                  | _ ->
                    Sexplib0.Sexp_conv_error.ptag_incorrect_n_args
                      error_source__057_
                      _tag__051_
                      _sexp__049_)
               | _ -> Sexplib0.Sexp_conv_error.no_variant_match ())
            | Sexplib0.Sexp.List (Sexplib0.Sexp.List _ :: _) as sexp__048_ ->
              Sexplib0.Sexp_conv_error.nested_list_invalid_poly_var
                error_source__057_
                sexp__048_
            | Sexplib0.Sexp.List [] as sexp__048_ ->
              Sexplib0.Sexp_conv_error.empty_list_invalid_poly_var
                error_source__057_
                sexp__048_
          with
          | Sexplib0.Sexp_conv_error.No_variant_match ->
            Sexplib0.Sexp_conv_error.no_matching_variant_found
              error_source__057_
              sexp__066_
        in
        res0__069_, res1__070_
      | sexp__071_ ->
        Sexplib0.Sexp_conv_error.tuple_of_size_n_expected error_source__057_ 2 sexp__071_
  ;;

  let sexp_of_t :
    'k 'v.
    ('k -> Sexplib0.Sexp.t)
    -> ('v -> Sexplib0.Sexp.t)
    -> ('k, 'v) t
    -> Sexplib0.Sexp.t
    =
    fun _of_k__072_ _of_v__073_ (arg0__081_, arg1__082_) ->
      let res0__083_ = _of_k__072_ arg0__081_
      and res1__084_ =
        match arg1__082_ with
        | `Left v__074_ ->
          Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "Left"; _of_v__073_ v__074_ ]
        | `Right v__075_ ->
          Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "Right"; _of_v__073_ v__075_ ]
        | `Unequal v__076_ ->
          Sexplib0.Sexp.List
            [ Sexplib0.Sexp.Atom "Unequal"
            ; (let arg0__077_, arg1__078_ = v__076_ in
               let res0__079_ = _of_v__073_ arg0__077_
               and res1__080_ = _of_v__073_ arg1__078_ in
               Sexplib0.Sexp.List [ res0__079_; res1__080_ ])
            ]
      in
      Sexplib0.Sexp.List [ res0__083_; res1__084_ ]
  ;;

  let t_sexp_grammar :
    'k 'v.
    'k Sexplib0.Sexp_grammar.t
    -> 'v Sexplib0.Sexp_grammar.t
    -> ('k, 'v) t Sexplib0.Sexp_grammar.t
    =
    fun _'k_sexp_grammar _'v_sexp_grammar ->
      { untyped =
          List
            (Cons
               ( _'k_sexp_grammar.untyped
               , Cons
                   ( Variant
                       { case_sensitivity = Case_sensitive
                       ; clauses =
                           [ No_tag
                               { name = "Left"
                               ; clause_kind =
                                   List_clause
                                     { args = Cons (_'v_sexp_grammar.untyped, Empty) }
                               }
                           ; No_tag
                               { name = "Right"
                               ; clause_kind =
                                   List_clause
                                     { args = Cons (_'v_sexp_grammar.untyped, Empty) }
                               }
                           ; No_tag
                               { name = "Unequal"
                               ; clause_kind =
                                   List_clause
                                     { args =
                                         Cons
                                           ( List
                                               (Cons
                                                  ( _'v_sexp_grammar.untyped
                                                  , Cons (_'v_sexp_grammar.untyped, Empty)
                                                  ))
                                           , Empty )
                                     }
                               }
                           ]
                       }
                   , Empty ) ))
      }
  ;;

  [@@@end]
end

module Merge_element = struct
  type ('left, 'right) t =
    [ `Left of 'left
    | `Right of 'right
    | `Both of 'left * 'right
    ]
  [@@deriving_inline compare, equal, sexp_of]

  let compare :
    'left 'right.
    ('left -> 'left -> int)
    -> ('right -> 'right -> int)
    -> ('left, 'right) t
    -> ('left, 'right) t
    -> int
    =
    fun _cmp__left _cmp__right a__085_ b__086_ ->
    if Stdlib.( == ) a__085_ b__086_
    then 0
    else (
      match a__085_, b__086_ with
      | `Left _left__087_, `Left _right__088_ -> _cmp__left _left__087_ _right__088_
      | `Right _left__089_, `Right _right__090_ -> _cmp__right _left__089_ _right__090_
      | `Both _left__091_, `Both _right__092_ ->
        let t__093_, t__094_ = _left__091_ in
        let t__095_, t__096_ = _right__092_ in
        (match _cmp__left t__093_ t__095_ with
         | 0 -> _cmp__right t__094_ t__096_
         | n -> n)
      | x, y -> Stdlib.compare x y)
  ;;

  let equal :
    'left 'right.
    ('left -> 'left -> bool)
    -> ('right -> 'right -> bool)
    -> ('left, 'right) t
    -> ('left, 'right) t
    -> bool
    =
    fun _cmp__left _cmp__right a__097_ b__098_ ->
      if Stdlib.( == ) a__097_ b__098_
      then true
      else (
        match a__097_, b__098_ with
        | `Left _left__099_, `Left _right__100_ -> _cmp__left _left__099_ _right__100_
        | `Right _left__101_, `Right _right__102_ -> _cmp__right _left__101_ _right__102_
        | `Both _left__103_, `Both _right__104_ ->
          let t__105_, t__106_ = _left__103_ in
          let t__107_, t__108_ = _right__104_ in
          Stdlib.( && ) (_cmp__left t__105_ t__107_) (_cmp__right t__106_ t__108_)
        | x, y -> Stdlib.( = ) x y)
  ;;

  let sexp_of_t :
    'left 'right.
    ('left -> Sexplib0.Sexp.t)
    -> ('right -> Sexplib0.Sexp.t)
    -> ('left, 'right) t
    -> Sexplib0.Sexp.t
    =
    fun _of_left__109_ _of_right__110_ -> function
      | `Left v__111_ ->
        Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "Left"; _of_left__109_ v__111_ ]
      | `Right v__112_ ->
        Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "Right"; _of_right__110_ v__112_ ]
      | `Both v__113_ ->
        Sexplib0.Sexp.List
          [ Sexplib0.Sexp.Atom "Both"
          ; (let arg0__114_, arg1__115_ = v__113_ in
             let res0__116_ = _of_left__109_ arg0__114_
             and res1__117_ = _of_right__110_ arg1__115_ in
             Sexplib0.Sexp.List [ res0__116_; res1__117_ ])
          ]
  ;;

  [@@@end]
end

(** @canonical Base.Map.Continue_or_stop *)
module Continue_or_stop = struct
  type t =
    | Continue
    | Stop
  [@@deriving_inline compare, enumerate, equal, sexp_of]

  let compare = (Stdlib.compare : t -> t -> int)
  let all = ([ Continue; Stop ] : t list)
  let equal = (Stdlib.( = ) : t -> t -> bool)

  let sexp_of_t =
    (function
      | Continue -> Sexplib0.Sexp.Atom "Continue"
      | Stop -> Sexplib0.Sexp.Atom "Stop"
                : t -> Sexplib0.Sexp.t)
  ;;

  [@@@end]
end

(** @canonical Base.Map.Finished_or_unfinished *)
module Finished_or_unfinished = struct
  type t =
    | Finished
    | Unfinished
  [@@deriving_inline compare, enumerate, equal, sexp_of]

  let compare = (Stdlib.compare : t -> t -> int)
  let all = ([ Finished; Unfinished ] : t list)
  let equal = (Stdlib.( = ) : t -> t -> bool)

  let sexp_of_t =
    (function
      | Finished -> Sexplib0.Sexp.Atom "Finished"
      | Unfinished -> Sexplib0.Sexp.Atom "Unfinished"
                      : t -> Sexplib0.Sexp.t)
  ;;

  [@@@end]
end

module type Accessors_generic = sig
  type ('a, 'b, 'cmp) t
  type ('a, 'b, 'cmp) tree
  type 'a key
  type 'cmp cmp
  type ('a, 'cmp, 'z) access_options

  val invariants : ('k, 'cmp, ('k, 'v, 'cmp) t -> bool) access_options
  val is_empty : (_, _, _) t -> bool
  val length : (_, _, _) t -> int

  val add
    : ( 'k
      , 'cmp
      , ('k, 'v, 'cmp) t -> key:'k key -> data:'v -> ('k, 'v, 'cmp) t Or_duplicate.t )
        access_options

  val add_exn
    : ( 'k
      , 'cmp
      , ('k, 'v, 'cmp) t -> key:'k key -> data:'v -> ('k, 'v, 'cmp) t )
        access_options

  val set
    : ( 'k
      , 'cmp
      , ('k, 'v, 'cmp) t -> key:'k key -> data:'v -> ('k, 'v, 'cmp) t )
        access_options

  val add_multi
    : ( 'k
      , 'cmp
      , ('k, 'v list, 'cmp) t -> key:'k key -> data:'v -> ('k, 'v list, 'cmp) t )
        access_options

  val remove_multi
    : ('k, 'cmp, ('k, 'v list, 'cmp) t -> 'k key -> ('k, 'v list, 'cmp) t) access_options

  val find_multi : ('k, 'cmp, ('k, 'v list, 'cmp) t -> 'k key -> 'v list) access_options

  val change
    : ( 'k
      , 'cmp
      , ('k, 'v, 'cmp) t
      -> 'k key
      -> f:(('v option -> 'v option)[@local])
      -> ('k, 'v, 'cmp) t )
        access_options

  val update
    : ( 'k
      , 'cmp
      , ('k, 'v, 'cmp) t -> 'k key -> f:(('v option -> 'v)[@local]) -> ('k, 'v, 'cmp) t
      )
        access_options

  val find : ('k, 'cmp, ('k, 'v, 'cmp) t -> 'k key -> 'v option) access_options
  val find_exn : ('k, 'cmp, ('k, 'v, 'cmp) t -> 'k key -> 'v) access_options
  val remove : ('k, 'cmp, ('k, 'v, 'cmp) t -> 'k key -> ('k, 'v, 'cmp) t) access_options
  val mem : ('k, 'cmp, ('k, _, 'cmp) t -> 'k key -> bool) access_options
  val iter_keys : ('k, _, _) t -> f:(('k key -> unit)[@local]) -> unit
  val iter : (_, 'v, _) t -> f:(('v -> unit)[@local]) -> unit
  val iteri : ('k, 'v, _) t -> f:((key:'k key -> data:'v -> unit)[@local]) -> unit

  val iteri_until
    :  ('k, 'v, _) t
    -> f:((key:'k key -> data:'v -> Continue_or_stop.t)[@local])
    -> Finished_or_unfinished.t

  val iter2
    : ( 'k
      , 'cmp
      , ('k, 'v1, 'cmp) t
      -> ('k, 'v2, 'cmp) t
      -> f:((key:'k key -> data:('v1, 'v2) Merge_element.t -> unit)[@local])
      -> unit )
        access_options

  val map : ('k, 'v1, 'cmp) t -> f:(('v1 -> 'v2)[@local]) -> ('k, 'v2, 'cmp) t

  val mapi
    :  ('k, 'v1, 'cmp) t
    -> f:((key:'k key -> data:'v1 -> 'v2)[@local])
    -> ('k, 'v2, 'cmp) t

  val fold
    :  ('k, 'v, _) t
    -> init:'acc
    -> f:((key:'k key -> data:'v -> 'acc -> 'acc)[@local])
    -> 'acc

  val fold_until
    :  ('k, 'v, _) t
    -> init:'acc
    -> f:
         ((key:'k key -> data:'v -> 'acc -> ('acc, 'final) Container.Continue_or_stop.t)
          [@local])
    -> finish:(('acc -> 'final)[@local])
    -> 'final

  val fold_right
    :  ('k, 'v, _) t
    -> init:'acc
    -> f:((key:'k key -> data:'v -> 'acc -> 'acc)[@local])
    -> 'acc

  val fold2
    : ( 'k
      , 'cmp
      , ('k, 'v1, 'cmp) t
      -> ('k, 'v2, 'cmp) t
      -> init:'acc
      -> f:((key:'k key -> data:('v1, 'v2) Merge_element.t -> 'acc -> 'acc)[@local])
      -> 'acc )
        access_options

  val filter_keys : ('k, 'v, 'cmp) t -> f:(('k key -> bool)[@local]) -> ('k, 'v, 'cmp) t
  val filter : ('k, 'v, 'cmp) t -> f:(('v -> bool)[@local]) -> ('k, 'v, 'cmp) t

  val filteri
    :  ('k, 'v, 'cmp) t
    -> f:((key:'k key -> data:'v -> bool)[@local])
    -> ('k, 'v, 'cmp) t

  val filter_map
    :  ('k, 'v1, 'cmp) t
    -> f:(('v1 -> 'v2 option)[@local])
    -> ('k, 'v2, 'cmp) t

  val filter_mapi
    :  ('k, 'v1, 'cmp) t
    -> f:((key:'k key -> data:'v1 -> 'v2 option)[@local])
    -> ('k, 'v2, 'cmp) t

  val partition_mapi
    :  ('k, 'v1, 'cmp) t
    -> f:((key:'k key -> data:'v1 -> ('v2, 'v3) Either.t)[@local])
    -> ('k, 'v2, 'cmp) t * ('k, 'v3, 'cmp) t

  val partition_map
    :  ('k, 'v1, 'cmp) t
    -> f:(('v1 -> ('v2, 'v3) Either.t)[@local])
    -> ('k, 'v2, 'cmp) t * ('k, 'v3, 'cmp) t

  val partitioni_tf
    :  ('k, 'v, 'cmp) t
    -> f:((key:'k key -> data:'v -> bool)[@local])
    -> ('k, 'v, 'cmp) t * ('k, 'v, 'cmp) t

  val partition_tf
    :  ('k, 'v, 'cmp) t
    -> f:(('v -> bool)[@local])
    -> ('k, 'v, 'cmp) t * ('k, 'v, 'cmp) t

  val combine_errors
    : ( 'k
      , 'cmp
      , ('k, 'v Or_error.t, 'cmp) t -> ('k, 'v, 'cmp) t Or_error.t )
        access_options

  val compare_direct
    : ( 'k
      , 'cmp
      , ('v -> 'v -> int) -> ('k, 'v, 'cmp) t -> ('k, 'v, 'cmp) t -> int )
        access_options

  val equal
    : ( 'k
      , 'cmp
      , ('v -> 'v -> bool) -> ('k, 'v, 'cmp) t -> ('k, 'v, 'cmp) t -> bool )
        access_options

  val keys : ('k, _, _) t -> 'k key list
  val data : (_, 'v, _) t -> 'v list

  val to_alist
    :  ?key_order:[ `Increasing | `Decreasing ]
    -> ('k, 'v, _) t
    -> ('k key * 'v) list

  val merge
    : ( 'k
      , 'cmp
      , ('k, 'v1, 'cmp) t
      -> ('k, 'v2, 'cmp) t
      -> f:((key:'k key -> ('v1, 'v2) Merge_element.t -> 'v3 option)[@local])
      -> ('k, 'v3, 'cmp) t )
        access_options

  val merge_skewed
    : ( 'k
      , 'cmp
      , ('k, 'v, 'cmp) t
      -> ('k, 'v, 'cmp) t
      -> combine:((key:'k key -> 'v -> 'v -> 'v)[@local])
      -> ('k, 'v, 'cmp) t )
        access_options

  val symmetric_diff
    : ( 'k
      , 'cmp
      , ('k, 'v, 'cmp) t
      -> ('k, 'v, 'cmp) t
      -> data_equal:('v -> 'v -> bool)
      -> ('k key, 'v) Symmetric_diff_element.t Sequence.t )
        access_options

  val fold_symmetric_diff
    : ( 'k
      , 'cmp
      , ('k, 'v, 'cmp) t
      -> ('k, 'v, 'cmp) t
      -> data_equal:(('v -> 'v -> bool)[@local])
      -> init:'acc
      -> f:(('acc -> ('k key, 'v) Symmetric_diff_element.t -> 'acc)[@local])
      -> 'acc )
        access_options

  val min_elt : ('k, 'v, _) t -> ('k key * 'v) option
  val min_elt_exn : ('k, 'v, _) t -> 'k key * 'v
  val max_elt : ('k, 'v, _) t -> ('k key * 'v) option
  val max_elt_exn : ('k, 'v, _) t -> 'k key * 'v
  val for_all : ('k, 'v, _) t -> f:(('v -> bool)[@local]) -> bool
  val for_alli : ('k, 'v, _) t -> f:((key:'k key -> data:'v -> bool)[@local]) -> bool
  val exists : ('k, 'v, _) t -> f:(('v -> bool)[@local]) -> bool
  val existsi : ('k, 'v, _) t -> f:((key:'k key -> data:'v -> bool)[@local]) -> bool
  val count : ('k, 'v, _) t -> f:(('v -> bool)[@local]) -> int
  val counti : ('k, 'v, _) t -> f:((key:'k key -> data:'v -> bool)[@local]) -> int

  val split
    : ( 'k
      , 'cmp
      , ('k, 'v, 'cmp) t
      -> 'k key
      -> ('k, 'v, 'cmp) t * ('k key * 'v) option * ('k, 'v, 'cmp) t )
        access_options

  val split_le_gt
    : ( 'k
      , 'cmp
      , ('k, 'v, 'cmp) t -> 'k key -> ('k, 'v, 'cmp) t * ('k, 'v, 'cmp) t )
        access_options

  val split_lt_ge
    : ( 'k
      , 'cmp
      , ('k, 'v, 'cmp) t -> 'k key -> ('k, 'v, 'cmp) t * ('k, 'v, 'cmp) t )
        access_options

  val append
    : ( 'k
      , 'cmp
      , lower_part:('k, 'v, 'cmp) t
      -> upper_part:('k, 'v, 'cmp) t
      -> [ `Ok of ('k, 'v, 'cmp) t | `Overlapping_key_ranges ] )
        access_options

  val subrange
    : ( 'k
      , 'cmp
      , ('k, 'v, 'cmp) t
      -> lower_bound:'k key Maybe_bound.t
      -> upper_bound:'k key Maybe_bound.t
      -> ('k, 'v, 'cmp) t )
        access_options

  val fold_range_inclusive
    : ( 'k
      , 'cmp
      , ('k, 'v, 'cmp) t
      -> min:'k key
      -> max:'k key
      -> init:'acc
      -> f:((key:'k key -> data:'v -> 'acc -> 'acc)[@local])
      -> 'acc )
        access_options

  val range_to_alist
    : ( 'k
      , 'cmp
      , ('k, 'v, 'cmp) t -> min:'k key -> max:'k key -> ('k key * 'v) list )
        access_options

  val closest_key
    : ( 'k
      , 'cmp
      , ('k, 'v, 'cmp) t
      -> [ `Greater_or_equal_to | `Greater_than | `Less_or_equal_to | `Less_than ]
      -> 'k key
      -> ('k key * 'v) option )
        access_options

  val nth : ('k, 'v, 'cmp) t -> int -> ('k key * 'v) option
  val nth_exn : ('k, 'v, 'cmp) t -> int -> 'k key * 'v
  val rank : ('k, 'cmp, ('k, _, 'cmp) t -> 'k key -> int option) access_options
  val to_tree : ('k, 'v, 'cmp) t -> ('k key, 'v, 'cmp) tree

  val to_sequence
    : ( 'k
      , 'cmp
      , ?order:[ `Increasing_key | `Decreasing_key ]
      -> ?keys_greater_or_equal_to:'k key
      -> ?keys_less_or_equal_to:'k key
      -> ('k, 'v, 'cmp) t
      -> ('k key * 'v) Sequence.t )
        access_options

  val binary_search
    : ( 'k
      , 'cmp
      , ('k, 'v, 'cmp) t
      -> compare:((key:'k key -> data:'v -> 'key -> int)[@local])
      -> Binary_searchable.Which_target_by_key.t
      -> 'key
      -> ('k key * 'v) option )
        access_options

  val binary_search_segmented
    : ( 'k
      , 'cmp
      , ('k, 'v, 'cmp) t
      -> segment_of:((key:'k key -> data:'v -> [ `Left | `Right ])[@local])
      -> Binary_searchable.Which_target_by_segment.t
      -> ('k key * 'v) option )
        access_options

  val binary_search_subrange
    : ( 'k
      , 'cmp
      , ('k, 'v, 'cmp) t
      -> compare:((key:'k key -> data:'v -> 'bound -> int)[@local])
      -> lower_bound:'bound Maybe_bound.t
      -> upper_bound:'bound Maybe_bound.t
      -> ('k, 'v, 'cmp) t )
        access_options

  module Make_applicative_traversals (A : Applicative.Lazy_applicative) : sig
    val mapi
      :  ('k, 'v1, 'cmp) t
      -> f:(key:'k key -> data:'v1 -> 'v2 A.t)
      -> ('k, 'v2, 'cmp) t A.t

    val filter_mapi
      :  ('k, 'v1, 'cmp) t
      -> f:(key:'k key -> data:'v1 -> 'v2 option A.t)
      -> ('k, 'v2, 'cmp) t A.t
  end
end

module type Creators_generic = sig
  type ('k, 'v, 'cmp) t
  type ('k, 'v, 'cmp) tree
  type 'k key
  type ('a, 'cmp, 'z) create_options
  type ('a, 'cmp, 'z) access_options
  type 'cmp cmp

  val empty : ('k, 'cmp, ('k, _, 'cmp) t) create_options
  val singleton : ('k, 'cmp, 'k key -> 'v -> ('k, 'v, 'cmp) t) create_options

  val map_keys
    : ( 'k2
      , 'cmp2
      , ('k1, 'v, 'cmp1) t
      -> f:(('k1 key -> 'k2 key)[@local])
      -> [ `Ok of ('k2, 'v, 'cmp2) t | `Duplicate_key of 'k2 key ] )
        create_options

  val map_keys_exn
    : ( 'k2
      , 'cmp2
      , ('k1, 'v, 'cmp1) t -> f:(('k1 key -> 'k2 key)[@local]) -> ('k2, 'v, 'cmp2) t )
        create_options

  val transpose_keys
    : ( 'k1
      , 'cmp1
      , ( 'k2
        , 'cmp2
        , ('k1, ('k2, 'a, 'cmp2) t, 'cmp1) t -> ('k2, ('k1, 'a, 'cmp1) t, 'cmp2) t )
          create_options )
        access_options

  val of_sorted_array
    : ('k, 'cmp, ('k key * 'v) array -> ('k, 'v, 'cmp) t Or_error.t) create_options

  val of_sorted_array_unchecked
    : ('k, 'cmp, ('k key * 'v) array -> ('k, 'v, 'cmp) t) create_options

  val of_increasing_iterator_unchecked
    : ( 'k
      , 'cmp
      , len:int -> f:((int -> 'k key * 'v)[@local]) -> ('k, 'v, 'cmp) t )
        create_options

  val of_alist
    : ( 'k
      , 'cmp
      , ('k key * 'v) list -> [ `Ok of ('k, 'v, 'cmp) t | `Duplicate_key of 'k key ] )
        create_options

  val of_alist_or_error
    : ('k, 'cmp, ('k key * 'v) list -> ('k, 'v, 'cmp) t Or_error.t) create_options

  val of_alist_exn : ('k, 'cmp, ('k key * 'v) list -> ('k, 'v, 'cmp) t) create_options

  val of_alist_multi
    : ('k, 'cmp, ('k key * 'v) list -> ('k, 'v list, 'cmp) t) create_options

  val of_alist_fold
    : ( 'k
      , 'cmp
      , ('k key * 'v1) list
      -> init:'v2
      -> f:(('v2 -> 'v1 -> 'v2)[@local])
      -> ('k, 'v2, 'cmp) t )
        create_options

  val of_alist_reduce
    : ( 'k
      , 'cmp
      , ('k key * 'v) list -> f:(('v -> 'v -> 'v)[@local]) -> ('k, 'v, 'cmp) t )
        create_options

  val of_increasing_sequence
    : ('k, 'cmp, ('k key * 'v) Sequence.t -> ('k, 'v, 'cmp) t Or_error.t) create_options

  val of_sequence
    : ( 'k
      , 'cmp
      , ('k key * 'v) Sequence.t -> [ `Ok of ('k, 'v, 'cmp) t | `Duplicate_key of 'k key ]
      )
        create_options

  val of_sequence_or_error
    : ('k, 'cmp, ('k key * 'v) Sequence.t -> ('k, 'v, 'cmp) t Or_error.t) create_options

  val of_sequence_exn
    : ('k, 'cmp, ('k key * 'v) Sequence.t -> ('k, 'v, 'cmp) t) create_options

  val of_sequence_multi
    : ('k, 'cmp, ('k key * 'v) Sequence.t -> ('k, 'v list, 'cmp) t) create_options

  val of_sequence_fold
    : ( 'k
      , 'cmp
      , ('k key * 'v1) Sequence.t
      -> init:'v2
      -> f:(('v2 -> 'v1 -> 'v2)[@local])
      -> ('k, 'v2, 'cmp) t )
        create_options

  val of_sequence_reduce
    : ( 'k
      , 'cmp
      , ('k key * 'v) Sequence.t -> f:(('v -> 'v -> 'v)[@local]) -> ('k, 'v, 'cmp) t )
        create_options

  val of_list_with_key
    : ( 'k
      , 'cmp
      , 'v list
      -> get_key:(('v -> 'k key)[@local])
      -> [ `Ok of ('k, 'v, 'cmp) t | `Duplicate_key of 'k key ] )
        create_options

  val of_list_with_key_or_error
    : ( 'k
      , 'cmp
      , 'v list -> get_key:(('v -> 'k key)[@local]) -> ('k, 'v, 'cmp) t Or_error.t )
        create_options

  val of_list_with_key_exn
    : ( 'k
      , 'cmp
      , 'v list -> get_key:(('v -> 'k key)[@local]) -> ('k, 'v, 'cmp) t )
        create_options

  val of_list_with_key_multi
    : ( 'k
      , 'cmp
      , 'v list -> get_key:(('v -> 'k key)[@local]) -> ('k, 'v list, 'cmp) t )
        create_options

  val of_iteri
    : ( 'k
      , 'cmp
      , iteri:((f:((key:'k key -> data:'v -> unit)[@local]) -> unit)[@local])
      -> [ `Ok of ('k, 'v, 'cmp) t | `Duplicate_key of 'k key ] )
        create_options

  val of_iteri_exn
    : ( 'k
      , 'cmp
      , iteri:((f:((key:'k key -> data:'v -> unit)[@local]) -> unit)[@local])
      -> ('k, 'v, 'cmp) t )
        create_options

  val of_tree : ('k, 'cmp, ('k key, 'v, 'cmp) tree -> ('k, 'v, 'cmp) t) create_options
end

module type Creators_and_accessors_generic = sig
  type ('a, 'b, 'c) t
  type ('a, 'b, 'c) tree
  type 'a key
  type 'a cmp
  type ('a, 'b, 'c) create_options
  type ('a, 'b, 'c) access_options

  include
    Creators_generic
    with type ('a, 'b, 'c) t := ('a, 'b, 'c) t
    with type ('a, 'b, 'c) tree := ('a, 'b, 'c) tree
    with type 'a key := 'a key
    with type 'a cmp := 'a cmp
    with type ('a, 'b, 'c) create_options := ('a, 'b, 'c) create_options
    with type ('a, 'b, 'c) access_options := ('a, 'b, 'c) access_options

  include
    Accessors_generic
    with type ('a, 'b, 'c) t := ('a, 'b, 'c) t
    with type ('a, 'b, 'c) tree := ('a, 'b, 'c) tree
    with type 'a key := 'a key
    with type 'a cmp := 'a cmp
    with type ('a, 'b, 'c) access_options := ('a, 'b, 'c) access_options
end

module type S_poly = sig
  type ('a, 'b) t
  type ('a, 'b) tree
  type comparator_witness

  include
    Creators_and_accessors_generic
    with type ('a, 'b, 'c) t := ('a, 'b) t
    with type ('a, 'b, 'c) tree := ('a, 'b) tree
    with type 'k key := 'k
    with type 'c cmp := comparator_witness
    with type ('a, 'b, 'c) create_options := ('a, 'b, 'c) Without_comparator.t
    with type ('a, 'b, 'c) access_options := ('a, 'b, 'c) Without_comparator.t
end

module type For_deriving = sig
  type ('a, 'b, 'c) t

  module type Sexp_of_m = sig
    type t [@@deriving_inline sexp_of]

    val sexp_of_t : t -> Sexplib0.Sexp.t

    [@@@end]
  end

  module type M_of_sexp = sig
    type t [@@deriving_inline of_sexp]

    val t_of_sexp : Sexplib0.Sexp.t -> t

    [@@@end]

    include Comparator.S with type t := t
  end

  module type M_sexp_grammar = sig
    type t [@@deriving_inline sexp_grammar]

    val t_sexp_grammar : t Sexplib0.Sexp_grammar.t

    [@@@end]
  end

  module type Compare_m = sig end
  module type Equal_m = sig end
  module type Hash_fold_m = Hasher.S

  val sexp_of_m__t
    :  (module Sexp_of_m with type t = 'k)
    -> ('v -> Sexp.t)
    -> ('k, 'v, 'cmp) t
    -> Sexp.t

  val m__t_of_sexp
    :  (module M_of_sexp with type t = 'k and type comparator_witness = 'cmp)
    -> (Sexp.t -> 'v)
    -> Sexp.t
    -> ('k, 'v, 'cmp) t

  val m__t_sexp_grammar
    :  (module M_sexp_grammar with type t = 'k)
    -> 'v Sexplib0.Sexp_grammar.t
    -> ('k, 'v, 'cmp) t Sexplib0.Sexp_grammar.t

  val compare_m__t
    :  (module Compare_m)
    -> ('v -> 'v -> int)
    -> ('k, 'v, 'cmp) t
    -> ('k, 'v, 'cmp) t
    -> int

  val equal_m__t
    :  (module Equal_m)
    -> ('v -> 'v -> bool)
    -> ('k, 'v, 'cmp) t
    -> ('k, 'v, 'cmp) t
    -> bool

  val hash_fold_m__t
    :  (module Hash_fold_m with type t = 'k)
    -> (Hash.state -> 'v -> Hash.state)
    -> Hash.state
    -> ('k, 'v, _) t
    -> Hash.state
end

module type Map = sig
  (** [Map] is a functional data structure (balanced binary tree) implementing finite maps
      over a totally-ordered domain, called a "key". *)

  type (!'key, +!'value, !'cmp) t

  module Or_duplicate = Or_duplicate
  module Continue_or_stop = Continue_or_stop

  module Finished_or_unfinished : sig
    type t = Finished_or_unfinished.t =
      | Finished
      | Unfinished
    [@@deriving_inline compare, enumerate, equal, sexp_of]

    include Ppx_compare_lib.Comparable.S with type t := t
    include Ppx_enumerate_lib.Enumerable.S with type t := t
    include Ppx_compare_lib.Equal.S with type t := t

    val sexp_of_t : t -> Sexplib0.Sexp.t

    [@@@end]

    (** Maps [Continue] to [Finished] and [Stop] to [Unfinished]. *)
    val of_continue_or_stop : Continue_or_stop.t -> t

    (** Maps [Finished] to [Continue] and [Unfinished] to [Stop]. *)
    val to_continue_or_stop : t -> Continue_or_stop.t
  end

  module Merge_element : sig
    type ('left, 'right) t =
      [ `Left of 'left
      | `Right of 'right
      | `Both of 'left * 'right
      ]
    [@@deriving_inline compare, equal, sexp_of]

    val compare
      :  ('left -> 'left -> int)
      -> ('right -> 'right -> int)
      -> ('left, 'right) t
      -> ('left, 'right) t
      -> int

    val equal
      :  ('left -> 'left -> bool)
      -> ('right -> 'right -> bool)
      -> ('left, 'right) t
      -> ('left, 'right) t
      -> bool

    val sexp_of_t
      :  ('left -> Sexplib0.Sexp.t)
      -> ('right -> Sexplib0.Sexp.t)
      -> ('left, 'right) t
      -> Sexplib0.Sexp.t

    [@@@end]

    val left : ('left, _) t -> 'left option
    val right : (_, 'right) t -> 'right option
    val left_value : ('left, _) t -> default:'left -> 'left
    val right_value : (_, 'right) t -> default:'right -> 'right

    val values
      :  ('left, 'right) t
      -> left_default:'left
      -> right_default:'right
      -> 'left * 'right
  end

  type ('k, 'cmp) comparator = ('k, 'cmp) Comparator.Module.t
  [@@deprecated "[since 2021-12] use [Comparator.Module.t] instead"]

  (** Test if the invariants of the internal AVL search tree hold. *)
  val invariants : (_, _, _) t -> bool

  (** Returns a first-class module that can be used to build other map/set/etc.
      with the same notion of comparison. *)
  val comparator_s : ('a, _, 'cmp) t -> ('a, 'cmp) Comparator.Module.t

  val comparator : ('a, _, 'cmp) t -> ('a, 'cmp) Comparator.t

  (** The empty map. *)
  val empty : ('a, 'cmp) Comparator.Module.t -> ('a, 'b, 'cmp) t

  (** A map with one (key, data) pair. *)
  val singleton : ('a, 'cmp) Comparator.Module.t -> 'a -> 'b -> ('a, 'b, 'cmp) t

  (** Creates a map from an association list with unique keys. *)
  val of_alist
    :  ('a, 'cmp) Comparator.Module.t
    -> ('a * 'b) list
    -> [ `Ok of ('a, 'b, 'cmp) t | `Duplicate_key of 'a ]

  (** Creates a map from an association list with unique keys, returning an error if
      duplicate ['a] keys are found. *)
  val of_alist_or_error
    :  ('a, 'cmp) Comparator.Module.t
    -> ('a * 'b) list
    -> ('a, 'b, 'cmp) t Or_error.t

  (** Creates a map from an association list with unique keys, raising an exception if
      duplicate ['a] keys are found. *)
  val of_alist_exn : ('a, 'cmp) Comparator.Module.t -> ('a * 'b) list -> ('a, 'b, 'cmp) t

  (** Creates a map from an association list with possibly repeated keys. The values in
      the map for a given key appear in the same order as they did in the association
      list. *)
  val of_alist_multi
    :  ('a, 'cmp) Comparator.Module.t
    -> ('a * 'b) list
    -> ('a, 'b list, 'cmp) t

  (** Combines an association list into a map, folding together bound values with common
      keys. The accumulator is per-key.

      Example:

      {[
        # let map = String.Map.of_alist_fold
                      [ "a", 1; "a", 10; "b", 2; "b", 20; "b", 200 ]
                      ~init:Int.Set.empty
                      ~f:Set.add
          in
          print_s [%sexp (map : Int.Set.t String.Map.t)];;
        ((a (1 10)) (b (2 20 200)))
        - : unit = ()
      ]}
  *)
  val of_alist_fold
    :  ('a, 'cmp) Comparator.Module.t
    -> ('a * 'b) list
    -> init:'c
    -> f:(('c -> 'b -> 'c)[@local])
    -> ('a, 'c, 'cmp) t

  (** Combines an association list into a map, reducing together bound values with common
      keys. *)
  val of_alist_reduce
    :  ('a, 'cmp) Comparator.Module.t
    -> ('a * 'b) list
    -> f:(('b -> 'b -> 'b)[@local])
    -> ('a, 'b, 'cmp) t

  (** [of_iteri ~iteri] behaves like [of_alist], except that instead of taking a concrete
      data structure, it takes an iteration function.  For instance, to convert a string table
      into a map: [of_iteri (module String) ~f:(Hashtbl.iteri table)].  It is faster than
      adding the elements one by one. *)
  val of_iteri
    :  ('a, 'cmp) Comparator.Module.t
    -> iteri:((f:((key:'a -> data:'b -> unit)[@local]) -> unit)[@local])
    -> [ `Ok of ('a, 'b, 'cmp) t | `Duplicate_key of 'a ]

  (** Like [of_iteri] except that it raises an exception if duplicate ['a] keys are found. *)
  val of_iteri_exn
    :  ('a, 'cmp) Comparator.Module.t
    -> iteri:((f:((key:'a -> data:'b -> unit)[@local]) -> unit)[@local])
    -> ('a, 'b, 'cmp) t

  (** Creates a map from a sorted array of key-data pairs. The input array must be sorted
      (either in ascending or descending order), as given by the relevant comparator, and
      must not contain duplicate keys. If either of these conditions does not hold,
      an error is returned.  *)
  val of_sorted_array
    :  ('a, 'cmp) Comparator.Module.t
    -> ('a * 'b) array
    -> ('a, 'b, 'cmp) t Or_error.t

  (** Like [of_sorted_array] except that it returns a map with broken invariants when an
      [Error] would have been returned. *)
  val of_sorted_array_unchecked
    :  ('a, 'cmp) Comparator.Module.t
    -> ('a * 'b) array
    -> ('a, 'b, 'cmp) t

  (** [of_increasing_iterator_unchecked c ~len ~f] behaves like [of_sorted_array_unchecked c
      (Array.init len ~f)], with the additional restriction that a decreasing order is not
      supported.  The advantage is not requiring you to allocate an intermediate array.  [f]
      will be called with 0, 1, ... [len - 1], in order. *)
  val of_increasing_iterator_unchecked
    :  ('a, 'cmp) Comparator.Module.t
    -> len:int
    -> f:((int -> 'a * 'b)[@local])
    -> ('a, 'b, 'cmp) t

  (** [of_increasing_sequence c seq] behaves like [of_sorted_array c (Sequence.to_array
      seq)], but does not allocate the intermediate array.

      The sequence will be folded over once, and the additional time complexity is {e O(n)}.
  *)
  val of_increasing_sequence
    :  ('k, 'cmp) Comparator.Module.t
    -> ('k * 'v) Sequence.t
    -> ('k, 'v, 'cmp) t Or_error.t

  (** Creates a map from an association sequence with unique keys.

      [of_sequence c seq] behaves like [of_alist c (Sequence.to_list seq)] but
      does not allocate the intermediate list.

      If your sequence is increasing, use [of_increasing_sequence].
  *)
  val of_sequence
    :  ('k, 'cmp) Comparator.Module.t
    -> ('k * 'v) Sequence.t
    -> [ `Ok of ('k, 'v, 'cmp) t | `Duplicate_key of 'k ]

  (** Creates a map from an association sequence with unique keys, returning an error if
      duplicate ['a] keys are found.

      [of_sequence_or_error c seq] behaves like [of_alist_or_error c (Sequence.to_list seq)]
      but does not allocate the intermediate list.
  *)
  val of_sequence_or_error
    :  ('a, 'cmp) Comparator.Module.t
    -> ('a * 'b) Sequence.t
    -> ('a, 'b, 'cmp) t Or_error.t

  (** Creates a map from an association sequence with unique keys, raising an exception if
      duplicate ['a] keys are found.

      [of_sequence_exn c seq] behaves like [of_alist_exn c (Sequence.to_list seq)] but
      does not allocate the intermediate list.
  *)
  val of_sequence_exn
    :  ('a, 'cmp) Comparator.Module.t
    -> ('a * 'b) Sequence.t
    -> ('a, 'b, 'cmp) t

  (** Creates a map from an association sequence with possibly repeated keys. The values in
      the map for a given key appear in the same order as they did in the association
      list.

      [of_sequence_multi c seq] behaves like [of_alist_exn c (Sequence.to_list seq)] but
      does not allocate the intermediate list.
  *)
  val of_sequence_multi
    :  ('a, 'cmp) Comparator.Module.t
    -> ('a * 'b) Sequence.t
    -> ('a, 'b list, 'cmp) t

  (** Combines an association sequence into a map, folding together bound values with common
      keys.

      [of_sequence_fold c seq ~init ~f] behaves like [of_alist_fold c (Sequence.to_list seq) ~init ~f]
      but does not allocate the intermediate list.
  *)
  val of_sequence_fold
    :  ('a, 'cmp) Comparator.Module.t
    -> ('a * 'b) Sequence.t
    -> init:'c
    -> f:(('c -> 'b -> 'c)[@local])
    -> ('a, 'c, 'cmp) t

  (** Combines an association sequence into a map, reducing together bound values with common
      keys.

      [of_sequence_reduce c seq ~f] behaves like [of_alist_reduce c (Sequence.to_list seq) ~f]
      but does not allocate the intermediate list.  *)
  val of_sequence_reduce
    :  ('a, 'cmp) Comparator.Module.t
    -> ('a * 'b) Sequence.t
    -> f:(('b -> 'b -> 'b)[@local])
    -> ('a, 'b, 'cmp) t

  (** Constructs a map from a list of values, where [get_key] extracts a key from a value.
  *)
  val of_list_with_key
    :  ('k, 'cmp) Comparator.Module.t
    -> 'v list
    -> get_key:(('v -> 'k)[@local])
    -> [ `Ok of ('k, 'v, 'cmp) t | `Duplicate_key of 'k ]

  (** Like [of_list_with_key]; returns [Error] on duplicate key. *)
  val of_list_with_key_or_error
    :  ('k, 'cmp) Comparator.Module.t
    -> 'v list
    -> get_key:(('v -> 'k)[@local])
    -> ('k, 'v, 'cmp) t Or_error.t

  (** Like [of_list_with_key]; raises on duplicate key. *)
  val of_list_with_key_exn
    :  ('k, 'cmp) Comparator.Module.t
    -> 'v list
    -> get_key:(('v -> 'k)[@local])
    -> ('k, 'v, 'cmp) t

  (** Like [of_list_with_key]; produces lists of all values associated with each key. *)
  val of_list_with_key_multi
    :  ('k, 'cmp) Comparator.Module.t
    -> 'v list
    -> get_key:(('v -> 'k)[@local])
    -> ('k, 'v list, 'cmp) t

  (** Tests whether a map is empty. *)
  val is_empty : (_, _, _) t -> bool

  (** [length map] returns the number of elements in [map].  O(1), but [Tree.length] is
      O(n). *)
  val length : (_, _, _) t -> int

  (** Returns a new map with the specified new binding; if the key was already bound, its
      previous binding disappears. *)
  val set : ('k, 'v, 'cmp) t -> key:'k -> data:'v -> ('k, 'v, 'cmp) t

  (** [add t ~key ~data] adds a new entry to [t] mapping [key] to [data] and returns [`Ok]
      with the new map, or if [key] is already present in [t], returns [`Duplicate]. *)
  val add : ('k, 'v, 'cmp) t -> key:'k -> data:'v -> ('k, 'v, 'cmp) t Or_duplicate.t

  val add_exn : ('k, 'v, 'cmp) t -> key:'k -> data:'v -> ('k, 'v, 'cmp) t

  (** If [key] is not present then add a singleton list, otherwise, cons data onto the
      head of the existing list. *)
  val add_multi : ('k, 'v list, 'cmp) t -> key:'k -> data:'v -> ('k, 'v list, 'cmp) t

  (** If the key is present, then remove its head element; if the result is empty, remove
      the key. *)
  val remove_multi : ('k, 'v list, 'cmp) t -> 'k -> ('k, 'v list, 'cmp) t

  (** Returns the value bound to the given key, or the empty list if there is none. *)
  val find_multi : ('k, 'v list, 'cmp) t -> 'k -> 'v list

  (** [change t key ~f] returns a new map [m] that is the same as [t] on all keys except
      for [key], and whose value for [key] is defined by [f], i.e., [find m key = f (find
      t key)]. *)
  val change
    :  ('k, 'v, 'cmp) t
    -> 'k
    -> f:(('v option -> 'v option)[@local])
    -> ('k, 'v, 'cmp) t

  (** [update t key ~f] is [change t key ~f:(fun o -> Some (f o))]. *)
  val update : ('k, 'v, 'cmp) t -> 'k -> f:(('v option -> 'v)[@local]) -> ('k, 'v, 'cmp) t


  (** Returns [Some value] bound to the given key, or [None] if none exists. *)
  val find : ('k, 'v, 'cmp) t -> 'k -> 'v option

  (** Returns the value bound to the given key, raising [Stdlib.Not_found] or [Not_found_s]
      if none exists. *)
  val find_exn : ('k, 'v, 'cmp) t -> 'k -> 'v

  (** Returns a new map with any binding for the key in question removed. *)
  val remove : ('k, 'v, 'cmp) t -> 'k -> ('k, 'v, 'cmp) t

  (** [mem map key] tests whether [map] contains a binding for [key]. *)
  val mem : ('k, _, 'cmp) t -> 'k -> bool

  val iter_keys : ('k, _, _) t -> f:(('k -> unit)[@local]) -> unit
  val iter : (_, 'v, _) t -> f:(('v -> unit)[@local]) -> unit
  val iteri : ('k, 'v, _) t -> f:((key:'k -> data:'v -> unit)[@local]) -> unit

  (** Iterates until the first time [f] returns [Stop]. If [f] returns [Stop], the final
      result is [Unfinished]. Otherwise, the final result is [Finished]. *)
  val iteri_until
    :  ('k, 'v, _) t
    -> f:((key:'k -> data:'v -> Continue_or_stop.t)[@local])
    -> Finished_or_unfinished.t

  (** Iterates two maps side by side. The complexity of this function is O(M + N).  If two
      inputs are [[(0, a); (1, a)]] and [[(1, b); (2, b)]], [f] will be called with [[(0,
      `Left a); (1, `Both (a, b)); (2, `Right b)]]. *)
  val iter2
    :  ('k, 'v1, 'cmp) t
    -> ('k, 'v2, 'cmp) t
    -> f:((key:'k -> data:('v1, 'v2) Merge_element.t -> unit)[@local])
    -> unit

  (** Returns a new map with bound values replaced by [f] applied to the bound values.*)
  val map : ('k, 'v1, 'cmp) t -> f:(('v1 -> 'v2)[@local]) -> ('k, 'v2, 'cmp) t

  (** Like [map], but the passed function takes both [key] and [data] as arguments. *)
  val mapi
    :  ('k, 'v1, 'cmp) t
    -> f:((key:'k -> data:'v1 -> 'v2)[@local])
    -> ('k, 'v2, 'cmp) t

  (** Convert map with keys of type ['k2] to a map with keys of type ['k2] using [f]. *)
  val map_keys
    :  ('k2, 'cmp2) Comparator.Module.t
    -> ('k1, 'v, 'cmp1) t
    -> f:(('k1 -> 'k2)[@local])
    -> [ `Ok of ('k2, 'v, 'cmp2) t | `Duplicate_key of 'k2 ]

  (** Like [map_keys], but raises on duplicate key. *)
  val map_keys_exn
    :  ('k2, 'cmp2) Comparator.Module.t
    -> ('k1, 'v, 'cmp1) t
    -> f:(('k1 -> 'k2)[@local])
    -> ('k2, 'v, 'cmp2) t

  (** Folds over keys and data in the map in increasing order of [key]. *)
  val fold
    :  ('k, 'v, _) t
    -> init:'acc
    -> f:((key:'k -> data:'v -> 'acc -> 'acc)[@local])
    -> 'acc

  (** Folds over keys and data in the map in increasing order of [key], until the first
      time that [f] returns [Stop _]. If [f] returns [Stop final], this function returns
      immediately with the value [final]. If [f] never returns [Stop _], and the final
      call to [f] returns [Continue last], this function returns [finish last]. *)
  val fold_until
    :  ('k, 'v, _) t
    -> init:'acc
    -> f:
         ((key:'k -> data:'v -> 'acc -> ('acc, 'final) Container.Continue_or_stop.t)
          [@local])
    -> finish:(('acc -> 'final)[@local])
    -> 'final

  (** Folds over keys and data in the map in decreasing order of [key]. *)
  val fold_right
    :  ('k, 'v, _) t
    -> init:'acc
    -> f:((key:'k -> data:'v -> 'acc -> 'acc)[@local])
    -> 'acc

  (** Folds over two maps side by side, like [iter2]. *)
  val fold2
    :  ('k, 'v1, 'cmp) t
    -> ('k, 'v2, 'cmp) t
    -> init:'acc
    -> f:((key:'k -> data:('v1, 'v2) Merge_element.t -> 'acc -> 'acc)[@local])
    -> 'acc

  (** [filter], [filteri], [filter_keys], [filter_map], and [filter_mapi] run in O(n)
      time.

      [filter], [filteri], [filter_keys], [partition_tf] and [partitioni_tf] keep a lot
      of sharing between their result and the original map.  Dropping or keeping a run of
      [k] consecutive elements costs [O(log(k))] extra memory. Keeping the entire map
      costs no extra memory at all: [filter ~f:(fun _ -> true)] returns the original map.
  *)
  val filter_keys : ('k, 'v, 'cmp) t -> f:(('k -> bool)[@local]) -> ('k, 'v, 'cmp) t

  val filter : ('k, 'v, 'cmp) t -> f:(('v -> bool)[@local]) -> ('k, 'v, 'cmp) t

  val filteri
    :  ('k, 'v, 'cmp) t
    -> f:((key:'k -> data:'v -> bool)[@local])
    -> ('k, 'v, 'cmp) t

  (** Returns a new map with bound values filtered by [f] applied to the bound values. *)
  val filter_map
    :  ('k, 'v1, 'cmp) t
    -> f:(('v1 -> 'v2 option)[@local])
    -> ('k, 'v2, 'cmp) t

  (** Like [filter_map], but the passed function takes both [key] and [data] as
      arguments. *)
  val filter_mapi
    :  ('k, 'v1, 'cmp) t
    -> f:((key:'k -> data:'v1 -> 'v2 option)[@local])
    -> ('k, 'v2, 'cmp) t

  (** [partition_mapi t ~f] returns two new [t]s, with each key in [t] appearing in
      exactly one of the resulting maps depending on its mapping in [f]. *)
  val partition_mapi
    :  ('k, 'v1, 'cmp) t
    -> f:((key:'k -> data:'v1 -> ('v2, 'v3) Either.t)[@local])
    -> ('k, 'v2, 'cmp) t * ('k, 'v3, 'cmp) t

  (** [partition_map t ~f = partition_mapi t ~f:(fun ~key:_ ~data -> f data)] *)
  val partition_map
    :  ('k, 'v1, 'cmp) t
    -> f:(('v1 -> ('v2, 'v3) Either.t)[@local])
    -> ('k, 'v2, 'cmp) t * ('k, 'v3, 'cmp) t

  (**
     {[
       partitioni_tf t ~f
       =
       partition_mapi t ~f:(fun ~key ~data ->
         if f ~key ~data
         then First data
         else Second data)
     ]} *)
  val partitioni_tf
    :  ('k, 'v, 'cmp) t
    -> f:((key:'k -> data:'v -> bool)[@local])
    -> ('k, 'v, 'cmp) t * ('k, 'v, 'cmp) t

  (** [partition_tf t ~f = partitioni_tf t ~f:(fun ~key:_ ~data -> f data)] *)
  val partition_tf
    :  ('k, 'v, 'cmp) t
    -> f:(('v -> bool)[@local])
    -> ('k, 'v, 'cmp) t * ('k, 'v, 'cmp) t

  (** Produces [Ok] of a map including all keys if all data is [Ok], or an [Error]
      including all errors otherwise. *)
  val combine_errors : ('k, 'v Or_error.t, 'cmp) t -> ('k, 'v, 'cmp) t Or_error.t

  (** Returns a total ordering between maps. The first argument is a total ordering used
      to compare data associated with equal keys in the two maps. *)
  val compare_direct : ('v -> 'v -> int) -> ('k, 'v, 'cmp) t -> ('k, 'v, 'cmp) t -> int

  (** Hash function: a building block to use when hashing data structures containing maps in
      them. [hash_fold_direct hash_fold_key] is compatible with [compare_direct] iff
      [hash_fold_key] is compatible with [(comparator m).compare] of the map [m] being
      hashed. *)
  val hash_fold_direct : 'k Hash.folder -> 'v Hash.folder -> ('k, 'v, 'cmp) t Hash.folder

  (** [equal cmp m1 m2] tests whether the maps [m1] and [m2] are equal, that is, contain
      the same keys and associate each key with the same value.  [cmp] is the equality
      predicate used to compare the values associated with the keys. *)
  val equal : ('v -> 'v -> bool) -> ('k, 'v, 'cmp) t -> ('k, 'v, 'cmp) t -> bool

  (** Returns a list of the keys in the given map. *)
  val keys : ('k, _, _) t -> 'k list

  (** Returns a list of the data in the given map. *)
  val data : (_, 'v, _) t -> 'v list

  (** Creates an association list from the given map. *)
  val to_alist
    :  ?key_order:[ `Increasing | `Decreasing ] (** default is [`Increasing] *)
    -> ('k, 'v, _) t
    -> ('k * 'v) list

  (** {2 Additional operations on maps} *)

  (** Merges two maps. The runtime is O(length(t1) + length(t2)). You shouldn't use this
      function to merge a list of maps; consider using [merge_skewed] instead. *)
  val merge
    :  ('k, 'v1, 'cmp) t
    -> ('k, 'v2, 'cmp) t
    -> f:((key:'k -> ('v1, 'v2) Merge_element.t -> 'v3 option)[@local])
    -> ('k, 'v3, 'cmp) t

  (** A special case of [merge], [merge_skewed t1 t2] is a map containing all the
      bindings of [t1] and [t2]. Bindings that appear in both [t1] and [t2] are
      combined into a single value using the [combine] function. In a call
      [combine ~key v1 v2], the value [v1] comes from [t1] and [v2] from [t2].

      The runtime of [merge_skewed] is [O(min(l1, l2) * log(max(l1, l2)))], where [l1] is
      the length of [t1] and [l2] the length of [t2]. This is likely to be faster than
      [merge] when one of the maps is a lot smaller, or when you merge a list of maps. *)
  val merge_skewed
    :  ('k, 'v, 'cmp) t
    -> ('k, 'v, 'cmp) t
    -> combine:((key:'k -> 'v -> 'v -> 'v)[@local])
    -> ('k, 'v, 'cmp) t

  module Symmetric_diff_element : sig
    type ('k, 'v) t = 'k * [ `Left of 'v | `Right of 'v | `Unequal of 'v * 'v ]
    [@@deriving_inline compare, equal, sexp, sexp_grammar]

    include Ppx_compare_lib.Comparable.S2 with type ('k, 'v) t := ('k, 'v) t
    include Ppx_compare_lib.Equal.S2 with type ('k, 'v) t := ('k, 'v) t
    include Sexplib0.Sexpable.S2 with type ('k, 'v) t := ('k, 'v) t

    val t_sexp_grammar
      :  'k Sexplib0.Sexp_grammar.t
      -> 'v Sexplib0.Sexp_grammar.t
      -> ('k, 'v) t Sexplib0.Sexp_grammar.t

    [@@@end]
  end

  (** [symmetric_diff t1 t2 ~data_equal] returns a list of changes between [t1] and [t2].
      It is intended to be efficient in the case where [t1] and [t2] share a large amount
      of structure. The keys in the output sequence will be in sorted order.

      It is assumed that [data_equal] is at least as equating as physical equality: that
      [phys_equal x y] implies [data_equal x y]. Otherwise, [symmetric_diff] may behave in
      unexpected ways. For example, with [~data_equal:(fun _ _ -> false)] it is NOT
      necessarily the case the resulting change sequence will contain an element
      [(k, `Unequal _)] for every key [k] shared by both maps.

      Warning: Float equality violates this property! [phys_equal Float.nan Float.nan] is
      true, but [Float.(=) Float.nan Float.nan] is false. *)
  val symmetric_diff
    :  ('k, 'v, 'cmp) t
    -> ('k, 'v, 'cmp) t
    -> data_equal:('v -> 'v -> bool)
    -> ('k, 'v) Symmetric_diff_element.t Sequence.t

  (** [fold_symmetric_diff t1 t2 ~data_equal] folds across an implicit sequence of changes
      between [t1] and [t2], in sorted order by keys. Equivalent to
      [Sequence.fold (symmetric_diff t1 t2 ~data_equal)], and more efficient. *)
  val fold_symmetric_diff
    :  ('k, 'v, 'cmp) t
    -> ('k, 'v, 'cmp) t
    -> data_equal:(('v -> 'v -> bool)[@local])
    -> init:'acc
    -> f:(('acc -> ('k, 'v) Symmetric_diff_element.t -> 'acc)[@local])
    -> 'acc

  (** [min_elt map] returns [Some (key, data)] pair corresponding to the minimum key in
      [map], or [None] if empty. *)
  val min_elt : ('k, 'v, _) t -> ('k * 'v) option

  val min_elt_exn : ('k, 'v, _) t -> 'k * 'v

  (** [max_elt map] returns [Some (key, data)] pair corresponding to the maximum key in
      [map], or [None] if [map] is empty. *)
  val max_elt : ('k, 'v, _) t -> ('k * 'v) option

  val max_elt_exn : ('k, 'v, _) t -> 'k * 'v

  (** Swap the inner and outer keys of nested maps. If [transpose_keys m a = b], then
      [find_exn (find_exn a i) j = find_exn (find_exn b j) i]. *)
  val transpose_keys
    :  ('k2, 'cmp2) Comparator.Module.t
    -> ('k1, ('k2, 'v, 'cmp2) t, 'cmp1) t
    -> ('k2, ('k1, 'v, 'cmp1) t, 'cmp2) t

  (** These functions have the same semantics as similar functions in [List]. *)

  val for_all : ('k, 'v, _) t -> f:(('v -> bool)[@local]) -> bool
  val for_alli : ('k, 'v, _) t -> f:((key:'k -> data:'v -> bool)[@local]) -> bool
  val exists : ('k, 'v, _) t -> f:(('v -> bool)[@local]) -> bool
  val existsi : ('k, 'v, _) t -> f:((key:'k -> data:'v -> bool)[@local]) -> bool
  val count : ('k, 'v, _) t -> f:(('v -> bool)[@local]) -> int
  val counti : ('k, 'v, _) t -> f:((key:'k -> data:'v -> bool)[@local]) -> int


  (** [split t key] returns a map of keys strictly less than [key], the mapping of [key] if
      any, and a map of keys strictly greater than [key].

      Runtime is O(m + log n), where n is the size of the input map and m is the size of
      the smaller of the two output maps.  The O(m) term is due to the need to calculate
      the length of the output maps. *)
  val split
    :  ('k, 'v, 'cmp) t
    -> 'k
    -> ('k, 'v, 'cmp) t * ('k * 'v) option * ('k, 'v, 'cmp) t

  (** [split_le_gt t key] returns a map of keys that are less or equal to [key] and a
      map of keys strictly greater than [key].

      Runtime is O(m + log n), where n is the size of the input map and m is the size of
      the smaller of the two output maps.  The O(m) term is due to the need to calculate
      the length of the output maps. *)
  val split_le_gt : ('k, 'v, 'cmp) t -> 'k -> ('k, 'v, 'cmp) t * ('k, 'v, 'cmp) t

  (** [split_lt_ge t key] returns a map of keys strictly less than [key] and a map of
      keys that are greater or equal to [key].

      Runtime is O(m + log n), where n is the size of the input map and m is the size of
      the smaller of the two output maps.  The O(m) term is due to the need to calculate
      the length of the output maps. *)
  val split_lt_ge : ('k, 'v, 'cmp) t -> 'k -> ('k, 'v, 'cmp) t * ('k, 'v, 'cmp) t

  (** [append ~lower_part ~upper_part] returns [`Ok map] where [map] contains all the
      [(key, value)] pairs from the two input maps if all the keys from [lower_part] are
      less than all the keys from [upper_part].  Otherwise it returns
      [`Overlapping_key_ranges].

      Runtime is O(log n) where n is the size of the larger input map.  This can be
      significantly faster than [Map.merge] or repeated [Map.add].

      {[
        assert (match Map.append ~lower_part ~upper_part with
          | `Ok whole_map ->
            Map.to_alist whole_map
            = List.append (to_alist lower_part) (to_alist upper_part)
          | `Overlapping_key_ranges -> true);
      ]} *)
  val append
    :  lower_part:('k, 'v, 'cmp) t
    -> upper_part:('k, 'v, 'cmp) t
    -> [ `Ok of ('k, 'v, 'cmp) t | `Overlapping_key_ranges ]

  (** [subrange t ~lower_bound ~upper_bound] returns a map containing all the entries from
      [t] whose keys lie inside the interval indicated by [~lower_bound] and
      [~upper_bound].  If this interval is empty, an empty map is returned.

      Runtime is O(m + log n), where n is the size of the input map and m is the size of
      the output map.  The O(m) term is due to the need to calculate the length of the
      output map. *)
  val subrange
    :  ('k, 'v, 'cmp) t
    -> lower_bound:'k Maybe_bound.t
    -> upper_bound:'k Maybe_bound.t
    -> ('k, 'v, 'cmp) t

  (** [fold_range_inclusive t ~min ~max ~init ~f] folds [f] (with initial value [~init])
      over all keys (and their associated values) that are in the range [[min, max]]
      (inclusive).  *)
  val fold_range_inclusive
    :  ('k, 'v, 'cmp) t
    -> min:'k
    -> max:'k
    -> init:'acc
    -> f:((key:'k -> data:'v -> 'acc -> 'acc)[@local])
    -> 'acc

  (** [range_to_alist t ~min ~max] returns an associative list of the elements whose keys
      lie in [[min, max]] (inclusive), with the smallest key being at the head of the
      list. *)
  val range_to_alist : ('k, 'v, 'cmp) t -> min:'k -> max:'k -> ('k * 'v) list

  (** [closest_key t dir k] returns the [(key, value)] pair in [t] with [key] closest to
      [k] that satisfies the given inequality bound.

      For example, [closest_key t `Less_than k] would be the pair with the closest key to
      [k] where [key < k].

      [to_sequence] can be used to get the same results as [closest_key].  It is less
      efficient for individual lookups but more efficient for finding many elements starting
      at some value. *)
  val closest_key
    :  ('k, 'v, 'cmp) t
    -> [ `Greater_or_equal_to | `Greater_than | `Less_or_equal_to | `Less_than ]
    -> 'k
    -> ('k * 'v) option

  (** [nth t n] finds the (key, value) pair of rank n (i.e., such that there are exactly n
      keys strictly less than the found key), if one exists.  O(log(length t) + n) time. *)
  val nth : ('k, 'v, _) t -> int -> ('k * 'v) option

  val nth_exn : ('k, 'v, _) t -> int -> 'k * 'v

  (** [rank t k] If [k] is in [t], returns the number of keys strictly less than [k] in
      [t], and [None] otherwise. *)
  val rank : ('k, 'v, 'cmp) t -> 'k -> int option



  (** [to_sequence ?order ?keys_greater_or_equal_to ?keys_less_or_equal_to t]
      gives a sequence of key-value pairs between [keys_less_or_equal_to] and
      [keys_greater_or_equal_to] inclusive, presented in [order].  If
      [keys_greater_or_equal_to > keys_less_or_equal_to], the sequence is
      empty.

      When neither [keys_greater_or_equal_to] nor [keys_less_or_equal_to] are
      provided, the cost is O(log n) up front and amortized O(1) to produce
      each element. If either is provided (and is used by the order parameter
      provided), then the the cost is O(n) up front, and amortized O(1) to
      produce each element. *)
  val to_sequence
    :  ?order:[ `Increasing_key (** default *) | `Decreasing_key ]
    -> ?keys_greater_or_equal_to:'k
    -> ?keys_less_or_equal_to:'k
    -> ('k, 'v, 'cmp) t
    -> ('k * 'v) Sequence.t

  (** [binary_search t ~compare which elt] returns the [(key, value)] pair in [t]
      specified by [compare] and [which], if one exists.

      [t] must be sorted in increasing order according to [compare], where [compare] and
      [elt] divide [t] into three (possibly empty) segments:

      {v
        |  < elt  |  = elt  |  > elt  |
      v}

      [binary_search] returns an element on the boundary of segments as specified by
      [which].  See the diagram below next to the [which] variants.

      [binary_search] does not check that [compare] orders [t], and behavior is
      unspecified if [compare] doesn't order [t].  Behavior is also unspecified if
      [compare] mutates [t]. *)
  val binary_search
    :  ('k, 'v, 'cmp) t
    -> compare:((key:'k -> data:'v -> 'key -> int)[@local])
    -> [ `Last_strictly_less_than (**        {v | < elt X |                       v} *)
       | `Last_less_than_or_equal_to (**     {v |      <= elt       X |           v} *)
       | `Last_equal_to (**                  {v           |   = elt X |           v} *)
       | `First_equal_to (**                 {v           | X = elt   |           v} *)
       | `First_greater_than_or_equal_to (** {v           | X       >= elt      | v} *)
       | `First_strictly_greater_than (**    {v                       | X > elt | v} *)
       ]
    -> 'key
    -> ('k * 'v) option

  (** [binary_search_segmented t ~segment_of which] takes a [segment_of] function that
      divides [t] into two (possibly empty) segments:

      {v
        | segment_of elt = `Left | segment_of elt = `Right |
      v}

      [binary_search_segmented] returns the [(key, value)] pair on the boundary of the
      segments as specified by [which]: [`Last_on_left] yields the last element of the
      left segment, while [`First_on_right] yields the first element of the right segment.
      It returns [None] if the segment is empty.

      [binary_search_segmented] does not check that [segment_of] segments [t] as in the
      diagram, and behavior is unspecified if [segment_of] doesn't segment [t].  Behavior
      is also unspecified if [segment_of] mutates [t]. *)
  val binary_search_segmented
    :  ('k, 'v, 'cmp) t
    -> segment_of:((key:'k -> data:'v -> [ `Left | `Right ])[@local])
    -> [ `Last_on_left | `First_on_right ]
    -> ('k * 'v) option

  (** [binary_search_subrange] takes a [compare] function that divides [t] into three
      (possibly empty) segments with respect to [lower_bound] and [upper_bound]:

      {v
        | Below_lower_bound | In_range | Above_upper_bound |
      v}

      and returns a map of the [In_range] segment.

      Runtime is O(log m + n) where [m] is the length of the input map and [n] is the
      length of the output. The linear term in [n] is to compute the length of the output.

      Behavior is undefined if [compare] does not segment [t] as shown above, or if
      [compare] mutates its inputs. *)
  val binary_search_subrange
    :  ('k, 'v, 'cmp) t
    -> compare:((key:'k -> data:'v -> 'bound -> int)[@local])
    -> lower_bound:'bound Maybe_bound.t
    -> upper_bound:'bound Maybe_bound.t
    -> ('k, 'v, 'cmp) t

  (** Creates traversals to reconstruct a map within an applicative. Uses
      [Lazy_applicative] so that the map can be traversed within the applicative, rather
      than needing to be traversed all at once, outside the applicative. *)
  module Make_applicative_traversals (A : Applicative.Lazy_applicative) : sig
    val mapi
      :  ('k, 'v1, 'cmp) t
      -> f:(key:'k -> data:'v1 -> 'v2 A.t)
      -> ('k, 'v2, 'cmp) t A.t

    val filter_mapi
      :  ('k, 'v1, 'cmp) t
      -> f:(key:'k -> data:'v1 -> 'v2 option A.t)
      -> ('k, 'v2, 'cmp) t A.t
  end

  (** [M] is meant to be used in combination with OCaml applicative functor types:

      {[
        type string_to_int_map = int Map.M(String).t
      ]}

      which stands for:

      {[
        type string_to_int_map = (String.t, int, String.comparator_witness) Map.t
      ]}

      The point is that [int Map.M(String).t] supports deriving, whereas the second syntax
      doesn't (because there is no such thing as, say, [String.sexp_of_comparator_witness]
      -- instead you would want to pass the comparator directly).

      In addition, when using [@@deriving], the requirements on the key module are only
      those needed to satisfy what you are trying to derive on the map itself. Say you
      write:

      {[
        type t = int Map.M(X).t [@@deriving hash]
      ]}

      then this will be well typed exactly if [X] contains at least:
      - a type [t] with no parameters
      - a comparator witness
      - a [hash_fold_t] function with the right type *)
  module M (K : sig
      type t
      type comparator_witness
    end) : sig
    type nonrec 'v t = (K.t, 'v, K.comparator_witness) t
  end

  include For_deriving with type ('key, 'value, 'cmp) t := ('key, 'value, 'cmp) t

  (** [Using_comparator] is a similar interface as the toplevel of [Map], except the
      functions take a [~comparator:('k, 'cmp) Comparator.t], whereas the functions at the
      toplevel of [Map] take a [('k, 'cmp) comparator]. *)
  module Using_comparator : sig
    type nonrec ('k, +'v, 'cmp) t = ('k, 'v, 'cmp) t [@@deriving_inline sexp_of]

    val sexp_of_t
      :  ('k -> Sexplib0.Sexp.t)
      -> ('v -> Sexplib0.Sexp.t)
      -> ('cmp -> Sexplib0.Sexp.t)
      -> ('k, 'v, 'cmp) t
      -> Sexplib0.Sexp.t

    [@@@end]

    val t_of_sexp_direct
      :  comparator:('k, 'cmp) Comparator.t
      -> (Sexp.t -> 'k)
      -> (Sexp.t -> 'v)
      -> Sexp.t
      -> ('k, 'v, 'cmp) t

    module Tree : sig
      type (+'k, +'v, 'cmp) t [@@deriving_inline sexp_of]

      val sexp_of_t
        :  ('k -> Sexplib0.Sexp.t)
        -> ('v -> Sexplib0.Sexp.t)
        -> ('cmp -> Sexplib0.Sexp.t)
        -> ('k, 'v, 'cmp) t
        -> Sexplib0.Sexp.t

      [@@@end]

      val t_of_sexp_direct
        :  comparator:('k, 'cmp) Comparator.t
        -> (Sexp.t -> 'k)
        -> (Sexp.t -> 'v)
        -> Sexp.t
        -> ('k, 'v, 'cmp) t

      include
        Creators_and_accessors_generic
        with type ('a, 'b, 'c) t := ('a, 'b, 'c) t
        with type ('a, 'b, 'c) tree := ('a, 'b, 'c) t
        with type 'k key := 'k
        with type 'c cmp := 'c
        with type ('a, 'b, 'c) create_options := ('a, 'b, 'c) With_comparator.t
        with type ('a, 'b, 'c) access_options := ('a, 'b, 'c) With_comparator.t

      val empty_without_value_restriction : (_, _, _) t

      (** [Build_increasing] can be used to construct a map incrementally from a
          sequence that is known to be increasing.

          The total time complexity of constructing a map this way is O(n), which is more
          efficient than using [Map.add] by a logarithmic factor.

          This interface can be thought of as a dual of [to_sequence], but we don't have
          an equally neat idiom for the duals of sequences ([of_sequence] is much less
          general because it does not allow the sequence to be produced asynchronously). *)
      module Build_increasing : sig
        type ('a, 'b, 'c) tree := ('a, 'b, 'c) t
        type ('k, 'v, 'w) t

        val empty : ('k, 'v, 'w) t

        (** Time complexity of [add_exn] is amortized constant-time (if [t] is used
            linearly), with a worst-case O(log(n)) time. *)
        val add_exn
          :  ('k, 'v, 'w) t
          -> comparator:('k, 'w) Comparator.t
          -> key:'k
          -> data:'v
          -> ('k, 'v, 'w) t

        (** Time complexity is O(log(n)). *)
        val to_tree : ('k, 'v, 'w) t -> ('k, 'v, 'w) tree
      end
    end

    include
      Creators_and_accessors_generic
      with type ('a, 'b, 'c) t := ('a, 'b, 'c) t
      with type ('a, 'b, 'c) tree := ('a, 'b, 'c) Tree.t
      with type 'k key := 'k
      with type 'c cmp := 'c
      with type ('a, 'b, 'c) access_options := ('a, 'b, 'c) Without_comparator.t
      with type ('a, 'b, 'c) create_options := ('a, 'b, 'c) With_comparator.t

    val comparator : ('a, _, 'cmp) t -> ('a, 'cmp) Comparator.t

    val hash_fold_direct
      :  'k Hash.folder
      -> 'v Hash.folder
      -> ('k, 'v, 'cmp) t Hash.folder

    (** To get around the value restriction, apply the functor and include it. You
        can see an example of this in the [Poly] submodule below. *)
    module Empty_without_value_restriction (K : Comparator.S1) : sig
      val empty : ('a K.t, 'v, K.comparator_witness) t
    end
  end

  (** A polymorphic Map. *)
  module Poly :
    S_poly
    with type ('key, +'value) t = ('key, 'value, Comparator.Poly.comparator_witness) t
     and type ('key, +'value) tree =
           ('key, 'value, Comparator.Poly.comparator_witness) Using_comparator.Tree.t
     and type comparator_witness = Comparator.Poly.comparator_witness

  (** Create a map from a tree using the given comparator. *)
  val of_tree
    :  ('k, 'cmp) Comparator.Module.t
    -> ('k, 'v, 'cmp) Using_comparator.Tree.t
    -> ('k, 'v, 'cmp) t

  (** Extract a tree from a map. *)
  val to_tree : ('k, 'v, 'cmp) t -> ('k, 'v, 'cmp) Using_comparator.Tree.t


  (** {2 Modules and module types for extending [Map]}

      For use in extensions of Base, like [Core]. *)

  module With_comparator = With_comparator
  module With_first_class_module = With_first_class_module
  module Without_comparator = Without_comparator

  module type For_deriving = For_deriving
  module type S_poly = S_poly
  module type Accessors_generic = Accessors_generic
  module type Creators_and_accessors_generic = Creators_and_accessors_generic
  module type Creators_generic = Creators_generic
end
OCaml

Innovation. Community. Security.