{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE TypeOperators #-}
module Data.Typeable
(
Typeable
, typeOf
, typeRep
, (:~:)(Refl)
, (:~~:)(HRefl)
, cast
, eqT
, gcast
, gcast1
, gcast2
, Proxy (..)
, TypeRep
, rnfTypeRep
, showsTypeRep
, mkFunTy
, funResultTy
, splitTyConApp
, typeRepArgs
, typeRepTyCon
, typeRepFingerprint
, I.TyCon
, I.tyConPackage
, I.tyConModule
, I.tyConName
, I.rnfTyCon
, I.tyConFingerprint
, typeOf1, typeOf2, typeOf3, typeOf4, typeOf5, typeOf6, typeOf7
) where
import qualified Data.Typeable.Internal as I
import Data.Typeable.Internal (Typeable)
import Data.Type.Equality
import Data.Maybe
import Data.Proxy
import GHC.Fingerprint.Type
import GHC.Show
import GHC.Base
type TypeRep = I.SomeTypeRep
typeOf :: forall a. Typeable a => a -> TypeRep
typeOf _ = I.someTypeRep (Proxy :: Proxy a)
typeRep :: forall proxy a. Typeable a => proxy a -> TypeRep
typeRep = I.someTypeRep
showsTypeRep :: TypeRep -> ShowS
showsTypeRep = shows
cast :: forall a b. (Typeable a, Typeable b) => a -> Maybe b
cast x
| Just HRefl <- ta `I.eqTypeRep` tb = Just x
| otherwise = Nothing
where
ta = I.typeRep :: I.TypeRep a
tb = I.typeRep :: I.TypeRep b
eqT :: forall a b. (Typeable a, Typeable b) => Maybe (a :~: b)
eqT
| Just HRefl <- ta `I.eqTypeRep` tb = Just Refl
| otherwise = Nothing
where
ta = I.typeRep :: I.TypeRep a
tb = I.typeRep :: I.TypeRep b
gcast :: forall a b c. (Typeable a, Typeable b) => c a -> Maybe (c b)
gcast x = fmap (\Refl -> x) (eqT :: Maybe (a :~: b))
gcast1 :: forall c t t' a. (Typeable t, Typeable t')
=> c (t a) -> Maybe (c (t' a))
gcast1 x = fmap (\Refl -> x) (eqT :: Maybe (t :~: t'))
gcast2 :: forall c t t' a b. (Typeable t, Typeable t')
=> c (t a b) -> Maybe (c (t' a b))
gcast2 x = fmap (\Refl -> x) (eqT :: Maybe (t :~: t'))
funResultTy :: TypeRep -> TypeRep -> Maybe TypeRep
funResultTy (I.SomeTypeRep f) (I.SomeTypeRep x)
| Just HRefl <- (I.typeRep :: I.TypeRep Type) `I.eqTypeRep` I.typeRepKind f
, I.Fun arg res <- f
, Just HRefl <- arg `I.eqTypeRep` x
= Just (I.SomeTypeRep res)
| otherwise = Nothing
mkFunTy :: TypeRep -> TypeRep -> TypeRep
mkFunTy (I.SomeTypeRep arg) (I.SomeTypeRep res)
| Just HRefl <- I.typeRepKind arg `I.eqTypeRep` liftedTy
, Just HRefl <- I.typeRepKind res `I.eqTypeRep` liftedTy
= I.SomeTypeRep (I.Fun arg res)
| otherwise
= error $ "mkFunTy: Attempted to construct function type from non-lifted "++
"type: arg="++show arg++", res="++show res
where liftedTy = I.typeRep :: I.TypeRep Type
splitTyConApp :: TypeRep -> (TyCon, [TypeRep])
splitTyConApp (I.SomeTypeRep x) = I.splitApps x
typeRepArgs :: TypeRep -> [TypeRep]
typeRepArgs ty = case splitTyConApp ty of (_, args) -> args
typeRepTyCon :: TypeRep -> TyCon
typeRepTyCon = I.someTypeRepTyCon
typeRepFingerprint :: TypeRep -> Fingerprint
typeRepFingerprint = I.someTypeRepFingerprint
rnfTypeRep :: TypeRep -> ()
rnfTypeRep = I.rnfSomeTypeRep
typeOf1 :: forall t (a :: Type). Typeable t => t a -> TypeRep
typeOf1 _ = I.someTypeRep (Proxy :: Proxy t)
typeOf2 :: forall t (a :: Type) (b :: Type). Typeable t => t a b -> TypeRep
typeOf2 _ = I.someTypeRep (Proxy :: Proxy t)
typeOf3 :: forall t (a :: Type) (b :: Type) (c :: Type).
Typeable t => t a b c -> TypeRep
typeOf3 _ = I.someTypeRep (Proxy :: Proxy t)
typeOf4 :: forall t (a :: Type) (b :: Type) (c :: Type) (d :: Type).
Typeable t => t a b c d -> TypeRep
typeOf4 _ = I.someTypeRep (Proxy :: Proxy t)
typeOf5 :: forall t (a :: Type) (b :: Type) (c :: Type) (d :: Type) (e :: Type).
Typeable t => t a b c d e -> TypeRep
typeOf5 _ = I.someTypeRep (Proxy :: Proxy t)
typeOf6 :: forall t (a :: Type) (b :: Type) (c :: Type)
(d :: Type) (e :: Type) (f :: Type).
Typeable t => t a b c d e f -> TypeRep
typeOf6 _ = I.someTypeRep (Proxy :: Proxy t)
typeOf7 :: forall t (a :: Type) (b :: Type) (c :: Type)
(d :: Type) (e :: Type) (f :: Type) (g :: Type).
Typeable t => t a b c d e f g -> TypeRep
typeOf7 _ = I.someTypeRep (Proxy :: Proxy t)