Debian Codegraph @

clib/cList.mli

raw symbols SCIP
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 module type of List
15
16 (** {6 Equality, testing} *)
17
18 val compare : 'a cmp -> 'a list cmp
19 (** Lexicographic order on lists. *)
20
21 val equal : 'a eq -> 'a list eq
22 (** Lift equality to list type. *)
23
24 val is_empty : 'a list -> bool
25 (** Check whether a list is empty *)
26
27 val mem_f : 'a eq -> 'a -> 'a list -> bool
28 (** Same as [List.mem], for some specific equality *)
29
30 val for_all_i : (int -> 'a -> bool) -> int -> 'a list -> bool
31 (** Same as [List.for_all] but with an index *)
32
33 val for_all2eq : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool
34 (** Same as [List.for_all2] but returning [false] when of different length *)
35
36 val exists_i : (int -> 'a -> bool) -> int -> 'a list -> bool
37 (** Same as [List.exists] but with an index *)
38
39 val prefix_of : 'a eq -> 'a list eq
40 (** [prefix_of eq l1 l2] returns [true] if [l1] is a prefix of [l2], [false]
41 otherwise. It uses [eq] to compare elements *)
42
43 val same_length : 'a list -> 'b list -> bool
44 (** A more efficient variant of [for_all2eq (fun _ _ -> true)] *)
45
46 (** {6 Creating lists} *)
47
48 val interval : int -> int -> int list
49 (** [interval i j] creates the list [[i; i + 1; ...; j]], or [[]] when
50 [j < i]. *)
51
52 val make : int -> 'a -> 'a list
53 (** [make n x] returns a list made of [n] times [x]. Raise
54 [Invalid_argument _] if [n] is negative. *)
55
56 val addn : int -> 'a -> 'a list -> 'a list
57 (** [addn n x l] adds [n] times [x] on the left of [l]. *)
58
59 val init : int -> (int -> 'a) -> 'a list
60 (** [init n f] constructs the list [f 0; ... ; f (n - 1)]. Raise
61 [Invalid_argument _] if [n] is negative *)
62
63 val append : 'a list -> 'a list -> 'a list
64 (** Like OCaml's [List.append] but tail-recursive. *)
65
66 val concat : 'a list list -> 'a list
67 (** Like OCaml's [List.concat] but tail-recursive. *)
68
69 val flatten : 'a list list -> 'a list
70 (** Synonymous of [concat] *)
71
72 (** {6 Lists as arrays} *)
73
74 val assign : 'a list -> int -> 'a -> 'a list
75 (** [assign l i x] sets the [i]-th element of [l] to [x], starting
76 from [0]. Raise [Failure _] if [i] is out of range. *)
77
78 (** {6 Filtering} *)
79
80 val filter : ('a -> bool) -> 'a list -> 'a list
81 (** Like OCaml [List.filter] but tail-recursive and physically returns
82 the original list if the predicate holds for all elements. *)
83
84 val filter2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> 'a list * 'b list
85 (** Like [List.filter] but with 2 arguments, raise [Invalid_argument _]
86 if the lists are not of same length. *)
87
88 val filteri : (int -> 'a -> bool) -> 'a list -> 'a list
89 (** Like [List.filter] but with an index starting from [0] *)
90
91 val filter_with : bool list -> 'a list -> 'a list
92 (** [filter_with bl l] selects elements of [l] whose corresponding element in
93 [bl] is [true]. Raise [Invalid_argument _] if sizes differ. *)
94
95 val map_filter : ('a -> 'b option) -> 'a list -> 'b list
96 (** Like [map] but keeping only non-[None] elements *)
97
98 val map_filter_i : (int -> 'a -> 'b option) -> 'a list -> 'b list
99 (** Like [map_filter] but with an index starting from [0] *)
100
101 val partitioni : (int -> 'a -> bool) -> 'a list -> 'a list * 'a list
102 (** Like [List.partition] but with an index starting from [0] *)
103
104 (** {6 Applying functorially} *)
105
106 val map : ('a -> 'b) -> 'a list -> 'b list
107 (** Like OCaml [List.map] but tail-recursive *)
108
109 val map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
110 (** Like OCaml [List.map2] but tail-recursive *)
111
112 val map_left : ('a -> 'b) -> 'a list -> 'b list
113 (** As [map] but ensures the left-to-right order of evaluation. *)
114
115 val concat_map : ('a -> 'b list) -> 'a list -> 'b list
116 (** Like OCaml [List.concat_map] but tail-recursive. Alternatively,
117 the composition of [concat] and [map] *)
118
119 val map_i : (int -> 'a -> 'b) -> int -> 'a list -> 'b list
120 (** Like OCaml [List.mapi] but tail-recursive. Alternatively, like
121 [map] but with an index *)
122
123 val map2_i :
124 (int -> 'a -> 'b -> 'c) -> int -> 'a list -> 'b list -> 'c list
125 (** Like [map2] but with an index *)
126
127 val map3 :
128 ('a -> 'b -> 'c -> 'd) -> 'a list -> 'b list -> 'c list -> 'd list
129 (** Like [map] but for 3 lists. *)
130
131 val map4 : ('a -> 'b -> 'c -> 'd -> 'e) -> 'a list -> 'b list -> 'c list ->
132 'd list -> 'e list
133 (** Like [map] but for 4 lists. *)
134
135 val map_until : ('a -> 'b option) -> 'a list -> 'b list * 'a list
136 (** [map_until f l] applies f to the elements of l until one returns None,
137 then returns the list of elements where f was applied
138 and the tail where f was not applied *)
139
140 val map_of_array : ('a -> 'b) -> 'a array -> 'b list
141 (** [map_of_array f a] behaves as [List.map f (Array.to_list a)] *)
142
143 val map_append : ('a -> 'b list) -> 'a list -> 'b list
144 (** [map_append f [x1; ...; xn]] returns [f x1 @ ... @ f xn]. *)
145
146 val map_append2 : ('a -> 'b -> 'c list) -> 'a list -> 'b list -> 'c list
147 (** Like [map_append] but for two lists; raises [Invalid_argument _]
148 if the two lists do not have the same length. *)
149
150 val extend : bool list -> 'a -> 'a list -> 'a list
151 (** [extend l a [a1..an]] assumes that the number of [true] in [l] is [n];
152 it extends [a1..an] by inserting [a] at the position of [false] in [l] *)
153
154 val count : ('a -> bool) -> 'a list -> int
155 (** Count the number of elements satisfying a predicate *)
156
157 (** {6 Finding position} *)
158
159 val index : 'a eq -> 'a -> 'a list -> int
160 (** [index] returns the 1st index of an element in a list (counting from 1). *)
161
162 val index_opt : 'a eq -> 'a -> 'a list -> int option
163 (** [index_opt] returns the 1st index of an element in a list (counting from 1)
164 and None otherwise. *)
165
166 val index0 : 'a eq -> 'a -> 'a list -> int
167 (** [index0] behaves as [index] except that it starts counting at 0. *)
168
169 (** {6 Folding} *)
170
171 val fold_left_error : ('c -> 'a -> ('c,'e) result) -> 'c -> 'a list -> ('c,'e) result
172 (** acts like [Ok (fold_left f acc s)] while [f] returns
173 [Ok acc']; it stops returning [Error c] as soon as [f] returns [Error c]. *)
174
175 val fold_left_until : ('c -> 'a -> 'c CSig.until) -> 'c -> 'a list -> 'c
176 (** acts like [fold_left f acc s] while [f] returns
177 [Cont acc']; it stops returning [c] as soon as [f] returns [Stop c]. *)
178
179 val fold_right_i : (int -> 'a -> 'b -> 'b) -> int -> 'a list -> 'b -> 'b
180 (** Like [List.fold_right] but with an index *)
181
182 val fold_left_i : (int -> 'a -> 'b -> 'a) -> int -> 'a -> 'b list -> 'a
183 (** Like [List.fold_left] but with an index *)
184
185 val fold_right_and_left : ('b -> 'a -> 'a list -> 'b) -> 'a list -> 'b -> 'b
186 (** [fold_right_and_left f [a1;...;an] hd] is
187 [f (f (... (f (f hd an [an-1;...;a1]) an-1 [an-2;...;a1]) ...) a2 [a1]) a1 []] *)
188
189 val fold_left3 : ('a -> 'b -> 'c -> 'd -> 'a) -> 'a -> 'b list -> 'c list -> 'd list -> 'a
190 (** Like [List.fold_left] but for 3 lists; raise [Invalid_argument _] if
191 not all lists of the same size *)
192
193 val fold_left4 : ('a -> 'b -> 'c -> 'd -> 'e -> 'a) -> 'a -> 'b list -> 'c list -> 'd list -> 'e list -> 'a
194 (** Like [List.fold_left] but for 4 lists; raise [Invalid_argument _] if
195 not all lists of the same size *)
196
197 val fold_left2_set : exn -> ('a -> 'b -> 'c -> 'b list -> 'c list -> 'a) -> 'a -> 'b list -> 'c list -> 'a
198 (** Fold sets, i.e. lists up to order; the folding function tells
199 when elements match by returning a value and raising the given
200 exception otherwise; sets should have the same size; raise the
201 given exception if no pairing of the two sets is found;;
202 complexity in O(n^2) *)
203
204 val fold_left_map : ('a -> 'b -> 'a * 'c) -> 'a -> 'b list -> 'a * 'c list
205 (** [fold_left_map f e_0 [a1;...;an]] is [e_n,[k_1...k_n]]
206 where [(e_i,k_i)] is [f e_{i-1} ai] for each i<=n *)
207
208 val fold_right_map : ('b -> 'a -> 'c * 'a) -> 'b list -> 'a -> 'c list * 'a
209 (** Same, folding on the right *)
210
211 val fold_left2_map : ('a -> 'b -> 'c -> 'a * 'd) -> 'a -> 'b list -> 'c list -> 'a * 'd list
212 (** Same with two lists, folding on the left *)
213
214 val fold_right2_map : ('b -> 'c -> 'a -> 'd * 'a) -> 'b list -> 'c list -> 'a -> 'd list * 'a
215 (** Same with two lists, folding on the right *)
216
217 val fold_left3_map : ('a -> 'b -> 'c -> 'd -> 'a * 'e) -> 'a -> 'b list -> 'c list -> 'd list -> 'a * 'e list
218 (** Same with three lists, folding on the left *)
219
220 val fold_left4_map : ('a -> 'b -> 'c -> 'd -> 'e -> 'a * 'r) -> 'a -> 'b list -> 'c list -> 'd list -> 'e list -> 'a * 'r list
221 (** Same with four lists, folding on the left *)
222
223 val fold_left5_map : ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'a * 'r) -> 'a -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'a * 'r list
224 (** Same with five lists, folding on the left *)
225
226 (** {6 Splitting} *)
227
228 val remove : 'a eq -> 'a -> 'a list -> 'a list
229 (** [remove eq a l] Remove all occurrences of [a] in [l] *)
230
231 val remove_first : ('a -> bool) -> 'a list -> 'a list
232 (** Remove the first element satisfying a predicate, or raise [Not_found] *)
233
234 val extract_first : ('a -> bool) -> 'a list -> 'a list * 'a
235 (** Remove and return the first element satisfying a predicate,
236 or raise [Not_found] *)
237
238 val find_map_exn : ('a -> 'b option) -> 'a list -> 'b
239 (** Like [find_map] but raises [Not_found] instead of returning [None]. *)
240
241 exception IndexOutOfRange
242 val goto: int -> 'a list -> 'a list * 'a list
243 (** [goto i l] splits [l] into two lists [(l1,l2)] such that
244 [(List.rev l1)++l2=l] and [l1] has length [i]. It raises
245 [IndexOutOfRange] when [i] is negative or greater than the
246 length of [l]. *)
247
248 val split_when : ('a -> bool) -> 'a list -> 'a list * 'a list
249 (** [split_when p l] splits [l] into two lists [(l1,a::l2)] such that
250 [l1++(a::l2)=l], [p a=true] and [p b = false] for every element [b] of [l1].
251 if there is no such [a], then it returns [(l,[])] instead. *)
252
253 val sep_first : 'a list -> 'a * 'a list
254 (** [sep_first l] returns [(a,l')] such that [l] is [a::l'].
255 It raises [Failure _] if the list is empty. *)
256
257 val sep_last : 'a list -> 'a * 'a list
258 (** [sep_last l] returns [(a,l')] such that [l] is [l'@[a]].
259 It raises [Failure _] if the list is empty. *)
260
261 val drop_last : 'a list -> 'a list
262 (** Remove the last element of the list. It raises [Failure _] if the
263 list is empty. This is the second part of [sep_last]. *)
264
265 val last : 'a list -> 'a
266 (** Return the last element of the list. It raises [Failure _] if the
267 list is empty. This is the first part of [sep_last]. *)
268
269 val lastn : int -> 'a list -> 'a list
270 (** [lastn n l] returns the [n] last elements of [l]. It raises
271 [Failure _] if [n] is less than 0 or larger than the length of [l] *)
272
273 val chop : int -> 'a list -> 'a list * 'a list
274 (** [chop i l] splits [l] into two lists [(l1,l2)] such that
275 [l1++l2=l] and [l1] has length [i]. It raises [Failure _] when
276 [i] is negative or greater than the length of [l]. *)
277
278 val firstn : int -> 'a list -> 'a list
279 (** [firstn n l] Returns the [n] first elements of [l]. It raises
280 [Failure _] if [n] negative or too large. This is the first part
281 of [chop]. *)
282
283 val skipn : int -> 'a list -> 'a list
284 (** [skipn n l] drops the [n] first elements of [l]. It raises
285 [Failure _] if [n] is less than 0 or larger than the length of [l].
286 This is the second part of [chop]. *)
287
288 val skipn_at_best : int -> 'a list -> 'a list
289 (** Same as [skipn] but returns [] if [n] is larger than the length of
290 the list. *)
291
292 val drop_prefix : 'a eq -> 'a list -> 'a list -> 'a list
293 (** [drop_prefix eq l1 l] returns [l2] if [l=l1++l2] else return [l]. *)
294
295 val insert : 'a eq -> 'a -> 'a list -> 'a list
296 (** Insert at the (first) position so that if the list is ordered wrt to the
297 total order given as argument, the order is preserved *)
298
299 val share_tails : 'a eq -> 'a list -> 'a list -> 'a list * 'a list * 'a list
300 (** [share_tails l1 l2] returns [(l1',l2',l)] such that [l1] is
301 [l1'\@l] and [l2] is [l2'\@l] and [l] is maximal amongst all such
302 decompositions *)
303
304 (** {6 Association lists} *)
305
306 val map_assoc : ('a -> 'b) -> ('c * 'a) list -> ('c * 'b) list
307 (** Applies a function on the codomain of an association list *)
308
309 val assoc_f : 'a eq -> 'a -> ('a * 'b) list -> 'b
310 (** Like [List.assoc] but using the equality given as argument *)
311
312 val assoc_f_opt : 'a eq -> 'a -> ('a * 'b) list -> 'b option
313 (** Like [List.assoc_opt] but using the equality given as argument *)
314
315 val remove_assoc_f : 'a eq -> 'a -> ('a * 'b) list -> ('a * 'b) list
316 (** Remove first matching element; unchanged if no such element *)
317
318 val mem_assoc_f : 'a eq -> 'a -> ('a * 'b) list -> bool
319 (** Like [List.mem_assoc] but using the equality given as argument *)
320
321 val factorize_left : 'a eq -> ('a * 'b) list -> ('a * 'b list) list
322 (** Create a list of associations from a list of pairs *)
323
324 (** {6 Operations on lists of tuples} *)
325
326 val split : ('a * 'b) list -> 'a list * 'b list
327 (** Like OCaml's [List.split] but tail-recursive. *)
328
329 val combine : 'a list -> 'b list -> ('a * 'b) list
330 (** Like OCaml's [List.combine] but tail-recursive. *)
331
332 val split3 : ('a * 'b * 'c) list -> 'a list * 'b list * 'c list
333 (** Like [split] but for triples *)
334
335 val split4 : ('a * 'b * 'c * 'd) list -> 'a list * 'b list * 'c list * 'd list
336 (** Like [split] but for quads *)
337
338 val combine3 : 'a list -> 'b list -> 'c list -> ('a * 'b * 'c) list
339 (** Like [combine] but for triples *)
340
341 (** {6 Operations on lists seen as sets, preserving uniqueness of elements} *)
342
343 val add_set : 'a eq -> 'a -> 'a list -> 'a list
344 (** [add_set x l] adds [x] in [l] if it is not already there, or returns [l]
345 otherwise. *)
346
347 val eq_set : 'a eq -> 'a list eq
348 (** Test equality up to permutation. It respects multiple occurrences
349 and thus works also on multisets. *)
350
351 val subset : 'a list eq
352 (** Tell if a list is a subset of another up to permutation. It expects
353 each element to occur only once. *)
354
355 val merge_set : 'a cmp -> 'a list -> 'a list -> 'a list
356 (** Merge two sorted lists and preserves the uniqueness property. *)
357
358 val intersect : 'a eq -> 'a list -> 'a list -> 'a list
359 (** Return the intersection of two lists, assuming and preserving
360 uniqueness of elements *)
361
362 val union : 'a eq -> 'a list -> 'a list -> 'a list
363 (** Return the union of two lists, assuming and preserving
364 uniqueness of elements *)
365
366 val unionq : 'a list -> 'a list -> 'a list
367 (** [union] specialized to physical equality *)
368
369 val subtract : 'a eq -> 'a list -> 'a list -> 'a list
370 (** Remove from the first list all elements from the second list. *)
371
372 val subtractq : 'a list -> 'a list -> 'a list
373 (** [subtract] specialized to physical equality *)
374
375 (** {6 Uniqueness and duplication} *)
376
377 val distinct : 'a list -> bool
378 (** Return [true] if all elements of the list are distinct. *)
379
380 val distinct_f : 'a cmp -> 'a list -> bool
381 (** Like [distinct] but using the equality given as argument *)
382
383 val duplicates : 'a eq -> 'a list -> 'a list
384 (** Return the list of unique elements which appear at least twice. Elements
385 are kept in the order of their first appearance. *)
386
387 val uniquize_key : ('a -> 'b) -> 'a list -> 'a list
388 (** Return the list of elements without duplicates using the
389 function to associate a comparison key to each element.
390 This is the list unchanged if there was none. *)
391
392 val uniquize : 'a list -> 'a list
393 (** Return the list of elements without duplicates.
394 This is the list unchanged if there was none. *)
395
396 val sort_uniquize : 'a cmp -> 'a list -> 'a list
397 (** Return a sorted version of a list without duplicates
398 according to some comparison function. *)
399
400 val min : 'a cmp -> 'a list -> 'a
401 (** Return minimum element according to some comparison function.
402
403 @raise Not_found on an empty list. *)
404
405 (** {6 Cartesian product} *)
406
407 val cartesian : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
408 (** A generic binary cartesian product: for any operator (**),
409 [cartesian (**) [x1;x2] [y1;y2] = [x1**y1; x1**y2; x2**y1; x2**y1]],
410 and so on if there are more elements in the lists. *)
411
412 val cartesians : ('a -> 'b -> 'b) -> 'b -> 'a list list -> 'b list
413 (** [cartesians op init l] is an n-ary cartesian product: it builds
414 the list of all [op a1 .. (op an init) ..] for [a1], ..., [an] in
415 the product of the elements of the lists *)
416
417 val combinations : 'a list list -> 'a list list
418 (** [combinations l] returns the list of [n_1] * ... * [n_p] tuples
419 [[a11;...;ap1];...;[a1n_1;...;apn_pd]] whenever [l] is a list
420 [[a11;..;a1n_1];...;[ap1;apn_p]]; otherwise said, it is
421 [cartesians (::) [] l] *)
422
423 val cartesians_filter :
424 ('a -> 'b -> 'b option) -> 'b -> 'a list list -> 'b list
425 (** Like [cartesians op init l] but keep only the tuples for which
426 [op] returns [Some _] on all the elements of the tuple. *)
427
428 (** When returning a list of same type as the input, maximally
429 shares the suffix of the output which is physically equal to the
430 corresponding suffix of the input *)
431 module Smart :
432 sig
433 val map : ('a -> 'a) -> 'a list -> 'a list
434 (** Like [List.map] but sharing with the input the longest suffix
435 of the output which is physically the same as the input; in
436 particular, [Smart.map f l == l] (physically) if [f a == a]
437 (physically) for all members of the list *)
438
439 val fold_left_map : ('a -> 'b -> 'a * 'b) -> 'a -> 'b list -> 'a * 'b list
440 (** Idem for the second argument of [List.fold_left_map f e l]
441 relatively to the second argument of [f] *)
442
443 val fold_right_map : ('b -> 'a -> 'b * 'a) -> 'b list -> 'a -> 'b list * 'a
444 (** Idem for the first argument of [List.fold_right_map f l e]
445 relatively to the second argument of [f] *)
446 end
447
448 module type MonoS = sig
449 type elt
450 val equal : elt list -> elt list -> bool
451 val mem : elt -> elt list -> bool
452 val assoc : elt -> (elt * 'a) list -> 'a
453 val mem_assoc : elt -> (elt * 'a) list -> bool
454 val remove_assoc : elt -> (elt * 'a) list -> (elt * 'a) list
455 val mem_assoc_sym : elt -> ('a * elt) list -> bool
456 end