@@ -4,8 +4,10 @@
4{-# LANGUAGE GADTs #-}
4{-# LANGUAGE GADTs #-}
5{-# LANGUAGE KindSignatures #-}
5{-# LANGUAGE KindSignatures #-}
6{-# LANGUAGE MagicHash #-}
6{-# LANGUAGE MagicHash #-}
7−{-# LANGUAGE TypeInType #-}
7+{-# LANGUAGE PolyKinds #-}
8+{-# LANGUAGE Rank2Types #-}
8{-# LANGUAGE TypeFamilies #-}
9{-# LANGUAGE TypeFamilies #-}
10+{-# LANGUAGE TypeInType #-}
10-- {-# OPTIONS_GHC -ddump-simpl -dsuppress-idinfo -dsuppress-coercions -dsuppress-type-applications -dsuppress-uniques -dsuppress-module-prefixes #-}
12-- {-# OPTIONS_GHC -ddump-simpl -dsuppress-idinfo -dsuppress-coercions -dsuppress-type-applications -dsuppress-uniques -dsuppress-module-prefixes #-}
@@ -18,6 +20,7 @@ module Data.TypeRep.CacheMap
@@ -66,8 +69,6 @@ toFps TypeRepMap{..} = zipWith Fingerprint
66 (Unboxed.toList fingerprintAs)
69 (Unboxed.toList fingerprintAs)
67 (Unboxed.toList fingerprintBs)
70 (Unboxed.toList fingerprintBs)
70−fromAny = unsafeCoerce
72-- | Empty structure.
73-- | Empty structure.
73empty :: TypeRepMap f
74empty :: TypeRepMap f
@@ -83,7 +84,7 @@ insert :: forall a f . Typeable a => f a -> TypeRepMap f -> TypeRepMap f
83insert x = fromListPairs . addX . toPairList
84insert x = fromListPairs . addX . toPairList
85 pairX :: (Fingerprint, Any)
86 pairX :: (Fingerprint, Any)
86− pairX@(fpX, _) = (calcFp x, unsafeCoerce x)
87+ pairX@(fpX, _) = (calcFp x, toAny x)
88 addX :: [(Fingerprint, Any)] -> [(Fingerprint, Any)]
89 addX :: [(Fingerprint, Any)] -> [(Fingerprint, Any)]
89 addX l = pairX : deleteByFst fpX l
90 addX l = pairX : deleteByFst fpX l
@@ -106,6 +107,22 @@ delete :: forall a (f :: KindOf a -> Type) . Typeable a => TypeRepMap f -> TypeR
106delete = fromListPairs . deleteByFst (typeFp @a) . toPairList
107delete = fromListPairs . deleteByFst (typeFp @a) . toPairList
107{-# INLINE delete #-}
108{-# INLINE delete #-}
110+{- | Map over elements.
112+>>> let trMap = insert (Identity True) $ one (Identity 'a')
113+>>> lookup @Bool trMap
115+>>> lookup @Char trMap
117+>>> let trMap2 = hoist ((:[]) . runIdentity) trMap
118+>>> lookup @Bool trMap2
120+>>> lookup @Char trMap2
123+hoist :: (forall x. f x -> g x) -> TypeRepMap f -> TypeRepMap g
124+hoist f (TypeRepMap as bs ans) = TypeRepMap as bs $ V.map (toAny . f . fromAny) ans
109{- | Returns 'True' if there exist value of given type.
126{- | Returns 'True' if there exist value of given type.
111>>> member @Char $ one (Identity 'a')
128>>> member @Char $ one (Identity 'a')
@@ -166,6 +183,12 @@ cachedBinarySearch (Fingerprint (W64# a) (W64# b)) fpAs fpBs = inline (go 0#)
166-- Internal functions
183-- Internal functions
167----------------------------------------------------------------------------
184----------------------------------------------------------------------------
189+fromAny :: Any -> f a
190+fromAny = unsafeCoerce
169typeFp :: forall a . Typeable a => Fingerprint
192typeFp :: forall a . Typeable a => Fingerprint
170typeFp = typeRepFingerprint $ typeRep $ Proxy @a
193typeFp = typeRepFingerprint $ typeRep $ Proxy @a
171{-# INLINE typeFp #-}
194{-# INLINE typeFp #-}
@@ -204,7 +227,7 @@ fromList = fromListPairs . map (fp &&& an)
204 fp (TF x) = calcFp x
227 fp (TF x) = calcFp x
206 an :: TF f -> Any
229 an :: TF f -> Any
207− an (TF x) = unsafeCoerce x
230+ an (TF x) = toAny x
209fromF :: Typeable a => f a -> Proxy a
232fromF :: Typeable a => f a -> Proxy a
210fromF _ = Proxy
233fromF _ = Proxy