Documentation

Init.Data.Array.Basic

Array literal syntax #

Syntax for Array α.

Conventions for notations in identifiers:

  • The recommended spelling of #[] in identifiers is empty.

  • The recommended spelling of #[x] in identifiers is singleton.

Equations
  • One or more equations did not get rendered due to their size.
@[reducible, inline, deprecated Array.toList (since := "2024-10-13")]
abbrev Array.data {α : Type u_1} (self : Array α) :
List α
Equations

Preliminary theorems #

@[simp]
theorem Array.size_set {α : Type u} (xs : Array α) (i : Nat) (v : α) (h : i < xs.size) :
(xs.set i v h).size = xs.size
@[simp]
theorem Array.size_push {α : Type u} (xs : Array α) (v : α) :
(xs.push v).size = xs.size + 1
theorem Array.ext {α : Type u} (xs ys : Array α) (h₁ : xs.size = ys.size) (h₂ : ∀ (i : Nat) (hi₁ : i < xs.size) (hi₂ : i < ys.size), xs[i] = ys[i]) :
xs = ys
theorem Array.ext.extAux {α : Type u} (as bs : List α) (h₁ : as.length = bs.length) (h₂ : ∀ (i : Nat) (hi₁ : i < as.length) (hi₂ : i < bs.length), as[i] = bs[i]) :
as = bs
theorem Array.ext' {α : Type u} {xs ys : Array α} (h : xs.toList = ys.toList) :
xs = ys
@[simp]
theorem Array.toArrayAux_eq {α : Type u} (as : List α) (acc : Array α) :
(as.toArrayAux acc).toList = acc.toList ++ as
@[simp]
theorem Array.toArray_toList {α : Type u} (xs : Array α) :
@[simp]
theorem Array.getElem_toList {α : Type u} {xs : Array α} {i : Nat} (h : i < xs.size) :
xs.toList[i] = xs[i]
@[simp]
theorem Array.getElem?_toList {α : Type u} {xs : Array α} {i : Nat} :
xs.toList[i]? = xs[i]?
structure Array.Mem {α : Type u} (as : Array α) (a : α) :

a ∈ as is a predicate which asserts that a is in the array as.

instance Array.instMembership {α : Type u} :
Equations
theorem Array.mem_def {α : Type u} {a : α} {as : Array α} :
a as a as.toList
@[simp]
theorem Array.mem_toArray {α : Type u} {a : α} {l : List α} :
a l.toArray a l
@[simp]
theorem Array.getElem_mem {α : Type u} {xs : Array α} {i : Nat} (h : i < xs.size) :
xs[i] xs
@[reducible, inline, deprecated Array.toArray_toList (since := "2025-02-17")]
abbrev List.toArray_toList {α : Type u_1} (xs : Array α) :
Equations
theorem List.toList_toArray {α : Type u} (as : List α) :
@[reducible, inline, deprecated List.toList_toArray (since := "2025-02-17")]
abbrev Array.toList_toArray {α : Type u_1} (as : List α) :
Equations
@[simp]
theorem List.size_toArray {α : Type u} (as : List α) :
@[reducible, inline, deprecated List.size_toArray (since := "2025-02-17")]
abbrev Array.size_toArray {α : Type u_1} (as : List α) :
Equations
@[simp]
theorem List.getElem_toArray {α : Type u} {xs : List α} {i : Nat} (h : i < xs.toArray.size) :
xs.toArray[i] = xs[i]
@[simp]
theorem List.getElem?_toArray {α : Type u} {xs : List α} {i : Nat} :
@[simp]
theorem List.getElem!_toArray {α : Type u} [Inhabited α] {xs : List α} {i : Nat} :
theorem Array.size_eq_length_toList {α : Type u} (xs : Array α) :
@[reducible, inline, deprecated Array.toList_toArray (since := "2024-09-09")]
abbrev Array.data_toArray {α : Type u_1} (as : List α) :
Equations
@[reducible, inline, deprecated Array.toList (since := "2024-09-10")]
abbrev Array.Array.data {α : Type u_1} (self : Array α) :
List α
Equations

Externs #

@[extern lean_array_size]
def Array.usize {α : Type u} (a : Array α) :

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
@[extern lean_array_uget]
def Array.uget {α : Type u} (a : Array α) (i : USize) (h : i.toNat < a.size) :
α

Low-level version of fget which is as fast as a C array read. Fin values are represented as tag pointers in the Lean runtime. Thus, fget may be slightly slower than uget.

Equations
@[extern lean_array_uset]
def Array.uset {α : Type u} (xs : Array α) (i : USize) (v : α) (h : i.toNat < xs.size) :

Low-level version of fset which is as fast as a C array fset. Fin values are represented as tag pointers in the Lean runtime. Thus, fset may be slightly slower than uset.

Equations
@[extern lean_array_pop]
def Array.pop {α : Type u} (xs : Array α) :
Equations
@[simp]
theorem Array.size_pop {α : Type u} (xs : Array α) :
xs.pop.size = xs.size - 1
@[extern lean_mk_array]
def Array.mkArray {α : Type u} (n : Nat) (v : α) :
Equations
@[extern lean_array_fswap]
def Array.swap {α : Type u} (xs : Array α) (i j : Nat) (hi : i < xs.size := by get_elem_tactic) (hj : j < xs.size := by get_elem_tactic) :

Swaps two entries in an array.

This will perform the update destructively provided that a has a reference count of 1 when called.

Equations
@[simp]
theorem Array.size_swap {α : Type u} (xs : Array α) (i j : Nat) {hi : i < xs.size} {hj : j < xs.size} :
(xs.swap i j hi hj).size = xs.size
@[extern lean_array_swap]
def Array.swapIfInBounds {α : Type u} (xs : Array α) (i j : Nat) :

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
@[reducible, inline, deprecated Array.swapIfInBounds (since := "2024-11-24")]
abbrev Array.swap! {α : Type u_1} (xs : Array α) (i j : Nat) :
Equations

GetElem instance for USize, backed by uget #

instance Array.instGetElemUSizeLtNatToNatSize {α : Type u} :
GetElem (Array α) USize α fun (xs : Array α) (i : USize) => i.toNat < xs.size
Equations

Definitions #

Equations
instance Array.instInhabited {α : Type u} :
Equations
def Array.isEmpty {α : Type u} (xs : Array α) :
Equations
@[specialize #[]]
def Array.isEqvAux {α : Type u} (xs ys : Array α) (hsz : xs.size = ys.size) (p : ααBool) (i : Nat) :
i xs.sizeBool
Equations
@[inline]
def Array.isEqv {α : Type u} (xs ys : Array α) (p : ααBool) :
Equations
instance Array.instBEq {α : Type u} [BEq α] :
BEq (Array α)
Equations
def Array.ofFn {α : Type u} {n : Nat} (f : Fin nα) :

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
def Array.ofFn.go {α : Type u} {n : Nat} (f : Fin nα) (i : Nat) (acc : Array α) :

Auxiliary for ofFn. ofFn.go f i acc = acc ++ #[f i, ..., f(n - 1)]

Equations

The array #[0, 1, ..., n - 1].

Equations
def Array.range' (start size : Nat) (step : Nat := 1) :

The array #[start, start + step, ..., start + step * (size - 1)].

Equations
@[inline]
def Array.singleton {α : Type u} (v : α) :
Equations
def Array.back! {α : Type u} [Inhabited α] (xs : Array α) :
α

Return the last element of an array, or panic if the array is empty.

See back for the version that requires a proof the array is non-empty, or back? for the version that returns an option.

Equations
def Array.back {α : Type u} (xs : Array α) (h : 0 < xs.size := by get_elem_tactic) :
α

Return the last element of an array, given a proof that the array is not empty.

See back! for the version that panics if the array is empty, or back? for the version that returns an option.

Equations
def Array.back? {α : Type u} (xs : Array α) :

Return the last element of an array, or none if the array is empty.

See back! for the version that panics if the array is empty, or back for the version that requires a proof the array is non-empty.

Equations
@[deprecated "Use `a[i]?` instead." (since := "2025-02-12")]
def Array.get? {α : Type u} (xs : Array α) (i : Nat) :
Equations
@[inline]
def Array.swapAt {α : Type u} (xs : Array α) (i : Nat) (v : α) (hi : i < xs.size := by get_elem_tactic) :
α × Array α
Equations
@[inline]
def Array.swapAt! {α : Type u} (xs : Array α) (i : Nat) (v : α) :
α × Array α
Equations
  • One or more equations did not get rendered due to their size.
def Array.shrink {α : Type u} (xs : Array α) (n : Nat) :

shrink a n returns the first n elements of a, implemented by repeatedly popping the last element.

Equations
def Array.shrink.loop {α : Type u} :
NatArray αArray α
Equations
@[reducible, inline]
abbrev Array.take {α : Type u} (xs : Array α) (i : Nat) :

take a n returns the first n elements of a, implemented by copying the first n elements.

Equations
@[simp]
theorem Array.take_eq_extract {α : Type u} (xs : Array α) (i : Nat) :
xs.take i = xs.extract 0 i
@[reducible, inline]
abbrev Array.drop {α : Type u} (xs : Array α) (i : Nat) :

drop a n removes the first n elements of a, implemented by copying the remaining elements.

Equations
@[simp]
theorem Array.drop_eq_extract {α : Type u} (xs : Array α) (i : Nat) :
xs.drop i = xs.extract i
@[inline]
unsafe def Array.modifyMUnsafe {α : Type u} {m : Type u → Type u_1} [Monad m] (xs : Array α) (i : Nat) (f : αm α) :
m (Array α)
Equations
@[implemented_by Array.modifyMUnsafe]
def Array.modifyM {α : Type u} {m : Type u → Type u_1} [Monad m] (xs : Array α) (i : Nat) (f : αm α) :
m (Array α)
Equations
@[inline]
def Array.modify {α : Type u} (xs : Array α) (i : Nat) (f : αα) :
Equations
@[inline]
def Array.modifyOp {α : Type u} (xs : Array α) (idx : Nat) (f : αα) :
Equations
@[inline]
unsafe def Array.forIn'Unsafe {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (as : Array α) (b : β) (f : (a : α) → a asβm (ForInStep β)) :
m β

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
@[specialize #[]]
unsafe def Array.forIn'Unsafe.loop {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (as : Array α) (f : (a : α) → a asβm (ForInStep β)) (sz i : USize) (b : β) :
m β
Equations
  • One or more equations did not get rendered due to their size.
@[implemented_by Array.forIn'Unsafe]
def Array.forIn' {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (as : Array α) (b : β) (f : (a : α) → a asβm (ForInStep β)) :
m β

Reference implementation for forIn'

Equations
def Array.forIn'.loop {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (as : Array α) (f : (a : α) → a asβm (ForInStep β)) (i : Nat) (h : i as.size) (b : β) :
m β
Equations
instance Array.instForIn'InferInstanceMembership {α : Type u} {m : Type u_1 → Type u_2} :
Equations
@[simp]
theorem Array.forIn'_eq_forIn' {α : Type u} {m : Type u_1 → Type u_2} {β : Type u_1} [Monad m] :
@[inline]
unsafe def Array.foldlMUnsafe {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : βαm β) (init : β) (as : Array α) (start : Nat := 0) (stop : Nat := as.size) :
m β

See comment at forIn'Unsafe

Equations
@[specialize #[]]
unsafe def Array.foldlMUnsafe.fold {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : βαm β) (as : Array α) (i stop : USize) (b : β) :
m β
Equations
@[implemented_by Array.foldlMUnsafe]
def Array.foldlM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : βαm β) (init : β) (as : Array α) (start : Nat := 0) (stop : Nat := as.size) :
m β

Reference implementation for foldlM

Equations
  • One or more equations did not get rendered due to their size.
def Array.foldlM.loop {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : βαm β) (as : Array α) (stop : Nat) (h : stop as.size) (i j : Nat) (b : β) :
m β
Equations
  • One or more equations did not get rendered due to their size.
@[inline]
unsafe def Array.foldrMUnsafe {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : αβm β) (init : β) (as : Array α) (start : Nat := as.size) (stop : Nat := 0) :
m β

See comment at forIn'Unsafe

Equations
  • One or more equations did not get rendered due to their size.
@[specialize #[]]
unsafe def Array.foldrMUnsafe.fold {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : αβm β) (as : Array α) (i stop : USize) (b : β) :
m β
Equations
@[implemented_by Array.foldrMUnsafe]
def Array.foldrM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : αβm β) (init : β) (as : Array α) (start : Nat := as.size) (stop : Nat := 0) :
m β

Reference implementation for foldrM

Equations
  • One or more equations did not get rendered due to their size.
def Array.foldrM.fold {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : αβm β) (as : Array α) (stop : Nat := 0) (i : Nat) (h : i as.size) (b : β) :
m β
Equations
@[inline]
unsafe def Array.mapMUnsafe {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : αm β) (as : Array α) :
m (Array β)

See comment at forIn'Unsafe

Equations
@[specialize #[]]
unsafe def Array.mapMUnsafe.map {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : αm β) (sz i : USize) (bs : Array NonScalar) :
Equations
  • One or more equations did not get rendered due to their size.
@[implemented_by Array.mapMUnsafe]
def Array.mapM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : αm β) (as : Array α) :
m (Array β)

Reference implementation for mapM

Equations
def Array.mapM.map {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : αm β) (as : Array α) (i : Nat) (bs : Array β) :
m (Array β)
Equations
@[reducible, inline, deprecated Array.mapM (since := "2024-11-11")]
abbrev Array.sequenceMap {α : Type u_1} {β : Type u_2} {m : Type u_2 → Type u_3} [Monad m] (f : αm β) (as : Array α) :
m (Array β)
Equations
@[inline]
def Array.mapFinIdxM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (as : Array α) (f : (i : Nat) → αi < as.sizem β) :
m (Array β)

Variant of mapIdxM which receives the index i along with the bound i < as.size.

Equations
@[specialize #[]]
def Array.mapFinIdxM.map {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (as : Array α) (f : (i : Nat) → αi < as.sizem β) (i j : Nat) (inv : i + j = as.size) (bs : Array β) :
m (Array β)
Equations
@[inline]
def Array.mapIdxM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : Natαm β) (as : Array α) :
m (Array β)
Equations
@[inline]
def Array.firstM {β : Type v} {α : Type u} {m : Type v → Type w} [Alternative m] (f : αm β) (as : Array α) :
m β
Equations
@[irreducible]
def Array.firstM.go {β : Type v} {α : Type u} {m : Type v → Type w} [Alternative m] (f : αm β) (as : Array α) (i : Nat) :
m β
Equations
@[inline]
def Array.findSomeM? {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : αm (Option β)) (as : Array α) :
m (Option β)
Equations
  • One or more equations did not get rendered due to their size.
@[inline]
def Array.findM? {α : Type} {m : TypeType} [Monad m] (p : αm Bool) (as : Array α) :
m (Option α)

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.
@[inline]
def Array.findIdxM? {α : Type u} {m : TypeType u_1} [Monad m] (p : αm Bool) (as : Array α) :
Equations
  • One or more equations did not get rendered due to their size.
@[inline]
unsafe def Array.anyMUnsafe {α : Type u} {m : TypeType w} [Monad m] (p : αm Bool) (as : Array α) (start : Nat := 0) (stop : Nat := as.size) :
Equations
  • One or more equations did not get rendered due to their size.
@[specialize #[]]
unsafe def Array.anyMUnsafe.any {α : Type u} {m : TypeType w} [Monad m] (p : αm Bool) (as : Array α) (i stop : USize) :
Equations
  • One or more equations did not get rendered due to their size.
@[implemented_by Array.anyMUnsafe]
def Array.anyM {α : Type u} {m : TypeType w} [Monad m] (p : αm Bool) (as : Array α) (start : Nat := 0) (stop : Nat := as.size) :
Equations
  • One or more equations did not get rendered due to their size.
def Array.anyM.loop {α : Type u} {m : TypeType w} [Monad m] (p : αm Bool) (as : Array α) (stop : Nat) (h : stop as.size) (j : Nat) :
Equations
  • One or more equations did not get rendered due to their size.
@[inline]
def Array.allM {α : Type u} {m : TypeType w} [Monad m] (p : αm Bool) (as : Array α) (start : Nat := 0) (stop : Nat := as.size) :
Equations
@[inline]
def Array.findSomeRevM? {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : αm (Option β)) (as : Array α) :
m (Option β)
Equations
@[specialize #[]]
def Array.findSomeRevM?.find {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : αm (Option β)) (as : Array α) (i : Nat) :
i as.sizem (Option β)
Equations
@[inline]
def Array.findRevM? {α : Type} {m : TypeType w} [Monad m] (p : αm Bool) (as : Array α) :
m (Option α)
Equations
@[inline]
def Array.forM {α : Type u} {m : Type v → Type w} [Monad m] (f : αm PUnit) (as : Array α) (start : Nat := 0) (stop : Nat := as.size) :
Equations
instance Array.instForM {α : Type u} {m : Type u_1 → Type u_2} :
ForM m (Array α) α
Equations
@[simp]
theorem Array.forM_eq_forM {α : Type u} {m : Type u_1 → Type u_2} {as : Array α} [Monad m] (f : αm PUnit) :
Array.forM f as = forM as f
@[inline]
def Array.forRevM {α : Type u} {m : Type v → Type w} [Monad m] (f : αm PUnit) (as : Array α) (start : Nat := as.size) (stop : Nat := 0) :
Equations
@[inline]
def Array.foldl {α : Type u} {β : Type v} (f : βαβ) (init : β) (as : Array α) (start : Nat := 0) (stop : Nat := as.size) :
β
Equations
@[inline]
def Array.foldr {α : Type u} {β : Type v} (f : αββ) (init : β) (as : Array α) (start : Nat := as.size) (stop : Nat := 0) :
β
Equations
@[inline]
def Array.sum {α : Type u_1} [Add α] [Zero α] :
Array αα

Sum of an array.

Array.sum #[a, b, c] = a + (b + (c + 0))

Equations
@[inline]
def Array.countP {α : Type u} (p : αBool) (as : Array α) :
Equations
@[inline]
def Array.count {α : Type u} [BEq α] (a : α) (as : Array α) :
Equations
@[inline]
def Array.map {α : Type u} {β : Type v} (f : αβ) (as : Array α) :
Equations
Equations
@[inline]
def Array.mapFinIdx {α : Type u} {β : Type v} (as : Array α) (f : (i : Nat) → αi < as.sizeβ) :

Variant of mapIdx which receives the index as a Fin as.size.

Equations
@[inline]
def Array.mapIdx {α : Type u} {β : Type v} (f : Natαβ) (as : Array α) :
Equations
def Array.zipIdx {α : Type u} (xs : Array α) (start : Nat := 0) :
Array (α × Nat)

Turns #[a, b] into #[(a, 0), (b, 1)].

Equations
@[reducible, inline, deprecated Array.zipIdx (since := "2025-01-21")]
abbrev Array.zipWithIndex {α : Type u_1} (xs : Array α) (start : Nat := 0) :
Array (α × Nat)
Equations
@[inline]
def Array.find? {α : Type u} (p : αBool) (as : Array α) :
Equations
  • One or more equations did not get rendered due to their size.
@[inline]
def Array.findSome? {α : Type u} {β : Type v} (f : αOption β) (as : Array α) :
Equations
@[inline]
def Array.findSome! {α : Type u} {β : Type v} [Inhabited β] (f : αOption β) (xs : Array α) :
β
Equations
@[inline]
def Array.findSomeRev? {α : Type u} {β : Type v} (f : αOption β) (as : Array α) :
Equations
@[inline]
def Array.findRev? {α : Type} (p : αBool) (as : Array α) :
Equations
@[inline]
def Array.findIdx? {α : Type u} (p : αBool) (as : Array α) :
Equations
def Array.findIdx?.loop {α : Type u} (p : αBool) (as : Array α) (j : Nat) :
Equations
@[inline]
def Array.findFinIdx? {α : Type u} (p : αBool) (as : Array α) :
Equations
def Array.findFinIdx?.loop {α : Type u} (p : αBool) (as : Array α) (j : Nat) :
Equations
@[irreducible]
theorem Array.findIdx?_loop_eq_map_findFinIdx?_loop_val {α : Type u} {xs : Array α} {p : αBool} {j : Nat} :
findIdx?.loop p xs j = Option.map (fun (x : Fin xs.size) => x) (findFinIdx?.loop p xs j)
theorem Array.findIdx?_eq_map_findFinIdx?_val {α : Type u} {xs : Array α} {p : αBool} :
findIdx? p xs = Option.map (fun (x : Fin xs.size) => x) (findFinIdx? p xs)
@[inline]
def Array.findIdx {α : Type u} (p : αBool) (as : Array α) :
Equations
def Array.idxOfAux {α : Type u} [BEq α] (xs : Array α) (v : α) (i : Nat) :
Equations
@[reducible, inline, deprecated Array.idxOfAux (since := "2025-01-29")]
abbrev Array.indexOfAux {α : Type u_1} [BEq α] (xs : Array α) (v : α) (i : Nat) :
Equations
def Array.finIdxOf? {α : Type u} [BEq α] (xs : Array α) (v : α) :
Equations
@[reducible, inline, deprecated "`Array.indexOf?` has been deprecated, use `idxOf?` or `finIdxOf?` instead." (since := "2025-01-29")]
abbrev Array.indexOf? {α : Type u_1} [BEq α] (xs : Array α) (v : α) :
Equations
def Array.idxOf {α : Type u} [BEq α] (a : α) :
Array αNat

Returns the index of the first element equal to a, or the length of the array otherwise.

Equations
def Array.idxOf? {α : Type u} [BEq α] (xs : Array α) (v : α) :
Equations
@[deprecated Array.idxOf? (since := "2024-11-20")]
def Array.getIdx? {α : Type u} [BEq α] (xs : Array α) (v : α) :
Equations
@[inline]
def Array.any {α : Type u} (as : Array α) (p : αBool) (start : Nat := 0) (stop : Nat := as.size) :
Equations
@[inline]
def Array.all {α : Type u} (as : Array α) (p : αBool) (start : Nat := 0) (stop : Nat := as.size) :
Equations
def Array.contains {α : Type u} [BEq α] (as : Array α) (a : α) :

as.contains a is true if there is some element b in as such that a == b.

Equations
def Array.elem {α : Type u} [BEq α] (a : α) (as : Array α) :

Variant of Array.contains with arguments reversed.

For verification purposes, we simplify this to contains.

Equations
@[export lean_array_to_list_impl]
def Array.toListImpl {α : Type u} (as : Array α) :
List α

Convert a Array α into an List α. This is O(n) in the size of the array.

Equations
@[inline]
def Array.toListAppend {α : Type u} (as : Array α) (l : List α) :
List α

Prepends an Array α onto the front of a list. Equivalent to as.toList ++ l.

Equations
def Array.append {α : Type u} (as bs : Array α) :
Equations
instance Array.instAppend {α : Type u} :
Equations
def Array.appendList {α : Type u} (as : Array α) (bs : List α) :
Equations
instance Array.instHAppendList {α : Type u} :
HAppend (Array α) (List α) (Array α)
Equations
@[inline]
def Array.flatMapM {α : Type u} {m : Type u_1 → Type u_2} {β : Type u_1} [Monad m] (f : αm (Array β)) (as : Array α) :
m (Array β)
Equations
@[reducible, inline, deprecated Array.flatMapM (since := "2024-10-16")]
abbrev Array.concatMapM {α : Type u_1} {m : Type u_2 → Type u_3} {β : Type u_2} [Monad m] (f : αm (Array β)) (as : Array α) :
m (Array β)
Equations
@[inline]
def Array.flatMap {α : Type u} {β : Type u_1} (f : αArray β) (as : Array α) :
Equations
@[reducible, inline, deprecated Array.flatMap (since := "2024-10-16")]
abbrev Array.concatMap {α : Type u_1} {β : Type u_2} (f : αArray β) (as : Array α) :
Equations
@[inline]
def Array.flatten {α : Type u} (xss : Array (Array α)) :

Joins array of array into a single array.

flatten #[#[a₁, a₂, ⋯], #[b₁, b₂, ⋯], ⋯] = #[a₁, a₂, ⋯, b₁, b₂, ⋯]

Equations
def Array.reverse {α : Type u} (as : Array α) :
Equations
theorem Array.reverse.termination {i j : Nat} (h : i < j) :
j - 1 - (i + 1) < j - i
@[irreducible]
def Array.reverse.loop {α : Type u} (as : Array α) (i : Nat) (j : Fin as.size) :
Equations
@[inline]
def Array.filter {α : Type u} (p : αBool) (as : Array α) (start : Nat := 0) (stop : Nat := as.size) :
Equations
@[inline]
def Array.filterM {m : TypeType u_1} {α : Type} [Monad m] (p : αm Bool) (as : Array α) (start : Nat := 0) (stop : Nat := as.size) :
m (Array α)
Equations
@[inline]
def Array.filterRevM {m : TypeType u_1} {α : Type} [Monad m] (p : αm Bool) (as : Array α) (start : Nat := as.size) (stop : Nat := 0) :
m (Array α)
Equations
  • One or more equations did not get rendered due to their size.
@[specialize #[]]
def Array.filterMapM {α : Type u} {m : Type u_1 → Type u_2} {β : Type u_1} [Monad m] (f : αm (Option β)) (as : Array α) (start : Nat := 0) (stop : Nat := as.size) :
m (Array β)
Equations
  • One or more equations did not get rendered due to their size.
@[inline]
def Array.filterMap {α : Type u} {β : Type u_1} (f : αOption β) (as : Array α) (start : Nat := 0) (stop : Nat := as.size) :
Equations
@[specialize #[]]
def Array.getMax? {α : Type u} (as : Array α) (lt : ααBool) :
Equations
@[inline]
def Array.partition {α : Type u} (p : αBool) (as : Array α) :
Array α × Array α
Equations
  • One or more equations did not get rendered due to their size.
def Array.popWhile {α : Type u} (p : αBool) (as : Array α) :
Equations
@[simp]
theorem Array.popWhile_empty {α : Type u} (p : αBool) :
def Array.takeWhile {α : Type u} (p : αBool) (as : Array α) :
Equations
def Array.takeWhile.go {α : Type u} (p : αBool) (as : Array α) (i : Nat) (acc : Array α) :
Equations
def Array.eraseIdx {α : Type u} (xs : Array α) (i : Nat) (h : i < xs.size := by get_elem_tactic) :

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.

Equations
@[simp]
theorem Array.size_eraseIdx {α : Type u} (xs : Array α) (i : Nat) (h : i < xs.size) :
(xs.eraseIdx i h).size = xs.size - 1
def Array.eraseIdxIfInBounds {α : Type u} (xs : Array α) (i : Nat) :

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
def Array.eraseIdx! {α : Type u} (xs : Array α) (i : Nat) :

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.

Equations
def Array.erase {α : Type u} [BEq α] (as : Array α) (a : α) :

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.

Equations
def Array.eraseP {α : Type u} (as : Array α) (p : αBool) :

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.

Equations
@[inline]
def Array.insertIdx {α : Type u} (as : Array α) (i : Nat) (a : α) :

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
def Array.insertIdx.loop {α : Type u} (i : Nat) (as : Array α) (j : Fin as.size) :
Equations
@[reducible, inline, deprecated Array.insertIdx (since := "2024-11-20")]
abbrev Array.insertAt {α : Type u_1} (as : Array α) (i : Nat) (a : α) :
Equations
def Array.insertIdx! {α : Type u} (as : Array α) (i : Nat) (a : α) :

Insert element a at position i. Panics if i is not i ≤ as.size.

Equations
@[reducible, inline, deprecated Array.insertIdx! (since := "2024-11-20")]
abbrev Array.insertAt! {α : Type u_1} (as : Array α) (i : Nat) (a : α) :
Equations
def Array.insertIdxIfInBounds {α : Type u} (as : Array α) (i : Nat) (a : α) :

Insert element a at position i, or do nothing if as.size < i.

Equations
def Array.isPrefixOfAux {α : Type u} [BEq α] (as bs : Array α) (hle : as.size bs.size) (i : Nat) :
Equations
def Array.isPrefixOf {α : Type u} [BEq α] (as bs : Array α) :

Return true iff as is a prefix of bs. That is, bs = as ++ t for some t : List α.

Equations
@[specialize #[]]
def Array.zipWithAux {α : Type u} {β : Type u_1} {γ : Type u_2} (as : Array α) (bs : Array β) (f : αβγ) (i : Nat) (cs : Array γ) :
Equations
@[inline]
def Array.zipWith {α : Type u} {β : Type u_1} {γ : Type u_2} (f : αβγ) (as : Array α) (bs : Array β) :
Equations
def Array.zip {α : Type u} {β : Type u_1} (as : Array α) (bs : Array β) :
Array (α × β)
Equations
def Array.zipWithAll {α : Type u} {β : Type u_1} {γ : Type u_2} (f : Option αOption βγ) (as : Array α) (bs : Array β) :
Equations
@[irreducible]
def Array.zipWithAll.go {α : Type u} {β : Type u_1} {γ : Type u_2} (f : Option αOption βγ) (as : Array α) (bs : Array β) (i : Nat) (cs : Array γ) :
Equations
def Array.unzip {α : Type u} {β : Type u_1} (as : Array (α × β)) :
Array α × Array β
Equations
@[deprecated Array.partition (since := "2024-11-06")]
def Array.split {α : Type u} (as : Array α) (p : αBool) :
Array α × Array α
Equations
def Array.replace {α : Type u} [BEq α] (xs : Array α) (a b : α) :
Equations

Lexicographic ordering #

instance Array.instLT {α : Type u} [LT α] :
LT (Array α)
Equations
instance Array.instLE {α : Type u} [LT α] :
LE (Array α)
Equations

Auxiliary functions used in metaprogramming. #

We do not currently intend to provide verification theorems for these functions.

leftpad and rightpad #

def Array.leftpad {α : Type u} (n : Nat) (a : α) (xs : Array α) :

Pads l : Array α on the left with repeated occurrences of a : α until it is of size n. If l is initially larger than n, just return l.

Equations
def Array.rightpad {α : Type u} (n : Nat) (a : α) (xs : Array α) :

Pads l : Array α on the right with repeated occurrences of a : α until it is of size n. If l is initially larger than n, just return l.

Equations
@[inline]
def Array.reduceOption {α : Type u} (as : Array (Option α)) :

Drop nones from a Array, and replace each remaining some a with a.

Equations

eraseReps #

def Array.eraseReps {α : Type u_1} [BEq α] (as : Array α) :

O(|l|). Erase repeated adjacent elements. Keeps the first occurrence of each run.

  • eraseReps #[1, 3, 2, 2, 2, 3, 5] = #[1, 3, 2, 3, 5]
Equations
  • One or more equations did not get rendered due to their size.

allDiff #

def Array.allDiff {α : Type u} [BEq α] (as : Array α) :
Equations

getEvenElems #

@[inline]
def Array.getEvenElems {α : Type u} (as : Array α) :
Equations
  • One or more equations did not get rendered due to their size.

Repr and ToString #

instance Array.instRepr {α : Type u} [Repr α] :
Repr (Array α)
Equations
  • One or more equations did not get rendered due to their size.
instance Array.instToString {α : Type u} [ToString α] :
Equations