| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Some Vector-related properties |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Vec.Functional.Properties where |
| 10 |
|
| 11 |
open import Data.Fin.Base using (Fin; zero; suc; toℕ; fromℕ<; reduce≥; |
| 12 |
_↑ˡ_; _↑ʳ_; punchIn; punchOut) |
| 13 |
open import Data.Nat.Base as ℕ using (ℕ; zero; suc) |
| 14 |
import Data.Nat.Properties as ℕ |
| 15 |
open import Data.Product.Base as Product using (_×_; _,_; proj₁; proj₂) |
| 16 |
open import Data.List.Base as List using (List) |
| 17 |
import Data.List.Properties as List |
| 18 |
open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂) |
| 19 |
open import Data.Vec.Base as Vec using (Vec) |
| 20 |
import Data.Vec.Properties as Vec |
| 21 |
open import Data.Vec.Functional using (Vector; head; tail; updateAt; |
| 22 |
map; _++_; insertAt; removeAt; toVec; fromVec; toList; fromList) |
| 23 |
open import Function.Base using (_∘_; id) |
| 24 |
open import Level using (Level) |
| 25 |
open import Relation.Binary.Definitions using (DecidableEquality; Decidable) |
| 26 |
open import Relation.Binary.PropositionalEquality.Core |
| 27 |
using (_≡_; _≗_; refl; _≢_; cong) |
| 28 |
open import Relation.Binary.PropositionalEquality.Properties |
| 29 |
using (module ≡-Reasoning) |
| 30 |
open import Relation.Nullary.Decidable |
| 31 |
using (Dec; does; yes; no; map′; _×-dec_) |
| 32 |
open import Relation.Nullary.Negation using (contradiction) |
| 33 |
|
| 34 |
import Data.Fin.Properties as Finₚ |
| 35 |
|
| 36 |
private |
| 37 |
variable |
| 38 |
a b c : Level |
| 39 |
A B C : Set a |
| 40 |
m n : ℕ |
| 41 |
|
| 42 |
------------------------------------------------------------------------ |
| 43 |
|
| 44 |
module _ {xs ys : Vector A (suc n)} where |
| 45 |
|
| 46 |
∷-cong : head xs ≡ head ys → tail xs ≗ tail ys → xs ≗ ys |
| 47 |
∷-cong eq _ zero = eq |
| 48 |
∷-cong _ eq (suc i) = eq i |
| 49 |
|
| 50 |
∷-injective : xs ≗ ys → head xs ≡ head ys × tail xs ≗ tail ys |
| 51 |
∷-injective eq = eq zero , eq ∘ suc |
| 52 |
|
| 53 |
≗-dec : DecidableEquality A → Decidable {A = Vector A n} _≗_ |
| 54 |
≗-dec {n = zero} _≟_ xs ys = yes λ () |
| 55 |
≗-dec {n = suc n} _≟_ xs ys = |
| 56 |
map′ (Product.uncurry ∷-cong) ∷-injective |
| 57 |
(head xs ≟ head ys ×-dec ≗-dec _≟_ (tail xs) (tail ys)) |
| 58 |
|
| 59 |
------------------------------------------------------------------------ |
| 60 |
-- updateAt |
| 61 |
|
| 62 |
-- (+) updateAt i actually updates the element at index i. |
| 63 |
|
| 64 |
updateAt-updates : ∀ (i : Fin n) {f : A → A} (xs : Vector A n) → |
| 65 |
updateAt xs i f i ≡ f (xs i) |
| 66 |
updateAt-updates zero xs = refl |
| 67 |
updateAt-updates (suc i) xs = updateAt-updates i (tail xs) |
| 68 |
|
| 69 |
-- (-) updateAt i does not touch the elements at other indices. |
| 70 |
|
| 71 |
updateAt-minimal : ∀ (i j : Fin n) {f : A → A} (xs : Vector A n) → |
| 72 |
i ≢ j → updateAt xs j f i ≡ xs i |
| 73 |
updateAt-minimal zero zero xs 0≢0 = contradiction refl 0≢0 |
| 74 |
updateAt-minimal zero (suc j) xs _ = refl |
| 75 |
updateAt-minimal (suc i) zero xs _ = refl |
| 76 |
updateAt-minimal (suc i) (suc j) xs i≢j = updateAt-minimal i j (tail xs) (i≢j ∘ cong suc) |
| 77 |
|
| 78 |
-- updateAt i is a monoid morphism from A → A to Vector A n → Vector A n. |
| 79 |
|
| 80 |
updateAt-id-local : ∀ (i : Fin n) {f : A → A} (xs : Vector A n) → |
| 81 |
f (xs i) ≡ xs i → |
| 82 |
updateAt xs i f ≗ xs |
| 83 |
updateAt-id-local zero xs eq zero = eq |
| 84 |
updateAt-id-local zero xs eq (suc j) = refl |
| 85 |
updateAt-id-local (suc i) xs eq zero = refl |
| 86 |
updateAt-id-local (suc i) xs eq (suc j) = updateAt-id-local i (tail xs) eq j |
| 87 |
|
| 88 |
updateAt-id : ∀ (i : Fin n) (xs : Vector A n) → |
| 89 |
updateAt xs i id ≗ xs |
| 90 |
updateAt-id i xs = updateAt-id-local i xs refl |
| 91 |
|
| 92 |
updateAt-updateAt-local : ∀ (i : Fin n) {f g h : A → A} (xs : Vector A n) → |
| 93 |
f (g (xs i)) ≡ h (xs i) → |
| 94 |
updateAt (updateAt xs i g) i f ≗ updateAt xs i h |
| 95 |
updateAt-updateAt-local zero xs eq zero = eq |
| 96 |
updateAt-updateAt-local zero xs eq (suc j) = refl |
| 97 |
updateAt-updateAt-local (suc i) xs eq zero = refl |
| 98 |
updateAt-updateAt-local (suc i) xs eq (suc j) = updateAt-updateAt-local i (tail xs) eq j |
| 99 |
|
| 100 |
updateAt-updateAt : ∀ (i : Fin n) {f g : A → A} (xs : Vector A n) → |
| 101 |
updateAt (updateAt xs i g) i f ≗ updateAt xs i (f ∘ g) |
| 102 |
updateAt-updateAt i xs = updateAt-updateAt-local i xs refl |
| 103 |
|
| 104 |
updateAt-cong-local : ∀ (i : Fin n) {f g : A → A} (xs : Vector A n) → |
| 105 |
f (xs i) ≡ g (xs i) → |
| 106 |
updateAt xs i f ≗ updateAt xs i g |
| 107 |
updateAt-cong-local zero xs eq zero = eq |
| 108 |
updateAt-cong-local zero xs eq (suc j) = refl |
| 109 |
updateAt-cong-local (suc i) xs eq zero = refl |
| 110 |
updateAt-cong-local (suc i) xs eq (suc j) = updateAt-cong-local i (tail xs) eq j |
| 111 |
|
| 112 |
updateAt-cong : ∀ (i : Fin n) {f g : A → A} → f ≗ g → (xs : Vector A n) → |
| 113 |
updateAt xs i f ≗ updateAt xs i g |
| 114 |
updateAt-cong i eq xs = updateAt-cong-local i xs (eq (xs i)) |
| 115 |
|
| 116 |
-- The order of updates at different indices i ≢ j does not matter. |
| 117 |
|
| 118 |
updateAt-commutes : ∀ (i j : Fin n) {f g : A → A} → i ≢ j → (xs : Vector A n) → |
| 119 |
updateAt (updateAt xs j g) i f ≗ updateAt (updateAt xs i f) j g |
| 120 |
updateAt-commutes zero zero 0≢0 xs k = contradiction refl 0≢0 |
| 121 |
updateAt-commutes zero (suc j) _ xs zero = refl |
| 122 |
updateAt-commutes zero (suc j) _ xs (suc k) = refl |
| 123 |
updateAt-commutes (suc i) zero _ xs zero = refl |
| 124 |
updateAt-commutes (suc i) zero _ xs (suc k) = refl |
| 125 |
updateAt-commutes (suc i) (suc j) _ xs zero = refl |
| 126 |
updateAt-commutes (suc i) (suc j) i≢j xs (suc k) = updateAt-commutes i j (i≢j ∘ cong suc) (tail xs) k |
| 127 |
|
| 128 |
------------------------------------------------------------------------ |
| 129 |
-- map |
| 130 |
|
| 131 |
map-id : (xs : Vector A n) → map id xs ≗ xs |
| 132 |
map-id xs = λ _ → refl |
| 133 |
|
| 134 |
map-cong : ∀ {f g : A → B} → f ≗ g → (xs : Vector A n) → map f xs ≗ map g xs |
| 135 |
map-cong f≗g xs = f≗g ∘ xs |
| 136 |
|
| 137 |
map-∘ : ∀ {f : B → C} {g : A → B} (xs : Vector A n) → |
| 138 |
map (f ∘ g) xs ≗ map f (map g xs) |
| 139 |
map-∘ xs = λ _ → refl |
| 140 |
|
| 141 |
lookup-map : ∀ (i : Fin n) (f : A → B) (xs : Vector A n) → |
| 142 |
map f xs i ≡ f (xs i) |
| 143 |
lookup-map i f xs = refl |
| 144 |
|
| 145 |
map-updateAt-local : ∀ {f : A → B} {g : A → A} {h : B → B} |
| 146 |
(xs : Vector A n) (i : Fin n) → |
| 147 |
f (g (xs i)) ≡ h (f (xs i)) → |
| 148 |
map f (updateAt xs i g) ≗ updateAt (map f xs) i h |
| 149 |
map-updateAt-local {n = suc n} {f = f} xs zero eq zero = eq |
| 150 |
map-updateAt-local {n = suc n} {f = f} xs zero eq (suc j) = refl |
| 151 |
map-updateAt-local {n = suc (suc n)} {f = f} xs (suc i) eq zero = refl |
| 152 |
map-updateAt-local {n = suc (suc n)} {f = f} xs (suc i) eq (suc j) = map-updateAt-local {f = f} (tail xs) i eq j |
| 153 |
|
| 154 |
map-updateAt : ∀ {f : A → B} {g : A → A} {h : B → B} → |
| 155 |
f ∘ g ≗ h ∘ f → |
| 156 |
(xs : Vector A n) (i : Fin n) → |
| 157 |
map f (updateAt xs i g) ≗ updateAt (map f xs) i h |
| 158 |
map-updateAt {f = f} {g = g} f∘g≗h∘f xs i = map-updateAt-local {f = f} {g = g} xs i (f∘g≗h∘f (xs i)) |
| 159 |
|
| 160 |
------------------------------------------------------------------------ |
| 161 |
-- _++_ |
| 162 |
|
| 163 |
lookup-++-< : ∀ (xs : Vector A m) (ys : Vector A n) → |
| 164 |
∀ i (i<m : toℕ i ℕ.< m) → |
| 165 |
(xs ++ ys) i ≡ xs (fromℕ< i<m) |
| 166 |
lookup-++-< {m = m} xs ys i i<m = cong Sum.[ xs , ys ] (Finₚ.splitAt-< m i i<m) |
| 167 |
|
| 168 |
lookup-++-≥ : ∀ (xs : Vector A m) (ys : Vector A n) → |
| 169 |
∀ i (i≥m : toℕ i ℕ.≥ m) → |
| 170 |
(xs ++ ys) i ≡ ys (reduce≥ i i≥m) |
| 171 |
lookup-++-≥ {m = m} xs ys i i≥m = cong Sum.[ xs , ys ] (Finₚ.splitAt-≥ m i i≥m) |
| 172 |
|
| 173 |
lookup-++ˡ : ∀ (xs : Vector A m) (ys : Vector A n) i → |
| 174 |
(xs ++ ys) (i ↑ˡ n) ≡ xs i |
| 175 |
lookup-++ˡ {m = m} {n = n} xs ys i = cong Sum.[ xs , ys ] (Finₚ.splitAt-↑ˡ m i n) |
| 176 |
|
| 177 |
lookup-++ʳ : ∀ (xs : Vector A m) (ys : Vector A n) i → |
| 178 |
(xs ++ ys) (m ↑ʳ i) ≡ ys i |
| 179 |
lookup-++ʳ {m = m} {n = n} xs ys i = cong Sum.[ xs , ys ] (Finₚ.splitAt-↑ʳ m n i) |
| 180 |
|
| 181 |
module _ {ys ys′ : Vector A m} where |
| 182 |
|
| 183 |
++-cong : ∀ (xs xs′ : Vector A n) → |
| 184 |
xs ≗ xs′ → ys ≗ ys′ → xs ++ ys ≗ xs′ ++ ys′ |
| 185 |
++-cong {n} xs xs′ eq₁ eq₂ i with toℕ i ℕ.<? n |
| 186 |
... | yes i<n = begin |
| 187 |
(xs ++ ys) i ≡⟨ lookup-++-< xs ys i i<n ⟩ |
| 188 |
xs (fromℕ< i<n) ≡⟨ eq₁ (fromℕ< i<n) ⟩ |
| 189 |
xs′ (fromℕ< i<n) ≡⟨ lookup-++-< xs′ ys′ i i<n ⟨ |
| 190 |
(xs′ ++ ys′) i ∎ |
| 191 |
where open ≡-Reasoning |
| 192 |
... | no i≮n = begin |
| 193 |
(xs ++ ys) i ≡⟨ lookup-++-≥ xs ys i (ℕ.≮⇒≥ i≮n) ⟩ |
| 194 |
ys (reduce≥ i (ℕ.≮⇒≥ i≮n)) ≡⟨ eq₂ (reduce≥ i (ℕ.≮⇒≥ i≮n)) ⟩ |
| 195 |
ys′ (reduce≥ i (ℕ.≮⇒≥ i≮n)) ≡⟨ lookup-++-≥ xs′ ys′ i (ℕ.≮⇒≥ i≮n) ⟨ |
| 196 |
(xs′ ++ ys′) i ∎ |
| 197 |
where open ≡-Reasoning |
| 198 |
|
| 199 |
++-injectiveˡ : ∀ (xs xs′ : Vector A n) → |
| 200 |
xs ++ ys ≗ xs′ ++ ys′ → xs ≗ xs′ |
| 201 |
++-injectiveˡ xs xs′ eq i = begin |
| 202 |
xs i ≡⟨ lookup-++ˡ xs ys i ⟨ |
| 203 |
(xs ++ ys) (i ↑ˡ m) ≡⟨ eq (i ↑ˡ m) ⟩ |
| 204 |
(xs′ ++ ys′) (i ↑ˡ m) ≡⟨ lookup-++ˡ xs′ ys′ i ⟩ |
| 205 |
xs′ i ∎ |
| 206 |
where open ≡-Reasoning |
| 207 |
|
| 208 |
++-injectiveʳ : ∀ (xs xs′ : Vector A n) → xs ++ ys ≗ xs′ ++ ys′ → ys ≗ ys′ |
| 209 |
++-injectiveʳ {n} xs xs′ eq i = begin |
| 210 |
ys i ≡⟨ lookup-++ʳ xs ys i ⟨ |
| 211 |
(xs ++ ys) (n ↑ʳ i) ≡⟨ eq (n ↑ʳ i) ⟩ |
| 212 |
(xs′ ++ ys′) (n ↑ʳ i) ≡⟨ lookup-++ʳ xs′ ys′ i ⟩ |
| 213 |
ys′ i ∎ |
| 214 |
where open ≡-Reasoning |
| 215 |
|
| 216 |
++-injective : ∀ (xs xs′ : Vector A n) → |
| 217 |
xs ++ ys ≗ xs′ ++ ys′ → xs ≗ xs′ × ys ≗ ys′ |
| 218 |
++-injective xs xs′ eq = ++-injectiveˡ xs xs′ eq , ++-injectiveʳ xs xs′ eq |
| 219 |
|
| 220 |
------------------------------------------------------------------------ |
| 221 |
-- insertAt |
| 222 |
|
| 223 |
insertAt-lookup : ∀ (xs : Vector A n) (i : Fin (suc n)) (v : A) → |
| 224 |
insertAt xs i v i ≡ v |
| 225 |
insertAt-lookup {n = n} xs zero v = refl |
| 226 |
insertAt-lookup {n = suc n} xs (suc i) v = insertAt-lookup (tail xs) i v |
| 227 |
|
| 228 |
insertAt-punchIn : ∀ (xs : Vector A n) (i : Fin (suc n)) (v : A) |
| 229 |
(j : Fin n) → |
| 230 |
insertAt xs i v (punchIn i j) ≡ xs j |
| 231 |
insertAt-punchIn {n = suc n} xs zero v j = refl |
| 232 |
insertAt-punchIn {n = suc n} xs (suc i) v zero = refl |
| 233 |
insertAt-punchIn {n = suc n} xs (suc i) v (suc j) = insertAt-punchIn (tail xs) i v j |
| 234 |
|
| 235 |
------------------------------------------------------------------------ |
| 236 |
-- removeAt |
| 237 |
|
| 238 |
removeAt-punchOut : ∀ (xs : Vector A (suc n)) |
| 239 |
{i : Fin (suc n)} {j : Fin (suc n)} (i≢j : i ≢ j) → |
| 240 |
removeAt xs i (punchOut i≢j) ≡ xs j |
| 241 |
removeAt-punchOut {n = n} xs {zero} {zero} i≢j = contradiction refl i≢j |
| 242 |
removeAt-punchOut {n = suc n} xs {zero} {suc j} i≢j = refl |
| 243 |
removeAt-punchOut {n = suc n} xs {suc i} {zero} i≢j = refl |
| 244 |
removeAt-punchOut {n = suc n} xs {suc i} {suc j} i≢j = removeAt-punchOut (tail xs) (i≢j ∘ cong suc) |
| 245 |
|
| 246 |
removeAt-insertAt : ∀ (xs : Vector A n) (i : Fin (suc n)) (v : A) → |
| 247 |
removeAt (insertAt xs i v) i ≗ xs |
| 248 |
removeAt-insertAt xs zero v j = refl |
| 249 |
removeAt-insertAt xs (suc i) v zero = refl |
| 250 |
removeAt-insertAt xs (suc i) v (suc j) = removeAt-insertAt (tail xs) i v j |
| 251 |
|
| 252 |
insertAt-removeAt : ∀ (xs : Vector A (suc n)) (i : Fin (suc n)) → |
| 253 |
insertAt (removeAt xs i) i (xs i) ≗ xs |
| 254 |
insertAt-removeAt {n = n} xs zero zero = refl |
| 255 |
insertAt-removeAt {n = n} xs zero (suc j) = refl |
| 256 |
insertAt-removeAt {n = suc n} xs (suc i) zero = refl |
| 257 |
insertAt-removeAt {n = suc n} xs (suc i) (suc j) = insertAt-removeAt (tail xs) i j |
| 258 |
|
| 259 |
------------------------------------------------------------------------ |
| 260 |
-- Conversion functions |
| 261 |
|
| 262 |
toVec∘fromVec : (xs : Vec A n) → toVec (fromVec xs) ≡ xs |
| 263 |
toVec∘fromVec = Vec.tabulate∘lookup |
| 264 |
|
| 265 |
fromVec∘toVec : (xs : Vector A n) → fromVec (toVec xs) ≗ xs |
| 266 |
fromVec∘toVec = Vec.lookup∘tabulate |
| 267 |
|
| 268 |
toList∘fromList : (xs : List A) → toList (fromList xs) ≡ xs |
| 269 |
toList∘fromList = List.tabulate-lookup |
| 270 |
|
| 271 |
------------------------------------------------------------------------ |
| 272 |
-- DEPRECATED NAMES |
| 273 |
------------------------------------------------------------------------ |
| 274 |
-- Please use the new names as continuing support for the old names is |
| 275 |
-- not guaranteed. |
| 276 |
|
| 277 |
-- Version 2.0 |
| 278 |
|
| 279 |
updateAt-id-relative = updateAt-id-local |
| 280 |
{-# WARNING_ON_USAGE updateAt-id-relative |
| 281 |
"Warning: updateAt-id-relative was deprecated in v2.0. |
| 282 |
Please use updateAt-id-local instead." |
| 283 |
#-} |
| 284 |
updateAt-compose-relative = updateAt-updateAt-local |
| 285 |
{-# WARNING_ON_USAGE updateAt-compose-relative |
| 286 |
"Warning: updateAt-compose-relative was deprecated in v2.0. |
| 287 |
Please use updateAt-updateAt-local instead." |
| 288 |
#-} |
| 289 |
updateAt-compose = updateAt-updateAt |
| 290 |
{-# WARNING_ON_USAGE updateAt-compose |
| 291 |
"Warning: updateAt-compose was deprecated in v2.0. |
| 292 |
Please use updateAt-updateAt instead." |
| 293 |
#-} |
| 294 |
updateAt-cong-relative = updateAt-cong-local |
| 295 |
{-# WARNING_ON_USAGE updateAt-cong-relative |
| 296 |
"Warning: updateAt-cong-relative was deprecated in v2.0. |
| 297 |
Please use updateAt-cong-local instead." |
| 298 |
#-} |
| 299 |
|
| 300 |
insert-lookup = insertAt-lookup |
| 301 |
{-# WARNING_ON_USAGE insert-lookup |
| 302 |
"Warning: insert-lookup was deprecated in v2.0. |
| 303 |
Please use insertAt-lookup instead." |
| 304 |
#-} |
| 305 |
insert-punchIn = insertAt-punchIn |
| 306 |
{-# WARNING_ON_USAGE insert-punchIn |
| 307 |
"Warning: insert-punchIn was deprecated in v2.0. |
| 308 |
Please use insertAt-punchIn instead." |
| 309 |
#-} |
| 310 |
remove-punchOut = removeAt-punchOut |
| 311 |
{-# WARNING_ON_USAGE remove-punchOut |
| 312 |
"Warning: remove-punchOut was deprecated in v2.0. |
| 313 |
Please use removeAt-punchOut instead." |
| 314 |
#-} |
| 315 |
remove-insert = removeAt-insertAt |
| 316 |
{-# WARNING_ON_USAGE remove-insert |
| 317 |
"Warning: remove-insert was deprecated in v2.0. |
| 318 |
Please use removeAt-insertAt instead." |
| 319 |
#-} |
| 320 |
insert-remove = insertAt-removeAt |
| 321 |
{-# WARNING_ON_USAGE insert-remove |
| 322 |
"Warning: insert-remove was deprecated in v2.0. |
| 323 |
Please use insertAt-removeAt instead." |
| 324 |
#-} |