简体   繁体   English

Agda编程-证明Insertionsort在大小为3的列表上进行了3个或更少的比较

[英]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. 我试图证明插入排序将在排序时在大小为3的列表中执行<= 3个比较。 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 . insertionsort过程中要进行的所有比较实际上都是在从属insert的过程中完成的。 It may help to establish a useful fact about the comparison cost of insert . 建立关于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. 如果您可以限制每个insert调用的费用,则应该能够将这些有限制的部分费用组合在一起,以构成有限制的总费用。 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: 这是针对您问题的Agda标准库的实现:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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