forked from jonsterling/agda-calf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InsertionSort.agda
192 lines (172 loc) · 7.99 KB
/
InsertionSort.agda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
{-# OPTIONS --prop --rewriting #-}
open import Examples.Sorting.Parallel.Comparable
module Examples.Sorting.Parallel.InsertionSort (M : Comparable) where
open Comparable M
open import Examples.Sorting.Parallel.Core M
open import Calf costMonoid
open import Calf.Types.Bool
open import Calf.Types.List
open import Calf.Types.Eq
open import Calf.Types.Bounded costMonoid
open import Calf.Types.BigO costMonoid
open import Relation.Nullary
open import Relation.Nullary.Negation
open import Relation.Binary.PropositionalEquality as Eq using (_≡_; refl; module ≡-Reasoning)
open import Data.Product using (_×_; _,_; ∃; proj₁; proj₂)
open import Data.Sum using (inj₁; inj₂)
open import Function
open import Data.Nat as Nat using (ℕ; zero; suc; z≤n; s≤s; _+_; _*_)
open import Data.Nat.Properties as N using (module ≤-Reasoning)
open import Data.Nat.Square
insert : cmp (Π A λ _ → Π (list A) λ _ → F (list A))
insert x [] = ret [ x ]
insert x (y ∷ ys) =
bind (F (list A)) (x ≤ᵇ y)
λ { false → bind (F (list A)) (insert x ys) (ret ∘ (y ∷_))
; true → ret (x ∷ (y ∷ ys)) }
insert/correct : ∀ x l → Sorted l → ◯ (∃ λ l' → insert x l ≡ ret l' × SortedOf (x ∷ l) l')
insert/correct x [] [] u = [ x ] , refl , refl , [] ∷ []
insert/correct x (y ∷ ys) (h ∷ hs) u with h-cost x y
insert/correct x (y ∷ ys) (h ∷ hs) u | ⇓ b withCost q [ _ , h-eq ] rewrite eq/ref h-eq
with ≤ᵇ-reflects-≤ u (Eq.trans (eq/ref h-eq) (step/ext (F bool) (ret b) q u)) | ≤-total x y
insert/correct x (y ∷ ys) (h ∷ hs) u | ⇓ false withCost q [ _ , _ ] | ofⁿ ¬x≤y | inj₁ x≤y = contradiction x≤y ¬x≤y
insert/correct x (y ∷ ys) (h ∷ hs) u | ⇓ false withCost q [ _ , _ ] | ofⁿ ¬x≤y | inj₂ x≤y =
let (ys' , h-ys' , x∷ys↭ys' , sorted-ys') = insert/correct x ys hs u in
y ∷ ys' , (
let open ≡-Reasoning in
begin
step (F (list A)) q (bind (F (list A)) (insert x ys) (ret ∘ (y ∷_)))
≡⟨ step/ext (F (list A)) (bind (F (list A)) (insert x ys) (ret ∘ (y ∷_))) q u ⟩
bind (F (list A)) (insert x ys) (ret ∘ (y ∷_))
≡⟨ Eq.cong (λ e → bind (F (list A)) e (ret ∘ (y ∷_))) h-ys' ⟩
ret (y ∷ ys')
∎
) , (
let open PermutationReasoning in
begin
x ∷ y ∷ ys
<<⟨ refl ⟩
y ∷ (x ∷ ys)
<⟨ x∷ys↭ys' ⟩
y ∷ ys'
∎
) , All-resp-↭ x∷ys↭ys' (x≤y ∷ h) ∷ sorted-ys'
insert/correct x (y ∷ ys) (h ∷ hs) u | ⇓ true withCost q [ _ , _ ] | ofʸ x≤y | _ =
x ∷ (y ∷ ys) , step/ext (F (list A)) (ret _) q u , refl , (x≤y ∷ ≤-≤* x≤y h) ∷ (h ∷ hs)
insert/cost : cmp (Π A λ _ → Π (list A) λ _ → cost)
insert/cost x [] = 𝟘
insert/cost x (y ∷ ys) with h-cost x y
... | ⇓ false withCost q [ q≤1 , h-eq ] = q ⊕ (insert/cost x ys ⊕ 𝟘)
... | ⇓ true withCost q [ q≤1 , h-eq ] = q ⊕ 𝟘
insert/cost/closed : cmp (Π A λ _ → Π (list A) λ _ → cost)
insert/cost/closed x l = length l , length l
insert/cost≤insert/cost/closed : ∀ x l → ◯ (insert/cost x l ≤ₚ insert/cost/closed x l)
insert/cost≤insert/cost/closed x [] u = ≤ₚ-refl
insert/cost≤insert/cost/closed x (y ∷ ys) u with h-cost x y
... | ⇓ false withCost q [ q≤1 , h-eq ] =
Eq.subst (λ n → (q ⊕ n) ≤ₚ (suc (length ys) , suc (length ys))) (Eq.sym (⊕-identityʳ (insert/cost x ys))) (
≤ₚ-trans
(⊕-monoˡ-≤ _ (q≤1 u))
(s≤s (proj₁ (insert/cost≤insert/cost/closed x ys u)) ,
s≤s (proj₂ (insert/cost≤insert/cost/closed x ys u)))
)
... | ⇓ true withCost q [ q≤1 , h-eq ] =
Eq.subst (_≤ₚ (suc (length ys) , suc (length ys))) (Eq.sym (⊕-identityʳ q)) (
≤ₚ-trans (q≤1 u) (s≤s z≤n , s≤s z≤n)
)
insert≤insert/cost : ∀ x l → IsBounded (list A) (insert x l) (insert/cost x l)
insert≤insert/cost x [] = bound/ret
insert≤insert/cost x (y ∷ ys) with h-cost x y
... | ⇓ false withCost q [ q≤1 , h-eq ] rewrite eq/ref h-eq =
bound/step q (insert/cost x ys ⊕ 𝟘) (bound/bind/const (insert/cost x ys) 𝟘 (insert≤insert/cost x ys) λ l → bound/ret {a = y ∷ l})
... | ⇓ true withCost q [ q≤1 , h-eq ] rewrite eq/ref h-eq =
bound/step q 𝟘 bound/ret
insert≤insert/cost/closed : ∀ x l → IsBounded (list A) (insert x l) (insert/cost/closed x l)
insert≤insert/cost/closed x l = bound/relax (insert/cost≤insert/cost/closed x l) (insert≤insert/cost x l)
sort : cmp (Π (list A) λ _ → F (list A))
sort [] = ret []
sort (x ∷ xs) = bind (F (list A)) (sort xs) (insert x)
sort/correct : IsSort sort
sort/correct [] u = [] , refl , refl , []
sort/correct (x ∷ xs) u =
let (xs' , h-xs' , xs↭xs' , sorted-xs' ) = sort/correct xs u in
let (x∷xs' , h-x∷xs' , x∷xs↭x∷xs' , sorted-x∷xs') = insert/correct x xs' sorted-xs' u in
x∷xs' , (
let open ≡-Reasoning in
begin
sort (x ∷ xs)
≡⟨⟩
bind (F (list A)) (sort xs) (insert x)
≡⟨ Eq.cong (λ e → bind (F (list A)) e (insert x)) h-xs' ⟩
bind (F (list A)) (ret {list A} xs') (insert x)
≡⟨⟩
insert x xs'
≡⟨ h-x∷xs' ⟩
ret x∷xs'
∎
) , (
let open PermutationReasoning in
begin
x ∷ xs
<⟨ xs↭xs' ⟩
x ∷ xs'
↭⟨ x∷xs↭x∷xs' ⟩
x∷xs'
∎
) , sorted-x∷xs'
sort/cost : cmp (Π (list A) λ _ → cost)
sort/cost [] = 𝟘
sort/cost (x ∷ xs) = bind cost (sort xs) (λ xs' → sort/cost xs ⊕ insert/cost/closed x xs')
sort/cost/closed : cmp (Π (list A) λ _ → cost)
sort/cost/closed l = length l ² , length l ²
sort/cost≤sort/cost/closed : ∀ l → ◯ (sort/cost l ≤ₚ sort/cost/closed l)
sort/cost≤sort/cost/closed [] u = ≤ₚ-refl
sort/cost≤sort/cost/closed (x ∷ xs) u =
let (xs' , h-xs' , xs↭xs' , sorted-xs' ) = sort/correct xs u in
let (x∷xs' , h-x∷xs' , x∷xs↭x∷xs' , sorted-x∷xs') = insert/correct x xs' sorted-xs' u in
let open ≤ₚ-Reasoning in
begin
sort/cost (x ∷ xs)
≡⟨⟩
bind cost (sort xs) (λ xs' → sort/cost xs ⊕ insert/cost/closed x xs')
≡⟨ Eq.cong (λ e → bind cost e λ xs' → sort/cost xs ⊕ insert/cost/closed x xs') h-xs' ⟩
sort/cost xs ⊕ insert/cost/closed x xs'
≡⟨⟩
sort/cost xs ⊕ (length xs' , length xs')
≡˘⟨ Eq.cong (sort/cost xs ⊕_) (Eq.cong₂ _,_ (↭-length xs↭xs') (↭-length xs↭xs')) ⟩
sort/cost xs ⊕ (length xs , length xs)
≤⟨ ⊕-monoˡ-≤ (length xs , length xs) (sort/cost≤sort/cost/closed xs u) ⟩
sort/cost/closed xs ⊕ (length xs , length xs)
≡⟨⟩
(length xs ² , length xs ²) ⊕ (length xs , length xs)
≤⟨ lemma/arithmetic (length xs) , lemma/arithmetic (length xs) ⟩
length (x ∷ xs) ² , length (x ∷ xs) ²
≡⟨⟩
sort/cost/closed (x ∷ xs)
∎
where
lemma/arithmetic : ∀ n → n ² + n Nat.≤ suc n ²
lemma/arithmetic n =
begin
n ² + n
≡⟨ N.+-comm (n ²) n ⟩
n + n ²
≡⟨⟩
n + n * n
≤⟨ N.m≤n+m (n + n * n) (suc n) ⟩
suc n + (n + n * n)
≡⟨⟩
suc (n + (n + n * n))
≡˘⟨ Eq.cong (λ m → suc (n + m)) (N.*-suc n n) ⟩
suc (n + n * suc n)
≡⟨⟩
suc n ²
∎
where open ≤-Reasoning
sort≤sort/cost : ∀ l → IsBounded (list A) (sort l) (sort/cost l)
sort≤sort/cost [] = bound/ret
sort≤sort/cost (x ∷ xs) = bound/bind (sort/cost xs) (insert/cost/closed x) (sort≤sort/cost xs) (insert≤insert/cost/closed x)
sort≤sort/cost/closed : ∀ l → IsBounded (list A) (sort l) (sort/cost/closed l)
sort≤sort/cost/closed l = bound/relax (sort/cost≤sort/cost/closed l) (sort≤sort/cost l)
sort/asymptotic : given (list A) measured-via length , sort ∈𝓞(λ n → n ² , n ²)
sort/asymptotic = 0 ≤n⇒f[n]≤g[n]via λ l _ → sort≤sort/cost/closed l