~ubuntu-branches/ubuntu/raring/agda/raring-proposed

« back to all changes in this revision

Viewing changes to src/full/Agda/Utils/Map.hs

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane
  • Date: 2009-07-20 19:49:41 UTC
  • Revision ID: james.westby@ubuntu.com-20090720194941-hcjy91vrn16csh7d
Tags: upstream-2.2.4+dfsg
ImportĀ upstreamĀ versionĀ 2.2.4+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
{-# LANGUAGE CPP #-}
 
2
 
 
3
module Agda.Utils.Map where
 
4
 
 
5
import Prelude hiding (map, lookup, mapM)
 
6
import Control.Applicative
 
7
import Data.Map
 
8
import Data.Traversable
 
9
import Agda.Utils.Monad
 
10
 
 
11
#include "../undefined.h"
 
12
import Agda.Utils.Impossible
 
13
 
 
14
data EitherOrBoth a b = L a | B a b | R b
 
15
 
 
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)
 
19
    where
 
20
        m = unionWith both (map L m1) (map R m2)
 
21
 
 
22
        both (L a) (R b) = B a b
 
23
        both _     _     = __IMPOSSIBLE__
 
24
 
 
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)
 
28
 
 
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 =
 
31
    case lookup k m of
 
32
        Just y  -> do
 
33
            z <- clash k x y
 
34
            return $ insert k z m
 
35
        Nothing -> return $ insert k x m
 
36
 
 
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)
 
40