Debian Codegraph @

src/Relation/Binary/Consequences.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Some properties imply others
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Relation.Binary.Consequences where
10
11 open import Data.Product.Base using (_,_)
12 open import Data.Sum.Base as Sum using (inj₁; inj₂; [_,_]′)
13 open import Function.Base using (_∘_; _∘₂_; _$_; flip)
14 open import Level using (Level)
15 open import Relation.Binary.Core
16 open import Relation.Binary.Definitions
17 open import Relation.Nullary.Negation.Core using (¬_; contradiction)
18 open import Relation.Nullary.Decidable.Core
19 using (yes; no; recompute; map′; dec⇒maybe)
20 open import Relation.Unary using (∁; Pred)
21
22 private
23 variable
24 a ℓ ℓ₁ ℓ₂ ℓ₃ ℓ₄ p : Level
25 A B C : Set a
26
27 ------------------------------------------------------------------------
28 -- Substitutive properties
29
30 module _ {_∼_ : Rel A ℓ} (R : Rel A p) where
31
32 subst⇒respˡ : Substitutive _∼_ p → R Respectsˡ _∼_
33 subst⇒respˡ subst {y} x′∼x Px′y = subst (flip R y) x′∼x Px′y
34
35 subst⇒respʳ : Substitutive _∼_ p → R Respectsʳ _∼_
36 subst⇒respʳ subst {x} y′∼y Pxy′ = subst (R x) y′∼y Pxy′
37
38 subst⇒resp₂ : Substitutive _∼_ p → R Respects₂ _∼_
39 subst⇒resp₂ subst = subst⇒respʳ subst , subst⇒respˡ subst
40
41 module _ {_∼_ : Rel A ℓ} {P : Pred A p} where
42
43 resp⇒¬-resp : Symmetric _∼_ → P Respects _∼_ → (∁ P) Respects _∼_
44 resp⇒¬-resp sym resp x∼y ¬Px Py = ¬Px (resp (sym x∼y) Py)
45
46 ------------------------------------------------------------------------
47 -- Proofs for negation
48
49 module _ {_∼_ : Rel A ℓ} where
50
51 sym⇒¬-sym : Symmetric _∼_ → Symmetric (¬_ ∘₂ _∼_)
52 sym⇒¬-sym sym≁ x≁y y∼x = x≁y (sym≁ y∼x)
53
54 -- N.B. the implicit arguments to Cotransitive are permuted w.r.t.
55 -- those of Transitive
56 cotrans⇒¬-trans : Cotransitive _∼_ → Transitive (¬_ ∘₂ _∼_)
57 cotrans⇒¬-trans cotrans {j = z} x≁z z≁y x∼y =
58 [ x≁z , z≁y ]′ (cotrans x∼y z)
59
60 ------------------------------------------------------------------------
61 -- Proofs for Irreflexive relations
62
63 module _ {_≈_ : Rel A ℓ₁} {_∼_ : Rel A ℓ₂} where
64
65 irrefl⇒¬-refl : Reflexive _≈_ → Irreflexive _≈_ _∼_ →
66 Reflexive (¬_ ∘₂ _∼_)
67 irrefl⇒¬-refl re irr = irr re
68
69 ------------------------------------------------------------------------
70 -- Proofs for non-strict orders
71
72 module _ {_≈_ : Rel A ℓ₁} {_≤_ : Rel A ℓ₂} where
73
74 total⇒refl : _≤_ Respects₂ _≈_ → Symmetric _≈_ →
75 Total _≤_ → _≈_ ⇒ _≤_
76 total⇒refl (respʳ , respˡ) sym total {x} {y} x≈y with total x y
77 ... | inj₁ x∼y = x∼y
78 ... | inj₂ y∼x = respʳ x≈y (respˡ (sym x≈y) y∼x)
79
80 total∧dec⇒dec : _≈_ ⇒ _≤_ → Antisymmetric _≈_ _≤_ →
81 Total _≤_ → Decidable _≈_ → Decidable _≤_
82 total∧dec⇒dec refl antisym total _≟_ x y with total x y
83 ... | inj₁ x≤y = yes x≤y
84 ... | inj₂ y≤x = map′ refl (flip antisym y≤x) (x ≟ y)
85
86 module _ (≈₁ : Rel A ℓ₁) (≈₂ : Rel B ℓ₂) {≤₁ : Rel A ℓ₃} {≤₂ : Rel B ℓ₄} where
87
88 mono⇒cong : Symmetric ≈₁ → ≈₁ ⇒ ≤₁ → Antisymmetric ≈₂ ≤₂ →
89 ∀ {f} → Monotonic₁ ≤₁ ≤₂ f → Monotonic₁ ≈₁ ≈₂ f
90 mono⇒cong sym reflexive antisym mono x≈y = antisym
91 (mono (reflexive x≈y))
92 (mono (reflexive (sym x≈y)))
93
94 antimono⇒cong : Symmetric ≈₁ → ≈₁ ⇒ ≤₁ → Antisymmetric ≈₂ ≤₂ →
95 ∀ {f} → f Preserves ≤₁ ⟶ (flip ≤₂) → Monotonic₁ ≈₁ ≈₂ f
96 antimono⇒cong sym reflexive antisym antimono p≈q = antisym
97 (antimono (reflexive (sym p≈q)))
98 (antimono (reflexive p≈q))
99
100 mono₂⇒cong₂ : Symmetric ≈₁ → ≈₁ ⇒ ≤₁ → Antisymmetric ≈₂ ≤₂ →
101 ∀ {f} → Monotonic₂ ≤₁ ≤₁ ≤₂ f → Monotonic₂ ≈₁ ≈₁ ≈₂ f
102 mono₂⇒cong₂ sym reflexive antisym mono x≈y u≈v = antisym
103 (mono (reflexive x≈y) (reflexive u≈v))
104 (mono (reflexive (sym x≈y)) (reflexive (sym u≈v)))
105
106 module _ (≤₁ : Rel A ℓ₁) (≤₂ : Rel B ℓ₂) (≤₃ : Rel C ℓ₂) where
107
108 mono₂⇒monoˡ : ∀ {f} → Reflexive ≤₁ →
109 Monotonic₂ ≤₁ ≤₂ ≤₃ f → LeftMonotonic ≤₂ ≤₃ f
110 mono₂⇒monoˡ refl mono x = mono (refl {x = x})
111
112 mono₂⇒monoʳ : ∀ {f} → Reflexive ≤₂ →
113 Monotonic₂ ≤₁ ≤₂ ≤₃ f → RightMonotonic ≤₁ ≤₃ f
114 mono₂⇒monoʳ refl mono y = flip mono (refl {x = y})
115
116 monoˡ∧monoʳ⇒mono₂ : ∀ {f} → Transitive ≤₃ →
117 LeftMonotonic ≤₂ ≤₃ f → RightMonotonic ≤₁ ≤₃ f →
118 Monotonic₂ ≤₁ ≤₂ ≤₃ f
119 monoˡ∧monoʳ⇒mono₂ trans monoˡ monoʳ x≤₁y u≤₂v =
120 trans (monoˡ _ u≤₂v) (monoʳ _ x≤₁y)
121
122 ------------------------------------------------------------------------
123 -- Proofs for strict orders
124
125 module _ {_≈_ : Rel A ℓ₁} {_<_ : Rel A ℓ₂} where
126
127 trans∧irr⇒asym : Reflexive _≈_ → Transitive _<_ →
128 Irreflexive _≈_ _<_ → Asymmetric _<_
129 trans∧irr⇒asym refl trans irrefl x<y y<x =
130 irrefl refl (trans x<y y<x)
131
132 irr∧antisym⇒asym : Irreflexive _≈_ _<_ → Antisymmetric _≈_ _<_ →
133 Asymmetric _<_
134 irr∧antisym⇒asym irrefl antisym x<y y<x =
135 irrefl (antisym x<y y<x) x<y
136
137 asym⇒antisym : Asymmetric _<_ → Antisymmetric _≈_ _<_
138 asym⇒antisym asym x<y y<x = contradiction y<x (asym x<y)
139
140 asym⇒irr : _<_ Respects₂ _≈_ → Symmetric _≈_ →
141 Asymmetric _<_ → Irreflexive _≈_ _<_
142 asym⇒irr (respʳ , respˡ) sym asym {x} {y} x≈y x<y =
143 asym x<y (respʳ (sym x≈y) (respˡ x≈y x<y))
144
145 tri⇒asym : Trichotomous _≈_ _<_ → Asymmetric _<_
146 tri⇒asym tri {x} {y} x<y x>y with tri x y
147 ... | tri< _ _ x≯y = x≯y x>y
148 ... | tri≈ _ _ x≯y = x≯y x>y
149 ... | tri> x≮y _ _ = x≮y x<y
150
151 tri⇒irr : Trichotomous _≈_ _<_ → Irreflexive _≈_ _<_
152 tri⇒irr compare {x} {y} x≈y x<y with compare x y
153 ... | tri< _ x≉y y≮x = x≉y x≈y
154 ... | tri> x≮y x≉y y<x = x≉y x≈y
155 ... | tri≈ x≮y _ y≮x = x≮y x<y
156
157 tri⇒dec≈ : Trichotomous _≈_ _<_ → Decidable _≈_
158 tri⇒dec≈ compare x y with compare x y
159 ... | tri< _ x≉y _ = no x≉y
160 ... | tri≈ _ x≈y _ = yes x≈y
161 ... | tri> _ x≉y _ = no x≉y
162
163 tri⇒dec< : Trichotomous _≈_ _<_ → Decidable _<_
164 tri⇒dec< compare x y with compare x y
165 ... | tri< x<y _ _ = yes x<y
166 ... | tri≈ x≮y _ _ = no x≮y
167 ... | tri> x≮y _ _ = no x≮y
168
169 trans∧tri⇒respʳ : Symmetric _≈_ → Transitive _≈_ →
170 Transitive _<_ → Trichotomous _≈_ _<_ →
171 _<_ Respectsʳ _≈_
172 trans∧tri⇒respʳ sym ≈-tr <-tr tri {x} {y} {z} y≈z x<y with tri x z
173 ... | tri< x<z _ _ = x<z
174 ... | tri≈ _ x≈z _ = contradiction x<y (tri⇒irr tri (≈-tr x≈z (sym y≈z)))
175 ... | tri> _ _ z<x = contradiction (<-tr z<x x<y) (tri⇒irr tri (sym y≈z))
176
177 trans∧tri⇒respˡ : Transitive _≈_ →
178 Transitive _<_ → Trichotomous _≈_ _<_ →
179 _<_ Respectsˡ _≈_
180 trans∧tri⇒respˡ ≈-tr <-tr tri {z} {_} {y} x≈y x<z with tri y z
181 ... | tri< y<z _ _ = y<z
182 ... | tri≈ _ y≈z _ = contradiction x<z (tri⇒irr tri (≈-tr x≈y y≈z))
183 ... | tri> _ _ z<y = contradiction (<-tr x<z z<y) (tri⇒irr tri x≈y)
184
185 trans∧tri⇒resp : Symmetric _≈_ → Transitive _≈_ →
186 Transitive _<_ → Trichotomous _≈_ _<_ →
187 _<_ Respects₂ _≈_
188 trans∧tri⇒resp sym ≈-tr <-tr tri =
189 trans∧tri⇒respʳ sym ≈-tr <-tr tri ,
190 trans∧tri⇒respˡ ≈-tr <-tr tri
191
192 ------------------------------------------------------------------------
193 -- Without Loss of Generality
194
195 module _ {_R_ : Rel A ℓ₁} {Q : Rel A ℓ₂} where
196
197 wlog : Total _R_ → Symmetric Q →
198 (∀ a b → a R b → Q a b) →
199 ∀ a b → Q a b
200 wlog r-total q-sym prf a b with r-total a b
201 ... | inj₁ aRb = prf a b aRb
202 ... | inj₂ bRa = q-sym (prf b a bRa)
203
204 ------------------------------------------------------------------------
205 -- Other proofs
206
207 module _ {R : REL A B p} where
208
209 dec⇒weaklyDec : Decidable R → WeaklyDecidable R
210 dec⇒weaklyDec dec x y = dec⇒maybe (dec x y)
211
212 dec⇒recomputable : Decidable R → Recomputable R
213 dec⇒recomputable dec {a} {b} = recompute $ dec a b
214
215 module _ {R : REL A B ℓ₁} {S : REL A B ℓ₂} where
216
217 map-NonEmpty : R ⇒ S → NonEmpty R → NonEmpty S
218 map-NonEmpty f x = nonEmpty (f (NonEmpty.proof x))
219
220 module _ {R : REL A B ℓ₁} {S : REL B A ℓ₂} where
221
222 flip-Connex : Connex R S → Connex S R
223 flip-Connex f x y = Sum.swap (f y x)
224
225
226
227 ------------------------------------------------------------------------
228 -- DEPRECATED NAMES
229 ------------------------------------------------------------------------
230 -- Please use the new names as continuing support for the old names is
231 -- not guaranteed.
232
233 -- Version 1.6
234
235 subst⟶respˡ = subst⇒respˡ
236 {-# WARNING_ON_USAGE subst⟶respˡ
237 "Warning: subst⟶respˡ was deprecated in v1.6.
238 Please use subst⇒respˡ instead."
239 #-}
240 subst⟶respʳ = subst⇒respʳ
241 {-# WARNING_ON_USAGE subst⟶respʳ
242 "Warning: subst⟶respʳ was deprecated in v1.6.
243 Please use subst⇒respʳ instead."
244 #-}
245 subst⟶resp₂ = subst⇒resp₂
246 {-# WARNING_ON_USAGE subst⟶resp₂
247 "Warning: subst⟶resp₂ was deprecated in v1.6.
248 Please use subst⇒resp₂ instead."
249 #-}
250 P-resp⟶¬P-resp = resp⇒¬-resp
251 {-# WARNING_ON_USAGE P-resp⟶¬P-resp
252 "Warning: P-resp⟶¬P-resp was deprecated in v1.6.
253 Please use resp⇒¬-resp instead."
254 #-}
255 total⟶refl = total⇒refl
256 {-# WARNING_ON_USAGE total⟶refl
257 "Warning: total⟶refl was deprecated in v1.6.
258 Please use total⇒refl instead."
259 #-}
260 total+dec⟶dec = total∧dec⇒dec
261 {-# WARNING_ON_USAGE total+dec⟶dec
262 "Warning: total+dec⟶dec was deprecated in v1.6.
263 Please use total∧dec⇒dec instead."
264 #-}
265 trans∧irr⟶asym = trans∧irr⇒asym
266 {-# WARNING_ON_USAGE trans∧irr⟶asym
267 "Warning: trans∧irr⟶asym was deprecated in v1.6.
268 Please use trans∧irr⇒asym instead."
269 #-}
270 irr∧antisym⟶asym = irr∧antisym⇒asym
271 {-# WARNING_ON_USAGE irr∧antisym⟶asym
272 "Warning: irr∧antisym⟶asym was deprecated in v1.6.
273 Please use irr∧antisym⇒asym instead."
274 #-}
275 asym⟶antisym = asym⇒antisym
276 {-# WARNING_ON_USAGE asym⟶antisym
277 "Warning: asym⟶antisym was deprecated in v1.6.
278 Please use asym⇒antisym instead."
279 #-}
280 asym⟶irr = asym⇒irr
281 {-# WARNING_ON_USAGE asym⟶irr
282 "Warning: asym⟶irr was deprecated in v1.6.
283 Please use asym⇒irr instead."
284 #-}
285 tri⟶asym = tri⇒asym
286 {-# WARNING_ON_USAGE tri⟶asym
287 "Warning: tri⟶asym was deprecated in v1.6.
288 Please use tri⇒asym instead."
289 #-}
290 tri⟶irr = tri⇒irr
291 {-# WARNING_ON_USAGE tri⟶irr
292 "Warning: tri⟶irr was deprecated in v1.6.
293 Please use tri⇒irr instead."
294 #-}
295 tri⟶dec≈ = tri⇒dec≈
296 {-# WARNING_ON_USAGE tri⟶dec≈
297 "Warning: tri⟶dec≈ was deprecated in v1.6.
298 Please use tri⇒dec≈ instead."
299 #-}
300 tri⟶dec< = tri⇒dec<
301 {-# WARNING_ON_USAGE tri⟶dec<
302 "Warning: tri⟶dec< was deprecated in v1.6.
303 Please use tri⇒dec< instead."
304 #-}
305 trans∧tri⟶respʳ≈ = trans∧tri⇒respʳ
306 {-# WARNING_ON_USAGE trans∧tri⟶respʳ≈
307 "Warning: trans∧tri⟶respʳ≈ was deprecated in v1.6.
308 Please use trans∧tri⇒respʳ instead."
309 #-}
310 trans∧tri⟶respˡ≈ = trans∧tri⇒respˡ
311 {-# WARNING_ON_USAGE trans∧tri⟶respˡ≈
312 "Warning: trans∧tri⟶respˡ≈ was deprecated in v1.6.
313 Please use trans∧tri⇒respˡ instead."
314 #-}
315 trans∧tri⟶resp≈ = trans∧tri⇒resp
316 {-# WARNING_ON_USAGE trans∧tri⟶resp≈
317 "Warning: trans∧tri⟶resp≈ was deprecated in v1.6.
318 Please use trans∧tri⇒resp instead."
319 #-}
320 dec⟶weaklyDec = dec⇒weaklyDec
321 {-# WARNING_ON_USAGE dec⟶weaklyDec
322 "Warning: dec⟶weaklyDec was deprecated in v1.6.
323 Please use dec⇒weaklyDec instead."
324 #-}
325 dec⟶recomputable = dec⇒recomputable
326 {-# WARNING_ON_USAGE dec⟶recomputable
327 "Warning: dec⟶recomputable was deprecated in v1.6.
328 Please use dec⇒recomputable instead."
329 #-}