Skip to content

Sync insert, remove, and update functionalities for List and Vec #2049

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

Merged
merged 28 commits into from
Oct 4, 2023
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
f732e32
Sync insert, delete, and update functions for List and Vec
Saransh-cpp Aug 9, 2023
f65a0e7
Update CHANGELOG
Saransh-cpp Aug 9, 2023
01cb7d2
Merge branch 'master' into insert-delete-at
Saransh-cpp Aug 12, 2023
2bf741c
Fix tests
Saransh-cpp Aug 13, 2023
3fe528b
Update the definition of insert + add insert-remove, remove-insert, t…
Saransh-cpp Aug 21, 2023
3eb4a4b
Fix tests
Saransh-cpp Aug 22, 2023
2649cbb
Merge branch 'master' of https://github.com/agda/agda-stdlib into ins…
Saransh-cpp Aug 22, 2023
0b1ae97
Update CHANGELOG + deprecations
Saransh-cpp Aug 22, 2023
a36b6b0
Quick comment for reviewers
Saransh-cpp Aug 22, 2023
2dce69c
More CHANGELOG updates
Saransh-cpp Aug 22, 2023
259aaf7
insert -> insertAt , remove -> removeAt
Saransh-cpp Sep 14, 2023
fef9019
add length-toList
Saransh-cpp Sep 14, 2023
a50b5d3
Simplify proofs
Saransh-cpp Sep 14, 2023
3b4d666
Update CHANGELOG
Saransh-cpp Sep 14, 2023
c6b576c
Resolve conflicts
Saransh-cpp Sep 14, 2023
ee6bd4d
More deprecations
Saransh-cpp Sep 15, 2023
bf756ad
Simplify proofs more
Saransh-cpp Sep 15, 2023
a2d755f
Resolve conflicts
Saransh-cpp Sep 15, 2023
ccd7ef5
Simplify imports, fix tests
Saransh-cpp Sep 15, 2023
6ce3f6c
Update CHANGELOG.md
Saransh-cpp Sep 15, 2023
90310ea
Update type of Vec.updateAt
Saransh-cpp Sep 17, 2023
28ff3fb
Merge branch 'insert-delete-at' of https://github.com/Saransh-cpp/agd…
Saransh-cpp Sep 17, 2023
9a5c12b
Resolve conflicts
Saransh-cpp Sep 17, 2023
fb7ce78
Better removeAt
Saransh-cpp Sep 19, 2023
5735cb1
Push through Vec.Functional as well
MatthewDaggitt Oct 3, 2023
6273762
Merge branch 'master' into insert-delete-at
MatthewDaggitt Oct 3, 2023
9d376e6
Fix CHANGELOG
MatthewDaggitt Oct 3, 2023
e530fef
Another CHANGELOG fix
MatthewDaggitt Oct 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 95 additions & 15 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -797,6 +797,50 @@ Non-backwards compatible changes
IO.Instances
```

### Standardisation of `insertAt`/`updateAt`/`removeAt`

* Previously, the names and argument order of index-based insertion, update and removal functions for
various types of lists and vectors were inconsistent.

* To fix this the names have all been standardised to `insertAt`/`updateAt`/`removeAt`.

* Correspondingly the following changes have occurred:

* In `Data.List.Base` the following have been added:
```agda
insertAt : (xs : List A) → Fin (suc (length xs)) → A → List A
updateAt : (xs : List A) → Fin (length xs) → (A → A) → List A
removeAt : (xs : List A) → Fin (length xs) → List A
```
and the following has been deprecated
```
_─_ ↦ removeAt
```

* In `Data.Vec.Base`:
```agda
insert ↦ insertAt
remove ↦ removeAt

updateAt : Fin n → (A → A) → Vec A n → Vec A n
updateAt : Vec A n → Fin n → (A → A) → Vec A n
```

* In `Data.Vec.Functional`:
```agda
remove : Fin (suc n) → Vector A (suc n) → Vector A n
removeAt : Vector A (suc n) → Fin (suc n) → Vector A n

updateAt : Fin n → (A → A) → Vector A n → Vector A n
updateAt : Vector A n → Fin n → (A → A) → Vector A n
```

* The old names (and the names of all proofs about these functions) have been deprecated appropriately.


### Changes to triple reasoning interface

* The module `Relation.Binary.Reasoning.Base.Triple` now takes an extra proof that the strict
@@ -1321,6 +1365,11 @@ Deprecated names
+-isAbelianGroup ↦ +-0-isAbelianGroup
```

* In `Data.List.Base`:
```
_─_ ↦ removeAt
```

* In `Data.List.Properties`:
```agda
map-id₂ ↦ map-id-local
@@ -1337,7 +1386,10 @@ Deprecated names

ʳ++-++ ↦ ++-ʳ++

take++drop ↦ take++drop≡id
take++drop ↦ take++drop≡id

length-─ ↦ length-removeAt
map-─ ↦ map-removeAt
```

* In `Data.List.NonEmpty.Properties`:
@@ -1350,8 +1402,8 @@ Deprecated names
* In `Data.List.Relation.Unary.All.Properties`:
```agda
updateAt-id-relative ↦ updateAt-id-local
updateAt-compose-relative ↦ updateAt--local
updateAt-compose ↦ updateAt-
updateAt-compose-relative ↦ updateAt-updateAt-local
updateAt-compose ↦ updateAt-updateAt
updateAt-cong-relative ↦ updateAt-cong-local
```

@@ -1497,6 +1549,12 @@ Deprecated names
map-compose ↦ map-∘
```

* In `Data.Vec.Base`:
```
remove ↦ removeAt
insert ↦ insertAt
```

* In `Data.Vec.Properties`:
```
take-distr-zipWith ↦ take-zipWith
@@ -1505,8 +1563,8 @@ Deprecated names
drop-distr-map ↦ drop-map

updateAt-id-relative ↦ updateAt-id-local
updateAt-compose-relative ↦ updateAt--local
updateAt-compose ↦ updateAt-
updateAt-compose-relative ↦ updateAt-updateAt-local
updateAt-compose ↦ updateAt-updateAt
updateAt-cong-relative ↦ updateAt-cong-local

[]%=-compose ↦ []%=-∘
@@ -1516,7 +1574,15 @@ Deprecated names
idIsFold ↦ id-is-foldr
sum-++-commute ↦ sum-++

take-drop-id ↦ take++drop≡id
take-drop-id ↦ take++drop≡id

map-insert ↦ map-insertAt

insert-lookup ↦ insertAt-lookup
insert-punchIn ↦ insertAt-punchIn
remove-PunchOut ↦ removeAt-punchOut
remove-insert ↦ removeAt-insertAt
insert-remove ↦ insertAt-removeAt

lookup-inject≤-take ↦ lookup-take-inject≤
```
@@ -1532,11 +1598,17 @@ Deprecated names
* In `Data.Vec.Functional.Properties`:
```
updateAt-id-relative ↦ updateAt-id-local
updateAt-compose-relative ↦ updateAt--local
updateAt-compose ↦ updateAt-
updateAt-compose-relative ↦ updateAt-updateAt-local
updateAt-compose ↦ updateAt-updateAt
updateAt-cong-relative ↦ updateAt-cong-local

map-updateAt ↦ map-updateAt-local

insert-lookup ↦ insertAt-lookup
insert-punchIn ↦ insertAt-punchIn
remove-punchOut ↦ removeAt-punchOut
remove-insert ↦ removeAt-insertAt
insert-remove ↦ insertAt-removeAt
```
NB. This last one is complicated by the *addition* of a 'global' property `map-updateAt`

@@ -2312,7 +2384,7 @@ Additions to existing modules
gcd-zero : Zero 1ℤ gcd
```

* Added new functions in `Data.List`:
* Added new functions in `Data.List.Base`:
```agda
takeWhileᵇ : (A → Bool) → List A → List A
dropWhileᵇ : (A → Bool) → List A → List A
@@ -2331,14 +2403,14 @@ Additions to existing modules
find : Decidable P → List A → Maybe A
findIndex : Decidable P → (xs : List A) → Maybe $ Fin (length xs)
findIndices : Decidable P → (xs : List A) → List $ Fin (length xs)
```

* Added new functions and definitions to `Data.List.Base`:
```agda
catMaybes : List (Maybe A) → List A
ap : List (A → B) → List A → List B
++-rawMagma : Set a → RawMagma a _
catMaybes : List (Maybe A) → List A
ap : List (A → B) → List A → List B
++-rawMagma : Set a → RawMagma a _
++-[]-rawMonoid : Set a → RawMonoid a _

insertAt : (xs : List A) → Fin (suc (length xs)) → A → List A
updateAt : (xs : List A) → Fin (length xs) → (A → A) → List A
```

* Added new proofs in `Data.List.Relation.Binary.Lex.Strict`:
@@ -2405,6 +2477,11 @@ Additions to existing modules
map-replicate : map f (replicate n x) ≡ replicate n (f x)

drop-drop : drop n (drop m xs) ≡ drop (m + n) xs

length-insertAt : length (insertAt xs i v) ≡ suc (length xs)
length-removeAt′ : length xs ≡ suc (length (removeAt xs k))
removeAt-insertAt : removeAt (insertAt xs i v) ((cast (sym (length-insertAt xs i v)) i)) ≡ xs
insertAt-removeAt : insertAt (removeAt xs i) (cast (sym (lengthAt-removeAt xs i)) i) (lookup xs i) ≡ xs
```

* Added new patterns and definitions to `Data.Nat.Base`:
@@ -2841,6 +2918,9 @@ Additions to existing modules
fromList-map : cast _ (fromList (List.map f xs)) ≡ map f (fromList xs)
fromList-++ : cast _ (fromList (xs List.++ ys)) ≡ fromList xs ++ fromList ys

length-toList : List.length (toList xs) ≡ length xs
toList-insertAt : toList (insertAt xs i v) ≡ List.insertAt (toList xs) (Fin.cast (cong suc (sym (length-toList xs))) i) v

truncate≡take : .(eq : n ≡ m + o) → truncate m≤n xs ≡ take m (cast eq xs)
take≡truncate : take m xs ≡ truncate (m≤m+n m n) xs
lookup-truncate : lookup (truncate m≤n xs) i ≡ lookup xs (Fin.inject≤ i m≤n)
8 changes: 4 additions & 4 deletions src/Algebra/Properties/CommutativeMonoid/Sum.agda
Original file line number Diff line number Diff line change
@@ -46,7 +46,7 @@ open import Algebra.Properties.Monoid.Sum monoid public

-- When summing over a function from a finite set, we can pull out any
-- value and move it to the front.
sum-remove : ∀ {n} {i : Fin (suc n)} t → sum t ≈ t i + sum (remove i t)
sum-remove : ∀ {n} {i : Fin (suc n)} t → sum t ≈ t i + sum (removeAt t i)
sum-remove {_} {zero} xs = refl
sum-remove {suc n} {suc i} xs = begin
t₀ + ∑t ≈⟨ +-congˡ (sum-remove t) ⟩
@@ -57,7 +57,7 @@ sum-remove {suc n} {suc i} xs = begin
t₀ = head xs
tᵢ = t i
∑t = sum t
∑t′ = sum (remove i t)
∑t′ = sum (removeAt t i)

-- The '∑' operator distributes over addition.
∑-distrib-+ : ∀ {n} (f g : Vector Carrier n) → ∑[ i < n ] (f i + g i) ≈ ∑[ i < n ] f i + ∑[ i < n ] g i
@@ -93,10 +93,10 @@ sum-permute {suc m} {suc n} f π = begin
f 0F + sum f/0 ≡˘⟨ P.cong (_+ sum f/0) (P.cong f (Perm.inverseʳ π)) ⟩
πf π₀ + sum f/0 ≈⟨ +-congˡ (sum-permute f/0 (Perm.remove π₀ π)) ⟩
πf π₀ + sum (rearrange (π/0 ⟨$⟩ʳ_) f/0) ≡˘⟨ P.cong (πf π₀ +_) (sum-cong-≗ (P.cong f ∘ Perm.punchIn-permute′ π 0F)) ⟩
πf π₀ + sum (remove π₀ πf) ≈⟨ sym (sum-remove πf) ⟩
πf π₀ + sum (removeAt πf π₀) ≈⟨ sym (sum-remove πf) ⟩
sum πf ∎
where
f/0 = remove 0F f
f/0 = removeAt f 0F
π₀ = π ⟨$⟩ˡ 0F
π/0 = Perm.remove π₀ π
πf = rearrange (π ⟨$⟩ʳ_) f
4 changes: 2 additions & 2 deletions src/Algebra/Properties/Semiring/Binomial.agda
Original file line number Diff line number Diff line change
@@ -71,7 +71,7 @@ sum₂ n = ∑[ k ≤ suc n ] term₂ n k
------------------------------------------------------------------------
-- Properties

term₂[n,n+1]≈0# : ∀ n → term₂ n (fromℕ (suc n)) ≈ 0#
term₂[n,n+1]≈0# : ∀ n → term₂ n (fromℕ (suc n)) ≈ 0#
term₂[n,n+1]≈0# n rewrite view-fromℕ (suc n) = refl

lemma₁ : ∀ n → x * binomialExpansion n ≈ sum₁ n
@@ -117,7 +117,7 @@ x*lemma {n} i = begin
------------------------------------------------------------------------
-- Next, a lemma which does require commutativity

y*lemma : x * y ≈ y * x → ∀ {n : ℕ} (j : Fin n) →
y*lemma : x * y ≈ y * x → ∀ {n : ℕ} (j : Fin n) →
y * binomialTerm n (suc j) ≈ (n C toℕ (suc j)) × binomial (suc n) (suc (inject₁ j))
y*lemma x*y≈y*x {n} j = begin
y * binomialTerm n (suc j)
34 changes: 27 additions & 7 deletions src/Data/List/Base.agda
Original file line number Diff line number Diff line change
@@ -196,6 +196,14 @@ tails : List A → List (List A)
tails [] = [] ∷ []
tails (x ∷ xs) = (x ∷ xs) ∷ tails xs

insertAt : (xs : List A) → Fin (suc (length xs)) → A → List A
insertAt xs zero v = v ∷ xs
insertAt (x ∷ xs) (suc i) v = x ∷ insertAt xs i v

updateAt : (xs : List A) → Fin (length xs) → (A → A) → List A
updateAt (x ∷ xs) zero f = f x ∷ xs
updateAt (x ∷ xs) (suc i) f = x ∷ updateAt xs i f

-- Scans

scanr : (A → B → B) → B → List A → List B
@@ -330,6 +338,10 @@ splitAt zero xs = ([] , xs)
splitAt (suc n) [] = ([] , [])
splitAt (suc n) (x ∷ xs) = Prod.map₁ (x ∷_) (splitAt n xs)

removeAt : (xs : List A) → Fin (length xs) → List A
removeAt (x ∷ xs) zero = xs
removeAt (x ∷ xs) (suc i) = x ∷ removeAt xs i

-- The following are functions which split a list up using boolean
-- predicates. However, in practice they are difficult to use and
-- prove properties about, and are mainly provided for advanced use
@@ -469,19 +481,18 @@ findIndices P? = findIndicesᵇ (does ∘ P?)
------------------------------------------------------------------------
-- Actions on single elements

infixl 5 _[_]%=_ _[_]∷=_ _─_
infixl 5 _[_]%=_ _[_]∷=_

-- xs [ i ]%= f modifies the i-th element of xs according to f

_[_]%=_ : (xs : List A) → Fin (length xs) → (A → A) → List A
(x ∷ xs) [ zero ]%= f = f x ∷ xs
(x ∷ xs) [ suc k ]%= f = x ∷ (xs [ k ]%= f)
xs [ i ]%= f = updateAt xs i f

-- xs [ i ]≔ y overwrites the i-th element of xs with y

_[_]∷=_ : (xs : List A) → Fin (length xs) → A → List A
xs [ k ]∷= v = xs [ k ]%= const v

_─_ : (xs : List A) → Fin (length xs) → List A
(x ∷ xs) ─ zero = xs
(x ∷ xs) ─ suc k = x ∷ (xs ─ k)

------------------------------------------------------------------------
-- Conditional versions of cons and snoc

@@ -527,3 +538,12 @@ _∷ʳ'_ = InitLast._∷ʳ′_
"Warning: _∷ʳ'_ (ending in an apostrophe) was deprecated in v1.4.
Please use _∷ʳ′_ (ending in a prime) instead."
#-}

-- Version 2.0

infixl 5 _─_
_─_ = removeAt
{-# WARNING_ON_USAGE _─_
"Warning: _─_ was deprecated in v2.0.
Please use removeAt instead."
#-}
54 changes: 45 additions & 9 deletions src/Data/List/Properties.agda
Original file line number Diff line number Diff line change
@@ -746,17 +746,41 @@ map-∷= (x ∷ xs) zero v f = refl
map-∷= (x ∷ xs) (suc k) v f = cong (f x ∷_) (map-∷= xs k v f)

------------------------------------------------------------------------
-- _─_
-- insertAt

length-─ : ∀ (xs : List A) k → length (xs ─ k) ≡ pred (length xs)
length-─ (x ∷ xs) zero = refl
length-─ (x ∷ y ∷ xs) (suc k) = cong suc (length-─ (y ∷ xs) k)
length-insertAt : ∀ (xs : List A) (i : Fin (suc (length xs))) v →
length (insertAt xs i v) ≡ suc (length xs)
length-insertAt xs zero v = refl
length-insertAt (x ∷ xs) (suc i) v = cong suc (length-insertAt xs i v)

map-─ : ∀ xs k (f : A → B) →
let eq = sym (length-map f xs) in
map f (xs ─ k) ≡ map f xs ─ cast eq k
map-─ (x ∷ xs) zero f = refl
map-─ (x ∷ xs) (suc k) f = cong (f x ∷_) (map-─ xs k f)
------------------------------------------------------------------------
-- removeAt

length-removeAt : ∀ (xs : List A) k → length (removeAt xs k) ≡ pred (length xs)
length-removeAt (x ∷ xs) zero = refl
length-removeAt (x ∷ xs@(_ ∷ _)) (suc k) = cong suc (length-removeAt xs k)

length-removeAt′ : ∀ (xs : List A) k → length xs ≡ suc (length (removeAt xs k))
length-removeAt′ xs@(_ ∷ _) k rewrite length-removeAt xs k = refl

map-removeAt : ∀ xs k (f : A → B) →
let eq = sym (length-map f xs) in
map f (removeAt xs k) ≡ removeAt (map f xs) (cast eq k)
map-removeAt (x ∷ xs) zero f = refl
map-removeAt (x ∷ xs) (suc k) f = cong (f x ∷_) (map-removeAt xs k f)

------------------------------------------------------------------------
-- insertAt and removeAt

removeAt-insertAt : ∀ (xs : List A) (i : Fin (suc (length xs))) v →
removeAt (insertAt xs i v) ((cast (sym (length-insertAt xs i v)) i)) ≡ xs
removeAt-insertAt xs zero v = refl
removeAt-insertAt (x ∷ xs) (suc i) v = cong (_ ∷_) (removeAt-insertAt xs i v)

insertAt-removeAt : (xs : List A) (i : Fin (length xs)) →
insertAt (removeAt xs i) (cast (length-removeAt′ xs i) i) (lookup xs i) ≡ xs
insertAt-removeAt (x ∷ xs) zero = refl
insertAt-removeAt (x ∷ xs) (suc i) = cong (x ∷_) (insertAt-removeAt xs i)

------------------------------------------------------------------------
-- take
@@ -1224,3 +1248,15 @@ take++drop = take++drop≡id
"Warning: take++drop was deprecated in v2.0.
Please use take++drop≡id instead."
#-}

length-─ = length-removeAt
{-# WARNING_ON_USAGE length-─
"Warning: length-─ was deprecated in v2.0.
Please use length-removeAt instead."
#-}

map-─ = map-removeAt
{-# WARNING_ON_USAGE map-─
"Warning: map-─ was deprecated in v2.0.
Please use map-removeAt instead."
#-}
2 changes: 1 addition & 1 deletion src/Data/List/Relation/Unary/All/Properties.agda
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ open import Data.Bool.Base using (Bool; T; true; false)
open import Data.Bool.Properties using (T-∧)
open import Data.Empty
open import Data.Fin.Base using (Fin; zero; suc)
open import Data.List.Base as List hiding (lookup)
open import Data.List.Base as List hiding (lookup; updateAt)
open import Data.List.Properties as Listₚ using (partition-defn)
open import Data.List.Membership.Propositional
open import Data.List.Membership.Propositional.Properties
Loading