This PR lets recursive functions defined by well-founded recursion use a different `fix` function when the termination measure is of type `Nat`. This fix-point operator use structural recursion on “fuel”, initialized by the given measure, and is thus reasonable to reduce, e.g. in `by decide` proofs. Extra provisions are in place that the fixpoint operator only starts reducing when the fuel is fully known, to prevent “accidential” defeqs when the remaining fuel for the recursive calls match the initial fuel for that recursive argument. To opt-out, the idiom `termination_by (n,0)` can be used. We still use `@[irreducible]` as the default for such recursive definitions, to avoid unexpected `defeq` lemmas. Making these functions `@[semireducible]` by default showed performance regressions in lean. When the measure is of type `Nat`, the system will accept an explicit `@[semireducible]` without the usual warning. Fixes #5234. Fixes: #11181.
146 lines
2.9 KiB
Text
146 lines
2.9 KiB
Text
|
||
def foo : Nat → Nat → Nat
|
||
| 0, m => match m with | 0 => 0 | m => m
|
||
| n+1, m => foo n m
|
||
termination_by n => n
|
||
|
||
/--
|
||
info: equations:
|
||
@[defeq] theorem foo.eq_1 : foo 0 0 = 0
|
||
theorem foo.eq_2 : ∀ (x : Nat), (x = 0 → False) → foo 0 x = x
|
||
theorem foo.eq_3 : ∀ (x n : Nat), foo n.succ x = foo n x
|
||
-/
|
||
#guard_msgs in
|
||
#print equations foo
|
||
|
||
/--
|
||
info: foo.eq_def (x✝ x✝¹ : Nat) :
|
||
foo x✝ x✝¹ =
|
||
match x✝, x✝¹ with
|
||
| 0, m =>
|
||
match m with
|
||
| 0 => 0
|
||
| m => m
|
||
| n.succ, m => foo n m
|
||
-/
|
||
#guard_msgs in
|
||
#check foo.eq_def
|
||
|
||
/-- error: Unknown identifier `foo.eq_4` -/
|
||
#guard_msgs in
|
||
#check foo.eq_4
|
||
|
||
/--
|
||
info: foo._unary.eq_def (_x : (_ : Nat) ×' Nat) :
|
||
foo._unary _x =
|
||
PSigma.casesOn _x fun a a_1 =>
|
||
match a, a_1 with
|
||
| 0, m =>
|
||
match m with
|
||
| 0 => 0
|
||
| m => m
|
||
| n.succ, m => foo._unary ⟨n, m⟩
|
||
-/
|
||
#guard_msgs in
|
||
#check foo._unary.eq_def
|
||
|
||
|
||
set_option backward.eqns.deepRecursiveSplit false in
|
||
def bar : Nat → Nat → Nat
|
||
| 0, m => match m with | 0 => 0 | m => m
|
||
| n+1, m => bar n m
|
||
termination_by n => n
|
||
|
||
/--
|
||
info: equations:
|
||
@[defeq] theorem bar.eq_1 : ∀ (x : Nat),
|
||
bar 0 x =
|
||
match x with
|
||
| 0 => 0
|
||
| m => m
|
||
theorem bar.eq_2 : ∀ (x n : Nat), bar n.succ x = bar n x
|
||
-/
|
||
#guard_msgs in
|
||
#print equations bar
|
||
|
||
/--
|
||
info: bar.eq_def (x✝ x✝¹ : Nat) :
|
||
bar x✝ x✝¹ =
|
||
match x✝, x✝¹ with
|
||
| 0, m =>
|
||
match m with
|
||
| 0 => 0
|
||
| m => m
|
||
| n.succ, m => bar n m
|
||
-/
|
||
#guard_msgs in
|
||
#check bar.eq_def
|
||
|
||
-- Now the same for structural recursion
|
||
|
||
namespace Structural
|
||
|
||
def foo : Nat → Nat → Nat
|
||
| 0, m => match m with | 0 => 0 | m => m
|
||
| n+1, m => foo n m
|
||
termination_by structural n => n
|
||
|
||
/--
|
||
info: equations:
|
||
@[defeq] theorem Structural.foo.eq_1 : foo 0 0 = 0
|
||
theorem Structural.foo.eq_2 : ∀ (x : Nat), (x = 0 → False) → foo 0 x = x
|
||
@[defeq] theorem Structural.foo.eq_3 : ∀ (x n : Nat), foo n.succ x = foo n x
|
||
-/
|
||
#guard_msgs in
|
||
#print equations foo
|
||
|
||
/--
|
||
info: Structural.foo.eq_def (x✝ x✝¹ : Nat) :
|
||
foo x✝ x✝¹ =
|
||
match x✝, x✝¹ with
|
||
| 0, m =>
|
||
match m with
|
||
| 0 => 0
|
||
| m => m
|
||
| n.succ, m => foo n m
|
||
-/
|
||
#guard_msgs in
|
||
#check foo.eq_def
|
||
|
||
/-- error: Unknown identifier `Structural.foo.eq_4` -/
|
||
#guard_msgs in
|
||
#check Structural.foo.eq_4
|
||
|
||
|
||
set_option backward.eqns.deepRecursiveSplit false in
|
||
def bar : Nat → Nat → Nat
|
||
| 0, m => match m with | 0 => 0 | m => m
|
||
| n+1, m => bar n m
|
||
termination_by n => n
|
||
|
||
/--
|
||
info: equations:
|
||
@[defeq] theorem Structural.bar.eq_1 : ∀ (x : Nat),
|
||
bar 0 x =
|
||
match x with
|
||
| 0 => 0
|
||
| m => m
|
||
theorem Structural.bar.eq_2 : ∀ (x n : Nat), bar n.succ x = bar n x
|
||
-/
|
||
#guard_msgs in
|
||
#print equations bar
|
||
|
||
/--
|
||
info: Structural.bar.eq_def (x✝ x✝¹ : Nat) :
|
||
bar x✝ x✝¹ =
|
||
match x✝, x✝¹ with
|
||
| 0, m =>
|
||
match m with
|
||
| 0 => 0
|
||
| m => m
|
||
| n.succ, m => bar n m
|
||
-/
|
||
#guard_msgs in
|
||
#check bar.eq_def
|
||
|
||
end Structural
|