|
| 1 | +/- |
| 2 | +Copyright (c) 2026 Lean FRO, LLC. All rights reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +Authors: Kim Morrison, Sebastian Graf, Paul Reichert |
| 5 | +-/ |
| 6 | +module |
| 7 | + |
| 8 | +prelude |
| 9 | +public import Init.Data.Int.DivMod.Bootstrap |
| 10 | +import Init.Data.Int.DivMod.Lemmas |
| 11 | +import Init.Data.List.MinMax |
| 12 | + |
| 13 | +public section |
| 14 | + |
| 15 | +set_option linter.listVariables true -- Enforce naming conventions for `List`/`Array`/`Vector` variables. |
| 16 | +set_option linter.indexVariables true -- Enforce naming conventions for index variables. |
| 17 | + |
| 18 | +namespace List |
| 19 | + |
| 20 | +@[simp] |
| 21 | +theorem sum_replicate_int {n : Nat} {a : Int} : (replicate n a).sum = n * a := by |
| 22 | + induction n <;> simp_all [replicate_succ, Int.add_mul, Int.add_comm] |
| 23 | + |
| 24 | +theorem sum_append_int {l₁ l₂ : List Int} : (l₁ ++ l₂).sum = l₁.sum + l₂.sum := by |
| 25 | + simp [sum_append] |
| 26 | + |
| 27 | +theorem sum_reverse_int (xs : List Int) : xs.reverse.sum = xs.sum := by |
| 28 | + simp [sum_reverse] |
| 29 | + |
| 30 | +theorem min_mul_length_le_sum_int {xs : List Int} (h : xs ≠ []) : |
| 31 | + xs.min h * xs.length ≤ xs.sum := by |
| 32 | + induction xs |
| 33 | + · contradiction |
| 34 | + · rename_i x xs ih |
| 35 | + cases xs |
| 36 | + · simp_all [List.min_singleton] |
| 37 | + · simp only [ne_eq, reduceCtorEq, not_false_eq_true, min_eq_get_min?, |
| 38 | + List.min?_cons (α := Int), Option.get_some, length_cons, Int.natCast_add, Int.cast_ofNat_Int, |
| 39 | + forall_const] at ih ⊢ |
| 40 | + rw [Int.mul_add, Int.mul_one, Int.add_comm] |
| 41 | + apply Int.add_le_add |
| 42 | + · apply Int.min_le_left |
| 43 | + · refine Int.le_trans ?_ ih |
| 44 | + rw [Int.mul_le_mul_right (by omega)] |
| 45 | + apply Int.min_le_right |
| 46 | + |
| 47 | +theorem mul_length_le_sum_of_min?_eq_some_int {xs : List Int} (h : xs.min? = some x) : |
| 48 | + x * xs.length ≤ xs.sum := by |
| 49 | + cases xs |
| 50 | + · simp_all |
| 51 | + · simp only [min?_eq_some_min (cons_ne_nil _ _), Option.some.injEq] at h |
| 52 | + simpa [← h] using min_mul_length_le_sum_int _ |
| 53 | + |
| 54 | +theorem min_le_sum_div_length_int {xs : List Int} (h : xs ≠ []) : |
| 55 | + xs.min h ≤ xs.sum / xs.length := by |
| 56 | + have := min_mul_length_le_sum_int h |
| 57 | + rwa [Int.le_ediv_iff_mul_le] |
| 58 | + simp [List.length_pos_iff, h] |
| 59 | + |
| 60 | +theorem le_sum_div_length_of_min?_eq_some_int {xs : List Int} (h : xs.min? = some x) : |
| 61 | + x ≤ xs.sum / xs.length := by |
| 62 | + cases xs |
| 63 | + · simp_all |
| 64 | + · simp only [min?_eq_some_min (cons_ne_nil _ _), Option.some.injEq] at h |
| 65 | + simpa [← h] using min_le_sum_div_length_int _ |
| 66 | + |
| 67 | +theorem sum_le_max_mul_length_int {xs : List Int} (h : xs ≠ []) : |
| 68 | + xs.sum ≤ xs.max h * xs.length := by |
| 69 | + induction xs |
| 70 | + · contradiction |
| 71 | + · rename_i x xs ih |
| 72 | + cases xs |
| 73 | + · simp_all [List.max_singleton] |
| 74 | + · simp only [ne_eq, reduceCtorEq, not_false_eq_true, max_eq_get_max?, |
| 75 | + List.max?_cons (α := Int), Option.get_some, length_cons, Int.natCast_add, Int.cast_ofNat_Int, |
| 76 | + forall_const] at ih ⊢ |
| 77 | + rw [Int.mul_add, Int.mul_one, Int.add_comm] |
| 78 | + apply Int.add_le_add |
| 79 | + · apply Int.le_max_left |
| 80 | + · refine Int.le_trans ih ?_ |
| 81 | + rw [Int.mul_le_mul_right (by omega)] |
| 82 | + apply Int.le_max_right |
| 83 | + |
| 84 | +theorem sum_le_max_mul_length_of_max?_eq_some_int {xs : List Int} (h : xs.max? = some x) : |
| 85 | + xs.sum ≤ x * xs.length := by |
| 86 | + cases xs |
| 87 | + · simp_all |
| 88 | + · simp only [max?_eq_some_max (cons_ne_nil _ _), Option.some.injEq] at h |
| 89 | + simpa [← h] using sum_le_max_mul_length_int _ |
| 90 | + |
| 91 | +theorem sum_div_length_le_max_int {xs : List Int} (h : xs ≠ []) : |
| 92 | + xs.sum / xs.length ≤ xs.max h := by |
| 93 | + have := sum_le_max_mul_length_int h |
| 94 | + rw [Int.ediv_le_iff_le_mul] |
| 95 | + · refine Int.lt_of_le_of_lt this ?_ |
| 96 | + apply Int.lt_add_of_pos_right |
| 97 | + simp [← Nat.ne_zero_iff_zero_lt, h] |
| 98 | + · simp [List.length_pos_iff, h] |
| 99 | + |
| 100 | +theorem sum_div_length_le_max_of_max?_eq_some_int {xs : List Int} (h : xs.max? = some x) : |
| 101 | + xs.sum / xs.length ≤ x := by |
| 102 | + cases xs |
| 103 | + · simp_all |
| 104 | + · simp only [max?_eq_some_max (cons_ne_nil _ _), Option.some.injEq] at h |
| 105 | + simpa [← h] using sum_div_length_le_max_int _ |
| 106 | + |
| 107 | +end List |
0 commit comments