Debian Codegraph @

src/Function/Bundles.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Bundles for types of functions
5 ------------------------------------------------------------------------
6
7 -- The contents of this file should usually be accessed from `Function`.
8
9 -- Note that these bundles differ from those found elsewhere in other
10 -- library hierarchies as they take Setoids as parameters. This is
11 -- because a function is of no use without knowing what its domain and
12 -- codomain is, as well which equalities are being considered over them.
13 -- One consequence of this is that they are not built from the
14 -- definitions found in `Function.Structures` as is usually the case in
15 -- other library hierarchies, as this would duplicate the equality
16 -- axioms.
17
18 {-# OPTIONS --cubical-compatible --safe #-}
19
20 module Function.Bundles where
21
22 open import Function.Base using (_∘_)
23 open import Function.Definitions
24 import Function.Structures as FunctionStructures
25 open import Level using (Level; _⊔_; suc)
26 open import Data.Product.Base using (_,_; proj₁; proj₂)
27 open import Relation.Binary.Bundles using (Setoid)
28 open import Relation.Binary.Core using (_Preserves_⟶_)
29 open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_)
30 import Relation.Binary.PropositionalEquality.Properties as ≡
31 open import Function.Consequences.Propositional
32 open Setoid using (isEquivalence)
33
34 private
35 variable
36 a b ℓ₁ ℓ₂ : Level
37
38 ------------------------------------------------------------------------
39 -- Setoid bundles
40 ------------------------------------------------------------------------
41
42 module _ (From : Setoid a ℓ₁) (To : Setoid b ℓ₂) where
43
44 open Setoid From using () renaming (Carrier to A; _≈_ to _≈₁_)
45 open Setoid To using () renaming (Carrier to B; _≈_ to _≈₂_)
46 open FunctionStructures _≈₁_ _≈₂_
47
48 ------------------------------------------------------------------------
49 -- Bundles with one element
50
51 -- Called `Func` rather than `Function` in order to avoid clashing
52 -- with the top-level module.
53 record Func : Set (a ⊔ b ⊔ ℓ₁ ⊔ ℓ₂) where
54 field
55 to : A → B
56 cong : Congruent _≈₁_ _≈₂_ to
57
58 isCongruent : IsCongruent to
59 isCongruent = record
60 { cong = cong
61 ; isEquivalence₁ = isEquivalence From
62 ; isEquivalence₂ = isEquivalence To
63 }
64
65 open IsCongruent isCongruent public
66 using (module Eq₁; module Eq₂)
67
68
69 record Injection : Set (a ⊔ b ⊔ ℓ₁ ⊔ ℓ₂) where
70 field
71 to : A → B
72 cong : Congruent _≈₁_ _≈₂_ to
73 injective : Injective _≈₁_ _≈₂_ to
74
75 function : Func
76 function = record
77 { to = to
78 ; cong = cong
79 }
80
81 open Func function public
82 hiding (to; cong)
83
84 isInjection : IsInjection to
85 isInjection = record
86 { isCongruent = isCongruent
87 ; injective = injective
88 }
89
90
91 record Surjection : Set (a ⊔ b ⊔ ℓ₁ ⊔ ℓ₂) where
92 field
93 to : A → B
94 cong : Congruent _≈₁_ _≈₂_ to
95 surjective : Surjective _≈₁_ _≈₂_ to
96
97 function : Func
98 function = record
99 { to = to
100 ; cong = cong
101 }
102
103 open Func function public
104 hiding (to; cong)
105
106 isSurjection : IsSurjection to
107 isSurjection = record
108 { isCongruent = isCongruent
109 ; surjective = surjective
110 }
111
112 open IsSurjection isSurjection public
113 using
114 ( strictlySurjective
115 )
116
117 to⁻ : B → A
118 to⁻ = proj₁ ∘ surjective
119
120 to∘to⁻ : ∀ x → to (to⁻ x) ≈₂ x
121 to∘to⁻ = proj₂ ∘ strictlySurjective
122
123
124 record Bijection : Set (a ⊔ b ⊔ ℓ₁ ⊔ ℓ₂) where
125 field
126 to : A → B
127 cong : Congruent _≈₁_ _≈₂_ to
128 bijective : Bijective _≈₁_ _≈₂_ to
129
130 injective : Injective _≈₁_ _≈₂_ to
131 injective = proj₁ bijective
132
133 surjective : Surjective _≈₁_ _≈₂_ to
134 surjective = proj₂ bijective
135
136 injection : Injection
137 injection = record
138 { cong = cong
139 ; injective = injective
140 }
141
142 surjection : Surjection
143 surjection = record
144 { cong = cong
145 ; surjective = surjective
146 }
147
148 open Injection injection public using (isInjection)
149 open Surjection surjection public using (isSurjection; to⁻; strictlySurjective)
150
151 isBijection : IsBijection to
152 isBijection = record
153 { isInjection = isInjection
154 ; surjective = surjective
155 }
156
157 open IsBijection isBijection public using (module Eq₁; module Eq₂)
158
159
160 ------------------------------------------------------------------------
161 -- Bundles with two elements
162
163 module _ (From : Setoid a ℓ₁) (To : Setoid b ℓ₂) where
164
165 open Setoid From using () renaming (Carrier to A; _≈_ to _≈₁_)
166 open Setoid To using () renaming (Carrier to B; _≈_ to _≈₂_)
167 open FunctionStructures _≈₁_ _≈₂_
168
169 record Equivalence : Set (a ⊔ b ⊔ ℓ₁ ⊔ ℓ₂) where
170 field
171 to : A → B
172 from : B → A
173 to-cong : Congruent _≈₁_ _≈₂_ to
174 from-cong : Congruent _≈₂_ _≈₁_ from
175
176 toFunction : Func From To
177 toFunction = record
178 { to = to
179 ; cong = to-cong
180 }
181
182 open Func toFunction public
183 using (module Eq₁; module Eq₂)
184 renaming (isCongruent to to-isCongruent)
185
186 fromFunction : Func To From
187 fromFunction = record
188 { to = from
189 ; cong = from-cong
190 }
191
192 open Func fromFunction public
193 using ()
194 renaming (isCongruent to from-isCongruent)
195
196
197 record LeftInverse : Set (a ⊔ b ⊔ ℓ₁ ⊔ ℓ₂) where
198 field
199 to : A → B
200 from : B → A
201 to-cong : Congruent _≈₁_ _≈₂_ to
202 from-cong : Congruent _≈₂_ _≈₁_ from
203 inverseˡ : Inverseˡ _≈₁_ _≈₂_ to from
204
205 isCongruent : IsCongruent to
206 isCongruent = record
207 { cong = to-cong
208 ; isEquivalence₁ = isEquivalence From
209 ; isEquivalence₂ = isEquivalence To
210 }
211
212 isLeftInverse : IsLeftInverse to from
213 isLeftInverse = record
214 { isCongruent = isCongruent
215 ; from-cong = from-cong
216 ; inverseˡ = inverseˡ
217 }
218
219 open IsLeftInverse isLeftInverse public
220 using (module Eq₁; module Eq₂; strictlyInverseˡ; isSurjection)
221
222 equivalence : Equivalence
223 equivalence = record
224 { to-cong = to-cong
225 ; from-cong = from-cong
226 }
227
228 isSplitSurjection : IsSplitSurjection to
229 isSplitSurjection = record
230 { from = from
231 ; isLeftInverse = isLeftInverse
232 }
233
234 surjection : Surjection From To
235 surjection = record
236 { to = to
237 ; cong = to-cong
238 ; surjective = λ y → from y , inverseˡ
239 }
240
241
242
243 record RightInverse : Set (a ⊔ b ⊔ ℓ₁ ⊔ ℓ₂) where
244 field
245 to : A → B
246 from : B → A
247 to-cong : Congruent _≈₁_ _≈₂_ to
248 from-cong : from Preserves _≈₂_ ⟶ _≈₁_
249 inverseʳ : Inverseʳ _≈₁_ _≈₂_ to from
250
251 isCongruent : IsCongruent to
252 isCongruent = record
253 { cong = to-cong
254 ; isEquivalence₁ = isEquivalence From
255 ; isEquivalence₂ = isEquivalence To
256 }
257
258 isRightInverse : IsRightInverse to from
259 isRightInverse = record
260 { isCongruent = isCongruent
261 ; from-cong = from-cong
262 ; inverseʳ = inverseʳ
263 }
264
265 open IsRightInverse isRightInverse public
266 using (module Eq₁; module Eq₂; strictlyInverseʳ)
267
268 equivalence : Equivalence
269 equivalence = record
270 { to-cong = to-cong
271 ; from-cong = from-cong
272 }
273
274
275 record Inverse : Set (a ⊔ b ⊔ ℓ₁ ⊔ ℓ₂) where
276 field
277 to : A → B
278 from : B → A
279 to-cong : Congruent _≈₁_ _≈₂_ to
280 from-cong : Congruent _≈₂_ _≈₁_ from
281 inverse : Inverseᵇ _≈₁_ _≈₂_ to from
282
283 inverseˡ : Inverseˡ _≈₁_ _≈₂_ to from
284 inverseˡ = proj₁ inverse
285
286 inverseʳ : Inverseʳ _≈₁_ _≈₂_ to from
287 inverseʳ = proj₂ inverse
288
289 leftInverse : LeftInverse
290 leftInverse = record
291 { to-cong = to-cong
292 ; from-cong = from-cong
293 ; inverseˡ = inverseˡ
294 }
295
296 rightInverse : RightInverse
297 rightInverse = record
298 { to-cong = to-cong
299 ; from-cong = from-cong
300 ; inverseʳ = inverseʳ
301 }
302
303 open LeftInverse leftInverse public using (isLeftInverse; strictlyInverseˡ)
304 open RightInverse rightInverse public using (isRightInverse; strictlyInverseʳ)
305
306 isInverse : IsInverse to from
307 isInverse = record
308 { isLeftInverse = isLeftInverse
309 ; inverseʳ = inverseʳ
310 }
311
312 open IsInverse isInverse public using (module Eq₁; module Eq₂)
313
314
315 ------------------------------------------------------------------------
316 -- Bundles with three elements
317
318 record BiEquivalence : Set (a ⊔ b ⊔ ℓ₁ ⊔ ℓ₂) where
319 field
320 to : A → B
321 from₁ : B → A
322 from₂ : B → A
323 to-cong : Congruent _≈₁_ _≈₂_ to
324 from₁-cong : Congruent _≈₂_ _≈₁_ from₁
325 from₂-cong : Congruent _≈₂_ _≈₁_ from₂
326
327
328 record BiInverse : Set (a ⊔ b ⊔ ℓ₁ ⊔ ℓ₂) where
329 field
330 to : A → B
331 from₁ : B → A
332 from₂ : B → A
333 to-cong : Congruent _≈₁_ _≈₂_ to
334 from₁-cong : Congruent _≈₂_ _≈₁_ from₁
335 from₂-cong : Congruent _≈₂_ _≈₁_ from₂
336 inverseˡ : Inverseˡ _≈₁_ _≈₂_ to from₁
337 inverseʳ : Inverseʳ _≈₁_ _≈₂_ to from₂
338
339 to-isCongruent : IsCongruent to
340 to-isCongruent = record
341 { cong = to-cong
342 ; isEquivalence₁ = isEquivalence From
343 ; isEquivalence₂ = isEquivalence To
344 }
345
346 isBiInverse : IsBiInverse to from₁ from₂
347 isBiInverse = record
348 { to-isCongruent = to-isCongruent
349 ; from₁-cong = from₁-cong
350 ; from₂-cong = from₂-cong
351 ; inverseˡ = inverseˡ
352 ; inverseʳ = inverseʳ
353 }
354
355 biEquivalence : BiEquivalence
356 biEquivalence = record
357 { to-cong = to-cong
358 ; from₁-cong = from₁-cong
359 ; from₂-cong = from₂-cong
360 }
361
362 ------------------------------------------------------------------------
363 -- Other
364
365 -- A left inverse is also known as a “split surjection”.
366 --
367 -- As the name implies, a split surjection is a special kind of
368 -- surjection where the witness generated in the domain in the
369 -- function for elements `x₁` and `x₂` are equal if `x₁ ≈ x₂` .
370 --
371 -- The difference is the `from-cong` law --- generally, the section
372 -- (called `Surjection.to⁻` or `SplitSurjection.from`) of a surjection
373 -- need not respect equality, whereas it must in a split surjection.
374 --
375 -- The two notions coincide when the equivalence relation on `B` is
376 -- propositional equality (because all functions respect propositional
377 -- equality).
378 --
379 -- For further background on (split) surjections, one may consult any
380 -- general mathematical references which work without the principle
381 -- of choice. For example:
382 --
383 -- https://ncatlab.org/nlab/show/split+epimorphism.
384 --
385 -- The connection to set-theoretic notions with the same names is
386 -- justified by the setoid type theory/homotopy type theory
387 -- observation/definition that (∃x : A. P) = ∥ Σx : A. P ∥ --- i.e.,
388 -- we can read set-theoretic ∃ as squashed/propositionally truncated Σ.
389 --
390 -- We see working with setoids as working in the MLTT model of a setoid
391 -- type theory, in which ∥ X ∥ is interpreted as the setoid with carrier
392 -- set X and the equivalence relation that relates all elements.
393 -- All maps into ∥ X ∥ respect equality, so in the idiomatic definitions
394 -- here, we drop the corresponding trivial `cong` field completely.
395
396 SplitSurjection : Set _
397 SplitSurjection = LeftInverse
398
399 module SplitSurjection (splitSurjection : SplitSurjection) =
400 LeftInverse splitSurjection
401
402 ------------------------------------------------------------------------
403 -- Infix abbreviations for oft-used items
404 ------------------------------------------------------------------------
405
406 -- Same naming convention as used for propositional equality below, with
407 -- appended ₛ (for 'S'etoid).
408
409 infixr 0 _⟶ₛ_
410 _⟶ₛ_ : Setoid a ℓ₁ → Setoid b ℓ₂ → Set _
411 _⟶ₛ_ = Func
412
413 ------------------------------------------------------------------------
414 -- Bundles specialised for propositional equality
415 ------------------------------------------------------------------------
416
417 infix 3 _⟶_ _↣_ _↠_ _⤖_ _⇔_ _↩_ _↪_ _↩↪_ _↔_
418 _⟶_ : Set a → Set b → Set _
419 A ⟶ B = Func (≡.setoid A) (≡.setoid B)
420
421 _↣_ : Set a → Set b → Set _
422 A ↣ B = Injection (≡.setoid A) (≡.setoid B)
423
424 _↠_ : Set a → Set b → Set _
425 A ↠ B = Surjection (≡.setoid A) (≡.setoid B)
426
427 _⤖_ : Set a → Set b → Set _
428 A ⤖ B = Bijection (≡.setoid A) (≡.setoid B)
429
430 _⇔_ : Set a → Set b → Set _
431 A ⇔ B = Equivalence (≡.setoid A) (≡.setoid B)
432
433 _↩_ : Set a → Set b → Set _
434 A ↩ B = LeftInverse (≡.setoid A) (≡.setoid B)
435
436 _↪_ : Set a → Set b → Set _
437 A ↪ B = RightInverse (≡.setoid A) (≡.setoid B)
438
439 _↩↪_ : Set a → Set b → Set _
440 A ↩↪ B = BiInverse (≡.setoid A) (≡.setoid B)
441
442 _↔_ : Set a → Set b → Set _
443 A ↔ B = Inverse (≡.setoid A) (≡.setoid B)
444
445 -- We now define some constructors for the above that
446 -- automatically provide the required congruency proofs.
447
448 module _ {A : Set a} {B : Set b} where
449
450 mk⟶ : (A → B) → A ⟶ B
451 mk⟶ to = record
452 { to = to
453 ; cong = ≡.cong to
454 }
455
456 mk↣ : ∀ {to : A → B} → Injective _≡_ _≡_ to → A ↣ B
457 mk↣ {to} inj = record
458 { to = to
459 ; cong = ≡.cong to
460 ; injective = inj
461 }
462
463 mk↠ : ∀ {to : A → B} → Surjective _≡_ _≡_ to → A ↠ B
464 mk↠ {to} surj = record
465 { to = to
466 ; cong = ≡.cong to
467 ; surjective = surj
468 }
469
470 mk⤖ : ∀ {to : A → B} → Bijective _≡_ _≡_ to → A ⤖ B
471 mk⤖ {to} bij = record
472 { to = to
473 ; cong = ≡.cong to
474 ; bijective = bij
475 }
476
477 mk⇔ : ∀ (to : A → B) (from : B → A) → A ⇔ B
478 mk⇔ to from = record
479 { to = to
480 ; from = from
481 ; to-cong = ≡.cong to
482 ; from-cong = ≡.cong from
483 }
484
485 mk↩ : ∀ {to : A → B} {from : B → A} → Inverseˡ _≡_ _≡_ to from → A ↩ B
486 mk↩ {to} {from} invˡ = record
487 { to = to
488 ; from = from
489 ; to-cong = ≡.cong to
490 ; from-cong = ≡.cong from
491 ; inverseˡ = invˡ
492 }
493
494 mk↪ : ∀ {to : A → B} {from : B → A} → Inverseʳ _≡_ _≡_ to from → A ↪ B
495 mk↪ {to} {from} invʳ = record
496 { to = to
497 ; from = from
498 ; to-cong = ≡.cong to
499 ; from-cong = ≡.cong from
500 ; inverseʳ = invʳ
501 }
502
503 mk↩↪ : ∀ {to : A → B} {from₁ : B → A} {from₂ : B → A} →
504 Inverseˡ _≡_ _≡_ to from₁ → Inverseʳ _≡_ _≡_ to from₂ → A ↩↪ B
505 mk↩↪ {to} {from₁} {from₂} invˡ invʳ = record
506 { to = to
507 ; from₁ = from₁
508 ; from₂ = from₂
509 ; to-cong = ≡.cong to
510 ; from₁-cong = ≡.cong from₁
511 ; from₂-cong = ≡.cong from₂
512 ; inverseˡ = invˡ
513 ; inverseʳ = invʳ
514 }
515
516 mk↔ : ∀ {to : A → B} {from : B → A} → Inverseᵇ _≡_ _≡_ to from → A ↔ B
517 mk↔ {to} {from} inv = record
518 { to = to
519 ; from = from
520 ; to-cong = ≡.cong to
521 ; from-cong = ≡.cong from
522 ; inverse = inv
523 }
524
525
526 -- Strict variant of the above.
527 mk↠ₛ : ∀ {to : A → B} → StrictlySurjective _≡_ to → A ↠ B
528 mk↠ₛ = mk↠ ∘ strictlySurjective⇒surjective
529
530 mk↔ₛ′ : ∀ (to : A → B) (from : B → A) →
531 StrictlyInverseˡ _≡_ to from →
532 StrictlyInverseʳ _≡_ to from →
533 A ↔ B
534 mk↔ₛ′ to from invˡ invʳ = mk↔ {to} {from}
535 ( strictlyInverseˡ⇒inverseˡ to invˡ
536 , strictlyInverseʳ⇒inverseʳ to invʳ
537 )
538
539 ------------------------------------------------------------------------
540 -- Other
541 ------------------------------------------------------------------------
542
543 -- Alternative syntax for the application of functions
544
545 module _ {From : Setoid a ℓ₁} {To : Setoid b ℓ₂} where
546 open Setoid
547
548 infixl 5 _⟨$⟩_
549 _⟨$⟩_ : Func From To → Carrier From → Carrier To
550 _⟨$⟩_ = Func.to