Debian Codegraph @

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

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Lexicographic ordering of same-length vector
5 ------------------------------------------------------------------------
6
7 -- The definitions of lexicographic orderings used here is suitable if
8 -- the argument order is a (non-strict) partial order.
9
10 {-# OPTIONS --cubical-compatible --safe #-}
11
12 module Data.Vec.Relation.Binary.Lex.NonStrict where
13
14 open import Data.Empty using (⊥; ⊥-elim)
15 open import Data.Unit.Base using (⊤; tt)
16 open import Data.Product.Base using (proj₁; proj₂)
17 open import Data.Nat.Base using (ℕ)
18 open import Data.Vec.Base using (Vec; []; _∷_)
19 import Data.Vec.Relation.Binary.Lex.Strict as Strict
20 open import Data.Vec.Relation.Binary.Pointwise.Inductive as Pointwise
21 using (Pointwise; []; _∷_; head; tail)
22 open import Function.Base using (id)
23 open import Level using (Level; _⊔_)
24 open import Relation.Binary.Core using (REL; Rel; _⇒_)
25 open import Relation.Binary.Bundles
26 using (Poset; StrictPartialOrder; DecPoset; DecStrictPartialOrder
27 ; DecTotalOrder; StrictTotalOrder; Preorder; TotalOrder)
28 open import Relation.Binary.Structures
29 using (IsEquivalence; IsPartialOrder; IsStrictPartialOrder; IsDecPartialOrder
30 ; IsDecStrictPartialOrder; IsDecTotalOrder; IsStrictTotalOrder
31 ; IsPreorder; IsTotalOrder)
32 open import Relation.Binary.Definitions
33 using (Irreflexive; _Respects₂_; Antisymmetric; Asymmetric; Symmetric; Trans
34 ; Decidable; Total; Trichotomous)
35 import Relation.Binary.Construct.NonStrictToStrict as Conv
36 open import Relation.Nullary.Decidable.Core using (yes; no)
37
38 private
39 variable
40 a ℓ₁ ℓ₂ : Level
41 A : Set a
42
43 ------------------------------------------------------------------------
44 -- Publicly re-export definitions from Core
45 ------------------------------------------------------------------------
46
47 open import Data.Vec.Relation.Binary.Lex.Core as Core public
48 using (base; this; next; ≰-this; ≰-next)
49
50 ------------------------------------------------------------------------
51 -- Definitions
52 ------------------------------------------------------------------------
53
54 module _ {A : Set a} (_≈_ : Rel A ℓ₁) (_≼_ : Rel A ℓ₂) where
55
56 Lex-< : ∀ {m n} → REL (Vec A m) (Vec A n) (a ⊔ ℓ₁ ⊔ ℓ₂)
57 Lex-< = Core.Lex {A = A} ⊥ _≈_ (Conv._<_ _≈_ _≼_)
58
59 Lex-≤ : ∀ {m n} → REL (Vec A m) (Vec A n) (a ⊔ ℓ₁ ⊔ ℓ₂)
60 Lex-≤ = Core.Lex {A = A} ⊤ _≈_ (Conv._<_ _≈_ _≼_)
61
62 ------------------------------------------------------------------------
63 -- Properties of Lex-<
64 ------------------------------------------------------------------------
65
66 module _ {_≈_ : Rel A ℓ₁} {_≼_ : Rel A ℓ₂} where
67
68 private
69 _≋_ = Pointwise _≈_
70 _<_ = Lex-< _≈_ _≼_
71
72 <-irrefl : ∀ {m n} → Irreflexive (_≋_ {m} {n}) _<_
73 <-irrefl = Strict.<-irrefl (Conv.<-irrefl _≈_ _≼_)
74
75 <-asym : IsEquivalence _≈_ → _≼_ Respects₂ _≈_ → Antisymmetric _≈_ _≼_ →
76 ∀ {n} → Asymmetric (_<_ {n} {n})
77 <-asym ≈-equiv ≼-resp-≈ ≼-antisym = Strict.<-asym sym
78 (Conv.<-resp-≈ _ _ ≈-equiv ≼-resp-≈)
79 (Conv.<-asym _≈_ _ ≼-antisym)
80 where open IsEquivalence ≈-equiv
81
82 <-antisym : Symmetric _≈_ → Antisymmetric _≈_ _≼_ →
83 ∀ {n} → Antisymmetric (_≋_ {n} {n}) _<_
84 <-antisym ≈-sym ≼-antisym = Core.antisym ≈-sym
85 (Conv.<-irrefl _≈_ _≼_)
86 (Conv.<-asym _≈_ _≼_ ≼-antisym)
87
88 <-trans : IsPartialOrder _≈_ _≼_ →
89 ∀ {m n o} → Trans (_<_ {m} {n}) (_<_ {n} {o}) _<_
90 <-trans ≼-isPartialOrder = Core.transitive Eq.isPartialEquivalence
91 (Conv.<-resp-≈ _ _ isEquivalence ≤-resp-≈)
92 (Conv.<-trans _ _ ≼-isPartialOrder)
93 where open IsPartialOrder ≼-isPartialOrder
94
95 <-resp₂ : IsEquivalence _≈_ → _≼_ Respects₂ _≈_ →
96 ∀ {n} → _Respects₂_ (_<_ {n} {n}) _≋_
97 <-resp₂ ≈-equiv ≼-resp-≈ = Core.respects₂
98 (IsEquivalence.isPartialEquivalence ≈-equiv)
99 (Conv.<-resp-≈ _ _ ≈-equiv ≼-resp-≈)
100
101 <-cmp : Symmetric _≈_ → Decidable _≈_ → Antisymmetric _≈_ _≼_ → Total _≼_ →
102 ∀ {n} → Trichotomous (_≋_ {n} {n}) _<_
103 <-cmp ≈-sym _≟_ ≼-antisym ≼-total = Strict.<-cmp ≈-sym
104 (Conv.<-trichotomous _ _ ≈-sym _≟_ ≼-antisym ≼-total)
105
106 <-dec : Decidable _≈_ → Decidable _≼_ → ∀ {m n} → Decidable (_<_ {m} {n})
107 <-dec _≟_ _≼?_ = Core.decidable (no id) _≟_
108 (Conv.<-decidable _ _ _≟_ _≼?_)
109
110 ------------------------------------------------------------------------
111 -- Structures
112
113 <-isStrictPartialOrder : IsPartialOrder _≈_ _≼_ →
114 ∀ {n} → IsStrictPartialOrder (_≋_ {n} {n}) _<_
115 <-isStrictPartialOrder ≼-isPartialOrder {n} = Strict.<-isStrictPartialOrder
116 (Conv.<-isStrictPartialOrder _ _ ≼-isPartialOrder)
117
118 <-isDecStrictPartialOrder : IsDecPartialOrder _≈_ _≼_ →
119 ∀ {n} → IsDecStrictPartialOrder (_≋_ {n} {n}) _<_
120 <-isDecStrictPartialOrder ≼-isDecPartialOrder {n} = Strict.<-isDecStrictPartialOrder
121 (Conv.<-isDecStrictPartialOrder _ _ ≼-isDecPartialOrder)
122
123 <-isStrictTotalOrder : IsDecTotalOrder _≈_ _≼_ →
124 ∀ {n} → IsStrictTotalOrder (_≋_ {n} {n}) _<_
125 <-isStrictTotalOrder ≼-isDecTotalOrder {n} = Strict.<-isStrictTotalOrder
126 (Conv.<-isStrictTotalOrder₂ _ _ ≼-isDecTotalOrder)
127
128 ------------------------------------------------------------------------
129 -- Bundles
130
131 <-strictPartialOrder : Poset a ℓ₁ ℓ₂ → ℕ → StrictPartialOrder _ _ _
132 <-strictPartialOrder ≼-po n = record
133 { isStrictPartialOrder = <-isStrictPartialOrder isPartialOrder {n = n}
134 } where open Poset ≼-po
135
136 <-decStrictPartialOrder : DecPoset a ℓ₁ ℓ₂ → ℕ → DecStrictPartialOrder _ _ _
137 <-decStrictPartialOrder ≼-dpo n = record
138 { isDecStrictPartialOrder = <-isDecStrictPartialOrder isDecPartialOrder {n = n}
139 } where open DecPoset ≼-dpo
140
141 <-strictTotalOrder : DecTotalOrder a ℓ₁ ℓ₂ → ℕ → StrictTotalOrder _ _ _
142 <-strictTotalOrder ≼-dto n = record
143 { isStrictTotalOrder = <-isStrictTotalOrder isDecTotalOrder {n = n}
144 } where open DecTotalOrder ≼-dto
145
146 ------------------------------------------------------------------------
147 -- Properties of Lex-≤
148 ------------------------------------------------------------------------
149
150 module _ {_≈_ : Rel A ℓ₁} {_≼_ : Rel A ℓ₂} where
151
152 private
153 _≋_ = Pointwise _≈_
154 _<_ = Lex-< _≈_ _≼_
155 _≤_ = Lex-≤ _≈_ _≼_
156
157 <⇒≤ : ∀ {m n} {xs : Vec A m} {ys : Vec A n} → xs < ys → xs ≤ ys
158 <⇒≤ = Core.map-P ⊥-elim
159
160 ≤-refl : ∀ {m n} → (_≋_ {m} {n}) ⇒ _≤_
161 ≤-refl = Strict.≤-refl
162
163 ≤-antisym : Symmetric _≈_ → Antisymmetric _≈_ _≼_ →
164 ∀ {n} → Antisymmetric (_≋_ {n} {n}) _≤_
165 ≤-antisym ≈-sym ≼-antisym = Core.antisym ≈-sym
166 (Conv.<-irrefl _≈_ _≼_)
167 (Conv.<-asym _ _≼_ ≼-antisym)
168
169 private
170 trans : IsPartialOrder _≈_ _≼_ → ∀ {P₁ P₂} {m n o} →
171 Trans (Core.Lex P₁ _≈_ (Conv._<_ _≈_ _≼_) {m} {n}) (Core.Lex P₂ _≈_ (Conv._<_ _≈_ _≼_) {n} {o}) _
172 trans ≼-po = Core.transitive′
173 (IsEquivalence.isPartialEquivalence isEquivalence)
174 (Conv.<-resp-≈ _ _ isEquivalence ≤-resp-≈)
175 (Conv.<-trans _ _≼_ ≼-po)
176 where open IsPartialOrder ≼-po
177
178 ≤-trans : IsPartialOrder _≈_ _≼_ → ∀ {m n o} → Trans (_≤_ {m} {n}) (_≤_ {n} {o}) _≤_
179 ≤-trans ≼-po xs≤ys ys≤zs = Core.map-P proj₁ (trans ≼-po xs≤ys ys≤zs)
180
181 <-transʳ : IsPartialOrder _≈_ _≼_ → ∀ {m n o} → Trans (_≤_ {m} {n}) (_<_ {n} {o}) _<_
182 <-transʳ ≼-po xs≤ys ys<zs = Core.map-P proj₂ (trans ≼-po xs≤ys ys<zs)
183
184 <-transˡ : IsPartialOrder _≈_ _≼_ → ∀ {m n o} → Trans (_<_ {m} {n}) (_≤_ {n} {o}) _<_
185 <-transˡ ≼-po xs<ys ys≤zs = Core.map-P proj₁ (trans ≼-po xs<ys ys≤zs)
186
187 ≤-total : Symmetric _≈_ → Decidable _≈_ → Antisymmetric _≈_ _≼_ → Total _≼_ →
188 ∀ {n} → Total (_≤_ {n})
189 ≤-total ≈-sym _≟_ ≼-antisym ≼-total = Strict.≤-total ≈-sym
190 (Conv.<-trichotomous _ _ ≈-sym _≟_ ≼-antisym ≼-total)
191
192 ≤-dec : Decidable _≈_ → Decidable _≼_ →
193 ∀ {m n} → Decidable (_≤_ {m} {n})
194 ≤-dec _≟_ _≼?_ = Core.decidable (yes tt) _≟_
195 (Conv.<-decidable _ _ _≟_ _≼?_)
196
197 ≤-resp₂ : IsEquivalence _≈_ → _≼_ Respects₂ _≈_ →
198 ∀ {n} → _Respects₂_ (_≤_ {n} {n}) _≋_
199 ≤-resp₂ ≈-equiv ≼-resp-≈ = Core.respects₂
200 (IsEquivalence.isPartialEquivalence ≈-equiv)
201 (Conv.<-resp-≈ _ _ ≈-equiv ≼-resp-≈)
202
203 ------------------------------------------------------------------------
204 -- Structures
205
206 ≤-isPreorder : IsPartialOrder _≈_ _≼_ →
207 ∀ {n} → IsPreorder (_≋_ {n} {n}) _≤_
208 ≤-isPreorder ≼-po = Strict.≤-isPreorder isEquivalence (Conv.<-trans _ _ ≼-po) (Conv.<-resp-≈ _ _ isEquivalence ≤-resp-≈)
209 where open IsPartialOrder ≼-po
210
211 ≤-isPartialOrder : IsPartialOrder _≈_ _≼_ →
212 ∀ {n} → IsPartialOrder (_≋_ {n} {n}) _≤_
213 ≤-isPartialOrder ≼-po = Strict.≤-isPartialOrder (Conv.<-isStrictPartialOrder _ _ ≼-po)
214
215 ≤-isDecPartialOrder : IsDecPartialOrder _≈_ _≼_ →
216 ∀ {n} → IsDecPartialOrder (_≋_ {n} {n}) _≤_
217 ≤-isDecPartialOrder ≼-dpo = Strict.≤-isDecPartialOrder (Conv.<-isDecStrictPartialOrder _ _ ≼-dpo)
218
219 ≤-isTotalOrder : Decidable _≈_ → IsTotalOrder _≈_ _≼_ →
220 ∀ {n} → IsTotalOrder (_≋_ {n} {n}) _≤_
221 ≤-isTotalOrder _≟_ ≼-isTotalOrder = Strict.≤-isTotalOrder (Conv.<-isStrictTotalOrder₁ _ _ _≟_ ≼-isTotalOrder)
222
223 ≤-isDecTotalOrder : IsDecTotalOrder _≈_ _≼_ →
224 ∀ {n} → IsDecTotalOrder (_≋_ {n} {n}) _≤_
225 ≤-isDecTotalOrder ≼-isDecTotalOrder = Strict.≤-isDecTotalOrder (Conv.<-isStrictTotalOrder₂ _ _ ≼-isDecTotalOrder)
226
227 ------------------------------------------------------------------------
228 -- Bundles
229
230 ≤-preorder : Poset a ℓ₁ ℓ₂ → ℕ → Preorder _ _ _
231 ≤-preorder ≼-po n = record
232 { isPreorder = ≤-isPreorder isPartialOrder {n = n}
233 } where open Poset ≼-po
234
235 ≤-poset : Poset a ℓ₁ ℓ₂ → ℕ → Poset _ _ _
236 ≤-poset ≼-po n = record
237 { isPartialOrder = ≤-isPartialOrder isPartialOrder {n = n}
238 } where open Poset ≼-po
239
240 ≤-decPoset : DecPoset a ℓ₁ ℓ₂ → ℕ → DecPoset _ _ _
241 ≤-decPoset ≼-dpo n = record
242 { isDecPartialOrder = ≤-isDecPartialOrder isDecPartialOrder {n = n}
243 } where open DecPoset ≼-dpo
244
245 ≤-totalOrder : (≼-dto : TotalOrder a ℓ₁ ℓ₂) → Decidable (TotalOrder._≈_ ≼-dto) → ℕ → TotalOrder _ _ _
246 ≤-totalOrder ≼-dto _≟_ n = record
247 { isTotalOrder = ≤-isTotalOrder _≟_ isTotalOrder {n = n}
248 } where open TotalOrder ≼-dto
249
250 ≤-decTotalOrder : DecTotalOrder a ℓ₁ ℓ₂ → ℕ → DecTotalOrder _ _ _
251 ≤-decTotalOrder ≼-dto n = record
252 { isDecTotalOrder = ≤-isDecTotalOrder isDecTotalOrder {n = n}
253 } where open DecTotalOrder ≼-dto
254
255 ------------------------------------------------------------------------
256 -- Reasoning
257 ------------------------------------------------------------------------
258
259 module ≤-Reasoning {_≈_ : Rel A ℓ₁} {_≼_ : Rel A ℓ₂}
260 (≼-po : IsPartialOrder _≈_ _≼_)
261 (n : ℕ)
262 where
263
264 open IsPartialOrder ≼-po
265
266 open import Relation.Binary.Reasoning.Base.Triple
267 (≤-isPreorder ≼-po {n})
268 (<-asym isEquivalence ≤-resp-≈ antisym)
269 (<-trans ≼-po)
270 (<-resp₂ isEquivalence ≤-resp-≈)
271 <⇒≤
272 (<-transˡ ≼-po)
273 (<-transʳ ≼-po)
274 public