Skip to content
Merged
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,8 @@ Non-backwards compatible changes

* In `Data.Sum.Base` the definitions `fromDec` and `toDec` have been moved to `Data.Sum`.

* In `Data.Vec.Base`: the definitions `init` and `last` have been replaced by an inductive definition.

* In `Codata.Guarded.Stream` the following functions have been modified to have simpler definitions:
* `cycle`
* `interleave⁺`
Expand Down Expand Up @@ -2610,6 +2612,12 @@ Other minor changes
∷ʳ-injectiveˡ : xs ∷ʳ x ≡ ys ∷ʳ y → xs ≡ ys
∷ʳ-injectiveʳ : xs ∷ʳ x ≡ ys ∷ʳ y → x ≡ y

unfold-∷ʳ : cast eq (xs ++ x ∷ []) ≡ xs ∷ʳ x
init-∷ʳ : init (xs ∷ʳ x) ≡ xs
last-∷ʳ : last (xs ∷ʳ x) ≡ x
cast-∷ʳ : cast eq (xs ∷ʳ x) ≡ (cast (cong pred eq) xs) ∷ʳ x
++-∷ʳ : (cast eq ((xs ++ ys) ∷ʳ z)) ≡ xs ++ (ys ∷ʳ z)

reverse-∷ : reverse (x ∷ xs) ≡ reverse xs ∷ʳ x
reverse-involutive : Involutive _≡_ reverse
reverse-reverse : reverse xs ≡ ys → reverse ys ≡ xs
Expand Down
8 changes: 4 additions & 4 deletions src/Data/Vec/Base.agda
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,12 @@ initLast {n = suc n} (x ∷ xs) with initLast xs
... | (ys , y , refl) = (x ∷ ys , y , refl)

init : Vec A (1 + n) → Vec A n
init xs with initLast xs
... | (ys , y , refl) = ys
init {n = zero} (x ∷ xs) = []
init {n = suc n} (x ∷ xs) = x ∷ init xs

last : Vec A (1 + n) → A
last xs with initLast xs
... | (ys , y , refl) = y
last {n = zero} (x ∷ xs) = x
last {n = suc n} (x ∷ xs) = last xs

------------------------------------------------------------------------
-- Other operations
Expand Down
33 changes: 32 additions & 1 deletion src/Data/Vec/Properties.agda
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ cast-is-id eq (x ∷ xs) = cong (x ∷_) (cast-is-id (suc-injective eq) xs)
subst-is-cast : (eq : m ≡ n) (xs : Vec A m) → subst (Vec A) eq xs ≡ cast eq xs
subst-is-cast refl xs = sym (cast-is-id refl xs)

cast-trans : .(eq₁ : m ≡ n) (eq₂ : n ≡ o) (xs : Vec A m) →
cast-trans : .(eq₁ : m ≡ n) .(eq₂ : n ≡ o) (xs : Vec A m) →
cast eq₂ (cast eq₁ xs) ≡ cast (trans eq₁ eq₂) xs
cast-trans {m = zero} {n = zero} {o = zero} eq₁ eq₂ [] = refl
cast-trans {m = suc _} {n = suc _} {o = suc _} eq₁ eq₂ (x ∷ xs) =
Expand Down Expand Up @@ -839,6 +839,12 @@ map-is-foldr f = foldr-universal (Vec _) (λ x ys → f x ∷ ys) (map f) refl (
------------------------------------------------------------------------
-- _∷ʳ_

-- snoc is snoc

unfold-∷ʳ : ∀ .(eq : n + 1 ≡ suc n) x (xs : Vec A n) → cast eq (xs ++ x ∷ []) ≡ xs ∷ʳ x
unfold-∷ʳ eq x [] = refl
unfold-∷ʳ eq x (y ∷ xs) = cong (y ∷_) (unfold-∷ʳ (cong pred eq) x xs)

∷ʳ-injective : ∀ (xs ys : Vec A n) → xs ∷ʳ x ≡ ys ∷ʳ y → xs ≡ ys × x ≡ y
∷ʳ-injective [] [] refl = (refl , refl)
∷ʳ-injective (x ∷ xs) (y ∷ ys) eq with ∷-injective eq
Expand All @@ -860,12 +866,37 @@ foldr-∷ʳ : ∀ (B : ℕ → Set b) (f : FoldrOp A B) {e} y (ys : Vec A n) →
foldr-∷ʳ B f y [] = refl
foldr-∷ʳ B f y (x ∷ xs) = cong (f x) (foldr-∷ʳ B f y xs)

-- init, last and _∷ʳ_

init-∷ʳ : ∀ x (xs : Vec A n) → init (xs ∷ʳ x) ≡ xs
init-∷ʳ x [] = refl
init-∷ʳ x (y ∷ xs) = cong (y ∷_) (init-∷ʳ x xs)

last-∷ʳ : ∀ x (xs : Vec A n) → last (xs ∷ʳ x) ≡ x
last-∷ʳ x [] = refl
last-∷ʳ x (y ∷ xs) = last-∷ʳ x xs

-- map and _∷ʳ_

map-∷ʳ : ∀ (f : A → B) x (xs : Vec A n) → map f (xs ∷ʳ x) ≡ map f xs ∷ʳ f x
map-∷ʳ f x [] = refl
map-∷ʳ f x (y ∷ xs) = cong (f y ∷_) (map-∷ʳ f x xs)

-- cast and _∷ʳ_

cast-∷ʳ : ∀ .(eq : suc n ≡ suc m) x (xs : Vec A n) →
cast eq (xs ∷ʳ x) ≡ (cast (cong pred eq) xs) ∷ʳ x
cast-∷ʳ {m = zero} eq x [] = refl
cast-∷ʳ {m = suc m} eq x (y ∷ xs) = cong (y ∷_) (cast-∷ʳ (cong pred eq) x xs)

-- _++_ and _∷ʳ_

++-∷ʳ : ∀ .(eq : suc (n + m) ≡ n + suc m) z (xs : Vec A n) (ys : Vec A m) →
(cast eq ((xs ++ ys) ∷ʳ z)) ≡ xs ++ (ys ∷ʳ z)
++-∷ʳ {n = zero} eq z [] [] = refl
++-∷ʳ {n = zero} eq z [] (y ∷ ys) = cong (y ∷_) (++-∷ʳ refl z [] ys)
++-∷ʳ {n = suc n} eq z (x ∷ xs) ys = cong (x ∷_) (++-∷ʳ (cong pred eq) z xs ys)

------------------------------------------------------------------------
-- reverse

Expand Down