-- IMPLEMENT and VERIFY the CORRECTNESS of INSERTION SORT -- Two different techniques: -- (1) first implement, then verify the correctness -- (2) correct by construction infixr 5 _∷_ data List (A : Set) : Set where [] : List A _∷_ : A → List A → List A data _⊎_ (X Y : Set) : Set where inj₁ : X → X ⊎ Y inj₂ : Y → X ⊎ Y -- alternatively: open import Data.Sum module Implementation (A : Set) (_≤_ : A → A → Set) (cmp : (x y : A) → (x ≤ y) ⊎ (y ≤ x)) where -- The following functions inserts an element into a list (which is -- assumed to be sorted) at the correct position. -- Invariant: If the input list to "insert" is sorted, then so is the output list. insert : A → List A → List A insert x [] = x ∷ [] insert x (y ∷ ys) with cmp x y ... | inj₁ x≤y = x ∷ y ∷ ys ... | inj₂ y≤x = y ∷ insert x ys sort : List A → List A sort [] = [] sort (x ∷ xs) = insert x (sort xs) -- First part of verification: "sort" returns ordered lists. module Verification₁ (A : Set) (_≤_ : A → A → Set) (cmp : (x y : A) → (x ≤ y) ⊎ (y ≤ x)) where open Implementation A _≤_ cmp data IsSorted : List A → Set where empty : IsSorted [] singleton : {x : A} → IsSorted (x ∷ []) cons : {x y : A} {ys : List A} → x ≤ y → IsSorted (y ∷ ys) → IsSorted (x ∷ y ∷ ys) -- alternative approach : {x : A} {ys : List A} → IsSorted ys → x ≤* ys → IsSorted (x ∷ ys), -- where _≤*_ is a predicate expressing that the left argument is -- less than or equal to all the elements of the right argument lemma₀ : (x y : A) (ys : List A) → y ≤ x → IsSorted (y ∷ ys) → IsSorted (y ∷ insert x ys) lemma₀ x y [] y≤x p = cons y≤x singleton lemma₀ x y (y' ∷ ys) y≤x (cons y≤y' p') with cmp x y' ... | inj₁ x≤y' = cons y≤x (cons x≤y' p') ... | inj₂ y'≤x = cons y≤y' (lemma₀ x y' ys y'≤x p') lemma : (x : A) (ys : List A) → IsSorted ys → IsSorted (insert x ys) lemma x [] p = singleton lemma x (y ∷ ys) p with cmp x y ... | inj₁ x≤y = cons x≤y p ... | inj₂ y≤x = lemma₀ x y ys y≤x p theorem : (xs : List A) → IsSorted (sort xs) theorem [] = empty theorem (x ∷ xs) = lemma x (sort xs) (theorem xs) -- There is still an issue: We only verified that "sort" returns -- sorted lists, but we did not verify that "sort xs" is a -- permutation of xs. The following function also returns sorted lists ... cheatSort : List A → List A cheatSort xs = [] -- ... as evidenced by this proof: trivial-theorem : (xs : List A) → IsSorted (cheatSort xs) trivial-theorem xs = empty -- Second part of verification: "sort xs" is a permutation of the input list "xs". module Verification₂ (A : Set) (_≤_ : A → A → Set) (cmp : (x y : A) → (x ≤ y) ⊎ (y ≤ x)) where open Implementation A _≤_ cmp -- The type "x ◂ xs ↝ xys" is the type of witnesses that inserting the element x -- somewhere into the list xs yields the list xys. data _◂_↝_ : A → List A → List A → Set where here : {x : A} {xs : List A} → (x ◂ xs ↝ (x ∷ xs)) there : {x y : A} {ys xys : List A} → (x ◂ ys ↝ xys) → (x ◂ (y ∷ ys) ↝ (y ∷ xys)) data IsPerm : List A → List A → Set where empty : IsPerm [] [] cons : {x : A} {xs ys xys : List A} → (x ◂ ys ↝ xys) → IsPerm xs ys → IsPerm (x ∷ xs) xys lemma : (x : A) (ys : List A) → x ◂ ys ↝ insert x ys lemma x [] = here lemma x (y ∷ ys) with cmp x y ... | inj₁ x≤y = here ... | inj₂ y≤x = there (lemma x ys) theorem : (xs : List A) → IsPerm xs (sort xs) theorem [] = empty theorem (x ∷ xs) = cons (lemma x (sort xs)) (theorem xs) -- The second approach to correctness: correct by construction. -- Here without regard to permutations. module CorrectByConstruction₁ (A : Set) (_≤_ : A → A → Set) (cmp : (x y : A) → (x ≤ y) ⊎ (y ≤ x)) (min : A) (min≤ : {x : A} → min ≤ x) where -- "OList l" is the type of ordered lists of elements of A such that l -- is less than or equal to all the elements in the list. -- More succinctly: "OList l" is the type of redered lists bounded from below by l. data OList (l : A) : Set where nil : OList l cons : (x : A) → l ≤ x → OList x → OList l -- too strict: cons : (x : A) → OList x → OList x insert : {l : A} → (x : A) → l ≤ x → OList l → OList l insert x l≤x nil = cons x l≤x nil insert x l≤x (cons y l≤y ys) with cmp x y ... | inj₁ x≤y = cons x l≤x (cons y x≤y ys) ... | inj₂ y≤x = cons y l≤y (insert x y≤x ys) sort : List A → OList min sort [] = nil sort (x ∷ xs) = insert x min≤ (sort xs) -- Now full correctness, including a proof-by-construction that -- sorting computes a permutation of the input list. module CorrectByConstruction₂ (A : Set) (_≤_ : A → A → Set) (cmp : (x y : A) → (x ≤ y) ⊎ (y ≤ x)) (min : A) (min≤ : {x : A} → min ≤ x) (trans : {x y z : A} → x ≤ y → y ≤ z → x ≤ z) where -- The type "x ◂ xs ↝ xys" is the type of witnesses that inserting the element x -- somewhere into the list xs yields the list xys. data _◂_↝_ : A → List A → List A → Set where here : {x : A} {xs : List A} → (x ◂ xs ↝ (x ∷ xs)) there : {x y : A} {ys xys : List A} → (x ◂ ys ↝ xys) → (x ◂ (y ∷ ys) ↝ (y ∷ xys)) -- "OPList l xs" is the type of ordered lists which are -- (a) bounded by l and -- (b) a permutation of xs. data OPList (l : A) : List A → Set where nil : OPList l [] cons : {ys xys : List A} → (x : A) → OPList x ys → l ≤ x → (x ◂ ys ↝ xys) → OPList l xys insert : {l : A} {zs : List A} → (x : A) → OPList l zs → l ≤ x → OPList l (x ∷ zs) insert x nil l≤x = cons x nil l≤x here insert x (cons y ys l≤y p) l≤x with cmp x y ... | inj₁ x≤y = cons x (cons y ys x≤y p) l≤x here ... | inj₂ y≤x = cons y (insert x ys y≤x) l≤y (there p) sort : (xs : List A) → OPList min xs sort [] = nil sort (x ∷ xs) = insert x (sort xs) min≤ data Inhabited : List A → Set where have : {x : A} {xs : List A} → Inhabited (x ∷ xs) _∧_ : A → A → A x ∧ y with cmp x y ... | inj₁ _ = x ... | inj₂ _ = y data BList (l : A) : Set where nil : BList l cons : (x : A) → BList l → l ≤ x → BList l toList : {l : A} → BList l → List A toList nil = [] toList (cons x xs _) = x ∷ toList xs record Split (xs : List A) : Set where field elem : A rest : BList elem proof : elem ◂ (toList rest) ↝ xs trans* : {x y : A} → x ≤ y → BList y → BList x trans* p nil = nil trans* p (cons z zs y≤z) = cons z (trans* p zs) (trans p y≤z) minimum : (xs : List A) → Inhabited xs → Split xs minimum (x ∷ []) p = record { elem = x ; rest = nil ; proof = here } minimum (x ∷ x' ∷ xs) p with minimum (x' ∷ xs) have ... | record { elem = e ; rest = rest ; proof = proof } with cmp x e ... | inj₁ x≤e = record { elem = x ; rest = cons e {!trans* x≤e rest!} x≤e ; proof = {!!} } ... | inj₂ e≤x = record { elem = e ; rest = cons x {!!} {!!} ; proof = {!!} } select : (xs : List A) → Inhabited xs → OPList min xs select xs p with minimum xs p ... | record { elem = e ; rest = rest ; proof = proof } = cons e {!!} min≤ {!!} {- ADDITIONAL EXERCISES: (1) The modules CorrectByConstruction₁ and CorrectByConstruction₂ require a least element "min". Define for any type A together with a relation _≤_ on A a new type "* A" which is A adjoined by a new least element -∞. Use this construction to get rid of the requirement. (2) Verify in a similar manner the correctness of your favorite sorting algorithm. (Might be somewhat involved, depending on your choice of algorithm.) -} {- data Dec (A : Set) : Set where yes : A → Dec A no : (A → ⊥) → Dec A -- "Dec A" is the type of witnesses that either A or not A holds, that is, that A is decidable. minimum : (P : ℕ → Set) → ((n : ℕ) → Dec (P n)) → (n : ℕ) → P n → ℕ minimum P in? zero p = zero minimum P in? (succ n) p with in? zero ... | yes q = zero ... | no _ = succ (minimum (λ k → P (succ k)) (λ k → in? (succ k)) n p) has-P : (P : ℕ → Set) → (in? : (n : ℕ) → Dec (P n)) → (n : ℕ) → (p : P n) → P (minimum P in? n p) has-P P in? zero p = p has-P P in? (succ n) p with in? zero ... | yes q = q ... | no _ = has-P (λ k → P (succ k)) (λ k → in? (succ k)) n p min-P : (P : ℕ → Set) → (in? : (n : ℕ) → Dec (P n)) → (n : ℕ) → (p : P n) → (m : ℕ) → P m → minimum P in? n p ≤ m min-P P in? zero p m q = base min-P P in? (succ n) p m q with in? zero ... | yes r = base min-P P in? (succ n) p zero q | no r with r q ... | () min-P P in? (succ n) p (succ m) q | no r = step (min-P (λ k → P (succ k)) (λ k → in? (succ k)) n p m q) -}