| 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 |
(* This is mostly a translation of OCaml stdlib/array.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 |
(** This module implements fixed sized, mutable arrays. |
| 35 |
|
| 36 |
Unless specified otherwise, all functions defined below throw |
| 37 |
an exception in case of out of bounds indices, and arrays are indexed |
| 38 |
starting at [0]. *) |
| 39 |
|
| 40 |
(** The empty array. *) |
| 41 |
Ltac2 @external empty : 'a array := "rocq-runtime.plugins.ltac2" "array_empty". |
| 42 |
|
| 43 |
(** [make n x] creates an array of length [n], with all elements set to [x]. *) |
| 44 |
Ltac2 @external make : int -> 'a -> 'a array := "rocq-runtime.plugins.ltac2" "array_make". |
| 45 |
|
| 46 |
(** Return the length of an array. *) |
| 47 |
Ltac2 @external length : 'a array -> int := "rocq-runtime.plugins.ltac2" "array_length". |
| 48 |
|
| 49 |
(** [get arr i] returns the [i]-th element of [arr]. *) |
| 50 |
Ltac2 @external get : 'a array -> int -> 'a := "rocq-runtime.plugins.ltac2" "array_get". |
| 51 |
|
| 52 |
(** [set arr i x] replaces [arr[i]] with [x]. This modifies [arr] in place. *) |
| 53 |
Ltac2 @external set : 'a array -> int -> 'a -> unit := "rocq-runtime.plugins.ltac2" "array_set". |
| 54 |
|
| 55 |
(** [lowlevel_blit from fofs to tofs len] copies [from[fofs...fofs+len-1]] to [to[tofs...tofs+len-1]] (bounds included). |
| 56 |
This modifies [to] in place. |
| 57 |
|
| 58 |
Consider using [blit] for nicer error messages. *) |
| 59 |
Ltac2 @external lowlevel_blit : 'a array -> int -> 'a array -> int -> int -> unit := "rocq-runtime.plugins.ltac2" "array_blit". |
| 60 |
|
| 61 |
(** [lowlevel_fill arr ofs len x] replaces [arr[ofs]], [arr[ofs+1]], ..., [arr[ofs+len-1]] with [x]. |
| 62 |
This modifes [arr] in place. |
| 63 |
|
| 64 |
Consider using [fill] for nicer error messages. *) |
| 65 |
Ltac2 @external lowlevel_fill : 'a array -> int -> int -> 'a -> unit := "rocq-runtime.plugins.ltac2" "array_fill". |
| 66 |
|
| 67 |
(** [concat [a1 ; a2 ; ... ; aN]] returns the array [a1 ++ a2 ++ ... ++ an]. *) |
| 68 |
Ltac2 @external concat : ('a array) list -> 'a array := "rocq-runtime.plugins.ltac2" "array_concat". |
| 69 |
|
| 70 |
(** [lowlevel_sub arr ofs len] returns the subarray [arr[ofs...ofs+len-1]]. |
| 71 |
The result is allocated as a new array, i.e. is not aliased with [arr]. |
| 72 |
|
| 73 |
Consider using [sub] for nicer error messages. *) |
| 74 |
Ltac2 lowlevel_sub (arr : 'a array) (start : int) (len : int) : 'a array := |
| 75 |
let l := length arr in |
| 76 |
match Int.equal l 0 with |
| 77 |
| true => empty |
| 78 |
| false => |
| 79 |
let newarr:=make len (get arr 0) in |
| 80 |
lowlevel_blit arr start newarr 0 len; |
| 81 |
newarr |
| 82 |
end. |
| 83 |
|
| 84 |
(** [init n f] creates the array [| f 0 ; f 1 ; ... ; f (n-1) |]. *) |
| 85 |
Ltac2 init (l : int) (f : int -> 'a) : 'a array := |
| 86 |
let rec init_aux (dst : 'a array) (pos : int) (len : int) (f : int->'a) := |
| 87 |
match Int.equal len 0 with |
| 88 |
| true => () |
| 89 |
| false => |
| 90 |
set dst pos (f pos); |
| 91 |
init_aux dst (Int.add pos 1) (Int.sub len 1) f |
| 92 |
end |
| 93 |
in |
| 94 |
match Int.le l 0 with |
| 95 |
| true => empty |
| 96 |
| false => |
| 97 |
let arr:=make l (f 0) in |
| 98 |
init_aux arr 1 (Int.sub l 1) f; |
| 99 |
arr |
| 100 |
end. |
| 101 |
|
| 102 |
(** [make_matrix sx sy v] creates an array of size [sx] |
| 103 |
which contains array of size [sy], filled with [v]. *) |
| 104 |
Ltac2 make_matrix (sx : int) (sy : int) (v : 'a) : 'a array array := |
| 105 |
let initr _ := make sy v in |
| 106 |
init sx initr. |
| 107 |
|
| 108 |
(** Copy an array. The copied array can be modified without |
| 109 |
changing the original array (and vice versa). *) |
| 110 |
Ltac2 copy (a : 'a array) : 'a array := |
| 111 |
lowlevel_sub a 0 (length a). |
| 112 |
|
| 113 |
(** [append a1 a2] appends [a1] and [a2]. |
| 114 |
This allocates a new array, even if [a1] or [a2] is empty. *) |
| 115 |
Ltac2 append (a1 : 'a array) (a2 : 'a array) : 'a array := |
| 116 |
match Int.equal (length a1) 0 with |
| 117 |
| true => copy a2 |
| 118 |
| false => match Int.equal (length a2) 0 with |
| 119 |
| true => copy a1 |
| 120 |
| false => |
| 121 |
let newarr:=make (Int.add (length a1) (length a2)) (get a1 0) in |
| 122 |
lowlevel_blit a1 0 newarr 0 (length a1); |
| 123 |
lowlevel_blit a2 0 newarr (length a1) (length a2); |
| 124 |
newarr |
| 125 |
end |
| 126 |
end. |
| 127 |
|
| 128 |
(** [sub arr ofs len] returns the subarray [arr[ofs...ofs+len-1]]. |
| 129 |
The result is allocated as a new array, i.e. is not aliased with [arr]. *) |
| 130 |
Ltac2 sub (a : 'a array) (ofs : int) (len : int) : 'a array := |
| 131 |
Control.assert_valid_argument "Array.sub ofs<0" (Int.ge ofs 0); |
| 132 |
Control.assert_valid_argument "Array.sub len<0" (Int.ge len 0); |
| 133 |
Control.assert_bounds "Array.sub" (Int.le ofs (Int.sub (length a) len)); |
| 134 |
lowlevel_sub a ofs len. |
| 135 |
|
| 136 |
(** [fill arr ofs len x] replaces [arr[ofs]], [arr[ofs+1]], ..., [arr[ofs+len-1]] with [x]. |
| 137 |
This modifes [arr] in place. *) |
| 138 |
Ltac2 fill (a : 'a array) (ofs : int) (len : int) (v : 'a) : unit := |
| 139 |
Control.assert_valid_argument "Array.fill ofs<0" (Int.ge ofs 0); |
| 140 |
Control.assert_valid_argument "Array.fill len<0" (Int.ge len 0); |
| 141 |
Control.assert_bounds "Array.fill" (Int.le ofs (Int.sub (length a) len)); |
| 142 |
lowlevel_fill a ofs len v. |
| 143 |
|
| 144 |
(** [blit from fofs to tofs len] copies [from[fofs...fofs+len-1]] to [to[tofs...tofs+len-1]] (bounds included). |
| 145 |
This modifies [to] in place. *) |
| 146 |
Ltac2 blit (a1 : 'a array) (ofs1 : int) (a2 : 'a array) (ofs2 : int) (len : int) : unit := |
| 147 |
Control.assert_valid_argument "Array.blit ofs1<0" (Int.ge ofs1 0); |
| 148 |
Control.assert_valid_argument "Array.blit ofs2<0" (Int.ge ofs2 0); |
| 149 |
Control.assert_valid_argument "Array.blit len<0" (Int.ge len 0); |
| 150 |
Control.assert_bounds "Array.blit ofs1+len>len a1" (Int.le ofs1 (Int.sub (length a1) len)); |
| 151 |
Control.assert_bounds "Array.blit ofs2+len>len a2" (Int.le ofs2 (Int.sub (length a2) len)); |
| 152 |
lowlevel_blit a1 ofs1 a2 ofs2 len. |
| 153 |
|
| 154 |
(** Helper function for [iter]. *) |
| 155 |
Ltac2 rec iter_aux (f : 'a -> unit) (a : 'a array) (pos : int) (len : int) : unit := |
| 156 |
match Int.equal len 0 with |
| 157 |
| true => () |
| 158 |
| false => f (get a pos); iter_aux f a (Int.add pos 1) (Int.sub len 1) |
| 159 |
end. |
| 160 |
|
| 161 |
(** [iter f arr] calls [f] on every element of [arr], from first to last. *) |
| 162 |
Ltac2 iter (f : 'a -> unit) (a : 'a array) : unit := |
| 163 |
iter_aux f a 0 (length a). |
| 164 |
|
| 165 |
(** Helper function for [iter2]. *) |
| 166 |
Ltac2 rec iter2_aux (f : 'a -> 'b -> unit) (a : 'a array) (b : 'b array) (pos : int) (len : int) : unit := |
| 167 |
match Int.equal len 0 with |
| 168 |
| true => () |
| 169 |
| false => f (get a pos) (get b pos); iter2_aux f a b (Int.add pos 1) (Int.sub len 1) |
| 170 |
end. |
| 171 |
|
| 172 |
(** Same as [iter] but with two arrays. |
| 173 |
Throws an exception when the lengths of the arrays differ. *) |
| 174 |
Ltac2 rec iter2 (f : 'a -> 'b -> unit) (a : 'a array) (b : 'b array) : unit := |
| 175 |
Control.assert_valid_argument "Array.iter2" (Int.equal (length a) (length b)); |
| 176 |
iter2_aux f a b 0 (length a). |
| 177 |
|
| 178 |
(** Map a function over an array. Elements are processed from first to last. |
| 179 |
The result is allocated as a new array, i.e. it does not alias the original array. *) |
| 180 |
Ltac2 map (f : 'a -> 'b) (a : 'a array) : 'b array := |
| 181 |
init (length a) (fun i => f (get a i)). |
| 182 |
|
| 183 |
(** Same as [map] but with two arrays. |
| 184 |
Throws an exception when the lengths of the arrays differ. *) |
| 185 |
Ltac2 map2 (f : 'a -> 'b -> 'c) (a : 'a array) (b : 'b array) : 'c array := |
| 186 |
Control.assert_valid_argument "Array.map2" (Int.equal (length a) (length b)); |
| 187 |
init (length a) (fun i => f (get a i) (get b i)). |
| 188 |
|
| 189 |
(** Helper function for [iteri]. *) |
| 190 |
Ltac2 rec iteri_aux (f : int -> 'a -> unit) (a : 'a array) (pos : int) (len : int) : unit := |
| 191 |
match Int.equal len 0 with |
| 192 |
| true => () |
| 193 |
| false => f pos (get a pos); iteri_aux f a (Int.add pos 1) (Int.sub len 1) |
| 194 |
end. |
| 195 |
|
| 196 |
(** Iterate a function over an array, passing the index of each argument to the function. |
| 197 |
Elements are processed from first to last. *) |
| 198 |
Ltac2 iteri (f : int -> 'a -> unit) (a : 'a array) : unit := |
| 199 |
iteri_aux f a 0 (length a). |
| 200 |
|
| 201 |
(** Map a function over an array, passing the index of each argument to the function. |
| 202 |
Elements are processed from first to last. The result is allocated as a new array, |
| 203 |
i.e. it does not alias the original array. *) |
| 204 |
Ltac2 mapi (f : int -> 'a -> 'b) (a : 'a array) : 'b array := |
| 205 |
init (length a) (fun i => f i (get a i)). |
| 206 |
|
| 207 |
(** Helper function to [to_list]. *) |
| 208 |
Ltac2 rec to_list_aux (a : 'a array) (pos : int) (len : int) : 'a list := |
| 209 |
match Int.equal len 0 with |
| 210 |
| true => [] |
| 211 |
| false => get a pos :: to_list_aux a (Int.add pos 1) (Int.sub len 1) |
| 212 |
end. |
| 213 |
|
| 214 |
(** Convert an array to a list. *) |
| 215 |
Ltac2 to_list (a : 'a array) : 'a list := |
| 216 |
to_list_aux a 0 (length a). |
| 217 |
|
| 218 |
(** Helper function to [of_list]. *) |
| 219 |
Ltac2 rec of_list_aux (ls : 'a list) (dst : 'a array) (pos : int) : unit := |
| 220 |
match ls with |
| 221 |
| [] => () |
| 222 |
| hd::tl => |
| 223 |
set dst pos hd; |
| 224 |
of_list_aux tl dst (Int.add pos 1) |
| 225 |
end. |
| 226 |
|
| 227 |
(** Convert a list into an array. *) |
| 228 |
Ltac2 of_list (ls : 'a list) : 'a array := |
| 229 |
(* Don't use List.length here because the List module might depend on Array some day *) |
| 230 |
let rec list_length (ls : 'a list) := |
| 231 |
match ls with |
| 232 |
| [] => 0 |
| 233 |
| _ :: tl => Int.add 1 (list_length tl) |
| 234 |
end in |
| 235 |
match ls with |
| 236 |
| [] => empty |
| 237 |
| hd :: _ => |
| 238 |
let anew := make (list_length ls) hd in |
| 239 |
of_list_aux ls anew 0; |
| 240 |
anew |
| 241 |
end. |
| 242 |
|
| 243 |
(** Helper function for [fold_left]. *) |
| 244 |
Ltac2 rec fold_left_aux (f : 'a -> 'b -> 'a) (x : 'a) (a : 'b array) (pos : int) (len : int) : 'a := |
| 245 |
match Int.equal len 0 with |
| 246 |
| true => x |
| 247 |
| false => fold_left_aux f (f x (get a pos)) a (Int.add pos 1) (Int.sub len 1) |
| 248 |
end. |
| 249 |
|
| 250 |
(** Left fold over an array. *) |
| 251 |
Ltac2 fold_left (f : 'a -> 'b -> 'a) (x : 'a) (a : 'b array) : 'a := |
| 252 |
fold_left_aux f x a 0 (length a). |
| 253 |
|
| 254 |
(** Helper function for [fold_right]. *) |
| 255 |
Ltac2 rec fold_right_aux (f : 'a -> 'b -> 'b) (a : 'a array) (x : 'b) (pos : int) (len : int) : 'b := |
| 256 |
(* Note: one could compare pos<0. |
| 257 |
We keep an extra len parameter so that the function can be used for any sub array *) |
| 258 |
match Int.equal len 0 with |
| 259 |
| true => x |
| 260 |
| false => fold_right_aux f a (f (get a pos) x) (Int.sub pos 1) (Int.sub len 1) |
| 261 |
end. |
| 262 |
|
| 263 |
(** Right fold over an array. *) |
| 264 |
Ltac2 fold_right (f : 'a -> 'b -> 'b) (a : 'a array) (x : 'b) : 'b := |
| 265 |
fold_right_aux f a x (Int.sub (length a) 1) (length a). |
| 266 |
|
| 267 |
(** Helper function for [exist]. *) |
| 268 |
Ltac2 rec exist_aux (p : 'a -> bool) (a : 'a array) (pos : int) (len : int) : bool := |
| 269 |
match Int.equal len 0 with |
| 270 |
| true => false |
| 271 |
| false => match p (get a pos) with |
| 272 |
| true => true |
| 273 |
| false => exist_aux p a (Int.add pos 1) (Int.sub len 1) |
| 274 |
end |
| 275 |
end. |
| 276 |
|
| 277 |
(** [exist f arr] checks if [f] is true for _at least one_ element of [arr]. |
| 278 |
In particular [exist f empty] is [false]. |
| 279 |
|
| 280 |
We would call this [exists] a la OCaml's [List.exists], |
| 281 |
but that would be a syntax error (because it conflicts with the notation for tactic `exists`), so instead we name it exist. *) |
| 282 |
Ltac2 exist (p : 'a -> bool) (a : 'a array) : bool := |
| 283 |
exist_aux p a 0 (length a). |
| 284 |
|
| 285 |
(** Helper function for [for_all]. *) |
| 286 |
Ltac2 rec for_all_aux (p : 'a -> bool) (a : 'a array) (pos : int) (len : int) : bool := |
| 287 |
match Int.equal len 0 with |
| 288 |
| true => true |
| 289 |
| false => match p (get a pos) with |
| 290 |
| true => for_all_aux p a (Int.add pos 1) (Int.sub len 1) |
| 291 |
| false => false |
| 292 |
end |
| 293 |
end. |
| 294 |
|
| 295 |
(** [for_all f arr] checks if [f] is true for _all_ elements of [arr]. |
| 296 |
In particular [for_all f empty] is [true]. *) |
| 297 |
Ltac2 for_all (p : 'a -> bool) (a : 'a array) : bool := |
| 298 |
for_all_aux p a 0 (length a). |
| 299 |
|
| 300 |
(** [mem eq x arr] checks if an element of [arr] is equal to [x] according |
| 301 |
to the user-supplied equality function [eq] *) |
| 302 |
Ltac2 mem (eq : 'a -> 'a -> bool) (x : 'a) (a : 'a array) : bool := |
| 303 |
exist (eq x) a. |
| 304 |
|
| 305 |
(** Helper function for [for_all2]. *) |
| 306 |
Ltac2 rec for_all2_aux (p : 'a -> 'b -> bool) (a : 'a array) (b : 'b array) (pos : int) (len : int) : bool := |
| 307 |
if Int.equal len 0 |
| 308 |
then true |
| 309 |
else if p (get a pos) (get b pos) |
| 310 |
then for_all2_aux p a b (Int.add pos 1) (Int.sub len 1) |
| 311 |
else false. |
| 312 |
|
| 313 |
(** Same as [for_all] but for two lists. |
| 314 |
Throws an exception when the lengths of the lists differ. *) |
| 315 |
Ltac2 for_all2 (p : 'a -> 'b -> bool) (a : 'a array) (b : 'b array) : bool := |
| 316 |
let lena := length a in |
| 317 |
let lenb := length b in |
| 318 |
if Int.equal lena lenb |
| 319 |
then for_all2_aux p a b 0 lena |
| 320 |
else Control.throw_invalid_argument "Array.for_all2". |
| 321 |
|
| 322 |
(** Same as [for_all] but for two lists. |
| 323 |
Returns [false] when the lengths of the lists differ. *) |
| 324 |
Ltac2 equal (p : 'a -> 'b -> bool) (a : 'a array) (b : 'b array) : bool := |
| 325 |
let lena := length a in |
| 326 |
let lenb := length b in |
| 327 |
if Int.equal lena lenb |
| 328 |
then for_all2_aux p a b 0 lena |
| 329 |
else false. |
| 330 |
|
| 331 |
(** Reverse an array. The result is allocated as a new array, |
| 332 |
i.e. it does not alias the original array. *) |
| 333 |
Ltac2 rev (ar : 'a array) : 'a array := |
| 334 |
let len := length ar in |
| 335 |
init len (fun i => get ar (Int.sub (Int.sub len i) 1)). |