module Test.Speculate.Utils.Typeable
( tyArity
, typesIn
, unFunTy
, isFunTy
, argumentTy
, resultTy
, finalResultTy
, boolTy
, mkEqnTy
, funTyCon
, compareTy
, module Data.Typeable
)
where
import Data.Typeable
import Data.Monoid ((<>))
import Test.Speculate.Utils.List ((+++))
compareTy :: TypeRep -> TypeRep -> Ordering
compareTy t1 t2 | t1 == t2 = EQ
compareTy t1 t2 = tyArity t1 `compare` tyArity t2
<> length ts1 `compare` length ts2
<> show c1 `compare` show c2
<> foldr (<>) EQ (zipWith compareTy ts1 ts2)
where
(c1,ts1) = splitTyConApp t1
(c2,ts2) = splitTyConApp t2
tyArity :: TypeRep -> Int
tyArity t
| isFunTy t = 1 + tyArity (resultTy t)
| otherwise = 0
typesIn :: TypeRep -> [TypeRep]
typesIn t
| isFunTy t = typesIn (argumentTy t)
+++ typesIn (resultTy t)
| otherwise = [t]
finalResultTy :: TypeRep -> TypeRep
finalResultTy t
| isFunTy t = finalResultTy (resultTy t)
| otherwise = t
unFunTy :: TypeRep -> (TypeRep,TypeRep)
unFunTy t
| isFunTy t = let (f,[a,b]) = splitTyConApp t in (a,b)
| otherwise = error $ "error (unFunTy): `" ++ show t ++ "` is not a function type"
argumentTy :: TypeRep -> TypeRep
argumentTy = fst . unFunTy
resultTy :: TypeRep -> TypeRep
resultTy = snd . unFunTy
boolTy :: TypeRep
boolTy = typeOf (undefined :: Bool)
funTyCon :: TyCon
funTyCon = typeRepTyCon $ typeOf (undefined :: () -> ())
isFunTy :: TypeRep -> Bool
isFunTy t =
case splitTyConApp t of
(con,[_,_]) | con == funTyCon -> True
_ -> False
mkEqnTy :: TypeRep -> TypeRep
mkEqnTy a = a `mkFunTy` (a `mkFunTy` boolTy)