@@ -23,6 +23,7 @@ module Data.TypeRepMap.Internal where
2323
2424import Prelude hiding (lookup)
2525
26+import Control.Monad.Zip (mzip)
2627import Data.Function (on)
2728import Data.IntMap.Strict (IntMap)
2829import Data.Kind (Type)
@@ -36,7 +37,7 @@ import GHC.Exts (IsList (..), inline, sortWith)
3637import GHC.Fingerprint (Fingerprint (..))
3738import GHC.Prim (eqWord#, ltWord#)
3839import GHC.Word (Word64 (..))
39−import Type.Reflection (Typeable, typeRep, withTypeable)
40+import Type.Reflection (TypeRep, Typeable, typeRep, withTypeable)
4041import Type.Reflection.Unsafe (typeRepFingerprint)
4142import Unsafe.Coerce (unsafeCoerce)
4243
@@ -180,6 +181,16 @@ hoistA f (TypeRepMap as bs (toList -> ans) ks) = (\l -> TypeRepMap as bs (fromLi
180181 <$> traverse (f . fromAny) ans
181182{-# INLINE hoistA #-}
182183
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
186+ where
187+ newAns = mapArray' mapAns (mzip ans ks)
188+ mapAns (a, k) = toAny $ withTr (unsafeCoerce k) $ fromAny a
189+
190+ withTr :: forall x. TypeRep x -> f x -> g x
191+ withTr t = withTypeable t f
192+{-# INLINE hoistWithKey #-}
193+
183194-- | The union of two 'TypeRepMap's using a combining function.
184195unionWith :: (forall x. f x -> f x -> f x) -> TypeRepMap f -> TypeRepMap f -> TypeRepMap f
185196unionWith f m1 m2 = fromTriples