I've found out today that ghc (since version 7.8) implements dictionary reification for type literals with the help of some compiler magic. For example (see GHC.TypeLits):
newtype SSymbol (s :: Symbol) = SSymbol String
data WrapS a b = WrapS (KnownSymbol a => Proxy a -> b)
-- See Note [magicDictId magic] in "basicType/MkId.hs"
withSSymbol :: (KnownSymbol a => Proxy a -> b)
-> SSymbol a -> Proxy a -> b
withSSymbol f x y = magicDict (WrapS f) x y
In a nutshell, the compiler magic is that there's a builtin rewrite rule that turns magicDict (WrapS f) x y into the moral equivalent of f (coerce x) y on the Core level, where dictionaries are represented as values (see MkId:Note [magicDictId magic]). This is actually better than what we get with the unsafeCoerce trick: rather than coercing f, it merely coerces the dictionary itself, so f can potentially be optimized more.
In light of this advantage, would it make sense for Data.Reflection to use magicDict as well?
It should be possible to tackle this together with #26.
I've found out today that ghc (since version 7.8) implements dictionary reification for type literals with the help of some compiler magic. For example (see GHC.TypeLits):
In a nutshell, the compiler magic is that there's a builtin rewrite rule that turns
magicDict (WrapS f) x yinto the moral equivalent off (coerce x) yon the Core level, where dictionaries are represented as values (see MkId:Note [magicDictId magic]). This is actually better than what we get with theunsafeCoercetrick: rather than coercingf, it merely coerces the dictionary itself, sofcan potentially be optimized more.In light of this advantage, would it make sense for
Data.Reflectionto usemagicDictas well?It should be possible to tackle this together with #26.