Skip to content
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

Open
wants to merge 25 commits into
base: master
Choose a base branch
from

Conversation

davikrehalt
Copy link
Collaborator

@davikrehalt davikrehalt commented Jan 10, 2024

This PR includes several useful lemmas to the Data/Finset and Data/List modules in Lean's mathlib:

  1. toFinset_filter (List): Shows that filtering commutes with toFinset.
  2. toFinset_is_singleton_implies_replicate (List): Shows a list with a singleton toFinset is a List.replicate.
  3. filter_sort_commute (Finset): Sorting and filtering can be interchanged in Finsets.
  4. Sorted.filter (List): A sorted list stays sorted after filtering.
  5. Sorted.append_largest (List): Appending the largest element keeps a list sorted.
  6. sort_monotone_map (Finset): Relates sorting of a Finset after mapping to sorting of a list.
  7. sort_insert_largest (Finset): Sorting a Finset with an inserted largest element.
  8. sort_range (Finset): Sorting a range in a Finset equals the corresponding range list.
  9. filter_eval_true (List): Filtering a list with a predicate always true keeps the list unchanged.
  10. filter_eval_false (List): Filtering with an always-false predicate gives an empty list.
  11. pairwise_concat (List): Iff condition for Pairwise relation in concatenated lists (similar to pairwise_join).
  12. list_map_toFinset (Finset): toFinset commutes with map under injection

This 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.

remove space

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@urkud
Copy link
Member

urkud commented Jan 22, 2024

Please remove extra indentation inside the list in the PR description and use backticks for identifiers.

rw [toFinset_filter f]

theorem toFinset_is_singleton_implies_replicate {l : List α} {a : α}
(h : l.toFinset ⊆ {a}) : l = List.replicate l.length a := by
Copy link
Member

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.

Copy link
Member

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?)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this lemma

@@ -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
Copy link
Member

@urkud urkud Jan 22, 2024

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:

Please add the first fact and use it to get your lemma. Also, this lemma should be protected.

Copy link
Collaborator

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
Copy link
Member

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?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please apply this

@urkud urkud added awaiting-author A reviewer has asked the author a question or requested changes and removed awaiting-review labels Jan 22, 2024
@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Feb 12, 2024
@grunweg grunweg added the new-contributor This PR was made by a contributor with at most 5 merged PRs. Welcome to the community! label Mar 25, 2024
Copy link

github-actions bot commented Jun 30, 2024

PR summary 9f72d47cba

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference

Declarations diff

+ Sorted.append_largest
+ Sorted.filter
+ filter_sort_commute
+ list_map_toFinset
+ sort_insert_largest
+ sort_monotone_map
+ sort_range
+ toFinset_is_singleton_implies_replicate

You can run this locally as follows
## summary with just the declaration names:
./scripts/declarations_diff.sh <optional_commit>

## more verbose report:
./scripts/declarations_diff.sh long <optional_commit>

The doc-module for script/declarations_diff.sh contains some details about this script.

@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot removed the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Jun 30, 2024
Comment on lines 3000 to 3001
theorem filter_eval_false (l : List α)
(h : ∀ a ∈ l, p a = false) : List.filter p l = []:= by
Copy link
Member

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
Copy link
Member

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?)

@YaelDillies YaelDillies self-assigned this Jul 23, 2024
@jcommelin
Copy link
Member

@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 please-adopt.)

@@ -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 α) :
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 β]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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']
Copy link
Collaborator

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?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please answer this

@davikrehalt
Copy link
Collaborator Author

@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 please-adopt.)

Sorry! I got busy since January--will label accordingly because I don't trust myself to be on top of this. again sorry!

@davikrehalt davikrehalt added the please-adopt Inactive PR (would be valuable to adopt) label Aug 6, 2024
@grunweg grunweg added the t-data Data (lists, quotients, numbers, etc) label Aug 15, 2024
@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot added blocked-by-other-PR This PR depends on another PR to Mathlib (this label is automatically managed by a bot) and removed blocked-by-other-PR This PR depends on another PR to Mathlib (this label is automatically managed by a bot) labels Aug 19, 2024
@leanprover-community-mathlib4-bot
Copy link
Collaborator

This PR/issue depends on:

@eric-wieser
Copy link
Member

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.

@davikrehalt
Copy link
Collaborator Author

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!

Copy link
Collaborator

@YaelDillies YaelDillies left a 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
Copy link
Collaborator

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 α) :
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
theorem list_map_toFinset [DecidableEq α] [DecidableEq β] (l : List α) :
theorem map_listToFinset [DecidableEq α] [DecidableEq β] (l : List α) :

Comment on lines +72 to +73
theorem filter_sort_commute [DecidableEq α] (f : α → Prop) [DecidablePred f] (s : Finset α) :
sort r (filter (f ·) s) = List.filter f (sort r s) := by
Copy link
Collaborator

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 filters here

Suggested change
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']
Copy link
Collaborator

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please apply this

@leanprover-community-bot-assistant leanprover-community-bot-assistant added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Oct 4, 2024
rw [List.perm_cons]
apply (sort_perm_toList _ _).symm

theorem sort_range (k : ℕ) : sort (· ≤ ·) (range k) = List.range k := by
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-author A reviewer has asked the author a question or requested changes merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) new-contributor This PR was made by a contributor with at most 5 merged PRs. Welcome to the community! please-adopt Inactive PR (would be valuable to adopt) t-data Data (lists, quotients, numbers, etc)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants