`ac_rfl` and `ac_nf` leave a subterm unnormalized when it also occurs inside a longer chain
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 59/100
Direzione di ricerca
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.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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.
- Lingua principale
- Lean
- Stelle
- 10.3k
- Fork
- 1.1k
- Merge medio
- 19h 8m
- PR unite (30g)
- 421
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di leanprover/lean4
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
leanprover/lean4#16681 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
leanprover/lean4#16305 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
leanprover/lean4#16299 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
leanprover/lean4#16257 ·
I maintainer di solito rispondono entro 1 giorno
-
`IO.FS.realPath` reports every failure as `noFileOrDirectory` instead of classifying by `errno`Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
leanprover/lean4#15683 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di leanprover/lean4
Issue simili
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 92/100
I maintainer di solito rispondono entro 1 giorno
-
garden
Difficoltà 2/5 1-3 ore Idoneità per principianti 64/100
taecontrol/google-accounts-mcp#97 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
TigreGotico/scriptconv-copyleft#13 ·
I maintainer di solito rispondono entro 1 giorno
-
bug mac test-rig
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 75/100
I maintainer di solito rispondono entro 1 giorno