-
Notifications
You must be signed in to change notification settings - Fork 1
/
QS.agda
37 lines (32 loc) · 1.16 KB
/
QS.agda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
open import Data.Bool
open import Data.Nat
open import Data.List hiding (filter)
filter : ∀ {A : Set} → (A → Bool) → List A → List A
filter p [] = []
filter p (x ∷ xs) with (p x)
... | true = x ∷ filter p xs
... | false = filter p xs
{-
An accessibility predicate on lists (of naturals)
based on the recursive calls of quicksort.
-}
data Accqs : List ℕ → Set where
accnil : Accqs []
acccons : ∀ (x : ℕ) (xs : List ℕ) →
Accqs (filter (_<ᵇ x) xs) →
Accqs (filter (x ≤ᵇ_) xs) →
Accqs (x ∷ xs)
{- Wellfoundedness: All lists are accessible according to Accqs. -}
accqs : ∀ (xs : List ℕ) → Accqs xs
accqs [] = accnil
accqs (x ∷ xs) with (accqs xs)
... | accnil = acccons x [] accnil accnil
... | acccons y ys acc<ᵇ acc≤ᵇ = acccons x (y ∷ ys) {! !} {! !}
{- Quicksort by structural induction on Accqs. -}
quicksort : (xs : List ℕ) → Accqs xs → List ℕ
quicksort [] accnil = []
quicksort .(x ∷ xs) (acccons x xs acc<ᵇ acc≤ᵇ) =
(quicksort (filter (_<ᵇ x) xs) acc<ᵇ) ++
(quicksort (filter (x ≤ᵇ_) xs) acc≤ᵇ)
quicksorted : List ℕ → List ℕ
quicksorted xs = quicksort xs (accqs xs)