lean4-htt/tests/lean/run/1026.lean
Joachim Breitner f9dc77673b
feat: dedicated fix operator for well-founded recursion on Nat (#7965)
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.
2025-12-01 12:51:55 +00:00

35 lines
565 B
Text

def foo (n : Nat) : Nat :=
if n = 0 then 0 else
let x := n - 1
have := match () with | _ => trivial
foo x
termination_by n
decreasing_by sorry
theorem ex : foo 0 = 0 := by
unfold foo
sorry
/--
info: foo.eq_def (n : Nat) :
foo n =
if n = 0 then 0
else
have x := n - 1;
have this := foo._proof_3;
foo x
-/
#guard_msgs in
#check foo.eq_def
/--
info: foo.eq_unfold :
foo = fun n =>
if n = 0 then 0
else
have x := n - 1;
have this := foo._proof_3;
foo x
-/
#guard_msgs in
#check foo.eq_unfold