{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeOperators #-}
module Pinch.Internal.TType
(
TType(..)
, IsTType(..)
, SomeTType(..)
, ttypeEquality
, ttypeEqT
, TBool
, TByte
, TDouble
, TInt16
, TInt32
, TEnum
, TInt64
, TBinary
, TText
, TStruct
, TUnion
, TException
, TMap
, TSet
, TList
) where
import Data.Hashable (Hashable (..))
import Data.Typeable ((:~:) (..), Typeable)
data TBool deriving (Typeable)
data TByte deriving (Typeable)
data TDouble deriving (Typeable)
data TInt16 deriving (Typeable)
data TInt32 deriving (Typeable)
type TEnum = TInt32
data TInt64 deriving (Typeable)
data TBinary deriving (Typeable)
type TText = TBinary
data TStruct deriving (Typeable)
type TUnion = TStruct
type TException = TStruct
data TMap deriving (Typeable)
data TSet deriving (Typeable)
data TList deriving (Typeable)
data TType a where
TBool :: TType TBool
TByte :: TType TByte
TDouble :: TType TDouble
TInt16 :: TType TInt16
TInt32 :: TType TInt32
TInt64 :: TType TInt64
TBinary :: TType TBinary
TStruct :: TType TStruct
TMap :: TType TMap
TSet :: TType TSet
TList :: TType TList
deriving (Typeable)
deriving instance Show (TType a)
deriving instance Eq (TType a)
instance Hashable (TType a) where
hashWithSalt s TBool = s `hashWithSalt` (0 :: Int)
hashWithSalt s TByte = s `hashWithSalt` (1 :: Int)
hashWithSalt s TDouble = s `hashWithSalt` (2 :: Int)
hashWithSalt s TInt16 = s `hashWithSalt` (3 :: Int)
hashWithSalt s TInt32 = s `hashWithSalt` (4 :: Int)
hashWithSalt s TInt64 = s `hashWithSalt` (5 :: Int)
hashWithSalt s TBinary = s `hashWithSalt` (6 :: Int)
hashWithSalt s TStruct = s `hashWithSalt` (7 :: Int)
hashWithSalt s TMap = s `hashWithSalt` (8 :: Int)
hashWithSalt s TSet = s `hashWithSalt` (9 :: Int)
hashWithSalt s TList = s `hashWithSalt` (10 :: Int)
class Typeable a => IsTType a where
ttype :: TType a
instance IsTType TBool where ttype = TBool
instance IsTType TByte where ttype = TByte
instance IsTType TDouble where ttype = TDouble
instance IsTType TInt16 where ttype = TInt16
instance IsTType TInt32 where ttype = TInt32
instance IsTType TInt64 where ttype = TInt64
instance IsTType TBinary where ttype = TBinary
instance IsTType TStruct where ttype = TStruct
instance IsTType TMap where ttype = TMap
instance IsTType TSet where ttype = TSet
instance IsTType TList where ttype = TList
data SomeTType where
SomeTType :: forall a. IsTType a => TType a -> SomeTType
deriving Typeable
deriving instance Show SomeTType
ttypeEquality :: TType a -> TType b -> Maybe (a :~: b)
ttypeEquality TBool TBool = Just Refl
ttypeEquality TByte TByte = Just Refl
ttypeEquality TDouble TDouble = Just Refl
ttypeEquality TInt16 TInt16 = Just Refl
ttypeEquality TInt32 TInt32 = Just Refl
ttypeEquality TInt64 TInt64 = Just Refl
ttypeEquality TBinary TBinary = Just Refl
ttypeEquality TStruct TStruct = Just Refl
ttypeEquality TMap TMap = Just Refl
ttypeEquality TSet TSet = Just Refl
ttypeEquality TList TList = Just Refl
ttypeEquality _ _ = Nothing
{-# INLINE ttypeEquality #-}
ttypeEqT :: forall a b. (IsTType a, IsTType b) => Maybe (a :~: b)
ttypeEqT = ttypeEquality ttype ttype
{-# INLINE ttypeEqT #-}