Debian Codegraph @

src/Data/List/Relation/Unary/Any/Properties.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Properties related to Any
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.List.Relation.Unary.Any.Properties where
10
11 open import Data.Bool.Base using (Bool; false; true; T)
12 open import Data.Bool.ListAction using (any)
13 open import Data.Bool.Properties using (T-∨; T-≡)
14 open import Data.Empty using (⊥)
15 open import Data.Fin.Base using (Fin; zero; suc)
16 open import Data.List.Base as List hiding (find; and; or; all; any)
17 open import Data.List.Effectful as List using (monad)
18 open import Data.List.Relation.Unary.Any as Any using (Any; here; there)
19 open import Data.List.Membership.Propositional
20 open import Data.List.Membership.Propositional.Properties.Core
21 using (Any↔; find∘map; map∘find; lose∘find)
22 open import Data.List.Relation.Binary.Pointwise
23 using (Pointwise; []; _∷_)
24 open import Data.Nat.Base using (zero; suc; _<_; z<s; s<s; s≤s)
25 open import Data.Nat.Properties using (_≟_; ≤∧≢⇒<; ≤-refl; m<n⇒m<1+n)
26 open import Data.Maybe.Base using (Maybe; just; nothing)
27 open import Data.Maybe.Relation.Unary.Any as MAny using (just)
28 open import Data.Product.Base as Product
29 using (_×_; _,_; ∃; ∃₂; proj₁; proj₂)
30 open import Data.Product.Function.NonDependent.Propositional
31 using (_×-cong_)
32 import Data.Product.Function.Dependent.Propositional as Σ
33 open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂; [_,_]′)
34 open import Data.Sum.Function.Propositional using (_⊎-cong_)
35 open import Effect.Monad using (RawMonad)
36 open import Function.Base using (_$_; _∘_; flip; const; id; _∘′_)
37 open import Function.Bundles
38 import Function.Properties.Inverse as Inverse
39 open import Function.Related.Propositional as Related using (Kind; Related)
40 open import Level using (Level)
41 open import Relation.Binary.Core using (Rel; REL)
42 open import Relation.Binary.Definitions as B
43 open import Relation.Binary.PropositionalEquality.Core
44 using (_≡_; refl; sym; trans; cong; cong₂; resp)
45 open import Relation.Binary.PropositionalEquality.Properties
46 using (module ≡-Reasoning)
47 open import Relation.Unary as U
48 using (Pred; _⟨×⟩_; _⟨→⟩_) renaming (_⊆_ to _⋐_)
49 open import Relation.Nullary.Decidable.Core
50 using (¬?; _because_; does; yes; no; decidable-stable)
51 open import Relation.Nullary.Negation.Core using (¬_; contradiction)
52 open import Relation.Nullary.Reflects using (invert)
53
54 private
55 open module ListMonad {ℓ} = RawMonad (monad {ℓ = ℓ})
56
57 private
58 variable
59 a b c p q r ℓ : Level
60 A B C : Set a
61 P Q R : Pred A p
62 x y : A
63 xs ys : List A
64
65 ------------------------------------------------------------------------
66 -- Equality properties
67
68 lift-resp : ∀ {_≈_ : Rel A ℓ} → P Respects _≈_ →
69 (Any P) Respects (Pointwise _≈_)
70 lift-resp resp (x≈y ∷ xs≈ys) (here px) = here (resp x≈y px)
71 lift-resp resp (x≈y ∷ xs≈ys) (there pxs) = there (lift-resp resp xs≈ys pxs)
72
73 here-injective : ∀ {p q : P x} → here {P = P} {xs = xs} p ≡ here q → p ≡ q
74 here-injective refl = refl
75
76 there-injective : ∀ {p q : Any P xs} → there {x = x} p ≡ there q → p ≡ q
77 there-injective refl = refl
78
79 ------------------------------------------------------------------------
80 -- Misc
81
82 ¬Any[] : ¬ Any P []
83 ¬Any[] ()
84
85 ------------------------------------------------------------------------
86 -- Any is a congruence
87
88 Any-cong : ∀ {k : Kind} → (∀ x → Related k (P x) (Q x)) →
89 (∀ {z} → Related k (z ∈ xs) (z ∈ ys)) →
90 Related k (Any P xs) (Any Q ys)
91 Any-cong {P = P} {Q = Q} {xs = xs} {ys} P↔Q xs≈ys =
92 Any P xs ↔⟨ Related.SK-sym Any↔ ⟩
93 (∃ λ x → x ∈ xs × P x) ∼⟨ Σ.cong Inverse.↔-refl (xs≈ys ×-cong P↔Q _) ⟩
94 (∃ λ x → x ∈ ys × Q x) ↔⟨ Any↔ ⟩
95 Any Q ys ∎
96 where open Related.EquationalReasoning
97
98 ------------------------------------------------------------------------
99 -- Any.map
100
101 map-cong : (f g : P ⋐ Q) → (∀ {x} (p : P x) → f p ≡ g p) →
102 (p : Any P xs) → Any.map f p ≡ Any.map g p
103 map-cong f g hyp (here p) = cong here (hyp p)
104 map-cong f g hyp (there p) = cong there $ map-cong f g hyp p
105
106 map-id : ∀ (f : P ⋐ P) → (∀ {x} (p : P x) → f p ≡ p) →
107 (p : Any P xs) → Any.map f p ≡ p
108 map-id f hyp (here p) = cong here (hyp p)
109 map-id f hyp (there p) = cong there $ map-id f hyp p
110
111 map-∘ : ∀ (f : Q ⋐ R) (g : P ⋐ Q) (p : Any P xs) →
112 Any.map (f ∘ g) p ≡ Any.map f (Any.map g p)
113 map-∘ f g (here p) = refl
114 map-∘ f g (there p) = cong there $ map-∘ f g p
115
116 ------------------------------------------------------------------------
117 -- Any.lookup
118
119 lookup-result : ∀ (p : Any P xs) → P (Any.lookup p)
120 lookup-result (here px) = px
121 lookup-result (there p) = lookup-result p
122
123 lookup-index : ∀ (p : Any P xs) → P (lookup xs (Any.index p))
124 lookup-index (here px) = px
125 lookup-index (there pxs) = lookup-index pxs
126
127 ------------------------------------------------------------------------
128 -- Swapping
129
130 -- Nested occurrences of Any can sometimes be swapped. See also ×↔.
131
132 module _ {R : REL A B ℓ} where
133
134 swap : Any (λ x → Any (R x) ys) xs → Any (λ y → Any (flip R y) xs) ys
135 swap (here pys) = Any.map here pys
136 swap (there pxys) = Any.map there (swap pxys)
137
138 swap-there : (any : Any (λ x → Any (R x) ys) xs) →
139 swap (Any.map (there {x = x}) any) ≡ there (swap any)
140 swap-there (here pys) = refl
141 swap-there (there pxys) = cong (Any.map there) (swap-there pxys)
142
143 module _ {R : REL A B ℓ} where
144
145 swap-invol : (any : Any (λ x → Any (R x) ys) xs) →
146 swap (swap any) ≡ any
147 swap-invol (here (here px)) = refl
148 swap-invol (here (there pys)) =
149 cong (Any.map there) (swap-invol (here pys))
150 swap-invol (there pxys) =
151 trans (swap-there (swap pxys)) (cong there (swap-invol pxys))
152
153 module _ {R : REL A B ℓ} where
154
155 swap↔ : Any (λ x → Any (R x) ys) xs ↔ Any (λ y → Any (flip R y) xs) ys
156 swap↔ = mk↔ₛ′ swap swap swap-invol swap-invol
157
158 ------------------------------------------------------------------------
159 -- Lemmas relating Any to ⊥
160
161 ⊥↔Any⊥ : ⊥ ↔ Any (const ⊥) xs
162 ⊥↔Any⊥ = mk↔ₛ′ (λ()) (λ p → from p) (λ p → from p) (λ())
163 where
164 from : Any (const ⊥) xs → B
165 from (there p) = from p
166
167 ⊥↔Any[] : ⊥ ↔ Any P []
168 ⊥↔Any[] = mk↔ₛ′ (λ()) (λ()) (λ()) (λ())
169
170 ------------------------------------------------------------------------
171 -- Lemmas relating Any to ⊤
172
173 -- These introduction and elimination rules are not inverses, though.
174
175 any⁺ : ∀ (p : A → Bool) → Any (T ∘ p) xs → T (any p xs)
176 any⁺ p (here px) = Equivalence.from T-∨ (inj₁ px)
177 any⁺ p (there {x = x} pxs) with p x
178 ... | true = _
179 ... | false = any⁺ p pxs
180
181 any⁻ : ∀ (p : A → Bool) xs → T (any p xs) → Any (T ∘ p) xs
182 any⁻ p (x ∷ xs) px∷xs with p x in eq
183 ... | true = here (Equivalence.from T-≡ eq)
184 ... | false = there (any⁻ p xs px∷xs)
185
186 any⇔ : ∀ {p : A → Bool} → Any (T ∘ p) xs ⇔ T (any p xs)
187 any⇔ = mk⇔ (any⁺ _) (any⁻ _ _)
188
189 ------------------------------------------------------------------------
190 -- Sums commute with Any
191
192 Any-⊎⁺ : Any P xs ⊎ Any Q xs → Any (λ x → P x ⊎ Q x) xs
193 Any-⊎⁺ = [ Any.map inj₁ , Any.map inj₂ ]′
194
195 Any-⊎⁻ : Any (λ x → P x ⊎ Q x) xs → Any P xs ⊎ Any Q xs
196 Any-⊎⁻ (here (inj₁ p)) = inj₁ (here p)
197 Any-⊎⁻ (here (inj₂ q)) = inj₂ (here q)
198 Any-⊎⁻ (there p) = Sum.map there there (Any-⊎⁻ p)
199
200 ⊎↔ : (Any P xs ⊎ Any Q xs) ↔ Any (λ x → P x ⊎ Q x) xs
201 ⊎↔ {P = P} {Q = Q} = mk↔ₛ′ Any-⊎⁺ Any-⊎⁻ to∘from from∘to
202 where
203 from∘to : (p : Any P xs ⊎ Any Q xs) → Any-⊎⁻ (Any-⊎⁺ p) ≡ p
204 from∘to (inj₁ (here p)) = refl
205 from∘to (inj₁ (there p)) rewrite from∘to (inj₁ p) = refl
206 from∘to (inj₂ (here q)) = refl
207 from∘to (inj₂ (there q)) rewrite from∘to (inj₂ q) = refl
208
209 to∘from : (p : Any (λ x → P x ⊎ Q x) xs) → Any-⊎⁺ (Any-⊎⁻ p) ≡ p
210 to∘from (here (inj₁ p)) = refl
211 to∘from (here (inj₂ q)) = refl
212 to∘from (there p) with Any-⊎⁻ p | to∘from p
213 ... | inj₁ p | refl = refl
214 ... | inj₂ q | refl = refl
215
216 ------------------------------------------------------------------------
217 -- Products "commute" with Any.
218
219 Any-×⁺ : Any P xs × Any Q ys → Any (λ x → Any (λ y → P x × Q y) ys) xs
220 Any-×⁺ (p , q) = Any.map (λ p → Any.map (λ q → (p , q)) q) p
221
222 Any-×⁻ : Any (λ x → Any (λ y → P x × Q y) ys) xs →
223 Any P xs × Any Q ys
224 Any-×⁻ pq = let x , x∈xs , pq′ = find pq in
225 let y , y∈ys , p , q = find pq′ in
226 lose x∈xs p , lose y∈ys q
227
228 ×↔ : ∀ {xs ys} →
229 (Any P xs × Any Q ys) ↔ Any (λ x → Any (λ y → P x × Q y) ys) xs
230 ×↔ {P = P} {Q = Q} {xs} {ys} = mk↔ₛ′ Any-×⁺ Any-×⁻ to∘from from∘to
231 where
232 open ≡-Reasoning
233
234 from∘to : ∀ pq → Any-×⁻ (Any-×⁺ pq) ≡ pq
235 from∘to (p , q) = let x , x∈xs , px = find p in
236
237 Any-×⁻ (Any-×⁺ (p , q))
238
239 ≡⟨⟩
240
241 (let (x , x∈xs , pq) = find (Any-×⁺ (p , q))
242 (y , y∈ys , p , q) = find pq
243 in lose x∈xs p , lose y∈ys q)
244
245 ≡⟨ cong (λ • → let (x , x∈xs , pq) = •
246 (y , y∈ys , p , q) = find pq
247 in lose x∈xs p , lose y∈ys q)
248 (find∘map p (λ p → Any.map (p ,_) q)) ⟩
249
250 (let (x , x∈xs , p) = find p
251 (y , y∈ys , p , q) = find (Any.map (p ,_) q)
252 in lose x∈xs p , lose y∈ys q)
253
254 ≡⟨ cong (λ • → let (x , x∈xs , _) = find p
255 (y , y∈ys , p , q) = •
256 in lose x∈xs p , lose y∈ys q)
257 (find∘map q (px ,_)) ⟩
258
259 (let (x , x∈xs , p) = find p
260 (y , y∈ys , q) = find q
261 in lose x∈xs p , lose y∈ys q)
262
263 ≡⟨ cong₂ _,_ (lose∘find p) (lose∘find q) ⟩
264
265 (p , q) ∎
266
267 to∘from : ∀ pq → Any-×⁺ (Any-×⁻ pq) ≡ pq
268 to∘from pq =
269 let x , x∈xs , pq′ = find pq
270 y , y∈ys , px , qy = find pq′
271
272 h : P ⋐ λ x → Any (λ y → (P x) × (Q y)) ys
273 h p = Any.map (p ,_) (lose y∈ys qy)
274
275 helper : h px ≡ pq′
276 helper = begin
277 Any.map (px ,_) (lose y∈ys qy)
278 ≡⟨ map-∘ (px ,_) (λ z → resp Q z qy) y∈ys ⟨
279 Any.map (λ z → px , resp Q z qy) y∈ys
280 ≡⟨ map∘find pq′ refl ⟩
281 pq′
282 ∎
283
284 in begin
285 Any-×⁺ (Any-×⁻ pq)
286 ≡⟨⟩
287 Any.map h (lose x∈xs px)
288 ≡⟨ map-∘ h (λ z → resp P z px) x∈xs ⟨
289 Any.map (λ z → Any.map (resp P z px ,_) (lose y∈ys qy)) x∈xs
290 ≡⟨ map∘find pq helper ⟩
291 pq
292 ∎
293
294 ------------------------------------------------------------------------
295 -- Half-applied product commutes with Any.
296
297 module _ {_~_ : REL A B r} where
298
299 Any-Σ⁺ʳ : (∃ λ x → Any (_~ x) xs) → Any (∃ ∘ _~_) xs
300 Any-Σ⁺ʳ (b , here px) = here (b , px)
301 Any-Σ⁺ʳ (b , there pxs) = there (Any-Σ⁺ʳ (b , pxs))
302
303 Any-Σ⁻ʳ : Any (∃ ∘ _~_) xs → ∃ λ x → Any (_~ x) xs
304 Any-Σ⁻ʳ (here (b , x)) = b , here x
305 Any-Σ⁻ʳ (there xs) = Product.map₂ there $ Any-Σ⁻ʳ xs
306
307 ------------------------------------------------------------------------
308 -- Invertible introduction (⁺) and elimination (⁻) rules for various
309 -- list functions
310 ------------------------------------------------------------------------
311
312 ------------------------------------------------------------------------
313 -- singleton
314
315 singleton⁺ : P x → Any P [ x ]
316 singleton⁺ Px = here Px
317
318 singleton⁻ : Any P [ x ] → P x
319 singleton⁻ (here Px) = Px
320
321 ------------------------------------------------------------------------
322 -- map
323
324 module _ {f : A → B} where
325
326 map⁺ : Any (P ∘ f) xs → Any P (List.map f xs)
327 map⁺ (here p) = here p
328 map⁺ (there p) = there $ map⁺ p
329
330 map⁻ : Any P (List.map f xs) → Any (P ∘ f) xs
331 map⁻ {xs = x ∷ xs} (here p) = here p
332 map⁻ {xs = x ∷ xs} (there p) = there $ map⁻ p
333
334 map⁺∘map⁻ : (p : Any P (List.map f xs)) → map⁺ (map⁻ p) ≡ p
335 map⁺∘map⁻ {xs = x ∷ xs} (here p) = refl
336 map⁺∘map⁻ {xs = x ∷ xs} (there p) = cong there (map⁺∘map⁻ p)
337
338 map⁻∘map⁺ : ∀ (P : Pred B p) →
339 (p : Any (P ∘ f) xs) → map⁻ {P = P} (map⁺ p) ≡ p
340 map⁻∘map⁺ P (here p) = refl
341 map⁻∘map⁺ P (there p) = cong there (map⁻∘map⁺ P p)
342
343 map↔ : Any (P ∘ f) xs ↔ Any P (List.map f xs)
344 map↔ = mk↔ₛ′ map⁺ map⁻ map⁺∘map⁻ (map⁻∘map⁺ _)
345
346 gmap : P ⋐ Q ∘ f → Any P ⋐ Any Q ∘ map f
347 gmap g = map⁺ ∘ Any.map g
348
349 ------------------------------------------------------------------------
350 -- mapMaybe
351
352 module _ (f : A → Maybe B) where
353
354 mapMaybe⁺ : ∀ xs → Any (MAny.Any P) (map f xs) → Any P (mapMaybe f xs)
355 mapMaybe⁺ (x ∷ xs) ps with f x | ps
356 ... | nothing | there pxs = mapMaybe⁺ xs pxs
357 ... | just _ | here (just py) = here py
358 ... | just _ | there pxs = there (mapMaybe⁺ xs pxs)
359
360 ------------------------------------------------------------------------
361 -- _++_
362
363 module _ {P : A → Set p} where
364
365 ++⁺ˡ : Any P xs → Any P (xs ++ ys)
366 ++⁺ˡ (here p) = here p
367 ++⁺ˡ (there p) = there (++⁺ˡ p)
368
369 ++⁺ʳ : ∀ xs {ys} → Any P ys → Any P (xs ++ ys)
370 ++⁺ʳ [] p = p
371 ++⁺ʳ (x ∷ xs) p = there (++⁺ʳ xs p)
372
373 ++⁻ : ∀ xs {ys} → Any P (xs ++ ys) → Any P xs ⊎ Any P ys
374 ++⁻ [] p = inj₂ p
375 ++⁻ (x ∷ xs) (here p) = inj₁ (here p)
376 ++⁻ (x ∷ xs) (there p) = Sum.map there id (++⁻ xs p)
377
378 ++⁺∘++⁻ : ∀ xs {ys} (p : Any P (xs ++ ys)) → [ ++⁺ˡ , ++⁺ʳ xs ]′ (++⁻ xs p) ≡ p
379 ++⁺∘++⁻ [] p = refl
380 ++⁺∘++⁻ (x ∷ xs) (here p) = refl
381 ++⁺∘++⁻ (x ∷ xs) (there p) with ih ← ++⁺∘++⁻ xs p | ++⁻ xs p
382 ... | inj₁ _ = cong there ih
383 ... | inj₂ _ = cong there ih
384
385 ++⁻∘++⁺ : ∀ xs {ys} (p : Any P xs ⊎ Any P ys) →
386 ++⁻ xs ([ ++⁺ˡ , ++⁺ʳ xs ]′ p) ≡ p
387 ++⁻∘++⁺ [] (inj₂ p) = refl
388 ++⁻∘++⁺ (x ∷ xs) (inj₁ (here p)) = refl
389 ++⁻∘++⁺ (x ∷ xs) {ys} (inj₁ (there p)) rewrite ++⁻∘++⁺ xs {ys} (inj₁ p) = refl
390 ++⁻∘++⁺ (x ∷ xs) (inj₂ p) rewrite ++⁻∘++⁺ xs (inj₂ p) = refl
391
392 ++↔ : ∀ {xs ys} → (Any P xs ⊎ Any P ys) ↔ Any P (xs ++ ys)
393 ++↔ {xs = xs} = mk↔ₛ′ [ ++⁺ˡ , ++⁺ʳ xs ]′ (++⁻ xs) (++⁺∘++⁻ xs) (++⁻∘++⁺ xs)
394
395 ++-comm : ∀ xs ys → Any P (xs ++ ys) → Any P (ys ++ xs)
396 ++-comm xs ys = [ ++⁺ʳ ys , ++⁺ˡ ]′ ∘ ++⁻ xs
397
398 ++-comm∘++-comm : ∀ xs {ys} (p : Any P (xs ++ ys)) →
399 ++-comm ys xs (++-comm xs ys p) ≡ p
400 ++-comm∘++-comm [] {ys} p
401 rewrite ++⁻∘++⁺ ys {ys = []} (inj₁ p) = refl
402 ++-comm∘++-comm (x ∷ xs) {ys} (here p)
403 rewrite ++⁻∘++⁺ ys {ys = x ∷ xs} (inj₂ (here p)) = refl
404 ++-comm∘++-comm (x ∷ xs) (there p) with ++⁻ xs p | ++-comm∘++-comm xs p
405 ++-comm∘++-comm (x ∷ xs) {ys} (there .([ ++⁺ʳ xs , ++⁺ˡ ]′ (++⁻ ys (++⁺ʳ ys p))))
406 | inj₁ p | refl
407 rewrite ++⁻∘++⁺ ys (inj₂ p)
408 | ++⁻∘++⁺ ys (inj₂ $ there {x = x} p) = refl
409 ++-comm∘++-comm (x ∷ xs) {ys} (there .([ ++⁺ʳ xs , ++⁺ˡ ]′ (++⁻ ys (++⁺ˡ p))))
410 | inj₂ p | refl
411 rewrite ++⁻∘++⁺ ys {ys = xs} (inj₁ p)
412 | ++⁻∘++⁺ ys {ys = x ∷ xs} (inj₁ p) = refl
413
414 ++↔++ : ∀ xs ys → Any P (xs ++ ys) ↔ Any P (ys ++ xs)
415 ++↔++ xs ys = mk↔ₛ′ (++-comm xs ys) (++-comm ys xs)
416 (++-comm∘++-comm ys) (++-comm∘++-comm xs)
417
418 ++-insert : ∀ xs {ys} → P x → Any P (xs ++ [ x ] ++ ys)
419 ++-insert xs Px = ++⁺ʳ xs (++⁺ˡ (singleton⁺ Px))
420
421 ------------------------------------------------------------------------
422 -- concat
423
424 module _ {P : A → Set p} where
425
426 concat⁺ : ∀ {xss} → Any (Any P) xss → Any P (concat xss)
427 concat⁺ (here p) = ++⁺ˡ p
428 concat⁺ (there {x = xs} p) = ++⁺ʳ xs (concat⁺ p)
429
430 concat⁻ : ∀ xss → Any P (concat xss) → Any (Any P) xss
431 concat⁻ ([] ∷ xss) p = there $ concat⁻ xss p
432 concat⁻ ((x ∷ xs) ∷ xss) (here p) = here (here p)
433 concat⁻ ((x ∷ xs) ∷ xss) (there p) with concat⁻ (xs ∷ xss) p
434 ... | here p′ = here (there p′)
435 ... | there p′ = there p′
436
437 concat⁻∘++⁺ˡ : ∀ {xs} xss (p : Any P xs) →
438 concat⁻ (xs ∷ xss) (++⁺ˡ p) ≡ here p
439 concat⁻∘++⁺ˡ xss (here p) = refl
440 concat⁻∘++⁺ˡ xss (there p) rewrite concat⁻∘++⁺ˡ xss p = refl
441
442 concat⁻∘++⁺ʳ : ∀ xs xss (p : Any P (concat xss)) →
443 concat⁻ (xs ∷ xss) (++⁺ʳ xs p) ≡ there (concat⁻ xss p)
444 concat⁻∘++⁺ʳ [] xss p = refl
445 concat⁻∘++⁺ʳ (x ∷ xs) xss p rewrite concat⁻∘++⁺ʳ xs xss p = refl
446
447 concat⁺∘concat⁻ : ∀ xss (p : Any P (concat xss)) →
448 concat⁺ (concat⁻ xss p) ≡ p
449 concat⁺∘concat⁻ ([] ∷ xss) p = concat⁺∘concat⁻ xss p
450 concat⁺∘concat⁻ ((x ∷ xs) ∷ xss) (here p) = refl
451 concat⁺∘concat⁻ ((x ∷ xs) ∷ xss) (there p)
452 with p | concat⁻ (xs ∷ xss) p | concat⁺∘concat⁻ (xs ∷ xss) p
453 ... | .(++⁺ˡ p′) | here p′ | refl = refl
454 ... | .(++⁺ʳ xs (concat⁺ p′)) | there p′ | refl = refl
455
456 concat⁻∘concat⁺ : ∀ {xss} (p : Any (Any P) xss) → concat⁻ xss (concat⁺ p) ≡ p
457 concat⁻∘concat⁺ (here p) = concat⁻∘++⁺ˡ _ p
458 concat⁻∘concat⁺ (there {x = xs} {xs = xss} p)
459 rewrite concat⁻∘++⁺ʳ xs xss (concat⁺ p) =
460 cong there $ concat⁻∘concat⁺ p
461
462 concat↔ : ∀ {xss} → Any (Any P) xss ↔ Any P (concat xss)
463 concat↔ {xss} = mk↔ₛ′ concat⁺ (concat⁻ xss) (concat⁺∘concat⁻ xss) concat⁻∘concat⁺
464
465 ------------------------------------------------------------------------
466 -- concatMap
467
468 module _ (f : A → List B) {p} {P : Pred B p} where
469
470 concatMap⁺ : Any (Any P ∘ f) xs → Any P (concatMap f xs)
471 concatMap⁺ = concat⁺ ∘ map⁺
472
473 concatMap⁻ : Any P (concatMap f xs) → Any (Any P ∘ f) xs
474 concatMap⁻ = map⁻ ∘ concat⁻ _
475
476 ------------------------------------------------------------------------
477 -- cartesianProductWith
478
479 module _ (f : A → B → C) where
480
481 cartesianProductWith⁺ : (∀ {x y} → P x → Q y → R (f x y)) →
482 Any P xs → Any Q ys →
483 Any R (cartesianProductWith f xs ys)
484 cartesianProductWith⁺ pres (here px) qys = ++⁺ˡ (map⁺ (Any.map (pres px) qys))
485 cartesianProductWith⁺ pres (there qxs) qys = ++⁺ʳ _ (cartesianProductWith⁺ pres qxs qys)
486
487 cartesianProductWith⁻ : (∀ {x y} → R (f x y) → P x × Q y) → ∀ xs ys →
488 Any R (cartesianProductWith f xs ys) →
489 Any P xs × Any Q ys
490 cartesianProductWith⁻ resp (x ∷ xs) ys Rxsys with ++⁻ (map (f x) ys) Rxsys
491 ... | inj₁ Rfxys = let Rxys = map⁻ Rfxys
492 in here (proj₁ (resp (proj₂ (Any.satisfied Rxys)))) , Any.map (proj₂ ∘ resp) Rxys
493 ... | inj₂ Rc = let pxs , qys = cartesianProductWith⁻ resp xs ys Rc
494 in there pxs , qys
495
496 ------------------------------------------------------------------------
497 -- cartesianProduct
498
499 cartesianProduct⁺ : Any P xs → Any Q ys →
500 Any (P ⟨×⟩ Q) (cartesianProduct xs ys)
501 cartesianProduct⁺ = cartesianProductWith⁺ _,_ _,_
502
503 cartesianProduct⁻ : ∀ xs ys → Any (P ⟨×⟩ Q) (cartesianProduct xs ys) →
504 Any P xs × Any Q ys
505 cartesianProduct⁻ = cartesianProductWith⁻ _,_ id
506
507 ------------------------------------------------------------------------
508 -- applyUpTo
509
510 applyUpTo⁺ : ∀ f {i n} → P (f i) → i < n → Any P (applyUpTo f n)
511 applyUpTo⁺ _ p z<s = here p
512 applyUpTo⁺ f p (s<s i<n@(s≤s _)) =
513 there (applyUpTo⁺ (f ∘ suc) p i<n)
514
515 applyUpTo⁻ : ∀ f {n} → Any P (applyUpTo f n) →
516 ∃ λ i → i < n × P (f i)
517 applyUpTo⁻ f {suc n} (here p) = zero , z<s , p
518 applyUpTo⁻ f {suc n} (there p) =
519 let i , i<n , q = applyUpTo⁻ (f ∘ suc) p in suc i , s<s i<n , q
520
521 ------------------------------------------------------------------------
522 -- applyDownFrom
523
524 applyDownFrom⁺ : ∀ f {i n} → P (f i) → i < n → Any P (applyDownFrom f n)
525 applyDownFrom⁺ f {i} {suc n} p (s≤s i≤n) with i ≟ n
526 ... | yes refl = here p
527 ... | no i≢n = there (applyDownFrom⁺ f p (≤∧≢⇒< i≤n i≢n))
528
529 applyDownFrom⁻ : ∀ f {n} → Any P (applyDownFrom f n) →
530 ∃ λ i → i < n × P (f i)
531 applyDownFrom⁻ f {suc n} (here p) = n , ≤-refl , p
532 applyDownFrom⁻ f {suc n} (there p) =
533 let i , i<n , q = applyDownFrom⁻ f p in i , m<n⇒m<1+n i<n , q
534
535 ------------------------------------------------------------------------
536 -- tabulate
537
538 tabulate⁺ : ∀ {n} {f : Fin n → A} i → P (f i) → Any P (tabulate f)
539 tabulate⁺ zero p = here p
540 tabulate⁺ (suc i) p = there (tabulate⁺ i p)
541
542 tabulate⁻ : ∀ {n} {f : Fin n → A} → Any P (tabulate f) → ∃ λ i → P (f i)
543 tabulate⁻ {n = suc _} (here p) = zero , p
544 tabulate⁻ {n = suc _} (there p) = Product.map suc id (tabulate⁻ p)
545
546 ------------------------------------------------------------------------
547 -- filter
548
549 module _ (Q? : U.Decidable Q) where
550
551 filter⁺ : (p : Any P xs) → Any P (filter Q? xs) ⊎ ¬ Q (Any.lookup p)
552 filter⁺ {xs = x ∷ _} (here px) with Q? x
553 ... | true because _ = inj₁ (here px)
554 ... | false because [¬Qx] = inj₂ (invert [¬Qx])
555 filter⁺ {xs = x ∷ _} (there p) with does (Q? x)
556 ... | true = Sum.map₁ there (filter⁺ p)
557 ... | false = filter⁺ p
558
559 filter⁻ : Any P (filter Q? xs) → Any P xs
560 filter⁻ {xs = x ∷ xs} p with does (Q? x) | p
561 ... | true | here px = here px
562 ... | true | there pxs = there (filter⁻ pxs)
563 ... | false | pxs = there (filter⁻ pxs)
564
565 ------------------------------------------------------------------------
566 -- derun and deduplicate
567
568 module _ {R : Rel A r} (R? : B.Decidable R) where
569
570 private
571 derun⁺-aux : ∀ x xs → P Respects R → P x → Any P (derun R? (x ∷ xs))
572 derun⁺-aux x [] resp Px = here Px
573 derun⁺-aux x (y ∷ xs) resp Px with R? x y
574 ... | true because [Rxy] = derun⁺-aux y xs resp (resp (invert [Rxy]) Px)
575 ... | false because _ = here Px
576
577 derun⁺ : P Respects R → Any P xs → Any P (derun R? xs)
578 derun⁺ {xs = x ∷ xs} resp (here px) = derun⁺-aux x xs resp px
579 derun⁺ {xs = x ∷ y ∷ xs} resp (there pxs) with does (R? x y)
580 ... | true = derun⁺ resp pxs
581 ... | false = there (derun⁺ resp pxs)
582
583 deduplicate⁺ : ∀ {xs} → P Respects (flip R) → Any P xs → Any P (deduplicate R? xs)
584 deduplicate⁺ {xs = x ∷ xs} resp (here px) = here px
585 deduplicate⁺ {xs = x ∷ xs} resp (there pxs)
586 with filter⁺ (¬? ∘ R? x) (deduplicate⁺ resp pxs)
587 ... | inj₁ p = there p
588 ... | inj₂ ¬¬q =
589 let q = decidable-stable (R? x (Any.lookup (deduplicate⁺ resp pxs))) ¬¬q
590 in here (resp q (lookup-result (deduplicate⁺ resp pxs)))
591
592 private
593 derun⁻-aux : Any P (derun R? (x ∷ xs)) → Any P (x ∷ xs)
594 derun⁻-aux {x = x} {[]} (here px) = here px
595 derun⁻-aux {x = x} {y ∷ _} p[x∷y∷xs] with does (R? x y) | p[x∷y∷xs]
596 ... | true | p[y∷xs] = there (derun⁻-aux p[y∷xs])
597 ... | false | here px = here px
598 ... | false | there p[y∷xs]! = there (derun⁻-aux p[y∷xs]!)
599
600 derun⁻ : Any P (derun R? xs) → Any P xs
601 derun⁻ {xs = x ∷ xs} p[x∷xs]! = derun⁻-aux p[x∷xs]!
602
603 deduplicate⁻ : Any P (deduplicate R? xs) → Any P xs
604 deduplicate⁻ {xs = x ∷ _} (here px) = here px
605 deduplicate⁻ {xs = x ∷ _} (there pxs!) = there (deduplicate⁻ (filter⁻ (¬? ∘ R? x) pxs!))
606
607 ------------------------------------------------------------------------
608 -- mapWith∈.
609
610 module _ {P : B → Set p} where
611
612 mapWith∈⁺ : ∀ {xs : List A} (f : ∀ {x} → x ∈ xs → B) →
613 (∃₂ λ x (x∈xs : x ∈ xs) → P (f x∈xs)) →
614 Any P (mapWith∈ xs f)
615 mapWith∈⁺ f (_ , here refl , p) = here p
616 mapWith∈⁺ f (_ , there x∈xs , p) =
617 there $ mapWith∈⁺ (f ∘ there) (_ , x∈xs , p)
618
619 mapWith∈⁻ : ∀ (xs : List A) (f : ∀ {x} → x ∈ xs → B) →
620 Any P (mapWith∈ xs f) →
621 ∃₂ λ x (x∈xs : x ∈ xs) → P (f x∈xs)
622 mapWith∈⁻ (y ∷ xs) f (here p) = (y , here refl , p)
623 mapWith∈⁻ (y ∷ xs) f (there p) =
624 Product.map₂ (Product.map there id) $ mapWith∈⁻ xs (f ∘ there) p
625
626 mapWith∈↔ : ∀ {xs : List A} {f : ∀ {x} → x ∈ xs → B} →
627 (∃₂ λ x (x∈xs : x ∈ xs) → P (f x∈xs)) ↔ Any P (mapWith∈ xs f)
628 mapWith∈↔ = mk↔ₛ′ (mapWith∈⁺ _) (mapWith∈⁻ _ _) (to∘from _ _) (from∘to _)
629 where
630 from∘to : ∀ {xs : List A} (f : ∀ {x} → x ∈ xs → B)
631 (p : ∃₂ λ x (x∈xs : x ∈ xs) → P (f x∈xs)) →
632 mapWith∈⁻ xs f (mapWith∈⁺ f p) ≡ p
633 from∘to f (_ , here refl , p) = refl
634 from∘to f (_ , there x∈xs , p)
635 rewrite from∘to (f ∘ there) (_ , x∈xs , p) = refl
636
637 to∘from : ∀ (xs : List A) (f : ∀ {x} → x ∈ xs → B)
638 (p : Any P (mapWith∈ xs f)) →
639 mapWith∈⁺ f (mapWith∈⁻ xs f p) ≡ p
640 to∘from (y ∷ xs) f (here p) = refl
641 to∘from (y ∷ xs) f (there p) =
642 cong there $ to∘from xs (f ∘ there) p
643
644 ------------------------------------------------------------------------
645 -- reverse
646
647 reverseAcc⁺ : ∀ acc xs → Any P acc ⊎ Any P xs → Any P (reverseAcc acc xs)
648 reverseAcc⁺ acc [] (inj₁ ps) = ps
649 reverseAcc⁺ acc (x ∷ xs) (inj₁ ps) = reverseAcc⁺ (x ∷ acc) xs (inj₁ (there ps))
650 reverseAcc⁺ acc (x ∷ xs) (inj₂ (here px)) = reverseAcc⁺ (x ∷ acc) xs (inj₁ (here px))
651 reverseAcc⁺ acc (x ∷ xs) (inj₂ (there y)) = reverseAcc⁺ (x ∷ acc) xs (inj₂ y)
652
653 reverseAcc⁻ : ∀ acc xs → Any P (reverseAcc acc xs) → Any P acc ⊎ Any P xs
654 reverseAcc⁻ acc [] ps = inj₁ ps
655 reverseAcc⁻ acc (x ∷ xs) ps with reverseAcc⁻ (x ∷ acc) xs ps
656 ... | inj₁ (here px) = inj₂ (here px)
657 ... | inj₁ (there pxs) = inj₁ pxs
658 ... | inj₂ pxs = inj₂ (there pxs)
659
660 reverse⁺ : Any P xs → Any P (reverse xs)
661 reverse⁺ ps = reverseAcc⁺ [] _ (inj₂ ps)
662
663 reverse⁻ : Any P (reverse xs) → Any P xs
664 reverse⁻ ps with inj₂ pxs ← reverseAcc⁻ [] _ ps = pxs
665
666 ------------------------------------------------------------------------
667 -- pure
668
669 pure⁺ : P x → Any P (pure x)
670 pure⁺ = here
671
672 pure⁻ : Any P (pure x) → P x
673 pure⁻ (here p) = p
674
675 pure⁺∘pure⁻ : (p : Any P (pure x)) → pure⁺ (pure⁻ p) ≡ p
676 pure⁺∘pure⁻ (here p) = refl
677
678 pure⁻∘pure⁺ : (p : P x) → pure⁻ {P = P} (pure⁺ p) ≡ p
679 pure⁻∘pure⁺ p = refl
680
681 pure↔ : P x ↔ Any P (pure x)
682 pure↔ {P = P} = mk↔ₛ′ pure⁺ pure⁻ pure⁺∘pure⁻ (pure⁻∘pure⁺ {P = P})
683
684 ------------------------------------------------------------------------
685 -- _∷_
686
687 ∷↔ : (P : Pred A p) → (P x ⊎ Any P xs) ↔ Any P (x ∷ xs)
688 ∷↔ {x = x} {xs} P =
689 (P x ⊎ Any P xs) ↔⟨ pure↔ ⊎-cong (Any P xs ∎) ⟩
690 (Any P [ x ] ⊎ Any P xs) ↔⟨ ++↔ ⟩
691 Any P (x ∷ xs) ∎
692 where open Related.EquationalReasoning
693
694 ------------------------------------------------------------------------
695 -- _>>=_
696
697 module _ {A B : Set ℓ} {P : B → Set p} {f : A → List B} where
698
699 >>=↔ : Any (Any P ∘ f) xs ↔ Any P (xs >>= f)
700 >>=↔ {xs = xs} =
701 Any (Any P ∘ f) xs ↔⟨ map↔ ⟩
702 Any (Any P) (List.map f xs) ↔⟨ concat↔ ⟩
703 Any P (xs >>= f) ∎
704 where open Related.EquationalReasoning
705
706 ------------------------------------------------------------------------
707 -- _⊛_
708
709 ⊛↔ : ∀ {P : B → Set ℓ} {fs : List (A → B)} {xs : List A} →
710 Any (λ f → Any (P ∘ f) xs) fs ↔ Any P (fs ⊛ xs)
711 ⊛↔ {P = P} {fs} {xs} =
712 Any (λ f → Any (P ∘ f) xs) fs ↔⟨ Any-cong (λ _ → Any-cong (λ _ → pure↔) (_ ∎)) (_ ∎) ⟩
713 Any (λ f → Any (Any P ∘ pure ∘ f) xs) fs ↔⟨ Any-cong (λ _ → >>=↔ ) (_ ∎) ⟩
714 Any (λ f → Any P (xs >>= pure ∘ f)) fs ↔⟨ >>=↔ ⟩
715 Any P (fs >>= λ f → xs >>= λ x → pure (f x)) ≡⟨ cong (Any P) (List.Applicative.unfold-⊛ fs xs) ⟨
716 Any P (fs ⊛ xs) ∎
717 where open Related.EquationalReasoning
718
719
720 -- An alternative introduction rule for _⊛_
721
722 ⊛⁺′ : ∀ {P : Pred A ℓ} {Q : Pred B ℓ} {fs : List (A → B)} {xs} →
723 Any (P ⟨→⟩ Q) fs → Any P xs → Any Q (fs ⊛ xs)
724 ⊛⁺′ pq p = Inverse.to ⊛↔ (Any.map (λ pq → Any.map (λ {x} → pq {x}) p) pq)
725
726 ------------------------------------------------------------------------
727 -- _⊗_
728
729 ⊗↔ : {P : A × B → Set ℓ} {xs : List A} {ys : List B} →
730 Any (λ x → Any (λ y → P (x , y)) ys) xs ↔ Any P (xs ⊗ ys)
731 ⊗↔ {P = P} {xs} {ys} =
732 Any (λ x → Any (λ y → P (x , y)) ys) xs ↔⟨ pure↔ ⟩
733 Any (λ _,_ → Any (λ x → Any (λ y → P (x , y)) ys) xs) (pure _,_) ↔⟨ ⊛↔ ⟩
734 Any (λ x, → Any (P ∘ x,) ys) (pure _,_ ⊛ xs) ↔⟨ ⊛↔ ⟩
735 Any P (pure _,_ ⊛ xs ⊛ ys) ≡⟨ cong (Any P ∘′ (_⊛ ys)) (List.Applicative.unfold-<$> _,_ xs) ⟨
736 Any P (xs ⊗ ys) ∎
737 where open Related.EquationalReasoning
738
739 ⊗↔′ : {P : A → Set ℓ} {Q : B → Set ℓ} {xs : List A} {ys : List B} →
740 (Any P xs × Any Q ys) ↔ Any (P ⟨×⟩ Q) (xs ⊗ ys)
741 ⊗↔′ {P = P} {Q} {xs} {ys} =
742 (Any P xs × Any Q ys) ↔⟨ ×↔ ⟩
743 Any (λ x → Any (λ y → P x × Q y) ys) xs ↔⟨ ⊗↔ ⟩
744 Any (P ⟨×⟩ Q) (xs ⊗ ys) ∎
745 where open Related.EquationalReasoning
746
747 map-with-∈⁺ = mapWith∈⁺
748 {-# WARNING_ON_USAGE map-with-∈⁺
749 "Warning: map-with-∈⁺ was deprecated in v2.0.
750 Please use mapWith∈⁺ instead."
751 #-}
752 map-with-∈⁻ = mapWith∈⁻
753 {-# WARNING_ON_USAGE map-with-∈⁻
754 "Warning: map-with-∈⁻ was deprecated in v2.0.
755 Please use mapWith∈⁻ instead."
756 #-}
757 map-with-∈↔ = mapWith∈↔
758 {-# WARNING_ON_USAGE map-with-∈↔
759 "Warning: map-with-∈↔ was deprecated in v2.0.
760 Please use mapWith∈↔ instead."
761 #-}