Debian Codegraph @

src/Relation/Binary/Structures.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Structures for homogeneous binary relations
5 ------------------------------------------------------------------------
6
7 -- The contents of this module should be accessed via `Relation.Binary`.
8
9 {-# OPTIONS --cubical-compatible --safe #-}
10
11 open import Relation.Binary.Core
12
13 module Relation.Binary.Structures
14 {a ℓ} {A : Set a} -- The underlying set
15 (_≈_ : Rel A ℓ) -- The underlying equality relation
16 where
17
18 open import Data.Product.Base using (proj₁; proj₂; _,_)
19 open import Level using (Level; _⊔_)
20 open import Relation.Nullary.Negation.Core using (¬_)
21 open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_)
22 open import Relation.Binary.Consequences
23 using (tri⇒dec≈; tri⇒dec<; trans∧irr⇒asym)
24 open import Relation.Binary.Definitions
25
26 private
27 variable
28 ℓ₂ : Level
29
30 ------------------------------------------------------------------------
31 -- Equivalences
32 ------------------------------------------------------------------------
33 -- Note all the following equivalences refer to the equality provided
34 -- as a module parameter at the top of this file.
35
36 record IsPartialEquivalence : Set (a ⊔ ℓ) where
37 field
38 sym : Symmetric _≈_
39 trans : Transitive _≈_
40
41 -- The preorders of this library are defined in terms of an underlying
42 -- equivalence relation, and hence equivalence relations are not
43 -- defined in terms of preorders.
44
45 -- To preserve backwards compatability, equivalence relations are
46 -- not defined in terms of their partial counterparts.
47
48 record IsEquivalence : Set (a ⊔ ℓ) where
49 field
50 refl : Reflexive _≈_
51 sym : Symmetric _≈_
52 trans : Transitive _≈_
53
54 reflexive : _≡_ ⇒ _≈_
55 reflexive ≡.refl = refl
56
57 isPartialEquivalence : IsPartialEquivalence
58 isPartialEquivalence = record
59 { sym = sym
60 ; trans = trans
61 }
62
63
64 record IsDecEquivalence : Set (a ⊔ ℓ) where
65 infix 4 _≟_
66 field
67 isEquivalence : IsEquivalence
68 _≟_ : Decidable _≈_
69
70 open IsEquivalence isEquivalence public
71
72
73 ------------------------------------------------------------------------
74 -- Preorders
75 ------------------------------------------------------------------------
76
77 record IsPreorder (_≲_ : Rel A ℓ₂) : Set (a ⊔ ℓ ⊔ ℓ₂) where
78 field
79 isEquivalence : IsEquivalence
80 -- Reflexivity is expressed in terms of the underlying equality:
81 reflexive : _≈_ ⇒ _≲_
82 trans : Transitive _≲_
83
84 module Eq = IsEquivalence isEquivalence
85
86 refl : Reflexive _≲_
87 refl = reflexive Eq.refl
88
89 ≲-respˡ-≈ : _≲_ Respectsˡ _≈_
90 ≲-respˡ-≈ x≈y x∼z = trans (reflexive (Eq.sym x≈y)) x∼z
91
92 ≲-respʳ-≈ : _≲_ Respectsʳ _≈_
93 ≲-respʳ-≈ x≈y z∼x = trans z∼x (reflexive x≈y)
94
95 ≲-resp-≈ : _≲_ Respects₂ _≈_
96 ≲-resp-≈ = ≲-respʳ-≈ , ≲-respˡ-≈
97
98 ∼-respˡ-≈ = ≲-respˡ-≈
99 {-# WARNING_ON_USAGE ∼-respˡ-≈
100 "Warning: ∼-respˡ-≈ was deprecated in v2.0.
101 Please use ≲-respˡ-≈ instead. "
102 #-}
103 ∼-respʳ-≈ = ≲-respʳ-≈
104 {-# WARNING_ON_USAGE ∼-respʳ-≈
105 "Warning: ∼-respʳ-≈ was deprecated in v2.0.
106 Please use ≲-respʳ-≈ instead. "
107 #-}
108 ∼-resp-≈ = ≲-resp-≈
109 {-# WARNING_ON_USAGE ∼-resp-≈
110 "Warning: ∼-resp-≈ was deprecated in v2.0.
111 Please use ≲-resp-≈ instead. "
112 #-}
113
114
115 record IsTotalPreorder (_≲_ : Rel A ℓ₂) : Set (a ⊔ ℓ ⊔ ℓ₂) where
116 field
117 isPreorder : IsPreorder _≲_
118 total : Total _≲_
119
120 open IsPreorder isPreorder public
121
122
123 record IsDecPreorder (_≲_ : Rel A ℓ₂) : Set (a ⊔ ℓ ⊔ ℓ₂) where
124 field
125 isPreorder : IsPreorder _≲_
126 _≟_ : Decidable _≈_
127 _≲?_ : Decidable _≲_
128
129 open IsPreorder isPreorder public
130 hiding (module Eq)
131
132 module Eq where
133
134 isDecEquivalence : IsDecEquivalence
135 isDecEquivalence = record
136 { isEquivalence = isEquivalence
137 ; _≟_ = _≟_
138 }
139
140 open IsDecEquivalence isDecEquivalence public
141
142
143 ------------------------------------------------------------------------
144 -- Partial orders
145 ------------------------------------------------------------------------
146
147 record IsPartialOrder (_≤_ : Rel A ℓ₂) : Set (a ⊔ ℓ ⊔ ℓ₂) where
148 field
149 isPreorder : IsPreorder _≤_
150 antisym : Antisymmetric _≈_ _≤_
151
152 open IsPreorder isPreorder public
153 renaming
154 ( ∼-respˡ-≈ to ≤-respˡ-≈
155 ; ∼-respʳ-≈ to ≤-respʳ-≈
156 ; ∼-resp-≈ to ≤-resp-≈
157 )
158
159
160 record IsDecPartialOrder (_≤_ : Rel A ℓ₂) : Set (a ⊔ ℓ ⊔ ℓ₂) where
161 infix 4 _≟_ _≤?_
162 field
163 isPartialOrder : IsPartialOrder _≤_
164 _≟_ : Decidable _≈_
165 _≤?_ : Decidable _≤_
166
167 open IsPartialOrder isPartialOrder public
168 hiding (module Eq)
169
170 isDecPreorder : IsDecPreorder _≤_
171 isDecPreorder = record
172 { isPreorder = isPreorder
173 ; _≟_ = _≟_
174 ; _≲?_ = _≤?_
175 }
176
177 open IsDecPreorder isDecPreorder public
178 using (module Eq)
179
180
181 record IsStrictPartialOrder (_<_ : Rel A ℓ₂) : Set (a ⊔ ℓ ⊔ ℓ₂) where
182 field
183 isEquivalence : IsEquivalence
184 irrefl : Irreflexive _≈_ _<_
185 trans : Transitive _<_
186 <-resp-≈ : _<_ Respects₂ _≈_
187
188 module Eq = IsEquivalence isEquivalence
189
190 asym : Asymmetric _<_
191 asym {x} {y} = trans∧irr⇒asym Eq.refl trans irrefl {x = x} {y}
192
193 <-respʳ-≈ : _<_ Respectsʳ _≈_
194 <-respʳ-≈ = proj₁ <-resp-≈
195
196 <-respˡ-≈ : _<_ Respectsˡ _≈_
197 <-respˡ-≈ = proj₂ <-resp-≈
198
199
200 record IsDecStrictPartialOrder (_<_ : Rel A ℓ₂) : Set (a ⊔ ℓ ⊔ ℓ₂) where
201 infix 4 _≟_ _<?_
202 field
203 isStrictPartialOrder : IsStrictPartialOrder _<_
204 _≟_ : Decidable _≈_
205 _<?_ : Decidable _<_
206
207 private
208 module SPO = IsStrictPartialOrder isStrictPartialOrder
209
210 open SPO public hiding (module Eq)
211
212 module Eq where
213
214 isDecEquivalence : IsDecEquivalence
215 isDecEquivalence = record
216 { isEquivalence = SPO.isEquivalence
217 ; _≟_ = _≟_
218 }
219
220 open IsDecEquivalence isDecEquivalence public
221
222
223 ------------------------------------------------------------------------
224 -- Total orders
225 ------------------------------------------------------------------------
226
227 record IsTotalOrder (_≤_ : Rel A ℓ₂) : Set (a ⊔ ℓ ⊔ ℓ₂) where
228 field
229 isPartialOrder : IsPartialOrder _≤_
230 total : Total _≤_
231
232 open IsPartialOrder isPartialOrder public
233
234 isTotalPreorder : IsTotalPreorder _≤_
235 isTotalPreorder = record
236 { isPreorder = isPreorder
237 ; total = total
238 }
239
240
241 record IsDecTotalOrder (_≤_ : Rel A ℓ₂) : Set (a ⊔ ℓ ⊔ ℓ₂) where
242 infix 4 _≟_ _≤?_
243 field
244 isTotalOrder : IsTotalOrder _≤_
245 _≟_ : Decidable _≈_
246 _≤?_ : Decidable _≤_
247
248 open IsTotalOrder isTotalOrder public
249 hiding (module Eq)
250
251 isDecPartialOrder : IsDecPartialOrder _≤_
252 isDecPartialOrder = record
253 { isPartialOrder = isPartialOrder
254 ; _≟_ = _≟_
255 ; _≤?_ = _≤?_
256 }
257
258 open IsDecPartialOrder isDecPartialOrder public
259 using (isDecPreorder; module Eq)
260
261 -- Note that these orders are decidable. The current implementation
262 -- of `Trichotomous` subsumes irreflexivity and asymmetry. See
263 -- `Relation.Binary.Structures.Biased` for ways of constructing this
264 -- record without having to prove `isStrictPartialOrder`.
265
266 record IsStrictTotalOrder (_<_ : Rel A ℓ₂) : Set (a ⊔ ℓ ⊔ ℓ₂) where
267 field
268 isStrictPartialOrder : IsStrictPartialOrder _<_
269 compare : Trichotomous _≈_ _<_
270
271 open IsStrictPartialOrder isStrictPartialOrder public
272 hiding (module Eq)
273
274 -- `Trichotomous` necessarily separates out the equality case so
275 -- it implies decidability.
276 infix 4 _≟_ _<?_
277
278 _≟_ : Decidable _≈_
279 _≟_ = tri⇒dec≈ compare
280
281 _<?_ : Decidable _<_
282 _<?_ = tri⇒dec< compare
283
284 isDecStrictPartialOrder : IsDecStrictPartialOrder _<_
285 isDecStrictPartialOrder = record
286 { isStrictPartialOrder = isStrictPartialOrder
287 ; _≟_ = _≟_
288 ; _<?_ = _<?_
289 }
290
291 -- Redefine the `Eq` module to include decidability proofs
292 module Eq where
293
294 isDecEquivalence : IsDecEquivalence
295 isDecEquivalence = record
296 { isEquivalence = isEquivalence
297 ; _≟_ = _≟_
298 }
299
300 open IsDecEquivalence isDecEquivalence public
301
302 isDecEquivalence : IsDecEquivalence
303 isDecEquivalence = record
304 { isEquivalence = isEquivalence
305 ; _≟_ = _≟_
306 }
307 {-# WARNING_ON_USAGE isDecEquivalence
308 "Warning: isDecEquivalence was deprecated in v2.0.
309 Please use Eq.isDecEquivalence instead. "
310 #-}
311
312 record IsDenseLinearOrder (_<_ : Rel A ℓ₂) : Set (a ⊔ ℓ ⊔ ℓ₂) where
313 field
314 isStrictTotalOrder : IsStrictTotalOrder _<_
315 dense : Dense _<_
316
317 open IsStrictTotalOrder isStrictTotalOrder public
318
319
320 ------------------------------------------------------------------------
321 -- Apartness relations
322 ------------------------------------------------------------------------
323
324 record IsApartnessRelation (_#_ : Rel A ℓ₂) : Set (a ⊔ ℓ ⊔ ℓ₂) where
325 field
326 irrefl : Irreflexive _≈_ _#_
327 sym : Symmetric _#_
328 cotrans : Cotransitive _#_
329
330 _¬#_ : A → A → Set _
331 x ¬# y = ¬ (x # y)