Skip to content

Commit 279fa18

Browse files
authored
Vec.Properties: introduce ≈-cong′ (#2424)
* Vec.Properties: introduce `≈-cong′` * derive cast-* lemmas from it * use it in proofs * match m and n before the Vecs * convert Cast's implicit module param to variable (needed for next commit) * move ≈-cong′ to Cast * add changelog entry * implement suggested changes - do not export lemma from Vec.Properties - use Function.Base rather than Function
1 parent 4a8cd32 commit 279fa18

File tree

4 files changed

+38
-36
lines changed

4 files changed

+38
-36
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,13 @@ Additions to existing modules
545545
map-concat : map f (concat xss) ≡ concat (map (map f) xss)
546546
```
547547

548+
* New lemma in `Data.Vec.Relation.Binary.Equality.Cast`:
549+
```agda
550+
≈-cong′ : ∀ {f-len : ℕ → ℕ} (f : ∀ {n} → Vec A n → Vec B (f-len n))
551+
{m n} {xs : Vec A m} {ys : Vec A n} .{eq} →
552+
xs ≈[ eq ] ys → f xs ≈[ _ ] f ys
553+
```
554+
548555
* In `Data.Vec.Relation.Binary.Equality.DecPropositional`:
549556
```agda
550557
_≡?_ : DecidableEquality (Vec A n)

doc/README/Data/Vec/Relation/Binary/Equality/Cast.agda

+4-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ open import Data.Nat.Properties
2020
open import Data.Vec.Base
2121
open import Data.Vec.Properties
2222
open import Data.Vec.Relation.Binary.Equality.Cast
23+
open import Function using (_∘_)
2324
open import Relation.Binary.PropositionalEquality
2425
using (_≡_; refl; sym; cong; module ≡-Reasoning)
2526

@@ -187,7 +188,7 @@ example3a-fromList-++-++ {xs = xs} {ys} {zs} eq = begin
187188
fromList (xs List.++ ys List.++ zs)
188189
≈⟨ fromList-++ xs ⟩
189190
fromList xs ++ fromList (ys List.++ zs)
190-
≈⟨ ≈-cong (fromList xs ++_) (cast-++ʳ (List.length-++ ys) (fromList xs)) (fromList-++ ys) ⟩
191+
≈⟨ ≈-cong (fromList xs ++_) (fromList-++ ys) ⟩
191192
fromList xs ++ fromList ys ++ fromList zs
192193
193194
where open CastReasoning
@@ -218,9 +219,7 @@ example4-cong² : ∀ .(eq : (m + 1) + n ≡ n + suc m) a (xs : Vec A m) ys →
218219
cast eq (reverse ((xs ++ [ a ]) ++ ys)) ≡ ys ʳ++ reverse (xs ∷ʳ a)
219220
example4-cong² {m = m} {n} eq a xs ys = begin
220221
reverse ((xs ++ [ a ]) ++ ys)
221-
≈⟨ ≈-cong reverse (cast-reverse (cong (_+ n) (+-comm 1 m)) ((xs ∷ʳ a) ++ ys))
222-
(≈-cong (_++ ys) (cast-++ˡ (+-comm 1 m) (xs ∷ʳ a))
223-
(unfold-∷ʳ-eqFree a xs)) ⟨
222+
≈⟨ ≈-cong′ (reverse ∘ (_++ ys)) (unfold-∷ʳ-eqFree a xs) ⟨
224223
reverse ((xs ∷ʳ a) ++ ys)
225224
≈⟨ reverse-++-eqFree (xs ∷ʳ a) ys ⟩
226225
reverse ys ++ reverse (xs ∷ʳ a)
@@ -264,7 +263,7 @@ example6a-reverse-∷ʳ {n = n} x xs = begin-≡
264263
reverse (xs ∷ʳ x)
265264
≡⟨ ≈-reflexive refl ⟨
266265
reverse (xs ∷ʳ x)
267-
≈⟨ ≈-cong reverse (cast-reverse _ _) (unfold-∷ʳ-eqFree x xs) ⟩
266+
≈⟨ ≈-cong reverse (unfold-∷ʳ-eqFree x xs) ⟩
268267
reverse (xs ++ [ x ])
269268
≈⟨ reverse-++-eqFree xs [ x ] ⟩
270269
x ∷ reverse xs

src/Data/Vec/Properties.agda

+11-26
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,8 @@ lookup∘update′ {i = i} {j} i≢j xs y = lookup∘updateAt′ i j i≢j xs
375375
open VecCast public
376376
using (cast-is-id; cast-trans)
377377

378+
open VecCast using (≈-cong′)
379+
378380
subst-is-cast : (eq : m ≡ n) (xs : Vec A m) subst (Vec A) eq xs ≡ cast eq xs
379381
subst-is-cast refl xs = sym (cast-is-id refl xs)
380382

@@ -398,9 +400,7 @@ map-const (_ ∷ xs) y = cong (y ∷_) (map-const xs y)
398400

399401
map-cast : (f : A B) .(eq : m ≡ n) (xs : Vec A m)
400402
map f (cast eq xs) ≡ cast eq (map f xs)
401-
map-cast {n = zero} f eq [] = refl
402-
map-cast {n = suc _} f eq (x ∷ xs)
403-
= cong (f x ∷_) (map-cast f (suc-injective eq) xs)
403+
map-cast f _ _ = sym (≈-cong′ (map f) refl)
404404

405405
map-++ : (f : A B) (xs : Vec A m) (ys : Vec A n)
406406
map f (xs ++ ys) ≡ map f xs ++ map f ys
@@ -494,13 +494,11 @@ toList-map f (x ∷ xs) = cong (f x List.∷_) (toList-map f xs)
494494

495495
cast-++ˡ : .(eq : m ≡ o) (xs : Vec A m) {ys : Vec A n}
496496
cast (cong (_+ n) eq) (xs ++ ys) ≡ cast eq xs ++ ys
497-
cast-++ˡ {o = zero} eq [] {ys} = cast-is-id refl (cast eq [] ++ ys)
498-
cast-++ˡ {o = suc o} eq (x ∷ xs) {ys} = cong (x ∷_) (cast-++ˡ (cong pred eq) xs)
497+
cast-++ˡ _ _ {ys} = ≈-cong′ (_++ ys) refl
499498

500499
cast-++ʳ : .(eq : n ≡ o) (xs : Vec A m) {ys : Vec A n}
501500
cast (cong (m +_) eq) (xs ++ ys) ≡ xs ++ cast eq ys
502-
cast-++ʳ {m = zero} eq [] {ys} = refl
503-
cast-++ʳ {m = suc m} eq (x ∷ xs) {ys} = cong (x ∷_) (cast-++ʳ eq xs)
501+
cast-++ʳ _ xs = ≈-cong′ (xs ++_) refl
504502

505503
lookup-++-< : (xs : Vec A m) (ys : Vec A n)
506504
i (i<m : toℕ i < m)
@@ -929,8 +927,7 @@ map-∷ʳ f x (y ∷ xs) = cong (f y ∷_) (map-∷ʳ f x xs)
929927

930928
cast-∷ʳ : .(eq : suc n ≡ suc m) x (xs : Vec A n)
931929
cast eq (xs ∷ʳ x) ≡ (cast (cong pred eq) xs) ∷ʳ x
932-
cast-∷ʳ {m = zero} eq x [] = refl
933-
cast-∷ʳ {m = suc m} eq x (y ∷ xs) = cong (y ∷_) (cast-∷ʳ (cong pred eq) x xs)
930+
cast-∷ʳ _ x _ = ≈-cong′ (_∷ʳ x) refl
934931

935932
-- _++_ and _∷ʳ_
936933

@@ -1034,23 +1031,14 @@ reverse-++-eqFree : ∀ (xs : Vec A m) (ys : Vec A n) → let eq = +-comm m n in
10341031
reverse-++-eqFree {m = zero} {n = n} [] ys = ≈-sym (++-identityʳ-eqFree (reverse ys))
10351032
reverse-++-eqFree {m = suc m} {n = n} (x ∷ xs) ys = begin
10361033
reverse (x ∷ xs ++ ys) ≂⟨ reverse-∷ x (xs ++ ys) ⟩
1037-
reverse (xs ++ ys) ∷ʳ x ≈⟨ ≈-cong (_∷ʳ x) (cast-∷ʳ (cong suc (+-comm m n)) x (reverse (xs ++ ys)))
1038-
(reverse-++-eqFree xs ys) ⟩
1034+
reverse (xs ++ ys) ∷ʳ x ≈⟨ ≈-cong′ (_∷ʳ x) (reverse-++-eqFree xs ys) ⟩
10391035
(reverse ys ++ reverse xs) ∷ʳ x ≈⟨ ++-∷ʳ-eqFree x (reverse ys) (reverse xs) ⟩
10401036
reverse ys ++ (reverse xs ∷ʳ x) ≂⟨ cong (reverse ys ++_) (reverse-∷ x xs) ⟨
10411037
reverse ys ++ (reverse (x ∷ xs)) ∎
10421038
where open CastReasoning
10431039

10441040
cast-reverse : .(eq : m ≡ n) cast eq ∘ reverse {A = A} {n = m} ≗ reverse ∘ cast eq
1045-
cast-reverse {n = zero} eq [] = refl
1046-
cast-reverse {n = suc n} eq (x ∷ xs) = begin
1047-
reverse (x ∷ xs) ≂⟨ reverse-∷ x xs ⟩
1048-
reverse xs ∷ʳ x ≈⟨ ≈-cong (_∷ʳ x) (cast-∷ʳ eq x (reverse xs))
1049-
(cast-reverse (cong pred eq) xs) ⟩
1050-
reverse (cast _ xs) ∷ʳ x ≂⟨ reverse-∷ x (cast (cong pred eq) xs) ⟨
1051-
reverse (x ∷ cast _ xs) ≈⟨⟩
1052-
reverse (cast eq (x ∷ xs)) ∎
1053-
where open CastReasoning
1041+
cast-reverse _ _ = ≈-cong′ reverse refl
10541042

10551043
------------------------------------------------------------------------
10561044
-- _ʳ++_
@@ -1094,8 +1082,7 @@ map-ʳ++ {ys = ys} f xs = begin
10941082
cast eq ((xs ++ ys) ʳ++ zs) ≡ ys ʳ++ (xs ʳ++ zs)
10951083
++-ʳ++-eqFree {m = m} {n} {o} xs {ys} {zs} = begin
10961084
((xs ++ ys) ʳ++ zs) ≂⟨ unfold-ʳ++ (xs ++ ys) zs ⟩
1097-
reverse (xs ++ ys) ++ zs ≈⟨ ≈-cong (_++ zs) (cast-++ˡ (+-comm m n) (reverse (xs ++ ys)))
1098-
(reverse-++-eqFree xs ys) ⟩
1085+
reverse (xs ++ ys) ++ zs ≈⟨ ≈-cong′ (_++ zs) (reverse-++-eqFree xs ys) ⟩
10991086
(reverse ys ++ reverse xs) ++ zs ≈⟨ ++-assoc-eqFree (reverse ys) (reverse xs) zs ⟩
11001087
reverse ys ++ (reverse xs ++ zs) ≂⟨ cong (reverse ys ++_) (unfold-ʳ++ xs zs) ⟨
11011088
reverse ys ++ (xs ʳ++ zs) ≂⟨ unfold-ʳ++ ys (xs ʳ++ zs) ⟨
@@ -1107,8 +1094,7 @@ map-ʳ++ {ys = ys} f xs = begin
11071094
ʳ++-ʳ++-eqFree {m = m} {n} {o} xs {ys} {zs} = begin
11081095
(xs ʳ++ ys) ʳ++ zs ≂⟨ cong (_ʳ++ zs) (unfold-ʳ++ xs ys) ⟩
11091096
(reverse xs ++ ys) ʳ++ zs ≂⟨ unfold-ʳ++ (reverse xs ++ ys) zs ⟩
1110-
reverse (reverse xs ++ ys) ++ zs ≈⟨ ≈-cong (_++ zs) (cast-++ˡ (+-comm m n) (reverse (reverse xs ++ ys)))
1111-
(reverse-++-eqFree (reverse xs) ys) ⟩
1097+
reverse (reverse xs ++ ys) ++ zs ≈⟨ ≈-cong′ (_++ zs) (reverse-++-eqFree (reverse xs) ys) ⟩
11121098
(reverse ys ++ reverse (reverse xs)) ++ zs ≂⟨ cong ((_++ zs) ∘ (reverse ys ++_)) (reverse-involutive xs) ⟩
11131099
(reverse ys ++ xs) ++ zs ≈⟨ ++-assoc-eqFree (reverse ys) xs zs ⟩
11141100
reverse ys ++ (xs ++ zs) ≂⟨ unfold-ʳ++ ys (xs ++ zs) ⟨
@@ -1338,8 +1324,7 @@ fromList-reverse (x List.∷ xs) = begin
13381324
fromList (List.reverse (x List.∷ xs)) ≈⟨ cast-fromList (List.ʳ++-defn xs) ⟩
13391325
fromList (List.reverse xs List.++ List.[ x ]) ≈⟨ fromList-++ (List.reverse xs) ⟩
13401326
fromList (List.reverse xs) ++ [ x ] ≈⟨ unfold-∷ʳ-eqFree x (fromList (List.reverse xs)) ⟨
1341-
fromList (List.reverse xs) ∷ʳ x ≈⟨ ≈-cong (_∷ʳ x) (cast-∷ʳ (cong suc (List.length-reverse xs)) _ _)
1342-
(fromList-reverse xs) ⟩
1327+
fromList (List.reverse xs) ∷ʳ x ≈⟨ ≈-cong′ (_∷ʳ x) (fromList-reverse xs) ⟩
13431328
reverse (fromList xs) ∷ʳ x ≂⟨ reverse-∷ x (fromList xs) ⟨
13441329
reverse (x ∷ fromList xs) ≈⟨⟩
13451330
reverse (fromList (x List.∷ xs)) ∎

src/Data/Vec/Relation/Binary/Equality/Cast.agda

+16-5
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
{-# OPTIONS --cubical-compatible --safe #-}
1212

13-
module Data.Vec.Relation.Binary.Equality.Cast {a} {A : Set a} where
13+
module Data.Vec.Relation.Binary.Equality.Cast where
1414

15+
open import Level using (Level)
16+
open import Function.Base using (_∘_)
1517
open import Data.Nat.Base using (ℕ; zero; suc)
1618
open import Data.Nat.Properties using (suc-injective)
1719
open import Data.Vec.Base
@@ -24,6 +26,8 @@ open import Relation.Binary.PropositionalEquality.Properties
2426

2527
private
2628
variable
29+
a b : Level
30+
A B : Set a
2731
l m n o :
2832
xs ys zs : Vec A n
2933

@@ -41,31 +45,38 @@ cast-trans {m = suc _} {n = suc _} {o = suc _} eq₁ eq₂ (x ∷ xs) =
4145

4246
infix 3 _≈[_]_
4347

44-
_≈[_]_ : {n m} Vec A n .(eq : n ≡ m) Vec A m Set a
48+
_≈[_]_ : {n m} Vec A n .(eq : n ≡ m) Vec A m Set _
4549
xs ≈[ eq ] ys = cast eq xs ≡ ys
4650

4751
------------------------------------------------------------------------
4852
-- _≈[_]_ is ‘reflexive’, ‘symmetric’ and ‘transitive’
4953

50-
≈-reflexive : {n} _≡_ ⇒ (λ xs ys _≈[_]_ {n} xs refl ys)
54+
≈-reflexive : {n} _≡_ ⇒ (λ xs ys _≈[_]_ {A = A} {n} xs refl ys)
5155
≈-reflexive {x = x} eq = trans (cast-is-id refl x) eq
5256

53-
≈-sym : .{m≡n : m ≡ n} Sym _≈[ m≡n ]_ _≈[ sym m≡n ]_
57+
≈-sym : .{m≡n : m ≡ n} Sym {A = Vec A m} _≈[ m≡n ]_ _≈[ sym m≡n ]_
5458
≈-sym {m≡n = m≡n} {xs} {ys} xs≈ys = begin
5559
cast (sym m≡n) ys ≡⟨ cong (cast (sym m≡n)) xs≈ys ⟨
5660
cast (sym m≡n) (cast m≡n xs) ≡⟨ cast-trans m≡n (sym m≡n) xs ⟩
5761
cast (trans m≡n (sym m≡n)) xs ≡⟨ cast-is-id (trans m≡n (sym m≡n)) xs ⟩
5862
xs ∎
5963
where open ≡-Reasoning
6064

61-
≈-trans : .{m≡n : m ≡ n} .{n≡o : n ≡ o} Trans _≈[ m≡n ]_ _≈[ n≡o ]_ _≈[ trans m≡n n≡o ]_
65+
≈-trans : .{m≡n : m ≡ n} .{n≡o : n ≡ o}
66+
Trans {A = Vec A m} _≈[ m≡n ]_ _≈[ n≡o ]_ _≈[ trans m≡n n≡o ]_
6267
≈-trans {m≡n = m≡n} {n≡o} {xs} {ys} {zs} xs≈ys ys≈zs = begin
6368
cast (trans m≡n n≡o) xs ≡⟨ cast-trans m≡n n≡o xs ⟨
6469
cast n≡o (cast m≡n xs) ≡⟨ cong (cast n≡o) xs≈ys ⟩
6570
cast n≡o ys ≡⟨ ys≈zs ⟩
6671
zs ∎
6772
where open ≡-Reasoning
6873

74+
≈-cong′ : {f-len : ℕ} (f : {n} Vec A n Vec B (f-len n))
75+
{m n} {xs : Vec A m} {ys : Vec A n} .{eq} xs ≈[ eq ] ys
76+
f xs ≈[ cong f-len eq ] f ys
77+
≈-cong′ f {m = zero} {n = zero} {xs = []} {ys = []} refl = cast-is-id refl (f [])
78+
≈-cong′ f {m = suc m} {n = suc n} {xs = x ∷ xs} {ys = y ∷ ys} refl = ≈-cong′ (f ∘ (x ∷_)) refl
79+
6980
------------------------------------------------------------------------
7081
-- Reasoning combinators
7182

0 commit comments

Comments
 (0)