| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- An implementation of merge sort along with proofs of correctness. |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
-- Unless you are need a particular property of MergeSort, you should |
| 8 |
-- import and use the sorting algorithm from `Data.List.Sort` instead |
| 9 |
-- of this file. |
| 10 |
|
| 11 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 12 |
|
| 13 |
open import Relation.Binary.Bundles using (DecTotalOrder) |
| 14 |
|
| 15 |
module Data.List.Sort.MergeSort.Properties |
| 16 |
{a ℓ₁ ℓ₂} (O : DecTotalOrder a ℓ₁ ℓ₂) where |
| 17 |
|
| 18 |
open import Data.Bool.Base using (true; false) |
| 19 |
open import Data.List.Base |
| 20 |
using (List; []; _∷_; merge; length; map; [_]; concat; _++_) |
| 21 |
open import Data.List.Properties using (length-partition; ++-assoc; concat-map-[_]) |
| 22 |
open import Data.List.Relation.Unary.Linked using ([]; [-]) |
| 23 |
import Data.List.Relation.Unary.Sorted.TotalOrder.Properties as Sorted |
| 24 |
open import Data.List.Relation.Unary.All as All using (All; []; _∷_) |
| 25 |
import Data.List.Relation.Unary.All.Properties as All |
| 26 |
open import Data.List.Relation.Binary.Permutation.Propositional |
| 27 |
import Data.List.Relation.Binary.Permutation.Propositional.Properties as Perm |
| 28 |
open import Data.Maybe.Base using (just) |
| 29 |
open import Data.Nat.Base using (_<_; _>_; z<s; s<s) |
| 30 |
open import Data.Nat.Induction |
| 31 |
open import Data.Nat.Properties using (m<n⇒m<1+n) |
| 32 |
open import Data.Product.Base as Product using (_,_) |
| 33 |
open import Function.Base using (_∘_) |
| 34 |
open import Relation.Nullary.Negation.Core using (¬_) |
| 35 |
open import Relation.Nullary.Decidable.Core using (does) |
| 36 |
|
| 37 |
open DecTotalOrder O renaming (Carrier to A) |
| 38 |
|
| 39 |
open import Data.List.Sort.Base totalOrder |
| 40 |
open import Data.List.Sort.MergeSort.Base O |
| 41 |
open import Data.List.Relation.Unary.Sorted.TotalOrder totalOrder hiding (head) |
| 42 |
open import Relation.Binary.Properties.DecTotalOrder O using (≰⇒≥; ≰-respˡ-≈) |
| 43 |
|
| 44 |
open PermutationReasoning |
| 45 |
|
| 46 |
------------------------------------------------------------------------ |
| 47 |
-- Permutation property |
| 48 |
|
| 49 |
mergePairs-↭ : ∀ xss → concat (mergePairs xss) ↭ concat xss |
| 50 |
mergePairs-↭ [] = ↭-refl |
| 51 |
mergePairs-↭ (xs ∷ []) = ↭-refl |
| 52 |
mergePairs-↭ (xs ∷ ys ∷ xss) = begin |
| 53 |
merge _ xs ys ++ concat (mergePairs xss) ↭⟨ Perm.++⁺ (Perm.merge-↭ _ xs ys) (mergePairs-↭ xss) ⟩ |
| 54 |
(xs ++ ys) ++ concat xss ≡⟨ ++-assoc xs ys (concat xss) ⟩ |
| 55 |
xs ++ ys ++ concat xss ∎ |
| 56 |
|
| 57 |
mergeAll-↭ : ∀ xss (rec : Acc _<_ (length xss)) → mergeAll xss rec ↭ concat xss |
| 58 |
mergeAll-↭ [] _ = ↭-refl |
| 59 |
mergeAll-↭ (xs ∷ []) _ = ↭-sym (Perm.++-identityʳ xs) |
| 60 |
mergeAll-↭ (xs ∷ ys ∷ xss) (acc rec) = begin |
| 61 |
mergeAll (mergePairs (xs ∷ ys ∷ xss)) _ ↭⟨ mergeAll-↭ (mergePairs (xs ∷ ys ∷ xss)) _ ⟩ |
| 62 |
concat (mergePairs (xs ∷ ys ∷ xss)) ↭⟨ mergePairs-↭ (xs ∷ ys ∷ xss) ⟩ |
| 63 |
concat (xs ∷ ys ∷ xss) ∎ |
| 64 |
|
| 65 |
sort-↭ : ∀ xs → sort xs ↭ xs |
| 66 |
sort-↭ xs = begin |
| 67 |
mergeAll (map [_] xs) _ ↭⟨ mergeAll-↭ (map [_] xs) _ ⟩ |
| 68 |
concat (map [_] xs) ≡⟨ concat-map-[ xs ] ⟩ |
| 69 |
xs ∎ |
| 70 |
|
| 71 |
------------------------------------------------------------------------ |
| 72 |
-- Sorted property |
| 73 |
|
| 74 |
mergePairs-↗ : ∀ {xss} → All Sorted xss → All Sorted (mergePairs xss) |
| 75 |
mergePairs-↗ [] = [] |
| 76 |
mergePairs-↗ (xs↗ ∷ []) = xs↗ ∷ [] |
| 77 |
mergePairs-↗ (xs↗ ∷ ys↗ ∷ xss↗) = Sorted.merge⁺ O xs↗ ys↗ ∷ mergePairs-↗ xss↗ |
| 78 |
|
| 79 |
mergeAll-↗ : ∀ {xss} (rec : Acc _<_ (length xss)) → |
| 80 |
All Sorted xss → Sorted (mergeAll xss rec) |
| 81 |
mergeAll-↗ rec [] = [] |
| 82 |
mergeAll-↗ rec (xs↗ ∷ []) = xs↗ |
| 83 |
mergeAll-↗ (acc rec) (xs↗ ∷ ys↗ ∷ xss↗) = mergeAll-↗ _ (mergePairs-↗ (xs↗ ∷ ys↗ ∷ xss↗)) |
| 84 |
|
| 85 |
sort-↗ : ∀ xs → Sorted (sort xs) |
| 86 |
sort-↗ xs = mergeAll-↗ _ (All.map⁺ (All.universal (λ _ → [-]) xs)) |
| 87 |
|
| 88 |
------------------------------------------------------------------------ |
| 89 |
-- Algorithm |
| 90 |
|
| 91 |
mergeSort : SortingAlgorithm |
| 92 |
mergeSort = record |
| 93 |
{ sort = sort |
| 94 |
; sort-↭ = sort-↭ |
| 95 |
; sort-↗ = sort-↗ |
| 96 |
} |