`ac_rfl` and `ac_nf` leave a subterm unnormalized when it also occurs inside a longer chain

Open
#16,599 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
59/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Domain
testing, tooling

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

bug
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:

https://github.com/leanprover/lean4/blob/e45bb4888ddc7b3f6361092171ca9aa087a40f5a/src/Lean/Meta/Tactic/AC/Main.lean#L177-L179

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.

https://github.com/leanprover/lean4/blob/e45bb4888ddc7b3f6361092171ca9aa087a40f5a/src/Lean/Meta/Tactic/Simp/Types.lean#L79-L82

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.

https://github.com/leanprover/lean4/blob/e45bb4888ddc7b3f6361092171ca9aa087a40f5a/src/Lean/Meta/Tactic/Simp/Main.lean#L693

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 no llvm-ar or lld
  • the files in src/ that use ac_rfl or ac_nf (in Init, Std, and bv_decide's normalizer) elaborate with the patched stage1
  • in Mathlib (master, on v4.36.0, whose AC/Main.lean is identical), the 41 files that use ac_rfl or ac_nf, directly or through ac_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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from leanprover/lean4

All issues in leanprover/lean4

Similar issues

More Testing & QA issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.