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(..), funTc, listTc)
# if MIN_VERSION_base(4,8,0)
import Data.Typeable.Internal (typeRepKinds)
# 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 == listTc -> singleton '[' <> showb x <> singleton ']'
[a,r] | tycon == funTc -> 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))
listTc :: TyCon
listTc = typeRepTyCon $ typeOf [()]
funTc :: TyCon
funTc = mkTyCon "->"
#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)