-
Notifications
You must be signed in to change notification settings - Fork 1
/
Algebra.agda
488 lines (387 loc) Β· 17.3 KB
/
Algebra.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
{-# OPTIONS --cubical --safe #-}
module Algebra where
open import Prelude
module _ {a} {A : Type a} (_β_ : A β A β A) where
Associative : Type a
Associative = β x y z β (x β y) β z β‘ x β (y β z)
Commutative : Type _
Commutative = β x y β x β y β‘ y β x
Idempotent : Type _
Idempotent = β x β x β x β‘ x
IdentityΛ‘ : (A β B β B) β A β Type _
IdentityΛ‘ _β_ x = β y β x β y β‘ y
ZeroΛ‘ : (A β B β A) β A β Type _
ZeroΛ‘ _β_ x = β y β x β y β‘ x
ZeroΚ³ : (A β B β B) β B β Type _
ZeroΚ³ _β_ x = β y β y β x β‘ x
IdentityΚ³ : (A β B β A) β B β Type _
IdentityΚ³ _β_ x = β y β y β x β‘ y
_DistributesΚ³_ : (A β B β B) β (B β B β B) β Type _
_β_ DistributesΚ³ _β_ = β x y z β x β (y β z) β‘ (x β y) β (x β z)
_DistributesΛ‘_ : (B β A β B) β (B β B β B) β Type _
_β_ DistributesΛ‘ _β_ = β x y z β (x β y) β z β‘ (x β z) β (y β z)
CancellableΛ‘ : (A β B β C) β A β Type _
CancellableΛ‘ _β_ c = β x y β c β x β‘ c β y β x β‘ y
CancellableΚ³ : (A β B β C) β B β Type _
CancellableΚ³ _β_ c = β x y β x β c β‘ y β c β x β‘ y
CancellativeΛ‘ : (A β B β C) β Type _
CancellativeΛ‘ _β_ = β c β CancellableΛ‘ _β_ c
CancellativeΚ³ : (A β B β C) β Type _
CancellativeΚ³ _β_ = β c β CancellableΚ³ _β_ c
module _ {β} (π : Type β) where
record Semigroup : Type β where
infixl 6 _β_
field
_β_ : π β π β π
assoc : β x y z β (x β y) β z β‘ x β (y β z)
record Monoid : Type β where
infixl 6 _β_
field
_β_ : π β π β π
Ξ΅ : π
assoc : β x y z β (x β y) β z β‘ x β (y β z)
Ξ΅β : β x β Ξ΅ β x β‘ x
βΞ΅ : β x β x β Ξ΅ β‘ x
semigroup : Semigroup
semigroup = record
{ _β_ = _β_; assoc = assoc }
record Group : Type β where
field
monoid : Monoid
open Monoid monoid public
field
-_ : π β π
ββ» : β x β x β - x β‘ Ξ΅
β»β : β x β - x β x β‘ Ξ΅
open import Path.Reasoning
cancelΛ‘ : CancellativeΛ‘ _β_
cancelΛ‘ x y z p =
y β‘Λβ¨ Ξ΅β y β©
Ξ΅ β y β‘Λβ¨ cong (_β y) (β»β x) β©
(- x β x) β y β‘β¨ assoc (- x) x y β©
- x β (x β y) β‘β¨ cong (- x β_) p β©
- x β (x β z) β‘Λβ¨ assoc (- x) x z β©
(- x β x) β z β‘β¨ cong (_β z) (β»β x) β©
Ξ΅ β z β‘β¨ Ξ΅β z β©
z β
cancelΚ³ : CancellativeΚ³ _β_
cancelΚ³ x y z p =
y β‘Λβ¨ βΞ΅ y β©
y β Ξ΅ β‘Λβ¨ cong (y β_) (ββ» x) β©
y β (x β - x) β‘Λβ¨ assoc y x (- x) β©
(y β x) β - x β‘β¨ cong (_β - x) p β©
(z β x) β - x β‘β¨ assoc z x (- x) β©
z β (x β - x) β‘β¨ cong (z β_) (ββ» x) β©
z β Ξ΅ β‘β¨ βΞ΅ z β©
z β
record CommutativeMonoid : Type β where
field
monoid : Monoid
open Monoid monoid public
field
comm : Commutative _β_
record Semilattice : Type β where
field
commutativeMonoid : CommutativeMonoid
open CommutativeMonoid commutativeMonoid public
field
idem : Idempotent _β_
record NearSemiring : Type β where
infixl 6 _+_
infixl 7 _*_
field
_+_ : π β π β π
_*_ : π β π β π
1# : π
0# : π
+-assoc : Associative _+_
*-assoc : Associative _*_
0+ : IdentityΛ‘ _+_ 0#
+0 : IdentityΚ³ _+_ 0#
1* : IdentityΛ‘ _*_ 1#
*1 : IdentityΚ³ _*_ 1#
0* : ZeroΛ‘ _*_ 0#
β¨+β©* : _*_ DistributesΛ‘ _+_
+-monoid : Monoid
+-monoid .Monoid._β_ = _+_
+-monoid .Monoid.Ξ΅ = 0#
+-monoid .Monoid.assoc = +-assoc
+-monoid .Monoid.Ξ΅β = 0+
+-monoid .Monoid.βΞ΅ = +0
*-monoid : Monoid
*-monoid .Monoid._β_ = _*_
*-monoid .Monoid.Ξ΅ = 1#
*-monoid .Monoid.assoc = *-assoc
*-monoid .Monoid.Ξ΅β = 1*
*-monoid .Monoid.βΞ΅ = *1
record Semiring : Type β where
field
nearSemiring : NearSemiring
open NearSemiring nearSemiring public
field
+-comm : Commutative _+_
*0 : ZeroΚ³ _*_ 0#
*β¨+β© : _*_ DistributesΚ³ _+_
record IdempotentSemiring : Type β where
field
semiring : Semiring
open Semiring semiring public
field
+-idem : Idempotent _+_
record CommutativeSemiring : Type β where
field
semiring : Semiring
open Semiring semiring public
field
*-comm : Commutative _*_
record StarSemiring : Type β where
field semiring : Semiring
open Semiring semiring public
field
_β : π β π
star-iterΚ³ : β x β x β β‘ 1# + x * x β
star-iterΛ‘ : β x β x β β‘ 1# + x β * x
_βΊ : π β π
x βΊ = x * x β
record LeftSemimodule {ββ ββ} {π : Type ββ} (semiring : Semiring π) (π : Type ββ) : Type (ββ ββ ββ) where
open Semiring semiring public
field semimodule : CommutativeMonoid π
open CommutativeMonoid semimodule renaming (_β_ to _βͺ_) public
renaming ( assoc to βͺ-assoc
; Ξ΅β to β
βͺ
; βΞ΅ to βͺβ
; Ξ΅ to β
)
infixr 7 _β_
field
_β_ : π β π β π
β¨*β©β : β x y z β (x * y) β z β‘ x β (y β z)
β¨+β©β : β x y z β (x + y) β z β‘ (x β z) βͺ (y β z)
ββ¨βͺβ© : _β_ DistributesΚ³ _βͺ_
1β : IdentityΛ‘ _β_ 1#
0β : β x β 0# β x β‘ β
ββ
: β x β x β β
β‘ β
record MonoidHomomorphism_βΆ_
{ββ ββ} {π : Type ββ} {π : Type ββ}
(from : Monoid π)
(to : Monoid π)
: Type (ββ ββ ββ) where
open Monoid from
open Monoid to
renaming ( _β_ to _β_
; Ξ΅ to β)
field
f : π β π
β-homo : β x y β f (x β y) β‘ f x β f y
Ξ΅-homo : f Ξ΅ β‘ β
record SemimoduleHomomorphism[_]_βΆ_
{ββ ββ ββ} {π : Type ββ} {πβ : Type ββ} {πβ : Type ββ}
(rng : Semiring π)
(from : LeftSemimodule rng πβ)
(to : LeftSemimodule rng πβ) : Type (ββ ββ ββ ββ ββ) where
open Semiring rng
open LeftSemimodule from using (_β_; monoid)
open LeftSemimodule to using () renaming (_β_ to _ββ²_; monoid to monoidβ²)
field mon-homo : MonoidHomomorphism monoid βΆ monoidβ²
open MonoidHomomorphism_βΆ_ mon-homo public
field β-homo : β r x β f (r β x) β‘ r ββ² f x
module _ {ββ ββ} (πΉ : Type ββ β Type ββ) where
record Functor : Type (βsuc ββ ββ ββ) where
field
map : (A β B) β πΉ A β πΉ B
map-id : map (id {ββ} {A}) β‘ id
map-comp : (f : B β C) β (g : A β B) β map (f β g) β‘ map f β map g
record Applicative : Type (βsuc ββ ββ ββ) where
infixl 5 _<*>_
field
pure : A β πΉ A
_<*>_ : πΉ (A β B) β πΉ A β πΉ B
map : (A β B) β πΉ A β πΉ B
map f = _<*>_ (pure f)
field
pure-homo : (f : A β B) β (x : A) β pure f <*> (pure x) β‘ pure (f x)
<*>-interchange : (u : πΉ (A β B)) β (y : A) β u <*> pure y β‘ map (_$ y) u
<*>-comp : (u : πΉ (B β C)) β (v : πΉ (A β B)) β (w : πΉ A) β pure _ββ²_ <*> u <*> v <*> w β‘ u <*> (v <*> w)
record Monad : Type (βsuc ββ ββ ββ) where
infixl 1 _>>=_
field
_>>=_ : πΉ A β (A β πΉ B) β πΉ B
return : A β πΉ A
>>=-idΛ‘ : (f : A β πΉ B) β (x : A) β (return x >>= f) β‘ f x
>>=-idΚ³ : (x : πΉ A) β (x >>= return) β‘ x
>>=-assoc : (xs : πΉ A) (f : A β πΉ B) (g : B β πΉ C) β ((xs >>= f) >>= g) β‘ (xs >>= (Ξ» x β f x >>= g))
mmap : (A β B) β πΉ A β πΉ B
mmap f xs = xs >>= (return β f)
record Comonad : Type (βsuc ββ ββ ββ) where
field
extend : (πΉ A β B) β πΉ A β πΉ B
extract : πΉ A β A
infixl 1 _=>>_
_=>>_ : πΉ A β (πΉ A β B) β πΉ B
_=>>_ = flip extend
cmap : (A β B) β πΉ A β πΉ B
cmap f xs = xs =>> (f β extract)
-- liftA2 : (A β B β C) β πΉ A β πΉ B β πΉ C
-- liftA2 f xs ys = xs =>> Ξ» x β {!ys =>> Ξ» y β {!!}!}
record Alternative : Type (βsuc ββ ββ ββ) where
field
applicative : Applicative
open Applicative applicative public
field
0# : πΉ A
_<|>_ : πΉ A β πΉ A β πΉ A
<|>-idΛ‘ : (x : πΉ A) β 0# <|> x β‘ x
<|>-idΚ³ : (x : πΉ A) β x <|> 0# β‘ x
0-annΛ‘ : (x : πΉ A) β 0# <*> x β‘ 0# {B}
<|>-distrib : (x y : πΉ (A β B)) β (z : πΉ A) β (x <|> y) <*> z β‘ (x <*> z) <|> (y <*> z)
-- record MonadPlus ββ ββ : Type (βsuc (ββ ββ ββ)) where
-- field
-- monad : Monad ββ ββ
-- open Monad monad public
-- field
-- 0# : πΉ A
-- _<|>_ : πΉ A β πΉ A β πΉ A
-- <|>-idΛ‘ : (x : πΉ A) β 0# <|> x β‘ x
-- <|>-idΚ³ : (x : πΉ A) β x <|> 0# β‘ x
-- 0-annΛ‘ : (x : A β πΉ B) β (0# >>= x) β‘ 0#
-- <|>-distrib : (x y : πΉ A) β (z : A β πΉ B) β ((x <|> y) >>= z) β‘ (x >>= z) <|> (y >>= z)
-- Endo : Type a β Type a
-- Endo A = A β A
-- endoMonoid : β {a} β Type a β Monoid a
-- endoMonoid A .Monoid.π = Endo A
-- endoMonoid A .Monoid.Ξ΅ x = x
-- endoMonoid A .Monoid._β_ f g x = f (g x)
-- endoMonoid A .Monoid.assoc _ _ _ = refl
-- endoMonoid A .Monoid.Ξ΅β _ = refl
-- endoMonoid A .Monoid.βΞ΅ _ = refl
-- record Foldable ββ ββ : Type (βsuc (ββ ββ ββ)) where
-- field
-- πΉ : Type ββ β Type ββ
-- open Monoid β¦ ... β¦
-- field
-- foldMap : {A : Type ββ} β¦ _ : Monoid ββ β¦ β (A β π) β πΉ A β π
-- foldr : {A B : Type ββ} β (A β B β B) β B β πΉ A β B
-- foldr f b xs = foldMap β¦ endoMonoid _ β¦ f xs b
--
record IsSetMonad {ββ} {ββ} (πΉ : Type ββ β Type ββ) : Type (βsuc ββ ββ ββ) where
infixl 1 _>>=_
field
_>>=_ : πΉ A β (A β πΉ B) β πΉ B
return : A β πΉ A
trunc : isSet A β isSet (πΉ A)
>>=-idΛ‘ : isSet B β (f : A β πΉ B) β (x : A) β (return x >>= f) β‘ f x
>>=-idΚ³ : isSet A β (x : πΉ A) β (x >>= return) β‘ x
>>=-assoc : isSet C β (xs : πΉ A) (f : A β πΉ B) (g : B β πΉ C) β ((xs >>= f) >>= g) β‘ (xs >>= (Ξ» x β f x >>= g))
record SetMonad ββ ββ : Type (βsuc (ββ ββ ββ)) where
field
πΉ : Type ββ β Type ββ
isSetMonad : IsSetMonad πΉ
open IsSetMonad isSetMonad public
record SetMonadHomomorphism_βΆ_
{ββ ββ ββ}
(from : SetMonad ββ ββ)
(to : SetMonad ββ ββ) : Type (βsuc ββ ββ ββ ββ ββ) where
module F = SetMonad from
module T = SetMonad to
field
f : F.πΉ A β T.πΉ A
>>=-homo : (xs : F.πΉ A) (k : A β F.πΉ B) β (f xs T.>>= (f β k)) β‘ f (xs F.>>= k)
return-homo : (x : A) β f (F.return x) β‘ T.return x
module _ {ββ} {π : Type ββ} (monoid : Monoid π) {ββ} (πΉ : π β Type ββ β Type ββ) where
open Monoid monoid
record GradedMonad : Type (ββ ββ βsuc ββ) where
field
return : A β πΉ Ξ΅ A
_>>=_ : β {x y} β πΉ x A β (A β πΉ y B) β πΉ (x β y) B
_<=<_ : β {x y} β (B β πΉ y C) β (A β πΉ x B) β A β πΉ (x β y) C
(g <=< f) x = f x >>= g
_>=>_ : β {x y} β (A β πΉ x B) β (B β πΉ y C) β A β πΉ (x β y) C
(f >=> g) x = f x >>= g
field
>>=-idΛ‘ : β {s} (f : A β πΉ s B) β (x : A) β (return x >>= f) β‘[ i β πΉ (Ξ΅β s i) B ]β‘ (f x)
>>=-idΚ³ : β {s} (x : πΉ s A) β (x >>= return) β‘[ i β πΉ (βΞ΅ s i) A ]β‘ x
>>=-assoc : β {x y z} (xs : πΉ x A) (f : A β πΉ y B) (g : B β πΉ z C) β ((xs >>= f) >>= g) β‘[ i β πΉ (assoc x y z i) C ]β‘ (xs >>= (Ξ» x β f x >>= g))
infixr 0 proven-bind
proven-bind : β {x y z} β πΉ x A β (A β πΉ y B) β (x β y) β‘ z β πΉ z B
proven-bind xs f proof = subst (flip πΉ _) proof (xs >>= f)
syntax proven-bind xs f proof = xs >>=[ proof ] f
infixr 0 proven-do
proven-do : β {x y z} β πΉ x A β (A β πΉ y B) β (x β y) β‘ z β πΉ z B
proven-do = proven-bind
syntax proven-do xs (Ξ» x β e) proof = x β xs [ proof ] e
map : β {x} β (A β B) β πΉ x A β πΉ x B
map f xs = xs >>=[ βΞ΅ _ ] (return β f)
_<*>_ : β {x y} β πΉ x (A β B) β πΉ y A β πΉ (x β y) B
fs <*> xs = fs >>= flip map xs
_>>=Ξ΅_ : β {x} β πΉ x A β (A β πΉ Ξ΅ B) β πΉ x B
xs >>=Ξ΅ f = xs >>=[ βΞ΅ _ ] f
record GradedComonad : Type (ββ ββ βsuc ββ) where
field
extract : πΉ Ξ΅ A β A
extend : β {x y} β (πΉ y A β B) β πΉ (x β y) A β πΉ x B
extend[_] : β {x y z} β x β y β‘ z β (πΉ y A β B) β πΉ z A β πΉ x B
extend[ p ] k = subst (Ξ» w β (πΉ w _ β _)) p (extend k)
_=>>_ : β {x y} β πΉ (x β y) A β (πΉ y A β B) β πΉ x B
_=>>_ = flip extend
proven-cobind : β {x y z} β (πΉ y A β B) β x β y β‘ z β πΉ z A β πΉ x B
proven-cobind k prf = extend[ prf ] k
syntax proven-cobind f prf xs = xs =>>[ prf ] f
_=<=_ : β {x y} β (πΉ x B β C) β (πΉ y A β B) β πΉ (x β y) A β C
(g =<= f) x = g (extend f x)
_=>=_ : β {x y} β (πΉ y A β B) β (πΉ x B β C) β πΉ (x β y) A β C
_=>=_ = flip _=<=_
field
=>>-idΛ‘ : β {x} {B : Type ββ} β (f : πΉ x A β B) β PathP (Ξ» i β πΉ (Ξ΅β x i) A β B) (extract =<= f) f
=>>-idΚ³ : β {x} {B : Type ββ} β (f : πΉ x A β B) β PathP (Ξ» i β πΉ (βΞ΅ x i) A β B) (f =<= extract) f
=>>-assoc : β {x y z} {D : Type ββ} β (f : πΉ x C β D) (g : πΉ y B β C) (h : πΉ z A β B) β
PathP (Ξ» i β πΉ (assoc x y z i) A β D) ((f =<= g) =<= h) (f =<= (g =<= h))
{-# INLINE proven-cobind #-}
{-# INLINE extend[_] #-}
infixr 1 codo-syntax
codo-syntax : β {x y} β πΉ (x β y) A β (πΉ y A β B) β πΉ x B
codo-syntax = _=>>_
syntax codo-syntax xs (Ξ» x β r) = x β’ xs ΝΎ r
infixr 1 proven-codo-syntax
proven-codo-syntax : β {x y z} β πΉ z A β (πΉ y A β B) β x β y β‘ z β πΉ x B
proven-codo-syntax xs k p = proven-cobind k p xs
syntax proven-codo-syntax xs (Ξ» x β r) prf = x β’ xs [ prf ]ΝΎ r
map : β {x} β (A β B) β πΉ x A β πΉ x B
map f = extend[ βΞ΅ _ ] (f ββ² extract)
{-# INLINE map #-}
open import Cubical.Foundations.Prelude using (fromPathP; transportRefl; substRefl)
map-id : β {x} β (xs : πΉ x A) β map id xs β‘ xs
map-id xs = cong (_$ xs) (fromPathP (=>>-idΚ³ id))
extract-extend : β {x} (xs : πΉ x A) β extract (extend[ Ξ΅β x ] id xs) β‘ xs
extract-extend {A = A} {x = x} xs =
cong extract (J (Ξ» y p β (xs : πΉ y A) β extend[ p ] id xs β‘ extend id (subst (flip πΉ A) (sym p) xs)) (Ξ» xs β cong (_$ xs) (transportRefl (extend id)) ΝΎ cong (extend id) (sym (transportRefl xs))) (Ξ΅β x) xs ) ΝΎ
sym (transportRefl _) ΝΎ
cong (_$ xs) (fromPathP (=>>-idΛ‘ {x = x} id))
open import Path.Reasoning
-- map-comp : β {x} (g : B β C) (f : A β B) β map {x = x} g β map f β‘ map {x = x} (g β f)
-- map-comp {x = x} g f = funExt Ξ» xs β
-- map g (map f xs) β‘β¨β©
-- extend[ βΞ΅ x ] (g β extract) (extend[ βΞ΅ x ] (f β extract) xs) β‘β¨ {!!} β©
-- extend[ βΞ΅ x ] (g β f β extract) xs β‘β¨β©
-- map (g β f) xs β
record SGradedComonad {ββ ββ} {π : Type ββ} (semiring : Semiring π) (πΉ : π β Type ββ β Type ββ) : Type (ββ ββ βsuc ββ) where
open Semiring semiring
field gradedComonad : GradedComonad *-monoid πΉ
open GradedComonad gradedComonad
field
pure : β {x} β πΉ x A
_<*>_ : β {x} β πΉ x (A β B) β πΉ x A β πΉ x B
separate : β {x y} β πΉ (x + y) A β πΉ x A Γ πΉ y A
record MatchedPair {ββ ββ} {π : Type ββ} {πΈ : Type ββ} (R : Monoid π) (E : Monoid πΈ) : Type (ββ ββ ββ) where
open Monoid R using () renaming (_β_ to _*_; Ξ΅ to r1)
open Monoid E using (_β_) renaming (Ξ΅ to e1)
field
ΞΉ : π β πΈ β π
ΞΊ : π β πΈ β πΈ
law1 : β x β ΞΉ x e1 β‘ x
law2 : β x β ΞΉ r1 x β‘ r1
law3 : β x β ΞΊ r1 x β‘ x
law4 : β x β ΞΊ x e1 β‘ e1
law5 : β x y z β ΞΉ x (y β z) β‘ ΞΉ (ΞΉ x y) z
law6 : β x y z β ΞΉ (x * y) z β‘ ΞΉ x (ΞΊ y z) * ΞΉ y z
law7 : β x y z β ΞΊ (x * y) z β‘ ΞΊ x (ΞΊ y z)
law8 : β x y z β ΞΊ x (y β z) β‘ ΞΊ x y β ΞΊ (ΞΉ x y) z