3
module Agda.Utils.Map where
5
import Prelude hiding (map, lookup, mapM)
6
import Control.Applicative
8
import Data.Traversable
9
import Agda.Utils.Monad
11
#include "../undefined.h"
12
import Agda.Utils.Impossible
14
data EitherOrBoth a b = L a | B a b | R b
16
-- | Not very efficient (goes via a list), but it'll do.
17
unionWithM :: (Ord k, Functor m, Monad m) => (a -> a -> m a) -> Map k a -> Map k a -> m (Map k a)
18
unionWithM f m1 m2 = fromList <$> mapM combine (toList m)
20
m = unionWith both (map L m1) (map R m2)
22
both (L a) (R b) = B a b
23
both _ _ = __IMPOSSIBLE__
25
combine (k, B a b) = (,) k <$> f a b
26
combine (k, L a) = return (k, a)
27
combine (k, R b) = return (k, b)
29
insertWithKeyM :: (Ord k, Monad m) => (k -> a -> a -> m a) -> k -> a -> Map k a -> m (Map k a)
30
insertWithKeyM clash k x m =
35
Nothing -> return $ insert k x m
37
-- | Filter a map based on the keys.
38
filterKeys :: Ord k => (k -> Bool) -> Map k a -> Map k a
39
filterKeys p = filterWithKey (const . p)