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