Debian Codegraph @

src/Data/List/Membership/Setoid/Properties.agda

raw
1 -----------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Properties related to setoid list membership
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.List.Membership.Setoid.Properties where
10
11 open import Algebra using (Op₂; Selective)
12 open import Data.Bool.Base using (true; false)
13 open import Data.Fin.Base using (Fin; zero; suc)
14 open import Data.Fin.Properties using (suc-injective)
15 open import Data.List.Base hiding (find)
16 import Data.List.Membership.Setoid as Membership
17 import Data.List.Relation.Binary.Equality.Setoid as Equality
18 open import Data.List.Relation.Unary.All as All using (All)
19 open import Data.List.Relation.Unary.Any as Any using (Any; here; there)
20 import Data.List.Relation.Unary.All.Properties.Core as All
21 import Data.List.Relation.Unary.Any.Properties as Any
22 import Data.List.Relation.Unary.Unique.Setoid as Unique
23 open import Data.Nat.Base using (suc; z<s; _<_)
24 open import Data.Product.Base as Product using (∃; _×_; _,_ ; ∃₂; ∃-syntax)
25 open import Data.Product.Relation.Binary.Pointwise.NonDependent using (_×ₛ_)
26 open import Data.Sum.Base using (_⊎_; inj₁; inj₂; [_,_]′)
27 open import Function.Base using (_$_; flip; _∘_; _∘′_; id)
28 open import Function.Bundles using (_↔_; mk↔; _⇔_; mk⇔)
29 open import Level using (Level)
30 open import Relation.Binary.Core using (Rel; _Preserves₂_⟶_⟶_; _Preserves_⟶_)
31 open import Relation.Binary.Definitions as Binary hiding (Decidable)
32 open import Relation.Binary.Bundles using (Setoid)
33 open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_)
34 open import Relation.Nullary.Decidable using (does; _because_; yes; no)
35 open import Relation.Nullary.Negation.Core using (¬_; contradiction)
36 open import Relation.Nullary.Reflects using (invert)
37 open import Relation.Unary as Unary using (Decidable; Pred)
38
39 open Setoid using (Carrier)
40
41 private
42 variable
43 c c₁ c₂ c₃ p ℓ ℓ₁ ℓ₂ ℓ₃ : Level
44
45 ------------------------------------------------------------------------
46 -- Basics
47
48 module _ (S : Setoid c ℓ) where
49
50 open Membership S
51
52 ∉[] : ∀ {x} → x ∉ []
53 ∉[] ()
54
55 ------------------------------------------------------------------------
56 -- Equality properties
57
58 module _ (S : Setoid c ℓ) where
59
60 open Setoid S
61 open Equality S
62 open Membership S
63
64 -- _∈_ respects the underlying equality
65
66 ∈-resp-≈ : ∀ {xs} → (_∈ xs) Respects _≈_
67 ∈-resp-≈ x≈y x∈xs = Any.map (trans (sym x≈y)) x∈xs
68
69 ∉-resp-≈ : ∀ {xs} → (_∉ xs) Respects _≈_
70 ∉-resp-≈ v≈w v∉xs w∈xs = v∉xs (∈-resp-≈ (sym v≈w) w∈xs)
71
72 ∈-resp-≋ : ∀ {x} → (x ∈_) Respects _≋_
73 ∈-resp-≋ = Any.lift-resp (flip trans)
74
75 ∉-resp-≋ : ∀ {x} → (x ∉_) Respects _≋_
76 ∉-resp-≋ xs≋ys v∉xs v∈ys = v∉xs (∈-resp-≋ (≋-sym xs≋ys) v∈ys)
77
78 -- x ∉_ is equivalent to All (x ≉_)
79
80 ∉⇒All[≉] : ∀ {x xs} → x ∉ xs → All (x ≉_) xs
81 ∉⇒All[≉] = All.¬Any⇒All¬ _
82
83 All[≉]⇒∉ : ∀ {x xs} → All (x ≉_) xs → x ∉ xs
84 All[≉]⇒∉ = All.All¬⇒¬Any
85
86 -- index is injective in its first argument.
87
88 index-injective : ∀ {x₁ x₂ xs} (x₁∈xs : x₁ ∈ xs) (x₂∈xs : x₂ ∈ xs) →
89 Any.index x₁∈xs ≡ Any.index x₂∈xs → x₁ ≈ x₂
90 index-injective (here x₁≈x) (here x₂≈x) _ = trans x₁≈x (sym x₂≈x)
91 index-injective (there x₁∈xs) (there x₂∈xs) eq = index-injective x₁∈xs x₂∈xs (suc-injective eq)
92
93 ------------------------------------------------------------------------
94 -- Irrelevance
95
96 module _ (S : Setoid c ℓ) where
97
98 open Setoid S
99 open Unique S
100 open Membership S
101
102 private
103 ∉×∈⇒≉ : ∀ {x y xs} → All (y ≉_) xs → x ∈ xs → x ≉ y
104 ∉×∈⇒≉ ≉s x∈xs x≈y = All[≉]⇒∉ S ≉s (∈-resp-≈ S x≈y x∈xs)
105
106 unique⇒irrelevant : Binary.Irrelevant _≈_ → ∀ {xs} → Unique xs → Unary.Irrelevant (_∈ xs)
107 unique⇒irrelevant ≈-irr _ (here p) (here q) =
108 ≡.cong here (≈-irr p q)
109 unique⇒irrelevant ≈-irr (_ ∷ u) (there p) (there q) =
110 ≡.cong there (unique⇒irrelevant ≈-irr u p q)
111 unique⇒irrelevant ≈-irr (≉s ∷ _) (here p) (there q) =
112 contradiction p (∉×∈⇒≉ ≉s q)
113 unique⇒irrelevant ≈-irr (≉s ∷ _) (there p) (here q) =
114 contradiction q (∉×∈⇒≉ ≉s p)
115
116 ------------------------------------------------------------------------
117 -- mapWith∈
118
119 module _ (S₁ : Setoid c₁ ℓ₁) (S₂ : Setoid c₂ ℓ₂) where
120
121 open Setoid S₁ renaming (Carrier to A₁; _≈_ to _≈₁_; refl to refl₁)
122 open Setoid S₂ renaming (Carrier to A₂; _≈_ to _≈₂_; refl to refl₂)
123 open Equality S₁ using ([]; _∷_) renaming (_≋_ to _≋₁_)
124 open Equality S₂ using () renaming (_≋_ to _≋₂_)
125 open Membership S₁
126
127 mapWith∈-cong : ∀ {xs ys} → xs ≋₁ ys →
128 (f : ∀ {x} → x ∈ xs → A₂) →
129 (g : ∀ {y} → y ∈ ys → A₂) →
130 (∀ {x y} → x ≈₁ y → (x∈xs : x ∈ xs) (y∈ys : y ∈ ys) →
131 f x∈xs ≈₂ g y∈ys) →
132 mapWith∈ xs f ≋₂ mapWith∈ ys g
133 mapWith∈-cong [] f g cong = []
134 mapWith∈-cong (x≈y ∷ xs≋ys) f g cong =
135 cong x≈y (here refl₁) (here refl₁) ∷
136 mapWith∈-cong xs≋ys (f ∘ there) (g ∘ there)
137 (λ x≈y x∈xs y∈ys → cong x≈y (there x∈xs) (there y∈ys))
138
139 mapWith∈≗map : ∀ f xs → mapWith∈ xs (λ {x} _ → f x) ≋₂ map f xs
140 mapWith∈≗map f [] = []
141 mapWith∈≗map f (x ∷ xs) = refl₂ ∷ mapWith∈≗map f xs
142
143
144 module _ (S : Setoid c ℓ) where
145
146 open Setoid S
147 open Membership S
148
149 length-mapWith∈ : ∀ {a} {A : Set a} xs {f : ∀ {x} → x ∈ xs → A} →
150 length (mapWith∈ xs f) ≡ length xs
151 length-mapWith∈ [] = ≡.refl
152 length-mapWith∈ (x ∷ xs) = ≡.cong suc (length-mapWith∈ xs)
153
154 mapWith∈-id : ∀ xs → mapWith∈ xs (λ {x} _ → x) ≡ xs
155 mapWith∈-id [] = ≡.refl
156 mapWith∈-id (x ∷ xs) = ≡.cong (x ∷_) (mapWith∈-id xs)
157
158 map-mapWith∈ : ∀ {a b} {A : Set a} {B : Set b} →
159 ∀ xs (f : ∀ {x} → x ∈ xs → A) (g : A → B) →
160 map g (mapWith∈ xs f) ≡ mapWith∈ xs (g ∘′ f)
161 map-mapWith∈ [] f g = ≡.refl
162 map-mapWith∈ (x ∷ xs) f g = ≡.cong (_ ∷_) (map-mapWith∈ xs (f ∘ there) g)
163
164 ------------------------------------------------------------------------
165 -- map
166
167 module _ (S₁ : Setoid c₁ ℓ₁) (S₂ : Setoid c₂ ℓ₂) where
168
169 open Setoid S₁ renaming (_≈_ to _≈₁_)
170 open Setoid S₂ renaming (_≈_ to _≈₂_)
171 private module M₁ = Membership S₁; open M₁ using (find) renaming (_∈_ to _∈₁_)
172 private module M₂ = Membership S₂; open M₂ using () renaming (_∈_ to _∈₂_)
173
174 ∈-map⁺ : ∀ {f} → f Preserves _≈₁_ ⟶ _≈₂_ →
175 ∀ {v xs} → v ∈₁ xs → f v ∈₂ map f xs
176 ∈-map⁺ pres x∈xs = Any.map⁺ (Any.map pres x∈xs)
177
178 ∈-map⁻ : ∀ {v xs f} → v ∈₂ map f xs →
179 ∃ λ x → x ∈₁ xs × v ≈₂ f x
180 ∈-map⁻ x∈map = find (Any.map⁻ x∈map)
181
182 map-∷= : ∀ {f} (pres : f Preserves _≈₁_ ⟶ _≈₂_) →
183 ∀ {xs x v} → (x∈xs : x ∈₁ xs) →
184 map f (x∈xs M₁.∷= v) ≡ ∈-map⁺ pres x∈xs M₂.∷= f v
185 map-∷= pres (here x≈y) = ≡.refl
186 map-∷= pres (there x∈xs) = ≡.cong (_ ∷_) (map-∷= pres x∈xs)
187
188 ------------------------------------------------------------------------
189 -- _++_
190
191 module _ (S : Setoid c ℓ) where
192
193 open Membership S using (_∈_)
194 open Setoid S
195 open Equality S using (_≋_; _∷_; ≋-refl)
196
197 ∈-++⁺ˡ : ∀ {v xs ys} → v ∈ xs → v ∈ xs ++ ys
198 ∈-++⁺ˡ = Any.++⁺ˡ
199
200 ∈-++⁺ʳ : ∀ {v} xs {ys} → v ∈ ys → v ∈ xs ++ ys
201 ∈-++⁺ʳ = Any.++⁺ʳ
202
203 ∈-++⁻ : ∀ {v} xs {ys} → v ∈ xs ++ ys → (v ∈ xs) ⊎ (v ∈ ys)
204 ∈-++⁻ = Any.++⁻
205
206 ∈-++⁺∘++⁻ : ∀ {v} xs {ys} (p : v ∈ xs ++ ys) →
207 [ ∈-++⁺ˡ , ∈-++⁺ʳ xs ]′ (∈-++⁻ xs p) ≡ p
208 ∈-++⁺∘++⁻ = Any.++⁺∘++⁻
209
210 ∈-++⁻∘++⁺ : ∀ {v} xs {ys} (p : v ∈ xs ⊎ v ∈ ys) →
211 ∈-++⁻ xs ([ ∈-++⁺ˡ , ∈-++⁺ʳ xs ]′ p) ≡ p
212 ∈-++⁻∘++⁺ = Any.++⁻∘++⁺
213
214 ∈-++↔ : ∀ {v xs ys} → (v ∈ xs ⊎ v ∈ ys) ↔ v ∈ xs ++ ys
215 ∈-++↔ = Any.++↔
216
217 ∈-++-comm : ∀ {v} xs ys → v ∈ xs ++ ys → v ∈ ys ++ xs
218 ∈-++-comm = Any.++-comm
219
220 ∈-++-comm∘++-comm : ∀ {v} xs {ys} (p : v ∈ xs ++ ys) →
221 ∈-++-comm ys xs (∈-++-comm xs ys p) ≡ p
222 ∈-++-comm∘++-comm = Any.++-comm∘++-comm
223
224 ∈-++↔++ : ∀ {v} xs ys → v ∈ xs ++ ys ↔ v ∈ ys ++ xs
225 ∈-++↔++ = Any.++↔++
226
227 ∈-insert : ∀ xs {ys v w} → v ≈ w → v ∈ xs ++ [ w ] ++ ys
228 ∈-insert xs = Any.++-insert xs
229
230 ∈-∃++ : ∀ {v xs} → v ∈ xs → ∃₂ λ ys zs → ∃ λ w →
231 v ≈ w × xs ≋ ys ++ [ w ] ++ zs
232 ∈-∃++ (here px) = [] , _ , _ , px , ≋-refl
233 ∈-∃++ (there {d} v∈xs) =
234 let hs , _ , _ , v≈v′ , eq = ∈-∃++ v∈xs
235 in d ∷ hs , _ , _ , v≈v′ , refl ∷ eq
236
237 ------------------------------------------------------------------------
238 -- concat
239
240 module _ (S : Setoid c ℓ) where
241
242 open Setoid S using (_≈_)
243 open Membership S using (_∈_)
244 open Equality S using (≋-setoid)
245 open Membership ≋-setoid using (find) renaming (_∈_ to _∈ₗ_)
246
247 ∈-concat⁺ : ∀ {v xss} → Any (v ∈_) xss → v ∈ concat xss
248 ∈-concat⁺ = Any.concat⁺
249
250 ∈-concat⁻ : ∀ {v} xss → v ∈ concat xss → Any (v ∈_) xss
251 ∈-concat⁻ = Any.concat⁻
252
253 ∈-concat⁺′ : ∀ {v vs xss} → v ∈ vs → vs ∈ₗ xss → v ∈ concat xss
254 ∈-concat⁺′ v∈vs = ∈-concat⁺ ∘ Any.map (flip (∈-resp-≋ S) v∈vs)
255
256 ∈-concat⁻′ : ∀ {v} xss → v ∈ concat xss → ∃ λ xs → v ∈ xs × xs ∈ₗ xss
257 ∈-concat⁻′ xss v∈c[xss] =
258 let xs , xs∈xss , v∈xs = find (∈-concat⁻ xss v∈c[xss]) in xs , v∈xs , xs∈xss
259
260 ------------------------------------------------------------------------
261 -- concatMap
262
263 module _
264 (S₁ : Setoid c₁ ℓ₁) (S₂ : Setoid c₂ ℓ₂)
265 {f : Carrier S₁ → List (Carrier S₂)} {xs y} where
266
267 open Membership S₂ using (_∈_)
268
269 ∈-concatMap⁺ : Any ((y ∈_) ∘ f) xs → y ∈ concatMap f xs
270 ∈-concatMap⁺ = Any.concatMap⁺ f
271
272 ∈-concatMap⁻ : y ∈ concatMap f xs → Any ((y ∈_) ∘ f) xs
273 ∈-concatMap⁻ = Any.concatMap⁻ f
274
275 ------------------------------------------------------------------------
276 -- reverse
277
278 module _ (S : Setoid c ℓ) where
279
280 open Membership S using (_∈_)
281
282 reverse⁺ : ∀ {x xs} → x ∈ xs → x ∈ reverse xs
283 reverse⁺ = Any.reverse⁺
284
285 reverse⁻ : ∀ {x xs} → x ∈ reverse xs → x ∈ xs
286 reverse⁻ = Any.reverse⁻
287
288 ------------------------------------------------------------------------
289 -- cartesianProductWith
290
291 module _ (S₁ : Setoid c₁ ℓ₁) (S₂ : Setoid c₂ ℓ₂) (S₃ : Setoid c₃ ℓ₃) where
292
293 open Setoid S₁ renaming (_≈_ to _≈₁_; refl to refl₁)
294 open Setoid S₂ renaming (_≈_ to _≈₂_)
295 open Setoid S₃ renaming (_≈_ to _≈₃_)
296 open Membership S₁ renaming (_∈_ to _∈₁_)
297 open Membership S₂ renaming (_∈_ to _∈₂_)
298 open Membership S₃ renaming (_∈_ to _∈₃_)
299
300 ∈-cartesianProductWith⁺ : ∀ {f} → f Preserves₂ _≈₁_ ⟶ _≈₂_ ⟶ _≈₃_ →
301 ∀ {xs ys a b} → a ∈₁ xs → b ∈₂ ys →
302 f a b ∈₃ cartesianProductWith f xs ys
303 ∈-cartesianProductWith⁺ pres = Any.cartesianProductWith⁺ _ pres
304
305 ∈-cartesianProductWith⁻ : ∀ f xs ys {v} → v ∈₃ cartesianProductWith f xs ys →
306 ∃₂ λ a b → a ∈₁ xs × b ∈₂ ys × v ≈₃ f a b
307 ∈-cartesianProductWith⁻ f (x ∷ xs) ys v∈c with ∈-++⁻ S₃ (map (f x) ys) v∈c
308 ... | inj₁ v∈map =
309 let b , b∈ys , v≈fxb = ∈-map⁻ S₂ S₃ v∈map
310 in x , b , here refl₁ , b∈ys , v≈fxb
311 ... | inj₂ v∈com =
312 let a , b , a∈xs , b∈ys , v≈fab = ∈-cartesianProductWith⁻ f xs ys v∈com
313 in a , b , there a∈xs , b∈ys , v≈fab
314
315 ------------------------------------------------------------------------
316 -- cartesianProduct
317
318 module _ (S₁ : Setoid c₁ ℓ₁) (S₂ : Setoid c₂ ℓ₂) where
319
320 open Setoid S₁ renaming (Carrier to A)
321 open Setoid S₂ renaming (Carrier to B)
322 open Membership S₁ renaming (_∈_ to _∈₁_)
323 open Membership S₂ renaming (_∈_ to _∈₂_)
324 open Membership (S₁ ×ₛ S₂) renaming (_∈_ to _∈₁₂_)
325
326 ∈-cartesianProduct⁺ : ∀ {x y xs ys} → x ∈₁ xs → y ∈₂ ys →
327 (x , y) ∈₁₂ cartesianProduct xs ys
328 ∈-cartesianProduct⁺ = Any.cartesianProduct⁺
329
330 ∈-cartesianProduct⁻ : ∀ xs ys {xy@(x , y) : A × B} →
331 xy ∈₁₂ cartesianProduct xs ys →
332 x ∈₁ xs × y ∈₂ ys
333 ∈-cartesianProduct⁻ xs ys = Any.cartesianProduct⁻ xs ys
334
335 ------------------------------------------------------------------------
336 -- applyUpTo
337
338 module _ (S : Setoid c ℓ) where
339
340 open Setoid S using (_≈_; refl)
341 open Membership S using (_∈_)
342
343 ∈-applyUpTo⁺ : ∀ f {i n} → i < n → f i ∈ applyUpTo f n
344 ∈-applyUpTo⁺ f = Any.applyUpTo⁺ f refl
345
346 ∈-applyUpTo⁻ : ∀ {v} f {n} → v ∈ applyUpTo f n →
347 ∃ λ i → i < n × v ≈ f i
348 ∈-applyUpTo⁻ = Any.applyUpTo⁻
349
350 ------------------------------------------------------------------------
351 -- applyDownFrom
352
353 ∈-applyDownFrom⁺ : ∀ f {i n} → i < n → f i ∈ applyDownFrom f n
354 ∈-applyDownFrom⁺ f = Any.applyDownFrom⁺ f refl
355
356 ∈-applyDownFrom⁻ : ∀ {v} f {n} → v ∈ applyDownFrom f n →
357 ∃ λ i → i < n × v ≈ f i
358 ∈-applyDownFrom⁻ = Any.applyDownFrom⁻
359
360 ------------------------------------------------------------------------
361 -- tabulate
362
363 module _ (S : Setoid c ℓ) where
364
365 open Setoid S using (_≈_; refl) renaming (Carrier to A)
366 open Membership S using (_∈_)
367
368 ∈-tabulate⁺ : ∀ {n} {f : Fin n → A} i → f i ∈ tabulate f
369 ∈-tabulate⁺ i = Any.tabulate⁺ i refl
370
371 ∈-tabulate⁻ : ∀ {n} {f : Fin n → A} {v} →
372 v ∈ tabulate f → ∃ λ i → v ≈ f i
373 ∈-tabulate⁻ = Any.tabulate⁻
374
375 ------------------------------------------------------------------------
376 -- filter
377
378 module _ (S : Setoid c ℓ) {P : Pred (Carrier S) p}
379 (P? : Decidable P) (resp : P Respects (Setoid._≈_ S)) where
380
381 open Setoid S using (_≈_; sym)
382 open Membership S using (_∈_)
383
384 ∈-filter⁺ : ∀ {v xs} → v ∈ xs → P v → v ∈ filter P? xs
385 ∈-filter⁺ {xs = x ∷ _} (here v≈x) Pv with P? x
386 ... | true because _ = here v≈x
387 ... | false because [¬Px] = contradiction (resp v≈x Pv) (invert [¬Px])
388 ∈-filter⁺ {xs = x ∷ _} (there v∈xs) Pv with does (P? x)
389 ... | true = there (∈-filter⁺ v∈xs Pv)
390 ... | false = ∈-filter⁺ v∈xs Pv
391
392 ∈-filter⁻ : ∀ {v xs} → v ∈ filter P? xs → v ∈ xs × P v
393 ∈-filter⁻ {xs = x ∷ xs} v∈f[x∷xs] with P? x
394 ... | false because _ = Product.map there id (∈-filter⁻ v∈f[x∷xs])
395 ... | true because [Px] with v∈f[x∷xs]
396 ... | here v≈x = here v≈x , resp (sym v≈x) (invert [Px])
397 ... | there v∈fxs = Product.map there id (∈-filter⁻ v∈fxs)
398
399 ------------------------------------------------------------------------
400 -- map∘filter
401
402 module _
403 (S₁ : Setoid c₁ ℓ₁) (S₂ : Setoid c₂ ℓ₂)
404 {P : Pred _ p} (P? : Decidable P) (resp : P Respects _)
405 {f xs y} where
406
407 open Setoid S₁ renaming (_≈_ to _≈₁_)
408 open Setoid S₂ renaming (_≈_ to _≈₂_; sym to sym₂)
409 open Membership S₁ renaming (_∈_ to _∈₁_)
410 open Membership S₂ renaming (_∈_ to _∈₂_)
411
412 ∈-map∘filter⁻ : y ∈₂ map f (filter P? xs) →
413 ∃[ x ] x ∈₁ xs × y ≈₂ f x × P x
414 ∈-map∘filter⁻ h =
415 let x , x∈ , y≈ = ∈-map⁻ S₁ S₂ h
416 y∈ , Py = ∈-filter⁻ S₁ P? resp x∈
417 in x , y∈ , y≈ , Py
418
419 ∈-map∘filter⁺ : f Preserves _≈₁_ ⟶ _≈₂_ →
420 ∃[ x ] x ∈₁ xs × y ≈₂ f x × P x →
421 y ∈₂ map f (filter P? xs)
422 ∈-map∘filter⁺ pres (x , x∈ , y≈ , Px)
423 = ∈-resp-≈ S₂ (sym₂ y≈)
424 $ ∈-map⁺ S₁ S₂ pres (∈-filter⁺ S₁ P? resp x∈ Px)
425
426 ------------------------------------------------------------------------
427 -- derun and deduplicate
428
429 module _ (S : Setoid c ℓ) {R : Rel (Carrier S) ℓ₂} (R? : Binary.Decidable R) where
430
431 open Setoid S using (_≈_)
432 open Membership S using (_∈_)
433
434 ∈-derun⁺ : _≈_ Respectsʳ R → ∀ {xs z} → z ∈ xs → z ∈ derun R? xs
435 ∈-derun⁺ ≈-resp-R z∈xs = Any.derun⁺ R? ≈-resp-R z∈xs
436
437 ∈-derun⁻ : ∀ xs {z} → z ∈ derun R? xs → z ∈ xs
438 ∈-derun⁻ xs z∈derun[R,xs] = Any.derun⁻ R? z∈derun[R,xs]
439
440 ∈-deduplicate⁺ : _≈_ Respectsʳ (flip R) → ∀ {xs z} →
441 z ∈ xs → z ∈ deduplicate R? xs
442 ∈-deduplicate⁺ ≈-resp-R z∈xs = Any.deduplicate⁺ R? ≈-resp-R z∈xs
443
444 ∈-deduplicate⁻ : ∀ xs {z} → z ∈ deduplicate R? xs → z ∈ xs
445 ∈-deduplicate⁻ xs z∈dedup[R,xs] = Any.deduplicate⁻ R? z∈dedup[R,xs]
446
447 deduplicate-∈⇔ : _≈_ Respectsʳ (flip R) → ∀ {xs z} →
448 z ∈ xs ⇔ z ∈ deduplicate R? xs
449 deduplicate-∈⇔ p = mk⇔ (∈-deduplicate⁺ p) (∈-deduplicate⁻ _)
450
451 ------------------------------------------------------------------------
452 -- length
453
454 module _ (S : Setoid c ℓ) where
455
456 open Membership S using (_∈_)
457
458 ∈-length : ∀ {x xs} → x ∈ xs → 0 < length xs
459 ∈-length (here px) = z<s
460 ∈-length (there x∈xs) = z<s
461
462 ------------------------------------------------------------------------
463 -- lookup
464
465 module _ (S : Setoid c ℓ) where
466
467 open Setoid S using (refl)
468 open Membership S using (_∈_)
469
470 ∈-lookup : ∀ xs i → lookup xs i ∈ xs
471 ∈-lookup (x ∷ xs) zero = here refl
472 ∈-lookup (x ∷ xs) (suc i) = there (∈-lookup xs i)
473
474 ------------------------------------------------------------------------
475 -- foldr
476
477 module _ (S : Setoid c ℓ) {_•_ : Op₂ (Carrier S)} where
478
479 open Setoid S using (_≈_; refl; sym; trans)
480 open Membership S using (_∈_)
481
482 foldr-selective : Selective _≈_ _•_ → ∀ e xs →
483 (foldr _•_ e xs ≈ e) ⊎ (foldr _•_ e xs ∈ xs)
484 foldr-selective •-sel i [] = inj₁ refl
485 foldr-selective •-sel i (x ∷ xs) with •-sel x (foldr _•_ i xs)
486 ... | inj₁ x•f≈x = inj₂ (here x•f≈x)
487 ... | inj₂ x•f≈f with foldr-selective •-sel i xs
488 ... | inj₁ f≈i = inj₁ (trans x•f≈f f≈i)
489 ... | inj₂ f∈xs = inj₂ (∈-resp-≈ S (sym x•f≈f) (there f∈xs))
490
491 ------------------------------------------------------------------------
492 -- _∷=_
493
494 module _ (S : Setoid c ℓ) where
495
496 open Setoid S
497 open Membership S
498
499 ∈-∷=⁺-updated : ∀ {xs x v} (x∈xs : x ∈ xs) → v ∈ (x∈xs ∷= v)
500 ∈-∷=⁺-updated (here px) = here refl
501 ∈-∷=⁺-updated (there pxs) = there (∈-∷=⁺-updated pxs)
502
503 ∈-∷=⁺-untouched : ∀ {xs x y v} (x∈xs : x ∈ xs) → (¬ x ≈ y) → y ∈ xs → y ∈ (x∈xs ∷= v)
504 ∈-∷=⁺-untouched (here x≈z) x≉y (here y≈z) = contradiction (trans x≈z (sym y≈z)) x≉y
505 ∈-∷=⁺-untouched (here x≈z) x≉y (there y∈xs) = there y∈xs
506 ∈-∷=⁺-untouched (there x∈xs) x≉y (here y≈z) = here y≈z
507 ∈-∷=⁺-untouched (there x∈xs) x≉y (there y∈xs) = there (∈-∷=⁺-untouched x∈xs x≉y y∈xs)
508
509 ∈-∷=⁻ : ∀ {xs x y v} (x∈xs : x ∈ xs) → (¬ y ≈ v) → y ∈ (x∈xs ∷= v) → y ∈ xs
510 ∈-∷=⁻ (here x≈z) y≉v (here y≈v) = contradiction y≈v y≉v
511 ∈-∷=⁻ (here x≈z) y≉v (there y∈) = there y∈
512 ∈-∷=⁻ (there x∈xs) y≉v (here y≈z) = here y≈z
513 ∈-∷=⁻ (there x∈xs) y≉v (there y∈) = there (∈-∷=⁻ x∈xs y≉v y∈)
514
515 ------------------------------------------------------------------------
516 -- Any/All symmetry wrt _∈_/_∉_
517
518 module _ (S : Setoid c ℓ) where
519
520 open Setoid S using (sym)
521 open Membership S
522
523 Any-∈-swap : ∀ {xs ys} → Any (_∈ ys) xs → Any (_∈ xs) ys
524 Any-∈-swap = Any.swap ∘ Any.map (Any.map sym)
525
526 All-∉-swap : ∀ {xs ys} → All (_∉ ys) xs → All (_∉ xs) ys
527 All-∉-swap p = All.¬Any⇒All¬ _ ((All.All¬⇒¬Any p) ∘ Any-∈-swap)