Skip to content

fix: key visited expressions by structural equality - #59

Open
JoAdo07 wants to merge 3 commits into
leanprover:masterfrom
JoAdo07:fix/expr-cache-structural-equality
Open

JoAdo07 wants to merge 3 commits into
leanprover:masterfrom
JoAdo07:fix/expr-cache-structural-equality

Conversation

@JoAdo07

@JoAdo07 JoAdo07 commented Oct 28, 2026 •

Copy link
Copy Markdown

Fixes #58.

visitedExprs and noMDataExprs were both keyed by Expr using its default BEq, which is alpha-equivalence and ignores binder names and binder info. An expression could therefore reuse an index, or a cached removeMData result, from an alpha-equivalent expression exported earlier. This made exported binder names and binder info depend on which declarations had already been exported.

This change introduces a small ExprKey wrapper whose BEq is Expr.equal (structural equality), and uses it for both expression caches:

  • visitedExprs, which maps an expression to its export index.
  • noMDataExprs, which caches removeMData results. That cache is reset once per dumpExpr call, not per subterm, so two alpha-equivalent subtrees with different binder info inside a single type collided.

Expr.hash is still used for hashing; structural equality implies alpha-equivalence, so the existing hash remains compatible.

Two regression tests:

  • Two lambda expressions that differ only in binder name and binder info. On the old code the second expression reuses the first index and is not emitted; with this change both are emitted with their own indices.
  • mixedBinders, one theorem whose type mixes an implicit and an explicit ∀ over the same body: (∀ {a : Prop}, a → a) ∧ (∀ (a : Prop), a → a). A #guard_msgs dump of ci.type pins both forallE nodes with their distinct binderInfo values and the shared body.

The mixed-binder case fails on the old code: the explicit conjunct reuses the implicit forallE node at ie 5, so the second binder is lost.

This can slightly increase export size because alpha-equivalent copies are no longer deduplicated, but it makes each declaration's export independent of export order.

Validation:

  • lake build passes.
  • lake test passes on leanprover/lean4:v4.36.0-rc1.
  • The pre-existing functionWithLet linter warning is unchanged.

@nyapicom

Copy link
Copy Markdown

Thanks for this fix. I hit the same issue (#58) and tested this branch (5169499). visitedExprs is fixed, but noMDataExprs is still a HashMap Expr Expr, keyed by Expr.eqv. removeMData runs on every exported type and value, and its cache is reset once per dumpExpr call, not per subterm. So alpha-equivalent
subterms with different binder info inside a single type still collide.

Repro (Lean v4.36.0-rc1):

theorem both : (∀ {a : Prop}, a → a) ∧ (∀ (a : Prop), a → a) := ⟨fun h => h, fun _ h => h⟩

With this branch, the exported type of both decodes to (∀ {a}, a → a) ∧ (∀ {a}, a → a): the second conjunct's explicit binder comes out implicit.

Keying noMDataExprs on ExprKey as well fixes it, and lake test still passes:

-  noMDataExprs : HashMap Expr Expr := HashMap.emptyWithCapacity 100000
+  noMDataExprs : HashMap ExprKey Expr := HashMap.emptyWithCapacity 100000
...
-  if let some x := (← get).noMDataExprs[e]? then
+  if let some x := (← get).noMDataExprs[ExprKey.mk e]? then
...
-  modify (fun st => { st with noMDataExprs := st.noMDataExprs.insert e e' })
+  modify (fun st => { st with noMDataExprs := st.noMDataExprs.insert (ExprKey.mk e) e' })

With both maps keyed this way, the type of both exports as (∀ {a}, a → a) ∧ (∀ (a), a → a).

`noMDataExprs` was still keyed by `Expr`, whose `BEq` is alpha-equivalence,
so alpha-equivalent subterms that differ only in binder info shared one
cache entry inside a single type. Key it on `ExprKey` as well and cover a
type with mixed implicit and explicit binders.
@JoAdo07

JoAdo07 commented Nov 27, 2026

Copy link
Copy Markdown
Author

Thanks for the detailed repro — you tested 5169499, which only covered visitedExprs.

ad0cfd0 keys noMDataExprs by the same ExprKey (BEq = Expr.equal), and adds your mixed-binder case as a regression test: mixedBinders : (∀ {a : Prop}, a → a) ∧ (∀ (a : Prop), a → a), with a #guard_msgs dump of ci.type that pins the implicit and explicit forallE nodes separately.

With Export.lean reverted, that test fails exactly as you described — the explicit conjunct reuses the implicit node at ie 5. With the fix, lake build and lake test pass on v4.36.0-rc1 (47 s). Branch head is ad0cfd0.

@nyapicom

Copy link
Copy Markdown

@JoAdo07 Thanks! I re-tested at ad0cfd0: build and tests pass, and the Mathlib export that hit this bug now comes out correct on our side. Thanks for adding the regression test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exported binder names and binder info depend on what else is exported

2 participants