module Data.PolyTypeable.Utils where
import Data.PolyTypeable
import Data.Typeable
import List (union)
import Char (isLower)
freeTyVars :: TypeRep -> [String]
freeTyVars tyrep
| isLower (head tcstr) = [tcstr]
| otherwise = foldr union [] $ map freeTyVars tcargs
where
(tycon,tcargs) = splitTyConApp tyrep
tcstr = tyConString tycon
substTyVar :: (String, TypeRep) -> TypeRep -> TypeRep
substTyVar p@(x, t) tyrep
| x == tcstr = t
| otherwise = tycon `mkTyConApp` map (substTyVar p) tcargs
where
(tycon,tcargs) = splitTyConApp tyrep
tcstr = tyConString tycon
substTyVars :: [(String,TypeRep)] -> TypeRep -> TypeRep
substTyVars = foldr (.) id . zipWith ($) (repeat substTyVar)