-
Notifications
You must be signed in to change notification settings - Fork 234
/
Structures.agda
318 lines (237 loc) · 8.9 KB
/
Structures.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
314
315
316
317
318
------------------------------------------------------------------------
-- The Agda standard library
--
-- Structures for homogeneous binary relations
------------------------------------------------------------------------
-- The contents of this module should be accessed via `Relation.Binary`.
{-# OPTIONS --cubical-compatible --safe #-}
open import Relation.Binary.Core
module Relation.Binary.Structures
{a ℓ} {A : Set a} -- The underlying set
(_≈_ : Rel A ℓ) -- The underlying equality relation
where
open import Data.Product.Base using (proj₁; proj₂; _,_)
open import Level using (Level; _⊔_)
open import Relation.Nullary.Negation.Core using (¬_)
open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_)
open import Relation.Binary.Consequences
open import Relation.Binary.Definitions
private
variable
ℓ₂ : Level
------------------------------------------------------------------------
-- Equivalences
------------------------------------------------------------------------
-- Note all the following equivalences refer to the equality provided
-- as a module parameter at the top of this file.
record IsPartialEquivalence : Set (a ⊔ ℓ) where
field
sym : Symmetric _≈_
trans : Transitive _≈_
-- The preorders of this library are defined in terms of an underlying
-- equivalence relation, and hence equivalence relations are not
-- defined in terms of preorders.
-- To preserve backwards compatability, equivalence relations are
-- not defined in terms of their partial counterparts.
record IsEquivalence : Set (a ⊔ ℓ) where
field
refl : Reflexive _≈_
sym : Symmetric _≈_
trans : Transitive _≈_
reflexive : _≡_ ⇒ _≈_
reflexive ≡.refl = refl
isPartialEquivalence : IsPartialEquivalence
isPartialEquivalence = record
{ sym = sym
; trans = trans
}
record IsDecEquivalence : Set (a ⊔ ℓ) where
infix 4 _≟_
field
isEquivalence : IsEquivalence
_≟_ : Decidable _≈_
open IsEquivalence isEquivalence public
------------------------------------------------------------------------
-- Preorders
------------------------------------------------------------------------
record IsPreorder (_≲_ : Rel A ℓ₂) : Set (a ⊔ ℓ ⊔ ℓ₂) where
field
isEquivalence : IsEquivalence
-- Reflexivity is expressed in terms of the underlying equality:
reflexive : _≈_ ⇒ _≲_
trans : Transitive _≲_
module Eq = IsEquivalence isEquivalence
refl : Reflexive _≲_
refl = reflexive Eq.refl
≲-respˡ-≈ : _≲_ Respectsˡ _≈_
≲-respˡ-≈ x≈y x∼z = trans (reflexive (Eq.sym x≈y)) x∼z
≲-respʳ-≈ : _≲_ Respectsʳ _≈_
≲-respʳ-≈ x≈y z∼x = trans z∼x (reflexive x≈y)
≲-resp-≈ : _≲_ Respects₂ _≈_
≲-resp-≈ = ≲-respʳ-≈ , ≲-respˡ-≈
∼-respˡ-≈ = ≲-respˡ-≈
{-# WARNING_ON_USAGE ∼-respˡ-≈
"Warning: ∼-respˡ-≈ was deprecated in v2.0.
Please use ≲-respˡ-≈ instead. "
#-}
∼-respʳ-≈ = ≲-respʳ-≈
{-# WARNING_ON_USAGE ∼-respʳ-≈
"Warning: ∼-respʳ-≈ was deprecated in v2.0.
Please use ≲-respʳ-≈ instead. "
#-}
∼-resp-≈ = ≲-resp-≈
{-# WARNING_ON_USAGE ∼-resp-≈
"Warning: ∼-resp-≈ was deprecated in v2.0.
Please use ≲-resp-≈ instead. "
#-}
record IsTotalPreorder (_≲_ : Rel A ℓ₂) : Set (a ⊔ ℓ ⊔ ℓ₂) where
field
isPreorder : IsPreorder _≲_
total : Total _≲_
open IsPreorder isPreorder public
------------------------------------------------------------------------
-- Partial orders
------------------------------------------------------------------------
record IsPartialOrder (_≤_ : Rel A ℓ₂) : Set (a ⊔ ℓ ⊔ ℓ₂) where
field
isPreorder : IsPreorder _≤_
antisym : Antisymmetric _≈_ _≤_
open IsPreorder isPreorder public
renaming
( ∼-respˡ-≈ to ≤-respˡ-≈
; ∼-respʳ-≈ to ≤-respʳ-≈
; ∼-resp-≈ to ≤-resp-≈
)
record IsDecPartialOrder (_≤_ : Rel A ℓ₂) : Set (a ⊔ ℓ ⊔ ℓ₂) where
infix 4 _≟_ _≤?_
field
isPartialOrder : IsPartialOrder _≤_
_≟_ : Decidable _≈_
_≤?_ : Decidable _≤_
open IsPartialOrder isPartialOrder public
hiding (module Eq)
module Eq where
isDecEquivalence : IsDecEquivalence
isDecEquivalence = record
{ isEquivalence = isEquivalence
; _≟_ = _≟_
}
open IsDecEquivalence isDecEquivalence public
record IsStrictPartialOrder (_<_ : Rel A ℓ₂) : Set (a ⊔ ℓ ⊔ ℓ₂) where
field
isEquivalence : IsEquivalence
irrefl : Irreflexive _≈_ _<_
trans : Transitive _<_
<-resp-≈ : _<_ Respects₂ _≈_
module Eq = IsEquivalence isEquivalence
asym : Asymmetric _<_
asym {x} {y} = trans∧irr⇒asym Eq.refl trans irrefl {x = x} {y}
<-respʳ-≈ : _<_ Respectsʳ _≈_
<-respʳ-≈ = proj₁ <-resp-≈
<-respˡ-≈ : _<_ Respectsˡ _≈_
<-respˡ-≈ = proj₂ <-resp-≈
record IsDecStrictPartialOrder (_<_ : Rel A ℓ₂) : Set (a ⊔ ℓ ⊔ ℓ₂) where
infix 4 _≟_ _<?_
field
isStrictPartialOrder : IsStrictPartialOrder _<_
_≟_ : Decidable _≈_
_<?_ : Decidable _<_
private
module SPO = IsStrictPartialOrder isStrictPartialOrder
open SPO public hiding (module Eq)
module Eq where
isDecEquivalence : IsDecEquivalence
isDecEquivalence = record
{ isEquivalence = SPO.isEquivalence
; _≟_ = _≟_
}
open IsDecEquivalence isDecEquivalence public
------------------------------------------------------------------------
-- Total orders
------------------------------------------------------------------------
record IsTotalOrder (_≤_ : Rel A ℓ₂) : Set (a ⊔ ℓ ⊔ ℓ₂) where
field
isPartialOrder : IsPartialOrder _≤_
total : Total _≤_
open IsPartialOrder isPartialOrder public
isTotalPreorder : IsTotalPreorder _≤_
isTotalPreorder = record
{ isPreorder = isPreorder
; total = total
}
record IsDecTotalOrder (_≤_ : Rel A ℓ₂) : Set (a ⊔ ℓ ⊔ ℓ₂) where
infix 4 _≟_ _≤?_
field
isTotalOrder : IsTotalOrder _≤_
_≟_ : Decidable _≈_
_≤?_ : Decidable _≤_
open IsTotalOrder isTotalOrder public
hiding (module Eq)
isDecPartialOrder : IsDecPartialOrder _≤_
isDecPartialOrder = record
{ isPartialOrder = isPartialOrder
; _≟_ = _≟_
; _≤?_ = _≤?_
}
module Eq where
isDecEquivalence : IsDecEquivalence
isDecEquivalence = record
{ isEquivalence = isEquivalence
; _≟_ = _≟_
}
open IsDecEquivalence isDecEquivalence public
-- Note that these orders are decidable. The current implementation
-- of `Trichotomous` subsumes irreflexivity and asymmetry. See
-- `Relation.Binary.Structures.Biased` for ways of constructing this
-- record without having to prove `isStrictPartialOrder`.
record IsStrictTotalOrder (_<_ : Rel A ℓ₂) : Set (a ⊔ ℓ ⊔ ℓ₂) where
field
isStrictPartialOrder : IsStrictPartialOrder _<_
compare : Trichotomous _≈_ _<_
open IsStrictPartialOrder isStrictPartialOrder public
hiding (module Eq)
-- `Trichotomous` necessarily separates out the equality case so
-- it implies decidability.
infix 4 _≟_ _<?_
_≟_ : Decidable _≈_
_≟_ = tri⇒dec≈ compare
_<?_ : Decidable _<_
_<?_ = tri⇒dec< compare
isDecStrictPartialOrder : IsDecStrictPartialOrder _<_
isDecStrictPartialOrder = record
{ isStrictPartialOrder = isStrictPartialOrder
; _≟_ = _≟_
; _<?_ = _<?_
}
-- Redefine the `Eq` module to include decidability proofs
module Eq where
isDecEquivalence : IsDecEquivalence
isDecEquivalence = record
{ isEquivalence = isEquivalence
; _≟_ = _≟_
}
open IsDecEquivalence isDecEquivalence public
isDecEquivalence : IsDecEquivalence
isDecEquivalence = record
{ isEquivalence = isEquivalence
; _≟_ = _≟_
}
{-# WARNING_ON_USAGE isDecEquivalence
"Warning: isDecEquivalence was deprecated in v2.0.
Please use Eq.isDecEquivalence instead. "
#-}
record IsDenseLinearOrder (_<_ : Rel A ℓ₂) : Set (a ⊔ ℓ ⊔ ℓ₂) where
field
isStrictTotalOrder : IsStrictTotalOrder _<_
dense : Dense _<_
open IsStrictTotalOrder isStrictTotalOrder public
------------------------------------------------------------------------
-- Apartness relations
------------------------------------------------------------------------
record IsApartnessRelation (_#_ : Rel A ℓ₂) : Set (a ⊔ ℓ ⊔ ℓ₂) where
field
irrefl : Irreflexive _≈_ _#_
sym : Symmetric _#_
cotrans : Cotransitive _#_
_¬#_ : A → A → Set _
x ¬# y = ¬ (x # y)