Skip to content

Commit a4ee316

Browse files
kim-emkmillnomeata
authored
feat: verification of qsort via grind (#7995)
This PR adds a verification of `Array.qsort` properties, trying to use `grind` and `fun_induction` where possible. Currently this is in the `tests/` folder, but once `grind` is ready for production use we will move it out into the library. Note that the current `qsort` algorithm has quadratic behaviour on constant lists, and needs to be adjusted. We'll only move the verification out into the library once this has been fixed (and the proofs adapted). These verification theorems may be commented out in the meantime if it's urgent to fix `qsort`. --------- Co-authored-by: Kyle Miller <[email protected]> Co-authored-by: Joachim Breitner <[email protected]>
1 parent 01dbbee commit a4ee316

File tree

3 files changed

+435
-1
lines changed

3 files changed

+435
-1
lines changed

src/Init/Data/Array/QSort/Basic.lean

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ def qpartition {n} (as : Vector α n) (lt : α → α → Bool) (lo hi : Nat)
3737
let as := if lt as[hi] as[lo] then as.swap lo hi else as
3838
let as := if lt as[mid] as[hi] then as.swap mid hi else as
3939
let pivot := as[hi]
40+
-- During this loop, elements below in `[lo, i)` are less than `pivot`,
41+
-- elements in `[i, j)` are greater than or equal to `pivot`,
42+
-- elements in `[j, hi)` are unexamined,
43+
-- while `as[hi]` is (by definition) the pivot.
4044
let rec loop (as : Vector α n) (i j : Nat)
4145
(ilo : lo ≤ i := by omega) (jh : j < n := by omega) (w : i ≤ j := by omega) :=
4246
if h : j < hi then
@@ -60,8 +64,11 @@ In-place quicksort.
6064
if h₁ : lo < hi then
6165
let ⟨⟨mid, hmid⟩, as⟩ := qpartition as lt lo hi
6266
if h₂ : mid ≥ hi then
67+
-- This only occurs when `hi ≤ lo`,
68+
-- and thus `as[low:high+1]` is trivially already sorted.
6369
as
6470
else
71+
-- Otherwise, we recursively sort the two subarrays.
6572
sort (sort as lo mid) (mid+1) hi
6673
else as
6774
if h : as.size = 0 then

src/Init/Grind/Tactics.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ structure Config where
9898
This approach is cheaper but incomplete. -/
9999
qlia : Bool := false
100100
/--
101-
If `mbtc` is `true`, `grind` will use model-based theory commbination for creating new case splits.
101+
If `mbtc` is `true`, `grind` will use model-based theory combination for creating new case splits.
102102
See paper "Model-based Theory Combination" for details.
103103
-/
104104
mbtc : Bool := true

0 commit comments

Comments
 (0)