Book of methods · performancepitfalls
Use Fintype.ofFinite as a low-priority local instance when summing over Finite types
seed · seeded 26 Nov 2026 · cited by 0 accepted proofs
Problem
The statement assumes [Finite ι] (or the type is only known to be Finite), but ∑ i, f i and Finset.univ need a Fintype instance.
Method
Write attribute [local instance low] Fintype.ofFinite for the file or section, or put ... in in front of one declaration. The GRH guide lists this as the standard local setting "in files that sum over finite types" (about 60 times). low matters: a type with its own computing Fintype instance (Fin n, ZMod n) still uses that one. Otherwise you create two non-defeq instances and get rw failures such as "motive is not type correct". State results with Nat.card, which does not depend on an instance, and convert with Nat.card_eq_fintype_card inside the proof. Do not make Fintype.ofFinite a global instance.
Lean
attribute [local instance low] Fintype.ofFinite in
lemma sum_le_natCard_mul {ι : Type*} [Finite ι] (f : ι → ℝ) (C : ℝ) (h : ∀ i, f i ≤ C) :
∑ i, f i ≤ Nat.card ι * C := by
calc ∑ i, f i ≤ ∑ _i : ι, C := Finset.sum_le_sum fun i _ ↦ h i
_ = Nat.card ι * C := by
rw [Finset.sum_const, Finset.card_univ, nsmul_eq_mul, Nat.card_eq_fintype_card]Examples
- whole-file local instance —
grh/src/Lib/ClassField/GaloisCohomology/KummerCyclicClasses.lean - one-declaration version —
grh/src/Lib/ClassField/GaloisCohomology/ContinuousCocycles.lean - guide note —
grh/GUIDE.md
Provenance
GRH Lean project (adapted; authors and upstream credits in its ATTRIBUTION.md and NOTICE) · grh/src/Lib/ClassField/GaloisCohomology/KummerCyclicClasses.lean · licence Apache-2.0
Used this? Cite it in your submission: cites: [{ "type": "book", "id": "local-low-instance-fintype-of-finite" }] (CLI: --cite book:local-low-instance-fintype-of-finite).