{-# LANGUAGE RankNTypes, TypeFamilies, FlexibleInstances,
GADTs, UndecidableInstances, ScopedTypeVariables,
MagicHash, TypeOperators, PolyKinds, TypeApplications #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module Data.Singletons.TypeRepTYPE (
Sing,
SomeTypeRepTYPE(..)
) where
import Data.Kind (Type)
import Data.Singletons.Prelude.Instances
import Data.Singletons.Internal
import Data.Singletons.Prelude.Eq
import Data.Singletons.Decide
import GHC.Exts (RuntimeRep, TYPE)
import Type.Reflection
import Type.Reflection.Unsafe
import Unsafe.Coerce
type instance Sing @(TYPE rep) = TypeRep
data SomeTypeRepTYPE :: RuntimeRep -> Type where
SomeTypeRepTYPE :: forall (rep :: RuntimeRep) (a :: TYPE rep). !(TypeRep a) -> SomeTypeRepTYPE rep
instance Eq (SomeTypeRepTYPE rep) where
SomeTypeRepTYPE a :: TypeRep a
a == :: SomeTypeRepTYPE rep -> SomeTypeRepTYPE rep -> Bool
== SomeTypeRepTYPE b :: TypeRep a
b =
case TypeRep a -> TypeRep a -> Maybe (a :~~: a)
forall k1 k2 (a :: k1) (b :: k2).
TypeRep a -> TypeRep b -> Maybe (a :~~: b)
eqTypeRep TypeRep a
a TypeRep a
b of
Just HRefl -> Bool
True
Nothing -> Bool
False
instance Ord (SomeTypeRepTYPE rep) where
SomeTypeRepTYPE a :: TypeRep a
a compare :: SomeTypeRepTYPE rep -> SomeTypeRepTYPE rep -> Ordering
`compare` SomeTypeRepTYPE b :: TypeRep a
b =
TypeRep a -> Fingerprint
forall k (a :: k). TypeRep a -> Fingerprint
typeRepFingerprint TypeRep a
a Fingerprint -> Fingerprint -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` TypeRep a -> Fingerprint
forall k (a :: k). TypeRep a -> Fingerprint
typeRepFingerprint TypeRep a
b
instance Show (SomeTypeRepTYPE rep) where
showsPrec :: Int -> SomeTypeRepTYPE rep -> ShowS
showsPrec p :: Int
p (SomeTypeRepTYPE ty :: TypeRep a
ty) = Int -> TypeRep a -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
p TypeRep a
ty
instance Typeable a => SingI (a :: TYPE rep) where
sing :: Sing a
sing = Sing a
forall k (a :: k). Typeable a => TypeRep a
typeRep
instance SingKind (TYPE rep) where
type Demote (TYPE rep) = SomeTypeRepTYPE rep
fromSing :: Sing a -> Demote (TYPE rep)
fromSing = Sing a -> Demote (TYPE rep)
forall a. TypeRep a -> SomeTypeRepTYPE 'LiftedRep
SomeTypeRepTYPE
toSing :: Demote (TYPE rep) -> SomeSing (TYPE rep)
toSing (SomeTypeRepTYPE tr) = Sing a -> SomeSing (TYPE rep)
forall k (a :: k). Sing a -> SomeSing k
SomeSing TypeRep a
Sing a
tr
instance PEq (TYPE rep)
instance SEq (TYPE rep) where
tra :: Sing a
tra %== :: Sing a -> Sing b -> Sing (a == b)
%== trb :: Sing b
trb =
case TypeRep a -> TypeRep b -> Maybe (a :~~: b)
forall k1 k2 (a :: k1) (b :: k2).
TypeRep a -> TypeRep b -> Maybe (a :~~: b)
eqTypeRep TypeRep a
Sing a
tra TypeRep b
Sing b
trb of
Just HRefl -> Sing (a == b)
SBool 'True
STrue
Nothing -> SBool 'False -> SBool (DefaultEq a b)
forall a b. a -> b
unsafeCoerce SBool 'False
SFalse
instance SDecide (TYPE rep) where
tra :: Sing a
tra %~ :: Sing a -> Sing b -> Decision (a :~: b)
%~ trb :: Sing b
trb =
case TypeRep a -> TypeRep b -> Maybe (a :~~: b)
forall k1 k2 (a :: k1) (b :: k2).
TypeRep a -> TypeRep b -> Maybe (a :~~: b)
eqTypeRep TypeRep a
Sing a
tra TypeRep b
Sing b
trb of
Just HRefl -> (a :~: a) -> Decision (a :~: a)
forall a. a -> Decision a
Proved a :~: a
forall k (a :: k). a :~: a
Refl
Nothing -> Refuted (a :~: b) -> Decision (a :~: b)
forall a. Refuted a -> Decision a
Disproved (\Refl -> String -> Void
forall a. HasCallStack => String -> a
error "Type.Reflection.eqTypeRep failed")