Skip to content

opt: fvar reuse optimization - #5

Open
rish987 wants to merge 1 commit into
digama0:masterfrom
rish987:fvar-reuse
Open

rish987 wants to merge 1 commit into
digama0:masterfrom
rish987:fvar-reuse

Conversation

@rish987

@rish987 rish987 commented Nov 5, 2024

Copy link
Copy Markdown
Contributor

This PR adds an optimization to the typechecker implementation to make better use of the EquivManager definitional equality cache by reusing free variables where possible.

As a motivating example, suppose we have defeq terms T and S containing free variable x, and we call isDefEq on fun (a : A) (b : B) => T[x := a] and fun (a : A) (b : B) => S[x := a]. This will cache the equality between T[x := f1] and S[x := f2], where f1 and f2 are new free variables generated upon entering the lambda binders for a and b.

However, if we later call isDefEq on fun (b : B) (a : A) => T[x := a] and fun (b : B) (a : A) => S[x := a], we will check the cache for T[x := f3] and S[x := f4], using the newly generated fvars f3 and f4, resulting in a miss. Ideally, we would want to have reused f1 and f2 here so that we could take advantage of the cached knowledge that T[x := f1] and S[x := f2] are defeq.

When entering into a forall or lambda binder of domain type D, it should be sound to reuse an fvar that was previously generated for D in a different context (as long as D has not already been bound in the current context). Any fvars appearing in D would also have to be reused fvars that were generated for previously seen domain types, and so on for the fvars in their types.

I've implemented this by replacing the NameGenerator in the typechecker state with a simple counter, and using a hashmap from domain type expressions to previously generated fvar names that is checked whenever a new fvar name needs to be generated upon entering a lambda or forall binder. With this optimization, Mathlib typechecks about 20% faster when Lean4Lean is run sequentially.

rish987 added a commit to rish987/lean4 that referenced this pull request Sep 14, 2026
This PR speeds up kernel typechecking by reusing free variable names across lambda and forall binders that share the same domain type, so that subterms recurring under different binders become structurally identical instead of differing only in a fresh variable name. This lets the kernel's definitional-equality cache reuse previously computed results far more often, including nontrivial facts about terms that are merely defeq rather than syntactically equal. On a full Mathlib replay the kernel is about 16% faster, with no change in which declarations are accepted.

When entering a binder of domain type `D`, the type checker now reuses a free variable name previously generated for `D` (picking the lowest index not currently in scope) rather than always generating a fresh one. Because a reused free variable consistently denotes a variable of type `D`, this is a consistent renaming of free variables that preserves definitional equality, and the in-scope check guarantees it never conflates two distinct variables. The checker state maps each domain type to a reusable name prefix; `let` binders keep using fresh names. Ported from the lean4lean fvar-reuse optimization (digama0/lean4lean#5).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QpT8QtSD8fb2RL9r59eXKx
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.

1 participant