-
Notifications
You must be signed in to change notification settings - Fork 247
/
Copy pathBundles.agda
584 lines (470 loc) · 17.2 KB
/
Bundles.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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
------------------------------------------------------------------------
-- The Agda standard library
--
-- Bundles for types of functions
------------------------------------------------------------------------
-- The contents of this file should usually be accessed from `Function`.
-- Note that these bundles differ from those found elsewhere in other
-- library hierarchies as they take Setoids as parameters. This is
-- because a function is of no use without knowing what its domain and
-- codomain is, as well which equalities are being considered over them.
-- One consequence of this is that they are not built from the
-- definitions found in `Function.Structures` as is usually the case in
-- other library hierarchies, as this would duplicate the equality
-- axioms.
{-# OPTIONS --cubical-compatible --safe #-}
module Function.Bundles where
open import Function.Base using (_∘_)
open import Function.Consequences.Propositional
using (strictlySurjective⇒surjective; strictlyInverseˡ⇒inverseˡ; strictlyInverseʳ⇒inverseʳ)
open import Function.Definitions
import Function.Structures as FunctionStructures
open import Level using (Level; _⊔_; suc)
open import Data.Product.Base using (_,_; proj₁; proj₂)
open import Relation.Binary.Bundles using (Setoid)
open import Relation.Binary.PropositionalEquality.Core as ≡
using (_≡_)
import Relation.Binary.PropositionalEquality.Properties as ≡
open Setoid using (isEquivalence)
private
variable
a b ℓ₁ ℓ₂ : Level
------------------------------------------------------------------------
-- Setoid bundles
------------------------------------------------------------------------
module _ (From : Setoid a ℓ₁) (To : Setoid b ℓ₂) where
open Setoid From using () renaming (Carrier to A; _≈_ to _≈₁_)
open Setoid To using () renaming (Carrier to B; _≈_ to _≈₂_)
open FunctionStructures _≈₁_ _≈₂_
------------------------------------------------------------------------
-- Bundles with one element
-- Called `Func` rather than `Function` in order to avoid clashing
-- with the top-level module.
record Func : Set (a ⊔ b ⊔ ℓ₁ ⊔ ℓ₂) where
field
to : A → B
cong : Congruent _≈₁_ _≈₂_ to
isCongruent : IsCongruent to
isCongruent = record
{ cong = cong
; isEquivalence₁ = isEquivalence From
; isEquivalence₂ = isEquivalence To
}
open IsCongruent isCongruent public
using (module Eq₁; module Eq₂)
record Injection : Set (a ⊔ b ⊔ ℓ₁ ⊔ ℓ₂) where
field
to : A → B
cong : Congruent _≈₁_ _≈₂_ to
injective : Injective _≈₁_ _≈₂_ to
function : Func
function = record
{ to = to
; cong = cong
}
open Func function public
hiding (to; cong)
isInjection : IsInjection to
isInjection = record
{ isCongruent = isCongruent
; injective = injective
}
record Surjection : Set (a ⊔ b ⊔ ℓ₁ ⊔ ℓ₂) where
field
to : A → B
cong : Congruent _≈₁_ _≈₂_ to
surjective : Surjective _≈₁_ _≈₂_ to
function : Func
function = record
{ to = to
; cong = cong
}
open Func function public
hiding (to; cong)
isSurjection : IsSurjection to
isSurjection = record
{ isCongruent = isCongruent
; surjective = surjective
}
open IsSurjection isSurjection public
using
( strictlySurjective
; from
; inverseˡ
; strictlyInverseˡ
)
to⁻ : B → A
to⁻ = from
{-# WARNING_ON_USAGE to⁻
"Warning: to⁻ was deprecated in v2.3.
Please use Function.Structures.IsSurjection.from instead. "
#-}
to∘to⁻ : StrictlyInverseˡ _≈₂_ to from
to∘to⁻ = strictlyInverseˡ
{-# WARNING_ON_USAGE to∘to⁻
"Warning: to∘to⁻ was deprecated in v2.3.
Please use Function.Structures.IsSurjection.strictlyInverseˡ instead. "
#-}
record Bijection : Set (a ⊔ b ⊔ ℓ₁ ⊔ ℓ₂) where
field
to : A → B
cong : Congruent _≈₁_ _≈₂_ to
bijective : Bijective _≈₁_ _≈₂_ to
injective : Injective _≈₁_ _≈₂_ to
injective = proj₁ bijective
surjective : Surjective _≈₁_ _≈₂_ to
surjective = proj₂ bijective
injection : Injection
injection = record
{ cong = cong
; injective = injective
}
surjection : Surjection
surjection = record
{ cong = cong
; surjective = surjective
}
open Injection injection public
using (isInjection)
open Surjection surjection public
using (isSurjection
; strictlySurjective
; from
; inverseˡ
; strictlyInverseˡ
)
isBijection : IsBijection to
isBijection = record
{ isInjection = isInjection
; surjective = surjective
}
open IsBijection isBijection public
using (module Eq₁; module Eq₂
; inverseʳ; strictlyInverseʳ
; from-cong; from-injective; from-surjective; from-bijective
)
------------------------------------------------------------------------
-- Bundles with two elements
module _ (From : Setoid a ℓ₁) (To : Setoid b ℓ₂) where
open Setoid From using () renaming (Carrier to A; _≈_ to _≈₁_)
open Setoid To using () renaming (Carrier to B; _≈_ to _≈₂_)
open FunctionStructures _≈₁_ _≈₂_
record Equivalence : Set (a ⊔ b ⊔ ℓ₁ ⊔ ℓ₂) where
field
to : A → B
from : B → A
to-cong : Congruent _≈₁_ _≈₂_ to
from-cong : Congruent _≈₂_ _≈₁_ from
toFunction : Func From To
toFunction = record
{ to = to
; cong = to-cong
}
open Func toFunction public
using (module Eq₁; module Eq₂)
renaming (isCongruent to to-isCongruent)
fromFunction : Func To From
fromFunction = record
{ to = from
; cong = from-cong
}
open Func fromFunction public
using ()
renaming (isCongruent to from-isCongruent)
record LeftInverse : Set (a ⊔ b ⊔ ℓ₁ ⊔ ℓ₂) where
field
to : A → B
from : B → A
to-cong : Congruent _≈₁_ _≈₂_ to
from-cong : Congruent _≈₂_ _≈₁_ from
inverseˡ : Inverseˡ _≈₁_ _≈₂_ to from
isCongruent : IsCongruent to
isCongruent = record
{ cong = to-cong
; isEquivalence₁ = isEquivalence From
; isEquivalence₂ = isEquivalence To
}
isLeftInverse : IsLeftInverse to from
isLeftInverse = record
{ isCongruent = isCongruent
; from-cong = from-cong
; inverseˡ = inverseˡ
}
open IsLeftInverse isLeftInverse public
using (module Eq₁; module Eq₂; strictlyInverseˡ; isSurjection)
open IsSurjection isSurjection public using (surjective)
equivalence : Equivalence
equivalence = record
{ to-cong = to-cong
; from-cong = from-cong
}
isSplitSurjection : IsSplitSurjection to
isSplitSurjection = record
{ from = from
; isLeftInverse = isLeftInverse
}
surjection : Surjection From To
surjection = record
{ to = to
; cong = to-cong
; surjective = surjective
}
record RightInverse : Set (a ⊔ b ⊔ ℓ₁ ⊔ ℓ₂) where
field
to : A → B
from : B → A
to-cong : Congruent _≈₁_ _≈₂_ to
from-cong : Congruent _≈₂_ _≈₁_ from
inverseʳ : Inverseʳ _≈₁_ _≈₂_ to from
isCongruent : IsCongruent to
isCongruent = record
{ cong = to-cong
; isEquivalence₁ = isEquivalence From
; isEquivalence₂ = isEquivalence To
}
isRightInverse : IsRightInverse to from
isRightInverse = record
{ isCongruent = isCongruent
; from-cong = from-cong
; inverseʳ = inverseʳ
}
open IsRightInverse isRightInverse public
using (module Eq₁; module Eq₂; strictlyInverseʳ; isInjection)
open IsInjection isInjection public using (injective)
equivalence : Equivalence
equivalence = record
{ to-cong = to-cong
; from-cong = from-cong
}
injection : Injection From To
injection = record
{ to = to
; cong = to-cong
; injective = injective
}
record Inverse : Set (a ⊔ b ⊔ ℓ₁ ⊔ ℓ₂) where
field
to : A → B
from : B → A
to-cong : Congruent _≈₁_ _≈₂_ to
from-cong : Congruent _≈₂_ _≈₁_ from
inverse : Inverseᵇ _≈₁_ _≈₂_ to from
inverseˡ : Inverseˡ _≈₁_ _≈₂_ to from
inverseˡ = proj₁ inverse
inverseʳ : Inverseʳ _≈₁_ _≈₂_ to from
inverseʳ = proj₂ inverse
leftInverse : LeftInverse
leftInverse = record
{ to-cong = to-cong
; from-cong = from-cong
; inverseˡ = inverseˡ
}
rightInverse : RightInverse
rightInverse = record
{ to-cong = to-cong
; from-cong = from-cong
; inverseʳ = inverseʳ
}
open LeftInverse leftInverse public using (isLeftInverse; strictlyInverseˡ)
open RightInverse rightInverse public using (isRightInverse; strictlyInverseʳ)
isInverse : IsInverse to from
isInverse = record
{ isLeftInverse = isLeftInverse
; inverseʳ = inverseʳ
}
open IsInverse isInverse public using (module Eq₁; module Eq₂)
------------------------------------------------------------------------
-- Bundles with three elements
record BiEquivalence : Set (a ⊔ b ⊔ ℓ₁ ⊔ ℓ₂) where
field
to : A → B
from₁ : B → A
from₂ : B → A
to-cong : Congruent _≈₁_ _≈₂_ to
from₁-cong : Congruent _≈₂_ _≈₁_ from₁
from₂-cong : Congruent _≈₂_ _≈₁_ from₂
record BiInverse : Set (a ⊔ b ⊔ ℓ₁ ⊔ ℓ₂) where
field
to : A → B
from₁ : B → A
from₂ : B → A
to-cong : Congruent _≈₁_ _≈₂_ to
from₁-cong : Congruent _≈₂_ _≈₁_ from₁
from₂-cong : Congruent _≈₂_ _≈₁_ from₂
inverseˡ : Inverseˡ _≈₁_ _≈₂_ to from₁
inverseʳ : Inverseʳ _≈₁_ _≈₂_ to from₂
to-isCongruent : IsCongruent to
to-isCongruent = record
{ cong = to-cong
; isEquivalence₁ = isEquivalence From
; isEquivalence₂ = isEquivalence To
}
isBiInverse : IsBiInverse to from₁ from₂
isBiInverse = record
{ to-isCongruent = to-isCongruent
; from₁-cong = from₁-cong
; from₂-cong = from₂-cong
; inverseˡ = inverseˡ
; inverseʳ = inverseʳ
}
biEquivalence : BiEquivalence
biEquivalence = record
{ to-cong = to-cong
; from₁-cong = from₁-cong
; from₂-cong = from₂-cong
}
------------------------------------------------------------------------
-- Other
-- A left inverse is also known as a “split surjection”.
--
-- As the name implies, a split surjection is a special kind of
-- surjection where the witness generated in the domain in the
-- function for elements `x₁` and `x₂` are equal if `x₁ ≈ x₂` .
--
-- The difference is the `from-cong` law --- generally, the section
-- (called `Surjection.from` or `SplitSurjection.from`) of a surjection
-- need not respect equality, whereas it must in a split surjection.
--
-- The two notions coincide when the equivalence relation on `B` is
-- propositional equality (because all functions respect propositional
-- equality).
--
-- For further background on (split) surjections, one may consult any
-- general mathematical references which work without the principle
-- of choice. For example:
--
-- https://ncatlab.org/nlab/show/split+epimorphism.
--
-- The connection to set-theoretic notions with the same names is
-- justified by the setoid type theory/homotopy type theory
-- observation/definition that (∃x : A. P) = ∥ Σx : A. P ∥ --- i.e.,
-- we can read set-theoretic ∃ as squashed/propositionally truncated Σ.
--
-- We see working with setoids as working in the MLTT model of a setoid
-- type theory, in which ∥ X ∥ is interpreted as the setoid with carrier
-- set X and the equivalence relation that relates all elements.
-- All maps into ∥ X ∥ respect equality, so in the idiomatic definitions
-- here, we drop the corresponding trivial `cong` field completely.
SplitSurjection : Set _
SplitSurjection = LeftInverse
module SplitSurjection (splitSurjection : SplitSurjection) =
LeftInverse splitSurjection
------------------------------------------------------------------------
-- Infix abbreviations for oft-used items
------------------------------------------------------------------------
-- Same naming convention as used for propositional equality below, with
-- appended ₛ (for 'S'etoid).
infixr 0 _⟶ₛ_
_⟶ₛ_ : Setoid a ℓ₁ → Setoid b ℓ₂ → Set _
_⟶ₛ_ = Func
------------------------------------------------------------------------
-- Bundles specialised for propositional equality
------------------------------------------------------------------------
infix 3 _⟶_ _↣_ _↠_ _⤖_ _⇔_ _↩_ _↪_ _↩↪_ _↔_
_⟶_ : Set a → Set b → Set _
A ⟶ B = Func (≡.setoid A) (≡.setoid B)
_↣_ : Set a → Set b → Set _
A ↣ B = Injection (≡.setoid A) (≡.setoid B)
_↠_ : Set a → Set b → Set _
A ↠ B = Surjection (≡.setoid A) (≡.setoid B)
_⤖_ : Set a → Set b → Set _
A ⤖ B = Bijection (≡.setoid A) (≡.setoid B)
_⇔_ : Set a → Set b → Set _
A ⇔ B = Equivalence (≡.setoid A) (≡.setoid B)
_↩_ : Set a → Set b → Set _
A ↩ B = LeftInverse (≡.setoid A) (≡.setoid B)
_↪_ : Set a → Set b → Set _
A ↪ B = RightInverse (≡.setoid A) (≡.setoid B)
_↩↪_ : Set a → Set b → Set _
A ↩↪ B = BiInverse (≡.setoid A) (≡.setoid B)
_↔_ : Set a → Set b → Set _
A ↔ B = Inverse (≡.setoid A) (≡.setoid B)
-- We now define some constructors for the above that
-- automatically provide the required congruency proofs.
module _ {A : Set a} {B : Set b} where
mk⟶ : (A → B) → A ⟶ B
mk⟶ to = record
{ to = to
; cong = ≡.cong to
}
mk↣ : ∀ {to : A → B} → Injective _≡_ _≡_ to → A ↣ B
mk↣ {to} inj = record
{ to = to
; cong = ≡.cong to
; injective = inj
}
mk↠ : ∀ {to : A → B} → Surjective _≡_ _≡_ to → A ↠ B
mk↠ {to} surj = record
{ to = to
; cong = ≡.cong to
; surjective = surj
}
mk⤖ : ∀ {to : A → B} → Bijective _≡_ _≡_ to → A ⤖ B
mk⤖ {to} bij = record
{ to = to
; cong = ≡.cong to
; bijective = bij
}
mk⇔ : ∀ (to : A → B) (from : B → A) → A ⇔ B
mk⇔ to from = record
{ to = to
; from = from
; to-cong = ≡.cong to
; from-cong = ≡.cong from
}
mk↩ : ∀ {to : A → B} {from : B → A} → Inverseˡ _≡_ _≡_ to from → A ↩ B
mk↩ {to} {from} invˡ = record
{ to = to
; from = from
; to-cong = ≡.cong to
; from-cong = ≡.cong from
; inverseˡ = invˡ
}
mk↪ : ∀ {to : A → B} {from : B → A} → Inverseʳ _≡_ _≡_ to from → A ↪ B
mk↪ {to} {from} invʳ = record
{ to = to
; from = from
; to-cong = ≡.cong to
; from-cong = ≡.cong from
; inverseʳ = invʳ
}
mk↩↪ : ∀ {to : A → B} {from₁ : B → A} {from₂ : B → A} →
Inverseˡ _≡_ _≡_ to from₁ → Inverseʳ _≡_ _≡_ to from₂ → A ↩↪ B
mk↩↪ {to} {from₁} {from₂} invˡ invʳ = record
{ to = to
; from₁ = from₁
; from₂ = from₂
; to-cong = ≡.cong to
; from₁-cong = ≡.cong from₁
; from₂-cong = ≡.cong from₂
; inverseˡ = invˡ
; inverseʳ = invʳ
}
mk↔ : ∀ {to : A → B} {from : B → A} → Inverseᵇ _≡_ _≡_ to from → A ↔ B
mk↔ {to} {from} inv = record
{ to = to
; from = from
; to-cong = ≡.cong to
; from-cong = ≡.cong from
; inverse = inv
}
-- Strict variant of the above.
mk↠ₛ : ∀ {to : A → B} → StrictlySurjective _≡_ to → A ↠ B
mk↠ₛ = mk↠ ∘ strictlySurjective⇒surjective
mk↔ₛ′ : ∀ (to : A → B) (from : B → A) →
StrictlyInverseˡ _≡_ to from →
StrictlyInverseʳ _≡_ to from →
A ↔ B
mk↔ₛ′ to from invˡ invʳ = mk↔ {to} {from}
( strictlyInverseˡ⇒inverseˡ to invˡ
, strictlyInverseʳ⇒inverseʳ to invʳ
)
------------------------------------------------------------------------
-- Other
------------------------------------------------------------------------
-- Alternative syntax for the application of functions
module _ {From : Setoid a ℓ₁} {To : Setoid b ℓ₂} where
open Setoid
infixl 5 _⟨$⟩_
_⟨$⟩_ : Func From To → Carrier From → Carrier To
_⟨$⟩_ = Func.to