| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Properties of vector's Any |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Vec.Relation.Unary.Any.Properties where |
| 10 |
|
| 11 |
open import Data.Nat.Base using (suc; zero) |
| 12 |
open import Data.Fin.Base using (Fin; zero; suc) |
| 13 |
open import Data.Empty using (⊥) |
| 14 |
open import Data.List.Base using ([]; _∷_) |
| 15 |
import Data.List.Relation.Unary.Any as List |
| 16 |
open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂; [_,_]′) |
| 17 |
open import Data.Sum.Function.Propositional using (_⊎-cong_) |
| 18 |
open import Data.Product.Base as Product using (∃; ∃₂; _×_; _,_; proj₁; proj₂) |
| 19 |
open import Data.Vec.Base hiding (here; there) |
| 20 |
open import Data.Vec.Relation.Unary.Any as Any using (Any; here; there) |
| 21 |
open import Data.Vec.Membership.Propositional |
| 22 |
using (_∈_; mapWith∈; find; lose) |
| 23 |
open import Data.Vec.Relation.Binary.Pointwise.Inductive |
| 24 |
using (Pointwise; []; _∷_) |
| 25 |
open import Function.Base |
| 26 |
open import Function.Bundles using (_↔_; mk↔ₛ′) |
| 27 |
open import Function.Properties.Inverse using (↔-refl; ↔-trans) |
| 28 |
open import Level using (Level) |
| 29 |
open import Relation.Nullary.Negation using (¬_) |
| 30 |
open import Relation.Unary hiding (_∈_) |
| 31 |
open import Relation.Binary.Core using (Rel) |
| 32 |
open import Relation.Binary.Definitions using (_Respects_) |
| 33 |
open import Relation.Binary.PropositionalEquality.Core |
| 34 |
using (_≡_; refl; sym; trans; cong) |
| 35 |
|
| 36 |
private |
| 37 |
variable |
| 38 |
a b p q r ℓ : Level |
| 39 |
A : Set a |
| 40 |
B : Set b |
| 41 |
|
| 42 |
------------------------------------------------------------------------ |
| 43 |
-- Equality properties |
| 44 |
|
| 45 |
module _ {P : Pred A p} {_≈_ : Rel A ℓ} where |
| 46 |
|
| 47 |
lift-resp : ∀ {n} → P Respects _≈_ → (Any P {n}) Respects (Pointwise _≈_) |
| 48 |
lift-resp resp (x∼y ∷ xs∼ys) (here px) = here (resp x∼y px) |
| 49 |
lift-resp resp (x∼y ∷ xs∼ys) (there pxs) = there (lift-resp resp xs∼ys pxs) |
| 50 |
|
| 51 |
module _ {P : Pred A p} where |
| 52 |
|
| 53 |
here-injective : ∀ {n x xs} {p q : P x} → |
| 54 |
here {P = P} {n = n} {xs = xs} p ≡ here q → p ≡ q |
| 55 |
here-injective refl = refl |
| 56 |
|
| 57 |
there-injective : ∀ {n x xs} {p q : Any P xs} → |
| 58 |
there {n = n} {x = x} p ≡ there q → p ≡ q |
| 59 |
there-injective refl = refl |
| 60 |
|
| 61 |
------------------------------------------------------------------------ |
| 62 |
-- Misc |
| 63 |
|
| 64 |
¬Any[] : ¬ Any P [] |
| 65 |
¬Any[] () |
| 66 |
|
| 67 |
lookup-index : ∀ {m} {xs : Vec A m} (p : Any P xs) → |
| 68 |
P (lookup xs (Any.index p)) |
| 69 |
lookup-index (here px) = px |
| 70 |
lookup-index (there p) = lookup-index p |
| 71 |
|
| 72 |
------------------------------------------------------------------------ |
| 73 |
-- Convert from/to List.Any |
| 74 |
|
| 75 |
fromList⁺ : ∀ {xs} → List.Any P xs → Any P (fromList xs) |
| 76 |
fromList⁺ (List.here px) = here px |
| 77 |
fromList⁺ (List.there v) = there (fromList⁺ v) |
| 78 |
|
| 79 |
fromList⁻ : ∀ {xs} → Any P (fromList xs) → List.Any P xs |
| 80 |
fromList⁻ {x ∷ xs} (here px) = List.here px |
| 81 |
fromList⁻ {x ∷ xs} (there pxs) = List.there (fromList⁻ pxs) |
| 82 |
|
| 83 |
toList⁺ : ∀ {n} {xs : Vec A n} → Any P xs → List.Any P (toList xs) |
| 84 |
toList⁺ (here px) = List.here px |
| 85 |
toList⁺ (there v) = List.there (toList⁺ v) |
| 86 |
|
| 87 |
toList⁻ : ∀ {n} {xs : Vec A n} → List.Any P (toList xs) → Any P xs |
| 88 |
toList⁻ {xs = x ∷ xs} (List.here px) = here px |
| 89 |
toList⁻ {xs = x ∷ xs} (List.there pxs) = there (toList⁻ pxs) |
| 90 |
|
| 91 |
------------------------------------------------------------------------ |
| 92 |
-- map |
| 93 |
|
| 94 |
map-id : ∀ {P : Pred A p} (f : P ⊆ P) {n xs} → |
| 95 |
(∀ {x} (p : P x) → f p ≡ p) → |
| 96 |
(p : Any P {n} xs) → Any.map f p ≡ p |
| 97 |
map-id f hyp (here p) = cong here (hyp p) |
| 98 |
map-id f hyp (there p) = cong there $ map-id f hyp p |
| 99 |
|
| 100 |
map-∘ : ∀ {P : Pred A p} {Q : A → Set q} {R : A → Set r} |
| 101 |
(f : Q ⊆ R) (g : P ⊆ Q) |
| 102 |
{n xs} (p : Any P {n} xs) → |
| 103 |
Any.map (f ∘ g) p ≡ Any.map f (Any.map g p) |
| 104 |
map-∘ f g (here p) = refl |
| 105 |
map-∘ f g (there p) = cong there $ map-∘ f g p |
| 106 |
|
| 107 |
------------------------------------------------------------------------ |
| 108 |
-- Swapping |
| 109 |
|
| 110 |
module _ {P : A → B → Set ℓ} where |
| 111 |
|
| 112 |
swap : ∀ {n m} {xs : Vec A n} {ys : Vec B m} → |
| 113 |
Any (λ x → Any (P x) ys) xs → |
| 114 |
Any (λ y → Any (flip P y) xs) ys |
| 115 |
swap (here pys) = Any.map here pys |
| 116 |
swap (there pxys) = Any.map there (swap pxys) |
| 117 |
|
| 118 |
swap-there : ∀ {n m x xs ys} → (any : Any (λ x → Any (P x) {n} ys) {m} xs) → |
| 119 |
swap (Any.map (there {x = x}) any) ≡ there (swap any) |
| 120 |
swap-there (here pys) = refl |
| 121 |
swap-there (there pxys) = cong (Any.map there) (swap-there pxys) |
| 122 |
|
| 123 |
module _ {P : A → B → Set ℓ} where |
| 124 |
|
| 125 |
swap-invol : ∀ {n m} {xs : Vec A n} {ys : Vec B m} → |
| 126 |
(any : Any (λ x → Any (P x) ys) xs) → |
| 127 |
swap (swap any) ≡ any |
| 128 |
swap-invol (here (here _)) = refl |
| 129 |
swap-invol (here (there pys)) = cong (Any.map there) (swap-invol (here pys)) |
| 130 |
swap-invol (there pxys) = trans (swap-there (swap pxys)) |
| 131 |
$ cong there (swap-invol pxys) |
| 132 |
|
| 133 |
module _ {P : A → B → Set ℓ} where |
| 134 |
|
| 135 |
swap↔ : ∀ {n m} {xs : Vec A n} {ys : Vec B m} → |
| 136 |
Any (λ x → Any (P x) ys) xs ↔ Any (λ y → Any (flip P y) xs) ys |
| 137 |
swap↔ = mk↔ₛ′ swap swap swap-invol swap-invol |
| 138 |
|
| 139 |
------------------------------------------------------------------------ |
| 140 |
-- Lemmas relating Any to ⊥ |
| 141 |
|
| 142 |
⊥↔Any⊥ : ∀ {n} {xs : Vec A n} → ⊥ ↔ Any (const ⊥) xs |
| 143 |
⊥↔Any⊥ = mk↔ₛ′ (λ ()) (λ p → from p) (λ p → from p) (λ ()) |
| 144 |
where |
| 145 |
from : ∀ {n xs} → Any (const ⊥) {n} xs → ∀ {b} {B : Set b} → B |
| 146 |
from (there p) = from p |
| 147 |
|
| 148 |
⊥↔Any[] : ∀ {P : Pred A p} → ⊥ ↔ Any P [] |
| 149 |
⊥↔Any[] = mk↔ₛ′ (λ()) (λ()) (λ()) (λ()) |
| 150 |
|
| 151 |
------------------------------------------------------------------------ |
| 152 |
-- Sums commute with Any |
| 153 |
|
| 154 |
module _ {P : Pred A p} {Q : A → Set q} where |
| 155 |
|
| 156 |
Any-⊎⁺ : ∀ {n} {xs : Vec A n} → Any P xs ⊎ Any Q xs → Any (λ x → P x ⊎ Q x) xs |
| 157 |
Any-⊎⁺ = [ Any.map inj₁ , Any.map inj₂ ]′ |
| 158 |
|
| 159 |
Any-⊎⁻ : ∀ {n} {xs : Vec A n} → Any (λ x → P x ⊎ Q x) xs → Any P xs ⊎ Any Q xs |
| 160 |
Any-⊎⁻ (here (inj₁ p)) = inj₁ (here p) |
| 161 |
Any-⊎⁻ (here (inj₂ q)) = inj₂ (here q) |
| 162 |
Any-⊎⁻ (there p) = Sum.map there there (Any-⊎⁻ p) |
| 163 |
|
| 164 |
⊎↔ : ∀ {n} {xs : Vec A n} → (Any P xs ⊎ Any Q xs) ↔ Any (λ x → P x ⊎ Q x) xs |
| 165 |
⊎↔ = mk↔ₛ′ Any-⊎⁺ Any-⊎⁻ to∘from from∘to |
| 166 |
where |
| 167 |
from∘to : ∀ {n} {xs : Vec A n} (p : Any P xs ⊎ Any Q xs) → Any-⊎⁻ (Any-⊎⁺ p) ≡ p |
| 168 |
from∘to (inj₁ (here p)) = refl |
| 169 |
from∘to (inj₁ (there p)) rewrite from∘to (inj₁ p) = refl |
| 170 |
from∘to (inj₂ (here q)) = refl |
| 171 |
from∘to (inj₂ (there q)) rewrite from∘to (inj₂ q) = refl |
| 172 |
|
| 173 |
to∘from : ∀ {n} {xs : Vec A n} (p : Any (λ x → P x ⊎ Q x) xs) → |
| 174 |
Any-⊎⁺ (Any-⊎⁻ p) ≡ p |
| 175 |
to∘from (here (inj₁ p)) = refl |
| 176 |
to∘from (here (inj₂ q)) = refl |
| 177 |
to∘from (there p) with Any-⊎⁻ p | to∘from p |
| 178 |
to∘from (there .(Any.map inj₁ p)) | inj₁ p | refl = refl |
| 179 |
to∘from (there .(Any.map inj₂ q)) | inj₂ q | refl = refl |
| 180 |
|
| 181 |
------------------------------------------------------------------------ |
| 182 |
-- Products "commute" with Any. |
| 183 |
|
| 184 |
module _ {P : Pred A p} {Q : Pred B q} where |
| 185 |
|
| 186 |
Any-×⁺ : ∀ {n m} {xs : Vec A n} {ys : Vec B m} → Any P xs × Any Q ys → |
| 187 |
Any (λ x → Any (λ y → P x × Q y) ys) xs |
| 188 |
Any-×⁺ (p , q) = Any.map (λ p → Any.map (p ,_) q) p |
| 189 |
|
| 190 |
Any-×⁻ : ∀ {n m} {xs : Vec A n} {ys : Vec B m} → |
| 191 |
Any (λ x → Any (λ y → P x × Q y) ys) xs → |
| 192 |
Any P xs × Any Q ys |
| 193 |
Any-×⁻ pq with find pq |
| 194 |
... | x , x∈xs , pxys with find pxys |
| 195 |
... | y , y∈ys , px , py = lose x∈xs px , lose y∈ys py |
| 196 |
|
| 197 |
------------------------------------------------------------------------ |
| 198 |
-- Invertible introduction (⁺) and elimination (⁻) rules for various |
| 199 |
-- vector functions |
| 200 |
|
| 201 |
------------------------------------------------------------------------ |
| 202 |
-- Singleton ([_]) |
| 203 |
|
| 204 |
module _ {P : Pred A p} where |
| 205 |
|
| 206 |
singleton⁺ : ∀ {x} → P x → Any P [ x ] |
| 207 |
singleton⁺ Px = here Px |
| 208 |
|
| 209 |
singleton⁻ : ∀ {x} → Any P [ x ] → P x |
| 210 |
singleton⁻ (here Px) = Px |
| 211 |
|
| 212 |
singleton⁺∘singleton⁻ : ∀ {x} (p : Any P [ x ]) → |
| 213 |
singleton⁺ (singleton⁻ p) ≡ p |
| 214 |
singleton⁺∘singleton⁻ (here px) = refl |
| 215 |
|
| 216 |
singleton⁻∘singleton⁺ : ∀ {x} (p : P x) → |
| 217 |
singleton⁻ (singleton⁺ p) ≡ p |
| 218 |
singleton⁻∘singleton⁺ p = refl |
| 219 |
|
| 220 |
singleton↔ : ∀ {x} → P x ↔ Any P [ x ] |
| 221 |
singleton↔ = mk↔ₛ′ singleton⁺ singleton⁻ singleton⁺∘singleton⁻ singleton⁻∘singleton⁺ |
| 222 |
|
| 223 |
------------------------------------------------------------------------ |
| 224 |
-- map |
| 225 |
|
| 226 |
module _ {f : A → B} where |
| 227 |
|
| 228 |
map⁺ : ∀ {P : Pred B p} {n} {xs : Vec A n} → |
| 229 |
Any (P ∘ f) xs → Any P (map f xs) |
| 230 |
map⁺ (here p) = here p |
| 231 |
map⁺ (there p) = there $ map⁺ p |
| 232 |
|
| 233 |
map⁻ : ∀ {P : Pred B p} {n} {xs : Vec A n} → |
| 234 |
Any P (map f xs) → Any (P ∘ f) xs |
| 235 |
map⁻ {xs = x ∷ xs} (here p) = here p |
| 236 |
map⁻ {xs = x ∷ xs} (there p) = there $ map⁻ p |
| 237 |
|
| 238 |
map⁺∘map⁻ : ∀ {P : Pred B p} {n} {xs : Vec A n} → |
| 239 |
(p : Any P (map f xs)) → map⁺ (map⁻ p) ≡ p |
| 240 |
map⁺∘map⁻ {xs = x ∷ xs} (here p) = refl |
| 241 |
map⁺∘map⁻ {xs = x ∷ xs} (there p) = cong there (map⁺∘map⁻ p) |
| 242 |
|
| 243 |
map⁻∘map⁺ : ∀ (P : Pred B p) {n} {xs : Vec A n} → |
| 244 |
(p : Any (P ∘ f) xs) → map⁻ {P = P} (map⁺ p) ≡ p |
| 245 |
map⁻∘map⁺ P (here p) = refl |
| 246 |
map⁻∘map⁺ P (there p) = cong there (map⁻∘map⁺ P p) |
| 247 |
|
| 248 |
map↔ : ∀ {P : Pred B p} {n} {xs : Vec A n} → |
| 249 |
Any (P ∘ f) xs ↔ Any P (map f xs) |
| 250 |
map↔ = mk↔ₛ′ map⁺ map⁻ map⁺∘map⁻ (map⁻∘map⁺ _) |
| 251 |
|
| 252 |
------------------------------------------------------------------------ |
| 253 |
-- _++_ |
| 254 |
|
| 255 |
module _ {P : Pred A p} where |
| 256 |
|
| 257 |
++⁺ˡ : ∀ {n m} {xs : Vec A n} {ys : Vec A m} → Any P xs → Any P (xs ++ ys) |
| 258 |
++⁺ˡ (here p) = here p |
| 259 |
++⁺ˡ (there p) = there (++⁺ˡ p) |
| 260 |
|
| 261 |
++⁺ʳ : ∀ {n m} (xs : Vec A n) {ys : Vec A m} → Any P ys → Any P (xs ++ ys) |
| 262 |
++⁺ʳ [] p = p |
| 263 |
++⁺ʳ (x ∷ xs) p = there (++⁺ʳ xs p) |
| 264 |
|
| 265 |
++⁻ : ∀ {n m} (xs : Vec A n) {ys : Vec A m} → Any P (xs ++ ys) → Any P xs ⊎ Any P ys |
| 266 |
++⁻ [] p = inj₂ p |
| 267 |
++⁻ (x ∷ xs) (here p) = inj₁ (here p) |
| 268 |
++⁻ (x ∷ xs) (there p) = Sum.map there id (++⁻ xs p) |
| 269 |
|
| 270 |
++⁺∘++⁻ : ∀ {n m} (xs : Vec A n) {ys : Vec A m} (p : Any P (xs ++ ys)) → |
| 271 |
[ ++⁺ˡ , ++⁺ʳ xs ]′ (++⁻ xs p) ≡ p |
| 272 |
++⁺∘++⁻ [] p = refl |
| 273 |
++⁺∘++⁻ (x ∷ xs) (here p) = refl |
| 274 |
++⁺∘++⁻ (x ∷ xs) (there p) with ++⁻ xs p | ++⁺∘++⁻ xs p |
| 275 |
++⁺∘++⁻ (x ∷ xs) (there p) | inj₁ p′ | ih = cong there ih |
| 276 |
++⁺∘++⁻ (x ∷ xs) (there p) | inj₂ p′ | ih = cong there ih |
| 277 |
|
| 278 |
++⁻∘++⁺ : ∀ {n m} (xs : Vec A n) {ys : Vec A m} (p : Any P xs ⊎ Any P ys) → |
| 279 |
++⁻ xs ([ ++⁺ˡ , ++⁺ʳ xs ]′ p) ≡ p |
| 280 |
++⁻∘++⁺ [] (inj₂ p) = refl |
| 281 |
++⁻∘++⁺ (x ∷ xs) (inj₁ (here p)) = refl |
| 282 |
++⁻∘++⁺ (x ∷ xs) {ys} (inj₁ (there p)) rewrite ++⁻∘++⁺ xs {ys} (inj₁ p) = refl |
| 283 |
++⁻∘++⁺ (x ∷ xs) (inj₂ p) rewrite ++⁻∘++⁺ xs (inj₂ p) = refl |
| 284 |
|
| 285 |
++↔ : ∀ {n m} {xs : Vec A n} {ys : Vec A m} → |
| 286 |
(Any P xs ⊎ Any P ys) ↔ Any P (xs ++ ys) |
| 287 |
++↔ {xs = xs} = mk↔ₛ′ [ ++⁺ˡ , ++⁺ʳ xs ]′ (++⁻ xs) (++⁺∘++⁻ xs) (++⁻∘++⁺ xs) |
| 288 |
|
| 289 |
++-comm : ∀ {n m} (xs : Vec A n) (ys : Vec A m) → |
| 290 |
Any P (xs ++ ys) → Any P (ys ++ xs) |
| 291 |
++-comm xs ys = [ ++⁺ʳ ys , ++⁺ˡ ]′ ∘ ++⁻ xs |
| 292 |
|
| 293 |
++-comm∘++-comm : ∀ {n m} (xs : Vec A n) {ys : Vec A m} (p : Any P (xs ++ ys)) → |
| 294 |
++-comm ys xs (++-comm xs ys p) ≡ p |
| 295 |
++-comm∘++-comm [] {ys} p |
| 296 |
rewrite ++⁻∘++⁺ ys {ys = []} (inj₁ p) = refl |
| 297 |
++-comm∘++-comm (x ∷ xs) {ys} (here p) |
| 298 |
rewrite ++⁻∘++⁺ ys {ys = x ∷ xs} (inj₂ (here p)) = refl |
| 299 |
++-comm∘++-comm (x ∷ xs) (there p) with ++⁻ xs p | ++-comm∘++-comm xs p |
| 300 |
++-comm∘++-comm (x ∷ xs) {ys} (there .([ ++⁺ʳ xs , ++⁺ˡ ]′ (++⁻ ys (++⁺ʳ ys p)))) |
| 301 |
| inj₁ p | refl |
| 302 |
rewrite ++⁻∘++⁺ ys (inj₂ p) |
| 303 |
| ++⁻∘++⁺ ys (inj₂ $ there {x = x} p) = refl |
| 304 |
++-comm∘++-comm (x ∷ xs) {ys} (there .([ ++⁺ʳ xs , ++⁺ˡ ]′ (++⁻ ys (++⁺ˡ p)))) |
| 305 |
| inj₂ p | refl |
| 306 |
rewrite ++⁻∘++⁺ ys {ys = xs} (inj₁ p) |
| 307 |
| ++⁻∘++⁺ ys {ys = x ∷ xs} (inj₁ p) = refl |
| 308 |
|
| 309 |
++↔++ : ∀ {n m} (xs : Vec A n) (ys : Vec A m) → Any P (xs ++ ys) ↔ Any P (ys ++ xs) |
| 310 |
++↔++ xs ys = mk↔ₛ′ (++-comm xs ys) (++-comm ys xs) |
| 311 |
(++-comm∘++-comm ys) (++-comm∘++-comm xs) |
| 312 |
|
| 313 |
++-insert : ∀ {n m x} (xs : Vec A n) {ys : Vec A m} → P x → Any P (xs ++ [ x ] ++ ys) |
| 314 |
++-insert xs Px = ++⁺ʳ xs (++⁺ˡ (singleton⁺ Px)) |
| 315 |
|
| 316 |
------------------------------------------------------------------------ |
| 317 |
-- concat |
| 318 |
|
| 319 |
module _ {P : Pred A p} where |
| 320 |
|
| 321 |
concat⁺ : ∀ {n m} {xss : Vec (Vec A n) m} → Any (Any P) xss → Any P (concat xss) |
| 322 |
concat⁺ (here p) = ++⁺ˡ p |
| 323 |
concat⁺ (there {x = xs} p) = ++⁺ʳ xs (concat⁺ p) |
| 324 |
|
| 325 |
concat⁻ : ∀ {n m} (xss : Vec (Vec A n) m) → Any P (concat xss) → Any (Any P) xss |
| 326 |
concat⁻ (xs ∷ xss) p = [ here , there ∘ concat⁻ xss ]′ (++⁻ xs p) |
| 327 |
|
| 328 |
concat⁻∘++⁺ˡ : ∀ {n m xs} (xss : Vec (Vec A n) m) (p : Any P xs) → |
| 329 |
concat⁻ (xs ∷ xss) (++⁺ˡ p) ≡ here p |
| 330 |
concat⁻∘++⁺ˡ xss p rewrite ++⁻∘++⁺ _ {concat xss} (inj₁ p) = refl |
| 331 |
|
| 332 |
concat⁻∘++⁺ʳ : ∀ {n m} xs (xss : Vec (Vec A n) m) (p : Any P (concat xss)) → |
| 333 |
concat⁻ (xs ∷ xss) (++⁺ʳ xs p) ≡ there (concat⁻ xss p) |
| 334 |
concat⁻∘++⁺ʳ xs xss p rewrite ++⁻∘++⁺ xs (inj₂ p) = refl |
| 335 |
|
| 336 |
concat⁺∘concat⁻ : ∀ {n m} (xss : Vec (Vec A n) m) (p : Any P (concat xss)) → |
| 337 |
concat⁺ (concat⁻ xss p) ≡ p |
| 338 |
concat⁺∘concat⁻ (xs ∷ xss) p with ++⁻ xs p in eq |
| 339 |
... | inj₁ pxs |
| 340 |
= trans (cong [ ++⁺ˡ , ++⁺ʳ xs ]′ (sym eq)) |
| 341 |
$ ++⁺∘++⁻ xs p |
| 342 |
... | inj₂ pxss rewrite concat⁺∘concat⁻ xss pxss |
| 343 |
= trans (cong [ ++⁺ˡ , ++⁺ʳ xs ]′ (sym eq)) |
| 344 |
$ ++⁺∘++⁻ xs p |
| 345 |
|
| 346 |
concat⁻∘concat⁺ : ∀ {n m} {xss : Vec (Vec A n) m} (p : Any (Any P) xss) → |
| 347 |
concat⁻ xss (concat⁺ p) ≡ p |
| 348 |
concat⁻∘concat⁺ {xss = xs ∷ xss} (here p) |
| 349 |
rewrite ++⁻∘++⁺ xs {concat xss} (inj₁ p) = refl |
| 350 |
concat⁻∘concat⁺ {xss = xs ∷ xss} (there p) |
| 351 |
rewrite ++⁻∘++⁺ xs {concat xss} (inj₂ (concat⁺ p)) |
| 352 |
| concat⁻∘concat⁺ p = refl |
| 353 |
|
| 354 |
concat↔ : ∀ {n m} {xss : Vec (Vec A n) m} → Any (Any P) xss ↔ Any P (concat xss) |
| 355 |
concat↔ {xss = xss} = mk↔ₛ′ concat⁺ (concat⁻ xss) (concat⁺∘concat⁻ xss) concat⁻∘concat⁺ |
| 356 |
|
| 357 |
------------------------------------------------------------------------ |
| 358 |
-- tabulate |
| 359 |
|
| 360 |
module _ {P : Pred A p} where |
| 361 |
|
| 362 |
tabulate⁺ : ∀ {n} {f : Fin n → A} i → P (f i) → Any P (tabulate f) |
| 363 |
tabulate⁺ zero p = here p |
| 364 |
tabulate⁺ (suc i) p = there (tabulate⁺ i p) |
| 365 |
|
| 366 |
tabulate⁻ : ∀ {n} {f : Fin n → A} → |
| 367 |
Any P (tabulate f) → ∃ λ i → P (f i) |
| 368 |
tabulate⁻ (here p) = zero , p |
| 369 |
tabulate⁻ (there p) = Product.map suc id (tabulate⁻ p) |
| 370 |
|
| 371 |
------------------------------------------------------------------------ |
| 372 |
-- mapWith∈ |
| 373 |
|
| 374 |
module _ {P : Pred B p} where |
| 375 |
|
| 376 |
mapWith∈⁺ : ∀ {n} {xs : Vec A n} (f : ∀ {x} → x ∈ xs → B) → |
| 377 |
(∃₂ λ x (x∈xs : x ∈ xs) → P (f x∈xs)) → |
| 378 |
Any P (mapWith∈ xs f) |
| 379 |
mapWith∈⁺ f (_ , here refl , p) = here p |
| 380 |
mapWith∈⁺ f (_ , there x∈xs , p) = |
| 381 |
there $ mapWith∈⁺ (f ∘ there) (_ , x∈xs , p) |
| 382 |
|
| 383 |
mapWith∈⁻ : ∀ {n} (xs : Vec A n) (f : ∀ {x} → x ∈ xs → B) → |
| 384 |
Any P (mapWith∈ xs f) → |
| 385 |
∃₂ λ x (x∈xs : x ∈ xs) → P (f x∈xs) |
| 386 |
mapWith∈⁻ (y ∷ xs) f (here p) = (y , here refl , p) |
| 387 |
mapWith∈⁻ (y ∷ xs) f (there p) = |
| 388 |
Product.map id (Product.map there id) $ mapWith∈⁻ xs (f ∘ there) p |
| 389 |
|
| 390 |
mapWith∈↔ : ∀ {n} {xs : Vec A n} {f : ∀ {x} → x ∈ xs → B} → |
| 391 |
(∃₂ λ x (x∈xs : x ∈ xs) → P (f x∈xs)) ↔ Any P (mapWith∈ xs f) |
| 392 |
mapWith∈↔ = mk↔ₛ′ (mapWith∈⁺ _) (mapWith∈⁻ _ _) (to∘from _ _) (from∘to _) |
| 393 |
where |
| 394 |
from∘to : ∀ {n} {xs : Vec A n} (f : ∀ {x} → x ∈ xs → B) |
| 395 |
(p : ∃₂ λ x (x∈xs : x ∈ xs) → P (f x∈xs)) → |
| 396 |
mapWith∈⁻ xs f (mapWith∈⁺ f p) ≡ p |
| 397 |
from∘to f (_ , here refl , p) = refl |
| 398 |
from∘to f (_ , there x∈xs , p) |
| 399 |
rewrite from∘to (f ∘ there) (_ , x∈xs , p) = refl |
| 400 |
|
| 401 |
to∘from : ∀ {n} (xs : Vec A n) (f : ∀ {x} → x ∈ xs → B) |
| 402 |
(p : Any P (mapWith∈ xs f)) → |
| 403 |
mapWith∈⁺ f (mapWith∈⁻ xs f p) ≡ p |
| 404 |
to∘from (y ∷ xs) f (here p) = refl |
| 405 |
to∘from (y ∷ xs) f (there p) = |
| 406 |
cong there $ to∘from xs (f ∘ there) p |
| 407 |
|
| 408 |
------------------------------------------------------------------------ |
| 409 |
-- _∷_ |
| 410 |
|
| 411 |
∷↔ : ∀ {n} (P : Pred A p) {x} {xs : Vec A n} → |
| 412 |
(P x ⊎ Any P xs) ↔ Any P (x ∷ xs) |
| 413 |
∷↔ P {x} {xs} = ↔-trans (singleton↔ ⊎-cong ↔-refl) ++↔ |
| 414 |
|
| 415 |
------------------------------------------------------------------------ |
| 416 |
-- _>>=_ |
| 417 |
|
| 418 |
module _ {A B : Set a} {P : Pred B p} {m} {f : A → Vec B m} where |
| 419 |
|
| 420 |
open CartesianBind |
| 421 |
|
| 422 |
>>=↔ : ∀ {n} {xs : Vec A n} → Any (Any P ∘ f) xs ↔ Any P (xs >>= f) |
| 423 |
>>=↔ = ↔-trans map↔ concat↔ |