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(multiset/basic): add lemma exists_minimal_subset #5783

Closed
wants to merge 6 commits into from

Conversation

faenuccio
Copy link
Collaborator

Add lemma can_assume_minimal showing that if a property p holds for some multiset, that it is possible to find a
multiset which has minimal cardinality satisfying it.


@faenuccio faenuccio added the awaiting-review The author would like community review of the PR label Jan 17, 2021
@adomani
Copy link
Collaborator

adomani commented Jan 17, 2021

In line with the thread https://leanprover.zulipchat.com/#narrow/stream/217875-Is-there.20code.20for.20X.3F/topic/can.20assume.20n.20is.20minimal, would you mind minimizing over all nonempty subsets? For instance, if your property is "having odd cardinality", I would probably expect a singleton, rather than a subset of odd cardinality.

Thanks!

@kmill
Copy link
Collaborator

kmill commented Jan 17, 2021

@adomani If you want a nonempty subset, couldn't you use the statement as-is but then make sure to add an s.nonempty clause into the predicate p? Otherwise you'd be excluding applications where the empty subset might be the minimal subset.

src/data/multiset/basic.lean Outdated Show resolved Hide resolved
Co-authored-by: Kyle Miller <kmill31415@gmail.com>
@faenuccio
Copy link
Collaborator Author

@kmill @adomani Well, indeed, as I tried to implement adomani's request, I realized that the best option seems (to me) to add two lemmas, one for the case where the empty set is allowed and one when it is not. It is on progress, hence I have changed the label to WIP

@faenuccio faenuccio added WIP Work in progress and removed awaiting-review The author would like community review of the PR labels Jan 17, 2021
@adomani
Copy link
Collaborator

adomani commented Jan 17, 2021

You are right: I had in mind of minimizing over all proper subsets, not the nonempty ones! Thus, the statement would return the empty set, if it satisfies p, since the empty set has no proper subsets. Sorry about my mistake!

we can assume that this multiset has minimal cardinality -/

lemma can_assume_min [decidable_eq α] (p : multiset α → Prop) [decidable_pred p]
(s₀ : multiset α) (H : p s₀) : ∃ s ≤ s₀, p s ∧ (∀ a ∈ s, ¬ p (s.erase a)) :=
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this statement is not as strong as you'd like it to be. What if p s is that s.card is odd? Then s₀ would satisfy the conclusion since you can't remove a single element to make the multiset smaller while having it satisfy p.

Here's a stronger statement that you could use to prove yours:

lemma exists_minimal_subset [decidable_eq α] (p : multiset α → Prop) [decidable_pred p]
  (s₀ : multiset α) (H : p s₀) : ∃ s ≤ s₀, p s ∧ (∀ s' ≤ s, p s' → s' = s) :=
begin
  refine multiset.strong_induction_on s₀ (λ s₀ ih H, _) H,
  by_cases h : ∃ s < s₀, p s,
  { rcases h with ⟨s₁, hs₁, ps₁⟩,
    specialize ih s₁ hs₁ ps₁,
    rcases ih with ⟨s, hsle, hs⟩,
    use ⟨s, le_of_lt (lt_of_le_of_lt hsle hs₁), hs⟩, },
  { push_neg at h,
    use [s₀, rfl.ge, H],
    intros s' hs' ps',
    cases lt_or_eq_of_le hs' with hs'' hs'',
    { exact (h _ hs'' ps').elim, },
    { exact hs'', }, },
end

Copy link
Collaborator

Choose a reason for hiding this comment

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

lemma exists_minimal_subset' [decidable_eq α] (p : multiset α → Prop) [decidable_pred p]
  (s₀ : multiset α) (H : p s₀) : ∃ s ≤ s₀, p s ∧ (∀ a ∈ s, ¬p (s.erase a)) :=
begin
  rcases exists_minimal_subset p s₀ H with ⟨s, hsle, ps, hs⟩,
  use [s, hsle, ps],
  intros a ha perase,
  have : s.erase a < s := erase_lt.mpr ha,
  rw hs (s.erase a) (erase_le a s) perase at this,
  exact lt_irrefl _ this,
end

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Very good point! I'll study your suggestions, but just to understand better: have you commited any change, or are you proposing that I incorporate both exists_minimal_subset and exists_minimal_subset' in a new commit? Also, as far as I understand, the current version of my lemma can_assume_min would become obsolete, is that right?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I haven't committed anything -- I'm just giving these to you, and you can use them however you like.

It seems to me that exists_minimal_subset is the more useful one, and if you need a statement like exists_minimal_subset', you should instead have a lemma that if you have a minimal subset for which a predicate is true, then it's not true for any erasure:

/- not sure what to call this -/
lemma not_erase_of_minimal [decidable_eq α] (s : multiset α) (p : multiset α → Prop)
  (h : ∀ s' ≤ s, p s' → s' = s) (a : α) (ha : a ∈ s) : ¬p (s.erase a) :=
begin
  intro perase,
  have : s.erase a < s  := erase_lt.mpr ha,
  rw h _ (erase_le a s) perase at this,
  exact lt_irrefl _ this,
end

(Note: for exists_minimal_subset it turns out you can remove all the decidability constraints.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok, thanks so much. I'll work on this and turn the label back to awaiting-review once done and commited.

Copy link
Collaborator

@adomani adomani Jan 18, 2021

Choose a reason for hiding this comment

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

In case you do not mind assuming classical facts, below is a short proof of exists_minimal_subset.

-- I could not find this in mathlib, but it seems like it could be there...
lemma nat.well_ordered_of_exists {s : set ℕ} [decidable_pred (λ w, w ∈ s)] (ex : ∃ w : ℕ, w ∈ s) :
  ∃ n ∈ s, ∀ m ∈ s, n ≤ m :=
⟨nat.find ex, nat.find_spec ex, λ m, nat.find_min' ex⟩

lemma exists_minimal_subset {p : multiset α → Prop} {s₀ : multiset α} (H : p s₀)
  [decidable_pred (λ w, w ∈ {i | ∃ u, u ≤ s₀ ∧ p u ∧ u.card = i})] :
  ∃ s ≤ s₀, p s ∧ (∀ s' ≤ s, p s' → s' = s) :=
begin
  obtain ⟨n, ⟨sm, sm0, ps, sc⟩, F⟩ := nat.well_ordered_of_exists
    (⟨_, _, le_rfl, H, rfl⟩ : ∃ w : ℕ, w ∈ { i | ∃ u, u ≤ s₀ ∧ p u ∧ u.card = i }),
  exact ⟨sm, sm0, ps, λ t tsm pt, eq_of_le_of_card_le tsm
    (by {rw sc, exact F _ ⟨t, le_trans tsm sm0, pt, rfl⟩})⟩,
end

Note that, if you do not want to get into the decidable stuff, you can write open_locale classical at the beginning of your file and all these decidable issues go away.

If I understand this correctly, the proof above uses classical, since it finds a subset of minimum cardinality by going through subsets of . Kyle's proof stays on multiset: I think that multisets already have all the decidable stuff inbuilt. For me, this is the reason why working with multisets and finsets is hard, without classical.

@bryangingechen bryangingechen changed the title feat(multiset/basic.lean) : add lemma can_assume_minimal feat(multiset/basic): add lemma can_assume_minimal Jan 17, 2021
@faenuccio faenuccio removed the request for review from digama0 January 17, 2021 20:34
@faenuccio faenuccio changed the title feat(multiset/basic): add lemma can_assume_minimal feat(multiset/basic): add lemma exists_minimal_subset Feb 4, 2021
@faenuccio faenuccio added awaiting-review The author would like community review of the PR and removed WIP Work in progress labels Feb 4, 2021
@bryangingechen bryangingechen added awaiting-author A reviewer has asked the author a question or requested changes and removed awaiting-review The author would like community review of the PR labels Feb 25, 2021
@bryangingechen
Copy link
Collaborator

@faenuccio I put this to "awaiting-author" for now since there's still a sorry left, but is this still waiting for comments from @adomani ?

@faenuccio faenuccio marked this pull request as draft February 25, 2021 14:37
@faenuccio
Copy link
Collaborator Author

I have even converted this to a draft since I will not be able to work on this for some time. I realise I do not know how to bring it back to life as a normal PR, but this is not urgent.

@bryangingechen
Copy link
Collaborator

bryangingechen commented Feb 25, 2021

@faenuccio Thanks for the update! If I recall correctly, there's a link to change draft PRs back to normal PRs at the top right, near where the list of assigned reviewers is.

@faenuccio faenuccio added the WIP Work in progress label Mar 1, 2021
@github-actions github-actions bot added the merge-conflict Please `git merge origin/master` then a bot will remove this label. label Mar 10, 2021
@faenuccio faenuccio closed this Jun 13, 2021
@faenuccio faenuccio deleted the assume_n_min branch June 13, 2021 15:57
Vierkantor added a commit that referenced this pull request Jul 14, 2021
This is vaguely related to #5783, in that it tries to solve a similar goal of
finding a minimal multiset with some property.
bors bot pushed a commit that referenced this pull request Jul 15, 2021
This is vaguely related to #5783, in that it tries to solve a similar goal of finding a minimal multiset with some property.
b-mehta pushed a commit that referenced this pull request Jul 20, 2021
This is vaguely related to #5783, in that it tries to solve a similar goal of finding a minimal multiset with some property.
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 Please `git merge origin/master` then a bot will remove this label. WIP Work in progress
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants