@@ -0,0 +1,639 @@
1+{-# LANGUAGE CPP #-}
2+{-# LANGUAGE ScopedTypeVariables #-}
3+{-# LANGUAGE MultiParamTypeClasses #-}
4+{-# LANGUAGE TypeFamilies #-}
5+{-# LANGUAGE FlexibleInstances #-}
6+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
7+{-# LANGUAGE DeriveTraversable #-}
8+{-# LANGUAGE DeriveDataTypeable #-}
9+{-# LANGUAGE StandaloneDeriving #-}
10+
11+-- | This module provides a 'Data.IntMap' variant which uses the value's
12+-- 'Monoid' instance to accumulate conflicting entries when merging
13+-- 'Map's.
14+--
15+-- While some functions mirroring those of 'Data.IntMap' are provided
16+-- here for convenience, more specialized needs will likely want to use
17+-- either the 'Newtype' or 'Wrapped' instances to manipulate the
18+-- underlying 'Map'.
19+
20+module Data.IntMap.Monoidal.Strict
21+ ( MonoidalIntMap(..)
22+ -- * Often-needed functions
23+ , singleton
24+ , size
25+ , member
26+ , notMember
27+ , findWithDefault
28+ , assocs
29+ , elems
30+ , keys
31+ , (!)
32+ , (\\)
33+ , adjust
34+ , adjustWithKey
35+ , alter
36+ , delete
37+-- , deleteAt
38+ , deleteFindMax
39+ , deleteFindMin
40+ , deleteMax
41+ , deleteMin
42+ , difference
43+ , differenceWith
44+ , differenceWithKey
45+-- , elemAt
46+ , empty
47+ , filter
48+ , filterWithKey
49+-- , findIndex
50+ , findMax
51+ , findMin
52+ , foldMapWithKey
53+ , foldl
54+ , foldl'
55+ , foldlWithKey
56+ , foldlWithKey'
57+ , foldr
58+ , foldr'
59+ , foldrWithKey
60+ , foldrWithKey'
61+ , fromAscList
62+ , fromAscListWith
63+ , fromAscListWithKey
64+ , fromDistinctAscList
65+ , fromDistinctList
66+ , fromList
67+ , fromListWith
68+ , fromListWithKey
69+ , fromSet
70+ , insert
71+ , insertLookupWithKey
72+ , insertWith
73+ , insertWithKey
74+ , intersectionWith
75+ , intersectionWithKey
76+ , isProperSubmapOf
77+ , isProperSubmapOfBy
78+ , isSubmapOf
79+ , isSubmapOfBy
80+ , keysSet
81+ , lookup
82+ , lookupGE
83+ , lookupGT
84+-- , lookupIndex
85+ , lookupLE
86+ , lookupLT
87+ , map
88+ , mapAccum
89+ , mapAccumRWithKey
90+ , mapAccumWithKey
91+ , mapEither
92+ , mapEitherWithKey
93+ , mapKeys
94+ , mapKeysMonotonic
95+ , mapKeysWith
96+ , mapMaybe
97+ , mapMaybeWithKey
98+ , mapWithKey
99+ , maxView
100+ , maxViewWithKey
101+ , mergeWithKey
102+ , minView
103+ , minViewWithKey
104+ , null
105+ , partition
106+ , partitionWithKey
107+ , split
108+ , splitLookup
109+ , splitRoot
110+ , toAscList
111+ , toDescList
112+ , toList
113+ , traverseWithKey
114+ , unionWith
115+ , unionWithKey
116+ , unionsWith
117+ , update
118+-- , updateAt
119+ , updateLookupWithKey
120+ , updateMax
121+ , updateMaxWithKey
122+ , updateMin
123+ , updateMinWithKey
124+ , updateWithKey
125+-- , valid
126+ -- , showTree
127+ -- , showTreeWith
128+ ) where
129+
130+import Prelude hiding (null, lookup, map, foldl, foldr, filter)
131+
132+import Data.Coerce (coerce)
133+import Data.IntSet (IntSet)
134+import Data.Semigroup
135+import Data.Foldable (Foldable)
136+import Data.Traversable (Traversable)
137+import Control.Applicative (Applicative, pure)
138+import Data.Data (Data)
139+import Data.Typeable (Typeable)
140+
141+#if MIN_VERSION_base(4,7,0)
142+import qualified GHC.Exts as IsList
143+#endif
144+
145+import Control.DeepSeq
146+import qualified Data.IntMap.Strict as M
147+import Control.Lens
148+import Control.Newtype
149+import Data.Aeson(FromJSON, ToJSON, FromJSON1, ToJSON1)
150+#if MIN_VERSION_containers(0,5,9)
151+import Data.Functor.Classes
152+#endif
153+import Data.Align
154+
155+-- | An 'IntMap' with monoidal accumulation
156+newtype MonoidalIntMap a = MonoidalIntMap { getMonoidalIntMap :: M.IntMap a }
157+ deriving (Show, Read, Functor, Eq, Ord, NFData,
158+ Foldable, Traversable,
159+ FromJSON, ToJSON, FromJSON1, ToJSON1,
160+ Data, Typeable, Align
161+#if MIN_VERSION_these(0,8,0)
162+ , Semialign
163+#endif
164+ )
165+
166+#if MIN_VERSION_containers(0,5,9)
167+deriving instance Eq1 MonoidalIntMap
168+deriving instance Ord1 MonoidalIntMap
169+deriving instance Show1 MonoidalIntMap
170+#endif
171+
172+type instance Index (MonoidalIntMap a) = Int
173+type instance IxValue (MonoidalIntMap a) = a
174+instance Ixed (MonoidalIntMap a) where
175+ ix k f (MonoidalIntMap m) = case M.lookup k m of
176+ Just v -> f v <&> \v' -> MonoidalIntMap (M.insert k v' m)
177+ Nothing -> pure (MonoidalIntMap m)
178+ {-# INLINE ix #-}
179+
180+instance At (MonoidalIntMap a) where
181+ at k f (MonoidalIntMap m) = f mv <&> \r -> case r of
182+ Nothing -> maybe (MonoidalIntMap m) (const (MonoidalIntMap $ M.delete k m)) mv
183+ Just v' -> MonoidalIntMap $ M.insert k v' m
184+ where mv = M.lookup k m
185+ {-# INLINE at #-}
186+
187+instance Each (MonoidalIntMap a) (MonoidalIntMap b) a b
188+
189+instance FunctorWithIndex Int MonoidalIntMap
190+instance FoldableWithIndex Int MonoidalIntMap
191+instance TraversableWithIndex Int MonoidalIntMap where
192+ itraverse f (MonoidalIntMap m) = fmap MonoidalIntMap $ itraverse f m
193+ {-# INLINE itraverse #-}
194+
195+instance TraverseMin Int MonoidalIntMap where
196+ traverseMin f (MonoidalIntMap m) = fmap MonoidalIntMap $ traverseMin f m
197+ {-# INLINE traverseMin #-}
198+instance TraverseMax Int MonoidalIntMap where
199+ traverseMax f (MonoidalIntMap m) = fmap MonoidalIntMap $ traverseMax f m
200+ {-# INLINE traverseMax #-}
201+
202+instance AsEmpty (MonoidalIntMap a) where
203+ _Empty = nearly (MonoidalIntMap M.empty) (M.null . unpack)
204+ {-# INLINE _Empty #-}
205+
206+instance Wrapped (MonoidalIntMap a) where
207+ type Unwrapped (MonoidalIntMap a) = M.IntMap a
208+ _Wrapped' = iso unpack pack
209+ {-# INLINE _Wrapped' #-}
210+
211+instance Semigroup a => Semigroup (MonoidalIntMap a) where
212+ MonoidalIntMap a <> MonoidalIntMap b = MonoidalIntMap $ M.unionWith (<>) a b
213+ {-# INLINE (<>) #-}
214+
215+instance Semigroup a => Monoid (MonoidalIntMap a) where
216+ mempty = MonoidalIntMap mempty
217+ {-# INLINE mempty #-}
218+#if !(MIN_VERSION_base(4,11,0))
219+ mappend (MonoidalIntMap a) (MonoidalIntMap b) = MonoidalIntMap $ M.unionWith (<>) a b
220+ {-# INLINE mappend #-}
221+#endif
222+
223+instance Newtype (MonoidalIntMap a) (M.IntMap a) where
224+ pack = MonoidalIntMap
225+ {-# INLINE pack #-}
226+ unpack (MonoidalIntMap a) = a
227+ {-# INLINE unpack #-}
228+
229+#if MIN_VERSION_base(4,7,0)
230+instance Semigroup a => IsList.IsList (MonoidalIntMap a) where
231+ type Item (MonoidalIntMap a) = (Int, a)
232+ fromList = MonoidalIntMap . M.fromListWith (<>)
233+ {-# INLINE fromList #-}
234+ toList = M.toList . unpack
235+ {-# INLINE toList #-}
236+#endif
237+
238+-- | /O(1)/. A map with a single element.
239+singleton :: Int -> a -> MonoidalIntMap a
240+singleton k a = MonoidalIntMap $ M.singleton k a
241+{-# INLINE singleton #-}
242+
243+-- | /O(1)/. The number of elements in the map.
244+size :: MonoidalIntMap a -> Int
245+size = M.size . unpack
246+{-# INLINE size #-}
247+
248+-- | /O(log n)/. Is the key a member of the map? See also 'notMember'.
249+member :: Int -> MonoidalIntMap a -> Bool
250+member k = M.member k . unpack
251+{-# INLINE member #-}
252+
253+-- | /O(log n)/. Is the key not a member of the map? See also 'member'.
254+notMember :: Int -> MonoidalIntMap a -> Bool
255+notMember k = not . M.member k . unpack
256+{-# INLINE notMember #-}
257+
258+-- | /O(log n)/. The expression @('findWithDefault' def k map)@ returns
259+-- the value at key @k@ or returns default value @def@
260+-- when the key is not in the map.
261+findWithDefault :: forall a. a -> Int -> MonoidalIntMap a -> a
262+findWithDefault def k = M.findWithDefault def k . unpack
263+{-# INLINE findWithDefault #-}
264+
265+-- | /O(log n)/. Delete a key and its value from the map. When the key is not
266+-- a member of the map, the original map is returned.
267+delete :: Int -> MonoidalIntMap a -> MonoidalIntMap a
268+delete k = _Wrapping' MonoidalIntMap %~ M.delete k
269+{-# INLINE delete #-}
270+
271+-- | /O(n)/. Return all elements of the map and their keys
272+assocs :: MonoidalIntMap a -> [(Int,a)]
273+assocs = M.assocs . unpack
274+{-# INLINE assocs #-}
275+
276+-- | /O(n)/. Return all elements of the map in the ascending order of their
277+-- keys. Subject to list fusion.
278+elems :: MonoidalIntMap a -> [a]
279+elems = M.elems . unpack
280+{-# INLINE elems #-}
281+
282+-- | /O(n)/. Return all keys of the map in ascending order. Subject to list
283+-- fusion.
284+keys :: MonoidalIntMap a -> [Int]
285+keys = M.keys . unpack
286+{-# INLINE keys #-}
287+
288+(!) :: forall a. MonoidalIntMap a -> Int -> a
289+(!) = coerce ((M.!) :: M.IntMap a -> Int -> a)
290+infixl 9 !
291+
292+(\\) :: forall a b. MonoidalIntMap a -> MonoidalIntMap b -> MonoidalIntMap a
293+(\\) = coerce ((M.\\) :: M.IntMap a -> M.IntMap b -> M.IntMap a)
294+infixl 9 \\ --
295+
296+null :: forall a. MonoidalIntMap a -> Bool
297+null = coerce (M.null :: M.IntMap a -> Bool)
298+{-# INLINE null #-}
299+
300+lookup :: forall a. Int -> MonoidalIntMap a -> Maybe a
301+lookup = coerce (M.lookup :: Int -> M.IntMap a -> Maybe a)
302+{-# INLINE lookup #-}
303+
304+lookupLT :: forall a. Int -> MonoidalIntMap a -> Maybe (Int, a)
305+lookupLT = coerce (M.lookupLT :: Int -> M.IntMap a -> Maybe (Int,a))
306+{-# INLINE lookupLT #-}
307+
308+lookupGT :: forall a. Int -> MonoidalIntMap a -> Maybe (Int, a)
309+lookupGT = coerce (M.lookupGT :: Int -> M.IntMap a -> Maybe (Int,a))
310+{-# INLINE lookupGT #-}
311+
312+lookupLE :: forall a. Int -> MonoidalIntMap a -> Maybe (Int, a)
313+lookupLE = coerce (M.lookupLE :: Int -> M.IntMap a -> Maybe (Int,a))
314+{-# INLINE lookupLE #-}
315+
316+lookupGE :: forall a. Int -> MonoidalIntMap a -> Maybe (Int, a)
317+lookupGE = coerce (M.lookupGE :: Int -> M.IntMap a -> Maybe (Int,a))
318+{-# INLINE lookupGE #-}
319+
320+empty :: forall a. MonoidalIntMap a
321+empty = coerce (M.empty :: M.IntMap a)
322+{-# INLINE empty #-}
323+
324+insert :: forall a. Semigroup a => Int -> a -> MonoidalIntMap a -> MonoidalIntMap a
325+insert = coerce (M.insertWith (<>) :: Int -> a -> M.IntMap a -> M.IntMap a)
326+{-# INLINE insert #-}
327+
328+insertWith :: forall a. (a -> a -> a) -> Int -> a -> MonoidalIntMap a -> MonoidalIntMap a
329+insertWith = coerce (M.insertWith :: (a -> a -> a) -> Int -> a -> M.IntMap a -> M.IntMap a)
330+{-# INLINE insertWith #-}
331+
332+insertWithKey :: forall a. (Int -> a -> a -> a) -> Int -> a -> MonoidalIntMap a -> MonoidalIntMap a
333+insertWithKey = coerce (M.insertWithKey :: (Int -> a -> a -> a) -> Int -> a -> M.IntMap a -> M.IntMap a)
334+{-# INLINE insertWithKey #-}
335+
336+insertLookupWithKey :: forall a. (Int -> a -> a -> a) -> Int -> a -> MonoidalIntMap a -> (Maybe a, MonoidalIntMap a)
337+insertLookupWithKey = coerce (M.insertLookupWithKey :: (Int -> a -> a -> a) -> Int -> a -> M.IntMap a -> (Maybe a, M.IntMap a))
338+{-# INLINE insertLookupWithKey #-}
339+
340+adjust :: forall a. (a -> a) -> Int -> MonoidalIntMap a -> MonoidalIntMap a
341+adjust = coerce (M.adjust :: (a -> a) -> Int -> M.IntMap a -> M.IntMap a)
342+{-# INLINE adjust #-}
343+
344+adjustWithKey :: forall a. (Int -> a -> a) -> Int -> MonoidalIntMap a -> MonoidalIntMap a
345+adjustWithKey = coerce (M.adjustWithKey :: (Int -> a -> a) -> Int -> M.IntMap a -> M.IntMap a)
346+{-# INLINE adjustWithKey #-}
347+
348+update :: forall a. (a -> Maybe a) -> Int -> MonoidalIntMap a -> MonoidalIntMap a
349+update = coerce (M.update :: (a -> Maybe a) -> Int -> M.IntMap a -> M.IntMap a)
350+{-# INLINE update #-}
351+
352+updateWithKey :: forall a. (Int -> a -> Maybe a) -> Int -> MonoidalIntMap a -> MonoidalIntMap a
353+updateWithKey = coerce (M.updateWithKey :: (Int -> a -> Maybe a) -> Int -> M.IntMap a -> M.IntMap a)
354+{-# INLINE updateWithKey #-}
355+
356+updateLookupWithKey :: forall a. (Int -> a -> Maybe a) -> Int -> MonoidalIntMap a -> (Maybe a, MonoidalIntMap a)
357+updateLookupWithKey = coerce (M.updateLookupWithKey :: (Int -> a -> Maybe a) -> Int -> M.IntMap a -> (Maybe a, M.IntMap a))
358+{-# INLINE updateLookupWithKey #-}
359+
360+alter :: forall a. (Maybe a -> Maybe a) -> Int -> MonoidalIntMap a -> MonoidalIntMap a
361+alter = coerce (M.alter :: (Maybe a -> Maybe a) -> Int -> M.IntMap a -> M.IntMap a)
362+{-# INLINE alter #-}
363+
364+unionWith :: forall a. (a -> a -> a) -> MonoidalIntMap a -> MonoidalIntMap a -> MonoidalIntMap a
365+unionWith = coerce (M.unionWith :: (a -> a -> a) -> M.IntMap a -> M.IntMap a -> M.IntMap a)
366+{-# INLINE unionWith #-}
367+
368+unionWithKey :: forall a. (Int -> a -> a -> a) -> MonoidalIntMap a -> MonoidalIntMap a -> MonoidalIntMap a
369+unionWithKey = coerce (M.unionWithKey :: (Int -> a -> a -> a) -> M.IntMap a -> M.IntMap a -> M.IntMap a)
370+{-# INLINE unionWithKey #-}
371+
372+unionsWith :: forall a. (a -> a -> a) -> [MonoidalIntMap a] -> MonoidalIntMap a
373+unionsWith = coerce (M.unionsWith :: (a -> a -> a) -> [M.IntMap a] -> M.IntMap a)
374+{-# INLINE unionsWith #-}
375+
376+difference :: forall a b. MonoidalIntMap a -> MonoidalIntMap b -> MonoidalIntMap a
377+difference = (\\)
378+{-# INLINE difference #-}
379+
380+differenceWith :: forall a b. (a -> b -> Maybe a) -> MonoidalIntMap a -> MonoidalIntMap b -> MonoidalIntMap a
381+differenceWith = coerce (M.differenceWith :: (a -> b -> Maybe a) -> M.IntMap a -> M.IntMap b -> M.IntMap a)
382+{-# INLINE differenceWith #-}
383+
384+differenceWithKey :: forall a b. (Int -> a -> b -> Maybe a) -> MonoidalIntMap a -> MonoidalIntMap b -> MonoidalIntMap a
385+differenceWithKey = coerce (M.differenceWithKey :: (Int -> a -> b -> Maybe a) -> M.IntMap a -> M.IntMap b -> M.IntMap a)
386+{-# INLINE differenceWithKey #-}
387+
388+intersectionWith :: forall a b c. (a -> b -> c) -> MonoidalIntMap a -> MonoidalIntMap b -> MonoidalIntMap c
389+intersectionWith = coerce (M.intersectionWith :: (a -> b -> c) -> M.IntMap a -> M.IntMap b -> M.IntMap c)
390+{-# INLINE intersectionWith #-}
391+
392+intersectionWithKey :: forall a b c. (Int -> a -> b -> c) -> MonoidalIntMap a -> MonoidalIntMap b -> MonoidalIntMap c
393+intersectionWithKey = coerce (M.intersectionWithKey :: (Int -> a -> b -> c) -> M.IntMap a -> M.IntMap b -> M.IntMap c)
394+{-# INLINE intersectionWithKey #-}
395+
396+mergeWithKey :: forall a b c. (Int -> a -> b -> Maybe c) -> (MonoidalIntMap a -> MonoidalIntMap c) -> (MonoidalIntMap b -> MonoidalIntMap c) -> MonoidalIntMap a -> MonoidalIntMap b -> MonoidalIntMap c
397+mergeWithKey = coerce (M.mergeWithKey :: (Int -> a -> b -> Maybe c) -> (M.IntMap a -> M.IntMap c) -> (M.IntMap b -> M.IntMap c) -> M.IntMap a -> M.IntMap b -> M.IntMap c)
398+{-# INLINE mergeWithKey #-}
399+
400+map :: (a -> b) -> MonoidalIntMap a -> MonoidalIntMap b
401+map = fmap
402+{-# INLINE map #-}
403+
404+mapWithKey :: forall a b. (Int -> a -> b) -> MonoidalIntMap a -> MonoidalIntMap b
405+mapWithKey = coerce (M.mapWithKey :: (Int -> a -> b) -> M.IntMap a -> M.IntMap b)
406+{-# INLINE mapWithKey #-}
407+
408+traverseWithKey :: Applicative t => (Int -> a -> t b) -> MonoidalIntMap a -> t (MonoidalIntMap b)
409+traverseWithKey = itraverse
410+{-# INLINE traverseWithKey #-}
411+
412+mapAccum :: forall a b c. (a -> b -> (a, c)) -> a -> MonoidalIntMap b -> (a, MonoidalIntMap c)
413+mapAccum = coerce (M.mapAccum :: (a -> b -> (a, c)) -> a -> M.IntMap b -> (a, M.IntMap c))
414+{-# INLINE mapAccum #-}
415+
416+mapAccumWithKey :: forall a b c. (a -> Int -> b -> (a, c)) -> a -> MonoidalIntMap b -> (a, MonoidalIntMap c)
417+mapAccumWithKey = coerce (M.mapAccumWithKey :: (a -> Int -> b -> (a, c)) -> a -> M.IntMap b -> (a, M.IntMap c))
418+{-# INLINE mapAccumWithKey #-}
419+
420+mapAccumRWithKey :: forall a b c. (a -> Int -> b -> (a, c)) -> a -> MonoidalIntMap b -> (a, MonoidalIntMap c)
421+mapAccumRWithKey = coerce (M.mapAccumRWithKey :: (a -> Int -> b -> (a, c)) -> a -> M.IntMap b -> (a, M.IntMap c))
422+{-# INLINE mapAccumRWithKey #-}
423+
424+mapKeys :: forall a. Semigroup a => (Int -> Int) -> MonoidalIntMap a -> MonoidalIntMap a
425+mapKeys = coerce (M.mapKeysWith (<>) :: (Int -> Int) -> M.IntMap a -> M.IntMap a)
426+{-# INLINE mapKeys #-}
427+
428+mapKeysWith :: forall a. (a -> a -> a) -> (Int -> Int) -> MonoidalIntMap a -> MonoidalIntMap a
429+mapKeysWith = coerce (M.mapKeysWith :: (a -> a -> a) -> (Int -> Int) -> M.IntMap a -> M.IntMap a)
430+{-# INLINE mapKeysWith #-}
431+
432+mapKeysMonotonic :: forall a. (Int -> Int) -> MonoidalIntMap a -> MonoidalIntMap a
433+mapKeysMonotonic = coerce (M.mapKeysMonotonic :: (Int -> Int) -> M.IntMap a -> M.IntMap a)
434+{-# INLINE mapKeysMonotonic #-}
435+
436+foldr :: forall a b. (a -> b -> b) -> b -> MonoidalIntMap a -> b
437+foldr = coerce (M.foldr :: (a -> b -> b) -> b -> M.IntMap a -> b)
438+{-# INLINE foldr #-}
439+
440+foldl :: forall a b. (a -> b -> a) -> a -> MonoidalIntMap b -> a
441+foldl = coerce (M.foldl :: (a -> b -> a) -> a -> M.IntMap b -> a)
442+{-# INLINE foldl #-}
443+
444+foldrWithKey :: forall a b. (Int -> a -> b -> b) -> b -> MonoidalIntMap a -> b
445+foldrWithKey = coerce (M.foldrWithKey :: (Int -> a -> b -> b) -> b -> M.IntMap a -> b)
446+{-# INLINE foldrWithKey #-}
447+
448+foldlWithKey :: forall a b. (a -> Int -> b -> a) -> a -> MonoidalIntMap b -> a
449+foldlWithKey = coerce (M.foldlWithKey :: (a -> Int -> b -> a) -> a -> M.IntMap b -> a)
450+{-# INLINE foldlWithKey #-}
451+
452+foldMapWithKey :: forall a m. Monoid m => (Int -> a -> m) -> MonoidalIntMap a -> m
453+foldMapWithKey = coerce (M.foldMapWithKey :: Monoid m => (Int -> a -> m) -> M.IntMap a -> m)
454+{-# INLINE foldMapWithKey #-}
455+
456+foldr' :: forall a b. (a -> b -> b) -> b -> MonoidalIntMap a -> b
457+foldr' = coerce (M.foldr' :: (a -> b -> b) -> b -> M.IntMap a -> b)
458+{-# INLINE foldr' #-}
459+
460+foldl' :: forall a b. (a -> b -> a) -> a -> MonoidalIntMap b -> a
461+foldl' = coerce (M.foldl' :: (a -> b -> a) -> a -> M.IntMap b -> a)
462+{-# INLINE foldl' #-}
463+
464+foldrWithKey' :: forall a b. (Int -> a -> b -> b) -> b -> MonoidalIntMap a -> b
465+foldrWithKey' = coerce (M.foldrWithKey' :: (Int -> a -> b -> b) -> b -> M.IntMap a -> b)
466+{-# INLINE foldrWithKey' #-}
467+
468+foldlWithKey' :: forall a b. (a -> Int -> b -> a) -> a -> MonoidalIntMap b -> a
469+foldlWithKey' = coerce (M.foldlWithKey' :: (a -> Int -> b -> a) -> a -> M.IntMap b -> a)
470+{-# INLINE foldlWithKey' #-}
471+
472+keysSet :: forall a. MonoidalIntMap a -> IntSet
473+keysSet = coerce (M.keysSet :: M.IntMap a -> IntSet)
474+{-# INLINE keysSet #-}
475+
476+fromSet :: forall a. (Int -> a) -> IntSet -> MonoidalIntMap a
477+fromSet = coerce (M.fromSet :: (Int -> a) -> IntSet -> M.IntMap a)
478+{-# INLINE fromSet #-}
479+
480+toList :: forall a. MonoidalIntMap a -> [(Int, a)]
481+toList = coerce (M.toList :: M.IntMap a -> [(Int, a)])
482+{-# INLINE toList #-}
483+
484+fromList :: forall a. Semigroup a => [(Int, a)] -> MonoidalIntMap a
485+fromList = fromListWith (<>)
486+{-# INLINE fromList #-}
487+
488+fromListWith :: forall a. (a -> a -> a) -> [(Int, a)] -> MonoidalIntMap a
489+fromListWith = coerce (M.fromListWith :: (a -> a -> a) -> [(Int, a)] -> M.IntMap a)
490+{-# INLINE fromListWith #-}
491+
492+fromListWithKey :: forall a. (Int -> a -> a -> a) -> [(Int, a)] -> MonoidalIntMap a
493+fromListWithKey = coerce (M.fromListWithKey :: (Int -> a -> a -> a) -> [(Int, a)] -> M.IntMap a)
494+{-# INLINE fromListWithKey #-}
495+
496+toAscList :: forall a. MonoidalIntMap a -> [(Int, a)]
497+toAscList = coerce (M.toAscList :: M.IntMap a -> [(Int, a)])
498+{-# INLINE toAscList #-}
499+
500+toDescList :: forall a. MonoidalIntMap a -> [(Int, a)]
501+toDescList = coerce (M.toDescList :: M.IntMap a -> [(Int, a)])
502+{-# INLINE toDescList #-}
503+
504+fromAscList :: forall a. Semigroup a => [(Int, a)] -> MonoidalIntMap a
505+fromAscList = fromAscListWith (<>)
506+{-# INLINE fromAscList #-}
507+
508+fromAscListWith :: forall a. (a -> a -> a) -> [(Int, a)] -> MonoidalIntMap a
509+fromAscListWith = coerce (M.fromAscListWith :: (a -> a -> a) -> [(Int, a)] -> M.IntMap a)
510+{-# INLINE fromAscListWith #-}
511+
512+fromAscListWithKey :: forall a. (Int -> a -> a -> a) -> [(Int, a)] -> MonoidalIntMap a
513+fromAscListWithKey = coerce (M.fromAscListWithKey :: (Int -> a -> a -> a) -> [(Int, a)] -> M.IntMap a)
514+{-# INLINE fromAscListWithKey #-}
515+
516+fromDistinctAscList :: forall a. [(Int, a)] -> MonoidalIntMap a
517+fromDistinctAscList = coerce (M.fromDistinctAscList :: [(Int, a)] -> M.IntMap a)
518+{-# INLINE fromDistinctAscList #-}
519+
520+fromDistinctList :: forall a. [(Int, a)] -> MonoidalIntMap a
521+fromDistinctList = coerce (M.fromList :: [(Int, a)] -> M.IntMap a)
522+{-# INLINE fromDistinctList #-}
523+
524+filter :: forall a. (a -> Bool) -> MonoidalIntMap a -> MonoidalIntMap a
525+filter = coerce (M.filter :: (a -> Bool) -> M.IntMap a -> M.IntMap a)
526+{-# INLINE filter #-}
527+
528+filterWithKey :: forall a. (Int -> a -> Bool) -> MonoidalIntMap a -> MonoidalIntMap a
529+filterWithKey = coerce (M.filterWithKey :: (Int -> a -> Bool) -> M.IntMap a -> M.IntMap a)
530+{-# INLINE filterWithKey #-}
531+
532+partition :: forall a. (a -> Bool) -> MonoidalIntMap a -> (MonoidalIntMap a, MonoidalIntMap a)
533+partition = coerce (M.partition :: (a -> Bool) -> M.IntMap a -> (M.IntMap a, M.IntMap a))
534+{-# INLINE partition #-}
535+
536+partitionWithKey :: forall a. (Int -> a -> Bool) -> MonoidalIntMap a -> (MonoidalIntMap a, MonoidalIntMap a)
537+partitionWithKey = coerce (M.partitionWithKey :: (Int -> a -> Bool) -> M.IntMap a -> (M.IntMap a, M.IntMap a))
538+{-# INLINE partitionWithKey #-}
539+
540+mapMaybe :: forall a b. (a -> Maybe b) -> MonoidalIntMap a -> MonoidalIntMap b
541+mapMaybe = coerce (M.mapMaybe :: (a -> Maybe b) -> M.IntMap a -> M.IntMap b)
542+{-# INLINE mapMaybe #-}
543+
544+mapMaybeWithKey :: forall a b. (Int -> a -> Maybe b) -> MonoidalIntMap a -> MonoidalIntMap b
545+mapMaybeWithKey = coerce (M.mapMaybeWithKey :: (Int -> a -> Maybe b) -> M.IntMap a -> M.IntMap b)
546+{-# INLINE mapMaybeWithKey #-}
547+
548+mapEither :: forall a b c. (a -> Either b c) -> MonoidalIntMap a -> (MonoidalIntMap b, MonoidalIntMap c)
549+mapEither = coerce (M.mapEither :: (a -> Either b c) -> M.IntMap a -> (M.IntMap b, M.IntMap c))
550+{-# INLINE mapEither #-}
551+
552+mapEitherWithKey :: forall a b c. (Int -> a -> Either b c) -> MonoidalIntMap a -> (MonoidalIntMap b, MonoidalIntMap c)
553+mapEitherWithKey = coerce (M.mapEitherWithKey :: (Int -> a -> Either b c) -> M.IntMap a -> (M.IntMap b, M.IntMap c))
554+{-# INLINE mapEitherWithKey #-}
555+
556+split :: forall a. Int -> MonoidalIntMap a -> (MonoidalIntMap a, MonoidalIntMap a)
557+split = coerce (M.split :: Int -> M.IntMap a -> (M.IntMap a, M.IntMap a))
558+{-# INLINE split #-}
559+
560+splitLookup :: forall a. Int -> MonoidalIntMap a -> (MonoidalIntMap a, Maybe a, MonoidalIntMap a)
561+splitLookup = coerce (M.splitLookup :: Int -> M.IntMap a -> (M.IntMap a, Maybe a, M.IntMap a))
562+{-# INLINE splitLookup #-}
563+
564+splitRoot :: forall a. MonoidalIntMap a -> [MonoidalIntMap a]
565+splitRoot = coerce (M.splitRoot :: M.IntMap a -> [M.IntMap a])
566+{-# INLINE splitRoot #-}
567+
568+isSubmapOf :: forall a. Eq a => MonoidalIntMap a -> MonoidalIntMap a -> Bool
569+isSubmapOf = coerce (M.isSubmapOf :: M.IntMap a -> M.IntMap a -> Bool)
570+{-# INLINE isSubmapOf #-}
571+
572+isSubmapOfBy :: forall a b. (a -> b -> Bool) -> MonoidalIntMap a -> MonoidalIntMap b -> Bool
573+isSubmapOfBy = coerce (M.isSubmapOfBy :: (a -> b -> Bool) -> M.IntMap a -> M.IntMap b -> Bool)
574+{-# INLINE isSubmapOfBy #-}
575+
576+isProperSubmapOf :: forall a. Eq a => MonoidalIntMap a -> MonoidalIntMap a -> Bool
577+isProperSubmapOf = coerce (M.isProperSubmapOf :: M.IntMap a -> M.IntMap a -> Bool)
578+{-# INLINE isProperSubmapOf #-}
579+
580+isProperSubmapOfBy :: forall a b. (a -> b -> Bool) -> MonoidalIntMap a -> MonoidalIntMap b -> Bool
581+isProperSubmapOfBy = coerce (M.isProperSubmapOfBy :: (a -> b -> Bool) -> M.IntMap a -> M.IntMap b -> Bool)
582+{-# INLINE isProperSubmapOfBy #-}
583+
584+findMin :: forall a. MonoidalIntMap a -> (Int, a)
585+findMin = coerce (M.findMin :: M.IntMap a -> (Int, a))
586+{-# INLINE findMin #-}
587+
588+findMax :: forall a. MonoidalIntMap a -> (Int, a)
589+findMax = coerce (M.findMax :: M.IntMap a -> (Int, a))
590+{-# INLINE findMax #-}
591+
592+deleteMin :: forall a. MonoidalIntMap a -> MonoidalIntMap a
593+deleteMin = coerce (M.deleteMin :: M.IntMap a -> M.IntMap a)
594+{-# INLINE deleteMin #-}
595+
596+deleteMax :: forall a. MonoidalIntMap a -> MonoidalIntMap a
597+deleteMax = coerce (M.deleteMax :: M.IntMap a -> M.IntMap a)
598+{-# INLINE deleteMax #-}
599+
600+deleteFindMin :: forall a. MonoidalIntMap a -> ((Int, a), MonoidalIntMap a)
601+deleteFindMin = coerce (M.deleteFindMin :: M.IntMap a -> ((Int, a), M.IntMap a))
602+{-# INLINE deleteFindMin #-}
603+
604+deleteFindMax :: forall a. MonoidalIntMap a -> ((Int, a), MonoidalIntMap a)
605+deleteFindMax = coerce (M.deleteFindMax :: M.IntMap a -> ((Int, a), M.IntMap a))
606+{-# INLINE deleteFindMax #-}
607+
608+updateMin :: forall a. (a -> Maybe a) -> MonoidalIntMap a -> MonoidalIntMap a
609+updateMin = coerce (M.updateMin :: (a -> Maybe a) -> M.IntMap a -> M.IntMap a)
610+{-# INLINE updateMin #-}
611+
612+updateMax :: forall a. (a -> Maybe a) -> MonoidalIntMap a -> MonoidalIntMap a
613+updateMax = coerce (M.updateMax :: (a -> Maybe a) -> M.IntMap a -> M.IntMap a)
614+{-# INLINE updateMax #-}
615+
616+updateMinWithKey :: forall a. (Int -> a -> Maybe a) -> MonoidalIntMap a -> MonoidalIntMap a
617+updateMinWithKey = coerce (M.updateMinWithKey :: (Int -> a -> Maybe a) -> M.IntMap a -> M.IntMap a)
618+{-# INLINE updateMinWithKey #-}
619+
620+updateMaxWithKey :: forall a. (Int -> a -> Maybe a) -> MonoidalIntMap a -> MonoidalIntMap a
621+updateMaxWithKey = coerce (M.updateMaxWithKey :: (Int -> a -> Maybe a) -> M.IntMap a -> M.IntMap a)
622+{-# INLINE updateMaxWithKey #-}
623+
624+minView :: forall a. MonoidalIntMap a -> Maybe (a, MonoidalIntMap a)
625+minView = coerce (M.minView :: M.IntMap a -> Maybe (a, M.IntMap a))
626+{-# INLINE minView #-}
627+
628+maxView :: forall a. MonoidalIntMap a -> Maybe (a, MonoidalIntMap a)
629+maxView = coerce (M.maxView :: M.IntMap a -> Maybe (a, M.IntMap a))
630+{-# INLINE maxView #-}
631+
632+minViewWithKey :: forall a. MonoidalIntMap a -> Maybe ((Int, a), MonoidalIntMap a)
633+minViewWithKey = coerce (M.minViewWithKey :: M.IntMap a -> Maybe ((Int, a), M.IntMap a))
634+{-# INLINE minViewWithKey #-}
635+
636+maxViewWithKey :: forall a. MonoidalIntMap a -> Maybe ((Int, a), MonoidalIntMap a)
637+maxViewWithKey = coerce (M.maxViewWithKey :: M.IntMap a -> Maybe ((Int, a), M.IntMap a))
638+{-# INLINE maxViewWithKey #-}
639+