| 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 |
(* The interface is an extended version of OCaml stdlib/list.ml. *) |
| 12 |
|
| 13 |
(**************************************************************************) |
| 14 |
(* *) |
| 15 |
(* OCaml *) |
| 16 |
(* *) |
| 17 |
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *) |
| 18 |
(* *) |
| 19 |
(* Copyright 1996 Institut National de Recherche en Informatique et *) |
| 20 |
(* en Automatique. *) |
| 21 |
(* *) |
| 22 |
(* All rights reserved. This file is distributed under the terms of *) |
| 23 |
(* the GNU Lesser General Public License version 2.1, with the *) |
| 24 |
(* special exception on linking described in the file LICENSE. *) |
| 25 |
(* *) |
| 26 |
(**************************************************************************) |
| 27 |
|
| 28 |
Require Import Ltac2.Init. |
| 29 |
Require Ltac2.Int. |
| 30 |
Require Ltac2.Control. |
| 31 |
Require Ltac2.Bool. |
| 32 |
Require Ltac2.Message. |
| 33 |
|
| 34 |
(** Compute the length of a list. *) |
| 35 |
Ltac2 rec length (ls : 'a list) : int := |
| 36 |
match ls with |
| 37 |
| [] => 0 |
| 38 |
| _ :: xs => Int.add 1 (length xs) |
| 39 |
end. |
| 40 |
|
| 41 |
(** [compare_lengths ls1 ls2] is equal to [Int.compare (length ls1) (length ls2)], |
| 42 |
but is more efficient in most cases: it runs in O(min (length ls1) (length ls2)) |
| 43 |
instead of O(max (length ls1) (length ls2)). *) |
| 44 |
Ltac2 rec compare_lengths (ls1 : 'a list) (ls2 : 'b list) : int := |
| 45 |
match ls1 with |
| 46 |
| [] |
| 47 |
=> match ls2 with |
| 48 |
| [] => 0 |
| 49 |
| _ :: _ => -1 |
| 50 |
end |
| 51 |
| _ :: ls1 |
| 52 |
=> match ls2 with |
| 53 |
| [] => 1 |
| 54 |
| _ :: ls2 => compare_lengths ls1 ls2 |
| 55 |
end |
| 56 |
end. |
| 57 |
|
| 58 |
(** [compare_length_with ls n] is equal to [Int.compare (length ls) n], |
| 59 |
but is more efficient in most cases: it runs in O(min (length ls) n) |
| 60 |
instead of O(length ls). *) |
| 61 |
Ltac2 rec compare_length_with (ls : 'a list) (n : int) : int := |
| 62 |
match Int.lt n 0 with |
| 63 |
| true => 1 |
| 64 |
| false |
| 65 |
=> match ls with |
| 66 |
| [] => Int.compare 0 n |
| 67 |
| _ :: ls => compare_length_with ls (Int.sub n 1) |
| 68 |
end |
| 69 |
end. |
| 70 |
|
| 71 |
Ltac2 cons (x : 'a) (xs : 'a list) : 'a list := |
| 72 |
x :: xs. |
| 73 |
|
| 74 |
(** Since Ltac2 distinguishes between backtracking and fatal exceptions, |
| 75 |
we provide option and default variants of functions which throw in the |
| 76 |
OCaml stdlib. *) |
| 77 |
|
| 78 |
(** Return the first element of a list. |
| 79 |
Returns [None] if the list is empty. *) |
| 80 |
Ltac2 hd_opt (ls : 'a list) : 'a option := |
| 81 |
match ls with |
| 82 |
| [] => None |
| 83 |
| x :: _ => Some x |
| 84 |
end. |
| 85 |
|
| 86 |
(** Return the first element of a list. |
| 87 |
Throw an exception if the list is empty. *) |
| 88 |
Ltac2 hd (ls : 'a list) : 'a := |
| 89 |
match ls with |
| 90 |
| [] => Control.throw_invalid_argument "List.hd" |
| 91 |
| x :: _ => x |
| 92 |
end. |
| 93 |
|
| 94 |
(** Remove the first element from a list. |
| 95 |
The empty list is returned as is. *) |
| 96 |
Ltac2 tl (ls : 'a list) : 'a list := |
| 97 |
match ls with |
| 98 |
| [] => [] |
| 99 |
| _ :: xs => xs |
| 100 |
end. |
| 101 |
|
| 102 |
(** Destruct a list into its head and tail. |
| 103 |
Throws an exception if the list is empty. *) |
| 104 |
Ltac2 dest (xs : 'a list) : 'a * 'a list := |
| 105 |
match xs with |
| 106 |
| x :: xs => (x, xs) |
| 107 |
| [] => Control.throw_invalid_argument "List.dest: list empty" |
| 108 |
end. |
| 109 |
|
| 110 |
(** Return [true] if the list is empty. *) |
| 111 |
Ltac2 is_empty (xs : 'a list) : bool := |
| 112 |
match xs with |
| 113 |
| _ :: _ => false |
| 114 |
| _ => true |
| 115 |
end. |
| 116 |
|
| 117 |
(** Return the last element of a list. |
| 118 |
Returns [None] if the list is empty. *) |
| 119 |
Ltac2 rec last_opt (ls : 'a list) : 'a option := |
| 120 |
match ls with |
| 121 |
| [] => None |
| 122 |
| x :: xs |
| 123 |
=> match xs with |
| 124 |
| [] => Some x |
| 125 |
| _ :: _ => last_opt xs |
| 126 |
end |
| 127 |
end. |
| 128 |
|
| 129 |
(** Return the last element of a list. |
| 130 |
Throws an exception if the list is empty. *) |
| 131 |
Ltac2 last (ls : 'a list) : 'a := |
| 132 |
match last_opt ls with |
| 133 |
| None => Control.throw_invalid_argument "List.last" |
| 134 |
| Some v => v |
| 135 |
end. |
| 136 |
|
| 137 |
(** Remove the last element of a list. |
| 138 |
The empty list is returned as is. *) |
| 139 |
Ltac2 rec removelast (ls : 'a list) : 'a list := |
| 140 |
match ls with |
| 141 |
| [] => [] |
| 142 |
| x :: xs |
| 143 |
=> match xs with |
| 144 |
| [] => [] |
| 145 |
| _ :: _ => x :: removelast xs |
| 146 |
end |
| 147 |
end. |
| 148 |
|
| 149 |
(** Helper function for [nth_opt]. *) |
| 150 |
Ltac2 rec nth_opt_aux (ls : 'a list) (n : int) := |
| 151 |
match ls with |
| 152 |
| [] => None |
| 153 |
| x :: xs |
| 154 |
=> match Int.equal n 0 with |
| 155 |
| true => Some x |
| 156 |
| false => nth_opt_aux xs (Int.sub n 1) |
| 157 |
end |
| 158 |
end. |
| 159 |
|
| 160 |
(** [nth_opt ls n] returns the [n]-th element [ls], starting at [0]. |
| 161 |
Throws an exception if [n < 0]. Returns [None] if [n >= length ls]. *) |
| 162 |
Ltac2 nth_opt (ls : 'a list) (n : int) : 'a option := |
| 163 |
Control.assert_valid_argument "List.nth" (Int.ge n 0); |
| 164 |
nth_opt_aux ls n. |
| 165 |
|
| 166 |
(** [nth ls n] returns the [n]-th element of [ls], starting at [0]. |
| 167 |
Throws an exception if [n < 0] or [n >= length ls]. *) |
| 168 |
Ltac2 nth (ls : 'a list) (n : int) : 'a := |
| 169 |
match nth_opt ls n with |
| 170 |
| Some v => v |
| 171 |
| None => Control.throw_out_of_bounds "List.nth" |
| 172 |
end. |
| 173 |
|
| 174 |
(** Reverse [l1], and append it with [l2]: |
| 175 |
[rev_append l1 l2 = append (rev l1) l2]. *) |
| 176 |
Ltac2 rec rev_append (l1 : 'a list) (l2 : 'a list) : 'a list := |
| 177 |
match l1 with |
| 178 |
| [] => l2 |
| 179 |
| a :: l => rev_append l (a :: l2) |
| 180 |
end. |
| 181 |
|
| 182 |
(** Reverse a list. *) |
| 183 |
Ltac2 rev (l : 'a list) : 'a list := |
| 184 |
rev_append l []. |
| 185 |
|
| 186 |
(** Append two lists [ls1] and [ls2]. Complexity: O(length ls1). *) |
| 187 |
Ltac2 rec append (ls1 : 'a list) (ls2 : 'a list) : 'a list := |
| 188 |
match ls1 with |
| 189 |
| [] => ls2 |
| 190 |
| x :: xs => x :: append xs ls2 |
| 191 |
end. |
| 192 |
|
| 193 |
(** Concatenate a list of lists. *) |
| 194 |
Ltac2 rec concat (ls : 'a list list) : 'a list := |
| 195 |
match ls with |
| 196 |
| [] => [] |
| 197 |
| x :: xs => append x (concat xs) |
| 198 |
end. |
| 199 |
|
| 200 |
(** Synonym for [concat]. *) |
| 201 |
Ltac2 flatten (ls : 'a list list) : 'a list := |
| 202 |
concat ls. |
| 203 |
|
| 204 |
(** Iterate a function on each element of a list. |
| 205 |
Elements are processed from first to last. *) |
| 206 |
Ltac2 rec iter (f : 'a -> unit) (ls : 'a list) : unit := |
| 207 |
match ls with |
| 208 |
| [] => () |
| 209 |
| l :: ls => f l; iter f ls |
| 210 |
end. |
| 211 |
|
| 212 |
(** Helper function for [iteri]. *) |
| 213 |
Ltac2 rec iteri_aux (i : int) (f : int -> 'a -> unit) (ls : 'a list) : unit := |
| 214 |
match ls with |
| 215 |
| [] => () |
| 216 |
| l :: ls => f i l; iteri_aux (Int.add i 1) f ls |
| 217 |
end. |
| 218 |
|
| 219 |
(** Iterate a function [f] on each element of a list. |
| 220 |
[f] is additionally supplied the index of each element, starting at [0]. |
| 221 |
Elements are processed from first to last. *) |
| 222 |
Ltac2 iteri (f : int -> 'a -> unit) (ls : 'a list) : unit := |
| 223 |
iteri_aux 0 f ls. |
| 224 |
|
| 225 |
(** [map f [x0 ; x1 ; ... ; xN]] is [f x0 ; f x1 ; ... ; f xN]. |
| 226 |
Elements are processed from first to last. *) |
| 227 |
Ltac2 rec map (f : 'a -> 'b) (ls : 'a list) : 'b list := |
| 228 |
match ls with |
| 229 |
| [] => [] |
| 230 |
| l :: ls => f l :: map f ls |
| 231 |
end. |
| 232 |
|
| 233 |
(** Helper function for [mapi]. *) |
| 234 |
Ltac2 rec mapi_aux (i : int) (f : int -> 'a -> 'b) (ls : 'a list) : 'b list := |
| 235 |
match ls with |
| 236 |
| [] => [] |
| 237 |
| l :: ls => f i l :: mapi_aux (Int.add i 1) f ls |
| 238 |
end. |
| 239 |
|
| 240 |
(** [mapi f [x0 ; x1 ; ... ; xN]] is [f 0 x0 ; f 1 x1 ; ... ; f N xN]. |
| 241 |
Elements are processed from first to last. *) |
| 242 |
Ltac2 mapi (f : int -> 'a -> 'b) (ls : 'a list) : 'b list := |
| 243 |
mapi_aux 0 f ls. |
| 244 |
|
| 245 |
(** Map a function over a list, and then concatenate the results. *) |
| 246 |
Ltac2 rec flat_map (f : 'a -> 'b list) (xs : 'a list) : 'b list := |
| 247 |
match xs with |
| 248 |
| [] => [] |
| 249 |
| x :: xs => append (f x) (flat_map f xs) |
| 250 |
end. |
| 251 |
|
| 252 |
(** [rev_map f l] is equal to [rev (map f l)], |
| 253 |
but is more efficient in most cases. *) |
| 254 |
Ltac2 rev_map (f : 'a -> 'b) (ls : 'a list) : 'b list := |
| 255 |
let rec rmap_f accu ls := |
| 256 |
match ls with |
| 257 |
| [] => accu |
| 258 |
| a::l => rmap_f (f a :: accu) l |
| 259 |
end in |
| 260 |
rmap_f [] ls. |
| 261 |
|
| 262 |
(** [fold_right f [a1; ...; an] init] is [f a1 (f a2 (... (f an init) ...))]. *) |
| 263 |
Ltac2 rec fold_right (f : 'a -> 'b -> 'b) (ls : 'a list) (a : 'b) : 'b := |
| 264 |
match ls with |
| 265 |
| [] => a |
| 266 |
| l :: ls => f l (fold_right f ls a) |
| 267 |
end. |
| 268 |
|
| 269 |
(** [fold_left f init [b1; ...; bn]] is [f (... (f (f init b1) b2) ...) bn]. *) |
| 270 |
Ltac2 rec fold_left (f : 'a -> 'b -> 'a) (a : 'a) (xs : 'b list) : 'a := |
| 271 |
match xs with |
| 272 |
| [] => a |
| 273 |
| x :: xs => fold_left f (f a x) xs |
| 274 |
end. |
| 275 |
|
| 276 |
Ltac2 fold_lefti (f : int -> 'a -> 'b -> 'a) (a : 'a) (xs : 'b list) : 'a := |
| 277 |
let rec go i a xs := |
| 278 |
match xs with |
| 279 |
| [] => a |
| 280 |
| x :: xs => go (Int.add i 1) (f i a x) xs |
| 281 |
end |
| 282 |
in go 0 a xs. |
| 283 |
|
| 284 |
(** Iterate a function over two lists. |
| 285 |
Elements are processed from first to last. |
| 286 |
Throws an exception if the lengths of the lists differ. *) |
| 287 |
Ltac2 rec iter2 (f : 'a -> 'b -> unit) (ls1 : 'a list) (ls2 : 'b list) : unit := |
| 288 |
match ls1 with |
| 289 |
| [] |
| 290 |
=> match ls2 with |
| 291 |
| [] => () |
| 292 |
| _ :: _ => Control.throw_invalid_argument "List.iter2" |
| 293 |
end |
| 294 |
| l1 :: ls1 |
| 295 |
=> match ls2 with |
| 296 |
| [] => Control.throw_invalid_argument "List.iter2" |
| 297 |
| l2 :: ls2 |
| 298 |
=> f l1 l2; iter2 f ls1 ls2 |
| 299 |
end |
| 300 |
end. |
| 301 |
|
| 302 |
(** Map a function over two lists. |
| 303 |
Elements are processed from first to last. |
| 304 |
Throws an exception if the lengths of the lists differ. *) |
| 305 |
Ltac2 rec map2 (f : 'a -> 'b -> 'c) (ls1 : 'a list) (ls2 : 'b list) : 'c list := |
| 306 |
match ls1 with |
| 307 |
| [] |
| 308 |
=> match ls2 with |
| 309 |
| [] => [] |
| 310 |
| _ :: _ => Control.throw_invalid_argument "List.map2" |
| 311 |
end |
| 312 |
| l1 :: ls1 |
| 313 |
=> match ls2 with |
| 314 |
| [] => Control.throw_invalid_argument "List.map2" |
| 315 |
| l2 :: ls2 |
| 316 |
=> f l1 l2 :: map2 f ls1 ls2 |
| 317 |
end |
| 318 |
end. |
| 319 |
|
| 320 |
(** [rev_map2 f l1 l2] is equal to [rev (map2 f l1 l2)], |
| 321 |
but is more efficient in most cases. *) |
| 322 |
Ltac2 rev_map2 (f : 'a -> 'b -> 'c) (ls1 : 'a list) (ls2 : 'b list) : 'c list := |
| 323 |
let rec rmap2_f accu ls1 ls2 := |
| 324 |
match ls1 with |
| 325 |
| [] |
| 326 |
=> match ls2 with |
| 327 |
| [] => accu |
| 328 |
| _ :: _ => Control.throw_invalid_argument "List.rev_map2" |
| 329 |
end |
| 330 |
| l1 :: ls1 |
| 331 |
=> match ls2 with |
| 332 |
| [] => Control.throw_invalid_argument "List.rev_map2" |
| 333 |
| l2 :: ls2 |
| 334 |
=> rmap2_f (f l1 l2 :: accu) ls1 ls2 |
| 335 |
end |
| 336 |
end in |
| 337 |
rmap2_f [] ls1 ls2. |
| 338 |
|
| 339 |
(** Right fold over two lists. |
| 340 |
Throws an exception if the lengths of the lists differ. *) |
| 341 |
Ltac2 rec fold_right2 (f : 'a -> 'b -> 'c -> 'c) (ls1 : 'a list) (ls2 : 'b list) (a : 'c) : 'c := |
| 342 |
match ls1 with |
| 343 |
| [] |
| 344 |
=> match ls2 with |
| 345 |
| [] => a |
| 346 |
| _ :: _ => Control.throw_invalid_argument "List.fold_right2" |
| 347 |
end |
| 348 |
| l1 :: ls1 |
| 349 |
=> match ls2 with |
| 350 |
| [] => Control.throw_invalid_argument "List.fold_right2" |
| 351 |
| l2 :: ls2 |
| 352 |
=> f l1 l2 (fold_right2 f ls1 ls2 a) |
| 353 |
end |
| 354 |
end. |
| 355 |
|
| 356 |
(** Left fold over two lists. |
| 357 |
Throws an exception if the lengths of the lists differ. *) |
| 358 |
Ltac2 rec fold_left2 (f : 'a -> 'b -> 'c -> 'a) (a : 'a) (ls1 : 'b list) (ls2 : 'c list) : 'a := |
| 359 |
match ls1 with |
| 360 |
| [] |
| 361 |
=> match ls2 with |
| 362 |
| [] => a |
| 363 |
| _ :: _ => Control.throw_invalid_argument "List.fold_left2" |
| 364 |
end |
| 365 |
| l1 :: ls1 |
| 366 |
=> match ls2 with |
| 367 |
| [] => Control.throw_invalid_argument "List.fold_left2" |
| 368 |
| l2 :: ls2 |
| 369 |
=> fold_left2 f (f a l1 l2) ls1 ls2 |
| 370 |
end |
| 371 |
end. |
| 372 |
|
| 373 |
(** [for_all f l] checks that [f] returns true on _all_ elements of the list [l]. |
| 374 |
In particular [for_all f []] is [true]. *) |
| 375 |
Ltac2 rec for_all (f : 'a -> bool) (ls : 'a list) : bool := |
| 376 |
match ls with |
| 377 |
| [] => true |
| 378 |
| x :: xs => match f x with |
| 379 |
| true => for_all f xs |
| 380 |
| false => false |
| 381 |
end |
| 382 |
end. |
| 383 |
|
| 384 |
(** [exist f l] checks that [f] returns true on _at least one_ element of the list [l]. |
| 385 |
In particular [exist f []] is [false]. |
| 386 |
|
| 387 |
We would call this [exists] a la OCaml's [List.exists], |
| 388 |
but that would be a syntax error (because it conflicts with the notation for tactic `exists`), so instead we name it exist. *) |
| 389 |
Ltac2 rec exist (f : 'a -> bool) (ls : 'a list) : bool := |
| 390 |
match ls with |
| 391 |
| [] => false |
| 392 |
| x :: xs => match f x with |
| 393 |
| true => true |
| 394 |
| false => exist f xs |
| 395 |
end |
| 396 |
end. |
| 397 |
|
| 398 |
(** Helper function for [for_all2]. *) |
| 399 |
Ltac2 rec for_all2_aux (on_length_mismatch : 'a list -> 'b list -> bool) f xs ys : bool := |
| 400 |
match xs with |
| 401 |
| [] => match ys with |
| 402 |
| [] => true |
| 403 |
| _ :: _ => on_length_mismatch xs ys |
| 404 |
end |
| 405 |
| x :: xs' |
| 406 |
=> match ys with |
| 407 |
| [] => on_length_mismatch xs ys |
| 408 |
| y :: ys' |
| 409 |
=> match f x y with |
| 410 |
| true => for_all2_aux on_length_mismatch f xs' ys' |
| 411 |
| false => false |
| 412 |
end |
| 413 |
end |
| 414 |
end. |
| 415 |
|
| 416 |
(** Same as [for_all] but for two lists. |
| 417 |
Throws an exception in case the lengths of the lists differ. *) |
| 418 |
Ltac2 for_all2 (f : 'a -> 'b -> bool) (xs : 'a list) (ys : 'b list) : bool := |
| 419 |
for_all2_aux (fun _ _ => Control.throw_invalid_argument "List.for_all2") f xs ys. |
| 420 |
|
| 421 |
(** Same as [for_all] but for two lists. |
| 422 |
Returns [false] in case the lengths of the lists differ. *) |
| 423 |
Ltac2 equal (f : 'a -> 'b -> bool) (xs : 'a list) (ys : 'b list) : bool := |
| 424 |
for_all2_aux (fun _ _ => false) f xs ys. |
| 425 |
|
| 426 |
(** Same as [exist] but for two lists. |
| 427 |
Throws an exception if the lengths of the lists differ. *) |
| 428 |
Ltac2 rec exist2 (f : 'a -> 'b -> bool) (xs : 'a list) (ys : 'b list) : bool := |
| 429 |
match xs with |
| 430 |
| [] => match ys with |
| 431 |
| [] => false |
| 432 |
| _ :: _ => Control.throw_invalid_argument "List.exist2" |
| 433 |
end |
| 434 |
| x :: xs' |
| 435 |
=> match ys with |
| 436 |
| [] => Control.throw_invalid_argument "List.exist2" |
| 437 |
| y :: ys' |
| 438 |
=> match f x y with |
| 439 |
| true => true |
| 440 |
| false => exist2 f xs' ys' |
| 441 |
end |
| 442 |
end |
| 443 |
end. |
| 444 |
|
| 445 |
(** [find_opt f xs] returns the _first_ element of the list [xs] satisfying [f]. |
| 446 |
Returns [None] if no element is found. *) |
| 447 |
Ltac2 rec find_opt (f : 'a -> bool) (xs : 'a list) : 'a option := |
| 448 |
match xs with |
| 449 |
| [] => None |
| 450 |
| x :: xs => match f x with |
| 451 |
| true => Some x |
| 452 |
| false => find_opt f xs |
| 453 |
end |
| 454 |
end. |
| 455 |
|
| 456 |
(** [find f xs] returns the _first_ element of the list [xs] satisfying [f]. |
| 457 |
Throws an exception if no element is found. *) |
| 458 |
Ltac2 find (f : 'a -> bool) (xs : 'a list) : 'a := |
| 459 |
match find_opt f xs with |
| 460 |
| Some v => v |
| 461 |
| None => Control.throw Not_found |
| 462 |
end. |
| 463 |
|
| 464 |
(** [find_rev_opt f xs] returns the _last_ element of the list [xs] satisfying [f]. |
| 465 |
Returns [None] if no element is found. *) |
| 466 |
Ltac2 rec find_rev_opt (f : 'a -> bool) (xs : 'a list) : 'a option := |
| 467 |
match xs with |
| 468 |
| [] => None |
| 469 |
| x :: xs => match find_rev_opt f xs with |
| 470 |
| Some v => Some v |
| 471 |
| None => match f x with |
| 472 |
| true => Some x |
| 473 |
| false => None |
| 474 |
end |
| 475 |
end |
| 476 |
end. |
| 477 |
|
| 478 |
(** [find_rev f xs] returns the _last_ element of the list [xs] satisfying [f]. |
| 479 |
Throws an exception if no element is found. *) |
| 480 |
Ltac2 find_rev (f : 'a -> bool) (xs : 'a list) : 'a := |
| 481 |
match find_rev_opt f xs with |
| 482 |
| Some v => v |
| 483 |
| None => Control.throw Not_found |
| 484 |
end. |
| 485 |
|
| 486 |
(** [mem eq a ls] checks if an element of [ls] is equal to [a] according |
| 487 |
to the user-supplied equality function [eq]. *) |
| 488 |
Ltac2 mem (eq : 'a -> 'a -> bool) (a : 'a) (ls : 'a list) : bool := |
| 489 |
exist (eq a) ls. |
| 490 |
|
| 491 |
(** [filter f xs] removes all the elements of [xs] which do not satisfy [f]. *) |
| 492 |
Ltac2 rec filter (f : 'a -> bool) (xs : 'a list) : 'a list := |
| 493 |
match xs with |
| 494 |
| [] => [] |
| 495 |
| x :: xs |
| 496 |
=> match f x with |
| 497 |
| true => x :: filter f xs |
| 498 |
| false => filter f xs |
| 499 |
end |
| 500 |
end. |
| 501 |
|
| 502 |
(** [filter_out f xs] removes all the elements of [xs] which satisfy [f]. *) |
| 503 |
Ltac2 rec filter_out (f : 'a -> bool) (xs : 'a list) : 'a list := |
| 504 |
filter (fun x => Bool.neg (f x)) xs. |
| 505 |
|
| 506 |
(** Synonym for [filter]. *) |
| 507 |
Ltac2 find_all (f : 'a -> bool) (ls : 'a list) : 'a list := filter f ls. |
| 508 |
|
| 509 |
(** [remove eqb x ls] removes all elements of [ls] which are equal to [x], |
| 510 |
according to the user-supplied equality function [eqb]. *) |
| 511 |
Ltac2 remove (eqb : 'a -> 'a -> bool) (x : 'a) (ls : 'a list) : 'a list := |
| 512 |
filter_out (eqb x) ls. |
| 513 |
|
| 514 |
(** [count_occ eqb x ls] counts how many elements of [ls] are equal to [x], according |
| 515 |
to the user-supplied equality function [eqb]. *) |
| 516 |
Ltac2 count_occ (eqb : 'a -> 'a -> bool) (x : 'a) (ls : 'a list) : int := |
| 517 |
length (filter (eqb x) ls). |
| 518 |
|
| 519 |
(** [list_power ls1 ls2] is [ls2]^[ls1], or the set of sequences of elements |
| 520 |
of [ls2] indexed by elements of [ls1], sorted in lexicographic order. *) |
| 521 |
Ltac2 rec list_power (ls1 : 'a list) (ls2 : 'b list) : ('a * 'b) list list := |
| 522 |
match ls1 with |
| 523 |
| [] => [] :: [] |
| 524 |
| x :: t |
| 525 |
=> flat_map (fun f => map (fun y => (x, y) :: f) ls2) |
| 526 |
(list_power t ls2) |
| 527 |
end. |
| 528 |
|
| 529 |
(** [partition f l] returns two lists [(l_true, l_false)] such that: |
| 530 |
- [l_true] is the sublist of elements of [l] which satisfy [f]. |
| 531 |
- [l_false] is the sublist of elements of [l] which do not satisfy [f]. *) |
| 532 |
Ltac2 rec partition (f : 'a -> bool) (l : 'a list) : 'a list * 'a list := |
| 533 |
match l with |
| 534 |
| [] => ([], []) |
| 535 |
| x :: tl |
| 536 |
=> let (g, d) := partition f tl in |
| 537 |
match f x with |
| 538 |
| true => ((x::g), d) |
| 539 |
| false => (g, (x::d)) |
| 540 |
end |
| 541 |
end. |
| 542 |
|
| 543 |
(** [list_prod ls1 ls2] returns the cartesian product of [ls1] and [ls2], |
| 544 |
i.e. the list of _all_ pairs [(x, y)] where [x] is in [ls1] and [y] is in [ls2]. *) |
| 545 |
Ltac2 rec list_prod (ls1 : 'a list) (ls2 : 'b list) : ('a * 'b) list := |
| 546 |
match ls1 with |
| 547 |
| [] => [] |
| 548 |
| x :: t |
| 549 |
=> append (map (fun y => (x, y)) ls2) (list_prod t ls2) |
| 550 |
end. |
| 551 |
|
| 552 |
(** [fistn n ls] returns the first [n] elements of [ls]. |
| 553 |
Throws an exception if [n < 0] or [n > length ls]. *) |
| 554 |
Ltac2 rec firstn (n : int) (ls : 'a list) : 'a list := |
| 555 |
Control.assert_valid_argument "List.firstn" (Int.ge n 0); |
| 556 |
match Int.equal n 0 with |
| 557 |
| true => [] |
| 558 |
| false |
| 559 |
=> match ls with |
| 560 |
| [] => Control.throw_out_of_bounds "List.firstn" |
| 561 |
| x :: xs |
| 562 |
=> x :: firstn (Int.sub n 1) xs |
| 563 |
end |
| 564 |
end. |
| 565 |
|
| 566 |
(** [skipn n ls] removes the first [n] elements of [ls]. |
| 567 |
Throws an exception if [n < 0] or [n > length ls]. *) |
| 568 |
Ltac2 rec skipn (n : int) (ls : 'a list) : 'a list := |
| 569 |
Control.assert_valid_argument "List.skipn" (Int.ge n 0); |
| 570 |
match Int.equal n 0 with |
| 571 |
| true => ls |
| 572 |
| false |
| 573 |
=> match ls with |
| 574 |
| [] => Control.throw_out_of_bounds "List.skipn" |
| 575 |
| _ :: xs |
| 576 |
=> skipn (Int.sub n 1) xs |
| 577 |
end |
| 578 |
end. |
| 579 |
|
| 580 |
(** [lastn n ls] returns the last [n] elements of [ls]. |
| 581 |
Throws an exception if [n < 0] or [n > length ls]. *) |
| 582 |
Ltac2 lastn (n : int) (ls : 'a list) : 'a list := |
| 583 |
let l := length ls in |
| 584 |
Control.assert_valid_argument "List.lastn" (Int.ge n 0); |
| 585 |
Control.assert_bounds "List.lastn" (Int.le n l); |
| 586 |
skipn (Int.sub l n) ls. |
| 587 |
|
| 588 |
(** [nodup eqb ls] removes duplicates from [ls], according |
| 589 |
to the user-suppplied equality function [eq]. *) |
| 590 |
Ltac2 rec nodup (eqb : 'a -> 'a -> bool) (ls : 'a list) : 'a list := |
| 591 |
match ls with |
| 592 |
| [] => [] |
| 593 |
| x :: xs |
| 594 |
=> match mem eqb x xs with |
| 595 |
| true => nodup eqb xs |
| 596 |
| false => x :: nodup eqb xs |
| 597 |
end |
| 598 |
end. |
| 599 |
|
| 600 |
(** [seq start step last] returns the list [start ; (start + step) ; (start + 2*step) ; ...], |
| 601 |
stopping when [start + n*step >= last]. In particular the lower bound [start] is included, |
| 602 |
whereas the upper bound [last] is excluded. *) |
| 603 |
Ltac2 rec seq (start : int) (step : int) (last : int) : int list := |
| 604 |
match Int.lt (Int.sub last start) step with |
| 605 |
| true |
| 606 |
=> [] |
| 607 |
| false |
| 608 |
=> start :: seq (Int.add start step) step last |
| 609 |
end. |
| 610 |
|
| 611 |
(** [init n f] returns the list [f 0 ; f 1 ; ... ; f (n-1)]. |
| 612 |
Throws an exception if [n < 0]. *) |
| 613 |
Ltac2 init (n : int) (f : int -> 'a) : 'a list := |
| 614 |
Control.assert_valid_argument "List.init" (Int.ge n 0); |
| 615 |
map f (seq 0 1 n). |
| 616 |
|
| 617 |
(** [repeat x n] returns the list of length [n] with all elements equal to [x]. *) |
| 618 |
Ltac2 repeat (x : 'a) (n : int) : 'a list := |
| 619 |
init n (fun _ => x). |
| 620 |
|
| 621 |
(** [assoc eqk k l] returns the first value associated to key [k] in an association list [l], |
| 622 |
using [eqk] to compare keys for equality. |
| 623 |
Throws an exception if no value is found. *) |
| 624 |
Ltac2 assoc (eqk : 'k -> 'k -> bool) (k : 'k) (l : ('k * 'v) list) : 'v := |
| 625 |
let eq_key kv := let (k', _) := kv in eqk k k' in |
| 626 |
let (_, v) := find eq_key l in |
| 627 |
v. |
| 628 |
|
| 629 |
(** [assoc eqk k l] returns the first value associated to key [k] in an association list [l], |
| 630 |
using [eqk] to compare keys for equality. |
| 631 |
Returns [None] if no value is found. *) |
| 632 |
Ltac2 assoc_opt (eqk : 'k -> 'k -> bool) (k : 'k) (l : ('k * 'v) list) : 'v option := |
| 633 |
let eq_key kv := let (k', _) := kv in eqk k k' in |
| 634 |
match find_opt eq_key l with |
| 635 |
| Some kv => let (_, v) := kv in Some v |
| 636 |
| None => None |
| 637 |
end. |
| 638 |
|
| 639 |
(** [mem_assoc eqk k l] checks if the key [k] is present in an association list [l], |
| 640 |
using [eqk] to compare keys for equality. *) |
| 641 |
Ltac2 mem_assoc (eqk : 'k -> 'k -> bool) (k : 'k) (l : ('k * 'v) list) : bool := |
| 642 |
let eq_key kv := let (k', _) := kv in eqk k k' in |
| 643 |
exist eq_key l. |
| 644 |
|
| 645 |
(** [remove_assoc eqk k l] removes _all_ key/value pairs associated with key [k] |
| 646 |
from an association list [l], using [eqk] to compare keys for equality. *) |
| 647 |
Ltac2 remove_assoc (eqk : 'k -> 'k -> bool) (k : 'k) (l : ('k * 'v) list) : ('k * 'v) list := |
| 648 |
let eq_key kv := let (k', _) := kv in eqk k k' in |
| 649 |
filter_out eq_key l. |
| 650 |
|
| 651 |
(** Split a list of pairs into a pair of lists. *) |
| 652 |
Ltac2 rec split (ls : ('a * 'b) list) : 'a list * 'b list := |
| 653 |
match ls with |
| 654 |
| [] => ([], []) |
| 655 |
| xy :: tl |
| 656 |
=> let (x, y) := xy in |
| 657 |
let (left, right) := split tl in |
| 658 |
((x::left), (y::right)) |
| 659 |
end. |
| 660 |
|
| 661 |
(** Combine two lists into a list of pairs. Only pairs of elements |
| 662 |
at the same position are considered. |
| 663 |
Throws an exception if the lengths of the lists differ. *) |
| 664 |
Ltac2 rec combine (ls1 : 'a list) (ls2 : 'b list) : ('a * 'b) list := |
| 665 |
match ls1 with |
| 666 |
| [] |
| 667 |
=> match ls2 with |
| 668 |
| [] => [] |
| 669 |
| _ :: _ => Control.throw_invalid_argument "List.combine" |
| 670 |
end |
| 671 |
| x :: xs |
| 672 |
=> match ls2 with |
| 673 |
| y :: ys |
| 674 |
=> (x, y) :: combine xs ys |
| 675 |
| [] => Control.throw_invalid_argument "List.combine" |
| 676 |
end |
| 677 |
end. |
| 678 |
|
| 679 |
(** [enumerate [x0 ; x1 ; x2 ; ...]] returns the list [(0, x0) ; (1, x1) ; (2, x2) ; ...]. *) |
| 680 |
Ltac2 enumerate (ls : 'a list) := |
| 681 |
mapi (fun i v => (i,v)) ls. |
| 682 |
|
| 683 |
(** Merge two list sorted in increasing order into a sorted list, |
| 684 |
using a user-provided comparison function. *) |
| 685 |
Ltac2 rec merge (cmp : 'a -> 'a -> int) (l1 : 'a list) (l2 : 'a list) : 'a list := |
| 686 |
let rec merge_aux l2 := |
| 687 |
match l1 with |
| 688 |
| [] => l2 |
| 689 |
| a1 :: l1' |
| 690 |
=> match l2 with |
| 691 |
| [] => l1 |
| 692 |
| a2 :: l2' |
| 693 |
=> match Int.le (cmp a1 a2) 0 with |
| 694 |
| true => a1 :: merge cmp l1' l2 |
| 695 |
| false => a2 :: merge_aux l2' |
| 696 |
end |
| 697 |
end |
| 698 |
end in |
| 699 |
merge_aux l2. |
| 700 |
|
| 701 |
Ltac2 rec merge_list_to_stack cmp stack l := |
| 702 |
match stack with |
| 703 |
| [] => [Some l] |
| 704 |
| l' :: stack' |
| 705 |
=> match l' with |
| 706 |
| None => Some l :: stack' |
| 707 |
| Some l' |
| 708 |
=> None :: merge_list_to_stack cmp stack' (merge cmp l' l) |
| 709 |
end |
| 710 |
end. |
| 711 |
|
| 712 |
Ltac2 rec merge_stack cmp stack := |
| 713 |
match stack with |
| 714 |
| [] => [] |
| 715 |
| l :: stack' |
| 716 |
=> match l with |
| 717 |
| None => merge_stack cmp stack' |
| 718 |
| Some l => merge cmp l (merge_stack cmp stack') |
| 719 |
end |
| 720 |
end. |
| 721 |
|
| 722 |
Ltac2 rec iter_merge cmp stack l := |
| 723 |
match l with |
| 724 |
| [] => merge_stack cmp stack |
| 725 |
| a::l' => iter_merge cmp (merge_list_to_stack cmp stack [a]) l' |
| 726 |
end. |
| 727 |
|
| 728 |
(** [sort cmp l] sorts the list [l] in increasing order, |
| 729 |
according to the user-supplied comparison function [cmp]. |
| 730 |
|
| 731 |
It is currently implemented using a variant of merge sort. *) |
| 732 |
Ltac2 sort (cmp : 'a -> 'a -> int) (l : 'a list) : 'a list := |
| 733 |
iter_merge cmp [] l. |
| 734 |
|
| 735 |
(** [sort cmp l] sorts the list [l] in increasing order and removes duplicates, |
| 736 |
according to the user-supplied comparison function [cmp]. |
| 737 |
|
| 738 |
This takes advantage of the fact that removing duplicates from a sorted list |
| 739 |
is much faster than for an arbitrary list. *) |
| 740 |
Ltac2 sort_uniq (cmp : 'a -> 'a -> int) (l : 'a list) : 'a list := |
| 741 |
let rec uniq l := |
| 742 |
match l with |
| 743 |
| [] => [] |
| 744 |
| x1 :: xs |
| 745 |
=> match xs with |
| 746 |
| [] => x1 :: xs |
| 747 |
| x2 :: _ |
| 748 |
=> match Int.equal (cmp x1 x2) 0 with |
| 749 |
| true => uniq xs |
| 750 |
| false => x1 :: uniq xs |
| 751 |
end |
| 752 |
end |
| 753 |
end in |
| 754 |
uniq (sort cmp l). |
| 755 |
|
| 756 |
(** [inclusive_range lb ub] returns the list [lb ; lb+1 ; lb+2 ; ... ; up]. |
| 757 |
In particular both the lower bound [lb] and upper bound [ub] are included. *) |
| 758 |
Ltac2 inclusive_range (lb : int) (ub : int) : int list := |
| 759 |
seq lb 1 (Int.add ub 1). |
| 760 |
|
| 761 |
(** [range lb ub] returns the list [lb ; lb+1 ; lb+2 ; ... ; up-1]. |
| 762 |
In particular the lower bound [lb] is included, whereas the upper bound [ub] is excluded. *) |
| 763 |
Ltac2 range (lb : int) (ub : int) : int list := |
| 764 |
seq lb 1 ub. |
| 765 |
|
| 766 |
(** [concat_rev [x1; ..; xN-1; xN]] computes [rev xN ++ rev xN-1 ++ .. ++ x1]. |
| 767 |
Note that [x1] is not reversed and appears in its original order. |
| 768 |
|
| 769 |
[concat_rev] is faster than [concat] and should be preferred over [concat] |
| 770 |
when the order of items does not matter. *) |
| 771 |
Ltac2 concat_rev (ls : 'a list list) : 'a list := |
| 772 |
let rec go ls acc := |
| 773 |
match ls with |
| 774 |
| [] => acc |
| 775 |
| l :: ls => go ls (rev_append l acc) |
| 776 |
end |
| 777 |
in |
| 778 |
match ls with |
| 779 |
| [] => [] |
| 780 |
| l :: ls => go ls l |
| 781 |
end. |
| 782 |
|
| 783 |
(** [map_filter f l] maps [f] over [l], then removes all elements that are [None]. *) |
| 784 |
Ltac2 rec map_filter (f : 'a -> 'b option) (l : 'a list) : 'b list := |
| 785 |
match l with |
| 786 |
| [] => [] |
| 787 |
| x :: l => |
| 788 |
match f x with |
| 789 |
| Some y => y :: map_filter f l |
| 790 |
| None => map_filter f l |
| 791 |
end |
| 792 |
end. |