package binsec

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

Source file zmap.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
(**************************************************************************)
(*  This file is part of BINSEC.                                          *)
(*                                                                        *)
(*  Copyright (C) 2016-2026                                               *)
(*    CEA (Commissariat à l'énergie atomique et aux énergies              *)
(*         alternatives)                                                  *)
(*                                                                        *)
(*  you can redistribute it and/or modify it under the terms of the GNU   *)
(*  Lesser General Public License as published by the Free Software       *)
(*  Foundation, version 2.1.                                              *)
(*                                                                        *)
(*  It is distributed in the hope that it will be useful,                 *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *)
(*  GNU Lesser General Public License for more details.                   *)
(*                                                                        *)
(*  See the GNU Lesser General Public License version 2.1                 *)
(*  for more details (enclosed in the file licenses/LGPLv2.1).            *)
(*                                                                        *)
(**************************************************************************)

type ('a, _) node =
  | Empty : ('a, [< `Empty | `Item_or_empty | `Any ]) node
  | Item : {
      lo : Z.t;
      hi : Z.t;
      elt : 'a;
    }
      -> ('a, [< `Non_empty | `Item | `Item_or_empty | `Any ]) node
  | Node : {
      lo : Z.t;
      hi : Z.t;
      mask : Z.t;
      zero : 'a tree;
      one : 'a tree;
    }
      -> ('a, [< `Non_empty | `Node | `Any ]) node

and 'a tree = ('a, [ `Non_empty ]) node
and 'a t = ('a, [ `Any ]) node
and 'a item = ('a, [ `Item ]) node
and 'a item_opt = ('a, [ `Item_or_empty ]) node
and 'a branch = ('a, [ `Node ]) node

type ('a, 'b) continuation =
  | Root : ('a, [< `Root | `Any ]) continuation
  | Right : 'a path * 'a tree -> ('a, [< `Right | `Any ]) continuation

and 'a path = ('a, [ `Any ]) continuation
and 'a right = ('a, [ `Right ]) continuation

(* most significant bit of difference betweeb two key *)
let diff k k' = Z.numbits (Z.logxor k k')
let any : 'a tree -> 'a t = function (Item _ | Node _) as t -> t

let iter : ('a item -> unit) -> 'a t -> unit =
  let rec iter : ('a item -> unit) -> 'a tree -> unit =
   fun f t ->
    match t with
    | Item _ as item -> f item
    | Node { zero; one; _ } ->
        iter f zero;
        iter f one
  in
  fun f t ->
    match t with Empty -> () | (Item _ | Node _) as tree -> iter f tree

let rev_iter : ('a item -> unit) -> 'a t -> unit =
  let rec rev_iter : ('a item -> unit) -> 'a tree -> unit =
   fun f t ->
    match t with
    | Item _ as item -> f item
    | Node { zero; one; _ } ->
        rev_iter f one;
        rev_iter f zero
  in
  fun f t ->
    match t with Empty -> () | (Item _ | Node _) as tree -> rev_iter f tree

let fold : ('a item -> 'b -> 'b) -> 'b -> 'a t -> 'b =
  let rec fold : ('a item -> 'b -> 'b) -> 'b -> 'a tree -> 'b =
   fun f acc t ->
    match t with
    | Item _ as item -> f item acc
    | Node { zero; one; _ } -> fold f (fold f acc zero) one
  in
  fun f acc t ->
    match t with Empty -> acc | (Item _ | Node _) as tree -> fold f acc tree

let rev_fold : ('a item -> 'b -> 'b) -> 'b -> 'a t -> 'b =
  let rec rev_fold : ('a item -> 'b -> 'b) -> 'b -> 'a tree -> 'b =
   fun f acc t ->
    match t with
    | Item _ as item -> f item acc
    | Node { zero; one; _ } -> rev_fold f (rev_fold f acc one) zero
  in
  fun f acc t ->
    match t with
    | Empty -> acc
    | (Item _ | Node _) as tree -> rev_fold f acc tree

let is_empty : 'a t -> bool = function
  | Empty -> true
  | Item _ | Node _ -> false

let lower_bound : 'a tree -> Z.t = function
  | Item { lo; _ } | Node { lo; _ } -> lo

let upper_bound : 'a tree -> Z.t = function
  | Item { hi; _ } | Node { hi; _ } -> hi

let rec is_empty_between_left : Z.t -> Z.t -> 'a tree -> bool =
 fun lo' hi' tree ->
  match tree with
  | Item _ -> false
  | Node { lo; zero; one; _ } ->
      Z.lt lo lo' && is_empty_between_choice lo' hi' zero one

and is_empty_between_right : Z.t -> Z.t -> 'a tree -> bool =
 fun lo' hi' tree ->
  match tree with
  | Item _ -> false
  | Node { hi; zero; one; _ } ->
      Z.lt hi' hi && is_empty_between_choice lo' hi' zero one

and is_empty_between_choice : Z.t -> Z.t -> 'a tree -> 'a tree -> bool =
 fun lo' hi' zero one ->
  let ub = upper_bound zero in
  if Z.leq hi' ub then is_empty_between_left lo' hi' zero
  else
    let lb = lower_bound one in
    if Z.leq lb lo' then is_empty_between_right lo' hi' one
    else Z.lt ub lo' && Z.lt hi' lb

let is_empty_between : Z.t -> Z.t -> 'a t -> bool =
 fun lo' hi' t ->
  match t with
  | Empty -> true
  | Item { lo; hi; _ } -> Z.lt hi' lo || Z.lt hi lo'
  | Node { lo; hi; zero; one; _ } ->
      Z.lt hi' lo || Z.lt hi lo' || is_empty_between_choice lo' hi' zero one

let map : ('a item -> 'b) -> 'a t -> 'b t =
  let rec map : ('a item -> 'b) -> 'a tree -> 'b tree =
   fun f t ->
    match t with
    | Item { lo; hi; _ } as item -> Item { lo; hi; elt = f item }
    | Node { lo; hi; mask; zero; one } ->
        Node { lo; hi; mask; zero = map f zero; one = map f one }
  in
  fun f t ->
    match t with
    | Empty -> Empty
    | (Item _ | Node _) as tree -> any (map f tree)

let bindings : 'a t -> 'a item list =
  let rec bindings : 'a tree -> 'a item list -> 'a item list =
   fun t acc ->
    match t with
    | Item _ as item -> item :: acc
    | Node { zero; one; _ } -> bindings zero (bindings one acc)
  in
  function Empty -> [] | (Item _ | Node _) as tree -> bindings tree []

let mem : Z.t -> 'a t -> bool =
  let rec mem : Z.t -> 'a tree -> bool =
   fun index tree ->
    match tree with
    | Item _ -> true
    | Node { zero; one; _ } -> mem_choice index zero one
  and mem_choice : Z.t -> 'a tree -> 'a tree -> bool =
   fun index zero one ->
    let ub = upper_bound zero in
    if Z.leq index ub then mem index zero
    else
      let lb = lower_bound one in
      Z.leq lb index && mem index one
  in
  fun index t ->
    match t with
    | Empty -> false
    | Item { lo; hi; _ } -> Z.leq lo index && Z.leq index hi
    | Node { lo; hi; zero; one; _ } ->
        Z.leq lo index && Z.leq index hi && mem_choice index zero one

let find_opt : Z.t -> 'a t -> 'a item_opt =
  let rec find : Z.t -> 'a tree -> 'a item_opt =
   fun index tree ->
    match tree with
    | Item _ as item -> item
    | Node { zero; one; _ } -> find_choice index zero one
  and find_choice : Z.t -> 'a tree -> 'a tree -> 'a item_opt =
   fun index zero one ->
    let ub = upper_bound zero in
    if Z.leq index ub then find index zero
    else
      let lb = lower_bound one in
      if Z.leq lb index then find index one else Empty
  in
  fun index t ->
    match t with
    | Empty -> Empty
    | Item { lo; hi; _ } as item ->
        if Z.lt index lo || Z.lt hi index then Empty else item
    | Node { lo; hi; zero; one; _ } ->
        if Z.lt index lo || Z.lt hi index then Empty
        else find_choice index zero one

let none : 'a item_opt = Empty

let find : Z.t -> 'a t -> 'a item =
 fun index t ->
  match find_opt index t with
  | Empty -> raise Not_found
  | Item _ as item -> item

let choose : 'a t -> 'a item =
  let rec choose : 'a tree -> 'a item = function
    | Item _ as item -> item
    | Node { zero; _ } -> choose zero
  in
  function Empty -> raise Not_found | (Item _ | Node _) as tree -> choose tree

let empty : 'a t = Empty

let singleton : lo:Z.t -> hi:Z.t -> 'a -> 'a t =
 fun ~lo ~hi elt ->
  if Z.lt hi lo then raise (Invalid_argument "singleton");
  Item { lo; hi; elt }

let rec join :
    stich:('a item -> 'a item -> 'a option) option ->
    'a tree ->
    'a tree ->
    'a tree =
  let join_make_nodes :
      stich:('a item -> 'a item -> 'a option) option ->
      lo:Z.t ->
      hi:Z.t ->
      mask:Z.t ->
      zero:'a tree ->
      one:'a tree ->
      'a tree =
    let rec union_eq_make_nodes_rebuilt_left :
        'a branch list -> 'a tree -> Z.t -> 'a tree =
     fun path0 rightmost1 hi1 ->
      match path0 with
      | [] -> rightmost1
      | Node { lo = lo0; mask = mask0; zero = zero0; _ } :: path0 ->
          union_eq_make_nodes_rebuilt_left path0
            (Node
               {
                 lo = lo0;
                 hi = hi1;
                 mask = mask0;
                 zero = zero0;
                 one = rightmost1;
               })
            hi1
    in
    let rec join_make_nodes_rebuilt_right :
        'a tree -> Z.t -> 'a branch list -> 'a tree =
     fun leftmost0 lo0 path1 ->
      match path1 with
      | [] -> leftmost0
      | Node { hi = hi1; mask = mask1; one = one1; _ } :: path1 ->
          join_make_nodes_rebuilt_right
            (Node
               {
                 lo = lo0;
                 hi = hi1;
                 mask = mask1;
                 zero = leftmost0;
                 one = one1;
               })
            lo0 path1
    in
    let rec join_make_nodes_leftmost :
        stich:('a item -> 'a item -> 'a option) ->
        lo:Z.t ->
        hi:Z.t ->
        mask:Z.t ->
        zero:'a tree ->
        one:'a tree ->
        'a item ->
        'a branch list ->
        'a tree ->
        'a branch list ->
        'a tree =
     fun ~stich ~lo ~hi ~mask ~zero ~one (Item { lo = lo0; _ } as rightmost0)
         path0 leftmost1 path1 ->
      match leftmost1 with
      | Item { hi = hi1; _ } as leftmost1 -> (
          match stich rightmost0 leftmost1 with
          | None -> Node { lo; hi; mask; zero; one }
          | Some elt -> (
              let zero =
                union_eq_make_nodes_rebuilt_left path0
                  (Item { lo = lo0; hi = hi1; elt })
                  hi1
              in
              match path1 with
              | [] -> zero
              | Node { one = one1; _ } :: path1 ->
                  Node
                    {
                      lo;
                      hi;
                      mask;
                      zero;
                      one =
                        join_make_nodes_rebuilt_right one1 (lower_bound one1)
                          path1;
                    }))
      | Node { zero = zero0; _ } as node1 ->
          join_make_nodes_leftmost ~stich ~lo ~hi ~mask ~zero ~one rightmost0
            path0 zero0 (node1 :: path1)
    in
    let rec join_make_nodes_rightmost :
        stich:('a item -> 'a item -> 'a option) ->
        lo:Z.t ->
        hi:Z.t ->
        mask:Z.t ->
        zero:'a tree ->
        one:'a tree ->
        'a tree ->
        'a branch list ->
        'a tree =
     fun ~stich ~lo ~hi ~mask ~zero ~one rightmost0 path0 ->
      match rightmost0 with
      | Item _ as item0 ->
          join_make_nodes_leftmost ~stich ~lo ~hi ~mask ~zero ~one item0 path0
            one []
      | Node { one = one1; _ } as node0 ->
          join_make_nodes_rightmost ~stich ~lo ~hi ~mask ~zero ~one one1
            (node0 :: path0)
    in
    fun ~stich ~lo ~hi ~mask ~zero ~one ->
      match stich with
      | Some stich
        when Z.equal Z.one (Z.sub (lower_bound one) (upper_bound zero)) ->
          join_make_nodes_rightmost ~stich ~lo ~hi ~mask ~zero ~one zero []
      | Some _ | None -> Node { lo; hi; mask; zero; one }
  in
  let join_disjoint_items :
      stich:('a item -> 'a item -> 'a option) option ->
      'a item ->
      'a item ->
      'a tree =
   fun ~stich (Item { lo = lo0; _ } as item0)
       (Item { lo = lo1; hi = hi1; _ } as item1) ->
    join_make_nodes ~stich ~lo:lo0 ~hi:hi1
      ~mask:(Z.logand lo1 (Z.shift_left Z.minus_one (diff lo0 lo1 - 1)))
      ~zero:item0 ~one:item1
  and join_disjoint_item_node :
      stich:('a item -> 'a item -> 'a option) option ->
      'a item ->
      'a branch ->
      'a tree =
   fun ~stich (Item { lo = lo0; _ } as item0)
       (Node { hi = hi1; mask = mask1; zero = zero1; one = one1; _ } as node1) ->
    let n = diff lo0 mask1 in
    let z' = Z.trailing_zeros mask1 in
    if z' + 1 >= n then
      join_make_nodes ~stich ~lo:lo0 ~hi:hi1 ~mask:mask1
        ~zero:(join ~stich item0 zero1) ~one:one1
    else
      join_make_nodes ~stich ~lo:lo0 ~hi:hi1
        ~mask:(Z.logand mask1 (Z.shift_left Z.minus_one (n - 1)))
        ~zero:item0 ~one:node1
  and join_disjoint_node_item :
      stich:('a item -> 'a item -> 'a option) option ->
      'a branch ->
      'a item ->
      'a tree =
   fun ~stich
       (Node { lo = lo0; mask = mask0; zero = zero0; one = one0; _ } as node0)
       (Item { lo = lo1; hi = hi1; _ } as item1) ->
    let n = diff mask0 lo1 and z = Z.trailing_zeros mask0 in
    if z >= n then
      join_make_nodes ~stich ~lo:lo0 ~hi:hi1 ~mask:mask0 ~zero:zero0
        ~one:(join ~stich one0 item1)
    else
      join_make_nodes ~stich ~lo:lo0 ~hi:hi1
        ~mask:(Z.logand lo1 (Z.shift_left Z.minus_one (n - 1)))
        ~zero:node0 ~one:item1
  and join_disjoint_nodes :
      stich:('a item -> 'a item -> 'a option) option ->
      'a branch ->
      'a branch ->
      'a tree =
   fun ~stich
       (Node { lo = lo0; mask = mask0; zero = zero0; one = one0; _ } as node0)
       (Node { hi = hi1; mask = mask1; zero = zero1; one = one1; _ } as node1) ->
    let n = diff mask0 mask1 and z = Z.trailing_zeros mask0 in
    if z >= n then
      join_make_nodes ~stich ~lo:lo0 ~hi:hi1 ~mask:mask0 ~zero:zero0
        ~one:(join ~stich one0 node1)
    else
      let z' = Z.trailing_zeros mask1 in
      if z' + 1 >= n then
        join_make_nodes ~stich ~lo:lo0 ~hi:hi1 ~mask:mask1
          ~zero:(join ~stich node0 zero1) ~one:one1
      else
        join_make_nodes ~stich ~lo:lo0 ~hi:hi1
          ~mask:(Z.logand mask1 (Z.shift_left Z.minus_one (n - 1)))
          ~zero:node0 ~one:node1
  in
  fun ~stich tree0 tree1 ->
    match (tree0, tree1) with
    | (Item _ as item0), (Item _ as item1) ->
        join_disjoint_items ~stich item0 item1
    | (Item _ as item0), (Node _ as node1) ->
        join_disjoint_item_node ~stich item0 node1
    | (Node _ as node0), (Item _ as item1) ->
        join_disjoint_node_item ~stich node0 item1
    | (Node _ as node0), (Node _ as node1) ->
        join_disjoint_nodes ~stich node0 node1

let join_left :
    stich:('a item -> 'a item -> 'a option) option -> 'a tree -> 'a t -> 'a tree
    =
 fun ~stich tree0 t1 ->
  match t1 with
  | Empty -> tree0
  | (Item _ | Node _) as tree1 -> join ~stich tree0 tree1

let join_right :
    stich:('a item -> 'a item -> 'a option) option -> 'a t -> 'a tree -> 'a tree
    =
 fun ~stich t0 tree1 ->
  match t0 with
  | Empty -> tree1
  | (Item _ | Node _) as tree0 -> join ~stich tree0 tree1

(* (* type mode =
      | Left_right  (** Left and right *)
      | Left_acc  (** Left and accumulator *)
      | Acc_right  (** Accumulator and right *) *)

   type 'a overlap =
     | Ll_Rl_Ru_Lu : [ `Four_endpoints ] overlap
         (** Left starts before and ends after right.
             \[     left      \]
                \[  right  \]
         *)
     | Rl_Ll_Lu_Ru : [ `Four_endpoints ] overlap
         (** Right starts before and ends after left.
                \[  left    \]
             \[     right     \]
         *)
     | Ll_Rl_Lu_Ru : [ `Four_endpoints ] overlap
         (** Right starts and ends after left.
             \[  left  \]
                \[  right  \]
         *)
     | Rl_Ll_Ru_Lu : [ `Four_endpoints ] overlap
         (** Left starts and ends after right.
                 \[  left  \]
             \[  right  \]
         *)
     | LRl_Ru_Lu : [ `Three_endpoints ] overlap
         (** Left ends after right.
             \[  left        \]
             \[  right    \]
         *)
     | LRl_Lu_Ru : [ `Three_endpoints ] overlap
         (** Right ends after left.
             \[  left     \]
             \[  right      \]
         *)
     | Ll_Rl_LRu : [ `Three_endpoints ] overlap
         (** Left starts before right.
             \[  left        \]
                \[  right    \]
         *)
     | Rl_Ll_LRu : [ `Three_endpoints ] overlap
         (** Right starts before left.
                  \[  left  \]
             \[  right      \]
         *)
     | LRl_LRu : [ `Two_endpoints ] overlap
         (** Left and right are equal.
             \[  left  \]
             \[  right \]
         *)

   type ('a, 'b) update =
     | Left_overlay
         : ('a, [< `Two_endpoints | `Three_endpoints | `Four_endpoints ]) update
     | Right_overlay
         : ('a, [< `Two_endpoints | `Three_endpoints | `Four_endpoints ]) update
     | Merge_overlay :
         'a
         -> ('a, [< `Two_endpoints | `Three_endpoints | `Four_endpoints ]) update
         (** Insert a single item ranging from first to last endpoints *)
     | Split_FSL : 'a * 'a -> ('a, [< `Three_endpoints | `Four_endpoints ]) update
         (** Insert one item between first and second endpoints, then one item between second and last endpoints. *)
     | Split_FPL : 'a * 'a -> ('a, [< `Three_endpoints | `Four_endpoints ]) update
         (** Insert one item between first and penultimate endpoints, then one item between penultimate and last endpoints. *)
     | Split_FSTL : 'a * 'a * 'a -> ('a, [< `Four_endpoints ]) update
         (** Insert three items between the four endpoints.  *)

   type 'a update_handler = {
     f : 'k. 'k overlap -> 'a item -> 'a item -> ('a, 'k) update;
   }
   [@@unboxed]

   let union_update : 'a update_handler -> 'a t -> 'a t -> 'a t =
     let union_update_overlap_items_ordered :
         'a update_handler -> 'a item -> 'a item -> 'a tree =
      fun { f = update } (Item { lo = lo0; hi = hi0; elt = elt0 } as item0)
          (Item { lo = lo1; hi = hi1; elt = elt1 } as item1) ->
       if Z.equal lo0 lo1 then
         if Z.equal hi0 hi1 then
           match update LRl_LRu item0 item1 with
           | Left_overlay -> item0
           | Right_overlay -> item1
           | Merge_overlay elt -> Item { lo = lo0; hi = hi0; elt }
         else if Z.lt hi0 hi1 then
           match update LRl_Lu_Ru item0 item1 with
           | Left_overlay -> Item { lo = lo0; hi = hi1; elt = elt0 }
           | Right_overlay -> item1
           | Merge_overlay elt -> Item { lo = lo0; hi = hi1; elt }
           | Split_FSL (eltf, elts) | Split_FPL (eltf, elts) ->
               join
                 (Item { lo = lo0; hi = Z.pred hi0; elt = eltf })
                 (Item { lo = hi0; hi = hi1; elt = elts })
         else
           match update LRl_Ru_Lu item0 item1 with
           | Left_overlay -> item0
           | Right_overlay -> Item { lo = lo0; hi = hi0; elt = elt1 }
           | Merge_overlay elt -> Item { lo = lo0; hi = hi0; elt }
           | Split_FSL (eltf, elts) | Split_FPL (eltf, elts) ->
               join
                 (Item { lo = lo0; hi = Z.pred hi1; elt = eltf })
                 (Item { lo = hi1; hi = hi0; elt = elts })
       else if Z.equal hi0 hi1 then
         match update Ll_Rl_LRu item0 item1 with
         | Left_overlay -> item0
         | Right_overlay -> Item { lo = lo0; hi = hi0; elt = elt1 }
         | Merge_overlay elt -> Item { lo = lo0; hi = hi0; elt }
         | Split_FSL (eltf, elts) | Split_FPL (eltf, elts) ->
             join
               (Item { lo = lo0; hi = Z.pred lo1; elt = eltf })
               (Item { lo = lo1; hi = hi1; elt = elts })
       else if Z.lt hi0 hi1 then
         match update Ll_Rl_Lu_Ru item0 item1 with
         | Left_overlay -> Item { lo = lo0; hi = hi1; elt = elt0 }
         | Right_overlay -> Item { lo = lo0; hi = hi1; elt = elt1 }
         | Merge_overlay elt -> Item { lo = lo0; hi = hi1; elt }
         | Split_FSL (eltf, elts) ->
             join
               (Item { lo = lo0; hi = Z.pred lo1; elt = eltf })
               (Item { lo = lo1; hi = hi1; elt = elts })
         | Split_FPL (eltf, elts) ->
             join
               (Item { lo = lo0; hi = Z.pred hi0; elt = eltf })
               (Item { lo = hi0; hi = hi1; elt = elts })
         | Split_FSTL (eltf, eltm, eltl) ->
             join
               (join
                  (Item { lo = lo0; hi = Z.pred lo1; elt = eltf })
                  (Item { lo = lo1; hi = Z.pred hi0; elt = eltm }))
               (Item { lo = hi0; hi = hi1; elt = eltl })
       else
         match update Ll_Rl_Ru_Lu item0 item1 with
         | Left_overlay -> item0
         | Right_overlay -> Item { lo = lo0; hi = hi0; elt = elt1 }
         | Merge_overlay elt -> Item { lo = lo0; hi = hi0; elt }
         | Split_FSL (eltf, elts) ->
             join
               (Item { lo = lo0; hi = Z.pred lo1; elt = eltf })
               (Item { lo = lo1; hi = hi0; elt = elts })
         | Split_FPL (eltf, elts) ->
             join
               (Item { lo = lo0; hi = Z.pred hi1; elt = eltf })
               (Item { lo = hi1; hi = hi0; elt = elts })
         | Split_FSTL (eltf, eltm, eltl) ->
             join
               (join
                  (Item { lo = lo0; hi = Z.pred lo1; elt = eltf })
                  (Item { lo = lo1; hi = Z.pred hi0; elt = eltm }))
               (Item { lo = hi0; hi = hi1; elt = eltl })
     in
     let union_update_overlap_items_reverted :
         'a update_handler -> 'a item -> 'a item -> 'a tree =
      fun { f = update } (Item { lo = lo0; hi = hi0; elt = elt0 } as item0)
          (Item { lo = lo1; hi = hi1; elt = elt1 } as item1) ->
       if Z.equal lo0 lo1 then
         if Z.equal hi0 hi1 then
           match update LRl_LRu item0 item1 with
           | Left_overlay -> item0
           | Right_overlay -> item1
           | Merge_overlay elt -> Item { lo = lo0; hi = hi0; elt }
         else if Z.lt hi0 hi1 then
           match update LRl_Lu_Ru item0 item1 with
           | Left_overlay -> Item { lo = lo0; hi = hi1; elt = elt0 }
           | Right_overlay -> item1
           | Merge_overlay elt -> Item { lo = lo0; hi = hi1; elt }
           | Split_FSL (eltf, elts) | Split_FPL (eltf, elts) ->
               join
                 (Item { lo = lo0; hi = Z.pred hi0; elt = eltf })
                 (Item { lo = hi0; hi = hi1; elt = elts })
         else
           match update LRl_Ru_Lu item0 item1 with
           | Left_overlay -> item0
           | Right_overlay -> Item { lo = lo0; hi = hi0; elt = elt1 }
           | Merge_overlay elt -> Item { lo = lo0; hi = hi0; elt }
           | Split_FSL (eltf, elts) | Split_FPL (eltf, elts) ->
               join
                 (Item { lo = lo0; hi = Z.pred hi1; elt = eltf })
                 (Item { lo = hi1; hi = hi0; elt = elts })
       else if Z.equal hi0 hi1 then
         match update Rl_Ll_LRu item0 item1 with
         | Left_overlay -> Item { lo = lo1; hi = hi1; elt = elt0 }
         | Right_overlay -> item1
         | Merge_overlay elt -> Item { lo = lo1; hi = hi1; elt }
         | Split_FSL (eltf, elts) | Split_FPL (eltf, elts) ->
             join
               (Item { lo = lo1; hi = Z.pred lo0; elt = eltf })
               (Item { lo = lo0; hi = hi0; elt = elts })
       else if Z.lt hi0 hi1 then
         match update Rl_Ll_Lu_Ru item0 item1 with
         | Left_overlay -> Item { lo = lo1; hi = hi1; elt = elt0 }
         | Right_overlay -> item1
         | Merge_overlay elt -> Item { lo = lo1; hi = hi1; elt }
         | Split_FSL (eltf, elts) ->
             join
               (Item { lo = lo1; hi = Z.pred lo0; elt = eltf })
               (Item { lo = lo0; hi = hi1; elt = elts })
         | Split_FPL (eltf, elts) ->
             join
               (Item { lo = lo1; hi = Z.pred hi0; elt = eltf })
               (Item { lo = hi0; hi = hi1; elt = elts })
         | Split_FSTL (eltf, eltm, eltl) ->
             join
               (join
                  (Item { lo = lo1; hi = Z.pred lo0; elt = eltf })
                  (Item { lo = lo0; hi = Z.pred hi0; elt = eltm }))
               (Item { lo = hi0; hi = hi1; elt = eltl })
       else
         match update Rl_Ll_Ru_Lu item0 item1 with
         | Left_overlay -> Item { lo = lo1; hi = hi0; elt = elt0 }
         | Right_overlay -> Item { lo = lo1; hi = hi0; elt = elt1 }
         | Merge_overlay elt -> Item { lo = lo1; hi = hi0; elt }
         | Split_FSL (eltf, elts) ->
             join
               (Item { lo = lo1; hi = Z.pred lo0; elt = eltf })
               (Item { lo = lo0; hi = hi0; elt = elts })
         | Split_FPL (eltf, elts) ->
             join
               (Item { lo = lo1; hi = Z.pred hi1; elt = eltf })
               (Item { lo = hi1; hi = hi0; elt = elts })
         | Split_FSTL (eltf, eltm, eltl) ->
             join
               (join
                  (Item { lo = lo1; hi = Z.pred lo0; elt = eltf })
                  (Item { lo = lo0; hi = Z.pred hi1; elt = eltm }))
               (Item { lo = hi1; hi = hi0; elt = eltl })
     in
     let union_update_overlap_items :
         'a update_handler -> 'a item -> 'a item -> bool -> 'a tree =
      fun update item0 item1 swap ->
       if swap then union_update_overlap_items_reverted update item1 item0
       else union_update_overlap_items_ordered update item0 item1
     in
     let rec union_update_ordered :
         'a update_handler -> 'a tree -> 'a tree -> bool -> 'a tree =
      fun update tree0 tree1 swap ->
       if tree0 == tree1 then tree0
       else
         let ub0 = upper_bound tree0 and lb1 = lower_bound tree1 in
         if Z.lt ub0 lb1 then join tree0 tree1
         else union_update_overlap update tree0 tree1 swap
     and union_update_overlap :
         'a update_handler -> 'a tree -> 'a tree -> bool -> 'a tree =
      fun update tree0 tree1 swap ->
       match (tree0, tree1) with
       | (Item _ as item0), (Item _ as item1) ->
           union_update_overlap_items update item0 item1 swap
       | ( (Item { hi = hi0; _ } as item0),
           (Node { zero = zero1; one = one1; _ } as node1) ) ->
           if Z.leq hi0 (upper_bound zero1) then
             join (union_update_overlap update item0 zero1 swap) one1
           else union_update_destruct_left update Root Root item0 node1 swap
       | ( (Node { zero = zero0; one = one0; _ } as node0),
           (Item { lo = lo1; _ } as item1) ) ->
           if Z.leq (upper_bound one0) lo1 then
             join zero0 (union_update_overlap update one0 item1 swap)
           else union_update_destruct_left update Root Root node0 item1 swap
       | ( (Node { hi = hi0; zero = zero0; one = one0; _ } as node0),
           (Node { lo = lo1; zero = zero1; one = one1; _ } as node1) ) ->
           if Z.leq hi0 (upper_bound zero1) then
             join (union_update_overlap update node0 zero1 swap) one1
           else if Z.leq (lower_bound one0) lo1 then
             join zero0 (union_update_overlap update one0 node1 swap)
           else union_update_destruct_left update Root Root node0 node1 swap
     and union_update_destruct_left :
         'a update_handler ->
         'a path ->
         'a path ->
         'a tree ->
         'a tree ->
         bool ->
         'a tree =
      fun update path0 path1 tree0 tree1 swap ->
       match (tree0, tree1) with
       | (Item _ as item0), (Item _ as item1) ->
           union_update_destruct_right update path0 path1
             (union_update_overlap_items update item0 item1 swap)
             swap
       | (Item _ as item0), Node { zero = zero1; one = one1; _ } ->
           union_update_destruct_left update path0
             (Right (path1, one1))
             item0 zero1 swap
       | Node { zero = zero0; one = one0; _ }, (Item { lo = lo1; _ } as item1) ->
           if Z.leq (lower_bound one0) lo1 then
             union_update_destruct_right update path0 path1
               (join zero0 (union_update_overlap update one0 item1 swap))
               swap
           else if Z.lt (upper_bound zero0) lo1 then
             union_update_destruct_right update path0 path1
               (join zero0 (union_update_ordered update item1 one0 (not swap)))
               swap
           else
             union_update_destruct_left update
               (Right (path0, one0))
               path1 zero0 item1 swap
       | ( Node { zero = zero0; one = one0; _ },
           (Node { lo = lo1; zero = zero1; one = one1; _ } as node1) ) ->
           if Z.leq (lower_bound one0) lo1 then
             union_update_destruct_right update path0 path1
               (join zero0 (union_update_overlap update one0 node1 swap))
               swap
           else if Z.lt (upper_bound zero0) lo1 then
             union_update_destruct_right update path0 path1
               (join zero0 (union_update_ordered update node1 one0 (not swap)))
               swap
           else
             union_update_destruct_left update
               (Right (path0, one0))
               (Right (path1, one1))
               zero0 zero1 swap
     and union_update_destruct_right :
         'a update_handler -> 'a path -> 'a path -> 'a tree -> bool -> 'a tree =
       let rec union_update_destruct_right_single :
           'a update_handler -> 'a right -> 'a tree -> bool -> 'a tree =
        fun update (Right (path0, tree0)) acc swap ->
         if Z.lt (upper_bound acc) (lower_bound tree0) then
           union_update_destruct_right_single_disjoint path0 (join acc tree0)
         else
           match path0 with
           | Root -> union_update_overlap update acc tree0 (not swap)
           | Right _ as right0 ->
               union_update_destruct_right_single update right0
                 (union_update_overlap update acc tree0 (not swap))
                 swap
       and union_update_destruct_right_single_disjoint :
           'a path -> 'a tree -> 'a tree =
        fun path0 acc ->
         match path0 with
         | Root -> acc
         | Right (path0, tree0) ->
             union_update_destruct_right_single_disjoint path0 (join acc tree0)
       and union_update_destruct_right_double :
           'a update_handler -> 'a right -> 'a right -> 'a tree -> bool -> 'a tree
           =
        fun update (Right (_, tree0) as right0) (Right (_, tree1) as right1) acc
            swap ->
         if Z.lt (lower_bound tree1) (lower_bound tree0) then
           union_update_destruct_right_double_ordered update right1 right0 acc
             (not swap)
         else
           union_update_destruct_right_double_ordered update right0 right1 acc swap
       and union_update_destruct_right_double_ordered :
           'a update_handler -> 'a right -> 'a right -> 'a tree -> bool -> 'a tree
           =
        fun update (Right (path0, tree0)) (Right (path1, tree1) as right1) acc swap ->
         if Z.lt (upper_bound tree0) (lower_bound tree1) then
           union_update_destruct_right update path0 right1
             (union_update_ordered update acc tree0 (not swap))
             swap
         else
           union_update_destruct_left update path0 path1
             (union_update_ordered update acc tree0 (not swap))
             tree1 swap
       in
       fun update path0 path1 acc swap ->
         match (path0, path1) with
         | Root, Root -> acc
         | Root, (Right _ as right1) ->
             union_update_destruct_right_single update right1 acc (not swap)
         | (Right _ as right0), Root ->
             union_update_destruct_right_single update right0 acc swap
         | (Right _ as right0), (Right _ as right1) ->
             union_update_destruct_right_double update right0 right1 acc swap
     in
     fun update t0 t1 ->
       match (t0, t1) with
       | Empty, t1 -> t1
       | t0, Empty -> t0
       | ( ((Item { lo = lo0; _ } | Node { lo = lo0; _ }) as tree0),
           ((Item { lo = lo1; _ } | Node { lo = lo1; _ }) as tree1) ) ->
           any
             (if Z.lt lo1 lo0 then union_update_ordered update tree1 tree0 true
              else union_update_ordered update tree0 tree1 false) *)

let union_update :
    ?stich:('a item -> 'a item -> 'a option) ->
    ('a item -> 'a item -> 'a t) ->
    'a t ->
    'a t ->
    'a t =
  let union_update_overlap_items :
      ('a item -> 'a item -> 'a t) -> 'a item -> 'a item -> bool -> 'a t =
   fun update item0 item1 swap ->
    if swap then update item1 item0 else update item0 item1
  in
  let rec union_update_ordered :
      stich:('a item -> 'a item -> 'a option) option ->
      ('a item -> 'a item -> 'a t) ->
      'a tree ->
      'a tree ->
      bool ->
      'a t =
   fun ~stich update tree0 tree1 swap ->
    if tree0 == tree1 then any tree0
    else
      let ub0 = upper_bound tree0 and lb1 = lower_bound tree1 in
      if Z.lt ub0 lb1 then any (join ~stich tree0 tree1)
      else union_update_overlap ~stich update tree0 tree1 swap
  and union_update_overlap :
      stich:('a item -> 'a item -> 'a option) option ->
      ('a item -> 'a item -> 'a t) ->
      'a tree ->
      'a tree ->
      bool ->
      'a t =
   fun ~stich update tree0 tree1 swap ->
    match (tree0, tree1) with
    | (Item _ as item0), (Item _ as item1) ->
        union_update_overlap_items update item0 item1 swap
    | ( (Item { hi = hi0; _ } as item0),
        (Node { zero = zero1; one = one1; _ } as node1) ) ->
        if Z.leq hi0 (upper_bound zero1) then
          any
            (join_right ~stich
               (union_update_overlap ~stich update item0 zero1 swap)
               one1)
        else union_update_destruct_left ~stich update Root Root item0 node1 swap
    | ( (Node { zero = zero0; one = one0; _ } as node0),
        (Item { lo = lo1; _ } as item1) ) ->
        if Z.leq (upper_bound one0) lo1 then
          any
            (join_left ~stich zero0
               (union_update_overlap ~stich update one0 item1 swap))
        else union_update_destruct_left ~stich update Root Root node0 item1 swap
    | ( (Node { hi = hi0; zero = zero0; one = one0; _ } as node0),
        (Node { lo = lo1; zero = zero1; one = one1; _ } as node1) ) ->
        if Z.leq hi0 (upper_bound zero1) then
          any
            (join_right ~stich
               (union_update_overlap ~stich update node0 zero1 swap)
               one1)
        else if Z.leq (lower_bound one0) lo1 then
          any
            (join_left ~stich zero0
               (union_update_overlap ~stich update one0 node1 swap))
        else union_update_destruct_left ~stich update Root Root node0 node1 swap
  and union_update_destruct_left :
      stich:('a item -> 'a item -> 'a option) option ->
      ('a item -> 'a item -> 'a t) ->
      'a path ->
      'a path ->
      'a tree ->
      'a tree ->
      bool ->
      'a t =
   fun ~stich update path0 path1 tree0 tree1 swap ->
    match (tree0, tree1) with
    | (Item _ as item0), (Item _ as item1) ->
        union_update_destruct_right ~stich update path0 path1
          (union_update_overlap_items update item0 item1 swap)
          swap
    | (Item _ as item0), Node { zero = zero1; one = one1; _ } ->
        union_update_destruct_left ~stich update path0
          (Right (path1, one1))
          item0 zero1 swap
    | Node { zero = zero0; one = one0; _ }, (Item { lo = lo1; _ } as item1) ->
        if Z.leq (lower_bound one0) lo1 then
          union_update_destruct_right_acc ~stich update path0 path1
            (join_left ~stich zero0
               (union_update_overlap ~stich update one0 item1 swap))
            swap
        else if Z.lt (upper_bound zero0) lo1 then
          union_update_destruct_right_acc ~stich update path0 path1
            (join_left ~stich zero0
               (union_update_ordered ~stich update item1 one0 (not swap)))
            swap
        else
          union_update_destruct_left ~stich update
            (Right (path0, one0))
            path1 zero0 item1 swap
    | ( Node { zero = zero0; one = one0; _ },
        (Node { lo = lo1; zero = zero1; one = one1; _ } as node1) ) ->
        if Z.leq (lower_bound one0) lo1 then
          union_update_destruct_right_acc ~stich update path0 path1
            (join_left ~stich zero0
               (union_update_overlap ~stich update one0 node1 swap))
            swap
        else if Z.lt (upper_bound zero0) lo1 then
          union_update_destruct_right_acc ~stich update path0 path1
            (join_left ~stich zero0
               (union_update_ordered ~stich update node1 one0 (not swap)))
            swap
        else
          union_update_destruct_left ~stich update
            (Right (path0, one0))
            (Right (path1, one1))
            zero0 zero1 swap
  and union_update_destruct_right :
      stich:('a item -> 'a item -> 'a option) option ->
      ('a item -> 'a item -> 'a t) ->
      'a path ->
      'a path ->
      'a t ->
      bool ->
      'a t =
   fun ~stich update path0 path1 acc swap ->
    match (path0, path1) with
    | Root, Root -> acc
    | Root, (Right _ as right1) ->
        union_update_destruct_right_single ~stich update right1 acc (not swap)
    | (Right _ as right0), Root ->
        union_update_destruct_right_single ~stich update right0 acc swap
    | (Right _ as right0), (Right _ as right1) ->
        union_update_destruct_right_double ~stich update right0 right1 acc swap
  and union_update_destruct_right_acc :
      stich:('a item -> 'a item -> 'a option) option ->
      ('a item -> 'a item -> 'a t) ->
      'a path ->
      'a path ->
      'a tree ->
      bool ->
      'a t =
   fun ~stich update path0 path1 acc swap ->
    match (path0, path1) with
    | Root, Root -> any acc
    | Root, (Right _ as right1) ->
        union_update_destruct_right_single_acc ~stich update right1 acc
          (not swap)
    | (Right _ as right0), Root ->
        union_update_destruct_right_single_acc ~stich update right0 acc swap
    | (Right _ as right0), (Right _ as right1) ->
        union_update_destruct_right_double ~stich update right0 right1 (any acc)
          swap
  and union_update_destruct_right_single :
      stich:('a item -> 'a item -> 'a option) option ->
      ('a item -> 'a item -> 'a t) ->
      'a right ->
      'a t ->
      bool ->
      'a t =
   fun ~stich update (Right (path0, tree0) as right0) acc swap ->
    match acc with
    | Empty -> union_update_destruct_right_single_disjoint ~stich path0 tree0
    | (Item _ | Node _) as acc ->
        union_update_destruct_right_single_acc ~stich update right0 acc swap
  and union_update_destruct_right_single_acc :
      stich:('a item -> 'a item -> 'a option) option ->
      ('a item -> 'a item -> 'a t) ->
      'a right ->
      'a tree ->
      bool ->
      'a t =
   fun ~stich update (Right (path0, tree0)) acc swap ->
    if Z.lt (upper_bound acc) (lower_bound tree0) then
      union_update_destruct_right_single_disjoint ~stich path0
        (join ~stich acc tree0)
    else
      match path0 with
      | Root -> union_update_overlap ~stich update acc tree0 (not swap)
      | Right _ as right0 ->
          union_update_destruct_right_single ~stich update right0
            (union_update_overlap ~stich update acc tree0 (not swap))
            swap
  and union_update_destruct_right_single_disjoint :
      stich:('a item -> 'a item -> 'a option) option ->
      'a path ->
      'a tree ->
      'a t =
   fun ~stich path0 acc ->
    match path0 with
    | Root -> any acc
    | Right (path0, tree0) ->
        union_update_destruct_right_single_disjoint ~stich path0
          (join ~stich acc tree0)
  and union_update_destruct_right_double :
      stich:('a item -> 'a item -> 'a option) option ->
      ('a item -> 'a item -> 'a t) ->
      'a right ->
      'a right ->
      'a t ->
      bool ->
      'a t =
   fun ~stich update (Right (_, tree0) as right0) (Right (_, tree1) as right1)
       acc swap ->
    if Z.lt (lower_bound tree1) (lower_bound tree0) then
      union_update_destruct_right_double_ordered ~stich update right1 right0 acc
        (not swap)
    else
      union_update_destruct_right_double_ordered ~stich update right0 right1 acc
        swap
  and union_update_destruct_right_double_ordered :
      stich:('a item -> 'a item -> 'a option) option ->
      ('a item -> 'a item -> 'a t) ->
      'a right ->
      'a right ->
      'a t ->
      bool ->
      'a t =
   fun ~stich update (Right (path0, tree0)) (Right (path1, tree1) as right1) acc
       swap ->
    if Z.lt (upper_bound tree0) (lower_bound tree1) then
      match acc with
      | Empty ->
          union_update_destruct_right_acc ~stich update path0 right1 tree0 swap
      | (Item _ | Node _) as acc ->
          union_update_destruct_right ~stich update path0 right1
            (union_update_ordered ~stich update acc tree0 (not swap))
            swap
    else
      match acc with
      | Empty ->
          union_update_destruct_left ~stich update path0 path1 tree0 tree1 swap
      | (Item _ | Node _) as acc -> (
          match union_update_ordered ~stich update acc tree0 (not swap) with
          | Empty ->
              union_update_destruct_right ~stich update path0 right1 Empty swap
          | (Item _ | Node _) as acc ->
              union_update_destruct_left ~stich update path0 path1 acc tree1
                swap)
  in
  fun ?stich update t0 t1 ->
    match (t0, t1) with
    | Empty, t1 -> t1
    | t0, Empty -> t0
    | ( ((Item { lo = lo0; _ } | Node { lo = lo0; _ }) as tree0),
        ((Item { lo = lo1; _ } | Node { lo = lo1; _ }) as tree1) ) ->
        if Z.lt lo1 lo0 then union_update_ordered ~stich update tree1 tree0 true
        else union_update_ordered ~stich update tree0 tree1 false

(* let union_eq : ('a item -> 'a item -> 'a) -> 'a t -> 'a t -> 'a t =
   let union_eq_overlap_items :
       ('a item -> 'a item -> 'a) -> 'a item -> 'a item -> 'a tree =
    fun merge (Item { lo = lo0; hi = hi0; elt = elt0 } as item0)
        (Item { hi = hi1; elt = elt1; _ } as item1) ->
     if Z.leq hi1 hi0 && elt0 == elt1 then item0
     else Item { lo = lo0; hi = Z.max hi0 hi1; elt = merge item0 item1 }
   in
   let rec union_eq_ordered :
       ('a item -> 'a item -> 'a) -> 'a tree -> 'a tree -> 'a tree =
    fun merge tree0 tree1 ->
     (* invariant: lower_bound(tree0) <= lower_bound(tree1) *)
     (* assert (Z.leq (lower_bound tree0) (lower_bound tree1)); *)
     if tree0 == tree1 then tree0
     else
       let ub0 = upper_bound tree0 and lb1 = lower_bound tree1 in
       if Z.lt ub0 lb1 then join tree0 tree1
       else union_eq_overlap merge tree0 tree1
   and union_eq_overlap :
       ('a item -> 'a item -> 'a) -> 'a tree -> 'a tree -> 'a tree =
    fun merge tree0 tree1 ->
     match (tree0, tree1) with
     | (Item _ as item0), (Item _ as item1) ->
         union_eq_overlap_items merge item0 item1
     | ( (Item { hi = hi0; _ } as item0),
         (Node { zero = zero1; one = one1; _ } as node1) ) ->
         if Z.leq hi0 (upper_bound zero1) then
           join (union_eq_overlap merge item0 zero1) one1
         else union_eq_destruct_left merge Root Root item0 node1
     | ( (Node { zero = zero0; one = one0; _ } as node0),
         (Item { lo = lo1; _ } as item1) ) ->
         if Z.leq (upper_bound one0) lo1 then
           join zero0 (union_eq_overlap merge one0 item1)
         else union_eq_destruct_left merge Root Root node0 item1
     | ( (Node { hi = hi0; zero = zero0; one = one0; _ } as node0),
         (Node { lo = lo1; zero = zero1; one = one1; _ } as node1) ) ->
         if Z.leq hi0 (upper_bound zero1) then
           join (union_eq_overlap merge node0 zero1) one1
         else if Z.leq (lower_bound one0) lo1 then
           join zero0 (union_eq_overlap merge one0 node1)
         else union_eq_destruct_left merge Root Root node0 node1
   and union_eq_destruct_left :
       ('a item -> 'a item -> 'a) ->
       'a path ->
       'a path ->
       'a tree ->
       'a tree ->
       'a tree =
    fun merge path0 path1 tree0 tree1 ->
     match (tree0, tree1) with
     | (Item _ as item0), (Item _ as item1) ->
         union_eq_destruct_right merge path0 path1
           (union_eq_overlap_items merge item0 item1)
     | (Item _ as item0), Node { zero = zero1; one = one1; _ } ->
         union_eq_destruct_left merge path0 (Right (path1, one1)) item0 zero1
     | Node { zero = zero0; one = one0; _ }, (Item { lo = lo1; _ } as item1) ->
         if Z.leq (lower_bound one0) lo1 then
           union_eq_destruct_right merge path0 path1
             (join zero0 (union_eq_overlap merge one0 item1))
         else if Z.lt (upper_bound zero0) lo1 then
           union_eq_destruct_right merge path0 path1
             (join zero0 (union_eq_ordered merge item1 one0))
         else
           union_eq_destruct_left merge (Right (path0, one0)) path1 zero0 item1
     | ( Node { zero = zero0; one = one0; _ },
         (Node { lo = lo1; zero = zero1; one = one1; _ } as node1) ) ->
         if Z.leq (lower_bound one0) lo1 then
           union_eq_destruct_right merge path0 path1
             (join zero0 (union_eq_overlap merge one0 node1))
         else if Z.lt (upper_bound zero0) lo1 then
           union_eq_destruct_right merge path0 path1
             (join zero0 (union_eq_ordered merge node1 one0))
         else
           union_eq_destruct_left merge
             (Right (path0, one0))
             (Right (path1, one1))
             zero0 zero1
   and union_eq_destruct_right :
       ('a item -> 'a item -> 'a) -> 'a path -> 'a path -> 'a tree -> 'a tree =
     let rec union_eq_destruct_right_single :
         ('a item -> 'a item -> 'a) -> 'a right -> 'a tree -> 'a tree =
      fun merge (Right (path0, tree0)) acc ->
       if Z.lt (upper_bound acc) (lower_bound tree0) then
         union_eq_destruct_right_single_disjoint path0 (join acc tree0)
       else
         match path0 with
         | Root -> union_eq_overlap merge acc tree0
         | Right _ as right0 ->
             union_eq_destruct_right_single merge right0
               (union_eq_overlap merge acc tree0)
     and union_eq_destruct_right_single_disjoint : 'a path -> 'a tree -> 'a tree
         =
      fun path0 acc ->
       match path0 with
       | Root -> acc
       | Right (path0, tree0) ->
           union_eq_destruct_right_single_disjoint path0 (join acc tree0)
     and union_eq_destruct_right_double :
         ('a item -> 'a item -> 'a) -> 'a right -> 'a right -> 'a tree -> 'a tree
         =
      fun merge (Right (_, tree0) as right0) (Right (_, tree1) as right1) acc ->
       if Z.lt (lower_bound tree1) (lower_bound tree0) then
         union_eq_destruct_right_double_ordered merge right1 right0 acc
       else union_eq_destruct_right_double_ordered merge right0 right1 acc
     and union_eq_destruct_right_double_ordered :
         ('a item -> 'a item -> 'a) -> 'a right -> 'a right -> 'a tree -> 'a tree
         =
      fun merge (Right (path0, tree0)) (Right (path1, tree1) as right1) acc ->
       if Z.lt (upper_bound tree0) (lower_bound tree1) then
         union_eq_destruct_right merge path0 right1
           (union_eq_ordered merge acc tree0)
       else
         union_eq_destruct_left merge path0 path1
           (union_eq_ordered merge acc tree0)
           tree1
     in
     fun merge path0 path1 acc ->
       match (path0, path1) with
       | Root, Root -> acc
       | Root, (Right _ as right1) ->
           union_eq_destruct_right_single merge right1 acc
       | (Right _ as right0), Root ->
           union_eq_destruct_right_single merge right0 acc
       | (Right _ as right0), (Right _ as right1) ->
           union_eq_destruct_right_double merge right0 right1 acc
   in
   fun merge t0 t1 ->
     match (t0, t1) with
     | Empty, t1 -> t1
     | t0, Empty -> t0
     | ( ((Item { lo = lo0; _ } | Node { lo = lo0; _ }) as tree0),
         ((Item { lo = lo1; _ } | Node { lo = lo1; _ }) as tree1) ) ->
         any
           (if Z.lt lo1 lo0 then union_eq_ordered merge tree1 tree0
            else union_eq_ordered merge tree0 tree1) *)

let union_eq :
    ?stich:('a item -> 'a item -> 'a option) ->
    ('a item -> 'a item -> 'a) ->
    'a t ->
    'a t ->
    'a t =
  let union : ('a item -> 'a item -> 'a) -> 'a item -> 'a item -> 'a t =
   fun merge (Item { lo = lo0; hi = hi0; elt = elt0 } as item0)
       (Item { lo = lo1; hi = hi1; elt = elt1; _ } as item1) ->
    if elt0 == elt1 then
      if Z.leq lo0 lo1 then
        if Z.leq hi1 hi0 then item0 else Item { lo = lo0; hi = hi1; elt = elt0 }
      else if Z.leq hi0 hi1 then item1
      else Item { lo = lo1; hi = hi0; elt = elt0 }
    else
      Item { lo = Z.min lo0 lo1; hi = Z.max hi0 hi1; elt = merge item0 item1 }
  in
  fun ?stich merge t0 t1 -> union_update ?stich (union merge) t0 t1

let union_left :
    ?stich:('a item -> 'a item -> 'a option) ->
    ?crop:(lo:Z.t -> hi:Z.t -> 'a -> 'a) ->
    'a t ->
    'a t ->
    'a t =
  let keep_left : (lo:Z.t -> hi:Z.t -> 'a -> 'a) -> 'a item -> 'a item -> 'a t =
   fun crop (Item { lo = lo0; hi = hi0; _ } as item0)
       (Item { lo = lo1; hi = hi1; elt = elt1 }) ->
    if Z.leq lo0 lo1 then
      if Z.leq hi1 hi0 then (* Ll Rl Rh Lh  *)
        item0
      else
        (* Ll Rl Lh Rh *)
        let loa = Z.succ hi0 in
        any
          (join ~stich:None item0
             (Item
                {
                  lo = loa;
                  hi = hi1;
                  elt = crop ~lo:(Z.sub loa lo1) ~hi:(Z.sub hi1 lo1) elt1;
                }))
    else
      let hib = Z.pred lo0 in
      if Z.leq hi1 hi0 then
        (* Rl Ll Rh Lh  *)
        any
          (join ~stich:None
             (Item
                {
                  lo = lo1;
                  hi = hib;
                  elt = crop ~lo:Z.zero ~hi:(Z.sub hib lo1) elt1;
                })
             item0)
      else
        (* Rl Ll Lh Rh *)
        let loa = Z.succ hi0 in
        any
          (join ~stich:None
             (join ~stich:None
                (Item
                   {
                     lo = lo1;
                     hi = hib;
                     elt = crop ~lo:Z.zero ~hi:(Z.sub hib lo1) elt1;
                   })
                item0)
             (Item
                {
                  lo = loa;
                  hi = hi1;
                  elt = crop ~lo:(Z.sub loa lo1) ~hi:(Z.sub hi1 lo1) elt1;
                }))
  in
  fun ?stich ?(crop = fun ~lo:_ ~hi:_ a -> a) t0 t1 ->
    union_update ?stich (keep_left crop) t0 t1

(* let union_split :
     ?crop:(lo:Z.t -> hi:Z.t -> 'a -> 'a) ->
     ('a item -> 'a item -> 'a) ->
     'a t ->
     'a t ->
     'a t =
   let split :
       (lo:Z.t -> hi:Z.t -> 'a -> 'a) ->
       ('a item -> 'a item -> 'a) ->
       'a item ->
       'a item ->
       'a t =
    fun crop merge (Item { lo = lo0; hi = hi0; elt = elt0 } as item0)
        (Item { lo = lo1; hi = hi1; elt = elt1 } as item1) ->
     if Z.leq lo0 lo1 then
       if Z.equal lo0 lo1 then
         if Z.leq hi0 hi1 then
           if Z.equal hi0 hi1 then
           (Item
                    {
                      lo = lo0;
                      hi = hi0;
                      elt = merge item0 item1;
                    })
           else assert false
         else assert false
       else if Z.leq hi1 hi0 then
         (* Ll Rl Rh Lh  *)
         let hib = Z.pred lo1 and loa = Z.succ hi1 in
         any
           (join
              (join
                 (Item
                    {
                      lo = lo0;
                      hi = hib;
                      elt = crop ~lo:Z.zero ~hi:(Z.sub hib lo0) elt0;
                    })
                 (Item { lo = lo1; hi = hi1; elt = merge item0 item1 }))
              (Item
                 {
                   lo = loa;
                   hi = hi0;
                   elt = crop ~lo:(Z.sub loa lo0) ~hi:(Z.sub hi0 lo0) elt0;
                 }))
       else
         (* Ll Rl Lh Rh *)
         let loa = Z.succ hi0 in
         any
           (join item0
              (Item
                 {
                   lo = loa;
                   hi = hi1;
                   elt = crop ~lo:(Z.sub loa lo1) ~hi:(Z.sub hi1 lo1) elt1;
                 }))
     else
       let hib = Z.pred lo0 in
       if Z.leq hi1 hi0 then
         (* Rl Ll Rh Lh  *)
         any
           (join
              (Item
                 {
                   lo = lo1;
                   hi = hib;
                   elt = crop ~lo:Z.zero ~hi:(Z.sub hib lo1) elt1;
                 })
              item0)
       else
         (* Rl Ll Lh Rh *)
         let loa = Z.succ hi0 in
         any
           (join
              (join
                 (Item
                    {
                      lo = lo1;
                      hi = hib;
                      elt = crop ~lo:Z.zero ~hi:(Z.sub hib lo1) elt1;
                    })
                 item0)
              (Item
                 {
                   lo = loa;
                   hi = hi1;
                   elt = crop ~lo:(Z.sub loa lo1) ~hi:(Z.sub hi1 lo1) elt1;
                 }))
   in
   fun ?(crop = fun ~lo:_ ~hi:_ a -> a) merge t0 t1 ->
     union_update (split crop merge) t0 t1 *)

let disjoint : type a b. a t -> b t -> bool =
  let rec disjoint_ordered : type a b. a tree -> b tree -> bool =
   fun tree0 tree1 ->
    let ub0 = upper_bound tree0 and lb1 = lower_bound tree1 in
    Z.lt ub0 lb1
    ||
    match (tree0, tree1) with
    | Item _, Item _ -> false
    | Item { lo = lb0; _ }, Node { zero = zero1; one = one1; _ } ->
        is_empty_between_choice lb0 ub0 zero1 one1
    | Node { zero = zero0; one = one0; _ }, Item { hi = ub1; _ } ->
        is_empty_between_choice lb1 ub1 zero0 one0
    | ( (Node { zero = zero0; one = one0; _ } as node0),
        (Node { zero = zero1; one = one1; _ } as node1) ) ->
        if Z.leq (lower_bound one0) lb1 then disjoint_ordered one0 node1
        else if Z.lt ub0 (lower_bound one1) then disjoint_ordered node0 zero1
        else disjoint_ordered zero0 node1 && disjoint_ordered node1 one0
  in
  fun t0 t1 ->
    match (t0, t1) with
    | Empty, _ | _, Empty -> true
    | ( ((Item { lo = lo0; _ } | Node { lo = lo0; _ }) as tree0),
        ((Item { lo = lo1; _ } | Node { lo = lo1; _ }) as tree1) ) ->
        if Z.lt lo1 lo0 then disjoint_ordered tree1 tree0
        else disjoint_ordered tree0 tree1

let substract :
    type a b. ?crop:(lo:Z.t -> hi:Z.t -> a -> a) -> a t -> b t -> a t =
  let union_disjoint : type a. a t -> a t -> a t =
   fun t0 t1 ->
    match (t0, t1) with
    | Empty, t1 -> t1
    | t0, Empty -> t0
    | ((Item _ | Node _) as tree0), ((Item _ | Node _) as tree1) ->
        any (join ~stich:None tree0 tree1)
  in
  let rec substract :
      type a b. crop:(lo:Z.t -> hi:Z.t -> a -> a) -> a tree -> b tree -> a t =
   fun ~crop tree0 tree1 ->
    let ub0 = upper_bound tree0 and lb1 = lower_bound tree1 in
    if Z.lt ub0 lb1 then any tree0
    else
      let lb0 = lower_bound tree0 and ub1 = upper_bound tree1 in
      if Z.lt ub1 lb0 then any tree0
      else
        match (tree0, tree1) with
        | ( Item { lo = lo0; hi = hi0; elt = elt0 },
            Item { lo = lo1; hi = hi1; _ } ) ->
            if Z.leq lo1 lo0 then
              if Z.leq hi0 hi1 then Empty
              else
                let loa = Z.succ hi1 in
                Item
                  {
                    lo = loa;
                    hi = hi0;
                    elt = crop ~lo:(Z.sub loa lo0) ~hi:(Z.sub hi0 lo0) elt0;
                  }
            else if Z.leq hi0 hi1 then
              let hib = Z.pred lo1 in
              Item
                {
                  lo = lo0;
                  hi = hib;
                  elt = crop ~lo:Z.zero ~hi:(Z.sub hib lo0) elt0;
                }
            else
              let loa = Z.succ hi1 and hib = Z.pred lo1 in
              any
                (join ~stich:None
                   (Item
                      {
                        lo = lo0;
                        hi = hib;
                        elt = crop ~lo:Z.zero ~hi:(Z.sub hib lo0) elt0;
                      })
                   (Item
                      {
                        lo = loa;
                        hi = hi0;
                        elt = crop ~lo:(Z.sub loa lo0) ~hi:(Z.sub hi0 lo0) elt0;
                      }))
        | (Item _ as item0), Node { zero = zero1; one = one1; _ } -> (
            let item0' = substract ~crop item0 zero1 in
            match item0' with
            | Empty -> Empty
            | (Item _ | Node _) as tree0' -> substract ~crop tree0' one1)
        | ( (Node { zero = zero0; one = one0; _ } as node0),
            ((Item _ | Node _) as tree1) ) ->
            let zero0' = substract ~crop zero0 tree1
            and one0' = substract ~crop one0 tree1 in
            if any zero0 == zero0' && any one0 == one0' then node0
            else union_disjoint zero0' one0'
  in
  fun ?(crop = fun ~lo:_ ~hi:_ a -> a) t0 t1 ->
    match (t0, t1) with
    | Empty, _ -> Empty
    | t0, Empty -> t0
    | ((Item _ | Node _) as tree0), ((Item _ | Node _) as tree1) ->
        substract ~crop tree0 tree1

let fold_inter :
    type a b. (a item -> b item -> 'c -> 'c) -> 'c -> a t -> b t -> 'c =
  let rec fold_inter :
      type a b. (a item -> b item -> 'c -> 'c) -> 'c -> a tree -> b tree -> 'c =
   fun f acc tree0 tree1 ->
    let ub0 = upper_bound tree0 and lb1 = lower_bound tree1 in
    if Z.lt ub0 lb1 then acc
    else
      let lb0 = lower_bound tree0 and ub1 = upper_bound tree1 in
      if Z.lt ub1 lb0 then acc
      else
        match (tree0, tree1) with
        | (Item _ as item0), (Item _ as item1) -> f item0 item1 acc
        | (Item _ as item0), Node { zero = zero1; one = one1; _ } ->
            fold_inter f (fold_inter f acc item0 zero1) item0 one1
        | Node { zero = zero0; one = one0; _ }, ((Item _ | Node _) as tree1) ->
            fold_inter f (fold_inter f acc zero0 tree1) one0 tree1
  in
  fun f acc t0 t1 ->
    match (t0, t1) with
    | Empty, _ | _, Empty -> acc
    | ((Item _ | Node _) as tree0), ((Item _ | Node _) as tree1) ->
        fold_inter f acc tree0 tree1