| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Some Vec-related properties |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Vec.Properties where |
| 10 |
|
| 11 |
open import Algebra.Definitions |
| 12 |
open import Data.Bool.Base using (true; false) |
| 13 |
open import Data.Fin.Base as Fin |
| 14 |
using (Fin; zero; suc; toℕ; fromℕ<; _↑ˡ_; _↑ʳ_) |
| 15 |
open import Data.List.Base as List using (List) |
| 16 |
import Data.List.Properties as List |
| 17 |
open import Data.Nat.Base |
| 18 |
using (ℕ; zero; suc; _+_; _≤_; _<_; s≤s; pred; s<s⁻¹; _≥_; s≤s⁻¹; z≤n) |
| 19 |
open import Data.Nat.Properties |
| 20 |
using (+-assoc; m≤n⇒m≤1+n; m≤m+n; ≤-refl; ≤-trans; ≤-irrelevant; ≤⇒≤″ |
| 21 |
; suc-injective; +-comm; +-suc; +-identityʳ) |
| 22 |
open import Data.Product.Base as Product |
| 23 |
using (_×_; _,_; proj₁; proj₂; <_,_>; uncurry) |
| 24 |
open import Data.Sum.Base using ([_,_]′) |
| 25 |
open import Data.Sum.Properties using ([,]-map) |
| 26 |
open import Data.Vec.Base |
| 27 |
open import Data.Vec.Relation.Binary.Equality.Cast as VecCast |
| 28 |
using (_≈[_]_; ≈-sym; ≈-cong′; module CastReasoning) |
| 29 |
open import Function.Base using (_∘_; id; _$_; const; _ˢ_; flip) |
| 30 |
open import Function.Bundles using (_↔_; mk↔ₛ′) |
| 31 |
open import Level using (Level) |
| 32 |
open import Relation.Binary.Definitions using (DecidableEquality) |
| 33 |
open import Relation.Binary.PropositionalEquality.Core |
| 34 |
using (_≡_; _≢_; _≗_; refl; sym; trans; cong; cong₂; subst) |
| 35 |
open import Relation.Binary.PropositionalEquality.Properties |
| 36 |
using (module ≡-Reasoning) |
| 37 |
open import Relation.Unary using (Pred; Decidable) |
| 38 |
open import Relation.Nullary.Decidable.Core |
| 39 |
using (Dec; does; yes; _×-dec_; map′) |
| 40 |
open import Relation.Nullary.Negation.Core using (contradiction) |
| 41 |
import Data.Nat.GeneralisedArithmetic as ℕ |
| 42 |
|
| 43 |
private |
| 44 |
m+n+o≡n+[m+o] : ∀ m n o → m + n + o ≡ n + (m + o) |
| 45 |
m+n+o≡n+[m+o] m n o = trans (cong (_+ o) (+-comm m n)) (+-assoc n m o) |
| 46 |
|
| 47 |
private |
| 48 |
variable |
| 49 |
a b c d p : Level |
| 50 |
A B C D : Set a |
| 51 |
w x y z : A |
| 52 |
m n o : ℕ |
| 53 |
ws xs ys zs : Vec A n |
| 54 |
|
| 55 |
------------------------------------------------------------------------ |
| 56 |
-- Properties of toList |
| 57 |
|
| 58 |
toList-injective : .(m≡n : m ≡ n) → (xs : Vec A m) (ys : Vec A n) → |
| 59 |
toList xs ≡ toList ys → xs ≈[ m≡n ] ys |
| 60 |
toList-injective m≡n [] [] xs=ys = refl |
| 61 |
toList-injective m≡n (x ∷ xs) (y ∷ ys) xs=ys = |
| 62 |
cong₂ _∷_ (List.∷-injectiveˡ xs=ys) (toList-injective (cong pred m≡n) xs ys (List.∷-injectiveʳ xs=ys)) |
| 63 |
|
| 64 |
------------------------------------------------------------------------ |
| 65 |
-- Properties of propositional equality over vectors |
| 66 |
|
| 67 |
∷-injectiveˡ : x ∷ xs ≡ y ∷ ys → x ≡ y |
| 68 |
∷-injectiveˡ refl = refl |
| 69 |
|
| 70 |
∷-injectiveʳ : x ∷ xs ≡ y ∷ ys → xs ≡ ys |
| 71 |
∷-injectiveʳ refl = refl |
| 72 |
|
| 73 |
∷-injective : x ∷ xs ≡ y ∷ ys → x ≡ y × xs ≡ ys |
| 74 |
∷-injective refl = refl , refl |
| 75 |
|
| 76 |
≡-dec : DecidableEquality A → DecidableEquality (Vec A n) |
| 77 |
≡-dec _≟_ [] [] = yes refl |
| 78 |
≡-dec _≟_ (x ∷ xs) (y ∷ ys) = map′ (uncurry (cong₂ _∷_)) |
| 79 |
∷-injective (x ≟ y ×-dec ≡-dec _≟_ xs ys) |
| 80 |
|
| 81 |
------------------------------------------------------------------------ |
| 82 |
-- _[_]=_ |
| 83 |
|
| 84 |
[]=-injective : ∀ {i} → xs [ i ]= x → xs [ i ]= y → x ≡ y |
| 85 |
[]=-injective here here = refl |
| 86 |
[]=-injective (there xsᵢ≡x) (there xsᵢ≡y) = []=-injective xsᵢ≡x xsᵢ≡y |
| 87 |
|
| 88 |
-- See also Data.Vec.Properties.WithK.[]=-irrelevant. |
| 89 |
|
| 90 |
------------------------------------------------------------------------ |
| 91 |
-- take |
| 92 |
|
| 93 |
unfold-take : ∀ n x (xs : Vec A (n + m)) → take (suc n) (x ∷ xs) ≡ x ∷ take n xs |
| 94 |
unfold-take n x xs = refl |
| 95 |
|
| 96 |
take-zipWith : ∀ (f : A → B → C) → |
| 97 |
(xs : Vec A (m + n)) (ys : Vec B (m + n)) → |
| 98 |
take m (zipWith f xs ys) ≡ zipWith f (take m xs) (take m ys) |
| 99 |
take-zipWith {m = zero} f xs ys = refl |
| 100 |
take-zipWith {m = suc m} f (x ∷ xs) (y ∷ ys) = cong (f x y ∷_) (take-zipWith f xs ys) |
| 101 |
|
| 102 |
take-map : ∀ (f : A → B) (m : ℕ) (xs : Vec A (m + n)) → |
| 103 |
take m (map f xs) ≡ map f (take m xs) |
| 104 |
take-map f zero xs = refl |
| 105 |
take-map f (suc m) (x ∷ xs) = cong (f x ∷_) (take-map f m xs) |
| 106 |
|
| 107 |
------------------------------------------------------------------------ |
| 108 |
-- drop |
| 109 |
|
| 110 |
unfold-drop : ∀ n x (xs : Vec A (n + m)) → |
| 111 |
drop (suc n) (x ∷ xs) ≡ drop n xs |
| 112 |
unfold-drop n x xs = refl |
| 113 |
|
| 114 |
drop-zipWith : (f : A → B → C) → |
| 115 |
(xs : Vec A (m + n)) (ys : Vec B (m + n)) → |
| 116 |
drop m (zipWith f xs ys) ≡ zipWith f (drop m xs) (drop m ys) |
| 117 |
drop-zipWith {m = zero} f xs ys = refl |
| 118 |
drop-zipWith {m = suc m} f (x ∷ xs) (y ∷ ys) = drop-zipWith f xs ys |
| 119 |
|
| 120 |
drop-map : ∀ (f : A → B) (m : ℕ) (xs : Vec A (m + n)) → |
| 121 |
drop m (map f xs) ≡ map f (drop m xs) |
| 122 |
drop-map f zero xs = refl |
| 123 |
drop-map f (suc m) (x ∷ xs) = drop-map f m xs |
| 124 |
|
| 125 |
------------------------------------------------------------------------ |
| 126 |
-- take and drop together |
| 127 |
|
| 128 |
take++drop≡id : ∀ (m : ℕ) (xs : Vec A (m + n)) → take m xs ++ drop m xs ≡ xs |
| 129 |
take++drop≡id zero xs = refl |
| 130 |
take++drop≡id (suc m) (x ∷ xs) = cong (x ∷_) (take++drop≡id m xs) |
| 131 |
|
| 132 |
------------------------------------------------------------------------ |
| 133 |
-- truncate |
| 134 |
|
| 135 |
truncate-refl : (xs : Vec A n) → truncate ≤-refl xs ≡ xs |
| 136 |
truncate-refl [] = refl |
| 137 |
truncate-refl (x ∷ xs) = cong (x ∷_) (truncate-refl xs) |
| 138 |
|
| 139 |
truncate-trans : ∀ {p} (m≤n : m ≤ n) (n≤p : n ≤ p) (xs : Vec A p) → |
| 140 |
truncate (≤-trans m≤n n≤p) xs ≡ truncate m≤n (truncate n≤p xs) |
| 141 |
truncate-trans z≤n n≤p xs = refl |
| 142 |
truncate-trans (s≤s m≤n) (s≤s n≤p) (x ∷ xs) = cong (x ∷_) (truncate-trans m≤n n≤p xs) |
| 143 |
|
| 144 |
truncate-irrelevant : (m≤n₁ m≤n₂ : m ≤ n) → truncate {A = A} m≤n₁ ≗ truncate m≤n₂ |
| 145 |
truncate-irrelevant m≤n₁ m≤n₂ xs = cong (λ m≤n → truncate m≤n xs) (≤-irrelevant m≤n₁ m≤n₂) |
| 146 |
|
| 147 |
truncate≡take : (m≤n : m ≤ n) (xs : Vec A n) .(eq : n ≡ m + o) → |
| 148 |
truncate m≤n xs ≡ take m (cast eq xs) |
| 149 |
truncate≡take z≤n _ eq = refl |
| 150 |
truncate≡take (s≤s m≤n) (x ∷ xs) eq = cong (x ∷_) (truncate≡take m≤n xs (suc-injective eq)) |
| 151 |
|
| 152 |
take≡truncate : ∀ m (xs : Vec A (m + n)) → |
| 153 |
take m xs ≡ truncate (m≤m+n m n) xs |
| 154 |
take≡truncate zero _ = refl |
| 155 |
take≡truncate (suc m) (x ∷ xs) = cong (x ∷_) (take≡truncate m xs) |
| 156 |
|
| 157 |
------------------------------------------------------------------------ |
| 158 |
-- pad |
| 159 |
|
| 160 |
padRight-refl : (a : A) (xs : Vec A n) → padRight ≤-refl a xs ≡ xs |
| 161 |
padRight-refl a [] = refl |
| 162 |
padRight-refl a (x ∷ xs) = cong (x ∷_) (padRight-refl a xs) |
| 163 |
|
| 164 |
padRight-replicate : (m≤n : m ≤ n) (a : A) → replicate n a ≡ padRight m≤n a (replicate m a) |
| 165 |
padRight-replicate z≤n a = refl |
| 166 |
padRight-replicate (s≤s m≤n) a = cong (a ∷_) (padRight-replicate m≤n a) |
| 167 |
|
| 168 |
padRight-trans : ∀ {p} (m≤n : m ≤ n) (n≤p : n ≤ p) (a : A) (xs : Vec A m) → |
| 169 |
padRight (≤-trans m≤n n≤p) a xs ≡ padRight n≤p a (padRight m≤n a xs) |
| 170 |
padRight-trans z≤n n≤p a [] = padRight-replicate n≤p a |
| 171 |
padRight-trans (s≤s m≤n) (s≤s n≤p) a (x ∷ xs) = cong (x ∷_) (padRight-trans m≤n n≤p a xs) |
| 172 |
|
| 173 |
------------------------------------------------------------------------ |
| 174 |
-- truncate and padRight together |
| 175 |
|
| 176 |
truncate-padRight : (m≤n : m ≤ n) (a : A) (xs : Vec A m) → |
| 177 |
truncate m≤n (padRight m≤n a xs) ≡ xs |
| 178 |
truncate-padRight z≤n a [] = refl |
| 179 |
truncate-padRight (s≤s m≤n) a (x ∷ xs) = cong (x ∷_) (truncate-padRight m≤n a xs) |
| 180 |
|
| 181 |
------------------------------------------------------------------------ |
| 182 |
-- lookup |
| 183 |
|
| 184 |
[]=⇒lookup : ∀ {i} → xs [ i ]= x → lookup xs i ≡ x |
| 185 |
[]=⇒lookup here = refl |
| 186 |
[]=⇒lookup (there xs[i]=x) = []=⇒lookup xs[i]=x |
| 187 |
|
| 188 |
lookup⇒[]= : ∀ (i : Fin n) xs → lookup xs i ≡ x → xs [ i ]= x |
| 189 |
lookup⇒[]= zero (_ ∷ _) refl = here |
| 190 |
lookup⇒[]= (suc i) (_ ∷ xs) p = there (lookup⇒[]= i xs p) |
| 191 |
|
| 192 |
[]=↔lookup : ∀ {i} → xs [ i ]= x ↔ lookup xs i ≡ x |
| 193 |
[]=↔lookup {xs = ys} {i = i} = |
| 194 |
mk↔ₛ′ []=⇒lookup (lookup⇒[]= i ys) ([]=⇒lookup∘lookup⇒[]= _ i) lookup⇒[]=∘[]=⇒lookup |
| 195 |
where |
| 196 |
lookup⇒[]=∘[]=⇒lookup : ∀ {i} (p : xs [ i ]= x) → |
| 197 |
lookup⇒[]= i xs ([]=⇒lookup p) ≡ p |
| 198 |
lookup⇒[]=∘[]=⇒lookup here = refl |
| 199 |
lookup⇒[]=∘[]=⇒lookup (there p) = cong there (lookup⇒[]=∘[]=⇒lookup p) |
| 200 |
|
| 201 |
[]=⇒lookup∘lookup⇒[]= : ∀ xs (i : Fin n) (p : lookup xs i ≡ x) → |
| 202 |
[]=⇒lookup (lookup⇒[]= i xs p) ≡ p |
| 203 |
[]=⇒lookup∘lookup⇒[]= (x ∷ xs) zero refl = refl |
| 204 |
[]=⇒lookup∘lookup⇒[]= (x ∷ xs) (suc i) p = []=⇒lookup∘lookup⇒[]= xs i p |
| 205 |
|
| 206 |
lookup-truncate : (m≤n : m ≤ n) (xs : Vec A n) (i : Fin m) → |
| 207 |
lookup (truncate m≤n xs) i ≡ lookup xs (Fin.inject≤ i m≤n) |
| 208 |
lookup-truncate (s≤s m≤m+n) (_ ∷ _) zero = refl |
| 209 |
lookup-truncate (s≤s m≤m+n) (_ ∷ xs) (suc i) = lookup-truncate m≤m+n xs i |
| 210 |
|
| 211 |
lookup-take-inject≤ : (xs : Vec A (m + n)) (i : Fin m) → |
| 212 |
lookup (take m xs) i ≡ lookup xs (Fin.inject≤ i (m≤m+n m n)) |
| 213 |
lookup-take-inject≤ {m = m} {n = n} xs i = begin |
| 214 |
lookup (take _ xs) i |
| 215 |
≡⟨ cong (λ ys → lookup ys i) (take≡truncate m xs) ⟩ |
| 216 |
lookup (truncate _ xs) i |
| 217 |
≡⟨ lookup-truncate (m≤m+n m n) xs i ⟩ |
| 218 |
lookup xs (Fin.inject≤ i (m≤m+n m n)) |
| 219 |
∎ where open ≡-Reasoning |
| 220 |
|
| 221 |
------------------------------------------------------------------------ |
| 222 |
-- updateAt (_[_]%=_) |
| 223 |
|
| 224 |
-- (+) updateAt i actually updates the element at index i. |
| 225 |
|
| 226 |
updateAt-updates : ∀ (i : Fin n) {f : A → A} (xs : Vec A n) → |
| 227 |
xs [ i ]= x → (updateAt xs i f) [ i ]= f x |
| 228 |
updateAt-updates zero (x ∷ xs) here = here |
| 229 |
updateAt-updates (suc i) (x ∷ xs) (there loc) = there (updateAt-updates i xs loc) |
| 230 |
|
| 231 |
-- (-) updateAt i does not touch the elements at other indices. |
| 232 |
|
| 233 |
updateAt-minimal : ∀ (i j : Fin n) {f : A → A} (xs : Vec A n) → |
| 234 |
i ≢ j → xs [ i ]= x → (updateAt xs j f) [ i ]= x |
| 235 |
updateAt-minimal zero zero (x ∷ xs) 0≢0 here = contradiction refl 0≢0 |
| 236 |
updateAt-minimal zero (suc j) (x ∷ xs) _ here = here |
| 237 |
updateAt-minimal (suc i) zero (x ∷ xs) _ (there loc) = there loc |
| 238 |
updateAt-minimal (suc i) (suc j) (x ∷ xs) i≢j (there loc) = |
| 239 |
there (updateAt-minimal i j xs (i≢j ∘ cong suc) loc) |
| 240 |
|
| 241 |
-- The other properties are consequences of (+) and (-). |
| 242 |
-- We spell the most natural properties out. |
| 243 |
-- Direct inductive proofs are in most cases easier than just using |
| 244 |
-- the defining properties. |
| 245 |
|
| 246 |
-- In the explanations, we make use of shorthand f = g ↾ x |
| 247 |
-- meaning that f and g agree locally at point x, i.e. f x ≡ g x. |
| 248 |
|
| 249 |
-- updateAt i is a morphism from the monoid of endofunctions A → A |
| 250 |
-- to the monoid of endofunctions Vec A n → Vec A n |
| 251 |
|
| 252 |
-- 1a. local identity: f = id ↾ (lookup xs i) |
| 253 |
-- implies updateAt i f = id ↾ xs |
| 254 |
|
| 255 |
updateAt-id-local : ∀ (i : Fin n) {f : A → A} (xs : Vec A n) → |
| 256 |
f (lookup xs i) ≡ lookup xs i → |
| 257 |
updateAt xs i f ≡ xs |
| 258 |
updateAt-id-local zero (x ∷ xs) eq = cong (_∷ xs) eq |
| 259 |
updateAt-id-local (suc i) (x ∷ xs) eq = cong (x ∷_) (updateAt-id-local i xs eq) |
| 260 |
|
| 261 |
-- 1b. identity: updateAt i id ≗ id |
| 262 |
|
| 263 |
updateAt-id : ∀ (i : Fin n) (xs : Vec A n) → updateAt xs i id ≡ xs |
| 264 |
updateAt-id i xs = updateAt-id-local i xs refl |
| 265 |
|
| 266 |
-- 2a. local composition: f ∘ g = h ↾ (lookup xs i) |
| 267 |
-- implies updateAt i f ∘ updateAt i g = updateAt i h ↾ xs |
| 268 |
|
| 269 |
updateAt-updateAt-local : ∀ (i : Fin n) {f g h : A → A} (xs : Vec A n) → |
| 270 |
f (g (lookup xs i)) ≡ h (lookup xs i) → |
| 271 |
updateAt (updateAt xs i g) i f ≡ updateAt xs i h |
| 272 |
updateAt-updateAt-local zero (x ∷ xs) fg=h = cong (_∷ xs) fg=h |
| 273 |
updateAt-updateAt-local (suc i) (x ∷ xs) fg=h = cong (x ∷_) (updateAt-updateAt-local i xs fg=h) |
| 274 |
|
| 275 |
-- 2b. composition: updateAt i f ∘ updateAt i g ≗ updateAt i (f ∘ g) |
| 276 |
|
| 277 |
updateAt-updateAt : ∀ (i : Fin n) {f g : A → A} (xs : Vec A n) → |
| 278 |
updateAt (updateAt xs i g) i f ≡ updateAt xs i (f ∘ g) |
| 279 |
updateAt-updateAt i xs = updateAt-updateAt-local i xs refl |
| 280 |
|
| 281 |
-- 3. congruence: updateAt i is a congruence wrt. extensional equality. |
| 282 |
|
| 283 |
-- 3a. If f = g ↾ (lookup xs i) |
| 284 |
-- then updateAt i f = updateAt i g ↾ xs |
| 285 |
|
| 286 |
updateAt-cong-local : ∀ (i : Fin n) {f g : A → A} (xs : Vec A n) → |
| 287 |
f (lookup xs i) ≡ g (lookup xs i) → |
| 288 |
updateAt xs i f ≡ updateAt xs i g |
| 289 |
updateAt-cong-local zero (x ∷ xs) f=g = cong (_∷ xs) f=g |
| 290 |
updateAt-cong-local (suc i) (x ∷ xs) f=g = cong (x ∷_) (updateAt-cong-local i xs f=g) |
| 291 |
|
| 292 |
-- 3b. congruence: f ≗ g → updateAt i f ≗ updateAt i g |
| 293 |
|
| 294 |
updateAt-cong : ∀ (i : Fin n) {f g : A → A} → f ≗ g → (xs : Vec A n) → |
| 295 |
updateAt xs i f ≡ updateAt xs i g |
| 296 |
updateAt-cong i f≗g xs = updateAt-cong-local i xs (f≗g (lookup xs i)) |
| 297 |
|
| 298 |
-- The order of updates at different indices i ≢ j does not matter. |
| 299 |
|
| 300 |
-- This a consequence of updateAt-updates and updateAt-minimal |
| 301 |
-- but easier to prove inductively. |
| 302 |
|
| 303 |
updateAt-commutes : ∀ (i j : Fin n) {f g : A → A} → i ≢ j → (xs : Vec A n) → |
| 304 |
updateAt (updateAt xs j g) i f ≡ updateAt (updateAt xs i f) j g |
| 305 |
updateAt-commutes zero zero 0≢0 (x ∷ xs) = contradiction refl 0≢0 |
| 306 |
updateAt-commutes zero (suc j) i≢j (x ∷ xs) = refl |
| 307 |
updateAt-commutes (suc i) zero i≢j (x ∷ xs) = refl |
| 308 |
updateAt-commutes (suc i) (suc j) i≢j (x ∷ xs) = |
| 309 |
cong (x ∷_) (updateAt-commutes i j (i≢j ∘ cong suc) xs) |
| 310 |
|
| 311 |
-- lookup after updateAt reduces. |
| 312 |
|
| 313 |
-- For same index this is an easy consequence of updateAt-updates |
| 314 |
-- using []=↔lookup. |
| 315 |
|
| 316 |
lookup∘updateAt : ∀ (i : Fin n) {f : A → A} xs → |
| 317 |
lookup (updateAt xs i f) i ≡ f (lookup xs i) |
| 318 |
lookup∘updateAt i xs = |
| 319 |
[]=⇒lookup (updateAt-updates i xs (lookup⇒[]= i _ refl)) |
| 320 |
|
| 321 |
-- For different indices it easily follows from updateAt-minimal. |
| 322 |
|
| 323 |
lookup∘updateAt′ : ∀ (i j : Fin n) {f : A → A} → i ≢ j → ∀ xs → |
| 324 |
lookup (updateAt xs j f) i ≡ lookup xs i |
| 325 |
lookup∘updateAt′ i j xs i≢j = |
| 326 |
[]=⇒lookup (updateAt-minimal i j i≢j xs (lookup⇒[]= i _ refl)) |
| 327 |
|
| 328 |
-- Aliases for notation _[_]%=_ |
| 329 |
|
| 330 |
[]%=-id : ∀ (xs : Vec A n) (i : Fin n) → xs [ i ]%= id ≡ xs |
| 331 |
[]%=-id xs i = updateAt-id i xs |
| 332 |
|
| 333 |
[]%=-∘ : ∀ (xs : Vec A n) (i : Fin n) {f g : A → A} → |
| 334 |
xs [ i ]%= f |
| 335 |
[ i ]%= g |
| 336 |
≡ xs [ i ]%= g ∘ f |
| 337 |
[]%=-∘ xs i = updateAt-updateAt i xs |
| 338 |
|
| 339 |
|
| 340 |
------------------------------------------------------------------------ |
| 341 |
-- _[_]≔_ (update) |
| 342 |
-- |
| 343 |
-- _[_]≔_ is defined in terms of updateAt, and all of its properties |
| 344 |
-- are special cases of the ones for updateAt. |
| 345 |
|
| 346 |
[]≔-idempotent : ∀ (xs : Vec A n) (i : Fin n) → |
| 347 |
(xs [ i ]≔ x) [ i ]≔ y ≡ xs [ i ]≔ y |
| 348 |
[]≔-idempotent xs i = updateAt-updateAt i xs |
| 349 |
|
| 350 |
[]≔-commutes : ∀ (xs : Vec A n) (i j : Fin n) → i ≢ j → |
| 351 |
(xs [ i ]≔ x) [ j ]≔ y ≡ (xs [ j ]≔ y) [ i ]≔ x |
| 352 |
[]≔-commutes xs i j i≢j = updateAt-commutes j i (i≢j ∘ sym) xs |
| 353 |
|
| 354 |
[]≔-updates : ∀ (xs : Vec A n) (i : Fin n) → (xs [ i ]≔ x) [ i ]= x |
| 355 |
[]≔-updates xs i = updateAt-updates i xs (lookup⇒[]= i xs refl) |
| 356 |
|
| 357 |
[]≔-minimal : ∀ (xs : Vec A n) (i j : Fin n) → i ≢ j → |
| 358 |
xs [ i ]= x → (xs [ j ]≔ y) [ i ]= x |
| 359 |
[]≔-minimal xs i j i≢j loc = updateAt-minimal i j xs i≢j loc |
| 360 |
|
| 361 |
[]≔-lookup : ∀ (xs : Vec A n) (i : Fin n) → xs [ i ]≔ lookup xs i ≡ xs |
| 362 |
[]≔-lookup xs i = updateAt-id-local i xs refl |
| 363 |
|
| 364 |
[]≔-++-↑ˡ : ∀ (xs : Vec A m) (ys : Vec A n) i → |
| 365 |
(xs ++ ys) [ i ↑ˡ n ]≔ x ≡ (xs [ i ]≔ x) ++ ys |
| 366 |
[]≔-++-↑ˡ (x ∷ xs) ys zero = refl |
| 367 |
[]≔-++-↑ˡ (x ∷ xs) ys (suc i) = |
| 368 |
cong (x ∷_) $ []≔-++-↑ˡ xs ys i |
| 369 |
|
| 370 |
[]≔-++-↑ʳ : ∀ (xs : Vec A m) (ys : Vec A n) i → |
| 371 |
(xs ++ ys) [ m ↑ʳ i ]≔ y ≡ xs ++ (ys [ i ]≔ y) |
| 372 |
[]≔-++-↑ʳ {m = zero} [] (y ∷ ys) i = refl |
| 373 |
[]≔-++-↑ʳ {m = suc n} (x ∷ xs) (y ∷ ys) i = cong (x ∷_) $ []≔-++-↑ʳ xs (y ∷ ys) i |
| 374 |
|
| 375 |
lookup∘update : ∀ (i : Fin n) (xs : Vec A n) x → |
| 376 |
lookup (xs [ i ]≔ x) i ≡ x |
| 377 |
lookup∘update i xs x = lookup∘updateAt i xs |
| 378 |
|
| 379 |
lookup∘update′ : ∀ {i j} → i ≢ j → ∀ (xs : Vec A n) y → |
| 380 |
lookup (xs [ j ]≔ y) i ≡ lookup xs i |
| 381 |
lookup∘update′ {i = i} {j} i≢j xs y = lookup∘updateAt′ i j i≢j xs |
| 382 |
|
| 383 |
------------------------------------------------------------------------ |
| 384 |
-- cast |
| 385 |
|
| 386 |
open VecCast public |
| 387 |
using (cast-is-id; cast-trans) |
| 388 |
|
| 389 |
subst-is-cast : (eq : m ≡ n) (xs : Vec A m) → subst (Vec A) eq xs ≡ cast eq xs |
| 390 |
subst-is-cast refl xs = sym (cast-is-id refl xs) |
| 391 |
|
| 392 |
cast-sym : .(eq : m ≡ n) {xs : Vec A m} {ys : Vec A n} → |
| 393 |
cast eq xs ≡ ys → cast (sym eq) ys ≡ xs |
| 394 |
cast-sym eq {xs = []} {ys = []} _ = refl |
| 395 |
cast-sym eq {xs = x ∷ xs} {ys = y ∷ ys} xxs[eq]≡yys = |
| 396 |
let x≡y , xs[eq]≡ys = ∷-injective xxs[eq]≡yys |
| 397 |
in cong₂ _∷_ (sym x≡y) (cast-sym (suc-injective eq) xs[eq]≡ys) |
| 398 |
|
| 399 |
------------------------------------------------------------------------ |
| 400 |
-- map |
| 401 |
|
| 402 |
map-id : map id ≗ id {A = Vec A n} |
| 403 |
map-id [] = refl |
| 404 |
map-id (x ∷ xs) = cong (x ∷_) (map-id xs) |
| 405 |
|
| 406 |
map-const : ∀ (xs : Vec A n) (y : B) → map (const y) xs ≡ replicate n y |
| 407 |
map-const [] _ = refl |
| 408 |
map-const (_ ∷ xs) y = cong (y ∷_) (map-const xs y) |
| 409 |
|
| 410 |
map-cast : (f : A → B) .(eq : m ≡ n) (xs : Vec A m) → |
| 411 |
map f (cast eq xs) ≡ cast eq (map f xs) |
| 412 |
map-cast f _ _ = sym (≈-cong′ (map f) refl) |
| 413 |
|
| 414 |
map-++ : ∀ (f : A → B) (xs : Vec A m) (ys : Vec A n) → |
| 415 |
map f (xs ++ ys) ≡ map f xs ++ map f ys |
| 416 |
map-++ f [] ys = refl |
| 417 |
map-++ f (x ∷ xs) ys = cong (f x ∷_) (map-++ f xs ys) |
| 418 |
|
| 419 |
map-cong : ∀ {f g : A → B} → f ≗ g → map {n = n} f ≗ map g |
| 420 |
map-cong f≗g [] = refl |
| 421 |
map-cong f≗g (x ∷ xs) = cong₂ _∷_ (f≗g x) (map-cong f≗g xs) |
| 422 |
|
| 423 |
map-∘ : ∀ (f : B → C) (g : A → B) → |
| 424 |
map {n = n} (f ∘ g) ≗ map f ∘ map g |
| 425 |
map-∘ f g [] = refl |
| 426 |
map-∘ f g (x ∷ xs) = cong (f (g x) ∷_) (map-∘ f g xs) |
| 427 |
|
| 428 |
lookup-map : ∀ (i : Fin n) (f : A → B) (xs : Vec A n) → |
| 429 |
lookup (map f xs) i ≡ f (lookup xs i) |
| 430 |
lookup-map zero f (x ∷ xs) = refl |
| 431 |
lookup-map (suc i) f (x ∷ xs) = lookup-map i f xs |
| 432 |
|
| 433 |
map-updateAt : ∀ {f : A → B} {g : A → A} {h : B → B} |
| 434 |
(xs : Vec A n) (i : Fin n) → |
| 435 |
f (g (lookup xs i)) ≡ h (f (lookup xs i)) → |
| 436 |
map f (updateAt xs i g) ≡ updateAt (map f xs) i h |
| 437 |
map-updateAt (x ∷ xs) zero eq = cong (_∷ _) eq |
| 438 |
map-updateAt (x ∷ xs) (suc i) eq = cong (_ ∷_) (map-updateAt xs i eq) |
| 439 |
|
| 440 |
map-insertAt : ∀ (f : A → B) (x : A) (xs : Vec A n) (i : Fin (suc n)) → |
| 441 |
map f (insertAt xs i x) ≡ insertAt (map f xs) i (f x) |
| 442 |
map-insertAt f _ [] Fin.zero = refl |
| 443 |
map-insertAt f _ (x' ∷ xs) Fin.zero = refl |
| 444 |
map-insertAt f x (x' ∷ xs) (Fin.suc i) = cong (_ ∷_) (map-insertAt f x xs i) |
| 445 |
|
| 446 |
map-[]≔ : ∀ (f : A → B) (xs : Vec A n) (i : Fin n) → |
| 447 |
map f (xs [ i ]≔ x) ≡ map f xs [ i ]≔ f x |
| 448 |
map-[]≔ f xs i = map-updateAt xs i refl |
| 449 |
|
| 450 |
map-⊛ : ∀ (f : A → B → C) (g : A → B) (xs : Vec A n) → |
| 451 |
(map f xs ⊛ map g xs) ≡ map (f ˢ g) xs |
| 452 |
map-⊛ f g [] = refl |
| 453 |
map-⊛ f g (x ∷ xs) = cong (f x (g x) ∷_) (map-⊛ f g xs) |
| 454 |
|
| 455 |
map-concat : (f : A → B) (xss : Vec (Vec A m) n) → |
| 456 |
map f (concat xss) ≡ concat (map (map f) xss) |
| 457 |
map-concat f [] = refl |
| 458 |
map-concat f (xs ∷ xss) = begin |
| 459 |
map f (concat (xs ∷ xss)) |
| 460 |
≡⟨⟩ |
| 461 |
map f (xs ++ concat xss) |
| 462 |
≡⟨ map-++ f xs (concat xss) ⟩ |
| 463 |
map f xs ++ map f (concat xss) |
| 464 |
≡⟨ cong (map f xs ++_) (map-concat f xss) ⟩ |
| 465 |
map f xs ++ concat (map (map f) xss) |
| 466 |
≡⟨⟩ |
| 467 |
concat (map (map f) (xs ∷ xss)) |
| 468 |
∎ where open ≡-Reasoning |
| 469 |
|
| 470 |
toList-map : ∀ (f : A → B) (xs : Vec A n) → |
| 471 |
toList (map f xs) ≡ List.map f (toList xs) |
| 472 |
toList-map f [] = refl |
| 473 |
toList-map f (x ∷ xs) = cong (f x List.∷_) (toList-map f xs) |
| 474 |
|
| 475 |
------------------------------------------------------------------------ |
| 476 |
-- _++_ |
| 477 |
|
| 478 |
-- See also Data.Vec.Properties.WithK.++-assoc. |
| 479 |
|
| 480 |
++-injectiveˡ : ∀ {n} (ws xs : Vec A n) → ws ++ ys ≡ xs ++ zs → ws ≡ xs |
| 481 |
++-injectiveˡ [] [] _ = refl |
| 482 |
++-injectiveˡ (_ ∷ ws) (_ ∷ xs) eq = |
| 483 |
cong₂ _∷_ (∷-injectiveˡ eq) (++-injectiveˡ _ _ (∷-injectiveʳ eq)) |
| 484 |
|
| 485 |
++-injectiveʳ : ∀ {n} (ws xs : Vec A n) → ws ++ ys ≡ xs ++ zs → ys ≡ zs |
| 486 |
++-injectiveʳ [] [] eq = eq |
| 487 |
++-injectiveʳ (x ∷ ws) (x′ ∷ xs) eq = |
| 488 |
++-injectiveʳ ws xs (∷-injectiveʳ eq) |
| 489 |
|
| 490 |
++-injective : ∀ (ws xs : Vec A n) → |
| 491 |
ws ++ ys ≡ xs ++ zs → ws ≡ xs × ys ≡ zs |
| 492 |
++-injective ws xs eq = |
| 493 |
(++-injectiveˡ ws xs eq , ++-injectiveʳ ws xs eq) |
| 494 |
|
| 495 |
++-assoc-eqFree : ∀ (xs : Vec A m) (ys : Vec A n) (zs : Vec A o) → let eq = +-assoc m n o in |
| 496 |
cast eq ((xs ++ ys) ++ zs) ≡ xs ++ (ys ++ zs) |
| 497 |
++-assoc-eqFree [] ys zs = cast-is-id refl (ys ++ zs) |
| 498 |
++-assoc-eqFree (x ∷ xs) ys zs = cong (x ∷_) (++-assoc-eqFree xs ys zs) |
| 499 |
|
| 500 |
++-identityʳ-eqFree : ∀ (xs : Vec A n) → cast (+-identityʳ n) (xs ++ []) ≡ xs |
| 501 |
++-identityʳ-eqFree [] = refl |
| 502 |
++-identityʳ-eqFree (x ∷ xs) = cong (x ∷_) (++-identityʳ-eqFree xs) |
| 503 |
|
| 504 |
cast-++ˡ : ∀ .(eq : m ≡ o) (xs : Vec A m) {ys : Vec A n} → |
| 505 |
cast (cong (_+ n) eq) (xs ++ ys) ≡ cast eq xs ++ ys |
| 506 |
cast-++ˡ _ _ {ys} = ≈-cong′ (_++ ys) refl |
| 507 |
|
| 508 |
cast-++ʳ : ∀ .(eq : n ≡ o) (xs : Vec A m) {ys : Vec A n} → |
| 509 |
cast (cong (m +_) eq) (xs ++ ys) ≡ xs ++ cast eq ys |
| 510 |
cast-++ʳ _ xs = ≈-cong′ (xs ++_) refl |
| 511 |
|
| 512 |
lookup-++-< : ∀ (xs : Vec A m) (ys : Vec A n) → |
| 513 |
∀ i (i<m : toℕ i < m) → |
| 514 |
lookup (xs ++ ys) i ≡ lookup xs (Fin.fromℕ< i<m) |
| 515 |
lookup-++-< (x ∷ xs) ys zero _ = refl |
| 516 |
lookup-++-< (x ∷ xs) ys (suc i) si<sm = lookup-++-< xs ys i (s<s⁻¹ si<sm) |
| 517 |
|
| 518 |
lookup-++-≥ : ∀ (xs : Vec A m) (ys : Vec A n) → |
| 519 |
∀ i (i≥m : toℕ i ≥ m) → |
| 520 |
lookup (xs ++ ys) i ≡ lookup ys (Fin.reduce≥ i i≥m) |
| 521 |
lookup-++-≥ [] ys i _ = refl |
| 522 |
lookup-++-≥ (x ∷ xs) ys (suc i) si≥sm = lookup-++-≥ xs ys i (s≤s⁻¹ si≥sm) |
| 523 |
|
| 524 |
lookup-++ˡ : ∀ (xs : Vec A m) (ys : Vec A n) i → |
| 525 |
lookup (xs ++ ys) (i ↑ˡ n) ≡ lookup xs i |
| 526 |
lookup-++ˡ (x ∷ xs) ys zero = refl |
| 527 |
lookup-++ˡ (x ∷ xs) ys (suc i) = lookup-++ˡ xs ys i |
| 528 |
|
| 529 |
lookup-++ʳ : ∀ (xs : Vec A m) (ys : Vec A n) i → |
| 530 |
lookup (xs ++ ys) (m ↑ʳ i) ≡ lookup ys i |
| 531 |
lookup-++ʳ [] ys zero = refl |
| 532 |
lookup-++ʳ [] (y ∷ xs) (suc i) = lookup-++ʳ [] xs i |
| 533 |
lookup-++ʳ (x ∷ xs) ys i = lookup-++ʳ xs ys i |
| 534 |
|
| 535 |
lookup-splitAt : ∀ m (xs : Vec A m) (ys : Vec A n) i → |
| 536 |
lookup (xs ++ ys) i ≡ [ lookup xs , lookup ys ]′ |
| 537 |
(Fin.splitAt m i) |
| 538 |
lookup-splitAt zero [] ys i = refl |
| 539 |
lookup-splitAt (suc m) (x ∷ xs) ys zero = refl |
| 540 |
lookup-splitAt (suc m) (x ∷ xs) ys (suc i) = trans |
| 541 |
(lookup-splitAt m xs ys i) |
| 542 |
(sym ([,]-map (Fin.splitAt m i))) |
| 543 |
|
| 544 |
toList-++ : (xs : Vec A n) (ys : Vec A m) → |
| 545 |
toList (xs ++ ys) ≡ toList xs List.++ toList ys |
| 546 |
toList-++ [] ys = refl |
| 547 |
toList-++ (x ∷ xs) ys = cong (x List.∷_) (toList-++ xs ys) |
| 548 |
|
| 549 |
------------------------------------------------------------------------ |
| 550 |
-- concat |
| 551 |
|
| 552 |
lookup-cast : .(eq : m ≡ n) (xs : Vec A m) (i : Fin m) → |
| 553 |
lookup (cast eq xs) (Fin.cast eq i) ≡ lookup xs i |
| 554 |
lookup-cast {n = suc _} eq (x ∷ _) zero = refl |
| 555 |
lookup-cast {n = suc _} eq (_ ∷ xs) (suc i) = |
| 556 |
lookup-cast (suc-injective eq) xs i |
| 557 |
|
| 558 |
lookup-cast₁ : .(eq : m ≡ n) (xs : Vec A m) (i : Fin n) → |
| 559 |
lookup (cast eq xs) i ≡ lookup xs (Fin.cast (sym eq) i) |
| 560 |
lookup-cast₁ eq (x ∷ _) zero = refl |
| 561 |
lookup-cast₁ eq (_ ∷ xs) (suc i) = |
| 562 |
lookup-cast₁ (suc-injective eq) xs i |
| 563 |
|
| 564 |
lookup-cast₂ : .(eq : m ≡ n) (xs : Vec A n) (i : Fin m) → |
| 565 |
lookup xs (Fin.cast eq i) ≡ lookup (cast (sym eq) xs) i |
| 566 |
lookup-cast₂ eq (x ∷ _) zero = refl |
| 567 |
lookup-cast₂ eq (_ ∷ xs) (suc i) = |
| 568 |
lookup-cast₂ (suc-injective eq) xs i |
| 569 |
|
| 570 |
lookup-concat : ∀ (xss : Vec (Vec A m) n) i j → |
| 571 |
lookup (concat xss) (Fin.combine i j) ≡ lookup (lookup xss i) j |
| 572 |
lookup-concat (xs ∷ xss) zero j = lookup-++ˡ xs (concat xss) j |
| 573 |
lookup-concat (xs ∷ xss) (suc i) j = begin |
| 574 |
lookup (concat (xs ∷ xss)) (Fin.combine (suc i) j) |
| 575 |
≡⟨ lookup-++ʳ xs (concat xss) (Fin.combine i j) ⟩ |
| 576 |
lookup (concat xss) (Fin.combine i j) |
| 577 |
≡⟨ lookup-concat xss i j ⟩ |
| 578 |
lookup (lookup (xs ∷ xss) (suc i)) j |
| 579 |
∎ where open ≡-Reasoning |
| 580 |
|
| 581 |
------------------------------------------------------------------------ |
| 582 |
-- zipWith |
| 583 |
|
| 584 |
module _ {f : A → A → A} where |
| 585 |
|
| 586 |
zipWith-assoc : Associative _≡_ f → |
| 587 |
Associative _≡_ (zipWith {n = n} f) |
| 588 |
zipWith-assoc assoc [] [] [] = refl |
| 589 |
zipWith-assoc assoc (x ∷ xs) (y ∷ ys) (z ∷ zs) = |
| 590 |
cong₂ _∷_ (assoc x y z) (zipWith-assoc assoc xs ys zs) |
| 591 |
|
| 592 |
zipWith-idem : Idempotent _≡_ f → |
| 593 |
Idempotent _≡_ (zipWith {n = n} f) |
| 594 |
zipWith-idem idem [] = refl |
| 595 |
zipWith-idem idem (x ∷ xs) = |
| 596 |
cong₂ _∷_ (idem x) (zipWith-idem idem xs) |
| 597 |
|
| 598 |
module _ {f : A → A → A} {e : A} where |
| 599 |
|
| 600 |
zipWith-identityˡ : LeftIdentity _≡_ e f → |
| 601 |
LeftIdentity _≡_ (replicate n e) (zipWith f) |
| 602 |
zipWith-identityˡ idˡ [] = refl |
| 603 |
zipWith-identityˡ idˡ (x ∷ xs) = |
| 604 |
cong₂ _∷_ (idˡ x) (zipWith-identityˡ idˡ xs) |
| 605 |
|
| 606 |
zipWith-identityʳ : RightIdentity _≡_ e f → |
| 607 |
RightIdentity _≡_ (replicate n e) (zipWith f) |
| 608 |
zipWith-identityʳ idʳ [] = refl |
| 609 |
zipWith-identityʳ idʳ (x ∷ xs) = |
| 610 |
cong₂ _∷_ (idʳ x) (zipWith-identityʳ idʳ xs) |
| 611 |
|
| 612 |
zipWith-zeroˡ : LeftZero _≡_ e f → |
| 613 |
LeftZero _≡_ (replicate n e) (zipWith f) |
| 614 |
zipWith-zeroˡ zeˡ [] = refl |
| 615 |
zipWith-zeroˡ zeˡ (x ∷ xs) = |
| 616 |
cong₂ _∷_ (zeˡ x) (zipWith-zeroˡ zeˡ xs) |
| 617 |
|
| 618 |
zipWith-zeroʳ : RightZero _≡_ e f → |
| 619 |
RightZero _≡_ (replicate n e) (zipWith f) |
| 620 |
zipWith-zeroʳ zeʳ [] = refl |
| 621 |
zipWith-zeroʳ zeʳ (x ∷ xs) = |
| 622 |
cong₂ _∷_ (zeʳ x) (zipWith-zeroʳ zeʳ xs) |
| 623 |
|
| 624 |
module _ {f : A → A → A} {e : A} {⁻¹ : A → A} where |
| 625 |
|
| 626 |
zipWith-inverseˡ : LeftInverse _≡_ e ⁻¹ f → |
| 627 |
LeftInverse _≡_ (replicate n e) (map ⁻¹) (zipWith f) |
| 628 |
zipWith-inverseˡ invˡ [] = refl |
| 629 |
zipWith-inverseˡ invˡ (x ∷ xs) = |
| 630 |
cong₂ _∷_ (invˡ x) (zipWith-inverseˡ invˡ xs) |
| 631 |
|
| 632 |
zipWith-inverseʳ : RightInverse _≡_ e ⁻¹ f → |
| 633 |
RightInverse _≡_ (replicate n e) (map ⁻¹) (zipWith f) |
| 634 |
zipWith-inverseʳ invʳ [] = refl |
| 635 |
zipWith-inverseʳ invʳ (x ∷ xs) = |
| 636 |
cong₂ _∷_ (invʳ x) (zipWith-inverseʳ invʳ xs) |
| 637 |
|
| 638 |
module _ {f g : A → A → A} where |
| 639 |
|
| 640 |
zipWith-distribˡ : _DistributesOverˡ_ _≡_ f g → |
| 641 |
_DistributesOverˡ_ _≡_ (zipWith {n = n} f) (zipWith g) |
| 642 |
zipWith-distribˡ distribˡ [] [] [] = refl |
| 643 |
zipWith-distribˡ distribˡ (x ∷ xs) (y ∷ ys) (z ∷ zs) = |
| 644 |
cong₂ _∷_ (distribˡ x y z) (zipWith-distribˡ distribˡ xs ys zs) |
| 645 |
|
| 646 |
zipWith-distribʳ : _DistributesOverʳ_ _≡_ f g → |
| 647 |
_DistributesOverʳ_ _≡_ (zipWith {n = n} f) (zipWith g) |
| 648 |
zipWith-distribʳ distribʳ [] [] [] = refl |
| 649 |
zipWith-distribʳ distribʳ (x ∷ xs) (y ∷ ys) (z ∷ zs) = |
| 650 |
cong₂ _∷_ (distribʳ x y z) (zipWith-distribʳ distribʳ xs ys zs) |
| 651 |
|
| 652 |
zipWith-absorbs : _Absorbs_ _≡_ f g → |
| 653 |
_Absorbs_ _≡_ (zipWith {n = n} f) (zipWith g) |
| 654 |
zipWith-absorbs abs [] [] = refl |
| 655 |
zipWith-absorbs abs (x ∷ xs) (y ∷ ys) = |
| 656 |
cong₂ _∷_ (abs x y) (zipWith-absorbs abs xs ys) |
| 657 |
|
| 658 |
module _ {f : A → B → C} {g : B → A → C} where |
| 659 |
|
| 660 |
zipWith-comm : ∀ (comm : ∀ x y → f x y ≡ g y x) (xs : Vec A n) ys → |
| 661 |
zipWith f xs ys ≡ zipWith g ys xs |
| 662 |
zipWith-comm comm [] [] = refl |
| 663 |
zipWith-comm comm (x ∷ xs) (y ∷ ys) = |
| 664 |
cong₂ _∷_ (comm x y) (zipWith-comm comm xs ys) |
| 665 |
|
| 666 |
zipWith-map₁ : ∀ (_⊕_ : B → C → D) (f : A → B) |
| 667 |
(xs : Vec A n) (ys : Vec C n) → |
| 668 |
zipWith _⊕_ (map f xs) ys ≡ zipWith (λ x y → f x ⊕ y) xs ys |
| 669 |
zipWith-map₁ _⊕_ f [] [] = refl |
| 670 |
zipWith-map₁ _⊕_ f (x ∷ xs) (y ∷ ys) = |
| 671 |
cong (f x ⊕ y ∷_) (zipWith-map₁ _⊕_ f xs ys) |
| 672 |
|
| 673 |
zipWith-map₂ : ∀ (_⊕_ : A → C → D) (f : B → C) |
| 674 |
(xs : Vec A n) (ys : Vec B n) → |
| 675 |
zipWith _⊕_ xs (map f ys) ≡ zipWith (λ x y → x ⊕ f y) xs ys |
| 676 |
zipWith-map₂ _⊕_ f [] [] = refl |
| 677 |
zipWith-map₂ _⊕_ f (x ∷ xs) (y ∷ ys) = |
| 678 |
cong (x ⊕ f y ∷_) (zipWith-map₂ _⊕_ f xs ys) |
| 679 |
|
| 680 |
lookup-zipWith : ∀ (f : A → B → C) (i : Fin n) xs ys → |
| 681 |
lookup (zipWith f xs ys) i ≡ f (lookup xs i) (lookup ys i) |
| 682 |
lookup-zipWith _ zero (x ∷ _) (y ∷ _) = refl |
| 683 |
lookup-zipWith _ (suc i) (_ ∷ xs) (_ ∷ ys) = lookup-zipWith _ i xs ys |
| 684 |
|
| 685 |
zipWith-++ : ∀ (f : A → B → C) |
| 686 |
(xs : Vec A n) (ys : Vec A m) |
| 687 |
(xs' : Vec B n) (ys' : Vec B m) → |
| 688 |
zipWith f (xs ++ ys) (xs' ++ ys') ≡ |
| 689 |
zipWith f xs xs' ++ zipWith f ys ys' |
| 690 |
zipWith-++ f [] ys [] ys' = refl |
| 691 |
zipWith-++ f (x ∷ xs) ys (x' ∷ xs') ys' = |
| 692 |
cong (_ ∷_) (zipWith-++ f xs ys xs' ys') |
| 693 |
|
| 694 |
------------------------------------------------------------------------ |
| 695 |
-- zip |
| 696 |
|
| 697 |
lookup-zip : ∀ (i : Fin n) (xs : Vec A n) (ys : Vec B n) → |
| 698 |
lookup (zip xs ys) i ≡ (lookup xs i , lookup ys i) |
| 699 |
lookup-zip = lookup-zipWith _,_ |
| 700 |
|
| 701 |
-- map lifts projections to vectors of products. |
| 702 |
|
| 703 |
map-proj₁-zip : ∀ (xs : Vec A n) (ys : Vec B n) → |
| 704 |
map proj₁ (zip xs ys) ≡ xs |
| 705 |
map-proj₁-zip [] [] = refl |
| 706 |
map-proj₁-zip (x ∷ xs) (y ∷ ys) = cong (x ∷_) (map-proj₁-zip xs ys) |
| 707 |
|
| 708 |
map-proj₂-zip : ∀ (xs : Vec A n) (ys : Vec B n) → |
| 709 |
map proj₂ (zip xs ys) ≡ ys |
| 710 |
map-proj₂-zip [] [] = refl |
| 711 |
map-proj₂-zip (x ∷ xs) (y ∷ ys) = cong (y ∷_) (map-proj₂-zip xs ys) |
| 712 |
|
| 713 |
-- map lifts pairing to vectors of products. |
| 714 |
|
| 715 |
map-<,>-zip : ∀ (f : A → B) (g : A → C) (xs : Vec A n) → |
| 716 |
map < f , g > xs ≡ zip (map f xs) (map g xs) |
| 717 |
map-<,>-zip f g [] = refl |
| 718 |
map-<,>-zip f g (x ∷ xs) = cong (_ ∷_) (map-<,>-zip f g xs) |
| 719 |
|
| 720 |
map-zip : ∀ (f : A → B) (g : C → D) (xs : Vec A n) (ys : Vec C n) → |
| 721 |
map (Product.map f g) (zip xs ys) ≡ zip (map f xs) (map g ys) |
| 722 |
map-zip f g [] [] = refl |
| 723 |
map-zip f g (x ∷ xs) (y ∷ ys) = cong (_ ∷_) (map-zip f g xs ys) |
| 724 |
|
| 725 |
------------------------------------------------------------------------ |
| 726 |
-- unzip |
| 727 |
|
| 728 |
lookup-unzip : ∀ (i : Fin n) (xys : Vec (A × B) n) → |
| 729 |
let xs , ys = unzip xys |
| 730 |
in (lookup xs i , lookup ys i) ≡ lookup xys i |
| 731 |
lookup-unzip () [] |
| 732 |
lookup-unzip zero ((x , y) ∷ xys) = refl |
| 733 |
lookup-unzip (suc i) ((x , y) ∷ xys) = lookup-unzip i xys |
| 734 |
|
| 735 |
map-unzip : ∀ (f : A → B) (g : C → D) (xys : Vec (A × C) n) → |
| 736 |
let xs , ys = unzip xys |
| 737 |
in (map f xs , map g ys) ≡ unzip (map (Product.map f g) xys) |
| 738 |
map-unzip f g [] = refl |
| 739 |
map-unzip f g ((x , y) ∷ xys) = |
| 740 |
cong (Product.map (f x ∷_) (g y ∷_)) (map-unzip f g xys) |
| 741 |
|
| 742 |
-- Products of vectors are isomorphic to vectors of products. |
| 743 |
|
| 744 |
unzip∘zip : ∀ (xs : Vec A n) (ys : Vec B n) → |
| 745 |
unzip (zip xs ys) ≡ (xs , ys) |
| 746 |
unzip∘zip [] [] = refl |
| 747 |
unzip∘zip (x ∷ xs) (y ∷ ys) = |
| 748 |
cong (Product.map (x ∷_) (y ∷_)) (unzip∘zip xs ys) |
| 749 |
|
| 750 |
zip∘unzip : ∀ (xys : Vec (A × B) n) → uncurry zip (unzip xys) ≡ xys |
| 751 |
zip∘unzip [] = refl |
| 752 |
zip∘unzip (xy ∷ xys) = cong (xy ∷_) (zip∘unzip xys) |
| 753 |
|
| 754 |
×v↔v× : (Vec A n × Vec B n) ↔ Vec (A × B) n |
| 755 |
×v↔v× = mk↔ₛ′ (uncurry zip) unzip zip∘unzip (uncurry unzip∘zip) |
| 756 |
|
| 757 |
------------------------------------------------------------------------ |
| 758 |
-- _⊛_ |
| 759 |
|
| 760 |
lookup-⊛ : ∀ i (fs : Vec (A → B) n) (xs : Vec A n) → |
| 761 |
lookup (fs ⊛ xs) i ≡ (lookup fs i $ lookup xs i) |
| 762 |
lookup-⊛ zero (f ∷ fs) (x ∷ xs) = refl |
| 763 |
lookup-⊛ (suc i) (f ∷ fs) (x ∷ xs) = lookup-⊛ i fs xs |
| 764 |
|
| 765 |
map-is-⊛ : ∀ (f : A → B) (xs : Vec A n) → |
| 766 |
map f xs ≡ (replicate n f ⊛ xs) |
| 767 |
map-is-⊛ f [] = refl |
| 768 |
map-is-⊛ f (x ∷ xs) = cong (_ ∷_) (map-is-⊛ f xs) |
| 769 |
|
| 770 |
⊛-is-zipWith : ∀ (fs : Vec (A → B) n) (xs : Vec A n) → |
| 771 |
(fs ⊛ xs) ≡ zipWith _$_ fs xs |
| 772 |
⊛-is-zipWith [] [] = refl |
| 773 |
⊛-is-zipWith (f ∷ fs) (x ∷ xs) = cong (f x ∷_) (⊛-is-zipWith fs xs) |
| 774 |
|
| 775 |
zipWith-is-⊛ : ∀ (f : A → B → C) (xs : Vec A n) (ys : Vec B n) → |
| 776 |
zipWith f xs ys ≡ (replicate n f ⊛ xs ⊛ ys) |
| 777 |
zipWith-is-⊛ f [] [] = refl |
| 778 |
zipWith-is-⊛ f (x ∷ xs) (y ∷ ys) = cong (_ ∷_) (zipWith-is-⊛ f xs ys) |
| 779 |
|
| 780 |
⊛-is->>= : ∀ (fs : Vec (A → B) n) (xs : Vec A n) → |
| 781 |
(fs ⊛ xs) ≡ (fs DiagonalBind.>>= flip map xs) |
| 782 |
⊛-is->>= [] [] = refl |
| 783 |
⊛-is->>= (f ∷ fs) (x ∷ xs) = cong (f x ∷_) $ begin |
| 784 |
fs ⊛ xs ≡⟨ ⊛-is->>= fs xs ⟩ |
| 785 |
diagonal (map (flip map xs) fs) ≡⟨⟩ |
| 786 |
diagonal (map (tail ∘ flip map (x ∷ xs)) fs) ≡⟨ cong diagonal (map-∘ _ _ _) ⟩ |
| 787 |
diagonal (map tail (map (flip map (x ∷ xs)) fs)) ∎ |
| 788 |
where open ≡-Reasoning |
| 789 |
|
| 790 |
------------------------------------------------------------------------ |
| 791 |
-- _⊛*_ |
| 792 |
|
| 793 |
lookup-⊛* : ∀ (fs : Vec (A → B) m) (xs : Vec A n) i j → |
| 794 |
lookup (fs ⊛* xs) (Fin.combine i j) ≡ (lookup fs i $ lookup xs j) |
| 795 |
lookup-⊛* (f ∷ fs) xs zero j = trans (lookup-++ˡ (map f xs) _ j) (lookup-map j f xs) |
| 796 |
lookup-⊛* (f ∷ fs) xs (suc i) j = trans (lookup-++ʳ (map f xs) _ (Fin.combine i j)) (lookup-⊛* fs xs i j) |
| 797 |
|
| 798 |
------------------------------------------------------------------------ |
| 799 |
-- foldl |
| 800 |
|
| 801 |
-- The (uniqueness part of the) universality property for foldl. |
| 802 |
|
| 803 |
foldl-universal : ∀ (B : ℕ → Set b) (f : FoldlOp A B) e |
| 804 |
(h : ∀ {c} (C : ℕ → Set c) (g : FoldlOp A C) (e : C zero) → |
| 805 |
∀ {n} → Vec A n → C n) → |
| 806 |
(∀ {c} {C} {g : FoldlOp A C} e → h {c} C g e [] ≡ e) → |
| 807 |
(∀ {c} {C} {g : FoldlOp A C} e {n} x → |
| 808 |
(h {c} C g e {suc n}) ∘ (x ∷_) ≗ h (C ∘ suc) g (g e x)) → |
| 809 |
h B f e ≗ foldl {n = n} B f e |
| 810 |
foldl-universal B f e h base step [] = base e |
| 811 |
foldl-universal B f e h base step (x ∷ xs) = begin |
| 812 |
h B f e (x ∷ xs) ≡⟨ step e x xs ⟩ |
| 813 |
h (B ∘ suc) f (f e x) xs ≡⟨ foldl-universal _ f (f e x) h base step xs ⟩ |
| 814 |
foldl (B ∘ suc) f (f e x) xs ≡⟨⟩ |
| 815 |
foldl B f e (x ∷ xs) ∎ |
| 816 |
where open ≡-Reasoning |
| 817 |
|
| 818 |
foldl-fusion : ∀ {B : ℕ → Set b} {C : ℕ → Set c} |
| 819 |
(h : ∀ {n} → B n → C n) → |
| 820 |
{f : FoldlOp A B} {d : B zero} → |
| 821 |
{g : FoldlOp A C} {e : C zero} → |
| 822 |
(h d ≡ e) → |
| 823 |
(∀ {n} b x → h (f {n} b x) ≡ g (h b) x) → |
| 824 |
h ∘ foldl {n = n} B f d ≗ foldl C g e |
| 825 |
foldl-fusion h {f} {d} {g} {e} base fuse [] = base |
| 826 |
foldl-fusion h {f} {d} {g} {e} base fuse (x ∷ xs) = |
| 827 |
foldl-fusion h eq fuse xs |
| 828 |
where |
| 829 |
open ≡-Reasoning |
| 830 |
eq : h (f d x) ≡ g e x |
| 831 |
eq = begin |
| 832 |
h (f d x) ≡⟨ fuse d x ⟩ |
| 833 |
g (h d) x ≡⟨ cong (λ e → g e x) base ⟩ |
| 834 |
g e x ∎ |
| 835 |
|
| 836 |
foldl-[] : ∀ (B : ℕ → Set b) (f : FoldlOp A B) {e} → foldl B f e [] ≡ e |
| 837 |
foldl-[] _ _ = refl |
| 838 |
|
| 839 |
------------------------------------------------------------------------ |
| 840 |
-- foldr |
| 841 |
|
| 842 |
-- See also Data.Vec.Properties.WithK.foldr-cong. |
| 843 |
|
| 844 |
-- The (uniqueness part of the) universality property for foldr. |
| 845 |
|
| 846 |
module _ (B : ℕ → Set b) (f : FoldrOp A B) {e : B zero} where |
| 847 |
|
| 848 |
foldr-universal : (h : ∀ {n} → Vec A n → B n) → |
| 849 |
h [] ≡ e → |
| 850 |
(∀ {n} x → h ∘ (x ∷_) ≗ f {n} x ∘ h) → |
| 851 |
h ≗ foldr {n = n} B f e |
| 852 |
foldr-universal h base step [] = base |
| 853 |
foldr-universal h base step (x ∷ xs) = begin |
| 854 |
h (x ∷ xs) ≡⟨ step x xs ⟩ |
| 855 |
f x (h xs) ≡⟨ cong (f x) (foldr-universal h base step xs) ⟩ |
| 856 |
f x (foldr B f e xs) ∎ |
| 857 |
where open ≡-Reasoning |
| 858 |
|
| 859 |
foldr-[] : foldr B f e [] ≡ e |
| 860 |
foldr-[] = refl |
| 861 |
|
| 862 |
foldr-++ : ∀ (xs : Vec A m) → |
| 863 |
foldr B f e (xs ++ ys) ≡ foldr (B ∘ (_+ n)) f (foldr B f e ys) xs |
| 864 |
foldr-++ [] = refl |
| 865 |
foldr-++ (x ∷ xs) = cong (f x) (foldr-++ xs) |
| 866 |
|
| 867 |
-- fusion and identity as consequences of universality |
| 868 |
|
| 869 |
foldr-fusion : ∀ {B : ℕ → Set b} {f : FoldrOp A B} e |
| 870 |
{C : ℕ → Set c} {g : FoldrOp A C} |
| 871 |
(h : ∀ {n} → B n → C n) → |
| 872 |
(∀ {n} x → h ∘ f {n} x ≗ g x ∘ h) → |
| 873 |
h ∘ foldr {n = n} B f e ≗ foldr C g (h e) |
| 874 |
foldr-fusion {B = B} {f} e {C} h fuse = |
| 875 |
foldr-universal C _ _ refl (λ x xs → fuse x (foldr B f e xs)) |
| 876 |
|
| 877 |
id-is-foldr : id ≗ foldr {n = n} (Vec A) _∷_ [] |
| 878 |
id-is-foldr = foldr-universal _ _ id refl (λ _ _ → refl) |
| 879 |
|
| 880 |
map-is-foldr : ∀ (f : A → B) → |
| 881 |
map {n = n} f ≗ foldr (Vec B) (λ x ys → f x ∷ ys) [] |
| 882 |
map-is-foldr f = foldr-universal (Vec _) (λ x ys → f x ∷ ys) (map f) refl (λ _ _ → refl) |
| 883 |
|
| 884 |
++-is-foldr : ∀ (xs : Vec A m) → |
| 885 |
xs ++ ys ≡ foldr (Vec A ∘ (_+ n)) _∷_ ys xs |
| 886 |
++-is-foldr {A = A} {n = n} {ys} xs = |
| 887 |
foldr-universal (Vec A ∘ (_+ n)) _∷_ (_++ ys) refl (λ _ _ → refl) xs |
| 888 |
|
| 889 |
------------------------------------------------------------------------ |
| 890 |
-- _∷ʳ_ |
| 891 |
|
| 892 |
-- snoc is snoc |
| 893 |
|
| 894 |
unfold-∷ʳ-eqFree : ∀ x (xs : Vec A n) → cast (+-comm 1 n) (xs ∷ʳ x) ≡ xs ++ [ x ] |
| 895 |
unfold-∷ʳ-eqFree x [] = refl |
| 896 |
unfold-∷ʳ-eqFree x (y ∷ xs) = cong (y ∷_) (unfold-∷ʳ-eqFree x xs) |
| 897 |
|
| 898 |
∷ʳ-injective : ∀ (xs ys : Vec A n) → xs ∷ʳ x ≡ ys ∷ʳ y → xs ≡ ys × x ≡ y |
| 899 |
∷ʳ-injective [] [] refl = (refl , refl) |
| 900 |
∷ʳ-injective (x ∷ xs) (y ∷ ys) eq with ∷-injective eq |
| 901 |
... | refl , eq′ = Product.map₁ (cong (x ∷_)) (∷ʳ-injective xs ys eq′) |
| 902 |
|
| 903 |
∷ʳ-injectiveˡ : ∀ (xs ys : Vec A n) → xs ∷ʳ x ≡ ys ∷ʳ y → xs ≡ ys |
| 904 |
∷ʳ-injectiveˡ xs ys eq = proj₁ (∷ʳ-injective xs ys eq) |
| 905 |
|
| 906 |
∷ʳ-injectiveʳ : ∀ (xs ys : Vec A n) → xs ∷ʳ x ≡ ys ∷ʳ y → x ≡ y |
| 907 |
∷ʳ-injectiveʳ xs ys eq = proj₂ (∷ʳ-injective xs ys eq) |
| 908 |
|
| 909 |
foldl-∷ʳ : ∀ (B : ℕ → Set b) (f : FoldlOp A B) e y (ys : Vec A n) → |
| 910 |
foldl B f e (ys ∷ʳ y) ≡ f (foldl B f e ys) y |
| 911 |
foldl-∷ʳ B f e y [] = refl |
| 912 |
foldl-∷ʳ B f e y (x ∷ xs) = foldl-∷ʳ (B ∘ suc) f (f e x) y xs |
| 913 |
|
| 914 |
foldr-∷ʳ : ∀ (B : ℕ → Set b) (f : FoldrOp A B) {e} y (ys : Vec A n) → |
| 915 |
foldr B f e (ys ∷ʳ y) ≡ foldr (B ∘ suc) f (f y e) ys |
| 916 |
foldr-∷ʳ B f y [] = refl |
| 917 |
foldr-∷ʳ B f y (x ∷ xs) = cong (f x) (foldr-∷ʳ B f y xs) |
| 918 |
|
| 919 |
-- init, last and _∷ʳ_ |
| 920 |
|
| 921 |
init-∷ʳ : ∀ x (xs : Vec A n) → init (xs ∷ʳ x) ≡ xs |
| 922 |
init-∷ʳ x [] = refl |
| 923 |
init-∷ʳ x (y ∷ xs) = cong (y ∷_) (init-∷ʳ x xs) |
| 924 |
|
| 925 |
last-∷ʳ : ∀ x (xs : Vec A n) → last (xs ∷ʳ x) ≡ x |
| 926 |
last-∷ʳ x [] = refl |
| 927 |
last-∷ʳ x (y ∷ xs) = last-∷ʳ x xs |
| 928 |
|
| 929 |
-- map and _∷ʳ_ |
| 930 |
|
| 931 |
map-∷ʳ : ∀ (f : A → B) x (xs : Vec A n) → map f (xs ∷ʳ x) ≡ map f xs ∷ʳ f x |
| 932 |
map-∷ʳ f x [] = refl |
| 933 |
map-∷ʳ f x (y ∷ xs) = cong (f y ∷_) (map-∷ʳ f x xs) |
| 934 |
|
| 935 |
-- cast and _∷ʳ_ |
| 936 |
|
| 937 |
cast-∷ʳ : ∀ .(eq : suc n ≡ suc m) x (xs : Vec A n) → |
| 938 |
cast eq (xs ∷ʳ x) ≡ (cast (cong pred eq) xs) ∷ʳ x |
| 939 |
cast-∷ʳ _ x _ = ≈-cong′ (_∷ʳ x) refl |
| 940 |
|
| 941 |
-- _++_ and _∷ʳ_ |
| 942 |
|
| 943 |
++-∷ʳ-eqFree : ∀ z (xs : Vec A m) (ys : Vec A n) → let eq = sym (+-suc m n) in |
| 944 |
cast eq ((xs ++ ys) ∷ʳ z) ≡ xs ++ (ys ∷ʳ z) |
| 945 |
++-∷ʳ-eqFree {m = zero} z [] [] = refl |
| 946 |
++-∷ʳ-eqFree {m = zero} z [] (y ∷ ys) = cong (y ∷_) (++-∷ʳ-eqFree z [] ys) |
| 947 |
++-∷ʳ-eqFree {m = suc m} z (x ∷ xs) ys = cong (x ∷_) (++-∷ʳ-eqFree z xs ys) |
| 948 |
|
| 949 |
∷ʳ-++-eqFree : ∀ a (xs : Vec A n) {ys : Vec A m} → let eq = sym (+-suc n m) in |
| 950 |
cast eq ((xs ∷ʳ a) ++ ys) ≡ xs ++ (a ∷ ys) |
| 951 |
∷ʳ-++-eqFree a [] {ys} = cong (a ∷_) (cast-is-id refl ys) |
| 952 |
∷ʳ-++-eqFree a (x ∷ xs) {ys} = cong (x ∷_) (∷ʳ-++-eqFree a xs) |
| 953 |
|
| 954 |
------------------------------------------------------------------------ |
| 955 |
-- reverse |
| 956 |
|
| 957 |
-- reverse of cons is snoc of reverse. |
| 958 |
|
| 959 |
reverse-∷ : ∀ x (xs : Vec A n) → reverse (x ∷ xs) ≡ reverse xs ∷ʳ x |
| 960 |
reverse-∷ x xs = sym (foldl-fusion (_∷ʳ x) refl (λ b x → refl) xs) |
| 961 |
|
| 962 |
-- foldl after a reverse is a foldr |
| 963 |
|
| 964 |
foldl-reverse : ∀ (B : ℕ → Set b) (f : FoldlOp A B) {e} → |
| 965 |
foldl {n = n} B f e ∘ reverse ≗ foldr B (flip f) e |
| 966 |
foldl-reverse _ _ {e} [] = refl |
| 967 |
foldl-reverse B f {e} (x ∷ xs) = begin |
| 968 |
foldl B f e (reverse (x ∷ xs)) ≡⟨ cong (foldl B f e) (reverse-∷ x xs) ⟩ |
| 969 |
foldl B f e (reverse xs ∷ʳ x) ≡⟨ foldl-∷ʳ B f e x (reverse xs) ⟩ |
| 970 |
f (foldl B f e (reverse xs)) x ≡⟨ cong (flip f x) (foldl-reverse B f xs) ⟩ |
| 971 |
f (foldr B (flip f) e xs) x ≡⟨⟩ |
| 972 |
foldr B (flip f) e (x ∷ xs) ∎ |
| 973 |
where open ≡-Reasoning |
| 974 |
|
| 975 |
-- foldr after a reverse is a foldl |
| 976 |
|
| 977 |
foldr-reverse : ∀ (B : ℕ → Set b) (f : FoldrOp A B) {e} → |
| 978 |
foldr {n = n} B f e ∘ reverse ≗ foldl B (flip f) e |
| 979 |
foldr-reverse B f {e} xs = foldl-fusion (foldr B f e) refl (λ _ _ → refl) xs |
| 980 |
|
| 981 |
-- reverse is involutive. |
| 982 |
|
| 983 |
reverse-involutive : Involutive {A = Vec A n} _≡_ reverse |
| 984 |
reverse-involutive xs = begin |
| 985 |
reverse (reverse xs) ≡⟨ foldl-reverse (Vec _) (flip _∷_) xs ⟩ |
| 986 |
foldr (Vec _) _∷_ [] xs ≡⟨ id-is-foldr xs ⟨ |
| 987 |
xs ∎ |
| 988 |
where open ≡-Reasoning |
| 989 |
|
| 990 |
reverse-reverse : reverse xs ≡ ys → reverse ys ≡ xs |
| 991 |
reverse-reverse {xs = xs} {ys} eq = begin |
| 992 |
reverse ys ≡⟨ cong reverse eq ⟨ |
| 993 |
reverse (reverse xs) ≡⟨ reverse-involutive xs ⟩ |
| 994 |
xs ∎ |
| 995 |
where open ≡-Reasoning |
| 996 |
|
| 997 |
-- reverse is injective. |
| 998 |
|
| 999 |
reverse-injective : reverse xs ≡ reverse ys → xs ≡ ys |
| 1000 |
reverse-injective {xs = xs} {ys} eq = begin |
| 1001 |
xs ≡⟨ reverse-reverse eq ⟨ |
| 1002 |
reverse (reverse ys) ≡⟨ reverse-involutive ys ⟩ |
| 1003 |
ys ∎ |
| 1004 |
where open ≡-Reasoning |
| 1005 |
|
| 1006 |
-- init and last of reverse |
| 1007 |
|
| 1008 |
init-reverse : init ∘ reverse ≗ reverse ∘ tail {A = A} {n = n} |
| 1009 |
init-reverse (x ∷ xs) = begin |
| 1010 |
init (reverse (x ∷ xs)) ≡⟨ cong init (reverse-∷ x xs) ⟩ |
| 1011 |
init (reverse xs ∷ʳ x) ≡⟨ init-∷ʳ x (reverse xs) ⟩ |
| 1012 |
reverse xs ∎ |
| 1013 |
where open ≡-Reasoning |
| 1014 |
|
| 1015 |
last-reverse : last ∘ reverse ≗ head {A = A} {n = n} |
| 1016 |
last-reverse (x ∷ xs) = begin |
| 1017 |
last (reverse (x ∷ xs)) ≡⟨ cong last (reverse-∷ x xs) ⟩ |
| 1018 |
last (reverse xs ∷ʳ x) ≡⟨ last-∷ʳ x (reverse xs) ⟩ |
| 1019 |
x ∎ |
| 1020 |
where open ≡-Reasoning |
| 1021 |
|
| 1022 |
-- map and reverse |
| 1023 |
|
| 1024 |
map-reverse : ∀ (f : A → B) (xs : Vec A n) → |
| 1025 |
map f (reverse xs) ≡ reverse (map f xs) |
| 1026 |
map-reverse f [] = refl |
| 1027 |
map-reverse f (x ∷ xs) = begin |
| 1028 |
map f (reverse (x ∷ xs)) ≡⟨ cong (map f) (reverse-∷ x xs) ⟩ |
| 1029 |
map f (reverse xs ∷ʳ x) ≡⟨ map-∷ʳ f x (reverse xs) ⟩ |
| 1030 |
map f (reverse xs) ∷ʳ f x ≡⟨ cong (_∷ʳ f x) (map-reverse f xs ) ⟩ |
| 1031 |
reverse (map f xs) ∷ʳ f x ≡⟨ reverse-∷ (f x) (map f xs) ⟨ |
| 1032 |
reverse (f x ∷ map f xs) ≡⟨⟩ |
| 1033 |
reverse (map f (x ∷ xs)) ∎ |
| 1034 |
where open ≡-Reasoning |
| 1035 |
|
| 1036 |
-- append and reverse |
| 1037 |
toList-∷ʳ : ∀ x (xs : Vec A n) → toList (xs ∷ʳ x) ≡ toList xs List.++ List.[ x ] |
| 1038 |
toList-∷ʳ x [] = refl |
| 1039 |
toList-∷ʳ x (y ∷ xs) = cong (y List.∷_) (toList-∷ʳ x xs) |
| 1040 |
|
| 1041 |
toList-reverse : ∀ (xs : Vec A n) → toList (reverse xs) ≡ List.reverse (toList xs) |
| 1042 |
toList-reverse [] = refl |
| 1043 |
toList-reverse (x ∷ xs) = begin |
| 1044 |
toList (reverse (x ∷ xs)) ≡⟨ cong toList (reverse-∷ x xs) ⟩ |
| 1045 |
toList (reverse xs ∷ʳ x) ≡⟨ toList-∷ʳ x (reverse xs) ⟩ |
| 1046 |
toList (reverse xs) List.++ List.[ x ] ≡⟨ cong (List._++ List.[ x ]) (toList-reverse xs) ⟩ |
| 1047 |
List.reverse (toList xs) List.++ List.[ x ] ≡⟨ List.unfold-reverse x (toList xs) ⟨ |
| 1048 |
List.reverse (toList (x ∷ xs)) ∎ |
| 1049 |
where open ≡-Reasoning |
| 1050 |
|
| 1051 |
reverse-++-eqFree : ∀ (xs : Vec A m) (ys : Vec A n) |
| 1052 |
→ reverse (xs ++ ys) ≈[ +-comm m n ] reverse ys ++ reverse xs |
| 1053 |
reverse-++-eqFree {m = m} {n = n} xs ys = |
| 1054 |
toList-injective (+-comm m n) (reverse (xs ++ ys)) (reverse ys ++ reverse xs) $ |
| 1055 |
begin |
| 1056 |
toList (reverse (xs ++ ys)) ≡⟨ toList-reverse ((xs ++ ys)) ⟩ |
| 1057 |
List.reverse (toList (xs ++ ys)) ≡⟨ cong List.reverse (toList-++ xs ys) ⟩ |
| 1058 |
List.reverse (toList xs List.++ toList ys) ≡⟨ List.reverse-++ (toList xs) (toList ys) ⟩ |
| 1059 |
List.reverse (toList ys) List.++ List.reverse (toList xs) ≡⟨ cong₂ List._++_ (toList-reverse ys) (toList-reverse xs) ⟨ |
| 1060 |
toList (reverse ys) List.++ toList (reverse xs) ≡⟨ toList-++ (reverse ys) (reverse xs) ⟨ |
| 1061 |
toList (reverse ys ++ reverse xs) ∎ |
| 1062 |
where open ≡-Reasoning |
| 1063 |
|
| 1064 |
cast-reverse : ∀ .(eq : m ≡ n) → cast eq ∘ reverse {A = A} {n = m} ≗ reverse ∘ cast eq |
| 1065 |
cast-reverse _ _ = ≈-cong′ reverse refl |
| 1066 |
|
| 1067 |
------------------------------------------------------------------------ |
| 1068 |
-- _ʳ++_ |
| 1069 |
|
| 1070 |
-- reverse-append is append of reverse. |
| 1071 |
|
| 1072 |
unfold-ʳ++ : ∀ (xs : Vec A m) (ys : Vec A n) → xs ʳ++ ys ≡ reverse xs ++ ys |
| 1073 |
unfold-ʳ++ xs ys = sym (foldl-fusion (_++ ys) refl (λ b x → refl) xs) |
| 1074 |
|
| 1075 |
-- foldr after a reverse-append is a foldl |
| 1076 |
|
| 1077 |
foldr-ʳ++ : ∀ (B : ℕ → Set b) (f : FoldrOp A B) {e} |
| 1078 |
(xs : Vec A m) {ys : Vec A n} → |
| 1079 |
foldr B f e (xs ʳ++ ys) ≡ |
| 1080 |
foldl (B ∘ (_+ n)) (flip f) (foldr B f e ys) xs |
| 1081 |
foldr-ʳ++ B f {e} xs = foldl-fusion (foldr B f e) refl (λ _ _ → refl) xs |
| 1082 |
|
| 1083 |
-- map and _ʳ++_ |
| 1084 |
|
| 1085 |
map-ʳ++ : ∀ (f : A → B) (xs : Vec A m) → |
| 1086 |
map f (xs ʳ++ ys) ≡ map f xs ʳ++ map f ys |
| 1087 |
map-ʳ++ {ys = ys} f xs = begin |
| 1088 |
map f (xs ʳ++ ys) ≡⟨ cong (map f) (unfold-ʳ++ xs ys) ⟩ |
| 1089 |
map f (reverse xs ++ ys) ≡⟨ map-++ f (reverse xs) ys ⟩ |
| 1090 |
map f (reverse xs) ++ map f ys ≡⟨ cong (_++ map f ys) (map-reverse f xs) ⟩ |
| 1091 |
reverse (map f xs) ++ map f ys ≡⟨ unfold-ʳ++ (map f xs) (map f ys) ⟨ |
| 1092 |
map f xs ʳ++ map f ys ∎ |
| 1093 |
where open ≡-Reasoning |
| 1094 |
|
| 1095 |
toList-ʳ++ : ∀ (xs : Vec A m) {ys : Vec A n} → |
| 1096 |
toList (xs ʳ++ ys) ≡ (toList xs) List.ʳ++ toList ys |
| 1097 |
toList-ʳ++ xs {ys} = begin |
| 1098 |
toList (xs ʳ++ ys) ≡⟨ cong toList (unfold-ʳ++ xs ys) ⟩ |
| 1099 |
toList (reverse xs ++ ys) ≡⟨ toList-++ ((reverse xs )) ys ⟩ |
| 1100 |
toList (reverse xs) List.++ toList ys ≡⟨ cong (List._++ toList ys) (toList-reverse xs) ⟩ |
| 1101 |
List.reverse (toList xs) List.++ toList ys ≡⟨ List.ʳ++-defn (toList xs) ⟨ |
| 1102 |
toList xs List.ʳ++ toList ys ∎ |
| 1103 |
where open ≡-Reasoning |
| 1104 |
|
| 1105 |
|
| 1106 |
++-ʳ++-eqFree : ∀ (xs : Vec A m) {ys : Vec A n} {zs : Vec A o} → let eq = m+n+o≡n+[m+o] m n o in |
| 1107 |
cast eq ((xs ++ ys) ʳ++ zs) ≡ ys ʳ++ (xs ʳ++ zs) |
| 1108 |
++-ʳ++-eqFree {m = m} {n} {o} xs {ys} {zs} = |
| 1109 |
toList-injective (m+n+o≡n+[m+o] m n o) ((xs ++ ys) ʳ++ zs) (ys ʳ++ (xs ʳ++ zs)) $ |
| 1110 |
begin |
| 1111 |
toList ((xs ++ ys) ʳ++ zs) ≡⟨ toList-ʳ++ (xs ++ ys) ⟩ |
| 1112 |
toList (xs ++ ys) List.ʳ++ toList zs ≡⟨ cong (List._ʳ++ toList zs) (toList-++ xs ys) ⟩ |
| 1113 |
((toList xs) List.++ toList ys ) List.ʳ++ toList zs ≡⟨ List.++-ʳ++ (toList xs) ⟩ |
| 1114 |
toList ys List.ʳ++ (toList xs List.ʳ++ toList zs) ≡⟨ cong (toList ys List.ʳ++_) (toList-ʳ++ xs) ⟨ |
| 1115 |
toList ys List.ʳ++ toList (xs ʳ++ zs) ≡⟨ toList-ʳ++ ys ⟨ |
| 1116 |
toList (ys ʳ++ (xs ʳ++ zs)) ∎ |
| 1117 |
where open ≡-Reasoning |
| 1118 |
|
| 1119 |
ʳ++-ʳ++-eqFree : ∀ (xs : Vec A m) {ys : Vec A n} {zs : Vec A o} → let eq = m+n+o≡n+[m+o] m n o in |
| 1120 |
cast eq ((xs ʳ++ ys) ʳ++ zs) ≡ ys ʳ++ (xs ++ zs) |
| 1121 |
ʳ++-ʳ++-eqFree {m = m} {n} {o} xs {ys} {zs} = |
| 1122 |
toList-injective (m+n+o≡n+[m+o] m n o) ((xs ʳ++ ys) ʳ++ zs) (ys ʳ++ (xs ++ zs)) $ |
| 1123 |
begin |
| 1124 |
toList ((xs ʳ++ ys) ʳ++ zs) ≡⟨ toList-ʳ++ (xs ʳ++ ys) ⟩ |
| 1125 |
toList (xs ʳ++ ys) List.ʳ++ toList zs ≡⟨ cong (List._ʳ++ toList zs) (toList-ʳ++ xs) ⟩ |
| 1126 |
(toList xs List.ʳ++ toList ys) List.ʳ++ toList zs ≡⟨ List.ʳ++-ʳ++ (toList xs) ⟩ |
| 1127 |
toList ys List.ʳ++ (toList xs List.++ toList zs) ≡⟨ cong (toList ys List.ʳ++_) (toList-++ xs zs) ⟨ |
| 1128 |
toList ys List.ʳ++ (toList (xs ++ zs)) ≡⟨ toList-ʳ++ ys ⟨ |
| 1129 |
toList (ys ʳ++ (xs ++ zs)) ∎ |
| 1130 |
where open ≡-Reasoning |
| 1131 |
|
| 1132 |
------------------------------------------------------------------------ |
| 1133 |
--sum |
| 1134 |
|
| 1135 |
sum-++ : ∀ (xs : Vec ℕ m) → sum (xs ++ ys) ≡ sum xs + sum ys |
| 1136 |
sum-++ {_} [] = refl |
| 1137 |
sum-++ {ys = ys} (x ∷ xs) = begin |
| 1138 |
x + sum (xs ++ ys) ≡⟨ cong (x +_) (sum-++ xs) ⟩ |
| 1139 |
x + (sum xs + sum ys) ≡⟨ +-assoc x (sum xs) (sum ys) ⟨ |
| 1140 |
sum (x ∷ xs) + sum ys ∎ |
| 1141 |
where open ≡-Reasoning |
| 1142 |
|
| 1143 |
------------------------------------------------------------------------ |
| 1144 |
-- replicate |
| 1145 |
|
| 1146 |
lookup-replicate : ∀ (i : Fin n) (x : A) → lookup (replicate n x) i ≡ x |
| 1147 |
lookup-replicate zero x = refl |
| 1148 |
lookup-replicate (suc i) x = lookup-replicate i x |
| 1149 |
|
| 1150 |
map-replicate : ∀ (f : A → B) (x : A) n → |
| 1151 |
map f (replicate n x) ≡ replicate n (f x) |
| 1152 |
map-replicate f x zero = refl |
| 1153 |
map-replicate f x (suc n) = cong (f x ∷_) (map-replicate f x n) |
| 1154 |
|
| 1155 |
transpose-replicate : ∀ (xs : Vec A m) → |
| 1156 |
transpose (replicate n xs) ≡ map (replicate n) xs |
| 1157 |
transpose-replicate {n = zero} _ = sym (map-const _ []) |
| 1158 |
transpose-replicate {n = suc n} xs = begin |
| 1159 |
transpose (replicate (suc n) xs) ≡⟨⟩ |
| 1160 |
(replicate _ _∷_ ⊛ xs ⊛ transpose (replicate _ xs)) ≡⟨ cong₂ _⊛_ (sym (map-is-⊛ _∷_ xs)) (transpose-replicate xs) ⟩ |
| 1161 |
(map _∷_ xs ⊛ map (replicate n) xs) ≡⟨ map-⊛ _∷_ (replicate n) xs ⟩ |
| 1162 |
map (replicate (suc n)) xs ∎ |
| 1163 |
where open ≡-Reasoning |
| 1164 |
|
| 1165 |
zipWith-replicate : ∀ (_⊕_ : A → B → C) (x : A) (y : B) → |
| 1166 |
zipWith _⊕_ (replicate n x) (replicate n y) ≡ replicate n (x ⊕ y) |
| 1167 |
zipWith-replicate {n = zero} _⊕_ x y = refl |
| 1168 |
zipWith-replicate {n = suc n} _⊕_ x y = cong (x ⊕ y ∷_) (zipWith-replicate _⊕_ x y) |
| 1169 |
|
| 1170 |
zipWith-replicate₁ : ∀ (_⊕_ : A → B → C) (x : A) (ys : Vec B n) → |
| 1171 |
zipWith _⊕_ (replicate n x) ys ≡ map (x ⊕_) ys |
| 1172 |
zipWith-replicate₁ _⊕_ x [] = refl |
| 1173 |
zipWith-replicate₁ _⊕_ x (y ∷ ys) = |
| 1174 |
cong (x ⊕ y ∷_) (zipWith-replicate₁ _⊕_ x ys) |
| 1175 |
|
| 1176 |
zipWith-replicate₂ : ∀ (_⊕_ : A → B → C) (xs : Vec A n) (y : B) → |
| 1177 |
zipWith _⊕_ xs (replicate n y) ≡ map (_⊕ y) xs |
| 1178 |
zipWith-replicate₂ _⊕_ [] y = refl |
| 1179 |
zipWith-replicate₂ _⊕_ (x ∷ xs) y = |
| 1180 |
cong (x ⊕ y ∷_) (zipWith-replicate₂ _⊕_ xs y) |
| 1181 |
|
| 1182 |
toList-replicate : ∀ (n : ℕ) (x : A) → |
| 1183 |
toList (replicate n x) ≡ List.replicate n x |
| 1184 |
toList-replicate zero x = refl |
| 1185 |
toList-replicate (suc n) x = cong (_ List.∷_) (toList-replicate n x) |
| 1186 |
|
| 1187 |
------------------------------------------------------------------------ |
| 1188 |
-- iterate |
| 1189 |
|
| 1190 |
iterate-id : ∀ (x : A) n → iterate id x n ≡ replicate n x |
| 1191 |
iterate-id x zero = refl |
| 1192 |
iterate-id x (suc n) = cong (_ ∷_) (iterate-id (id x) n) |
| 1193 |
|
| 1194 |
take-iterate : ∀ n f (x : A) → take n (iterate f x (n + m)) ≡ iterate f x n |
| 1195 |
take-iterate zero f x = refl |
| 1196 |
take-iterate (suc n) f x = cong (_ ∷_) (take-iterate n f (f x)) |
| 1197 |
|
| 1198 |
drop-iterate : ∀ n f (x : A) → drop n (iterate f x (n + zero)) ≡ [] |
| 1199 |
drop-iterate zero f x = refl |
| 1200 |
drop-iterate (suc n) f x = drop-iterate n f (f x) |
| 1201 |
|
| 1202 |
lookup-iterate : ∀ f (x : A) (i : Fin n) → lookup (iterate f x n) i ≡ ℕ.iterate f x (toℕ i) |
| 1203 |
lookup-iterate f x zero = refl |
| 1204 |
lookup-iterate f x (suc i) = lookup-iterate f (f x) i |
| 1205 |
|
| 1206 |
toList-iterate : ∀ f (x : A) n → toList (iterate f x n) ≡ List.iterate f x n |
| 1207 |
toList-iterate f x zero = refl |
| 1208 |
toList-iterate f x (suc n) = cong (_ List.∷_) (toList-iterate f (f x) n) |
| 1209 |
|
| 1210 |
------------------------------------------------------------------------ |
| 1211 |
-- tabulate |
| 1212 |
|
| 1213 |
lookup∘tabulate : ∀ (f : Fin n → A) (i : Fin n) → |
| 1214 |
lookup (tabulate f) i ≡ f i |
| 1215 |
lookup∘tabulate f zero = refl |
| 1216 |
lookup∘tabulate f (suc i) = lookup∘tabulate (f ∘ suc) i |
| 1217 |
|
| 1218 |
tabulate∘lookup : ∀ (xs : Vec A n) → tabulate (lookup xs) ≡ xs |
| 1219 |
tabulate∘lookup [] = refl |
| 1220 |
tabulate∘lookup (x ∷ xs) = cong (x ∷_) (tabulate∘lookup xs) |
| 1221 |
|
| 1222 |
tabulate-∘ : ∀ (f : A → B) (g : Fin n → A) → |
| 1223 |
tabulate (f ∘ g) ≡ map f (tabulate g) |
| 1224 |
tabulate-∘ {n = zero} f g = refl |
| 1225 |
tabulate-∘ {n = suc n} f g = cong (f (g zero) ∷_) (tabulate-∘ f (g ∘ suc)) |
| 1226 |
|
| 1227 |
tabulate-cong : ∀ {f g : Fin n → A} → f ≗ g → tabulate f ≡ tabulate g |
| 1228 |
tabulate-cong {n = zero} p = refl |
| 1229 |
tabulate-cong {n = suc n} p = cong₂ _∷_ (p zero) (tabulate-cong (p ∘ suc)) |
| 1230 |
|
| 1231 |
------------------------------------------------------------------------ |
| 1232 |
-- allFin |
| 1233 |
|
| 1234 |
lookup-allFin : ∀ (i : Fin n) → lookup (allFin n) i ≡ i |
| 1235 |
lookup-allFin = lookup∘tabulate id |
| 1236 |
|
| 1237 |
allFin-map : ∀ n → allFin (suc n) ≡ zero ∷ map suc (allFin n) |
| 1238 |
allFin-map n = cong (zero ∷_) $ tabulate-∘ suc id |
| 1239 |
|
| 1240 |
tabulate-allFin : ∀ (f : Fin n → A) → tabulate f ≡ map f (allFin n) |
| 1241 |
tabulate-allFin f = tabulate-∘ f id |
| 1242 |
|
| 1243 |
-- If you look up every possible index, in increasing order, then you |
| 1244 |
-- get back the vector you started with. |
| 1245 |
|
| 1246 |
map-lookup-allFin : ∀ (xs : Vec A n) → map (lookup xs) (allFin n) ≡ xs |
| 1247 |
map-lookup-allFin {n = n} xs = begin |
| 1248 |
map (lookup xs) (allFin n) ≡⟨ tabulate-∘ (lookup xs) id ⟨ |
| 1249 |
tabulate (lookup xs) ≡⟨ tabulate∘lookup xs ⟩ |
| 1250 |
xs ∎ |
| 1251 |
where open ≡-Reasoning |
| 1252 |
|
| 1253 |
------------------------------------------------------------------------ |
| 1254 |
-- count |
| 1255 |
|
| 1256 |
module _ {P : Pred A p} (P? : Decidable P) where |
| 1257 |
|
| 1258 |
count≤n : ∀ (xs : Vec A n) → count P? xs ≤ n |
| 1259 |
count≤n [] = z≤n |
| 1260 |
count≤n (x ∷ xs) with does (P? x) |
| 1261 |
... | true = s≤s (count≤n xs) |
| 1262 |
... | false = m≤n⇒m≤1+n (count≤n xs) |
| 1263 |
|
| 1264 |
------------------------------------------------------------------------ |
| 1265 |
-- length |
| 1266 |
|
| 1267 |
length-toList : (xs : Vec A n) → List.length (toList xs) ≡ length xs |
| 1268 |
length-toList [] = refl |
| 1269 |
length-toList (x ∷ xs) = cong suc (length-toList xs) |
| 1270 |
|
| 1271 |
------------------------------------------------------------------------ |
| 1272 |
-- insertAt |
| 1273 |
|
| 1274 |
insertAt-lookup : ∀ (xs : Vec A n) (i : Fin (suc n)) (v : A) → |
| 1275 |
lookup (insertAt xs i v) i ≡ v |
| 1276 |
insertAt-lookup xs zero v = refl |
| 1277 |
insertAt-lookup (x ∷ xs) (suc i) v = insertAt-lookup xs i v |
| 1278 |
|
| 1279 |
insertAt-punchIn : ∀ (xs : Vec A n) (i : Fin (suc n)) (v : A) (j : Fin n) → |
| 1280 |
lookup (insertAt xs i v) (Fin.punchIn i j) ≡ lookup xs j |
| 1281 |
insertAt-punchIn xs zero v j = refl |
| 1282 |
insertAt-punchIn (x ∷ xs) (suc i) v zero = refl |
| 1283 |
insertAt-punchIn (x ∷ xs) (suc i) v (suc j) = insertAt-punchIn xs i v j |
| 1284 |
|
| 1285 |
toList-insertAt : ∀ (xs : Vec A n) (i : Fin (suc n)) (v : A) → |
| 1286 |
toList (insertAt xs i v) ≡ List.insertAt (toList xs) (Fin.cast (cong suc (sym (length-toList xs))) i) v |
| 1287 |
toList-insertAt xs zero v = refl |
| 1288 |
toList-insertAt (x ∷ xs) (suc i) v = cong (_ List.∷_) (toList-insertAt xs i v) |
| 1289 |
|
| 1290 |
------------------------------------------------------------------------ |
| 1291 |
-- removeAt |
| 1292 |
|
| 1293 |
removeAt-punchOut : ∀ (xs : Vec A (suc n)) {i} {j} (i≢j : i ≢ j) → |
| 1294 |
lookup (removeAt xs i) (Fin.punchOut i≢j) ≡ lookup xs j |
| 1295 |
removeAt-punchOut (x ∷ xs) {zero} {zero} i≢j = contradiction refl i≢j |
| 1296 |
removeAt-punchOut (x ∷ xs) {zero} {suc j} i≢j = refl |
| 1297 |
removeAt-punchOut (x ∷ y ∷ xs) {suc i} {zero} i≢j = refl |
| 1298 |
removeAt-punchOut (x ∷ y ∷ xs) {suc i} {suc j} i≢j = |
| 1299 |
removeAt-punchOut (y ∷ xs) (i≢j ∘ cong suc) |
| 1300 |
|
| 1301 |
------------------------------------------------------------------------ |
| 1302 |
-- insertAt and removeAt |
| 1303 |
|
| 1304 |
removeAt-insertAt : ∀ (xs : Vec A n) (i : Fin (suc n)) (v : A) → |
| 1305 |
removeAt (insertAt xs i v) i ≡ xs |
| 1306 |
removeAt-insertAt xs zero v = refl |
| 1307 |
removeAt-insertAt (x ∷ xs) (suc zero) v = refl |
| 1308 |
removeAt-insertAt (x ∷ xs@(_ ∷ _)) (suc (suc i)) v = |
| 1309 |
cong (x ∷_) (removeAt-insertAt xs (suc i) v) |
| 1310 |
|
| 1311 |
insertAt-removeAt : ∀ (xs : Vec A (suc n)) (i : Fin (suc n)) → |
| 1312 |
insertAt (removeAt xs i) i (lookup xs i) ≡ xs |
| 1313 |
insertAt-removeAt (x ∷ xs) zero = refl |
| 1314 |
insertAt-removeAt (x ∷ xs@(_ ∷ _)) (suc i) = |
| 1315 |
cong (x ∷_) (insertAt-removeAt xs i) |
| 1316 |
|
| 1317 |
------------------------------------------------------------------------ |
| 1318 |
-- Conversion function |
| 1319 |
|
| 1320 |
toList∘fromList : (xs : List A) → toList (fromList xs) ≡ xs |
| 1321 |
toList∘fromList List.[] = refl |
| 1322 |
toList∘fromList (x List.∷ xs) = cong (x List.∷_) (toList∘fromList xs) |
| 1323 |
|
| 1324 |
fromList∘toList : ∀ (xs : Vec A n) → fromList (toList xs) ≈[ length-toList xs ] xs |
| 1325 |
fromList∘toList [] = refl |
| 1326 |
fromList∘toList (x ∷ xs) = cong (x ∷_) (fromList∘toList xs) |
| 1327 |
|
| 1328 |
toList-cast : ∀ .(eq : m ≡ n) (xs : Vec A m) → toList (cast eq xs) ≡ toList xs |
| 1329 |
toList-cast {n = zero} eq [] = refl |
| 1330 |
toList-cast {n = suc _} eq (x ∷ xs) = |
| 1331 |
cong (x List.∷_) (toList-cast (cong pred eq) xs) |
| 1332 |
|
| 1333 |
cast-fromList : ∀ {xs ys : List A} (eq : xs ≡ ys) → |
| 1334 |
cast (cong List.length eq) (fromList xs) ≡ fromList ys |
| 1335 |
cast-fromList {xs = List.[]} {ys = List.[]} eq = refl |
| 1336 |
cast-fromList {xs = x List.∷ xs} {ys = y List.∷ ys} eq = |
| 1337 |
let x≡y , xs≡ys = List.∷-injective eq in begin |
| 1338 |
x ∷ cast (cong (pred ∘ List.length) eq) (fromList xs) ≡⟨ cong (_ ∷_) (cast-fromList xs≡ys) ⟩ |
| 1339 |
x ∷ fromList ys ≡⟨ cong (_∷ _) x≡y ⟩ |
| 1340 |
y ∷ fromList ys ∎ |
| 1341 |
where open ≡-Reasoning |
| 1342 |
|
| 1343 |
fromList-map : ∀ (f : A → B) (xs : List A) → |
| 1344 |
cast (List.length-map f xs) (fromList (List.map f xs)) ≡ map f (fromList xs) |
| 1345 |
fromList-map f List.[] = refl |
| 1346 |
fromList-map f (x List.∷ xs) = cong (f x ∷_) (fromList-map f xs) |
| 1347 |
|
| 1348 |
fromList-++ : ∀ (xs : List A) {ys : List A} → |
| 1349 |
cast (List.length-++ xs) (fromList (xs List.++ ys)) ≡ fromList xs ++ fromList ys |
| 1350 |
fromList-++ List.[] {ys} = cast-is-id refl (fromList ys) |
| 1351 |
fromList-++ (x List.∷ xs) {ys} = cong (x ∷_) (fromList-++ xs) |
| 1352 |
|
| 1353 |
fromList-reverse : (xs : List A) → |
| 1354 |
(fromList (List.reverse xs)) ≈[ List.length-reverse xs ] reverse (fromList xs) |
| 1355 |
fromList-reverse xs = |
| 1356 |
toList-injective (List.length-reverse xs) (fromList (List.reverse xs)) (reverse (fromList xs)) $ |
| 1357 |
begin |
| 1358 |
toList (fromList (List.reverse xs)) ≡⟨ toList∘fromList (List.reverse xs) ⟩ |
| 1359 |
List.reverse xs ≡⟨ cong (λ x → List.reverse x) (toList∘fromList xs) ⟨ |
| 1360 |
List.reverse (toList (fromList xs)) ≡⟨ toList-reverse (fromList xs) ⟨ |
| 1361 |
toList (reverse (fromList xs)) ∎ |
| 1362 |
where open ≡-Reasoning |
| 1363 |
|
| 1364 |
|
| 1365 |
------------------------------------------------------------------------ |
| 1366 |
-- DEPRECATED NAMES |
| 1367 |
------------------------------------------------------------------------ |
| 1368 |
-- Please use the new names as continuing support for the old names is |
| 1369 |
-- not guaranteed. |
| 1370 |
|
| 1371 |
-- Version 2.3 |
| 1372 |
|
| 1373 |
∷-ʳ++-eqFree : ∀ a (xs : Vec A m) {ys : Vec A n} → let eq = sym (+-suc m n) in |
| 1374 |
cast eq ((a ∷ xs) ʳ++ ys) ≡ xs ʳ++ (a ∷ ys) |
| 1375 |
∷-ʳ++-eqFree a xs {ys} = ʳ++-ʳ++-eqFree (a ∷ []) {ys = xs} {zs = ys} |
| 1376 |
{-# WARNING_ON_USAGE ∷-ʳ++-eqFree |
| 1377 |
"Warning: ∷-ʳ++-eqFree was deprecated in v2.3. |
| 1378 |
Please use ʳ++-ʳ++-eqFree instead, which does not take eq." |
| 1379 |
#-} |
| 1380 |
|
| 1381 |
-- Version 2.2 |
| 1382 |
|
| 1383 |
-- TRANSITION TO EQ-FREE LEMMA |
| 1384 |
-- |
| 1385 |
-- Please use the new proofs, which do not require an `eq` parameter. |
| 1386 |
-- In v3, `name` will be changed to be the same lemma as `name-eqFree`, |
| 1387 |
-- and `name-eqFree` will be deprecated. |
| 1388 |
|
| 1389 |
++-identityʳ : ∀ .(eq : n + zero ≡ n) (xs : Vec A n) → cast eq (xs ++ []) ≡ xs |
| 1390 |
++-identityʳ _ = ++-identityʳ-eqFree |
| 1391 |
{-# WARNING_ON_USAGE ++-identityʳ |
| 1392 |
"Warning: ++-identityʳ was deprecated in v2.2. |
| 1393 |
Please use ++-identityʳ-eqFree instead, which does not take eq." |
| 1394 |
#-} |
| 1395 |
|
| 1396 |
unfold-∷ʳ : ∀ .(eq : suc n ≡ n + 1) x (xs : Vec A n) → cast eq (xs ∷ʳ x) ≡ xs ++ [ x ] |
| 1397 |
unfold-∷ʳ _ = unfold-∷ʳ-eqFree |
| 1398 |
{-# WARNING_ON_USAGE unfold-∷ʳ |
| 1399 |
"Warning: unfold-∷ʳ was deprecated in v2.2. |
| 1400 |
Please use unfold-∷ʳ-eqFree instead, which does not take eq." |
| 1401 |
#-} |
| 1402 |
|
| 1403 |
++-∷ʳ : ∀ .(eq : suc (m + n) ≡ m + suc n) z (xs : Vec A m) (ys : Vec A n) → |
| 1404 |
cast eq ((xs ++ ys) ∷ʳ z) ≡ xs ++ (ys ∷ʳ z) |
| 1405 |
++-∷ʳ _ = ++-∷ʳ-eqFree |
| 1406 |
{-# WARNING_ON_USAGE ++-∷ʳ |
| 1407 |
"Warning: ++-∷ʳ was deprecated in v2.2. |
| 1408 |
Please use ++-∷ʳ-eqFree instead, which does not take eq." |
| 1409 |
#-} |
| 1410 |
|
| 1411 |
∷ʳ-++ : ∀ .(eq : (suc n) + m ≡ n + suc m) a (xs : Vec A n) {ys} → |
| 1412 |
cast eq ((xs ∷ʳ a) ++ ys) ≡ xs ++ (a ∷ ys) |
| 1413 |
∷ʳ-++ _ = ∷ʳ-++-eqFree |
| 1414 |
{-# WARNING_ON_USAGE ∷ʳ-++ |
| 1415 |
"Warning: ∷ʳ-++ was deprecated in v2.2. |
| 1416 |
Please use ∷ʳ-++-eqFree instead, which does not take eq." |
| 1417 |
#-} |
| 1418 |
|
| 1419 |
reverse-++ : ∀ .(eq : m + n ≡ n + m) (xs : Vec A m) (ys : Vec A n) → |
| 1420 |
cast eq (reverse (xs ++ ys)) ≡ reverse ys ++ reverse xs |
| 1421 |
reverse-++ _ = reverse-++-eqFree |
| 1422 |
{-# WARNING_ON_USAGE reverse-++ |
| 1423 |
"Warning: reverse-++ was deprecated in v2.2. |
| 1424 |
Please use reverse-++-eqFree instead, which does not take eq." |
| 1425 |
#-} |
| 1426 |
|
| 1427 |
∷-ʳ++ : ∀ .(eq : (suc m) + n ≡ m + suc n) a (xs : Vec A m) {ys} → |
| 1428 |
cast eq ((a ∷ xs) ʳ++ ys) ≡ xs ʳ++ (a ∷ ys) |
| 1429 |
∷-ʳ++ _ a xs {ys} = ʳ++-ʳ++-eqFree (a ∷ []) {ys = xs} {zs = ys} |
| 1430 |
|
| 1431 |
{-# WARNING_ON_USAGE ∷-ʳ++ |
| 1432 |
"Warning: ∷-ʳ++ was deprecated in v2.2. |
| 1433 |
Please use ∷-ʳ++-eqFree instead, which does not take eq." |
| 1434 |
#-} |
| 1435 |
|
| 1436 |
++-ʳ++ : ∀ .(eq : m + n + o ≡ n + (m + o)) (xs : Vec A m) {ys : Vec A n} {zs : Vec A o} → |
| 1437 |
cast eq ((xs ++ ys) ʳ++ zs) ≡ ys ʳ++ (xs ʳ++ zs) |
| 1438 |
++-ʳ++ _ = ++-ʳ++-eqFree |
| 1439 |
{-# WARNING_ON_USAGE ++-ʳ++ |
| 1440 |
"Warning: ++-ʳ++ was deprecated in v2.2. |
| 1441 |
Please use ++-ʳ++-eqFree instead, which does not take eq." |
| 1442 |
#-} |
| 1443 |
|
| 1444 |
ʳ++-ʳ++ : ∀ .(eq : (m + n) + o ≡ n + (m + o)) (xs : Vec A m) {ys : Vec A n} {zs} → |
| 1445 |
cast eq ((xs ʳ++ ys) ʳ++ zs) ≡ ys ʳ++ (xs ++ zs) |
| 1446 |
ʳ++-ʳ++ _ = ʳ++-ʳ++-eqFree |
| 1447 |
{-# WARNING_ON_USAGE ʳ++-ʳ++ |
| 1448 |
"Warning: ʳ++-ʳ++ was deprecated in v2.2. |
| 1449 |
Please use ʳ++-ʳ++-eqFree instead, which does not take eq." |
| 1450 |
#-} |
| 1451 |
|
| 1452 |
++-assoc : ∀ .(eq : (m + n) + o ≡ m + (n + o)) (xs : Vec A m) (ys : Vec A n) (zs : Vec A o) → |
| 1453 |
cast eq ((xs ++ ys) ++ zs) ≡ xs ++ (ys ++ zs) |
| 1454 |
++-assoc _ = ++-assoc-eqFree |
| 1455 |
{-# WARNING_ON_USAGE ++-assoc |
| 1456 |
"Warning: ++-assoc was deprecated in v2.2. |
| 1457 |
Please use ++-assoc-eqFree instead, which does not take eq." |
| 1458 |
#-} |
| 1459 |
|
| 1460 |
-- Version 2.0 |
| 1461 |
|
| 1462 |
updateAt-id-relative = updateAt-id-local |
| 1463 |
{-# WARNING_ON_USAGE updateAt-id-relative |
| 1464 |
"Warning: updateAt-id-relative was deprecated in v2.0. |
| 1465 |
Please use updateAt-id-local instead." |
| 1466 |
#-} |
| 1467 |
|
| 1468 |
updateAt-compose-relative = updateAt-updateAt-local |
| 1469 |
{-# WARNING_ON_USAGE updateAt-compose-relative |
| 1470 |
"Warning: updateAt-compose-relative was deprecated in v2.0. |
| 1471 |
Please use updateAt-updateAt-local instead." |
| 1472 |
#-} |
| 1473 |
|
| 1474 |
updateAt-compose = updateAt-updateAt |
| 1475 |
{-# WARNING_ON_USAGE updateAt-compose |
| 1476 |
"Warning: updateAt-compose was deprecated in v2.0. |
| 1477 |
Please use updateAt-updateAt instead." |
| 1478 |
#-} |
| 1479 |
|
| 1480 |
updateAt-cong-relative = updateAt-cong-local |
| 1481 |
{-# WARNING_ON_USAGE updateAt-cong-relative |
| 1482 |
"Warning: updateAt-cong-relative was deprecated in v2.0. |
| 1483 |
Please use updateAt-cong-local instead." |
| 1484 |
#-} |
| 1485 |
|
| 1486 |
[]%=-compose = []%=-∘ |
| 1487 |
{-# WARNING_ON_USAGE []%=-compose |
| 1488 |
"Warning: []%=-compose was deprecated in v2.0. |
| 1489 |
Please use []%=-∘ instead." |
| 1490 |
#-} |
| 1491 |
|
| 1492 |
[]≔-++-inject+ : ∀ {m n x} (xs : Vec A m) (ys : Vec A n) i → |
| 1493 |
(xs ++ ys) [ i ↑ˡ n ]≔ x ≡ (xs [ i ]≔ x) ++ ys |
| 1494 |
[]≔-++-inject+ = []≔-++-↑ˡ |
| 1495 |
{-# WARNING_ON_USAGE []≔-++-inject+ |
| 1496 |
"Warning: []≔-++-inject+ was deprecated in v2.0. |
| 1497 |
Please use []≔-++-↑ˡ instead." |
| 1498 |
#-} |
| 1499 |
idIsFold = id-is-foldr |
| 1500 |
{-# WARNING_ON_USAGE idIsFold |
| 1501 |
"Warning: idIsFold was deprecated in v2.0. |
| 1502 |
Please use id-is-foldr instead." |
| 1503 |
#-} |
| 1504 |
sum-++-commute = sum-++ |
| 1505 |
{-# WARNING_ON_USAGE sum-++-commute |
| 1506 |
"Warning: sum-++-commute was deprecated in v2.0. |
| 1507 |
Please use sum-++ instead." |
| 1508 |
#-} |
| 1509 |
take-drop-id = take++drop≡id |
| 1510 |
{-# WARNING_ON_USAGE take-drop-id |
| 1511 |
"Warning: take-drop-id was deprecated in v2.0. |
| 1512 |
Please use take++drop≡id instead." |
| 1513 |
#-} |
| 1514 |
take-distr-zipWith = take-zipWith |
| 1515 |
{-# WARNING_ON_USAGE take-distr-zipWith |
| 1516 |
"Warning: take-distr-zipWith was deprecated in v2.0. |
| 1517 |
Please use take-zipWith instead." |
| 1518 |
#-} |
| 1519 |
take-distr-map = take-map |
| 1520 |
{-# WARNING_ON_USAGE take-distr-map |
| 1521 |
"Warning: take-distr-map was deprecated in v2.0. |
| 1522 |
Please use take-map instead." |
| 1523 |
#-} |
| 1524 |
drop-distr-zipWith = drop-zipWith |
| 1525 |
{-# WARNING_ON_USAGE drop-distr-zipWith |
| 1526 |
"Warning: drop-distr-zipWith was deprecated in v2.0. |
| 1527 |
Please use drop-zipWith instead." |
| 1528 |
#-} |
| 1529 |
drop-distr-map = drop-map |
| 1530 |
{-# WARNING_ON_USAGE drop-distr-map |
| 1531 |
"Warning: drop-distr-map was deprecated in v2.0. |
| 1532 |
Please use drop-map instead." |
| 1533 |
#-} |
| 1534 |
|
| 1535 |
map-insert = map-insertAt |
| 1536 |
{-# WARNING_ON_USAGE map-insert |
| 1537 |
"Warning: map-insert was deprecated in v2.0. |
| 1538 |
Please use map-insertAt instead." |
| 1539 |
#-} |
| 1540 |
insert-lookup = insertAt-lookup |
| 1541 |
{-# WARNING_ON_USAGE insert-lookup |
| 1542 |
"Warning: insert-lookup was deprecated in v2.0. |
| 1543 |
Please use insertAt-lookup instead." |
| 1544 |
#-} |
| 1545 |
insert-punchIn = insertAt-punchIn |
| 1546 |
{-# WARNING_ON_USAGE insert-punchIn |
| 1547 |
"Warning: insert-punchIn was deprecated in v2.0. |
| 1548 |
Please use insertAt-punchIn instead." |
| 1549 |
#-} |
| 1550 |
remove-PunchOut = removeAt-punchOut |
| 1551 |
{-# WARNING_ON_USAGE remove-PunchOut |
| 1552 |
"Warning: remove-PunchOut was deprecated in v2.0. |
| 1553 |
Please use removeAt-punchOut instead." |
| 1554 |
#-} |
| 1555 |
remove-insert = removeAt-insertAt |
| 1556 |
{-# WARNING_ON_USAGE remove-insert |
| 1557 |
"Warning: remove-insert was deprecated in v2.0. |
| 1558 |
Please use removeAt-insertAt instead." |
| 1559 |
#-} |
| 1560 |
insert-remove = insertAt-removeAt |
| 1561 |
{-# WARNING_ON_USAGE insert-remove |
| 1562 |
"Warning: insert-remove was deprecated in v2.0. |
| 1563 |
Please use insertAt-removeAt instead." |
| 1564 |
#-} |
| 1565 |
|
| 1566 |
lookup-inject≤-take : ∀ m (m≤m+n : m ≤ m + n) (i : Fin m) (xs : Vec A (m + n)) → |
| 1567 |
lookup xs (Fin.inject≤ i m≤m+n) ≡ lookup (take m xs) i |
| 1568 |
lookup-inject≤-take m m≤m+n i xs = sym (begin |
| 1569 |
lookup (take m xs) i |
| 1570 |
≡⟨ lookup-take-inject≤ xs i ⟩ |
| 1571 |
lookup xs (Fin.inject≤ i _) |
| 1572 |
≡⟨⟩ |
| 1573 |
lookup xs (Fin.inject≤ i m≤m+n) |
| 1574 |
∎) where open ≡-Reasoning |
| 1575 |
{-# WARNING_ON_USAGE lookup-inject≤-take |
| 1576 |
"Warning: lookup-inject≤-take was deprecated in v2.0. |
| 1577 |
Please use lookup-take-inject≤ or lookup-truncate, take≡truncate instead." |
| 1578 |
#-} |