Skip to content

Use withDict for reify & co. #44

@int-e

Description

@int-e

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions