Debian Codegraph @

src/Function/Related/TypeIsomorphisms.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Basic lemmas showing that various types are related (isomorphic or
5 -- equivalent or…)
6 ------------------------------------------------------------------------
7
8 {-# OPTIONS --cubical-compatible --safe #-}
9
10 module Function.Related.TypeIsomorphisms where
11
12 open import Algebra.Bundles public
13 using (Magma; Semigroup; Monoid; CommutativeMonoid; CommutativeSemiring)
14 open import Algebra.Definitions
15 using (Identity; LeftIdentity; RightIdentity; Zero; LeftZero; RightZero
16 ; Associative; _DistributesOverˡ_; _DistributesOverʳ_; _DistributesOver_)
17 open import Algebra.Structures public
18 using (IsMagma; IsSemigroup; IsMonoid; IsCommutativeMonoid
19 ; IsCommutativeSemiring)
20 open import Algebra.Structures.Biased using (isCommutativeSemiringˡ)
21 open import Axiom.Extensionality.Propositional using (Extensionality)
22 open import Data.Bool.Base using (true; false)
23 open import Data.Empty.Polymorphic using (⊥; ⊥-elim)
24 open import Data.Product.Base as Product
25 using (_×_; Σ; curry; uncurry; _,_; -,_; <_,_>; proj₁; proj₂; ∃₂; ∃; ∃-syntax)
26 open import Data.Product.Function.NonDependent.Propositional
27 open import Data.Sum.Base as Sum
28 open import Data.Sum.Properties using (swap-involutive)
29 open import Data.Sum.Function.Propositional using (_⊎-cong_)
30 open import Data.Unit.Polymorphic.Base using (⊤)
31 open import Level using (Level; Lift; 0ℓ; suc)
32 open import Function.Base
33 open import Function.Bundles
34 open import Function.Related.Propositional
35 import Function.Construct.Identity as Identity
36 open import Relation.Binary hiding (_⇔_)
37 open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl; cong)
38 open import Relation.Binary.PropositionalEquality.Properties
39 using (module ≡-Reasoning)
40 open import Relation.Nullary.Negation.Core using (¬_)
41 import Relation.Nullary.Indexed as I
42
43 private
44 variable
45 a b c d : Level
46 A B C D : Set a
47
48 ------------------------------------------------------------------------
49 -- A lemma relating True dec and P, where dec : Dec P
50
51 open import Relation.Nullary.Decidable public
52 using ()
53 renaming (True-↔ to True↔)
54
55 ------------------------------------------------------------------------
56 -- Properties of Σ and _×_
57
58 -- Σ is associative
59 Σ-assoc : ∀ {A : Set a} {B : A → Set b} {C : (a : A) → B a → Set c} →
60 Σ (Σ A B) (uncurry C) ↔ Σ A (λ a → Σ (B a) (C a))
61 Σ-assoc = mk↔ₛ′ Product.assocʳ Product.assocˡ (λ _ → refl) (λ _ → refl)
62
63 -- × is commutative
64
65 ×-comm : ∀ (A : Set a) (B : Set b) → (A × B) ↔ (B × A)
66 ×-comm _ _ = mk↔ₛ′ Product.swap Product.swap (λ _ → refl) λ _ → refl
67
68 -- × has ⊤ as its identity
69
70 ×-identityˡ : ∀ ℓ → LeftIdentity {ℓ = ℓ} _↔_ ⊤ _×_
71 ×-identityˡ _ _ = mk↔ₛ′ proj₂ -,_ (λ _ → refl) (λ _ → refl)
72
73 ×-identityʳ : ∀ ℓ → RightIdentity {ℓ = ℓ} _↔_ ⊤ _×_
74 ×-identityʳ _ _ = mk↔ₛ′ proj₁ (_, _) (λ _ → refl) (λ _ → refl)
75
76 ×-identity : ∀ ℓ → Identity _↔_ ⊤ _×_
77 ×-identity ℓ = ×-identityˡ ℓ , ×-identityʳ ℓ
78
79 -- × has ⊥ has its zero
80
81 ×-zeroˡ : ∀ ℓ → LeftZero {ℓ = ℓ} _↔_ ⊥ _×_
82 ×-zeroˡ ℓ A = mk↔ₛ′ proj₁ < id , ⊥-elim > (λ _ → refl) (λ { () })
83
84 ×-zeroʳ : ∀ ℓ → RightZero {ℓ = ℓ} _↔_ ⊥ _×_
85 ×-zeroʳ ℓ A = mk↔ₛ′ proj₂ < ⊥-elim , id > (λ _ → refl) (λ { () })
86
87 ×-zero : ∀ ℓ → Zero _↔_ ⊥ _×_
88 ×-zero ℓ = ×-zeroˡ ℓ , ×-zeroʳ ℓ
89
90 ------------------------------------------------------------------------
91 -- Properties of ⊎
92
93 -- ⊎ is associative
94
95 ⊎-assoc : ∀ ℓ → Associative {ℓ = ℓ} _↔_ _⊎_
96 ⊎-assoc ℓ _ _ _ = mk↔ₛ′
97 [ [ inj₁ , inj₂ ∘′ inj₁ ]′ , inj₂ ∘′ inj₂ ]′
98 [ inj₁ ∘′ inj₁ , [ inj₁ ∘′ inj₂ , inj₂ ]′ ]′
99 [ (λ _ → refl) , [ (λ _ → refl) , (λ _ → refl) ] ]
100 [ [ (λ _ → refl) , (λ _ → refl) ] , (λ _ → refl) ]
101
102 -- ⊎ is commutative
103
104 ⊎-comm : ∀ (A : Set a) (B : Set b) → (A ⊎ B) ↔ (B ⊎ A)
105 ⊎-comm _ _ = mk↔ₛ′ swap swap swap-involutive swap-involutive
106
107 -- ⊎ has ⊥ as its identity
108
109 ⊎-identityˡ : ∀ ℓ → LeftIdentity _↔_ (⊥ {ℓ}) _⊎_
110 ⊎-identityˡ _ _ = mk↔ₛ′ [ (λ ()) , id ]′ inj₂ (λ _ → refl)
111 [ (λ ()) , (λ _ → refl) ]
112
113 ⊎-identityʳ : ∀ ℓ → RightIdentity _↔_ (⊥ {ℓ}) _⊎_
114 ⊎-identityʳ _ _ = mk↔ₛ′ [ id , (λ ()) ]′ inj₁ (λ _ → refl)
115 [ (λ _ → refl) , (λ ()) ]
116
117 ⊎-identity : ∀ ℓ → Identity _↔_ ⊥ _⊎_
118 ⊎-identity ℓ = ⊎-identityˡ ℓ , ⊎-identityʳ ℓ
119
120 ------------------------------------------------------------------------
121 -- Properties of Σ and ⊎
122
123 -- Σ distributes over ⊎
124
125 Σ-distribˡ-⊎ : {P : A → Set a} {Q : A → Set b} →
126 (∃ λ a → P a ⊎ Q a) ↔ (∃ P ⊎ ∃ Q)
127 Σ-distribˡ-⊎ = mk↔ₛ′
128 (uncurry λ x → [ inj₁ ∘′ (x ,_) , inj₂ ∘′ (x ,_) ]′)
129 [ Product.map₂ inj₁ , Product.map₂ inj₂ ]′
130 [ (λ _ → refl) , (λ _ → refl) ]
131 (uncurry λ _ → [ (λ _ → refl) , (λ _ → refl) ])
132
133 Σ-distribʳ-⊎ : {P : A ⊎ B → Set c} →
134 (Σ (A ⊎ B) P) ↔ (Σ A (P ∘ inj₁) ⊎ Σ B (P ∘ inj₂))
135 Σ-distribʳ-⊎ = mk↔ₛ′
136 (uncurry [ curry inj₁ , curry inj₂ ])
137 [ Product.dmap inj₁ id , Product.dmap inj₂ id ]
138 [ (λ _ → refl) , (λ _ → refl) ]
139 (uncurry [ (λ _ _ → refl) , (λ _ _ → refl) ])
140
141 ------------------------------------------------------------------------
142 -- Properties of × and ⊎
143
144 -- × distributes over ⊎
145 -- primed variants are less level polymorphic
146
147 ×-distribˡ-⊎ : (A × (B ⊎ C)) ↔ (A × B ⊎ A × C)
148 ×-distribˡ-⊎ = Σ-distribˡ-⊎
149
150 ×-distribˡ-⊎′ : ∀ ℓ → _DistributesOverˡ_ {ℓ = ℓ} _↔_ _×_ _⊎_
151 ×-distribˡ-⊎′ ℓ _ _ _ = ×-distribˡ-⊎
152
153 ×-distribʳ-⊎ : ((A ⊎ B) × C) ↔ (A × C ⊎ B × C)
154 ×-distribʳ-⊎ = Σ-distribʳ-⊎
155
156 ×-distribʳ-⊎′ : ∀ ℓ → _DistributesOverʳ_ {ℓ = ℓ} _↔_ _×_ _⊎_
157 ×-distribʳ-⊎′ ℓ _ _ _ = ×-distribʳ-⊎
158
159 ×-distrib-⊎′ : ∀ ℓ → _DistributesOver_ {ℓ = ℓ} _↔_ _×_ _⊎_
160 ×-distrib-⊎′ ℓ = ×-distribˡ-⊎′ ℓ , ×-distribʳ-⊎′ ℓ
161
162 ------------------------------------------------------------------------
163 -- ⊥, ⊤, _×_ and _⊎_ form a commutative semiring
164
165 -- ⊤, _×_ form a commutative monoid
166
167 ×-isMagma : ∀ k ℓ → IsMagma {Level.suc ℓ} (Related ⌊ k ⌋) _×_
168 ×-isMagma k ℓ = record
169 { isEquivalence = SK-isEquivalence k
170 ; ∙-cong = _×-cong_
171 }
172
173 ×-magma : SymmetricKind → (ℓ : Level) → Magma _ _
174 ×-magma k ℓ = record
175 { isMagma = ×-isMagma k ℓ
176 }
177
178 ×-isSemigroup : ∀ k ℓ → IsSemigroup {Level.suc ℓ} (Related ⌊ k ⌋) _×_
179 ×-isSemigroup k ℓ = record
180 { isMagma = ×-isMagma k ℓ
181 ; assoc = λ _ _ _ → ↔⇒ Σ-assoc
182 }
183
184 ×-semigroup : SymmetricKind → (ℓ : Level) → Semigroup _ _
185 ×-semigroup k ℓ = record
186 { isSemigroup = ×-isSemigroup k ℓ
187 }
188
189 ×-isMonoid : ∀ k ℓ → IsMonoid (Related ⌊ k ⌋) _×_ ⊤
190 ×-isMonoid k ℓ = record
191 { isSemigroup = ×-isSemigroup k ℓ
192 ; identity = (↔⇒ ∘ ×-identityˡ ℓ) , (↔⇒ ∘ ×-identityʳ ℓ)
193 }
194
195 ×-monoid : SymmetricKind → (ℓ : Level) → Monoid _ _
196 ×-monoid k ℓ = record
197 { isMonoid = ×-isMonoid k ℓ
198 }
199
200 ×-isCommutativeMonoid : ∀ k ℓ → IsCommutativeMonoid (Related ⌊ k ⌋) _×_ ⊤
201 ×-isCommutativeMonoid k ℓ = record
202 { isMonoid = ×-isMonoid k ℓ
203 ; comm = λ _ _ → ↔⇒ (×-comm _ _)
204 }
205
206 ×-commutativeMonoid : SymmetricKind → (ℓ : Level) → CommutativeMonoid _ _
207 ×-commutativeMonoid k ℓ = record
208 { isCommutativeMonoid = ×-isCommutativeMonoid k ℓ
209 }
210
211 -- ⊥, _⊎_ form a commutative monoid
212
213 ⊎-isMagma : ∀ k ℓ → IsMagma {Level.suc ℓ} (Related ⌊ k ⌋) _⊎_
214 ⊎-isMagma k ℓ = record
215 { isEquivalence = SK-isEquivalence k
216 ; ∙-cong = _⊎-cong_
217 }
218
219 ⊎-magma : SymmetricKind → (ℓ : Level) → Magma _ _
220 ⊎-magma k ℓ = record
221 { isMagma = ⊎-isMagma k ℓ
222 }
223
224 ⊎-isSemigroup : ∀ k ℓ → IsSemigroup {Level.suc ℓ} (Related ⌊ k ⌋) _⊎_
225 ⊎-isSemigroup k ℓ = record
226 { isMagma = ⊎-isMagma k ℓ
227 ; assoc = λ A B C → ↔⇒ (⊎-assoc ℓ A B C)
228 }
229
230 ⊎-semigroup : SymmetricKind → (ℓ : Level) → Semigroup _ _
231 ⊎-semigroup k ℓ = record
232 { isSemigroup = ⊎-isSemigroup k ℓ
233 }
234
235 ⊎-isMonoid : ∀ k ℓ → IsMonoid (Related ⌊ k ⌋) _⊎_ ⊥
236 ⊎-isMonoid k ℓ = record
237 { isSemigroup = ⊎-isSemigroup k ℓ
238 ; identity = (↔⇒ ∘ ⊎-identityˡ ℓ) , (↔⇒ ∘ ⊎-identityʳ ℓ)
239 }
240
241 ⊎-monoid : SymmetricKind → (ℓ : Level) → Monoid _ _
242 ⊎-monoid k ℓ = record
243 { isMonoid = ⊎-isMonoid k ℓ
244 }
245
246 ⊎-isCommutativeMonoid : ∀ k ℓ → IsCommutativeMonoid (Related ⌊ k ⌋) _⊎_ ⊥
247 ⊎-isCommutativeMonoid k ℓ = record
248 { isMonoid = ⊎-isMonoid k ℓ
249 ; comm = λ _ _ → ↔⇒ (⊎-comm _ _)
250 }
251
252 ⊎-commutativeMonoid : SymmetricKind → (ℓ : Level) →
253 CommutativeMonoid _ _
254 ⊎-commutativeMonoid k ℓ = record
255 { isCommutativeMonoid = ⊎-isCommutativeMonoid k ℓ
256 }
257
258 ×-⊎-isCommutativeSemiring : ∀ k ℓ →
259 IsCommutativeSemiring (Related ⌊ k ⌋) _⊎_ _×_ ⊥ ⊤
260 ×-⊎-isCommutativeSemiring k ℓ = isCommutativeSemiringˡ record
261 { +-isCommutativeMonoid = ⊎-isCommutativeMonoid k ℓ
262 ; *-isCommutativeMonoid = ×-isCommutativeMonoid k ℓ
263 ; distribʳ = λ _ _ _ → ↔⇒ ×-distribʳ-⊎
264 ; zeroˡ = ↔⇒ ∘ ×-zeroˡ ℓ
265 }
266
267 ×-⊎-commutativeSemiring : SymmetricKind → (ℓ : Level) →
268 CommutativeSemiring (Level.suc ℓ) ℓ
269 ×-⊎-commutativeSemiring k ℓ = record
270 { isCommutativeSemiring = ×-⊎-isCommutativeSemiring k ℓ
271 }
272
273 ------------------------------------------------------------------------
274 -- Some reordering lemmas
275
276 ΠΠ↔ΠΠ : ∀ {a b p} {A : Set a} {B : Set b} (P : A → B → Set p) →
277 ((x : A) (y : B) → P x y) ↔ ((y : B) (x : A) → P x y)
278 ΠΠ↔ΠΠ _ = mk↔ₛ′ flip flip (λ _ → refl) (λ _ → refl)
279
280 ∃∃↔∃∃ : ∀ {a b p} {A : Set a} {B : Set b} (P : A → B → Set p) →
281 (∃₂ λ x y → P x y) ↔ (∃₂ λ y x → P x y)
282 ∃∃↔∃∃ P = mk↔ₛ′ to from (λ _ → refl) (λ _ → refl)
283 where
284 to : (∃₂ λ x y → P x y) → (∃₂ λ y x → P x y)
285 to (x , y , Pxy) = (y , x , Pxy)
286
287 from : (∃₂ λ y x → P x y) → (∃₂ λ x y → P x y)
288 from (y , x , Pxy) = (x , y , Pxy)
289
290 ------------------------------------------------------------------------
291 -- Implicit and explicit function spaces are isomorphic
292
293 Π↔Π : ∀ {A : Set a} {B : A → Set b} →
294 ((x : A) → B x) ↔ ({x : A} → B x)
295 Π↔Π = mk↔ₛ′ _$- λ- (λ _ → refl) (λ _ → refl)
296
297 ------------------------------------------------------------------------
298 -- _→_ preserves the symmetric relations
299
300 →-cong-⇔ : A ⇔ B → C ⇔ D → (A → C) ⇔ (B → D)
301 →-cong-⇔ A⇔B C⇔D = mk⇔
302 (λ f → to C⇔D ∘ f ∘ from A⇔B)
303 (λ f → from C⇔D ∘ f ∘ to A⇔B)
304 where open Equivalence
305
306 →-cong-↔ : Extensionality a c → Extensionality b d →
307 {A : Set a} {B : Set b} {C : Set c} {D : Set d} →
308 A ↔ B → C ↔ D → (A → C) ↔ (B → D)
309 →-cong-↔ ext₁ ext₂ A↔B C↔D = mk↔ₛ′
310 (λ f → to C↔D ∘ f ∘ from A↔B)
311 (λ f → from C↔D ∘ f ∘ to A↔B)
312 (λ f → ext₂ λ x → begin
313 to C↔D (from C↔D (f (to A↔B (from A↔B x)))) ≡⟨ strictlyInverseˡ C↔D _ ⟩
314 f (to A↔B (from A↔B x)) ≡⟨ cong f $ strictlyInverseˡ A↔B x ⟩
315 f x ∎)
316 (λ f → ext₁ λ x → begin
317 from C↔D (to C↔D (f (from A↔B (to A↔B x)))) ≡⟨ strictlyInverseʳ C↔D _ ⟩
318 f (from A↔B (to A↔B x)) ≡⟨ cong f $ strictlyInverseʳ A↔B x ⟩
319 f x ∎)
320 where open Inverse; open ≡-Reasoning
321
322 →-cong : Extensionality a c → Extensionality b d →
323 ∀ {k} {A : Set a} {B : Set b} {C : Set c} {D : Set d} →
324 A ∼[ ⌊ k ⌋ ] B → C ∼[ ⌊ k ⌋ ] D → (A → C) ∼[ ⌊ k ⌋ ] (B → D)
325 →-cong extAC extBD {equivalence} = →-cong-⇔
326 →-cong extAC extBD {bijection} = →-cong-↔ extAC extBD
327
328 ------------------------------------------------------------------------
329 -- ¬_ (at Level 0) preserves the symmetric relations
330
331 ¬-cong-⇔ : A ⇔ B → (¬ A) ⇔ (¬ B)
332 ¬-cong-⇔ A⇔B = →-cong-⇔ A⇔B (Identity.⇔-id _)
333
334 ¬-cong : Extensionality a 0ℓ → Extensionality b 0ℓ →
335 ∀ {k} {A : Set a} {B : Set b} →
336 A ∼[ ⌊ k ⌋ ] B → (¬ A) ∼[ ⌊ k ⌋ ] (¬ B)
337 ¬-cong extA extB A≈B = →-cong extA extB A≈B (K-reflexive refl)
338
339 ------------------------------------------------------------------------
340 -- _⇔_ preserves _⇔_
341
342 -- The type of the following proof is a bit more general.
343
344 Related-cong :
345 ∀ {k} →
346 A ∼[ ⌊ k ⌋ ] B → C ∼[ ⌊ k ⌋ ] D → (A ∼[ ⌊ k ⌋ ] C) ⇔ (B ∼[ ⌊ k ⌋ ] D)
347 Related-cong {A = A} {B = B} {C = C} {D = D} A≈B C≈D = mk⇔
348 (λ A≈C → B ∼⟨ SK-sym A≈B ⟩
349 A ∼⟨ A≈C ⟩
350 C ∼⟨ C≈D ⟩
351 D ∎)
352 (λ B≈D → A ∼⟨ A≈B ⟩
353 B ∼⟨ B≈D ⟩
354 D ∼⟨ SK-sym C≈D ⟩
355 C ∎)
356 where open EquationalReasoning
357
358 ------------------------------------------------------------------------
359 -- Relating a predicate to an existentially quantified one with the
360 -- restriction that the quantified variable is equal to the given one
361
362 ∃-≡ : ∀ (P : A → Set b) {x} → P x ↔ (∃[ y ] y ≡ x × P y)
363 ∃-≡ P {x} = mk↔ₛ′ (λ Px → x , refl , Px) (λ where (_ , refl , Py) → Py)
364 (λ where (_ , refl , _) → refl) (λ where _ → refl)
365