Debian Codegraph @

clib/cList.ml

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 type 'a cmp = 'a -> 'a -> int
12 type 'a eq = 'a -> 'a -> bool
13
14 include List
15
16 (** Tail-rec implementation of usual functions. This is a well-known trick used
17 in, for instance, ExtLib and Batteries. *)
18
19 type 'a cell = {
20 head : 'a;
21 mutable tail : 'a list;
•22 } [@@warning "-unused-field"]
23
24 external cast : 'a cell -> 'a list = "%identity"
25
26 (** Extensions and redefinitions of OCaml Stdlib *)
27
28 (** {6 Equality, testing} *)
29
30 let rec same_length l1 l2 = match l1, l2 with
31 | [], [] -> true
32 | _ :: l1, _ :: l2 -> same_length l1 l2
33 | ([], _ :: _) | (_ :: _, []) -> false
34
35 let rec compare cmp l1 l2 =
36 if l1 == l2 then 0 else
37 match l1,l2 with
38 | [], [] -> 0
39 | _::_, [] -> 1
40 | [], _::_ -> -1
41 | x1::l1, x2::l2 ->
42 match cmp x1 x2 with
43 | 0 -> compare cmp l1 l2
44 | c -> c
45
46 let rec equal cmp l1 l2 =
47 l1 == l2 ||
48 match l1, l2 with
49 | [], [] -> true
50 | x1 :: l1, x2 :: l2 -> cmp x1 x2 && equal cmp l1 l2
51 | _ -> false
52
53 let is_empty = function
54 | [] -> true
55 | _ -> false
56
57 let mem_f cmp x l =
58 List.exists (cmp x) l
59
60 let for_all_i p =
61 let rec for_all_p i = function
62 | [] -> true
63 | a::l -> p i a && for_all_p (i+1) l
64 in
65 for_all_p
66
67 let for_all2eq f l1 l2 =
68 try List.for_all2 f l1 l2 with Invalid_argument _ -> false
69
70 let exists_i p =
71 let rec exists_p i = function
72 | [] -> false
73 | a::l -> p i a || exists_p (i+1) l
74 in
75 exists_p
76
77 let prefix_of cmp prefl l =
78 let rec prefrec = function
79 | (h1::t1, h2::t2) -> cmp h1 h2 && prefrec (t1,t2)
80 | ([], _) -> true
81 | _ -> false
82 in
83 prefrec (prefl,l)
84
85 (** {6 Creating lists} *)
86
87 let interval n m =
88 let rec interval_n (l,m) =
89 if n > m then l else interval_n (m::l, pred m)
90 in
91 interval_n ([], m)
92
93 let addn n v =
94 let rec aux n l =
95 if Int.equal n 0 then l
96 else aux (pred n) (v :: l)
97 in
98 if n < 0 then invalid_arg "List.addn"
99 else aux n
100
101 let make n v =
102 addn n v []
103
104 let rec init_loop len f p i =
105 if Int.equal i len then ()
106 else
107 let c = { head = f i; tail = [] } in
108 p.tail <- cast c;
109 init_loop len f c (succ i)
110
111 let init len f =
112 if len < 0 then invalid_arg "List.init"
113 else if Int.equal len 0 then []
114 else
115 let c = { head = f 0; tail = [] } in
116 init_loop len f c 1;
117 cast c
118
119 let rec append_loop p tl = function
120 | [] -> p.tail <- tl
121 | x :: l ->
122 let c = { head = x; tail = [] } in
123 p.tail <- cast c;
124 append_loop c tl l
125
126 let append l1 l2 = match l1 with
127 | [] -> l2
128 | x :: l ->
129 let c = { head = x; tail = [] } in
130 append_loop c l2 l;
131 cast c
132
133 let rec copy p = function
134 | [] -> p
135 | x :: l ->
136 let c = { head = x; tail = [] } in
137 p.tail <- cast c;
138 copy c l
139
140 let rec concat_loop p = function
141 | [] -> ()
142 | x :: l -> concat_loop (copy p x) l
143
144 let concat l =
145 let dummy = { head = Obj.magic 0; tail = [] } in
146 concat_loop dummy l;
147 dummy.tail
148
149 let flatten = concat
150
151 (** {6 Lists as arrays} *)
152
153 let assign l n e =
154 let rec assrec stk l i = match l, i with
155 | (h :: t, 0) -> List.rev_append stk (e :: t)
156 | (h :: t, n) -> assrec (h :: stk) t (pred n)
157 | ([], _) -> failwith "List.assign"
158 in
159 assrec [] l n
160
161 (** {6 Filtering} *)
162
163 (* [filter_loop f (Some (c0,l0)) c l] will do c0.tail <- l0 if [for_all f l] *)
164 let rec filter_loop f reset p = function
165 | [] -> begin match reset with
166 | None -> ()
167 | Some (c,orig) -> c.tail <- orig
168 end
169 | x :: l as orig ->
170 if f x then
171 let c = { head = x; tail = [] } in
172 let () = p.tail <- cast c in
173 let reset = match reset with
174 | Some _ -> reset
175 | None -> Some (p,orig)
176 in
177 filter_loop f reset c l
178 else
179 filter_loop f None p l
180
181 let rec filter f = function
182 | [] -> []
183 | x :: l' as orig ->
184 if f x then
185 let c = { head = x; tail = [] } in
186 filter_loop f None c l';
187 if c.tail == l' then orig else cast c
188 else
189 filter f l'
190
191 let rec filter2_loop f p q l1 l2 = match l1, l2 with
192 | [], [] -> ()
193 | x :: l1', y :: l2' ->
194 let b = f x y in
195 filter2_loop f p q l1' l2';
196 if b then
197 if p.tail == l1' then begin
198 p.tail <- l1;
199 q.tail <- l2
200 end
201 else begin
202 p.tail <- x :: p.tail;
203 q.tail <- y :: q.tail
204 end
205 | _ -> invalid_arg "List.filter2"
206
207 let rec filter2 f l1 l2 = match l1, l2 with
208 | [], [] -> ([],[])
209 | x1 :: l1', x2 :: l2' ->
210 let b = f x1 x2 in
211 if b then
212 let c1 = { head = x1; tail = [] } in
213 let c2 = { head = x2; tail = [] } in
214 filter2_loop f c1 c2 l1' l2';
215 if c1.tail == l1' then (l1, l2) else (cast c1, cast c2)
216 else
217 filter2 f l1' l2'
218 | _ -> invalid_arg "List.filter2"
219
220 let filteri p =
221 let rec filter_i_rec i = function
222 | [] -> []
223 | x :: l -> let l' = filter_i_rec (succ i) l in if p i x then x :: l' else l'
224 in
225 filter_i_rec 0
226
227 let rec filter_with_loop filter p l = match filter, l with
228 | [], [] -> ()
229 | b :: filter, x :: l' ->
230 filter_with_loop filter p l';
231 if b then if p.tail == l' then p.tail <- l else p.tail <- x :: p.tail
232 | _ -> invalid_arg "List.filter_with"
233
234 let rec filter_with filter l = match filter, l with
235 | [], [] -> []
236 | b :: filter, x :: l' ->
237 if b then
238 let c = { head = x; tail = [] } in
239 filter_with_loop filter c l';
240 if c.tail == l' then l else cast c
241 else filter_with filter l'
242 | _ -> invalid_arg "List.filter_with"
243
244 let rec map_filter_loop f p = function
245 | [] -> ()
246 | x :: l ->
247 match f x with
248 | None -> map_filter_loop f p l
249 | Some y ->
250 let c = { head = y; tail = [] } in
251 p.tail <- cast c;
252 map_filter_loop f c l
253
254 let rec map_filter f = function
255 | [] -> []
256 | x :: l' ->
257 match f x with
258 | None -> map_filter f l'
259 | Some y ->
260 let c = { head = y; tail = [] } in
261 map_filter_loop f c l';
262 cast c
263
264 let rec map_filter_i_loop f i p = function
265 | [] -> ()
266 | x :: l ->
267 match f i x with
268 | None -> map_filter_i_loop f (succ i) p l
269 | Some y ->
270 let c = { head = y; tail = [] } in
271 p.tail <- cast c;
272 map_filter_i_loop f (succ i) c l
273
274 let rec map_filter_i_loop' f i = function
275 | [] -> []
276 | x :: l' ->
277 match f i x with
278 | None -> map_filter_i_loop' f (succ i) l'
279 | Some y ->
280 let c = { head = y; tail = [] } in
281 map_filter_i_loop f (succ i) c l';
282 cast c
283
284 let map_filter_i f l =
285 map_filter_i_loop' f 0 l
286
287 let partitioni p =
288 let rec aux i = function
289 | [] -> [], []
290 | x :: l ->
291 let (l1, l2) = aux (succ i) l in
292 if p i x then (x :: l1, l2)
293 else (l1, x :: l2)
294 in
295 aux 0
296
297 (** {6 Applying functorially} *)
298
299 let rec map_loop f p = function
300 | [] -> ()
301 | x :: l ->
302 let c = { head = f x; tail = [] } in
303 p.tail <- cast c;
304 map_loop f c l
305
306 let map f = function
307 | [] -> []
308 | x :: l ->
309 let c = { head = f x; tail = [] } in
310 map_loop f c l;
311 cast c
312
313 let rec map2_loop f p l1 l2 = match l1, l2 with
314 | [], [] -> ()
315 | x :: l1, y :: l2 ->
316 let c = { head = f x y; tail = [] } in
317 p.tail <- cast c;
318 map2_loop f c l1 l2
319 | _ -> invalid_arg "List.map2"
320
321 let map2 f l1 l2 = match l1, l2 with
322 | [], [] -> []
323 | x :: l1, y :: l2 ->
324 let c = { head = f x y; tail = [] } in
325 map2_loop f c l1 l2;
326 cast c
327 | _ -> invalid_arg "List.map2"
328
329 (* remove when requiring OCaml >= 5.1.0 *)
330 let rec concat_map_loop f p = function
331 | [] -> ()
332 | x :: l -> concat_map_loop f (copy p (f x)) l
333
334 (* remove when requiring OCaml >= 5.1.0 *)
335 let concat_map f l =
336 let dummy = { head = Obj.magic 0; tail = [] } in
337 concat_map_loop f dummy l;
338 dummy.tail
339
340 (** Like OCaml [List.mapi] but tail-recursive *)
341
342 let rec map_i_loop f i p = function
343 | [] -> ()
344 | x :: l ->
345 let c = { head = f i x; tail = [] } in
346 p.tail <- cast c;
347 map_i_loop f (succ i) c l
348
349 let map_i f i = function
350 | [] -> []
351 | x :: l ->
352 let c = { head = f i x; tail = [] } in
353 map_i_loop f (succ i) c l;
354 cast c
355
356 let map_left = map
357
358 let map2_i f i l1 l2 =
359 let rec map_i i = function
360 | ([], []) -> []
361 | (h1 :: t1, h2 :: t2) -> let v = f i h1 h2 in v :: map_i (succ i) (t1,t2)
362 | (_, _) -> invalid_arg "map2_i"
363 in
364 map_i i (l1,l2)
365
366 let rec map3_loop f p l1 l2 l3 = match l1, l2, l3 with
367 | [], [], [] -> ()
368 | x :: l1, y :: l2, z :: l3 ->
369 let c = { head = f x y z; tail = [] } in
370 p.tail <- cast c;
371 map3_loop f c l1 l2 l3
372 | _ -> invalid_arg "List.map3"
373
374 let map3 f l1 l2 l3 = match l1, l2, l3 with
375 | [], [], [] -> []
376 | x :: l1, y :: l2, z :: l3 ->
377 let c = { head = f x y z; tail = [] } in
378 map3_loop f c l1 l2 l3;
379 cast c
380 | _ -> invalid_arg "List.map3"
381
382 let rec map4_loop f p l1 l2 l3 l4 = match l1, l2, l3, l4 with
383 | [], [], [], [] -> ()
384 | x :: l1, y :: l2, z :: l3, t :: l4 ->
385 let c = { head = f x y z t; tail = [] } in
386 p.tail <- cast c;
387 map4_loop f c l1 l2 l3 l4
388 | _ -> invalid_arg "List.map4"
389
390 let map4 f l1 l2 l3 l4 = match l1, l2, l3, l4 with
391 | [], [], [], [] -> []
392 | x :: l1, y :: l2, z :: l3, t :: l4 ->
393 let c = { head = f x y z t; tail = [] } in
394 map4_loop f c l1 l2 l3 l4;
395 cast c
396 | _ -> invalid_arg "List.map4"
397
398 let rec map_until_loop f p = function
399 | [] -> []
400 | x :: l as l' ->
401 match f x with
402 | None -> l'
403 | Some fx ->
404 let c = { head = fx; tail = [] } in
405 p.tail <- cast c;
406 map_until_loop f c l
407
408 let map_until f = function
409 | [] -> [], []
410 | x :: l as l' ->
411 match f x with
412 | None -> [], l'
413 | Some fx ->
414 let c = { head = fx; tail = [] } in
415 let l = map_until_loop f c l in
416 cast c, l
417
418 let rec map_of_array_loop f p a i l =
419 if Int.equal i l then ()
420 else
421 let c = { head = f (Array.unsafe_get a i); tail = [] } in
422 p.tail <- cast c;
423 map_of_array_loop f c a (i + 1) l
424
425 let map_of_array f a =
426 let l = Array.length a in
427 if Int.equal l 0 then []
428 else
429 let c = { head = f (Array.unsafe_get a 0); tail = [] } in
430 map_of_array_loop f c a 1 l;
431 cast c
432
433 let map_append f l = flatten (map f l)
434
435 let map_append2 f l1 l2 = flatten (map2 f l1 l2)
436
437 let rec extend l a l' = match l,l' with
438 | true :: l, b :: l' -> b :: extend l a l'
439 | false :: l, l' -> a :: extend l a l'
440 | [], [] -> []
441 | _ -> invalid_arg "extend"
442
443 let count f l =
444 let rec aux acc = function
445 | [] -> acc
446 | h :: t -> if f h then aux (acc + 1) t else aux acc t
447 in
448 aux 0 l
449
450 (** {6 Finding position} *)
451
452 let rec index_f f x l n = match l with
453 | [] -> raise Not_found
454 | y :: l -> if f x y then n else index_f f x l (succ n)
455
456 let index f x l = index_f f x l 1
457
458 let index_opt f x l = try Some (index f x l) with Not_found -> None
459
460 let index0 f x l = index_f f x l 0
461
462 (** {6 Folding} *)
463
464 let fold_left_error f accu s =
465 let rec aux accu = function
466 | [] -> Ok accu
467 | x :: xs -> match f accu x with Error _ as x -> x | Ok i -> aux i xs
468 in
469 aux accu s
470
471 let fold_left_until f accu s =
472 let rec aux accu = function
473 | [] -> accu
474 | x :: xs -> match f accu x with CSig.Stop x -> x | CSig.Cont i -> aux i xs
475 in
476 aux accu s
477
478 let fold_right_i f i l =
479 let rec it_f i l a = match l with
480 | [] -> a
481 | b :: l -> f (i-1) b (it_f (i-1) l a)
482 in
483 it_f (List.length l + i) l
484
485 let fold_left_i f =
486 let rec it_list_f i a = function
487 | [] -> a
488 | b :: l -> it_list_f (i+1) (f i a b) l
489 in
490 it_list_f
491
492 let rec fold_left3 f accu l1 l2 l3 =
493 match (l1, l2, l3) with
494 | ([], [], []) -> accu
495 | (a1 :: l1, a2 :: l2, a3 :: l3) -> fold_left3 f (f accu a1 a2 a3) l1 l2 l3
496 | (_, _, _) -> invalid_arg "List.fold_left3"
497
498 let rec fold_left4 f accu l1 l2 l3 l4 =
499 match (l1, l2, l3, l4) with
500 | ([], [], [], []) -> accu
501 | (a1 :: l1, a2 :: l2, a3 :: l3, a4 :: l4) -> fold_left4 f (f accu a1 a2 a3 a4) l1 l2 l3 l4
502 | (_, _, _, _) -> invalid_arg "List.fold_left4"
503
504 let rec fold_left5 f accu l1 l2 l3 l4 l5 =
505 match (l1, l2, l3, l4, l5) with
506 | ([], [], [], [], []) -> accu
507 | (a1 :: l1, a2 :: l2, a3 :: l3, a4 :: l4, a5 :: l5) -> fold_left5 f (f accu a1 a2 a3 a4 a5) l1 l2 l3 l4 l5
508 | (_, _, _, _, _) -> invalid_arg "List.fold_left5"
509
510 (* [fold_right_and_left f [a1;...;an] hd =
511 f (f (... (f (f hd
512 an
513 [an-1;...;a1])
514 an-1
515 [an-2;...;a1])
516 ...)
517 a2
518 [a1])
519 a1
520 []] *)
521
522 let fold_right_and_left f l hd =
523 let rec aux tl = function
524 | [] -> hd
525 | a :: l -> let hd = aux (a :: tl) l in f hd a tl
526 in
527 aux [] l
528
529 (* Match sets as lists according to a matching function, also folding a side effect *)
530 let rec fold_left2_set e f x l1 l2 =
531 match l1 with
532 | a1 :: l1 ->
533 let rec find seen = function
534 | [] -> raise e
535 | a2 :: l2 ->
536 try fold_left2_set e f (f x a1 a2 l1 l2) l1 (List.rev_append seen l2)
537 with e' when e' = e -> find (a2 :: seen) l2 in
538 find [] l2
539 | [] ->
540 if l2 = [] then x else raise e
541
542 (* Poor man's monadic map *)
543 let rec fold_left_map f e = function
544 | [] -> (e,[])
545 | h :: t ->
546 let e',h' = f e h in
547 let e'',t' = fold_left_map f e' t in
548 e'',h' :: t'
549
550 (* (* tail-recursive version of the above function *)
551 let fold_left_map f e l =
552 let g (e,b') h =
553 let (e',h') = f e h in
554 (e',h'::b')
555 in
556 let (e',lrev) = List.fold_left g (e,[]) l in
557 (e',List.rev lrev)
558 *)
559
560 (* The same, based on fold_right, with the effect accumulated on the right *)
561 let fold_right_map f l e =
562 List.fold_right (fun x (l,e) -> let (y,e) = f x e in (y::l,e)) l ([],e)
563
564 let on_snd f (x,y) = (x,f y)
565
566 let fold_left2_map f e l l' =
567 on_snd List.rev @@
568 List.fold_left2 (fun (e,l) x x' ->
569 let (e,y) = f e x x' in
570 (e, y::l)
571 ) (e, []) l l'
572
573 let fold_right2_map f l l' e =
574 List.fold_right2 (fun x x' (l,e) -> let (y,e) = f x x' e in (y::l,e)) l l' ([],e)
575
576 let fold_left3_map f e l l' l'' =
577 on_snd List.rev @@
578 fold_left3 (fun (e,l) x x' x'' -> let (e,y) = f e x x' x'' in (e,y::l)) (e,[]) l l' l''
579
580 let fold_left4_map f e l1 l2 l3 l4 =
581 on_snd List.rev @@
582 fold_left4 (fun (e,l) x1 x2 x3 x4 -> let (e,y) = f e x1 x2 x3 x4 in (e,y::l)) (e,[]) l1 l2 l3 l4
583
584 let fold_left5_map f e l1 l2 l3 l4 l5 =
585 on_snd List.rev @@
586 fold_left5 (fun (e,l) x1 x2 x3 x4 x5 -> let (e,y) = f e x1 x2 x3 x4 x5 in (e,y::l)) (e,[]) l1 l2 l3 l4 l5
587
588 (** {6 Splitting} *)
589
590 let remove cmp x l =
591 List.filter (fun y -> not (cmp x y)) l
592
593 let rec remove_first p = function
594 | b :: l when p b -> l
595 | b :: l -> b :: remove_first p l
596 | [] -> raise Not_found
597
598 let extract_first p li =
599 let rec loop rev_left = function
600 | [] -> raise Not_found
601 | x :: right ->
602 if p x then List.rev_append rev_left right, x
603 else loop (x :: rev_left) right
604 in
605 loop [] li
606
607 let insert p v l =
608 let rec insrec = function
609 | [] -> [v]
610 | h :: tl -> if p v h then v :: h :: tl else h :: insrec tl
611 in
612 insrec l
613
614 let find_map_exn f l =
615 match find_map f l with
616 | Some v -> v
617 | None -> raise Not_found
618
619 (* FIXME: again, generic hash function *)
620
621 let subset l1 l2 =
622 let t2 = Hashtbl.create 151 in
623 List.iter (fun x -> Hashtbl.add t2 x ()) l2;
624 let rec look = function
625 | [] -> true
626 | x :: ll -> try Hashtbl.find t2 x; look ll with Not_found -> false
627 in
628 look l1
629
630 (** [goto i l] splits [l] into two lists [(l1,l2)] such that
631 [(List.rev l1)++l2=l] and [l1] has length [i]. It raises
632 [IndexOutOfRange] when [i] is negative or greater than the
633 length of [l]. *)
634 exception IndexOutOfRange
635 let goto n l =
636 let rec goto i acc = function
637 | tl when Int.equal i 0 -> (acc, tl)
638 | h :: t -> goto (pred i) (h :: acc) t
639 | [] -> raise IndexOutOfRange
640 in
641 goto n [] l
642
643 (* [chop i l] splits [l] into two lists [(l1,l2)] such that
644 [l1++l2=l] and [l1] has length [i].
645 It raises [Failure] when [i] is negative or greater than the length of [l] *)
646
647 let chop n l =
648 try let (h,t) = goto n l in (List.rev h,t)
649 with IndexOutOfRange -> failwith "List.chop"
650 (* spiwack: should raise [IndexOutOfRange] but I'm afraid of missing
651 a try/with when replacing the exception. *)
652
653 (* [split_when p l] splits [l] into two lists [(l1,a::l2)] such that
654 [l1++(a::l2)=l], [p a=true] and [p b = false] for every element [b] of [l1].
655 If there is no such [a], then it returns [(l,[])] instead *)
656 let split_when p =
657 let rec split_when_loop x y =
658 match y with
659 | [] -> (List.rev x,[])
660 | (a :: l) -> if (p a) then (List.rev x,y) else split_when_loop (a :: x) l
661 in
662 split_when_loop []
663
664 let firstn n ol =
665 let rec aux acc n l =
666 match n, l with
667 | 0, [] -> ol
668 | 0, _ :: _ -> List.rev acc
669 | n, h :: t -> aux (h :: acc) (pred n) t
670 | _ -> failwith "firstn"
671 in
672 aux [] n ol
673
674 let sep_first = function
675 | [] -> failwith "sep_first"
676 | hd :: tl -> (hd, tl)
677
678 let rec sep_last = function
679 | [] -> failwith "sep_last"
680 | hd :: [] -> (hd,[])
681 | hd :: tl -> let (l,tl) = sep_last tl in (l,hd :: tl)
682
683 (* Drop the last element of a list *)
684
685 let rec drop_last = function
686 | [] -> failwith "drop_last"
687 | hd :: [] -> []
688 | hd :: tl -> hd :: drop_last tl
689
690 let rec last = function
691 | [] -> failwith "List.last"
692 | hd :: [] -> hd
693 | _ :: tl -> last tl
694
695 let lastn n l =
696 let len = List.length l in
697 let rec aux m l =
698 if Int.equal m n then l else aux (m - 1) (List.tl l)
699 in
700 if len < n then failwith "lastn" else aux len l
701
702 let rec skipn n l = match n,l with
703 | 0, _ -> l
704 | _, [] -> failwith "List.skipn"
705 | n, _ :: l -> skipn (pred n) l
706
707 let skipn_at_best n l =
708 try skipn n l with Failure _ when n >= 0 -> []
709
710 (** if [l=p++t] then [drop_prefix p l] is [t] else [l] *)
711
712 let drop_prefix cmp p l =
713 let rec drop_prefix_rec = function
714 | (h1 :: tp, h2 :: tl) when cmp h1 h2 -> drop_prefix_rec (tp,tl)
715 | ([], tl) -> tl
716 | _ -> l
717 in
718 drop_prefix_rec (p,l)
719
720 let share_tails eq l1 l2 =
721 let rec shr_rev acc = function
722 | (x1 :: l1, x2 :: l2) when eq x1 x2 -> shr_rev (x1 :: acc) (l1,l2)
723 | (l1, l2) -> (List.rev l1, List.rev l2, acc)
724 in
725 shr_rev [] (List.rev l1, List.rev l2)
726
727 (** {6 Association lists} *)
728
729 let map_assoc f = map (fun (x,a) -> (x,f a))
730
731 let rec assoc_f f a = function
732 | (x, e) :: xs -> if f a x then e else assoc_f f a xs
733 | [] -> raise Not_found
734
735 let rec assoc_f_opt f a = function
736 | (x, e) :: xs -> if f a x then Some e else assoc_f_opt f a xs
737 | [] -> None
738
739 let remove_assoc_f f a l =
740 try remove_first (fun (x,_) -> f a x) l with Not_found -> l
741
742 let mem_assoc_f f a l = List.exists (fun (x,_) -> f a x) l
743
744 (** {6 Operations on lists of tuples} *)
745
746 let rec split_loop p q = function
747 | [] -> ()
748 | (x, y) :: l ->
749 let cl = { head = x; tail = [] } in
750 let cr = { head = y; tail = [] } in
751 p.tail <- cast cl;
752 q.tail <- cast cr;
753 split_loop cl cr l
754
755 let split = function
756 | [] -> [], []
757 | (x, y) :: l ->
758 let cl = { head = x; tail = [] } in
759 let cr = { head = y; tail = [] } in
760 split_loop cl cr l;
761 (cast cl, cast cr)
762
763 let rec combine_loop p l1 l2 = match l1, l2 with
764 | [], [] -> ()
765 | x :: l1, y :: l2 ->
766 let c = { head = (x, y); tail = [] } in
767 p.tail <- cast c;
768 combine_loop c l1 l2
769 | _ -> invalid_arg "List.combine"
770
771 let combine l1 l2 = match l1, l2 with
772 | [], [] -> []
773 | x :: l1, y :: l2 ->
774 let c = { head = (x, y); tail = [] } in
775 combine_loop c l1 l2;
776 cast c
777 | _ -> invalid_arg "List.combine"
778
779 let rec split3_loop p q r = function
780 | [] -> ()
781 | (x, y, z) :: l ->
782 let cp = { head = x; tail = [] } in
783 let cq = { head = y; tail = [] } in
784 let cr = { head = z; tail = [] } in
785 p.tail <- cast cp;
786 q.tail <- cast cq;
787 r.tail <- cast cr;
788 split3_loop cp cq cr l
789
790 let split3 = function
791 | [] -> [], [], []
792 | (x, y, z) :: l ->
793 let cp = { head = x; tail = [] } in
794 let cq = { head = y; tail = [] } in
795 let cr = { head = z; tail = [] } in
796 split3_loop cp cq cr l;
797 (cast cp, cast cq, cast cr)
798
799 (** XXX TODO tailrec *)
800 let rec split4 = function
801 | [] -> ([], [], [], [])
802 | (a,b,c,d)::l ->
803 let (ra, rb, rc, rd) = split4 l in (a::ra, b::rb, c::rc, d::rd)
804
805 let rec combine3_loop p l1 l2 l3 = match l1, l2, l3 with
806 | [], [], [] -> ()
807 | x :: l1, y :: l2, z :: l3 ->
808 let c = { head = (x, y, z); tail = [] } in
809 p.tail <- cast c;
810 combine3_loop c l1 l2 l3
811 | _ -> invalid_arg "List.combine3"
812
813 let combine3 l1 l2 l3 = match l1, l2, l3 with
814 | [], [], [] -> []
815 | x :: l1, y :: l2, z :: l3 ->
816 let c = { head = (x, y, z); tail = [] } in
817 combine3_loop c l1 l2 l3;
818 cast c
819 | _ -> invalid_arg "List.combine3"
820
821 (** {6 Operations on lists seen as sets, preserving uniqueness of elements} *)
822
823 (** Add an element, preserving uniqueness of elements *)
824
825 let add_set cmp x l =
826 if mem_f cmp x l then l else x :: l
827
828 (** List equality up to permutation (but considering multiple occurrences) *)
829
830 let eq_set cmp l1 l2 =
831 let rec aux l1 = function
832 | [] -> is_empty l1
833 | a :: l2 -> aux (remove_first (cmp a) l1) l2
834 in
835 try aux l1 l2 with Not_found -> false
836
837 let rec merge_set cmp l1 l2 = match l1, l2 with
838 | [], l2 -> l2
839 | l1, [] -> l1
840 | h1 :: t1, h2 :: t2 ->
841 let c = cmp h1 h2 in
842 if Int.equal c 0
843 then h1 :: merge_set cmp t1 t2
844 else if c <= 0
845 then h1 :: merge_set cmp t1 l2
846 else h2 :: merge_set cmp l1 t2
847
848 let intersect cmp l1 l2 =
849 filter (fun x -> mem_f cmp x l2) l1
850
851 let union cmp l1 l2 =
852 let rec urec = function
853 | [] -> l2
854 | a :: l -> if mem_f cmp a l2 then urec l else a :: urec l
855 in
856 urec l1
857
858 let subtract cmp l1 l2 =
859 if is_empty l2 then l1
860 else List.filter (fun x -> not (mem_f cmp x l2)) l1
861
862 let unionq l1 l2 = union (==) l1 l2
863 let subtractq l1 l2 = subtract (==) l1 l2
864
865 (** {6 Uniqueness and duplication} *)
866
867 (* FIXME: we should avoid relying on the generic hash function,
868 just as we'd better avoid Pervasives.compare *)
869
870 let distinct l =
871 let visited = Hashtbl.create 23 in
872 let rec loop = function
873 | h :: t ->
874 if Hashtbl.mem visited h then false
875 else
876 begin
877 Hashtbl.add visited h h;
878 loop t
879 end
880 | [] -> true
881 in
882 loop l
883
884 let distinct_f cmp l =
885 let rec loop = function
886 | a :: b :: _ when Int.equal (cmp a b) 0 -> false
887 | a :: l -> loop l
888 | [] -> true
889 in loop (List.sort cmp l)
890
891 (* FIXME: again, generic hash function *)
892
893 let uniquize_key f l =
894 let visited = Hashtbl.create 23 in
895 let rec aux acc changed = function
896 | h :: t ->
897 let x = f h in
898 if Hashtbl.mem visited x then aux acc true t else
899 begin
900 Hashtbl.add visited x x;
901 aux (h :: acc) changed t
902 end
903 | [] -> if changed then List.rev acc else l
904 in
905 aux [] false l
906
907 let uniquize l = uniquize_key (fun x -> x) l
908
909 (** [sort_uniquize] might be an alternative to the hashtbl-based
910 [uniquize], when the order of the elements is irrelevant *)
911
912 let rec uniquize_sorted cmp = function
913 | a :: b :: l when Int.equal (cmp a b) 0 -> uniquize_sorted cmp (a :: l)
914 | a :: l -> a :: uniquize_sorted cmp l
915 | [] -> []
916
917 let sort_uniquize cmp l =
918 uniquize_sorted cmp (List.sort cmp l)
919
920 let min cmp l =
921 let rec aux cur = function
922 | [] -> cur
923 | x :: l -> if cmp x cur < 0 then aux x l else aux cur l
924 in
925 match l with
926 | x :: l -> aux x l
927 | [] -> raise Not_found
928
929 let rec duplicates cmp = function
930 | [] -> []
931 | x :: l ->
932 let l' = duplicates cmp l in
933 if mem_f cmp x l then add_set cmp x l' else l'
934
935 (** {6 Cartesian product} *)
936
937 (* A generic cartesian product: for any operator (**),
938 [cartesian (**) [x1;x2] [y1;y2] = [x1**y1; x1**y2; x2**y1; x2**y1]],
939 and so on if there are more elements in the lists. *)
940
941 let cartesian op l1 l2 =
942 map_append (fun x -> map (op x) l2) l1
943
944 (* [cartesians] is an n-ary cartesian product: it iterates
945 [cartesian] over a list of lists. *)
946
947 let cartesians op init ll =
948 List.fold_right (cartesian op) ll [init]
949
950 (* combinations [[a;b];[c;d]] gives [[a;c];[a;d];[b;c];[b;d]] *)
951
952 let combinations l =
953 cartesians (fun x l -> x :: l) [] l
954
955 (* Keep only those products that do not return None *)
956
957 let cartesian_filter op l1 l2 =
958 map_append (fun x -> map_filter (op x) l2) l1
959
960 (* Keep only those products that do not return None *)
961
962 let cartesians_filter op init ll =
963 List.fold_right (cartesian_filter op) ll [init]
964
965 (* Factorize lists of pairs according to the left argument *)
966 let rec factorize_left cmp = function
967 | (a,b) :: l ->
968 let al,l' = partition (fun (a',_) -> cmp a a') l in
969 (a,(b :: map snd al)) :: factorize_left cmp l'
970 | [] -> []
971
972 module Smart =
973 struct
974
975 let rec map f l = match l with
976 | [] -> l
977 | h :: tl ->
978 let h' = f h in
979 let tl' = map f tl in
980 if h' == h && tl' == tl then l else h' :: tl'
981
982 let rec fold_left_map f e l =
983 match l with
984 | [] -> e, []
985 | h :: tl ->
986 let e', h' = f e h in
987 let e'', tl' = fold_left_map f e' tl in
988 e'', (if h' == h && tl' == tl then l else h' :: tl')
989
990 let rec fold_right_map f l e =
991 match l with
992 | [] -> [], e
993 | h :: tl ->
994 let tl', e' = fold_right_map f tl e in
995 let h', e'' = f h e' in
996 (if h' == h && tl' == tl then l else h' :: tl'), e''
997
998 end
999
1000 module type MonoS = sig
1001 type elt
1002 val equal : elt list -> elt list -> bool
1003 val mem : elt -> elt list -> bool
1004 val assoc : elt -> (elt * 'a) list -> 'a
1005 val mem_assoc : elt -> (elt * 'a) list -> bool
1006 val remove_assoc : elt -> (elt * 'a) list -> (elt * 'a) list
1007 val mem_assoc_sym : elt -> ('a * elt) list -> bool
1008 end