forked from jonsterling/agda-calf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CostMonoids.agda
415 lines (389 loc) · 14.5 KB
/
CostMonoids.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
{-# OPTIONS --without-K #-}
-- Common cost monoids.
module Calf.CostMonoids where
open import Calf.CostMonoid
open import Data.Product
open import Function
open import Relation.Nullary.Negation
open import Relation.Binary.PropositionalEquality as Eq using (_≡_; refl; module ≡-Reasoning)
ℕ-CostMonoid : CostMonoid
ℕ-CostMonoid = record
{ ℂ = ℕ
; zero = zero
; _+_ = _+_
; _≤_ = _≤_
; isCostMonoid = record
{ isMonoid = +-0-isMonoid
; isPreorder = ≤-isPreorder
; isMonotone = record { ∙-mono-≤ = +-mono-≤ }
}
}
where
open import Data.Nat
open import Data.Nat.Properties
ℕ⊔-CostMonoid : CostMonoid
ℕ⊔-CostMonoid = record
{ ℂ = ℕ
; zero = zero
; _+_ = _⊔_
; _≤_ = _≤_
; isCostMonoid = record
{ isMonoid = ⊔-0-isMonoid
; isPreorder = ≤-isPreorder
; isMonotone = record { ∙-mono-≤ = ⊔-mono-≤ }
}
}
where
open import Data.Nat
open import Data.Nat.Properties
ℤ-CostMonoid : CostMonoid
ℤ-CostMonoid = record
{ ℂ = ℤ
; zero = 0ℤ
; _+_ = _+_
; _≤_ = _≤_
; isCostMonoid = record
{ isMonoid = +-0-isMonoid
; isPreorder = ≤-isPreorder
; isMonotone = record { ∙-mono-≤ = +-mono-≤ }
}
}
where
open import Data.Integer
open import Data.Integer.Properties
ℚ-CostMonoid : CostMonoid
ℚ-CostMonoid = record
{ ℂ = ℚ
; zero = 0ℚ
; _+_ = _+_
; _≤_ = _≤_
; isCostMonoid = record
{ isMonoid = +-0-isMonoid
; isPreorder = ≤-isPreorder
; isMonotone = record { ∙-mono-≤ = +-mono-≤ }
}
}
where
open import Data.Rational
open import Data.Rational.Properties
ResourceMonoid : CostMonoid
ResourceMonoid = record
{ ℂ = ℕ × ℕ
; zero = 0 , 0
; _+_ = _·_
; _≤_ = _≤ᵣ_
; isCostMonoid = record
{ isMonoid = record
{ isSemigroup = record
{ isMagma = record
{ isEquivalence = Eq.isEquivalence
; ∙-cong = Eq.cong₂ _·_
}
; assoc = assoc
}
; identity = identityˡ , identityʳ
}
; isPreorder = record
{ isEquivalence = Eq.isEquivalence
; reflexive = λ { refl → (≤-refl , ≤-refl) }
; trans = λ (h₁ , h₂) (h₁' , h₂') → ≤-trans h₁ h₁' , ≤-trans h₂' h₂
}
; isMonotone = record { ∙-mono-≤ = ∙-mono-≤ᵣ }
}
}
where
open import Data.Nat
open import Data.Nat.Properties
open import Data.Sum
open ≤-Reasoning
open import Algebra.Definitions {A = ℕ × ℕ} _≡_
open import Relation.Binary
_·_ : ℕ × ℕ → ℕ × ℕ → ℕ × ℕ
(p , p') · (q , q') = p + (q ∸ p') , q' + (p' ∸ q)
_≤ᵣ_ : ℕ × ℕ → ℕ × ℕ → Set
(p , p') ≤ᵣ (q , q') = (p ≤ q) × (q' ≤ p')
+-∸-comm′ : (m : ℕ) {n o : ℕ} → n ≤ o → (m + n) ∸ o ≡ m ∸ (o ∸ n)
+-∸-comm′ m {zero} {o} z≤n = Eq.cong (_∸ o) (+-identityʳ m)
+-∸-comm′ m {suc n} {suc o} (s≤s n≤o) = begin-equality
(m + suc n) ∸ suc o ≡⟨ Eq.cong (_∸ suc o) (+-suc m n) ⟩
suc (m + n) ∸ suc o ≡⟨ +-∸-comm′ m n≤o ⟩
m ∸ (o ∸ n) ∎
assoc : Associative _·_
assoc (p , p') (q , q') (r , r') with ≤-total p' q | ≤-total q' r
... | inj₁ p'≤q | inj₁ q'≤r =
Eq.cong₂ _,_
(begin-equality
(p + (q ∸ p')) + (r ∸ (q' + (p' ∸ q)))
≡⟨ Eq.cong (λ x → (p + (q ∸ p')) + (r ∸ (q' + x))) (m≤n⇒m∸n≡0 p'≤q) ⟩
(p + (q ∸ p')) + (r ∸ (q' + 0))
≡⟨ Eq.cong (λ x → (p + (q ∸ p')) + (r ∸ x)) (+-identityʳ q') ⟩
(p + (q ∸ p')) + (r ∸ q')
≡⟨ +-assoc p (q ∸ p') (r ∸ q') ⟩
p + ((q ∸ p') + (r ∸ q'))
≡˘⟨ Eq.cong (p +_) (+-∸-comm (r ∸ q') p'≤q) ⟩
p + ((q + (r ∸ q')) ∸ p')
∎)
(begin-equality
r' + ((q' + (p' ∸ q)) ∸ r)
≡⟨ Eq.cong (λ x → r' + ((q' + x) ∸ r)) (m≤n⇒m∸n≡0 p'≤q) ⟩
r' + ((q' + 0) ∸ r)
≡⟨ Eq.cong (λ x → r' + (x ∸ r)) (+-identityʳ q') ⟩
r' + (q' ∸ r)
≡˘⟨ +-identityʳ (r' + (q' ∸ r)) ⟩
(r' + (q' ∸ r)) + 0
≡˘⟨ Eq.cong (λ x → (r' + (q' ∸ r)) + x) (m≤n⇒m∸n≡0 (≤-trans p'≤q (m≤m+n q (r ∸ q')))) ⟩
(r' + (q' ∸ r)) + (p' ∸ (q + (r ∸ q')))
∎)
... | inj₁ p'≤q | inj₂ r≤q' =
Eq.cong₂ _,_
(begin-equality
(p + (q ∸ p')) + (r ∸ (q' + (p' ∸ q)))
≡⟨ Eq.cong ((p + (q ∸ p')) +_) (m≤n⇒m∸n≡0 (≤-trans r≤q' (m≤m+n q' (p' ∸ q)))) ⟩
(p + (q ∸ p')) + 0
≡⟨ +-identityʳ (p + (q ∸ p')) ⟩
p + (q ∸ p')
≡˘⟨ Eq.cong (λ x → p + (x ∸ p')) (+-identityʳ q) ⟩
p + ((q + 0) ∸ p')
≡˘⟨ Eq.cong (λ x → p + ((q + x) ∸ p')) (m≤n⇒m∸n≡0 r≤q') ⟩
p + ((q + (r ∸ q')) ∸ p')
∎)
(begin-equality
r' + ((q' + (p' ∸ q)) ∸ r)
≡⟨ Eq.cong (λ x → r' + ((q' + x) ∸ r)) (m≤n⇒m∸n≡0 p'≤q) ⟩
r' + ((q' + 0) ∸ r)
≡⟨ Eq.cong (λ x → r' + (x ∸ r)) (+-identityʳ q') ⟩
r' + (q' ∸ r)
≡˘⟨ +-identityʳ (r' + (q' ∸ r)) ⟩
(r' + (q' ∸ r)) + 0
≡˘⟨ Eq.cong ((r' + (q' ∸ r)) +_) (m≤n⇒m∸n≡0 (≤-trans p'≤q (m≤m+n q (r ∸ q')))) ⟩
(r' + (q' ∸ r)) + (p' ∸ (q + (r ∸ q')))
∎)
... | inj₂ q≤p' | inj₁ q'≤r =
Eq.cong₂ _,_
(begin-equality
(p + (q ∸ p')) + (r ∸ (q' + (p' ∸ q)))
≡⟨ Eq.cong (λ x → (p + x) + (r ∸ (q' + (p' ∸ q)))) (m≤n⇒m∸n≡0 q≤p') ⟩
(p + 0) + (r ∸ (q' + (p' ∸ q)))
≡⟨ Eq.cong (_+ (r ∸ (q' + (p' ∸ q)))) (+-identityʳ p) ⟩
p + (r ∸ (q' + (p' ∸ q)))
≡⟨ Eq.cong (p +_) (arithmetic p' q q' r q≤p' q'≤r) ⟩
p + ((q + (r ∸ q')) ∸ p')
∎)
(begin-equality
r' + ((q' + (p' ∸ q)) ∸ r)
≡˘⟨ Eq.cong (r' +_) (arithmetic r q' q p' q'≤r q≤p') ⟩
r' + (p' ∸ (q + (r ∸ q')))
≡˘⟨ Eq.cong (_+ (p' ∸ (q + (r ∸ q')))) (+-identityʳ r') ⟩
(r' + 0) + (p' ∸ (q + (r ∸ q')))
≡˘⟨ Eq.cong (λ x → (r' + x) + (p' ∸ (q + (r ∸ q')))) (m≤n⇒m∸n≡0 q'≤r) ⟩
(r' + (q' ∸ r)) + (p' ∸ (q + (r ∸ q')))
∎)
where
arithmetic : (p' q q' r : ℕ) → q ≤ p' → q' ≤ r → r ∸ (q' + (p' ∸ q)) ≡ ((q + (r ∸ q')) ∸ p')
arithmetic p' q q' r q≤p' q'≤r =
begin-equality
r ∸ (q' + (p' ∸ q))
≡˘⟨ ∸-+-assoc r q' (p' ∸ q) ⟩
(r ∸ q') ∸ (p' ∸ q)
≡˘⟨ +-∸-comm′ (r ∸ q') q≤p' ⟩
((r ∸ q') + q) ∸ p'
≡⟨ Eq.cong (_∸ p') (+-comm (r ∸ q') q) ⟩
(q + (r ∸ q')) ∸ p'
∎
... | inj₂ q≤p' | inj₂ r≤q' =
Eq.cong₂ _,_
(begin-equality
(p + (q ∸ p')) + (r ∸ (q' + (p' ∸ q)))
≡⟨ Eq.cong ((p + (q ∸ p')) +_) (m≤n⇒m∸n≡0 (≤-trans r≤q' (m≤m+n q' (p' ∸ q)))) ⟩
(p + (q ∸ p')) + 0
≡⟨ +-identityʳ (p + (q ∸ p')) ⟩
p + (q ∸ p')
≡˘⟨ Eq.cong (λ x → p + (x ∸ p')) (+-identityʳ q) ⟩
p + ((q + 0) ∸ p')
≡˘⟨ Eq.cong (λ x → p + ((q + x) ∸ p')) (m≤n⇒m∸n≡0 r≤q') ⟩
p + ((q + (r ∸ q')) ∸ p')
∎)
(begin-equality
r' + ((q' + (p' ∸ q)) ∸ r)
≡⟨ Eq.cong (r' +_) (+-∸-comm (p' ∸ q) r≤q') ⟩
r' + ((q' ∸ r) + (p' ∸ q))
≡˘⟨ +-assoc r' (q' ∸ r) (p' ∸ q) ⟩
(r' + (q' ∸ r)) + (p' ∸ q)
≡˘⟨ Eq.cong (λ x → (r' + (q' ∸ r)) + (p' ∸ x)) (+-identityʳ q) ⟩
(r' + (q' ∸ r)) + (p' ∸ (q + 0))
≡˘⟨ Eq.cong (λ x → (r' + (q' ∸ r)) + (p' ∸ (q + x))) (m≤n⇒m∸n≡0 r≤q') ⟩
(r' + (q' ∸ r)) + (p' ∸ (q + (r ∸ q')))
∎)
identityˡ : LeftIdentity (0 , 0) _·_
identityˡ (q , q') =
Eq.cong
(q ,_)
(begin-equality
q' + (0 ∸ q)
≡⟨ Eq.cong (q' +_) (0∸n≡0 q) ⟩
q' + 0
≡⟨ +-identityʳ q' ⟩
q'
∎)
identityʳ : RightIdentity (0 , 0) _·_
identityʳ (q , q') =
Eq.cong
(_, q')
(begin-equality
q + (0 ∸ q')
≡⟨ Eq.cong (q +_) (0∸n≡0 q') ⟩
q + 0
≡⟨ +-identityʳ q ⟩
q
∎)
∙-mono-≤ᵣ : _·_ Preserves₂ _≤ᵣ_ ⟶ _≤ᵣ_ ⟶ _≤ᵣ_
∙-mono-≤ᵣ (h₁ , h₁') (h₂ , h₂') =
+-mono-≤ h₁ (∸-mono h₂ h₁') ,
+-mono-≤ h₂' (∸-mono h₁' h₂)
List-CostMonoid : Set → CostMonoid
List-CostMonoid A = record
{ ℂ = List A
; zero = []
; _+_ = _++_
; _≤_ = _⊆_
; isCostMonoid = record
{ isMonoid = ++-isMonoid
; isPreorder = ⊆-isPreorder
; isMonotone = record { ∙-mono-≤ = ++⁺ }
}
}
where
open import Data.List
open import Data.List.Properties
open import Data.List.Relation.Binary.Sublist.Propositional
open import Data.List.Relation.Binary.Sublist.Propositional.Properties
cm-× : CostMonoid → CostMonoid → CostMonoid
cm-× cm₁ cm₂ = record
{ ℂ = ℂ cm₁ × ℂ cm₂
; zero = zero cm₁ , zero cm₂
; _+_ = λ (a₁ , a₂) (b₁ , b₂) → _+_ cm₁ a₁ b₁ , _+_ cm₂ a₂ b₂
; _≤_ = λ (a₁ , a₂) (b₁ , b₂) → _≤_ cm₁ a₁ b₁ × _≤_ cm₂ a₂ b₂
; isCostMonoid = record
{ isMonoid = record
{ isSemigroup = record
{ isMagma = record
{ isEquivalence = Eq.isEquivalence
; ∙-cong = Eq.cong₂ _
}
; assoc =
λ (a₁ , a₂) (b₁ , b₂) (c₁ , c₂) →
Eq.cong₂ _,_ (+-assoc cm₁ a₁ b₁ c₁) (+-assoc cm₂ a₂ b₂ c₂)
}
; identity =
(λ (a₁ , a₂) → Eq.cong₂ _,_ (+-identityˡ cm₁ a₁) (+-identityˡ cm₂ a₂)) ,
(λ (a₁ , a₂) → Eq.cong₂ _,_ (+-identityʳ cm₁ a₁) (+-identityʳ cm₂ a₂))
}
; isPreorder = record
{ isEquivalence = Eq.isEquivalence
; reflexive = λ { refl → ≤-refl cm₁ , ≤-refl cm₂ }
; trans = λ (h₁ , h₂) (h₁' , h₂') → ≤-trans cm₁ h₁ h₁' , ≤-trans cm₂ h₂ h₂'
}
; isMonotone = record
{ ∙-mono-≤ = λ (h₁ , h₂) (h₁' , h₂') → +-mono-≤ cm₁ h₁ h₁' , +-mono-≤ cm₂ h₂ h₂'
}
}
}
where
open CostMonoid
sequentialParCostMonoid :
(cm : CostMonoid)
→ IsCommutativeMonoid (CostMonoid._+_ cm) (CostMonoid.zero cm)
→ ParCostMonoid
sequentialParCostMonoid cm isCommutativeMonoid = record
{ ℂ = ℂ
; 𝟘 = zero
; _⊕_ = _+_
; _⊗_ = _+_
; _≤_ = _≤_
; isParCostMonoid = record
{ isMonoid = isMonoid
; isCommutativeMonoid = isCommutativeMonoid
; isPreorder = isPreorder
; isMonotone-⊕ = isMonotone
; isMonotone-⊗ = isMonotone
}
}
where open CostMonoid cm
ℕ-Work-ParCostMonoid : ParCostMonoid
ℕ-Work-ParCostMonoid = sequentialParCostMonoid ℕ-CostMonoid +-0-isCommutativeMonoid
where open import Data.Nat.Properties using (+-0-isCommutativeMonoid)
ℕ-Span-ParCostMonoid : ParCostMonoid
ℕ-Span-ParCostMonoid = record
{ ℂ = ℕ
; 𝟘 = 0
; _⊕_ = _+_
; _⊗_ = _⊔_
; _≤_ = _≤_
; isParCostMonoid = record
{ isMonoid = +-0-isMonoid
; isCommutativeMonoid = ⊔-0-isCommutativeMonoid
; isPreorder = ≤-isPreorder
; isMonotone-⊕ = record { ∙-mono-≤ = +-mono-≤ }
; isMonotone-⊗ = record { ∙-mono-≤ = ⊔-mono-≤ }
}
}
where
open import Data.Nat
open import Data.Nat.Properties
pcm-× : ParCostMonoid → ParCostMonoid → ParCostMonoid
pcm-× pcm₁ pcm₂ = record
{ ℂ = ℂ pcm₁ × ℂ pcm₂
; 𝟘 = 𝟘 pcm₁ , 𝟘 pcm₂
; _⊕_ = λ (a₁ , a₂) (b₁ , b₂) → _⊕_ pcm₁ a₁ b₁ , _⊕_ pcm₂ a₂ b₂
; _⊗_ = λ (a₁ , a₂) (b₁ , b₂) → _⊗_ pcm₁ a₁ b₁ , _⊗_ pcm₂ a₂ b₂
; _≤_ = Pointwise (_≤_ pcm₁) (_≤_ pcm₂)
; isParCostMonoid = record
{ isMonoid = record
{ isSemigroup = record
{ isMagma = record
{ isEquivalence = Eq.isEquivalence
; ∙-cong = Eq.cong₂ _
}
; assoc =
λ (a₁ , a₂) (b₁ , b₂) (c₁ , c₂) →
Eq.cong₂ _,_ (⊕-assoc pcm₁ a₁ b₁ c₁) (⊕-assoc pcm₂ a₂ b₂ c₂)
}
; identity =
(λ (a₁ , a₂) → Eq.cong₂ _,_ (⊕-identityˡ pcm₁ a₁) (⊕-identityˡ pcm₂ a₂)) ,
(λ (a₁ , a₂) → Eq.cong₂ _,_ (⊕-identityʳ pcm₁ a₁) (⊕-identityʳ pcm₂ a₂))
}
; isCommutativeMonoid = record
{ isMonoid = record
{ isSemigroup = record
{ isMagma = record
{ isEquivalence = Eq.isEquivalence
; ∙-cong = Eq.cong₂ _
}
; assoc =
λ (a₁ , a₂) (b₁ , b₂) (c₁ , c₂) →
Eq.cong₂ _,_ (⊗-assoc pcm₁ a₁ b₁ c₁) (⊗-assoc pcm₂ a₂ b₂ c₂)
}
; identity =
(λ (a₁ , a₂) → Eq.cong₂ _,_ (⊗-identityˡ pcm₁ a₁) (⊗-identityˡ pcm₂ a₂)) ,
(λ (a₁ , a₂) → Eq.cong₂ _,_ (⊗-identityʳ pcm₁ a₁) (⊗-identityʳ pcm₂ a₂))
}
; comm = λ (a₁ , a₂) (b₁ , b₂) → Eq.cong₂ _,_ (⊗-comm pcm₁ a₁ b₁) (⊗-comm pcm₂ a₂ b₂)
}
; isPreorder = record
{ isEquivalence = Eq.isEquivalence
; reflexive = λ { refl → ≤-refl pcm₁ , ≤-refl pcm₂ }
; trans = λ (h₁ , h₂) (h₁' , h₂') → ≤-trans pcm₁ h₁ h₁' , ≤-trans pcm₂ h₂ h₂'
}
; isMonotone-⊕ = record
{ ∙-mono-≤ = λ (h₁ , h₂) (h₁' , h₂') → ⊕-mono-≤ pcm₁ h₁ h₁' , ⊕-mono-≤ pcm₂ h₂ h₂'
}
; isMonotone-⊗ = record
{ ∙-mono-≤ = λ (h₁ , h₂) (h₁' , h₂') → ⊗-mono-≤ pcm₁ h₁ h₁' , ⊗-mono-≤ pcm₂ h₂ h₂'
}
}
}
where
open ParCostMonoid
open import Data.Product.Relation.Binary.Pointwise.NonDependent
ℕ²-ParCostMonoid : ParCostMonoid
ℕ²-ParCostMonoid = pcm-× ℕ-Work-ParCostMonoid ℕ-Span-ParCostMonoid