module Data.TypeRep.Representation where
import Control.Monad.Except
import Data.Char (isAlphaNum)
import qualified Data.Typeable as Typeable
import Data.Constraint (Constraint, Dict (..))
import Data.Proxy (Proxy (..))
import Language.Syntactic
type TR = AST
class Typeable t a
where
typeRep' :: TR t (Full a)
newtype TypeRep t a = TypeRep { unTypeRep :: TR t (Full a) }
instance Render t => Show (TypeRep t a)
where
show = render . desugar
instance Syntactic (TypeRep t a)
where
type Domain (TypeRep t a) = t
type Internal (TypeRep t a) = a
desugar = unTypeRep
sugar = TypeRep
typeRep :: Typeable t a => TypeRep t a
typeRep = TypeRep typeRep'
class Render t => TypeEq t u
where
typeEqSym
:: (t sig1, Args (AST u) sig1)
-> (t sig2, Args (AST u) sig2)
-> Either String (Dict (DenResult sig1 ~ DenResult sig2))
instance (TypeEq t1 t, TypeEq t2 t) => TypeEq (t1 :+: t2) t
where
typeEqSym (InjL t1, as1) (InjL t2, as2) = typeEqSym (t1,as1) (t2,as2)
typeEqSym (InjR t1, as1) (InjR t2, as2) = typeEqSym (t1,as1) (t2,as2)
typeEqSym (t1,_) (t2,_) = throwError ""
instance TypeEq Empty t
where
typeEqSym = error "typeEqSym: Empty"
typeEqM :: (TypeEq t t, MonadError String m) => TypeRep t a -> TypeRep t b -> m (Dict (a ~ b))
typeEqM t1@(TypeRep s1) t2@(TypeRep s2) = case go (s1, Nil) (s2, Nil) of
Left _ -> throwError $ "type mismatch: " ++ show t1 ++ " /= " ++ show t2
Right Dict -> return Dict
where
go :: TypeEq t t
=> (AST t sig1, Args (AST t) sig1)
-> (AST t sig2, Args (AST t) sig2)
-> Either String (Dict ((DenResult sig1 ~ DenResult sig2)))
go (Sym t1, as1) (Sym t2, as2) = typeEqSym (t1,as1) (t2,as2)
go (s1 :$ a1, as1) (s2 :$ a2, as2) = go (s1, a1 :* as1) (s2, a2 :* as2)
go _ _ = throwError ""
typeEq :: TypeEq t t => TypeRep t a -> TypeRep t b -> Maybe (Dict (a ~ b))
typeEq t1 t2 = either (const Nothing) Just $ typeEqM t1 t2
matchCon :: TypeRep t c -> [E (TypeRep t)]
matchCon = simpleMatch (\_ -> foldrArgs (\t -> (E (TypeRep t) :)) []) . unTypeRep
matchConM :: Monad m => TypeRep t c -> m [E (TypeRep t)]
matchConM = return . matchCon
class Witness p t u
where
witSym :: t sig -> Args (AST u) sig -> Dict (p (DenResult sig))
instance (Witness p t1 t, Witness p t2 t) => Witness p (t1 :+: t2) t
where
witSym (InjL s) as = witSym s as
witSym (InjR s) as = witSym s as
instance Witness p t t => Witness p (AST t) t
where
witSym (Sym s) as = witSym s as
witSym (s :$ a) as = witSym s (a :* as)
class (ShowClass p, Render t) => PWitness p t u
where
pwitSym :: t sig -> Args (AST u) sig -> Either String (Dict (p (DenResult sig)))
pwitSym _ _ = throwError ""
instance (PWitness p t1 t, PWitness p t2 t) => PWitness p (t1 :+: t2) t
where
pwitSym (InjL s) as = pwitSym s as
pwitSym (InjR s) as = pwitSym s as
pwitSymDefault :: Witness p t u =>
t sig -> Args (AST u) sig -> Either String (Dict (p (DenResult sig)))
pwitSymDefault t = return . witSym t
wit :: forall p t a . Witness p t t => Proxy p -> TypeRep t a -> Dict (p a)
wit _ (TypeRep a) = witSym a (Nil :: Args (AST t) (Full a))
pwit :: forall p t m a . (PWitness p t t, MonadError String m) =>
Proxy p -> TypeRep t a -> m (Dict (p a))
pwit p t@(TypeRep a) = case go a Nil of
Left _ -> throwError $ unwords ["cannot deduce", showClass p, classArg]
Right a -> return a
where
st = show t
classArg = if all isAlphaNum st then st else "(" ++ st ++ ")"
go :: AST t sig -> Args (AST t) sig -> Either String (Dict (p (DenResult sig)))
go (Sym s) as = pwitSym s as
go (s :$ a) as = go s (a :* as)
witTypeable :: Witness (Typeable t) t t => TypeRep t a -> Dict (Typeable t a)
witTypeable = wit Proxy
pwitTypeable :: PWitness (Typeable t) t t =>
TypeRep t a -> Either String (Dict (Typeable t a))
pwitTypeable = pwit Proxy
cast :: forall t a b . (Typeable t a, Typeable t b, TypeEq t t) =>
Proxy t -> a -> Either String b
cast _ a = do
Dict <- typeEqM (typeRep :: TypeRep t a) (typeRep :: TypeRep t b)
return a
gcast :: forall t a b c . (Typeable t a, Typeable t b, TypeEq t t) =>
Proxy t -> c a -> Either String (c b)
gcast _ a = do
Dict <- typeEqM (typeRep :: TypeRep t a) (typeRep :: TypeRep t b)
return a
data Dynamic t
where
Dyn :: TypeRep t a -> a -> Dynamic t
toDyn :: Typeable t a => a -> Dynamic t
toDyn = Dyn typeRep
fromDyn :: forall t a . (Typeable t a, TypeEq t t) => Dynamic t -> Either String a
fromDyn (Dyn t a) = do
Dict <- typeEqM t (typeRep :: TypeRep t a)
return a
instance (TypeEq t t, Witness Eq t t) => Eq (Dynamic t)
where
Dyn ta a == Dyn tb b
| Just Dict <- typeEq ta tb
, Dict <- wit (Proxy :: Proxy Eq) ta
= a == b
_ == _ = False
instance Witness Show t t => Show (Dynamic t)
where
show (Dyn t a) | Dict <- wit (Proxy :: Proxy Show) t = show a
class Any a
instance Any a
class ShowClass (p :: * -> Constraint)
where
showClass :: Proxy p -> String
instance ShowClass Any where showClass _ = "Any"
instance ShowClass Typeable.Typeable where showClass _ = "Data.Typeable"
instance ShowClass Eq where showClass _ = "Eq"
instance ShowClass Ord where showClass _ = "Ord"
instance ShowClass Show where showClass _ = "Show"
instance ShowClass Num where showClass _ = "Num"
instance ShowClass Integral where showClass _ = "Integral"
instance ShowClass (Typeable t) where showClass _ = "Typeable t"
pAny :: Proxy Any
pAny = Proxy
pDataTypeable :: Proxy Typeable.Typeable
pDataTypeable = Proxy
pEq :: Proxy Eq
pEq = Proxy
pOrd :: Proxy Ord
pOrd = Proxy
pShow :: Proxy Show
pShow = Proxy
pNum :: Proxy Num
pNum = Proxy
pIntegral :: Proxy Integral
pIntegral = Proxy