@@ -21,6 +21,8 @@ module Data.TypeRep.CacheMap
@@ -51,6 +53,7 @@ import GHC.Word (Word64 (..))
51import Unsafe.Coerce (unsafeCoerce)
53import Unsafe.Coerce (unsafeCoerce)
53import qualified Data.IntMap.Strict as IM
55import qualified Data.IntMap.Strict as IM
56+import qualified Data.Map.Strict as Map
54import qualified GHC.Exts as GHC (fromList, toList)
57import qualified GHC.Exts as GHC (fromList, toList)
56-- | Map-like data structure that keeps types as keys.
59-- | Map-like data structure that keeps types as keys.
@@ -123,6 +126,25 @@ Just "a"
124hoist :: (forall x. f x -> g x) -> TypeRepMap f -> TypeRepMap g
127hoist :: (forall x. f x -> g x) -> TypeRepMap f -> TypeRepMap g
125hoist f (TypeRepMap as bs ans) = TypeRepMap as bs $ mapArray' (toAny . f . fromAny) ans
128hoist f (TypeRepMap as bs ans) = TypeRepMap as bs $ mapArray' (toAny . f . fromAny) ans
131+-- | The union with a combining function.
132+unionWith :: (forall x. f x -> f x -> f x) -> TypeRepMap f -> TypeRepMap f -> TypeRepMap f
133+unionWith f m1 m2 = fromListPairs
135+ $ Map.unionWith combine
136+ (Map.fromList $ toPairList m1)
137+ (Map.fromList $ toPairList m2)
139+ combine :: Any -> Any -> Any
140+ combine a b = toAny $ f (fromAny a) (fromAny b)
141+{-# INLINE unionWith #-}
143+-- | The (left-biased) union of two maps. It prefers the first map when
144+-- duplicate keys are encountered, i.e. @'union' == 'unionWith' const@.
145+union :: TypeRepMap f -> TypeRepMap f -> TypeRepMap f
146+union = unionWith const
127{- | Returns 'True' if there exist value of given type.
149{- | Returns 'True' if there exist value of given type.