------------------------------------------------------------------------ -- The Agda standard library -- -- Some Vector-related properties ------------------------------------------------------------------------ {-# OPTIONS --cubical-compatible --safe #-} module Data.Vec.Functional.Properties where open import Data.Fin.Base using (Fin; zero; suc; toℕ; fromℕ<; reduce≥; _↑ˡ_; _↑ʳ_; punchIn; punchOut) open import Data.Nat.Base as ℕ using (ℕ; zero; suc) import Data.Nat.Properties as ℕ open import Data.Product.Base as Product using (_×_; _,_; proj₁; proj₂) open import Data.List.Base as List using (List) import Data.List.Properties as List open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂) open import Data.Vec.Base as Vec using (Vec) import Data.Vec.Properties as Vec open import Data.Vec.Functional using (Vector; head; tail; updateAt; map; _++_; insertAt; removeAt; toVec; fromVec; toList; fromList) open import Function.Base using (_∘_; id) open import Level using (Level) open import Relation.Binary.Definitions using (DecidableEquality; Decidable) open import Relation.Binary.PropositionalEquality.Core using (_≡_; _≗_; refl; _≢_; cong) open import Relation.Binary.PropositionalEquality.Properties using (module ≡-Reasoning) open import Relation.Nullary.Decidable using (Dec; does; yes; no; map′; _×-dec_) open import Relation.Nullary.Negation using (contradiction) import Data.Fin.Properties as Finₚ private variable a b c : Level A B C : Set a m n : ℕ ------------------------------------------------------------------------ module _ {xs ys : Vector A (suc n)} where ∷-cong : head xs ≡ head ys → tail xs ≗ tail ys → xs ≗ ys ∷-cong eq _ zero = eq ∷-cong _ eq (suc i) = eq i ∷-injective : xs ≗ ys → head xs ≡ head ys × tail xs ≗ tail ys ∷-injective eq = eq zero , eq ∘ suc ≗-dec : DecidableEquality A → Decidable {A = Vector A n} _≗_ ≗-dec {n = zero} _≟_ xs ys = yes λ () ≗-dec {n = suc n} _≟_ xs ys = map′ (Product.uncurry ∷-cong) ∷-injective (head xs ≟ head ys ×-dec ≗-dec _≟_ (tail xs) (tail ys)) ------------------------------------------------------------------------ -- updateAt -- (+) updateAt i actually updates the element at index i. updateAt-updates : ∀ (i : Fin n) {f : A → A} (xs : Vector A n) → updateAt xs i f i ≡ f (xs i) updateAt-updates zero xs = refl updateAt-updates (suc i) xs = updateAt-updates i (tail xs) -- (-) updateAt i does not touch the elements at other indices. updateAt-minimal : ∀ (i j : Fin n) {f : A → A} (xs : Vector A n) → i ≢ j → updateAt xs j f i ≡ xs i updateAt-minimal zero zero xs 0≢0 = contradiction refl 0≢0 updateAt-minimal zero (suc j) xs _ = refl updateAt-minimal (suc i) zero xs _ = refl updateAt-minimal (suc i) (suc j) xs i≢j = updateAt-minimal i j (tail xs) (i≢j ∘ cong suc) -- updateAt i is a monoid morphism from A → A to Vector A n → Vector A n. updateAt-id-local : ∀ (i : Fin n) {f : A → A} (xs : Vector A n) → f (xs i) ≡ xs i → updateAt xs i f ≗ xs updateAt-id-local zero xs eq zero = eq updateAt-id-local zero xs eq (suc j) = refl updateAt-id-local (suc i) xs eq zero = refl updateAt-id-local (suc i) xs eq (suc j) = updateAt-id-local i (tail xs) eq j updateAt-id : ∀ (i : Fin n) (xs : Vector A n) → updateAt xs i id ≗ xs updateAt-id i xs = updateAt-id-local i xs refl updateAt-updateAt-local : ∀ (i : Fin n) {f g h : A → A} (xs : Vector A n) → f (g (xs i)) ≡ h (xs i) → updateAt (updateAt xs i g) i f ≗ updateAt xs i h updateAt-updateAt-local zero xs eq zero = eq updateAt-updateAt-local zero xs eq (suc j) = refl updateAt-updateAt-local (suc i) xs eq zero = refl updateAt-updateAt-local (suc i) xs eq (suc j) = updateAt-updateAt-local i (tail xs) eq j updateAt-updateAt : ∀ (i : Fin n) {f g : A → A} (xs : Vector A n) → updateAt (updateAt xs i g) i f ≗ updateAt xs i (f ∘ g) updateAt-updateAt i xs = updateAt-updateAt-local i xs refl updateAt-cong-local : ∀ (i : Fin n) {f g : A → A} (xs : Vector A n) → f (xs i) ≡ g (xs i) → updateAt xs i f ≗ updateAt xs i g updateAt-cong-local zero xs eq zero = eq updateAt-cong-local zero xs eq (suc j) = refl updateAt-cong-local (suc i) xs eq zero = refl updateAt-cong-local (suc i) xs eq (suc j) = updateAt-cong-local i (tail xs) eq j updateAt-cong : ∀ (i : Fin n) {f g : A → A} → f ≗ g → (xs : Vector A n) → updateAt xs i f ≗ updateAt xs i g updateAt-cong i eq xs = updateAt-cong-local i xs (eq (xs i)) -- The order of updates at different indices i ≢ j does not matter. updateAt-commutes : ∀ (i j : Fin n) {f g : A → A} → i ≢ j → (xs : Vector A n) → updateAt (updateAt xs j g) i f ≗ updateAt (updateAt xs i f) j g updateAt-commutes zero zero 0≢0 xs k = contradiction refl 0≢0 updateAt-commutes zero (suc j) _ xs zero = refl updateAt-commutes zero (suc j) _ xs (suc k) = refl updateAt-commutes (suc i) zero _ xs zero = refl updateAt-commutes (suc i) zero _ xs (suc k) = refl updateAt-commutes (suc i) (suc j) _ xs zero = refl updateAt-commutes (suc i) (suc j) i≢j xs (suc k) = updateAt-commutes i j (i≢j ∘ cong suc) (tail xs) k ------------------------------------------------------------------------ -- map map-id : (xs : Vector A n) → map id xs ≗ xs map-id xs = λ _ → refl map-cong : ∀ {f g : A → B} → f ≗ g → (xs : Vector A n) → map f xs ≗ map g xs map-cong f≗g xs = f≗g ∘ xs map-∘ : ∀ {f : B → C} {g : A → B} (xs : Vector A n) → map (f ∘ g) xs ≗ map f (map g xs) map-∘ xs = λ _ → refl lookup-map : ∀ (i : Fin n) (f : A → B) (xs : Vector A n) → map f xs i ≡ f (xs i) lookup-map i f xs = refl map-updateAt-local : ∀ {f : A → B} {g : A → A} {h : B → B} (xs : Vector A n) (i : Fin n) → f (g (xs i)) ≡ h (f (xs i)) → map f (updateAt xs i g) ≗ updateAt (map f xs) i h map-updateAt-local {n = suc n} {f = f} xs zero eq zero = eq map-updateAt-local {n = suc n} {f = f} xs zero eq (suc j) = refl map-updateAt-local {n = suc (suc n)} {f = f} xs (suc i) eq zero = refl 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 map-updateAt : ∀ {f : A → B} {g : A → A} {h : B → B} → f ∘ g ≗ h ∘ f → (xs : Vector A n) (i : Fin n) → map f (updateAt xs i g) ≗ updateAt (map f xs) i h 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)) ------------------------------------------------------------------------ -- _++_ lookup-++-< : ∀ (xs : Vector A m) (ys : Vector A n) → ∀ i (i