@@ -40,6 +40,7 @@ module Record
4040 , hoistWithKeyHK
4141 , hoistWithKeyAndTagHK
4242 , hoistHKA
43+ , hoistWithKeyHKA
4344 , hkToListWith
4445 , hkToListWithTag
4546 , toHKOfSub
@@ -90,11 +91,17 @@ import Data.TypeRepMap (TypeRepMap)
9091import Data.TMap (TMap )
9192import qualified Data.TMap as TMap
9293import qualified Data.TypeRepMap as TRMap
94+ import qualified Data.TypeRepMap.Internal as TRMapInt
9395import Data.Typeable
96+ import qualified Type.Reflection as TyRefl
9497import GHC.Generics
9598import Record.Internal
9699import Data.Functor.Identity
97100import Data.Functor.Const (Const (.. ))
101+ import GHC.IsList
102+ import Control.Monad.Zip (mzip )
103+ import Data.Primitive.Array (mapArray' )
104+ import Unsafe.Coerce
98105
99106
100107newtype Sub t (xs :: [Symbol ]) = Sub TMap
@@ -278,6 +285,20 @@ hoistHKA :: forall f g m t.Applicative m =>(forall a.f a -> m (g a)) -> HK f t -
278285hoistHKA f (HK trmap) = HK <$> TRMap. hoistA (\ (HKField fa) -> HKField <$> (f fa)) trmap
279286{-# INLINE hoistHKA #-}
280287
288+ hoistWithKeyHKA :: forall f g m t . Applicative m => (forall a . Typeable a => f a -> m (g a )) -> HK f t -> m (HK g t )
289+ hoistWithKeyHKA f (HK trmap) = HK <$> hoistWithKeyA' (\ (HKField fa) -> HKField <$> (f fa)) trmap
290+ {-# INLINE hoistWithKeyHKA #-}
291+
292+ hoistWithKeyA' :: forall f g t . (Applicative t ) => (forall x . Typeable x => f x -> t (g x )) -> TypeRepMap f -> t (TypeRepMap g )
293+ hoistWithKeyA' f (TRMapInt. TypeRepMap as bs ans ks) = (\ newAns -> TRMapInt. TypeRepMap as bs (mapArray' TRMapInt. toAny newAns) ks) <$> newAnss
294+ where
295+ newAnss = traverse id $ mapArray' mapAns (mzip ans ks) -- :: t (Array (g x0))
296+ mapAns (a, k) = withTr (unsafeCoerce k) $ TRMapInt. fromAny a
297+
298+ withTr :: forall x . TyRefl. TypeRep x -> f x -> t (g x )
299+ withTr t = TyRefl. withTypeable t f
300+ {-# INLINE hoistWithKeyA' #-}
301+
281302hkToListWith :: forall r f t . (forall a . Typeable a => f a -> r ) -> HK f t -> [r ]
282303hkToListWith f (HK trmap) = TRMap. toListWith (\ (HKField fa) -> f fa) trmap
283304
0 commit comments