Debian Codegraph @

src/Function/Related.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- This module is DEPRECATED.
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8 {-# OPTIONS --warning=noUserWarning #-}
9
10 module Function.Related where
11
12 {-# WARNING_ON_IMPORT
13 "Function.Related was deprecated in v2.0.
14 Use Function.Related.Propositional instead."
15 #-}
16
17 open import Level
18 open import Function.Base
19 open import Function.Equality using (_⟨$⟩_)
20 open import Function.Equivalence as Eq using (Equivalence)
21 open import Function.Injection as Inj using (Injection; _↣_)
22 open import Function.Inverse as Inv using (Inverse; _↔_)
23 open import Function.LeftInverse as LeftInv using (LeftInverse)
24 open import Function.Surjection as Surj using (Surjection)
25 open import Function.Consequences.Propositional
26 open import Relation.Binary.Core using (_⇒_)
27 open import Relation.Binary.Bundles using (Setoid; Preorder)
28 open import Relation.Binary.Structures using (IsEquivalence; IsPreorder)
29 open import Relation.Binary.Definitions using (Reflexive; Trans; Sym)
30 open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_)
31 import Relation.Binary.PropositionalEquality.Properties as ≡
32 open import Data.Product.Base using (_,_; proj₁; proj₂; <_,_>)
33
34 import Function.Related.Propositional as R
35 import Function.Bundles as B
36
37 private
38 variable
39 ℓ₁ ℓ₂ : Level
40 A : Set ℓ₁
41 B : Set ℓ₂
42
43 ------------------------------------------------------------------------
44 -- Re-export core concepts from non-deprecated Related code
45
46 open R public using
47 ( Kind
48 ; implication
49 ; equivalence
50 ; injection
51 ; surjection
52 ; bijection
53 ) renaming
54 ( reverseImplication to reverse-implication
55 ; reverseInjection to reverse-injection
56 ; leftInverse to left-inverse
57 )
58
59 ------------------------------------------------------------------------
60 -- Wrapper types
61
62 -- Synonyms which are used to make _∼[_]_ below "constructor-headed"
63 -- (which implies that Agda can deduce the universe code from an
64 -- expression matching any of the right-hand sides).
65
66 infix 3 _←_ _↢_
67
68 record _←_ {a b} (A : Set a) (B : Set b) : Set (a ⊔ b) where
69 constructor lam
70 field app-← : B → A
71
72 open _←_ public
73
74 record _↢_ {a b} (A : Set a) (B : Set b) : Set (a ⊔ b) where
75 constructor lam
76 field app-↢ : B ↣ A
77
78 open _↢_ public
79
80 ------------------------------------------------------------------------
81 -- Relatedness
82
83 -- There are several kinds of "relatedness".
84
85 -- The idea to include kinds other than equivalence and bijection came
86 -- from Simon Thompson and Bengt Nordström. /NAD
87
88 infix 4 _∼[_]_
89
90 _∼[_]_ : ∀ {ℓ₁ ℓ₂} → Set ℓ₁ → Kind → Set ℓ₂ → Set _
91 A ∼[ implication ] B = A → B
92 A ∼[ reverse-implication ] B = A ← B
93 A ∼[ equivalence ] B = Equivalence (≡.setoid A) (≡.setoid B)
94 A ∼[ injection ] B = Injection (≡.setoid A) (≡.setoid B)
95 A ∼[ reverse-injection ] B = A ↢ B
96 A ∼[ left-inverse ] B = LeftInverse (≡.setoid A) (≡.setoid B)
97 A ∼[ surjection ] B = Surjection (≡.setoid A) (≡.setoid B)
98 A ∼[ bijection ] B = Inverse (≡.setoid A) (≡.setoid B)
99
100 -- A non-infix synonym.
101
102 Related : Kind → ∀ {ℓ₁ ℓ₂} → Set ℓ₁ → Set ℓ₂ → Set _
103 Related k A B = A ∼[ k ] B
104
105 toRelated : {K : Kind} → A R.∼[ K ] B → A ∼[ K ] B
106 toRelated {K = implication} rel = B.Func.to rel
107 toRelated {K = reverse-implication} rel = lam (B.Func.to rel)
108 toRelated {K = equivalence} rel = Eq.equivalence (B.Equivalence.to rel) (B.Equivalence.from rel)
109 toRelated {K = injection} rel = Inj.injection (B.Injection.to rel) (B.Injection.injective rel)
110 toRelated {K = reverse-injection} rel = lam (Inj.injection (B.Injection.to rel) (B.Injection.injective rel))
111 toRelated {K = left-inverse} rel = LeftInv.leftInverse to from strictlyInverseʳ where open B.RightInverse rel
112 toRelated {K = surjection} rel = Surj.surjection to (proj₁ ∘ strictlySurjective) (proj₂ ∘ strictlySurjective)
113 where open B.Surjection rel
114 toRelated {K = bijection} rel =
115 Inv.inverse to from strictlyInverseʳ strictlyInverseˡ
116 where open B.Inverse rel
117
118 fromRelated : {K : Kind} → A ∼[ K ] B → A R.∼[ K ] B
119 fromRelated {K = implication} rel = B.mk⟶ rel
120 fromRelated {K = reverse-implication} rel = B.mk⟶ (app-← rel)
121 fromRelated {K = equivalence} record { to = to ; from = from } = B.mk⇔ (to ⟨$⟩_) (from ⟨$⟩_)
122 fromRelated {K = injection} rel = B.mk↣ (Inj.Injection.injective rel)
123 fromRelated {K = reverse-injection} (lam app-↢) = B.mk↣ (Inj.Injection.injective app-↢)
124 fromRelated {K = left-inverse} record { to = to ; from = from ; left-inverse-of = left-inverse-of } =
125 B.mk↪ {to = to ⟨$⟩_} {from = from ⟨$⟩_} (strictlyInverseʳ⇒inverseʳ (to ⟨$⟩_) left-inverse-of)
126 fromRelated {K = surjection} record { to = to ; surjective = surjective } with surjective
127 ... | record { from = from ; right-inverse-of = right-inverse-of } =
128 B.mk↠ {to = to ⟨$⟩_} < from ⟨$⟩_ , (λ { x ≡.refl → right-inverse-of x }) >
129 fromRelated {K = bijection} rel = B.mk↔ₛ′ (to ⟨$⟩_) (from ⟨$⟩_) right-inverse-of left-inverse-of
130 where open Inverse rel
131
132 -- The bijective equality implies any kind of relatedness.
133
134 ↔⇒ : ∀ {k x y} {X : Set x} {Y : Set y} →
135 X ∼[ bijection ] Y → X ∼[ k ] Y
136 ↔⇒ {implication} = _⟨$⟩_ ∘ Inverse.to
137 ↔⇒ {reverse-implication} = lam ∘′ _⟨$⟩_ ∘ Inverse.from
138 ↔⇒ {equivalence} = Inverse.equivalence
139 ↔⇒ {injection} = Inverse.injection
140 ↔⇒ {reverse-injection} = lam ∘′ Inverse.injection ∘ Inv.sym
141 ↔⇒ {left-inverse} = Inverse.left-inverse
142 ↔⇒ {surjection} = Inverse.surjection
143 ↔⇒ {bijection} = id
144
145 -- Actual equality also implies any kind of relatedness.
146
147 ≡⇒ : ∀ {k ℓ} {X Y : Set ℓ} → X ≡ Y → X ∼[ k ] Y
148 ≡⇒ ≡.refl = ↔⇒ Inv.id
149
150 ------------------------------------------------------------------------
151 -- Special kinds of kinds
152
153 -- Kinds whose interpretation is symmetric.
154
155 data Symmetric-kind : Set where
156 equivalence : Symmetric-kind
157 bijection : Symmetric-kind
158
159 -- Forgetful map.
160
161 ⌊_⌋ : Symmetric-kind → Kind
162 ⌊ equivalence ⌋ = equivalence
163 ⌊ bijection ⌋ = bijection
164
165 -- The proof of symmetry can be found below.
166
167 -- Kinds whose interpretation include a function which "goes in the
168 -- forward direction".
169
170 data Forward-kind : Set where
171 implication : Forward-kind
172 equivalence : Forward-kind
173 injection : Forward-kind
174 left-inverse : Forward-kind
175 surjection : Forward-kind
176 bijection : Forward-kind
177
178 -- Forgetful map.
179
180 ⌊_⌋→ : Forward-kind → Kind
181 ⌊ implication ⌋→ = implication
182 ⌊ equivalence ⌋→ = equivalence
183 ⌊ injection ⌋→ = injection
184 ⌊ left-inverse ⌋→ = left-inverse
185 ⌊ surjection ⌋→ = surjection
186 ⌊ bijection ⌋→ = bijection
187
188 -- The function.
189
190 ⇒→ : ∀ {k x y} {X : Set x} {Y : Set y} → X ∼[ ⌊ k ⌋→ ] Y → X → Y
191 ⇒→ {implication} = id
192 ⇒→ {equivalence} = _⟨$⟩_ ∘ Equivalence.to
193 ⇒→ {injection} = _⟨$⟩_ ∘ Injection.to
194 ⇒→ {left-inverse} = _⟨$⟩_ ∘ LeftInverse.to
195 ⇒→ {surjection} = _⟨$⟩_ ∘ Surjection.to
196 ⇒→ {bijection} = _⟨$⟩_ ∘ Inverse.to
197
198 -- Kinds whose interpretation include a function which "goes backwards".
199
200 data Backward-kind : Set where
201 reverse-implication : Backward-kind
202 equivalence : Backward-kind
203 reverse-injection : Backward-kind
204 left-inverse : Backward-kind
205 surjection : Backward-kind
206 bijection : Backward-kind
207
208 -- Forgetful map.
209
210 ⌊_⌋← : Backward-kind → Kind
211 ⌊ reverse-implication ⌋← = reverse-implication
212 ⌊ equivalence ⌋← = equivalence
213 ⌊ reverse-injection ⌋← = reverse-injection
214 ⌊ left-inverse ⌋← = left-inverse
215 ⌊ surjection ⌋← = surjection
216 ⌊ bijection ⌋← = bijection
217
218 -- The function.
219
220 ⇒← : ∀ {k x y} {X : Set x} {Y : Set y} → X ∼[ ⌊ k ⌋← ] Y → Y → X
221 ⇒← {reverse-implication} = app-←
222 ⇒← {equivalence} = _⟨$⟩_ ∘ Equivalence.from
223 ⇒← {reverse-injection} = _⟨$⟩_ ∘ Injection.to ∘ app-↢
224 ⇒← {left-inverse} = _⟨$⟩_ ∘ LeftInverse.from
225 ⇒← {surjection} = _⟨$⟩_ ∘ Surjection.from
226 ⇒← {bijection} = _⟨$⟩_ ∘ Inverse.from
227
228 -- Kinds whose interpretation include functions going in both
229 -- directions.
230
231 data Equivalence-kind : Set where
232 equivalence : Equivalence-kind
233 left-inverse : Equivalence-kind
234 surjection : Equivalence-kind
235 bijection : Equivalence-kind
236
237 -- Forgetful map.
238
239 ⌊_⌋⇔ : Equivalence-kind → Kind
240 ⌊ equivalence ⌋⇔ = equivalence
241 ⌊ left-inverse ⌋⇔ = left-inverse
242 ⌊ surjection ⌋⇔ = surjection
243 ⌊ bijection ⌋⇔ = bijection
244
245 -- The functions.
246
247 ⇒⇔ : ∀ {k x y} {X : Set x} {Y : Set y} →
248 X ∼[ ⌊ k ⌋⇔ ] Y → X ∼[ equivalence ] Y
249 ⇒⇔ {equivalence} = id
250 ⇒⇔ {left-inverse} = LeftInverse.equivalence
251 ⇒⇔ {surjection} = Surjection.equivalence
252 ⇒⇔ {bijection} = Inverse.equivalence
253
254 -- Conversions between special kinds.
255
256 ⇔⌊_⌋ : Symmetric-kind → Equivalence-kind
257 ⇔⌊ equivalence ⌋ = equivalence
258 ⇔⌊ bijection ⌋ = bijection
259
260 →⌊_⌋ : Equivalence-kind → Forward-kind
261 →⌊ equivalence ⌋ = equivalence
262 →⌊ left-inverse ⌋ = left-inverse
263 →⌊ surjection ⌋ = surjection
264 →⌊ bijection ⌋ = bijection
265
266 ←⌊_⌋ : Equivalence-kind → Backward-kind
267 ←⌊ equivalence ⌋ = equivalence
268 ←⌊ left-inverse ⌋ = left-inverse
269 ←⌊ surjection ⌋ = surjection
270 ←⌊ bijection ⌋ = bijection
271
272 ------------------------------------------------------------------------
273 -- Opposites
274
275 -- For every kind there is an opposite kind.
276
277 _op : Kind → Kind
278 implication op = reverse-implication
279 reverse-implication op = implication
280 equivalence op = equivalence
281 injection op = reverse-injection
282 reverse-injection op = injection
283 left-inverse op = surjection
284 surjection op = left-inverse
285 bijection op = bijection
286
287 -- For every morphism there is a corresponding reverse morphism of the
288 -- opposite kind.
289
290 reverse : ∀ {k a b} {A : Set a} {B : Set b} →
291 A ∼[ k ] B → B ∼[ k op ] A
292 reverse {implication} = lam
293 reverse {reverse-implication} = app-←
294 reverse {equivalence} = Eq.sym
295 reverse {injection} = lam
296 reverse {reverse-injection} = app-↢
297 reverse {left-inverse} = Surj.fromRightInverse
298 reverse {surjection} = Surjection.right-inverse
299 reverse {bijection} = Inv.sym
300
301 ------------------------------------------------------------------------
302 -- For a fixed universe level every kind is a preorder and each
303 -- symmetric kind is an equivalence
304
305 K-refl : ∀ {k ℓ} → Reflexive (Related k {ℓ})
306 K-refl {implication} = id
307 K-refl {reverse-implication} = lam id
308 K-refl {equivalence} = Eq.id
309 K-refl {injection} = Inj.id
310 K-refl {reverse-injection} = lam Inj.id
311 K-refl {left-inverse} = LeftInv.id
312 K-refl {surjection} = Surj.id
313 K-refl {bijection} = Inv.id
314
315 K-reflexive : ∀ {k ℓ} → _≡_ ⇒ Related k {ℓ}
316 K-reflexive ≡.refl = K-refl
317
318 K-trans : ∀ {k ℓ₁ ℓ₂ ℓ₃} → Trans (Related k {ℓ₁} {ℓ₂})
319 (Related k {ℓ₂} {ℓ₃})
320 (Related k {ℓ₁} {ℓ₃})
321 K-trans {implication} = flip _∘′_
322 K-trans {reverse-implication} = λ f g → lam (app-← f ∘ app-← g)
323 K-trans {equivalence} = flip Eq._∘_
324 K-trans {injection} = flip Inj._∘_
325 K-trans {reverse-injection} = λ f g → lam (Inj._∘_ (app-↢ f) (app-↢ g))
326 K-trans {left-inverse} = flip LeftInv._∘_
327 K-trans {surjection} = flip Surj._∘_
328 K-trans {bijection} = flip Inv._∘_
329
330 SK-sym : ∀ {k ℓ₁ ℓ₂} → Sym (Related ⌊ k ⌋ {ℓ₁} {ℓ₂})
331 (Related ⌊ k ⌋ {ℓ₂} {ℓ₁})
332 SK-sym {equivalence} = Eq.sym
333 SK-sym {bijection} = Inv.sym
334
335 SK-isEquivalence : ∀ k ℓ → IsEquivalence {ℓ = ℓ} (Related ⌊ k ⌋)
336 SK-isEquivalence k ℓ = record
337 { refl = K-refl
338 ; sym = SK-sym
339 ; trans = K-trans
340 }
341
342 SK-setoid : Symmetric-kind → (ℓ : Level) → Setoid _ _
343 SK-setoid k ℓ = record { isEquivalence = SK-isEquivalence k ℓ }
344
345 K-isPreorder : ∀ k ℓ → IsPreorder _↔_ (Related k)
346 K-isPreorder k ℓ = record
347 { isEquivalence = SK-isEquivalence bijection ℓ
348 ; reflexive = ↔⇒
349 ; trans = K-trans
350 }
351
352 K-preorder : Kind → (ℓ : Level) → Preorder _ _ _
353 K-preorder k ℓ = record { isPreorder = K-isPreorder k ℓ }
354
355 ------------------------------------------------------------------------
356 -- Equational reasoning
357
358 -- Equational reasoning for related things.
359
360 module EquationalReasoning where
361
362 infix 3 _∎
363 infixr 2 _∼⟨_⟩_ _↔⟨_⟩_ _↔⟨⟩_ _≡⟨_⟩_ _≡˘⟨_⟩_
364 infix 1 begin_
365
366 begin_ : ∀ {k x y} {X : Set x} {Y : Set y} →
367 X ∼[ k ] Y → X ∼[ k ] Y
368 begin_ x∼y = x∼y
369
370 _∼⟨_⟩_ : ∀ {k x y z} (X : Set x) {Y : Set y} {Z : Set z} →
371 X ∼[ k ] Y → Y ∼[ k ] Z → X ∼[ k ] Z
372 _ ∼⟨ X↝Y ⟩ Y↝Z = K-trans X↝Y Y↝Z
373
374 -- Isomorphisms can be combined with any other kind of relatedness.
375
376 _↔⟨_⟩_ : ∀ {k x y z} (X : Set x) {Y : Set y} {Z : Set z} →
377 X ↔ Y → Y ∼[ k ] Z → X ∼[ k ] Z
378 X ↔⟨ X↔Y ⟩ Y⇔Z = X ∼⟨ ↔⇒ X↔Y ⟩ Y⇔Z
379
380 _↔⟨⟩_ : ∀ {k x y} (X : Set x) {Y : Set y} →
381 X ∼[ k ] Y → X ∼[ k ] Y
382 X ↔⟨⟩ X⇔Y = X⇔Y
383
384 _≡˘⟨_⟩_ : ∀ {k ℓ z} (X : Set ℓ) {Y : Set ℓ} {Z : Set z} →
385 Y ≡ X → Y ∼[ k ] Z → X ∼[ k ] Z
386 X ≡˘⟨ Y≡X ⟩ Y⇔Z = X ∼⟨ ≡⇒ (≡.sym Y≡X) ⟩ Y⇔Z
387
388 _≡⟨_⟩_ : ∀ {k ℓ z} (X : Set ℓ) {Y : Set ℓ} {Z : Set z} →
389 X ≡ Y → Y ∼[ k ] Z → X ∼[ k ] Z
390 X ≡⟨ X≡Y ⟩ Y⇔Z = X ∼⟨ ≡⇒ X≡Y ⟩ Y⇔Z
391
392 _∎ : ∀ {k x} (X : Set x) → X ∼[ k ] X
393 X ∎ = K-refl
394
395 ------------------------------------------------------------------------
396 -- Every unary relation induces a preorder and, for symmetric kinds,
397 -- an equivalence. (No claim is made that these relations are unique.)
398
399 InducedRelation₁ : Kind → ∀ {a s} {A : Set a} →
400 (A → Set s) → A → A → Set _
401 InducedRelation₁ k S = λ x y → S x ∼[ k ] S y
402
403 InducedPreorder₁ : Kind → ∀ {a s} {A : Set a} →
404 (A → Set s) → Preorder _ _ _
405 InducedPreorder₁ k S = record
406 { _≈_ = _≡_
407 ; _≲_ = InducedRelation₁ k S
408 ; isPreorder = record
409 { isEquivalence = ≡.isEquivalence
410 ; reflexive = reflexive ∘
411 K-reflexive ∘
412 ≡.cong S
413 ; trans = K-trans
414 }
415 } where open Preorder (K-preorder _ _)
416
417 InducedEquivalence₁ : Symmetric-kind → ∀ {a s} {A : Set a} →
418 (A → Set s) → Setoid _ _
419 InducedEquivalence₁ k S = record
420 { _≈_ = InducedRelation₁ ⌊ k ⌋ S
421 ; isEquivalence = record
422 { refl = K-refl
423 ; sym = SK-sym
424 ; trans = K-trans
425 }
426 }
427
428 ------------------------------------------------------------------------
429 -- Every binary relation induces a preorder and, for symmetric kinds,
430 -- an equivalence. (No claim is made that these relations are unique.)
431
432 InducedRelation₂ : Kind → ∀ {a b s} {A : Set a} {B : Set b} →
433 (A → B → Set s) → B → B → Set _
434 InducedRelation₂ k _S_ = λ x y → ∀ {z} → (z S x) ∼[ k ] (z S y)
435
436 InducedPreorder₂ : Kind → ∀ {a b s} {A : Set a} {B : Set b} →
437 (A → B → Set s) → Preorder _ _ _
438 InducedPreorder₂ k _S_ = record
439 { _≈_ = _≡_
440 ; _≲_ = InducedRelation₂ k _S_
441 ; isPreorder = record
442 { isEquivalence = ≡.isEquivalence
443 ; reflexive = λ x≡y {z} →
444 reflexive $
445 K-reflexive $
446 ≡.cong (_S_ z) x≡y
447
448 ; trans = λ i↝j j↝k → K-trans i↝j j↝k
449 }
450 } where open Preorder (K-preorder _ _)
451
452 InducedEquivalence₂ : Symmetric-kind →
453 ∀ {a b s} {A : Set a} {B : Set b} →
454 (A → B → Set s) → Setoid _ _
455 InducedEquivalence₂ k _S_ = record
456 { _≈_ = InducedRelation₂ ⌊ k ⌋ _S_
457 ; isEquivalence = record
458 { refl = refl
459 ; sym = λ i↝j → sym i↝j
460 ; trans = λ i↝j j↝k → trans i↝j j↝k
461 }
462 } where open Setoid (SK-setoid _ _)