Debian Codegraph @

src/Function/Construct/Composition.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Composition of functional properties
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Function.Construct.Composition where
10
11 open import Data.Product.Base as Product using (_,_)
12 open import Function.Base using (_∘_; flip)
13 open import Function.Bundles
14 open import Function.Definitions
15 using (Congruent; Injective; Surjective; Bijective; Inverseˡ; Inverseʳ
16 ; Inverseᵇ)
17 open import Function.Structures
18 using (IsCongruent; IsInjection; IsSurjection ; IsBijection; IsLeftInverse
19 ; IsRightInverse; IsInverse)
20 open import Level using (Level)
21 open import Relation.Binary.Core using (Rel)
22 open import Relation.Binary.Bundles using (Setoid)
23 open import Relation.Binary.Definitions using (Transitive)
24
25 private
26 variable
27 a b c ℓ₁ ℓ₂ ℓ₃ : Level
28 A B C : Set a
29
30 ------------------------------------------------------------------------
31 -- Properties
32
33 module _ (≈₁ : Rel A ℓ₁) (≈₂ : Rel B ℓ₂) (≈₃ : Rel C ℓ₃)
34 {f : A → B} {g : B → C}
35 where
36
37 congruent : Congruent ≈₁ ≈₂ f → Congruent ≈₂ ≈₃ g →
38 Congruent ≈₁ ≈₃ (g ∘ f)
39 congruent f-cong g-cong = g-cong ∘ f-cong
40
41 injective : Injective ≈₁ ≈₂ f → Injective ≈₂ ≈₃ g →
42 Injective ≈₁ ≈₃ (g ∘ f)
43 injective f-inj g-inj = f-inj ∘ g-inj
44
45 surjective : Surjective ≈₁ ≈₂ f → Surjective ≈₂ ≈₃ g →
46 Surjective ≈₁ ≈₃ (g ∘ f)
47 surjective f-sur g-sur x with g-sur x
48 ... | y , gproof with f-sur y
49 ... | z , fproof = z , gproof ∘ fproof
50
51 bijective : Bijective ≈₁ ≈₂ f → Bijective ≈₂ ≈₃ g →
52 Bijective ≈₁ ≈₃ (g ∘ f)
53 bijective = Product.zip′ injective surjective
54
55 module _ (≈₁ : Rel A ℓ₁) (≈₂ : Rel B ℓ₂) (≈₃ : Rel C ℓ₃)
56 {f : A → B} {f⁻¹ : B → A} {g : B → C} {g⁻¹ : C → B}
57 where
58
59 inverseˡ : Inverseˡ ≈₁ ≈₂ f f⁻¹ → Inverseˡ ≈₂ ≈₃ g g⁻¹ →
60 Inverseˡ ≈₁ ≈₃ (g ∘ f) (f⁻¹ ∘ g⁻¹)
61 inverseˡ f-inv g-inv = g-inv ∘ f-inv
62
63 inverseʳ : Inverseʳ ≈₁ ≈₂ f f⁻¹ → Inverseʳ ≈₂ ≈₃ g g⁻¹ →
64 Inverseʳ ≈₁ ≈₃ (g ∘ f) (f⁻¹ ∘ g⁻¹)
65 inverseʳ f-inv g-inv = f-inv ∘ g-inv
66
67 inverseᵇ : Inverseᵇ ≈₁ ≈₂ f f⁻¹ → Inverseᵇ ≈₂ ≈₃ g g⁻¹ →
68 Inverseᵇ ≈₁ ≈₃ (g ∘ f) (f⁻¹ ∘ g⁻¹)
69 inverseᵇ = Product.zip′ inverseˡ inverseʳ
70
71 ------------------------------------------------------------------------
72 -- Structures
73
74 module _ {≈₁ : Rel A ℓ₁} {≈₂ : Rel B ℓ₂} {≈₃ : Rel C ℓ₃}
75 {f : A → B} {g : B → C}
76 where
77
78 isCongruent : IsCongruent ≈₁ ≈₂ f → IsCongruent ≈₂ ≈₃ g →
79 IsCongruent ≈₁ ≈₃ (g ∘ f)
80 isCongruent f-cong g-cong = record
81 { cong = G.cong ∘ F.cong
82 ; isEquivalence₁ = F.isEquivalence₁
83 ; isEquivalence₂ = G.isEquivalence₂
84 } where module F = IsCongruent f-cong; module G = IsCongruent g-cong
85
86 isInjection : IsInjection ≈₁ ≈₂ f → IsInjection ≈₂ ≈₃ g →
87 IsInjection ≈₁ ≈₃ (g ∘ f)
88 isInjection f-inj g-inj = record
89 { isCongruent = isCongruent F.isCongruent G.isCongruent
90 ; injective = injective ≈₁ ≈₂ ≈₃ F.injective G.injective
91 } where module F = IsInjection f-inj; module G = IsInjection g-inj
92
93 isSurjection : IsSurjection ≈₁ ≈₂ f → IsSurjection ≈₂ ≈₃ g →
94 IsSurjection ≈₁ ≈₃ (g ∘ f)
95 isSurjection f-surj g-surj = record
96 { isCongruent = isCongruent F.isCongruent G.isCongruent
97 ; surjective = surjective ≈₁ ≈₂ ≈₃ F.surjective G.surjective
98 } where module F = IsSurjection f-surj; module G = IsSurjection g-surj
99
100 isBijection : IsBijection ≈₁ ≈₂ f → IsBijection ≈₂ ≈₃ g →
101 IsBijection ≈₁ ≈₃ (g ∘ f)
102 isBijection f-bij g-bij = record
103 { isInjection = isInjection F.isInjection G.isInjection
104 ; surjective = surjective ≈₁ ≈₂ ≈₃ F.surjective G.surjective
105 } where module F = IsBijection f-bij; module G = IsBijection g-bij
106
107 module _ {≈₁ : Rel A ℓ₁} {≈₂ : Rel B ℓ₂} {≈₃ : Rel C ℓ₃}
108 {f : A → B} {g : B → C} {f⁻¹ : B → A} {g⁻¹ : C → B}
109 where
110
111 isLeftInverse : IsLeftInverse ≈₁ ≈₂ f f⁻¹ → IsLeftInverse ≈₂ ≈₃ g g⁻¹ →
112 IsLeftInverse ≈₁ ≈₃ (g ∘ f) (f⁻¹ ∘ g⁻¹)
113 isLeftInverse f-invˡ g-invˡ = record
114 { isCongruent = isCongruent F.isCongruent G.isCongruent
115 ; from-cong = congruent ≈₃ ≈₂ ≈₁ G.from-cong F.from-cong
116 ; inverseˡ = inverseˡ ≈₁ ≈₂ ≈₃ F.inverseˡ G.inverseˡ
117 } where module F = IsLeftInverse f-invˡ; module G = IsLeftInverse g-invˡ
118
119 isRightInverse : IsRightInverse ≈₁ ≈₂ f f⁻¹ → IsRightInverse ≈₂ ≈₃ g g⁻¹ →
120 IsRightInverse ≈₁ ≈₃ (g ∘ f) (f⁻¹ ∘ g⁻¹)
121 isRightInverse f-invʳ g-invʳ = record
122 { isCongruent = isCongruent F.isCongruent G.isCongruent
123 ; from-cong = congruent ≈₃ ≈₂ ≈₁ G.from-cong F.from-cong
124 ; inverseʳ = inverseʳ ≈₁ ≈₂ ≈₃ F.inverseʳ G.inverseʳ
125 } where module F = IsRightInverse f-invʳ; module G = IsRightInverse g-invʳ
126
127 isInverse : IsInverse ≈₁ ≈₂ f f⁻¹ → IsInverse ≈₂ ≈₃ g g⁻¹ →
128 IsInverse ≈₁ ≈₃ (g ∘ f) (f⁻¹ ∘ g⁻¹)
129 isInverse f-inv g-inv = record
130 { isLeftInverse = isLeftInverse F.isLeftInverse G.isLeftInverse
131 ; inverseʳ = inverseʳ ≈₁ ≈₂ ≈₃ F.inverseʳ G.inverseʳ
132 } where module F = IsInverse f-inv; module G = IsInverse g-inv
133
134 ------------------------------------------------------------------------
135 -- Setoid bundles
136
137 module _ {R : Setoid a ℓ₁} {S : Setoid b ℓ₂} {T : Setoid c ℓ₃} where
138
139 open Setoid renaming (_≈_ to ≈)
140
141 function : Func R S → Func S T → Func R T
142 function f g = record
143 { to = G.to ∘ F.to
144 ; cong = congruent (≈ R) (≈ S) (≈ T) F.cong G.cong
145 } where module F = Func f; module G = Func g
146
147 injection : Injection R S → Injection S T → Injection R T
148 injection inj₁ inj₂ = record
149 { to = G.to ∘ F.to
150 ; cong = congruent (≈ R) (≈ S) (≈ T) F.cong G.cong
151 ; injective = injective (≈ R) (≈ S) (≈ T) F.injective G.injective
152 } where module F = Injection inj₁; module G = Injection inj₂
153
154 surjection : Surjection R S → Surjection S T → Surjection R T
155 surjection surj₁ surj₂ = record
156 { to = G.to ∘ F.to
157 ; cong = congruent (≈ R) (≈ S) (≈ T) F.cong G.cong
158 ; surjective = surjective (≈ R) (≈ S) (≈ T) F.surjective G.surjective
159 } where module F = Surjection surj₁; module G = Surjection surj₂
160
161 bijection : Bijection R S → Bijection S T → Bijection R T
162 bijection bij₁ bij₂ = record
163 { to = G.to ∘ F.to
164 ; cong = congruent (≈ R) (≈ S) (≈ T) F.cong G.cong
165 ; bijective = bijective (≈ R) (≈ S) (≈ T) F.bijective G.bijective
166 } where module F = Bijection bij₁; module G = Bijection bij₂
167
168 equivalence : Equivalence R S → Equivalence S T → Equivalence R T
169 equivalence equiv₁ equiv₂ = record
170 { to = G.to ∘ F.to
171 ; from = F.from ∘ G.from
172 ; to-cong = congruent (≈ R) (≈ S) (≈ T) F.to-cong G.to-cong
173 ; from-cong = congruent (≈ T) (≈ S) (≈ R) G.from-cong F.from-cong
174 } where module F = Equivalence equiv₁; module G = Equivalence equiv₂
175
176 leftInverse : LeftInverse R S → LeftInverse S T → LeftInverse R T
177 leftInverse invˡ₁ invˡ₂ = record
178 { to = G.to ∘ F.to
179 ; from = F.from ∘ G.from
180 ; to-cong = congruent (≈ R) (≈ S) (≈ T) F.to-cong G.to-cong
181 ; from-cong = congruent (≈ T) (≈ S) (≈ R) G.from-cong F.from-cong
182 ; inverseˡ = inverseˡ (≈ R) (≈ S) (≈ T) F.inverseˡ G.inverseˡ
183 } where module F = LeftInverse invˡ₁; module G = LeftInverse invˡ₂
184
185 rightInverse : RightInverse R S → RightInverse S T → RightInverse R T
186 rightInverse invʳ₁ invʳ₂ = record
187 { to = G.to ∘ F.to
188 ; from = F.from ∘ G.from
189 ; to-cong = congruent (≈ R) (≈ S) (≈ T) F.to-cong G.to-cong
190 ; from-cong = congruent (≈ T) (≈ S) (≈ R) G.from-cong F.from-cong
191 ; inverseʳ = inverseʳ (≈ R) (≈ S) (≈ T) F.inverseʳ G.inverseʳ
192 } where module F = RightInverse invʳ₁; module G = RightInverse invʳ₂
193
194 inverse : Inverse R S → Inverse S T → Inverse R T
195 inverse inv₁ inv₂ = record
196 { to = G.to ∘ F.to
197 ; from = F.from ∘ G.from
198 ; to-cong = congruent (≈ R) (≈ S) (≈ T) F.to-cong G.to-cong
199 ; from-cong = congruent (≈ T) (≈ S) (≈ R) G.from-cong F.from-cong
200 ; inverse = inverseᵇ (≈ R) (≈ S) (≈ T) F.inverse G.inverse
201 } where module F = Inverse inv₁; module G = Inverse inv₂
202
203 ------------------------------------------------------------------------
204 -- Propositional bundles
205
206 -- Notice the flipped order of the arguments to mirror composition.
207
208 infix 8 _⟶-∘_ _↣-∘_ _↠-∘_ _⤖-∘_ _⇔-∘_ _↩-∘_ _↪-∘_ _↔-∘_
209
210 _⟶-∘_ : (B ⟶ C) → (A ⟶ B) → (A ⟶ C)
211 _⟶-∘_ = flip function
212
213 _↣-∘_ : B ↣ C → A ↣ B → A ↣ C
214 _↣-∘_ = flip injection
215
216 _↠-∘_ : B ↠ C → A ↠ B → A ↠ C
217 _↠-∘_ = flip surjection
218
219 _⤖-∘_ : B ⤖ C → A ⤖ B → A ⤖ C
220 _⤖-∘_ = flip bijection
221
222 _⇔-∘_ : B ⇔ C → A ⇔ B → A ⇔ C
223 _⇔-∘_ = flip equivalence
224
225 _↩-∘_ : B ↩ C → A ↩ B → A ↩ C
226 _↩-∘_ = flip leftInverse
227
228 _↪-∘_ : B ↪ C → A ↪ B → A ↪ C
229 _↪-∘_ = flip rightInverse
230
231 _↔-∘_ : B ↔ C → A ↔ B → A ↔ C
232 _↔-∘_ = flip inverse
233
234
235 ------------------------------------------------------------------------
236 -- DEPRECATED NAMES
237 ------------------------------------------------------------------------
238 -- Please use the new names as continuing support for the old names is
239 -- not guaranteed.
240
241 -- Version v2.0
242
243 infix 8 _∘-⟶_ _∘-↣_ _∘-↠_ _∘-⤖_ _∘-⇔_ _∘-↩_ _∘-↪_ _∘-↔_
244
245 _∘-⟶_ = _⟶-∘_
246 {-# WARNING_ON_USAGE _∘-⟶_
247 "Warning: _∘-⟶_ was deprecated in v2.0.
248 Please use _⟶-∘_ instead."
249 #-}
250
251 _∘-↣_ = _↣-∘_
252 {-# WARNING_ON_USAGE _∘-↣_
253 "Warning: _∘-↣_ was deprecated in v2.0.
254 Please use _↣-∘_ instead."
255 #-}
256
257 _∘-↠_ = _↠-∘_
258 {-# WARNING_ON_USAGE _∘-↠_
259 "Warning: _∘-↠_ was deprecated in v2.0.
260 Please use _↠-∘_ instead."
261 #-}
262
263 _∘-⤖_ = _⤖-∘_
264 {-# WARNING_ON_USAGE _∘-⤖_
265 "Warning: _∘-⤖_ was deprecated in v2.0.
266 Please use _⤖-∘_ instead."
267 #-}
268
269 _∘-⇔_ = _⇔-∘_
270 {-# WARNING_ON_USAGE _∘-⇔_
271 "Warning: _∘-⇔_ was deprecated in v2.0.
272 Please use _⇔-∘_ instead."
273 #-}
274
275 _∘-↩_ = _↩-∘_
276 {-# WARNING_ON_USAGE _∘-↩_
277 "Warning: _∘-↩_ was deprecated in v2.0.
278 Please use _↩-∘_ instead."
279 #-}
280
281 _∘-↪_ = _↪-∘_
282 {-# WARNING_ON_USAGE _∘-↪_
283 "Warning: _∘-↪_ was deprecated in v2.0.
284 Please use _↪-∘_ instead."
285 #-}
286
287 _∘-↔_ = _↔-∘_
288 {-# WARNING_ON_USAGE _∘-↔_
289 "Warning: _∘-↔_ was deprecated in v2.0.
290 Please use _↔-∘_ instead."
291 #-}