lean4-htt/tests/elab/6789.lean
Joachim Breitner ac9a1cb415
feat: add @[backward_defeq] attribute and local useBackward simp option (#13492)
This PR introduces stricter inference for the `@[defeq]` attribute and a
companion `@[backward_defeq]` attribute that preserves the pre-PR
behavior
as an opt-in.

### What changed

* `@[defeq]` is now inferred only when the equation holds at
  `.instances` transparency (the transparency `dsimp` operates at).
* `@[backward_defeq]` is the old set: every theorem whose `rfl` proof
the legacy inference would have accepted is tagged `@[backward_defeq]`,
  so `defeq ⊆ backward_defeq` holds by construction.
* The option `backward.defeqAttrib.useBackward` (default `false`) makes
  `dsimp` also use `@[backward_defeq]` theorems, restoring the pre-PR
  behavior for a specific proof or file.
* The option is eqn-affecting: its value at the point of a function's
  definition is recorded so that the equation lemmas later generated for
  that function use the same value, regardless of the ambient option at
  the use site.

### Mathlib adaption

A companion adaption branch (`lean-pr-testing-backward-defeq-attrib` on
mathlib4) builds cleanly against this PR and passes `lake test` without
warnings. Most adaption changes are scoped
`set_option backward.defeqAttrib.useBackward true in` additions on the
failing declarations; a small number of files needed proof-level edits
where the stored form of a `dsimp%`/`@[reassoc]`/`@[elementwise]`
/`@[simps]`/`@[to_app]`-generated lemma had drifted under the stricter
regime.

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-27 10:07:59 +00:00

62 lines
2.5 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/-!
# Ensure equational theorems generation doesn't fail when metadata is involved
https://github.com/leanprover/lean4/issues/6789
The original error would happen because `casesOnStuckLHS` (https://github.com/leanprover/lean4/blob/4ca98dcca2b0995dddff444cfef1f3ccc89c7b12/src/Lean/Meta/Match/MatchEqs.lean#L51)
would fail to find an fvar to do `cases` on when that fvar would be encapsulated by some metadata.
-/
inductive Con
| nil
| ext (Γ : Con) (n : Nat)
variable {Op : Con → Con → Type u}
inductive Extension : Con → Con → Type
| zero : Extension Γ Γ
| succ : Extension Γ Δ → (n : Nat) → Extension Γ (.ext Δ n)
def Extension.recOn'
{motive : (Γ Δ : Con) → Extension Γ Δ → Sort v}
(zero : {Γ : Con} → motive Γ Γ .zero)
(succ
: {Γ Δ : Con} → (xt : Extension Γ Δ)
→ (A : Nat)
→ motive Γ Δ xt
→ motive Γ (.ext Δ A) (.succ xt A))
: {Γ Δ : Con} → (xt : Extension Γ Δ) → motive Γ Δ xt
| _, _, .zero => zero
| _, _, .succ xt A => succ xt A (Extension.recOn' zero succ xt)
/--
info: equations:
theorem Extension.recOn'.eq_1.{v} : ∀ {motive : (Γ Δ : Con) → Extension Γ Δ → Sort v}
(zero : {Γ : Con} → motive Γ Γ Extension.zero)
(succ : {Γ Δ : Con} → (xt : Extension Γ Δ) → (A : Nat) → motive Γ Δ xt → motive Γ (Δ.ext A) (xt.succ A)) (x : Con),
Extension.recOn' zero succ Extension.zero = zero
@[backward_defeq] theorem Extension.recOn'.eq_2.{v} : ∀ {motive : (Γ Δ : Con) → Extension Γ Δ → Sort v}
(zero : {Γ : Con} → motive Γ Γ Extension.zero)
(succ : {Γ Δ : Con} → (xt : Extension Γ Δ) → (A : Nat) → motive Γ Δ xt → motive Γ (Δ.ext A) (xt.succ A)) (x Δ : Con)
(A : Nat) (xt : Extension x Δ),
Extension.recOn' zero succ (xt.succ A) = succ xt A (Extension.recOn' (fun {Γ} => zero) (fun {Γ Δ} => succ) xt)
-/
#guard_msgs in
#print equations Extension.recOn'
def Extension.pullback_con
: (xt : Extension B Δ) → (σ : Op B' B)
→ Con
| .zero, σ => B'
| .succ xt A, σ => .ext (pullback_con xt σ) A
/--
info: equations:
@[backward_defeq] theorem Extension.pullback_con.eq_1.{u} : ∀ {Op : Con → Con → Type u} {B B' : Con} (x : Op B' B),
Extension.zero.pullback_con x = B'
@[backward_defeq] theorem Extension.pullback_con.eq_2.{u} : ∀ {Op : Con → Con → Type u} {B B' : Con} (x : Op B' B)
(Δ_2 : Con) (xt : Extension B Δ_2) (A : Nat), (xt.succ A).pullback_con x = (xt.pullback_con x).ext A
-/
#guard_msgs in
#print equations Extension.pullback_con