module Data.Singletons.TypeRepStar (
Sing(STypeRep)
) where
import Data.Singletons.Prelude.Instances
import Data.Singletons
import Data.Singletons.Prelude.Eq
import Data.Typeable
import Unsafe.Coerce
import Data.Singletons.Decide
import Data.Kind
import GHC.Exts ( Proxy# )
import Data.Type.Coercion
import Data.Type.Equality
data instance Sing (a :: *) where
STypeRep :: Typeable a => Sing a
instance Typeable a => SingI (a :: *) where
sing = STypeRep
instance SingKind Type where
type Demote Type = TypeRep
fromSing (STypeRep :: Sing a) = typeOf (undefined :: a)
toSing = dirty_mk_STypeRep
instance PEq Type where
type (a :: *) :== (b :: *) = a == b
instance SEq Type where
(STypeRep :: Sing a) %:== (STypeRep :: Sing b) =
case (eqT :: Maybe (a :~: b)) of
Just Refl -> STrue
Nothing -> unsafeCoerce SFalse
instance SDecide Type where
(STypeRep :: Sing a) %~ (STypeRep :: Sing b) =
case (eqT :: Maybe (a :~: b)) of
Just Refl -> Proved Refl
Nothing -> Disproved (\Refl -> error "Data.Typeable.eqT failed")
instance TestCoercion Sing where
testCoercion (STypeRep :: Sing a) (STypeRep :: Sing b) =
case (eqT :: Maybe (a :~: b)) of
Just Refl -> Just Coercion
Nothing -> Nothing
newtype DI = Don'tInstantiate (forall a. Typeable a => Sing a)
dirty_mk_STypeRep :: TypeRep -> SomeSing *
dirty_mk_STypeRep rep =
let justLikeTypeable :: Proxy# a -> TypeRep
justLikeTypeable _ = rep
in
unsafeCoerce (Don'tInstantiate STypeRep) justLikeTypeable