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 "}
{"# 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 α
/-- `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
instReprVector
instance
{α✝} {n✝} [Repr✝ α✝] : Repr✝ (@Vector✝ α✝ n✝)
Repr, DecidableEq
instDecidableEqVector
instance
{α✝} {n✝} [DecidableEq✝ α✝] : DecidableEq✝ (@Vector✝ α✝ n✝)
DecidableEq
Array.toVector
abbrev
(xs : Array α) : Vector α xs.size
Vector.term#v[_,]
definition
: Lean.ParserDescr✝
Vector.elimAsArray
definition
{motive : Vector α n → Sort u} (mk : ∀ (xs : Array α) (ha : xs.size = n), motive ⟨xs, ha⟩) :
(xs : Vector α n) → motive xs
Vector.elimAsList
definition
{motive : Vector α n → Sort u} (mk : ∀ (l : List α) (ha : l.length = n), motive ⟨⟨l⟩, ha⟩) :
(xs : Vector α n) → motive xs
Vector.emptyWithCapacity
definition
(capacity : Nat) : Vector α 0
/-- Make an empty vector with pre-allocated capacity. -/
@[inline] def emptyWithCapacity (capacity : Nat) : Vector α 0 := ⟨.mkEmpty capacity, rfl⟩
Vector.mkEmpty
abbrev
@[deprecated emptyWithCapacity (since := "2025-03-12"), inherit_doc emptyWithCapacity]
abbrev mkEmpty := @emptyWithCapacity
Vector.replicate
definition
(n) (v : α) : Vector α n
/-- Makes a vector of size `n` with all cells containing `v`. -/
@[inline] def replicate (n) (v : α) : Vector α n := ⟨Array.replicate n v, by simp⟩
Vector.mkVector
abbrev
@[deprecated replicate (since := "2025-03-18")]
abbrev mkVector := @replicate
Vector.instNonemptyOfNatNat
instance
: Nonempty (Vector α 0)
Vector.instNonempty
instance
[Nonempty α] : Nonempty (Vector α n)
instance [Nonempty α] : Nonempty (Vector α n) := ⟨replicate _ Classical.ofNonempty⟩
Vector.singleton
definition
(v : α) : Vector α 1
/-- Returns a vector of size `1` with element `v`. -/
@[inline] def singleton (v : α) : Vector α 1 := ⟨#[v], rfl⟩
Vector.instInhabited
instance
[Inhabited α] : Inhabited (Vector α n)
Vector.get
definition
(xs : Vector α n) (i : Fin n) : α
/-- 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.uget
definition
(xs : Vector α n) (i : USize) (h : i.toNat < n) : α
Vector.instGetElemNatLt
instance
: GetElem (Vector α n) Nat α fun _ i => i < n
Vector.contains
definition
[BEq α] (xs : Vector α n) (a : α) : Bool
Vector.Mem
structure
(xs : Vector α n) (a : α)
/-- `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
Vector.instMembership
instance
: Membership α (Vector α n)
instance : Membership α (Vector α n) where
mem := Mem
Vector.getD
definition
(xs : Vector α n) (i : Nat) (default : α) : α
Vector.back!
definition
[Inhabited α] (xs : Vector α n) : α
Vector.back?
definition
(xs : Vector α n) : Option α
Vector.back
definition
[NeZero n] (xs : Vector α n) : α
/-- 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))
Vector.head
definition
[NeZero n] (xs : Vector α n)
/-- The first element of a non-empty vector. -/
@[inline] def head [NeZero n] (xs : Vector α n) := xs[0]'(Nat.pos_of_neZero n)
Vector.push
definition
(xs : Vector α n) (x : α) : Vector α (n + 1)
/-- 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⟩
Vector.pop
definition
(xs : Vector α n) : Vector α (n - 1)
/-- Remove the last element of a vector. -/
@[inline] def pop (xs : Vector α n) : Vector α (n - 1) :=
⟨Array.pop xs.toArray, by simp⟩
Vector.setIfInBounds
definition
(xs : Vector α n) (i : Nat) (x : α) : Vector α n
/--
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⟩
Vector.set!
definition
(xs : Vector α n) (i : Nat) (x : α) : Vector α n
/--
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⟩
Vector.foldlM
definition
[Monad m] (f : β → α → m β) (b : β) (xs : Vector α n) : m β
Vector.foldrM
definition
[Monad m] (f : α → β → m β) (b : β) (xs : Vector α n) : m β
Vector.foldl
definition
(f : β → α → β) (b : β) (xs : Vector α n) : β
Vector.foldr
definition
(f : α → β → β) (b : β) (xs : Vector α n) : β
Vector.append
definition
(xs : Vector α n) (ys : Vector α m) : Vector α (n + m)
/-- Append two vectors. -/
@[inline] def append (xs : Vector α n) (ys : Vector α m) : Vector α (n + m) :=
⟨xs.toArray ++ ys.toArray, by simp⟩
Vector.instHAppendHAddNat
instance
: HAppend (Vector α n) (Vector α m) (Vector α (n + m))
Vector.cast
definition
(h : n = m) (xs : Vector α n) : Vector α m
/-- 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.take
definition
(xs : Vector α n) (i : Nat) : Vector α (min i n)
Vector.take_eq_extract
theorem
(xs : Vector α n) (i : Nat) : xs.take i = xs.extract 0 i
Vector.drop
definition
(xs : Vector α n) (i : Nat) : Vector α (n - i)
/--
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_eq_cast_extract
theorem
(xs : Vector α n) (i : Nat) : xs.drop i = (xs.extract i n).cast (by simp)
@[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]
Vector.shrink
definition
(xs : Vector α n) (i : Nat) : Vector α (min i n)
Vector.shrink_eq_take
theorem
(xs : Vector α n) (i : Nat) : xs.shrink i = xs.take i
Vector.map
definition
(f : α → β) (xs : Vector α n) : Vector β n
/-- 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.mapIdx
definition
(f : Nat → α → β) (xs : Vector α n) : Vector β n
/-- 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⟩
Vector.mapFinIdx
definition
(xs : Vector α n) (f : (i : Nat) → α → (h : i < n) → β) : Vector β n
/-- 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⟩
Vector.mapM
definition
[Monad m] (f : α → m β) (xs : Vector α n) : m (Vector β n)
/-- 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)
Vector.forM
definition
[Monad m] (xs : Vector α n) (f : α → m PUnit) : m PUnit
Vector.flatMapM
definition
[Monad m] (xs : Vector α n) (f : α → m (Vector β k)) : m (Vector β (n * k))
@[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)
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)
/-- 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))
Vector.mapIdxM
definition
{α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : Nat → α → m β) (xs : Vector α n) : m (Vector β n)
Vector.firstM
definition
{α : Type u} {m : Type v → Type w} [Alternative m] (f : α → m β) (xs : Vector α n) : m β
@[inline] def firstM {α : Type u} {m : Type v → Type w} [Alternative m] (f : α → m β) (xs : Vector α n) : m β :=
xs.toArray.firstM f
Vector.flatten
definition
(xs : Vector (Vector α n) m) : Vector α (m * n)
@[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']⟩
Vector.flatMap
definition
(xs : Vector α n) (f : α → Vector β m) : Vector β (n * m)
@[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']⟩
Vector.zipWithIndex
abbrev
@[deprecated zipIdx (since := "2025-01-21")]
abbrev zipWithIndex := @zipIdx
Vector.zip
definition
(as : Vector α n) (bs : Vector β n) : Vector (α × β) n
Vector.zipWith
definition
(f : α → β → φ) (as : Vector α n) (bs : Vector β n) : Vector φ n
/-- 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⟩
Vector.unzip
definition
(xs : Vector (α × β) n) : Vector α n × Vector β n
@[inline] def unzip (xs : Vector (α × β) n) : VectorVector α n × Vector β n :=
⟨⟨xs.toArray.unzip.1, by simp⟩, ⟨xs.toArray.unzip.2, by simp⟩⟩
Vector.ofFn
definition
(f : Fin n → α) : Vector α n
/-- 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.swapIfInBounds
definition
(xs : Vector α n) (i j : Nat) : Vector α n
/--
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⟩
Vector.swapAt!
definition
(xs : Vector α n) (i : Nat) (x : α) : α × Vector α n
/--
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.range
definition
(n : Nat) : Vector Nat n
/-- The vector `#v[0, 1, 2, ..., n-1]`. -/
@[inline] def range (n : Nat) : Vector Nat n := ⟨Array.range n, by simp⟩
Vector.isEqv
definition
(xs ys : Vector α n) (r : α → α → Bool) : Bool
/--
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)
Vector.instBEq
instance
[BEq α] : BEq (Vector α n)
Vector.reverse
definition
(xs : Vector α n) : Vector α n
/-- Reverse the elements of a vector. -/
@[inline] def reverse (xs : Vector α n) : Vector α n :=
⟨xs.toArray.reverse, by simp⟩
Vector.eraseIdx!
definition
(xs : Vector α n) (i : Nat) : Vector α (n - 1)
/-- 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"
Vector.insertIdx!
definition
(xs : Vector α n) (i : Nat) (x : α) : Vector α (n + 1)
/-- 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"
Vector.tail
definition
(xs : Vector α n) : Vector α (n - 1)
/-- 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)
Vector.finIdxOf?
definition
[BEq α] (xs : Vector α n) (x : α) : Option (Fin n)
/--
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)
Vector.indexOf?
abbrev
@[deprecated finIdxOf? (since := "2025-01-29")]
abbrev indexOf? := @finIdxOf?
Vector.findFinIdx?
definition
(p : α → Bool) (xs : Vector α n) : Option (Fin n)
/-- 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)
Vector.findM?
definition
{α : Type} {m : Type → Type} [Monad m] (f : α → m Bool) (as : Vector α n) : m (Option α)
Vector.findSomeM?
definition
[Monad m] (f : α → m (Option β)) (as : Vector α n) : m (Option β)
@[inline] def findSomeM? [Monad m] (f : α → m (Option β)) (as : Vector α n) : m (Option β) :=
as.toArray.findSomeM? f
Vector.findRevM?
definition
{α : Type} {m : Type → Type} [Monad m] (f : α → m Bool) (as : Vector α n) : m (Option α)
Vector.findSomeRevM?
definition
[Monad m] (f : α → m (Option β)) (as : Vector α n) : m (Option β)
@[inline] def findSomeRevM? [Monad m] (f : α → m (Option β)) (as : Vector α n) : m (Option β) :=
as.toArray.findSomeRevM? f
Vector.find?
definition
{α : Type} (f : α → Bool) (as : Vector α n) : Option α
Vector.findRev?
definition
{α : Type} (f : α → Bool) (as : Vector α n) : Option α
Vector.findSome?
definition
(f : α → Option β) (as : Vector α n) : Option β
Vector.findSomeRev?
definition
(f : α → Option β) (as : Vector α n) : Option β
@[inline] def findSomeRev? (f : α → Option β) (as : Vector α n) : Option β :=
as.toArray.findSomeRev? f
Vector.isPrefixOf
definition
[BEq α] (xs : Vector α m) (ys : Vector α n) : Bool
/-- 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.anyM
definition
[Monad m] (p : α → m Bool) (xs : Vector α n) : m Bool
Vector.allM
definition
[Monad m] (p : α → m Bool) (xs : Vector α n) : m Bool
Vector.any
definition
(xs : Vector α n) (p : α → Bool) : Bool
Vector.all
definition
(xs : Vector α n) (p : α → Bool) : Bool
Vector.countP
definition
(p : α → Bool) (xs : Vector α n) : Nat
Vector.count
definition
[BEq α] (a : α) (xs : Vector α n) : Nat
Vector.replace
definition
[BEq α] (xs : Vector α n) (a b : α) : Vector α n
@[inline] def replace [BEq α] (xs : Vector α n) (a b : α) : Vector α n :=
⟨xs.toArray.replace a b, by simp⟩
Vector.leftpad
definition
(n : Nat) (a : α) (xs : Vector α m) : Vector α (max n m)
/--
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)
Vector.rightpad
definition
(n : Nat) (a : α) (xs : Vector α m) : Vector α (max n m)
/--
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)
Vector.mem_toArray_iff
theorem
(a : α) (xs : Vector α n) : a ∈ xs.toArray ↔ a ∈ xs
@[simp] theorem mem_toArray_iff (a : α) (xs : Vector α n) : a ∈ xs.toArraya ∈ xs.toArray ↔ a ∈ xs :=
⟨fun h => ⟨h⟩, fun ⟨h⟩ => h⟩
Vector.instForIn'InferInstanceMembership
instance
: ForIn' m (Vector α n) α inferInstance
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)
Vector.instForM
instance
: ForM m (Vector α n) α
instance : ForM m (Vector α n) α where
forM := Vector.forM
Vector.forM_eq_forM
theorem
[Monad m] (f : α → m PUnit) : Vector.forM v f = forM v f
@[simp] theorem forM_eq_forM [Monad m] (f : α → m PUnit) :
Vector.forM v f = forM v f := rfl
Vector.instToStreamSubarray
instance
: ToStream (Vector α n) (Subarray α)
instance : ToStream (Vector α n) (Subarray α) where
toStream xs := xs.toArray[:n]
Vector.instLT
instance
[LT α] : LT (Vector α n)
instance instLT [LT α] : LT (Vector α n) := ⟨fun xs ys => xs.toArray < ys.toArray⟩
Vector.instLE
instance
[LT α] : LE (Vector α n)
instance instLE [LT α] : LE (Vector α n) := ⟨fun xs ys => xs.toArray ≤ ys.toArray⟩