{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module TextShow.GHC.TypeLits () where
import GHC.TypeLits (SomeNat(..), SomeSymbol(..), natVal, symbolVal)
#if MIN_VERSION_base(4,16,0)
import GHC.TypeLits (SomeChar(..), charVal)
#endif
import Prelude ()
import Prelude.Compat
import TextShow.Classes (TextShow(..))
import TextShow.Data.Char ()
import TextShow.Data.Integral ()
#if MIN_VERSION_base(4,18,0)
import Data.Text.Lazy.Builder (fromString)
import GHC.Show (appPrec, appPrec1)
import GHC.TypeLits ( SNat, SSymbol, SChar
, fromSNat, fromSSymbol, fromSChar
)
import TextShow.Classes (showbParen)
#endif
instance TextShow SomeNat where
showbPrec :: Int -> SomeNat -> Builder
showbPrec Int
p (SomeNat Proxy n
x) = Int -> Integer -> Builder
forall a. TextShow a => Int -> a -> Builder
showbPrec Int
p (Integer -> Builder) -> Integer -> Builder
forall a b. (a -> b) -> a -> b
$ Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal Proxy n
x
{-# INLINE showbPrec #-}
instance TextShow SomeSymbol where
showb :: SomeSymbol -> Builder
showb (SomeSymbol Proxy n
x) = [Char] -> Builder
forall a. TextShow a => [a] -> Builder
showbList ([Char] -> Builder) -> [Char] -> Builder
forall a b. (a -> b) -> a -> b
$ Proxy n -> [Char]
forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> [Char]
symbolVal Proxy n
x
{-# INLINE showb #-}
#if MIN_VERSION_base(4,16,0)
instance TextShow SomeChar where
showbPrec :: Int -> SomeChar -> Builder
showbPrec Int
p (SomeChar Proxy n
x) = Int -> Char -> Builder
forall a. TextShow a => Int -> a -> Builder
showbPrec Int
p (Char -> Builder) -> Char -> Builder
forall a b. (a -> b) -> a -> b
$ Proxy n -> Char
forall (n :: Char) (proxy :: Char -> *).
KnownChar n =>
proxy n -> Char
charVal Proxy n
x
{-# INLINE showbPrec #-}
#endif
#if MIN_VERSION_base(4,18,0)
instance TextShow (SNat n) where
showbPrec :: Int -> SNat n -> Builder
showbPrec Int
p SNat n
sn
= Bool -> Builder -> Builder
showbParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
appPrec)
( [Char] -> Builder
fromString [Char]
"SNat @"
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Int -> Integer -> Builder
forall a. TextShow a => Int -> a -> Builder
showbPrec Int
appPrec1 (SNat n -> Integer
forall (n :: Nat). SNat n -> Integer
fromSNat SNat n
sn)
)
instance TextShow (SSymbol s) where
showbPrec :: Int -> SSymbol s -> Builder
showbPrec Int
p SSymbol s
ss
= Bool -> Builder -> Builder
showbParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
appPrec)
( [Char] -> Builder
fromString [Char]
"SSymbol @"
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> [Char] -> Builder
forall a. TextShow a => [a] -> Builder
showbList (SSymbol s -> [Char]
forall (s :: Symbol). SSymbol s -> [Char]
fromSSymbol SSymbol s
ss)
)
instance TextShow (SChar c) where
showbPrec :: Int -> SChar c -> Builder
showbPrec Int
p SChar c
sc
= Bool -> Builder -> Builder
showbParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
appPrec)
( [Char] -> Builder
fromString [Char]
"SChar @"
Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Int -> Char -> Builder
forall a. TextShow a => Int -> a -> Builder
showbPrec Int
appPrec1 (SChar c -> Char
forall (c :: Char). SChar c -> Char
fromSChar SChar c
sc)
)
#endif