Files
lean4/tests/elab/mergeSortCPDT.lean
Garmelon 08eb78a5b2 chore: switch to new test/bench suite (#12590)
This PR sets up the new integrated test/bench suite. It then migrates
all benchmarks and some related tests to the new suite. There's also
some documentation and some linting.

For now, a lot of the old tests are left alone so this PR doesn't become
even larger than it already is. Eventually, all tests should be migrated
to the new suite though so there isn't a confusing mix of two systems.
2026-02-25 13:51:53 +00:00

51 lines
1.7 KiB
Lean4
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
def List.insert' (p : α α Bool) (a : α) (bs : List α) : List α :=
match bs with
| [] => [a]
| b :: bs' => if p a b then a :: bs else b :: bs'.insert' p a
def List.merge' (p : α α Bool) (as bs : List α) : List α :=
match as with
| [] => bs
| a :: as' => insert' p a (merge' p as' bs)
def List.split (as : List α) : List α × List α :=
match as with
| [] => ([], [])
| [a] => ([a], [])
| a :: b :: as =>
let (as, bs) := split as
(a :: as, b :: bs)
@[simp] def List.atLeast2 (as : List α) : Bool :=
match as with
| [] => false
| [_] => false
| _::_::_ => true
theorem List.length_split_of_atLeast2 {as : List α} (h : as.atLeast2) : as.split.1.length < as.length as.split.2.length < as.length := by
match as with
| [] => simp at h
| [_] => simp at h
| [_, _] => simp (config := { decide := true }) [split]
| [_, _, _] => simp (config := { decide := true }) [split]
| a::b::c::d::as =>
-- TODO: simplify using linear arith and more automation
have : (c::d::as).atLeast2 := by simp +arith
have ih := length_split_of_atLeast2 this
simp +arith [split] at ih |-
have ih₁, ih₂ := ih
exact Nat.le_trans ih₁ (by simp +arith), Nat.le_trans ih₂ (by simp +arith)
def List.mergeSort' (p : α α Bool) (as : List α) : List α :=
if h : as.atLeast2 then
match he:as.split with
| (as', bs') =>
-- TODO: simplify using more automation
have h₁, h₂ := length_split_of_atLeast2 h
have : as'.length < as.length := by simp [he] at h₁; assumption
have : bs'.length < as.length := by simp [he] at h₂; assumption
merge' p (mergeSort' p as') (mergeSort' p bs')
else
as
termination_by as.length