-
Notifications
You must be signed in to change notification settings - Fork 381
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
feat(Data/Finset & List): Add Lemmas for Sorting and Filtering #9605
base: master
Are you sure you want to change the base?
Conversation
…re idiomatically, probably should be rewritten somehow)
…lists, not sure if it's necessary
…Sort which addresses how sorted list changes upon adding an element larger than all
remove space Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…o list_lemma_davik merge to current state
…thlib4 into list_lemma_davik update to current state
…o list_lemma_davik current state
Please remove extra indentation inside the list in the PR description and use |
rw [toFinset_filter f] | ||
|
||
theorem toFinset_is_singleton_implies_replicate {l : List α} {a : α} | ||
(h : l.toFinset ⊆ {a}) : l = List.replicate l.length a := by |
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.
This lemma follows immediately from List.eq_replicate_of_mem.
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.
(and therefore should be removed?)
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.
Please remove this lemma
Mathlib/Data/List/Sort.lean
Outdated
@@ -157,6 +157,27 @@ theorem Sorted.rel_of_mem_take_of_mem_drop {l : List α} (h : List.Sorted r l) { | |||
exact h.rel_nthLe_of_lt _ _ (Nat.lt_add_right _ (lt_min_iff.mp hix).left) | |||
#align list.sorted.rel_of_mem_take_of_mem_drop List.Sorted.rel_of_mem_take_of_mem_drop | |||
|
|||
theorem Sorted.filter {l : List α} (f : α → Bool) (h : Sorted r l) : | |||
Sorted r (filter f l) := by |
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.
This follows from more general facts:
- (
not yet indefeq to List.Pairwise.sublist) a sublist of a sorted list is sorted;Mathlib
filter f l
is a sublist ofl
, see List.filter_sublist.
Please add the first fact and use it to get your lemma. Also, this lemma should be protected
.
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 moved this to #18466
exact Sorted.filter f (Sorted.of_cons h) | ||
|
||
theorem Sorted.append_largest {a : α} {l : List α} : | ||
Sorted r (l ++ [a]) ↔ (∀ b ∈ l, r b a) ∧ Sorted r l := by |
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.
We have List.concat in Lean core. Should it be protected theorem Sorted.concat
?
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.
Please apply this
PR summary 9f72d47cbaImport changes for modified filesNo significant changes to the import graph Import changes for all files
|
Mathlib/Data/List/Basic.lean
Outdated
theorem filter_eval_false (l : List α) | ||
(h : ∀ a ∈ l, p a = false) : List.filter p l = []:= by |
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.
This is subsumed by List.filter_eq_nil
rw [toFinset_filter f] | ||
|
||
theorem toFinset_is_singleton_implies_replicate {l : List α} {a : α} | ||
(h : l.toFinset ⊆ {a}) : l = List.replicate l.length a := by |
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.
(and therefore should be removed?)
@davikrehalt Are you planning to continue working on this PR? Would you like some help from others, or do you want to hand over completely? (In the latter case, please label it with |
Mathlib/Data/Finset/Sort.lean
Outdated
@@ -81,6 +81,76 @@ theorem sort_perm_toList (s : Finset α) : sort r s ~ s.toList := by | |||
simp only [coe_toList, sort_eq] | |||
#align finset.sort_perm_to_list Finset.sort_perm_toList | |||
|
|||
theorem filter_sort_commute [DecidableEq α](f : α → Prop) [DecidablePred f] (s : Finset α) : |
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.
theorem filter_sort_commute [DecidableEq α](f : α → Prop) [DecidablePred f] (s : Finset α) : | |
theorem filter_sort_comm [DecidableEq α](f : α → Prop) [DecidablePred f] (s : Finset α) : |
rw [List.toFinset_filter] | ||
simp | ||
|
||
theorem sort_monotone_map [DecidableEq α] [DecidableEq β] |
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.
theorem sort_monotone_map [DecidableEq α] [DecidableEq β] | |
theorem sort_map_of_monotone [DecidableEq α] [DecidableEq β] |
simp | ||
|
||
theorem sort_monotone_map [DecidableEq α] [DecidableEq β] | ||
(r' : β → β → Prop) [DecidableRel r'] [IsTrans β r'] [IsAntisymm β r'] [IsTotal β r'] |
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.
Why are you taking an unbundled relation here?
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.
Please answer this
Sorry! I got busy since January--will label accordingly because I don't trust myself to be on top of this. again sorry! |
This PR/issue depends on: |
I pushed a bunch of proof cleanups, and deleted the three lemmas that were exact duplicates of ones already in Mathlib. I haven't attempted to tidy the statements of the remaining lemmas. |
Hi I have some time to work on polishing this now. But since it's been a while it's unclear to me what the remaining issues are. If someone could help tell me this I can try to make it suitable to be merged. Thanks! |
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.
Here is a list of everything you should fix. You should also look at CI and perform the changes it's asking you to do.
rw [toFinset_filter f] | ||
|
||
theorem toFinset_is_singleton_implies_replicate {l : List α} {a : α} | ||
(h : l.toFinset ⊆ {a}) : l = List.replicate l.length a := by |
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.
Please remove this lemma
@@ -277,6 +277,12 @@ theorem disjiUnion_map {s : Finset α} {t : α → Finset β} {f : β ↪ γ} {h | |||
s.disjiUnion (fun a => (t a).map f) (h.mono' fun _ _ ↦ (disjoint_map _).2) := | |||
eq_of_veq <| Multiset.map_bind _ _ _ | |||
|
|||
theorem list_map_toFinset [DecidableEq α] [DecidableEq β] (l : List α) : |
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.
theorem list_map_toFinset [DecidableEq α] [DecidableEq β] (l : List α) : | |
theorem map_listToFinset [DecidableEq α] [DecidableEq β] (l : List α) : |
theorem filter_sort_commute [DecidableEq α] (f : α → Prop) [DecidablePred f] (s : Finset α) : | ||
sort r (filter (f ·) s) = List.filter f (sort r s) := by |
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.
It's not really a commutativity lemma as it talks about two different filter
s here
theorem filter_sort_commute [DecidableEq α] (f : α → Prop) [DecidablePred f] (s : Finset α) : | |
sort r (filter (f ·) s) = List.filter f (sort r s) := by | |
theorem sort_filter [DecidableEq α] (f : α → Prop) [DecidablePred f] (s : Finset α) : | |
sort r (filter (f ·) s) = List.filter f (sort r s) := by |
simp | ||
|
||
theorem sort_monotone_map [DecidableEq α] [DecidableEq β] | ||
(r' : β → β → Prop) [DecidableRel r'] [IsTrans β r'] [IsAntisymm β r'] [IsTotal β r'] |
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.
Please answer this
exact Sorted.filter f (Sorted.of_cons h) | ||
|
||
theorem Sorted.append_largest {a : α} {l : List α} : | ||
Sorted r (l ++ [a]) ↔ (∀ b ∈ l, r b a) ∧ Sorted r l := by |
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.
Please apply this
rw [List.perm_cons] | ||
apply (sort_perm_toList _ _).symm | ||
|
||
theorem sort_range (k : ℕ) : sort (· ≤ ·) (range k) = List.range k := by |
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 reproved this theorem in #18465
This PR includes several useful lemmas to the Data/Finset and Data/List modules in Lean's mathlib:
toFinset_filter
(List): Shows that filtering commutes with toFinset.toFinset_is_singleton_implies_replicate
(List): Shows a list with a singleton toFinset is a List.replicate.filter_sort_commute
(Finset): Sorting and filtering can be interchanged in Finsets.Sorted.filter
(List): A sorted list stays sorted after filtering.Sorted.append_largest
(List): Appending the largest element keeps a list sorted.sort_monotone_map
(Finset): Relates sorting of a Finset after mapping to sorting of a list.sort_insert_largest
(Finset): Sorting a Finset with an inserted largest element.sort_range
(Finset): Sorting a range in a Finset equals the corresponding range list.filter_eval_true
(List): Filtering a list with a predicate always true keeps the list unchanged.filter_eval_false
(List): Filtering with an always-false predicate gives an empty list.pairwise_concat
(List): Iff condition for Pairwise relation in concatenated lists (similar to pairwise_join).list_map_toFinset
(Finset): toFinset commutes with map under injectionList.map
and sorting lists #15952This is my first PR to mathlib, so I'm not too familiar with etiquette's and more specifically there are two proofs in the PR which uses aesop, and I am not sure if it's frowned upon. I kept the aesop in there for now as I couldn't construct very short proofs otherwise. The sort_range function proof was suggested in https://leanprover.zulipchat.com/#narrow/stream/113489-new-members/topic/Computing.20Finset.20sort.20of.20Finset.20range/near/410346731 by Ruben Van de Velde. Thanks for your time for improving this PR.