Skip to content

Commit 3d3b971

Browse files
committed
Previous PRs content -- will be gone after rebase
1 parent 0661b59 commit 3d3b971

File tree

4 files changed

+141
-13
lines changed

4 files changed

+141
-13
lines changed

CHANGELOG.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,9 @@ Non-backwards compatible changes
858858

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

861+
* In `Data.Vec.Base`: the definitions `init` and `last` have been changed from the `initLast`
862+
view-derived implementation to direct recursive definitions.
863+
861864
* In `Codata.Guarded.Stream` the following functions have been modified to have simpler definitions:
862865
* `cycle`
863866
* `interleave⁺`
@@ -2632,6 +2635,12 @@ Other minor changes
26322635
∷ʳ-injectiveˡ : xs ∷ʳ x ≡ ys ∷ʳ y → xs ≡ ys
26332636
∷ʳ-injectiveʳ : xs ∷ʳ x ≡ ys ∷ʳ y → x ≡ y
26342637
2638+
unfold-∷ʳ : cast eq (xs ∷ʳ x) ≡ xs ++ [ x ]
2639+
init-∷ʳ : init (xs ∷ʳ x) ≡ xs
2640+
last-∷ʳ : last (xs ∷ʳ x) ≡ x
2641+
cast-∷ʳ : cast eq (xs ∷ʳ x) ≡ (cast (cong pred eq) xs) ∷ʳ x
2642+
++-∷ʳ : cast eq ((xs ++ ys) ∷ʳ z) ≡ xs ++ (ys ∷ʳ z)
2643+
26352644
reverse-∷ : reverse (x ∷ xs) ≡ reverse xs ∷ʳ x
26362645
reverse-involutive : Involutive _≡_ reverse
26372646
reverse-reverse : reverse xs ≡ ys → reverse ys ≡ xs
@@ -2642,16 +2651,27 @@ Other minor changes
26422651
26432652
toList-++ : toList (xs ++ ys) ≡ toList xs List.++ toList ys
26442653
2645-
toList-cast : toList (cast eq xs) ≡ toList xs
26462654
cast-is-id : cast eq xs ≡ xs
26472655
subst-is-cast : subst (Vec A) eq xs ≡ cast eq xs
26482656
cast-trans : cast eq₂ (cast eq₁ xs) ≡ cast (trans eq₁ eq₂) xs
26492657
map-cast : map f (cast eq xs) ≡ cast eq (map f xs)
26502658
lookup-cast : lookup (cast eq xs) (Fin.cast eq i) ≡ lookup xs i
26512659
lookup-cast₁ : lookup (cast eq xs) i ≡ lookup xs (Fin.cast (sym eq) i)
26522660
lookup-cast₂ : lookup xs (Fin.cast eq i) ≡ lookup (cast (sym eq) xs) i
2661+
cast-reverse : cast eq ∘ reverse ≗ reverse ∘ cast eq
26532662
26542663
zipwith-++ : zipWith f (xs ++ ys) (xs' ++ ys') ≡ zipWith f xs xs' ++ zipWith f ys ys'
2664+
2665+
++-assoc : cast eq ((xs ++ ys) ++ zs) ≡ xs ++ (ys ++ zs)
2666+
++-identityʳ : cast eq (xs ++ []) ≡ xs
2667+
init-reverse : init ∘ reverse ≗ reverse ∘ tail
2668+
last-reverse : last ∘ reverse ≗ head
2669+
reverse-++ : cast eq (reverse (xs ++ ys)) ≡ reverse ys ++ reverse xs
2670+
2671+
toList-cast : toList (cast eq xs) ≡ toList xs
2672+
cast-fromList : cast _ (fromList xs) ≡ fromList ys
2673+
fromList-map : cast _ (fromList (List.map f xs)) ≡ map f (fromList xs)
2674+
fromList-++ : cast _ (fromList (xs List.++ ys)) ≡ fromList xs ++ fromList ys
26552675
```
26562676

26572677
* Added new proofs in `Data.Vec.Membership.Propositional.Properties`:

src/Data/Fin/Properties.agda

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ cast-is-id eq (suc k) = cong suc (cast-is-id (ℕₚ.suc-injective eq) k)
262262
subst-is-cast : (eq : m ≡ n) (k : Fin m) subst Fin eq k ≡ cast eq k
263263
subst-is-cast refl k = sym (cast-is-id refl k)
264264

265-
cast-trans : .(eq₁ : m ≡ n) (eq₂ : n ≡ o) (k : Fin m)
265+
cast-trans : .(eq₁ : m ≡ n) .(eq₂ : n ≡ o) (k : Fin m)
266266
cast eq₂ (cast eq₁ k) ≡ cast (trans eq₁ eq₂) k
267267
cast-trans {m = suc _} {n = suc _} {o = suc _} eq₁ eq₂ zero = refl
268268
cast-trans {m = suc _} {n = suc _} {o = suc _} eq₁ eq₂ (suc k) =

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: 115 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ open import Algebra.Definitions
1212
open import Data.Bool.Base using (true; false)
1313
open import Data.Fin.Base as Fin using (Fin; zero; suc; toℕ; fromℕ<; _↑ˡ_; _↑ʳ_)
1414
open import Data.List.Base as List using (List)
15+
import Data.List.Properties as Listₚ
1516
open import Data.Nat.Base
1617
open import Data.Nat.Properties
17-
using (+-assoc; m≤n⇒m≤1+n; ≤-refl; ≤-trans; suc-injective)
18+
using (+-assoc; m≤n⇒m≤1+n; ≤-refl; ≤-trans; suc-injective; +-comm; +-suc)
1819
open import Data.Product.Base as Prod
1920
using (_×_; _,_; proj₁; proj₂; <_,_>; uncurry)
2021
open import Data.Sum.Base using ([_,_]′)
@@ -387,19 +388,14 @@ lookup∘update′ {i = i} {j} i≢j xs y = lookup∘updateAt′ i j i≢j xs
387388
------------------------------------------------------------------------
388389
-- cast
389390

390-
toList-cast : .(eq : m ≡ n) (xs : Vec A m) toList (cast eq xs) ≡ toList xs
391-
toList-cast {n = zero} eq [] = refl
392-
toList-cast {n = suc _} eq (x ∷ xs) =
393-
cong (x List.∷_) (toList-cast (cong pred eq) xs)
394-
395391
cast-is-id : .(eq : m ≡ m) (xs : Vec A m) cast eq xs ≡ xs
396392
cast-is-id eq [] = refl
397393
cast-is-id eq (x ∷ xs) = cong (x ∷_) (cast-is-id (suc-injective eq) xs)
398394

399395
subst-is-cast : (eq : m ≡ n) (xs : Vec A m) subst (Vec A) eq xs ≡ cast eq xs
400396
subst-is-cast refl xs = sym (cast-is-id refl xs)
401397

402-
cast-trans : .(eq₁ : m ≡ n) (eq₂ : n ≡ o) (xs : Vec A m)
398+
cast-trans : .(eq₁ : m ≡ n) .(eq₂ : n ≡ o) (xs : Vec A m)
403399
cast eq₂ (cast eq₁ xs) ≡ cast (trans eq₁ eq₂) xs
404400
cast-trans {m = zero} {n = zero} {o = zero} eq₁ eq₂ [] = refl
405401
cast-trans {m = suc _} {n = suc _} {o = suc _} eq₁ eq₂ (x ∷ xs) =
@@ -488,6 +484,15 @@ toList-map f (x ∷ xs) = cong (f x List.∷_) (toList-map f xs)
488484
++-injective ws xs eq =
489485
(++-injectiveˡ ws xs eq , ++-injectiveʳ ws xs eq)
490486

487+
++-assoc : .(eq : (m + n) + o ≡ m + (n + o)) (xs : Vec A m) (ys : Vec A n) (zs : Vec A o)
488+
cast eq ((xs ++ ys) ++ zs) ≡ xs ++ (ys ++ zs)
489+
++-assoc eq [] ys zs = cast-is-id eq (ys ++ zs)
490+
++-assoc eq (x ∷ xs) ys zs = cong (x ∷_) (++-assoc (cong pred eq) xs ys zs)
491+
492+
++-identityʳ : .(eq : n + zero ≡ n) (xs : Vec A n) cast eq (xs ++ []) ≡ xs
493+
++-identityʳ eq [] = refl
494+
++-identityʳ eq (x ∷ xs) = cong (x ∷_) (++-identityʳ (cong pred eq) xs)
495+
491496
lookup-++-< : (xs : Vec A m) (ys : Vec A n)
492497
i (i<m : toℕ i < m)
493498
lookup (xs ++ ys) i ≡ lookup xs (Fin.fromℕ< i<m)
@@ -864,6 +869,12 @@ map-is-foldr f = foldr-universal (Vec _) (λ x ys → f x ∷ ys) (map f) refl (
864869
------------------------------------------------------------------------
865870
-- _∷ʳ_
866871

872+
-- snoc is snoc
873+
874+
unfold-∷ʳ : .(eq : suc n ≡ n + 1) x (xs : Vec A n) cast eq (xs ∷ʳ x) ≡ xs ++ [ x ]
875+
unfold-∷ʳ eq x [] = refl
876+
unfold-∷ʳ eq x (y ∷ xs) = cong (y ∷_) (unfold-∷ʳ (cong pred eq) x xs)
877+
867878
∷ʳ-injective : (xs ys : Vec A n) xs ∷ʳ x ≡ ys ∷ʳ y xs ≡ ys × x ≡ y
868879
∷ʳ-injective [] [] refl = (refl , refl)
869880
∷ʳ-injective (x ∷ xs) (y ∷ ys) eq with ∷-injective eq
@@ -885,12 +896,37 @@ foldr-∷ʳ : ∀ (B : ℕ → Set b) (f : FoldrOp A B) {e} y (ys : Vec A n) →
885896
foldr-∷ʳ B f y [] = refl
886897
foldr-∷ʳ B f y (x ∷ xs) = cong (f x) (foldr-∷ʳ B f y xs)
887898

899+
-- init, last and _∷ʳ_
900+
901+
init-∷ʳ : x (xs : Vec A n) init (xs ∷ʳ x) ≡ xs
902+
init-∷ʳ x [] = refl
903+
init-∷ʳ x (y ∷ xs) = cong (y ∷_) (init-∷ʳ x xs)
904+
905+
last-∷ʳ : x (xs : Vec A n) last (xs ∷ʳ x) ≡ x
906+
last-∷ʳ x [] = refl
907+
last-∷ʳ x (y ∷ xs) = last-∷ʳ x xs
908+
888909
-- map and _∷ʳ_
889910

890911
map-∷ʳ : (f : A B) x (xs : Vec A n) map f (xs ∷ʳ x) ≡ map f xs ∷ʳ f x
891912
map-∷ʳ f x [] = refl
892913
map-∷ʳ f x (y ∷ xs) = cong (f y ∷_) (map-∷ʳ f x xs)
893914

915+
-- cast and _∷ʳ_
916+
917+
cast-∷ʳ : .(eq : suc n ≡ suc m) x (xs : Vec A n)
918+
cast eq (xs ∷ʳ x) ≡ (cast (cong pred eq) xs) ∷ʳ x
919+
cast-∷ʳ {m = zero} eq x [] = refl
920+
cast-∷ʳ {m = suc m} eq x (y ∷ xs) = cong (y ∷_) (cast-∷ʳ (cong pred eq) x xs)
921+
922+
-- _++_ and _∷ʳ_
923+
924+
++-∷ʳ : .(eq : suc (m + n) ≡ m + suc n) z (xs : Vec A m) (ys : Vec A n)
925+
cast eq ((xs ++ ys) ∷ʳ z) ≡ xs ++ (ys ∷ʳ z)
926+
++-∷ʳ {m = zero} eq z [] [] = refl
927+
++-∷ʳ {m = zero} eq z [] (y ∷ ys) = cong (y ∷_) (++-∷ʳ refl z [] ys)
928+
++-∷ʳ {m = suc m} eq z (x ∷ xs) ys = cong (x ∷_) (++-∷ʳ (cong pred eq) z xs ys)
929+
894930
------------------------------------------------------------------------
895931
-- reverse
896932

@@ -939,6 +975,20 @@ reverse-injective {xs = xs} {ys} eq = begin
939975
reverse (reverse ys) ≡⟨ reverse-involutive ys ⟩
940976
ys ∎
941977

978+
-- init and last of reverse
979+
980+
init-reverse : init ∘ reverse ≗ reverse ∘ tail {A = A} {n = n}
981+
init-reverse (x ∷ xs) = begin
982+
init (reverse (x ∷ xs)) ≡⟨ cong init (reverse-∷ x xs) ⟩
983+
init (reverse xs ∷ʳ x) ≡⟨ init-∷ʳ x (reverse xs) ⟩
984+
reverse xs ∎
985+
986+
last-reverse : last ∘ reverse ≗ head {A = A} {n = n}
987+
last-reverse (x ∷ xs) = begin
988+
last (reverse (x ∷ xs)) ≡⟨ cong last (reverse-∷ x xs) ⟩
989+
last (reverse xs ∷ʳ x) ≡⟨ last-∷ʳ x (reverse xs) ⟩
990+
x ∎
991+
942992
-- map and reverse
943993

944994
map-reverse : (f : A B) (xs : Vec A n)
@@ -952,6 +1002,39 @@ map-reverse f (x ∷ xs) = begin
9521002
reverse (f x ∷ map f xs) ≡⟨⟩
9531003
reverse (map f (x ∷ xs)) ∎
9541004

1005+
-- append and reverse
1006+
1007+
reverse-++ : .(eq : m + n ≡ n + m) (xs : Vec A m) (ys : Vec A n)
1008+
cast eq (reverse (xs ++ ys)) ≡ reverse ys ++ reverse xs
1009+
reverse-++ {m = zero} {n = n} eq [] ys = begin
1010+
cast _ (reverse ys) ≡˘⟨ cong (cast eq) (++-identityʳ (sym eq) (reverse ys)) ⟩
1011+
cast _ (cast _ (reverse ys ++ [])) ≡⟨ cast-trans (sym eq) eq (reverse ys ++ []) ⟩
1012+
cast _ (reverse ys ++ []) ≡⟨ cast-is-id (trans (sym eq) eq) (reverse ys ++ []) ⟩
1013+
reverse ys ++ [] ≡⟨⟩
1014+
reverse ys ++ reverse [] ∎
1015+
reverse-++ {m = suc m} {n = n} eq (x ∷ xs) ys = begin
1016+
cast eq (reverse (x ∷ xs ++ ys)) ≡⟨ cong (cast eq) (reverse-∷ x (xs ++ ys)) ⟩
1017+
cast eq (reverse (xs ++ ys) ∷ʳ x) ≡˘⟨ cast-trans eq₂ eq₁ (reverse (xs ++ ys) ∷ʳ x) ⟩
1018+
(cast eq₁ ∘ cast eq₂) (reverse (xs ++ ys) ∷ʳ x) ≡⟨ cong (cast eq₁) (cast-∷ʳ _ x (reverse (xs ++ ys))) ⟩
1019+
cast eq₁ ((cast eq₃ (reverse (xs ++ ys))) ∷ʳ x) ≡⟨ cong (cast eq₁) (cong (_∷ʳ x) (reverse-++ _ xs ys)) ⟩
1020+
cast eq₁ ((reverse ys ++ reverse xs) ∷ʳ x) ≡⟨ ++-∷ʳ _ x (reverse ys) (reverse xs) ⟩
1021+
reverse ys ++ (reverse xs ∷ʳ x) ≡˘⟨ cong (reverse ys ++_) (reverse-∷ x xs) ⟩
1022+
reverse ys ++ (reverse (x ∷ xs)) ∎
1023+
where
1024+
eq₁ = sym (+-suc n m)
1025+
eq₂ = cong suc (+-comm m n)
1026+
eq₃ = cong pred eq₂
1027+
1028+
cast-reverse : .(eq : m ≡ n) cast eq ∘ reverse {A = A} {n = m} ≗ reverse ∘ cast eq
1029+
cast-reverse {n = zero} eq [] = refl
1030+
cast-reverse {n = suc n} eq (x ∷ xs) = begin
1031+
cast eq (reverse (x ∷ xs)) ≡⟨ cong (cast eq) (reverse-∷ x xs) ⟩
1032+
cast eq (reverse xs ∷ʳ x) ≡⟨ cast-∷ʳ eq x (reverse xs) ⟩
1033+
(cast (cong pred eq) (reverse xs)) ∷ʳ x ≡⟨ cong (_∷ʳ x) (cast-reverse (cong pred eq) xs) ⟩
1034+
(reverse (cast (cong pred eq) xs)) ∷ʳ x ≡˘⟨ reverse-∷ x (cast (cong pred eq) xs) ⟩
1035+
reverse (x ∷ cast (cong pred eq) xs) ≡⟨⟩
1036+
reverse (cast eq (x ∷ xs)) ∎
1037+
9551038
------------------------------------------------------------------------
9561039
-- _ʳ++_
9571040

@@ -1130,6 +1213,31 @@ toList∘fromList : (xs : List A) → toList (fromList xs) ≡ xs
11301213
toList∘fromList List.[] = refl
11311214
toList∘fromList (x List.∷ xs) = cong (x List.∷_) (toList∘fromList xs)
11321215

1216+
toList-cast : .(eq : m ≡ n) (xs : Vec A m) toList (cast eq xs) ≡ toList xs
1217+
toList-cast {n = zero} eq [] = refl
1218+
toList-cast {n = suc _} eq (x ∷ xs) =
1219+
cong (x List.∷_) (toList-cast (cong pred eq) xs)
1220+
1221+
cast-fromList : {xs ys : List A} (eq : xs ≡ ys)
1222+
cast (cong List.length eq) (fromList xs) ≡ fromList ys
1223+
cast-fromList {xs = List.[]} {ys = List.[]} eq = refl
1224+
cast-fromList {xs = x List.∷ xs} {ys = y List.∷ ys} eq = begin
1225+
x ∷ cast (cong (pred ∘ List.length) eq) (fromList xs) ≡⟨ cong (_ ∷_) (cast-fromList xs-equals-ys) ⟩
1226+
x ∷ fromList ys ≡⟨ cong (_∷ _) x-equals-y ⟩
1227+
y ∷ fromList ys ∎
1228+
where
1229+
x-equals-y = proj₁ (Listₚ.∷-injective eq)
1230+
xs-equals-ys = proj₂ (Listₚ.∷-injective eq)
1231+
1232+
fromList-map : (f : A B) (xs : List A)
1233+
cast (Listₚ.length-map f xs) (fromList (List.map f xs)) ≡ map f (fromList xs)
1234+
fromList-map f List.[] = refl
1235+
fromList-map f (x List.∷ xs) = cong (f x ∷_) (fromList-map f xs)
1236+
1237+
fromList-++ : (xs : List A) {ys : List A}
1238+
cast (Listₚ.length-++ xs) (fromList (xs List.++ ys)) ≡ fromList xs ++ fromList ys
1239+
fromList-++ List.[] {ys} = cast-is-id refl (fromList ys)
1240+
fromList-++ (x List.∷ xs) {ys} = cong (x ∷_) (fromList-++ xs)
11331241

11341242
------------------------------------------------------------------------
11351243
-- DEPRECATED NAMES

0 commit comments

Comments
 (0)