Debian Codegraph @

src/Data/List/Relation/Binary/Sublist/Heterogeneous/Properties.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Properties of the heterogeneous sublist relation
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.List.Relation.Binary.Sublist.Heterogeneous.Properties where
10
11 open import Level
12
13 open import Data.Bool.Base using (true; false)
14 open import Data.List.Relation.Unary.All using (Null; []; _∷_)
15 open import Data.List.Relation.Unary.Any using (Any; here; there)
16 open import Data.List.Base as List hiding (map; _∷ʳ_)
17 import Data.List.Properties as List
18 open import Data.List.Relation.Unary.Any.Properties
19 using (here-injective; there-injective)
20 open import Data.List.Relation.Binary.Pointwise as Pw using (Pointwise; []; _∷_)
21 open import Data.List.Relation.Binary.Sublist.Heterogeneous
22
23 open import Data.Maybe.Relation.Unary.All as Maybe using (nothing; just)
24 open import Data.Nat.Base using (ℕ; _≤_; _≥_); open ℕ; open _≤_
25 import Data.Nat.Properties as ℕ
26 open import Data.Product.Base using (∃₂; _×_; _,_; <_,_>; proj₂; uncurry)
27
28 open import Function.Base
29 open import Function.Bundles using (_⤖_; _⇔_ ; mk⤖; mk⇔)
30 open import Function.Consequences.Propositional using (strictlySurjective⇒surjective)
31 open import Relation.Nullary.Decidable as Dec using (Dec; does; _because_; yes; no; ¬?)
32 open import Relation.Nullary.Negation using (¬_; contradiction)
33 open import Relation.Nullary.Reflects using (invert)
34 open import Relation.Unary as U using (Pred)
35 open import Relation.Binary.Core using (Rel; REL; _⇒_)
36 open import Relation.Binary.Bundles using (Preorder; Poset; DecPoset)
37 open import Relation.Binary.Definitions
38 using (Reflexive; Trans; Antisym; Decidable; Irrelevant; Irreflexive)
39 open import Relation.Binary.Structures
40 using (IsPreorder; IsPartialOrder; IsDecPartialOrder)
41 open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_)
42 import Relation.Binary.Reasoning.Preorder as ≲-Reasoning
43 open import Relation.Binary.Reasoning.Syntax
44
45 private
46 variable
47 a b c d e p q r s t : Level
48 A : Set a
49 B : Set b
50 C : Set c
51 D : Set d
52 x : A
53 y : B
54 as cs xs : List A
55 bs ds ys : List B
56 ass : List (List A)
57 bss : List (List B)
58 m n : ℕ
59
60 ------------------------------------------------------------------------
61 -- Injectivity of constructors
62
63 module _ {R : REL A B r} where
64
65 ∷-injectiveˡ : {px qx : R x y} {pxs qxs : Sublist R xs ys} →
66 (Sublist R (x ∷ xs) (y ∷ ys) ∋ px ∷ pxs) ≡ (qx ∷ qxs) → px ≡ qx
67 ∷-injectiveˡ ≡.refl = ≡.refl
68
69 ∷-injectiveʳ : {px qx : R x y} {pxs qxs : Sublist R xs ys} →
70 (Sublist R (x ∷ xs) (y ∷ ys) ∋ px ∷ pxs) ≡ (qx ∷ qxs) → pxs ≡ qxs
71 ∷-injectiveʳ ≡.refl = ≡.refl
72
73 ∷ʳ-injective : {pxs qxs : Sublist R xs ys} →
74 (Sublist R xs (y ∷ ys) ∋ y ∷ʳ pxs) ≡ (y ∷ʳ qxs) → pxs ≡ qxs
75 ∷ʳ-injective ≡.refl = ≡.refl
76
77 module _ {R : REL A B r} where
78
79 length-mono-≤ : Sublist R as bs → length as ≤ length bs
80 length-mono-≤ [] = z≤n
81 length-mono-≤ (y ∷ʳ rs) = ℕ.m≤n⇒m≤1+n (length-mono-≤ rs)
82 length-mono-≤ (r ∷ rs) = s≤s (length-mono-≤ rs)
83
84 ------------------------------------------------------------------------
85 -- Conversion to and from Pointwise (proto-reflexivity)
86
87 fromPointwise : Pointwise R ⇒ Sublist R
88 fromPointwise [] = []
89 fromPointwise (p ∷ ps) = p ∷ fromPointwise ps
90
91 toPointwise : length as ≡ length bs →
92 Sublist R as bs → Pointwise R as bs
93 toPointwise {bs = []} eq [] = []
94 toPointwise {bs = b ∷ bs} eq (r ∷ rs) = r ∷ toPointwise (ℕ.suc-injective eq) rs
95 toPointwise {bs = b ∷ bs} eq (b ∷ʳ rs) =
96 contradiction (s≤s (length-mono-≤ rs)) (ℕ.<-irrefl eq)
97
98 ------------------------------------------------------------------------
99 -- Various functions' outputs are sublists
100
101 -- These lemmas are generalisations of results of the form `f xs ⊆ xs`.
102 -- (where _⊆_ stands for Sublist R). If R is reflexive then we can
103 -- indeed obtain `f xs ⊆ xs` from `xs ⊆ ys → f xs ⊆ ys`. The other
104 -- direction is only true if R is both reflexive and transitive.
105
106 module _ {R : REL A B r} where
107
108 tail-Sublist : Sublist R as bs →
109 Maybe.All (λ as → Sublist R as bs) (tail as)
110 tail-Sublist [] = nothing
111 tail-Sublist (b ∷ʳ ps) = Maybe.map (b ∷ʳ_) (tail-Sublist ps)
112 tail-Sublist (p ∷ ps) = just (_ ∷ʳ ps)
113
114 take-Sublist : ∀ n → Sublist R as bs → Sublist R (take n as) bs
115 take-Sublist n (y ∷ʳ rs) = y ∷ʳ take-Sublist n rs
116 take-Sublist zero rs = minimum _
117 take-Sublist (suc n) [] = []
118 take-Sublist (suc n) (r ∷ rs) = r ∷ take-Sublist n rs
119
120 drop-Sublist : ∀ n → Sublist R ⇒ (Sublist R ∘′ drop n)
121 drop-Sublist n (y ∷ʳ rs) = y ∷ʳ drop-Sublist n rs
122 drop-Sublist zero rs = rs
123 drop-Sublist (suc n) [] = []
124 drop-Sublist (suc n) (r ∷ rs) = _ ∷ʳ drop-Sublist n rs
125
126 module _ {R : REL A B r} {P : Pred A p} (P? : U.Decidable P) where
127
128 takeWhile-Sublist : Sublist R as bs → Sublist R (takeWhile P? as) bs
129 takeWhile-Sublist [] = []
130 takeWhile-Sublist (y ∷ʳ rs) = y ∷ʳ takeWhile-Sublist rs
131 takeWhile-Sublist {a ∷ as} (r ∷ rs) with does (P? a)
132 ... | true = r ∷ takeWhile-Sublist rs
133 ... | false = minimum _
134
135 dropWhile-Sublist : Sublist R as bs → Sublist R (dropWhile P? as) bs
136 dropWhile-Sublist [] = []
137 dropWhile-Sublist (y ∷ʳ rs) = y ∷ʳ dropWhile-Sublist rs
138 dropWhile-Sublist {a ∷ as} (r ∷ rs) with does (P? a)
139 ... | true = _ ∷ʳ dropWhile-Sublist rs
140 ... | false = r ∷ rs
141
142 filter-Sublist : Sublist R as bs → Sublist R (filter P? as) bs
143 filter-Sublist [] = []
144 filter-Sublist (y ∷ʳ rs) = y ∷ʳ filter-Sublist rs
145 filter-Sublist {a ∷ as} (r ∷ rs) with does (P? a)
146 ... | true = r ∷ filter-Sublist rs
147 ... | false = _ ∷ʳ filter-Sublist rs
148
149 ------------------------------------------------------------------------
150 -- Various functions are increasing wrt _⊆_
151
152 -- We write f⁺ for the proof that `xs ⊆ ys → f xs ⊆ f ys`
153 -- and f⁻ for the one that `f xs ⊆ f ys → xs ⊆ ys`.
154
155 module _ {R : REL A B r} where
156
157 ------------------------------------------------------------------------
158 -- _∷_
159
160 ∷ˡ⁻ : Sublist R (x ∷ xs) ys → Sublist R xs ys
161 ∷ˡ⁻ (y ∷ʳ rs) = y ∷ʳ ∷ˡ⁻ rs
162 ∷ˡ⁻ (r ∷ rs) = _ ∷ʳ rs
163
164 ∷ʳ⁻ : ¬ R x y → Sublist R (x ∷ xs) (y ∷ ys) →
165 Sublist R (x ∷ xs) ys
166 ∷ʳ⁻ ¬r (y ∷ʳ rs) = rs
167 ∷ʳ⁻ ¬r (r ∷ rs) = contradiction r ¬r
168
169 ∷⁻ : Sublist R (x ∷ xs) (y ∷ ys) → Sublist R xs ys
170 ∷⁻ (y ∷ʳ rs) = ∷ˡ⁻ rs
171 ∷⁻ (x ∷ rs) = rs
172
173 module _ {R : REL C D r} where
174
175 ------------------------------------------------------------------------
176 -- map
177
178 map⁺ : (f : A → C) (g : B → D) →
179 Sublist (λ a b → R (f a) (g b)) as bs →
180 Sublist R (List.map f as) (List.map g bs)
181 map⁺ f g [] = []
182 map⁺ f g (y ∷ʳ rs) = g y ∷ʳ map⁺ f g rs
183 map⁺ f g (r ∷ rs) = r ∷ map⁺ f g rs
184
185 map⁻ : (f : A → C) (g : B → D) →
186 Sublist R (List.map f as) (List.map g bs) →
187 Sublist (λ a b → R (f a) (g b)) as bs
188 map⁻ {as = []} {bs} f g rs = minimum _
189 map⁻ {as = a ∷ as} {b ∷ bs} f g (_ ∷ʳ rs) = b ∷ʳ map⁻ f g rs
190 map⁻ {as = a ∷ as} {b ∷ bs} f g (r ∷ rs) = r ∷ map⁻ f g rs
191
192
193 module _ {R : REL A B r} where
194
195 ------------------------------------------------------------------------
196 -- _++_
197
198 ++⁺ : Sublist R as bs → Sublist R cs ds →
199 Sublist R (as ++ cs) (bs ++ ds)
200 ++⁺ [] cds = cds
201 ++⁺ (y ∷ʳ abs) cds = y ∷ʳ ++⁺ abs cds
202 ++⁺ (ab ∷ abs) cds = ab ∷ ++⁺ abs cds
203
204 ++⁻ : length as ≡ length bs →
205 Sublist R (as ++ cs) (bs ++ ds) → Sublist R cs ds
206 ++⁻ {as = []} {[]} eq rs = rs
207 ++⁻ {as = a ∷ as} {b ∷ bs} eq rs = ++⁻ (ℕ.suc-injective eq) (∷⁻ rs)
208
209 ++ˡ : (cs : List B) → Sublist R as bs → Sublist R as (cs ++ bs)
210 ++ˡ zs = ++⁺ (minimum zs)
211
212 ++ʳ : (cs : List B) → Sublist R as bs → Sublist R as (bs ++ cs)
213 ++ʳ cs [] = minimum cs
214 ++ʳ cs (y ∷ʳ rs) = y ∷ʳ ++ʳ cs rs
215 ++ʳ cs (r ∷ rs) = r ∷ ++ʳ cs rs
216
217
218 ------------------------------------------------------------------------
219 -- concat
220
221 concat⁺ : Sublist (Sublist R) ass bss →
222 Sublist R (concat ass) (concat bss)
223 concat⁺ [] = []
224 concat⁺ (y ∷ʳ rss) = ++ˡ y (concat⁺ rss)
225 concat⁺ (rs ∷ rss) = ++⁺ rs (concat⁺ rss)
226
227 ------------------------------------------------------------------------
228 -- take / drop
229
230 take⁺ : m ≤ n → Pointwise R as bs →
231 Sublist R (take m as) (take n bs)
232 take⁺ z≤n ps = minimum _
233 take⁺ (s≤s m≤n) [] = []
234 take⁺ (s≤s m≤n) (p ∷ ps) = p ∷ take⁺ m≤n ps
235
236 drop⁺ : m ≥ n → Sublist R as bs →
237 Sublist R (drop m as) (drop n bs)
238 drop⁺ (z≤n {m}) rs = drop-Sublist m rs
239 drop⁺ (s≤s m≥n) [] = []
240 drop⁺ (s≤s m≥n) (y ∷ʳ rs) = drop⁺ (ℕ.m≤n⇒m≤1+n m≥n) rs
241 drop⁺ (s≤s m≥n) (r ∷ rs) = drop⁺ m≥n rs
242
243 drop⁺-≥ : m ≥ n → Pointwise R as bs →
244 Sublist R (drop m as) (drop n bs)
245 drop⁺-≥ m≥n pw = drop⁺ m≥n (fromPointwise pw)
246
247 drop⁺-⊆ : ∀ m → Sublist R as bs →
248 Sublist R (drop m as) (drop m bs)
249 drop⁺-⊆ m = drop⁺ (ℕ.≤-refl {m})
250
251 module _ {R : REL A B r} {P : Pred A p} {Q : Pred B q}
252 (P? : U.Decidable P) (Q? : U.Decidable Q) where
253
254 ⊆-takeWhile-Sublist : (∀ {a b} → R a b → P a → Q b) →
255 Pointwise R as bs →
256 Sublist R (takeWhile P? as) (takeWhile Q? bs)
257 ⊆-takeWhile-Sublist rp⇒q [] = []
258 ⊆-takeWhile-Sublist {a ∷ as} {b ∷ bs} rp⇒q (p ∷ ps) with P? a | Q? b
259 ... | false because _ | _ = minimum _
260 ... | true because _ | true because _ = p ∷ ⊆-takeWhile-Sublist rp⇒q ps
261 ... | yes pa | no ¬qb = contradiction (rp⇒q p pa) ¬qb
262
263 ⊇-dropWhile-Sublist : (∀ {a b} → R a b → Q b → P a) →
264 Pointwise R as bs →
265 Sublist R (dropWhile P? as) (dropWhile Q? bs)
266 ⊇-dropWhile-Sublist rq⇒p [] = []
267 ⊇-dropWhile-Sublist {a ∷ as} {b ∷ bs} rq⇒p (p ∷ ps) with P? a | Q? b
268 ... | true because _ | true because _ = ⊇-dropWhile-Sublist rq⇒p ps
269 ... | true because _ | false because _ =
270 b ∷ʳ dropWhile-Sublist P? (fromPointwise ps)
271 ... | false because _ | false because _ = p ∷ fromPointwise ps
272 ... | no ¬pa | yes qb = contradiction (rq⇒p p qb) ¬pa
273
274 ⊆-filter-Sublist : (∀ {a b} → R a b → P a → Q b) →
275 Sublist R as bs → Sublist R (filter P? as) (filter Q? bs)
276 ⊆-filter-Sublist rp⇒q [] = []
277 ⊆-filter-Sublist rp⇒q (y ∷ʳ rs) with does (Q? y)
278 ... | true = y ∷ʳ ⊆-filter-Sublist rp⇒q rs
279 ... | false = ⊆-filter-Sublist rp⇒q rs
280 ⊆-filter-Sublist {a ∷ as} {b ∷ bs} rp⇒q (r ∷ rs) with P? a | Q? b
281 ... | true because _ | true because _ = r ∷ ⊆-filter-Sublist rp⇒q rs
282 ... | false because _ | true because _ = _ ∷ʳ ⊆-filter-Sublist rp⇒q rs
283 ... | false because _ | false because _ = ⊆-filter-Sublist rp⇒q rs
284 ... | yes pa | no ¬qb = contradiction (rp⇒q r pa) ¬qb
285
286 module _ {R : Rel A r} {P : Pred A p} (P? : U.Decidable P) where
287
288 takeWhile-filter : Pointwise R as as →
289 Sublist R (takeWhile P? as) (filter P? as)
290 takeWhile-filter [] = []
291 takeWhile-filter {a ∷ as} (p ∷ ps) with does (P? a)
292 ... | true = p ∷ takeWhile-filter ps
293 ... | false = minimum _
294
295 filter-dropWhile : Pointwise R as as →
296 Sublist R (filter P? as) (dropWhile (¬? ∘ P?) as)
297 filter-dropWhile [] = []
298 filter-dropWhile {a ∷ as} (p ∷ ps) with does (P? a)
299 ... | true = p ∷ filter-Sublist P? (fromPointwise ps)
300 ... | false = filter-dropWhile ps
301
302 ------------------------------------------------------------------------
303 -- reverse
304
305 module _ {R : REL A B r} where
306
307 reverseAcc⁺ : Sublist R as bs → Sublist R cs ds →
308 Sublist R (reverseAcc cs as) (reverseAcc ds bs)
309 reverseAcc⁺ [] cds = cds
310 reverseAcc⁺ (y ∷ʳ abs) cds = reverseAcc⁺ abs (y ∷ʳ cds)
311 reverseAcc⁺ (r ∷ abs) cds = reverseAcc⁺ abs (r ∷ cds)
312
313 ʳ++⁺ : Sublist R as bs → Sublist R cs ds →
314 Sublist R (as ʳ++ cs) (bs ʳ++ ds)
315 ʳ++⁺ = reverseAcc⁺
316
317 reverse⁺ : Sublist R as bs → Sublist R (reverse as) (reverse bs)
318 reverse⁺ rs = reverseAcc⁺ rs []
319
320 reverse⁻ : Sublist R (reverse as) (reverse bs) → Sublist R as bs
321 reverse⁻ {as = as} {bs = bs} p = cast (reverse⁺ p) where
322 cast = ≡.subst₂ (Sublist R) (List.reverse-involutive as) (List.reverse-involutive bs)
323
324 ------------------------------------------------------------------------
325 -- Inversion lemmas
326
327 module _ {R : REL A B r} where
328
329 ∷⁻¹ : R x y → Sublist R xs ys ⇔ Sublist R (x ∷ xs) (y ∷ ys)
330 ∷⁻¹ r = mk⇔ (r ∷_) ∷⁻
331
332 ∷ʳ⁻¹ : ¬ R x y → Sublist R (x ∷ xs) ys ⇔ Sublist R (x ∷ xs) (y ∷ ys)
333 ∷ʳ⁻¹ ¬r = mk⇔ (_ ∷ʳ_) (∷ʳ⁻ ¬r)
334
335 ------------------------------------------------------------------------
336 -- Empty special case
337
338 module _ {R : REL A B r} where
339
340 Sublist-[]-universal : U.Universal (Sublist R [])
341 Sublist-[]-universal [] = []
342 Sublist-[]-universal (_ ∷ _) = _ ∷ʳ Sublist-[]-universal _
343
344 Sublist-[]-irrelevant : U.Irrelevant (Sublist R [])
345 Sublist-[]-irrelevant [] [] = ≡.refl
346 Sublist-[]-irrelevant (y ∷ʳ p) (.y ∷ʳ q) = ≡.cong (y ∷ʳ_) (Sublist-[]-irrelevant p q)
347
348 ------------------------------------------------------------------------
349 -- (to/from)Any is a bijection
350
351 toAny-injective :{p q : Sublist R [ x ] xs} → toAny p ≡ toAny q → p ≡ q
352 toAny-injective {p = y ∷ʳ p} {y ∷ʳ q} =
353 ≡.cong (y ∷ʳ_) ∘′ toAny-injective ∘′ there-injective
354 toAny-injective {p = _ ∷ p} {_ ∷ q} =
355 ≡.cong₂ (flip _∷_) (Sublist-[]-irrelevant p q) ∘′ here-injective
356
357 fromAny-injective : {p q : Any (R x) xs} →
358 fromAny {R = R} p ≡ fromAny q → p ≡ q
359 fromAny-injective {p = here px} {here qx} = ≡.cong here ∘′ ∷-injectiveˡ
360 fromAny-injective {p = there p} {there q} =
361 ≡.cong there ∘′ fromAny-injective ∘′ ∷ʳ-injective
362
363 toAny∘fromAny≗id : (p : Any (R x) xs) → toAny (fromAny {R = R} p) ≡ p
364 toAny∘fromAny≗id (here px) = ≡.refl
365 toAny∘fromAny≗id (there p) = ≡.cong there (toAny∘fromAny≗id p)
366
367 Sublist-[x]-bijection : (Sublist R [ x ] xs) ⤖ (Any (R x) xs)
368 Sublist-[x]-bijection = mk⤖ (toAny-injective , strictlySurjective⇒surjective < fromAny , toAny∘fromAny≗id >)
369
370 ------------------------------------------------------------------------
371 -- Relational properties
372
373 module Reflexivity
374 {R : Rel A r}
375 (R-refl : Reflexive R) where
376
377 reflexive : _≡_ ⇒ Sublist R
378 reflexive ≡.refl = fromPointwise (Pw.refl R-refl)
379
380 refl : Reflexive (Sublist R)
381 refl = reflexive ≡.refl
382
383 open Reflexivity public
384
385 module Transitivity
386 {R : REL A B r} {S : REL B C s} {T : REL A C t}
387 (rs⇒t : Trans R S T) where
388
389 trans : Trans (Sublist R) (Sublist S) (Sublist T)
390 trans rs (y ∷ʳ ss) = y ∷ʳ trans rs ss
391 trans (y ∷ʳ rs) (s ∷ ss) = _ ∷ʳ trans rs ss
392 trans (r ∷ rs) (s ∷ ss) = rs⇒t r s ∷ trans rs ss
393 trans [] [] = []
394
395 open Transitivity public
396
397 module Antisymmetry
398 {R : REL A B r} {S : REL B A s} {E : REL A B e}
399 (rs⇒e : Antisym R S E) where
400
401 open ℕ.≤-Reasoning
402
403 antisym : Antisym (Sublist R) (Sublist S) (Pointwise E)
404 antisym [] [] = []
405 antisym (r ∷ rs) (s ∷ ss) = rs⇒e r s ∷ antisym rs ss
406 -- impossible cases
407 antisym (_∷ʳ_ {xs} {ys₁} y rs) (_∷ʳ_ {ys₂} {zs} z ss) =
408 contradiction (begin
409 length (y ∷ ys₁) ≤⟨ length-mono-≤ ss ⟩
410 length zs ≤⟨ ℕ.n≤1+n (length zs) ⟩
411 length (z ∷ zs) ≤⟨ length-mono-≤ rs ⟩
412 length ys₁ ∎) $ ℕ.<-irrefl ≡.refl
413 antisym (_∷ʳ_ {xs} {ys₁} y rs) (_∷_ {y} {ys₂} {z} {zs} s ss) =
414 contradiction (begin
415 length (z ∷ zs) ≤⟨ length-mono-≤ rs ⟩
416 length ys₁ ≤⟨ length-mono-≤ ss ⟩
417 length zs ∎) $ ℕ.<-irrefl ≡.refl
418 antisym (_∷_ {x} {xs} {y} {ys₁} r rs) (_∷ʳ_ {ys₂} {zs} z ss) =
419 contradiction (begin
420 length (y ∷ ys₁) ≤⟨ length-mono-≤ ss ⟩
421 length xs ≤⟨ length-mono-≤ rs ⟩
422 length ys₁ ∎) $ ℕ.<-irrefl ≡.refl
423
424 open Antisymmetry public
425
426 module _ {R : REL A B r} (R? : Decidable R) where
427
428 sublist? : Decidable (Sublist R)
429 sublist? [] ys = yes (minimum ys)
430 sublist? (x ∷ xs) [] = no λ ()
431 sublist? (x ∷ xs) (y ∷ ys) with R? x y
432 ... | true because [r] =
433 Dec.map (∷⁻¹ (invert [r])) (sublist? xs ys)
434 ... | false because [¬r] =
435 Dec.map (∷ʳ⁻¹ (invert [¬r])) (sublist? (x ∷ xs) ys)
436
437 module _ {E : Rel A e} {R : Rel A r} where
438
439 isPreorder : IsPreorder E R → IsPreorder (Pointwise E) (Sublist R)
440 isPreorder ER-isPreorder = record
441 { isEquivalence = Pw.isEquivalence ER.isEquivalence
442 ; reflexive = fromPointwise ∘ Pw.map ER.reflexive
443 ; trans = trans ER.trans
444 } where module ER = IsPreorder ER-isPreorder
445
446 isPartialOrder : IsPartialOrder E R → IsPartialOrder (Pointwise E) (Sublist R)
447 isPartialOrder ER-isPartialOrder = record
448 { isPreorder = isPreorder ER.isPreorder
449 ; antisym = antisym ER.antisym
450 } where module ER = IsPartialOrder ER-isPartialOrder
451
452 isDecPartialOrder : IsDecPartialOrder E R →
453 IsDecPartialOrder (Pointwise E) (Sublist R)
454 isDecPartialOrder ER-isDecPartialOrder = record
455 { isPartialOrder = isPartialOrder ER.isPartialOrder
456 ; _≟_ = Pw.decidable ER._≟_
457 ; _≤?_ = sublist? ER._≤?_
458 } where module ER = IsDecPartialOrder ER-isDecPartialOrder
459
460 preorder : Preorder a e r → Preorder _ _ _
461 preorder ER-preorder = record
462 { isPreorder = isPreorder ER.isPreorder
463 } where module ER = Preorder ER-preorder
464
465 poset : Poset a e r → Poset _ _ _
466 poset ER-poset = record
467 { isPartialOrder = isPartialOrder ER.isPartialOrder
468 } where module ER = Poset ER-poset
469
470 decPoset : DecPoset a e r → DecPoset _ _ _
471 decPoset ER-poset = record
472 { isDecPartialOrder = isDecPartialOrder ER.isDecPartialOrder
473 } where module ER = DecPoset ER-poset
474
475 ------------------------------------------------------------------------
476 -- Reasoning over sublists
477 ------------------------------------------------------------------------
478
479 module ⊆-Reasoning (≲ : Preorder a e r) where
480
481 open Preorder ≲ using (module Eq)
482
483 open ≲-Reasoning (preorder ≲) public
484 renaming (≲-go to ⊆-go; ≈-go to ≋-go)
485
486 open ⊆-syntax _IsRelatedTo_ _IsRelatedTo_ ⊆-go public
487 open ≋-syntax _IsRelatedTo_ _IsRelatedTo_ ≋-go (Pw.symmetric Eq.sym) public
488
489 ------------------------------------------------------------------------
490 -- Properties of disjoint sublists
491
492 module Disjointness {a b r} {A : Set a} {B : Set b} {R : REL A B r} where
493
494 private
495 infix 4 _⊆_
496 _⊆_ = Sublist R
497
498 -- Forgetting the union
499
500 DisjointUnion→Disjoint : ∀ {xs ys zs us} {τ₁ : xs ⊆ zs} {τ₂ : ys ⊆ zs} {τ : us ⊆ zs} →
501 DisjointUnion τ₁ τ₂ τ → Disjoint τ₁ τ₂
502 DisjointUnion→Disjoint [] = []
503 DisjointUnion→Disjoint (y ∷ₙ u) = y ∷ₙ DisjointUnion→Disjoint u
504 DisjointUnion→Disjoint (x≈y ∷ₗ u) = x≈y ∷ₗ DisjointUnion→Disjoint u
505 DisjointUnion→Disjoint (x≈y ∷ᵣ u) = x≈y ∷ᵣ DisjointUnion→Disjoint u
506
507 -- Reconstructing the union
508
509 Disjoint→DisjointUnion : ∀ {xs ys zs} {τ₁ : xs ⊆ zs} {τ₂ : ys ⊆ zs} →
510 Disjoint τ₁ τ₂ → ∃₂ λ us (τ : us ⊆ zs) → DisjointUnion τ₁ τ₂ τ
511 Disjoint→DisjointUnion [] = _ , _ , []
512 Disjoint→DisjointUnion (y ∷ₙ u) = _ , _ , y ∷ₙ proj₂ (proj₂ (Disjoint→DisjointUnion u))
513 Disjoint→DisjointUnion (x≈y ∷ₗ u) = _ , _ , x≈y ∷ₗ proj₂ (proj₂ (Disjoint→DisjointUnion u))
514 Disjoint→DisjointUnion (x≈y ∷ᵣ u) = _ , _ , x≈y ∷ᵣ proj₂ (proj₂ (Disjoint→DisjointUnion u))
515
516 -- Disjoint is decidable
517
518 ⊆-disjoint? : ∀ {xs ys zs} (τ₁ : xs ⊆ zs) (τ₂ : ys ⊆ zs) → Dec (Disjoint τ₁ τ₂)
519 ⊆-disjoint? [] [] = yes []
520 -- Present in both sublists: not disjoint.
521 ⊆-disjoint? (x≈z ∷ τ₁) (y≈z ∷ τ₂) = no λ()
522 -- Present in either sublist: ok.
523 ⊆-disjoint? (y ∷ʳ τ₁) (x≈y ∷ τ₂) =
524 Dec.map′ (x≈y ∷ᵣ_) (λ{ (_ ∷ᵣ d) → d }) (⊆-disjoint? τ₁ τ₂)
525 ⊆-disjoint? (x≈y ∷ τ₁) (y ∷ʳ τ₂) =
526 Dec.map′ (x≈y ∷ₗ_) (λ{ (_ ∷ₗ d) → d }) (⊆-disjoint? τ₁ τ₂)
527 -- Present in neither sublist: ok.
528 ⊆-disjoint? (y ∷ʳ τ₁) (.y ∷ʳ τ₂) =
529 Dec.map′ (y ∷ₙ_) (λ{ (_ ∷ₙ d) → d }) (⊆-disjoint? τ₁ τ₂)
530
531 -- Disjoint is proof-irrelevant
532
533 Disjoint-irrelevant : ∀{xs ys zs} → Irrelevant (Disjoint {R = R} {xs} {ys} {zs})
534 Disjoint-irrelevant [] [] = ≡.refl
535 Disjoint-irrelevant (y ∷ₙ d₁) (.y ∷ₙ d₂) = ≡.cong (y ∷ₙ_) (Disjoint-irrelevant d₁ d₂)
536 Disjoint-irrelevant (x≈y ∷ₗ d₁) (.x≈y ∷ₗ d₂) = ≡.cong (x≈y ∷ₗ_) (Disjoint-irrelevant d₁ d₂)
537 Disjoint-irrelevant (x≈y ∷ᵣ d₁) (.x≈y ∷ᵣ d₂) = ≡.cong (x≈y ∷ᵣ_) (Disjoint-irrelevant d₁ d₂)
538
539 -- Note: DisjointUnion is not proof-irrelevant unless the underlying relation R is.
540 -- The proof is not entirely trivial, thus, we leave it for future work:
541 --
542 -- DisjointUnion-irrelevant : Irrelevant R →
543 -- ∀{xs ys us zs} {τ : us ⊆ zs} →
544 -- Irrelevant (λ (τ₁ : xs ⊆ zs) (τ₂ : ys ⊆ zs) → DisjointUnion τ₁ τ₂ τ)
545
546 -- Irreflexivity
547
548 Disjoint-irrefl′ : ∀{xs ys} {τ : xs ⊆ ys} → Disjoint τ τ → Null xs
549 Disjoint-irrefl′ [] = []
550 Disjoint-irrefl′ (y ∷ₙ d) = Disjoint-irrefl′ d
551
552 Disjoint-irrefl : ∀{x xs ys} → Irreflexive {A = x ∷ xs ⊆ ys } _≡_ Disjoint
553 Disjoint-irrefl ≡.refl x with Disjoint-irrefl′ x
554 ... | () ∷ _
555
556 -- Symmetry
557
558 DisjointUnion-sym : ∀ {xs ys xys} {zs} {τ₁ : xs ⊆ zs} {τ₂ : ys ⊆ zs} {τ : xys ⊆ zs} →
559 DisjointUnion τ₁ τ₂ τ → DisjointUnion τ₂ τ₁ τ
560 DisjointUnion-sym [] = []
561 DisjointUnion-sym (y ∷ₙ d) = y ∷ₙ DisjointUnion-sym d
562 DisjointUnion-sym (x≈y ∷ₗ d) = x≈y ∷ᵣ DisjointUnion-sym d
563 DisjointUnion-sym (x≈y ∷ᵣ d) = x≈y ∷ₗ DisjointUnion-sym d
564
565 Disjoint-sym : ∀ {xs ys} {zs} {τ₁ : xs ⊆ zs} {τ₂ : ys ⊆ zs} →
566 Disjoint τ₁ τ₂ → Disjoint τ₂ τ₁
567 Disjoint-sym [] = []
568 Disjoint-sym (y ∷ₙ d) = y ∷ₙ Disjoint-sym d
569 Disjoint-sym (x≈y ∷ₗ d) = x≈y ∷ᵣ Disjoint-sym d
570 Disjoint-sym (x≈y ∷ᵣ d) = x≈y ∷ₗ Disjoint-sym d
571
572 -- Empty sublist
573
574 DisjointUnion-[]ˡ : ∀{xs ys} {ε : [] ⊆ ys} {τ : xs ⊆ ys} → DisjointUnion ε τ τ
575 DisjointUnion-[]ˡ {ε = []} {τ = []} = []
576 DisjointUnion-[]ˡ {ε = y ∷ʳ ε} {τ = y ∷ʳ τ} = y ∷ₙ DisjointUnion-[]ˡ
577 DisjointUnion-[]ˡ {ε = y ∷ʳ ε} {τ = x≈y ∷ τ} = x≈y ∷ᵣ DisjointUnion-[]ˡ
578
579 DisjointUnion-[]ʳ : ∀{xs ys} {ε : [] ⊆ ys} {τ : xs ⊆ ys} → DisjointUnion τ ε τ
580 DisjointUnion-[]ʳ {ε = []} {τ = []} = []
581 DisjointUnion-[]ʳ {ε = y ∷ʳ ε} {τ = y ∷ʳ τ} = y ∷ₙ DisjointUnion-[]ʳ
582 DisjointUnion-[]ʳ {ε = y ∷ʳ ε} {τ = x≈y ∷ τ} = x≈y ∷ₗ DisjointUnion-[]ʳ
583
584 -- A sublist τ : x∷xs ⊆ ys can be split into two disjoint sublists
585 -- [x] ⊆ ys (canonical choice) and (∷ˡ⁻ τ) : xs ⊆ ys.
586
587 DisjointUnion-fromAny∘toAny-∷ˡ⁻ : ∀ {x xs ys} (τ : (x ∷ xs) ⊆ ys) → DisjointUnion (fromAny (toAny τ)) (∷ˡ⁻ τ) τ
588 DisjointUnion-fromAny∘toAny-∷ˡ⁻ (y ∷ʳ τ) = y ∷ₙ DisjointUnion-fromAny∘toAny-∷ˡ⁻ τ
589 DisjointUnion-fromAny∘toAny-∷ˡ⁻ (xRy ∷ τ) = xRy ∷ₗ DisjointUnion-[]ˡ
590
591 -- Disjoint union of three mutually disjoint lists.
592 --
593 -- τᵢⱼ denotes the disjoint union of τᵢ and τⱼ: DisjointUnion τᵢ τⱼ τᵢⱼ
594
595 record DisjointUnion³
596 {xs ys zs ts} (τ₁ : xs ⊆ ts) (τ₂ : ys ⊆ ts) (τ₃ : zs ⊆ ts)
597 {xys xzs yzs} (τ₁₂ : xys ⊆ ts) (τ₁₃ : xzs ⊆ ts) (τ₂₃ : yzs ⊆ ts) : Set (a ⊔ b ⊔ r) where
598 field
599 {union³} : List A
600 sub³ : union³ ⊆ ts
601 join₁ : DisjointUnion τ₁ τ₂₃ sub³
602 join₂ : DisjointUnion τ₂ τ₁₃ sub³
603 join₃ : DisjointUnion τ₃ τ₁₂ sub³
604
605 infixr 5 _∷ʳ-DisjointUnion³_ _∷₁-DisjointUnion³_ _∷₂-DisjointUnion³_ _∷₃-DisjointUnion³_
606
607 -- Weakening the target list ts of a disjoint union.
608
609 _∷ʳ-DisjointUnion³_ :
610 ∀ {xs ys zs ts} {τ₁ : xs ⊆ ts} {τ₂ : ys ⊆ ts} {τ₃ : zs ⊆ ts} →
611 ∀ {xys xzs yzs} {τ₁₂ : xys ⊆ ts} {τ₁₃ : xzs ⊆ ts} {τ₂₃ : yzs ⊆ ts} →
612 ∀ y →
613 DisjointUnion³ τ₁ τ₂ τ₃ τ₁₂ τ₁₃ τ₂₃ →
614 DisjointUnion³ (y ∷ʳ τ₁) (y ∷ʳ τ₂) (y ∷ʳ τ₃) (y ∷ʳ τ₁₂) (y ∷ʳ τ₁₃) (y ∷ʳ τ₂₃)
615 y ∷ʳ-DisjointUnion³ record{ sub³ = σ ; join₁ = d₁ ; join₂ = d₂ ; join₃ = d₃ } = record
616 { sub³ = y ∷ʳ σ
617 ; join₁ = y ∷ₙ d₁
618 ; join₂ = y ∷ₙ d₂
619 ; join₃ = y ∷ₙ d₃
620 }
621
622 -- Adding an element to the first list.
623
624 _∷₁-DisjointUnion³_ :
625 ∀ {xs ys zs ts} {τ₁ : xs ⊆ ts} {τ₂ : ys ⊆ ts} {τ₃ : zs ⊆ ts} →
626 ∀ {xys xzs yzs} {τ₁₂ : xys ⊆ ts} {τ₁₃ : xzs ⊆ ts} {τ₂₃ : yzs ⊆ ts} →
627 ∀ {x y} (xRy : R x y) →
628 DisjointUnion³ τ₁ τ₂ τ₃ τ₁₂ τ₁₃ τ₂₃ →
629 DisjointUnion³ (xRy ∷ τ₁) (y ∷ʳ τ₂) (y ∷ʳ τ₃) (xRy ∷ τ₁₂) (xRy ∷ τ₁₃) (y ∷ʳ τ₂₃)
630 xRy ∷₁-DisjointUnion³ record{ sub³ = σ ; join₁ = d₁ ; join₂ = d₂ ; join₃ = d₃ } = record
631 { sub³ = xRy ∷ σ
632 ; join₁ = xRy ∷ₗ d₁
633 ; join₂ = xRy ∷ᵣ d₂
634 ; join₃ = xRy ∷ᵣ d₃
635 }
636
637 -- Adding an element to the second list.
638
639 _∷₂-DisjointUnion³_ :
640 ∀ {xs ys zs ts} {τ₁ : xs ⊆ ts} {τ₂ : ys ⊆ ts} {τ₃ : zs ⊆ ts} →
641 ∀ {xys xzs yzs} {τ₁₂ : xys ⊆ ts} {τ₁₃ : xzs ⊆ ts} {τ₂₃ : yzs ⊆ ts} →
642 ∀ {x y} (xRy : R x y) →
643 DisjointUnion³ τ₁ τ₂ τ₃ τ₁₂ τ₁₃ τ₂₃ →
644 DisjointUnion³ (y ∷ʳ τ₁) (xRy ∷ τ₂) (y ∷ʳ τ₃) (xRy ∷ τ₁₂) (y ∷ʳ τ₁₃) (xRy ∷ τ₂₃)
645 xRy ∷₂-DisjointUnion³ record{ sub³ = σ ; join₁ = d₁ ; join₂ = d₂ ; join₃ = d₃ } = record
646 { sub³ = xRy ∷ σ
647 ; join₁ = xRy ∷ᵣ d₁
648 ; join₂ = xRy ∷ₗ d₂
649 ; join₃ = xRy ∷ᵣ d₃
650 }
651
652 -- Adding an element to the third list.
653
654 _∷₃-DisjointUnion³_ :
655 ∀ {xs ys zs ts} {τ₁ : xs ⊆ ts} {τ₂ : ys ⊆ ts} {τ₃ : zs ⊆ ts} →
656 ∀ {xys xzs yzs} {τ₁₂ : xys ⊆ ts} {τ₁₃ : xzs ⊆ ts} {τ₂₃ : yzs ⊆ ts} →
657 ∀ {x y} (xRy : R x y) →
658 DisjointUnion³ τ₁ τ₂ τ₃ τ₁₂ τ₁₃ τ₂₃ →
659 DisjointUnion³ (y ∷ʳ τ₁) (y ∷ʳ τ₂) (xRy ∷ τ₃) (y ∷ʳ τ₁₂) (xRy ∷ τ₁₃) (xRy ∷ τ₂₃)
660 xRy ∷₃-DisjointUnion³ record{ sub³ = σ ; join₁ = d₁ ; join₂ = d₂ ; join₃ = d₃ } = record
661 { sub³ = xRy ∷ σ
662 ; join₁ = xRy ∷ᵣ d₁
663 ; join₂ = xRy ∷ᵣ d₂
664 ; join₃ = xRy ∷ₗ d₃
665 }
666
667 -- Computing the disjoint union of three disjoint lists.
668
669 disjointUnion³ : ∀{xs ys zs ts} {τ₁ : xs ⊆ ts} {τ₂ : ys ⊆ ts} {τ₃ : zs ⊆ ts}
670 {xys xzs yzs} {τ₁₂ : xys ⊆ ts} {τ₁₃ : xzs ⊆ ts} {τ₂₃ : yzs ⊆ ts} →
671 DisjointUnion τ₁ τ₂ τ₁₂ →
672 DisjointUnion τ₁ τ₃ τ₁₃ →
673 DisjointUnion τ₂ τ₃ τ₂₃ →
674 DisjointUnion³ τ₁ τ₂ τ₃ τ₁₂ τ₁₃ τ₂₃
675 disjointUnion³ [] [] [] = record { sub³ = [] ; join₁ = [] ; join₂ = [] ; join₃ = [] }
676 disjointUnion³ (y ∷ₙ d₁₂) (.y ∷ₙ d₁₃) (.y ∷ₙ d₂₃) = y ∷ʳ-DisjointUnion³ disjointUnion³ d₁₂ d₁₃ d₂₃
677 disjointUnion³ (y ∷ₙ d₁₂) (xRy ∷ᵣ d₁₃) (.xRy ∷ᵣ d₂₃) = xRy ∷₃-DisjointUnion³ disjointUnion³ d₁₂ d₁₃ d₂₃
678 disjointUnion³ (xRy ∷ᵣ d₁₂) (y ∷ₙ d₁₃) (.xRy ∷ₗ d₂₃) = xRy ∷₂-DisjointUnion³ disjointUnion³ d₁₂ d₁₃ d₂₃
679 disjointUnion³ (xRy ∷ₗ d₁₂) (.xRy ∷ₗ d₁₃) (y ∷ₙ d₂₃) = xRy ∷₁-DisjointUnion³ disjointUnion³ d₁₂ d₁₃ d₂₃
680 disjointUnion³ (xRy ∷ᵣ d₁₂) (xRy′ ∷ᵣ d₁₃) ()
681
682 -- If a sublist τ is disjoint to two lists σ₁ and σ₂,
683 -- then also to their disjoint union σ.
684
685 disjoint⇒disjoint-to-union : ∀{xs ys zs yzs ts}
686 {τ : xs ⊆ ts} {σ₁ : ys ⊆ ts} {σ₂ : zs ⊆ ts} {σ : yzs ⊆ ts} →
687 Disjoint τ σ₁ → Disjoint τ σ₂ → DisjointUnion σ₁ σ₂ σ → Disjoint τ σ
688 disjoint⇒disjoint-to-union d₁ d₂ u = let
689 _ , _ , u₁ = Disjoint→DisjointUnion d₁
690 _ , _ , u₂ = Disjoint→DisjointUnion d₂
691 in DisjointUnion→Disjoint (DisjointUnion³.join₁ (disjointUnion³ u₁ u₂ u))
692
693 open Disjointness public
694
695 -- Monotonicity of disjointness.
696
697 module DisjointnessMonotonicity
698 {R : REL A B r} {S : REL B C s} {T : REL A C t}
699 (rs⇒t : Trans R S T) where
700
701 -- We can enlarge and convert the target list of a disjoint union.
702
703 weakenDisjointUnion : ∀ {xs ys xys zs ws}
704 {τ₁ : Sublist R xs zs}
705 {τ₂ : Sublist R ys zs}
706 {τ : Sublist R xys zs} (σ : Sublist S zs ws) →
707 DisjointUnion τ₁ τ₂ τ →
708 DisjointUnion (trans rs⇒t τ₁ σ) (trans rs⇒t τ₂ σ) (trans rs⇒t τ σ)
709 weakenDisjointUnion [] [] = []
710 weakenDisjointUnion (w ∷ʳ σ) d = w ∷ₙ weakenDisjointUnion σ d
711 weakenDisjointUnion (_ ∷ σ) (y ∷ₙ d) = _ ∷ₙ weakenDisjointUnion σ d
712 weakenDisjointUnion (zSw ∷ σ) (xRz ∷ₗ d) = rs⇒t xRz zSw ∷ₗ weakenDisjointUnion σ d
713 weakenDisjointUnion (zSw ∷ σ) (yRz ∷ᵣ d) = rs⇒t yRz zSw ∷ᵣ weakenDisjointUnion σ d
714
715 weakenDisjoint : ∀ {xs ys zs ws}
716 {τ₁ : Sublist R xs zs}
717 {τ₂ : Sublist R ys zs} (σ : Sublist S zs ws) →
718 Disjoint τ₁ τ₂ →
719 Disjoint (trans rs⇒t τ₁ σ) (trans rs⇒t τ₂ σ)
720 weakenDisjoint [] [] = []
721 weakenDisjoint (w ∷ʳ σ) d = w ∷ₙ weakenDisjoint σ d
722 weakenDisjoint (_ ∷ σ) (y ∷ₙ d) = _ ∷ₙ weakenDisjoint σ d
723 weakenDisjoint (zSw ∷ σ) (xRz ∷ₗ d) = rs⇒t xRz zSw ∷ₗ weakenDisjoint σ d
724 weakenDisjoint (zSw ∷ σ) (yRz ∷ᵣ d) = rs⇒t yRz zSw ∷ᵣ weakenDisjoint σ d
725
726 -- Lists remain disjoint when elements are removed.
727
728 shrinkDisjoint : ∀ {us vs xs ys zs}
729 (σ₁ : Sublist R us xs) {τ₁ : Sublist S xs zs}
730 (σ₂ : Sublist R vs ys) {τ₂ : Sublist S ys zs} →
731 Disjoint τ₁ τ₂ →
732 Disjoint (trans rs⇒t σ₁ τ₁) (trans rs⇒t σ₂ τ₂)
733 shrinkDisjoint σ₁ σ₂ (y ∷ₙ d) = y ∷ₙ shrinkDisjoint σ₁ σ₂ d
734 shrinkDisjoint (x ∷ʳ σ₁) σ₂ (xSz ∷ₗ d) = _ ∷ₙ shrinkDisjoint σ₁ σ₂ d
735 shrinkDisjoint (uRx ∷ σ₁) σ₂ (xSz ∷ₗ d) = rs⇒t uRx xSz ∷ₗ shrinkDisjoint σ₁ σ₂ d
736 shrinkDisjoint σ₁ (y ∷ʳ σ₂) (ySz ∷ᵣ d) = _ ∷ₙ shrinkDisjoint σ₁ σ₂ d
737 shrinkDisjoint σ₁ (vRy ∷ σ₂) (ySz ∷ᵣ d) = rs⇒t vRy ySz ∷ᵣ shrinkDisjoint σ₁ σ₂ d
738 shrinkDisjoint [] [] [] = []
739
740 open DisjointnessMonotonicity public