forked from agda/agda-stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsequences.agda
172 lines (132 loc) · 6.37 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
------------------------------------------------------------------------
-- The Agda standard library
--
-- Some properties imply others
------------------------------------------------------------------------
{-# OPTIONS --without-K --safe #-}
module Relation.Binary.Consequences where
open import Data.Maybe.Base using (just; nothing)
open import Data.Sum as Sum using (inj₁; inj₂)
open import Data.Product using (_,_)
open import Data.Empty.Irrelevant using (⊥-elim)
open import Function using (_∘_; flip)
open import Level using (Level)
open import Relation.Binary.Core
open import Relation.Nullary using (yes; no)
open import Relation.Unary using (∁)
private
variable
a b ℓ ℓ₁ ℓ₂ p : Level
A : Set a
B : Set b
------------------------------------------------------------------------
-- Substitutive properties
module _ {_∼_ : Rel A ℓ} (P : Rel A p) where
subst⟶respˡ : Substitutive _∼_ p → P Respectsˡ _∼_
subst⟶respˡ subst {y} x'∼x Px'y = subst (flip P y) x'∼x Px'y
subst⟶respʳ : Substitutive _∼_ p → P Respectsʳ _∼_
subst⟶respʳ subst {x} y'∼y Pxy' = subst (P x) y'∼y Pxy'
subst⟶resp₂ : Substitutive _∼_ p → P Respects₂ _∼_
subst⟶resp₂ subst = subst⟶respʳ subst , subst⟶respˡ subst
module _ {_∼_ : Rel A ℓ} {P : A → Set p} where
P-resp⟶¬P-resp : Symmetric _∼_ → P Respects _∼_ → (∁ P) Respects _∼_
P-resp⟶¬P-resp sym resp x∼y ¬Px Py = ¬Px (resp (sym x∼y) Py)
------------------------------------------------------------------------
-- 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 with x ≟ y
... | yes x≈y = yes (refl x≈y)
... | no x≉y = no (λ x≤y → x≉y (antisym x≤y y≤x))
------------------------------------------------------------------------
-- 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 = ⊥-elim (asym x<y y<x)
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 _ = ⊥-elim (tri⟶irr tri (≈-tr x≈z (sym y≈z)) x<y)
... | tri> _ _ z<x = ⊥-elim (tri⟶irr tri (sym y≈z) (<-tr z<x x<y))
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 _ = ⊥-elim (tri⟶irr tri (≈-tr x≈y y≈z) x<z)
... | tri> _ _ z<y = ⊥-elim (tri⟶irr tri x≈y (<-tr x<z z<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 _ {P : REL A B p} where
dec⟶weaklyDec : Decidable P → WeaklyDecidable P
dec⟶weaklyDec dec x y with dec x y
... | yes p = just p
... | no _ = nothing
module _ {P : REL A B ℓ₁} {Q : REL A B ℓ₂} where
map-NonEmpty : P ⇒ Q → NonEmpty P → NonEmpty Q
map-NonEmpty f x = nonEmpty (f (NonEmpty.proof x))
module _ {P : REL A B ℓ₁} {Q : REL B A ℓ₂} where
flip-Connex : Connex P Q → Connex Q P
flip-Connex f x y = Sum.swap (f y x)
module _ {a b r} {A : Set a} {B : Set b} {R : REL A B r} where
dec⟶irrel′ : Decidable R → Irrelevant′ R
dec⟶irrel′ dec {a} {b} _ with dec a b
dec⟶irrel′ _ _ | yes p = p
dec⟶irrel′ _ r | no ¬p = ⊥-elim (¬p r)