Debian Codegraph @

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

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Properties of the setoid sublist relation
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 open import Relation.Binary.Core using (Rel; _⇒_; _Preserves_⟶_)
10 open import Relation.Binary.Bundles using (Setoid)
11
12 module Data.List.Relation.Binary.Sublist.Setoid.Properties
13 {c ℓ} (S : Setoid c ℓ) where
14
15 open import Data.List.Base hiding (_∷ʳ_)
16 open import Data.List.Properties using (++-identityʳ)
17 open import Data.List.Relation.Unary.Any using (Any)
18 open import Data.List.Relation.Unary.All using (All; tabulateₛ)
19 import Data.Maybe.Relation.Unary.All as Maybe
20 open import Data.Nat.Base using (ℕ; _≤_; _≥_)
21 import Data.Nat.Properties as ℕ
22 open import Data.Product.Base using (∃; _,_; proj₂)
23 open import Function.Base
24 open import Function.Bundles using (_⇔_; _⤖_)
25 open import Level
26 open import Relation.Binary.Definitions using () renaming (Decidable to Decidable₂)
27 import Relation.Binary.Properties.Setoid as SetoidProperties
28 open import Relation.Binary.PropositionalEquality.Core as ≡
29 using (_≡_; refl; sym; cong; cong₂)
30 import Relation.Binary.Reasoning.Preorder as ≲-Reasoning
31 open import Relation.Binary.Reasoning.Syntax
32 open import Relation.Binary.Structures using (IsDecTotalOrder)
33 open import Relation.Unary using (Pred; Decidable; Universal; Irrelevant)
34 open import Relation.Nullary.Negation using (¬_)
35 open import Relation.Nullary.Decidable using (¬?; yes; no)
36
37 import Data.List.Relation.Binary.Equality.Setoid as SetoidEquality
38 import Data.List.Relation.Binary.Sublist.Setoid as SetoidSublist
39 import Data.List.Relation.Binary.Sublist.Heterogeneous
40 as Hetero
41 import Data.List.Relation.Binary.Sublist.Heterogeneous.Properties
42 as HeteroProperties
43 import Data.List.Membership.Setoid as SetoidMembership
44
45 open Setoid S using (_≈_; trans) renaming (Carrier to A; refl to ≈-refl)
46 open SetoidEquality S using (_≋_; ≋-refl; ≋-reflexive; ≋-setoid)
47 open SetoidSublist S hiding (map)
48 open SetoidProperties S using (≈-preorder)
49
50
51 private
52 variable
53 p q r s t : Level
54 a b x y : A
55 as bs cs ds xs ys : List A
56 xss yss : List (List A)
57 P : Pred A p
58 Q : Pred A q
59 m n : ℕ
60
61
62 ------------------------------------------------------------------------
63 -- Injectivity of constructors
64 ------------------------------------------------------------------------
65
66 module _ where
67
68 ∷-injectiveˡ : ∀ {px qx : x ≈ y} {pxs qxs : xs ⊆ ys} →
69 ((x ∷ xs) ⊆ (y ∷ ys) ∋ px ∷ pxs) ≡ (qx ∷ qxs) → px ≡ qx
70 ∷-injectiveˡ refl = refl
71
72 ∷-injectiveʳ : ∀ {px qx : x ≈ y} {pxs qxs : xs ⊆ ys} →
73 ((x ∷ xs) ⊆ (y ∷ ys) ∋ px ∷ pxs) ≡ (qx ∷ qxs) → pxs ≡ qxs
74 ∷-injectiveʳ refl = refl
75
76 ∷ʳ-injective : ∀ {pxs qxs : xs ⊆ ys} → y ∷ʳ pxs ≡ y ∷ʳ qxs → pxs ≡ qxs
77 ∷ʳ-injective refl = refl
78
79 ------------------------------------------------------------------------
80 -- Categorical properties
81 ------------------------------------------------------------------------
82
83 module _ (trans-reflˡ : ∀ {x y} (p : x ≈ y) → trans ≈-refl p ≡ p) where
84
85 ⊆-trans-idˡ : (pxs : xs ⊆ ys) → ⊆-trans ⊆-refl pxs ≡ pxs
86 ⊆-trans-idˡ [] = refl
87 ⊆-trans-idˡ (y ∷ʳ pxs) = cong (y ∷ʳ_) (⊆-trans-idˡ pxs)
88 ⊆-trans-idˡ (x ∷ pxs) = cong₂ _∷_ (trans-reflˡ x) (⊆-trans-idˡ pxs)
89
90 module _ (trans-reflʳ : ∀ {x y} (p : x ≈ y) → trans p ≈-refl ≡ p) where
91
92 ⊆-trans-idʳ : (pxs : xs ⊆ ys) → ⊆-trans pxs ⊆-refl ≡ pxs
93 ⊆-trans-idʳ [] = refl
94 ⊆-trans-idʳ (y ∷ʳ pxs) = cong (y ∷ʳ_) (⊆-trans-idʳ pxs)
95 ⊆-trans-idʳ (x ∷ pxs) = cong₂ _∷_ (trans-reflʳ x) (⊆-trans-idʳ pxs)
96
97 module _ (≈-assoc : ∀ {w x y z} (p : w ≈ x) (q : x ≈ y) (r : y ≈ z) →
98 trans p (trans q r) ≡ trans (trans p q) r) where
99
100 ⊆-trans-assoc : (ps : as ⊆ bs) (qs : bs ⊆ cs) (rs : cs ⊆ ds) →
101 ⊆-trans ps (⊆-trans qs rs) ≡ ⊆-trans (⊆-trans ps qs) rs
102 ⊆-trans-assoc ps qs (_ ∷ʳ rs) = cong (_ ∷ʳ_) (⊆-trans-assoc ps qs rs)
103 ⊆-trans-assoc ps (_ ∷ʳ qs) (_ ∷ rs) = cong (_ ∷ʳ_) (⊆-trans-assoc ps qs rs)
104 ⊆-trans-assoc (_ ∷ʳ ps) (_ ∷ qs) (_ ∷ rs) = cong (_ ∷ʳ_) (⊆-trans-assoc ps qs rs)
105 ⊆-trans-assoc (p ∷ ps) (q ∷ qs) (r ∷ rs) = cong₂ _∷_ (≈-assoc p q r) (⊆-trans-assoc ps qs rs)
106 ⊆-trans-assoc [] [] [] = refl
107
108
109 ------------------------------------------------------------------------
110 -- Reasoning over sublists
111 ------------------------------------------------------------------------
112
113 module ⊆-Reasoning = HeteroProperties.⊆-Reasoning ≈-preorder
114
115 ------------------------------------------------------------------------
116 -- Various functions' outputs are sublists
117 ------------------------------------------------------------------------
118
119 tail-⊆ : ∀ xs → Maybe.All (_⊆ xs) (tail xs)
120 tail-⊆ xs = HeteroProperties.tail-Sublist ⊆-refl
121
122 take-⊆ : ∀ n xs → take n xs ⊆ xs
123 take-⊆ n xs = HeteroProperties.take-Sublist n ⊆-refl
124
125 drop-⊆ : ∀ n xs → drop n xs ⊆ xs
126 drop-⊆ n xs = HeteroProperties.drop-Sublist n ⊆-refl
127
128 module _ (P? : Decidable P) where
129
130 takeWhile-⊆ : ∀ xs → takeWhile P? xs ⊆ xs
131 takeWhile-⊆ xs = HeteroProperties.takeWhile-Sublist P? ⊆-refl
132
133 dropWhile-⊆ : ∀ xs → dropWhile P? xs ⊆ xs
134 dropWhile-⊆ xs = HeteroProperties.dropWhile-Sublist P? ⊆-refl
135
136 filter-⊆ : ∀ xs → filter P? xs ⊆ xs
137 filter-⊆ xs = HeteroProperties.filter-Sublist P? ⊆-refl
138
139 module _ (P? : Decidable P) where
140
141 takeWhile⊆filter : ∀ xs → takeWhile P? xs ⊆ filter P? xs
142 takeWhile⊆filter xs = HeteroProperties.takeWhile-filter P? {xs} ≋-refl
143
144 filter⊆dropWhile : ∀ xs → filter P? xs ⊆ dropWhile (¬? ∘ P?) xs
145 filter⊆dropWhile xs = HeteroProperties.filter-dropWhile P? {xs} ≋-refl
146
147 ------------------------------------------------------------------------
148 -- Various list functions are increasing wrt _⊆_
149 ------------------------------------------------------------------------
150 -- We write f⁺ for the proof that `xs ⊆ ys → f xs ⊆ f ys`
151 -- and f⁻ for the one that `f xs ⊆ f ys → xs ⊆ ys`.
152
153 module _ where
154
155 ∷ˡ⁻ : a ∷ as ⊆ bs → as ⊆ bs
156 ∷ˡ⁻ = HeteroProperties.∷ˡ⁻
157
158 ∷ʳ⁻ : ¬ (a ≈ b) → a ∷ as ⊆ b ∷ bs → a ∷ as ⊆ bs
159 ∷ʳ⁻ = HeteroProperties.∷ʳ⁻
160
161 ∷⁻ : a ∷ as ⊆ b ∷ bs → as ⊆ bs
162 ∷⁻ = HeteroProperties.∷⁻
163
164 ------------------------------------------------------------------------
165 -- map
166
167 module _ {b ℓ} (R : Setoid b ℓ) where
168
169 open Setoid R using () renaming (Carrier to B; _≈_ to _≈′_)
170 open SetoidSublist R using () renaming (_⊆_ to _⊆′_)
171
172 map⁺ : ∀ {as bs} {f : A → B} → f Preserves _≈_ ⟶ _≈′_ →
173 as ⊆ bs → map f as ⊆′ map f bs
174 map⁺ {f = f} f-resp as⊆bs =
175 HeteroProperties.map⁺ f f (SetoidSublist.map S f-resp as⊆bs)
176
177 ------------------------------------------------------------------------
178 -- _++_
179
180 module _ where
181
182 ++⁺ˡ : ∀ cs → as ⊆ bs → as ⊆ cs ++ bs
183 ++⁺ˡ = HeteroProperties.++ˡ
184
185 ++⁺ʳ : ∀ cs → as ⊆ bs → as ⊆ bs ++ cs
186 ++⁺ʳ = HeteroProperties.++ʳ
187
188 ++⁺ : as ⊆ bs → cs ⊆ ds → as ++ cs ⊆ bs ++ ds
189 ++⁺ = HeteroProperties.++⁺
190
191 ++⁻ : length as ≡ length bs → as ++ cs ⊆ bs ++ ds → cs ⊆ ds
192 ++⁻ = HeteroProperties.++⁻
193
194 ------------------------------------------------------------------------
195 -- concat
196
197 module _ where
198
199 concat⁺ : Hetero.Sublist _⊆_ xss yss →
200 concat xss ⊆ concat yss
201 concat⁺ = HeteroProperties.concat⁺
202
203 open SetoidMembership ≋-setoid using (_∈_)
204 open SetoidSublist ≋-setoid
205 using ()
206 renaming (map to map-≋; from∈ to from∈-≋)
207
208 xs∈xss⇒xs⊆concat[xss] : xs ∈ xss → xs ⊆ concat xss
209 xs∈xss⇒xs⊆concat[xss] {xs = xs} {xss = xss} xs∈xss = begin
210 xs ≈⟨ ≋-reflexive (++-identityʳ xs) ⟨
211 xs ++ [] ⊆⟨ concat⁺ (map-≋ ⊆-reflexive (from∈-≋ xs∈xss)) ⟩
212 concat xss ∎
213 where open ⊆-Reasoning
214
215 all⊆concat : (xss : List (List A)) → All (_⊆ concat xss) xss
216 all⊆concat _ = tabulateₛ ≋-setoid xs∈xss⇒xs⊆concat[xss]
217
218 ------------------------------------------------------------------------
219 -- take
220
221 module _ where
222
223 take⁺ : m ≤ n → take m xs ⊆ take n xs
224 take⁺ m≤n = HeteroProperties.take⁺ m≤n ≋-refl
225
226 ------------------------------------------------------------------------
227 -- drop
228
229 module _ where
230
231 drop⁺ : m ≥ n → xs ⊆ ys → drop m xs ⊆ drop n ys
232 drop⁺ = HeteroProperties.drop⁺
233
234 module _ where
235
236 drop⁺-≥ : m ≥ n → drop m xs ⊆ drop n xs
237 drop⁺-≥ m≥n = drop⁺ m≥n ⊆-refl
238
239 module _ where
240
241 drop⁺-⊆ : ∀ n → xs ⊆ ys → drop n xs ⊆ drop n ys
242 drop⁺-⊆ n xs⊆ys = drop⁺ {n} ℕ.≤-refl xs⊆ys
243
244 ------------------------------------------------------------------------
245 -- takeWhile / dropWhile
246
247 module _ (P? : Decidable P) (Q? : Decidable Q) where
248
249 takeWhile⁺ : ∀ {xs} → (∀ {a b} → a ≈ b → P a → Q b) →
250 takeWhile P? xs ⊆ takeWhile Q? xs
251 takeWhile⁺ {xs} P⇒Q = HeteroProperties.⊆-takeWhile-Sublist P? Q? {xs} P⇒Q ≋-refl
252
253 dropWhile⁺ : ∀ {xs} → (∀ {a b} → a ≈ b → Q b → P a) →
254 dropWhile P? xs ⊆ dropWhile Q? xs
255 dropWhile⁺ {xs} P⇒Q = HeteroProperties.⊇-dropWhile-Sublist P? Q? {xs} P⇒Q ≋-refl
256
257 ------------------------------------------------------------------------
258 -- filter
259
260 module _ (P? : Decidable P) (Q? : Decidable Q) where
261
262 filter⁺ : (∀ {a b} → a ≈ b → P a → Q b) →
263 as ⊆ bs → filter P? as ⊆ filter Q? bs
264 filter⁺ = HeteroProperties.⊆-filter-Sublist P? Q?
265
266 ------------------------------------------------------------------------
267 -- reverse
268
269 module _ where
270
271 reverseAcc⁺ : as ⊆ bs → cs ⊆ ds →
272 reverseAcc cs as ⊆ reverseAcc ds bs
273 reverseAcc⁺ = HeteroProperties.reverseAcc⁺
274
275 ʳ++⁺ : as ⊆ bs → cs ⊆ ds →
276 as ʳ++ cs ⊆ bs ʳ++ ds
277 ʳ++⁺ = reverseAcc⁺
278
279 reverse⁺ : as ⊆ bs → reverse as ⊆ reverse bs
280 reverse⁺ = HeteroProperties.reverse⁺
281
282 reverse⁻ : reverse as ⊆ reverse bs → as ⊆ bs
283 reverse⁻ = HeteroProperties.reverse⁻
284
285 ------------------------------------------------------------------------
286 -- merge
287
288 module _ {ℓ′} {_≤_ : Rel A ℓ′} (_≤?_ : Decidable₂ _≤_) where
289
290 ⊆-mergeˡ : ∀ xs ys → xs ⊆ merge _≤?_ xs ys
291 ⊆-mergeˡ [] ys = minimum ys
292 ⊆-mergeˡ (x ∷ xs) [] = ⊆-refl
293 ⊆-mergeˡ (x ∷ xs) (y ∷ ys)
294 with x ≤? y | ⊆-mergeˡ xs (y ∷ ys)
295 | ⊆-mergeˡ (x ∷ xs) ys
296 ... | yes x≤y | rec | _ = ≈-refl ∷ rec
297 ... | no x≰y | _ | rec = y ∷ʳ rec
298
299 ⊆-mergeʳ : ∀ xs ys → ys ⊆ merge _≤?_ xs ys
300 ⊆-mergeʳ [] ys = ⊆-refl
301 ⊆-mergeʳ (x ∷ xs) [] = minimum (merge _≤?_ (x ∷ xs) [])
302 ⊆-mergeʳ (x ∷ xs) (y ∷ ys)
303 with x ≤? y | ⊆-mergeʳ xs (y ∷ ys)
304 | ⊆-mergeʳ (x ∷ xs) ys
305 ... | yes x≤y | rec | _ = x ∷ʳ rec
306 ... | no x≰y | _ | rec = ≈-refl ∷ rec
307
308 ------------------------------------------------------------------------
309 -- Inversion lemmas
310 ------------------------------------------------------------------------
311
312 module _ where
313
314 ∷⁻¹ : a ≈ b → as ⊆ bs ⇔ a ∷ as ⊆ b ∷ bs
315 ∷⁻¹ = HeteroProperties.∷⁻¹
316
317 ∷ʳ⁻¹ : ¬ (a ≈ b) → a ∷ as ⊆ bs ⇔ a ∷ as ⊆ b ∷ bs
318 ∷ʳ⁻¹ = HeteroProperties.∷ʳ⁻¹
319
320 ------------------------------------------------------------------------
321 -- Other
322 ------------------------------------------------------------------------
323
324 module _ where
325
326 length-mono-≤ : as ⊆ bs → length as ≤ length bs
327 length-mono-≤ = HeteroProperties.length-mono-≤
328
329 ------------------------------------------------------------------------
330 -- Conversion to and from list equality
331
332 to-≋ : length as ≡ length bs → as ⊆ bs → as ≋ bs
333 to-≋ = HeteroProperties.toPointwise
334
335 ------------------------------------------------------------------------
336 -- Empty special case
337
338 []⊆-universal : Universal ([] ⊆_)
339 []⊆-universal = HeteroProperties.Sublist-[]-universal
340
341 []⊆-irrelevant : Irrelevant ([] ⊆_)
342 []⊆-irrelevant = HeteroProperties.Sublist-[]-irrelevant
343
344 ------------------------------------------------------------------------
345 -- (to/from)∈ is a bijection
346
347 module _ where
348
349 open SetoidMembership S using (_∈_)
350
351 to∈-injective : ∀ {p q : [ x ] ⊆ xs} → to∈ p ≡ to∈ q → p ≡ q
352 to∈-injective = HeteroProperties.toAny-injective
353
354 from∈-injective : ∀ {p q : x ∈ xs} → from∈ p ≡ from∈ q → p ≡ q
355 from∈-injective = HeteroProperties.fromAny-injective
356
357 to∈∘from∈≗id : ∀ (p : x ∈ xs) → to∈ (from∈ p) ≡ p
358 to∈∘from∈≗id = HeteroProperties.toAny∘fromAny≗id
359
360 [x]⊆xs⤖x∈xs : ([ x ] ⊆ xs) ⤖ (x ∈ xs)
361 [x]⊆xs⤖x∈xs = HeteroProperties.Sublist-[x]-bijection
362
363 ------------------------------------------------------------------------
364 -- Properties of Disjoint(ness) and DisjointUnion
365
366 open HeteroProperties.Disjointness {R = _≈_} public
367 open HeteroProperties.DisjointnessMonotonicity {R = _≈_} {S = _≈_} {T = _≈_} trans public
368
369 -- Shrinking one of two disjoint lists preserves disjointness.
370
371 -- We would have liked to define shrinkDisjointˡ σ = shrinkDisjoint σ ⊆-refl
372 -- but alas, this is only possible for groupoids, not setoids in general.
373
374 shrinkDisjointˡ : ∀ {xs ys zs us} {τ₁ : xs ⊆ zs} {τ₂ : ys ⊆ zs} (σ : us ⊆ xs) →
375 Disjoint τ₁ τ₂ →
376 Disjoint (⊆-trans σ τ₁) τ₂
377 -- Not affected by σ:
378 shrinkDisjointˡ σ (y ∷ₙ d) = y ∷ₙ shrinkDisjointˡ σ d
379 shrinkDisjointˡ σ (y≈z ∷ᵣ d) = y≈z ∷ᵣ shrinkDisjointˡ σ d
380 -- In σ: keep x.
381 shrinkDisjointˡ (u≈x ∷ σ) (x≈z ∷ₗ d) = trans u≈x x≈z ∷ₗ shrinkDisjointˡ σ d
382 -- Not in σ: drop x.
383 shrinkDisjointˡ (x ∷ʳ σ) (x≈z ∷ₗ d) = _ ∷ₙ shrinkDisjointˡ σ d
384 shrinkDisjointˡ [] [] = []
385
386 shrinkDisjointʳ : ∀ {xs ys zs vs} {τ₁ : xs ⊆ zs} {τ₂ : ys ⊆ zs} (σ : vs ⊆ ys) →
387 Disjoint τ₁ τ₂ →
388 Disjoint τ₁ (⊆-trans σ τ₂)
389 -- Not affected by σ:
390 shrinkDisjointʳ σ (y ∷ₙ d) = y ∷ₙ shrinkDisjointʳ σ d
391 shrinkDisjointʳ σ (x≈z ∷ₗ d) = x≈z ∷ₗ shrinkDisjointʳ σ d
392 -- In σ: keep y.
393 shrinkDisjointʳ (v≈y ∷ σ) (y≈z ∷ᵣ d) = trans v≈y y≈z ∷ᵣ shrinkDisjointʳ σ d
394 -- Not in σ: drop y.
395 shrinkDisjointʳ (y ∷ʳ σ) (y≈z ∷ᵣ d) = _ ∷ₙ shrinkDisjointʳ σ d
396 shrinkDisjointʳ [] [] = []