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.
36 lines
831 B
Text
36 lines
831 B
Text
-- set_option trace.Elab.definition.eqns true
|
|
|
|
def f (x : Nat) : Nat :=
|
|
match x with
|
|
| 0 => 1
|
|
| 100 => 2
|
|
| 1000 => 3
|
|
| x+1 => f x
|
|
termination_by x
|
|
|
|
/--
|
|
info: equations:
|
|
@[defeq] theorem f.eq_1 : f 0 = 1
|
|
@[defeq] theorem f.eq_2 : f 100 = 2
|
|
@[defeq] theorem f.eq_3 : f 1000 = 3
|
|
theorem f.eq_4 : ∀ (x_2 : Nat), (x_2 = 99 → False) → (x_2 = 999 → False) → f x_2.succ = f x_2
|
|
-/
|
|
#guard_msgs(pass trace, all) in
|
|
#print equations f
|
|
|
|
def g (x : Nat) : Nat :=
|
|
match x with
|
|
| 0 => 1
|
|
| 100 => 2
|
|
| 1000 => 3
|
|
| x+1 => x
|
|
|
|
/--
|
|
info: equations:
|
|
@[defeq] theorem g.eq_1 : g 0 = 1
|
|
@[defeq] theorem g.eq_2 : g 100 = 2
|
|
@[defeq] theorem g.eq_3 : g 1000 = 3
|
|
theorem g.eq_4 : ∀ (x_2 : Nat), (x_2 = 99 → False) → (x_2 = 999 → False) → g x_2.succ = x_2
|
|
-/
|
|
#guard_msgs(pass trace, all) in
|
|
#print equations g
|