简体   繁体   中英

Agda Programming- Proving Insertionsort makes 3 or less comparisons on a list of size 3

Good Evening Fellows,

I am attempting to prove that insertionsort will perform <= 3 comparisons in a list of size 3 while sorting. Last part of my project and cannot make any headway on it. After spending fair amount of time pursuing an incorrect approach, my instructor informed me it may be accomplished by writing a helper function to assist. I unfortunately have not come up with any piece of code to help. If anyone can offer advice or assistance, any and all are appreciated. Code follows. Thanks!

insert : ℕ → 𝕃 ℕ → 𝕃 ℕ × ℕ 
insert x (h :: t) = if h < x then (x :: h :: t , 1) else let r = insert 
                                       x t  in h :: (fst r) , 1 + snd r
insert x [] = x :: [] , 0

insertionsort : 𝕃 ℕ → 𝕃 ℕ × ℕ 
insertionsort [] = [] , 0
insertionsort (h :: t) with insertionsort t
insertionsort (h :: t) | t' , c1 with insert h t' 
insertionsort (h :: t) | t' , c1 | r , c2 = r , c1 + c2

exampleThm : ∀(x y z c : ℕ)(r : 𝕃 ℕ) → insertionsort (x :: y :: z :: [])
                                                    ≡ r , c → c ≤ 3 ≡ tt 
exampleThm x y z = ?`

All the comparisons to be done in the course of insertionsort are actually done in the course of subordinate calls to insert . It may help to establish a useful fact about the comparison cost of insert . If you can bound the cost of each call to insert , you should be able to combine those bounded partial costs together to make a bounded total cost. In case your instructor is concerned that I am helping too much, let me summarize by saying that all I am saying is that the structure of the proof has to follow the structure of the program.

A general pattern when constructing proofs is to generalize them to make them easier. In this case I believe it is more clear to solve the generalized bound for the number of comparisons that insertion sort will do and then instantiate that to your particular input.

The structure of your proof will follow the structure of your program.

First we'll need to characterize the behavior of insert, since insertion sort is implemented in terms of it.

insert-bound : ∀ x ys → proj₂ (insert x ys) ≤ length ys

Then we'll use that to characterize the behavior of insertion sort

bound         : ℕ → ℕ
bound 0       = 0
bound (suc n) = bound n + n

insertionsort-bound : ∀ xs → proj₂ (insertionsort xs) ≤ bound (length xs)

Using the general solution we can solve the specific case of a three element list

exampleThm : ∀ x y z c r → insertionsort (x ∷ y ∷ z ∷ []) ≡ (r , c) → c ≤ 3
exampleThm x y z ._ ._ refl = insertionsort-bound (x ∷ y ∷ z ∷ [])

Here's an implementation against the Agda standard library of your problem:

http://www.galois.com/~emertens/insertionsort-agda/Insertionsort.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM