Documentation

CslibFrontier.Foundations.Data.List.FilterPos

Filtered list positions and order-preserving index helpers #

Generic list-indexing helpers for positions of elements matching a Boolean predicate pred:

Main definitions #

Main results #

def List.nthFilterPos {α : Type u_1} (pred : α → Bool) :
List α → ℕ → Option ℕ

Position in l of the n-th element satisfying pred (none if at most n elements in l satisfy pred).

@[simp]
theorem List.nthFilterPos_nil {α : Type u_1} (pred : α → Bool) (n : ℕ) :
theorem List.nthFilterPos_cons_of_false {α : Type u_1} {pred : α → Bool} {a : α} (rest : List α) (n : ℕ) (h : pred a = false) :
nthFilterPos pred (a :: rest) n = Option.map (fun (x : ℕ) => x + 1) (nthFilterPos pred rest n)

Skipping a head element with pred a = false shifts nthFilterPos by 1 for every n.

theorem List.nthFilterPos_cons_zero_of_true {α : Type u_1} {pred : α → Bool} {a : α} (rest : List α) (h : pred a = true) :
nthFilterPos pred (a :: rest) 0 = some 0

Matching head element at rank 0 occurs at index 0.

theorem List.nthFilterPos_cons_succ_of_true {α : Type u_1} {pred : α → Bool} {a : α} (rest : List α) (n : ℕ) (h : pred a = true) :
nthFilterPos pred (a :: rest) (n + 1) = Option.map (fun (x : ℕ) => x + 1) (nthFilterPos pred rest n)

Matching head element at rank n + 1 shifts the n-th match in rest by 1.

theorem List.nthFilterPos_isSome_iff {α : Type u_1} (pred : α → Bool) (l : List α) (n : ℕ) :
(nthFilterPos pred l n).isSome = true ↔ n < (filter pred l).length

nthFilterPos pred l n is some iff l.filter pred has length strictly greater than n.

theorem List.nthFilterPos_lt_length {α : Type u_1} {pred : α → Bool} {l : List α} {n pos : ℕ} (h : nthFilterPos pred l n = some pos) :
pos < l.length ∧ l[pos]? = (filter pred l)[n]? ∧ Option.map pred l[pos]? = some true

If nthFilterPos pred l n = some pos, then pos < l.length, l[pos]? = (l.filter pred)[n]?, and pred holds at l[pos]?.

theorem List.nthFilterPos_strictMono {α : Type u_1} {pred : α → Bool} {l : List α} {m n i j : ℕ} (hmn : m < n) (hm : nthFilterPos pred l m = some i) (hn : nthFilterPos pred l n = some j) :
i < j

nthFilterPos is strictly monotone in the filtered rank m < n.

theorem List.nthFilterPos_inj {α : Type u_1} {pred : α → Bool} {l : List α} {u v w : ℕ} (hu : nthFilterPos pred l u = some w) (hv : nthFilterPos pred l v = some w) :
u = v

nthFilterPos is injective in the filtered rank n.

theorem List.nthFilterPos_append_left {α : Type u_1} {pred : α → Bool} {l₁ : List α} (l₂ : List α) {n : ℕ} (hn : n < (filter pred l₁).length) :
nthFilterPos pred (l₁ ++ l₂) n = nthFilterPos pred l₁ n

Ranks inside l₁.filter pred have unchanged positions in l₁ ++ l₂.

theorem List.nthFilterPos_append_right {α : Type u_1} {pred : α → Bool} (l₁ l₂ : List α) {n : ℕ} (hn : (filter pred l₁).length ≤ n) :
nthFilterPos pred (l₁ ++ l₂) n = Option.map (fun (x : ℕ) => x + l₁.length) (nthFilterPos pred l₂ (n - (filter pred l₁).length))

Ranks at or beyond (l₁.filter pred).length shift into l₂ by l₁.length.

theorem List.nthFilterPos_of_append_lt {α : Type u_1} {pred : α → Bool} {l₁ l₂ : List α} {n pos : ℕ} (h : nthFilterPos pred (l₁ ++ l₂) n = some pos) (hlt : pos < l₁.length) :
nthFilterPos pred l₁ n = some pos

Any match of nthFilterPos pred (l₁ ++ l₂) landing strictly before l₁.length comes from l₁.

theorem List.filter_getElem_count_take {α : Type u_1} (pred : α → Bool) (cs : List α) (k : ℕ) (hk : k < cs.length) :
pred cs[k] = true → ∃ (hlt : (filter pred (take k cs)).length < (filter pred cs).length), (filter pred cs)[(filter pred (take k cs)).length] = cs[k]

If pred cs[k] = true, then cs[k] sits in cs.filter pred at index ((cs.take k).filter pred).length.

theorem List.map_eq_of_strictMono_surj {β : Type u_1} {γ : Type u_2} {src : List β} {dst : List γ} (pos : β → ℕ) (val : β → γ) (hvalid : ∀ b ∈ src, ∃ (hlt : pos b < dst.length), val b = dst[pos b]) (hmono : ∀ (i j : ℕ) (hi : i < src.length) (hj : j < src.length), i < j → pos src[i] < pos src[j]) (hsurj : ∀ k < dst.length, ∃ b ∈ src, pos b = k) :
map pos src = range dst.length ∧ map val src = dst

If pos : β → ℕ is strictly monotone on src, bounded below dst.length, and surjective onto 0 ..< dst.length with val b = dst[pos b], then src.map pos = List.range dst.length and src.map val = dst.

theorem List.filter_strictMono_indices {α : Type u_1} (l : List α) (pred : α → Bool) :
∃ (idx : Fin (filter pred l).length → Fin l.length), (∀ (i : Fin (filter pred l).length), (filter pred l)[↑i] = l[↑(idx i)]) ∧ ∀ (i j : Fin (filter pred l).length), ↑i < ↑j → ↑(idx i) < ↑(idx j)

Every filtered sublist l.filter pred embeds into l via a strictly monotone index map.