Skip to content

Commit 2ce5eb3

Browse files
authored
[Add] truncate properties to Data.Vec.Properties (#2795)
* adding truncate's properties * update changelog * cleaning whitespaces * Recleaning * corrections after reviews * correction of changelog * changes after review
1 parent e00e780 commit 2ce5eb3

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,27 @@ Additions to existing modules
134134

135135
* In `Data.Vec.Properties`:
136136
```agda
137+
updateAt-take : (xs : Vec A (m + n)) (i : Fin m) (f : A → A) →
138+
updateAt (take m xs) i f ≡ take m (updateAt xs (inject≤ i (m≤m+n m n)) f)
139+
140+
truncate-zipWith : (f : A → B → C) (m≤n : m ≤ n) (xs : Vec A n) (ys : Vec B n) →
141+
truncate m≤n (zipWith f xs ys) ≡ zipWith f (truncate m≤n xs) (truncate m≤n ys)
142+
143+
truncate-zipWith-truncate : (f : A → B → C) (m≤n : m ≤ n) (n≤o : n ≤ o) (xs : Vec A o) (ys : Vec B n) →
144+
truncate m≤n (zipWith f (truncate n≤o xs) ys) ≡
145+
zipWith f (truncate (≤-trans m≤n n≤o) xs) (truncate m≤n ys)
146+
147+
truncate-updateAt : (m≤n : m ≤ n) (xs : Vec A n) (i : Fin m) (f : A → A) →
148+
updateAt (truncate m≤n xs) i f ≡
149+
truncate m≤n (updateAt xs (inject≤ i m≤n) f)
150+
151+
updateAt-truncate : (xs : Vec A (m + n)) (i : Fin m) (f : A → A) →
152+
updateAt (truncate (m≤m+n m n) xs) i f ≡
153+
truncate (m≤m+n m n) (updateAt xs (inject≤ i (m≤m+n m n)) f)
154+
155+
map-truncate : (f : A → B) (m≤n : m ≤ n) (xs : Vec A n) →
156+
map f (truncate m≤n xs) ≡ truncate m≤n (map f xs)
157+
137158
padRight-lookup : (m≤n : m ≤ n) (a : A) (xs : Vec A m) (i : Fin m) → lookup (padRight m≤n a xs) (inject≤ i m≤n) ≡ lookup xs i
138159
139160
padRight-map : (f : A → B) (m≤n : m ≤ n) (a : A) (xs : Vec A m) → map f (padRight m≤n a xs) ≡ padRight m≤n (f a) (map f xs)

src/Data/Vec/Properties.agda

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ open import Data.Fin.Base as Fin
1515
open import Data.List.Base as List using (List)
1616
import Data.List.Properties as List
1717
open import Data.Nat.Base
18-
using (ℕ; zero; suc; _+_; _≤_; _<_; s≤s; pred; s<s⁻¹; _≥_; s≤s⁻¹; z≤n)
18+
using (ℕ; zero; suc; _+_; _≤_; _<_; s≤s; pred; s<s⁻¹; _≥_; s≤s⁻¹; z≤n; _∸_)
1919
open import Data.Nat.Properties
2020
using (+-assoc; m≤n⇒m≤1+n; m≤m+n; ≤-refl; ≤-trans; ≤-irrelevant; ≤⇒≤″
21-
; suc-injective; +-comm; +-suc; +-identityʳ)
21+
; suc-injective; +-comm; +-suc; +-identityʳ; m≤n⇒∃[o]m+o≡n)
2222
open import Data.Product.Base as Product
2323
using (_×_; _,_; proj₁; proj₂; <_,_>; uncurry)
2424
open import Data.Sum.Base using ([_,_]′)
@@ -104,6 +104,11 @@ take-map : ∀ (f : A → B) (m : ℕ) (xs : Vec A (m + n)) →
104104
take-map f zero xs = refl
105105
take-map f (suc m) (x ∷ xs) = cong (f x ∷_) (take-map f m xs)
106106

107+
updateAt-take : (xs : Vec A (m + n)) (i : Fin m) (f : A A)
108+
updateAt (take m xs) i f ≡ take m (updateAt xs (inject≤ i (m≤m+n m n)) f)
109+
updateAt-take (_ ∷ _ ) zero f = refl
110+
updateAt-take (x ∷ xs) (suc i) f = cong (x ∷_) (updateAt-take xs i f)
111+
107112
------------------------------------------------------------------------
108113
-- drop
109114

@@ -154,6 +159,37 @@ take≡truncate : ∀ m (xs : Vec A (m + n)) →
154159
take≡truncate zero _ = refl
155160
take≡truncate (suc m) (x ∷ xs) = cong (x ∷_) (take≡truncate m xs)
156161

162+
truncate-zipWith : (f : A B C) (m≤n : m ≤ n) (xs : Vec A n) (ys : Vec B n)
163+
truncate m≤n (zipWith f xs ys) ≡ zipWith f (truncate m≤n xs) (truncate m≤n ys)
164+
truncate-zipWith f z≤n xs ys = refl
165+
truncate-zipWith f (s≤s m≤n) (x ∷ xs) (y ∷ ys) =
166+
cong (f x y ∷_) (truncate-zipWith f m≤n xs ys)
167+
168+
truncate-zipWith-truncate : (f : A B C) (m≤n : m ≤ n) (n≤o : n ≤ o)
169+
(xs : Vec A o) (ys : Vec B n)
170+
truncate m≤n (zipWith f (truncate n≤o xs) ys) ≡
171+
zipWith f (truncate (≤-trans m≤n n≤o) xs) (truncate m≤n ys)
172+
truncate-zipWith-truncate f m≤n n≤o xs ys =
173+
trans (truncate-zipWith f m≤n (truncate n≤o xs) ys)
174+
(cong (λ zs zipWith f zs (truncate m≤n ys)) ((sym (truncate-trans m≤n n≤o xs))))
175+
176+
truncate-updateAt : (m≤n : m ≤ n) (xs : Vec A n) (i : Fin m) (f : A A)
177+
updateAt (truncate m≤n xs) i f ≡ truncate m≤n (updateAt xs (inject≤ i m≤n) f)
178+
truncate-updateAt (s≤s _ ) (_ ∷ _ ) zero f = refl
179+
truncate-updateAt (s≤s m≤n) (x ∷ xs) (suc i) f = cong (x ∷_) (truncate-updateAt m≤n xs i f)
180+
181+
module _ (xs : Vec A (m + n)) (i : Fin m) (f : A A) where
182+
private
183+
i′ = inject≤ i (m≤m+n m n)
184+
185+
updateAt-truncate : updateAt (truncate (m≤m+n m n) xs) i f ≡ truncate (m≤m+n m n) (updateAt xs i′ f)
186+
updateAt-truncate = begin
187+
updateAt (truncate (m≤m+n m n) xs) i f ≡⟨ cong (λ l updateAt l i f) (take≡truncate m xs) ⟨
188+
updateAt (take m xs) i f ≡⟨ updateAt-take xs i f ⟩
189+
take m (updateAt xs i′ f) ≡⟨ take≡truncate m (updateAt xs i′ f) ⟩
190+
truncate (m≤m+n m n) (updateAt xs i′ f) ∎
191+
where open ≡-Reasoning
192+
157193
------------------------------------------------------------------------
158194
-- truncate and padRight together
159195

@@ -456,6 +492,19 @@ toList-map : ∀ (f : A → B) (xs : Vec A n) →
456492
toList-map f [] = refl
457493
toList-map f (x ∷ xs) = cong (f x List.∷_) (toList-map f xs)
458494

495+
map-truncate : (f : A B) (m≤n : m ≤ n) (xs : Vec A n)
496+
map f (truncate m≤n xs) ≡ truncate m≤n (map f xs)
497+
map-truncate {m = m} {n = n} f m≤n xs =
498+
let _ , n≡m+o = m≤n⇒∃[o]m+o≡n m≤n
499+
in let m+o≡n = sym n≡m+o
500+
in begin
501+
map f (truncate m≤n xs) ≡⟨ cong (map f) (truncate≡take m≤n xs m+o≡n) ⟩
502+
map f (take m (cast m+o≡n xs)) ≡⟨ take-map f m _ ⟨
503+
take m (map f (cast m+o≡n xs)) ≡⟨ cong (take m) (map-cast f m+o≡n xs) ⟩
504+
take m (cast m+o≡n (map f xs)) ≡⟨ truncate≡take m≤n (map f xs) m+o≡n ⟨
505+
truncate m≤n (map f xs) ∎
506+
where open ≡-Reasoning
507+
459508
------------------------------------------------------------------------
460509
-- _++_
461510

0 commit comments

Comments
 (0)