`ac_rfl` and `ac_nf` leave a subterm unnormalized when it also occurs inside a longer chain
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 59/100
Research direction
Start by reading src/Lean/Meta/Tactic/AC/Main.lean around Lean.Meta.AC.post and the Simp.Context.parent? cache guidance in src/Lean/Meta/Tactic/Simp/Types.lean. Reproduce the reported failures with the examples in the issue and check whether the proposed cache setting fixes them. Done means all five examples pass; the issue reports broader test and elaboration checks already performed.
Written by the indexing model from the issue text.
Description
Prerequisites
- Check that your issue is not already filed:
https://github.com/leanprover/lean4/issues - Reduce the issue to a minimal, self-contained, reproducible test case.
Avoid dependencies to Mathlib or Batteries. - Test your test case against the latest nightly release, for example on
https://live.lean-lang.org/#project=lean-nightly
(You can also use the settings there to switch to “Lean nightly”) - When reporting a perceived soundness bug: Does it affect
lake check --paranoid?
If in doubt, please reach out on [email protected] for responsible disclosure.
Description
Hi!
There are certain goals based only on associativity and commutativity that ac_rfl and ac_nf should solve that they currently don't. This happens when a same subterm occurs twice: first as an inner node of a longer chain of its operator, and then as the top of a chain of its own. For instance, A ∩ (B ∩ A) = B ∩ A fails to be proven due to the second B ∩ A not being normalized, but B ∩ A = A ∩ (B ∩ A) succeeds. This happens due to simp's cache, as I explain more below.
Context
I ran into this issue while porting a set theory library to Lean, where I had the exact goal above not able to be proven after declaring ∩ on sets as Std.Associative, Std.Commutative, and Std.IdempotentOp. There are more examples below that need neither idempotence nor anything outside core. There is no prior discussion on Zulip.
Steps to Reproduce
-- One operator. Both examples below fail with the second
-- `b + a` as the top of its own chain:
example (a b : Nat) : (a + (b + a), b + a) = (a + a + b, a + b) := by ac_rfl -- fails
example (a b : Nat) : (a + (b + a), b + a) = (a + a + b, a + b) := by ac_nf -- fails
-- The same equation with the components of each pair swapped succeeds:
example (a b : Nat) : (b + a, a + (b + a)) = (a + b, a + a + b) := by ac_rfl -- succeeds
-- Two operators: `y * x` occurs inside a `*` chain, then directly under `+`:
example (x y z : Nat) : z * (y * x) + y * x = x * y * z + x * y := by ac_rfl -- fails
-- An idempotent operator: one side of the equation occurs inside the other.
example (a b : Bool) : (a && (b && a)) = (b && a) := by ac_rfl -- fails
Expected behavior: all five examples above should be proved.
Actual behavior: the four marked examples fail. For the first one:
error: Tactic `rfl` failed: equality lhs
(a + (a + b), b + a)
is not definitionally equal to rhs
(a + (a + b), a + b)
Versions
Lean (version 4.38.0-pre, arm64-apple-darwin24.6.0, commit e45bb4888ddc7b3f6361092171ca9aa087a40f5a, Release)
I'm using macOS 15.8.1 on arm64. The bug was also reproduced on v4.33.0, v4.36.0, and v4.37.0-rc1.
Additional Information
Cause
After investigating, the cause of the issue is as follows. ac_rfl and ac_nf run simp with the hook Lean.Meta.AC.post. When a binary application sits directly under the same operator, the hook returns it unchanged and leaves the whole chain to be normalized at its top:
That answer depends on the parent, but simp caches it under the term alone. The documentation of Simp.Context.parent?, as follows, says that a procedure whose result depends on the parent must set Result.cache := false, and the hook does not.
In the first example in the steps to reproduce, the inner b + a is visited as part of a + (b + a) and cached as unchanged. The second component b + a, whose parent is the pair, then hits the cache, and the hook never runs on it.
In the swapped version, the standalone b + a is visited first and cached in normal form, which is harmless.
Proposed solution
The above return to the simplification hook can set the cache field as false:
| bin op₁ l r, some (bin op₂ _ _) =>
if ←isDefEq op₁ op₂ then
- return Simp.Step.done { expr := e }
+ return Simp.Step.done { expr := e, cache := false }
Only this answer needs to stay out of the cache. The hook's other parent-dependent answer, a chain normalized at its top, can stay cached: if it is later reused for an inner node, the enclosing chain is still normalized from its atoms, so the final result is the same.
With this change on master (e45bb48), which I checked locally:
- the examples above pass (and fail without the change)
- the test suite passes, except five Lake tests (
cache,externLib,kinds,precompileLink,targets) that fail in my local macOS build because it bundles nollvm-arorlld - the files in
src/that useac_rflorac_nf(inInit,Std, andbv_decide's normalizer) elaborate with the patched stage1 - in Mathlib (
master, onv4.36.0, whoseAC/Main.leanis identical), the 41 files that useac_rflorac_nf, directly or throughac_change, give the same output with the patched tactic swapped in
I did not measure performance changes in Mathlib or Lean. Inner nodes are no longer cached, so a chain that occurs many times as an inner node is traversed again each time. With the existing uses in Mathlib, build times shouldn't perceptibly change, but I imagine it would be possible to construct an adversarial, long goal whose performance could be degraded (a goal in which the same chain occurs many times inside longer chains would be the worst case).
Impact
Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.
LLM usage
I used Claude Opus 5.5 to analyze the problem, run the experiments and tests, and proof-read this issue.
- Dominant language
- Lean
- Stars
- 10.3k
- Forks
- 1.1k
- Avg merge
- 19h 8m
- Merged PRs (30d)
- 421
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from leanprover/lean4
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
leanprover/lean4#16681 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
leanprover/lean4#16305 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
leanprover/lean4#16299 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
leanprover/lean4#16257 ·
Maintainers usually reply within 1 day
-
`IO.FS.realPath` reports every failure as `noFileOrDirectory` instead of classifying by `errno`Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
leanprover/lean4#15683 ·
Maintainers usually reply within 1 day
All issues in leanprover/lean4
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
TigreGotico/scriptconv-copyleft#13 ·
Maintainers usually reply within 1 day
-
bug mac test-rig
Difficulty 1/5 Under an hour Newbie friendliness 75/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
software-mansion-labs/Diplomat#20 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Maintainers usually reply within 1 day