Debian Codegraph @

src/Function/Related/Propositional.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Relatedness for the function hierarchy
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Function.Related.Propositional where
10
11 open import Level using (Level; _⊔_)
12 open import Relation.Binary
13 using (Rel; REL; Sym; Reflexive; Trans; IsEquivalence; Setoid; IsPreorder; Preorder)
14 open import Function.Bundles
15 using (Func; Inverse; _⇔_; _↣_; _↠_; _↪_; _⟶_; _↔_; mk⟶; Equivalence; Injection
16 ; Surjection; RightInverse)
17 open import Function.Base using (id; flip; _∘_; _∘′_; _$_)
18 open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_)
19 import Relation.Binary.PropositionalEquality.Properties as ≡
20 open import Relation.Binary.Reasoning.Syntax
21 open import Function.Properties.Surjection using (↠⇒↪; ↠⇒⇔)
22 open import Function.Properties.RightInverse using (↪⇒↠)
23 open import Function.Properties.Bijection using (⤖⇒↔; ⤖⇒⇔)
24 open import Function.Properties.Inverse using (↔⇒⤖; ↔⇒⇔; ↔⇒↣; ↔⇒↠)
25
26 import Function.Construct.Symmetry as Symmetry
27 import Function.Construct.Identity as Identity
28 import Function.Construct.Composition as Composition
29
30 ------------------------------------------------------------------------
31 -- Relatedness
32
33 -- There are several kinds of "relatedness".
34
35 -- The idea to include kinds other than equivalence and bijection came
36 -- from Simon Thompson and Bengt Nordström. /NAD
37
38 data Kind : Set where
39 implication : Kind
40 reverseImplication : Kind
41 equivalence : Kind
42 injection : Kind
43 reverseInjection : Kind
44 leftInverse : Kind
45 surjection : Kind
46 bijection : Kind
47
48 private
49 variable
50 a b c p : Level
51 A B C : Set a
52 k : Kind
53
54 -- Interpretation of the codes above. The code "bijection" is
55 -- interpreted as Inverse rather than Bijection; the two types are
56 -- equivalent.
57
58 infix 4 _∼[_]_
59
60 _∼[_]_ : Set a → Kind → Set b → Set _
61 A ∼[ implication ] B = A ⟶ B
62 A ∼[ reverseImplication ] B = B ⟶ A
63 A ∼[ equivalence ] B = A ⇔ B
64 A ∼[ injection ] B = A ↣ B
65 A ∼[ reverseInjection ] B = B ↣ A
66 A ∼[ leftInverse ] B = A ↪ B
67 A ∼[ surjection ] B = A ↠ B
68 A ∼[ bijection ] B = A ↔ B
69
70 -- A non-infix synonym.
71
72 Related : Kind → Set a → Set b → Set _
73 Related k A B = A ∼[ k ] B
74
75 -- The bijective equality implies any kind of relatedness.
76
77 ↔⇒ : A ∼[ bijection ] B → A ∼[ k ] B
78 ↔⇒ {k = implication} = mk⟶ ∘ Inverse.to
79 ↔⇒ {k = reverseImplication} = mk⟶ ∘ Inverse.from
80 ↔⇒ {k = equivalence} = ↔⇒⇔
81 ↔⇒ {k = injection} = ↔⇒↣
82 ↔⇒ {k = reverseInjection} = ↔⇒↣ ∘ Symmetry.inverse
83 ↔⇒ {k = leftInverse} = Inverse.rightInverse
84 ↔⇒ {k = surjection} = ↔⇒↠
85 ↔⇒ {k = bijection} = id
86
87 -- Propositional equality also implies any kind of relatedness.
88
89 ≡⇒ : A ≡ B → A ∼[ k ] B
90 ≡⇒ ≡.refl = ↔⇒ (Identity.↔-id _)
91
92 ------------------------------------------------------------------------
93 -- Special kinds of kinds
94
95 -- Kinds whose interpretation is symmetric.
96
97 data SymmetricKind : Set where
98 equivalence : SymmetricKind
99 bijection : SymmetricKind
100
101 -- Forgetful map.
102
103 ⌊_⌋ : SymmetricKind → Kind
104 ⌊ equivalence ⌋ = equivalence
105 ⌊ bijection ⌋ = bijection
106
107 -- The proof of symmetry can be found below.
108
109 -- Kinds whose interpretation include a function which "goes in the
110 -- forward direction".
111
112 data ForwardKind : Set where
113 implication : ForwardKind
114 equivalence : ForwardKind
115 injection : ForwardKind
116 leftInverse : ForwardKind
117 surjection : ForwardKind
118 bijection : ForwardKind
119
120 -- Forgetful map.
121
122 ⌊_⌋→ : ForwardKind → Kind
123 ⌊ implication ⌋→ = implication
124 ⌊ equivalence ⌋→ = equivalence
125 ⌊ injection ⌋→ = injection
126 ⌊ leftInverse ⌋→ = leftInverse
127 ⌊ surjection ⌋→ = surjection
128 ⌊ bijection ⌋→ = bijection
129
130 -- The function.
131
132 ⇒→ : ∀ {k} → A ∼[ ⌊ k ⌋→ ] B → A → B
133 ⇒→ {k = implication} = Func.to
134 ⇒→ {k = equivalence} = Equivalence.to
135 ⇒→ {k = injection} = Injection.to
136 ⇒→ {k = leftInverse} = RightInverse.to
137 ⇒→ {k = surjection} = Surjection.to
138 ⇒→ {k = bijection} = Inverse.to
139
140 -- Kinds whose interpretation include a function which "goes backwards".
141
142 data BackwardKind : Set where
143 reverseImplication : BackwardKind
144 equivalence : BackwardKind
145 reverseInjection : BackwardKind
146 leftInverse : BackwardKind
147 surjection : BackwardKind
148 bijection : BackwardKind
149
150 -- Forgetful map.
151
152 ⌊_⌋← : BackwardKind → Kind
153 ⌊ reverseImplication ⌋← = reverseImplication
154 ⌊ equivalence ⌋← = equivalence
155 ⌊ reverseInjection ⌋← = reverseInjection
156 ⌊ leftInverse ⌋← = leftInverse
157 ⌊ surjection ⌋← = surjection
158 ⌊ bijection ⌋← = bijection
159
160 -- The function.
161
162 ⇒← : ∀ {k} → A ∼[ ⌊ k ⌋← ] B → B → A
163 ⇒← {k = reverseImplication} = Func.to
164 ⇒← {k = equivalence} = Equivalence.from
165 ⇒← {k = reverseInjection} = Injection.to
166 ⇒← {k = leftInverse} = RightInverse.from
167 ⇒← {k = surjection} = RightInverse.to ∘ ↠⇒↪
168 ⇒← {k = bijection} = Inverse.from
169
170 -- Kinds whose interpretation include functions going in both
171 -- directions.
172
173 data EquivalenceKind : Set where
174 equivalence : EquivalenceKind
175 leftInverse : EquivalenceKind
176 surjection : EquivalenceKind
177 bijection : EquivalenceKind
178
179 -- Forgetful map.
180
181 ⌊_⌋⇔ : EquivalenceKind → Kind
182 ⌊ equivalence ⌋⇔ = equivalence
183 ⌊ leftInverse ⌋⇔ = leftInverse
184 ⌊ surjection ⌋⇔ = surjection
185 ⌊ bijection ⌋⇔ = bijection
186
187 -- The functions.
188
189 ⇒⇔ : ∀ {k} → A ∼[ ⌊ k ⌋⇔ ] B → A ∼[ equivalence ] B
190 ⇒⇔ {k = equivalence} = id
191 ⇒⇔ {k = leftInverse} = RightInverse.equivalence
192 ⇒⇔ {k = surjection} = ↠⇒⇔
193 ⇒⇔ {k = bijection} = ↔⇒⇔
194
195 -- Conversions between special kinds.
196
197 ⇔⌊_⌋ : SymmetricKind → EquivalenceKind
198 ⇔⌊ equivalence ⌋ = equivalence
199 ⇔⌊ bijection ⌋ = bijection
200
201 →⌊_⌋ : EquivalenceKind → ForwardKind
202 →⌊ equivalence ⌋ = equivalence
203 →⌊ leftInverse ⌋ = leftInverse
204 →⌊ surjection ⌋ = surjection
205 →⌊ bijection ⌋ = bijection
206
207 ←⌊_⌋ : EquivalenceKind → BackwardKind
208 ←⌊ equivalence ⌋ = equivalence
209 ←⌊ leftInverse ⌋ = leftInverse
210 ←⌊ surjection ⌋ = surjection
211 ←⌊ bijection ⌋ = bijection
212
213 ------------------------------------------------------------------------
214 -- Opposites
215
216 -- For every kind there is an opposite kind.
217
218 _op : Kind → Kind
219 implication op = reverseImplication
220 reverseImplication op = implication
221 equivalence op = equivalence
222 injection op = reverseInjection
223 reverseInjection op = injection
224 leftInverse op = surjection
225 surjection op = leftInverse
226 bijection op = bijection
227
228 -- For every morphism there is a corresponding reverse morphism of the
229 -- opposite kind.
230
231 reverse : A ∼[ k ] B → B ∼[ k op ] A
232 reverse {k = implication} = id
233 reverse {k = reverseImplication} = id
234 reverse {k = equivalence} = Symmetry.⇔-sym
235 reverse {k = injection} = id
236 reverse {k = reverseInjection} = id
237 reverse {k = leftInverse} = ↪⇒↠
238 reverse {k = surjection} = ↠⇒↪
239 reverse {k = bijection} = Symmetry.↔-sym
240
241 ------------------------------------------------------------------------
242 -- For a fixed universe level every kind is a preorder and each
243 -- symmetric kind is an equivalence
244
245 K-refl : Reflexive (Related {a} k)
246 K-refl {k = implication} = Identity.⟶-id _
247 K-refl {k = reverseImplication} = Identity.⟶-id _
248 K-refl {k = equivalence} = Identity.⇔-id _
249 K-refl {k = injection} = Identity.↣-id _
250 K-refl {k = reverseInjection} = Identity.↣-id _
251 K-refl {k = leftInverse} = Identity.↪-id _
252 K-refl {k = surjection} = Identity.↠-id _
253 K-refl {k = bijection} = Identity.↔-id _
254
255 K-reflexive : _≡_ Relation.Binary.⇒ Related {a} k
256 K-reflexive ≡.refl = K-refl
257
258 K-trans : Trans (Related {a} {b} k)
259 (Related {b} {c} k)
260 (Related {a} {c} k)
261 K-trans {k = implication} = flip Composition._⟶-∘_
262 K-trans {k = reverseImplication} = Composition._⟶-∘_
263 K-trans {k = equivalence} = flip Composition._⇔-∘_
264 K-trans {k = injection} = flip Composition._↣-∘_
265 K-trans {k = reverseInjection} = Composition._↣-∘_
266 K-trans {k = leftInverse} = flip Composition._↪-∘_
267 K-trans {k = surjection} = flip Composition._↠-∘_
268 K-trans {k = bijection} = flip Composition._↔-∘_
269
270 SK-sym : ∀ {k} → Sym (Related {a} {b} ⌊ k ⌋)
271 (Related {b} {a} ⌊ k ⌋)
272 SK-sym {k = equivalence} = reverse
273 SK-sym {k = bijection} = reverse
274
275 SK-isEquivalence : ∀ k → IsEquivalence {ℓ = a} (Related ⌊ k ⌋)
276 SK-isEquivalence k = record
277 { refl = K-refl
278 ; sym = SK-sym
279 ; trans = K-trans
280 }
281
282 SK-setoid : SymmetricKind → (ℓ : Level) → Setoid _ _
283 SK-setoid k ℓ = record { isEquivalence = SK-isEquivalence {ℓ} k }
284
285 K-isPreorder : ∀ k → IsPreorder {ℓ = a} _↔_ (Related k)
286 K-isPreorder k = record
287 { isEquivalence = SK-isEquivalence bijection
288 ; reflexive = ↔⇒
289 ; trans = K-trans
290 }
291
292 K-preorder : Kind → (ℓ : Level) → Preorder _ ℓ _
293 K-preorder k ℓ = record { isPreorder = K-isPreorder k }
294
295 ------------------------------------------------------------------------
296 -- Equational reasoning
297
298 -- Equational reasoning for related things. Note that we don't use
299 -- the `Relation.Binary.Reasoning.Syntax` for this as this relation
300 -- is heterogeneous.
301
302 module EquationalReasoning {k : Kind} where
303
304 -- Combinators with one heterogeneous relation
305 module _ {a b : Level} where
306 open begin-syntax (Related {a} {b} k) id public
307 open ≡-noncomputing-syntax (Related {a} {b} k) public
308
309 -- Combinators with two heterogeneous relations
310 module _ {a b c : Level} where
311 private
312 rel1 = Related {b} {c} k
313 rel2 = Related {a} {c} k
314
315 open ∼-syntax rel1 rel2 K-trans public
316 open ⤖-syntax rel1 rel2 (K-trans ∘′ ↔⇒ ∘′ ⤖⇒↔) Symmetry.⤖-sym public
317 open ↔-syntax rel1 rel2 (K-trans ∘′ ↔⇒) Symmetry.↔-sym public
318
319 -- Combinators with homogeneous relations
320 module _ {a : Level} where
321 open end-syntax (Related {a} k) K-refl public
322
323
324 infixr 2 _↔⟨⟩_
325 _↔⟨⟩_ : (A : Set a) → A ∼[ k ] B → A ∼[ k ] B
326 A ↔⟨⟩ A⇔B = A⇔B
327 {-# WARNING_ON_USAGE _↔⟨⟩_
328 "Warning: _↔⟨⟩_ was deprecated in v2.0.
329 Please use _≡⟨⟩_ instead. "
330 #-}
331
332 ------------------------------------------------------------------------
333 -- Every unary relation induces a preorder and, for symmetric kinds,
334 -- an equivalence. (No claim is made that these relations are unique.)
335
336 InducedRelation₁ : Kind → (P : A → Set p) → A → A → Set _
337 InducedRelation₁ k P = λ x y → P x ∼[ k ] P y
338
339 InducedPreorder₁ : Kind → (P : A → Set p) → Preorder _ _ _
340 InducedPreorder₁ k P = record
341 { _≈_ = _≡_
342 ; _≲_ = InducedRelation₁ k P
343 ; isPreorder = record
344 { isEquivalence = ≡.isEquivalence
345 ; reflexive = reflexive ∘
346 K-reflexive ∘
347 ≡.cong P
348 ; trans = K-trans
349 }
350 } where open Preorder (K-preorder _ _)
351
352 InducedEquivalence₁ : SymmetricKind → (P : A → Set p) → Setoid _ _
353 InducedEquivalence₁ k P = record
354 { _≈_ = InducedRelation₁ ⌊ k ⌋ P
355 ; isEquivalence = record
356 { refl = K-refl
357 ; sym = SK-sym
358 ; trans = K-trans
359 }
360 }
361
362 ------------------------------------------------------------------------
363 -- Every binary relation induces a preorder and, for symmetric kinds,
364 -- an equivalence. (No claim is made that these relations are unique.)
365
366 InducedRelation₂ : Kind → ∀ {s} → (A → B → Set s) → B → B → Set _
367 InducedRelation₂ k _S_ = λ x y → ∀ {z} → (z S x) ∼[ k ] (z S y)
368
369 InducedPreorder₂ : Kind → ∀ {s} → (A → B → Set s) → Preorder _ _ _
370 InducedPreorder₂ k _S_ = record
371 { _≈_ = _≡_
372 ; _≲_ = InducedRelation₂ k _S_
373 ; isPreorder = record
374 { isEquivalence = ≡.isEquivalence
375 ; reflexive = λ x≡y {z} →
376 reflexive $
377 K-reflexive $
378 ≡.cong (_S_ z) x≡y
379
380 ; trans = λ i↝j j↝k → K-trans i↝j j↝k
381 }
382 } where open Preorder (K-preorder _ _)
383
384 InducedEquivalence₂ : SymmetricKind → ∀ {s} → (A → B → Set s) → Setoid _ _
385 InducedEquivalence₂ k _S_ = record
386 { _≈_ = InducedRelation₂ ⌊ k ⌋ _S_
387 ; isEquivalence = record
388 { refl = refl
389 ; sym = λ i↝j → sym i↝j
390 ; trans = λ i↝j j↝k → trans i↝j j↝k
391 }
392 } where open Setoid (SK-setoid _ _)