Skip to content

[Merged by Bors] - fix: have to_additive unfold aux lemmas before translation - #23559

Closed
kmill wants to merge 9 commits into
masterfrom
kmill_toadditive_auxlemma_fixes
Closed

kmill wants to merge 9 commits into
masterfrom
kmill_toadditive_auxlemma_fixes

Conversation

@kmill

@kmill kmill commented Apr 1, 2025 •

Copy link
Copy Markdown
Contributor

This PR changes how to_additive approaches translation by first unfolding all aux lemmas. Unfolding aux lemmas was previously added in #14628, since it was observed that aux lemmas tend not to have enough context to properly be additivized. This was added as a step after processing any aux decls. The change here is to make the unfolding happen before processing aux decls. We now also abstract nested proofs in additivized definitions.

Additionally, we use Meta.check instead of Meta.inferType to correctly check the type of the translated declaration.

The aux lemma unfolding is necessary for the next Lean release (v4.19.0).


Open in Gitpod

@kmill kmill added the t-meta Tactics, attributes or user commands label Apr 1, 2025
@github-actions

github-actions Bot commented Apr 1, 2025 •

Copy link
Copy Markdown

PR summary 6a1f4f1876

Import changes for modified files

Dependency changes

File Base Count Head Count Change
Mathlib.Tactic.ToAdditive.Frontend 51 52 +1 (+1.96%)
Import changes for all files
Files Import difference
There are 5576 files with changed transitive imports taking up over 238229 characters: this is too many to display!
You can run scripts/import_trans_difference.sh all locally to see the whole output.

Declarations diff

+ declAbstractNestedProofs
+ declUnfoldAuxLemmas
- setInlineAttribute

You can run this locally as follows
## summary with just the declaration names:
./scripts/declarations_diff.sh <optional_commit>

## more verbose report:
./scripts/declarations_diff.sh long <optional_commit>

The doc-module for script/declarations_diff.sh contains some details about this script.


No changes to technical debt.

You can run this locally as

./scripts/technical-debt-metrics.sh 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).

@fpvandoorn fpvandoorn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

Could you bench this if it compiles?
Could you add one or more tests?

Comment on lines 804 to 805
for n in findAuxDecls srcDecl.type pre do
transformDeclAux cfg pre tgt_pre n

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Are these lines (and the few below this) still necessary?
It would be nice if you could specify in a comment somewhere which aux-decls we inline, and which ones we transform (and why). We call both of them "aux decls"/"aux lemmas", which is not very helpful.

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.

I've tried making the comments clearer. We're unfolding the "proof_nnn aux lemmas"; there are still aux decls that aren't unfolded.

I've also added a TODO to consider trying to additivize these aux lemmas and resort to unfolding them on failure.

I'm not sure exactly what kind of test to write. Any ideas?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe a test that tests whether abstractNestedProofs, i.e. testing for the existence of a nested abstracted proof in the body of a definition.

Maybe a test where simp generates an aux-lemma for the multiplicative version, where the aux-lemma cannot be additivized (by using a class with no additive counterpart, but using definitions that do have additive counterparts), but the whole declaration can be.

The last one is not that important, since I expect that Mathlib itself will have plenty of those tests.

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.

Could I ask you to write such test(s)? I won't be able to get to figuring out how to test this until this evening.

My motivation with this PR is getting mathlib to work on Lean v4.19.0, which is being released imminently.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah, I didn't realize this was blocking the 4.19 release. Then let's merge this as is. The tests don't seem that important.

Comment thread Mathlib/Tactic/ToAdditive/Frontend.lean Resolved
@kmill

kmill commented Apr 1, 2025

Copy link
Copy Markdown
Contributor Author

!bench

@leanprover-bot

Copy link
Copy Markdown

Here are the benchmark results for commit aab9bef.
There were significant changes against commit 1e7f890:

  Benchmark                                   Metric         Change
  =================================================================
- ~Mathlib.CategoryTheory.Monoidal.Discrete   instructions    31.3%

@github-actions

github-actions Bot commented Apr 1, 2025

Copy link
Copy Markdown
File Instructions %
build +254.808⬝10⁹ (+0.15%)
Mathlib.CategoryTheory.Monoidal.Discrete +13.745⬝10⁹ (+31.34%)
Mathlib.GroupTheory.MonoidLocalization.Basic +7.586⬝10⁹ (+8.52%)
Mathlib.Algebra.Category.Grp.Limits +5.530⬝10⁹ (+8.51%)
3 files, Instructions +4.0⬝10⁹
File Instructions %
Mathlib.Algebra.Category.MonCat.Limits +4.886⬝10⁹ (+9.90%)
Mathlib.Algebra.Category.MonCat.FilteredColimits +4.500⬝10⁹ (+8.10%)
Mathlib.GroupTheory.Coprod.Basic +4.387⬝10⁹ (+7.66%)
4 files, Instructions +3.0⬝10⁹
File Instructions %
Mathlib.MeasureTheory.Measure.Haar.Unique +3.858⬝10⁹ (+4.91%)
Mathlib.Algebra.Group.Basic +3.688⬝10⁹ (+8.94%)
Mathlib.GroupTheory.OreLocalization.Basic +3.285⬝10⁹ (+6.28%)
Mathlib.Combinatorics.Additive.SmallTripling +3.33⬝10⁹ (+4.52%)
14 files, Instructions +2.0⬝10⁹
File Instructions %
Mathlib.Analysis.Normed.Group.Basic +2.543⬝10⁹ (+3.48%)
Mathlib.GroupTheory.Coset.Basic +2.465⬝10⁹ (+6.16%)
Mathlib.Algebra.BigOperators.Group.Finset.Basic +2.433⬝10⁹ (+3.62%)
Mathlib.Algebra.Group.Subgroup.Basic +2.417⬝10⁹ (+6.11%)
Mathlib.Topology.Algebra.Group.Basic +2.270⬝10⁹ (+3.63%)
Mathlib.Algebra.Group.Submonoid.Operations +2.264⬝10⁹ (+5.06%)
Mathlib.Combinatorics.Additive.PluenneckeRuzsa +2.257⬝10⁹ (+6.24%)
Mathlib.Algebra.Group.Hom.Instances +2.222⬝10⁹ (+11.05%)
Mathlib.GroupTheory.CosetCover +2.214⬝10⁹ (+3.16%)
Mathlib.Algebra.Category.Grp.FilteredColimits +2.194⬝10⁹ (+8.65%)
Mathlib.GroupTheory.FreeGroup.Basic +2.188⬝10⁹ (+5.39%)
Mathlib.Algebra.Order.Group.Unbundled.Basic +2.170⬝10⁹ (+7.21%)
Mathlib.Algebra.Group.Prod +2.109⬝10⁹ (+8.32%)
Mathlib.Algebra.Order.Monovary +2.69⬝10⁹ (+3.84%)
52 files, Instructions +1.0⬝10⁹
File Instructions %
Mathlib.GroupTheory.Goursat +1.949⬝10⁹ (+3.00%)
Mathlib.GroupTheory.Complement +1.941⬝10⁹ (+3.00%)
Mathlib.GroupTheory.OrderOfElement +1.908⬝10⁹ (+1.97%)
Mathlib.MeasureTheory.Measure.Haar.Basic +1.863⬝10⁹ (+4.69%)
Mathlib.GroupTheory.Congruence.Defs +1.815⬝10⁹ (+5.59%)
Mathlib.GroupTheory.GroupAction.Blocks +1.814⬝10⁹ (+3.23%)
Mathlib.GroupTheory.NoncommPiCoprod +1.802⬝10⁹ (+8.03%)
Mathlib.Algebra.Colimit.DirectLimit +1.742⬝10⁹ (+2.52%)
Mathlib.Algebra.Group.UniqueProds.Basic +1.738⬝10⁹ (+4.65%)
Mathlib.Algebra.Group.Pointwise.Finset.Basic +1.695⬝10⁹ (+3.35%)
Mathlib.MeasureTheory.Group.FundamentalDomain +1.686⬝10⁹ (+1.23%)
Mathlib.GroupTheory.Index +1.681⬝10⁹ (+5.57%)
Mathlib.Algebra.Order.Group.Abs +1.677⬝10⁹ (+10.65%)
Mathlib.Algebra.Group.Pointwise.Set.Basic +1.677⬝10⁹ (+3.58%)
Mathlib.GroupTheory.SpecificGroups.Cyclic +1.655⬝10⁹ (+3.09%)
Mathlib.Order.Filter.Pointwise +1.629⬝10⁹ (+4.39%)
Mathlib.Algebra.BigOperators.Group.List.Basic +1.629⬝10⁹ (+7.14%)
Mathlib.Algebra.Category.Grp.Basic +1.625⬝10⁹ (+4.18%)
Mathlib.GroupTheory.MonoidLocalization.Order +1.605⬝10⁹ (+9.30%)
Mathlib.GroupTheory.GroupAction.Quotient +1.584⬝10⁹ (+4.31%)
Mathlib.Combinatorics.Additive.ApproximateSubgroup +1.571⬝10⁹ (+4.73%)
Mathlib.GroupTheory.QuotientGroup.Basic +1.553⬝10⁹ (+4.46%)
Mathlib.Algebra.Group.InjSurj +1.462⬝10⁹ (+6.55%)
Mathlib.Geometry.Group.Growth.LinearLowerBound +1.420⬝10⁹ (+14.73%)
Mathlib.Topology.Algebra.Category.ProfiniteGrp.Basic +1.389⬝10⁹ (+2.01%)
Mathlib.Algebra.Group.Subgroup.Map +1.377⬝10⁹ (+4.65%)
Mathlib.Algebra.BigOperators.Finprod +1.363⬝10⁹ (+2.55%)
Mathlib.GroupTheory.ArchimedeanDensely +1.324⬝10⁹ (+2.11%)
Mathlib.Geometry.Manifold.Algebra.SmoothFunctions +1.324⬝10⁹ (+2.43%)
Mathlib.Topology.Algebra.ContinuousMonoidHom +1.323⬝10⁹ (+4.73%)
Mathlib.Algebra.Group.Subgroup.Defs +1.311⬝10⁹ (+4.81%)
Mathlib.MeasureTheory.Group.Measure +1.290⬝10⁹ (+2.16%)
Mathlib.Analysis.Normed.Group.Uniform +1.279⬝10⁹ (+4.09%)
Mathlib.Algebra.Group.Subgroup.Ker +1.257⬝10⁹ (+4.57%)
Mathlib.Algebra.Group.Subgroup.Pointwise +1.214⬝10⁹ (+3.04%)
Mathlib.Algebra.Group.Defs +1.206⬝10⁹ (+5.11%)
Mathlib.Algebra.Free +1.188⬝10⁹ (+4.34%)
Mathlib.Analysis.Normed.Group.Pointwise +1.183⬝10⁹ (+4.85%)
Mathlib.MeasureTheory.Group.Prod +1.182⬝10⁹ (+3.64%)
Mathlib.Algebra.Order.Interval.Basic +1.176⬝10⁹ (+2.92%)
Mathlib.Data.Finset.NoncommProd +1.158⬝10⁹ (+6.47%)
Mathlib.Combinatorics.Additive.DoublingConst +1.138⬝10⁹ (+4.24%)
Mathlib.Algebra.Group.Subsemigroup.Operations +1.136⬝10⁹ (+4.04%)
Mathlib.Algebra.Category.MonCat.Basic +1.131⬝10⁹ (+3.28%)
Mathlib.Algebra.Group.Subgroup.Lattice +1.125⬝10⁹ (+4.04%)
Mathlib.GroupTheory.QuotientGroup.Defs +1.122⬝10⁹ (+5.17%)
Mathlib.MeasureTheory.Measure.Haar.Quotient +1.106⬝10⁹ (+2.20%)
Mathlib.GroupTheory.Exponent +1.89⬝10⁹ (+4.00%)
Mathlib.Algebra.Group.Submonoid.Units +1.39⬝10⁹ (+5.19%)
Mathlib.Topology.Algebra.InfiniteSum.NatInt +1.4⬝10⁹ (+4.15%)
Mathlib.Topology.Algebra.OpenSubgroup +1.4⬝10⁹ (+3.53%)
Mathlib.Algebra.Group.Units.Defs +1.3⬝10⁹ (+5.68%)

CI run

Comment thread Mathlib/Tactic/ToAdditive/Frontend.lean Resolved
Comment thread Mathlib/Lean/Name.lean Resolved
Comment thread Mathlib/Tactic/ToAdditive/Frontend.lean Resolved
Comment thread Mathlib/Tactic/ToAdditive/Frontend.lean Resolved
Comment thread Mathlib/Tactic/ToAdditive/Frontend.lean Resolved
@fpvandoorn fpvandoorn added the auto-merge-after-CI Please do not add manually. Requests for a bot to merge automatically once CI is done. label Apr 2, 2025
@ghost

ghost commented Apr 2, 2025

Copy link
Copy Markdown

As this PR is labelled auto-merge-after-CI, we are now sending it to bors:

bors merge

@ghost ghost added the ready-to-merge This PR has been sent to bors. label Apr 2, 2025
mathlib-bors Bot pushed a commit that referenced this pull request Apr 2, 2025
This PR changes how `to_additive` approaches translation by first unfolding all aux lemmas. Unfolding aux lemmas was previously added in #14628, since it was observed that aux lemmas tend not to have enough context to properly be additivized. This was added as a step after processing any aux decls. The change here is to make the unfolding happen before processing aux decls. We now also abstract nested proofs in additivized definitions.

Additionally, we use `Meta.check` instead of `Meta.inferType` to correctly check the type of the translated declaration.

The aux lemma unfolding is necessary for the next Lean release (v4.19.0).



Co-authored-by: Floris van Doorn <[email protected]>
@mathlib-bors

mathlib-bors Bot commented Apr 2, 2025

Copy link
Copy Markdown
Contributor

Pull request successfully merged into master.

Build succeeded:

@mathlib-bors mathlib-bors Bot changed the title fix: have to_additive unfold aux lemmas before translation [Merged by Bors] - fix: have to_additive unfold aux lemmas before translation Apr 2, 2025
@mathlib-bors mathlib-bors Bot closed this Apr 2, 2025
@mathlib-bors
mathlib-bors Bot deleted the kmill_toadditive_auxlemma_fixes branch April 2, 2025 13:27
tannerduve pushed a commit that referenced this pull request May 13, 2025
This PR changes how `to_additive` approaches translation by first unfolding all aux lemmas. Unfolding aux lemmas was previously added in #14628, since it was observed that aux lemmas tend not to have enough context to properly be additivized. This was added as a step after processing any aux decls. The change here is to make the unfolding happen before processing aux decls. We now also abstract nested proofs in additivized definitions.

Additionally, we use `Meta.check` instead of `Meta.inferType` to correctly check the type of the translated declaration.

The aux lemma unfolding is necessary for the next Lean release (v4.19.0).



Co-authored-by: Floris van Doorn <[email protected]>
JovanGerb added a commit to JovanGerb/mathlib4 that referenced this pull request Jul 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto-merge-after-CI Please do not add manually. Requests for a bot to merge automatically once CI is done. ready-to-merge This PR has been sent to bors. t-meta Tactics, attributes or user commands

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants