| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Properties related to All |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Vec.Relation.Unary.All.Properties where |
| 10 |
|
| 11 |
open import Data.Nat.Base using (ℕ; zero; suc; _+_) |
| 12 |
open import Data.Fin.Base using (Fin; zero; suc) |
| 13 |
open import Data.List.Base using ([]; _∷_) |
| 14 |
open import Data.List.Relation.Unary.All as List using ([]; _∷_) |
| 15 |
open import Data.Product.Base as Product using (_×_; _,_; uncurry; uncurry′) |
| 16 |
open import Data.Vec.Base as Vec using (Vec; []; _∷_; map; _++_; concat; |
| 17 |
tabulate; drop; take; toList; fromList) |
| 18 |
open import Data.Vec.Relation.Unary.All as All using (All; []; _∷_) |
| 19 |
open import Level using (Level) |
| 20 |
open import Function.Base using (_∘_; id) |
| 21 |
open import Function.Bundles using (_↔_; mk↔ₛ′) |
| 22 |
open import Relation.Unary using (Pred) renaming (_⊆_ to _⋐_) |
| 23 |
open import Relation.Binary.Core using (REL) |
| 24 |
open import Relation.Binary.PropositionalEquality.Core |
| 25 |
using (_≡_; refl; cong; cong₂) |
| 26 |
|
| 27 |
private |
| 28 |
variable |
| 29 |
a b p q : Level |
| 30 |
A : Set a |
| 31 |
B : Set b |
| 32 |
P : Pred A p |
| 33 |
Q : Pred B q |
| 34 |
m n : ℕ |
| 35 |
xs : Vec A n |
| 36 |
|
| 37 |
------------------------------------------------------------------------ |
| 38 |
-- lookup |
| 39 |
|
| 40 |
lookup⁺ : All P xs → ∀ i → P (Vec.lookup xs i) |
| 41 |
lookup⁺ (px ∷ _) zero = px |
| 42 |
lookup⁺ (_ ∷ pxs) (suc i) = lookup⁺ pxs i |
| 43 |
|
| 44 |
lookup⁻ : (∀ i → P (Vec.lookup xs i)) → All P xs |
| 45 |
lookup⁻ {xs = []} pxs = [] |
| 46 |
lookup⁻ {xs = _ ∷ _} pxs = pxs zero ∷ lookup⁻ (pxs ∘ suc) |
| 47 |
|
| 48 |
------------------------------------------------------------------------ |
| 49 |
-- map |
| 50 |
|
| 51 |
map⁺ : {f : A → B} → All (P ∘ f) xs → All P (map f xs) |
| 52 |
map⁺ [] = [] |
| 53 |
map⁺ (px ∷ pxs) = px ∷ map⁺ pxs |
| 54 |
|
| 55 |
map⁻ : {f : A → B} → All P (map f xs) → All (P ∘ f) xs |
| 56 |
map⁻ {xs = []} [] = [] |
| 57 |
map⁻ {xs = _ ∷ _} (px ∷ pxs) = px ∷ map⁻ pxs |
| 58 |
|
| 59 |
-- A variant of All.map |
| 60 |
|
| 61 |
gmap : {f : A → B} → P ⋐ Q ∘ f → All P {n} ⋐ All Q {n} ∘ map f |
| 62 |
gmap g = map⁺ ∘ All.map g |
| 63 |
|
| 64 |
------------------------------------------------------------------------ |
| 65 |
-- _++_ |
| 66 |
|
| 67 |
++⁺ : {xs : Vec A m} {ys : Vec A n} → |
| 68 |
All P xs → All P ys → All P (xs ++ ys) |
| 69 |
++⁺ [] pys = pys |
| 70 |
++⁺ (px ∷ pxs) pys = px ∷ ++⁺ pxs pys |
| 71 |
|
| 72 |
++ˡ⁻ : (xs : Vec A m) {ys : Vec A n} → |
| 73 |
All P (xs ++ ys) → All P xs |
| 74 |
++ˡ⁻ [] _ = [] |
| 75 |
++ˡ⁻ (x ∷ xs) (px ∷ pxs) = px ∷ ++ˡ⁻ xs pxs |
| 76 |
|
| 77 |
++ʳ⁻ : (xs : Vec A m) {ys : Vec A n} → |
| 78 |
All P (xs ++ ys) → All P ys |
| 79 |
++ʳ⁻ [] pys = pys |
| 80 |
++ʳ⁻ (x ∷ xs) (px ∷ pxs) = ++ʳ⁻ xs pxs |
| 81 |
|
| 82 |
++⁻ : (xs : Vec A m) {ys : Vec A n} → |
| 83 |
All P (xs ++ ys) → All P xs × All P ys |
| 84 |
++⁻ [] p = [] , p |
| 85 |
++⁻ (x ∷ xs) (px ∷ pxs) = Product.map₁ (px ∷_) (++⁻ _ pxs) |
| 86 |
|
| 87 |
++⁺∘++⁻ : (xs : Vec A m) {ys : Vec A n} → |
| 88 |
(p : All P (xs ++ ys)) → |
| 89 |
uncurry′ ++⁺ (++⁻ xs p) ≡ p |
| 90 |
++⁺∘++⁻ [] p = refl |
| 91 |
++⁺∘++⁻ (x ∷ xs) (px ∷ pxs) = cong (px ∷_) (++⁺∘++⁻ xs pxs) |
| 92 |
|
| 93 |
++⁻∘++⁺ : {xs : Vec A m} {ys : Vec A n} → |
| 94 |
(p : All P xs × All P ys) → |
| 95 |
++⁻ xs (uncurry ++⁺ p) ≡ p |
| 96 |
++⁻∘++⁺ ([] , pys) = refl |
| 97 |
++⁻∘++⁺ (px ∷ pxs , pys) rewrite ++⁻∘++⁺ (pxs , pys) = refl |
| 98 |
|
| 99 |
++↔ : {xs : Vec A m} {ys : Vec A n} → |
| 100 |
(All P xs × All P ys) ↔ All P (xs ++ ys) |
| 101 |
++↔ {xs = xs} = mk↔ₛ′ (uncurry ++⁺) (++⁻ xs) (++⁺∘++⁻ xs) ++⁻∘++⁺ |
| 102 |
|
| 103 |
------------------------------------------------------------------------ |
| 104 |
-- concat |
| 105 |
|
| 106 |
concat⁺ : {xss : Vec (Vec A m) n} → |
| 107 |
All (All P) xss → All P (concat xss) |
| 108 |
concat⁺ [] = [] |
| 109 |
concat⁺ (pxs ∷ pxss) = ++⁺ pxs (concat⁺ pxss) |
| 110 |
|
| 111 |
concat⁻ : (xss : Vec (Vec A m) n) → |
| 112 |
All P (concat xss) → All (All P) xss |
| 113 |
concat⁻ [] [] = [] |
| 114 |
concat⁻ (xs ∷ xss) pxss = ++ˡ⁻ xs pxss ∷ concat⁻ xss (++ʳ⁻ xs pxss) |
| 115 |
|
| 116 |
------------------------------------------------------------------------ |
| 117 |
-- swap |
| 118 |
|
| 119 |
module _ {_~_ : REL A B p} where |
| 120 |
|
| 121 |
All-swap : ∀ {n m xs ys} → |
| 122 |
All (λ x → All (x ~_) ys) {n} xs → |
| 123 |
All (λ y → All (_~ y) xs) {m} ys |
| 124 |
All-swap {ys = []} _ = [] |
| 125 |
All-swap {ys = y ∷ ys} [] = All.universal (λ _ → []) (y ∷ ys) |
| 126 |
All-swap {ys = y ∷ ys} ((x~y ∷ x~ys) ∷ pxs) = |
| 127 |
(x~y ∷ (All.map All.head pxs)) ∷ |
| 128 |
All-swap (x~ys ∷ (All.map All.tail pxs)) |
| 129 |
|
| 130 |
|
| 131 |
------------------------------------------------------------------------ |
| 132 |
-- tabulate |
| 133 |
|
| 134 |
module _ {P : A → Set p} where |
| 135 |
|
| 136 |
tabulate⁺ : ∀ {n} {f : Fin n → A} → |
| 137 |
(∀ i → P (f i)) → All P (tabulate f) |
| 138 |
tabulate⁺ {zero} Pf = [] |
| 139 |
tabulate⁺ {suc n} Pf = Pf zero ∷ tabulate⁺ (Pf ∘ suc) |
| 140 |
|
| 141 |
tabulate⁻ : ∀ {n} {f : Fin n → A} → |
| 142 |
All P (tabulate f) → (∀ i → P (f i)) |
| 143 |
tabulate⁻ (px ∷ _) zero = px |
| 144 |
tabulate⁻ (_ ∷ pf) (suc i) = tabulate⁻ pf i |
| 145 |
|
| 146 |
------------------------------------------------------------------------ |
| 147 |
-- take and drop |
| 148 |
|
| 149 |
drop⁺ : ∀ m {xs} → All P {m + n} xs → All P {n} (drop m xs) |
| 150 |
drop⁺ zero pxs = pxs |
| 151 |
drop⁺ (suc m) {x ∷ xs} (px ∷ pxs) = drop⁺ m pxs |
| 152 |
|
| 153 |
take⁺ : ∀ m {xs} → All P {m + n} xs → All P {m} (take m xs) |
| 154 |
take⁺ zero pxs = [] |
| 155 |
take⁺ (suc m) {x ∷ xs} (px ∷ pxs) = px ∷ take⁺ m pxs |
| 156 |
|
| 157 |
------------------------------------------------------------------------ |
| 158 |
-- toList |
| 159 |
|
| 160 |
module _ {P : Pred A p} where |
| 161 |
|
| 162 |
toList⁺ : ∀ {n} {xs : Vec A n} → All P xs → List.All P (toList xs) |
| 163 |
toList⁺ [] = [] |
| 164 |
toList⁺ (px ∷ pxs) = px ∷ toList⁺ pxs |
| 165 |
|
| 166 |
toList⁻ : ∀ {n} {xs : Vec A n} → List.All P (toList xs) → All P xs |
| 167 |
toList⁻ {xs = []} [] = [] |
| 168 |
toList⁻ {xs = x ∷ xs} (px ∷ pxs) = px ∷ toList⁻ pxs |
| 169 |
|
| 170 |
------------------------------------------------------------------------ |
| 171 |
-- fromList |
| 172 |
|
| 173 |
module _ {P : Pred A p} where |
| 174 |
|
| 175 |
fromList⁺ : ∀ {xs} → List.All P xs → All P (fromList xs) |
| 176 |
fromList⁺ [] = [] |
| 177 |
fromList⁺ (px ∷ pxs) = px ∷ fromList⁺ pxs |
| 178 |
|
| 179 |
fromList⁻ : ∀ {xs} → All P (fromList xs) → List.All P xs |
| 180 |
fromList⁻ {[]} [] = [] |
| 181 |
fromList⁻ {x ∷ xs} (px ∷ pxs) = px ∷ (fromList⁻ pxs) |