Skip to content

Commit

Permalink
feat: add some List.takeWhile lemmas (#7397)
Browse files Browse the repository at this point in the history
Co-authored-by: Scott Morrison <scott@tqft.net>
  • Loading branch information
Ruben-VandeVelde and semorrison committed Sep 29, 2023
1 parent 2cb209a commit 071f47d
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions Mathlib/Data/List/Basic.lean
Expand Up @@ -3542,20 +3542,36 @@ theorem dropWhile_eq_nil_iff : dropWhile p l = [] ↔ ∀ x ∈ l, p x := by
· by_cases hp : p x <;> simp [hp, dropWhile, IH]
#align list.drop_while_eq_nil_iff List.dropWhile_eq_nil_iff

@[simp] theorem takeWhile_nil : List.takeWhile p [] = [] := rfl

theorem takeWhile_cons {x : α} :
List.takeWhile p (x :: l) = (match p x with
| true => x :: takeWhile p l
| false => []) :=
rfl

theorem takeWhile_cons_of_pos {x : α} (h : p x) :
List.takeWhile p (x :: l) = x :: takeWhile p l := by
simp [takeWhile_cons, h]

theorem takeWhile_cons_of_neg {x : α} (h : ¬ p x) :
List.takeWhile p (x :: l) = [] := by
simp [takeWhile_cons, h]

@[simp]
theorem takeWhile_eq_self_iff : takeWhile p l = l ↔ ∀ x ∈ l, p x := by
induction' l with x xs IH
· simp [takeWhile]
· by_cases hp : p x <;> simp [hp, takeWhile, IH]
· simp
· by_cases hp : p x <;> simp [hp, takeWhile_cons, IH]
#align list.take_while_eq_self_iff List.takeWhile_eq_self_iff

@[simp]
theorem takeWhile_eq_nil_iff : takeWhile p l = [] ↔ ∀ hl : 0 < l.length, ¬p (l.nthLe 0 hl) := by
induction' l with x xs IH
· simp [takeWhile, true_iff]
· simp only [takeWhile_nil, Bool.not_eq_true, true_iff]
intro h
simp at h
· by_cases hp : p x <;> simp [hp, takeWhile, IH, nthLe_cons]
· by_cases hp : p x <;> simp [hp, takeWhile_cons, IH, nthLe_cons]
#align list.take_while_eq_nil_iff List.takeWhile_eq_nil_iff

theorem mem_takeWhile_imp {x : α} (hx : x ∈ takeWhile p l) : p x := by
Expand All @@ -3572,7 +3588,7 @@ theorem mem_takeWhile_imp {x : α} (hx : x ∈ takeWhile p l) : p x := by
theorem takeWhile_takeWhile (p q : α → Bool) (l : List α) :
takeWhile p (takeWhile q l) = takeWhile (fun a => p a ∧ q a) l := by
induction' l with hd tl IH
· simp [takeWhile]
· simp
· by_cases hp : p hd <;> by_cases hq : q hd <;> simp [takeWhile, hp, hq, IH]
#align list.take_while_take_while List.takeWhile_takeWhile

Expand Down

0 comments on commit 071f47d

Please sign in to comment.