-
Notifications
You must be signed in to change notification settings - Fork 257
[Add] truncate properties to Data.Vec.Properties #2795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 4 commits
e23bb4b
ead92ab
d2e46d8
fd1d7f2
ac32928
56b9f0b
70b85de
f05746c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,3 +43,31 @@ Additions to existing modules | |
∸-suc : m ≤ n → suc n ∸ m ≡ suc (n ∸ m) | ||
``` | ||
|
||
* In `Data.Vec.Properties`: | ||
```agda | ||
take-updateAt : (f : A → A) {m n : ℕ} (xs : Vec A (m + n)) (i : Fin m) → | ||
updateAt (take m xs) i f ≡ take m (updateAt xs (inject≤ i (m≤m+n m n)) f) | ||
|
||
truncate-zipWith : (f : A → B → C) (m≤n : m ≤ n) (xs : Vec A n) (ys : Vec B n) → | ||
truncate m≤n (zipWith f xs ys) ≡ zipWith f (truncate m≤n xs) (truncate m≤n ys) | ||
|
||
|
||
truncate-zipWith-truncate : truncate o≤m (zipWith f (truncate m≤n xs) ys) ≡ | ||
zipWith f (truncate o≤n xs) (truncate o≤m ys) | ||
jamesmckinna marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
zipWith-truncate : zipWith f (truncate p≤p+q xs) (truncate p≤p+q ys) ≡ | ||
truncate p≤p+q (zipWith f xs ys) | ||
|
||
|
||
zipWith-truncate₁ : zipWith f (truncate o≤o+m+n xs) (truncate (o≤o+m) ys) ≡ | ||
truncate (o≤o+m) (zipWith f (truncate (o+m≤o+m+n) xs) ys) | ||
|
||
|
||
truncate-updateAt : (f : A → A) (m≤n : m ≤ n) (xs : Vec A n) (i : Fin m) → | ||
updateAt (truncate m≤n xs) i f ≡ truncate m≤n (updateAt xs (inject≤ i m≤n) f) | ||
|
||
|
||
updateAt-truncate : updateAt (truncate p≤p+q xs) i f ≡ truncate p≤p+q (updateAt xs i′ f) | ||
|
||
|
||
truncate++drop≡id : (xs : Vec A (m + n)) → truncate (m≤m+n m n) xs ++ drop m xs ≡ xs | ||
|
||
|
||
truncate-map : (f : A → B) (m : ℕ) (m≤n : m ≤ n) (xs : Vec A n) → | ||
map f (truncate m≤n xs) ≡ truncate m≤n (map f xs) | ||
|
||
|
||
``` |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -11,14 +11,14 @@ module Data.Vec.Properties where | |||||||||||||||||||||||||||||||||
open import Algebra.Definitions | ||||||||||||||||||||||||||||||||||
open import Data.Bool.Base using (true; false) | ||||||||||||||||||||||||||||||||||
open import Data.Fin.Base as Fin | ||||||||||||||||||||||||||||||||||
using (Fin; zero; suc; toℕ; fromℕ<; _↑ˡ_; _↑ʳ_) | ||||||||||||||||||||||||||||||||||
using (Fin; zero; suc; toℕ; fromℕ<; _↑ˡ_; _↑ʳ_; inject≤) | ||||||||||||||||||||||||||||||||||
open import Data.List.Base as List using (List) | ||||||||||||||||||||||||||||||||||
import Data.List.Properties as List | ||||||||||||||||||||||||||||||||||
open import Data.Nat.Base | ||||||||||||||||||||||||||||||||||
using (ℕ; zero; suc; _+_; _≤_; _<_; s≤s; pred; s<s⁻¹; _≥_; s≤s⁻¹; z≤n) | ||||||||||||||||||||||||||||||||||
using (ℕ; zero; suc; _+_; _≤_; _<_; s≤s; pred; s<s⁻¹; _≥_; s≤s⁻¹; z≤n; _∸_) | ||||||||||||||||||||||||||||||||||
open import Data.Nat.Properties | ||||||||||||||||||||||||||||||||||
using (+-assoc; m≤n⇒m≤1+n; m≤m+n; ≤-refl; ≤-trans; ≤-irrelevant; ≤⇒≤″ | ||||||||||||||||||||||||||||||||||
; suc-injective; +-comm; +-suc; +-identityʳ) | ||||||||||||||||||||||||||||||||||
; suc-injective; +-comm; +-suc; +-identityʳ; m≤n⇒∃[o]m+o≡n) | ||||||||||||||||||||||||||||||||||
open import Data.Product.Base as Product | ||||||||||||||||||||||||||||||||||
using (_×_; _,_; proj₁; proj₂; <_,_>; uncurry) | ||||||||||||||||||||||||||||||||||
open import Data.Sum.Base using ([_,_]′) | ||||||||||||||||||||||||||||||||||
|
@@ -104,6 +104,11 @@ take-map : ∀ (f : A → B) (m : ℕ) (xs : Vec A (m + n)) → | |||||||||||||||||||||||||||||||||
take-map f zero xs = refl | ||||||||||||||||||||||||||||||||||
take-map f (suc m) (x ∷ xs) = cong (f x ∷_) (take-map f m xs) | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
take-updateAt : (f : A → A) {m n : ℕ} (xs : Vec A (m + n)) (i : Fin m) → | ||||||||||||||||||||||||||||||||||
updateAt (take m xs) i f ≡ take m (updateAt xs (inject≤ i (m≤m+n m n)) f) | ||||||||||||||||||||||||||||||||||
take-updateAt f (_ ∷ _ ) zero = refl | ||||||||||||||||||||||||||||||||||
take-updateAt f (x ∷ xs) (suc i) = cong (x ∷_) (take-updateAt f xs i) | ||||||||||||||||||||||||||||||||||
|
take-updateAt : (f : A → A) {m n : ℕ} (xs : Vec A (m + n)) (i : Fin m) → | |
updateAt (take m xs) i f ≡ take m (updateAt xs (inject≤ i (m≤m+n m n)) f) | |
take-updateAt f (_ ∷ _ ) zero = refl | |
take-updateAt f (x ∷ xs) (suc i) = cong (x ∷_) (take-updateAt f xs i) | |
take-updateAt : (xs : Vec A (m + n)) (i : Fin m) (f : A → A) → | |
updateAt (take m xs) i f ≡ take m (updateAt xs (inject≤ i (m≤m+n m n)) f) | |
take-updateAt (_ ∷ _ ) zero f = refl | |
take-updateAt (x ∷ xs) (suc i) f = cong (x ∷_) (take-updateAt xs i f) |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A repeated let
binding between type and definition is a UX/cognitive annoyance, for which the only really satisfactory solution is to use an anonymous module to share the definition... but here o≤n
doesn't appear in the definition, so can be dropped in favour of
truncate-zipWith-truncate : ∀ (f : A → B → C) (o≤m : o ≤ m) (m≤n : m ≤ n) | |
(xs : Vec A n) (ys : Vec B m) → | |
let o≤n = ≤-trans o≤m m≤n in | |
truncate o≤m (zipWith f (truncate m≤n xs) ys) ≡ | |
zipWith f (truncate o≤n xs) (truncate o≤m ys) | |
truncate-zipWith-truncate f o≤m m≤n xs ys = | |
let o≤n = ≤-trans o≤m m≤n in | |
trans (truncate-zipWith f o≤m (truncate m≤n xs) ys) | |
(cong (λ zs → zipWith f zs (truncate o≤m ys)) ((sym (truncate-trans o≤m m≤n xs)))) | |
truncate-zipWith-truncate : ∀ (f : A → B → C) (o≤m : o ≤ m) (m≤n : m ≤ n) | |
(xs : Vec A n) (ys : Vec B m) → | |
truncate o≤m (zipWith f (truncate m≤n xs) ys) ≡ | |
zipWith f (truncate (≤-trans o≤m m≤n) xs) (truncate o≤m ys) | |
truncate-zipWith-truncate f o≤m m≤n xs ys = | |
trans (truncate-zipWith f o≤m (truncate m≤n xs) ys) | |
(cong (λ zs → zipWith f zs (truncate o≤m ys)) ((sym (truncate-trans o≤m m≤n xs)))) |
plus:
- fix up alignment
- if at all possible, rename and reorder so that
m ≤ n ≤o
in terms of the (implicit) binder ordering... much less cognitive strain on the reader
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assuming this meets the Fairbairn threshold, it's still very hard to parse, but AFAICT, this is a specialised instance of truncate-zipWith
above... or am I missing something?
Suggest: deletion
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm. Not quite sure what's being gained here over truncate-zipWith-truncate
above, given that
o≤o+m+n = ≤-trans o≤o+m o+m≤o+m+n
Fairbairn threshold?
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move f
binding?
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
module _ (f : A → A) {p q : ℕ} (xs : Vec A (p + q)) (i : Fin p) where | |
module _ (xs : Vec A (m + n)) (i : Fin m) (f : A → A) where |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private | |
p≤p+q : p ≤ p + q | |
p≤p+q = m≤m+n p q | |
i′ : Fin (p + q) | |
i′ = inject≤ i p≤p+q | |
private | |
i′ = inject≤ i (m≤m+n m n) |
ie. inline the whole lot!
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid explicit sym
updateAt (truncate p≤p+q xs) i f ≡⟨ cong (λ l → updateAt l i f) (sym (take≡truncate p xs)) ⟩ | |
updateAt (truncate p≤p+q xs) i f ≡⟨ cong (λ l → updateAt l i f) (take≡truncate p xs) ⟨ |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
truncate p≤p+q (updateAt xs i′ f) ∎ where open ≡-Reasoning | |
truncate p≤p+q (updateAt xs i′ f) ∎ | |
where open ≡-Reasoning |
See style-guide
for layout conventions wrt equational proofs.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
truncate-map : (f : A → B) (m : ℕ) (m≤n : m ≤ n) (xs : Vec A n) → | |
truncate-map : (f : A → B) (m≤n : m ≤ n) (xs : Vec A n) → |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... with corresponding
truncate-map {n = n} f m m≤n xs = | |
truncate-map {m = m} {n = n} f m≤n xs = |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to mark eq
as irrelevant; it's the proof which is ignored by cast
etc.
.eq : n ≡ m + (n ∸ m) | |
eq : n ≡ m + (n ∸ m) |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact, Data.Nat.Properties.guarded-∸≗∸
is (more like) what you need here... and could then be inlined?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See also padRight-drop′
and padRight-take′
that I've added to #2787 for comparison.
Uh oh!
There was an error while loading. Please reload this page.