Any.agda 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. ------------------------------------------------------------------------
  2. -- Properties related to Any
  3. ------------------------------------------------------------------------
  4. -- The other modules under Data.List.Any also contain properties
  5. -- related to Any.
  6. module Any where
  7. open import Algebra
  8. import Algebra.Definitions as FP
  9. open import Category.Monad
  10. open import Data.Bool
  11. open import Data.Bool.Properties
  12. open import Data.Empty
  13. open import Data.List as List
  14. open import Data.List.Relation.Unary.Any as Any using (Any; here; there)
  15. import Data.List.Categorical
  16. open import Data.Product as Prod hiding (swap)
  17. open import Data.Product.Function.NonDependent.Propositional
  18. using (_×-cong_)
  19. open import Data.Product.Relation.Binary.Pointwise.NonDependent
  20. import Data.Product.Function.Dependent.Propositional as Σ
  21. open import Data.Sum as Sum using (_⊎_; inj₁; inj₂; [_,_]′)
  22. open import Data.Sum.Relation.Binary.Pointwise
  23. open import Data.Sum.Function.Propositional using (_⊎-cong_)
  24. open import Function using (_$_; _$′_; _∘_; id; flip; const)
  25. open import Function.Equality using (_⟨$⟩_)
  26. open import Function.Equivalence as Eq using (_⇔_; module Equivalence)
  27. open import Function.Inverse as Inv using (_↔_; module Inverse)
  28. open import Function.Related as Related using (Related; SK-sym)
  29. open import Function.Related.TypeIsomorphisms
  30. open import Level
  31. open import Relation.Binary hiding (_⇔_)
  32. import Relation.Binary.HeterogeneousEquality as H
  33. open import Relation.Binary.PropositionalEquality as P
  34. using (_≡_; refl; inspect) renaming ([_] to P[_])
  35. open import Relation.Unary using (_⟨×⟩_; _⟨→⟩_) renaming (_⊆_ to _⋐_)
  36. open import Data.List.Membership.Propositional
  37. open import Data.List.Relation.Binary.BagAndSetEquality
  38. open Related.EquationalReasoning
  39. private
  40. module ×⊎ {k ℓ} = CommutativeSemiring (×-⊎-commutativeSemiring k ℓ)
  41. open module ListMonad {ℓ} =
  42. RawMonad (Data.List.Categorical.monad {ℓ = ℓ})
  43. ------------------------------------------------------------------------
  44. -- Some lemmas related to map, find and lose
  45. -- Any.map is functorial.
  46. map-id : ∀ {a p} {A : Set a} {P : A → Set p} (f : P ⋐ P) {xs} →
  47. (∀ {x} (p : P x) → f p ≡ p) →
  48. (p : Any P xs) → Any.map f p ≡ p
  49. map-id f hyp (here p) = P.cong here (hyp p)
  50. map-id f hyp (there p) = P.cong there $ map-id f hyp p
  51. map-∘ : ∀ {a p q r}
  52. {A : Set a} {P : A → Set p} {Q : A → Set q} {R : A → Set r}
  53. (f : Q ⋐ R) (g : P ⋐ Q)
  54. {xs} (p : Any P xs) →
  55. Any.map (f ∘ g) p ≡ Any.map f (Any.map g p)
  56. map-∘ f g (here p) = refl
  57. map-∘ f g (there p) = P.cong there $ map-∘ f g p
  58. -- Lemmas relating map and find.
  59. map∘find : ∀ {a p} {A : Set a} {P : A → Set p} {xs}
  60. (p : Any P xs) → let p′ = find p in
  61. {f : _≡_ (proj₁ p′) ⋐ P} →
  62. f refl ≡ proj₂ (proj₂ p′) →
  63. Any.map f (proj₁ (proj₂ p′)) ≡ p
  64. map∘find (here p) hyp = P.cong here hyp
  65. map∘find (there p) hyp = P.cong there (map∘find p hyp)
  66. find∘map : ∀ {a p q} {A : Set a} {P : A → Set p} {Q : A → Set q}
  67. {xs : List A} (p : Any P xs) (f : P ⋐ Q) →
  68. find (Any.map f p) ≡ Prod.map id (Prod.map id f) (find p)
  69. find∘map (here p) f = refl
  70. find∘map (there p) f rewrite find∘map p f = refl
  71. -- find satisfies a simple equality when the predicate is a
  72. -- propositional equality.
  73. find-∈ : ∀ {a} {A : Set a} {x : A} {xs : List A} (x∈xs : x ∈ xs) →
  74. find x∈xs ≡ (x , x∈xs , refl)
  75. find-∈ (here refl) = refl
  76. find-∈ (there x∈xs) rewrite find-∈ x∈xs = refl
  77. private
  78. -- find and lose are inverses (more or less).
  79. lose∘find : ∀ {a p} {A : Set a} {P : A → Set p} {xs : List A}
  80. (p : Any P xs) →
  81. uncurry′ lose (proj₂ (find p)) ≡ p
  82. lose∘find p = map∘find p P.refl
  83. find∘lose : ∀ {a p} {A : Set a} (P : A → Set p) {x xs}
  84. (x∈xs : x ∈ xs) (pp : P x) →
  85. find {P = P} (lose x∈xs pp) ≡ (x , x∈xs , pp)
  86. find∘lose P x∈xs p
  87. rewrite find∘map x∈xs (flip (P.subst P) p)
  88. | find-∈ x∈xs
  89. = refl
  90. -- Any can be expressed using _∈_.
  91. Any↔ : ∀ {a p} {A : Set a} {P : A → Set p} {xs} →
  92. (∃ λ x → x ∈ xs × P x) ↔ Any P xs
  93. Any↔ {P = P} {xs} = record
  94. { to = P.→-to-⟶ to
  95. ; from = P.→-to-⟶ (find {P = P})
  96. ; inverse-of = record
  97. { left-inverse-of = λ p →
  98. find∘lose P (proj₁ (proj₂ p)) (proj₂ (proj₂ p))
  99. ; right-inverse-of = lose∘find
  100. }
  101. }
  102. where
  103. to : (∃ λ x → x ∈ xs × P x) → Any P xs
  104. to = uncurry′ lose ∘ proj₂
  105. ------------------------------------------------------------------------
  106. -- Any is a congruence
  107. Any-cong : ∀ {k ℓ} {A : Set ℓ} {P₁ P₂ : A → Set ℓ} {xs₁ xs₂ : List A} →
  108. (∀ x → Related k (P₁ x) (P₂ x)) → xs₁ ∼[ k ] xs₂ →
  109. Related k (Any P₁ xs₁) (Any P₂ xs₂)
  110. Any-cong {P₁ = P₁} {P₂} {xs₁} {xs₂} P₁↔P₂ xs₁≈xs₂ =
  111. Any P₁ xs₁ ↔⟨ SK-sym $ Any↔ {P = P₁} ⟩
  112. (∃ λ x → x ∈ xs₁ × P₁ x) ∼⟨ Σ.cong Inv.id (xs₁≈xs₂ ×-cong P₁↔P₂ _) ⟩
  113. (∃ λ x → x ∈ xs₂ × P₂ x) ↔⟨ Any↔ {P = P₂} ⟩
  114. Any P₂ xs₂ ∎
  115. ------------------------------------------------------------------------
  116. -- Swapping
  117. -- Nested occurrences of Any can sometimes be swapped. See also ×↔.
  118. swap : ∀ {ℓ} {A B : Set ℓ} {P : A → B → Set ℓ} {xs ys} →
  119. Any (λ x → Any (P x) ys) xs ↔ Any (λ y → Any (flip P y) xs) ys
  120. swap {ℓ} {P = P} {xs} {ys} =
  121. Any (λ x → Any (P x) ys) xs ↔⟨ SK-sym Any↔ ⟩
  122. (∃ λ x → x ∈ xs × Any (P x) ys) ↔⟨ SK-sym $ Σ.cong Inv.id (Σ.cong Inv.id Any↔) ⟩
  123. (∃ λ x → x ∈ xs × ∃ λ y → y ∈ ys × P x y) ↔⟨ Σ.cong Inv.id (∃∃↔∃∃ _) ⟩
  124. (∃₂ λ x y → x ∈ xs × y ∈ ys × P x y) ↔⟨ ∃∃↔∃∃ _ ⟩
  125. (∃₂ λ y x → x ∈ xs × y ∈ ys × P x y) ↔⟨ Σ.cong Inv.id (Σ.cong Inv.id (∃∃↔∃∃ _)) ⟩
  126. (∃₂ λ y x → y ∈ ys × x ∈ xs × P x y) ↔⟨ Σ.cong Inv.id (∃∃↔∃∃ _) ⟩
  127. (∃ λ y → y ∈ ys × ∃ λ x → x ∈ xs × P x y) ↔⟨ Σ.cong Inv.id (Σ.cong Inv.id Any↔) ⟩
  128. (∃ λ y → y ∈ ys × Any (flip P y) xs) ↔⟨ Any↔ ⟩
  129. Any (λ y → Any (flip P y) xs) ys ∎
  130. ------------------------------------------------------------------------
  131. -- Lemmas relating Any to ⊥
  132. ⊥↔Any⊥ : ∀ {a} {A : Set a} {xs : List A} → ⊥ ↔ Any (const ⊥) xs
  133. ⊥↔Any⊥ {A = A} = record
  134. { to = P.→-to-⟶ (λ ())
  135. ; from = P.→-to-⟶ (λ p → from p)
  136. ; inverse-of = record
  137. { left-inverse-of = λ ()
  138. ; right-inverse-of = λ p → from p
  139. }
  140. }
  141. where
  142. from : {xs : List A} → Any (const ⊥) xs → ∀ {b} {B : Set b} → B
  143. from (here ())
  144. from (there p) = from p
  145. ⊥↔Any[] : ∀ {a} {A : Set a} {P : A → Set} → ⊥ ↔ Any P []
  146. ⊥↔Any[] = record
  147. { to = P.→-to-⟶ (λ ())
  148. ; from = P.→-to-⟶ (λ ())
  149. ; inverse-of = record
  150. { left-inverse-of = λ ()
  151. ; right-inverse-of = λ ()
  152. }
  153. }
  154. ------------------------------------------------------------------------
  155. -- Lemmas relating Any to sums and products
  156. -- Sums commute with Any (for a fixed list).
  157. ⊎↔ : ∀ {a p q} {A : Set a} {P : A → Set p} {Q : A → Set q} {xs} →
  158. (Any P xs ⊎ Any Q xs) ↔ Any (λ x → P x ⊎ Q x) xs
  159. ⊎↔ {P = P} {Q} = record
  160. { to = P.→-to-⟶ to
  161. ; from = P.→-to-⟶ from
  162. ; inverse-of = record
  163. { left-inverse-of = from∘to
  164. ; right-inverse-of = to∘from
  165. }
  166. }
  167. where
  168. to : ∀ {xs} → Any P xs ⊎ Any Q xs → Any (λ x → P x ⊎ Q x) xs
  169. to = [ Any.map inj₁ , Any.map inj₂ ]′
  170. from : ∀ {xs} → Any (λ x → P x ⊎ Q x) xs → Any P xs ⊎ Any Q xs
  171. from (here (inj₁ p)) = inj₁ (here p)
  172. from (here (inj₂ q)) = inj₂ (here q)
  173. from (there p) = Sum.map there there (from p)
  174. from∘to : ∀ {xs} (p : Any P xs ⊎ Any Q xs) → from (to p) ≡ p
  175. from∘to (inj₁ (here p)) = P.refl
  176. from∘to (inj₁ (there p)) rewrite from∘to (inj₁ p) = P.refl
  177. from∘to (inj₂ (here q)) = P.refl
  178. from∘to (inj₂ (there q)) rewrite from∘to (inj₂ q) = P.refl
  179. to∘from : ∀ {xs} (p : Any (λ x → P x ⊎ Q x) xs) →
  180. to (from p) ≡ p
  181. to∘from (here (inj₁ p)) = P.refl
  182. to∘from (here (inj₂ q)) = P.refl
  183. to∘from (there p) with from p | to∘from p
  184. to∘from (there .(Any.map inj₁ p)) | inj₁ p | P.refl = P.refl
  185. to∘from (there .(Any.map inj₂ q)) | inj₂ q | P.refl = P.refl
  186. -- Products "commute" with Any.
  187. ×↔ : {A B : Set} {P : A → Set} {Q : B → Set}
  188. {xs : List A} {ys : List B} →
  189. (Any P xs × Any Q ys) ↔ Any (λ x → Any (λ y → P x × Q y) ys) xs
  190. ×↔ {P = P} {Q} {xs} {ys} = record
  191. { to = P.→-to-⟶ to
  192. ; from = P.→-to-⟶ from
  193. ; inverse-of = record
  194. { left-inverse-of = from∘to
  195. ; right-inverse-of = to∘from
  196. }
  197. }
  198. where
  199. to : Any P xs × Any Q ys → Any (λ x → Any (λ y → P x × Q y) ys) xs
  200. to (p , q) = Any.map (λ p → Any.map (λ q → (p , q)) q) p
  201. from : Any (λ x → Any (λ y → P x × Q y) ys) xs → Any P xs × Any Q ys
  202. from pq with Prod.map id (Prod.map id find) (find pq)
  203. ... | (x , x∈xs , y , y∈ys , p , q) = (lose x∈xs p , lose y∈ys q)
  204. from∘to : ∀ pq → from (to pq) ≡ pq
  205. from∘to (p , q)
  206. rewrite find∘map {Q = λ x → Any (λ y → P x × Q y) ys}
  207. p (λ p → Any.map (λ q → (p , q)) q)
  208. | find∘map {Q = λ y → P (proj₁ (find p)) × Q y}
  209. q (λ q → proj₂ (proj₂ (find p)) , q)
  210. | lose∘find p
  211. | lose∘find q
  212. = refl
  213. to∘from : ∀ pq → to (from pq) ≡ pq
  214. to∘from pq
  215. with find pq
  216. | (λ (f : _≡_ (proj₁ (find pq)) ⋐ _) → map∘find pq {f})
  217. ... | (x , x∈xs , pq′) | lem₁
  218. with find pq′
  219. | (λ (f : _≡_ (proj₁ (find pq′)) ⋐ _) → map∘find pq′ {f})
  220. ... | (y , y∈ys , p , q) | lem₂
  221. rewrite P.sym $ map-∘ {R = λ x → Any (λ y → P x × Q y) ys}
  222. (λ p → Any.map (λ q → p , q) (lose y∈ys q))
  223. (λ y → P.subst P y p)
  224. x∈xs
  225. = lem₁ _ helper
  226. where
  227. helper : Any.map (λ q → p , q) (lose y∈ys q) ≡ pq′
  228. helper rewrite P.sym $ map-∘ {R = λ y → P x × Q y}
  229. (λ q → p , q)
  230. (λ y → P.subst Q y q)
  231. y∈ys
  232. = lem₂ _ refl
  233. ------------------------------------------------------------------------
  234. -- Invertible introduction (⁺) and elimination (⁻) rules for various
  235. -- list functions
  236. -- map.
  237. private
  238. map⁺ : ∀ {a b p} {A : Set a} {B : Set b} {P : B → Set p}
  239. {f : A → B} {xs} →
  240. Any (P ∘ f) xs → Any P (List.map f xs)
  241. map⁺ (here p) = here p
  242. map⁺ (there p) = there $ map⁺ p
  243. map⁻ : ∀ {a b p} {A : Set a} {B : Set b} {P : B → Set p}
  244. {f : A → B} {xs} →
  245. Any P (List.map f xs) → Any (P ∘ f) xs
  246. map⁻ {xs = []} ()
  247. map⁻ {xs = x ∷ xs} (here p) = here p
  248. map⁻ {xs = x ∷ xs} (there p) = there $ map⁻ p
  249. map⁺∘map⁻ : ∀ {a b p} {A : Set a} {B : Set b} {P : B → Set p}
  250. {f : A → B} {xs} →
  251. (p : Any P (List.map f xs)) →
  252. map⁺ (map⁻ p) ≡ p
  253. map⁺∘map⁻ {xs = []} ()
  254. map⁺∘map⁻ {xs = x ∷ xs} (here p) = refl
  255. map⁺∘map⁻ {xs = x ∷ xs} (there p) = P.cong there (map⁺∘map⁻ p)
  256. map⁻∘map⁺ : ∀ {a b p} {A : Set a} {B : Set b} (P : B → Set p)
  257. {f : A → B} {xs} →
  258. (p : Any (P ∘ f) xs) →
  259. map⁻ {P = P} (map⁺ p) ≡ p
  260. map⁻∘map⁺ P (here p) = refl
  261. map⁻∘map⁺ P (there p) = P.cong there (map⁻∘map⁺ P p)
  262. map↔ : ∀ {a b p} {A : Set a} {B : Set b} {P : B → Set p}
  263. {f : A → B} {xs} →
  264. Any (P ∘ f) xs ↔ Any P (List.map f xs)
  265. map↔ {P = P} {f = f} = record
  266. { to = P.→-to-⟶ $ map⁺ {P = P} {f = f}
  267. ; from = P.→-to-⟶ $ map⁻ {P = P} {f = f}
  268. ; inverse-of = record
  269. { left-inverse-of = map⁻∘map⁺ P
  270. ; right-inverse-of = map⁺∘map⁻
  271. }
  272. }
  273. -- _++_.
  274. private
  275. ++⁺ˡ : ∀ {a p} {A : Set a} {P : A → Set p} {xs ys} →
  276. Any P xs → Any P (xs ++ ys)
  277. ++⁺ˡ (here p) = here p
  278. ++⁺ˡ (there p) = there (++⁺ˡ p)
  279. ++⁺ʳ : ∀ {a p} {A : Set a} {P : A → Set p} xs {ys} →
  280. Any P ys → Any P (xs ++ ys)
  281. ++⁺ʳ [] p = p
  282. ++⁺ʳ (x ∷ xs) p = there (++⁺ʳ xs p)
  283. ++⁻ : ∀ {a p} {A : Set a} {P : A → Set p} xs {ys} →
  284. Any P (xs ++ ys) → Any P xs ⊎ Any P ys
  285. ++⁻ [] p = inj₂ p
  286. ++⁻ (x ∷ xs) (here p) = inj₁ (here p)
  287. ++⁻ (x ∷ xs) (there p) = Sum.map there id (++⁻ xs p)
  288. ++⁺∘++⁻ : ∀ {a p} {A : Set a} {P : A → Set p} xs {ys}
  289. (p : Any P (xs ++ ys)) →
  290. [ ++⁺ˡ , ++⁺ʳ xs ]′ (++⁻ xs p) ≡ p
  291. ++⁺∘++⁻ [] p = refl
  292. ++⁺∘++⁻ (x ∷ xs) (here p) = refl
  293. ++⁺∘++⁻ (x ∷ xs) (there p) with ++⁻ xs p | ++⁺∘++⁻ xs p
  294. ++⁺∘++⁻ (x ∷ xs) (there p) | inj₁ p′ | ih = P.cong there ih
  295. ++⁺∘++⁻ (x ∷ xs) (there p) | inj₂ p′ | ih = P.cong there ih
  296. ++⁻∘++⁺ : ∀ {a p} {A : Set a} {P : A → Set p} xs {ys}
  297. (p : Any P xs ⊎ Any P ys) →
  298. ++⁻ xs ([ ++⁺ˡ , ++⁺ʳ xs ]′ p) ≡ p
  299. ++⁻∘++⁺ [] (inj₁ ())
  300. ++⁻∘++⁺ [] (inj₂ p) = refl
  301. ++⁻∘++⁺ (x ∷ xs) (inj₁ (here p)) = refl
  302. ++⁻∘++⁺ (x ∷ xs) {ys} (inj₁ (there p)) rewrite ++⁻∘++⁺ xs {ys} (inj₁ p) = refl
  303. ++⁻∘++⁺ (x ∷ xs) (inj₂ p) rewrite ++⁻∘++⁺ xs (inj₂ p) = refl
  304. ++↔ : ∀ {a p} {A : Set a} {P : A → Set p} {xs ys} →
  305. (Any P xs ⊎ Any P ys) ↔ Any P (xs ++ ys)
  306. ++↔ {P = P} {xs = xs} = record
  307. { to = P.→-to-⟶ [ ++⁺ˡ {P = P}, ++⁺ʳ {P = P} xs ]′
  308. ; from = P.→-to-⟶ $ ++⁻ {P = P} xs
  309. ; inverse-of = record
  310. { left-inverse-of = ++⁻∘++⁺ xs
  311. ; right-inverse-of = ++⁺∘++⁻ xs
  312. }
  313. }
  314. -- return.
  315. private
  316. return⁺ : ∀ {a p} {A : Set a} {P : A → Set p} {x} →
  317. P x → Any P (return x)
  318. return⁺ = here
  319. return⁻ : ∀ {a p} {A : Set a} {P : A → Set p} {x} →
  320. Any P (return x) → P x
  321. return⁻ (here p) = p
  322. return⁻ (there ())
  323. return⁺∘return⁻ : ∀ {a p} {A : Set a} {P : A → Set p} {x}
  324. (p : Any P (return x)) →
  325. return⁺ (return⁻ p) ≡ p
  326. return⁺∘return⁻ (here p) = refl
  327. return⁺∘return⁻ (there ())
  328. return⁻∘return⁺ : ∀ {a p} {A : Set a} (P : A → Set p) {x} (p : P x) →
  329. return⁻ {P = P} (return⁺ p) ≡ p
  330. return⁻∘return⁺ P p = refl
  331. return↔ : ∀ {a p} {A : Set a} {P : A → Set p} {x} →
  332. P x ↔ Any P (return x)
  333. return↔ {P = P} = record
  334. { to = P.→-to-⟶ $ return⁺ {P = P}
  335. ; from = P.→-to-⟶ $ return⁻ {P = P}
  336. ; inverse-of = record
  337. { left-inverse-of = return⁻∘return⁺ P
  338. ; right-inverse-of = return⁺∘return⁻
  339. }
  340. }
  341. -- _∷_.
  342. ∷↔ : ∀ {a p} {A : Set a} (P : A → Set p) {x xs} →
  343. (P x ⊎ Any P xs) ↔ Any P (x ∷ xs)
  344. ∷↔ P {x} {xs} =
  345. (P x ⊎ Any P xs) ↔⟨ return↔ {P = P} ⊎-cong (Any P xs ∎) ⟩
  346. (Any P [ x ] ⊎ Any P xs) ↔⟨ ++↔ {P = P} {xs = [ x ]} ⟩
  347. Any P (x ∷ xs) ∎
  348. -- concat.
  349. private
  350. concat⁺ : ∀ {a p} {A : Set a} {P : A → Set p} {xss} →
  351. Any (Any P) xss → Any P (concat xss)
  352. concat⁺ (here p) = ++⁺ˡ p
  353. concat⁺ (there {x = xs} p) = ++⁺ʳ xs (concat⁺ p)
  354. concat⁻ : ∀ {a p} {A : Set a} {P : A → Set p} xss →
  355. Any P (concat xss) → Any (Any P) xss
  356. concat⁻ [] ()
  357. concat⁻ ([] ∷ xss) p = there $ concat⁻ xss p
  358. concat⁻ ((x ∷ xs) ∷ xss) (here p) = here (here p)
  359. concat⁻ ((x ∷ xs) ∷ xss) (there p)
  360. with concat⁻ (xs ∷ xss) p
  361. ... | here p′ = here (there p′)
  362. ... | there p′ = there p′
  363. concat⁻∘++⁺ˡ : ∀ {a p} {A : Set a} {P : A → Set p} {xs} xss (p : Any P xs) →
  364. concat⁻ (xs ∷ xss) (++⁺ˡ p) ≡ here p
  365. concat⁻∘++⁺ˡ xss (here p) = refl
  366. concat⁻∘++⁺ˡ xss (there p) rewrite concat⁻∘++⁺ˡ xss p = refl
  367. concat⁻∘++⁺ʳ : ∀ {a p} {A : Set a} {P : A → Set p} xs xss (p : Any P (concat xss)) →
  368. concat⁻ (xs ∷ xss) (++⁺ʳ xs p) ≡ there (concat⁻ xss p)
  369. concat⁻∘++⁺ʳ [] xss p = refl
  370. concat⁻∘++⁺ʳ (x ∷ xs) xss p rewrite concat⁻∘++⁺ʳ xs xss p = refl
  371. concat⁺∘concat⁻ : ∀ {a p} {A : Set a} {P : A → Set p} xss (p : Any P (concat xss)) →
  372. concat⁺ (concat⁻ xss p) ≡ p
  373. concat⁺∘concat⁻ [] ()
  374. concat⁺∘concat⁻ ([] ∷ xss) p = concat⁺∘concat⁻ xss p
  375. concat⁺∘concat⁻ ((x ∷ xs) ∷ xss) (here p) = refl
  376. concat⁺∘concat⁻ ((x ∷ xs) ∷ xss) (there p)
  377. with concat⁻ (xs ∷ xss) p | concat⁺∘concat⁻ (xs ∷ xss) p
  378. concat⁺∘concat⁻ ((x ∷ xs) ∷ xss) (there .(++⁺ˡ p′)) | here p′ | refl = refl
  379. concat⁺∘concat⁻ ((x ∷ xs) ∷ xss) (there .(++⁺ʳ xs (concat⁺ p′))) | there p′ | refl = refl
  380. concat⁻∘concat⁺ : ∀ {a p} {A : Set a} {P : A → Set p} {xss} (p : Any (Any P) xss) →
  381. concat⁻ xss (concat⁺ p) ≡ p
  382. concat⁻∘concat⁺ (here p) = concat⁻∘++⁺ˡ _ p
  383. concat⁻∘concat⁺ (there {x = xs} {xs = xss} p)
  384. rewrite concat⁻∘++⁺ʳ xs xss (concat⁺ p) =
  385. P.cong there $ concat⁻∘concat⁺ p
  386. concat↔ : ∀ {a p} {A : Set a} {P : A → Set p} {xss} →
  387. Any (Any P) xss ↔ Any P (concat xss)
  388. concat↔ {P = P} {xss = xss} = record
  389. { to = P.→-to-⟶ $ concat⁺ {P = P}
  390. ; from = P.→-to-⟶ $ concat⁻ {P = P} xss
  391. ; inverse-of = record
  392. { left-inverse-of = concat⁻∘concat⁺
  393. ; right-inverse-of = concat⁺∘concat⁻ xss
  394. }
  395. }
  396. -- _>>=_.
  397. >>=↔ : ∀ {ℓ p} {A B : Set ℓ} {P : B → Set p} {xs} {f : A → List B} →
  398. Any (Any P ∘ f) xs ↔ Any P (xs >>= f)
  399. >>=↔ {P = P} {xs} {f} =
  400. Any (Any P ∘ f) xs ↔⟨ map↔ {P = Any P} {f = f} ⟩
  401. Any (Any P) (List.map f xs) ↔⟨ concat↔ {P = P} ⟩
  402. Any P (xs >>= f) ∎
  403. -- _⊛_.
  404. ⊛↔ : ∀ {ℓ} {A B : Set ℓ} {P : B → Set ℓ}
  405. {fs : List (A → B)} {xs : List A} →
  406. Any (λ f → Any (P ∘ f) xs) fs ↔ Any P (fs ⊛ xs)
  407. ⊛↔ {ℓ} {P = P} {fs} {xs} =
  408. Any (λ f → Any (P ∘ f) xs) fs ↔⟨ Any-cong (λ _ → Any-cong (λ _ → return↔ {a = ℓ} {p = ℓ}) (_ ∎)) (_ ∎) ⟩
  409. Any (λ f → Any (Any P ∘ return ∘ f) xs) fs ↔⟨ Any-cong (λ _ → >>=↔ {ℓ = ℓ} {p = ℓ}) (_ ∎) ⟩
  410. Any (λ f → Any P (xs >>= return ∘ f)) fs ↔⟨ >>=↔ {ℓ = ℓ} {p = ℓ} ⟩
  411. Any P (fs ⊛ xs) ∎
  412. -- An alternative introduction rule for _⊛_.
  413. ⊛⁺′ : ∀ {ℓ} {A B : Set ℓ} {P : A → Set ℓ} {Q : B → Set ℓ}
  414. {fs : List (A → B)} {xs} →
  415. Any (P ⟨→⟩ Q) fs → Any P xs → Any Q (fs ⊛ xs)
  416. ⊛⁺′ {ℓ} pq p =
  417. Inverse.to (⊛↔ {ℓ = ℓ}) ⟨$⟩
  418. Any.map (λ pq → Any.map (λ {x} → pq {x}) p) pq
  419. -- _⊗_.
  420. ⊗↔ : ∀ {ℓ} {A B : Set ℓ} {P : A × B → Set ℓ}
  421. {xs : List A} {ys : List B} →
  422. Any (λ x → Any (λ y → P (x , y)) ys) xs ↔ Any P (xs ⊗ ys)
  423. ⊗↔ {ℓ} {P = P} {xs} {ys} =
  424. Any (λ x → Any (λ y → P (x , y)) ys) xs ↔⟨ return↔ {a = ℓ} {p = ℓ} ⟩
  425. Any (λ _,_ → Any (λ x → Any (λ y → P (x , y)) ys) xs) (return _,_) ↔⟨ ⊛↔ ⟩
  426. Any (λ x, → Any (P ∘ x,) ys) (_,_ <$> xs) ↔⟨ ⊛↔ ⟩
  427. Any P (xs ⊗ ys) ∎
  428. ⊗↔′ : {A B : Set} {P : A → Set} {Q : B → Set}
  429. {xs : List A} {ys : List B} →
  430. (Any P xs × Any Q ys) ↔ Any (P ⟨×⟩ Q) (xs ⊗ ys)
  431. ⊗↔′ {P = P} {Q} {xs} {ys} =
  432. (Any P xs × Any Q ys) ↔⟨ ×↔ ⟩
  433. Any (λ x → Any (λ y → P x × Q y) ys) xs ↔⟨ ⊗↔ ⟩
  434. Any (P ⟨×⟩ Q) (xs ⊗ ys) ∎
  435. -- map-with-∈.
  436. map-with-∈↔ :
  437. ∀ {a b p} {A : Set a} {B : Set b} {P : B → Set p} {xs : List A}
  438. {f : ∀ {x} → x ∈ xs → B} →
  439. (∃₂ λ x (x∈xs : x ∈ xs) → P (f x∈xs)) ↔ Any P (map-with-∈ xs f)
  440. map-with-∈↔ {A = A} {B} {P} = record
  441. { to = P.→-to-⟶ (map-with-∈⁺ _)
  442. ; from = P.→-to-⟶ (map-with-∈⁻ _ _)
  443. ; inverse-of = record
  444. { left-inverse-of = from∘to _
  445. ; right-inverse-of = to∘from _ _
  446. }
  447. }
  448. where
  449. map-with-∈⁺ : ∀ {xs : List A}
  450. (f : ∀ {x} → x ∈ xs → B) →
  451. (∃₂ λ x (x∈xs : x ∈ xs) → P (f x∈xs)) →
  452. Any P (map-with-∈ xs f)
  453. map-with-∈⁺ f (_ , here refl , p) = here p
  454. map-with-∈⁺ f (_ , there x∈xs , p) =
  455. there $ map-with-∈⁺ (f ∘ there) (_ , x∈xs , p)
  456. map-with-∈⁻ : ∀ (xs : List A)
  457. (f : ∀ {x} → x ∈ xs → B) →
  458. Any P (map-with-∈ xs f) →
  459. ∃₂ λ x (x∈xs : x ∈ xs) → P (f x∈xs)
  460. map-with-∈⁻ [] f ()
  461. map-with-∈⁻ (y ∷ xs) f (here p) = (y , here refl , p)
  462. map-with-∈⁻ (y ∷ xs) f (there p) =
  463. Prod.map id (Prod.map there id) $ map-with-∈⁻ xs (f ∘ there) p
  464. from∘to : ∀ {xs : List A} (f : ∀ {x} → x ∈ xs → B)
  465. (p : ∃₂ λ x (x∈xs : x ∈ xs) → P (f x∈xs)) →
  466. map-with-∈⁻ xs f (map-with-∈⁺ f p) ≡ p
  467. from∘to f (_ , here refl , p) = refl
  468. from∘to f (_ , there x∈xs , p)
  469. rewrite from∘to (f ∘ there) (_ , x∈xs , p) = refl
  470. to∘from : ∀ (xs : List A) (f : ∀ {x} → x ∈ xs → B)
  471. (p : Any P (map-with-∈ xs f)) →
  472. map-with-∈⁺ f (map-with-∈⁻ xs f p) ≡ p
  473. to∘from [] f ()
  474. to∘from (y ∷ xs) f (here p) = refl
  475. to∘from (y ∷ xs) f (there p) =
  476. P.cong there $ to∘from xs (f ∘ there) p
  477. ------------------------------------------------------------------------
  478. -- Any and any are related via T
  479. -- These introduction and elimination rules are not inverses, though.
  480. private
  481. any⁺ : ∀ {a} {A : Set a} (p : A → Bool) {xs} →
  482. Any (T ∘ p) xs → T (any p xs)
  483. any⁺ p (here px) = Equivalence.from T-∨ ⟨$⟩ inj₁ px
  484. any⁺ p (there {x = x} pxs) with p x
  485. ... | true = _
  486. ... | false = any⁺ p pxs
  487. any⁻ : ∀ {a} {A : Set a} (p : A → Bool) xs →
  488. T (any p xs) → Any (T ∘ p) xs
  489. any⁻ p [] ()
  490. any⁻ p (x ∷ xs) px∷xs with p x | inspect p x
  491. any⁻ p (x ∷ xs) px∷xs | true | P[ eq ] = here (Equivalence.from T-≡ ⟨$⟩ eq)
  492. any⁻ p (x ∷ xs) px∷xs | false | _ = there (any⁻ p xs px∷xs)
  493. any⇔ : ∀ {a} {A : Set a} {p : A → Bool} {xs} →
  494. Any (T ∘ p) xs ⇔ T (any p xs)
  495. any⇔ = Eq.equivalence (any⁺ _) (any⁻ _ _)
  496. ------------------------------------------------------------------------
  497. -- _++_ is commutative
  498. private
  499. ++-comm : ∀ {a p} {A : Set a} {P : A → Set p} xs ys →
  500. Any P (xs ++ ys) → Any P (ys ++ xs)
  501. ++-comm xs ys = [ ++⁺ʳ ys , ++⁺ˡ ]′ ∘ ++⁻ xs
  502. ++-comm∘++-comm : ∀ {a p} {A : Set a} {P : A → Set p}
  503. xs {ys} (p : Any P (xs ++ ys)) →
  504. ++-comm ys xs (++-comm xs ys p) ≡ p
  505. ++-comm∘++-comm [] {ys} p
  506. rewrite ++⁻∘++⁺ ys {ys = []} (inj₁ p) = P.refl
  507. ++-comm∘++-comm {P = P} (x ∷ xs) {ys} (here p)
  508. rewrite ++⁻∘++⁺ {P = P} ys {ys = x ∷ xs} (inj₂ (here p)) = P.refl
  509. ++-comm∘++-comm (x ∷ xs) (there p) with ++⁻ xs p | ++-comm∘++-comm xs p
  510. ++-comm∘++-comm (x ∷ xs) {ys} (there .([ ++⁺ʳ xs , ++⁺ˡ ]′ (++⁻ ys (++⁺ʳ ys p))))
  511. | inj₁ p | P.refl
  512. rewrite ++⁻∘++⁺ ys (inj₂ p)
  513. | ++⁻∘++⁺ ys (inj₂ $′ there {x = x} p) = refl
  514. ++-comm∘++-comm (x ∷ xs) {ys} (there .([ ++⁺ʳ xs , ++⁺ˡ ]′ (++⁻ ys (++⁺ˡ p))))
  515. | inj₂ p | P.refl
  516. rewrite ++⁻∘++⁺ ys {ys = xs} (inj₁ p)
  517. | ++⁻∘++⁺ ys {ys = x ∷ xs} (inj₁ p) = P.refl
  518. ++↔++ : ∀ {a p} {A : Set a} {P : A → Set p} xs ys →
  519. Any P (xs ++ ys) ↔ Any P (ys ++ xs)
  520. ++↔++ {P = P} xs ys = record
  521. { to = P.→-to-⟶ $ ++-comm {P = P} xs ys
  522. ; from = P.→-to-⟶ $ ++-comm {P = P} ys xs
  523. ; inverse-of = record
  524. { left-inverse-of = ++-comm∘++-comm xs
  525. ; right-inverse-of = ++-comm∘++-comm ys
  526. }
  527. }