Array literal syntax #
Equations
Preliminary theorems #
Equations
- Array.instMembership = { mem := Array.Mem }
Equations
Equations
Equations
Equations
Equations
Externs #
Low-level version of size
that directly queries the C array object cached size.
While this is not provable, usize
always returns the exact size of the array since
the implementation only supports arrays of size less than USize.size
.
Equations
- mkArray n v = { toList := List.replicate n v }
Swaps two entries in an array.
This will perform the update destructively provided that a
has a reference
count of 1 when called.
Swaps two entries in an array, or returns the array unchanged if either index is out of bounds.
This will perform the update destructively provided that a
has a reference
count of 1 when called.
Equations
Definitions #
Equations
- Array.instEmptyCollection = { emptyCollection := #[] }
Equations
- Array.instInhabited = { default := #[] }
ofFn f
with f : Fin n → α
returns the list whose ith element is f i
.
ofFn f = #[f 0, f 1, ... , f(n - 1)]
Equations
- Array.ofFn f = Array.ofFn.go f 0 (Array.mkEmpty n)
The array #[0, 1, ..., n - 1]
.
Equations
- Array.range n = Array.ofFn fun (i : Fin n) => ↑i
The array #[start, start + step, ..., start + step * (size - 1)]
.
Equations
- Array.range' start size step = Array.ofFn fun (i : Fin size) => start + step * ↑i
Equations
- Array.shrink.loop 0 x✝ = x✝
- Array.shrink.loop n.succ x✝ = Array.shrink.loop n x✝.pop
We claim this unsafe implementation is correct because an array cannot have more than usizeSz
elements in our runtime.
This kind of low level trick can be removed with a little bit of compiler support. For example, if the compiler simplifies as.size < usizeSz
to true.
Equations
- as.forIn'Unsafe b f = Array.forIn'Unsafe.loop as f as.usize 0 b
Equations
- Array.instForIn'InferInstanceMembership = { forIn' := fun {β : Type ?u.21} [Monad m] => Array.forIn' }
See comment at forIn'Unsafe
Equations
- Array.foldlMUnsafe f init as start stop = if start < stop then if stop ≤ as.size then Array.foldlMUnsafe.fold f as (USize.ofNat start) (USize.ofNat stop) init else pure init else pure init
Reference implementation for foldlM
Equations
- One or more equations did not get rendered due to their size.
Reference implementation for foldrM
Equations
- One or more equations did not get rendered due to their size.
See comment at forIn'Unsafe
Equations
- Array.mapMUnsafe f as = unsafeCast (Array.mapMUnsafe.map f as.usize 0 (unsafeCast as))
Reference implementation for mapM
Equations
- Array.mapM f as = Array.mapM.map f as 0 (Array.mkEmpty as.size)
Variant of mapIdxM
which receives the index i
along with the bound i < as.size
.
Equations
- as.mapFinIdxM f = Array.mapFinIdxM.map as f as.size 0 ⋯ (Array.mkEmpty as.size)
Equations
- Array.mapFinIdxM.map as f 0 j x bs = pure bs
- Array.mapFinIdxM.map as f i_2.succ j inv_2 bs = do let __do_lift ← f j as[j] ⋯ Array.mapFinIdxM.map as f i_2 (j + 1) ⋯ (bs.push __do_lift)
Equations
- Array.firstM f as = Array.firstM.go f as 0
Equations
- Array.firstM.go f as i = if hlt : i < as.size then f as[i] <|> Array.firstM.go f as (i + 1) else failure
Note that the universe level is contrained to Type
here,
to avoid having to have the predicate live in p : α → m (ULift Bool)
.
Equations
- One or more equations did not get rendered due to their size.
Equations
- Array.allM p as start stop = do let __do_lift ← Array.anyM (fun (v : α) => do let __do_lift ← p v pure !__do_lift) as start stop pure !__do_lift
Equations
- Array.findSomeRevM? f as = Array.findSomeRevM?.find f as as.size ⋯
Equations
- Array.findSomeRevM?.find f as 0 x_2 = pure none
- Array.findSomeRevM?.find f as i.succ h = do let r ← f as[i] match r with | some val => pure r | none => let_fun this := ⋯; Array.findSomeRevM?.find f as i this
Equations
- Array.forM f as start stop = Array.foldlM (fun (x : PUnit) => f) PUnit.unit as start stop
Equations
- Array.instForM = { forM := fun [Monad m] (xs : Array α) (f : α → m PUnit) => Array.forM f xs }
Equations
- Array.forRevM f as start stop = Array.foldrM (fun (a : α) (x : PUnit) => f a) PUnit.unit as start stop
Equations
- Array.foldl f init as start stop = (Array.foldlM f init as start stop).run
Equations
- Array.foldr f init as start stop = (Array.foldrM f init as start stop).run
Equations
- Array.countP p as = Array.foldr (fun (a : α) (acc : Nat) => bif p a then acc + 1 else acc) 0 as
Equations
- Array.count a as = Array.countP (fun (x : α) => x == a) as
Equations
- Array.instFunctor = { map := fun {α β : Type ?u.10} => Array.map, mapConst := fun {α β : Type ?u.10} => Array.map ∘ Function.const β }
Equations
- Array.mapIdx f as = (Array.mapIdxM f as).run
Equations
Equations
- Array.findSome? f as = (Array.findSomeM? f as).run
Equations
- Array.findSome! f xs = match Array.findSome? f xs with | some b => b | none => panicWithPosWithDecl "Init.Data.Array.Basic" "Array.findSome!" 708 14 "failed to find element"
Equations
- Array.findSomeRev? f as = (Array.findSomeRevM? f as).run
Equations
- Array.findRev? p as = (Array.findRevM? p as).run
Equations
- Array.findIdx? p as = Array.findIdx?.loop p as 0
Equations
- Array.findFinIdx? p as = Array.findFinIdx?.loop p as 0
Equations
- Array.findIdx p as = (Array.findIdx? p as).getD as.size
Equations
Equations
Returns the index of the first element equal to a
, or the length of the array otherwise.
Equations
- Array.idxOf a = Array.findIdx fun (x : α) => x == a
Variant of Array.contains
with arguments reversed.
For verification purposes, we simplify this to contains
.
Equations
- Array.elem a as = as.contains a
Convert a Array α
into an List α
. This is O(n) in the size of the array.
Equations
- as.toListImpl = Array.foldr List.cons [] as
Prepends an Array α
onto the front of a list. Equivalent to as.toList ++ l
.
Equations
- as.toListAppend l = Array.foldr List.cons l as
Equations
- as.append bs = Array.foldl (fun (xs : Array α) (v : α) => xs.push v) as bs
Equations
- Array.instAppend = { append := Array.append }
Equations
- as.appendList bs = List.foldl (fun (xs : Array α) (v : α) => xs.push v) as bs
Equations
- Array.instHAppendList = { hAppend := Array.appendList }
Equations
- Array.flatMap f as = Array.foldl (fun (bs : Array β) (a : α) => bs ++ f a) #[] as
Equations
Equations
- Array.reverse.loop as i j = if h : i < ↑j then let_fun this := ⋯; let as_1 := as.swap i ↑j ⋯ ⋯; let_fun this := ⋯; Array.reverse.loop as_1 (i + 1) ⟨↑j - 1, this⟩ else as
Equations
- Array.filterMap f as start stop = (Array.filterMapM f as start stop).run
Equations
- Array.takeWhile p as = Array.takeWhile.go p as 0 #[]
Remove the element at a given index from an array without a runtime bounds checks,
using a Nat
index and a tactic-provided bound.
This function takes worst case O(n) time because
it has to backshift all elements at positions greater than i
.
Remove the element at a given index from an array, or do nothing if the index is out of bounds.
This function takes worst case O(n) time because
it has to backshift all elements at positions greater than i
.
Equations
- xs.eraseIdxIfInBounds i = if h : i < xs.size then xs.eraseIdx i h else xs
Remove the element at a given index from an array, or panic if the index is out of bounds.
This function takes worst case O(n) time because
it has to backshift all elements at positions greater than i
.
Remove a specified element from an array, or do nothing if it is not present.
This function takes worst case O(n) time because it has to backshift all later elements.
Erase the first element that satisfies the predicate p
.
This function takes worst case O(n) time because it has to backshift all later elements.
Insert element a
at position i
.
This function takes worst case O(n) time because it has to swap the inserted element into place.
Equations
- as.insertIdx i a x✝ = Array.insertIdx.loop i (as.push a) ⟨as.size, ⋯⟩
Equations
Insert element a
at position i
. Panics if i
is not i ≤ as.size
.
Equations
- as.insertIdx! i a = if h : i ≤ as.size then as.insertIdx i a h else panicWithPosWithDecl "Init.Data.Array.Basic" "Array.insertIdx!" 1023 7 "invalid index"
Equations
Return true iff as
is a prefix of bs
.
That is, bs = as ++ t
for some t : List α
.
Equations
- as.isPrefixOf bs = if h : as.size ≤ bs.size then as.isPrefixOfAux bs h 0 else false
Equations
- Array.zipWith f as bs = as.zipWithAux bs f 0 #[]
Equations
- Array.zipWithAll f as bs = Array.zipWithAll.go f as bs 0 #[]
Lexicographic ordering #
Auxiliary functions used in metaprogramming. #
We do not currently intend to provide verification theorems for these functions.
leftpad and rightpad #
Drop none
s from a Array, and replace each remaining some a
with a
.
Equations
- as.reduceOption = Array.filterMap id as
eraseReps #
allDiff #
Equations
- as.allDiff = Array.allDiffAux✝ as 0
getEvenElems #
Equations
- One or more equations did not get rendered due to their size.