module TextShow.Data.Typeable (showbTyCon, showbTypeRepPrec) where
import Data.Monoid.Compat ((<>))
import Data.Text.Lazy.Builder (Builder, fromString, singleton)
import Data.Typeable (TypeRep, typeRepArgs, typeRepTyCon)
#if MIN_VERSION_base(4,4,0)
import Data.Typeable.Internal (TyCon, tyConName)
# if MIN_VERSION_base(4,8,0)
import Data.Typeable.Internal (typeRepKinds)
# endif
# if MIN_VERSION_base(4,9,0)
import Data.Typeable.Internal (tcFun, tcList)
# elif MIN_VERSION_base(4,4,0)
import Data.Typeable.Internal (funTc, listTc)
# endif
#else
import Data.Typeable (TyCon, mkTyCon, tyConString, typeOf)
#endif
import TextShow.Classes (TextShow(showb, showbPrec), showbParen, showbSpace)
import TextShow.Data.List ()
import TextShow.Data.Typeable.Utils (showbArgs, showbTuple)
import TextShow.Utils (isTupleString)
#include "inline.h"
showbTypeRepPrec :: Int -> TypeRep -> Builder
showbTypeRepPrec p tyrep =
case tys of
[] -> showbTyCon tycon
[x] | tycon == tcList -> singleton '[' <> showb x <> singleton ']'
[a,r] | tycon == tcFun -> showbParen (p > 8) $
showbPrec 9 a
<> " -> "
<> showbPrec 8 r
xs | isTupleTyCon tycon -> showbTuple xs
| otherwise -> showbParen (p > 9) $
showbPrec p tycon
<> showbSpace
<> showbArgs showbSpace
#if MIN_VERSION_base(4,8,0)
(kinds ++ tys)
#else
tys
#endif
where
tycon = typeRepTyCon tyrep
tys = typeRepArgs tyrep
#if MIN_VERSION_base(4,8,0)
kinds = typeRepKinds tyrep
#endif
#if !(MIN_VERSION_base(4,4,0))
tcList :: TyCon
tcList = typeRepTyCon $ typeOf [()]
tcFun :: TyCon
tcFun = mkTyCon "->"
#elif !(MIN_VERSION_base(4,9,0))
tcList :: TyCon
tcList = listTc
tcFun :: TyCon
tcFun = funTc
#endif
isTupleTyCon :: TyCon -> Bool
isTupleTyCon = isTupleString . tyConString
showbTyCon :: TyCon -> Builder
showbTyCon = fromString . tyConString
#if MIN_VERSION_base(4,4,0)
tyConString :: TyCon -> String
tyConString = tyConName
#endif
instance TextShow TypeRep where
showbPrec = showbTypeRepPrec
INLINE_INST_FUN(showbPrec)
instance TextShow TyCon where
showb = showbTyCon
INLINE_INST_FUN(showb)