Skip to content
This repository
tag: v1154
Fetching contributors…

Cannot retrieve contributors at this time

file 444 lines (379 sloc) 17.142 kb
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
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA 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 Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
val length : 'a list -> int
val empty : 'a list
val is_empty: 'a list -> bool
val hd : 'a list -> 'a
val tl : 'a list -> 'a list
val nth : 'a list -> int -> 'a
val rev : 'a list -> 'a list
val append : 'a list -> 'a list -> 'a list
val rev_append : 'a list -> 'a list -> 'a list
val rev_map_append : ('a -> 'b) -> 'a list -> 'b list -> 'b list
val rev_filter_map_append : ('a -> 'b option) -> 'a list -> 'b list -> 'b list
val tail_append : 'a list -> 'a list -> 'a list
  (**
Tail recursive version of [append]
*)

val concat : 'a list list -> 'a list
val flatten : 'a list list -> 'a list
val iter : ('a -> unit) -> 'a list -> unit
val map : ('a -> 'b) -> 'a list -> 'b list
val map_right : ('a -> 'b) -> 'a list -> 'b list
val rev_map : ('a -> 'b) -> 'a list -> 'b list
val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a
val fold_right : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
val iter2 : ('a -> 'b -> unit) -> 'a list -> 'b list -> unit
val map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
val rev_map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
val fold_left2 : ('a -> 'b -> 'c -> 'a) -> 'a -> 'b list -> 'c list -> 'a
val fold_right2 :
  ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c
val for_all : ('a -> bool) -> 'a list -> bool
val exists : ('a -> bool) -> 'a list -> bool
val for_all2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool
val exists2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool
val mem : 'a -> 'a list -> bool
val memq : 'a -> 'a list -> bool
val mem_eq : eq: ('a -> 'a -> bool) -> 'a -> 'a list -> bool
val find : ('a -> bool) -> 'a list -> 'a
val find_map : ('a -> 'b option) -> 'a list -> 'b option
val filter : ('a -> bool) -> 'a list -> 'a list
val tail_filter : ('a -> bool) -> 'a list -> 'a list
val filteri : (int -> 'a -> bool) -> 'a list -> 'a list
val find_all : ('a -> bool) -> 'a list -> 'a list
val partition : ('a -> bool) -> 'a list -> 'a list * 'a list
val assoc : 'a -> ('a * 'b) list -> 'b
val assq : 'a -> ('a * 'b) list -> 'b
val mem_assoc : 'a -> ('a * 'b) list -> bool
val mem_assq : 'a -> ('a * 'b) list -> bool
val remove_assoc : 'a -> ('a * 'b) list -> ('a * 'b) list
val remove_assq : 'a -> ('a * 'b) list -> ('a * 'b) list
val split : ('a * 'b) list -> 'a list * 'b list

(**
Combine two list into a single list of couple.
Cf function [combine_opt] if you do not want to deal
with Exceptions
@raise Invalid_argument if the 2 list do not have the same length
*)
val combine : 'a list -> 'b list -> ('a * 'b) list

val sort : ('a -> 'a -> int) -> 'a list -> 'a list
val stable_sort : ('a -> 'a -> int) -> 'a list -> 'a list
val fast_sort : ('a -> 'a -> int) -> 'a list -> 'a list
val merge : ('a -> 'a -> int) -> 'a list -> 'a list -> 'a list
exception Empty

(**
[subtract l1 l2] returns the list of all elements of [l1] that do not appear in [l2]
*)
val subtract : 'a list -> 'a list -> 'a list
(**
[substract_eq ~eq l1 l2] returns the list of all elements of [l1] that do
not appear in [l2] using the custom [~eq] comparaison function. *)
val substract_eq : eq: ('a -> 'a -> bool) -> 'a list -> 'a list -> 'a list

val subset : 'a list -> 'a list -> bool
val subset_eq : eq:('a -> 'a -> bool) -> 'a list -> 'a list -> bool
val iter_right : ('a -> 'b) -> 'a list -> unit
val iteri : ('a -> int -> unit) -> 'a list -> unit
val rev_mapi : (int -> 'a -> 'b) -> 'a list -> 'b list
val mapi : (int -> 'a -> 'b) -> 'a list -> 'b list

(**
similar to [(map f list) @ tail]
*)
val map_with_tail : ('a -> 'b) -> 'a list -> 'b list -> 'b list
val for_alli : (int -> 'a -> bool) -> 'a list -> bool

(**
[init i fct]
<!> images of [fct] are computed from [i-1] to [0].
Beware if [fct] makes any side effects, elts will not
be in the correct order. Use function [side_effect_init]
*)
val init : int -> (int -> 'a) -> 'a list

(**
same than [init] but works ok with function having side effects :
images of [fct] are computed from [0] to [i-1], and the implementation
contains a [List.rev]
*)
val side_effect_init : int -> (int -> 'a) -> 'a list

val take : int -> 'a list -> 'a list
val last : 'a list -> 'a
val drop : int -> 'a list -> 'a list
val extract_last : 'a list -> 'a list * 'a

(**
[split_at n list]
Take at most [n] elements of [list] in fst and put the rest in snd.
Does not fail if the [list] has less than [n] elements, simply returns
(list, []) in that case.
*)
val split_at : int -> 'a list -> 'a list * 'a list
val rev_filter : ('a -> bool) -> 'a list -> 'a list

(**
@raise Invalid_argument if the sum of the elements of the first list is not the length of the second list
*)
val split_ats : int list -> 'a list -> 'a list list

val split_at_sep : ('a -> bool) -> 'a list -> 'a list list
val splice : int -> int -> 'a list -> 'a list -> 'a list
val fold_left_i : ('a -> 'b -> int -> 'a) -> 'a -> 'b list -> 'a
val fold_right_i : ('a -> int -> 'b -> 'b) -> 'a list -> 'b -> 'b

(**
As [fold_left] with the head of the list as an accumulator
@raise Empty on empty list
*)
val fold : ('a -> 'a -> 'a) -> 'a list -> 'a

val to_string : ('a -> string) -> 'a list -> string

(**
Print on list format; i.e. [a;b;c;...]
*)
val print : ('a -> string) -> 'a list -> string
val max : 'a list -> 'a
val min : 'a list -> 'a
val minmax : 'a list -> 'a * 'a
val argmax : 'a list -> 'a
val argmin : 'a list -> 'a
val remove_all : 'a -> 'a list -> 'a list
val remove_first : 'a -> 'a list -> 'a list
(* ************************************************************************** *)
(** {b Descr}: Removes the first occurrence of an element from a list, using a
custom equality test and return the list with this element removed. The
element to remove is assumed to be present in the list: if no occurrence of
it is found, hence removed, this function raises [Not_found].
{b Visibility}: Exported outside this module. *)
(* ************************************************************************** *)
val remove_first_or_fail_eq :
  eq: ('a -> 'a -> bool) -> 'a -> 'a list -> 'a list
val remove_last : 'a list -> 'a list
(** [replace elt rep lst] replaces all occurrences of [elt] by given list [rep]. If [elt] does not occur in [lst], returns [lst]. *)
val replace : 'a -> 'a list -> 'a list -> 'a list
val cons : 'a -> 'a list -> 'a list

(**
* [choose_random l] returns a random element from list [l].
* Throws [Invalid_argument] if [l] empty.
*)
val choose_random : 'a list -> 'a
val get_only_element: 'a list -> 'a
  (**
If the list contains exactly one element, returns it
or else raise Invalid_argument
*)

(**
Remove duplicates from a sorted list.
The first occurrence is keeped, and the returned
list is still sorted.
*)
val uniq : ?cmp:('a -> 'a -> int) -> 'a list -> 'a list

(**
Remove duplicates from a unsorted list.
The returned list is not sorted, but does
contains only the first occurrence of each element.
*)
val uniq_unsorted : ?cmp:('a -> 'a -> int) -> ?conflict:('a -> 'a -> unit) -> 'a list -> 'a list

(**
[insert p e l] inserts element e at position p in list l
positions starting from offset [0]
@raise Empty if the end of the list comes before the insertion position
*)
val insert : int -> 'a -> 'a list -> 'a list

(**
[insert_sorted x l] assumes [l] is sorted and inserts element [x] in [l]
such that the result is still sorted.

optional arguments:
-[cmp]: the comparison used for sorting;
-[conflict]: the function used when there is already an element equal to [x] in [l].
by default, conflict will keep the 2 elements in the list.
*)
val insert_sorted : ?cmp:('a -> 'a -> int )-> ?conflict:('a -> 'a -> 'a list) ->'a -> 'a list -> 'a list

val filter_and_fold :
  ('a -> 'b -> 'a * bool) -> 'a -> 'b list -> 'a * 'b list
val memi : 'a -> 'a list -> int option (* same as pos_opt *)

(** flip is also known as transpose (think matrix) *)
val flip : 'a list list -> 'a list list

(**
Same than [combine] but returns [None] instead of raising
[Invalid_argument] if the 2 list do not have the same length.
*)
val combine_opt : 'a list -> 'b list -> ('a * 'b) list option
val assoc_opt : 'a -> ('a * 'b) list -> 'b option
val assq_opt : 'a -> ('a * 'b) list -> 'b option
val find_opt : ('a -> bool) -> 'a list -> 'a option

(**
[findi p l] returns the index (position) of the first element
that satisfy the predicate p
*)
val findi : ('a -> bool) -> 'a list -> int option

(**
[find_i p l] returns a pair of the index of the first element
that satisfy the predicate p, and this element
*)
val find_i : ('a -> bool) -> 'a list -> (int * 'a) option
val find_map : ('a -> 'b option) -> 'a list -> 'b option
val pos_opt : 'a -> 'a list -> int option (* same as memi *)
val filter_map : ('a -> 'b option) -> 'a list -> 'b list

val filter_mapi : (int -> 'a -> 'b option) -> 'a list -> 'b list

val partition_map : ('a -> 'b option * 'c option) -> 'a list -> 'b list * 'c list

(** return the first some result of functions in a' list or none *)
val get_first_some : ('a -> 'b option) list -> 'a -> 'b option
val get_first_some_ar2 :
  ('a -> 'b -> 'c option) list -> 'a -> 'b -> 'c option
val fold_right_map :
  ('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b

(**
{[foldl f [ 1, 2 , 3 ] acc = acc |> f 1 |> f 2 |> f 3]}
same as foldl except that the argument are in a different order
*)
val foldl : ('a -> 'acc -> 'acc) -> 'a list -> 'acc -> 'acc

(**
{[foldr f [ 1, 2 , 3 ] acc = acc |> f 3 |> f 2 |> f 1]}
*)
val foldr : ('a -> 'acc -> 'acc) -> 'a list -> 'acc -> 'acc

(**
{[foldl1 f [ 1, 2 , 3 ] = 1 |> f 2 |> f 3]}
@raise Invalid_argument if the list is empty
*)
val foldl1 : ('a -> 'a -> 'a) -> 'a list -> 'a

(**
{[foldr1 f [ 1, 2 , 3 ] = 3 |> f 2 |> f 1]}
@raise Invalid_argument is the list is empty
*)
val foldr1 : ('a -> 'a -> 'a) -> 'a list -> 'a

(**
{[fold_left1 f [ 1, 2 , 3 ] = (f(f 1 2) 3)]}
@raise Invalid_argument if the list is empty
*)
val fold_left1 : ('a -> 'a -> 'a) -> 'a list -> 'a

(**
Just like fold_left, except that you are given only the second element of
the assoc list
(useful for folding on the values of a record node in an ast for example)
*)
val fold_left_snd : ('acc -> 'elt -> 'acc) -> 'acc -> (_ * 'elt) list -> 'acc

(**
This is like a [map] but returns physically the same list if the maped
elt is physically the same for each element of the list. Tail recursive.
Used for optimizing traversal constructions
*)
val map_stable : ('a -> 'a) -> 'a list -> 'a list

(**
This is like [filter] but returns physically the same list if all
elements are kept by the filter function.
Tail recursive.
*)
val filter_stable : ('a -> bool) -> 'a list -> 'a list

(** This is a fold_left combined with a map. Tail recursive. *)
val fold_left_map :
  ((*accumulator*)'a -> 'b -> 'a * 'c) -> (*init*)'a -> 'b list -> 'a * 'c list

(** This is a fold_left combined with a rev_map. Tail recursive. *)
val fold_left_rev_map :
  ('acc -> 'input -> 'acc * 'output) -> 'acc -> 'input list -> 'acc * 'output list

(** Same than [fold_left_map] but returns physically the same list if the maped
elt is physically the same for each element of the list. Tail recursive.
Used for optimizing traversal constructions *)
val fold_left_map_stable :
  ((*accumulator*)'a -> 'b -> 'a * 'b) -> (*init*)'a -> 'b list -> 'a * 'b list
val fold_right_map_stable :
  ((*accumulator*)'a -> 'b -> 'a * 'b) -> (*init*)'a -> 'b list -> 'a * 'b list

val fold_left_filter_map :
  ('a -> 'b -> 'a * 'c option) -> 'a -> 'b list -> 'a * 'c list
val fold_left_map2 :
  ('a -> 'b -> 'c -> 'a * 'd) -> 'a -> 'b list -> 'c list -> 'a * 'd list

(**
Like a fold_left_map, but the function returns a list for each element.
At end, all the list are flattened. Tail rec.
*)
val fold_left_collect :
  ('a -> 'b -> 'a * 'c list) -> 'a -> 'b list -> 'a * 'c list
val fold_left_map_i :
  (int -> 'a -> 'b -> 'a * 'c) -> 'a -> 'b list -> 'a * 'c list
val for_all2_same_length :
  ('a -> 'b -> bool) -> 'a list -> 'b list -> bool

(**
Tail rec
*)
val rev_concat_map : ('a -> 'b list) -> 'a list -> 'b list

(**
Tail rec. (implemented with rev (rev_concat_map)
*)
val concat_map : ('a -> 'b list) -> 'a list -> 'b list (* same as collect *)

val rev_concat_map2 : ('a -> 'b -> 'c list) -> 'a list -> 'b list -> 'c list
val concat_map2 : ('a -> 'b -> 'c list) -> 'a list -> 'b list -> 'c list

val tail_concat : 'a list list -> 'a list
val tail_append_keep_length : 'a list -> 'a list -> int * int * 'a list
val tail_split : ('a * 'b) list -> 'a list * 'b list
val tail_map : ('a -> 'b) -> 'a list -> 'b list
val tail_map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list (* raises Invalid_argument *)
val vmap : ('a -> 'b) -> ('c * 'a) list -> ('c * 'b) list
val imap : ('a -> 'b) -> ('a * 'c) list -> ('a * 'b) list
val rmap : ('a -> 'b) -> ('a * 'c) list -> ('b * 'c) list
val tail_vmap : ('a -> 'b) -> ('c * 'a) list -> ('c * 'b) list
val tail_imap : ('a -> 'b) -> ('a * 'c) list -> ('a * 'b) list
val tail_rmap : ('a -> 'b) -> ('a * 'c) list -> ('b * 'c) list
val rectangle_map : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
val triangle_map : ('a -> 'a -> 'b) -> 'a list -> 'a list -> 'b list

val make_compare : ('a -> 'b -> int) -> 'a list -> 'b list -> int
  (**
Lists are compared first by comparing their elements pair-wise
and then, if one, is exhausted, by comparing their lengths
*)

val option_like_merge :
  ('a list -> 'a list -> 'a list) -> 'a list -> 'a list -> 'a list

val filterbounds : 'a option * int -> ('b -> 'a) -> 'b list -> 'b list

(* ************************************************************************** *)
(** {b Descr}: Returns the value (second component) associated with the key
(first component) equal to [x] in a list of pairs.
Raises [Not_found] if there is no value associated with [x] in the list.
Equality test is performed with the provided function [eq] instead of the
general [=] function. *)
(* ************************************************************************** *)
val assoc_custom_equality: eq:('a -> 'b -> bool) -> 'b -> ('a * 'c) list -> 'c

(* ************************************************************************** *)
(** {b Descr}: Returns the value (second component) associated with the key
(first component) equal to [x] in a list of pairs if it exists, else None.
Equality test is performed with the provided function [eq] instead of the
general [=] function. *)
(* ************************************************************************** *)
val assoc_custom_equality_opt: eq:('a -> 'b -> bool) -> 'b -> ('a * 'c) list -> 'c option

(* ************************************************************************** *)
(** {b Descr}: Transforms a list of triplets into a triplet of lists. *)
(* ************************************************************************** *)
val split3: ('a * 'b * 'c) list -> ('a list * 'b list * 'c list)

module MakeAssoc(S:Map.OrderedType) : sig
  type +'a t = (S.t * 'a) list
  val find : S.t -> 'a t -> 'a
  val find_opt : S.t -> 'a t -> 'a option
  val mem : S.t -> 'a t -> bool
  val remove : S.t -> 'a t -> 'a t

  (** input are assumed to be sorted
ouput list is sorted
duplicated keys are kept
*)
  val sorted_merge : 'a t -> 'a t -> 'a t

  (** input are assumed to be sorted and not have duplicates keys
output list is sorted and does not have duplicated keys
*)
  val unique_sorted_merge : merge:(S.t * 'a -> S.t * 'a -> S.t * 'a) -> 'a t -> 'a t -> 'a t

  val sort : 'a t -> 'a t
end

module StringAssoc : module type of MakeAssoc(String)

(**
The same as [map2] except that it stops when one list becomes
empty, instead of throwing an error when the lists don't have
the same length
Tail recursive
*)
val partial_map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list

(**
The same as [List.rev (List.partial_map2 f l1 l2)]
*)
val rev_partial_map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list

val fold_left_partial_map2 : ('acc -> 'a -> 'b -> 'acc * 'c) -> 'acc -> 'a list -> 'b list -> 'acc * 'c list
val rev_fold_left_partial_map2 : ('acc -> 'a -> 'b -> 'acc * 'c) -> 'acc -> 'a list -> 'b list -> 'acc * 'c list

val filter2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> 'a list * 'b list
Something went wrong with that request. Please try again.