`ac_rfl` and `ac_nf` leave a subterm unnormalized when it also occurs inside a longer chain
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Aptitud para principiantes
- 59/100
Línea de trabajo
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.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- Lean
- Estrellas
- 10.3k
- Forks
- 1.1k
- Merge medio
- 19 h 8 min
- PR fusionados (30 d)
- 421
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de leanprover/lean4
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
leanprover/lean4#16681 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
leanprover/lean4#16305 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 86/100
leanprover/lean4#16299 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
leanprover/lean4#16257 ·
Los mantenedores suelen responder en 1 día
-
`IO.FS.realPath` reports every failure as `noFileOrDirectory` instead of classifying by `errno`Abiertobug
Dificultad 2/5 1-3 horas Aptitud para principiantes 66/100
leanprover/lean4#15683 ·
Los mantenedores suelen responder en 1 día
Todos los issues de leanprover/lean4
Issues similares
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 92/100
Los mantenedores suelen responder en 1 día
-
garden
Dificultad 2/5 1-3 horas Aptitud para principiantes 64/100
taecontrol/google-accounts-mcp#97 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
TigreGotico/scriptconv-copyleft#13 ·
Los mantenedores suelen responder en 1 día
-
bug mac test-rig
Dificultad 1/5 Menos de una hora Aptitud para principiantes 75/100
Los mantenedores suelen responder en 1 día