Source file lm_list_util.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
# 1 "stdlib/lm_list_util.ml"
exception Unchanged
let sub =
let rec skip l off len =
if off = 0 then
collect [] l len
else
match l with
_ :: l ->
skip l (off - 1) len
| [] ->
raise (Invalid_argument "Lm_list_util.sub")
and collect l1 l2 len =
if len = 0 then
List.rev l1
else
match l2 with
x :: l2 ->
collect (x :: l1) l2 (len - 1)
| [] ->
raise (Invalid_argument "Lm_list_util.sub")
in
skip
let rec short_iter2 f l1 l2 =
match l1, l2 with
h1 :: l1, h2 :: l2 ->
f h1 h2;
short_iter2 f l1 l2
| _ ->
()
let rec iter3 f l1 l2 l3 =
match l1, l2, l3 with
h1 :: l1, h2 :: l2, h3 :: l3 ->
f h1 h2 h3;
iter3 f l1 l2 l3
| [], [], [] ->
()
| _ ->
raise (Invalid_argument "iter3")
let rec filter f = function
[] -> []
| (h::t) as l ->
if f h then
let rem = filter f t in
if rem == t then
l
else
h::rem
else
filter f t
let rec insert_nth i x l =
if i = 0 then
x :: l
else
match l with
h::t ->
h :: insert_nth (i - 1) x t
| [] ->
raise (Failure "Lm_list_util.insert_nth")
let rec remove_nth i l =
match l with
x :: l ->
if i = 0 then
l
else
x :: remove_nth (pred i) l
| [] ->
raise (Failure "Lm_list_util.remove_nth")
let rec rev_map' f l = function
h :: t ->
rev_map' f (f h :: l) t
| [] ->
l
let rev_map f l =
rev_map' f [] l
let rec rev_iter f = function
h::t ->
rev_iter f t;
ignore (f h)
| [] ->
()
let rec flat_map_aux f accum l = function
h::t ->
flat_map_aux f (h::accum) l t
| [] ->
begin match l with
[] ->
accum
| h :: t ->
flat_map_aux f accum t (f h)
end
let flat_map f l =
List.rev (flat_map_aux f [] l [])
let rec fail_map f = function
h::t ->
begin
try
let h = f h in
h :: fail_map f t
with
Failure _ ->
fail_map f t
end
| [] ->
[]
let rec some_map_aux unchanged f = function
h :: t ->
begin
match f h with
Some h' ->
h' :: some_map_aux (unchanged && h' == h) f t
| None ->
some_map_aux false f t
end
| [] ->
if unchanged then
raise Unchanged;
[]
let some_map_safe f l =
try some_map_aux true f l with
Unchanged ->
l
let rec some_map f = function
h :: t ->
begin
match f h with
Some h' ->
h' :: some_map f t
| None ->
some_map f t
end
| [] ->
[]
let rec fold_left_aux f x l = function
h :: t ->
let x', h' = f x h in
fold_left_aux f x' (h' :: l) t
| [] ->
x, List.rev l
let fold_left f x l = fold_left_aux f x [] l
let rec fold_left3 f arg l1 l2 l3 =
match l1, l2, l3 with
h1 :: t1, h2 :: t2, h3 :: t3 ->
fold_left3 f (f arg h1 h2 h3) t1 t2 t3
| [], [], [] ->
arg
| _ ->
raise (Invalid_argument "fold_left3")
let rec find f = function
h::t ->
if f h then
h
else
find f t
| [] ->
raise Not_found
let rec find_item_aux f i = function
h::t ->
if f h then
i
else
find_item_aux f (i + 1) t
| [] ->
raise Not_found
let find_item f l = find_item_aux f 0 l
let rec find_index_aux v i = function
h::t ->
if h = v then
i
else
find_index_aux v (i + 1) t
| [] ->
raise Not_found
let find_index v l = find_index_aux v 0 l
let rec fild_rindex_aux v i curr = function
[] -> i
| h :: t ->
fild_rindex_aux v (if h=v then curr else i) (curr + 1) t
let find_rindex v l =
let i = fild_rindex_aux v (-1) 0 l in
if i>=0 then i else raise Not_found
let rec find_indexq_aux v i = function
h::t ->
if h == v then
i
else
find_indexq_aux v (i + 1) t
| [] ->
raise Not_found
let find_indexq v l = find_indexq_aux v 0 l
let find_index_eq eq x l =
let rec search i = function
h :: t ->
if eq x h then
i
else
search (succ i) t
| [] ->
raise Not_found
in
search 0 l
let rec split_list i l = match (i,l) with
0, _ ->
[], l
| _, h::t ->
let l, l' = split_list (i - 1) t in
h::l, l'
| _, [] ->
raise (Failure "Lm_list_util.split_list")
let rec split_last = function
[h] ->
[], h
| h::t ->
let l, x = split_last t in
h::l, x
| [] ->
raise (Failure "Lm_list_util.split_last")
let rec split i l =
if i = 0 then
[], l
else
match l with
h :: t ->
let l1, l2 = split (pred i) t in
h :: l1, l2
| [] ->
raise (Invalid_argument "split")
let splitup n l =
let rec split i vll vl1 vl2 =
match vl2 with
v :: vl2 ->
if i = 0 then
split n (List.rev vl1 :: vll) [v] vl2
else
split (pred i) vll (v :: vl1) vl2
| [] ->
List.rev vl1 :: vll
in
match l with
[] ->
[]
| h :: t ->
split n [] [h] t
let rec last = function
[x] -> x
| _ :: t -> last t
| [] -> raise (Invalid_argument "last")
let rec firstn i l =
if i = 0 then
[]
else
match l with
h :: t ->
h :: firstn (pred i) t
| [] ->
[]
let rec nth_tl i l =
if i = 0 then
l
else
match l with
_ :: l ->
nth_tl (pred i) l
| [] ->
raise (Invalid_argument "nth_tl")
let rec replacef_nth i f = function
h::t ->
if i = 0 then
f h :: t
else
h :: replacef_nth (i - 1) f t
| [] ->
raise (Failure "Lm_list_util.replacef_nth")
let rec replacef_arg_nth i f = function
h::t ->
if i = 0 then
let h, arg = f h in
h :: t, arg
else
let t, arg = replacef_arg_nth (i - 1) f t in
h :: t, arg
| [] ->
raise (Failure "Lm_list_util.replacef_arg_nth")
let rec replace_nth i x l =
match l with
[] ->
raise (Invalid_argument "replace_nth")
| h :: l ->
if i = 0 then
x :: l
else
h :: replace_nth (pred i) x l
let rec replace_first f x = function
h::t ->
if f h then
x :: t
else
h :: replace_first f x t
| [] ->
raise Not_found
let rec replace_all f x = function
h::t ->
(if f h then x else h) :: (replace_all f x t)
| [] ->
[]
let rec replaceq x1 x2 = function
h::t ->
if h == x1 then
x2 :: replaceq x1 x2 t
else
h :: replaceq x1 x2 t
| [] ->
[]
let rec subtract_list l1 l2 =
match l1 with
h :: l1 ->
if List.mem h l2 then
subtract_list l1 l2
else
h :: subtract_list l1 l2
| [] ->
[]
let rec map2to1 f l1 l2 =
match l1, l2 with
[], [] -> []
| h1::l1, h2::l2 ->
f h1 h2 :: map2to1 f l1 l2
| _ -> raise (Invalid_argument "map2to1")
let rec fold_map f i l =
match l with
[] -> i, []
| hd :: tl ->
let i, hd = f i hd in
let i, l = fold_map f i tl in
i, hd :: l
let rec fold_map2to1 f i l1 l2 =
match l1, l2 with
[], [] -> i, []
| h1 :: l1, h2 :: l2 ->
let i, h = f i h1 h2 in
let i, l = fold_map2to1 f i l1 l2 in
i, h :: l
| _ -> raise (Invalid_argument "fold_map2to1")
let rec fold_map1to2 f i l =
match l with
[] -> i, [], []
| hd :: tl ->
let i, hd1, hd2 = f i hd in
let i, l1, l2 = fold_map1to2 f i tl in
i, hd1 :: l1, hd2 :: l2
let rec fold_filter f x l =
match l with
[] -> x, []
| h :: t ->
let x, b = f x h in
if b then
let x, t = fold_filter f x t in
x, h :: t
else
fold_filter f x t
let rec length_eq l1 l2 =
match l1, l2 with
[], [] -> true
| _ :: l1, _ :: l2 -> length_eq l1 l2
| _ -> false
let addq x l =
if List.memq x l then
l
else
x :: l
let rec intersect l = function
h::t ->
if List.mem h l then
h :: intersect l t
else
intersect l t
| [] -> []
let rec intersectq l = function
h::t ->
if List.memq h l then
h :: intersectq l t
else
intersectq l t
| [] -> []
let rec intersects l = function
h :: t ->
List.mem h l || intersects l t
| [] ->
false
let rec subtract l1 l2 =
match l1 with
h::t ->
if List.mem h l2 then
subtract t l2
else
h :: subtract t l2
| [] ->
[]
let rec mem_once v head = function
h :: t ->
if v = h then
Some (head @ t)
else
mem_once v (h :: head) t
| [] ->
None
let rec subtract_multiset l1 l2 =
match l1 with
h :: t ->
begin
match mem_once h [] l2 with
Some l2 ->
subtract_multiset t l2
| None ->
h :: subtract_multiset t l2
end
| [] ->
[]
let rec subtractq l1 l2 =
match l1 with
h::t ->
if List.memq h l2 then
subtractq t l2
else
h :: subtractq t l2
| [] ->
[]
let rec union l = function
h::t ->
if List.mem h l then
union l t
else
h::(union l t)
| [] ->
l
let rec unionq h l =
match h with
h :: t ->
if List.memq h l then
unionq t l
else
h :: unionq t l
| [] ->
l
let rec subset l1 l2 =
match l1 with
x :: l1 ->
List.mem x l2 && subset l1 l2
| [] ->
true
let rec remove_elements l1 l2 =
match l1, l2 with
flag::ft, h::t ->
if flag then
remove_elements ft t
else
h :: remove_elements ft t
| _, l ->
l
let rec removeq x = function
h::t ->
if h == x then
t
else
h :: removeq x t
| [] ->
raise (Failure "Lm_list_util.removeq")
let rec remove x = function
h::t ->
if h = x then
t
else
h :: remove x t
| [] ->
raise (Failure "Lm_list_util.remove")
let rec tryremove x = function
(h :: t) as l ->
if h = x then
t
else
let res = tryremove x t in
if res == t then
l
else
h :: res
| [] ->
[]
let rec remove_suffix_aux suffix = function
(0, l') ->
if l' = suffix then
[]
else
raise (Failure "Lm_list_util.remove_suffix")
| (i, _::t) ->
remove_suffix_aux suffix (i - 1, t)
| _ ->
raise (Failure "Lm_list_util.remove_suffix")
let remove_suffix l suffix =
let i = (List.length l) - (List.length suffix) in
if i >= 0 then
remove_suffix_aux suffix (i, l)
else
raise (Failure "Lm_list_util.remove_suffix")
let rec compare_lists cmp l1 l2 =
match (l1,l2) with
h1::t1, h2::t2 ->
let i = cmp h1 h2 in
if i = 0 then
compare_lists cmp t1 t2
else
i
| [], [] -> 0
| [], _ -> -1
| _ -> 1
let rec compare_cmp cmp l1 l2 =
match l1, l2 with
h1 :: t1, h2 :: t2 ->
cmp h1 h2 && compare_cmp cmp t1 t2
| [], [] -> true
| _ -> false
let rec compare_eq l1 l2 =
match l1, l2 with
h1::t1, h2::t2 ->
h1 == h2 && compare_eq t1 t2
| [], [] ->
true
| _ ->
false
let rec nth l i =
if i <= 0 then
raise (Failure "Lm_list_util.nth")
else
match l with
_::t ->
nth t (i - 1)
| [] ->
raise (Failure "Lm_list_util.nth")
let rec map2 f l1 l2 = match (l1,l2) with
h1::t1, h2::t2 ->
let h = f h1 h2 in
h :: map2 f t1 t2
| [], [] -> []
| _ -> raise (Failure "Lm_list_util.map2")
let rec iter2 f al bl =
match (al, bl) with
h1::t1, h2::t2 ->
f h1 h2;
iter2 f t1 t2
| [], [] ->
()
| _ ->
raise (Failure "Lm_list_util.iter2")
let rec rev_iter2 f a b =
match (a,b) with
([], []) -> ()
| (ha::ta, hb::tb) -> rev_iter2 f ta tb; f ha hb
| _ -> raise (Failure "Lm_list_util.rev_iter2")
let rec for_all2 f l1 l2 =
match (l1,l2) with
h1::t1, h2::t2 -> f h1 h2 && for_all2 f t1 t2
| [], [] -> true
| _ -> false
let rec exists2 f l1 l2 = match (l1,l2) with
h1::t1, h2::t2 ->
f h1 h2 || exists2 f t1 t2
| _ -> false
let rec fold_left2 f x al bl =
match (al, bl) with
(h1::t1, h2::t2) ->
fold_left2 f (f x h1 h2) t1 t2
| [], [] ->
x
| _ ->
raise (Failure "Lm_list_util.fold_left2")
let rec smap f = function
[] -> []
| hd::tl as l ->
let hd' = f hd in
let tl' = smap f tl in
if (hd == hd') && (tl == tl') then
l
else
hd'::tl'
let rec zip_list l l1 l2 = match (l1,l2) with
(h1::t1), (h2::t2) ->
zip_list ((h1,h2)::l) t1 t2
| [], [] ->
l
| _ -> raise (Failure "Lm_list_util.zip")
let rec zip a b = match (a,b) with
(h1::t1), (h2::t2) ->
(h1, h2) :: zip t1 t2
| [], [] ->
[]
|
_ -> raise (Failure "Lm_list_util.zip")
let rec fst_split = function
[] -> []
| (a, _) :: tl -> a :: fst_split tl
let rec apply_assoc v l f =
match l with
(v', k) as h :: t ->
if v' = v then
(v', f k) :: t
else
h :: apply_assoc v t f
| [] ->
raise Not_found
let rec assoc_index_aux a i = function
(a', _)::t ->
if a' = a then
i
else
assoc_index_aux a (i + 1) t
| [] -> raise Not_found
let assoc_index l a = assoc_index_aux a 0 l
let rec assoc_replace l a b = match l with
(a', b')::t ->
if a' = a then
(a, b)::t
else
(a', b')::(assoc_replace t a b)
| [] -> raise Not_found
let add_assoc (v1, v2) l =
try
let v2' = List.assoc v1 l in
if v2 = v2' then
l
else
raise (Failure "Lm_list_util.add_assoc")
with
Not_found -> (v1, v2)::l
let rec assoc_in_dom eq y = function
(y',_)::tl ->
(eq y y') || (assoc_in_dom eq y tl)
| [] ->
false
let rec assoc_in_range eq y = function
(_, y')::tl ->
(eq y y') || (assoc_in_range eq y tl)
| [] ->
false
let rec assoc_append_replace_snd l v = function
[] -> l
| (v', _) :: tl -> (v', v) :: (assoc_append_replace_snd l v tl)
let rec check_assoc v v' = function
[] -> v=v'
| (v1,v2)::tl ->
begin match v=v1, v'=v2 with
true, true -> true
| false, false -> check_assoc v v' tl
| _ -> false
end
let rec try_check_assoc v v' = function
[] -> raise Not_found
| (v1,v2)::tl ->
begin match v=v1, v'=v2 with
true, true -> true
| false, false -> try_check_assoc v v' tl
| _ -> false
end
let rec try_assoc v = function
[] -> v
| (v1,v2)::tl ->
if v1=v then v2 else try_assoc v tl
let rec assoc_eq eq x = function
(x', y) :: t ->
if eq x x' then
y
else
assoc_eq eq x t
| [] ->
raise Not_found