@@ -57,9 +57,12 @@ module Data.HashMap.Strict.Refined
5757 , insertLookupWithKey
5858 -- * Deletion/Update
5959 , Common.delete
60+ , adjust'
6061 , adjust
6162 , adjustWithKey
63+ , update'
6264 , update
65+ , updateWithKey
6366 , updateLookupWithKey
6467 -- * Query
6568 , Common.lookup
@@ -239,6 +242,10 @@ insertWithKey f k v (HashMap m) = SomeHashMapWith
239242
240243-- | Overwrite a key-value pair that is known to already be in the map. The set
241244-- of keys remains the same.
245+--
246+-- @
247+-- 'reinsert' k v = 'adjust (const v) k'
248+-- @
242249reinsert
243250 :: forall s k a. Hashable k
244251 => Key s k -> a -> HashMap s k a -> HashMap s k a
@@ -260,12 +267,18 @@ insertLookupWithKey f k v (HashMap m) =
260267 $ InsertProof (unsafeKey k) unsafeSubset
261268 )
262269
270+-- | If the given key is in the map, update the value at that key using the
271+-- given function. In any case, the set of keys remains the same.
272+adjust'
273+ :: forall s k a. Hashable k => (a -> a) -> k -> HashMap s k a -> HashMap s k a
274+adjust' = coerce $ HashMap.adjust @k @a
275+
263276-- | Update the value at a specific key known the be in the map using the given
264277-- function. The set of keys remains the same.
265278adjust
266279 :: forall s k a. Hashable k
267280 => (a -> a) -> Key s k -> HashMap s k a -> HashMap s k a
268−adjust = gcoerceWith (unsafeCastKey @s @k) $ coerce $ HashMap.adjust @k @a
281+adjust = gcoerceWith (unsafeCastKey @s @k) $ coerce $ adjust' @s @k @a
269282
270283-- | If the given key is in the map, update the associated value using the given
271284-- function with a proof that the key was in the map; otherwise return the map
@@ -275,6 +288,17 @@ adjustWithKey
275288 => (Key s k -> a -> a) -> k -> HashMap s k a -> HashMap s k a
276289adjustWithKey f k (HashMap m) = HashMap $ HashMap.adjust (f $ unsafeKey k) k m
277290
291+-- | If a key is present in the map, update its value or delete it using the
292+-- given function, returning a potentially smaller map.
293+update'
294+ :: forall s k a. Hashable k
295+ => (a -> Maybe a)
296+ -> k
297+ -> HashMap s k a
298+ -> SomeHashMapWith (SupersetProof 'Hashed s) k a
299+update' f k (HashMap m) = SomeHashMapWith (HashMap $ HashMap.update f k m)
300+ $ SupersetProof unsafeSubset
301+
278302-- | Update or delete a key known to be in the map using the given function,
279303-- returning a potentially smaller map.
280304update
@@ -283,9 +307,20 @@ update
283307 -> Key s k
284308 -> HashMap s k a
285309 -> SomeHashMapWith (SupersetProof 'Hashed s) k a
286−update f k (HashMap m)
287− = SomeHashMapWith (HashMap $ HashMap.update f (unrefine k) m)
288− $ SupersetProof unsafeSubset
310+update = gcoerceWith (unsafeCastKey @s @k) $ coerce $ update' @s @k @a
311+
312+-- | If a key is present in the map, update its value or delete it using the
313+-- given function with a proof that the key was in the map, returning a
314+-- potentially smaller map.
315+updateWithKey
316+ :: forall s k a. Hashable k
317+ => (Key s k -> a -> Maybe a)
318+ -> k
319+ -> HashMap s k a
320+ -> SomeHashMapWith (SupersetProof 'Hashed s) k a
321+updateWithKey f k (HashMap m) = SomeHashMapWith
322+ (HashMap $ HashMap.update (f $ unsafeKey k) k m)
323+ $ SupersetProof unsafeSubset
289324
290325-- | If the given key is in the map, update or delete it using the given
291326-- function with a proof that the key was in the map; otherwise the map is