{-# LANGUAGE UnliftedFFITypes #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE StandaloneKindSignatures #-}
{-# LANGUAGE LinearTypes #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  Data.Typeable.Internal
-- Copyright   :  (c) The University of Glasgow, CWI 2001--2011
-- License     :  BSD-style (see the file libraries/base/LICENSE)
--
-- The representations of the types TyCon and TypeRep, and the
-- function mkTyCon which is used by derived instances of Typeable to
-- construct a TyCon.
--
-----------------------------------------------------------------------------

module Data.Typeable.Internal (
    -- * Typeable and kind polymorphism
    --
    -- #kind_instantiation

    -- * Miscellaneous
    Fingerprint(..),

    -- * Typeable class
    Typeable(..),
    withTypeable,

    -- * Module
    Module,  -- Abstract
    moduleName, modulePackage, rnfModule,

    -- * TyCon
    TyCon,   -- Abstract
    tyConPackage, tyConModule, tyConName, tyConKindArgs, tyConKindRep,
    tyConFingerprint,
    KindRep(.., KindRepTypeLit), TypeLitSort(..),
    rnfTyCon,

    -- * TypeRep
    TypeRep,
    pattern App, pattern Con, pattern Con', pattern Fun,
    typeRep,
    typeOf,
    typeRepTyCon,
    typeRepFingerprint,
    rnfTypeRep,
    eqTypeRep,
    typeRepKind,
    splitApps,

    -- * SomeTypeRep
    SomeTypeRep(..),
    someTypeRep,
    someTypeRepTyCon,
    someTypeRepFingerprint,
    rnfSomeTypeRep,

    -- * Construction
    -- | These are for internal use only
    mkTrType, mkTrCon, mkTrApp, mkTrAppChecked, mkTrFun,
    mkTyCon, mkTyCon#,
    typeSymbolTypeRep, typeNatTypeRep, typeCharTypeRep,

    -- * For internal use
    trLiftedRep
  ) where

import GHC.Prim ( FUN )
import GHC.Base
import qualified GHC.Arr as A
import GHC.Types ( TYPE, Multiplicity (Many) )
import Data.Type.Equality
import GHC.List ( splitAt, foldl', elem )
import GHC.Word
import GHC.Show
import GHC.TypeLits ( KnownChar, charVal', KnownSymbol, symbolVal', AppendSymbol )
import GHC.TypeNats ( KnownNat, Nat, natVal' )
import Unsafe.Coerce ( unsafeCoerce )

import GHC.Fingerprint.Type
import {-# SOURCE #-} GHC.Fingerprint
   -- loop: GHC.Fingerprint -> Foreign.Ptr -> Data.Typeable
   -- Better to break the loop here, because we want non-SOURCE imports
   -- of Data.Typeable as much as possible so we can optimise the derived
   -- instances.
-- import {-# SOURCE #-} Debug.Trace (trace)

#include "MachDeps.h"

{- *********************************************************************
*                                                                      *
                The TyCon type
*                                                                      *
********************************************************************* -}

modulePackage :: Module -> String
modulePackage :: Module -> String
modulePackage (Module TrName
p TrName
_) = TrName -> String
trNameString TrName
p

moduleName :: Module -> String
moduleName :: Module -> String
moduleName (Module TrName
_ TrName
m) = TrName -> String
trNameString TrName
m

tyConPackage :: TyCon -> String
tyConPackage :: TyCon -> String
tyConPackage (TyCon Word#
_ Word#
_ Module
m TrName
_ Int#
_ KindRep
_) = Module -> String
modulePackage Module
m

tyConModule :: TyCon -> String
tyConModule :: TyCon -> String
tyConModule (TyCon Word#
_ Word#
_ Module
m TrName
_ Int#
_ KindRep
_) = Module -> String
moduleName Module
m

tyConName :: TyCon -> String
tyConName :: TyCon -> String
tyConName (TyCon Word#
_ Word#
_ Module
_ TrName
n Int#
_ KindRep
_) = TrName -> String
trNameString TrName
n

trNameString :: TrName -> String
trNameString :: TrName -> String
trNameString (TrNameS Addr#
s) = Addr# -> String
unpackCStringUtf8# Addr#
s
trNameString (TrNameD String
s) = String
s

tyConFingerprint :: TyCon -> Fingerprint
tyConFingerprint :: TyCon -> Fingerprint
tyConFingerprint (TyCon Word#
hi Word#
lo Module
_ TrName
_ Int#
_ KindRep
_)
  = Word64 -> Word64 -> Fingerprint
Fingerprint (Word# -> Word64
W64# Word#
hi) (Word# -> Word64
W64# Word#
lo)

tyConKindArgs :: TyCon -> Int
tyConKindArgs :: TyCon -> KindBndr
tyConKindArgs (TyCon Word#
_ Word#
_ Module
_ TrName
_ Int#
n KindRep
_) = Int# -> KindBndr
I# Int#
n

tyConKindRep :: TyCon -> KindRep
tyConKindRep :: TyCon -> KindRep
tyConKindRep (TyCon Word#
_ Word#
_ Module
_ TrName
_ Int#
_ KindRep
k) = KindRep
k

-- | Helper to fully evaluate 'TyCon' for use as @NFData(rnf)@ implementation
--
-- @since 4.8.0.0
rnfModule :: Module -> ()
rnfModule :: Module -> ()
rnfModule (Module TrName
p TrName
m) = TrName -> ()
rnfTrName TrName
p seq :: forall a b. a -> b -> b
`seq` TrName -> ()
rnfTrName TrName
m

rnfTrName :: TrName -> ()
rnfTrName :: TrName -> ()
rnfTrName (TrNameS Addr#
_) = ()
rnfTrName (TrNameD String
n) = String -> ()
rnfString String
n

rnfKindRep :: KindRep -> ()
rnfKindRep :: KindRep -> ()
rnfKindRep (KindRepTyConApp TyCon
tc [KindRep]
args) = TyCon -> ()
rnfTyCon TyCon
tc seq :: forall a b. a -> b -> b
`seq` forall a. (a -> ()) -> [a] -> ()
rnfList KindRep -> ()
rnfKindRep [KindRep]
args
rnfKindRep (KindRepVar KindBndr
_)   = ()
rnfKindRep (KindRepApp KindRep
a KindRep
b) = KindRep -> ()
rnfKindRep KindRep
a seq :: forall a b. a -> b -> b
`seq` KindRep -> ()
rnfKindRep KindRep
b
rnfKindRep (KindRepFun KindRep
a KindRep
b) = KindRep -> ()
rnfKindRep KindRep
a seq :: forall a b. a -> b -> b
`seq` KindRep -> ()
rnfKindRep KindRep
b
rnfKindRep (KindRepTYPE RuntimeRep
rr) = RuntimeRep -> ()
rnfRuntimeRep RuntimeRep
rr
rnfKindRep (KindRepTypeLitS TypeLitSort
_ Addr#
_) = ()
rnfKindRep (KindRepTypeLitD TypeLitSort
_ String
t) = String -> ()
rnfString String
t

rnfRuntimeRep :: RuntimeRep -> ()
rnfRuntimeRep :: RuntimeRep -> ()
rnfRuntimeRep (VecRep !VecCount
_ !VecElem
_) = ()
rnfRuntimeRep !RuntimeRep
_             = ()

rnfList :: (a -> ()) -> [a] -> ()
rnfList :: forall a. (a -> ()) -> [a] -> ()
rnfList a -> ()
_     []     = ()
rnfList a -> ()
force (a
x:[a]
xs) = a -> ()
force a
x seq :: forall a b. a -> b -> b
`seq` forall a. (a -> ()) -> [a] -> ()
rnfList a -> ()
force [a]
xs

rnfString :: [Char] -> ()
rnfString :: String -> ()
rnfString = forall a. (a -> ()) -> [a] -> ()
rnfList (seq :: forall a b. a -> b -> b
`seq` ())

rnfTyCon :: TyCon -> ()
rnfTyCon :: TyCon -> ()
rnfTyCon (TyCon Word#
_ Word#
_ Module
m TrName
n Int#
_ KindRep
k) = Module -> ()
rnfModule Module
m seq :: forall a b. a -> b -> b
`seq` TrName -> ()
rnfTrName TrName
n seq :: forall a b. a -> b -> b
`seq` KindRep -> ()
rnfKindRep KindRep
k


{- *********************************************************************
*                                                                      *
                The TypeRep type
*                                                                      *
********************************************************************* -}

-- | A concrete representation of a (monomorphic) type.
-- 'TypeRep' supports reasonably efficient equality.
type TypeRep :: k -> Type
data TypeRep a where
    -- The TypeRep of Type. See Note [Kind caching], Wrinkle 2
    TrType :: TypeRep Type
    TrTyCon :: { -- See Note [TypeRep fingerprints]
                 forall k (a :: k). TypeRep a -> Fingerprint
trTyConFingerprint :: {-# UNPACK #-} !Fingerprint

                 -- The TypeRep represents the application of trTyCon
                 -- to the kind arguments trKindVars. So for
                 -- 'Just :: Bool -> Maybe Bool, the trTyCon will be
                 -- 'Just and the trKindVars will be [Bool].
               , forall k (a :: k). TypeRep a -> TyCon
trTyCon :: !TyCon
               , forall k (a :: k). TypeRep a -> [SomeTypeRep]
trKindVars :: [SomeTypeRep]
               , forall k (a :: k). TypeRep a -> TypeRep k
trTyConKind :: !(TypeRep k) }  -- See Note [Kind caching]
            -> TypeRep (a :: k)

    -- | Invariant: Saturated arrow types (e.g. things of the form @a -> b@)
    -- are represented with @'TrFun' a b@, not @TrApp (TrApp funTyCon a) b@.
    TrApp   :: forall k1 k2 (a :: k1 -> k2) (b :: k1).
               { -- See Note [TypeRep fingerprints]
                 forall m k2 (a :: m -> k2) (x :: m). TypeRep (a x) -> Fingerprint
trAppFingerprint :: {-# UNPACK #-} !Fingerprint

                 -- The TypeRep represents the application of trAppFun
                 -- to trAppArg. For Maybe Int, the trAppFun will be Maybe
                 -- and the trAppArg will be Int.
               , forall m k2 (a :: m -> k2) (x :: m). TypeRep (a x) -> TypeRep a
trAppFun :: !(TypeRep (a :: k1 -> k2))
               , forall m k2 (a :: m -> k2) (x :: m). TypeRep (a x) -> TypeRep x
trAppArg :: !(TypeRep (b :: k1))
               , forall m k2 (a :: m -> k2) (x :: m). TypeRep (a x) -> TypeRep k2
trAppKind :: !(TypeRep k2) }   -- See Note [Kind caching]
            -> TypeRep (a b)

    -- | @TrFun fpr m a b@ represents a function type @a # m -> b@. We use this for
    -- the sake of efficiency as functions are quite ubiquitous.
    TrFun   :: forall (m :: Multiplicity) (r1 :: RuntimeRep) (r2 :: RuntimeRep)
                      (a :: TYPE r1) (b :: TYPE r2).
               { -- See Note [TypeRep fingerprints]
                 forall (m :: Multiplicity) a b.
TypeRep (LiftedRep %m -> b) -> Fingerprint
trFunFingerprint :: {-# UNPACK #-} !Fingerprint

                 -- The TypeRep represents a function from trFunArg to
                 -- trFunRes.
               , forall (m :: Multiplicity) a b.
TypeRep (LiftedRep %m -> b) -> TypeRep m
trFunMul :: !(TypeRep m)
               , forall (m :: Multiplicity) a b.
TypeRep (LiftedRep %m -> b) -> TypeRep LiftedRep
trFunArg :: !(TypeRep a)
               , forall (m :: Multiplicity) a b.
TypeRep (LiftedRep %m -> b) -> TypeRep b
trFunRes :: !(TypeRep b) }
            -> TypeRep (FUN m a b)

{- Note [TypeRep fingerprints]
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~
We store a Fingerprint of each TypeRep in its constructor. This allows
us to test whether two TypeReps are equal in constant time, rather than
having to walk their full structures.
-}

{- Note [Kind caching]
   ~~~~~~~~~~~~~~~~~~~

We cache the kind of the TypeRep in each TrTyCon and TrApp constructor.
This is necessary to ensure that typeRepKind (which is used, at least, in
deserialization and dynApply) is cheap. There are two reasons for this:

1. Calculating the kind of a nest of type applications, such as

  F X Y Z W   (App (App (App (App F X) Y) Z) W)

is linear in the depth, which is already a bit pricy. In deserialization,
we build up such a nest from the inside out, so without caching, that ends
up taking quadratic time, and calculating the KindRep of the constructor,
F, a linear number of times. See #14254.

2. Calculating the kind of a type constructor, in instantiateTypeRep,
requires building (allocating) a TypeRep for the kind "from scratch".
This can get pricy. When combined with point (1), we can end up with
a large amount of extra allocation deserializing very deep nests.
See #14337.

It is quite possible to speed up deserialization by structuring that process
very carefully. Unfortunately, that doesn't help dynApply or anything else
that may use typeRepKind. Since caching the kind isn't terribly expensive, it
seems better to just do that and solve all the potential problems at once.

There are two things we need to be careful about when caching kinds.

Wrinkle 1:

We want to do it eagerly. Suppose we have

  tf :: TypeRep (f :: j -> k)
  ta :: TypeRep (a :: j)

Then the cached kind of App tf ta should be eagerly evaluated to k, rather
than being stored as a thunk that will strip the (j ->) off of j -> k if
and when it is forced.

Wrinkle 2:

We need to be able to represent TypeRep Type. This is a bit tricky because
typeRepKind (typeRep @Type) = typeRep @Type, so if we actually cache the
typerep of the kind of Type, we will have a loop. One simple way to do this
is to make the cached kind fields lazy and allow TypeRep Type to be cyclical.

But we *do not* want TypeReps to have cyclical structure! Most importantly,
a cyclical structure cannot be stored in a compact region. Secondarily,
using :force in GHCi on a cyclical structure will lead to non-termination.

To avoid this trouble, we use a separate constructor for TypeRep Type.
mkTrApp is responsible for recognizing that TYPE is being applied to
'LiftedRep and produce trType; other functions must recognize that TrType
represents an application.
-}

-- Compare keys for equality

-- | @since 2.01
instance Eq (TypeRep a) where
  TypeRep a
_ == :: TypeRep a -> TypeRep a -> Bool
== TypeRep a
_  = Bool
True
  {-# INLINABLE (==) #-}

instance TestEquality TypeRep where
  TypeRep a
a testEquality :: forall (a :: k) (b :: k). TypeRep a -> TypeRep b -> Maybe (a :~: b)
`testEquality` TypeRep b
b
    | Just a :~~: b
HRefl <- forall k1 k2 (a :: k1) (b :: k2).
TypeRep a -> TypeRep b -> Maybe (a :~~: b)
eqTypeRep TypeRep a
a TypeRep b
b
    = forall a. a -> Maybe a
Just forall {k} (a :: k). a :~: a
Refl
    | Bool
otherwise
    = forall a. Maybe a
Nothing
  {-# INLINEABLE testEquality #-}

-- | @since 4.4.0.0
instance Ord (TypeRep a) where
  compare :: TypeRep a -> TypeRep a -> Ordering
compare TypeRep a
_ TypeRep a
_ = Ordering
EQ
  {-# INLINABLE compare #-}

-- | A non-indexed type representation.
data SomeTypeRep where
    SomeTypeRep :: forall k (a :: k). !(TypeRep a) %1 -> SomeTypeRep

instance Eq SomeTypeRep where
  SomeTypeRep TypeRep a
a == :: SomeTypeRep -> SomeTypeRep -> Bool
== SomeTypeRep TypeRep a
b =
      case TypeRep a
a forall k1 k2 (a :: k1) (b :: k2).
TypeRep a -> TypeRep b -> Maybe (a :~~: b)
`eqTypeRep` TypeRep a
b of
          Just a :~~: a
_  -> Bool
True
          Maybe (a :~~: a)
Nothing -> Bool
False

instance Ord SomeTypeRep where
  SomeTypeRep TypeRep a
a compare :: SomeTypeRep -> SomeTypeRep -> Ordering
`compare` SomeTypeRep TypeRep a
b =
    forall k (a :: k). TypeRep a -> Fingerprint
typeRepFingerprint TypeRep a
a forall a. Ord a => a -> a -> Ordering
`compare` forall k (a :: k). TypeRep a -> Fingerprint
typeRepFingerprint TypeRep a
b

-- | The function type constructor.
--
-- For instance,
--
-- @
-- typeRep \@(Int -> Char) === Fun (typeRep \@Int) (typeRep \@Char)
-- @
--
pattern Fun :: forall k (fun :: k). ()
            => forall (r1 :: RuntimeRep) (r2 :: RuntimeRep)
                      (arg :: TYPE r1) (res :: TYPE r2).
               (k ~ Type, fun ~~ (arg -> res))
            => TypeRep arg
            -> TypeRep res
            -> TypeRep fun
pattern $mFun :: forall {r} {k} {fun :: k}.
TypeRep fun
-> (forall {arg} {res}.
    (k ~ *, fun ~~ (arg -> res)) =>
    TypeRep arg -> TypeRep res -> r)
-> ((# #) -> r)
-> r
$bFun :: forall k (fun :: k) arg res.
(k ~ *, fun ~~ (arg -> res)) =>
TypeRep arg -> TypeRep res -> TypeRep fun
Fun arg res <- TrFun {trFunArg = arg, trFunRes = res, trFunMul = (eqTypeRep trMany -> Just HRefl)}
  where Fun TypeRep arg
arg TypeRep res
res = forall (m :: Multiplicity) a b.
TypeRep m -> TypeRep a -> TypeRep b -> TypeRep (a %m -> b)
mkTrFun TypeRep 'Many
trMany TypeRep arg
arg TypeRep res
res

-- | Observe the 'Fingerprint' of a type representation
--
-- @since 4.8.0.0
typeRepFingerprint :: TypeRep a -> Fingerprint
typeRepFingerprint :: forall k (a :: k). TypeRep a -> Fingerprint
typeRepFingerprint TypeRep a
TrType = Fingerprint
fpTYPELiftedRep
typeRepFingerprint (TrTyCon {trTyConFingerprint :: forall k (a :: k). TypeRep a -> Fingerprint
trTyConFingerprint = Fingerprint
fpr}) = Fingerprint
fpr
typeRepFingerprint (TrApp {trAppFingerprint :: forall m k2 (a :: m -> k2) (x :: m). TypeRep (a x) -> Fingerprint
trAppFingerprint = Fingerprint
fpr}) = Fingerprint
fpr
typeRepFingerprint (TrFun {trFunFingerprint :: forall (m :: Multiplicity) a b.
TypeRep (LiftedRep %m -> b) -> Fingerprint
trFunFingerprint = Fingerprint
fpr}) = Fingerprint
fpr

-- For compiler use
mkTrType :: TypeRep Type
mkTrType :: TypeRep (*)
mkTrType = TypeRep (*)
TrType

-- | Construct a representation for a type constructor
-- applied at a monomorphic kind.
--
-- Note that this is unsafe as it allows you to construct
-- ill-kinded types.
mkTrCon :: forall k (a :: k). TyCon -> [SomeTypeRep] -> TypeRep a
mkTrCon :: forall k (a :: k). TyCon -> [SomeTypeRep] -> TypeRep a
mkTrCon TyCon
tc [SomeTypeRep]
kind_vars = TrTyCon
    { trTyConFingerprint :: Fingerprint
trTyConFingerprint = Fingerprint
fpr
    , trTyCon :: TyCon
trTyCon = TyCon
tc
    , trKindVars :: [SomeTypeRep]
trKindVars = [SomeTypeRep]
kind_vars
    , trTyConKind :: TypeRep k
trTyConKind = TypeRep k
kind }
  where
    fpr_tc :: Fingerprint
fpr_tc  = TyCon -> Fingerprint
tyConFingerprint TyCon
tc
    fpr_kvs :: [Fingerprint]
fpr_kvs = forall a b. (a -> b) -> [a] -> [b]
map SomeTypeRep -> Fingerprint
someTypeRepFingerprint [SomeTypeRep]
kind_vars
    fpr :: Fingerprint
fpr     = [Fingerprint] -> Fingerprint
fingerprintFingerprints (Fingerprint
fpr_tcforall a. a -> [a] -> [a]
:[Fingerprint]
fpr_kvs)
    kind :: TypeRep k
kind    = forall {k} (a :: k). SomeTypeRep -> TypeRep a
unsafeCoerceRep forall a b. (a -> b) -> a -> b
$ TyCon -> [SomeTypeRep] -> SomeTypeRep
tyConKind TyCon
tc [SomeTypeRep]
kind_vars

-- The fingerprint of Type. We don't store this in the TrType
-- constructor, so we need to build it here.
fpTYPELiftedRep :: Fingerprint
fpTYPELiftedRep :: Fingerprint
fpTYPELiftedRep = [Fingerprint] -> Fingerprint
fingerprintFingerprints
      [ TyCon -> Fingerprint
tyConFingerprint TyCon
tyConTYPE
      , [Fingerprint] -> Fingerprint
fingerprintFingerprints
        [ TyCon -> Fingerprint
tyConFingerprint TyCon
tyCon'BoxedRep
        , TyCon -> Fingerprint
tyConFingerprint TyCon
tyCon'Lifted
        ]
      ]
-- There is absolutely nothing to gain and everything to lose
-- by inlining the worker. The wrapper should inline anyway.
{-# NOINLINE fpTYPELiftedRep #-}

trTYPE :: TypeRep TYPE
trTYPE :: TypeRep TYPE
trTYPE = forall {k} (a :: k). Typeable a => TypeRep a
typeRep

trLiftedRep :: TypeRep ('BoxedRep 'Lifted)
trLiftedRep :: TypeRep ('BoxedRep 'Lifted)
trLiftedRep = forall {k} (a :: k). Typeable a => TypeRep a
typeRep

trMany :: TypeRep 'Many
trMany :: TypeRep 'Many
trMany = forall {k} (a :: k). Typeable a => TypeRep a
typeRep

-- | Construct a representation for a type application that is
-- NOT a saturated arrow type. This is not checked!

-- Note that this is known-key to the compiler, which uses it in desugar
-- 'Typeable' evidence.
mkTrApp :: forall k1 k2 (a :: k1 -> k2) (b :: k1).
           TypeRep (a :: k1 -> k2)
        -> TypeRep (b :: k1)
        -> TypeRep (a b)
mkTrApp :: forall k1 k2 (a :: k1 -> k2) (b :: k1).
TypeRep a -> TypeRep b -> TypeRep (a b)
mkTrApp TypeRep a
a TypeRep b
b -- See Note [Kind caching], Wrinkle 2
  | Just a :~~: TYPE
HRefl <- TypeRep a
a forall k1 k2 (a :: k1) (b :: k2).
TypeRep a -> TypeRep b -> Maybe (a :~~: b)
`eqTypeRep` TypeRep TYPE
trTYPE
  , Just b :~~: 'BoxedRep 'Lifted
HRefl <- TypeRep b
b forall k1 k2 (a :: k1) (b :: k2).
TypeRep a -> TypeRep b -> Maybe (a :~~: b)
`eqTypeRep` TypeRep ('BoxedRep 'Lifted)
trLiftedRep
  = TypeRep (*)
TrType

  | TrFun {trFunRes :: forall (m :: Multiplicity) a b.
TypeRep (LiftedRep %m -> b) -> TypeRep b
trFunRes = TypeRep b
res_kind} <- forall k (a :: k). TypeRep a -> TypeRep k
typeRepKind TypeRep a
a
  = TrApp
    { trAppFingerprint :: Fingerprint
trAppFingerprint = Fingerprint
fpr
    , trAppFun :: TypeRep a
trAppFun = TypeRep a
a
    , trAppArg :: TypeRep b
trAppArg = TypeRep b
b
    , trAppKind :: TypeRep k2
trAppKind = TypeRep b
res_kind }

  | Bool
otherwise = forall a. HasCallStack => String -> a
error (String
"Ill-kinded type application: "
                           forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (forall k (a :: k). TypeRep a -> TypeRep k
typeRepKind TypeRep a
a))
  where
    fpr_a :: Fingerprint
fpr_a = forall k (a :: k). TypeRep a -> Fingerprint
typeRepFingerprint TypeRep a
a
    fpr_b :: Fingerprint
fpr_b = forall k (a :: k). TypeRep a -> Fingerprint
typeRepFingerprint TypeRep b
b
    fpr :: Fingerprint
fpr   = [Fingerprint] -> Fingerprint
fingerprintFingerprints [Fingerprint
fpr_a, Fingerprint
fpr_b]

-- | Construct a representation for a type application that
-- may be a saturated arrow type. This is renamed to mkTrApp in
-- Type.Reflection.Unsafe
mkTrAppChecked :: forall k1 k2 (a :: k1 -> k2) (b :: k1).
                  TypeRep (a :: k1 -> k2)
               -> TypeRep (b :: k1)
               -> TypeRep (a b)
mkTrAppChecked :: forall k1 k2 (a :: k1 -> k2) (b :: k1).
TypeRep a -> TypeRep b -> TypeRep (a b)
mkTrAppChecked rep :: TypeRep a
rep@(TrApp {trAppFun :: forall m k2 (a :: m -> k2) (x :: m). TypeRep (a x) -> TypeRep a
trAppFun = TypeRep a
p, trAppArg :: forall m k2 (a :: m -> k2) (x :: m). TypeRep (a x) -> TypeRep x
trAppArg = TypeRep b
x :: TypeRep x})
               (TypeRep b
y :: TypeRep y)
  | TrTyCon {trTyCon :: forall k (a :: k). TypeRep a -> TyCon
trTyCon=TyCon
con} <- TypeRep a
p
  , TyCon
con forall a. Eq a => a -> a -> Bool
== TyCon
funTyCon  -- cheap check first
  , Just (IsTYPE (TypeRep r
rx :: TypeRep rx)) <- forall a. TypeRep a -> Maybe (IsTYPE a)
isTYPE (forall k (a :: k). TypeRep a -> TypeRep k
typeRepKind TypeRep b
x)
  , Just (IsTYPE (TypeRep r
ry :: TypeRep ry)) <- forall a. TypeRep a -> Maybe (IsTYPE a)
isTYPE (forall k (a :: k). TypeRep a -> TypeRep k
typeRepKind TypeRep b
y)
  , Just (->) b :~~: a b
HRefl <- forall k (a :: k) r. TypeRep a -> (Typeable a => r) -> r
withTypeable TypeRep b
x forall a b. (a -> b) -> a -> b
$ forall k (a :: k) r. TypeRep a -> (Typeable a => r) -> r
withTypeable TypeRep r
rx forall a b. (a -> b) -> a -> b
$ forall k (a :: k) r. TypeRep a -> (Typeable a => r) -> r
withTypeable TypeRep r
ry
                  forall a b. (a -> b) -> a -> b
$ forall {k} (a :: k). Typeable a => TypeRep a
typeRep @((->) x :: TYPE ry -> Type) forall k1 k2 (a :: k1) (b :: k2).
TypeRep a -> TypeRep b -> Maybe (a :~~: b)
`eqTypeRep` TypeRep a
rep
  = forall (m :: Multiplicity) a b.
TypeRep m -> TypeRep a -> TypeRep b -> TypeRep (a %m -> b)
mkTrFun TypeRep 'Many
trMany TypeRep b
x TypeRep b
y
mkTrAppChecked TypeRep a
a TypeRep b
b = forall k1 k2 (a :: k1 -> k2) (b :: k1).
TypeRep a -> TypeRep b -> TypeRep (a b)
mkTrApp TypeRep a
a TypeRep b
b

-- | A type application.
--
-- For instance,
--
-- @
-- typeRep \@(Maybe Int) === App (typeRep \@Maybe) (typeRep \@Int)
-- @
--
-- Note that this will also match a function type,
--
-- @
-- typeRep \@(Int# -> Char)
--   ===
-- App (App arrow (typeRep \@Int#)) (typeRep \@Char)
-- @
--
-- where @arrow :: TypeRep ((->) :: TYPE IntRep -> Type -> Type)@.
--
pattern App :: forall k2 (t :: k2). ()
            => forall k1 (a :: k1 -> k2) (b :: k1). (t ~ a b)
            => TypeRep a -> TypeRep b -> TypeRep t
pattern $mApp :: forall {r} {k2} {t :: k2}.
TypeRep t
-> (forall {k1} {a :: k1 -> k2} {b :: k1}.
    (t ~ a b) =>
    TypeRep a -> TypeRep b -> r)
-> ((# #) -> r)
-> r
$bApp :: forall k2 (t :: k2) k1 (a :: k1 -> k2) (b :: k1).
(t ~ a b) =>
TypeRep a -> TypeRep b -> TypeRep t
App f x <- (splitApp -> IsApp f x)
  where App TypeRep a
f TypeRep b
x = forall k1 k2 (a :: k1 -> k2) (b :: k1).
TypeRep a -> TypeRep b -> TypeRep (a b)
mkTrAppChecked TypeRep a
f TypeRep b
x

data AppOrCon (a :: k) where
    IsApp :: forall k k' (f :: k' -> k) (x :: k'). ()
          => TypeRep f %1 -> TypeRep x %1 -> AppOrCon (f x)
    -- See Note [Con evidence]
    IsCon :: IsApplication a ~ "" => TyCon %1 -> [SomeTypeRep] %1 -> AppOrCon a

type family IsApplication (x :: k) :: Symbol where
  IsApplication (_ _) = "An error message about this unifying with \"\" "
     `AppendSymbol` "means that you tried to match a TypeRep with Con or "
     `AppendSymbol` "Con' when the represented type was known to be an "
     `AppendSymbol` "application."
  IsApplication _ = ""

splitApp :: forall k (a :: k). ()
         => TypeRep a
         -> AppOrCon a
splitApp :: forall k (a :: k). TypeRep a -> AppOrCon a
splitApp TypeRep a
TrType = forall k m (a :: m -> k) (x :: m).
TypeRep a -> TypeRep x -> AppOrCon (a x)
IsApp TypeRep TYPE
trTYPE TypeRep ('BoxedRep 'Lifted)
trLiftedRep
splitApp (TrApp {trAppFun :: forall m k2 (a :: m -> k2) (x :: m). TypeRep (a x) -> TypeRep a
trAppFun = TypeRep a
f, trAppArg :: forall m k2 (a :: m -> k2) (x :: m). TypeRep (a x) -> TypeRep x
trAppArg = TypeRep b
x}) = forall k m (a :: m -> k) (x :: m).
TypeRep a -> TypeRep x -> AppOrCon (a x)
IsApp TypeRep a
f TypeRep b
x
splitApp rep :: TypeRep a
rep@(TrFun {trFunArg :: forall (m :: Multiplicity) a b.
TypeRep (LiftedRep %m -> b) -> TypeRep LiftedRep
trFunArg=TypeRep a
a, trFunRes :: forall (m :: Multiplicity) a b.
TypeRep (LiftedRep %m -> b) -> TypeRep b
trFunRes=TypeRep b
b}) = forall k m (a :: m -> k) (x :: m).
TypeRep a -> TypeRep x -> AppOrCon (a x)
IsApp (forall k1 k2 (a :: k1 -> k2) (b :: k1).
TypeRep a -> TypeRep b -> TypeRep (a b)
mkTrApp TypeRep (FUN m)
arr TypeRep a
a) TypeRep b
b
  where arr :: TypeRep (FUN m)
arr = forall (m :: Multiplicity) a b.
TypeRep (a %m -> b) -> TypeRep (FUN m)
bareArrow TypeRep a
rep
splitApp (TrTyCon{trTyCon :: forall k (a :: k). TypeRep a -> TyCon
trTyCon = TyCon
con, trKindVars :: forall k (a :: k). TypeRep a -> [SomeTypeRep]
trKindVars = [SomeTypeRep]
kinds})
  = case forall a b. a -> b
unsafeCoerce forall {k} (a :: k). a :~: a
Refl :: IsApplication a :~: "" of
      IsApplication a :~: ""
Refl -> forall {k} (a :: k).
(IsApplication a ~ "") =>
TyCon -> [SomeTypeRep] -> AppOrCon a
IsCon TyCon
con [SomeTypeRep]
kinds

-- | Use a 'TypeRep' as 'Typeable' evidence.
withTypeable :: forall k (a :: k) rep (r :: TYPE rep). ()
             => TypeRep a -> (Typeable a => r) -> r
withTypeable :: forall k (a :: k) r. TypeRep a -> (Typeable a => r) -> r
withTypeable TypeRep a
rep Typeable a => r
k = forall a b. a -> b
unsafeCoerce Gift a r
k' TypeRep a
rep
  where k' :: Gift a r
        k' :: Gift a r
k' = forall {k} (a :: k) r. (Typeable a => r) -> Gift a r
Gift Typeable a => r
k

-- | A helper to satisfy the type checker in 'withTypeable'.
newtype Gift a (r :: TYPE rep) = Gift (Typeable a => r)

-- | Pattern match on a type constructor
pattern Con :: forall k (a :: k). ()
            => IsApplication a ~ "" -- See Note [Con evidence]
            => TyCon -> TypeRep a
pattern $mCon :: forall {r} {k} {a :: k}.
TypeRep a
-> ((IsApplication a ~ "") => TyCon -> r) -> ((# #) -> r) -> r
Con con <- (splitApp -> IsCon con _)

-- | Pattern match on a type constructor including its instantiated kind
-- variables.
--
-- For instance,
--
-- @
-- App (Con' proxyTyCon ks) intRep = typeRep @(Proxy \@Int)
-- @
--
-- will bring into scope,
--
-- @
-- proxyTyCon :: TyCon
-- ks         == [someTypeRep @Type] :: [SomeTypeRep]
-- intRep     == typeRep @Int
-- @
--
pattern Con' :: forall k (a :: k). ()
             => IsApplication a ~ "" -- See Note [Con evidence]
             => TyCon -> [SomeTypeRep] -> TypeRep a
pattern $mCon' :: forall {r} {k} {a :: k}.
TypeRep a
-> ((IsApplication a ~ "") => TyCon -> [SomeTypeRep] -> r)
-> ((# #) -> r)
-> r
Con' con ks <- (splitApp -> IsCon con ks)

-- TODO: Remove Fun when #14253 is fixed
{-# COMPLETE Fun, App, Con  #-}
{-# COMPLETE Fun, App, Con' #-}

{- Note [Con evidence]
    ~~~~~~~~~~~~~~~~~~~

Matching TypeRep t on Con or Con' fakes up evidence that

  IsApplication t ~ "".

Why should anyone care about the value of strange internal type family?
Well, almost nobody cares about it, but the pattern checker does!
For example, suppose we have TypeRep (f x) and we want to get
TypeRep f and TypeRep x. There is no chance that the Con constructor
will match, because (f x) is not a constructor, but without the
IsApplication evidence, omitting it will lead to an incomplete pattern
warning. With the evidence, the pattern checker will see that
Con wouldn't typecheck, so everything works out as it should.

Why do we use Symbols? We would really like to use something like

  type family NotApplication (t :: k) :: Constraint where
    NotApplication (f a) = TypeError ...
    NotApplication _ = ()

Unfortunately, #11503 means that the pattern checker and type checker
will fail to actually reject the mistaken patterns. So we describe the
error in the result type. It's a horrible hack.
-}

----------------- Observation ---------------------

-- | Observe the type constructor of a quantified type representation.
someTypeRepTyCon :: SomeTypeRep -> TyCon
someTypeRepTyCon :: SomeTypeRep -> TyCon
someTypeRepTyCon (SomeTypeRep TypeRep a
t) = forall k (a :: k). TypeRep a -> TyCon
typeRepTyCon TypeRep a
t

-- | Observe the type constructor of a type representation
typeRepTyCon :: TypeRep a -> TyCon
typeRepTyCon :: forall k (a :: k). TypeRep a -> TyCon
typeRepTyCon TypeRep a
TrType = TyCon
tyConTYPE
typeRepTyCon (TrTyCon {trTyCon :: forall k (a :: k). TypeRep a -> TyCon
trTyCon = TyCon
tc}) = TyCon
tc
typeRepTyCon (TrApp {trAppFun :: forall m k2 (a :: m -> k2) (x :: m). TypeRep (a x) -> TypeRep a
trAppFun = TypeRep a
a})   = forall k (a :: k). TypeRep a -> TyCon
typeRepTyCon TypeRep a
a
typeRepTyCon (TrFun {})               = forall k (a :: k). TypeRep a -> TyCon
typeRepTyCon forall a b. (a -> b) -> a -> b
$ forall {k} (a :: k). Typeable a => TypeRep a
typeRep @(->)

-- | Type equality
--
-- @since 4.10
eqTypeRep :: forall k1 k2 (a :: k1) (b :: k2).
             TypeRep a -> TypeRep b -> Maybe (a :~~: b)
eqTypeRep :: forall k1 k2 (a :: k1) (b :: k2).
TypeRep a -> TypeRep b -> Maybe (a :~~: b)
eqTypeRep TypeRep a
a TypeRep b
b
  | forall k1 k2 (a :: k1) (b :: k2). TypeRep a -> TypeRep b -> Bool
sameTypeRep TypeRep a
a TypeRep b
b = forall a. a -> Maybe a
Just (forall a b. a -> b
unsafeCoerce forall {k1} (a :: k1). a :~~: a
HRefl)
  | Bool
otherwise       = forall a. Maybe a
Nothing
-- We want GHC to inline eqTypeRep to get rid of the Maybe
-- in the usual case that it is scrutinized immediately. We
-- split eqTypeRep into a worker and wrapper because otherwise
-- it's much larger than anything we'd want to inline.
{-# INLINABLE eqTypeRep #-}

sameTypeRep :: forall k1 k2 (a :: k1) (b :: k2).
               TypeRep a -> TypeRep b -> Bool
sameTypeRep :: forall k1 k2 (a :: k1) (b :: k2). TypeRep a -> TypeRep b -> Bool
sameTypeRep TypeRep a
a TypeRep b
b = forall k (a :: k). TypeRep a -> Fingerprint
typeRepFingerprint TypeRep a
a forall a. Eq a => a -> a -> Bool
== forall k (a :: k). TypeRep a -> Fingerprint
typeRepFingerprint TypeRep b
b

-------------------------------------------------------------
--
--      Computing kinds
--
-------------------------------------------------------------

-- | Observe the kind of a type.
typeRepKind :: TypeRep (a :: k) -> TypeRep k
typeRepKind :: forall k (a :: k). TypeRep a -> TypeRep k
typeRepKind TypeRep a
TrType = TypeRep (*)
TrType
typeRepKind (TrTyCon {trTyConKind :: forall k (a :: k). TypeRep a -> TypeRep k
trTyConKind = TypeRep k
kind}) = TypeRep k
kind
typeRepKind (TrApp {trAppKind :: forall m k2 (a :: m -> k2) (x :: m). TypeRep (a x) -> TypeRep k2
trAppKind = TypeRep k
kind}) = TypeRep k
kind
typeRepKind (TrFun {}) = forall {k} (a :: k). Typeable a => TypeRep a
typeRep @Type

tyConKind :: TyCon -> [SomeTypeRep] -> SomeTypeRep
tyConKind :: TyCon -> [SomeTypeRep] -> SomeTypeRep
tyConKind (TyCon Word#
_ Word#
_ Module
_ TrName
_ Int#
nKindVars# KindRep
kindRep) [SomeTypeRep]
kindVars =
    let kindVarsArr :: A.Array KindBndr SomeTypeRep
        kindVarsArr :: Array KindBndr SomeTypeRep
kindVarsArr = forall i e. Ix i => (i, i) -> [e] -> Array i e
A.listArray (KindBndr
0, Int# -> KindBndr
I# (Int#
nKindVars# Int# -> Int# -> Int#
-# Int#
1#)) [SomeTypeRep]
kindVars
    in Array KindBndr SomeTypeRep -> KindRep -> SomeTypeRep
instantiateKindRep Array KindBndr SomeTypeRep
kindVarsArr KindRep
kindRep

instantiateKindRep :: A.Array KindBndr SomeTypeRep -> KindRep -> SomeTypeRep
instantiateKindRep :: Array KindBndr SomeTypeRep -> KindRep -> SomeTypeRep
instantiateKindRep Array KindBndr SomeTypeRep
vars = KindRep -> SomeTypeRep
go
  where
    go :: KindRep -> SomeTypeRep
    go :: KindRep -> SomeTypeRep
go (KindRepTyConApp TyCon
tc [KindRep]
args)
      = let n_kind_args :: KindBndr
n_kind_args = TyCon -> KindBndr
tyConKindArgs TyCon
tc
            ([KindRep]
kind_args, [KindRep]
ty_args) = forall a. KindBndr -> [a] -> ([a], [a])
splitAt KindBndr
n_kind_args [KindRep]
args
            -- First instantiate tycon kind arguments
            tycon_app :: SomeTypeRep
tycon_app = forall m (a :: m). TypeRep a -> SomeTypeRep
SomeTypeRep forall a b. (a -> b) -> a -> b
$ forall k (a :: k). TyCon -> [SomeTypeRep] -> TypeRep a
mkTrCon TyCon
tc (forall a b. (a -> b) -> [a] -> [b]
map KindRep -> SomeTypeRep
go [KindRep]
kind_args)
            -- Then apply remaining type arguments
            applyTy :: SomeTypeRep -> KindRep -> SomeTypeRep
            applyTy :: SomeTypeRep -> KindRep -> SomeTypeRep
applyTy (SomeTypeRep TypeRep a
acc) KindRep
ty
              | SomeTypeRep TypeRep a
ty' <- KindRep -> SomeTypeRep
go KindRep
ty
              = forall m (a :: m). TypeRep a -> SomeTypeRep
SomeTypeRep forall a b. (a -> b) -> a -> b
$ forall k1 k2 (a :: k1 -> k2) (b :: k1).
TypeRep a -> TypeRep b -> TypeRep (a b)
mkTrApp (forall a b. a -> b
unsafeCoerce TypeRep a
acc) TypeRep a
ty'
        in forall a b. (b -> a -> b) -> b -> [a] -> b
foldl' SomeTypeRep -> KindRep -> SomeTypeRep
applyTy SomeTypeRep
tycon_app [KindRep]
ty_args
    go (KindRepVar KindBndr
var)
      = Array KindBndr SomeTypeRep
vars forall i e. Ix i => Array i e -> i -> e
A.! KindBndr
var
    go (KindRepApp KindRep
f KindRep
a)
      = forall m (a :: m). TypeRep a -> SomeTypeRep
SomeTypeRep forall a b. (a -> b) -> a -> b
$ forall k1 k2 (a :: k1 -> k2) (b :: k1).
TypeRep a -> TypeRep b -> TypeRep (a b)
mkTrApp (forall {k} (a :: k). SomeTypeRep -> TypeRep a
unsafeCoerceRep forall a b. (a -> b) -> a -> b
$ KindRep -> SomeTypeRep
go KindRep
f) (forall {k} (a :: k). SomeTypeRep -> TypeRep a
unsafeCoerceRep forall a b. (a -> b) -> a -> b
$ KindRep -> SomeTypeRep
go KindRep
a)
    go (KindRepFun KindRep
a KindRep
b)
      = forall m (a :: m). TypeRep a -> SomeTypeRep
SomeTypeRep forall a b. (a -> b) -> a -> b
$ forall (m :: Multiplicity) a b.
TypeRep m -> TypeRep a -> TypeRep b -> TypeRep (a %m -> b)
mkTrFun TypeRep 'Many
trMany (forall {k} (a :: k). SomeTypeRep -> TypeRep a
unsafeCoerceRep forall a b. (a -> b) -> a -> b
$ KindRep -> SomeTypeRep
go KindRep
a) (forall {k} (a :: k). SomeTypeRep -> TypeRep a
unsafeCoerceRep forall a b. (a -> b) -> a -> b
$ KindRep -> SomeTypeRep
go KindRep
b)
    go (KindRepTYPE (BoxedRep Levity
Lifted)) = forall m (a :: m). TypeRep a -> SomeTypeRep
SomeTypeRep TypeRep (*)
TrType
    go (KindRepTYPE RuntimeRep
r) = forall k. SomeKindedTypeRep k -> SomeTypeRep
unkindedTypeRep forall a b. (a -> b) -> a -> b
$ SomeKindedTypeRep (RuntimeRep -> *)
tYPE forall k k'.
SomeKindedTypeRep (k -> k')
-> SomeKindedTypeRep k -> SomeKindedTypeRep k'
`kApp` RuntimeRep -> SomeKindedTypeRep RuntimeRep
runtimeRepTypeRep RuntimeRep
r
    go (KindRepTypeLitS TypeLitSort
sort Addr#
s)
      = TypeLitSort -> String -> SomeTypeRep
mkTypeLitFromString TypeLitSort
sort (Addr# -> String
unpackCStringUtf8# Addr#
s)
    go (KindRepTypeLitD TypeLitSort
sort String
s)
      = TypeLitSort -> String -> SomeTypeRep
mkTypeLitFromString TypeLitSort
sort String
s

    tYPE :: SomeKindedTypeRep (RuntimeRep -> *)
tYPE = forall k (a :: k). Typeable a => SomeKindedTypeRep k
kindedTypeRep @(RuntimeRep -> Type) @TYPE

unsafeCoerceRep :: SomeTypeRep -> TypeRep a
unsafeCoerceRep :: forall {k} (a :: k). SomeTypeRep -> TypeRep a
unsafeCoerceRep (SomeTypeRep TypeRep a
r) = forall a b. a -> b
unsafeCoerce TypeRep a
r

unkindedTypeRep :: SomeKindedTypeRep k -> SomeTypeRep
unkindedTypeRep :: forall k. SomeKindedTypeRep k -> SomeTypeRep
unkindedTypeRep (SomeKindedTypeRep TypeRep a
x) = forall m (a :: m). TypeRep a -> SomeTypeRep
SomeTypeRep TypeRep a
x

data SomeKindedTypeRep k where
    SomeKindedTypeRep :: forall k (a :: k). TypeRep a
                      %1 -> SomeKindedTypeRep k

kApp :: SomeKindedTypeRep (k -> k')
     -> SomeKindedTypeRep k
     -> SomeKindedTypeRep k'
kApp :: forall k k'.
SomeKindedTypeRep (k -> k')
-> SomeKindedTypeRep k -> SomeKindedTypeRep k'
kApp (SomeKindedTypeRep TypeRep a
f) (SomeKindedTypeRep TypeRep a
a) =
    forall k (m :: k). TypeRep m -> SomeKindedTypeRep k
SomeKindedTypeRep (forall k1 k2 (a :: k1 -> k2) (b :: k1).
TypeRep a -> TypeRep b -> TypeRep (a b)
mkTrApp TypeRep a
f TypeRep a
a)

kindedTypeRep :: forall k (a :: k). Typeable a => SomeKindedTypeRep k
kindedTypeRep :: forall k (a :: k). Typeable a => SomeKindedTypeRep k
kindedTypeRep = forall k (m :: k). TypeRep m -> SomeKindedTypeRep k
SomeKindedTypeRep (forall {k} (a :: k). Typeable a => TypeRep a
typeRep @a)

buildList :: forall k. Typeable k
          => [SomeKindedTypeRep k]
          -> SomeKindedTypeRep [k]
buildList :: forall k.
Typeable k =>
[SomeKindedTypeRep k] -> SomeKindedTypeRep [k]
buildList = forall a b. (a -> b -> b) -> b -> [a] -> b
foldr forall {k}.
Typeable k =>
SomeKindedTypeRep k
-> SomeKindedTypeRep [k] -> SomeKindedTypeRep [k]
cons SomeKindedTypeRep [k]
nil
  where
    nil :: SomeKindedTypeRep [k]
nil = forall k (a :: k). Typeable a => SomeKindedTypeRep k
kindedTypeRep @[k] @'[]
    cons :: SomeKindedTypeRep k
-> SomeKindedTypeRep [k] -> SomeKindedTypeRep [k]
cons SomeKindedTypeRep k
x SomeKindedTypeRep [k]
rest = forall k (m :: k). TypeRep m -> SomeKindedTypeRep k
SomeKindedTypeRep (forall {k} (a :: k). Typeable a => TypeRep a
typeRep @'(:)) forall k k'.
SomeKindedTypeRep (k -> k')
-> SomeKindedTypeRep k -> SomeKindedTypeRep k'
`kApp` SomeKindedTypeRep k
x forall k k'.
SomeKindedTypeRep (k -> k')
-> SomeKindedTypeRep k -> SomeKindedTypeRep k'
`kApp` SomeKindedTypeRep [k]
rest

runtimeRepTypeRep :: RuntimeRep -> SomeKindedTypeRep RuntimeRep
runtimeRepTypeRep :: RuntimeRep -> SomeKindedTypeRep RuntimeRep
runtimeRepTypeRep RuntimeRep
r =
    case RuntimeRep
r of
      BoxedRep Levity
Lifted -> forall k (m :: k). TypeRep m -> SomeKindedTypeRep k
SomeKindedTypeRep TypeRep ('BoxedRep 'Lifted)
trLiftedRep
      BoxedRep Levity
v  -> forall k (a :: k). Typeable a => SomeKindedTypeRep k
kindedTypeRep @_ @'BoxedRep
                     forall k k'.
SomeKindedTypeRep (k -> k')
-> SomeKindedTypeRep k -> SomeKindedTypeRep k'
`kApp` Levity -> SomeKindedTypeRep Levity
levityTypeRep Levity
v
      VecRep VecCount
c VecElem
e  -> forall k (a :: k). Typeable a => SomeKindedTypeRep k
kindedTypeRep @_ @'VecRep
                     forall k k'.
SomeKindedTypeRep (k -> k')
-> SomeKindedTypeRep k -> SomeKindedTypeRep k'
`kApp` VecCount -> SomeKindedTypeRep VecCount
vecCountTypeRep VecCount
c
                     forall k k'.
SomeKindedTypeRep (k -> k')
-> SomeKindedTypeRep k -> SomeKindedTypeRep k'
`kApp` VecElem -> SomeKindedTypeRep VecElem
vecElemTypeRep VecElem
e
      TupleRep [RuntimeRep]
rs -> forall k (a :: k). Typeable a => SomeKindedTypeRep k
kindedTypeRep @_ @'TupleRep
                     forall k k'.
SomeKindedTypeRep (k -> k')
-> SomeKindedTypeRep k -> SomeKindedTypeRep k'
`kApp` forall k.
Typeable k =>
[SomeKindedTypeRep k] -> SomeKindedTypeRep [k]
buildList (forall a b. (a -> b) -> [a] -> [b]
map RuntimeRep -> SomeKindedTypeRep RuntimeRep
runtimeRepTypeRep [RuntimeRep]
rs)
      SumRep [RuntimeRep]
rs   -> forall k (a :: k). Typeable a => SomeKindedTypeRep k
kindedTypeRep @_ @'SumRep
                     forall k k'.
SomeKindedTypeRep (k -> k')
-> SomeKindedTypeRep k -> SomeKindedTypeRep k'
`kApp` forall k.
Typeable k =>
[SomeKindedTypeRep k] -> SomeKindedTypeRep [k]
buildList (forall a b. (a -> b) -> [a] -> [b]
map RuntimeRep -> SomeKindedTypeRep RuntimeRep
runtimeRepTypeRep [RuntimeRep]
rs)
      RuntimeRep
IntRep      -> Typeable LiftedRep => SomeKindedTypeRep RuntimeRep
rep @'IntRep
      RuntimeRep
Int8Rep     -> Typeable LiftedRep => SomeKindedTypeRep RuntimeRep
rep @'Int8Rep
      RuntimeRep
Int16Rep    -> Typeable LiftedRep => SomeKindedTypeRep RuntimeRep
rep @'Int16Rep
      RuntimeRep
Int32Rep    -> Typeable LiftedRep => SomeKindedTypeRep RuntimeRep
rep @'Int32Rep
      RuntimeRep
Int64Rep    -> Typeable LiftedRep => SomeKindedTypeRep RuntimeRep
rep @'Int64Rep
      RuntimeRep
WordRep     -> Typeable LiftedRep => SomeKindedTypeRep RuntimeRep
rep @'WordRep
      RuntimeRep
Word8Rep    -> Typeable LiftedRep => SomeKindedTypeRep RuntimeRep
rep @'Word8Rep
      RuntimeRep
Word16Rep   -> Typeable LiftedRep => SomeKindedTypeRep RuntimeRep
rep @'Word16Rep
      RuntimeRep
Word32Rep   -> Typeable LiftedRep => SomeKindedTypeRep RuntimeRep
rep @'Word32Rep
      RuntimeRep
Word64Rep   -> Typeable LiftedRep => SomeKindedTypeRep RuntimeRep
rep @'Word64Rep
      RuntimeRep
AddrRep     -> Typeable LiftedRep => SomeKindedTypeRep RuntimeRep
rep @'AddrRep
      RuntimeRep
FloatRep    -> Typeable LiftedRep => SomeKindedTypeRep RuntimeRep
rep @'FloatRep
      RuntimeRep
DoubleRep   -> Typeable LiftedRep => SomeKindedTypeRep RuntimeRep
rep @'DoubleRep
  where
    rep :: forall (a :: RuntimeRep). Typeable a => SomeKindedTypeRep RuntimeRep
    rep :: Typeable LiftedRep => SomeKindedTypeRep RuntimeRep
rep = forall k (a :: k). Typeable a => SomeKindedTypeRep k
kindedTypeRep @RuntimeRep @a

levityTypeRep :: Levity -> SomeKindedTypeRep Levity
levityTypeRep :: Levity -> SomeKindedTypeRep Levity
levityTypeRep Levity
c =
    case Levity
c of
      Levity
Lifted   -> forall (a :: Levity). Typeable a => SomeKindedTypeRep Levity
rep @'Lifted
      Levity
Unlifted -> forall (a :: Levity). Typeable a => SomeKindedTypeRep Levity
rep @'Unlifted
  where
    rep :: forall (a :: Levity). Typeable a => SomeKindedTypeRep Levity
    rep :: forall (a :: Levity). Typeable a => SomeKindedTypeRep Levity
rep = forall k (a :: k). Typeable a => SomeKindedTypeRep k
kindedTypeRep @Levity @a

vecCountTypeRep :: VecCount -> SomeKindedTypeRep VecCount
vecCountTypeRep :: VecCount -> SomeKindedTypeRep VecCount
vecCountTypeRep VecCount
c =
    case VecCount
c of
      VecCount
Vec2  -> forall (a :: VecCount). Typeable a => SomeKindedTypeRep VecCount
rep @'Vec2
      VecCount
Vec4  -> forall (a :: VecCount). Typeable a => SomeKindedTypeRep VecCount
rep @'Vec4
      VecCount
Vec8  -> forall (a :: VecCount). Typeable a => SomeKindedTypeRep VecCount
rep @'Vec8
      VecCount
Vec16 -> forall (a :: VecCount). Typeable a => SomeKindedTypeRep VecCount
rep @'Vec16
      VecCount
Vec32 -> forall (a :: VecCount). Typeable a => SomeKindedTypeRep VecCount
rep @'Vec32
      VecCount
Vec64 -> forall (a :: VecCount). Typeable a => SomeKindedTypeRep VecCount
rep @'Vec64
  where
    rep :: forall (a :: VecCount). Typeable a => SomeKindedTypeRep VecCount
    rep :: forall (a :: VecCount). Typeable a => SomeKindedTypeRep VecCount
rep = forall k (a :: k). Typeable a => SomeKindedTypeRep k
kindedTypeRep @VecCount @a

vecElemTypeRep :: VecElem -> SomeKindedTypeRep VecElem
vecElemTypeRep :: VecElem -> SomeKindedTypeRep VecElem
vecElemTypeRep VecElem
e =
    case VecElem
e of
      VecElem
Int8ElemRep     -> forall (a :: VecElem). Typeable a => SomeKindedTypeRep VecElem
rep @'Int8ElemRep
      VecElem
Int16ElemRep    -> forall (a :: VecElem). Typeable a => SomeKindedTypeRep VecElem
rep @'Int16ElemRep
      VecElem
Int32ElemRep    -> forall (a :: VecElem). Typeable a => SomeKindedTypeRep VecElem
rep @'Int32ElemRep
      VecElem
Int64ElemRep    -> forall (a :: VecElem). Typeable a => SomeKindedTypeRep VecElem
rep @'Int64ElemRep
      VecElem
Word8ElemRep    -> forall (a :: VecElem). Typeable a => SomeKindedTypeRep VecElem
rep @'Word8ElemRep
      VecElem
Word16ElemRep   -> forall (a :: VecElem). Typeable a => SomeKindedTypeRep VecElem
rep @'Word16ElemRep
      VecElem
Word32ElemRep   -> forall (a :: VecElem). Typeable a => SomeKindedTypeRep VecElem
rep @'Word32ElemRep
      VecElem
Word64ElemRep   -> forall (a :: VecElem). Typeable a => SomeKindedTypeRep VecElem
rep @'Word64ElemRep
      VecElem
FloatElemRep    -> forall (a :: VecElem). Typeable a => SomeKindedTypeRep VecElem
rep @'FloatElemRep
      VecElem
DoubleElemRep   -> forall (a :: VecElem). Typeable a => SomeKindedTypeRep VecElem
rep @'DoubleElemRep
  where
    rep :: forall (a :: VecElem). Typeable a => SomeKindedTypeRep VecElem
    rep :: forall (a :: VecElem). Typeable a => SomeKindedTypeRep VecElem
rep = forall k (a :: k). Typeable a => SomeKindedTypeRep k
kindedTypeRep @VecElem @a

bareArrow :: forall (m :: Multiplicity) (r1 :: RuntimeRep) (r2 :: RuntimeRep)
                    (a :: TYPE r1) (b :: TYPE r2). ()
          => TypeRep (FUN m a b)
          -> TypeRep (FUN m :: TYPE r1 -> TYPE r2 -> Type)
bareArrow :: forall (m :: Multiplicity) a b.
TypeRep (a %m -> b) -> TypeRep (FUN m)
bareArrow (TrFun Fingerprint
_ TypeRep m
m TypeRep a
a TypeRep b
b) =
    forall k (a :: k). TyCon -> [SomeTypeRep] -> TypeRep a
mkTrCon TyCon
funTyCon [forall m (a :: m). TypeRep a -> SomeTypeRep
SomeTypeRep TypeRep m
m, forall m (a :: m). TypeRep a -> SomeTypeRep
SomeTypeRep TypeRep r1
rep1, forall m (a :: m). TypeRep a -> SomeTypeRep
SomeTypeRep TypeRep r2
rep2]
  where
    rep1 :: TypeRep r1
rep1 = TypeRep (*) -> TypeRep LiftedRep
getRuntimeRep forall a b. (a -> b) -> a -> b
$ forall k (a :: k). TypeRep a -> TypeRep k
typeRepKind TypeRep a
a :: TypeRep r1
    rep2 :: TypeRep r2
rep2 = TypeRep (*) -> TypeRep LiftedRep
getRuntimeRep forall a b. (a -> b) -> a -> b
$ forall k (a :: k). TypeRep a -> TypeRep k
typeRepKind TypeRep b
b :: TypeRep r2
bareArrow TypeRep (a %m -> b)
_ = forall a. HasCallStack => String -> a
error String
"Data.Typeable.Internal.bareArrow: impossible"

data IsTYPE (a :: Type) where
    IsTYPE :: forall (r :: RuntimeRep). TypeRep r %1 -> IsTYPE (TYPE r)

-- | Is a type of the form @TYPE rep@?
isTYPE :: TypeRep (a :: Type) -> Maybe (IsTYPE a)
isTYPE :: forall a. TypeRep a -> Maybe (IsTYPE a)
isTYPE TypeRep a
TrType = forall a. a -> Maybe a
Just (TypeRep LiftedRep -> IsTYPE (*)
IsTYPE TypeRep ('BoxedRep 'Lifted)
trLiftedRep)
isTYPE (TrApp {trAppFun :: forall m k2 (a :: m -> k2) (x :: m). TypeRep (a x) -> TypeRep a
trAppFun=TypeRep a
f, trAppArg :: forall m k2 (a :: m -> k2) (x :: m). TypeRep (a x) -> TypeRep x
trAppArg=TypeRep b
r})
  | Just a :~~: TYPE
HRefl <- TypeRep a
f forall k1 k2 (a :: k1) (b :: k2).
TypeRep a -> TypeRep b -> Maybe (a :~~: b)
`eqTypeRep` forall {k} (a :: k). Typeable a => TypeRep a
typeRep @TYPE
  = forall a. a -> Maybe a
Just (TypeRep LiftedRep -> IsTYPE (*)
IsTYPE TypeRep b
r)
isTYPE TypeRep a
_ = forall a. Maybe a
Nothing

getRuntimeRep :: forall (r :: RuntimeRep). TypeRep (TYPE r) -> TypeRep r
getRuntimeRep :: TypeRep (*) -> TypeRep LiftedRep
getRuntimeRep TypeRep (TYPE r)
TrType = TypeRep ('BoxedRep 'Lifted)
trLiftedRep
getRuntimeRep (TrApp {trAppArg :: forall m k2 (a :: m -> k2) (x :: m). TypeRep (a x) -> TypeRep x
trAppArg=TypeRep b
r}) = TypeRep b
r
getRuntimeRep TypeRep (TYPE r)
_ = forall a. HasCallStack => String -> a
error String
"Data.Typeable.Internal.getRuntimeRep: impossible"


-------------------------------------------------------------
--
--      The Typeable class and friends
--
-------------------------------------------------------------

-- | The class 'Typeable' allows a concrete representation of a type to
-- be calculated.
class Typeable (a :: k) where
  typeRep# :: TypeRep a

typeRep :: Typeable a => TypeRep a
typeRep :: forall {k} (a :: k). Typeable a => TypeRep a
typeRep = forall {k} (a :: k). Typeable a => TypeRep a
typeRep#

typeOf :: Typeable a => a -> TypeRep a
typeOf :: forall a. Typeable a => a -> TypeRep a
typeOf a
_ = forall {k} (a :: k). Typeable a => TypeRep a
typeRep

-- | Takes a value of type @a@ and returns a concrete representation
-- of that type.
--
-- @since 4.7.0.0
someTypeRep :: forall proxy a. Typeable a => proxy a -> SomeTypeRep
someTypeRep :: forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> SomeTypeRep
someTypeRep proxy a
_ = forall m (a :: m). TypeRep a -> SomeTypeRep
SomeTypeRep (forall {k} (a :: k). Typeable a => TypeRep a
typeRep :: TypeRep a)
{-# INLINE typeRep #-}

someTypeRepFingerprint :: SomeTypeRep -> Fingerprint
someTypeRepFingerprint :: SomeTypeRep -> Fingerprint
someTypeRepFingerprint (SomeTypeRep TypeRep a
t) = forall k (a :: k). TypeRep a -> Fingerprint
typeRepFingerprint TypeRep a
t

----------------- Showing TypeReps --------------------

-- This follows roughly the precedence structure described in Note [Precedence
-- in types].
instance Show (TypeRep (a :: k)) where
    showsPrec :: KindBndr -> TypeRep a -> ShowS
showsPrec = forall k (a :: k). KindBndr -> TypeRep a -> ShowS
showTypeable


showTypeable :: Int -> TypeRep (a :: k) -> ShowS
showTypeable :: forall k (a :: k). KindBndr -> TypeRep a -> ShowS
showTypeable KindBndr
_ TypeRep a
TrType = Char -> ShowS
showChar Char
'*'
showTypeable KindBndr
_ TypeRep a
rep
  | TyCon -> Bool
isListTyCon TyCon
tc, [SomeTypeRep
ty] <- [SomeTypeRep]
tys =
    Char -> ShowS
showChar Char
'[' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> ShowS
shows SomeTypeRep
ty forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
']'

    -- Take care only to render saturated tuple tycon applications
    -- with tuple notation (#14341).
  | TyCon -> Bool
isTupleTyCon TyCon
tc,
    Just * :~~: k
_ <- TypeRep (*)
TrType forall k1 k2 (a :: k1) (b :: k2).
TypeRep a -> TypeRep b -> Maybe (a :~~: b)
`eqTypeRep` forall k (a :: k). TypeRep a -> TypeRep k
typeRepKind TypeRep a
rep =
    Char -> ShowS
showChar Char
'(' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => ShowS -> [a] -> ShowS
showArgs (Char -> ShowS
showChar Char
',') [SomeTypeRep]
tys forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
')'
  where (TyCon
tc, [SomeTypeRep]
tys) = forall {k} (a :: k). TypeRep a -> (TyCon, [SomeTypeRep])
splitApps TypeRep a
rep
showTypeable KindBndr
_ (TrTyCon {trTyCon :: forall k (a :: k). TypeRep a -> TyCon
trTyCon = TyCon
tycon, trKindVars :: forall k (a :: k). TypeRep a -> [SomeTypeRep]
trKindVars = []})
  = TyCon -> ShowS
showTyCon TyCon
tycon
showTypeable KindBndr
p (TrTyCon {trTyCon :: forall k (a :: k). TypeRep a -> TyCon
trTyCon = TyCon
tycon, trKindVars :: forall k (a :: k). TypeRep a -> [SomeTypeRep]
trKindVars = [SomeTypeRep]
args})
  = Bool -> ShowS -> ShowS
showParen (KindBndr
p forall a. Ord a => a -> a -> Bool
> KindBndr
9) forall a b. (a -> b) -> a -> b
$
    TyCon -> ShowS
showTyCon TyCon
tycon forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    Char -> ShowS
showChar Char
' ' forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    forall a. Show a => ShowS -> [a] -> ShowS
showArgs (Char -> ShowS
showChar Char
' ') [SomeTypeRep]
args
showTypeable KindBndr
p (TrFun {trFunArg :: forall (m :: Multiplicity) a b.
TypeRep (LiftedRep %m -> b) -> TypeRep LiftedRep
trFunArg = TypeRep a
x, trFunRes :: forall (m :: Multiplicity) a b.
TypeRep (LiftedRep %m -> b) -> TypeRep b
trFunRes = TypeRep b
r})
  = Bool -> ShowS -> ShowS
showParen (KindBndr
p forall a. Ord a => a -> a -> Bool
> KindBndr
8) forall a b. (a -> b) -> a -> b
$
    forall a. Show a => KindBndr -> a -> ShowS
showsPrec KindBndr
9 TypeRep a
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
" -> " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => KindBndr -> a -> ShowS
showsPrec KindBndr
8 TypeRep b
r
showTypeable KindBndr
p (TrApp {trAppFun :: forall m k2 (a :: m -> k2) (x :: m). TypeRep (a x) -> TypeRep a
trAppFun = TypeRep a
f, trAppArg :: forall m k2 (a :: m -> k2) (x :: m). TypeRep (a x) -> TypeRep x
trAppArg = TypeRep b
x})
  = Bool -> ShowS -> ShowS
showParen (KindBndr
p forall a. Ord a => a -> a -> Bool
> KindBndr
9) forall a b. (a -> b) -> a -> b
$
    forall a. Show a => KindBndr -> a -> ShowS
showsPrec KindBndr
8 TypeRep a
f forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    Char -> ShowS
showChar Char
' ' forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    forall a. Show a => KindBndr -> a -> ShowS
showsPrec KindBndr
10 TypeRep b
x

-- | @since 4.10.0.0
instance Show SomeTypeRep where
  showsPrec :: KindBndr -> SomeTypeRep -> ShowS
showsPrec KindBndr
p (SomeTypeRep TypeRep a
ty) = forall a. Show a => KindBndr -> a -> ShowS
showsPrec KindBndr
p TypeRep a
ty

splitApps :: TypeRep a -> (TyCon, [SomeTypeRep])
splitApps :: forall {k} (a :: k). TypeRep a -> (TyCon, [SomeTypeRep])
splitApps = forall {k} (a :: k).
[SomeTypeRep] -> TypeRep a -> (TyCon, [SomeTypeRep])
go []
  where
    go :: [SomeTypeRep] -> TypeRep a -> (TyCon, [SomeTypeRep])
    go :: forall {k} (a :: k).
[SomeTypeRep] -> TypeRep a -> (TyCon, [SomeTypeRep])
go [SomeTypeRep]
xs (TrTyCon {trTyCon :: forall k (a :: k). TypeRep a -> TyCon
trTyCon = TyCon
tc})
      = (TyCon
tc, [SomeTypeRep]
xs)
    go [SomeTypeRep]
xs (TrApp {trAppFun :: forall m k2 (a :: m -> k2) (x :: m). TypeRep (a x) -> TypeRep a
trAppFun = TypeRep a
f, trAppArg :: forall m k2 (a :: m -> k2) (x :: m). TypeRep (a x) -> TypeRep x
trAppArg = TypeRep b
x})
      = forall {k} (a :: k).
[SomeTypeRep] -> TypeRep a -> (TyCon, [SomeTypeRep])
go (forall m (a :: m). TypeRep a -> SomeTypeRep
SomeTypeRep TypeRep b
x forall a. a -> [a] -> [a]
: [SomeTypeRep]
xs) TypeRep a
f
    go [] (TrFun {trFunArg :: forall (m :: Multiplicity) a b.
TypeRep (LiftedRep %m -> b) -> TypeRep LiftedRep
trFunArg = TypeRep a
a, trFunRes :: forall (m :: Multiplicity) a b.
TypeRep (LiftedRep %m -> b) -> TypeRep b
trFunRes = TypeRep b
b, trFunMul :: forall (m :: Multiplicity) a b.
TypeRep (LiftedRep %m -> b) -> TypeRep m
trFunMul = TypeRep m
mul})
      | Just 'Many :~~: m
HRefl <- forall k1 k2 (a :: k1) (b :: k2).
TypeRep a -> TypeRep b -> Maybe (a :~~: b)
eqTypeRep TypeRep 'Many
trMany TypeRep m
mul = (TyCon
funTyCon, [forall m (a :: m). TypeRep a -> SomeTypeRep
SomeTypeRep TypeRep a
a, forall m (a :: m). TypeRep a -> SomeTypeRep
SomeTypeRep TypeRep b
b])
      | Bool
otherwise = forall a. String -> a
errorWithoutStackTrace String
"Data.Typeable.Internal.splitApps: Only unrestricted functions are supported"
    go [SomeTypeRep]
_  (TrFun {})
      = forall a. String -> a
errorWithoutStackTrace String
"Data.Typeable.Internal.splitApps: Impossible 1"
    go [] TypeRep a
TrType = (TyCon
tyConTYPE, [forall m (a :: m). TypeRep a -> SomeTypeRep
SomeTypeRep TypeRep ('BoxedRep 'Lifted)
trLiftedRep])
    go [SomeTypeRep]
_ TypeRep a
TrType
      = forall a. String -> a
errorWithoutStackTrace String
"Data.Typeable.Internal.splitApps: Impossible 2"

-- This is incredibly shady! We don't really want to do this here; we
-- should really have the compiler reveal the TYPE TyCon directly
-- somehow. We need to construct this by hand because otherwise
-- we end up with horrible and somewhat mysterious loops trying to calculate
-- typeRep @TYPE. For the moment, we use the fact that we can get the proper
-- name of the ghc-prim package from the TyCon of LiftedRep (which we can
-- produce a TypeRep for without difficulty), and then just substitute in the
-- appropriate module and constructor names.
--
-- Prior to the introduction of BoxedRep, this was bad, but now it is
-- even worse! We have to construct several different TyCons by hand
-- so that we can build the fingerprint for TYPE ('BoxedRep 'LiftedRep).
-- If we call `typeRep @('BoxedRep 'LiftedRep)` while trying to compute
-- the fingerprint of `TYPE ('BoxedRep 'LiftedRep)`, we get a loop.
--
-- The ticket to find a better way to deal with this is
-- #14480.

tyConRuntimeRep :: TyCon
tyConRuntimeRep :: TyCon
tyConRuntimeRep = String -> String -> String -> KindBndr -> KindRep -> TyCon
mkTyCon String
ghcPrimPackage String
"GHC.Types" String
"RuntimeRep" KindBndr
0
  (RuntimeRep -> KindRep
KindRepTYPE (Levity -> RuntimeRep
BoxedRep Levity
Lifted))

tyConTYPE :: TyCon
tyConTYPE :: TyCon
tyConTYPE = String -> String -> String -> KindBndr -> KindRep -> TyCon
mkTyCon String
ghcPrimPackage String
"GHC.Prim" String
"TYPE" KindBndr
0
    (KindRep -> KindRep -> KindRep
KindRepFun
      (TyCon -> [KindRep] -> KindRep
KindRepTyConApp TyCon
tyConRuntimeRep [])
      (RuntimeRep -> KindRep
KindRepTYPE (Levity -> RuntimeRep
BoxedRep Levity
Lifted))
    )

tyConLevity :: TyCon
tyConLevity :: TyCon
tyConLevity = String -> String -> String -> KindBndr -> KindRep -> TyCon
mkTyCon String
ghcPrimPackage String
"GHC.Types" String
"Levity" KindBndr
0
  (RuntimeRep -> KindRep
KindRepTYPE (Levity -> RuntimeRep
BoxedRep Levity
Lifted))

tyCon'Lifted :: TyCon
tyCon'Lifted :: TyCon
tyCon'Lifted = String -> String -> String -> KindBndr -> KindRep -> TyCon
mkTyCon String
ghcPrimPackage String
"GHC.Types" String
"'Lifted" KindBndr
0
  (TyCon -> [KindRep] -> KindRep
KindRepTyConApp TyCon
tyConLevity [])

tyCon'BoxedRep :: TyCon
tyCon'BoxedRep :: TyCon
tyCon'BoxedRep = String -> String -> String -> KindBndr -> KindRep -> TyCon
mkTyCon String
ghcPrimPackage String
"GHC.Types" String
"'BoxedRep" KindBndr
0
  (KindRep -> KindRep -> KindRep
KindRepFun (TyCon -> [KindRep] -> KindRep
KindRepTyConApp TyCon
tyConLevity []) (TyCon -> [KindRep] -> KindRep
KindRepTyConApp TyCon
tyConRuntimeRep []))

ghcPrimPackage :: String
ghcPrimPackage :: String
ghcPrimPackage = TyCon -> String
tyConPackage (forall k (a :: k). TypeRep a -> TyCon
typeRepTyCon (forall {k} (a :: k). Typeable a => TypeRep a
typeRep @Bool))

funTyCon :: TyCon
funTyCon :: TyCon
funTyCon = forall k (a :: k). TypeRep a -> TyCon
typeRepTyCon (forall {k} (a :: k). Typeable a => TypeRep a
typeRep @(->))

isListTyCon :: TyCon -> Bool
isListTyCon :: TyCon -> Bool
isListTyCon TyCon
tc = TyCon
tc forall a. Eq a => a -> a -> Bool
== forall k (a :: k). TypeRep a -> TyCon
typeRepTyCon (forall {k} (a :: k). Typeable a => TypeRep a
typeRep :: TypeRep [])

isTupleTyCon :: TyCon -> Bool
isTupleTyCon :: TyCon -> Bool
isTupleTyCon TyCon
tc
  | (Char
'(':Char
',':String
_) <- TyCon -> String
tyConName TyCon
tc = Bool
True
  | Bool
otherwise                   = Bool
False

-- This is only an approximation. We don't have the general
-- character-classification machinery here, so we just do our best.
-- This should work for promoted Haskell 98 data constructors and
-- for TypeOperators type constructors that begin with ASCII
-- characters, but it will miss Unicode operators.
--
-- If we wanted to catch Unicode as well, we ought to consider moving
-- GHC.Lexeme from ghc-boot-th to base. Then we could just say:
--
--   startsVarSym symb || startsConSym symb
--
-- But this is a fair deal of work just for one corner case, so I think I'll
-- leave it like this unless someone shouts.
isOperatorTyCon :: TyCon -> Bool
isOperatorTyCon :: TyCon -> Bool
isOperatorTyCon TyCon
tc
  | Char
symb : String
_ <- TyCon -> String
tyConName TyCon
tc
  , Char
symb forall a. Eq a => a -> [a] -> Bool
`elem` String
"!#$%&*+./<=>?@\\^|-~:" = Bool
True
  | Bool
otherwise                           = Bool
False

showTyCon :: TyCon -> ShowS
showTyCon :: TyCon -> ShowS
showTyCon TyCon
tycon = Bool -> ShowS -> ShowS
showParen (TyCon -> Bool
isOperatorTyCon TyCon
tycon) (forall a. Show a => a -> ShowS
shows TyCon
tycon)

showArgs :: Show a => ShowS -> [a] -> ShowS
showArgs :: forall a. Show a => ShowS -> [a] -> ShowS
showArgs ShowS
_   []     = forall a. a -> a
id
showArgs ShowS
_   [a
a]    = forall a. Show a => KindBndr -> a -> ShowS
showsPrec KindBndr
10 a
a
showArgs ShowS
sep (a
a:[a]
as) = forall a. Show a => KindBndr -> a -> ShowS
showsPrec KindBndr
10 a
a forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShowS
sep forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => ShowS -> [a] -> ShowS
showArgs ShowS
sep [a]
as

-- | Helper to fully evaluate 'TypeRep' for use as @NFData(rnf)@ implementation
--
-- @since 4.8.0.0
rnfTypeRep :: TypeRep a -> ()
-- The TypeRep structure is almost entirely strict by definition. The
-- fingerprinting and strict kind caching ensure that everything
-- else is forced anyway. So we don't need to do anything special
-- to reduce to normal form.
rnfTypeRep :: forall {k} (a :: k). TypeRep a -> ()
rnfTypeRep !TypeRep a
_ = ()

-- | Helper to fully evaluate 'SomeTypeRep' for use as @NFData(rnf)@
-- implementation
--
-- @since 4.10.0.0
rnfSomeTypeRep :: SomeTypeRep -> ()
rnfSomeTypeRep :: SomeTypeRep -> ()
rnfSomeTypeRep (SomeTypeRep TypeRep a
r) = forall {k} (a :: k). TypeRep a -> ()
rnfTypeRep TypeRep a
r

{- *********************************************************
*                                                          *
*       TyCon/TypeRep definitions for type literals        *
*              (Symbol and Nat)                            *
*                                                          *
********************************************************* -}

pattern KindRepTypeLit :: TypeLitSort -> String -> KindRep
pattern $mKindRepTypeLit :: forall {r}.
KindRep -> (TypeLitSort -> String -> r) -> ((# #) -> r) -> r
$bKindRepTypeLit :: TypeLitSort -> String -> KindRep
KindRepTypeLit sort t <- (getKindRepTypeLit -> Just (sort, t))
  where
    KindRepTypeLit TypeLitSort
sort String
t = TypeLitSort -> String -> KindRep
KindRepTypeLitD TypeLitSort
sort String
t

{-# COMPLETE KindRepTyConApp, KindRepVar, KindRepApp, KindRepFun,
             KindRepTYPE, KindRepTypeLit #-}

getKindRepTypeLit :: KindRep -> Maybe (TypeLitSort, String)
getKindRepTypeLit :: KindRep -> Maybe (TypeLitSort, String)
getKindRepTypeLit (KindRepTypeLitS TypeLitSort
sort Addr#
t) = forall a. a -> Maybe a
Just (TypeLitSort
sort, Addr# -> String
unpackCStringUtf8# Addr#
t)
getKindRepTypeLit (KindRepTypeLitD TypeLitSort
sort String
t) = forall a. a -> Maybe a
Just (TypeLitSort
sort, String
t)
getKindRepTypeLit KindRep
_                        = forall a. Maybe a
Nothing

-- | Exquisitely unsafe.
mkTyCon# :: Addr#       -- ^ package name
         -> Addr#       -- ^ module name
         -> Addr#       -- ^ the name of the type constructor
         -> Int#        -- ^ number of kind variables
         -> KindRep     -- ^ kind representation
         -> TyCon       -- ^ A unique 'TyCon' object
mkTyCon# :: Addr# -> Addr# -> Addr# -> Int# -> KindRep -> TyCon
mkTyCon# Addr#
pkg Addr#
modl Addr#
name Int#
n_kinds KindRep
kind_rep
  | Fingerprint (W64# Word#
hi) (W64# Word#
lo) <- Fingerprint
fingerprint
  = Word# -> Word# -> Module -> TrName -> Int# -> KindRep -> TyCon
TyCon Word#
hi Word#
lo Module
mod (Addr# -> TrName
TrNameS Addr#
name) Int#
n_kinds KindRep
kind_rep
  where
    mod :: Module
mod = TrName -> TrName -> Module
Module (Addr# -> TrName
TrNameS Addr#
pkg) (Addr# -> TrName
TrNameS Addr#
modl)
    fingerprint :: Fingerprint
    fingerprint :: Fingerprint
fingerprint = String -> String -> String -> Fingerprint
mkTyConFingerprint (Addr# -> String
unpackCStringUtf8# Addr#
pkg)
                                     (Addr# -> String
unpackCStringUtf8# Addr#
modl)
                                     (Addr# -> String
unpackCStringUtf8# Addr#
name)

-- it is extremely important that this fingerprint computation
-- remains in sync with that in GHC.Tc.Instance.Typeable to ensure that type
-- equality is correct.

-- | Exquisitely unsafe.
mkTyCon :: String       -- ^ package name
        -> String       -- ^ module name
        -> String       -- ^ the name of the type constructor
        -> Int         -- ^ number of kind variables
        -> KindRep     -- ^ kind representation
        -> TyCon        -- ^ A unique 'TyCon' object
-- Used when the strings are dynamically allocated,
-- eg from binary deserialisation
mkTyCon :: String -> String -> String -> KindBndr -> KindRep -> TyCon
mkTyCon String
pkg String
modl String
name (I# Int#
n_kinds) KindRep
kind_rep
  | Fingerprint (W64# Word#
hi) (W64# Word#
lo) <- Fingerprint
fingerprint
  = Word# -> Word# -> Module -> TrName -> Int# -> KindRep -> TyCon
TyCon Word#
hi Word#
lo Module
mod (String -> TrName
TrNameD String
name) Int#
n_kinds KindRep
kind_rep
  where
    mod :: Module
mod = TrName -> TrName -> Module
Module (String -> TrName
TrNameD String
pkg) (String -> TrName
TrNameD String
modl)
    fingerprint :: Fingerprint
    fingerprint :: Fingerprint
fingerprint = String -> String -> String -> Fingerprint
mkTyConFingerprint String
pkg String
modl String
name

-- This must match the computation done in GHC.Tc.Instance.Typeable.mkTyConRepTyConRHS.
mkTyConFingerprint :: String -- ^ package name
                   -> String -- ^ module name
                   -> String -- ^ tycon name
                   -> Fingerprint
mkTyConFingerprint :: String -> String -> String -> Fingerprint
mkTyConFingerprint String
pkg_name String
mod_name String
tycon_name =
        [Fingerprint] -> Fingerprint
fingerprintFingerprints
        [ String -> Fingerprint
fingerprintString String
pkg_name
        , String -> Fingerprint
fingerprintString String
mod_name
        , String -> Fingerprint
fingerprintString String
tycon_name
        ]

mkTypeLitTyCon :: String -> TyCon -> TyCon
mkTypeLitTyCon :: String -> TyCon -> TyCon
mkTypeLitTyCon String
name TyCon
kind_tycon
  = String -> String -> String -> KindBndr -> KindRep -> TyCon
mkTyCon String
"base" String
"GHC.TypeLits" String
name KindBndr
0 KindRep
kind
  where kind :: KindRep
kind = TyCon -> [KindRep] -> KindRep
KindRepTyConApp TyCon
kind_tycon []

-- | Used to make `'Typeable' instance for things of kind Nat
typeNatTypeRep :: forall a. KnownNat a => TypeRep a
typeNatTypeRep :: forall (a :: Nat). KnownNat a => TypeRep a
typeNatTypeRep = forall k (a :: k). Typeable k => String -> TyCon -> TypeRep a
typeLitTypeRep (forall a. Show a => a -> String
show (forall (n :: Nat). KnownNat n => Proxy# n -> Nat
natVal' (forall {k} (a :: k). Proxy# a
proxy# @a))) TyCon
tcNat

-- | Used to make `'Typeable' instance for things of kind Symbol
typeSymbolTypeRep :: forall a. KnownSymbol a => TypeRep a
typeSymbolTypeRep :: forall (a :: Symbol). KnownSymbol a => TypeRep a
typeSymbolTypeRep = forall k (a :: k). Typeable k => String -> TyCon -> TypeRep a
typeLitTypeRep (forall a. Show a => a -> String
show (forall (n :: Symbol). KnownSymbol n => Proxy# n -> String
symbolVal' (forall {k} (a :: k). Proxy# a
proxy# @a))) TyCon
tcSymbol

-- | Used to make `'Typeable' instance for things of kind Char
typeCharTypeRep :: forall a. KnownChar a => TypeRep a
typeCharTypeRep :: forall (a :: Char). KnownChar a => TypeRep a
typeCharTypeRep = forall k (a :: k). Typeable k => String -> TyCon -> TypeRep a
typeLitTypeRep (forall a. Show a => a -> String
show (forall (n :: Char). KnownChar n => Proxy# n -> Char
charVal' (forall {k} (a :: k). Proxy# a
proxy# @a))) TyCon
tcChar

mkTypeLitFromString :: TypeLitSort -> String -> SomeTypeRep
mkTypeLitFromString :: TypeLitSort -> String -> SomeTypeRep
mkTypeLitFromString TypeLitSort
TypeLitSymbol String
s =
    forall m (a :: m). TypeRep a -> SomeTypeRep
SomeTypeRep forall a b. (a -> b) -> a -> b
$ (forall k (a :: k). Typeable k => String -> TyCon -> TypeRep a
typeLitTypeRep String
s TyCon
tcSymbol :: TypeRep Symbol)
mkTypeLitFromString TypeLitSort
TypeLitNat String
s =
    forall m (a :: m). TypeRep a -> SomeTypeRep
SomeTypeRep forall a b. (a -> b) -> a -> b
$ (forall k (a :: k). Typeable k => String -> TyCon -> TypeRep a
typeLitTypeRep String
s TyCon
tcNat :: TypeRep Nat)
mkTypeLitFromString TypeLitSort
TypeLitChar String
s =
    forall m (a :: m). TypeRep a -> SomeTypeRep
SomeTypeRep forall a b. (a -> b) -> a -> b
$ (forall k (a :: k). Typeable k => String -> TyCon -> TypeRep a
typeLitTypeRep String
s TyCon
tcChar :: TypeRep Char)

tcSymbol :: TyCon
tcSymbol :: TyCon
tcSymbol = forall k (a :: k). TypeRep a -> TyCon
typeRepTyCon (forall {k} (a :: k). Typeable a => TypeRep a
typeRep @Symbol)

tcNat :: TyCon
tcNat :: TyCon
tcNat = forall k (a :: k). TypeRep a -> TyCon
typeRepTyCon (forall {k} (a :: k). Typeable a => TypeRep a
typeRep @Nat)

tcChar :: TyCon
tcChar :: TyCon
tcChar = forall k (a :: k). TypeRep a -> TyCon
typeRepTyCon (forall {k} (a :: k). Typeable a => TypeRep a
typeRep @Char)

-- | An internal function, to make representations for type literals.
typeLitTypeRep :: forall k (a :: k). (Typeable k) =>
                  String -> TyCon -> TypeRep a
typeLitTypeRep :: forall k (a :: k). Typeable k => String -> TyCon -> TypeRep a
typeLitTypeRep String
nm TyCon
kind_tycon = forall k (a :: k). TyCon -> [SomeTypeRep] -> TypeRep a
mkTrCon (String -> TyCon -> TyCon
mkTypeLitTyCon String
nm TyCon
kind_tycon) []

-- | For compiler use.
mkTrFun :: forall (m :: Multiplicity) (r1 :: RuntimeRep) (r2 :: RuntimeRep)
                  (a :: TYPE r1) (b :: TYPE r2).
           TypeRep m -> TypeRep a -> TypeRep b -> TypeRep ((FUN m a b) :: Type)
mkTrFun :: forall (m :: Multiplicity) a b.
TypeRep m -> TypeRep a -> TypeRep b -> TypeRep (a %m -> b)
mkTrFun TypeRep m
mul TypeRep a
arg TypeRep b
res = TrFun
    { trFunFingerprint :: Fingerprint
trFunFingerprint = Fingerprint
fpr
    , trFunMul :: TypeRep m
trFunMul = TypeRep m
mul
    , trFunArg :: TypeRep a
trFunArg = TypeRep a
arg
    , trFunRes :: TypeRep b
trFunRes = TypeRep b
res }
  where fpr :: Fingerprint
fpr = [Fingerprint] -> Fingerprint
fingerprintFingerprints [ forall k (a :: k). TypeRep a -> Fingerprint
typeRepFingerprint TypeRep m
mul
                                      , forall k (a :: k). TypeRep a -> Fingerprint
typeRepFingerprint TypeRep a
arg
                                      , forall k (a :: k). TypeRep a -> Fingerprint
typeRepFingerprint TypeRep b
res]

{- $kind_instantiation

Consider a type like 'Data.Proxy.Proxy',

@
data Proxy :: forall k. k -> Type
@

One might think that one could decompose an instantiation of this type like
@Proxy Int@ into two applications,

@
'App' (App a b) c === typeRep @(Proxy Int)
@

where,

@
a = typeRep @Proxy
b = typeRep @Type
c = typeRep @Int
@

However, this isn't the case. Instead we can only decompose into an application
and a constructor,

@
'App' ('Con' proxyTyCon) (typeRep @Int) === typeRep @(Proxy Int)
@

The reason for this is that 'Typeable' can only represent /kind-monomorphic/
types. That is, we must saturate enough of @Proxy@\'s arguments to
fully determine its kind. In the particular case of @Proxy@ this means we must
instantiate the kind variable @k@ such that no @forall@-quantified variables
remain.

While it is not possible to decompose the 'Con' above into an application, it is
possible to observe the kind variable instantiations of the constructor with the
'Con\'' pattern,

@
'App' (Con' proxyTyCon kinds) _ === typeRep @(Proxy Int)
@

Here @kinds@ will be @[typeRep \@Type]@.

-}