| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- The core definition of a sorting algorithm |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
open import Relation.Binary.Bundles using (TotalOrder) |
| 10 |
|
| 11 |
module Data.List.Sort.Base |
| 12 |
{a ℓ₁ ℓ₂} (totalOrder : TotalOrder a ℓ₁ ℓ₂) |
| 13 |
where |
| 14 |
|
| 15 |
open import Data.List.Base using (List) |
| 16 |
open import Data.List.Relation.Binary.Permutation.Propositional |
| 17 |
using (_↭_; ↭⇒↭ₛ) |
| 18 |
import Data.List.Relation.Binary.Permutation.Homogeneous as Homo |
| 19 |
open import Level using (_⊔_) |
| 20 |
|
| 21 |
open TotalOrder totalOrder renaming (Carrier to A) |
| 22 |
open import Data.List.Relation.Unary.Sorted.TotalOrder totalOrder |
| 23 |
import Data.List.Relation.Binary.Permutation.Setoid Eq.setoid as S |
| 24 |
|
| 25 |
------------------------------------------------------------------------ |
| 26 |
-- Definition of a sorting algorithm |
| 27 |
|
| 28 |
record SortingAlgorithm : Set (a ⊔ ℓ₁ ⊔ ℓ₂) where |
| 29 |
field |
| 30 |
sort : List A → List A |
| 31 |
|
| 32 |
-- The output of `sort` is a propositional permutation of the input. |
| 33 |
-- Note that the choice of using a propositional equality here |
| 34 |
-- is very deliberate. A sorting algorithm should only be capable |
| 35 |
-- of altering the order of the elements of the list, and should not |
| 36 |
-- be capable of altering the elements themselves in any way. |
| 37 |
sort-↭ : ∀ xs → sort xs ↭ xs |
| 38 |
|
| 39 |
-- The output of `sort` is sorted. |
| 40 |
sort-↗ : ∀ xs → Sorted (sort xs) |
| 41 |
|
| 42 |
-- Lists are also permutations under the setoid versions of |
| 43 |
-- permutation. |
| 44 |
sort-↭ₛ : ∀ xs → sort xs S.↭ xs |
| 45 |
sort-↭ₛ xs = Homo.map Eq.reflexive (↭⇒↭ₛ (sort-↭ xs)) |