Debian Codegraph @

src/Data/Vec/Relation/Binary/Lex/Strict.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Lexicographic ordering of lists of same-length vectors
5 ------------------------------------------------------------------------
6
7 -- The definitions of lexicographic ordering used here are suitable if
8 -- the argument order is a strict partial order.
9
10 {-# OPTIONS --cubical-compatible --safe #-}
11
12 module Data.Vec.Relation.Binary.Lex.Strict where
13
14 open import Data.Empty using (⊥; ⊥-elim)
15 open import Data.Unit.Base using (⊤; tt)
16 open import Data.Unit.Properties using (⊤-irrelevant)
17 open import Data.Nat.Base using (ℕ; suc)
18 open import Data.Product.Base using (_×_; _,_; proj₁; proj₂)
19 open import Data.Product.Relation.Binary.Lex.Strict
20 open import Data.Sum.Base using (inj₁; inj₂)
21 open import Data.Vec.Base using (Vec; []; _∷_; uncons)
22 open import Data.Vec.Relation.Binary.Pointwise.Inductive as Pointwise
23 using (Pointwise; []; _∷_; head; tail)
24 open import Function.Base using (id; _on_; _∘_)
25 open import Induction.WellFounded
26 open import Level using (Level; _⊔_)
27 open import Relation.Binary.Core using (REL; Rel; _⇒_)
28 open import Relation.Binary.Bundles
29 using (Poset; StrictPartialOrder; DecPoset; DecStrictPartialOrder
30 ; DecTotalOrder; StrictTotalOrder; Preorder; TotalOrder)
31 open import Relation.Binary.Structures
32 using (IsEquivalence; IsPartialOrder; IsStrictPartialOrder; IsDecPartialOrder
33 ; IsDecStrictPartialOrder; IsDecTotalOrder; IsStrictTotalOrder
34 ; IsPreorder; IsTotalOrder; IsPartialEquivalence)
35 open import Relation.Binary.Definitions
36 using (Irreflexive; _Respects₂_; _Respectsˡ_; _Respectsʳ_; Antisymmetric
37 ; Asymmetric; Symmetric; Trans; Decidable; Total; Trichotomous
38 ; Transitive; Irrelevant; tri≈; tri>; tri<)
39 open import Relation.Binary.Consequences using (asym⇒irr)
40 open import Relation.Binary.Construct.On as On using (wellFounded)
41 open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl)
42 open import Relation.Nullary.Decidable.Core using (yes; no)
43 open import Relation.Nullary.Negation.Core using (¬_; contradiction)
44
45 private
46 variable
47 a ℓ₁ ℓ₂ : Level
48 A : Set a
49
50 ------------------------------------------------------------------------
51 -- Re-exports
52 ------------------------------------------------------------------------
53
54 open import Data.Vec.Relation.Binary.Lex.Core as Core public
55 using (base; this; next; ≰-this; ≰-next)
56
57 ------------------------------------------------------------------------
58 -- Definitions
59 ------------------------------------------------------------------------
60
61 module _ {A : Set a} (_≈_ : Rel A ℓ₁) (_≺_ : Rel A ℓ₂) where
62
63 Lex-< : ∀ {m n} → REL (Vec A m) (Vec A n) (a ⊔ ℓ₁ ⊔ ℓ₂)
64 Lex-< = Core.Lex {A = A} ⊥ _≈_ _≺_
65
66 Lex-≤ : ∀ {m n} → REL (Vec A m) (Vec A n) (a ⊔ ℓ₁ ⊔ ℓ₂)
67 Lex-≤ = Core.Lex {A = A} ⊤ _≈_ _≺_
68
69 ------------------------------------------------------------------------
70 -- Properties of Lex-<
71 ------------------------------------------------------------------------
72
73 module _ {_≈_ : Rel A ℓ₁} {_≺_ : Rel A ℓ₂} where
74 private
75 _≋_ = Pointwise _≈_
76 _<_ = Lex-< _≈_ _≺_
77
78 xs≮[] : ∀ {n} {xs : Vec A n} → ¬ xs < []
79 xs≮[] (base ())
80
81 ¬[]<[] : ¬ [] < []
82 ¬[]<[] = xs≮[]
83
84 module _ (≺-irrefl : Irreflexive _≈_ _≺_) where
85
86 <-irrefl : ∀ {m n} → Irreflexive (_≋_ {m} {n}) (_<_ {m} {n})
87 <-irrefl [] (base ())
88 <-irrefl (x≈y ∷ xs≋ys) (this x≺y m≡n) = ≺-irrefl x≈y x≺y
89 <-irrefl (x≈y ∷ xs≋ys) (next _ xs<ys) = <-irrefl xs≋ys xs<ys
90
91 module _ (≈-sym : Symmetric _≈_) (≺-resp-≈ : _≺_ Respects₂ _≈_) (≺-asym : Asymmetric _≺_) where
92
93 <-asym : ∀ {n} → Asymmetric (_<_ {n} {n})
94 <-asym (this x≺y m≡n) (this y≺x n≡m) = ≺-asym x≺y y≺x
95 <-asym (this x≺y m≡n) (next y≈x ys<xs) = asym⇒irr ≺-resp-≈ ≈-sym ≺-asym (≈-sym y≈x) x≺y
96 <-asym (next x≈y xs<ys) (this y≺x n≡m) = asym⇒irr ≺-resp-≈ ≈-sym ≺-asym (≈-sym x≈y) y≺x
97 <-asym (next x≈y xs<ys) (next y≈x ys<xs) = <-asym xs<ys ys<xs
98
99 <-antisym : Symmetric _≈_ → Irreflexive _≈_ _≺_ → Asymmetric _≺_ →
100 ∀ {n} → Antisymmetric (_≋_ {n} {n}) _<_
101 <-antisym = Core.antisym
102
103 <-trans : IsPartialEquivalence _≈_ → _≺_ Respects₂ _≈_ → Transitive _≺_ →
104 ∀ {m n o} → Trans (_<_ {m} {n}) (_<_ {n} {o}) _<_
105 <-trans = Core.transitive
106
107 module _ (≈-sym : Symmetric _≈_) (≺-cmp : Trichotomous _≈_ _≺_) where
108
109 <-cmp : ∀ {n} → Trichotomous _≋_ (_<_ {n})
110 <-cmp [] [] = tri≈ ¬[]<[] [] ¬[]<[]
111 <-cmp (x ∷ xs) (y ∷ ys) with ≺-cmp x y
112 ... | tri< x≺y x≉y x⊁y = tri< (this x≺y refl) (x≉y ∘ head) (≰-this (x≉y ∘ ≈-sym) x⊁y)
113 ... | tri> x⊀y x≉y x≻y = tri> (≰-this x≉y x⊀y) (x≉y ∘ head) (this x≻y refl)
114 ... | tri≈ x⊀y x≈y x⊁y with <-cmp xs ys
115 ... | tri< xs<ys xs≋̸ys xs≯ys = tri< (next x≈y xs<ys) (xs≋̸ys ∘ tail) (≰-next x⊁y xs≯ys)
116 ... | tri≈ xs≮ys xs≋ys xs≯ys = tri≈ (≰-next x⊀y xs≮ys) (x≈y ∷ xs≋ys) (≰-next x⊁y xs≯ys)
117 ... | tri> xs≮ys xs≋̸ys xs>ys = tri> (≰-next x⊀y xs≮ys) (xs≋̸ys ∘ tail) (next (≈-sym x≈y) xs>ys)
118
119 <-decidable : Decidable _≈_ → Decidable _≺_ →
120 ∀ {m n} → Decidable (_<_ {m} {n})
121 <-decidable = Core.decidable (no id)
122
123 <-respectsˡ : IsPartialEquivalence _≈_ → _≺_ Respectsˡ _≈_ →
124 ∀ {m n} → _Respectsˡ_ (_<_ {m} {n}) _≋_
125 <-respectsˡ = Core.respectsˡ
126
127 <-respectsʳ : IsPartialEquivalence _≈_ → _≺_ Respectsʳ _≈_ →
128 ∀ {m n} → _Respectsʳ_ (_<_ {m} {n}) _≋_
129 <-respectsʳ = Core.respectsʳ
130
131 <-respects₂ : IsPartialEquivalence _≈_ → _≺_ Respects₂ _≈_ →
132 ∀ {n} → _Respects₂_ (_<_ {n} {n}) _≋_
133 <-respects₂ = Core.respects₂
134
135 <-irrelevant : Irrelevant _≈_ → Irrelevant _≺_ → Irreflexive _≈_ _≺_ →
136 ∀ {m n} → Irrelevant (_<_ {m} {n})
137 <-irrelevant = Core.irrelevant (λ ())
138
139 module _ (≈-trans : Transitive _≈_) (≺-respʳ : _≺_ Respectsʳ _≈_ ) (≺-wf : WellFounded _≺_)
140 where
141
142 <-wellFounded : ∀ {n} → WellFounded (_<_ {n})
143 <-wellFounded {0} [] = acc λ ys<[] → contradiction ys<[] xs≮[]
144
145 <-wellFounded {suc n} xs = Subrelation.wellFounded <⇒uncons-Lex uncons-Lex-wellFounded xs
146 where
147 <⇒uncons-Lex : {xs ys : Vec A (suc n)} → xs < ys → (×-Lex _≈_ _≺_ _<_ on uncons) xs ys
148 <⇒uncons-Lex {x ∷ xs} {y ∷ ys} (this x<y _) = inj₁ x<y
149 <⇒uncons-Lex {x ∷ xs} {y ∷ ys} (next x≈y xs<ys) = inj₂ (x≈y , xs<ys)
150
151 uncons-Lex-wellFounded : WellFounded (×-Lex _≈_ _≺_ _<_ on uncons)
152 uncons-Lex-wellFounded = On.wellFounded uncons (×-wellFounded' ≈-trans ≺-respʳ ≺-wf <-wellFounded)
153
154 ------------------------------------------------------------------------
155 -- Structures
156
157 <-isStrictPartialOrder : IsStrictPartialOrder _≈_ _≺_ →
158 ∀ {n} → IsStrictPartialOrder (_≋_ {n} {n}) _<_
159 <-isStrictPartialOrder ≺-isStrictPartialOrder {n} = record
160 { isEquivalence = Pointwise.isEquivalence O.isEquivalence n
161 ; irrefl = <-irrefl O.irrefl
162 ; trans = <-trans O.Eq.isPartialEquivalence O.<-resp-≈ O.trans
163 ; <-resp-≈ = <-respects₂ O.Eq.isPartialEquivalence O.<-resp-≈
164 } where module O = IsStrictPartialOrder ≺-isStrictPartialOrder
165
166 <-isDecStrictPartialOrder : IsDecStrictPartialOrder _≈_ _≺_ →
167 ∀ {n} → IsDecStrictPartialOrder (_≋_ {n} {n}) _<_
168 <-isDecStrictPartialOrder ≺-isDecStrictPartialOrder = record
169 { isStrictPartialOrder = <-isStrictPartialOrder O.isStrictPartialOrder
170 ; _≟_ = Pointwise.decidable O._≟_
171 ; _<?_ = <-decidable O._≟_ O._<?_
172 } where module O = IsDecStrictPartialOrder ≺-isDecStrictPartialOrder
173
174 <-isStrictTotalOrder : IsStrictTotalOrder _≈_ _≺_ →
175 ∀ {n} → IsStrictTotalOrder (_≋_ {n} {n}) _<_
176 <-isStrictTotalOrder ≺-isStrictTotalOrder {n} = record
177 { isStrictPartialOrder = <-isStrictPartialOrder O.isStrictPartialOrder
178 ; compare = <-cmp O.Eq.sym O.compare
179 } where module O = IsStrictTotalOrder ≺-isStrictTotalOrder
180
181 ------------------------------------------------------------------------
182 -- Bundles for Lex-<
183
184 <-strictPartialOrder : StrictPartialOrder a ℓ₁ ℓ₂ → ℕ → StrictPartialOrder _ _ _
185 <-strictPartialOrder ≺-spo n = record
186 { isStrictPartialOrder = <-isStrictPartialOrder isStrictPartialOrder {n = n}
187 } where open StrictPartialOrder ≺-spo
188
189 <-decStrictPartialOrder : DecStrictPartialOrder a ℓ₁ ℓ₂ → ℕ → DecStrictPartialOrder _ _ _
190 <-decStrictPartialOrder ≺-dspo n = record
191 { isDecStrictPartialOrder = <-isDecStrictPartialOrder isDecStrictPartialOrder {n = n}
192 } where open DecStrictPartialOrder ≺-dspo
193
194 <-strictTotalOrder : StrictTotalOrder a ℓ₁ ℓ₂ → ℕ → StrictTotalOrder _ _ _
195 <-strictTotalOrder ≺-sto n = record
196 { isStrictTotalOrder = <-isStrictTotalOrder isStrictTotalOrder {n = n}
197 } where open StrictTotalOrder ≺-sto
198
199 ------------------------------------------------------------------------
200 -- Properties of Lex-≤
201 ------------------------------------------------------------------------
202
203 module _ {_≈_ : Rel A ℓ₁} {_≺_ : Rel A ℓ₂} where
204 private
205 _≋_ = Pointwise _≈_
206 _<_ = Lex-< _≈_ _≺_
207 _≤_ = Lex-≤ _≈_ _≺_
208
209 <⇒≤ : ∀ {m n} {xs : Vec A m} {ys : Vec A n} → xs < ys → xs ≤ ys
210 <⇒≤ = Core.map-P ⊥-elim
211
212 ≤-refl : ∀ {m n} → (_≋_ {m} {n}) ⇒ _≤_
213 ≤-refl [] = base tt
214 ≤-refl (x≈y ∷ xs≋ys) = next x≈y (≤-refl xs≋ys)
215
216 ≤-antisym : Symmetric _≈_ → Irreflexive _≈_ _≺_ → Asymmetric _≺_ →
217 ∀ {n} → Antisymmetric (_≋_ {n} {n}) _≤_
218 ≤-antisym = Core.antisym
219
220 ≤-resp₂ : IsPartialEquivalence _≈_ → _≺_ Respects₂ _≈_ →
221 ∀ {n} → _Respects₂_ (_≤_ {n} {n}) _≋_
222 ≤-resp₂ = Core.respects₂
223
224 ≤-trans : IsPartialEquivalence _≈_ → _≺_ Respects₂ _≈_ → Transitive _≺_ →
225 ∀ {m n o} → Trans (_≤_ {m} {n}) (_≤_ {n} {o}) _≤_
226 ≤-trans = Core.transitive
227
228 <-transʳ : IsPartialEquivalence _≈_ → _≺_ Respects₂ _≈_ → Transitive _≺_ →
229 ∀ {m n o} → Trans (_≤_ {m} {n}) (_<_ {n} {o}) _<_
230 <-transʳ ≈-equiv ≺-resp-≈ ≺-trans xs≤ys ys<zs = Core.map-P proj₂
231 (Core.transitive′ ≈-equiv ≺-resp-≈ ≺-trans xs≤ys ys<zs)
232
233 <-transˡ : IsPartialEquivalence _≈_ → _≺_ Respects₂ _≈_ → Transitive _≺_ →
234 ∀ {m n o} → Trans (_<_ {m} {n}) (_≤_ {n} {o}) _<_
235 <-transˡ ≈-equiv ≺-resp-≈ ≺-trans xs<ys ys≤zs = Core.map-P proj₁
236 (Core.transitive′ ≈-equiv ≺-resp-≈ ≺-trans xs<ys ys≤zs)
237
238 -- Note that trichotomy is an unnecessarily strong precondition for
239 -- the following lemma.
240
241 module _ (≈-sym : Symmetric _≈_) (≺-cmp : Trichotomous _≈_ _≺_) where
242
243 ≤-total : ∀ {n} → Total (_≤_ {n} {n})
244 ≤-total [] [] = inj₁ (base tt)
245 ≤-total (x ∷ xs) (y ∷ ys) with ≺-cmp x y
246 ... | tri< x≺y _ _ = inj₁ (this x≺y refl)
247 ... | tri> _ _ x≻y = inj₂ (this x≻y refl)
248 ... | tri≈ _ x≈y _ with ≤-total xs ys
249 ... | inj₁ xs<ys = inj₁ (next x≈y xs<ys)
250 ... | inj₂ xs>ys = inj₂ (next (≈-sym x≈y) xs>ys)
251
252 ≤-dec : Decidable _≈_ → Decidable _≺_ →
253 ∀ {m n} → Decidable (_≤_ {m} {n})
254 ≤-dec = Core.decidable (yes tt)
255
256 ≤-irrelevant : Irrelevant _≈_ → Irrelevant _≺_ → Irreflexive _≈_ _≺_ →
257 ∀ {m n} → Irrelevant (_≤_ {m} {n})
258 ≤-irrelevant = Core.irrelevant ⊤-irrelevant
259
260 ------------------------------------------------------------------------
261 -- Structures
262
263 ≤-isPreorder : IsEquivalence _≈_ → Transitive _≺_ → _≺_ Respects₂ _≈_ →
264 ∀ {n} → IsPreorder (_≋_ {n} {n}) _≤_
265 ≤-isPreorder ≈-equiv ≺-trans ≺-resp-≈ {n} = record
266 { isEquivalence = Pointwise.isEquivalence ≈-equiv n
267 ; reflexive = ≤-refl
268 ; trans = ≤-trans (IsEquivalence.isPartialEquivalence ≈-equiv) ≺-resp-≈ ≺-trans
269 }
270
271 ≤-isPartialOrder : IsStrictPartialOrder _≈_ _≺_ →
272 ∀ {n} → IsPartialOrder (_≋_ {n} {n}) _≤_
273 ≤-isPartialOrder ≺-isStrictPartialOrder = record
274 { isPreorder = ≤-isPreorder isEquivalence trans <-resp-≈
275 ; antisym = ≤-antisym Eq.sym irrefl asym
276 } where open IsStrictPartialOrder ≺-isStrictPartialOrder
277
278 ≤-isDecPartialOrder : IsDecStrictPartialOrder _≈_ _≺_ →
279 ∀ {n} → IsDecPartialOrder (_≋_ {n} {n}) _≤_
280 ≤-isDecPartialOrder ≺-isDecStrictPartialOrder = record
281 { isPartialOrder = ≤-isPartialOrder isStrictPartialOrder
282 ; _≟_ = Pointwise.decidable _≟_
283 ; _≤?_ = ≤-dec _≟_ _<?_
284 } where open IsDecStrictPartialOrder ≺-isDecStrictPartialOrder
285
286 ≤-isTotalOrder : IsStrictTotalOrder _≈_ _≺_ →
287 ∀ {n} → IsTotalOrder (_≋_ {n} {n}) _≤_
288 ≤-isTotalOrder ≺-isStrictTotalOrder = record
289 { isPartialOrder = ≤-isPartialOrder isStrictPartialOrder
290 ; total = ≤-total Eq.sym compare
291 } where open IsStrictTotalOrder ≺-isStrictTotalOrder
292
293 ≤-isDecTotalOrder : IsStrictTotalOrder _≈_ _≺_ →
294 ∀ {n} → IsDecTotalOrder (_≋_ {n} {n}) _≤_
295 ≤-isDecTotalOrder ≺-isStrictTotalOrder = record
296 { isTotalOrder = ≤-isTotalOrder ≺-isStrictTotalOrder
297 ; _≟_ = Pointwise.decidable _≟_
298 ; _≤?_ = ≤-dec _≟_ _<?_
299 } where open IsStrictTotalOrder ≺-isStrictTotalOrder
300
301 ------------------------------------------------------------------------
302 -- Bundles
303
304 ≤-preorder : Preorder a ℓ₁ ℓ₂ → ℕ → Preorder _ _ _
305 ≤-preorder ≺-pre n = record
306 { isPreorder = ≤-isPreorder isEquivalence trans ∼-resp-≈ {n = n}
307 } where open Preorder ≺-pre
308
309 ≤-partialOrder : StrictPartialOrder a ℓ₁ ℓ₂ → ℕ → Poset _ _ _
310 ≤-partialOrder ≺-spo n = record
311 { isPartialOrder = ≤-isPartialOrder isStrictPartialOrder {n = n}
312 } where open StrictPartialOrder ≺-spo
313
314 ≤-decPartialOrder : DecStrictPartialOrder a ℓ₁ ℓ₂ → ℕ → DecPoset _ _ _
315 ≤-decPartialOrder ≺-spo n = record
316 { isDecPartialOrder = ≤-isDecPartialOrder isDecStrictPartialOrder {n = n}
317 } where open DecStrictPartialOrder ≺-spo
318
319 ≤-totalOrder : StrictTotalOrder a ℓ₁ ℓ₂ → ℕ → TotalOrder _ _ _
320 ≤-totalOrder ≺-sto n = record
321 { isTotalOrder = ≤-isTotalOrder isStrictTotalOrder {n = n}
322 } where open StrictTotalOrder ≺-sto
323
324 ≤-decTotalOrder : StrictTotalOrder a ℓ₁ ℓ₂ → ℕ → DecTotalOrder _ _ _
325 ≤-decTotalOrder ≺-sto n = record
326 { isDecTotalOrder = ≤-isDecTotalOrder isStrictTotalOrder {n = n}
327 } where open StrictTotalOrder ≺-sto
328
329 ------------------------------------------------------------------------
330 -- Equational Reasoning
331 ------------------------------------------------------------------------
332
333 module ≤-Reasoning
334 {_≈_ : Rel A ℓ₁} {_≺_ : Rel A ℓ₂}
335 (≺-isStrictPartialOrder : IsStrictPartialOrder _≈_ _≺_)
336 (n : ℕ)
337 where
338
339 open IsStrictPartialOrder ≺-isStrictPartialOrder
340
341 open import Relation.Binary.Reasoning.Base.Triple
342 (≤-isPreorder isEquivalence trans <-resp-≈)
343 (<-asym Eq.sym <-resp-≈ asym)
344 (<-trans Eq.isPartialEquivalence <-resp-≈ trans)
345 (<-respects₂ Eq.isPartialEquivalence <-resp-≈)
346 (<⇒≤ {m = n})
347 (<-transˡ Eq.isPartialEquivalence <-resp-≈ trans)
348 (<-transʳ Eq.isPartialEquivalence <-resp-≈ trans)
349 public
350