Skip to content

[Add] truncate properties to Data.Vec.Properties - #2795

Merged
JacquesCarette merged 8 commits into
agda:masterfrom
e-mniang:TruncateProperties
Oct 10, 2025
Merged

[Add] truncate properties to Data.Vec.Properties#2795
JacquesCarette merged 8 commits into
agda:masterfrom
e-mniang:TruncateProperties

Conversation

@e-mniang

@e-mniang e-mniang commented Jul 30, 2025

Copy link
Copy Markdown
Contributor

As suggested by @jamesmckinna, this PR adds several equational properties for truncate to Data.Vec.Properties, extending the standard vector operations toolkit with useful lemmas for reasoning about truncate.

Added properties:

- take-updateAt : applying updateAt before or after take yields the same result on valid indices

- truncate-zipWith : truncate commutes with zipWith on vectors of equal length

- truncate-zipWith-truncate : generalizes truncate-zipWith when one vector is already truncated

- zipWith-truncate : zipWith followed by truncate equals truncating the inputs first, when lengths match

- zipWith-truncate₁ : a more general version of zipWith-truncate with shifted truncation offsets

- updateAt-truncate : rewriting updateAt after truncate via take equivalence

- truncate++drop≡id : a vector can be recovered by appending its truncated prefix and dropped suffix

- truncate-map : truncate commutes with map

@e-mniang
e-mniang marked this pull request as ready for review July 30, 2025 18:52
@MatthewDaggitt MatthewDaggitt added this to the v2.4 milestone Jul 31, 2025
@jamesmckinna

jamesmckinna commented Jul 31, 2025

Copy link
Copy Markdown
Collaborator

@MatthewDaggitt are you happy to proceed with this PR, and its predecessor #2769 for v2.4, and only consider #2770 / #2787 downstream, or should we move to reconcile them all together?

UPDATED: I'll review once the direction of travel is (a bit) clearer...

@MatthewDaggitt

Copy link
Copy Markdown
Collaborator

Yes, I'm happy to add these now and then the breaking changes in v3.0.

@jamesmckinna jamesmckinna left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lots of things to fix, hopefully all (most?) of which will simplify what's here.

Comment thread CHANGELOG.md Outdated
Comment thread CHANGELOG.md Outdated
Comment on lines +51 to +52
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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto. re: alignment

Comment thread CHANGELOG.md Outdated
Comment thread CHANGELOG.md Outdated
Comment on lines +57 to +58
zipWith-truncate : zipWith f (truncate p≤p+q xs) (truncate p≤p+q ys) ≡
truncate p≤p+q (zipWith f xs ys)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why you've changed to p and q here, rather than m and n?
Also, we 'know' that p ≤ p + q, so the (presumably) mnemonic name must be carrying other information? If not, simply use m≤n?

Comment thread CHANGELOG.md Outdated
Comment on lines +60 to +61
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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly here, the names are getting very unwieldy... I'll look at the code to see what might be going on...

Comment thread src/Data/Vec/Properties.agda Outdated
truncate++drop≡id {m = m} {n} xs = begin
truncate (m≤m+n m n) xs ++ drop m xs ≡⟨ cong (_++ drop m xs) (take≡truncate m xs) ⟨
take m xs ++ drop m xs ≡⟨ take++drop≡id m xs ⟩
xs ∎ where open ≡-Reasoning

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

Comment thread src/Data/Vec/Properties.agda Outdated
toList-map f [] = refl
toList-map f (x ∷ xs) = cong (f x List.∷_) (toList-map f xs)

truncate-map : (f : A → B) (m : ℕ) (m≤n : m ≤ n) (xs : Vec A n) →

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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) →

Comment thread src/Data/Vec/Properties.agda Outdated

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)
truncate-map {n = n} f m m≤n xs =

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... with corresponding

Suggested change
truncate-map {n = n} f m m≤n xs =
truncate-map {m = m} {n = n} f m≤n xs =

Comment thread src/Data/Vec/Properties.agda Outdated
take m (cast eq (map f xs)) ≡⟨ truncate≡take m≤n (map f xs) eq ⟨
truncate m≤n (map f xs) ∎
where
.eq : n ≡ m + (n ∸ m)

Copy link
Copy Markdown
Collaborator

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.

Suggested change
.eq : n ≡ m + (n ∸ m)
eq : n ≡ m + (n ∸ m)

Comment thread src/Data/Vec/Properties.agda Outdated
truncate m≤n (map f xs) ∎
where
.eq : n ≡ m + (n ∸ m)
eq = sym (proj₂ (m≤n⇒∃[o]m+o≡n m≤n))

Copy link
Copy Markdown
Collaborator

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?

Copy link
Copy Markdown
Collaborator

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.

@jamesmckinna

Copy link
Copy Markdown
Collaborator

Thanks for the second contribution!
Once the backports #2799 from v2.3 have been merged into master, it will make sense to fix up the CHANGELOG conflicts, merge master into this branch, and then look at the suggested fixes.

@e-mniang

e-mniang commented Aug 6, 2025

Copy link
Copy Markdown
Contributor Author

Hi!
Thanks for your review. I made some changes based on what you said and deleted most of the things that could conflict with the Fairbairn threshold.
Let me know if anything else needs corrections.

Comment thread CHANGELOG.md Outdated

@jamesmckinna jamesmckinna left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the last nitpicks, but otherwise all looks good!
(But: you'll need to fix the CHANGELOG again... happens a lot at the beginning of a new cycle of vx.y developemnt :-()

Comment thread src/Data/Vec/Properties.agda Outdated

@jamesmckinna jamesmckinna left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All looks good!
Thanks very much for this final round of nitpicks.

@JacquesCarette
JacquesCarette added this pull request to the merge queue Oct 10, 2025
Merged via the queue into agda:master with commit 2ce5eb3 Oct 10, 2025
12 checks passed
github-merge-queue Bot pushed a commit that referenced this pull request Jan 24, 2026
…ht}` irrelevant (#2787)

* refactor: make `truncate` and `padRight` take irrelevant argument

* fix: proofs of properties following #2769 and #2795; deprecate `truncate-irrelevant`

* fix: deprecation in `CHANGELOG`

* fix: duplication after resolving merge conflict

* fix: alignment

* add: specialised versions of `padRight-drop` and `padRight-take`

* fix: whitespace

* reset: `CHANGELOG`

* restore: new `CHANGELOG` entries

* refactor: weaken types of `truncate` properties

* fix:`CHANGELOG` to reflect weakened types

* fix: whitespace

* final tweak

* final tweak
plt-amy pushed a commit that referenced this pull request Feb 9, 2026
* adding truncate's properties

* update changelog

* cleaning whitespaces

* Recleaning

* corrections after reviews

* correction of changelog

* changes after review
plt-amy pushed a commit that referenced this pull request Feb 9, 2026
…ht}` irrelevant (#2787)

* refactor: make `truncate` and `padRight` take irrelevant argument

* fix: proofs of properties following #2769 and #2795; deprecate `truncate-irrelevant`

* fix: deprecation in `CHANGELOG`

* fix: duplication after resolving merge conflict

* fix: alignment

* add: specialised versions of `padRight-drop` and `padRight-take`

* fix: whitespace

* reset: `CHANGELOG`

* restore: new `CHANGELOG` entries

* refactor: weaken types of `truncate` properties

* fix:`CHANGELOG` to reflect weakened types

* fix: whitespace

* final tweak

* final tweak
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants