Skip to content

Commit 6deffbc

Browse files
committed
Add hoistWithKeyHKA
1 parent 13b29eb commit 6deffbc

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

rec/rec.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ library
2525
build-depends: base >=4.12 && < 5
2626
, record-hasfield >= 1
2727
, typerep-map >= 0.3.3.0
28+
, primitive
2829
hs-source-dirs: src
2930
default-language: Haskell2010
3031

rec/src/Record.hs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
9091
import Data.TMap (TMap)
9192
import qualified Data.TMap as TMap
9293
import qualified Data.TypeRepMap as TRMap
94+
import qualified Data.TypeRepMap.Internal as TRMapInt
9395
import Data.Typeable
96+
import qualified Type.Reflection as TyRefl
9497
import GHC.Generics
9598
import Record.Internal
9699
import Data.Functor.Identity
97100
import 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

100107
newtype 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 -
278285
hoistHKA 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+
281302
hkToListWith :: forall r f t. (forall a. Typeable a => f a -> r) -> HK f t -> [r]
282303
hkToListWith f (HK trmap) = TRMap.toListWith (\(HKField fa) -> f fa) trmap
283304

0 commit comments

Comments
 (0)