Skip to content

[Merged by Bors] - feat(Data/Set/Card): cardinality of complement and difference - #43424

Closed
SnirBroshi wants to merge 10 commits into
leanprover-community:masterfrom
SnirBroshi:feature/data/set-ncard-lemmas
Closed

SnirBroshi wants to merge 10 commits into
leanprover-community:masterfrom
SnirBroshi:feature/data/set-ncard-lemmas

Conversation

@SnirBroshi

@SnirBroshi SnirBroshi commented Sep 4, 2026 •

Copy link
Copy Markdown
Collaborator
  • ncard_compl doesn't need sᶜ.Finite
  • added sᶜ.encard lemma
  • added [e]ncard (t \ s) lemmas that don't require s ⊆ t
  • tag them simp

and generalized nearby similar things.

ncard_sdiff_singleton_of_mem and pred_ncard_le_ncard_sdiff_singleton were moved down to be able to use the ncard diff lemma.

Open in Gitpod

@github-actions

github-actions Bot commented Sep 4, 2026 •

Copy link
Copy Markdown

PR summary eb8ac302cd

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference

Declarations diff (regex)

+ Infinite.to_type
+ encard_compl
+ encard_sdiff'
+ even_ncard_compl_iff'
+ ncard_pair_eq_two_iff
+ ncard_sdiff''
+ odd_ncard_compl_iff'
++ addLECancellable_iff_ne_top

You can run this locally as follows
## from your `mathlib4` directory:
git clone https://github.com/leanprover-community/mathlib-ci.git ../mathlib-ci

## summary with just the declaration names:
../mathlib-ci/scripts/pr_summary/declarations_diff.sh <optional_commit>

## more verbose report:
../mathlib-ci/scripts/pr_summary/declarations_diff.sh long <optional_commit>

The doc-module for scripts/pr_summary/declarations_diff.sh in the mathlib-ci repository contains some details about this script.

Declarations diff (Lean)

✅ Lean-aware diff — post-build, computed from the Lean environment (commit eb8ac30).

  • +9 new declarations
  • −0 removed declarations
+ENNReal.addLECancellable_iff_ne_top
+ENat.addLECancellable_iff_ne_top
+Set.Infinite.to_type
+Set.encard_compl
+Set.encard_sdiff'
+Set.even_ncard_compl_iff'
+Set.ncard_pair_eq_two_iff
+Set.ncard_sdiff''
+Set.odd_ncard_compl_iff'

No changes to strong technical debt.
No changes to weak technical debt.

Current commit eb8ac302cd
Reference commit 2a885768da

This script lives in the mathlib-ci repository. To run it locally, from your mathlib4 directory:

git clone https://github.com/leanprover-community/mathlib-ci.git ../mathlib-ci
../mathlib-ci/scripts/reporting/technical-debt-metrics.py pr_summary
  • The relative value is the weighted sum of the differences with weight given by the inverse of the current value of the statistic.
  • The absolute value is the relative value divided by the total sum of the inverses of the current values (i.e. the weighted average of the differences).

@github-actions github-actions Bot added the t-data Data (lists, quotients, numbers, etc) label Sep 4, 2026
@SnirBroshi

Copy link
Copy Markdown
Collaborator Author

!radar

@leanprover-radar

leanprover-radar commented Sep 4, 2026 •

Copy link
Copy Markdown

Benchmark results for 3d2a658 against 2308bf9 are in. No significant results found. @SnirBroshi

  • 🟥 build//instructions: +17.7G (+0.01%)

Small changes (2🟥)

  • 🟥 build/module/Mathlib.Analysis.Complex.ValueDistribution.Proximity.IntegralPresentation//instructions: +340.7M (+1.60%)
  • 🟥 build/module/Mathlib.Data.Set.Card//instructions: +1.1G (+2.18%)

@b-mehta b-mehta left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

oops I started reviewing and got distracted - I think this looks broadly nice!

Comment thread Mathlib/Data/Set/Card.lean Resolved
@b-mehta b-mehta added the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label Sep 9, 2026
@SnirBroshi SnirBroshi removed the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label Sep 17, 2026

@Ruben-VandeVelde Ruben-VandeVelde left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maintainer delegate

Comment thread Mathlib/Data/ENat/Basic.lean Resolved
Comment thread Mathlib/Data/Set/Card.lean Resolved
@github-actions

Copy link
Copy Markdown

🚀 Pull request has been placed on the maintainer queue by Ruben-VandeVelde.

@mathlib-triage mathlib-triage Bot added the maintainer-merge A reviewer has approved the changed; awaiting maintainer approval. label Sep 22, 2026

@j-loreaux j-loreaux left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If you think the lemmas would succeed reasonably often with simps default discharger, then you can keep them.

bors d+

alias ncard_inter_add_ncard_diff_eq_ncard := ncard_inter_add_ncard_sdiff_eq_ncard

/-- `ncard` of a difference with no subset assumption -/
@[simp]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Unfortunately, this is mostly useless as a simp lemma, unless you expect (t ∩ s).Finite to be provable by whatever discharger the user provides to simp. The reason is that, unless the user supplies s and t first, the autoparam for by toFinite_tac doesn't fire, which means the user has to write the lemma manually. Essentially, the unification that causes the lemma to be applied happens too late (in the SimpM monad) to call the tactic (which would require TacticM). This is leanprover/lean4#3475

Suggested change
@[simp]

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This successfully fires in the simp call on line 1126 (right below this theorem), and also in something like

example {α : Type*} (x : α) : (Set.univ \ {x}).ncard = Nat.card α - 1 := by simp
example {α : Type*} {x y : α} (h : x ≠ y) : (Set.univ \ {x, y}).ncard = Nat.card α - 2 := by simp [h]

(which was the motivation to add this)
I think that in these cases the finiteness assumption is handled by simp, so it doesn't need to run the autoparam.

When the autoparam has to be used simp does seem to fail, but rw works:

example {α : Type*} [Finite α] (s t : Set α) : (s \ t).ncard ≤ s.ncard := by
  rw [Set.ncard_sdiff'']
  simp

This is unlike what the issue says, although the mwe there still fails. Not sure what causes the difference.

So these seem to succeed when at least one of the sets involved is given explicitly (with Insert notation), which I expect are the kinds of goals people try simp on.
Although the Lean issue is unfortunate here.

(h : Nat.card α = n + s.ncard) :
sᶜ.ncard = n := by
rwa [← ncard_compl_add_ncard s, Nat.add_right_cancel_iff] at h
@[simp]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
@[simp]

@mathlib-bors mathlib-bors Bot added the delegated This pull request has been delegated to the PR author (or occasionally another non-maintainer). label Sep 29, 2026
@mathlib-bors

mathlib-bors Bot commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

✌️ SnirBroshi can now approve this pull request until 2026-10-13 23:29 UTC (in 2 weeks). To approve and merge, reply with bors r+. More detailed instructions are available here.

⚠️ This delegation only covers changes within Archive/**, Counterexamples/**, docs/**, DownstreamTest/**, Mathlib/**, MathlibTest/**, Wanted/**, widget/**, Archive.lean, Counterexamples.lean, docs.lean, Mathlib.lean, Wanted.lean; an author commit touching anything else will revoke it. Bors also revokes it if a later push changes too many files for it to check the full list — even if it stays within scope.

@mathlib-triage mathlib-triage Bot removed the maintainer-merge A reviewer has approved the changed; awaiting maintainer approval. label Sep 29, 2026
@SnirBroshi

Copy link
Copy Markdown
Collaborator Author

Thanks!

bors merge

@mathlib-bors mathlib-bors Bot added the ready-to-merge This PR has been sent to bors. label Oct 2, 2026
mathlib-bors Bot pushed a commit that referenced this pull request Oct 2, 2026
- `ncard_compl` doesn't need `sᶜ.Finite`
- added `sᶜ.encard` lemma
- added `[e]ncard (t \ s)` lemmas that don't require `s ⊆ t`
- tag them `simp`
@mathlib-bors mathlib-bors Bot added the bors-staging This PR is currently being built by bors on the staging branch. label Oct 2, 2026
@mathlib-bors

mathlib-bors Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

@mathlib-bors mathlib-bors Bot changed the title feat(Data/Set/Card): cardinality of complement and difference [Merged by Bors] - feat(Data/Set/Card): cardinality of complement and difference Oct 2, 2026
@mathlib-bors mathlib-bors Bot closed this Oct 2, 2026
@mathlib-bors mathlib-bors Bot removed the delegated This pull request has been delegated to the PR author (or occasionally another non-maintainer). label Oct 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bors-staging This PR is currently being built by bors on the staging branch. ready-to-merge This PR has been sent to bors. t-data Data (lists, quotients, numbers, etc)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants