Skip to content

[Merged by Bors] - refactor(Data): make Set a one-field structure - #41506

Closed
YaelDillies wants to merge 8 commits into
leanprover-community:masterfrom
YaelDillies:set_struct
Closed

YaelDillies wants to merge 8 commits into
leanprover-community:masterfrom
YaelDillies:set_struct

Conversation

@YaelDillies

@YaelDillies YaelDillies commented Jul 8, 2026 •

Copy link
Copy Markdown
Contributor

@github-actions

github-actions Bot commented Jul 8, 2026 •

Copy link
Copy Markdown

PR summary 7dd11309c0

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference

Declarations diff (regex)

+ aemeasurable_comap_iff
+ decidableOfPred
+ instance : CoeFun Class fun _ => ZFSet → Prop
+ instance : PartialOrder (MulArchimedeanClass M)
+ memEquiv
+ memOrderIso
+ mem_eq_mem
+ sdiff_mem_nhdsWithin
+ toNat_nat
- Mem
- Set.ofPred

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 7dd1130).

  • +25 new declarations
  • −2 removed declarations
+ArchimedeanClass.instPartialOrder
+Eq.eventuallyEqSet
-FreeGroup.startsWith.congr_simp
+MulArchimedeanClass.instPartialOrder
+Set.casesOn
+Set.ctorIdx
+Set.decidableOfPred
+Set.memEquiv
+Set.memEquiv_apply
+Set.memEquiv_symm_apply
+Set.memOrderIso
+Set.memOrderIso_apply
+Set.memOrderIso_symm_apply
+Set.mem_eq_mem
+Set.noConfusion
+Set.noConfusionType
+Set.ofPred.inj
+Set.ofPred.injEq
+Set.ofPred.noConfusion
+Set.ofPred.sizeOf_spec
+Set.rec
+Set.recOn
+aemeasurable_comap_iff
-derangements.Equiv.RemoveNone.fiber.congr_simp
+instCoeFunClassForallZFSetProp
+sdiff_mem_nhdsWithin
+toNat_nat

Increase in strong tech debt: (relative, absolute) = (0.93, 0.12)
Current number Change Type (strong)
4065 -1 backward.defeqAttrib.useBackward
4236 2 backward.isDefEq.respectTransparency
494 -1 erw
384 -1 adaptation notes
8 1 disabled deprecation lints
No changes to weak technical debt.

Current commit 7dd11309c0
Reference commit 8d91c72b65

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).

Comment thread Archive/Imo/Imo2024Q3.lean Resolved
@mathlib-dependent-issues mathlib-dependent-issues Bot added the blocked-by-other-PR This PR depends on another PR (this label is automatically managed by a bot) label Jul 8, 2026
@YaelDillies YaelDillies added t-data Data (lists, quotients, numbers, etc) LLM-generated PRs with substantial input from LLMs - review accordingly labels Jul 8, 2026
@themathqueen

Copy link
Copy Markdown
Member

Note that this basically takes over #39211.

@YaelDillies
YaelDillies force-pushed the set_struct branch 3 times, most recently from 9c730e8 to 3b040e4 Compare July 14, 2026 08:44
@YaelDillies
YaelDillies force-pushed the set_struct branch 2 times, most recently from a0428f8 to 18bdab6 Compare July 15, 2026 07:05
@github-actions github-actions Bot added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Jul 15, 2026
@github-actions github-actions Bot removed the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Jul 15, 2026
@YaelDillies
YaelDillies force-pushed the set_struct branch 2 times, most recently from 0773904 to ee61bd8 Compare July 16, 2026 07:14
@mathlib-merge-conflicts mathlib-merge-conflicts Bot added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Jul 17, 2026
@mathlib-merge-conflicts

Copy link
Copy Markdown

This pull request has conflicts, please merge master and resolve them.

@github-actions github-actions Bot removed the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Jul 17, 2026
@mathlib-merge-conflicts mathlib-merge-conflicts Bot added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Jul 18, 2026
@mathlib-merge-conflicts

Copy link
Copy Markdown

This pull request has conflicts, please merge master and resolve them.

@github-actions github-actions Bot removed the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Jul 19, 2026
@mathlib-merge-conflicts

Copy link
Copy Markdown

This pull request has conflicts, please merge master and resolve them.

@mathlib-merge-conflicts mathlib-merge-conflicts Bot added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Jul 20, 2026
@mathlib-merge-conflicts

Copy link
Copy Markdown

This pull request has conflicts, please merge master and resolve them.

1 similar comment
@mathlib-merge-conflicts

Copy link
Copy Markdown

This pull request has conflicts, please merge master and resolve them.

@JovanGerb JovanGerb 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.

Thanks a lot for making this PR! I went through all the changes, and have just one small comment.

Comment thread Mathlib/Data/Set/Basic.lean Resolved
@tb65536

tb65536 commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

!radar

@leanprover-radar

leanprover-radar commented Oct 1, 2026 •

Copy link
Copy Markdown

Benchmark results for 28e880d against 18dc857 are in. There are significant results. @tb65536

  • ✅ build//instructions: -160.8G (-0.12%)

Large changes (31✅, 7🟥)

  • 🟥 build//maxrss: +497MiB (+17.24%)
  • 🟥 build/module/Mathlib.Algebra.Order.Ring.StandardPart//instructions: +40.5G (+52.36%)
  • ✅ build/module/Mathlib.Algebra.Tropical.Lattice//instructions: -5.4G (-44.02%)
  • 🟥 build/module/Mathlib.AlgebraicGeometry.ProjectiveSpectrum.Functor//instructions: +53.3G (+31.09%)
  • ✅ build/module/Mathlib.AlgebraicGeometry.SpreadingOut//instructions: -35.8G (-56.45%)
  • ✅ build/module/Mathlib.Analysis.Analytic.Composition//instructions: -12.1G (-13.83%)
  • 🟥 build/module/Mathlib.Analysis.Calculus.Deriv.Basic//instructions: +9.5G (+21.35%)
  • ✅ build/module/Mathlib.Analysis.Fourier.FourierTransform//instructions: -54.4G (-32.98%)
  • ✅ build/module/Mathlib.Analysis.SpecialFunctions.Complex.LogBounds//instructions: -18.9G (-35.30%)
  • ✅ build/module/Mathlib.Analysis.SpecialFunctions.Pow.Continuity//instructions: -14.4G (-42.06%)
  • 🟥 build/module/Mathlib.Dynamics.Ergodic.Conservative//instructions: +6.8G (+85.21%)
  • ✅ build/module/Mathlib.FieldTheory.RatFunc.Basic//instructions: -31.4G (-31.28%)
  • ✅ build/module/Mathlib.Geometry.Convex.Cone.Dual//instructions: -6.7G (-26.36%)
  • ✅ build/module/Mathlib.Geometry.Convex.ConvexSpace.Module//instructions: -18.9G (-31.13%)
  • ✅ build/module/Mathlib.GroupTheory.GroupAction.Jordan//instructions: -5.3G (-15.09%)
  • ✅ build/module/Mathlib.GroupTheory.OrderOfElement//instructions: -14.7G (-17.82%)
  • ✅ build/module/Mathlib.LinearAlgebra.CliffordAlgebra.Grading//instructions: -12.3G (-40.70%)
  • ✅ build/module/Mathlib.LinearAlgebra.Semisimple//instructions: -6.9G (-17.15%)
  • 🟥 build/module/Mathlib.LinearAlgebra.TensorProduct.Subalgebra//instructions: +35.4G (+41.43%)
  • ✅ build/module/Mathlib.MeasureTheory.Measure.ProbabilityMeasure//instructions: -6.6G (-23.42%)
  • and 18 more

Medium changes (73✅, 63🟥)

  • 🟥 build/module/Mathlib.Algebra.Algebra.Subalgebra.Basic//instructions: +3.1G (+7.39%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Mathlib.Algebra.Algebra.Subalgebra.Rank//instructions: +1.7G (+13.88%)
  • 🟥 build/module/Mathlib.Algebra.Category.AlgCat.Limits//instructions: +2.4G (+7.82%)
  • 🟥 build/module/Mathlib.Algebra.Category.Ring.Limits//instructions: +7.5G (+9.35%)
  • ✅ build/module/Mathlib.Algebra.Group.UniqueProds.Basic//instructions: -1.7G (-5.76%)
  • ✅ build/module/Mathlib.Algebra.Homology.BifunctorShift//instructions: -3.6G (-5.21%)
  • 🟥 build/module/Mathlib.Algebra.Lie.Derivation.Killing//instructions: +1.3G (+9.72%)
  • 🟥 build/module/Mathlib.Algebra.Lie.Nilpotent//instructions: +1.4G (+3.21%)
  • 🟥 build/module/Mathlib.Algebra.Lie.TraceForm//instructions: +2.7G (+4.22%)
  • 🟥 build/module/Mathlib.Algebra.Lie.Weights.Cartan//instructions: +1.6G (+5.18%)
  • ✅ build/module/Mathlib.Algebra.Order.Archimedean.Class//instructions: -2.7G (-6.30%)
  • 🟥 build/module/Mathlib.Algebra.Order.Ring.Archimedean//instructions: +3.9G (+14.01%)
  • ✅ build/module/Mathlib.Algebra.Ring.IsFormallyReal//instructions: -5.0G (-35.16%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Mathlib.Algebra.Star.NonUnitalSubalgebra//instructions: +4.0G (+4.97%)
  • 🟥 build/module/Mathlib.AlgebraicGeometry.AffineScheme//instructions: +4.9G (+5.08%)
  • 🟥 build/module/Mathlib.AlgebraicGeometry.AffineSpace//instructions: +3.1G (+5.88%)
  • 🟥 build/module/Mathlib.AlgebraicGeometry.AffineTransitionLimit//instructions: +18.1G (+8.38%)
  • 🟥 build/module/Mathlib.AlgebraicGeometry.GammaSpecAdjunction//instructions: +7.7G (+8.22%)
  • 🟥 build/module/Mathlib.AlgebraicGeometry.Group.Affine//instructions: +6.0G (+5.68%)
  • 🟥 build/module/Mathlib.AlgebraicGeometry.IdealSheaf.Subscheme//instructions: +4.5G (+6.00%)
  • and 116 more

Small changes (83✅, 189🟥)

  • 🟥 build/module/Mathlib.Algebra.Algebra.NonUnitalSubalgebra//instructions: +954.5M (+1.67%)
  • ✅ build/module/Mathlib.Algebra.Algebra.Operations//instructions: -1.5G (-3.17%)
  • 🟥 build/module/Mathlib.Algebra.Algebra.Subalgebra.Directed//instructions: +476.9M (+4.55%)
  • 🟥 build/module/Mathlib.Algebra.Category.Grp.Limits//instructions: +1.3G (+3.46%)
  • ✅ build/module/Mathlib.Algebra.Category.ModuleCat.Limits//instructions: -1.1G (-4.21%)
  • 🟥 build/module/Mathlib.Algebra.Field.Subfield.Basic//instructions: +1.1G (+4.83%)
  • 🟥 build/module/Mathlib.Algebra.Group.IsApply//instructions: +8.6M (+0.15%)
  • ✅ build/module/Mathlib.Algebra.Group.Subgroup.Ker//instructions: -556.6M (-2.17%)
  • ✅ build/module/Mathlib.Algebra.Lie.Basis.Base//instructions: -1.4G (-2.16%)
  • 🟥 build/module/Mathlib.Algebra.Lie.Basis.Prod//instructions: +1.2G (+2.47%)
  • 🟥 build/module/Mathlib.Algebra.Lie.CartanExists//instructions: +1.3G (+4.71%)
  • ✅ build/module/Mathlib.Algebra.Lie.Classical//instructions: -772.6M (-3.71%)
  • 🟥 build/module/Mathlib.Algebra.Lie.Extension//instructions: +1.9G (+3.33%)
  • 🟥 build/module/Mathlib.Algebra.Lie.LieTheorem//instructions: +678.6M (+2.09%)
  • 🟥 build/module/Mathlib.Algebra.Lie.Loop//instructions: +2.1G (+7.12%)
  • 🟥 build/module/Mathlib.Algebra.Lie.Subalgebra//instructions: +624.6M (+1.84%)
  • 🟥 build/module/Mathlib.Algebra.Lie.Submodule//instructions: +822.1M (+1.65%)
  • 🟥 build/module/Mathlib.Algebra.Lie.Weights.Basic//instructions: +1.2G (+2.19%)
  • 🟥 build/module/Mathlib.Algebra.Lie.Weights.IsSimple//instructions: +1.4G (+2.22%)
  • 🟥 build/module/Mathlib.Algebra.Lie.Weights.Killing//instructions: +1.1G (+1.36%)
  • and 251 more
  • and 1 hidden

@JovanGerb

Copy link
Copy Markdown
Contributor

maintainer merge

@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown

🚀 Pull request has been placed on the maintainer queue by JovanGerb.

@mathlib-merge-conflicts

Copy link
Copy Markdown

This pull request has conflicts, please merge master and resolve them.

@mathlib-merge-conflicts

Copy link
Copy Markdown

This pull request has conflicts, please merge master and resolve them.

@Vierkantor Vierkantor 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.

There are some unfortunate regressions but most of the worst ones are easy enough to fix. And a lot of very small annoyances like broken grind or needing to specify more implicit parameters, which I don't think we should block this PR over.

Looks good with these suggestions. 🎉

Comment thread Mathlib/Analysis/Convex/Gauge.lean Resolved
-/
def toNat (p : Set α) : Set ℕ :=
{ n | p ((Encodable.decode (α := α) n).getD default) }
{ n | (Encodable.decode (α := α) n).getD default ∈ p }

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.

This file has a lot of positive changes, nice work!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In fact, I meant to split it out to a separate PR but forgot, whoops! I have already split out all other big removals of defeq abuse removed (which is why the remaining diff doesn't look so glorious), but I guess there is little point splitting this out now that you have reviewed the PR? Up to you

Comment thread Mathlib/SetTheory/ZFC/Class.lean Resolved
@Vierkantor

Copy link
Copy Markdown
Contributor

The changes look good, and everything builds, so let's get this merged 🎉

bors r+

@mathlib-bors

mathlib-bors Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

@alexanderlhicks

Copy link
Copy Markdown

A note for anyone who finds proofs that the kernel refuses after this change.

Since Set became a structure, a ∈ s unfolds to the applied projection s.Mem a. Suppose
that Lean's kernel compares a ∈ s with a ∈ t, and s and t differ syntactically. The
kernel can now reduce s and t to Set.ofPred constructors before it compares them. Before
this PR, a ∈ s unfolded to s a, and lazy delta reduction compared s with t first. If the
reduction of s is expensive, a proof that checked before this PR can now fail.

The kernel behaviour is older than this PR, and leanprover/lean4#16613 reports it with a
reproduction that does not use Mathlib. This PR makes the code path common, because every
membership a ∈ s now unfolds to an applied projection. The file below shows the difference. It
adds each theorem with addDecl, so only the kernel checks it:

import Mathlib.Data.Set.Defs
open Lean Meta Elab Term

/-- Reducing `big n` needs `2 ^ n`. -/
def big (n : Nat) : Set Nat := if 2 ^ n = 0 then {a | a = 1} else {a | a = 0}

/-- `small` unfolds to `big (2 ^ 32)` in one delta step. -/
def small : Set Nat := big (2 ^ 32)

/-- Elaborate a statement `∀ xs, P → Q` and add the theorem `fun xs h => h` with `addDecl`, so
that only the kernel checks that `P` and `Q` are definitionally equal. -/
elab "#add_identity_theorem " name:ident " : " stmt:term : command =>
  Command.liftTermElabM do
    let type ← elabType stmt
    synthesizeSyntheticMVarsNoPostponing
    let type ← instantiateMVars type
    let value ← forallTelescope type fun xs _ => mkLambdaFVars xs xs.back!
    addDecl (.thmDecl { name := name.getId, levelParams := [], type, value })

-- Accepted before mathlib4#41506, rejected after it: "(kernel) the kernel refused to evaluate
-- `Nat.pow` because its second argument does not fit in a 32-bit unsigned integer".
#add_identity_theorem membership : ∀ a : Nat, a ∈ small → a ∈ big (2 ^ 32)

-- Accepted before and after mathlib4#41506.
#add_identity_theorem equality : ∀ P : Set Nat → Prop, P small → P (big (2 ^ 32))

Results:

  • Mathlib 56a0058 (three commits before this PR), Lean v4.35.0: the kernel accepts both theorems.
  • Mathlib fc4f59e, Lean v4.36.0: the kernel rejects membership and accepts equality.

The two runs also differ in the Lean version. The upstream issue shows the same kernel results on
v4.35.0, v4.36.0 and the 2026-11-19 nightly. The change in behaviour therefore comes from the
definition of Set.

We found this in VCVio, where support of a probabilistic computation is a Set. Reducing
support c to a constructor runs the computation c. A proof about a computation that samples a
2^32-bit value checked before this PR and is refused after it. Until the kernel changes, a
workaround is to avoid kernel comparisons of a ∈ s with a ∈ t where s and t differ
syntactically. For example, rewrite with equation lemmas, whose right-hand sides match the
definition bodies exactly, and generalize expensive subterms before applying a lemma.

🤖 Generated with Claude Code

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. increases-technical-debt Automatically added label for PRs which increase "strong" technical debts LLM-generated PRs with substantial input from LLMs - review accordingly ready-to-merge This PR has been sent to bors. t-data Data (lists, quotients, numbers, etc) tech debt Fixes cross-cutting technical debt, see the "technical debt counters" stream on zulip

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Diamond for CompleteBooleanAlgebra of Set's

10 participants