I am trying to create the mergesort definition in Lean and have created the following code:
def mergesort (a: ℕ): list ℕ → list ℕ
| [] := []
| [a] := [a]
| (x::xs) := merge (mergesort (fhalf xs)) (mergesort (sndhalf xs))
With the merge definition
def merge : list ℕ → list ℕ → list ℕ
| xs [] := xs
| [] ys := ys
| (x :: xs) (y :: ys) := if x < y then x :: (merge xs ( y :: ys))
else y :: (merge (x :: xs) ys)
And fhalf / sndhalf definitions:
def fhalf {α: Type} (xs: list α): list α :=
list.take (list.length xs/2) xs
def sndhalf {α: Type} (xs: list α): list α :=
list.drop (list.length xs/2) xs
However, I get the following error message:
failed to prove recursive application is decreasing, well founded relation
@has_well_founded.r (list ℕ) (@has_well_founded_of_has_sizeof (list ℕ) (@list.has_sizeof ℕ nat.has_sizeof))Possible solutions:
Use
using_well_foundedkeyword in the end of your definition to specify tactics for synthesizing well founded relations and decreasing proofs.The default decreasing tactic uses the 'assumption' tactic, thus hints (aka local proofs) can be provided using 'have'-expressions. The nested exception contains the failure state for the decreasing tactic.
Can anyone help me to prove that the mergesort is decreasing?