Module docstring
{"# Lemmas about List.min? and `List.max?.
","## Minima and maxima ","### min? ","### max? "}
{"# Lemmas about List.min? and `List.max?.
","## Minima and maxima ","### min? ","### max? "}
List.min?_nil
theorem
[Min α] : ([] : List α).min? = none
List.min?_cons'
theorem
[Min α] {xs : List α} : (x :: xs).min? = foldl min x xs
List.min?_cons
theorem
[Min α] [Std.Associative (min : α → α → α)] {xs : List α} : (x :: xs).min? = some (xs.min?.elim x (min x))
@[simp] theorem min?_cons [Min α] [Std.Associative (min : α → α → α)] {xs : List α} :
(x :: xs).min? = some (xs.min?.elim x (min x)) := by
cases xs <;> simp [min?_cons', foldl_assoc]
List.min?_eq_none_iff
theorem
{xs : List α} [Min α] : xs.min? = none ↔ xs = []
@[simp] theorem min?_eq_none_iff {xs : List α} [Min α] : xs.min? = none ↔ xs = [] := by
cases xs <;> simp [min?]
List.isSome_min?_of_mem
theorem
{l : List α} [Min α] {a : α} (h : a ∈ l) : l.min?.isSome
theorem isSome_min?_of_mem {l : List α} [Min α] {a : α} (h : a ∈ l) :
l.min?.isSome := by
cases l <;> simp_all [min?_cons']
List.min?_eq_head?
theorem
{α : Type u} [Min α] {l : List α} (h : l.Pairwise (fun a b => min a b = a)) : l.min? = l.head?
theorem min?_eq_head? {α : Type u} [Min α] {l : List α}
(h : l.Pairwise (fun a b => min a b = a)) : l.min? = l.head? := by
cases l with
| nil => rfl
| cons x l =>
rw [head?_cons, min?_cons', Option.some.injEq]
induction l generalizing x with
| nil => simp
| cons y l ih =>
have hx : min x y = x := rel_of_pairwise_cons h mem_cons_self
rw [foldl_cons, ih _ (hx.symm ▸ h.sublist (by simp)), hx]
List.min?_mem
theorem
[Min α] (min_eq_or : ∀ a b : α, min a b = a ∨ min a b = b) : {xs : List α} → xs.min? = some a → a ∈ xs
theorem min?_mem [Min α] (min_eq_or : ∀ a b : α, minmin a b = a ∨ min a b = b) :
{xs : List α} → xs.min? = some a → a ∈ xs := by
intro xs
match xs with
| nil => simp
| x :: xs =>
simp only [min?_cons', Option.some.injEq, mem_cons]
intro eq
induction xs generalizing x with
| nil =>
simp at eq
simp [eq]
| cons y xs ind =>
simp at eq
have p := ind _ eq
cases p with
| inl p =>
cases min_eq_or x y with | _ q => simp [p, q]
| inr p => simp [p, mem_cons]
List.le_min?_iff
theorem
[Min α] [LE α] (le_min_iff : ∀ a b c : α, a ≤ min b c ↔ a ≤ b ∧ a ≤ c) :
{xs : List α} → xs.min? = some a → ∀ {x}, x ≤ a ↔ ∀ b, b ∈ xs → x ≤ b
theorem le_min?_iff [Min α] [LE α]
(le_min_iff : ∀ a b c : α, a ≤ min b c ↔ a ≤ b ∧ a ≤ c) :
{xs : List α} → xs.min? = some a → ∀ {x}, x ≤ a ↔ ∀ b, b ∈ xs → x ≤ b
| nil => by simp
| cons x xs => by
rw [min?]
intro eq y
simp only [Option.some.injEq] at eq
induction xs generalizing x with
| nil =>
simp at eq
simp [eq]
| cons z xs ih =>
simp at eq
simp [ih _ eq, le_min_iff, and_assoc]
List.min?_replicate
theorem
[Min α] {n : Nat} {a : α} (w : min a a = a) : (replicate n a).min? = if n = 0 then none else some a
theorem min?_replicate [Min α] {n : Nat} {a : α} (w : min a a = a) :
(replicate n a).min? = if n = 0 then none else some a := by
induction n with
| zero => rfl
| succ n ih => cases n <;> simp_all [replicate_succ, min?_cons']
List.min?_replicate_of_pos
theorem
[Min α] {n : Nat} {a : α} (w : min a a = a) (h : 0 < n) : (replicate n a).min? = some a
@[simp] theorem min?_replicate_of_pos [Min α] {n : Nat} {a : α} (w : min a a = a) (h : 0 < n) :
(replicate n a).min? = some a := by
simp [min?_replicate, Nat.ne_of_gt h, w]
List.foldl_min
theorem
[Min α] [Std.IdempotentOp (min : α → α → α)] [Std.Associative (min : α → α → α)] {l : List α} {a : α} :
l.foldl (init := a) min = min a (l.min?.getD a)
theorem foldl_min [Min α] [Std.IdempotentOp (min : α → α → α)] [Std.Associative (min : α → α → α)]
{l : List α} {a : α} : l.foldl (init := a) min = min a (l.min?.getD a) := by
cases l <;> simp [min?, foldl_assoc, Std.IdempotentOp.idempotent]
List.max?_nil
theorem
[Max α] : ([] : List α).max? = none
List.max?_cons'
theorem
[Max α] {xs : List α} : (x :: xs).max? = foldl max x xs
List.max?_cons
theorem
[Max α] [Std.Associative (max : α → α → α)] {xs : List α} : (x :: xs).max? = some (xs.max?.elim x (max x))
@[simp] theorem max?_cons [Max α] [Std.Associative (max : α → α → α)] {xs : List α} :
(x :: xs).max? = some (xs.max?.elim x (max x)) := by
cases xs <;> simp [max?_cons', foldl_assoc]
List.max?_eq_none_iff
theorem
{xs : List α} [Max α] : xs.max? = none ↔ xs = []
@[simp] theorem max?_eq_none_iff {xs : List α} [Max α] : xs.max? = none ↔ xs = [] := by
cases xs <;> simp [max?]
List.isSome_max?_of_mem
theorem
{l : List α} [Max α] {a : α} (h : a ∈ l) : l.max?.isSome
theorem isSome_max?_of_mem {l : List α} [Max α] {a : α} (h : a ∈ l) :
l.max?.isSome := by
cases l <;> simp_all [max?_cons']
List.max?_mem
theorem
[Max α] (min_eq_or : ∀ a b : α, max a b = a ∨ max a b = b) : {xs : List α} → xs.max? = some a → a ∈ xs
theorem max?_mem [Max α] (min_eq_or : ∀ a b : α, maxmax a b = a ∨ max a b = b) :
{xs : List α} → xs.max? = some a → a ∈ xs
| nil => by simp
| cons x xs => by
rw [max?]; rintro ⟨⟩
induction xs generalizing x with simp at *
| cons y xs ih =>
rcases ih (max x y) with h | h <;> simp [h]
simp [← or_assoc, min_eq_or x y]
List.max?_le_iff
theorem
[Max α] [LE α] (max_le_iff : ∀ a b c : α, max b c ≤ a ↔ b ≤ a ∧ c ≤ a) :
{xs : List α} → xs.max? = some a → ∀ {x}, a ≤ x ↔ ∀ b ∈ xs, b ≤ x
theorem max?_le_iff [Max α] [LE α]
(max_le_iff : ∀ a b c : α, maxmax b c ≤ a ↔ b ≤ a ∧ c ≤ a) :
{xs : List α} → xs.max? = some a → ∀ {x}, a ≤ x ↔ ∀ b ∈ xs, b ≤ x
| nil => by simp
| cons x xs => by
rw [max?]; rintro ⟨⟩ y
induction xs generalizing x with
| nil => simp
| cons y xs ih => simp [ih, max_le_iff, and_assoc]
List.max?_eq_some_iff
theorem
[Max α] [LE α] [anti : Std.Antisymm ((· : α) ≤ ·)] (le_refl : ∀ a : α, a ≤ a)
(max_eq_or : ∀ a b : α, max a b = a ∨ max a b = b) (max_le_iff : ∀ a b c : α, max b c ≤ a ↔ b ≤ a ∧ c ≤ a)
{xs : List α} : xs.max? = some a ↔ a ∈ xs ∧ ∀ b ∈ xs, b ≤ a
theorem max?_eq_some_iff [Max α] [LE α] [anti : Std.Antisymm ((· : α) ≤ ·)]
(le_refl : ∀ a : α, a ≤ a)
(max_eq_or : ∀ a b : α, maxmax a b = a ∨ max a b = b)
(max_le_iff : ∀ a b c : α, maxmax b c ≤ a ↔ b ≤ a ∧ c ≤ a) {xs : List α} :
xs.max? = some a ↔ a ∈ xs ∧ ∀ b ∈ xs, b ≤ a := by
refine ⟨fun h => ⟨max?_mem max_eq_or h, (max?_le_iff max_le_iff h).1 (le_refl _)⟩, ?_⟩
intro ⟨h₁, h₂⟩
cases xs with
| nil => simp at h₁
| cons x xs =>
exact congrArg some <| anti.1 _ _
(h₂ _ (max?_mem max_eq_or (xs := x::xs) rfl))
((max?_le_iff max_le_iff (xs := x::xs) rfl).1 (le_refl _) _ h₁)
List.max?_replicate
theorem
[Max α] {n : Nat} {a : α} (w : max a a = a) : (replicate n a).max? = if n = 0 then none else some a
theorem max?_replicate [Max α] {n : Nat} {a : α} (w : max a a = a) :
(replicate n a).max? = if n = 0 then none else some a := by
induction n with
| zero => rfl
| succ n ih => cases n <;> simp_all [replicate_succ, max?_cons']
List.max?_replicate_of_pos
theorem
[Max α] {n : Nat} {a : α} (w : max a a = a) (h : 0 < n) : (replicate n a).max? = some a
@[simp] theorem max?_replicate_of_pos [Max α] {n : Nat} {a : α} (w : max a a = a) (h : 0 < n) :
(replicate n a).max? = some a := by
simp [max?_replicate, Nat.ne_of_gt h, w]
List.foldl_max
theorem
[Max α] [Std.IdempotentOp (max : α → α → α)] [Std.Associative (max : α → α → α)] {l : List α} {a : α} :
l.foldl (init := a) max = max a (l.max?.getD a)
theorem foldl_max [Max α] [Std.IdempotentOp (max : α → α → α)] [Std.Associative (max : α → α → α)]
{l : List α} {a : α} : l.foldl (init := a) max = max a (l.max?.getD a) := by
cases l <;> simp [max?, foldl_assoc, Std.IdempotentOp.idempotent]
List.minimum?_nil
abbrev
@[deprecated min?_nil (since := "2024-09-29")] abbrev minimum?_nil := @min?_nil
List.minimum?_cons
abbrev
@[deprecated min?_cons (since := "2024-09-29")] abbrev minimum?_cons := @min?_cons
List.mininmum?_eq_none_iff
abbrev
@[deprecated min?_eq_none_iff (since := "2024-09-29")] abbrev mininmum?_eq_none_iff := @min?_eq_none_iff
List.minimum?_mem
abbrev
@[deprecated min?_mem (since := "2024-09-29")] abbrev minimum?_mem := @min?_mem
List.le_minimum?_iff
abbrev
@[deprecated le_min?_iff (since := "2024-09-29")] abbrev le_minimum?_iff := @le_min?_iff
List.minimum?_eq_some_iff
abbrev
@[deprecated min?_eq_some_iff (since := "2024-09-29")] abbrev minimum?_eq_some_iff := @min?_eq_some_iff
List.minimum?_replicate
abbrev
@[deprecated min?_replicate (since := "2024-09-29")] abbrev minimum?_replicate := @min?_replicate
List.minimum?_replicate_of_pos
abbrev
@[deprecated min?_replicate_of_pos (since := "2024-09-29")] abbrev minimum?_replicate_of_pos := @min?_replicate_of_pos
List.maximum?_nil
abbrev
@[deprecated max?_nil (since := "2024-09-29")] abbrev maximum?_nil := @max?_nil
List.maximum?_cons
abbrev
@[deprecated max?_cons (since := "2024-09-29")] abbrev maximum?_cons := @max?_cons
List.maximum?_eq_none_iff
abbrev
@[deprecated max?_eq_none_iff (since := "2024-09-29")] abbrev maximum?_eq_none_iff := @max?_eq_none_iff
List.maximum?_mem
abbrev
@[deprecated max?_mem (since := "2024-09-29")] abbrev maximum?_mem := @max?_mem
List.maximum?_le_iff
abbrev
@[deprecated max?_le_iff (since := "2024-09-29")] abbrev maximum?_le_iff := @max?_le_iff
List.maximum?_eq_some_iff
abbrev
@[deprecated max?_eq_some_iff (since := "2024-09-29")] abbrev maximum?_eq_some_iff := @max?_eq_some_iff
List.maximum?_replicate
abbrev
@[deprecated max?_replicate (since := "2024-09-29")] abbrev maximum?_replicate := @max?_replicate
List.maximum?_replicate_of_pos
abbrev
@[deprecated max?_replicate_of_pos (since := "2024-09-29")] abbrev maximum?_replicate_of_pos := @max?_replicate_of_pos