{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
module TrieMap(
MaybeMap,
ListMap,
LiteralMap,
TrieMap(..), insertTM, deleteTM,
(>.>), (|>), (|>>), XT,
foldMaybe,
GenMap,
lkG, xtG, mapG, fdG,
xtList, lkList
) where
import GhcPrelude
import Literal
import UniqDFM
import Unique( Unique )
import qualified Data.Map as Map
import qualified Data.IntMap as IntMap
import Outputable
import Control.Monad( (>=>) )
type XT a = Maybe a -> Maybe a
class TrieMap m where
type Key m :: *
emptyTM :: m a
lookupTM :: forall b. Key m -> m b -> Maybe b
alterTM :: forall b. Key m -> XT b -> m b -> m b
mapTM :: (a->b) -> m a -> m b
foldTM :: (a -> b -> b) -> m a -> b -> b
insertTM :: TrieMap m => Key m -> a -> m a -> m a
insertTM :: Key m -> a -> m a -> m a
insertTM k :: Key m
k v :: a
v m :: m a
m = Key m -> XT a -> m a -> m a
forall (m :: * -> *) b. TrieMap m => Key m -> XT b -> m b -> m b
alterTM Key m
k (\_ -> a -> Maybe a
forall a. a -> Maybe a
Just a
v) m a
m
deleteTM :: TrieMap m => Key m -> m a -> m a
deleteTM :: Key m -> m a -> m a
deleteTM k :: Key m
k m :: m a
m = Key m -> XT a -> m a -> m a
forall (m :: * -> *) b. TrieMap m => Key m -> XT b -> m b -> m b
alterTM Key m
k (\_ -> Maybe a
forall a. Maybe a
Nothing) m a
m
(>.>) :: (a -> b) -> (b -> c) -> a -> c
infixr 1 >.>
(f :: a -> b
f >.> :: (a -> b) -> (b -> c) -> a -> c
>.> g :: b -> c
g) x :: a
x = b -> c
g (a -> b
f a
x)
infixr 1 |>, |>>
(|>) :: a -> (a->b) -> b
x :: a
x |> :: a -> (a -> b) -> b
|> f :: a -> b
f = a -> b
f a
x
(|>>) :: TrieMap m2
=> (XT (m2 a) -> m1 (m2 a) -> m1 (m2 a))
-> (m2 a -> m2 a)
-> m1 (m2 a) -> m1 (m2 a)
|>> :: (XT (m2 a) -> m1 (m2 a) -> m1 (m2 a))
-> (m2 a -> m2 a) -> m1 (m2 a) -> m1 (m2 a)
(|>>) f :: XT (m2 a) -> m1 (m2 a) -> m1 (m2 a)
f g :: m2 a -> m2 a
g = XT (m2 a) -> m1 (m2 a) -> m1 (m2 a)
f (m2 a -> Maybe (m2 a)
forall a. a -> Maybe a
Just (m2 a -> Maybe (m2 a)) -> (Maybe (m2 a) -> m2 a) -> XT (m2 a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m2 a -> m2 a
g (m2 a -> m2 a) -> (Maybe (m2 a) -> m2 a) -> Maybe (m2 a) -> m2 a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe (m2 a) -> m2 a
forall (m :: * -> *) a. TrieMap m => Maybe (m a) -> m a
deMaybe)
deMaybe :: TrieMap m => Maybe (m a) -> m a
deMaybe :: Maybe (m a) -> m a
deMaybe Nothing = m a
forall (m :: * -> *) a. TrieMap m => m a
emptyTM
deMaybe (Just m :: m a
m) = m a
m
instance TrieMap IntMap.IntMap where
type Key IntMap.IntMap = Int
emptyTM :: IntMap a
emptyTM = IntMap a
forall a. IntMap a
IntMap.empty
lookupTM :: Key IntMap -> IntMap b -> Maybe b
lookupTM k :: Key IntMap
k m :: IntMap b
m = Key -> IntMap b -> Maybe b
forall a. Key -> IntMap a -> Maybe a
IntMap.lookup Key
Key IntMap
k IntMap b
m
alterTM :: Key IntMap -> XT b -> IntMap b -> IntMap b
alterTM = Key IntMap -> XT b -> IntMap b -> IntMap b
forall a. Key -> XT a -> IntMap a -> IntMap a
xtInt
foldTM :: (a -> b -> b) -> IntMap a -> b -> b
foldTM k :: a -> b -> b
k m :: IntMap a
m z :: b
z = (a -> b -> b) -> b -> IntMap a -> b
forall a b. (a -> b -> b) -> b -> IntMap a -> b
IntMap.foldr a -> b -> b
k b
z IntMap a
m
mapTM :: (a -> b) -> IntMap a -> IntMap b
mapTM f :: a -> b
f m :: IntMap a
m = (a -> b) -> IntMap a -> IntMap b
forall a b. (a -> b) -> IntMap a -> IntMap b
IntMap.map a -> b
f IntMap a
m
xtInt :: Int -> XT a -> IntMap.IntMap a -> IntMap.IntMap a
xtInt :: Key -> XT a -> IntMap a -> IntMap a
xtInt k :: Key
k f :: XT a
f m :: IntMap a
m = XT a -> Key -> IntMap a -> IntMap a
forall a. (Maybe a -> Maybe a) -> Key -> IntMap a -> IntMap a
IntMap.alter XT a
f Key
k IntMap a
m
instance Ord k => TrieMap (Map.Map k) where
type Key (Map.Map k) = k
emptyTM :: Map k a
emptyTM = Map k a
forall k a. Map k a
Map.empty
lookupTM :: Key (Map k) -> Map k b -> Maybe b
lookupTM = Key (Map k) -> Map k b -> Maybe b
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup
alterTM :: Key (Map k) -> XT b -> Map k b -> Map k b
alterTM k :: Key (Map k)
k f :: XT b
f m :: Map k b
m = XT b -> k -> Map k b -> Map k b
forall k a.
Ord k =>
(Maybe a -> Maybe a) -> k -> Map k a -> Map k a
Map.alter XT b
f k
Key (Map k)
k Map k b
m
foldTM :: (a -> b -> b) -> Map k a -> b -> b
foldTM k :: a -> b -> b
k m :: Map k a
m z :: b
z = (a -> b -> b) -> b -> Map k a -> b
forall a b k. (a -> b -> b) -> b -> Map k a -> b
Map.foldr a -> b -> b
k b
z Map k a
m
mapTM :: (a -> b) -> Map k a -> Map k b
mapTM f :: a -> b
f m :: Map k a
m = (a -> b) -> Map k a -> Map k b
forall a b k. (a -> b) -> Map k a -> Map k b
Map.map a -> b
f Map k a
m
instance TrieMap UniqDFM where
type Key UniqDFM = Unique
emptyTM :: UniqDFM a
emptyTM = UniqDFM a
forall a. UniqDFM a
emptyUDFM
lookupTM :: Key UniqDFM -> UniqDFM b -> Maybe b
lookupTM k :: Key UniqDFM
k m :: UniqDFM b
m = UniqDFM b -> Unique -> Maybe b
forall key elt. Uniquable key => UniqDFM elt -> key -> Maybe elt
lookupUDFM UniqDFM b
m Unique
Key UniqDFM
k
alterTM :: Key UniqDFM -> XT b -> UniqDFM b -> UniqDFM b
alterTM k :: Key UniqDFM
k f :: XT b
f m :: UniqDFM b
m = XT b -> UniqDFM b -> Unique -> UniqDFM b
forall key elt.
Uniquable key =>
(Maybe elt -> Maybe elt) -> UniqDFM elt -> key -> UniqDFM elt
alterUDFM XT b
f UniqDFM b
m Unique
Key UniqDFM
k
foldTM :: (a -> b -> b) -> UniqDFM a -> b -> b
foldTM k :: a -> b -> b
k m :: UniqDFM a
m z :: b
z = (a -> b -> b) -> b -> UniqDFM a -> b
forall elt a. (elt -> a -> a) -> a -> UniqDFM elt -> a
foldUDFM a -> b -> b
k b
z UniqDFM a
m
mapTM :: (a -> b) -> UniqDFM a -> UniqDFM b
mapTM f :: a -> b
f m :: UniqDFM a
m = (a -> b) -> UniqDFM a -> UniqDFM b
forall a b. (a -> b) -> UniqDFM a -> UniqDFM b
mapUDFM a -> b
f UniqDFM a
m
data MaybeMap m a = MM { MaybeMap m a -> Maybe a
mm_nothing :: Maybe a, MaybeMap m a -> m a
mm_just :: m a }
instance TrieMap m => TrieMap (MaybeMap m) where
type Key (MaybeMap m) = Maybe (Key m)
emptyTM :: MaybeMap m a
emptyTM = MM :: forall (m :: * -> *) a. Maybe a -> m a -> MaybeMap m a
MM { mm_nothing :: Maybe a
mm_nothing = Maybe a
forall a. Maybe a
Nothing, mm_just :: m a
mm_just = m a
forall (m :: * -> *) a. TrieMap m => m a
emptyTM }
lookupTM :: Key (MaybeMap m) -> MaybeMap m b -> Maybe b
lookupTM = (forall b. Key m -> m b -> Maybe b)
-> Maybe (Key m) -> MaybeMap m b -> Maybe b
forall k (m :: * -> *) a.
(forall b. k -> m b -> Maybe b)
-> Maybe k -> MaybeMap m a -> Maybe a
lkMaybe forall b. Key m -> m b -> Maybe b
forall (m :: * -> *) b. TrieMap m => Key m -> m b -> Maybe b
lookupTM
alterTM :: Key (MaybeMap m) -> XT b -> MaybeMap m b -> MaybeMap m b
alterTM = (forall b. Key m -> XT b -> m b -> m b)
-> Maybe (Key m) -> XT b -> MaybeMap m b -> MaybeMap m b
forall k (m :: * -> *) a.
(forall b. k -> XT b -> m b -> m b)
-> Maybe k -> XT a -> MaybeMap m a -> MaybeMap m a
xtMaybe forall b. Key m -> XT b -> m b -> m b
forall (m :: * -> *) b. TrieMap m => Key m -> XT b -> m b -> m b
alterTM
foldTM :: (a -> b -> b) -> MaybeMap m a -> b -> b
foldTM = (a -> b -> b) -> MaybeMap m a -> b -> b
forall (m :: * -> *) a b.
TrieMap m =>
(a -> b -> b) -> MaybeMap m a -> b -> b
fdMaybe
mapTM :: (a -> b) -> MaybeMap m a -> MaybeMap m b
mapTM = (a -> b) -> MaybeMap m a -> MaybeMap m b
forall (m :: * -> *) a b.
TrieMap m =>
(a -> b) -> MaybeMap m a -> MaybeMap m b
mapMb
mapMb :: TrieMap m => (a->b) -> MaybeMap m a -> MaybeMap m b
mapMb :: (a -> b) -> MaybeMap m a -> MaybeMap m b
mapMb f :: a -> b
f (MM { mm_nothing :: forall (m :: * -> *) a. MaybeMap m a -> Maybe a
mm_nothing = Maybe a
mn, mm_just :: forall (m :: * -> *) a. MaybeMap m a -> m a
mm_just = m a
mj })
= MM :: forall (m :: * -> *) a. Maybe a -> m a -> MaybeMap m a
MM { mm_nothing :: Maybe b
mm_nothing = (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f Maybe a
mn, mm_just :: m b
mm_just = (a -> b) -> m a -> m b
forall (m :: * -> *) a b. TrieMap m => (a -> b) -> m a -> m b
mapTM a -> b
f m a
mj }
lkMaybe :: (forall b. k -> m b -> Maybe b)
-> Maybe k -> MaybeMap m a -> Maybe a
lkMaybe :: (forall b. k -> m b -> Maybe b)
-> Maybe k -> MaybeMap m a -> Maybe a
lkMaybe _ Nothing = MaybeMap m a -> Maybe a
forall (m :: * -> *) a. MaybeMap m a -> Maybe a
mm_nothing
lkMaybe lk :: forall b. k -> m b -> Maybe b
lk (Just x :: k
x) = MaybeMap m a -> m a
forall (m :: * -> *) a. MaybeMap m a -> m a
mm_just (MaybeMap m a -> m a)
-> (m a -> Maybe a) -> MaybeMap m a -> Maybe a
forall a b c. (a -> b) -> (b -> c) -> a -> c
>.> k -> m a -> Maybe a
forall b. k -> m b -> Maybe b
lk k
x
xtMaybe :: (forall b. k -> XT b -> m b -> m b)
-> Maybe k -> XT a -> MaybeMap m a -> MaybeMap m a
xtMaybe :: (forall b. k -> XT b -> m b -> m b)
-> Maybe k -> XT a -> MaybeMap m a -> MaybeMap m a
xtMaybe _ Nothing f :: XT a
f m :: MaybeMap m a
m = MaybeMap m a
m { mm_nothing :: Maybe a
mm_nothing = XT a
f (MaybeMap m a -> Maybe a
forall (m :: * -> *) a. MaybeMap m a -> Maybe a
mm_nothing MaybeMap m a
m) }
xtMaybe tr :: forall b. k -> XT b -> m b -> m b
tr (Just x :: k
x) f :: XT a
f m :: MaybeMap m a
m = MaybeMap m a
m { mm_just :: m a
mm_just = MaybeMap m a -> m a
forall (m :: * -> *) a. MaybeMap m a -> m a
mm_just MaybeMap m a
m m a -> (m a -> m a) -> m a
forall a b. a -> (a -> b) -> b
|> k -> XT a -> m a -> m a
forall b. k -> XT b -> m b -> m b
tr k
x XT a
f }
fdMaybe :: TrieMap m => (a -> b -> b) -> MaybeMap m a -> b -> b
fdMaybe :: (a -> b -> b) -> MaybeMap m a -> b -> b
fdMaybe k :: a -> b -> b
k m :: MaybeMap m a
m = (a -> b -> b) -> Maybe a -> b -> b
forall a b. (a -> b -> b) -> Maybe a -> b -> b
foldMaybe a -> b -> b
k (MaybeMap m a -> Maybe a
forall (m :: * -> *) a. MaybeMap m a -> Maybe a
mm_nothing MaybeMap m a
m)
(b -> b) -> (b -> b) -> b -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b -> b) -> m a -> b -> b
forall (m :: * -> *) a b.
TrieMap m =>
(a -> b -> b) -> m a -> b -> b
foldTM a -> b -> b
k (MaybeMap m a -> m a
forall (m :: * -> *) a. MaybeMap m a -> m a
mm_just MaybeMap m a
m)
data ListMap m a
= LM { ListMap m a -> Maybe a
lm_nil :: Maybe a
, ListMap m a -> m (ListMap m a)
lm_cons :: m (ListMap m a) }
instance TrieMap m => TrieMap (ListMap m) where
type Key (ListMap m) = [Key m]
emptyTM :: ListMap m a
emptyTM = LM :: forall (m :: * -> *) a. Maybe a -> m (ListMap m a) -> ListMap m a
LM { lm_nil :: Maybe a
lm_nil = Maybe a
forall a. Maybe a
Nothing, lm_cons :: m (ListMap m a)
lm_cons = m (ListMap m a)
forall (m :: * -> *) a. TrieMap m => m a
emptyTM }
lookupTM :: Key (ListMap m) -> ListMap m b -> Maybe b
lookupTM = (forall b. Key m -> m b -> Maybe b)
-> [Key m] -> ListMap m b -> Maybe b
forall (m :: * -> *) k a.
TrieMap m =>
(forall b. k -> m b -> Maybe b) -> [k] -> ListMap m a -> Maybe a
lkList forall b. Key m -> m b -> Maybe b
forall (m :: * -> *) b. TrieMap m => Key m -> m b -> Maybe b
lookupTM
alterTM :: Key (ListMap m) -> XT b -> ListMap m b -> ListMap m b
alterTM = (forall b. Key m -> XT b -> m b -> m b)
-> [Key m] -> XT b -> ListMap m b -> ListMap m b
forall (m :: * -> *) k a.
TrieMap m =>
(forall b. k -> XT b -> m b -> m b)
-> [k] -> XT a -> ListMap m a -> ListMap m a
xtList forall b. Key m -> XT b -> m b -> m b
forall (m :: * -> *) b. TrieMap m => Key m -> XT b -> m b -> m b
alterTM
foldTM :: (a -> b -> b) -> ListMap m a -> b -> b
foldTM = (a -> b -> b) -> ListMap m a -> b -> b
forall (m :: * -> *) a b.
TrieMap m =>
(a -> b -> b) -> ListMap m a -> b -> b
fdList
mapTM :: (a -> b) -> ListMap m a -> ListMap m b
mapTM = (a -> b) -> ListMap m a -> ListMap m b
forall (m :: * -> *) a b.
TrieMap m =>
(a -> b) -> ListMap m a -> ListMap m b
mapList
instance (TrieMap m, Outputable a) => Outputable (ListMap m a) where
ppr :: ListMap m a -> SDoc
ppr m :: ListMap m a
m = String -> SDoc
text "List elts" SDoc -> SDoc -> SDoc
<+> [a] -> SDoc
forall a. Outputable a => a -> SDoc
ppr ((a -> [a] -> [a]) -> ListMap m a -> [a] -> [a]
forall (m :: * -> *) a b.
TrieMap m =>
(a -> b -> b) -> m a -> b -> b
foldTM (:) ListMap m a
m [])
mapList :: TrieMap m => (a->b) -> ListMap m a -> ListMap m b
mapList :: (a -> b) -> ListMap m a -> ListMap m b
mapList f :: a -> b
f (LM { lm_nil :: forall (m :: * -> *) a. ListMap m a -> Maybe a
lm_nil = Maybe a
mnil, lm_cons :: forall (m :: * -> *) a. ListMap m a -> m (ListMap m a)
lm_cons = m (ListMap m a)
mcons })
= LM :: forall (m :: * -> *) a. Maybe a -> m (ListMap m a) -> ListMap m a
LM { lm_nil :: Maybe b
lm_nil = (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f Maybe a
mnil, lm_cons :: m (ListMap m b)
lm_cons = (ListMap m a -> ListMap m b) -> m (ListMap m a) -> m (ListMap m b)
forall (m :: * -> *) a b. TrieMap m => (a -> b) -> m a -> m b
mapTM ((a -> b) -> ListMap m a -> ListMap m b
forall (m :: * -> *) a b. TrieMap m => (a -> b) -> m a -> m b
mapTM a -> b
f) m (ListMap m a)
mcons }
lkList :: TrieMap m => (forall b. k -> m b -> Maybe b)
-> [k] -> ListMap m a -> Maybe a
lkList :: (forall b. k -> m b -> Maybe b) -> [k] -> ListMap m a -> Maybe a
lkList _ [] = ListMap m a -> Maybe a
forall (m :: * -> *) a. ListMap m a -> Maybe a
lm_nil
lkList lk :: forall b. k -> m b -> Maybe b
lk (x :: k
x:xs :: [k]
xs) = ListMap m a -> m (ListMap m a)
forall (m :: * -> *) a. ListMap m a -> m (ListMap m a)
lm_cons (ListMap m a -> m (ListMap m a))
-> (m (ListMap m a) -> Maybe a) -> ListMap m a -> Maybe a
forall a b c. (a -> b) -> (b -> c) -> a -> c
>.> k -> m (ListMap m a) -> Maybe (ListMap m a)
forall b. k -> m b -> Maybe b
lk k
x (m (ListMap m a) -> Maybe (ListMap m a))
-> (ListMap m a -> Maybe a) -> m (ListMap m a) -> Maybe a
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> (forall b. k -> m b -> Maybe b) -> [k] -> ListMap m a -> Maybe a
forall (m :: * -> *) k a.
TrieMap m =>
(forall b. k -> m b -> Maybe b) -> [k] -> ListMap m a -> Maybe a
lkList forall b. k -> m b -> Maybe b
lk [k]
xs
xtList :: TrieMap m => (forall b. k -> XT b -> m b -> m b)
-> [k] -> XT a -> ListMap m a -> ListMap m a
xtList :: (forall b. k -> XT b -> m b -> m b)
-> [k] -> XT a -> ListMap m a -> ListMap m a
xtList _ [] f :: XT a
f m :: ListMap m a
m = ListMap m a
m { lm_nil :: Maybe a
lm_nil = XT a
f (ListMap m a -> Maybe a
forall (m :: * -> *) a. ListMap m a -> Maybe a
lm_nil ListMap m a
m) }
xtList tr :: forall b. k -> XT b -> m b -> m b
tr (x :: k
x:xs :: [k]
xs) f :: XT a
f m :: ListMap m a
m = ListMap m a
m { lm_cons :: m (ListMap m a)
lm_cons = ListMap m a -> m (ListMap m a)
forall (m :: * -> *) a. ListMap m a -> m (ListMap m a)
lm_cons ListMap m a
m m (ListMap m a)
-> (m (ListMap m a) -> m (ListMap m a)) -> m (ListMap m a)
forall a b. a -> (a -> b) -> b
|> k -> XT (ListMap m a) -> m (ListMap m a) -> m (ListMap m a)
forall b. k -> XT b -> m b -> m b
tr k
x (XT (ListMap m a) -> m (ListMap m a) -> m (ListMap m a))
-> (ListMap m a -> ListMap m a)
-> m (ListMap m a)
-> m (ListMap m a)
forall (m2 :: * -> *) a (m1 :: * -> *).
TrieMap m2 =>
(XT (m2 a) -> m1 (m2 a) -> m1 (m2 a))
-> (m2 a -> m2 a) -> m1 (m2 a) -> m1 (m2 a)
|>> (forall b. k -> XT b -> m b -> m b)
-> [k] -> XT a -> ListMap m a -> ListMap m a
forall (m :: * -> *) k a.
TrieMap m =>
(forall b. k -> XT b -> m b -> m b)
-> [k] -> XT a -> ListMap m a -> ListMap m a
xtList forall b. k -> XT b -> m b -> m b
tr [k]
xs XT a
f }
fdList :: forall m a b. TrieMap m
=> (a -> b -> b) -> ListMap m a -> b -> b
fdList :: (a -> b -> b) -> ListMap m a -> b -> b
fdList k :: a -> b -> b
k m :: ListMap m a
m = (a -> b -> b) -> Maybe a -> b -> b
forall a b. (a -> b -> b) -> Maybe a -> b -> b
foldMaybe a -> b -> b
k (ListMap m a -> Maybe a
forall (m :: * -> *) a. ListMap m a -> Maybe a
lm_nil ListMap m a
m)
(b -> b) -> (b -> b) -> b -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ListMap m a -> b -> b) -> m (ListMap m a) -> b -> b
forall (m :: * -> *) a b.
TrieMap m =>
(a -> b -> b) -> m a -> b -> b
foldTM ((a -> b -> b) -> ListMap m a -> b -> b
forall (m :: * -> *) a b.
TrieMap m =>
(a -> b -> b) -> ListMap m a -> b -> b
fdList a -> b -> b
k) (ListMap m a -> m (ListMap m a)
forall (m :: * -> *) a. ListMap m a -> m (ListMap m a)
lm_cons ListMap m a
m)
foldMaybe :: (a -> b -> b) -> Maybe a -> b -> b
foldMaybe :: (a -> b -> b) -> Maybe a -> b -> b
foldMaybe _ Nothing b :: b
b = b
b
foldMaybe k :: a -> b -> b
k (Just a :: a
a) b :: b
b = a -> b -> b
k a
a b
b
type LiteralMap a = Map.Map Literal a
data GenMap m a
= EmptyMap
| SingletonMap (Key m) a
| MultiMap (m a)
instance (Outputable a, Outputable (m a)) => Outputable (GenMap m a) where
ppr :: GenMap m a -> SDoc
ppr EmptyMap = String -> SDoc
text "Empty map"
ppr (SingletonMap _ v :: a
v) = String -> SDoc
text "Singleton map" SDoc -> SDoc -> SDoc
<+> a -> SDoc
forall a. Outputable a => a -> SDoc
ppr a
v
ppr (MultiMap m :: m a
m) = m a -> SDoc
forall a. Outputable a => a -> SDoc
ppr m a
m
instance (Eq (Key m), TrieMap m) => TrieMap (GenMap m) where
type Key (GenMap m) = Key m
emptyTM :: GenMap m a
emptyTM = GenMap m a
forall (m :: * -> *) a. GenMap m a
EmptyMap
lookupTM :: Key (GenMap m) -> GenMap m b -> Maybe b
lookupTM = Key (GenMap m) -> GenMap m b -> Maybe b
forall (m :: * -> *) a.
(Eq (Key m), TrieMap m) =>
Key m -> GenMap m a -> Maybe a
lkG
alterTM :: Key (GenMap m) -> XT b -> GenMap m b -> GenMap m b
alterTM = Key (GenMap m) -> XT b -> GenMap m b -> GenMap m b
forall (m :: * -> *) a.
(Eq (Key m), TrieMap m) =>
Key m -> XT a -> GenMap m a -> GenMap m a
xtG
foldTM :: (a -> b -> b) -> GenMap m a -> b -> b
foldTM = (a -> b -> b) -> GenMap m a -> b -> b
forall (m :: * -> *) a b.
TrieMap m =>
(a -> b -> b) -> GenMap m a -> b -> b
fdG
mapTM :: (a -> b) -> GenMap m a -> GenMap m b
mapTM = (a -> b) -> GenMap m a -> GenMap m b
forall (m :: * -> *) a b.
TrieMap m =>
(a -> b) -> GenMap m a -> GenMap m b
mapG
{-# INLINEABLE lkG #-}
lkG :: (Eq (Key m), TrieMap m) => Key m -> GenMap m a -> Maybe a
lkG :: Key m -> GenMap m a -> Maybe a
lkG _ EmptyMap = Maybe a
forall a. Maybe a
Nothing
lkG k :: Key m
k (SingletonMap k' :: Key m
k' v' :: a
v') | Key m
k Key m -> Key m -> Bool
forall a. Eq a => a -> a -> Bool
== Key m
k' = a -> Maybe a
forall a. a -> Maybe a
Just a
v'
| Bool
otherwise = Maybe a
forall a. Maybe a
Nothing
lkG k :: Key m
k (MultiMap m :: m a
m) = Key m -> m a -> Maybe a
forall (m :: * -> *) b. TrieMap m => Key m -> m b -> Maybe b
lookupTM Key m
k m a
m
{-# INLINEABLE xtG #-}
xtG :: (Eq (Key m), TrieMap m) => Key m -> XT a -> GenMap m a -> GenMap m a
xtG :: Key m -> XT a -> GenMap m a -> GenMap m a
xtG k :: Key m
k f :: XT a
f EmptyMap
= case XT a
f Maybe a
forall a. Maybe a
Nothing of
Just v :: a
v -> Key m -> a -> GenMap m a
forall (m :: * -> *) a. Key m -> a -> GenMap m a
SingletonMap Key m
k a
v
Nothing -> GenMap m a
forall (m :: * -> *) a. GenMap m a
EmptyMap
xtG k :: Key m
k f :: XT a
f m :: GenMap m a
m@(SingletonMap k' :: Key m
k' v' :: a
v')
| Key m
k' Key m -> Key m -> Bool
forall a. Eq a => a -> a -> Bool
== Key m
k
= case XT a
f (a -> Maybe a
forall a. a -> Maybe a
Just a
v') of
Just v'' :: a
v'' -> Key m -> a -> GenMap m a
forall (m :: * -> *) a. Key m -> a -> GenMap m a
SingletonMap Key m
k' a
v''
Nothing -> GenMap m a
forall (m :: * -> *) a. GenMap m a
EmptyMap
| Bool
otherwise
= case XT a
f Maybe a
forall a. Maybe a
Nothing of
Nothing -> GenMap m a
m
Just v :: a
v -> m a
forall (m :: * -> *) a. TrieMap m => m a
emptyTM m a -> (m a -> GenMap m a) -> GenMap m a
forall a b. a -> (a -> b) -> b
|> Key m -> XT a -> m a -> m a
forall (m :: * -> *) b. TrieMap m => Key m -> XT b -> m b -> m b
alterTM Key m
k' (Maybe a -> XT a
forall a b. a -> b -> a
const (a -> Maybe a
forall a. a -> Maybe a
Just a
v'))
(m a -> m a) -> (m a -> GenMap m a) -> m a -> GenMap m a
forall a b c. (a -> b) -> (b -> c) -> a -> c
>.> Key m -> XT a -> m a -> m a
forall (m :: * -> *) b. TrieMap m => Key m -> XT b -> m b -> m b
alterTM Key m
k (Maybe a -> XT a
forall a b. a -> b -> a
const (a -> Maybe a
forall a. a -> Maybe a
Just a
v))
(m a -> m a) -> (m a -> GenMap m a) -> m a -> GenMap m a
forall a b c. (a -> b) -> (b -> c) -> a -> c
>.> m a -> GenMap m a
forall (m :: * -> *) a. m a -> GenMap m a
MultiMap
xtG k :: Key m
k f :: XT a
f (MultiMap m :: m a
m) = m a -> GenMap m a
forall (m :: * -> *) a. m a -> GenMap m a
MultiMap (Key m -> XT a -> m a -> m a
forall (m :: * -> *) b. TrieMap m => Key m -> XT b -> m b -> m b
alterTM Key m
k XT a
f m a
m)
{-# INLINEABLE mapG #-}
mapG :: TrieMap m => (a -> b) -> GenMap m a -> GenMap m b
mapG :: (a -> b) -> GenMap m a -> GenMap m b
mapG _ EmptyMap = GenMap m b
forall (m :: * -> *) a. GenMap m a
EmptyMap
mapG f :: a -> b
f (SingletonMap k :: Key m
k v :: a
v) = Key m -> b -> GenMap m b
forall (m :: * -> *) a. Key m -> a -> GenMap m a
SingletonMap Key m
k (a -> b
f a
v)
mapG f :: a -> b
f (MultiMap m :: m a
m) = m b -> GenMap m b
forall (m :: * -> *) a. m a -> GenMap m a
MultiMap ((a -> b) -> m a -> m b
forall (m :: * -> *) a b. TrieMap m => (a -> b) -> m a -> m b
mapTM a -> b
f m a
m)
{-# INLINEABLE fdG #-}
fdG :: TrieMap m => (a -> b -> b) -> GenMap m a -> b -> b
fdG :: (a -> b -> b) -> GenMap m a -> b -> b
fdG _ EmptyMap = \z :: b
z -> b
z
fdG k :: a -> b -> b
k (SingletonMap _ v :: a
v) = \z :: b
z -> a -> b -> b
k a
v b
z
fdG k :: a -> b -> b
k (MultiMap m :: m a
m) = (a -> b -> b) -> m a -> b -> b
forall (m :: * -> *) a b.
TrieMap m =>
(a -> b -> b) -> m a -> b -> b
foldTM a -> b -> b
k m a
m