@@ -26,6 +26,7 @@ open import Level using (Level)
26
26
open import Relation.Binary as B hiding (Decidable)
27
27
open import Relation.Binary.PropositionalEquality as P
28
28
using (_≡_; _≢_; refl; _≗_; cong₂)
29
+ open P.≡-Reasoning
29
30
open import Relation.Unary using (Pred; Decidable)
30
31
open import Relation.Nullary using (Dec; does; yes; no)
31
32
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
76
77
unfold-take n x xs with splitAt n xs
77
78
unfold-take n x .(xs ++ ys) | xs , ys , refl = refl
78
79
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
+
79
110
------------------------------------------------------------------------
80
111
-- drop
81
112
82
113
unfold-drop : ∀ n {m} x (xs : Vec A (n + m)) → drop (suc n) (x ∷ xs) ≡ drop n xs
83
114
unfold-drop n x xs with splitAt n xs
84
115
unfold-drop n x .(xs ++ ys) | xs , ys , refl = refl
85
116
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
+
86
158
------------------------------------------------------------------------
87
159
-- lookup
88
160
@@ -623,14 +695,19 @@ map-replicate : ∀ (f : A → B) (x : A) n →
623
695
map-replicate f x zero = refl
624
696
map-replicate f x (suc n) = P.cong (f x ∷_) (map-replicate f x n)
625
697
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
+
626
703
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
628
705
zipWith-replicate₁ _⊕_ x [] = refl
629
706
zipWith-replicate₁ _⊕_ x (y ∷ ys) =
630
707
P.cong (x ⊕ y ∷_) (zipWith-replicate₁ _⊕_ x ys)
631
708
632
709
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
634
711
zipWith-replicate₂ _⊕_ [] y = refl
635
712
zipWith-replicate₂ _⊕_ (x ∷ xs) y =
636
713
P.cong (x ⊕ y ∷_) (zipWith-replicate₂ _⊕_ xs y)
0 commit comments