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.List.DropRight

39 declarations. Mathlib docs · Source

Dropping or taking from lists on the right

Taking or removing element from the tail end of a list

Main definitions

  • rdrop n: drop n : ℕ elements from the tail
  • rtake n: take n : ℕ elements from the tail
  • rdropWhile p: remove all the elements from the tail of a list until it finds the first element for which p : α → Bool returns false. This element and everything before is returned.
  • rtakeWhile p: Returns the longest terminal segment of a list for which p : α → Bool returns true.

Implementation detail

The two predicate-based methods operate by performing the regular "from-left" operation on List.reverse, followed by another List.reverse, so they are not the most performant. The other two rely on List.length l so they still traverse the list twice. One could construct another function that takes a L : ℕ and use L - n. Under a proof condition that L = l.length, the function would do the right thing.

def List.rdrop E1 #

{α : Type u_1} → List α → ℕ → List α

Drop n elements from the tail end of a list.

theorem List.rdrop_nil E0 #

∀ {α : Type u_1} (n : ℕ), [].rdrop n = []

theorem List.rdrop_zero E0 #

∀ {α : Type u_1} (l : List α), l.rdrop 0 = l

theorem List.rdrop_eq_reverse_drop_reverse E1 #

∀ {α : Type u_1} (l : List α) (n : ℕ), l.rdrop n = (List.drop n l.reverse).reverse

theorem List.rdrop_concat_succ E0 #

∀ {α : Type u_1} (l : List α) (n : ℕ) (x : α), (l ++ [x]).rdrop (n + 1) = l.rdrop n

def List.rtake E1 #

{α : Type u_1} → List α → ℕ → List α

Take n elements from the tail end of a list.

theorem List.rtake_nil E0 #

∀ {α : Type u_1} (n : ℕ), [].rtake n = []

theorem List.rtake_zero E0 #

∀ {α : Type u_1} (l : List α), l.rtake 0 = []

theorem List.rtake_eq_reverse_take_reverse E1 #

∀ {α : Type u_1} (l : List α) (n : ℕ), l.rtake n = (List.take n l.reverse).reverse

theorem List.rtake_concat_succ E0 #

∀ {α : Type u_1} (l : List α) (n : ℕ) (x : α), (l ++ [x]).rtake (n + 1) = l.rtake n ++ [x]

def List.rdropWhile E1 #

{α : Type u_1} → (α → Bool) → List α → List α

Drop elements from the tail end of a list that satisfy p : α → Bool. Implemented naively via List.reverse

theorem List.rdropWhile_nil E0 #

∀ {α : Type u_1} (p : α → Bool), List.rdropWhile p [] = []

theorem List.rdropWhile_concat E1 #

∀ {α : Type u_1} (p : α → Bool) (l : List α) (x : α),
  List.rdropWhile p (l ++ [x]) = if p x = true then List.rdropWhile p l else l ++ [x]

theorem List.rdropWhile_concat_pos E0 #

∀ {α : Type u_1} (p : α → Bool) (l : List α) (x : α), p x = true → List.rdropWhile p (l ++ [x]) = List.rdropWhile p l

theorem List.rdropWhile_concat_neg E0 #

∀ {α : Type u_1} (p : α → Bool) (l : List α) (x : α), ¬p x = true → List.rdropWhile p (l ++ [x]) = l ++ [x]

theorem List.rdropWhile_singleton E1 #

∀ {α : Type u_1} (p : α → Bool) (x : α), List.rdropWhile p [x] = if p x = true then [] else [x]

theorem List.rdropWhile_last_not E1 #

∀ {α : Type u_1} (p : α → Bool) (l : List α) (hl : List.rdropWhile p l ≠ []),
  ¬p ((List.rdropWhile p l).getLast hl) = true

theorem List.rdropWhile_prefix E1 #

∀ {α : Type u_1} (p : α → Bool) (l : List α), List.rdropWhile p l <+: l

theorem List.rdropWhile_eq_nil_iff E0 #

∀ {α : Type u_1} {p : α → Bool} {l : List α}, List.rdropWhile p l = [] ↔ ∀ x ∈ l, p x = true

theorem List.rdropWhile_eq_self_iff E0 #

∀ {α : Type u_1} {p : α → Bool} {l : List α}, List.rdropWhile p l = l ↔ ∀ (hl : l ≠ []), ¬p (l.getLast hl) = true

theorem List.dropWhile_idempotent E1 #

∀ {α : Type u_1} (p : α → Bool) (l : List α), List.dropWhile p (List.dropWhile p l) = List.dropWhile p l

theorem List.rdropWhile_idempotent E1 #

∀ {α : Type u_1} (p : α → Bool) (l : List α), List.rdropWhile p (List.rdropWhile p l) = List.rdropWhile p l

theorem List.rdropWhile_reverse E1 #

∀ {α : Type u_1} (p : α → Bool) (l : List α), List.rdropWhile p l.reverse = (List.dropWhile p l).reverse

def List.rtakeWhile E1 #

{α : Type u_1} → (α → Bool) → List α → List α

Take elements from the tail end of a list that satisfy p : α → Bool. Implemented naively via List.reverse

theorem List.rtakeWhile_nil E0 #

∀ {α : Type u_1} (p : α → Bool), List.rtakeWhile p [] = []

theorem List.rtakeWhile_concat E1 #

∀ {α : Type u_1} (p : α → Bool) (l : List α) (x : α),
  List.rtakeWhile p (l ++ [x]) = if p x = true then List.rtakeWhile p l ++ [x] else []

theorem List.rtakeWhile_concat_pos E0 #

∀ {α : Type u_1} (p : α → Bool) (l : List α) (x : α),
  p x = true → List.rtakeWhile p (l ++ [x]) = List.rtakeWhile p l ++ [x]

theorem List.rtakeWhile_concat_neg E0 #

∀ {α : Type u_1} (p : α → Bool) (l : List α) (x : α), ¬p x = true → List.rtakeWhile p (l ++ [x]) = []

theorem List.rtakeWhile_suffix E1 #

∀ {α : Type u_1} (p : α → Bool) (l : List α), List.rtakeWhile p l <:+ l

theorem List.rtakeWhile_eq_self_iff E0 #

∀ {α : Type u_1} {p : α → Bool} {l : List α}, List.rtakeWhile p l = l ↔ ∀ x ∈ l, p x = true

theorem List.rtakeWhile_eq_nil_iff E0 #

∀ {α : Type u_1} {p : α → Bool} {l : List α}, List.rtakeWhile p l = [] ↔ ∀ (hl : l ≠ []), ¬p (l.getLast hl) = true

theorem List.mem_rtakeWhile_imp E0 #

∀ {α : Type u_1} {p : α → Bool} {l : List α} {x : α}, x ∈ List.rtakeWhile p l → p x = true

theorem List.rtakeWhile_idempotent E1 #

∀ {α : Type u_1} (p : α → Bool) (l : List α), List.rtakeWhile p (List.rtakeWhile p l) = List.rtakeWhile p l

theorem List.rtakeWhile_reverse E1 #

∀ {α : Type u_1} {p : α → Bool} {l : List α}, List.rtakeWhile p l.reverse = (List.takeWhile p l).reverse

theorem List.rdropWhile_append_rtakeWhile E0 #

∀ {α : Type u_1} {p : α → Bool} {l : List α}, List.rdropWhile p l ++ List.rtakeWhile p l = l

theorem List.rdrop_add E1 #

∀ {α : Type u_1} {l : List α} (i j : ℕ), (l.rdrop i).rdrop j = l.rdrop (i + j)

theorem List.rdrop_append_length E0 #

∀ {α : Type u_1} {l₁ l₂ : List α}, (l₁ ++ l₂).rdrop l₂.length = l₁

theorem List.rdrop_append_of_le_length E1 #

∀ {α : Type u_1} {l₁ l₂ : List α}, ∀ k ≤ l₂.length, (l₁ ++ l₂).rdrop k = l₁ ++ l₂.rdrop k

theorem List.rdrop_append_length_add E0 #

∀ {α : Type u_1} {l₁ l₂ : List α} (k : ℕ), (l₁ ++ l₂).rdrop (l₂.length + k) = l₁.rdrop k