lean4-htt/tests/lean/run/structuralEqns4.lean
Joachim Breitner 8655f7706f
refactor: structural recursion: prove .eq_def directly (#10606)
This PR changes how Lean proves the equational theorems for structural
recursion. The core idea is to let-bind the `f` argument to `brecOn` and
rewriting `.brecOn` with an unfolding theorem. This means no extra case
split for the `.rec` in `.brecOn` is needed, and `simp` doesn't change
the `f` argument which can break the definitional equality with the
defined function. With this, we can prove the unfolding theorem first,
and derive the equational theorems from that, like for all other ways of
defining recursive functions.

Backs out the changes from #10415, the old strategy works well with the
new goals.

Fixes #5667
Fixes #10431
Fixes #10195
Fixes #2962
2025-10-07 12:53:09 +00:00

44 lines
1,020 B
Text

inductive N where | zero | succ (n : N)
/--
info: protected theorem N.brecOn.eq.{u} : ∀ {motive : N → Sort u} (t : N) (F_1 : (t : N) → N.below t → motive t),
N.brecOn t F_1 = F_1 t (N.brecOn.go t F_1).2
-/
#guard_msgs in #print sig N.brecOn.eq
-- set_option trace.Elab.definition.structural.eqns true
def g (i : Nat) (j : N) : N :=
if i < 5 then .zero else
match j with
| .zero => N.zero.succ
| .succ j => g i j
termination_by structural j
/--
info: theorem g.eq_def : ∀ (i : Nat) (j : N),
g i j =
if i < 5 then N.zero
else
match j with
| N.zero => N.zero.succ
| j.succ => g i j
-/
#guard_msgs(pass trace, all) in #print sig g.eq_def
def N.beq : (m n : N) → Bool
| .zero, .zero => true
| .succ m, .succ n => N.beq m n
| _, _ => false
/--
info: theorem N.beq.eq_def : ∀ (x x_1 : N),
x.beq x_1 =
match x, x_1 with
| N.zero, N.zero => true
| m.succ, n.succ => m.beq n
| x, x_2 => false
-/
#guard_msgs(pass trace, all) in #print sig N.beq.eq_def