on 4e4600f help sign in
patch/src/Data/AppendMap.hs
409 lines 15 kB history raw
1{-# LANGUAGE DeriveFunctor #-}
2{-# LANGUAGE DeriveGeneric #-}
3{-# LANGUAGE DeriveTraversable #-}
4{-# LANGUAGE FlexibleContexts #-}
5{-# LANGUAGE FlexibleInstances #-}
6{-# LANGUAGE GeneralizedNewtypeDeriving #-}
7{-# LANGUAGE MultiParamTypeClasses #-}
8{-# LANGUAGE RoleAnnotations #-}
9{-# LANGUAGE ScopedTypeVariables #-}
10{-# LANGUAGE TypeApplications #-}
11{-# LANGUAGE TypeFamilies #-}
12-- | 'Data.Map' with a better 'Monoid' instance
13--
14-- 'Data.Map' has @mappend = union@, which is left-biased. AppendMap has
15-- @mappend = unionWith mappend@ instead.
16module Data.AppendMap where
18import Prelude hiding (map)
20import Control.Lens
21import Data.Align
22import Data.Coerce
23import Data.Default
24import Data.Map (Map)
25import qualified Data.Map as Map
26import Data.Semigroup
27import Data.Set (Set)
28import Data.Typeable
29import GHC.Generics (Generic)
30import Reflex (FunctorMaybe (..))
31import Reflex.Patch (Additive, Group (..))
33newtype AppendMap k m = AppendMap { _unAppendMap :: Map k m }
34 deriving (Eq, Ord, Show, Read, Typeable, Generic, Functor, Foldable, Traversable, Align)
36type instance (Index (AppendMap k m)) = k
37type instance (IxValue (AppendMap k m)) = m
39instance Ord k => Ixed (AppendMap k m) where
40 ix i = _Wrapped . ix i
42instance Ord k => At (AppendMap k m) where
43 at i = _Wrapped . at i
45instance FunctorMaybe (AppendMap k) where
46 fmapMaybe = mapMaybe
48-- | Deletes a key, returning 'Nothing' if the result is empty.
49nonEmptyDelete :: Ord k => k -> AppendMap k a -> Maybe (AppendMap k a)
50nonEmptyDelete k vs =
51 let deleted = delete k vs
52 in if Data.AppendMap.null deleted
53 then Nothing
54 else Just deleted
56mapMaybeNoNull :: (a -> Maybe b)
57 -> AppendMap token a
58 -> Maybe (AppendMap token b)
59mapMaybeNoNull f as =
60 let bs = fmapMaybe f as
61 in if Data.AppendMap.null bs
62 then Nothing
63 else Just bs
65instance FunctorWithIndex k (AppendMap k) where
66 imap f = AppendMap . imap f . _unAppendMap
67 imapped = _Wrapped . imapped
69instance FoldableWithIndex k (AppendMap k) where
70 ifolded = _Wrapped . ifolded
72instance TraversableWithIndex k (AppendMap k) where
73 itraverse = itraverseOf itraversed
74 itraversed = _Wrapped . itraversed
76instance (Ord k, Semigroup m) => Semigroup (AppendMap k m) where
77 (AppendMap m0) <> (AppendMap m1) = AppendMap $ Map.unionWith (<>) m0 m1
79instance (Ord k, Semigroup m) => Monoid (AppendMap k m) where
80 mempty = empty
81 mappend = (<>)
83instance (Ord k, Group q) => Group (AppendMap k q) where
84 negateG = map negateG
86instance (Ord k, Additive q) => Additive (AppendMap k q)
88(!) :: forall k a. Ord k => AppendMap k a -> k -> a
89(!) = coerce ((Map.!) @k @a)
90infixl 9 !
92(\\) :: forall k a b. Ord k => AppendMap k a -> AppendMap k b -> AppendMap k a
93(\\) = coerce ((Map.\\) @k @a @b)
94infixl 9 \\
96null :: forall k a. AppendMap k a -> Bool
97null = coerce (Map.null @k @a)
99size :: forall k a. AppendMap k a -> Int
100size = coerce (Map.size @k @a)
102member :: forall k a. Ord k => k -> AppendMap k a -> Bool
103member = coerce (Map.member @k @a)
105notMember :: forall k a. Ord k => k -> AppendMap k a -> Bool
106notMember = coerce (Map.notMember @k @a)
108lookup :: forall k a. Ord k => k -> AppendMap k a -> Maybe a
109lookup = coerce (Map.lookup @k @a)
111findWithDefault :: forall k a. Ord k => a -> k -> AppendMap k a -> a
112findWithDefault = coerce (Map.findWithDefault @k @a)
114lookupLT :: forall k a. Ord k => k -> AppendMap k a -> Maybe (k, a)
115lookupLT = coerce (Map.lookupLT @k @a)
117lookupGT :: forall k a. Ord k => k -> AppendMap k a -> Maybe (k, a)
118lookupGT = coerce (Map.lookupGT @k @a)
120lookupLE :: forall k a. Ord k => k -> AppendMap k a -> Maybe (k, a)
121lookupLE = coerce (Map.lookupLE @k @a)
123lookupGE :: forall k a. Ord k => k -> AppendMap k a -> Maybe (k, a)
124lookupGE = coerce (Map.lookupGE @k @a)
126empty :: forall k a. AppendMap k a
127empty = coerce (Map.empty @k @a)
129singleton :: forall k a. k -> a -> AppendMap k a
130singleton = coerce (Map.singleton @k @a)
132insert :: forall k a. Ord k => k -> a -> AppendMap k a -> AppendMap k a
133insert = coerce (Map.insert @k @a)
135insertWith :: Ord k => (a -> a -> a) -> k -> a -> AppendMap k a -> AppendMap k a
136insertWith f = coerce (Map.insertWith f)
138insertWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> AppendMap k a -> AppendMap k a
139insertWithKey f = coerce (Map.insertWithKey f)
141insertLookupWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> AppendMap k a -> (Maybe a, AppendMap k a)
142insertLookupWithKey f = coerce (Map.insertLookupWithKey f)
144delete :: forall k a. Ord k => k -> AppendMap k a -> AppendMap k a
145delete = coerce (Map.delete @k @a)
147adjust :: Ord k => (a -> a) -> k -> AppendMap k a -> AppendMap k a
148adjust f = coerce (Map.adjust f)
150adjustWithKey :: Ord k => (k -> a -> a) -> k -> AppendMap k a -> AppendMap k a
151adjustWithKey f = coerce (Map.adjustWithKey f)
153update :: Ord k => (a -> Maybe a) -> k -> AppendMap k a -> AppendMap k a
154update f = coerce (Map.update f)
156updateWithKey :: Ord k => (k -> a -> Maybe a) -> k -> AppendMap k a -> AppendMap k a
157updateWithKey f = coerce (Map.updateWithKey f)
159updateLookupWithKey :: Ord k => (k -> a -> Maybe a) -> k -> AppendMap k a -> (Maybe a, AppendMap k a)
160updateLookupWithKey f = coerce (Map.updateLookupWithKey f)
162alter :: Ord k => (Maybe a -> Maybe a) -> k -> AppendMap k a -> AppendMap k a
163alter f = coerce (Map.alter f)
165unionWith :: Ord k => (a -> a -> a) -> AppendMap k a -> AppendMap k a -> AppendMap k a
166unionWith f = coerce (Map.unionWith f)
168unionWithKey :: Ord k => (k -> a -> a -> a) -> AppendMap k a -> AppendMap k a -> AppendMap k a
169unionWithKey f = coerce (Map.unionWithKey f)
171unionsWith :: Ord k => (a -> a -> a) -> [AppendMap k a] -> AppendMap k a
172unionsWith f = coerce (Map.unionsWith f)
174difference :: Ord k => AppendMap k a -> AppendMap k b -> AppendMap k a
175difference = (\\)
177differenceWith :: Ord k => (a -> b -> Maybe a) -> AppendMap k a -> AppendMap k b -> AppendMap k a
178differenceWith f = coerce (Map.differenceWith f)
180differenceWithKey :: Ord k => (k -> a -> b -> Maybe a) -> AppendMap k a -> AppendMap k b -> AppendMap k a
181differenceWithKey f = coerce (Map.differenceWithKey f)
183intersectionWith :: Ord k => (a -> b -> c) -> AppendMap k a -> AppendMap k b -> AppendMap k c
184intersectionWith f = coerce (Map.intersectionWith f)
186intersectionWithKey :: Ord k => (k -> a -> b -> c) -> AppendMap k a -> AppendMap k b -> AppendMap k c
187intersectionWithKey f = coerce (Map.intersectionWithKey f)
189mergeWithKey :: Ord k => (k -> a -> b -> Maybe c) -> (AppendMap k a -> AppendMap k c) -> (AppendMap k b -> AppendMap k c) -> AppendMap k a -> AppendMap k b -> AppendMap k c
190mergeWithKey f = coerce (Map.mergeWithKey f)
192map :: (a -> b) -> AppendMap k a -> AppendMap k b
193map = fmap
195mapWithKey :: (k -> a -> b) -> AppendMap k a -> AppendMap k b
196mapWithKey = imap
198traverseWithKey :: Applicative t => (k -> a -> t b) -> AppendMap k a -> t (AppendMap k b)
199traverseWithKey = itraverse
201mapAccum :: (a -> b -> (a, c)) -> a -> AppendMap k b -> (a, AppendMap k c)
202mapAccum f = coerce (Map.mapAccum f)
204mapAccumWithKey :: (a -> k -> b -> (a, c)) -> a -> AppendMap k b -> (a, AppendMap k c)
205mapAccumWithKey f = coerce (Map.mapAccumWithKey f)
207mapAccumRWithKey :: (a -> k -> b -> (a, c)) -> a -> AppendMap k b -> (a, AppendMap k c)
208mapAccumRWithKey f = coerce (Map.mapAccumRWithKey f)
210mapKeys :: forall k1 k2 a. Ord k2 => (k1 -> k2) -> AppendMap k1 a -> AppendMap k2 a
211mapKeys = coerce (Map.mapKeys @k2 @k1 @a)
213mapKeysWith :: Ord k2 => (a -> a -> a) -> (k1 -> k2) -> AppendMap k1 a -> AppendMap k2 a
214mapKeysWith f = coerce (Map.mapKeysWith f)
216mapKeysMonotonic :: forall k1 k2 a. (k1 -> k2) -> AppendMap k1 a -> AppendMap k2 a
217mapKeysMonotonic = coerce (Map.mapKeysMonotonic @k1 @k2 @a)
219foldr :: (a -> b -> b) -> b -> AppendMap k a -> b
220foldr f = coerce (Map.foldr f)
222foldl :: (a -> b -> a) -> a -> AppendMap k b -> a
223foldl f = coerce (Map.foldl f)
225foldrWithKey :: (k -> a -> b -> b) -> b -> AppendMap k a -> b
226foldrWithKey f = coerce (Map.foldrWithKey f)
228foldlWithKey :: (a -> k -> b -> a) -> a -> AppendMap k b -> a
229foldlWithKey f = coerce (Map.foldlWithKey f)
231foldMapWithKey :: Monoid m => (k -> a -> m) -> AppendMap k a -> m
232foldMapWithKey f = coerce (Map.foldMapWithKey f)
234foldr' :: (a -> b -> b) -> b -> AppendMap k a -> b
235foldr' f = coerce (Map.foldr' f)
237foldl' :: (a -> b -> a) -> a -> AppendMap k b -> a
238foldl' f = coerce (Map.foldl' f)
240foldrWithKey' :: (k -> a -> b -> b) -> b -> AppendMap k a -> b
241foldrWithKey' f = coerce (Map.foldrWithKey' f)
243foldlWithKey' :: (a -> k -> b -> a) -> a -> AppendMap k b -> a
244foldlWithKey' f = coerce (Map.foldlWithKey' f)
246elems :: forall k a. AppendMap k a -> [a]
247elems = coerce (Map.elems @k @a)
249keys :: forall k a. AppendMap k a -> [k]
250keys = coerce (Map.keys @k @a)
252assocs :: forall k a. AppendMap k a -> [(k, a)]
253assocs = coerce (Map.assocs @k @a)
255keysSet :: forall k a. AppendMap k a -> Set k
256keysSet = coerce (Map.keysSet @k @a)
258fromSet :: (k -> a) -> Set k -> AppendMap k a
259fromSet f = coerce (Map.fromSet f)
261toList :: forall k a. AppendMap k a -> [(k, a)]
262toList = coerce (Map.toList @k @a)
264fromList :: forall k a. Ord k => [(k, a)] -> AppendMap k a
265fromList = coerce (Map.fromList @k @a)
267fromListWith :: Ord k => (a -> a -> a) -> [(k, a)] -> AppendMap k a
268fromListWith f = coerce (Map.fromListWith f)
270fromListWithKey :: Ord k => (k -> a -> a -> a) -> [(k, a)] -> AppendMap k a
271fromListWithKey f = coerce (Map.fromListWithKey f)
273toAscList :: forall k a. AppendMap k a -> [(k, a)]
274toAscList = coerce (Map.toAscList @k @a)
276toDescList :: forall k a. AppendMap k a -> [(k, a)]
277toDescList = coerce (Map.toDescList @k @a)
279fromAscList :: forall k a. Eq k => [(k, a)] -> AppendMap k a
280fromAscList = coerce (Map.fromAscList @k @a)
282fromAscListWith :: Eq k => (a -> a -> a) -> [(k, a)] -> AppendMap k a
283fromAscListWith f = coerce (Map.fromAscListWith f)
285fromAscListWithKey :: Eq k => (k -> a -> a -> a) -> [(k, a)] -> AppendMap k a
286fromAscListWithKey f = coerce (Map.fromAscListWithKey f)
288fromDistinctAscList :: forall k a. [(k, a)] -> AppendMap k a
289fromDistinctAscList = coerce (Map.fromDistinctAscList @k @a)
291filter :: (a -> Bool) -> AppendMap k a -> AppendMap k a
292filter f = coerce (Map.filter f)
294filterWithKey :: (k -> a -> Bool) -> AppendMap k a -> AppendMap k a
295filterWithKey f = coerce (Map.filterWithKey f)
297partition :: (a -> Bool) -> AppendMap k a -> (AppendMap k a, AppendMap k a)
298partition f = coerce (Map.partition f)
300partitionWithKey :: (k -> a -> Bool) -> AppendMap k a -> (AppendMap k a, AppendMap k a)
301partitionWithKey f = coerce (Map.partitionWithKey f)
303mapMaybe :: (a -> Maybe b) -> AppendMap k a -> AppendMap k b
304mapMaybe f = coerce (Map.mapMaybe f)
306mapMaybeWithKey :: (k -> a -> Maybe b) -> AppendMap k a -> AppendMap k b
307mapMaybeWithKey f = coerce (Map.mapMaybeWithKey f)
309mapEither :: (a -> Either b c) -> AppendMap k a -> (AppendMap k b, AppendMap k c)
310mapEither f = coerce (Map.mapEither f)
312mapEitherWithKey :: (k -> a -> Either b c) -> AppendMap k a -> (AppendMap k b, AppendMap k c)
313mapEitherWithKey f = coerce (Map.mapEitherWithKey f)
315split :: forall k a. Ord k => k -> AppendMap k a -> (AppendMap k a, AppendMap k a)
316split = coerce (Map.split @k @a)
318splitLookup :: forall k a. Ord k => k -> AppendMap k a -> (AppendMap k a, Maybe a, AppendMap k a)
319splitLookup = coerce (Map.splitLookup @k @a)
321splitRoot :: forall k a. AppendMap k a -> [AppendMap k a]
322splitRoot = coerce (Map.splitRoot @k @a)
324isSubmapOf :: forall k a. (Ord k, Eq a) => AppendMap k a -> AppendMap k a -> Bool
325isSubmapOf = coerce (Map.isSubmapOf @k @a)
327isSubmapOfBy :: Ord k => (a -> b -> Bool) -> AppendMap k a -> AppendMap k b -> Bool
328isSubmapOfBy f = coerce (Map.isSubmapOfBy f)
330isProperSubmapOf :: forall k a. (Ord k, Eq a) => AppendMap k a -> AppendMap k a -> Bool
331isProperSubmapOf = coerce (Map.isProperSubmapOf @k @a)
333isProperSubmapOfBy :: Ord k => (a -> b -> Bool) -> AppendMap k a -> AppendMap k b -> Bool
334isProperSubmapOfBy f = coerce (Map.isProperSubmapOfBy f)
336lookupIndex :: forall k a. Ord k => k -> AppendMap k a -> Maybe Int
337lookupIndex = coerce (Map.lookupIndex @k @a)
339findIndex :: forall k a. Ord k => k -> AppendMap k a -> Int
340findIndex = coerce (Map.findIndex @k @a)
342elemAt :: forall k a. Int -> AppendMap k a -> (k, a)
343elemAt = coerce (Map.elemAt @k @a)
345updateAt :: forall k a. (k -> a -> Maybe a) -> Int -> AppendMap k a -> AppendMap k a
346updateAt = coerce (Map.updateAt @k @a)
348deleteAt :: forall k a. Int -> AppendMap k a -> AppendMap k a
349deleteAt = coerce (Map.deleteAt @k @a)
351findMin :: forall k a. AppendMap k a -> (k, a)
352findMin = coerce (Map.findMin @k @a)
354findMax :: forall k a. AppendMap k a -> (k, a)
355findMax = coerce (Map.findMax @k @a)
357deleteMin :: forall k a. AppendMap k a -> AppendMap k a
358deleteMin = coerce (Map.deleteMin @k @ a)
360deleteMax :: forall k a. AppendMap k a -> AppendMap k a
361deleteMax = coerce (Map.deleteMax @k @a)
363deleteFindMin :: forall k a. AppendMap k a -> ((k, a), AppendMap k a)
364deleteFindMin = coerce (Map.deleteFindMin @k @a)
366deleteFindMax :: forall k a. AppendMap k a -> ((k, a), AppendMap k a)
367deleteFindMax = coerce (Map.deleteFindMax @k @a)
369updateMin :: (a -> Maybe a) -> AppendMap k a -> AppendMap k a
370updateMin f = coerce (Map.updateMin f)
372updateMax :: (a -> Maybe a) -> AppendMap k a -> AppendMap k a
373updateMax f = coerce (Map.updateMax f)
375updateMinWithKey :: (k -> a -> Maybe a) -> AppendMap k a -> AppendMap k a
376updateMinWithKey f = coerce (Map.updateMinWithKey f)
378updateMaxWithKey :: (k -> a -> Maybe a) -> AppendMap k a -> AppendMap k a
379updateMaxWithKey f = coerce (Map.updateMaxWithKey f)
381minView :: forall k a. AppendMap k a -> Maybe (a, AppendMap k a)
382minView = coerce (Map.minView @k @a)
384maxView :: forall k a. AppendMap k a -> Maybe (a, AppendMap k a)
385maxView = coerce (Map.maxView @k @a)
387minViewWithKey :: forall k a. AppendMap k a -> Maybe ((k, a), AppendMap k a)
388minViewWithKey = coerce (Map.minViewWithKey @k @a)
390maxViewWithKey :: forall k a. AppendMap k a -> Maybe ((k, a), AppendMap k a)
391maxViewWithKey = coerce (Map.maxViewWithKey @k @a)
393showTree :: forall k a. (Show k, Show a) => AppendMap k a -> String
394showTree = coerce (Map.showTree @k @a)
396showTreeWith :: (k -> a -> String) -> Bool -> Bool -> AppendMap k a -> String
397showTreeWith f = coerce (Map.showTreeWith f)
399valid :: forall k a. Ord k => AppendMap k a -> Bool
400valid = coerce (Map.valid @k @a)
402instance Default (AppendMap k a) where
403 def = empty
405instance AppendMap k' v' ~ t => Rewrapped (AppendMap k a) t
407instance Wrapped (AppendMap k a) where
408 type Unwrapped (AppendMap k a) = Map k a
409 _Wrapped' = iso (\(AppendMap m) -> m) AppendMap