Skip to content

Commit 01160b8

Browse files
authored
Add some _∷ʳ_ properties to Data.Vec.Properties (#2041)
1 parent 0f32775 commit 01160b8

File tree

4 files changed

+58
-10
lines changed

4 files changed

+58
-10
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,9 @@ Non-backwards compatible changes
864864

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

867+
* In `Data.Vec.Base`: the definitions `init` and `last` have been changed from the `initLast`
868+
view-derived implementation to direct recursive definitions.
869+
867870
* In `Codata.Guarded.Stream` the following functions have been modified to have simpler definitions:
868871
* `cycle`
869872
* `interleave⁺`
@@ -1253,6 +1256,8 @@ Deprecated names
12531256
zipWith-identityˡ ↦ zipWith-zeroˡ
12541257
zipWith-identityʳ ↦ zipWith-zeroʳ
12551258
1259+
ʳ++-++ ↦ ++-ʳ++
1260+
12561261
take++drop ↦ take++drop≡id
12571262
```
12581263

@@ -2658,6 +2663,12 @@ Other minor changes
26582663
∷ʳ-injectiveˡ : xs ∷ʳ x ≡ ys ∷ʳ y → xs ≡ ys
26592664
∷ʳ-injectiveʳ : xs ∷ʳ x ≡ ys ∷ʳ y → x ≡ y
26602665
2666+
unfold-∷ʳ : cast eq (xs ∷ʳ x) ≡ xs ++ [ x ]
2667+
init-∷ʳ : init (xs ∷ʳ x) ≡ xs
2668+
last-∷ʳ : last (xs ∷ʳ x) ≡ x
2669+
cast-∷ʳ : cast eq (xs ∷ʳ x) ≡ (cast (cong pred eq) xs) ∷ʳ x
2670+
++-∷ʳ : cast eq ((xs ++ ys) ∷ʳ z) ≡ xs ++ (ys ∷ʳ z)
2671+
26612672
reverse-∷ : reverse (x ∷ xs) ≡ reverse xs ∷ʳ x
26622673
reverse-involutive : Involutive _≡_ reverse
26632674
reverse-reverse : reverse xs ≡ ys → reverse ys ≡ xs

src/Data/List/Properties.agda

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -993,11 +993,11 @@ module _ {P : Pred A p} (P? : Decidable P) where
993993

994994
-- Reverse-append of append is reverse-append after reverse-append.
995995

996-
ʳ++-++ : (xs {ys zs} : List A) (xs ++ ys) ʳ++ zs ≡ ys ʳ++ xs ʳ++ zs
997-
ʳ++-++ [] = refl
998-
ʳ++-++ (x ∷ xs) {ys} {zs} = begin
996+
++-ʳ++ : (xs {ys zs} : List A) (xs ++ ys) ʳ++ zs ≡ ys ʳ++ xs ʳ++ zs
997+
++-ʳ++ [] = refl
998+
++-ʳ++ (x ∷ xs) {ys} {zs} = begin
999999
(x ∷ xs ++ ys) ʳ++ zs ≡⟨⟩
1000-
(xs ++ ys) ʳ++ x ∷ zs ≡⟨ ʳ++-++ xs ⟩
1000+
(xs ++ ys) ʳ++ x ∷ zs ≡⟨ ++-ʳ++ xs ⟩
10011001
ys ʳ++ xs ʳ++ x ∷ zs ≡⟨⟩
10021002
ys ʳ++ (x ∷ xs) ʳ++ zs ∎
10031003

@@ -1073,7 +1073,7 @@ reverse-++ : (xs ys : List A) →
10731073
reverse (xs ++ ys) ≡ reverse ys ++ reverse xs
10741074
reverse-++ xs ys = begin
10751075
reverse (xs ++ ys) ≡⟨⟩
1076-
(xs ++ ys) ʳ++ [] ≡⟨ ʳ++-++ xs ⟩
1076+
(xs ++ ys) ʳ++ [] ≡⟨ ++-ʳ++ xs ⟩
10771077
ys ʳ++ xs ʳ++ [] ≡⟨⟩
10781078
ys ʳ++ reverse xs ≡⟨ ʳ++-defn ys ⟩
10791079
reverse ys ++ reverse xs ∎
@@ -1213,6 +1213,12 @@ zipWith-identityʳ = zipWith-zeroʳ
12131213
Please use zipWith-zeroʳ instead."
12141214
#-}
12151215

1216+
ʳ++-++ = ++-ʳ++
1217+
{-# WARNING_ON_USAGE ʳ++-++
1218+
"Warning: ʳ++-++ was deprecated in v2.0.
1219+
Please use ++-ʳ++ instead."
1220+
#-}
1221+
12161222
take++drop = take++drop≡id
12171223
{-# WARNING_ON_USAGE take++drop
12181224
"Warning: take++drop was deprecated in v2.0.

src/Data/Vec/Base.agda

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,12 +345,12 @@ initLast {n = suc n} (x ∷ xs) with initLast xs
345345
... | (ys , y , refl) = (x ∷ ys , y , refl)
346346

347347
init : Vec A (1 + n) Vec A n
348-
init xs with initLast xs
349-
... | (ys , y , refl) = ys
348+
init {n = zero} (x ∷ _) = []
349+
init {n = suc n} (x ∷ xs) = x ∷ init xs
350350

351351
last : Vec A (1 + n) A
352-
last xs with initLast xs
353-
... | (ys , y , refl) = y
352+
last {n = zero} (x ∷ _) = x
353+
last {n = suc n} (_ ∷ xs) = last xs
354354

355355
------------------------------------------------------------------------
356356
-- Other operations

src/Data/Vec/Properties.agda

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ cast-is-id eq (x ∷ xs) = cong (x ∷_) (cast-is-id (suc-injective eq) xs)
399399
subst-is-cast : (eq : m ≡ n) (xs : Vec A m) subst (Vec A) eq xs ≡ cast eq xs
400400
subst-is-cast refl xs = sym (cast-is-id refl xs)
401401

402-
cast-trans : .(eq₁ : m ≡ n) (eq₂ : n ≡ o) (xs : Vec A m)
402+
cast-trans : .(eq₁ : m ≡ n) .(eq₂ : n ≡ o) (xs : Vec A m)
403403
cast eq₂ (cast eq₁ xs) ≡ cast (trans eq₁ eq₂) xs
404404
cast-trans {m = zero} {n = zero} {o = zero} eq₁ eq₂ [] = refl
405405
cast-trans {m = suc _} {n = suc _} {o = suc _} eq₁ eq₂ (x ∷ xs) =
@@ -864,6 +864,12 @@ map-is-foldr f = foldr-universal (Vec _) (λ x ys → f x ∷ ys) (map f) refl (
864864
------------------------------------------------------------------------
865865
-- _∷ʳ_
866866

867+
-- snoc is snoc
868+
869+
unfold-∷ʳ : .(eq : suc n ≡ n + 1) x (xs : Vec A n) cast eq (xs ∷ʳ x) ≡ xs ++ [ x ]
870+
unfold-∷ʳ eq x [] = refl
871+
unfold-∷ʳ eq x (y ∷ xs) = cong (y ∷_) (unfold-∷ʳ (cong pred eq) x xs)
872+
867873
∷ʳ-injective : (xs ys : Vec A n) xs ∷ʳ x ≡ ys ∷ʳ y xs ≡ ys × x ≡ y
868874
∷ʳ-injective [] [] refl = (refl , refl)
869875
∷ʳ-injective (x ∷ xs) (y ∷ ys) eq with ∷-injective eq
@@ -885,12 +891,37 @@ foldr-∷ʳ : ∀ (B : ℕ → Set b) (f : FoldrOp A B) {e} y (ys : Vec A n) →
885891
foldr-∷ʳ B f y [] = refl
886892
foldr-∷ʳ B f y (x ∷ xs) = cong (f x) (foldr-∷ʳ B f y xs)
887893

894+
-- init, last and _∷ʳ_
895+
896+
init-∷ʳ : x (xs : Vec A n) init (xs ∷ʳ x) ≡ xs
897+
init-∷ʳ x [] = refl
898+
init-∷ʳ x (y ∷ xs) = cong (y ∷_) (init-∷ʳ x xs)
899+
900+
last-∷ʳ : x (xs : Vec A n) last (xs ∷ʳ x) ≡ x
901+
last-∷ʳ x [] = refl
902+
last-∷ʳ x (y ∷ xs) = last-∷ʳ x xs
903+
888904
-- map and _∷ʳ_
889905

890906
map-∷ʳ : (f : A B) x (xs : Vec A n) map f (xs ∷ʳ x) ≡ map f xs ∷ʳ f x
891907
map-∷ʳ f x [] = refl
892908
map-∷ʳ f x (y ∷ xs) = cong (f y ∷_) (map-∷ʳ f x xs)
893909

910+
-- cast and _∷ʳ_
911+
912+
cast-∷ʳ : .(eq : suc n ≡ suc m) x (xs : Vec A n)
913+
cast eq (xs ∷ʳ x) ≡ (cast (cong pred eq) xs) ∷ʳ x
914+
cast-∷ʳ {m = zero} eq x [] = refl
915+
cast-∷ʳ {m = suc m} eq x (y ∷ xs) = cong (y ∷_) (cast-∷ʳ (cong pred eq) x xs)
916+
917+
-- _++_ and _∷ʳ_
918+
919+
++-∷ʳ : .(eq : suc (m + n) ≡ m + suc n) z (xs : Vec A m) (ys : Vec A n)
920+
cast eq ((xs ++ ys) ∷ʳ z) ≡ xs ++ (ys ∷ʳ z)
921+
++-∷ʳ {m = zero} eq z [] [] = refl
922+
++-∷ʳ {m = zero} eq z [] (y ∷ ys) = cong (y ∷_) (++-∷ʳ refl z [] ys)
923+
++-∷ʳ {m = suc m} eq z (x ∷ xs) ys = cong (x ∷_) (++-∷ʳ (cong pred eq) z xs ys)
924+
894925
------------------------------------------------------------------------
895926
-- reverse
896927

0 commit comments

Comments
 (0)