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
76 lines
2.2 KiB
Text
76 lines
2.2 KiB
Text
inductive Tree (α : Type u) : Type u where
|
||
| node : α → (Bool → List (Tree α)) → Tree α
|
||
|
||
mutual
|
||
def Tree.size : Tree α → Nat
|
||
| .node _ tsf => 1 + size_aux (tsf true) + size_aux (tsf false)
|
||
termination_by structural t => t
|
||
def Tree.size_aux : List (Tree α) → Nat
|
||
| [] => 0
|
||
| t :: ts => size t + size_aux ts
|
||
end
|
||
|
||
/--
|
||
info: theorem Tree.size.eq_def.{u_1} : ∀ {α : Type u_1} (x : Tree α),
|
||
x.size =
|
||
match x with
|
||
| Tree.node a tsf => 1 + Tree.size_aux (tsf true) + Tree.size_aux (tsf false)
|
||
-/
|
||
#guard_msgs in
|
||
#print sig Tree.size.eq_def
|
||
|
||
/--
|
||
info: theorem Tree.size_aux.eq_def.{u_1} : ∀ {α : Type u_1} (x : List (Tree α)),
|
||
Tree.size_aux x =
|
||
match x with
|
||
| [] => 0
|
||
| t :: ts => t.size + Tree.size_aux ts
|
||
-/
|
||
#guard_msgs in
|
||
#print sig Tree.size_aux.eq_def
|
||
|
||
mutual
|
||
def Tree.size1 : Tree α → Nat
|
||
| .node _ tsf => 1 + size_aux2 (tsf true) + size_aux2 (tsf false)
|
||
termination_by structural t => t
|
||
def Tree.size2 : Tree α → Nat
|
||
| .node _ tsf => 1 + size_aux1 (tsf true) + size_aux1 (tsf false)
|
||
termination_by structural t => t
|
||
def Tree.size_aux1 : List (Tree α) → Nat
|
||
| [] => 0
|
||
| t :: ts => size2 t + size_aux2 ts
|
||
def Tree.size_aux2 : List (Tree α) → Nat
|
||
| [] => 0
|
||
| t :: ts => size1 t + size_aux1 ts
|
||
end
|
||
|
||
/--
|
||
info: theorem Tree.size1.eq_def.{u_1} : ∀ {α : Type u_1} (x : Tree α),
|
||
x.size1 =
|
||
match x with
|
||
| Tree.node a tsf => 1 + Tree.size_aux2 (tsf true) + Tree.size_aux2 (tsf false)
|
||
-/
|
||
#guard_msgs in #print sig Tree.size1.eq_def
|
||
/--
|
||
info: theorem Tree.size2.eq_def.{u_1} : ∀ {α : Type u_1} (x : Tree α),
|
||
x.size2 =
|
||
match x with
|
||
| Tree.node a tsf => 1 + Tree.size_aux1 (tsf true) + Tree.size_aux1 (tsf false)
|
||
-/
|
||
#guard_msgs in #print sig Tree.size2.eq_def
|
||
/--
|
||
info: theorem Tree.size_aux1.eq_def.{u_1} : ∀ {α : Type u_1} (x : List (Tree α)),
|
||
Tree.size_aux1 x =
|
||
match x with
|
||
| [] => 0
|
||
| t :: ts => t.size2 + Tree.size_aux2 ts
|
||
-/
|
||
#guard_msgs in #print sig Tree.size_aux1.eq_def
|
||
/--
|
||
info: theorem Tree.size_aux2.eq_def.{u_1} : ∀ {α : Type u_1} (x : List (Tree α)),
|
||
Tree.size_aux2 x =
|
||
match x with
|
||
| [] => 0
|
||
| t :: ts => t.size1 + Tree.size_aux1 ts
|
||
-/
|
||
#guard_msgs in #print sig Tree.size_aux2.eq_def
|