The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V33. Data structures and computation

Mathlib.Data.Vector.Snoc

16 declarations. Mathlib docs · Source

Appending to the back of a vector

This file establishes a snoc : Vector α n → α → Vector α (n+1) operation, that appends a single element to the back of a vector.

It provides a collection of lemmas that show how different Vector operations reduce when their argument is snoc xs x.

Also, an alternative, reverse, induction principle is added, that breaks down a vector into snoc xs x for its inductive case. Effectively doing induction from right-to-left

Simplification lemmas

Reverse induction principle

More simplification lemmas

theorem List.Vector.cons.match_1.congr_eq_1 E1 #

∀ {α : Type u_1} {n : ℕ} (motive : α → List.Vector α n → Sort u_2) (x : α) (x_1 : List.Vector α n)
  (h_1 : (a : α) → (v : List α) → (h : v.length = n) → motive a ⟨v, h⟩) (a : α) (v : List α) (h : v.length = n),
  x = a →
    x_1 = ⟨v, h⟩ →
      (match x, x_1 with
        | a, ⟨v, h⟩ => h_1 a v h) ≍
        h_1 a v h

def List.Vector.snoc E1 #

{α : Type u_1} → {n : ℕ} → List.Vector α n → α → List.Vector α (n + 1)

Append a single element to the end of a vector

theorem List.Vector.snoc_cons E0 #

∀ {α : Type u_1} {n : ℕ} {x : α} (xs : List.Vector α n) {y : α}, (x ::ᵥ xs).snoc y = x ::ᵥ xs.snoc y

theorem List.Vector.snoc_nil E0 #

∀ {α : Type u_1} {x : α}, List.Vector.nil.snoc x = x ::ᵥ List.Vector.nil

theorem List.Vector.reverse_cons E0 #

∀ {α : Type u_1} {n : ℕ} {x : α} (xs : List.Vector α n), (x ::ᵥ xs).reverse = xs.reverse.snoc x

theorem List.Vector.reverse_snoc E0 #

∀ {α : Type u_1} {n : ℕ} {x : α} (xs : List.Vector α n), (xs.snoc x).reverse = x ::ᵥ xs.reverse

theorem List.Vector.replicate_succ_to_snoc E1 #

∀ {α : Type u_1} {n : ℕ} (val : α), List.Vector.replicate (n + 1) val = (List.Vector.replicate n val).snoc val

def List.Vector.revInductionOn E1 #

{α : Type u_1} →
  {C : {n : ℕ} → List.Vector α n → Sort u_5} →
    {n : ℕ} →
      (v : List.Vector α n) →
        C List.Vector.nil → ({n : ℕ} → (xs : List.Vector α n) → (x : α) → C xs → C (xs.snoc x)) → C v

Define C v by reverse induction on v : Vector α n. That is, break the vector down starting from the right-most element, using snoc

This function has two arguments: nil handles the base case on C nil, and snoc defines the inductive step using ∀ x : α, C xs → C (xs.snoc x).

This can be used as induction v using Vector.revInductionOn.

def List.Vector.revInductionOn₂ E1 #

{α : Type u_1} →
  {β : Type u_2} →
    {C : {n : ℕ} → List.Vector α n → List.Vector β n → Sort u_5} →
      {n : ℕ} →
        (v : List.Vector α n) →
          (w : List.Vector β n) →
            C List.Vector.nil List.Vector.nil →
              ({n : ℕ} →
                  (xs : List.Vector α n) →
                    (ys : List.Vector β n) → (x : α) → (y : β) → C xs ys → C (xs.snoc x) (ys.snoc y)) →
                C v w

Define C v w by reverse induction on a pair of vectors v : Vector α n and w : Vector β n.

def List.Vector.revCasesOn E1 #

{α : Type u_1} →
  {C : {n : ℕ} → List.Vector α n → Sort u_5} →
    {n : ℕ} →
      (v : List.Vector α n) → C List.Vector.nil → ({n : ℕ} → (xs : List.Vector α n) → (x : α) → C (xs.snoc x)) → C v

Define C v by reverse case analysis, i.e. by handling the cases nil and xs.snoc x separately

theorem List.Vector.map_snoc E0 #

∀ {α : Type u_1} {β : Type u_2} {n : ℕ} {x : α} (xs : List.Vector α n) {f : α → β},
  List.Vector.map f (xs.snoc x) = (List.Vector.map f xs).snoc (f x)

theorem List.Vector.mapAccumr_nil E0 #

∀ {α : Type u_1} {β : Type u_2} {σ : Type u_3} {f : α → σ → σ × β} {s : σ},
  List.Vector.mapAccumr f List.Vector.nil s = (s, List.Vector.nil)

theorem List.Vector.mapAccumr_snoc E0 #

∀ {α : Type u_1} {β : Type u_2} {σ : Type u_3} {n : ℕ} {x : α} (xs : List.Vector α n) {f : α → σ → σ × β} {s : σ},
  List.Vector.mapAccumr f (xs.snoc x) s =
    have q := f x s;
    have r := List.Vector.mapAccumr f xs q.1;
    (r.1, r.2.snoc q.2)

theorem List.Vector.map₂_snoc E0 #

∀ {α : Type u_1} {β : Type u_2} {σ : Type u_3} {n : ℕ} {x : α} (xs : List.Vector α n) (ys : List.Vector β n)
  {f : α → β → σ} {y : β}, List.Vector.map₂ f (xs.snoc x) (ys.snoc y) = (List.Vector.map₂ f xs ys).snoc (f x y)

theorem List.Vector.mapAccumr₂_nil E0 #

∀ {α : Type u_1} {β : Type u_2} {σ : Type u_3} {φ : Type u_4} {s : σ} {f : α → β → σ → σ × φ},
  List.Vector.mapAccumr₂ f List.Vector.nil List.Vector.nil s = (s, List.Vector.nil)

theorem List.Vector.mapAccumr₂_snoc E0 #

∀ {α : Type u_1} {β : Type u_2} {σ : Type u_3} {φ : Type u_4} {n : ℕ} {s : σ} (xs : List.Vector α n)
  (ys : List.Vector β n) (f : α → β → σ → σ × φ) (x : α) (y : β),
  List.Vector.mapAccumr₂ f (xs.snoc x) (ys.snoc y) s =
    have q := f x y s;
    have r := List.Vector.mapAccumr₂ f xs ys q.1;
    (r.1, r.2.snoc q.2)