module GHC.TypeLits
(
N.Natural, N.Nat, Symbol
, N.KnownNat, natVal, natVal'
, KnownSymbol, symbolVal, symbolVal'
, KnownChar, charVal, charVal'
, N.SomeNat(..), SomeSymbol(..), SomeChar(..)
, someNatVal, someSymbolVal, someCharVal
, N.sameNat, sameSymbol, sameChar
, OrderingI(..)
, N.cmpNat, cmpSymbol, cmpChar
, type (N.<=), type (N.<=?), type (N.+), type (N.*), type (N.^), type (N.-)
, type N.Div, type N.Mod, type N.Log2
, AppendSymbol
, N.CmpNat, CmpSymbol, CmpChar
, ConsSymbol, UnconsSymbol
, CharToNat, NatToChar
, TypeError
, ErrorMessage(..)
) where
import GHC.Base(Eq(..), Ord(..), Ordering(..), String, otherwise)
import GHC.Types(Symbol, Char)
import GHC.Num(Integer, fromInteger)
import GHC.Show(Show(..))
import GHC.Read(Read(..))
import GHC.Real(toInteger)
import GHC.Prim(magicDict, Proxy#)
import Data.Maybe(Maybe(..))
import Data.Proxy (Proxy(..))
import Data.Type.Equality((:~:)(Refl))
import Data.Type.Ord(OrderingI(..))
import Unsafe.Coerce(unsafeCoerce)
import GHC.TypeLits.Internal(CmpSymbol, CmpChar)
import qualified GHC.TypeNats as N
class KnownSymbol (n :: Symbol) where
symbolSing :: SSymbol n
natVal :: forall n proxy. N.KnownNat n => proxy n -> Integer
natVal p = toInteger (N.natVal p)
symbolVal :: forall n proxy. KnownSymbol n => proxy n -> String
symbolVal _ = case symbolSing :: SSymbol n of
SSymbol x -> x
natVal' :: forall n. N.KnownNat n => Proxy# n -> Integer
natVal' p = toInteger (N.natVal' p)
symbolVal' :: forall n. KnownSymbol n => Proxy# n -> String
symbolVal' _ = case symbolSing :: SSymbol n of
SSymbol x -> x
data SomeSymbol = forall n. KnownSymbol n => SomeSymbol (Proxy n)
class KnownChar (n :: Char) where
charSing :: SChar n
charVal :: forall n proxy. KnownChar n => proxy n -> Char
charVal _ = case charSing :: SChar n of
SChar x -> x
charVal' :: forall n. KnownChar n => Proxy# n -> Char
charVal' _ = case charSing :: SChar n of
SChar x -> x
data SomeChar = forall n. KnownChar n => SomeChar (Proxy n)
someNatVal :: Integer -> Maybe N.SomeNat
someNatVal n
| n >= 0 = Just (N.someNatVal (fromInteger n))
| otherwise = Nothing
someSymbolVal :: String -> SomeSymbol
someSymbolVal n = withSSymbol SomeSymbol (SSymbol n) Proxy
instance Eq SomeSymbol where
SomeSymbol x == SomeSymbol y = symbolVal x == symbolVal y
instance Ord SomeSymbol where
compare (SomeSymbol x) (SomeSymbol y) = compare (symbolVal x) (symbolVal y)
instance Show SomeSymbol where
showsPrec p (SomeSymbol x) = showsPrec p (symbolVal x)
instance Read SomeSymbol where
readsPrec p xs = [ (someSymbolVal a, ys) | (a,ys) <- readsPrec p xs ]
someCharVal :: Char -> SomeChar
someCharVal n = withSChar SomeChar (SChar n) Proxy
instance Eq SomeChar where
SomeChar x == SomeChar y = charVal x == charVal y
instance Ord SomeChar where
compare (SomeChar x) (SomeChar y) = compare (charVal x) (charVal y)
instance Show SomeChar where
showsPrec p (SomeChar x) = showsPrec p (charVal x)
instance Read SomeChar where
readsPrec p xs = [ (someCharVal a, ys) | (a,ys) <- readsPrec p xs ]
type family AppendSymbol (m ::Symbol) (n :: Symbol) :: Symbol
data ErrorMessage = Text Symbol
| forall t. ShowType t
| ErrorMessage :<>: ErrorMessage
| ErrorMessage :$$: ErrorMessage
infixl 5 :$$:
infixl 6 :<>:
type family TypeError (a :: ErrorMessage) :: b where
type family ConsSymbol (a :: Char) (b :: Symbol) :: Symbol
type family UnconsSymbol (a :: Symbol) :: Maybe (Char, Symbol)
type family CharToNat (c :: Char) :: N.Nat
type family NatToChar (n :: N.Nat) :: Char
sameSymbol :: (KnownSymbol a, KnownSymbol b) =>
proxy1 a -> proxy2 b -> Maybe (a :~: b)
sameSymbol x y
| symbolVal x == symbolVal y = Just (unsafeCoerce Refl)
| otherwise = Nothing
sameChar :: (KnownChar a, KnownChar b) =>
proxy1 a -> proxy2 b -> Maybe (a :~: b)
sameChar x y
| charVal x == charVal y = Just (unsafeCoerce Refl)
| otherwise = Nothing
cmpSymbol :: forall a b proxy1 proxy2. (KnownSymbol a, KnownSymbol b)
=> proxy1 a -> proxy2 b -> OrderingI a b
cmpSymbol x y = case compare (symbolVal x) (symbolVal y) of
EQ -> case unsafeCoerce (Refl, Refl) :: (CmpSymbol a b :~: 'EQ, a :~: b) of
(Refl, Refl) -> EQI
LT -> case unsafeCoerce Refl :: (CmpSymbol a b :~: 'LT) of
Refl -> LTI
GT -> case unsafeCoerce Refl :: (CmpSymbol a b :~: 'GT) of
Refl -> GTI
cmpChar :: forall a b proxy1 proxy2. (KnownChar a, KnownChar b)
=> proxy1 a -> proxy2 b -> OrderingI a b
cmpChar x y = case compare (charVal x) (charVal y) of
EQ -> case unsafeCoerce (Refl, Refl) :: (CmpChar a b :~: 'EQ, a :~: b) of
(Refl, Refl) -> EQI
LT -> case unsafeCoerce Refl :: (CmpChar a b :~: 'LT) of
Refl -> LTI
GT -> case unsafeCoerce Refl :: (CmpChar a b :~: 'GT) of
Refl -> GTI
newtype SSymbol (s :: Symbol) = SSymbol String
data WrapS a b = WrapS (KnownSymbol a => Proxy a -> b)
withSSymbol :: (KnownSymbol a => Proxy a -> b)
-> SSymbol a -> Proxy a -> b
withSSymbol f x y = magicDict (WrapS f) x y
newtype SChar (s :: Char) = SChar Char
data WrapC a b = WrapC (KnownChar a => Proxy a -> b)
withSChar :: (KnownChar a => Proxy a -> b)
-> SChar a -> Proxy a -> b
withSChar f x y = magicDict (WrapC f) x y