module InsertionSortLe where
open import Data.List.Base
open import Data.Nat.Base using (ℕ; zero; suc)
open import Logic
open import Le
insert : (x : ℕ) (ys : List ℕ) → List ℕ
insert x [] = x ∷ []
insert x (y ∷ ys) with ≤total x y
... | inl _ = x ∷ y ∷ ys
... | inr _ = y ∷ insert x ys
isort : (xs : List ℕ) → List ℕ
isort [] = []
isort (x ∷ xs) = insert x (isort xs)
module Naive-Ordered where
data Ordered : (ns : List ℕ) → prop where
onil : Ordered []
osg : ∀{n} → Ordered (n ∷ [])
ocons : ∀{n m ms} (p : n ≤ m) (o : Ordered (m ∷ ms)) → Ordered (n ∷ m ∷ ms)
data Ordered' (x : ℕ) : List ℕ → prop where
onil : Ordered' x []
ocons : ∀{y ys} (p : x ≤ y) (o : Ordered' y ys) → Ordered' x (y ∷ ys)
data Ordered : List ℕ → prop where
onil : Ordered []
ocons : ∀{x xs} (o : Ordered' x xs) → Ordered (x ∷ xs)
insert-ordered' : ∀ x y ys (p : x ≤ y) (o : Ordered' x ys) → Ordered' x (insert y ys)
insert-ordered' x y [] p o = ocons p onil
insert-ordered' x y (z ∷ ys) p (ocons p₁ o) with ≤total y z
... | inl q = ocons p (ocons q o)
... | inr q = ocons p₁ (insert-ordered' z y ys q o)
insert-ordered : ∀ x ys (o : Ordered ys) → Ordered (insert x ys)
insert-ordered x [] o = ocons onil
insert-ordered x (y ∷ ys) (ocons o) with ≤total x y
... | inl q = ocons (ocons q o)
... | inr q = ocons (insert-ordered' y x ys q o)
isort-ordered : ∀ ys → Ordered (isort ys)
isort-ordered [] = onil
isort-ordered (x ∷ ys) = insert-ordered x (isort ys) (isort-ordered ys)