I was hoping someone could shed a little light on the black magic in Data.Reflection. The pertinent snippet is:
{-# LANGUAGE CPP #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE KindSignatures #-}
module Data.Reflection
(
Reifies(..)
, reify
) where
import Data.Proxy
import Unsafe.Coerce
class Reifies s a | s -> a where
-- | Recover a value inside a 'reify' context, given a proxy for its
-- reified type.
reflect :: proxy s -> a
newtype Magic a r = Magic (forall (s :: *). Reifies s a => Proxy s -> r)
-- | Reify a value at the type level, to be recovered with 'reflect'.
reify :: forall a r. a -> (forall (s :: *). Reifies s a => Proxy s -> r) -> r
reify a k = unsafeCoerce (Magic k :: Magic a r) (const a) Proxy
- I can't parse the definition for
reify
. maybe I'm missing something simple about order of evaluation, but it looks likeunsafeCoerce::a->b
is applied to three arguments. - What are the isomorphic types used in the
unsafeCoerce
? - Where is the function
k
actually evaluated in the definition ofreify
? Where are any instances for
Reifes
? For example, I can run the following line in GHCi only loading Data.Reflection and Data.Proxy (and setting -XScopedTypeVariables):.reify (3::Int) (\(_:: Proxy q) -> print $ reflect (Proxy :: Proxy q))
Where/what is the phantom reified type?
- What is the "magic" in
newtype Magic
?