Skip to content

feat: add contextDependent to Sym.simp Result with two-tier cache#12996

Merged
leodemoura merged 5 commits intomasterfrom
lean-sym-interactive-2
Mar 20, 2026
Merged

feat: add contextDependent to Sym.simp Result with two-tier cache#12996
leodemoura merged 5 commits intomasterfrom
lean-sym-interactive-2

Conversation

@leodemoura
Copy link
Copy Markdown
Member

This PR adds per-result contextDependent tracking to Sym.Simp.Result and splits the simplifier cache into persistent (context-independent) and transient (context-dependent, cleared on binder entry). This replaces the coarse wellBehavedMethods flag.

Key changes:

  • Add contextDependent : Bool := false to Result.rfl and Result.step
  • Split State.cache into persistentCache and transientCache
  • Remove wellBehavedMethods from Methods
  • Replace withoutModifyingCacheIfNotWellBehaved with withFreshTransientCache
  • Change DischargeResult to an inductive (.failed/.solved)
  • Add dischargeAssumption (context-dependent discharger for testing)
  • Add sym.simp.debug.cache trace class
  • Propagate contextDependent through all combinators (congruence, transitivity, control flow, arrows, rewriting)
  • Add mkRflResult/mkRflResultCD to avoid dynamic allocation of rfl results
  • Fix isRfl to ignore contextDependent (was silently broken by the extra field)

Propagation invariant: when combining sub-results, cd is the disjunction of ALL sub-results' flags — including .rfl results. If simp returned .rfl (contextDependent := true), it means simp might take a completely different code path in another local context, so all downstream results must be marked context-dependent.

leodemoura and others added 5 commits March 19, 2026 15:13
Replace the coarse `wellBehavedMethods` flag with per-result
`contextDependent : Bool` tracking. Split the simp cache into
`persistentCache` (context-independent results, survives binder entry)
and `transientCache` (context-dependent results, cleared on binder entry).

Propagate `contextDependent` through all combinators (congruence,
transitivity, control flow, arrows, rewriting). The invariant:
when combining sub-results, `cd` is the disjunction of all sub-results'
flags — including `.rfl` results, since `simp` might take a completely
different code path in another local context.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Tests cover:
1. Ground evaluation → persistent cache hit
2. Conditional rewrite with dischargeAssumption → transient only
3. Congruence with mixed cd/non-cd sub-results → cd propagates
4. Arrow/implication → cd propagates through domain simplification
5. Lambda/funext → cd propagates through body under binder
6. Control flow (ite) → cd propagates through condition
When  fails (no matching hypothesis), it returns
`.failed true`. This cd propagates through the rewrite result, so
`n + 2` lands in the transient cache even though no rewrite occurred.
Exercises `simpForall'` with `withFreshTransientCache` — the body
`n + 2 = 2 + n` is simplified context-dependently inside the binder.
The binder type `Nat` hits persistent cache on second traversal.
isRfl was using `matches .rfl` which only matched when ALL fields
have default values. With the new contextDependent field, .rfl false true
(cd=true) no longer matched, causing Theorems.rewrite to incorrectly
return cd .rfl results instead of continuing to the next theorem.

Fix: match `.rfl false _` (done=false, ignore cd).

Add unit tests for mkEqTransResult, andThen, orElse cd propagation.
@leodemoura leodemoura requested a review from kim-em as a code owner March 20, 2026 00:04
@leodemoura leodemoura added changelog-language Language features and metaprograms changelog-tactics User facing tactics and removed changelog-language Language features and metaprograms labels Mar 20, 2026
@leodemoura leodemoura enabled auto-merge March 20, 2026 00:06
@leodemoura leodemoura added this pull request to the merge queue Mar 20, 2026
Merged via the queue into master with commit d2907b5 Mar 20, 2026
31 checks passed
@github-actions github-actions bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Mar 20, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 58ef418dda4be3508d3070bd2f48b9a76848dbfe --onto 61a3443a9569d548a235f785b9a33984bb7ff622. You can force Mathlib CI using the force-mathlib-ci label. (2026-03-20 00:53:07)

@leanprover-bot
Copy link
Copy Markdown
Collaborator

Reference manual CI status:

  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase 58ef418dda4be3508d3070bd2f48b9a76848dbfe --onto cfa8c5a036d6990635c6ec50b02d0e806995cec3. You can force reference manual CI using the force-manual-ci label. (2026-03-20 00:53:09)

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

Labels

changelog-tactics User facing tactics toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants