-
Notifications
You must be signed in to change notification settings - Fork 745
feat: lemmas about sums of lists/arrays/vectors #11994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
datokrat
wants to merge
1
commit into
master
Choose a base branch
from
paul/humanevup1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+449
−34
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /- | ||
| Copyright (c) 2026 Lean FRO, LLC. All rights reserved. | ||
| Released under Apache 2.0 license as described in the file LICENSE. | ||
| Authors: Kim Morrison, Sebastian Graf, Paul Reichert | ||
| -/ | ||
| module | ||
|
|
||
| prelude | ||
| public import Init.Data.List.Int.Sum | ||
| public import Init.Data.Array.Lemmas | ||
| public import Init.Data.Int.DivMod.Bootstrap | ||
| import Init.Data.Int.DivMod.Lemmas | ||
| import Init.Data.List.MinMax | ||
|
|
||
| public section | ||
|
|
||
| set_option linter.listVariables true -- Enforce naming conventions for `List`/`Array`/`Vector` variables. | ||
| set_option linter.indexVariables true -- Enforce naming conventions for index variables. | ||
|
|
||
| namespace Array | ||
|
|
||
| @[simp] theorem sum_replicate_int {n : Nat} {a : Int} : (replicate n a).sum = n * a := by | ||
| rw [← List.toArray_replicate, List.sum_toArray] | ||
| simp | ||
|
|
||
| theorem sum_append_int {as₁ as₂ : Array Int} : (as₁ ++ as₂).sum = as₁.sum + as₂.sum := by | ||
| simp [sum_append] | ||
|
|
||
| theorem sum_reverse_int (xs : Array Int) : xs.reverse.sum = xs.sum := by | ||
| simp [sum_reverse] | ||
|
|
||
| theorem sum_eq_foldl_int {xs : Array Int} : xs.sum = xs.foldl (init := 0) (· + ·) := by | ||
| simp only [foldl_eq_foldr_reverse, Int.add_comm, ← sum_eq_foldr, sum_reverse_int] | ||
|
|
||
| end Array |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /- | ||
| Copyright (c) 2026 Lean FRO, LLC. All rights reserved. | ||
| Released under Apache 2.0 license as described in the file LICENSE. | ||
| Authors: Kim Morrison, Sebastian Graf, Paul Reichert | ||
| -/ | ||
| module | ||
|
|
||
| prelude | ||
| public import Init.Data.Array.Lemmas | ||
| import Init.Data.List.Nat.Sum | ||
|
|
||
| public section | ||
|
|
||
| set_option linter.listVariables true -- Enforce naming conventions for `List`/`Array`/`Vector` variables. | ||
| set_option linter.indexVariables true -- Enforce naming conventions for index variables. | ||
|
|
||
| namespace Array | ||
|
|
||
| protected theorem sum_pos_iff_exists_pos_nat {xs : Array Nat} : 0 < xs.sum ↔ ∃ x ∈ xs, 0 < x := by | ||
| simp [← sum_toList, List.sum_pos_iff_exists_pos_nat] | ||
|
|
||
| protected theorem sum_eq_zero_iff_forall_eq_nat {xs : Array Nat} : | ||
| xs.sum = 0 ↔ ∀ x ∈ xs, x = 0 := by | ||
| simp [← sum_toList, List.sum_eq_zero_iff_forall_eq_nat] | ||
|
|
||
| @[simp] theorem sum_replicate_nat {n : Nat} {a : Nat} : (replicate n a).sum = n * a := by | ||
| rw [← List.toArray_replicate, List.sum_toArray] | ||
| simp | ||
|
|
||
| theorem sum_append_nat {as₁ as₂ : Array Nat} : (as₁ ++ as₂).sum = as₁.sum + as₂.sum := by | ||
| simp [sum_append] | ||
|
|
||
| theorem sum_reverse_nat (xs : Array Nat) : xs.reverse.sum = xs.sum := by | ||
| simp [sum_reverse] | ||
|
|
||
| theorem sum_eq_foldl_nat {xs : Array Nat} : xs.sum = xs.foldl (init := 0) (· + ·) := by | ||
| simp only [foldl_eq_foldr_reverse, Nat.add_comm, ← sum_eq_foldr, sum_reverse_nat] | ||
|
|
||
datokrat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| end Array | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| /- | ||
| Copyright (c) 2026 Lean FRO. All rights reserved. | ||
| Released under Apache 2.0 license as described in the file LICENSE. | ||
| Authors: Paul Reichert | ||
| -/ | ||
| module | ||
|
|
||
| prelude | ||
| public import Init.Data.List.Int.Sum |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| /- | ||
| Copyright (c) 2026 Lean FRO, LLC. All rights reserved. | ||
| Released under Apache 2.0 license as described in the file LICENSE. | ||
| Authors: Kim Morrison, Sebastian Graf, Paul Reichert | ||
| -/ | ||
| module | ||
|
|
||
| prelude | ||
| public import Init.Data.Int.DivMod.Bootstrap | ||
| import Init.Data.Int.DivMod.Lemmas | ||
| import Init.Data.List.MinMax | ||
|
|
||
| public section | ||
|
|
||
| set_option linter.listVariables true -- Enforce naming conventions for `List`/`Array`/`Vector` variables. | ||
| set_option linter.indexVariables true -- Enforce naming conventions for index variables. | ||
|
|
||
| namespace List | ||
|
|
||
| @[simp] | ||
| theorem sum_replicate_int {n : Nat} {a : Int} : (replicate n a).sum = n * a := by | ||
| induction n <;> simp_all [replicate_succ, Int.add_mul, Int.add_comm] | ||
|
|
||
| theorem sum_append_int {l₁ l₂ : List Int} : (l₁ ++ l₂).sum = l₁.sum + l₂.sum := by | ||
| simp [sum_append] | ||
|
|
||
| theorem sum_reverse_int (xs : List Int) : xs.reverse.sum = xs.sum := by | ||
| simp [sum_reverse] | ||
|
|
||
| theorem min_mul_length_le_sum_int {xs : List Int} (h : xs ≠ []) : | ||
| xs.min h * xs.length ≤ xs.sum := by | ||
| induction xs | ||
| · contradiction | ||
| · rename_i x xs ih | ||
| cases xs | ||
| · simp_all [List.min_singleton] | ||
| · simp only [ne_eq, reduceCtorEq, not_false_eq_true, min_eq_get_min?, | ||
| List.min?_cons (α := Int), Option.get_some, length_cons, Int.natCast_add, Int.cast_ofNat_Int, | ||
| forall_const] at ih ⊢ | ||
| rw [Int.mul_add, Int.mul_one, Int.add_comm] | ||
| apply Int.add_le_add | ||
| · apply Int.min_le_left | ||
| · refine Int.le_trans ?_ ih | ||
| rw [Int.mul_le_mul_right (by omega)] | ||
| apply Int.min_le_right | ||
|
|
||
| theorem mul_length_le_sum_of_min?_eq_some_int {xs : List Int} (h : xs.min? = some x) : | ||
| x * xs.length ≤ xs.sum := by | ||
| cases xs | ||
| · simp_all | ||
| · simp only [min?_eq_some_min (cons_ne_nil _ _), Option.some.injEq] at h | ||
| simpa [← h] using min_mul_length_le_sum_int _ | ||
|
|
||
| theorem min_le_sum_div_length_int {xs : List Int} (h : xs ≠ []) : | ||
| xs.min h ≤ xs.sum / xs.length := by | ||
| have := min_mul_length_le_sum_int h | ||
| rwa [Int.le_ediv_iff_mul_le] | ||
| simp [List.length_pos_iff, h] | ||
|
|
||
| theorem le_sum_div_length_of_min?_eq_some_int {xs : List Int} (h : xs.min? = some x) : | ||
| x ≤ xs.sum / xs.length := by | ||
| cases xs | ||
| · simp_all | ||
| · simp only [min?_eq_some_min (cons_ne_nil _ _), Option.some.injEq] at h | ||
| simpa [← h] using min_le_sum_div_length_int _ | ||
|
|
||
| theorem sum_le_max_mul_length_int {xs : List Int} (h : xs ≠ []) : | ||
| xs.sum ≤ xs.max h * xs.length := by | ||
| induction xs | ||
| · contradiction | ||
| · rename_i x xs ih | ||
| cases xs | ||
| · simp_all [List.max_singleton] | ||
| · simp only [ne_eq, reduceCtorEq, not_false_eq_true, max_eq_get_max?, | ||
| List.max?_cons (α := Int), Option.get_some, length_cons, Int.natCast_add, Int.cast_ofNat_Int, | ||
| forall_const] at ih ⊢ | ||
| rw [Int.mul_add, Int.mul_one, Int.add_comm] | ||
| apply Int.add_le_add | ||
| · apply Int.le_max_left | ||
| · refine Int.le_trans ih ?_ | ||
| rw [Int.mul_le_mul_right (by omega)] | ||
| apply Int.le_max_right | ||
|
|
||
| theorem sum_le_max_mul_length_of_max?_eq_some_int {xs : List Int} (h : xs.max? = some x) : | ||
| xs.sum ≤ x * xs.length := by | ||
| cases xs | ||
| · simp_all | ||
| · simp only [max?_eq_some_max (cons_ne_nil _ _), Option.some.injEq] at h | ||
| simpa [← h] using sum_le_max_mul_length_int _ | ||
|
|
||
| theorem sum_div_length_le_max_int {xs : List Int} (h : xs ≠ []) : | ||
| xs.sum / xs.length ≤ xs.max h := by | ||
| have := sum_le_max_mul_length_int h | ||
| rw [Int.ediv_le_iff_le_mul] | ||
| · refine Int.lt_of_le_of_lt this ?_ | ||
| apply Int.lt_add_of_pos_right | ||
| simp [← Nat.ne_zero_iff_zero_lt, h] | ||
| · simp [List.length_pos_iff, h] | ||
|
|
||
| theorem sum_div_length_le_max_of_max?_eq_some_int {xs : List Int} (h : xs.max? = some x) : | ||
| xs.sum / xs.length ≤ x := by | ||
| cases xs | ||
| · simp_all | ||
| · simp only [max?_eq_some_max (cons_ne_nil _ _), Option.some.injEq] at h | ||
| simpa [← h] using sum_div_length_le_max_int _ | ||
|
|
||
| end List |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be worthwhile to leave a comment here explaining why you haven't labelled these as
simp, grind =, but only done this on the specializations.In fact, even better would be to benchmark this against Mathlib, with and without annotations on the generic version. I suspect it is actually fine. (Perhaps not even that much work given our new infrastructure?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've annotated
sum_appendandsum_reverseinstead now, I think the benchmark turned out neutral (as long as 0.5% onbuild//instructionsand no other significant changes is natural variability...)