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.
130 lines
3.5 KiB
Text
130 lines
3.5 KiB
Text
/-!
|
||
Tests around the special case of well-founded recursion on Nat.
|
||
-/
|
||
|
||
set_option warn.sorry false
|
||
|
||
namespace T1
|
||
|
||
@[semireducible]
|
||
def foo : List α → Nat
|
||
| [] => 0
|
||
| _::xs => 1 + (foo xs)
|
||
termination_by xs => xs.length
|
||
|
||
-- Closed terms should evaluate
|
||
|
||
example : foo ([] : List Unit) = 0 := rfl
|
||
example : foo ([] : List Unit) = 0 := by decide
|
||
example : foo ([] : List Unit) = 0 := by decide +kernel
|
||
example : foo [1,2,3,4,5] = 5 := rfl
|
||
example : foo [1,2,3,4,5] = 5 := by decide
|
||
example : foo [1,2,3,4,5] = 5 := by decide +kernel
|
||
|
||
-- Open terms should not (these wouldn't even without the provisions with `WellFounded.Nat.eager`,
|
||
-- the fuel does not line up)
|
||
|
||
example : foo (x::xs) = 1 + foo xs := by (fail_if_success rfl); simp [foo]
|
||
example : foo (x::y::z::xs) = 1+ (1+(1+ foo xs)) := by (fail_if_success rfl); simp [foo]
|
||
|
||
end T1
|
||
|
||
-- Variant where the fuel does not line up
|
||
|
||
namespace T2
|
||
@[semireducible] def foo : List α → Nat
|
||
| [] => 0
|
||
| _::xs => 1 + (foo xs)
|
||
termination_by xs => 2 * xs.length
|
||
|
||
example : foo ([] : List Unit) = 0 := rfl
|
||
example : foo ([] : List Unit) = 0 := by decide
|
||
example : foo ([] : List Unit) = 0 := by decide +kernel
|
||
example : foo [1,2,3,4,5] = 5 := rfl
|
||
example : foo [1,2,3,4,5] = 5 := by decide
|
||
example : foo [1,2,3,4,5] = 5 := by decide +kernel
|
||
|
||
-- Open terms should not (these wouldn't even without the provisions, the fuel does not line up)
|
||
|
||
example : foo (x::xs) = 1 + foo xs := by (fail_if_success rfl); simp [foo]
|
||
example : foo (x::y::z::xs) = 1+ (1 + ( 1+ foo xs)) := by (fail_if_success rfl); simp [foo]
|
||
|
||
end T2
|
||
|
||
-- Idiom to switch to `WellFounded.fix`
|
||
|
||
namespace T3
|
||
def foo : List α → Nat
|
||
| [] => 0
|
||
| _::xs => 1 + (foo xs)
|
||
termination_by xs => (xs.length, 0)
|
||
|
||
example : foo ([] : List Unit) = 0 := by (fail_if_success rfl); simp [foo]
|
||
example : foo ([] : List Unit) = 0 := by (fail_if_success decide); simp [foo]
|
||
example : foo ([] : List Unit) = 0 := by (fail_if_success decide +kernel); simp [foo]
|
||
example : foo [1,2,3,4,5] = 5 := by (fail_if_success rfl); simp [foo]
|
||
example : foo [1,2,3,4,5] = 5 := by (fail_if_success decide); simp [foo]
|
||
example : foo [1,2,3,4,5] = 5 := by (fail_if_success decide +kernel); simp [foo]
|
||
|
||
-- Open terms should not (these wouldn't even without the provisions, the fuel does not line up)
|
||
|
||
example : foo (x::xs) = 1 + foo xs := by (fail_if_success rfl); simp [foo]
|
||
example : foo (x::y::z::xs) = 1+ (1 + ( 1+ foo xs)) := by (fail_if_success rfl); simp [foo]
|
||
|
||
end T3
|
||
|
||
-- Defeq between similar functions
|
||
|
||
namespace T4
|
||
|
||
@[semireducible] def foo (b : Bool) : Nat → Nat
|
||
| 0 => 0
|
||
| n+1 => 1 + foo b n
|
||
termination_by n => n
|
||
|
||
@[semireducible] def bar (b : Bool) : Nat → Nat
|
||
| 0 => 0
|
||
| n+1 => cond b 1 2 + bar b n
|
||
termination_by n => n
|
||
|
||
@[semireducible] def baz : Nat → Nat
|
||
| 0 => 0
|
||
| n+1 => 1 + baz n
|
||
termination_by n => n
|
||
|
||
example : foo true n = bar true n := rfl
|
||
example : foo true n = baz n := rfl
|
||
example : bar true n = baz n := rfl
|
||
|
||
end T4
|
||
|
||
-- For comparison: with wfrec
|
||
|
||
namespace T4wfrec
|
||
|
||
def foo (b : Bool) : Nat → Nat
|
||
| 0 => 0
|
||
| n+1 => 1 + foo b n
|
||
termination_by n => (n, 0)
|
||
|
||
def bar (b : Bool) : Nat → Nat
|
||
| 0 => 0
|
||
| n+1 => cond b 1 2 + bar b n
|
||
termination_by n => (n, 0)
|
||
|
||
def baz : Nat → Nat
|
||
| 0 => 0
|
||
| n+1 => 1 + baz n
|
||
termination_by n => (n, 0)
|
||
|
||
example : foo true n = bar true n := by (fail_if_success rfl); sorry
|
||
example : foo true n = baz n := by (fail_if_success rfl); sorry
|
||
example : bar true n = baz n := by (fail_if_success rfl); sorry
|
||
|
||
unseal foo bar baz
|
||
|
||
example : foo true n = bar true n := rfl
|
||
example : foo true n = baz n := rfl
|
||
example : bar true n = baz n := rfl
|
||
|
||
end T4wfrec
|