-
Notifications
You must be signed in to change notification settings - Fork 2
/
Nondeterminism.agda
379 lines (350 loc) · 16.3 KB
/
Nondeterminism.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
{-# OPTIONS --rewriting #-}
module Examples.Decalf.Nondeterminism where
open import Algebra.Cost
costMonoid = ℕ-CostMonoid
open CostMonoid costMonoid using (ℂ; _+_)
open import Calf costMonoid hiding (A)
open import Calf.Data.Nat as Nat using (nat; zero; suc; _*_)
import Data.Nat.Properties as Nat
open import Data.Nat.Square
open import Calf.Data.List as List using (list; []; _∷_; [_]; _++_; length)
import Data.Fin as Fin
open import Calf.Data.Bool using (bool; false; true; if_then_else_)
open import Calf.Data.Product using (unit; _×⁺_)
open import Calf.Data.Equality as Eq using (_≡_; refl; module ≡-Reasoning)
open import Calf.Data.IsBoundedG costMonoid
open import Calf.Data.IsBounded costMonoid
open import Relation.Nullary
open import Function
postulate
branch : (X : tp⁻) → cmp X → cmp X → cmp X
fail : (X : tp⁻) → cmp X
branch/idˡ : {e : cmp X} →
branch X (fail X) e ≡ e
branch/idʳ : {e : cmp X} →
branch X e (fail X) ≡ e
branch/assoc : {e₀ e₁ e₂ : cmp X} →
branch X (branch X e₀ e₁) e₂ ≡ branch X e₀ (branch X e₁ e₂)
branch/comm : {e₀ e₁ : cmp X} →
branch X e₀ e₁ ≡ branch X e₁ e₀
branch/idem : {e : cmp X} →
branch X e e ≡ e
branch/step : (c : ℂ) {e₀ e₁ : cmp X} →
step X c (branch X e₀ e₁) ≡ branch X (step X c e₀) (step X c e₁)
fail/step : (c : ℂ) →
step X c (fail X) ≡ fail X
bind/fail : {A : tp⁺} {f : val A → cmp X} →
bind X (fail (F A)) f ≡ fail X
bind/branch : {A : tp⁺} {e₀ e₁ : cmp (F A)} {f : val A → cmp X} →
bind X (branch (F A) e₀ e₁) f ≡ branch X (bind X e₀ f) (bind X e₁ f)
{-# REWRITE bind/fail bind/branch #-}
open import Examples.Sorting.Sequential.Comparable
module QuickSort (M : Comparable) where
open Comparable M
open import Examples.Sorting.Sequential.Core M
choose : cmp $ Π (list A) λ l → F (Σ⁺ A λ pivot → Σ⁺ (list A) λ l' → meta⁺ (l ↭ pivot ∷ l'))
choose [] = fail (F _)
choose (x ∷ xs) =
branch (F _)
(bind (F _) (choose xs) λ (pivot , l , xs↭pivot∷l) → ret (pivot , x ∷ l , trans (prep x xs↭pivot∷l) (swap x pivot refl)))
(ret (x , xs , refl))
choose/cost : cmp $ Π (list A) λ _ → cost
choose/cost l = ret triv
choose/is-bounded : ∀ x xs → IsBoundedG _ (choose (x ∷ xs)) (choose/cost (x ∷ xs))
choose/is-bounded x [] = ≤⁻-reflexive branch/idˡ
choose/is-bounded x (x' ∷ xs) =
let open ≤⁻-Reasoning cost in
begin
branch (F unit) (bind (F unit) (choose (x' ∷ xs)) λ _ → ret triv) (ret triv)
≲⟨ ≤⁻-mono (λ e → branch (F unit) (bind (F unit) e λ _ → ret triv) (ret triv)) (choose/is-bounded x' xs) ⟩
branch (F unit) (ret triv) (ret triv)
≡⟨ branch/idem ⟩
ret triv
∎
partition : cmp $ Π A λ pivot → Π (list A) λ l → F (Σ⁺ (list A) λ l₁ → Σ⁺ (list A) λ l₂ → meta⁺ (All (_≤ pivot) l₁) ×⁺ meta⁺ (All (pivot ≤_) l₂) ×⁺ meta⁺ (l₁ ++ l₂ ↭ l))
partition pivot [] = ret ([] , [] , [] , [] , refl)
partition pivot (x ∷ xs) =
bind (F _) (partition pivot xs) λ (xs₁ , xs₂ , h₁ , h₂ , xs₁++xs₂↭xs) →
bind (F _) (x ≤? pivot) $ case-≤
(λ x≤pivot → ret (x ∷ xs₁ , xs₂ , x≤pivot ∷ h₁ , h₂ , prep x xs₁++xs₂↭xs))
(λ x≰pivot → ret (xs₁ , x ∷ xs₂ , h₁ , ≰⇒≥ x≰pivot ∷ h₂ , trans (shift-↭ x xs₁ xs₂) (prep x xs₁++xs₂↭xs)))
partition/cost : cmp $ Π A λ a → Π (list A) λ l → cost
partition/cost _ l = step⋆ (length l)
partition/is-bounded : ∀ pivot l → IsBoundedG _ (partition pivot l) (partition/cost pivot l)
partition/is-bounded pivot [] = ≤⁻-refl
partition/is-bounded pivot (x ∷ xs) =
let open ≤⁻-Reasoning cost in
begin
( bind (F unit) (partition pivot xs) λ (xs₁ , xs₂ , h₁ , h₂ , xs₁++xs₂↭xs) →
bind (F unit) (x ≤? pivot) λ x≤?pivot →
bind {Σ⁺ (list A) λ l₁ → Σ⁺ (list A) λ l₂ → meta⁺ (All (_≤ pivot) l₁) ×⁺ meta⁺ (All (pivot ≤_) l₂) ×⁺ meta⁺ (l₁ ++ l₂ ↭ x ∷ xs)} (F unit)
( case-≤
(λ x≤pivot → ret (x ∷ xs₁ , xs₂ , x≤pivot ∷ h₁ , h₂ , prep x xs₁++xs₂↭xs))
(λ x≰pivot → ret (xs₁ , x ∷ xs₂ , h₁ , ≰⇒≥ x≰pivot ∷ h₂ , trans (shift-↭ x xs₁ xs₂) (prep x xs₁++xs₂↭xs)))
x≤?pivot
)
(λ _ → ret triv)
)
≡⟨
( Eq.cong (bind (F unit) (partition pivot xs)) $ funext λ (xs₁ , xs₂ , h₁ , h₂ , xs₁++xs₂↭xs) →
Eq.cong (bind (F unit) (x ≤? pivot)) $ funext $
bind/case-≤
{B = Σ⁺ (list A) λ l₁ → Σ⁺ (list A) λ l₂ → meta⁺ (All (_≤ pivot) l₁) ×⁺ meta⁺ (All (pivot ≤_) l₂) ×⁺ meta⁺ (l₁ ++ l₂ ↭ x ∷ xs)}
{X = F unit}
{f = λ _ → ret triv}
(λ x≤pivot → ret (x ∷ xs₁ , xs₂ , x≤pivot ∷ h₁ , h₂ , prep x xs₁++xs₂↭xs))
(λ x≰pivot → ret (xs₁ , x ∷ xs₂ , h₁ , ≰⇒≥ x≰pivot ∷ h₂ , trans (shift-↭ x xs₁ xs₂) (prep x xs₁++xs₂↭xs)))
)
⟩
( bind (F unit) (partition pivot xs) λ _ →
bind (F unit) (x ≤? pivot) $ case-≤
(λ _ → ret triv)
(λ _ → ret triv)
)
≡⟨
( Eq.cong (bind (F unit) (partition pivot xs)) $ funext λ (xs₁ , xs₂ , h₁ , h₂ , xs₁++xs₂↭xs) →
Eq.cong (bind (F unit) (x ≤? pivot)) $ funext $
case-≤/idem (ret triv)
)
⟩
( bind (F unit) (partition pivot xs) λ _ →
bind (F unit) (x ≤? pivot) λ _ →
ret triv
)
≲⟨
( ≤⁻-mono
{Π (Σ⁺ (list A) λ l₁ → Σ⁺ (list A) λ l₂ → meta⁺ (All (_≤ pivot) l₁) ×⁺ meta⁺ (All (pivot ≤_) l₂) ×⁺ meta⁺ (l₁ ++ l₂ ↭ xs)) λ _ → F unit}
(bind (F unit) (partition pivot xs)) $
λ-mono-≤⁻ λ _ →
h-cost x pivot
)
⟩
( bind (F unit) (partition pivot xs) λ _ →
step⋆ 1
)
≡⟨⟩
( bind (F unit) (bind (F unit) (partition pivot xs) λ _ → ret triv) λ _ →
step⋆ 1
)
≲⟨ ≤⁻-mono (λ e → bind (F unit) (bind (F unit) e λ _ → ret triv) λ _ → step (F unit) 1 (ret triv)) (partition/is-bounded pivot xs) ⟩
( bind (F unit) (step (F unit) (length xs) (ret triv)) λ _ →
step⋆ 1
)
≡⟨⟩
step⋆ (length xs + 1)
≡⟨ Eq.cong step⋆ (Nat.+-comm (length xs) 1) ⟩
step⋆ (length (x ∷ xs))
∎
{-# TERMINATING #-}
sort : cmp $ Π (list A) λ _ → F (list A)
sort [] = ret []
sort (x ∷ xs) =
bind (F _) (choose (x ∷ xs)) λ (pivot , l , x∷xs↭pivot∷l) →
bind (F _) (partition pivot l) λ (l₁ , l₂ , h₁ , h₂ , l₁++l₂↭l) →
bind (F _) (sort l₁) λ l₁' →
bind (F _) (sort l₂) λ l₂' →
ret (l₁' ++ [ x ] ++ l₂')
sort/cost : cmp $ Π (list A) λ _ → cost
sort/cost l = step⋆ (length l ²)
sort/arithmetic : (m n : val nat) → m ² + n ² Nat.≤ (m + n) ²
sort/arithmetic m n =
let open Nat.≤-Reasoning in
begin
m ² + n ²
≤⟨ Nat.+-mono-≤ (Nat.m≤m+n (m * m) (n * m)) (Nat.m≤n+m (n * n) (m * n)) ⟩
(m * m + n * m) + (m * n + n * n)
≡˘⟨ Eq.cong₂ _+_ (Nat.*-distribʳ-+ m m n) (Nat.*-distribʳ-+ n m n) ⟩
(m + n) * m + (m + n) * n
≡˘⟨ Nat.*-distribˡ-+ (m + n) m n ⟩
(m + n) * (m + n)
∎
{-# TERMINATING #-}
sort/is-bounded : ∀ l → IsBoundedG _ (sort l) (sort/cost l)
sort/is-bounded [] = ≤⁻-refl
sort/is-bounded (x ∷ xs) =
let open ≤⁻-Reasoning cost in
begin
( bind (F _) (choose (x ∷ xs)) λ (pivot , l , x∷xs↭pivot∷l) →
bind (F _) (partition pivot l) λ (l₁ , l₂ , h₁ , h₂ , l₁++l₂↭l) →
bind (F _) (sort l₁) λ _ →
bind (F _) (sort l₂) λ _ →
ret triv
)
≲⟨
( ≤⁻-mono
{Π (Σ⁺ A λ pivot → Σ⁺ (list A) λ l' → meta⁺ (x ∷ xs ↭ pivot ∷ l')) λ _ → F unit}
{F unit}
(bind (F unit) (choose (x ∷ xs)))
{λ (pivot , l , x∷xs↭pivot∷l) →
bind (F _) (partition pivot l) λ (l₁ , l₂ , h₁ , h₂ , l₁++l₂↭l) →
bind (F _) (sort l₁) λ _ →
bind (F _) (sort l₂) λ _ →
ret triv}
{λ (pivot , l , x∷xs↭pivot∷l) →
bind (F _) (partition pivot l) λ (l₁ , l₂ , h₁ , h₂ , l₁++l₂↭l) →
bind (F _) (sort l₁) λ _ →
step⋆ (length l₂ ²)} $
λ-mono-≤⁻ λ (pivot , l , x∷xs↭pivot∷l) →
≤⁻-mono
{Π (Σ⁺ (list A) λ l₁ → Σ⁺ (list A) λ l₂ → meta⁺ (All (_≤ pivot) l₁) ×⁺ meta⁺ (All (pivot ≤_) l₂) ×⁺ meta⁺ (l₁ ++ l₂ ↭ l)) λ _ → F unit}
{F unit}
(bind (F unit) (partition pivot l)) $
λ-mono-≤⁻ λ (l₁ , l₂ , h₁ , h₂ , l₁++l₂↭l) →
≤⁻-mono (λ e → bind (F unit) (sort l₁) λ _ → e) $
sort/is-bounded l₂
)
⟩
( bind (F _) (choose (x ∷ xs)) λ (pivot , l , x∷xs↭pivot∷l) →
bind (F _) (partition pivot l) λ (l₁ , l₂ , h₁ , h₂ , l₁++l₂↭l) →
bind (F _) (sort l₁) λ _ →
step⋆ (length l₂ ²)
)
≲⟨
( ≤⁻-mono
{Π (Σ⁺ A λ pivot → Σ⁺ (list A) λ l' → meta⁺ (x ∷ xs ↭ pivot ∷ l')) λ _ → F unit}
{F unit}
(bind (F _) (choose (x ∷ xs)))
{λ (pivot , l , x∷xs↭pivot∷l) →
bind (F _) (partition pivot l) λ (l₁ , l₂ , h₁ , h₂ , l₁++l₂↭l) →
bind (F _) (sort l₁) λ _ →
step⋆ (length l₂ ²)}
{λ (pivot , l , x∷xs↭pivot∷l) →
bind (F _) (partition pivot l) λ (l₁ , l₂ , h₁ , h₂ , l₁++l₂↭l) →
step⋆ (length l₁ ² + length l₂ ²)} $
λ-mono-≤⁻ λ (pivot , l , x∷xs↭pivot∷l) →
≤⁻-mono
{Π (Σ⁺ (list A) λ l₁ → Σ⁺ (list A) λ l₂ → meta⁺ (All (_≤ pivot) l₁) ×⁺ meta⁺ (All (pivot ≤_) l₂) ×⁺ meta⁺ (l₁ ++ l₂ ↭ l)) λ _ → F unit}
{F unit}
(bind (F _) (partition pivot l)) $
λ-mono-≤⁻ λ (l₁ , l₂ , h₁ , h₂ , l₁++l₂↭l) →
bind-irr-monoˡ-≤⁻ (sort/is-bounded l₁)
)
⟩
( bind (F _) (choose (x ∷ xs)) λ (pivot , l , x∷xs↭pivot∷l) →
bind (F _) (partition pivot l) λ (l₁ , l₂ , h₁ , h₂ , l₁++l₂↭l) →
step⋆ (length l₁ ² + length l₂ ²)
)
≲⟨
( ≤⁻-mono
{Π (Σ⁺ A λ pivot → Σ⁺ (list A) λ l' → meta⁺ (x ∷ xs ↭ pivot ∷ l')) λ _ → F unit}
{F unit}
(bind (F _) (choose (x ∷ xs)))
{λ (pivot , l , x∷xs↭pivot∷l) →
bind (F _) (partition pivot l) λ (l₁ , l₂ , h₁ , h₂ , l₁++l₂↭l) →
step⋆ (length l₁ ² + length l₂ ²)}
{λ (pivot , l , x∷xs↭pivot∷l) →
bind (F _) (partition pivot l) λ _ →
step⋆ (length l ²)} $
λ-mono-≤⁻ λ (pivot , l , x∷xs↭pivot∷l) →
≤⁻-mono
{Π (Σ⁺ (list A) λ l₁ → Σ⁺ (list A) λ l₂ → meta⁺ (All (_≤ pivot) l₁) ×⁺ meta⁺ (All (pivot ≤_) l₂) ×⁺ meta⁺ (l₁ ++ l₂ ↭ l)) λ _ → F unit}
{F unit}
(bind (F _) (partition pivot l)) $
λ-mono-≤⁻ λ (l₁ , l₂ , h₁ , h₂ , l₁++l₂↭l) →
≤⁺-mono step⋆ $
≤⇒≤⁺ (Nat.≤-trans (sort/arithmetic (length l₁) (length l₂)) (Nat.≤-reflexive (Eq.cong _² (Eq.trans (Eq.sym (length-++ l₁)) (↭-length l₁++l₂↭l)))))
)
⟩
( bind (F _) (choose (x ∷ xs)) λ (pivot , l , x∷xs↭pivot∷l) →
bind (F _) (partition pivot l) λ _ →
step⋆ (length l ²)
)
≲⟨
( ≤⁻-mono
{Π (Σ⁺ A λ pivot → Σ⁺ (list A) λ l' → meta⁺ (x ∷ xs ↭ pivot ∷ l')) λ _ → F unit}
{F unit}
(bind (F _) (choose (x ∷ xs)))
{λ (pivot , l , x∷xs↭pivot∷l) →
bind (F _) (partition pivot l) λ _ →
step⋆ (length l ²)}
{λ (pivot , l , x∷xs↭pivot∷l) →
step⋆ (length l + length l ²)} $
λ-mono-≤⁻ λ (pivot , l , x∷xs↭pivot∷l) →
bind-irr-monoˡ-≤⁻ (partition/is-bounded pivot l)
)
⟩
( bind (F _) (choose (x ∷ xs)) λ (pivot , l , x∷xs↭pivot∷l) →
step⋆ (length l + length l ²)
)
≡˘⟨
( Eq.cong (bind (F _) (choose (x ∷ xs))) $ funext λ (pivot , l , x∷xs↭pivot∷l) →
Eq.cong (λ c → step⋆ (c + c ²)) {length xs} {length l} (Eq.cong Nat.pred (↭-length x∷xs↭pivot∷l))
)
⟩
( bind (F _) (choose (x ∷ xs)) λ _ →
step⋆ (length xs + length xs ²)
)
≲⟨ bind-irr-monoˡ-≤⁻ (choose/is-bounded x xs) ⟩
step⋆ (length xs + length xs ²)
≲⟨ step⋆-mono-≤⁻ (Nat.+-mono-≤ (Nat.n≤1+n (length xs)) (Nat.*-monoʳ-≤ (length xs) (Nat.n≤1+n (length xs)))) ⟩
step⋆ (length (x ∷ xs) + length xs * length (x ∷ xs))
≡⟨⟩
step⋆ (length (x ∷ xs) ²)
∎
module Lookup {A : tp⁺} where
lookup : cmp $ Π (list A) λ _ → Π nat λ _ → F A
lookup [] i = fail (F _)
lookup (x ∷ xs) zero = ret x
lookup (x ∷ xs) (suc i) = step (F _) 1 (lookup xs i)
lookup/bound : cmp $ Π (list A) λ _ → Π nat λ _ → F A
lookup/bound l i with i Nat.<? length l
... | yes p = step (F _) i (ret (List.lookup l (Fin.fromℕ< p)))
... | no _ = fail (F _)
lookup/is-bounded : (l : val (list A)) (i : val nat) → lookup l i ≤⁻[ F A ] lookup/bound l i
lookup/is-bounded l i with i Nat.<? length l
... | yes p = lemma l i p
where
lemma : (l : val (list A)) (i : val nat) (p : i Nat.< length l) → lookup l i ≤⁻[ F A ] step (F _) i (ret (List.lookup l (Fin.fromℕ< p)))
lemma (x ∷ xs) zero (Nat.s≤s Nat.z≤n) = ≤⁻-refl
lemma (x ∷ xs) (suc i) (Nat.s≤s p) = ≤⁻-mono (step (F _) 1) (lemma xs i p)
... | no ¬p = lemma l i (Nat.≮⇒≥ ¬p)
where
lemma : (l : val (list A)) (i : val nat) → i Nat.≥ length l → lookup l i ≤⁻[ F A ] fail (F A)
lemma [] i Nat.z≤n = ≤⁻-refl
lemma (x ∷ xs) (suc i) (Nat.s≤s p) =
let open ≤⁻-Reasoning (F _) in
begin
step (F _) 1 (lookup xs i)
≲⟨ ≤⁻-mono (step (F _) 1) (lemma xs i p) ⟩
step (F _) 1 (fail (F _))
≡⟨ fail/step 1 ⟩
fail (F _)
∎
module Pervasive where
e : cmp $ F bool
e =
branch (F bool)
(step (F bool) 3 (ret true))
(step (F bool) 12 (ret false))
e/is-bounded : e ≤⁻[ F bool ] step (F bool) 12 (branch (F bool) (ret true) (ret false))
e/is-bounded =
let open ≤⁻-Reasoning (F bool) in
begin
e
≡⟨⟩
branch (F bool)
(step (F bool) 3 (ret true))
(step (F bool) 12 (ret false))
≲⟨
≤⁻-mono
(λ e → branch (F bool) e (step (F bool) 12 (ret false)))
(step-monoˡ-≤⁻ {F bool} (ret true) (Nat.s≤s (Nat.s≤s (Nat.s≤s Nat.z≤n))))
⟩
branch (F bool)
(step (F bool) 12 (ret true))
(step (F bool) 12 (ret false))
≡˘⟨ branch/step 12 ⟩
step (F bool) 12 (branch (F bool) (ret true) (ret false))
∎
e/is-bounded' : IsBounded bool e 12
e/is-bounded' =
let open ≤⁻-Reasoning (F unit) in
begin
bind (F unit) e (λ _ → ret triv)
≲⟨ ≤⁻-mono (λ e → bind (F _) e (λ _ → ret triv)) e/is-bounded ⟩
bind (F unit) (step (F bool) 12 (branch (F bool) (ret true) (ret false))) (λ _ → ret triv)
≡⟨⟩
step (F unit) 12 (branch (F unit) (ret triv) (ret triv))
≡⟨ Eq.cong (step (F unit) 12) branch/idem ⟩
step⋆ 12
∎