Skip to content

Commit a53588f

Browse files
Refactor Data.Vec.Properties : Add toList-injective and new lemmas (#2733)
* toList * better proof * update propreties * Revert changes to WithK.agda * update CHANGELOG * fixwhitespace * change L. to List. * update 1 * fix-whitespace * ∷-ʳ++-eqFree * Update Changelog * Update deprecation warnings --------- Co-authored-by: MatthewDaggitt <[email protected]>
1 parent c645989 commit a53588f

File tree

2 files changed

+126
-70
lines changed

2 files changed

+126
-70
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ Deprecated names
137137
∈⇒∣product ↦ Data.Nat.ListAction.Properties.∈⇒∣product
138138
product≢0 ↦ Data.Nat.ListAction.Properties.product≢0
139139
∈⇒≤product ↦ Data.Nat.ListAction.Properties.∈⇒≤product
140+
∷-ʳ++-eqFree ↦ Data.List.Properties.ʳ++-ʳ++-eqFree
140141
```
141142

142143
* In `Data.List.Relation.Binary.Permutation.Propositional.Properties`:
@@ -366,6 +367,17 @@ Additions to existing modules
366367
LeftInverse (I ×ₛ A) (J ×ₛ B)
367368
```
368369

370+
* In `Data.Vec.Properties`:
371+
```agda
372+
toList-injective : ∀ {m n} → .(m=n : m ≡ n) → (xs : Vec A m) (ys : Vec A n) → toList xs ≡ toList ys → xs ≈[ m=n ] ys
373+
374+
toList-∷ʳ : ∀ x (xs : Vec A n) → toList (xs ∷ʳ x) ≡ toList xs List.++ List.[ x ]
375+
376+
fromList-reverse : (xs : List A) → (fromList (List.reverse xs)) ≈[ List.length-reverse xs ] reverse (fromList xs)
377+
378+
fromList∘toList : ∀ (xs : Vec A n) → fromList (toList xs) ≈[ length-toList xs ] xs
379+
```
380+
369381
* In `Data.Product.Nary.NonDependent`:
370382
```agda
371383
HomoProduct′ n f = Product n (stabulate n (const _) f)

src/Data/Vec/Properties.agda

Lines changed: 114 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ private
5252
m n o :
5353
ws xs ys zs : Vec A n
5454

55+
------------------------------------------------------------------------
56+
-- Properties of toList
57+
58+
toList-injective : .(m≡n : m ≡ n) (xs : Vec A m) (ys : Vec A n)
59+
toList xs ≡ toList ys xs ≈[ m≡n ] ys
60+
toList-injective m≡n [] [] xs=ys = refl
61+
toList-injective m≡n (x ∷ xs) (y ∷ ys) xs=ys =
62+
cong₂ _∷_ (List.∷-injectiveˡ xs=ys) (toList-injective (cong pred m≡n) xs ys (List.∷-injectiveʳ xs=ys))
63+
5564
------------------------------------------------------------------------
5665
-- Properties of propositional equality over vectors
5766

@@ -1016,26 +1025,41 @@ map-reverse : ∀ (f : A → B) (xs : Vec A n) →
10161025
map f (reverse xs) ≡ reverse (map f xs)
10171026
map-reverse f [] = refl
10181027
map-reverse f (x ∷ xs) = begin
1019-
map f (reverse (x ∷ xs)) ≡⟨ cong (map f) (reverse-∷ x xs) ⟩
1020-
map f (reverse xs ∷ʳ x) ≡⟨ map-∷ʳ f x (reverse xs) ⟩
1021-
map f (reverse xs) ∷ʳ f x ≡⟨ cong (_∷ʳ f x) (map-reverse f xs ) ⟩
1028+
map f (reverse (x ∷ xs)) ≡⟨ cong (map f) (reverse-∷ x xs) ⟩
1029+
map f (reverse xs ∷ʳ x) ≡⟨ map-∷ʳ f x (reverse xs) ⟩
1030+
map f (reverse xs) ∷ʳ f x ≡⟨ cong (_∷ʳ f x) (map-reverse f xs ) ⟩
10221031
reverse (map f xs) ∷ʳ f x ≡⟨ reverse-∷ (f x) (map f xs) ⟨
10231032
reverse (f x ∷ map f xs) ≡⟨⟩
10241033
reverse (map f (x ∷ xs)) ∎
10251034
where open ≡-Reasoning
10261035

10271036
-- append and reverse
1037+
toList-∷ʳ : x (xs : Vec A n) toList (xs ∷ʳ x) ≡ toList xs List.++ List.[ x ]
1038+
toList-∷ʳ x [] = refl
1039+
toList-∷ʳ x (y ∷ xs) = cong (y List.∷_) (toList-∷ʳ x xs)
1040+
1041+
toList-reverse : (xs : Vec A n) toList (reverse xs) ≡ List.reverse (toList xs)
1042+
toList-reverse [] = refl
1043+
toList-reverse (x ∷ xs) = begin
1044+
toList (reverse (x ∷ xs)) ≡⟨ cong toList (reverse-∷ x xs) ⟩
1045+
toList (reverse xs ∷ʳ x) ≡⟨ toList-∷ʳ x (reverse xs) ⟩
1046+
toList (reverse xs) List.++ List.[ x ] ≡⟨ cong (List._++ List.[ x ]) (toList-reverse xs) ⟩
1047+
List.reverse (toList xs) List.++ List.[ x ] ≡⟨ List.unfold-reverse x (toList xs) ⟨
1048+
List.reverse (toList (x ∷ xs)) ∎
1049+
where open ≡-Reasoning
10281050

1029-
reverse-++-eqFree : (xs : Vec A m) (ys : Vec A n) let eq = +-comm m n in
1030-
cast eq (reverse (xs ++ ys)) ≡ reverse ys ++ reverse xs
1031-
reverse-++-eqFree {m = zero} {n = n} [] ys = ≈-sym (++-identityʳ-eqFree (reverse ys))
1032-
reverse-++-eqFree {m = suc m} {n = n} (x ∷ xs) ys = begin
1033-
reverse (x ∷ xs ++ ys) ≂⟨ reverse-∷ x (xs ++ ys) ⟩
1034-
reverse (xs ++ ys) ∷ʳ x ≈⟨ ≈-cong′ (_∷ʳ x) (reverse-++-eqFree xs ys) ⟩
1035-
(reverse ys ++ reverse xs) ∷ʳ x ≈⟨ ++-∷ʳ-eqFree x (reverse ys) (reverse xs) ⟩
1036-
reverse ys ++ (reverse xs ∷ʳ x) ≂⟨ cong (reverse ys ++_) (reverse-∷ x xs) ⟨
1037-
reverse ys ++ (reverse (x ∷ xs)) ∎
1038-
where open CastReasoning
1051+
reverse-++-eqFree : (xs : Vec A m) (ys : Vec A n)
1052+
reverse (xs ++ ys) ≈[ +-comm m n ] reverse ys ++ reverse xs
1053+
reverse-++-eqFree {m = m} {n = n} xs ys =
1054+
toList-injective (+-comm m n) (reverse (xs ++ ys)) (reverse ys ++ reverse xs) $
1055+
begin
1056+
toList (reverse (xs ++ ys)) ≡⟨ toList-reverse ((xs ++ ys)) ⟩
1057+
List.reverse (toList (xs ++ ys)) ≡⟨ cong List.reverse (toList-++ xs ys) ⟩
1058+
List.reverse (toList xs List.++ toList ys) ≡⟨ List.reverse-++ (toList xs) (toList ys) ⟩
1059+
List.reverse (toList ys) List.++ List.reverse (toList xs) ≡⟨ cong₂ List._++_ (toList-reverse ys) (toList-reverse xs) ⟨
1060+
toList (reverse ys) List.++ toList (reverse xs) ≡⟨ toList-++ (reverse ys) (reverse xs) ⟨
1061+
toList (reverse ys ++ reverse xs) ∎
1062+
where open ≡-Reasoning
10391063

10401064
cast-reverse : .(eq : m ≡ n) cast eq ∘ reverse {A = A} {n = m} ≗ reverse ∘ cast eq
10411065
cast-reverse _ _ = ≈-cong′ reverse refl
@@ -1061,53 +1085,57 @@ foldr-ʳ++ B f {e} xs = foldl-fusion (foldr B f e) refl (λ _ _ → refl) xs
10611085
map-ʳ++ : (f : A B) (xs : Vec A m)
10621086
map f (xs ʳ++ ys) ≡ map f xs ʳ++ map f ys
10631087
map-ʳ++ {ys = ys} f xs = begin
1064-
map f (xs ʳ++ ys) ≡⟨ cong (map f) (unfold-ʳ++ xs ys) ⟩
1065-
map f (reverse xs ++ ys) ≡⟨ map-++ f (reverse xs) ys ⟩
1066-
map f (reverse xs) ++ map f ys ≡⟨ cong (_++ map f ys) (map-reverse f xs) ⟩
1088+
map f (xs ʳ++ ys) ≡⟨ cong (map f) (unfold-ʳ++ xs ys) ⟩
1089+
map f (reverse xs ++ ys) ≡⟨ map-++ f (reverse xs) ys ⟩
1090+
map f (reverse xs) ++ map f ys ≡⟨ cong (_++ map f ys) (map-reverse f xs) ⟩
10671091
reverse (map f xs) ++ map f ys ≡⟨ unfold-ʳ++ (map f xs) (map f ys) ⟨
10681092
map f xs ʳ++ map f ys ∎
10691093
where open ≡-Reasoning
10701094

1071-
∷-ʳ++-eqFree : a (xs : Vec A m) {ys : Vec A n} let eq = sym (+-suc m n) in
1072-
cast eq ((a ∷ xs) ʳ++ ys) ≡ xs ʳ++ (a ∷ ys)
1073-
∷-ʳ++-eqFree a xs {ys} = begin
1074-
(a ∷ xs) ʳ++ ys ≂⟨ unfold-ʳ++ (a ∷ xs) ys ⟩
1075-
reverse (a ∷ xs) ++ ys ≂⟨ cong (_++ ys) (reverse-∷ a xs) ⟩
1076-
(reverse xs ∷ʳ a) ++ ys ≈⟨ ∷ʳ-++-eqFree a (reverse xs) ⟩
1077-
reverse xs ++ (a ∷ ys) ≂⟨ unfold-ʳ++ xs (a ∷ ys) ⟨
1078-
xs ʳ++ (a ∷ ys) ∎
1079-
where open CastReasoning
1095+
toList-ʳ++ : (xs : Vec A m) {ys : Vec A n}
1096+
toList (xs ʳ++ ys) ≡ (toList xs) List.ʳ++ toList ys
1097+
toList-ʳ++ xs {ys} = begin
1098+
toList (xs ʳ++ ys) ≡⟨ cong toList (unfold-ʳ++ xs ys) ⟩
1099+
toList (reverse xs ++ ys) ≡⟨ toList-++ ((reverse xs )) ys ⟩
1100+
toList (reverse xs) List.++ toList ys ≡⟨ cong (List._++ toList ys) (toList-reverse xs) ⟩
1101+
List.reverse (toList xs) List.++ toList ys ≡⟨ List.ʳ++-defn (toList xs) ⟨
1102+
toList xs List.ʳ++ toList ys ∎
1103+
where open ≡-Reasoning
1104+
10801105

10811106
++-ʳ++-eqFree : (xs : Vec A m) {ys : Vec A n} {zs : Vec A o} let eq = m+n+o≡n+[m+o] m n o in
10821107
cast eq ((xs ++ ys) ʳ++ zs) ≡ ys ʳ++ (xs ʳ++ zs)
1083-
++-ʳ++-eqFree {m = m} {n} {o} xs {ys} {zs} = begin
1084-
((xs ++ ys) ʳ++ zs) ≂⟨ unfold-ʳ++ (xs ++ ys) zs ⟩
1085-
reverse (xs ++ ys) ++ zs ≈⟨ ≈-cong′ (_++ zs) (reverse-++-eqFree xs ys) ⟩
1086-
(reverse ys ++ reverse xs) ++ zs ≈⟨ ++-assoc-eqFree (reverse ys) (reverse xs) zs ⟩
1087-
reverse ys ++ (reverse xs ++ zs) ≂⟨ cong (reverse ys ++_) (unfold-ʳ++ xs zs) ⟨
1088-
reverse ys ++ (xs ʳ++ zs) ≂⟨ unfold-ʳ++ ys (xs ʳ++ zs) ⟨
1089-
ys ʳ++ (xs ʳ++ zs) ∎
1090-
where open CastReasoning
1108+
++-ʳ++-eqFree {m = m} {n} {o} xs {ys} {zs} =
1109+
toList-injective (m+n+o≡n+[m+o] m n o) ((xs ++ ys) ʳ++ zs) (ys ʳ++ (xs ʳ++ zs)) $
1110+
begin
1111+
toList ((xs ++ ys) ʳ++ zs) ≡⟨ toList-ʳ++ (xs ++ ys) ⟩
1112+
toList (xs ++ ys) List.ʳ++ toList zs ≡⟨ cong (List._ʳ++ toList zs) (toList-++ xs ys) ⟩
1113+
((toList xs) List.++ toList ys ) List.ʳ++ toList zs ≡⟨ List.++-ʳ++ (toList xs) ⟩
1114+
toList ys List.ʳ++ (toList xs List.ʳ++ toList zs) ≡⟨ cong (toList ys List.ʳ++_) (toList-ʳ++ xs) ⟨
1115+
toList ys List.ʳ++ toList (xs ʳ++ zs) ≡⟨ toList-ʳ++ ys ⟨
1116+
toList (ys ʳ++ (xs ʳ++ zs)) ∎
1117+
where open ≡-Reasoning
10911118

10921119
ʳ++-ʳ++-eqFree : (xs : Vec A m) {ys : Vec A n} {zs : Vec A o} let eq = m+n+o≡n+[m+o] m n o in
10931120
cast eq ((xs ʳ++ ys) ʳ++ zs) ≡ ys ʳ++ (xs ++ zs)
1094-
ʳ++-ʳ++-eqFree {m = m} {n} {o} xs {ys} {zs} = begin
1095-
(xs ʳ++ ys) ʳ++ zs ≂⟨ cong (_ʳ++ zs) (unfold-ʳ++ xs ys) ⟩
1096-
(reverse xs ++ ys) ʳ++ zs ≂⟨ unfold-ʳ++ (reverse xs ++ ys) zs ⟩
1097-
reverse (reverse xs ++ ys) ++ zs ≈⟨ ≈-cong′ (_++ zs) (reverse-++-eqFree (reverse xs) ys) ⟩
1098-
(reverse ys ++ reverse (reverse xs)) ++ zs ≂⟨ cong ((_++ zs) ∘ (reverse ys ++_)) (reverse-involutive xs) ⟩
1099-
(reverse ys ++ xs) ++ zs ≈⟨ ++-assoc-eqFree (reverse ys) xs zs ⟩
1100-
reverse ys ++ (xs ++ zs) ≂⟨ unfold-ʳ++ ys (xs ++ zs) ⟨
1101-
ys ʳ++ (xs ++ zs) ∎
1102-
where open CastReasoning
1121+
ʳ++-ʳ++-eqFree {m = m} {n} {o} xs {ys} {zs} =
1122+
toList-injective (m+n+o≡n+[m+o] m n o) ((xs ʳ++ ys) ʳ++ zs) (ys ʳ++ (xs ++ zs)) $
1123+
begin
1124+
toList ((xs ʳ++ ys) ʳ++ zs) ≡⟨ toList-ʳ++ (xs ʳ++ ys) ⟩
1125+
toList (xs ʳ++ ys) List.ʳ++ toList zs ≡⟨ cong (List._ʳ++ toList zs) (toList-ʳ++ xs) ⟩
1126+
(toList xs List.ʳ++ toList ys) List.ʳ++ toList zs ≡⟨ List.ʳ++-ʳ++ (toList xs) ⟩
1127+
toList ys List.ʳ++ (toList xs List.++ toList zs) ≡⟨ cong (toList ys List.ʳ++_) (toList-++ xs zs) ⟨
1128+
toList ys List.ʳ++ (toList (xs ++ zs)) ≡⟨ toList-ʳ++ ys ⟨
1129+
toList (ys ʳ++ (xs ++ zs)) ∎
1130+
where open ≡-Reasoning
11031131

11041132
------------------------------------------------------------------------
1105-
-- sum
1133+
--sum
11061134

11071135
sum-++ : (xs : Vec ℕ m) sum (xs ++ ys) ≡ sum xs + sum ys
11081136
sum-++ {_} [] = refl
11091137
sum-++ {ys = ys} (x ∷ xs) = begin
1110-
x + sum (xs ++ ys) ≡⟨ cong (x +_) (sum-++ xs) ⟩
1138+
x + sum (xs ++ ys) ≡⟨ cong (x +_) (sum-++ xs) ⟩
11111139
x + (sum xs + sum ys) ≡⟨ +-assoc x (sum xs) (sum ys) ⟨
11121140
sum (x ∷ xs) + sum ys ∎
11131141
where open ≡-Reasoning
@@ -1293,6 +1321,10 @@ toList∘fromList : (xs : List A) → toList (fromList xs) ≡ xs
12931321
toList∘fromList List.[] = refl
12941322
toList∘fromList (x List.∷ xs) = cong (x List.∷_) (toList∘fromList xs)
12951323

1324+
fromList∘toList : (xs : Vec A n) fromList (toList xs) ≈[ length-toList xs ] xs
1325+
fromList∘toList [] = refl
1326+
fromList∘toList (x ∷ xs) = cong (x ∷_) (fromList∘toList xs)
1327+
12961328
toList-cast : .(eq : m ≡ n) (xs : Vec A m) toList (cast eq xs) ≡ toList xs
12971329
toList-cast {n = zero} eq [] = refl
12981330
toList-cast {n = suc _} eq (x ∷ xs) =
@@ -1318,33 +1350,42 @@ fromList-++ : ∀ (xs : List A) {ys : List A} →
13181350
fromList-++ List.[] {ys} = cast-is-id refl (fromList ys)
13191351
fromList-++ (x List.∷ xs) {ys} = cong (x ∷_) (fromList-++ xs)
13201352

1321-
fromList-reverse : (xs : List A) cast (List.length-reverse xs) (fromList (List.reverse xs)) ≡ reverse (fromList xs)
1322-
fromList-reverse List.[] = refl
1323-
fromList-reverse (x List.∷ xs) = begin
1324-
fromList (List.reverse (x List. xs)) ≈⟨ cast-fromList (List.ʳ++-defn xs)
1325-
fromList (List.reverse xs List.++ List.[ x ]) ≈⟨ fromList-++ (List.reverse xs) ⟩
1326-
fromList (List.reverse xs) ++ [ x ] ≈⟨ unfold-∷ʳ-eqFree x (fromList (List.reverse xs)) ⟨
1327-
fromList (List.reverse xs) ∷ʳ x ≈⟨ ≈-cong′ (_∷ʳ x) (fromList-reverse xs)
1328-
reverse (fromList xs) ∷ʳ x ≂⟨ reverse-∷ x (fromList xs) ⟨
1329-
reverse (x ∷ fromList xs) ≈⟨⟩
1330-
reverse (fromList (x List.∷ xs)) ∎
1331-
where open CastReasoning
1353+
fromList-reverse : (xs : List A)
1354+
(fromList (List.reverse xs)) ≈[ List.length-reverse xs ] reverse (fromList xs)
1355+
fromList-reverse xs =
1356+
toList-injective (List.length-reverse xs) (fromList (List.reverse xs)) (reverse (fromList xs)) $
1357+
begin
1358+
toList (fromList (List.reverse xs)) ≡⟨ toList∘fromList (List.reverse xs)
1359+
List.reverse xs ≡⟨ cong (λ x List.reverse x) (toList∘fromList xs)
1360+
List.reverse (toList (fromList xs)) ≡⟨ toList-reverse (fromList xs) ⟨
1361+
toList (reverse (fromList xs))
1362+
where open ≡-Reasoning
1363+
13321364

13331365
------------------------------------------------------------------------
1334-
-- TRANSITION TO EQ-FREE LEMMA
1366+
-- DEPRECATED NAMES
13351367
------------------------------------------------------------------------
1368+
-- Please use the new names as continuing support for the old names is
1369+
-- not guaranteed.
1370+
1371+
-- Version 2.3
1372+
1373+
∷-ʳ++-eqFree : a (xs : Vec A m) {ys : Vec A n} let eq = sym (+-suc m n) in
1374+
cast eq ((a ∷ xs) ʳ++ ys) ≡ xs ʳ++ (a ∷ ys)
1375+
∷-ʳ++-eqFree a xs {ys} = ʳ++-ʳ++-eqFree (a ∷ []) {ys = xs} {zs = ys}
1376+
{-# WARNING_ON_USAGE ∷-ʳ++-eqFree
1377+
"Warning: ∷-ʳ++-eqFree was deprecated in v2.3.
1378+
Please use ʳ++-ʳ++-eqFree instead, which does not take eq."
1379+
#-}
1380+
1381+
-- Version 2.2
1382+
1383+
-- TRANSITION TO EQ-FREE LEMMA
1384+
--
13361385
-- Please use the new proofs, which do not require an `eq` parameter.
13371386
-- In v3, `name` will be changed to be the same lemma as `name-eqFree`,
13381387
-- and `name-eqFree` will be deprecated.
13391388

1340-
++-assoc : .(eq : (m + n) + o ≡ m + (n + o)) (xs : Vec A m) (ys : Vec A n) (zs : Vec A o)
1341-
cast eq ((xs ++ ys) ++ zs) ≡ xs ++ (ys ++ zs)
1342-
++-assoc _ = ++-assoc-eqFree
1343-
{-# WARNING_ON_USAGE ++-assoc
1344-
"Warning: ++-assoc was deprecated in v2.2.
1345-
Please use ++-assoc-eqFree instead, which does not take eq."
1346-
#-}
1347-
13481389
++-identityʳ : .(eq : n + zero ≡ n) (xs : Vec A n) cast eq (xs ++ []) ≡ xs
13491390
++-identityʳ _ = ++-identityʳ-eqFree
13501391
{-# WARNING_ON_USAGE ++-identityʳ
@@ -1385,7 +1426,8 @@ Please use reverse-++-eqFree instead, which does not take eq."
13851426

13861427
∷-ʳ++ : .(eq : (suc m) + n ≡ m + suc n) a (xs : Vec A m) {ys}
13871428
cast eq ((a ∷ xs) ʳ++ ys) ≡ xs ʳ++ (a ∷ ys)
1388-
∷-ʳ++ _ = ∷-ʳ++-eqFree
1429+
∷-ʳ++ _ a xs {ys} = ʳ++-ʳ++-eqFree (a ∷ []) {ys = xs} {zs = ys}
1430+
13891431
{-# WARNING_ON_USAGE ∷-ʳ++
13901432
"Warning: ∷-ʳ++ was deprecated in v2.2.
13911433
Please use ∷-ʳ++-eqFree instead, which does not take eq."
@@ -1407,11 +1449,13 @@ Please use ++-ʳ++-eqFree instead, which does not take eq."
14071449
Please use ʳ++-ʳ++-eqFree instead, which does not take eq."
14081450
#-}
14091451

1410-
------------------------------------------------------------------------
1411-
-- DEPRECATED NAMES
1412-
------------------------------------------------------------------------
1413-
-- Please use the new names as continuing support for the old names is
1414-
-- not guaranteed.
1452+
++-assoc : .(eq : (m + n) + o ≡ m + (n + o)) (xs : Vec A m) (ys : Vec A n) (zs : Vec A o)
1453+
cast eq ((xs ++ ys) ++ zs) ≡ xs ++ (ys ++ zs)
1454+
++-assoc _ = ++-assoc-eqFree
1455+
{-# WARNING_ON_USAGE ++-assoc
1456+
"Warning: ++-assoc was deprecated in v2.2.
1457+
Please use ++-assoc-eqFree instead, which does not take eq."
1458+
#-}
14151459

14161460
-- Version 2.0
14171461

0 commit comments

Comments
 (0)