Skip to content

[Merged by Bors] - fix(Tactic/Ring): sort terms when evaluating nested powers#37494

Closed
FLDutchmann wants to merge 3 commits intoleanprover-community:masterfrom
FLDutchmann:ring-pow-fix
Closed

[Merged by Bors] - fix(Tactic/Ring): sort terms when evaluating nested powers#37494
FLDutchmann wants to merge 3 commits intoleanprover-community:masterfrom
FLDutchmann:ring-pow-fix

Conversation

@FLDutchmann
Copy link
Copy Markdown
Collaborator

ring currently misorders the terms in a product when evaluating nested powers. When evaluating e.g. (x^a*x)^b where x is an atom, it returns x^(a*b)*x^b even if b is supposed to come before a*b in the ordering on ExProds.

We fix this by running evalMulProd instead of using the .mul constructor directly. This in effect does an insertion sort on the terms in the ExProds. We do a similar thing when evaluating multiplication.

/--
error: ring failed, ring expressions not equal
a : ℚ
m n : ℕ
⊢ a ^ n * a ^ (m * n) = a ^ (m * n) * a ^ n
-/
#guard_msgs in
example (a : ℚ) (m n : ℕ) : (a ^ (m + 1)) ^ n = a ^ (n * (m + 1)) := by ring1

This bug was pointed out to me by @Multramate

The fix in this PR was written and investigated with the help of Claude code.

Open in Gitpod

@FLDutchmann FLDutchmann added t-meta Tactics, attributes or user commands LLM-generated PRs with substantial input from LLMs - review accordingly labels Apr 1, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 1, 2026

PR summary cbbd8c9b8b

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference

Declarations diff

+ mul_pow_mul

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

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

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


No changes to technical debt.

You can run this locally as

./scripts/reporting/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).

@FLDutchmann
Copy link
Copy Markdown
Collaborator Author

!bench

@leanprover-radar
Copy link
Copy Markdown

leanprover-radar commented Apr 1, 2026

Benchmark results for 21266de against cbbd8c9 are in. There are no significant changes. @FLDutchmann

  • 🟥 build//instructions: +7.8G (+0.00%)

Small changes (1✅)

  • build/module/Mathlib.Tactic.ClearExcept//instructions: -404.1M (-13.66%)

@FLDutchmann FLDutchmann marked this pull request as ready for review April 1, 2026 14:31
Copy link
Copy Markdown
Contributor

@grunweg grunweg left a comment

Choose a reason for hiding this comment

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

Seems reasonable, thanks! Let me only maintainer merge this since I'm not an expert on ring.
maintainer merge

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 1, 2026

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

@mathlib-triage mathlib-triage bot added the maintainer-merge A reviewer has approved the changed; awaiting maintainer approval. label Apr 1, 2026
Copy link
Copy Markdown
Contributor

@Vierkantor Vierkantor left a comment

Choose a reason for hiding this comment

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

Thanks! 🎉

bors r+

@mathlib-triage mathlib-triage bot added ready-to-merge This PR has been sent to bors. and removed maintainer-merge A reviewer has approved the changed; awaiting maintainer approval. labels Apr 2, 2026
mathlib-bors bot pushed a commit that referenced this pull request Apr 2, 2026
`ring` currently misorders the terms in a product when evaluating nested powers.  When evaluating e.g. `(x^a*x)^b` where `x`  is an atom, it returns `x^(a*b)*x^b` even if `b` is supposed to come before `a*b` in the ordering on `ExProd`s. 

We fix this by running `evalMulProd` instead of using the `.mul` constructor directly. This in effect does an insertion sort on the terms in the `ExProd`s. We do a similar thing when evaluating multiplication. 
```
/--
error: ring failed, ring expressions not equal
a : ℚ
m n : ℕ
⊢ a ^ n * a ^ (m * n) = a ^ (m * n) * a ^ n
-/
#guard_msgs in
example (a : ℚ) (m n : ℕ) : (a ^ (m + 1)) ^ n = a ^ (n * (m + 1)) := by ring1
```
@mathlib-bors
Copy link
Copy Markdown
Contributor

mathlib-bors bot commented Apr 2, 2026

Build failed (retrying...):

mathlib-bors bot pushed a commit that referenced this pull request Apr 2, 2026
`ring` currently misorders the terms in a product when evaluating nested powers.  When evaluating e.g. `(x^a*x)^b` where `x`  is an atom, it returns `x^(a*b)*x^b` even if `b` is supposed to come before `a*b` in the ordering on `ExProd`s. 

We fix this by running `evalMulProd` instead of using the `.mul` constructor directly. This in effect does an insertion sort on the terms in the `ExProd`s. We do a similar thing when evaluating multiplication. 
```
/--
error: ring failed, ring expressions not equal
a : ℚ
m n : ℕ
⊢ a ^ n * a ^ (m * n) = a ^ (m * n) * a ^ n
-/
#guard_msgs in
example (a : ℚ) (m n : ℕ) : (a ^ (m + 1)) ^ n = a ^ (n * (m + 1)) := by ring1
```
@mathlib-bors
Copy link
Copy Markdown
Contributor

mathlib-bors bot commented Apr 2, 2026

Pull request successfully merged into master.

Build succeeded:

@mathlib-bors mathlib-bors bot changed the title fix(Tactic/Ring): sort terms when evaluating nested powers [Merged by Bors] - fix(Tactic/Ring): sort terms when evaluating nested powers Apr 2, 2026
@mathlib-bors mathlib-bors bot closed this Apr 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

LLM-generated PRs with substantial input from LLMs - review accordingly 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.

4 participants