Skip to content

Commit 9a859b9

Browse files
Data.Vec.Properties: Add take/drop proofs, zipWith-replicate (#1316)
1 parent 6803957 commit 9a859b9

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,13 @@ Other minor additions
8080
```
8181

8282
* Add version to library name
83+
84+
* Add new properties to `Data.Vec.Properties`:
85+
```agda
86+
take-distr-zipWith : take m (zipWith f u v) ≡ zipWith f (take m u) (take m v)
87+
take-distr-map : take m (map f v) ≡ map f (take m v)
88+
drop-distr-zipWith : drop m (zipWith f u v) ≡ zipWith f (drop m u) (drop m v)
89+
drop-distr-map : drop m (map f v) ≡ map f (drop m v)
90+
take-drop-id : take m v ++ drop m v ≡ v
91+
zipWith-replicate : zipWith {n = n} _⊕_ (replicate x) (replicate y) ≡ replicate (x ⊕ y)
92+
```

src/Data/Vec/Properties.agda

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ open import Level using (Level)
2626
open import Relation.Binary as B hiding (Decidable)
2727
open import Relation.Binary.PropositionalEquality as P
2828
using (_≡_; _≢_; refl; _≗_; cong₂)
29+
open P.≡-Reasoning
2930
open import Relation.Unary using (Pred; Decidable)
3031
open import Relation.Nullary using (Dec; does; yes; no)
3132
open import Relation.Nullary.Decidable using (map′)
@@ -76,13 +77,84 @@ unfold-take : ∀ n {m} x (xs : Vec A (n + m)) → take (suc n) (x ∷ xs) ≡ x
7677
unfold-take n x xs with splitAt n xs
7778
unfold-take n x .(xs ++ ys) | xs , ys , refl = refl
7879

80+
take-distr-zipWith : {m n} (f : A B C)
81+
(xs : Vec A (m + n)) (ys : Vec B (m + n))
82+
take m (zipWith f xs ys) ≡ zipWith f (take m xs) (take m ys)
83+
take-distr-zipWith {m = zero} f xs ys = refl
84+
take-distr-zipWith {m = suc m} f (x ∷ xs) (y ∷ ys) = begin
85+
take (suc m) (zipWith f (x ∷ xs) (y ∷ ys))
86+
≡⟨⟩
87+
take (suc m) (f x y ∷ (zipWith f xs ys))
88+
≡⟨ unfold-take m (f x y) (zipWith f xs ys) ⟩
89+
f x y ∷ take m (zipWith f xs ys)
90+
≡⟨ P.cong (f x y ∷_) (take-distr-zipWith f xs ys) ⟩
91+
f x y ∷ (zipWith f (take m xs) (take m ys))
92+
≡⟨⟩
93+
zipWith f (x ∷ (take m xs)) (y ∷ (take m ys))
94+
≡˘⟨ P.cong₂ (zipWith f) (unfold-take m x xs) (unfold-take m y ys) ⟩
95+
zipWith f (take (suc m) (x ∷ xs)) (take (suc m) (y ∷ ys))
96+
97+
98+
take-distr-map : {n} (f : A B) (m : ℕ) (xs : Vec A (m + n))
99+
take m (map f xs) ≡ map f (take m xs)
100+
take-distr-map f zero xs = refl
101+
take-distr-map f (suc m) (x ∷ xs) =
102+
begin
103+
take (suc m) (map f (x ∷ xs)) ≡⟨⟩
104+
take (suc m) (f x ∷ map f xs) ≡⟨ unfold-take m (f x) (map f xs) ⟩
105+
f x ∷ (take m (map f xs)) ≡⟨ P.cong (f x ∷_) (take-distr-map f m xs) ⟩
106+
f x ∷ (map f (take m xs)) ≡⟨⟩
107+
map f (x ∷ take m xs) ≡˘⟨ P.cong (map f) (unfold-take m x xs) ⟩
108+
map f (take (suc m) (x ∷ xs)) ∎
109+
79110
------------------------------------------------------------------------
80111
-- drop
81112

82113
unfold-drop : n {m} x (xs : Vec A (n + m)) drop (suc n) (x ∷ xs) ≡ drop n xs
83114
unfold-drop n x xs with splitAt n xs
84115
unfold-drop n x .(xs ++ ys) | xs , ys , refl = refl
85116

117+
drop-distr-zipWith : {m n} (f : A B C)
118+
(x : Vec A (m + n)) (y : Vec B (m + n))
119+
drop m (zipWith f x y) ≡ zipWith f (drop m x) (drop m y)
120+
drop-distr-zipWith {m = zero} f xs ys = refl
121+
drop-distr-zipWith {m = suc m} f (x ∷ xs) (y ∷ ys) = begin
122+
drop (suc m) (zipWith f (x ∷ xs) (y ∷ ys))
123+
≡⟨⟩
124+
drop (suc m) (f x y ∷ (zipWith f xs ys))
125+
≡⟨ unfold-drop m (f x y) (zipWith f xs ys) ⟩
126+
drop m (zipWith f xs ys)
127+
≡⟨ drop-distr-zipWith f xs ys ⟩
128+
zipWith f (drop m xs) (drop m ys)
129+
≡˘⟨ P.cong₂ (zipWith f) (unfold-drop m x xs) (unfold-drop m y ys) ⟩
130+
zipWith f (drop (suc m) (x ∷ xs)) (drop (suc m) (y ∷ ys))
131+
132+
133+
drop-distr-map : {n} (f : A B) (m : ℕ) (x : Vec A (m + n))
134+
drop m (map f x) ≡ map f (drop m x)
135+
drop-distr-map f zero x = refl
136+
drop-distr-map f (suc m) (x ∷ xs) = begin
137+
drop (suc m) (map f (x ∷ xs)) ≡⟨⟩
138+
drop (suc m) (f x ∷ map f xs) ≡⟨ unfold-drop m (f x) (map f xs) ⟩
139+
drop m (map f xs) ≡⟨ drop-distr-map f m xs ⟩
140+
map f (drop m xs) ≡⟨ P.cong (map f) (P.sym (unfold-drop m x xs)) ⟩
141+
map f (drop (suc m) (x ∷ xs)) ∎
142+
143+
------------------------------------------------------------------------
144+
-- take and drop together
145+
146+
take-drop-id : {n} (m : ℕ) (x : Vec A (m + n)) take m x ++ drop m x ≡ x
147+
take-drop-id zero x = refl
148+
take-drop-id (suc m) (x ∷ xs) = begin
149+
take (suc m) (x ∷ xs) ++ drop (suc m) (x ∷ xs)
150+
≡⟨ cong₂ _++_ (unfold-take m x xs) (unfold-drop m x xs) ⟩
151+
(x ∷ take m xs) ++ (drop m xs)
152+
≡⟨⟩
153+
x ∷ (take m xs ++ drop m xs)
154+
≡⟨ P.cong (x ∷_) (take-drop-id m xs) ⟩
155+
x ∷ xs
156+
157+
86158
------------------------------------------------------------------------
87159
-- lookup
88160

@@ -623,14 +695,19 @@ map-replicate : ∀ (f : A → B) (x : A) n →
623695
map-replicate f x zero = refl
624696
map-replicate f x (suc n) = P.cong (f x ∷_) (map-replicate f x n)
625697

698+
zipWith-replicate : {n : ℕ} (_⊕_ : A B C) (x : A) (y : B)
699+
zipWith {n = n} _⊕_ (replicate x) (replicate y) ≡ replicate (x ⊕ y)
700+
zipWith-replicate {n = zero} _⊕_ x y = refl
701+
zipWith-replicate {n = suc n} _⊕_ x y = P.cong (x ⊕ y ∷_) (zipWith-replicate _⊕_ x y)
702+
626703
zipWith-replicate₁ : {n} (_⊕_ : A B C) (x : A) (ys : Vec B n)
627-
zipWith _⊕_ (replicate x) ys ≡ map (x ⊕_) ys
704+
zipWith _⊕_ (replicate x) ys ≡ map (x ⊕_) ys
628705
zipWith-replicate₁ _⊕_ x [] = refl
629706
zipWith-replicate₁ _⊕_ x (y ∷ ys) =
630707
P.cong (x ⊕ y ∷_) (zipWith-replicate₁ _⊕_ x ys)
631708

632709
zipWith-replicate₂ : {n} (_⊕_ : A B C) (xs : Vec A n) (y : B)
633-
zipWith _⊕_ xs (replicate y) ≡ map (_⊕ y) xs
710+
zipWith _⊕_ xs (replicate y) ≡ map (_⊕ y) xs
634711
zipWith-replicate₂ _⊕_ [] y = refl
635712
zipWith-replicate₂ _⊕_ (x ∷ xs) y =
636713
P.cong (x ⊕ y ∷_) (zipWith-replicate₂ _⊕_ xs y)

0 commit comments

Comments
 (0)