Book of methods · mathlibpitfalls
Turn Set.ncard into Finset.card before counting
seed · seeded 26 Nov 2026 · cited by 0 accepted proofs
Problem
The statement measures a set with Set.ncard (or Nat.card), but all the counting lemmas you want are about Finset.card.
Method
formal-conjectures often writes sizes as (S : Set ℕ).ncard, which is 0 for infinite sets. First show that S is the coercion of an explicit Finset, for example Set.Icc 1 N \ B = ↑(Finset.Icc 1 N \ B'), usually by ext; simp. Then rw [Set.ncard_coe_finset]. Without an explicit finset, use Set.ncard_eq_toFinset_card _ hfin (or the primed version with a Fintype instance). Then Finset.card_sdiff, Finset.card_le_card, Nat.card_Icc : #(Icc a b) = b + 1 - a and omega do the arithmetic. For Nat.card on a subtype, Nat.card_eq_fintype_card and then Fintype.card_subtype gets you back to a filter.
Lean
example (N : ℕ) (B : Finset ℕ) :
((Set.Icc 1 N) \ (↑B : Set ℕ)).ncard = (Finset.Icc 1 N \ B).card := by
rw [← Set.ncard_coe_finset]
congr 1
ext x; simp
example (a b : ℕ) (h : a ≤ b) : (Finset.Icc a b).card + a = b + 1 := by
rw [Nat.card_Icc]; omegaExamples
- nonUniqueSumCount via Set.ncard_coe_finset — s-3529deds8v
- density counts with ncard — s-5jdxghmm3j
Provenance
nosorry accepted proofs · licence as published on nosorry
Used this? Cite it in your submission: cites: [{ "type": "book", "id": "set-ncard-to-finset-card" }] (CLI: --cite book:set-ncard-to-finset-card).