doc-next-gen

Init.Data.Vector.Basic

Module docstring

{"# Vectors

Vector α n is a thin wrapper around Array α for arrays of fixed size n. ","### ForIn instance ","### ForM instance ","### ToStream instance ","### Lexicographic ordering "}

Vector structure
(α : Type u) (n : Nat) extends Array α
Full source
/-- `Vector α n` is an `Array α` with size `n`. -/
structure Vector (α : Type u) (n : Nat) extends Array α where
  /-- Array size. -/
  size_toArray : toArray.size = n
deriving Repr, DecidableEq
Fixed-length vector
Informal description
The structure `Vector α n` represents an array of elements of type `α` with a fixed length `n`. It is a thin wrapper around the `Array α` type, ensuring that the size of the array is always exactly `n`.
instReprVector instance
{α✝} {n✝} [Repr✝ α✝] : Repr✝ (@Vector✝ α✝ n✝)
Full source
Repr, DecidableEq
Representation Format for Fixed-Length Vectors
Informal description
For any type $\alpha$ with a representation format and any natural number $n$, the type of fixed-length vectors $\text{Vector}\,\alpha\,n$ also has a representation format, which is derived from the representation format of $\alpha$.
instDecidableEqVector instance
{α✝} {n✝} [DecidableEq✝ α✝] : DecidableEq✝ (@Vector✝ α✝ n✝)
Full source
DecidableEq
Decidable Equality for Fixed-Length Vectors
Informal description
For any type $\alpha$ with decidable equality and any natural number $n$, the type of fixed-length vectors $\text{Vector}\,\alpha\,n$ also has decidable equality.
Array.toVector abbrev
(xs : Array α) : Vector α xs.size
Full source
/--
Converts an array to a vector. The resulting vector's size is the array's size.
-/
abbrev Array.toVector (xs : Array α) : Vector α xs.size := .mk xs rfl
Conversion from Array to Fixed-Length Vector
Informal description
Given an array `xs` of type `Array α`, the function returns a fixed-length vector of type `Vector α n` where `n` is the size of the array `xs`.
Vector.elimAsArray definition
{motive : Vector α n → Sort u} (mk : ∀ (xs : Array α) (ha : xs.size = n), motive ⟨xs, ha⟩) : (xs : Vector α n) → motive xs
Full source
/-- Custom eliminator for `Vector α n` through `Array α` -/
@[elab_as_elim]
def elimAsArray {motive : Vector α n → Sort u}
    (mk : ∀ (xs : Array α) (ha : xs.size = n), motive ⟨xs, ha⟩) :
    (xs : Vector α n) → motive xs
  | ⟨xs, h⟩ => mk xs h
Vector eliminator via array
Informal description
The eliminator function for `Vector α n` that allows pattern matching through the underlying `Array α` structure. Given a function `mk` that constructs a result from an array `xs` of size `n` and a proof `ha` that `xs.size = n`, this function applies `mk` to the array and proof contained in a vector `xs : Vector α n`.
Vector.elimAsList definition
{motive : Vector α n → Sort u} (mk : ∀ (l : List α) (ha : l.length = n), motive ⟨⟨l⟩, ha⟩) : (xs : Vector α n) → motive xs
Full source
/-- Custom eliminator for `Vector α n` through `List α` -/
@[elab_as_elim]
def elimAsList {motive : Vector α n → Sort u}
    (mk : ∀ (l : List α) (ha : l.length = n), motive ⟨⟨l⟩, ha⟩) :
    (xs : Vector α n) → motive xs
  | ⟨⟨xs⟩, ha⟩ => mk xs ha
List-based vector eliminator
Informal description
Given a motive `motive` depending on vectors of type `Vector α n`, and a function `mk` that constructs a term of type `motive v` for any list `l : List α` of length `n` (wrapped as a vector `v`), the eliminator `Vector.elimAsList` allows pattern matching on a vector `xs : Vector α n` by treating it as a list of length `n`. More precisely, for any vector `xs : Vector α n` represented internally as a list `l` with `l.length = n`, applying `Vector.elimAsList mk xs` returns `mk l h` where `h` is the proof that `l.length = n`.
Vector.emptyWithCapacity definition
(capacity : Nat) : Vector α 0
Full source
/-- Make an empty vector with pre-allocated capacity. -/
@[inline] def emptyWithCapacity (capacity : Nat) : Vector α 0 := ⟨.mkEmpty capacity, rfl⟩
Empty vector with pre-allocated capacity
Informal description
The function creates an empty vector of type `α` with length 0 and a pre-allocated capacity of `capacity` elements.
Vector.replicate definition
(n) (v : α) : Vector α n
Full source
/-- Makes a vector of size `n` with all cells containing `v`. -/
@[inline] def replicate (n) (v : α) : Vector α n := ⟨Array.replicate n v, by simp⟩
Fixed-length vector with replicated elements
Informal description
The function creates a vector of length $n$ where every element is equal to $v$. For example, `Vector.replicate 2 true` produces a vector of length 2 containing `#[true, true]`, and `Vector.replicate 0 "anything"` produces an empty vector.
Vector.mkVector abbrev
Full source
@[deprecated replicate (since := "2025-03-18")]
abbrev mkVector := @replicate
Constructor for Fixed-Length Vectors
Informal description
Given a type $\alpha$ and a natural number $n$, the function `Vector.mkVector` constructs a vector of type `Vector α n` (a fixed-length array of size $n$ with elements of type $\alpha$).
Vector.instNonemptyOfNatNat instance
: Nonempty (Vector α 0)
Full source
instance : Nonempty (Vector α 0) := ⟨#v[]⟩
Nonempty Zero-Length Vectors
Informal description
For any type $\alpha$, the type of vectors of length $0$ over $\alpha$ is nonempty.
Vector.instNonempty instance
[Nonempty α] : Nonempty (Vector α n)
Full source
instance [Nonempty α] : Nonempty (Vector α n) := ⟨replicate _ Classical.ofNonempty⟩
Nonempty Vectors from Nonempty Types
Informal description
For any type $\alpha$ and natural number $n$, if $\alpha$ is nonempty (i.e., contains at least one element), then the type of vectors of length $n$ over $\alpha$ is also nonempty.
Vector.singleton definition
(v : α) : Vector α 1
Full source
/-- Returns a vector of size `1` with element `v`. -/
@[inline] def singleton (v : α) : Vector α 1 := ⟨#[v], rfl⟩
Singleton vector
Informal description
The function takes an element $v$ of type $\alpha$ and returns a vector of length $1$ containing $v$ as its only element.
Vector.instInhabited instance
[Inhabited α] : Inhabited (Vector α n)
Full source
instance [Inhabited α] : Inhabited (Vector α n) where
  default := replicate n default
Inhabited Vectors from Inhabited Types
Informal description
For any type $\alpha$ with a default element and any natural number $n$, the type of vectors of length $n$ over $\alpha$ also has a default element, which is the vector where every entry is the default element of $\alpha$.
Vector.get definition
(xs : Vector α n) (i : Fin n) : α
Full source
/-- Get an element of a vector using a `Fin` index. -/
@[inline] def get (xs : Vector α n) (i : Fin n) : α :=
  xs.toArray[(i.cast xs.size_toArray.symm).1]
Vector element access by bounded index
Informal description
Given a vector `xs` of type `Vector α n` (a fixed-length array with `n` elements of type `α`) and an index `i` of type `Fin n` (a natural number less than `n`), the function returns the element of `xs` at position `i`.
Vector.uget definition
(xs : Vector α n) (i : USize) (h : i.toNat < n) : α
Full source
/-- Get an element of a vector using a `USize` index and a proof that the index is within bounds. -/
@[inline] def uget (xs : Vector α n) (i : USize) (h : i.toNat < n) : α :=
  xs.toArray.uget i (xs.size_toArray.symm ▸ h)
Fast vector indexing with unsigned index
Informal description
Given a vector `xs` of type `Vector α n` (a fixed-length array with `n` elements of type `α`), an index `i` of type `USize` (a platform-dependent unsigned word-size integer), and a proof that the natural number representation of `i` is less than `n`, the function returns the element of `xs` at position `i`. This operation is optimized for performance by using the underlying array's fast indexing.
Vector.instGetElemNatLt instance
: GetElem (Vector α n) Nat α fun _ i => i < n
Full source
instance : GetElem (Vector α n) Nat α fun _ i => i < n where
  getElem xs i h := get xs ⟨i, h⟩
Element Access for Fixed-Length Vectors
Informal description
For any type $\alpha$ and natural number $n$, the fixed-length vector type $\text{Vector}\,\alpha\,n$ supports element access notation `v[i]` where $i$ is a natural number index, provided that $i < n$. This operation returns an element of type $\alpha$ from the vector $v$ at position $i$.
Vector.contains definition
[BEq α] (xs : Vector α n) (a : α) : Bool
Full source
/-- Check if there is an element which satisfies `a == ·`. -/
def contains [BEq α] (xs : Vector α n) (a : α) : Bool := xs.toArray.contains a
Vector membership check via boolean equality
Informal description
Given a fixed-length vector `xs` of type `Vector α n` with a boolean equality relation `BEq α` and an element `a` of type `α`, the function checks whether `a` is an element of `xs` by comparing elements using the boolean equality operator `==`. It returns `true` if `a` is found in `xs` and `false` otherwise.
Vector.Mem structure
(xs : Vector α n) (a : α)
Full source
/-- `a ∈ v` is a predicate which asserts that `a` is in the vector `v`. -/
structure Mem (xs : Vector α n) (a : α) : Prop where
  val : a ∈ xs.toArray
Membership in a fixed-length vector
Informal description
The predicate `a ∈ v` asserts that an element `a` of type `α` is contained in the vector `v` of fixed length `n`. Here, `v` is a fixed-length array of type `Vector α n`.
Vector.instMembership instance
: Membership α (Vector α n)
Full source
instance : Membership α (Vector α n) where
  mem := Mem
Membership Relation for Fixed-Length Vectors
Informal description
For any type $\alpha$ and natural number $n$, the fixed-length vector type $\text{Vector}\,\alpha\,n$ has a membership relation $\in$ where $a \in v$ holds if and only if the element $a$ appears in the vector $v$.
Vector.getD definition
(xs : Vector α n) (i : Nat) (default : α) : α
Full source
/--
Get an element of a vector using a `Nat` index. Returns the given default value if the index is out
of bounds.
-/
@[inline] def getD (xs : Vector α n) (i : Nat) (default : α) : α := xs.toArray.getD i default
Vector element access with default value
Informal description
Given a fixed-length vector $xs$ of type $\alpha$ with length $n$, a natural number index $i$, and a default value $default$ of type $\alpha$, the function returns the element at position $i$ in $xs$ if $i$ is within bounds (i.e., $0 \leq i < n$), and returns $default$ otherwise.
Vector.back! definition
[Inhabited α] (xs : Vector α n) : α
Full source
/-- The last element of a vector. Panics if the vector is empty. -/
@[inline] def back! [Inhabited α] (xs : Vector α n) : α := xs.toArray.back!
Last element of a fixed-length vector (unsafe)
Informal description
The function returns the last element of a fixed-length vector `xs` of type `α` with length `n`, or panics if the vector is empty. If the vector is non-empty, it returns the element at index `n - 1`.
Vector.back? definition
(xs : Vector α n) : Option α
Full source
/-- The last element of a vector, or `none` if the vector is empty. -/
@[inline] def back? (xs : Vector α n) : Option α := xs.toArray.back?
Optional last element of a vector
Informal description
The function returns the last element of a vector `xs` of type `Vector α n` as an optional value of type `Option α`. If the vector is non-empty, it returns `some x` where `x` is the last element; otherwise, it returns `none`.
Vector.back definition
[NeZero n] (xs : Vector α n) : α
Full source
/-- The last element of a non-empty vector. -/
@[inline] def back [NeZero n] (xs : Vector α n) : α :=
  xs[n - 1]'(Nat.sub_one_lt (NeZero.ne n))
Last element of a non-empty vector
Informal description
For a non-empty vector `xs` of type `Vector α n` (where `n` is non-zero), the function returns the last element of the vector, which is the element at index `n - 1`.
Vector.head definition
[NeZero n] (xs : Vector α n)
Full source
/-- The first element of a non-empty vector.  -/
@[inline] def head [NeZero n] (xs : Vector α n) := xs[0]'(Nat.pos_of_neZero n)
First element of a non-empty vector
Informal description
Given a non-empty vector \( xs \) of type \( \text{Vector}\,\alpha\,n \) (where \( n \) is a non-zero natural number), the function returns the first element of \( xs \).
Vector.push definition
(xs : Vector α n) (x : α) : Vector α (n + 1)
Full source
/-- Push an element `x` to the end of a vector. -/
@[inline] def push (xs : Vector α n) (x : α) : Vector α (n + 1) :=
  ⟨xs.toArray.push x, by simp⟩
Appending an element to a vector
Informal description
Given a vector $xs$ of length $n$ with elements of type $\alpha$ and an element $x$ of type $\alpha$, the function returns a new vector of length $n + 1$ obtained by appending $x$ to the end of $xs$.
Vector.pop definition
(xs : Vector α n) : Vector α (n - 1)
Full source
/-- Remove the last element of a vector. -/
@[inline] def pop (xs : Vector α n) : Vector α (n - 1) :=
  ⟨Array.pop xs.toArray, by simp⟩
Removing the last element of a fixed-length vector
Informal description
Given a vector `xs` of type `Vector α n` (a fixed-length array of length `n` with elements of type `α`), the function `Vector.pop` returns a new vector of length `n - 1` obtained by removing the last element of `xs`. If `n = 0`, the result is a vector of length `0`.
Vector.setIfInBounds definition
(xs : Vector α n) (i : Nat) (x : α) : Vector α n
Full source
/--
Set an element in a vector using a `Nat` index. Returns the vector unchanged if the index is out of
bounds.

This will perform the update destructively provided that the vector has a reference count of 1.
-/
@[inline] def setIfInBounds (xs : Vector α n) (i : Nat) (x : α) : Vector α n :=
  ⟨xs.toArray.setIfInBounds i x, by simp⟩
Conditional vector element replacement
Informal description
Given a vector $xs$ of type $\text{Vector}\,\alpha\,n$ (a fixed-length array of length $n$ with elements of type $\alpha$), an index $i$ of type $\mathbb{N}$, and an element $x$ of type $\alpha$, the function returns a new vector where the element at index $i$ is replaced with $x$ if $i$ is within the bounds of the vector (i.e., $i < n$). If $i$ is out of bounds, the original vector $xs$ is returned unchanged.
Vector.set! definition
(xs : Vector α n) (i : Nat) (x : α) : Vector α n
Full source
/--
Set an element in a vector using a `Nat` index. Panics if the index is out of bounds.

This will perform the update destructively provided that the vector has a reference count of 1.
-/
@[inline] def set! (xs : Vector α n) (i : Nat) (x : α) : Vector α n :=
  ⟨xs.toArray.set! i x, by simp⟩
Destructive vector element update (with bounds check)
Informal description
Given a vector \( \text{Vector}\,\alpha\,n \) (a fixed-length array of length \( n \) with elements of type \( \alpha \)), an index \( i \) of type \( \mathbb{N} \), and an element \( x \) of type \( \alpha \), the function destructively updates the vector by replacing the element at index \( i \) with \( x \). The operation panics if the index \( i \) is out of bounds (i.e., \( i \geq n \)).
Vector.foldlM definition
[Monad m] (f : β → α → m β) (b : β) (xs : Vector α n) : m β
Full source
@[inline] def foldlM [Monad m] (f : β → α → m β) (b : β) (xs : Vector α n) : m β :=
  xs.toArray.foldlM f b
Monadic left-fold over a vector
Informal description
Given a monad `m`, a function `f : β → α → m β`, an initial value `b : β`, and a vector `xs : Vector α n`, the function performs a left-fold operation over the elements of `xs` using `f`, accumulating the result in the monadic context `m`.
Vector.foldrM definition
[Monad m] (f : α → β → m β) (b : β) (xs : Vector α n) : m β
Full source
@[inline] def foldrM [Monad m] (f : α → β → m β) (b : β) (xs : Vector α n) : m β :=
  xs.toArray.foldrM f b
Monadic right-fold over a vector
Informal description
Given a monad `m`, a function `f : α → β → m β`, an initial value `b : β`, and a vector `xs : Vector α n`, the function `Vector.foldrM` performs a right-fold operation over the elements of `xs` using `f`, accumulating the result in the monadic context `m`.
Vector.foldl definition
(f : β → α → β) (b : β) (xs : Vector α n) : β
Full source
@[inline] def foldl (f : β → α → β) (b : β) (xs : Vector α n) : β :=
  xs.toArray.foldl f b
Left-fold over a vector
Informal description
Given a function \( f : \beta \to \alpha \to \beta \), an initial value \( b : \beta \), and a vector \( \text{xs} : \text{Vector} \, \alpha \, n \), the function performs a left-fold operation over the elements of \( \text{xs} \) using \( f \), returning the accumulated result of type \( \beta \).
Vector.foldr definition
(f : α → β → β) (b : β) (xs : Vector α n) : β
Full source
@[inline] def foldr (f : α → β → β) (b : β) (xs : Vector α n) : β :=
  xs.toArray.foldr f b
Right-fold over a vector
Informal description
Given a function \( f : \alpha \to \beta \to \beta \), an initial value \( b : \beta \), and a vector \( \text{xs} : \text{Vector} \, \alpha \, n \), the function \( \text{Vector.foldr} \) performs a right-fold operation over the elements of \( \text{xs} \) using \( f \), returning the accumulated result of type \( \beta \).
Vector.append definition
(xs : Vector α n) (ys : Vector α m) : Vector α (n + m)
Full source
/-- Append two vectors. -/
@[inline] def append (xs : Vector α n) (ys : Vector α m) : Vector α (n + m) :=
  ⟨xs.toArray ++ ys.toArray, by simp⟩
Vector concatenation
Informal description
Given two vectors \( \text{xs} : \text{Vector} \, \alpha \, n \) and \( \text{ys} : \text{Vector} \, \alpha \, m \), the function returns a new vector of length \( n + m \) obtained by concatenating the elements of \( \text{xs} \) followed by the elements of \( \text{ys} \).
Vector.instHAppendHAddNat instance
: HAppend (Vector α n) (Vector α m) (Vector α (n + m))
Full source
instance : HAppend (Vector α n) (Vector α m) (Vector α (n + m)) where
  hAppend := append
Vector Concatenation as Heterogeneous Append
Informal description
For any type $\alpha$ and natural numbers $n$ and $m$, the concatenation of two vectors $\text{xs} : \text{Vector} \, \alpha \, n$ and $\text{ys} : \text{Vector} \, \alpha \, m$ is a vector of type $\text{Vector} \, \alpha \, (n + m)$.
Vector.cast definition
(h : n = m) (xs : Vector α n) : Vector α m
Full source
/-- Creates a vector from another with a provably equal length. -/
@[inline] protected def cast (h : n = m) (xs : Vector α n) : Vector α m :=
  ⟨xs.toArray, by simp [*]⟩
Vector length conversion
Informal description
Given a proof that natural numbers \( n \) and \( m \) are equal, the function converts a vector of length \( n \) to a vector of length \( m \).
Vector.take definition
(xs : Vector α n) (i : Nat) : Vector α (min i n)
Full source
/--
Extract the first `i` elements of a vector. If `i` is greater than or equal to the size of the
vector then the vector is returned unchanged.
-/
@[inline] def take (xs : Vector α n) (i : Nat) : Vector α (min i n) :=
  ⟨xs.toArray.take i, by simp⟩
Take first `i` elements of a vector
Informal description
Given a vector `xs` of length `n` with elements of type `α` and a natural number `i`, the function returns a new vector containing the first `i` elements of `xs`. If `i` is greater than or equal to `n`, the entire vector `xs` is returned. The length of the resulting vector is the minimum of `i` and `n`.
Vector.take_eq_extract theorem
(xs : Vector α n) (i : Nat) : xs.take i = xs.extract 0 i
Full source
@[simp] theorem take_eq_extract (xs : Vector α n) (i : Nat) : xs.take i = xs.extract 0 i := rfl
Equality of Take and Extract Operations on Vectors: $\text{take}_i(xs) = \text{extract}_{0,i}(xs)$
Informal description
For any vector $xs$ of length $n$ with elements of type $\alpha$ and any natural number $i$, the vector obtained by taking the first $i$ elements of $xs$ is equal to the vector obtained by extracting the elements from index $0$ to $i$ of $xs$.
Vector.drop definition
(xs : Vector α n) (i : Nat) : Vector α (n - i)
Full source
/--
Deletes the first `i` elements of a vector. If `i` is greater than or equal to the size of the
vector then the empty vector is returned.
-/
@[inline] def drop (xs : Vector α n) (i : Nat) : Vector α (n - i) :=
  ⟨xs.toArray.drop i, by simp⟩
Vector drop operation
Informal description
Given a vector `xs` of type `α` with fixed length `n` and a natural number `i`, the function returns a new vector consisting of the elements of `xs` with the first `i` elements removed. The resulting vector has length `n - i`. If `i` is greater than or equal to `n`, the result is the empty vector (of length `0`).
Vector.drop_eq_cast_extract theorem
(xs : Vector α n) (i : Nat) : xs.drop i = (xs.extract i n).cast (by simp)
Full source
@[simp] theorem drop_eq_cast_extract (xs : Vector α n) (i : Nat) :
    xs.drop i = (xs.extract i n).cast (by simp) := by
  simp [drop, extract, Vector.cast]
Equality between vector drop and cast of extract: $\text{drop}_i(xs) = \text{cast}(\text{extract}_{i,n}(xs))$
Informal description
For any vector $xs$ of length $n$ with elements of type $\alpha$ and any natural number $i$, the vector obtained by dropping the first $i$ elements of $xs$ is equal to the vector obtained by extracting the subarray from index $i$ to $n$ and then casting the result to have length $n - i$.
Vector.shrink definition
(xs : Vector α n) (i : Nat) : Vector α (min i n)
Full source
/-- Shrinks a vector to the first `m` elements, by repeatedly popping the last element. -/
@[inline] def shrink (xs : Vector α n) (i : Nat) : Vector α (min i n) :=
  ⟨xs.toArray.shrink i, by simp⟩
Truncated vector to first `min(i, n)` elements
Informal description
Given a vector `xs` of type `α` with fixed length `n` and a natural number `i`, the function returns a new vector consisting of the first `min(i, n)` elements of `xs`. This is achieved by truncating the underlying array to the first `i` elements (or keeping the entire array if `i` exceeds `n`).
Vector.shrink_eq_take theorem
(xs : Vector α n) (i : Nat) : xs.shrink i = xs.take i
Full source
@[simp] theorem shrink_eq_take (xs : Vector α n) (i : Nat) : xs.shrink i = xs.take i := by
  simp [shrink, take]
Shrink Operation Equals Take Operation on Vectors: $\text{shrink}_i(xs) = \text{take}_i(xs)$
Informal description
For any vector $xs$ of type $\alpha$ with fixed length $n$ and any natural number $i$, the operation `xs.shrink i` produces the same vector as `xs.take i$, where `take` returns the prefix of the vector with at most $i$ elements (or the entire vector if $i \geq n$).
Vector.map definition
(f : α → β) (xs : Vector α n) : Vector β n
Full source
/-- Maps elements of a vector using the function `f`. -/
@[inline] def map (f : α → β) (xs : Vector α n) : Vector β n :=
  ⟨xs.toArray.map f, by simp⟩
Vector mapping function
Informal description
Given a function \( f : \alpha \to \beta \) and a vector \( \text{xs} : \text{Vector} \, \alpha \, n \), the function `Vector.map` applies \( f \) to each element of `xs` and returns a new vector of type \( \text{Vector} \, \beta \, n \) containing the results, maintaining the same length \( n \). Examples: - \( \text{map} \, f \, \langle a, b, c \rangle = \langle f \, a, f \, b, f \, c \rangle \) - \( \text{map} \, \text{Nat.succ} \, \langle \rangle = \langle \rangle \) - \( \text{map} \, (\cdot.\text{length}) \, \langle \text{"one"}, \text{"two"}, \text{"three"} \rangle = \langle 3, 3, 5 \rangle \) - \( \text{map} \, (\cdot.\text{reverse}) \, \langle \text{"one"}, \text{"two"}, \text{"three"} \rangle = \langle \text{"eno"}, \text{"owt"}, \text{"eerht"} \rangle \)
Vector.mapIdx definition
(f : Nat → α → β) (xs : Vector α n) : Vector β n
Full source
/-- Maps elements of a vector using the function `f`, which also receives the index of the element. -/
@[inline] def mapIdx (f : Nat → α → β) (xs : Vector α n) : Vector β n :=
  ⟨xs.toArray.mapIdx f, by simp⟩
Mapping over vector elements with indices
Informal description
Given a function \( f : \mathbb{N} \to \alpha \to \beta \) and a vector \( \text{xs} : \text{Vector} \, \alpha \, n \), the function `Vector.mapIdx` applies \( f \) to each element of `xs` along with its index (from 0 to \( n-1 \)), returning a new vector of type \( \text{Vector} \, \beta \, n \) containing the results while preserving the original length \( n \).
Vector.mapFinIdx definition
(xs : Vector α n) (f : (i : Nat) → α → (h : i < n) → β) : Vector β n
Full source
/-- Maps elements of a vector using the function `f`,
which also receives the index of the element, and the fact that the index is less than the size of the vector. -/
@[inline] def mapFinIdx (xs : Vector α n) (f : (i : Nat) → α → (h : i < n) → β) : Vector β n :=
  ⟨xs.toArray.mapFinIdx (fun i a h => f i a (by simpa [xs.size_toArray] using h)), by simp⟩
Mapping over vector elements with bounded indices
Informal description
Given a vector `xs` of type `Vector α n` and a function `f` that takes an index `i`, an element of the vector, and a proof that `i < n`, the function `Vector.mapFinIdx` applies `f` to each element of `xs` and returns a new vector of type `Vector β n` containing the results. The function ensures that the index `i` is always valid when accessing elements of `xs`.
Vector.mapM definition
[Monad m] (f : α → m β) (xs : Vector α n) : m (Vector β n)
Full source
/-- Map a monadic function over a vector. -/
@[inline] def mapM [Monad m] (f : α → m β) (xs : Vector α n) : m (Vector β n) := do
  go 0 (Nat.zero_le n) #v[]
where
  go (k : Nat) (h : k ≤ n) (acc : Vector β k) : m (Vector β n) := do
    if h' : k < n then
      go (k+1) (by omega) (acc.push (← f xs[k]))
    else
      return acc.cast (by omega)
Monadic map over a vector
Informal description
Given a monad `m`, a function `f : α → m β`, and a vector `xs : Vector α n`, the function `Vector.mapM` applies `f` to each element of `xs` in sequence, collecting the results into a new vector of type `Vector β n` within the monadic context `m`.
Vector.forM definition
[Monad m] (xs : Vector α n) (f : α → m PUnit) : m PUnit
Full source
@[inline] protected def forM [Monad m] (xs : Vector α n) (f : α → m PUnit) : m PUnit :=
  xs.toArray.forM f
Monadic iteration over a vector with side effects
Informal description
Given a monad `m`, a vector `xs` of length `n` with elements of type `α`, and a function `f : α → m PUnit`, the function `Vector.forM` applies `f` to each element of `xs` in sequence, performing monadic actions without returning any meaningful result (since the return type is `PUnit`). This is useful for performing side effects on each element of the vector.
Vector.flatMapM definition
[Monad m] (xs : Vector α n) (f : α → m (Vector β k)) : m (Vector β (n * k))
Full source
@[inline] def flatMapM [Monad m] (xs : Vector α n) (f : α → m (Vector β k)) : m (Vector β (n * k)) := do
  go 0 (Nat.zero_le n) (#v[].cast (by omega))
where
  go (i : Nat) (h : i ≤ n) (acc : Vector β (i * k)) : m (Vector β (n * k)) := do
    if h' : i < n then
      go (i+1) (by omega) ((acc ++ (← f xs[i])).cast (Nat.succ_mul i k).symm)
    else
      return acc.cast (by congr; omega)
Monadic flat map for vectors with concatenation
Informal description
Given a monad `m`, a vector `xs` of length `n` with elements of type `α`, and a function `f` that maps each element of `α` to a monadic computation producing a vector of length `k` with elements of type `β`, the function `Vector.flatMapM` applies `f` to each element of `xs` in sequence, concatenates the resulting vectors, and returns the concatenated vector of length `n * k` within the monadic context `m`.
Vector.mapFinIdxM definition
{α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (xs : Vector α n) (f : (i : Nat) → α → (h : i < n) → m β) : m (Vector β n)
Full source
/-- Variant of `mapIdxM` which receives the index `i` along with the bound `i < n. -/
@[inline]
def mapFinIdxM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m]
    (xs : Vector α n) (f : (i : Nat) → α → (h : i < n) → m β) : m (Vector β n) :=
  let rec @[specialize] map (i : Nat) (j : Nat) (inv : i + j = n) (ys : Vector β (n - i)) : m (Vector β n) := do
    match i, inv with
    | 0,    _  => pure ys
    | i+1, inv =>
      have j_lt : j < n := by
        rw [← inv, Nat.add_assoc, Nat.add_comm 1 j, Nat.add_comm]
        apply Nat.le_add_right
      have : i + (j + 1) = n := by rw [← inv, Nat.add_comm j 1, Nat.add_assoc]
      map i (j+1) this ((ys.push (← f j xs[j] j_lt)).cast (by omega))
  map n 0 rfl (#v[].cast (by simp))
Monadic mapping over a vector with index and bounds proof
Informal description
Given a monad `m`, a vector `xs` of length `n` with elements of type `α`, and a function `f` that takes an index `i`, an element of `xs`, and a proof that `i < n`, and returns a monadic computation producing an element of type `β`, the function `Vector.mapFinIdxM` applies `f` to each element of `xs` along with its index and the proof that the index is within bounds, and returns a monadic computation producing a new vector of length `n` with elements of type `β`.
Vector.mapIdxM definition
{α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : Nat → α → m β) (xs : Vector α n) : m (Vector β n)
Full source
@[inline]
def mapIdxM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : Nat → α → m β) (xs : Vector α n) : m (Vector β n) :=
  xs.mapFinIdxM fun i a _ => f i a
Monadic mapping over a vector with index
Informal description
Given a monad `m`, a vector `xs` of length `n` with elements of type `α`, and a function `f` that takes an index `i` and an element of `xs`, and returns a monadic computation producing an element of type `β`, the function `Vector.mapIdxM` applies `f` to each element of `xs` along with its index, and returns a monadic computation producing a new vector of length `n` with elements of type `β`.
Vector.firstM definition
{α : Type u} {m : Type v → Type w} [Alternative m] (f : α → m β) (xs : Vector α n) : m β
Full source
@[inline] def firstM {α : Type u} {m : Type v → Type w} [Alternative m] (f : α → m β) (xs : Vector α n) : m β :=
  xs.toArray.firstM f
First successful monadic application over a vector
Informal description
Given a monad `m` with an `Alternative` instance, a vector `xs` of length `n` with elements of type `α`, and a function `f : α → m β`, the function `Vector.firstM` applies `f` to each element of `xs` from left to right until it succeeds (i.e., returns a value in `m β`). If all applications of `f` result in `failure`, the function returns `failure`.
Vector.flatten definition
(xs : Vector (Vector α n) m) : Vector α (m * n)
Full source
@[inline] def flatten (xs : Vector (Vector α n) m) : Vector α (m * n) :=
  ⟨(xs.toArray.map Vector.toArray).flatten,
    by rcases xs; simp_all [Function.comp_def, Array.map_const']⟩
Flattening a vector of vectors
Informal description
Given a vector `xs` of type `Vector (Vector α n) m` (a vector of `m` vectors, each of length `n`), the function concatenates all the nested vectors into a single vector of type `Vector α (m * n)`, preserving the order of elements. The resulting vector has length `m * n`.
Vector.flatMap definition
(xs : Vector α n) (f : α → Vector β m) : Vector β (n * m)
Full source
@[inline] def flatMap (xs : Vector α n) (f : α → Vector β m) : Vector β (n * m) :=
  ⟨xs.toArray.flatMap fun a => (f a).toArray, by simp [Array.map_const']⟩
Flat map for vectors
Informal description
Given a vector `xs : Vector α n` and a function `f : α → Vector β m`, the function `Vector.flatMap` applies `f` to each element of `xs` and concatenates all the resulting vectors into a single vector of length `n * m`.
Vector.zipWithIndex abbrev
Full source
@[deprecated zipIdx (since := "2025-01-21")]
abbrev zipWithIndex := @zipIdx
Pairing Vector Elements with Indices
Informal description
Given a vector `v : Vector α n`, the function `Vector.zipWithIndex` returns a new vector of pairs where each element is formed by pairing each element of `v` with its corresponding index (from `0` to `n-1`). The resulting vector has the same length `n` as the input vector.
Vector.zip definition
(as : Vector α n) (bs : Vector β n) : Vector (α × β) n
Full source
@[inline] def zip (as : Vector α n) (bs : Vector β n) : Vector (α × β) n :=
  ⟨as.toArray.zip bs.toArray, by simp⟩
Zipping two vectors into a vector of pairs
Informal description
Given two vectors `as : Vector α n` and `bs : Vector β n` of the same length `n`, the function returns a new vector of pairs where each element is formed by combining the corresponding elements from `as` and `bs` at the same index. The resulting vector has the same length `n` as the input vectors.
Vector.zipWith definition
(f : α → β → φ) (as : Vector α n) (bs : Vector β n) : Vector φ n
Full source
/-- Maps corresponding elements of two vectors of equal size using the function `f`. -/
@[inline] def zipWith (f : α → β → φ) (as : Vector α n) (bs : Vector β n) : Vector φ n :=
  ⟨as.toArray.zipWith f bs.toArray, by simp⟩
Element-wise function application on fixed-length vectors
Informal description
Given a function \( f : \alpha \to \beta \to \varphi \) and two vectors \( \text{as} : \text{Vector} \, \alpha \, n \) and \( \text{bs} : \text{Vector} \, \beta \, n \), the function `Vector.zipWith` returns a new vector of type \( \text{Vector} \, \varphi \, n \) where each element at index \( i \) is the result of applying \( f \) to the \( i \)-th elements of \( \text{as} \) and \( \text{bs} \).
Vector.unzip definition
(xs : Vector (α × β) n) : Vector α n × Vector β n
Full source
@[inline] def unzip (xs : Vector (α × β) n) : VectorVector α n × Vector β n :=
  ⟨⟨xs.toArray.unzip.1, by simp⟩, ⟨xs.toArray.unzip.2, by simp⟩⟩
Vector pair separation (unzip)
Informal description
Given a vector `xs` of pairs of type `α × β` with fixed length `n`, the function separates the vector into two vectors: the first containing all the first components of the pairs, and the second containing all the second components. Both resulting vectors have the same length `n` as the input vector, and the order of elements is preserved in both vectors.
Vector.ofFn definition
(f : Fin n → α) : Vector α n
Full source
/-- The vector of length `n` whose `i`-th element is `f i`. -/
@[inline] def ofFn (f : Fin n → α) : Vector α n :=
  ⟨Array.ofFn f, by simp⟩
Vector constructed from a function on indices
Informal description
The vector of length $n$ whose $i$-th element is $f(i)$, where $i$ ranges over the finite set of natural numbers less than $n$.
Vector.swapIfInBounds definition
(xs : Vector α n) (i j : Nat) : Vector α n
Full source
/--
Swap two elements of a vector using `Nat` indices. Panics if either index is out of bounds.

This will perform the update destructively provided that the vector has a reference count of 1.
-/
@[inline] def swapIfInBounds (xs : Vector α n) (i j : Nat) : Vector α n :=
  ⟨xs.toArray.swapIfInBounds i j, by simp⟩
Conditional vector element swap
Informal description
Given a vector `xs` of type `α` with fixed length `n` and two indices `i` and `j`, the function swaps the elements at positions `i` and `j` if both indices are within bounds (i.e., `i < n` and `j < n`). If either index is out of bounds, the function returns the original vector unchanged. The swap is performed in-place when the vector reference is unique.
Vector.swapAt! definition
(xs : Vector α n) (i : Nat) (x : α) : α × Vector α n
Full source
/--
Swaps an element of a vector with a given value using a `Nat` index. Panics if the index is out of
bounds. The original value is returned along with the updated vector.

This will perform the update destructively provided that the vector has a reference count of 1.
-/
@[inline] def swapAt! (xs : Vector α n) (i : Nat) (x : α) : α × Vector α n :=
  let a := xs.toArray.swapAt! i x
  ⟨a.fst, a.snd, by simp [a]⟩
Vector element swap at index
Informal description
Given a vector `xs` of type `α` with fixed length `n`, an index `i`, and a value `x` of type `α`, the function returns a pair consisting of the original value at index `i` and a new vector where the element at index `i` has been replaced with `x`. If the index `i` is out of bounds (i.e., `i ≥ n`), the function panics with an error message. This operation performs the update destructively when the vector has a reference count of 1.
Vector.range definition
(n : Nat) : Vector Nat n
Full source
/-- The vector `#v[0, 1, 2, ..., n-1]`. -/
@[inline] def range (n : Nat) : Vector Nat n := ⟨Array.range n, by simp⟩
Vector of natural numbers from 0 to n-1
Informal description
The function constructs a vector of natural numbers from $0$ to $n-1$ with length $n$. For example: - `Vector.range 5` produces the vector $[0, 1, 2, 3, 4]$ - `Vector.range 0` produces the empty vector $[]$ - `Vector.range 1$ produces the vector $[0]$
Vector.isEqv definition
(xs ys : Vector α n) (r : α → α → Bool) : Bool
Full source
/--
Compares two vectors of the same size using a given boolean relation `r`. `isEqv v w r` returns
`true` if and only if `r v[i] w[i]` is true for all indices `i`.
-/
@[inline] def isEqv (xs ys : Vector α n) (r : α → α → Bool) : Bool :=
  Array.isEqvAux xs.toArray ys.toArray (by simp) r n (by simp)
Element-wise equivalence of vectors under a relation
Informal description
Given two vectors `xs` and `ys` of the same length `n` and a boolean relation `r : α → α → Bool`, the function `isEqv` returns `true` if and only if `r` holds for all corresponding elements of `xs` and `ys`, i.e., `r (xs[i]) (ys[i])` is true for every index `i`.
Vector.instBEq instance
[BEq α] : BEq (Vector α n)
Full source
instance [BEq α] : BEq (Vector α n) where
  beq xs ys := isEqv xs ys (· == ·)
Element-wise Boolean Equality for Fixed-length Vectors
Informal description
For any type $\alpha$ with a boolean equality relation and any natural number $n$, the type of fixed-length vectors $\text{Vector}\,\alpha\,n$ inherits a boolean equality relation that compares vectors element-wise.
Vector.reverse definition
(xs : Vector α n) : Vector α n
Full source
/-- Reverse the elements of a vector. -/
@[inline] def reverse (xs : Vector α n) : Vector α n :=
  ⟨xs.toArray.reverse, by simp⟩
Vector reversal function
Informal description
The function takes a vector `xs` of type `α` with fixed length `n` and returns a new vector of the same length where the elements are in reverse order. For example: - The reverse of the empty vector `⟨#[], n=0⟩` is `⟨#[], n=0⟩` - The reverse of `⟨#[0, 1], n=2⟩` is `⟨#[1, 0], n=2⟩` - The reverse of `⟨#[0, 1, 2], n=3⟩` is `⟨#[2, 1, 0], n=3⟩`
Vector.eraseIdx! definition
(xs : Vector α n) (i : Nat) : Vector α (n - 1)
Full source
/-- Delete an element of a vector using a `Nat` index. Panics if the index is out of bounds. -/
@[inline] def eraseIdx! (xs : Vector α n) (i : Nat) : Vector α (n-1) :=
  if _ : i < n then
    xs.eraseIdx i
  else
    have : Inhabited (Vector α (n-1)) := ⟨xs.pop⟩
    panic! "index out of bounds"
Remove element at index from fixed-length vector (panics if out of bounds)
Informal description
Given a vector \( \text{xs} \) of type \( \text{Vector} \alpha n \) (a fixed-length array of length \( n \) with elements of type \( \alpha \)) and an index \( i \) of type \( \mathbb{N} \), the function \( \text{Vector.eraseIdx!} \) removes the element at index \( i \) from \( \text{xs} \), returning a new vector of length \( n - 1 \). If \( i \) is out of bounds (i.e., \( i \geq n \)), the function panics with an "index out of bounds" error.
Vector.insertIdx! definition
(xs : Vector α n) (i : Nat) (x : α) : Vector α (n + 1)
Full source
/-- Insert an element into a vector using a `Nat` index. Panics if the index is out of bounds. -/
@[inline] def insertIdx! (xs : Vector α n) (i : Nat) (x : α) : Vector α (n+1) :=
  if _ : i ≤ n then
    xs.insertIdx i x
  else
    have : Inhabited (Vector α (n+1)) := ⟨xs.push x⟩
    panic! "index out of bounds"
Insert element into vector at index (panics if out of bounds)
Informal description
Given a vector \( \text{xs} \) of type \( \text{Vector} \alpha n \) (a fixed-length array of length \( n \) with elements of type \( \alpha \)), an index \( i \) of type \( \mathbb{N} \), and an element \( x \) of type \( \alpha \), the function \( \text{Vector.insertIdx!} \) inserts \( x \) into \( \text{xs} \) at index \( i \), returning a new vector of length \( n + 1 \). If \( i \) is out of bounds (i.e., \( i > n \)), the function panics with an "index out of bounds" error.
Vector.tail definition
(xs : Vector α n) : Vector α (n - 1)
Full source
/-- Delete the first element of a vector. Returns the empty vector if the input vector is empty. -/
@[inline] def tail (xs : Vector α n) : Vector α (n-1) :=
  if _ : 0 < n then
    xs.eraseIdx 0
  else
    xs.cast (by omega)
Tail of a vector
Informal description
Given a vector `xs` of type `Vector α n` (an array of fixed length `n` with elements of type `α`), the `tail` operation returns a new vector of length `n - 1` obtained by removing the first element. If the input vector is empty (n = 0), it returns the empty vector unchanged.
Vector.finIdxOf? definition
[BEq α] (xs : Vector α n) (x : α) : Option (Fin n)
Full source
/--
Finds the first index of a given value in a vector using `==` for comparison. Returns `none` if the
no element of the index matches the given value.
-/
@[inline] def finIdxOf? [BEq α] (xs : Vector α n) (x : α) : Option (Fin n) :=
  (xs.toArray.finIdxOf? x).map (Fin.cast xs.size_toArray)
First occurrence index of an element in a vector (as finite index)
Informal description
Given a vector `xs` of type `Vector α n` (a fixed-length array of length `n` with elements of type `α`) and an element `x` of type `α`, the function returns the index of the first occurrence of `x` in `xs` as an optional finite index (`Option (Fin n)`). If `x` is found, it returns `some i` where `i` is a valid index within the bounds of `xs`. If `x` is not found, it returns `none`. The comparison is performed using the boolean equality operator `==` from the `BEq` typeclass.
Vector.indexOf? abbrev
Full source
@[deprecated finIdxOf? (since := "2025-01-29")]
abbrev indexOf? := @finIdxOf?
First Occurrence Index of an Element in a Fixed-Length Vector
Informal description
Given a vector $\mathbf{v}$ of type `Vector α n` (a fixed-length array of length $n$ with elements of type $\alpha$) and an element $x$ of type $\alpha$, the function returns the index of the first occurrence of $x$ in $\mathbf{v}$ as an optional natural number (`Option Nat`). If $x$ is found, it returns `some i` where $i$ is a valid index within the bounds of $\mathbf{v}$. If $x$ is not found, it returns `none`. The comparison is performed using the boolean equality operator `==` from the `BEq` typeclass.
Vector.findFinIdx? definition
(p : α → Bool) (xs : Vector α n) : Option (Fin n)
Full source
/-- Finds the first index of a given value in a vector using a predicate. Returns `none` if the
no element of the index matches the given value. -/
@[inline] def findFinIdx? (p : α → Bool) (xs : Vector α n) : Option (Fin n) :=
  (xs.toArray.findFinIdx? p).map (Fin.cast xs.size_toArray)
First index in vector satisfying predicate (with bounds proof)
Informal description
Given a vector `xs` of type `α` with fixed length `n` and a predicate `p : α → Bool`, the function returns the first index `i` (as a `Fin n` value) where `p(xs[i])` is true, or `none` if no such element exists. The index is guaranteed to be within bounds of the vector.
Vector.findM? definition
{α : Type} {m : Type → Type} [Monad m] (f : α → m Bool) (as : Vector α n) : m (Option α)
Full source
/--
Note that the universe level is contrained to `Type` here,
to avoid having to have the predicate live in `p : α → m (ULift Bool)`.
-/
@[inline] def findM? {α : Type} {m : Type → Type} [Monad m] (f : α → m Bool) (as : Vector α n) : m (Option α) :=
  as.toArray.findM? f
Monadic find-first in fixed-length vector
Informal description
Given a monadic predicate \( f : \alpha \to m \text{Bool} \) and a fixed-length vector \( \text{as} : \text{Vector } \alpha \text{ } n \), the function applies \( f \) to each element of `as` in order and returns the first element for which \( f \) returns `true`, wrapped in `some`. If no such element exists, it returns `none`. The computation stops as soon as a matching element is found.
Vector.findSomeM? definition
[Monad m] (f : α → m (Option β)) (as : Vector α n) : m (Option β)
Full source
@[inline] def findSomeM? [Monad m] (f : α → m (Option β)) (as : Vector α n) : m (Option β) :=
  as.toArray.findSomeM? f
Monadic find-first-some operation on fixed-length vectors
Informal description
Given a monadic function \( f : \alpha \to m (\text{Option } \beta) \) and a fixed-length vector \( \text{as} : \text{Vector } \alpha \text{ } n \), the function applies \( f \) to each element of `as` in order and returns the first non-`none` result. If all applications of \( f \) return `none`, the result is `none`. The computation stops as soon as a non-`none` result is found.
Vector.findRevM? definition
{α : Type} {m : Type → Type} [Monad m] (f : α → m Bool) (as : Vector α n) : m (Option α)
Full source
/--
Note that the universe level is contrained to `Type` here,
to avoid having to have the predicate live in `p : α → m (ULift Bool)`.
-/
@[inline] def findRevM? {α : Type} {m : Type → Type} [Monad m] (f : α → m Bool) (as : Vector α n) : m (Option α) :=
  as.toArray.findRevM? f
Reverse monadic find in fixed-length vector
Informal description
Given a monadic predicate \( f : \alpha \to m \text{Bool} \) and a fixed-length vector \( \text{as} : \text{Vector } \alpha \text{ } n \), the function `findRevM?` applies \( f \) to each element of `as` in reverse order (from right to left) and returns the last element for which \( f \) returns `true`, wrapped in `some`. If no such element exists, it returns `none`. The computation stops as soon as a matching element is found.
Vector.findSomeRevM? definition
[Monad m] (f : α → m (Option β)) (as : Vector α n) : m (Option β)
Full source
@[inline] def findSomeRevM? [Monad m] (f : α → m (Option β)) (as : Vector α n) : m (Option β) :=
  as.toArray.findSomeRevM? f
Reverse monadic find with optional result on fixed-length vectors
Informal description
Given a monadic function \( f : \alpha \to m (\text{Option } \beta) \) and a fixed-length vector \( \text{as} : \text{Vector } \alpha \text{ } n \), the function `findSomeRevM?` applies \( f \) to each element of `as` in reverse order (from right to left) and returns the first non-`none` result. If all applications of \( f \) return `none`, the result is `none`. The computation stops as soon as a non-`none` result is found.
Vector.find? definition
{α : Type} (f : α → Bool) (as : Vector α n) : Option α
Full source
@[inline] def find? {α : Type} (f : α → Bool) (as : Vector α n) : Option α :=
  as.toArray.find? f
Find first element in fixed-length vector satisfying predicate
Informal description
Given a predicate \( f : \alpha \to \text{Bool} \) and a fixed-length vector \( \text{as} : \text{Vector } \alpha \text{ } n \), the function returns the first element \( a \) in \( \text{as} \) for which \( f(a) \) evaluates to \( \text{true} \), wrapped in \( \text{some} \). If no such element exists, it returns \( \text{none} \).
Vector.findRev? definition
{α : Type} (f : α → Bool) (as : Vector α n) : Option α
Full source
@[inline] def findRev? {α : Type} (f : α → Bool) (as : Vector α n) : Option α :=
  as.toArray.findRev? f
Reverse find in fixed-length vector
Informal description
Given a predicate \( f : \alpha \to \text{Bool} \) and a fixed-length vector \( \text{as} : \text{Vector } \alpha \ n \), the function returns the last element in `as` (when traversed from right to left) that satisfies \( f \), wrapped in `some`. If no such element exists, it returns `none`.
Vector.findSome? definition
(f : α → Option β) (as : Vector α n) : Option β
Full source
@[inline] def findSome? (f : α → Option β) (as : Vector α n) : Option β :=
  as.toArray.findSome? f
First non-none result in fixed-length vector
Informal description
Given a function \( f : \alpha \to \text{Option } \beta \) and a fixed-length vector \( \text{as} : \text{Vector } \alpha \ n \), the function applies \( f \) to each element of `as` in order and returns the first non-`none` result. If all applications of \( f \) return `none`, the result is `none`.
Vector.findSomeRev? definition
(f : α → Option β) (as : Vector α n) : Option β
Full source
@[inline] def findSomeRev? (f : α → Option β) (as : Vector α n) : Option β :=
  as.toArray.findSomeRev? f
Reverse find with optional result on fixed-length vectors
Informal description
Given a function \( f : \alpha \to \text{Option } \beta \) and a vector \( \text{as} : \text{Vector } \alpha \ n \), the function `findSomeRev?` applies \( f \) to each element of `as` in reverse order (from right to left) and returns the first non-`none` result. If all applications of \( f \) return `none`, the result is `none`.
Vector.isPrefixOf definition
[BEq α] (xs : Vector α m) (ys : Vector α n) : Bool
Full source
/-- Returns `true` when `xs` is a prefix of the vector `ys`. -/
@[inline] def isPrefixOf [BEq α] (xs : Vector α m) (ys : Vector α n) : Bool :=
  xs.toArray.isPrefixOf ys.toArray
Vector prefix check
Informal description
Given two vectors `xs : Vector α m` and `ys : Vector α n` with a boolean equality relation on `α`, the function returns `true` if `xs` is a prefix of `ys`, and `false` otherwise. Specifically: - If the length of `xs` is less than or equal to the length of `ys`, it checks element-wise equality starting from the first index. - If the length of `xs` is greater than the length of `ys`, it immediately returns `false`. Examples: - `#[0, 1, 2].isPrefixOf #[0, 1, 2, 3]` returns `true` - `#[0, 1, 2].isPrefixOf #[0, 1, 2]` returns `true` - `#[0, 1, 2].isPrefixOf #[0, 1]` returns `false` - `#[].isPrefixOf #[0, 1]` returns `true`
Vector.anyM definition
[Monad m] (p : α → m Bool) (xs : Vector α n) : m Bool
Full source
/-- Returns `true` with the monad if `p` returns `true` for any element of the vector. -/
@[inline] def anyM [Monad m] (p : α → m Bool) (xs : Vector α n) : m Bool :=
  xs.toArray.anyM p
Monadic any for fixed-length vectors
Informal description
Given a monad `m`, a predicate `p : α → m Bool`, and a fixed-length vector `xs : Vector α n`, the function `Vector.anyM` applies `p` to each element of `xs` in the monadic context and returns `true` if any application of `p` returns `true`. The result is wrapped in the monad `m`.
Vector.allM definition
[Monad m] (p : α → m Bool) (xs : Vector α n) : m Bool
Full source
/-- Returns `true` with the monad if `p` returns `true` for all elements of the vector. -/
@[inline] def allM [Monad m] (p : α → m Bool) (xs : Vector α n) : m Bool :=
  xs.toArray.allM p
Monadic all for fixed-length vectors
Informal description
Given a monad `m`, a predicate `p : α → m Bool`, and a fixed-length vector `xs : Vector α n`, the function `Vector.allM` applies `p` to each element of `xs` in the monadic context and returns `true` if all applications of `p` return `true`. The result is wrapped in the monad `m`.
Vector.any definition
(xs : Vector α n) (p : α → Bool) : Bool
Full source
/-- Returns `true` if `p` returns `true` for any element of the vector. -/
@[inline] def any (xs : Vector α n) (p : α → Bool) : Bool :=
  xs.toArray.any p
Existence of satisfying element in a fixed-length vector
Informal description
Given a fixed-length vector `xs : Vector α n` and a predicate `p : α → Bool`, the function `Vector.any` returns `true` if there exists at least one element in `xs` for which `p` evaluates to `true`.
Vector.all definition
(xs : Vector α n) (p : α → Bool) : Bool
Full source
/-- Returns `true` if `p` returns `true` for all elements of the vector. -/
@[inline] def all (xs : Vector α n) (p : α → Bool) : Bool :=
  xs.toArray.all p
Universal quantification over a fixed-length vector
Informal description
Given a fixed-length vector \( \text{xs} \) of type \( \alpha \) with length \( n \) and a predicate \( p : \alpha \to \text{Bool} \), the function \( \text{Vector.all} \) returns \( \text{true} \) if \( p \) evaluates to \( \text{true} \) for every element in \( \text{xs} \).
Vector.countP definition
(p : α → Bool) (xs : Vector α n) : Nat
Full source
/-- Count the number of elements of a vector that satisfy the predicate `p`. -/
@[inline] def countP (p : α → Bool) (xs : Vector α n) : Nat :=
  xs.toArray.countP p
Count elements satisfying a predicate in a fixed-length vector
Informal description
The function counts the number of elements in a fixed-length vector `xs` of type `α` with length `n` that satisfy the Boolean predicate `p`. It returns a natural number representing the count.
Vector.count definition
[BEq α] (a : α) (xs : Vector α n) : Nat
Full source
/-- Count the number of elements of a vector that are equal to `a`. -/
@[inline] def count [BEq α] (a : α) (xs : Vector α n) : Nat :=
  xs.toArray.count a
Count occurrences of an element in a fixed-length vector
Informal description
Given a type $\alpha$ with a boolean equality relation and a fixed-length vector $\mathtt{xs} : \mathtt{Vector}~\alpha~n$, the function counts the number of elements in $\mathtt{xs}$ that are equal to $a$. The result is a natural number representing the count.
Vector.replace definition
[BEq α] (xs : Vector α n) (a b : α) : Vector α n
Full source
@[inline] def replace [BEq α] (xs : Vector α n) (a b : α) : Vector α n :=
  ⟨xs.toArray.replace a b, by simp⟩
Replacement in fixed-length vector
Informal description
Given a vector `xs` of type `Vector α n` and elements `a, b` of type `α`, the function replaces the first occurrence of `a` with `b` in `xs` (if `a` is present) and returns the modified vector of the same length `n`. If `a` is not found in `xs`, the original vector is returned unchanged.
Vector.leftpad definition
(n : Nat) (a : α) (xs : Vector α m) : Vector α (max n m)
Full source
/--
Pad a vector on the left with a given element.

Note that we immediately simplify this to an `++` operation,
and do not provide separate verification theorems.
-/
@[inline, simp] def leftpad (n : Nat) (a : α) (xs : Vector α m) : Vector α (max n m) :=
  (replicate (n - m) a ++ xs).cast (by omega)
Left-padding a vector to a given length
Informal description
Given a natural number $n$, an element $a$ of type $\alpha$, and a vector $\mathtt{xs}$ of length $m$, the function pads $\mathtt{xs}$ on the left with $n - m$ copies of $a$ (if $n > m$) and returns a vector of length $\max(n, m)$. If $n \leq m$, the original vector $\mathtt{xs}$ is returned unchanged (but cast to length $\max(n, m)$).
Vector.rightpad definition
(n : Nat) (a : α) (xs : Vector α m) : Vector α (max n m)
Full source
/--
Pad a vector on the right with a given element.

Note that we immediately simplify this to an `++` operation,
and do not provide separate verification theorems.
-/
@[inline, simp] def rightpad (n : Nat) (a : α) (xs : Vector α m) : Vector α (max n m) :=
  (xs ++ replicate (n - m) a).cast (by omega)
Right-padding of a vector to a specified length
Informal description
Given a natural number `n`, an element `a` of type `α`, and a vector `xs` of length `m`, the function pads `xs` on the right with `a` until the resulting vector has length `max n m`. The padding is done by appending `n - m` copies of `a` to `xs` (if `n > m`), or leaving `xs` unchanged otherwise (if `n ≤ m`).
Vector.mem_toArray_iff theorem
(a : α) (xs : Vector α n) : a ∈ xs.toArray ↔ a ∈ xs
Full source
@[simp] theorem mem_toArray_iff (a : α) (xs : Vector α n) : a ∈ xs.toArraya ∈ xs.toArray ↔ a ∈ xs :=
  ⟨fun h => ⟨h⟩, fun ⟨h⟩ => h⟩
Membership Equivalence between Vector and Underlying Array
Informal description
For any element $a$ of type $\alpha$ and any fixed-length vector $xs$ of type $\text{Vector}\,\alpha\,n$, the element $a$ belongs to the underlying array $xs.\text{toArray}$ if and only if $a$ belongs to the vector $xs$.
Vector.instForIn'InferInstanceMembership instance
: ForIn' m (Vector α n) α inferInstance
Full source
instance : ForIn' m (Vector α n) α inferInstance where
  forIn' xs b f := Array.forIn' xs.toArray b (fun a h b => f a (by simpa using h) b)
Monadic Iteration with Membership Proofs for Fixed-Length Vectors
Informal description
For any monad `m`, type `α`, and natural number `n`, the fixed-length vector type `Vector α n` supports monadic iteration with membership proofs in the `for h : x in xs` notation. This means that for a vector `v : Vector α n`, we can iterate over its elements `x` while also obtaining a proof `h` that `x` is indeed an element of `v`.
Vector.instForM instance
: ForM m (Vector α n) α
Full source
instance : ForM m (Vector α n) α where
  forM := Vector.forM
Monadic Iteration over Fixed-Length Vectors
Informal description
For any monad `m`, type `α`, and natural number `n`, the fixed-length vector type `Vector α n` supports monadic iteration over its elements. This means that for a vector `v : Vector α n`, we can iterate over its elements `x` while performing monadic actions.
Vector.forM_eq_forM theorem
[Monad m] (f : α → m PUnit) : Vector.forM v f = forM v f
Full source
@[simp] theorem forM_eq_forM [Monad m] (f : α → m PUnit) :
    Vector.forM v f = forM v f := rfl
Equality of Monadic Iteration on Fixed-Length Vectors
Informal description
For any monad `m` and function `f : α → m PUnit`, the monadic iteration over a fixed-length vector `v : Vector α n` using `Vector.forM` is equal to the monadic iteration over `v` using the generic `forM` function. That is, `Vector.forM v f = forM v f`.
Vector.instToStreamSubarray instance
: ToStream (Vector α n) (Subarray α)
Full source
instance : ToStream (Vector α n) (Subarray α) where
  toStream xs := xs.toArray[:n]
Fixed-Length Vector to Subarray Stream Conversion
Informal description
For any type $\alpha$ and natural number $n$, a fixed-length vector of type `Vector α n` can be converted into a stream represented as a subarray of $\alpha$.
Vector.instLT instance
[LT α] : LT (Vector α n)
Full source
instance instLT [LT α] : LT (Vector α n) := ⟨fun xs ys => xs.toArray < ys.toArray⟩
Lexicographic Order on Fixed-Length Vectors
Informal description
For any type $\alpha$ with a "less than" relation, the type of fixed-length vectors over $\alpha$ inherits a lexicographic "less than" relation.
Vector.instLE instance
[LT α] : LE (Vector α n)
Full source
instance instLE [LT α] : LE (Vector α n) := ⟨fun xs ys => xs.toArray ≤ ys.toArray⟩
Lexicographic Order on Fixed-Length Vectors
Informal description
For any type $\alpha$ with a "less than" relation, the type of fixed-length vectors over $\alpha$ inherits a lexicographic "less than or equal to" relation.