@@ -12,9 +12,10 @@ open import Algebra.Definitions
12
12
open import Data.Bool.Base using (true; false)
13
13
open import Data.Fin.Base as Fin using (Fin; zero; suc; toℕ; fromℕ<; _↑ˡ_; _↑ʳ_)
14
14
open import Data.List.Base as List using (List)
15
+ import Data.List.Properties as Listₚ
15
16
open import Data.Nat.Base
16
17
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 )
18
19
open import Data.Product.Base as Prod
19
20
using (_×_; _,_; proj₁; proj₂; <_,_>; uncurry)
20
21
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
387
388
------------------------------------------------------------------------
388
389
-- cast
389
390
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
-
395
391
cast-is-id : .(eq : m ≡ m) (xs : Vec A m) → cast eq xs ≡ xs
396
392
cast-is-id eq [] = refl
397
393
cast-is-id eq (x ∷ xs) = cong (x ∷_) (cast-is-id (suc-injective eq) xs)
398
394
399
395
subst-is-cast : (eq : m ≡ n) (xs : Vec A m) → subst (Vec A) eq xs ≡ cast eq xs
400
396
subst-is-cast refl xs = sym (cast-is-id refl xs)
401
397
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) →
403
399
cast eq₂ (cast eq₁ xs) ≡ cast (trans eq₁ eq₂) xs
404
400
cast-trans {m = zero} {n = zero} {o = zero} eq₁ eq₂ [] = refl
405
401
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)
488
484
++-injective ws xs eq =
489
485
(++-injectiveˡ ws xs eq , ++-injectiveʳ ws xs eq)
490
486
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
+
491
496
lookup-++-< : ∀ (xs : Vec A m) (ys : Vec A n) →
492
497
∀ i (i<m : toℕ i < m) →
493
498
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 (
864
869
------------------------------------------------------------------------
865
870
-- _∷ʳ_
866
871
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
+
867
878
∷ʳ-injective : ∀ (xs ys : Vec A n) → xs ∷ʳ x ≡ ys ∷ʳ y → xs ≡ ys × x ≡ y
868
879
∷ʳ-injective [] [] refl = (refl , refl)
869
880
∷ʳ-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) →
885
896
foldr-∷ʳ B f y [] = refl
886
897
foldr-∷ʳ B f y (x ∷ xs) = cong (f x) (foldr-∷ʳ B f y xs)
887
898
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
+
888
909
-- map and _∷ʳ_
889
910
890
911
map-∷ʳ : ∀ (f : A → B) x (xs : Vec A n) → map f (xs ∷ʳ x) ≡ map f xs ∷ʳ f x
891
912
map-∷ʳ f x [] = refl
892
913
map-∷ʳ f x (y ∷ xs) = cong (f y ∷_) (map-∷ʳ f x xs)
893
914
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
+
894
930
------------------------------------------------------------------------
895
931
-- reverse
896
932
@@ -939,6 +975,20 @@ reverse-injective {xs = xs} {ys} eq = begin
939
975
reverse (reverse ys) ≡⟨ reverse-involutive ys ⟩
940
976
ys ∎
941
977
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
+
942
992
-- map and reverse
943
993
944
994
map-reverse : ∀ (f : A → B) (xs : Vec A n) →
@@ -952,6 +1002,39 @@ map-reverse f (x ∷ xs) = begin
952
1002
reverse (f x ∷ map f xs) ≡⟨⟩
953
1003
reverse (map f (x ∷ xs)) ∎
954
1004
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
+
955
1038
------------------------------------------------------------------------
956
1039
-- _ʳ++_
957
1040
@@ -1130,6 +1213,31 @@ toList∘fromList : (xs : List A) → toList (fromList xs) ≡ xs
1130
1213
toList∘fromList List.[] = refl
1131
1214
toList∘fromList (x List.∷ xs) = cong (x List.∷_) (toList∘fromList xs)
1132
1215
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)
1133
1241
1134
1242
------------------------------------------------------------------------
1135
1243
-- DEPRECATED NAMES
0 commit comments