Debian Codegraph @

src/Function/Construct/Symmetry.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Some functional properties are symmetric
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Function.Construct.Symmetry where
10
11 open import Data.Product.Base using (_,_; swap; proj₁; proj₂)
12 open import Function.Base using (_∘_)
13 open import Function.Definitions
14 using (Bijective; Injective; Surjective; Inverseˡ; Inverseʳ; Inverseᵇ
15 ; Congruent)
16 open import Function.Structures
17 using (IsBijection; IsCongruent; IsRightInverse; IsLeftInverse; IsInverse)
18 open import Function.Bundles
19 using (Bijection; Equivalence; LeftInverse; RightInverse; Inverse
20 ; _⤖_; _⇔_; _↩_; _↪_; _↔_)
21 open import Level using (Level)
22 open import Relation.Binary.Core using (Rel)
23 open import Relation.Binary.Definitions using (Reflexive; Symmetric; Transitive)
24 open import Relation.Binary.Bundles using (Setoid)
25 open import Relation.Binary.PropositionalEquality.Core using (_≡_; cong)
26 open import Relation.Binary.PropositionalEquality.Properties using (setoid)
27
28 private
29 variable
30 a b c ℓ₁ ℓ₂ ℓ₃ : Level
31 A B C : Set a
32
33 ------------------------------------------------------------------------
34 -- Properties
35
36 module _ {≈₁ : Rel A ℓ₁} {≈₂ : Rel B ℓ₂} {f : A → B}
37 ((inj , surj) : Bijective ≈₁ ≈₂ f)
38 where
39
40 private
41 f⁻¹ = proj₁ ∘ surj
42 f∘f⁻¹≡id = proj₂ ∘ surj
43
44 injective : Reflexive ≈₁ → Symmetric ≈₂ → Transitive ≈₂ →
45 Congruent ≈₁ ≈₂ f → Injective ≈₂ ≈₁ f⁻¹
46 injective refl sym trans cong gx≈gy =
47 trans (trans (sym (f∘f⁻¹≡id _ refl)) (cong gx≈gy)) (f∘f⁻¹≡id _ refl)
48
49 surjective : Reflexive ≈₁ → Transitive ≈₂ → Surjective ≈₂ ≈₁ f⁻¹
50 surjective refl trans x = f x , inj ∘ trans (f∘f⁻¹≡id _ refl)
51
52 bijective : Reflexive ≈₁ → Symmetric ≈₂ → Transitive ≈₂ →
53 Congruent ≈₁ ≈₂ f → Bijective ≈₂ ≈₁ f⁻¹
54 bijective refl sym trans cong = injective refl sym trans cong , surjective refl trans
55
56 module _ (≈₁ : Rel A ℓ₁) (≈₂ : Rel B ℓ₂) {f : A → B} {f⁻¹ : B → A} where
57
58 inverseʳ : Inverseˡ ≈₁ ≈₂ f f⁻¹ → Inverseʳ ≈₂ ≈₁ f⁻¹ f
59 inverseʳ inv = inv
60
61 inverseˡ : Inverseʳ ≈₁ ≈₂ f f⁻¹ → Inverseˡ ≈₂ ≈₁ f⁻¹ f
62 inverseˡ inv = inv
63
64 inverseᵇ : Inverseᵇ ≈₁ ≈₂ f f⁻¹ → Inverseᵇ ≈₂ ≈₁ f⁻¹ f
65 inverseᵇ (invˡ , invʳ) = (invʳ , invˡ)
66
67 ------------------------------------------------------------------------
68 -- Structures
69
70 module _ {≈₁ : Rel A ℓ₁} {≈₂ : Rel B ℓ₂}
71 {f : A → B} (isBij : IsBijection ≈₁ ≈₂ f)
72 where
73
74 private
75 module IB = IsBijection isBij
76 f⁻¹ = proj₁ ∘ IB.surjective
77
78 -- We can only flip a bijection if the witness produced by the
79 -- surjection proof respects the equality on the codomain.
80 isBijection : Congruent ≈₂ ≈₁ f⁻¹ → IsBijection ≈₂ ≈₁ f⁻¹
81 isBijection f⁻¹-cong = record
82 { isInjection = record
83 { isCongruent = record
84 { cong = f⁻¹-cong
85 ; isEquivalence₁ = IB.Eq₂.isEquivalence
86 ; isEquivalence₂ = IB.Eq₁.isEquivalence
87 }
88 ; injective = injective IB.bijective IB.Eq₁.refl IB.Eq₂.sym IB.Eq₂.trans IB.cong
89 }
90 ; surjective = surjective IB.bijective IB.Eq₁.refl IB.Eq₂.trans
91 }
92
93 module _ {≈₁ : Rel A ℓ₁} {f : A → B} (isBij : IsBijection ≈₁ _≡_ f) where
94
95 -- We can always flip a bijection if using the equality over the
96 -- codomain is propositional equality.
97 isBijection-≡ : IsBijection _≡_ ≈₁ _
98 isBijection-≡ = isBijection isBij (IB.Eq₁.reflexive ∘ cong _)
99 where module IB = IsBijection isBij
100
101 module _ {≈₁ : Rel A ℓ₁} {≈₂ : Rel B ℓ₂} {f : A → B} {f⁻¹ : B → A} where
102
103 isCongruent : IsCongruent ≈₁ ≈₂ f → Congruent ≈₂ ≈₁ f⁻¹ → IsCongruent ≈₂ ≈₁ f⁻¹
104 isCongruent ic cg = record
105 { cong = cg
106 ; isEquivalence₁ = F.isEquivalence₂
107 ; isEquivalence₂ = F.isEquivalence₁
108 } where module F = IsCongruent ic
109
110 isLeftInverse : IsRightInverse ≈₁ ≈₂ f f⁻¹ → IsLeftInverse ≈₂ ≈₁ f⁻¹ f
111 isLeftInverse inv = record
112 { isCongruent = isCongruent F.isCongruent F.from-cong
113 ; from-cong = F.to-cong
114 ; inverseˡ = inverseˡ ≈₁ ≈₂ F.inverseʳ
115 } where module F = IsRightInverse inv
116
117 isRightInverse : IsLeftInverse ≈₁ ≈₂ f f⁻¹ → IsRightInverse ≈₂ ≈₁ f⁻¹ f
118 isRightInverse inv = record
119 { isCongruent = isCongruent F.isCongruent F.from-cong
120 ; from-cong = F.to-cong
121 ; inverseʳ = inverseʳ ≈₁ ≈₂ F.inverseˡ
122 } where module F = IsLeftInverse inv
123
124 isInverse : IsInverse ≈₁ ≈₂ f f⁻¹ → IsInverse ≈₂ ≈₁ f⁻¹ f
125 isInverse f-inv = record
126 { isLeftInverse = isLeftInverse F.isRightInverse
127 ; inverseʳ = inverseʳ ≈₁ ≈₂ F.inverseˡ
128 } where module F = IsInverse f-inv
129
130 ------------------------------------------------------------------------
131 -- Setoid bundles
132
133 module _ {R : Setoid a ℓ₁} {S : Setoid b ℓ₂} (bij : Bijection R S) where
134
135 private
136 module IB = Bijection bij
137 from = proj₁ ∘ IB.surjective
138
139 -- We can only flip a bijection if the witness produced by the
140 -- surjection proof respects the equality on the codomain.
141 bijection : Congruent IB.Eq₂._≈_ IB.Eq₁._≈_ from → Bijection S R
142 bijection cong = record
143 { to = from
144 ; cong = cong
145 ; bijective = bijective IB.bijective IB.Eq₁.refl IB.Eq₂.sym IB.Eq₂.trans IB.cong
146 }
147
148 -- We can always flip a bijection if using the equality over the
149 -- codomain is propositional equality.
150 bijection-≡ : {R : Setoid a ℓ₁} {B : Set b} →
151 Bijection R (setoid B) → Bijection (setoid B) R
152 bijection-≡ bij = bijection bij (B.Eq₁.reflexive ∘ cong _)
153 where module B = Bijection bij
154
155 module _ {R : Setoid a ℓ₁} {S : Setoid b ℓ₂} where
156
157 equivalence : Equivalence R S → Equivalence S R
158 equivalence equiv = record
159 { to = E.from
160 ; from = E.to
161 ; to-cong = E.from-cong
162 ; from-cong = E.to-cong
163 } where module E = Equivalence equiv
164
165 rightInverse : LeftInverse R S → RightInverse S R
166 rightInverse left = record
167 { to = L.from
168 ; from = L.to
169 ; to-cong = L.from-cong
170 ; from-cong = L.to-cong
171 ; inverseʳ = L.inverseˡ
172 } where module L = LeftInverse left
173
174 leftInverse : RightInverse R S → LeftInverse S R
175 leftInverse right = record
176 { to = R.from
177 ; from = R.to
178 ; to-cong = R.from-cong
179 ; from-cong = R.to-cong
180 ; inverseˡ = R.inverseʳ
181 } where module R = RightInverse right
182
183 inverse : Inverse R S → Inverse S R
184 inverse inv = record
185 { to = I.from
186 ; from = I.to
187 ; to-cong = I.from-cong
188 ; from-cong = I.to-cong
189 ; inverse = swap I.inverse
190 } where module I = Inverse inv
191
192 ------------------------------------------------------------------------
193 -- Propositional bundles
194
195 ⤖-sym : A ⤖ B → B ⤖ A
196 ⤖-sym b = bijection b (cong _)
197
198 ⇔-sym : A ⇔ B → B ⇔ A
199 ⇔-sym = equivalence
200
201 ↩-sym : A ↩ B → B ↪ A
202 ↩-sym = rightInverse
203
204 ↪-sym : A ↪ B → B ↩ A
205 ↪-sym = leftInverse
206
207 ↔-sym : A ↔ B → B ↔ A
208 ↔-sym = inverse
209
210
211 ------------------------------------------------------------------------
212 -- DEPRECATED NAMES
213 ------------------------------------------------------------------------
214 -- Please use the new names as continuing support for the old names is
215 -- not guaranteed.
216
217 -- Version v2.0
218
219 sym-⤖ = ⤖-sym
220 {-# WARNING_ON_USAGE sym-⤖
221 "Warning: sym-⤖ was deprecated in v2.0.
222 Please use ⤖-sym instead."
223 #-}
224
225 sym-⇔ = ⇔-sym
226 {-# WARNING_ON_USAGE sym-⇔
227 "Warning: sym-⇔ was deprecated in v2.0.
228 Please use ⇔-sym instead."
229 #-}
230
231 sym-↩ = ↩-sym
232 {-# WARNING_ON_USAGE sym-↩
233 "Warning: sym-↩ was deprecated in v2.0.
234 Please use ↩-sym instead."
235 #-}
236
237 sym-↪ = ↪-sym
238 {-# WARNING_ON_USAGE sym-↪
239 "Warning: sym-↪ was deprecated in v2.0.
240 Please use ↪-sym instead."
241 #-}
242
243 sym-↔ = ↔-sym
244 {-# WARNING_ON_USAGE sym-↔
245 "Warning: sym-↔ was deprecated in v2.0.
246 Please use ↔-sym instead."
247 #-}