@@ -23,6 +23,7 @@ module Data.TypeRepMap.Internal where
24import Prelude hiding (lookup)
24import Prelude hiding (lookup)
26+import Control.Monad.Zip (mzip)
26import Data.Function (on)
27import Data.Function (on)
27import Data.IntMap.Strict (IntMap)
28import Data.IntMap.Strict (IntMap)
28import Data.Kind (Type)
29import Data.Kind (Type)
@@ -36,7 +37,7 @@ import GHC.Exts (IsList (..), inline, sortWith)
36import GHC.Fingerprint (Fingerprint (..))
37import GHC.Fingerprint (Fingerprint (..))
37import GHC.Prim (eqWord#, ltWord#)
38import GHC.Prim (eqWord#, ltWord#)
38import GHC.Word (Word64 (..))
39import GHC.Word (Word64 (..))
39−import Type.Reflection (Typeable, typeRep, withTypeable)
40+import Type.Reflection (TypeRep, Typeable, typeRep, withTypeable)
40import Type.Reflection.Unsafe (typeRepFingerprint)
41import Type.Reflection.Unsafe (typeRepFingerprint)
41import Unsafe.Coerce (unsafeCoerce)
42import Unsafe.Coerce (unsafeCoerce)
@@ -180,6 +181,16 @@ hoistA f (TypeRepMap as bs (toList -> ans) ks) = (\l -> TypeRepMap as bs (fromLi
180 <$> traverse (f . fromAny) ans
181 <$> traverse (f . fromAny) ans
181{-# INLINE hoistA #-}
182{-# INLINE hoistA #-}
184+hoistWithKey :: forall f g. (forall x. Typeable x => f x -> g x) -> TypeRepMap f -> TypeRepMap g
185+hoistWithKey f (TypeRepMap as bs ans ks) = TypeRepMap as bs newAns ks
187+ newAns = mapArray' mapAns (mzip ans ks)
188+ mapAns (a, k) = toAny $ withTr (unsafeCoerce k) $ fromAny a
190+ withTr :: forall x. TypeRep x -> f x -> g x
191+ withTr t = withTypeable t f
192+{-# INLINE hoistWithKey #-}
183-- | The union of two 'TypeRepMap's using a combining function.
194-- | The union of two 'TypeRepMap's using a combining function.
184unionWith :: (forall x. f x -> f x -> f x) -> TypeRepMap f -> TypeRepMap f -> TypeRepMap f
195unionWith :: (forall x. f x -> f x -> f x) -> TypeRepMap f -> TypeRepMap f -> TypeRepMap f
185unionWith f m1 m2 = fromTriples
196unionWith f m1 m2 = fromTriples