Debian Codegraph @

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

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Lexicographic products of binary relations
5 ------------------------------------------------------------------------
6
7 -- The definition of lexicographic product used here is suitable if
8 -- the left-hand relation is a strict partial order.
9
10 {-# OPTIONS --cubical-compatible --safe #-}
11
12 module Data.Product.Relation.Binary.Lex.Strict where
13
14 open import Data.Product.Base
15 open import Data.Product.Relation.Binary.Pointwise.NonDependent as Pointwise
16 using (Pointwise)
17 open import Data.Sum.Base using (inj₁; inj₂; _-⊎-_; [_,_])
18 open import Function.Base using (flip; _on_; _$_; _∘_)
19 open import Induction.WellFounded using (Acc; acc; WfRec; WellFounded; Acc-resp-flip-≈)
20 open import Level using (Level)
21 open import Relation.Binary.Core using (Rel; _⇒_)
22 open import Relation.Binary.Bundles
23 using (Preorder; StrictPartialOrder; StrictTotalOrder)
24 open import Relation.Binary.Structures
25 using (IsEquivalence; IsPreorder; IsStrictPartialOrder; IsStrictTotalOrder)
26 open import Relation.Binary.Definitions
27 using (Transitive; Symmetric; Irreflexive; Asymmetric; Total; Decidable
28 ; Antisymmetric; Trichotomous; _Respects₂_; _Respectsʳ_; _Respectsˡ_
29 ; tri<; tri>; tri≈)
30 open import Relation.Binary.Consequences using (asym⇒irr)
31 open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_)
32 open import Relation.Nullary.Decidable.Core using (yes; no; _⊎-dec_; _×-dec_)
33 open import Relation.Nullary.Negation.Core using (contradiction)
34
35 private
36 variable
37 a b ℓ₁ ℓ₂ ℓ₃ ℓ₄ : Level
38 A : Set a
39 B : Set b
40
41 ------------------------------------------------------------------------
42 -- A lexicographic ordering over products
43
44 ×-Lex : (_≈₁_ : Rel A ℓ₁) (_<₁_ : Rel A ℓ₂) (_≤₂_ : Rel B ℓ₃) →
45 Rel (A × B) _
46 ×-Lex _≈₁_ _<₁_ _≤₂_ =
47 (_<₁_ on proj₁) -⊎- (_≈₁_ on proj₁) -×- (_≤₂_ on proj₂)
48
49 ------------------------------------------------------------------------
50 -- Some properties which are preserved by ×-Lex (under certain
51 -- assumptions).
52
53 ×-reflexive : (_≈₁_ : Rel A ℓ₁) (_∼₁_ : Rel A ℓ₂)
54 {_≈₂_ : Rel B ℓ₃} (_≤₂_ : Rel B ℓ₄) →
55 _≈₂_ ⇒ _≤₂_ → (Pointwise _≈₁_ _≈₂_) ⇒ (×-Lex _≈₁_ _∼₁_ _≤₂_)
56 ×-reflexive _ _ _ refl₂ = λ x≈y →
57 inj₂ (proj₁ x≈y , refl₂ (proj₂ x≈y))
58
59 module _ {_≈₁_ : Rel A ℓ₁} {_<₁_ : Rel A ℓ₂} {_<₂_ : Rel B ℓ₃} where
60
61 private
62 _<ₗₑₓ_ = ×-Lex _≈₁_ _<₁_ _<₂_
63
64
65 ×-transitive : IsEquivalence _≈₁_ → _<₁_ Respects₂ _≈₁_ → Transitive _<₁_ →
66 Transitive _<₂_ → Transitive _<ₗₑₓ_
67 ×-transitive eq₁ resp₁ trans₁ trans₂ = trans
68 where
69 module Eq₁ = IsEquivalence eq₁
70
71 trans : Transitive _<ₗₑₓ_
72 trans (inj₁ x₁<y₁) (inj₁ y₁<z₁) = inj₁ (trans₁ x₁<y₁ y₁<z₁)
73 trans (inj₁ x₁<y₁) (inj₂ y≈≤z) =
74 inj₁ (proj₁ resp₁ (proj₁ y≈≤z) x₁<y₁)
75 trans (inj₂ x≈≤y) (inj₁ y₁<z₁) =
76 inj₁ (proj₂ resp₁ (Eq₁.sym $ proj₁ x≈≤y) y₁<z₁)
77 trans (inj₂ x≈≤y) (inj₂ y≈≤z) =
78 inj₂ ( Eq₁.trans (proj₁ x≈≤y) (proj₁ y≈≤z)
79 , trans₂ (proj₂ x≈≤y) (proj₂ y≈≤z))
80
81 ×-asymmetric : Symmetric _≈₁_ → _<₁_ Respects₂ _≈₁_ →
82 Asymmetric _<₁_ → Asymmetric _<₂_ →
83 Asymmetric _<ₗₑₓ_
84 ×-asymmetric sym₁ resp₁ asym₁ asym₂ = asym
85 where
86 irrefl₁ : Irreflexive _≈₁_ _<₁_
87 irrefl₁ = asym⇒irr resp₁ sym₁ asym₁
88
89 asym : Asymmetric _<ₗₑₓ_
90 asym (inj₁ x₁<y₁) (inj₁ y₁<x₁) = asym₁ x₁<y₁ y₁<x₁
91 asym (inj₁ x₁<y₁) (inj₂ y≈<x) = irrefl₁ (sym₁ $ proj₁ y≈<x) x₁<y₁
92 asym (inj₂ x≈<y) (inj₁ y₁<x₁) = irrefl₁ (sym₁ $ proj₁ x≈<y) y₁<x₁
93 asym (inj₂ x≈<y) (inj₂ y≈<x) = asym₂ (proj₂ x≈<y) (proj₂ y≈<x)
94
95 ×-total₁ : Total _<₁_ → Total _<ₗₑₓ_
96 ×-total₁ total₁ x y with total₁ (proj₁ x) (proj₁ y)
97 ... | inj₁ x₁<y₁ = inj₁ (inj₁ x₁<y₁)
98 ... | inj₂ x₁>y₁ = inj₂ (inj₁ x₁>y₁)
99
100 ×-total₂ : Symmetric _≈₁_ →
101 Trichotomous _≈₁_ _<₁_ → Total _<₂_ →
102 Total _<ₗₑₓ_
103 ×-total₂ sym tri₁ total₂ x y with tri₁ (proj₁ x) (proj₁ y)
104 ... | tri< x₁<y₁ _ _ = inj₁ (inj₁ x₁<y₁)
105 ... | tri> _ _ y₁<x₁ = inj₂ (inj₁ y₁<x₁)
106 ... | tri≈ _ x₁≈y₁ _ with total₂ (proj₂ x) (proj₂ y)
107 ... | inj₁ x₂≤y₂ = inj₁ (inj₂ (x₁≈y₁ , x₂≤y₂))
108 ... | inj₂ y₂≤x₂ = inj₂ (inj₂ (sym x₁≈y₁ , y₂≤x₂))
109
110 ×-decidable : Decidable _≈₁_ → Decidable _<₁_ → Decidable _<₂_ →
111 Decidable _<ₗₑₓ_
112 ×-decidable dec-≈₁ dec-<₁ dec-≤₂ x y =
113 dec-<₁ (proj₁ x) (proj₁ y)
114 ⊎-dec
115 (dec-≈₁ (proj₁ x) (proj₁ y)
116 ×-dec
117 dec-≤₂ (proj₂ x) (proj₂ y))
118
119 module _ {_≈₁_ : Rel A ℓ₁} {_<₁_ : Rel A ℓ₂}
120 {_≈₂_ : Rel B ℓ₃} {_<₂_ : Rel B ℓ₄} where
121
122 private
123 _≋_ = Pointwise _≈₁_ _≈₂_
124 _<ₗₑₓ_ = ×-Lex _≈₁_ _<₁_ _<₂_
125
126 ×-irreflexive : Irreflexive _≈₁_ _<₁_ → Irreflexive _≈₂_ _<₂_ →
127 Irreflexive (Pointwise _≈₁_ _≈₂_) _<ₗₑₓ_
128 ×-irreflexive ir₁ ir₂ x≈y (inj₁ x₁<y₁) = ir₁ (proj₁ x≈y) x₁<y₁
129 ×-irreflexive ir₁ ir₂ x≈y (inj₂ x≈<y) = ir₂ (proj₂ x≈y) (proj₂ x≈<y)
130
131 ×-antisymmetric : Symmetric _≈₁_ → Irreflexive _≈₁_ _<₁_ → Asymmetric _<₁_ →
132 Antisymmetric _≈₂_ _<₂_ → Antisymmetric _≋_ _<ₗₑₓ_
133 ×-antisymmetric sym₁ irrefl₁ asym₁ antisym₂ = antisym
134 where
135 antisym : Antisymmetric _≋_ _<ₗₑₓ_
136 antisym (inj₁ x₁<y₁) (inj₁ y₁<x₁) =
137 contradiction y₁<x₁ (asym₁ x₁<y₁)
138 antisym (inj₁ x₁<y₁) (inj₂ y≈≤x) =
139 contradiction x₁<y₁ (irrefl₁ (sym₁ $ proj₁ y≈≤x))
140 antisym (inj₂ x≈≤y) (inj₁ y₁<x₁) =
141 contradiction y₁<x₁ (irrefl₁ (sym₁ $ proj₁ x≈≤y))
142 antisym (inj₂ x≈≤y) (inj₂ y≈≤x) =
143 proj₁ x≈≤y , antisym₂ (proj₂ x≈≤y) (proj₂ y≈≤x)
144
145 ×-respectsʳ : Transitive _≈₁_ →
146 _<₁_ Respectsʳ _≈₁_ → _<₂_ Respectsʳ _≈₂_ →
147 _<ₗₑₓ_ Respectsʳ _≋_
148 ×-respectsʳ trans resp₁ resp₂ y≈y' (inj₁ x₁<y₁) = inj₁ (resp₁ (proj₁ y≈y') x₁<y₁)
149 ×-respectsʳ trans resp₁ resp₂ y≈y' (inj₂ x≈<y) = inj₂ (trans (proj₁ x≈<y) (proj₁ y≈y')
150 , (resp₂ (proj₂ y≈y') (proj₂ x≈<y)))
151
152 ×-respectsˡ : Symmetric _≈₁_ → Transitive _≈₁_ →
153 _<₁_ Respectsˡ _≈₁_ → _<₂_ Respectsˡ _≈₂_ →
154 _<ₗₑₓ_ Respectsˡ _≋_
155 ×-respectsˡ sym trans resp₁ resp₂ x≈x' (inj₁ x₁<y₁) = inj₁ (resp₁ (proj₁ x≈x') x₁<y₁)
156 ×-respectsˡ sym trans resp₁ resp₂ x≈x' (inj₂ x≈<y) = inj₂ (trans (sym $ proj₁ x≈x') (proj₁ x≈<y)
157 , (resp₂ (proj₂ x≈x') (proj₂ x≈<y)))
158
159 ×-respects₂ : IsEquivalence _≈₁_ →
160 _<₁_ Respects₂ _≈₁_ → _<₂_ Respects₂ _≈₂_ →
161 _<ₗₑₓ_ Respects₂ _≋_
162 ×-respects₂ eq₁ resp₁ resp₂ = ×-respectsʳ trans (proj₁ resp₁) (proj₁ resp₂)
163 , ×-respectsˡ sym trans (proj₂ resp₁) (proj₂ resp₂)
164 where open IsEquivalence eq₁
165
166 ×-compare : Symmetric _≈₁_ →
167 Trichotomous _≈₁_ _<₁_ → Trichotomous _≈₂_ _<₂_ →
168 Trichotomous _≋_ _<ₗₑₓ_
169 ×-compare sym₁ cmp₁ cmp₂ (x₁ , x₂) (y₁ , y₂) with cmp₁ x₁ y₁
170 ... | (tri< x₁<y₁ x₁≉y₁ x₁≯y₁) =
171 tri< (inj₁ x₁<y₁)
172 (x₁≉y₁ ∘ proj₁)
173 [ x₁≯y₁ , x₁≉y₁ ∘ sym₁ ∘ proj₁ ]
174 ... | (tri> x₁≮y₁ x₁≉y₁ x₁>y₁) =
175 tri> [ x₁≮y₁ , x₁≉y₁ ∘ proj₁ ]
176 (x₁≉y₁ ∘ proj₁)
177 (inj₁ x₁>y₁)
178 ... | (tri≈ x₁≮y₁ x₁≈y₁ x₁≯y₁) with cmp₂ x₂ y₂
179 ... | (tri< x₂<y₂ x₂≉y₂ x₂≯y₂) =
180 tri< (inj₂ (x₁≈y₁ , x₂<y₂))
181 (x₂≉y₂ ∘ proj₂)
182 [ x₁≯y₁ , x₂≯y₂ ∘ proj₂ ]
183 ... | (tri> x₂≮y₂ x₂≉y₂ x₂>y₂) =
184 tri> [ x₁≮y₁ , x₂≮y₂ ∘ proj₂ ]
185 (x₂≉y₂ ∘ proj₂)
186 (inj₂ (sym₁ x₁≈y₁ , x₂>y₂))
187 ... | (tri≈ x₂≮y₂ x₂≈y₂ x₂≯y₂) =
188 tri≈ [ x₁≮y₁ , x₂≮y₂ ∘ proj₂ ]
189 (x₁≈y₁ , x₂≈y₂)
190 [ x₁≯y₁ , x₂≯y₂ ∘ proj₂ ]
191
192 module _ {_≈₁_ : Rel A ℓ₁} {_<₁_ : Rel A ℓ₂} {_<₂_ : Rel B ℓ₃} where
193
194 private
195 _<ₗₑₓ_ = ×-Lex _≈₁_ _<₁_ _<₂_
196
197 ×-wellFounded' : Transitive _≈₁_ →
198 _<₁_ Respectsʳ _≈₁_ →
199 WellFounded _<₁_ →
200 WellFounded _<₂_ →
201 WellFounded _<ₗₑₓ_
202 ×-wellFounded' trans resp wf₁ wf₂ (x , y) = acc (×-acc (wf₁ x) (wf₂ y))
203 where
204 ×-acc : ∀ {x y} →
205 Acc _<₁_ x → Acc _<₂_ y →
206 WfRec _<ₗₑₓ_ (Acc _<ₗₑₓ_) (x , y)
207 ×-acc (acc rec₁) acc₂ (inj₁ u<x)
208 = acc (×-acc (rec₁ u<x) (wf₂ _))
209 ×-acc acc₁ (acc rec₂) (inj₂ (u≈x , v<y))
210 = Acc-resp-flip-≈
211 (×-respectsʳ {_<₁_ = _<₁_} {_<₂_ = _<₂_} trans resp (≡.respʳ _<₂_))
212 (u≈x , ≡.refl)
213 (acc (×-acc acc₁ (rec₂ v<y)))
214
215
216 module _ {_<₁_ : Rel A ℓ₁} {_<₂_ : Rel B ℓ₂} where
217
218 private
219 _<ₗₑₓ_ = ×-Lex _≡_ _<₁_ _<₂_
220
221 ×-wellFounded : WellFounded _<₁_ →
222 WellFounded _<₂_ →
223 WellFounded _<ₗₑₓ_
224 ×-wellFounded = ×-wellFounded' ≡.trans (≡.respʳ _<₁_)
225
226 ------------------------------------------------------------------------
227 -- Collections of properties which are preserved by ×-Lex.
228
229 module _ {_≈₁_ : Rel A ℓ₁} {_<₁_ : Rel A ℓ₂}
230 {_≈₂_ : Rel B ℓ₃} {_<₂_ : Rel B ℓ₄} where
231
232 private
233 _≋_ = Pointwise _≈₁_ _≈₂_
234 _<ₗₑₓ_ = ×-Lex _≈₁_ _<₁_ _<₂_
235
236 ×-isPreorder : IsPreorder _≈₁_ _<₁_ →
237 IsPreorder _≈₂_ _<₂_ →
238 IsPreorder _≋_ _<ₗₑₓ_
239 ×-isPreorder pre₁ pre₂ =
240 record
241 { isEquivalence = Pointwise.×-isEquivalence
242 (isEquivalence pre₁) (isEquivalence pre₂)
243 ; reflexive = ×-reflexive _≈₁_ _<₁_ _<₂_ (reflexive pre₂)
244 ; trans = ×-transitive {_<₂_ = _<₂_}
245 (isEquivalence pre₁) (≲-resp-≈ pre₁)
246 (trans pre₁) (trans pre₂)
247 }
248 where open IsPreorder
249
250 ×-isStrictPartialOrder : IsStrictPartialOrder _≈₁_ _<₁_ →
251 IsStrictPartialOrder _≈₂_ _<₂_ →
252 IsStrictPartialOrder _≋_ _<ₗₑₓ_
253 ×-isStrictPartialOrder spo₁ spo₂ =
254 record
255 { isEquivalence = Pointwise.×-isEquivalence
256 (isEquivalence spo₁) (isEquivalence spo₂)
257 ; irrefl = ×-irreflexive {_<₁_ = _<₁_} {_<₂_ = _<₂_}
258 (irrefl spo₁) (irrefl spo₂)
259 ; trans = ×-transitive {_<₁_ = _<₁_} {_<₂_ = _<₂_}
260 (isEquivalence spo₁)
261 (<-resp-≈ spo₁) (trans spo₁)
262 (trans spo₂)
263 ; <-resp-≈ = ×-respects₂ (isEquivalence spo₁)
264 (<-resp-≈ spo₁)
265 (<-resp-≈ spo₂)
266 }
267 where open IsStrictPartialOrder
268
269 ×-isStrictTotalOrder : IsStrictTotalOrder _≈₁_ _<₁_ →
270 IsStrictTotalOrder _≈₂_ _<₂_ →
271 IsStrictTotalOrder _≋_ _<ₗₑₓ_
272 ×-isStrictTotalOrder spo₁ spo₂ =
273 record
274 { isStrictPartialOrder = ×-isStrictPartialOrder
275 (isStrictPartialOrder spo₁)
276 (isStrictPartialOrder spo₂)
277 ; compare = ×-compare (Eq.sym spo₁) (compare spo₁)
278 (compare spo₂)
279 }
280 where open IsStrictTotalOrder
281
282 ------------------------------------------------------------------------
283 -- "Bundles" can also be combined.
284
285 ×-preorder : Preorder a ℓ₁ ℓ₂ →
286 Preorder b ℓ₃ ℓ₄ →
287 Preorder _ _ _
288 ×-preorder p₁ p₂ = record
289 { isPreorder = ×-isPreorder (isPreorder p₁) (isPreorder p₂)
290 } where open Preorder
291
292 ×-strictPartialOrder : StrictPartialOrder a ℓ₁ ℓ₂ →
293 StrictPartialOrder b ℓ₃ ℓ₄ →
294 StrictPartialOrder _ _ _
295 ×-strictPartialOrder s₁ s₂ = record
296 { isStrictPartialOrder = ×-isStrictPartialOrder
297 (isStrictPartialOrder s₁) (isStrictPartialOrder s₂)
298 } where open StrictPartialOrder
299
300 ×-strictTotalOrder : StrictTotalOrder a ℓ₁ ℓ₂ →
301 StrictTotalOrder b ℓ₃ ℓ₄ →
302 StrictTotalOrder _ _ _
303 ×-strictTotalOrder s₁ s₂ = record
304 { isStrictTotalOrder = ×-isStrictTotalOrder
305 (isStrictTotalOrder s₁) (isStrictTotalOrder s₂)
306 } where open StrictTotalOrder