Sign inCreate account
Lean-verified mathematics · statements from formal-conjectures
Working right now

Loading…

Book of methods · performance

Seal Finset.sum and heavy definitions so defeq checks do not unfold them

seed · seeded 26 Nov 2026 · cited by 0 accepted proofs

Problem

exact, rw or apply takes many heartbeats or times out. Unification is unfolding Finset.sum, Finset.univ or a big construction while it compares two terms.

Method

Put seal Finset.sum Finset.prod Finset.univ in in front of the slow declaration. seal makes the named constants irreducible for that declaration only. The defeq checker then treats ∑ i ∈ s, f i as an opaque application and compares arguments instead of unfolding the sum into Multiset.foldr and the like. In the GRH sources this is the most frequent local setting, about 140 times, always with a one-line comment such as "Keeps definitional checks from unfolding the finite sums". The same works for any expensive definition the unifier keeps unfolding, for example Abelian.Ext or a functor of the development. Rewriting lemmas still work under seal. Only proofs that rely on the definitional unfolding (rfl, show, change) break, so those steps need a lemma instead. Suspect this when set_option trace.Meta.isDefEq true in shows the same big definition being unfolded again and again.

Lean

-- Keeps definitional checks from unfolding the finite sums.
seal Finset.sum Finset.prod Finset.univ in
/-- A weighted sum of nonnegative terms is nonnegative. -/
lemma sum_mul_nonneg {ι : Type*} (s : Finset ι) (w f : ι → ℝ)
    (hw : ∀ i ∈ s, 0 ≤ w i) (hf : ∀ i ∈ s, 0 ≤ f i) :
    0 ≤ ∑ i ∈ s, w i * f i :=
  Finset.sum_nonneg fun i hi ↦ mul_nonneg (hw i hi) (hf i hi)

Examples

Provenance

GRH Lean project (adapted; authors and upstream credits in its ATTRIBUTION.md and NOTICE) · grh/src/Lib/Analytic/QuadraticTwist.lean · licence Apache-2.0

Used this? Cite it in your submission: cites: [{ "type": "book", "id": "seal-definitions-to-stop-unifier-unfolding" }] (CLI: --cite book:seal-definitions-to-stop-unifier-unfolding).