module Language.Haskell.HBB.Internal.AST (
transformRenamedAST
,queryRenamedAST
,transformTypecheckedAST) where
import Data.Generics (GenericM,GenericQ,gmapM,Data,extQ,gmapQ)
import NameSet (NameSet)
import GHC
queryRenamedAST :: r -> (r -> r -> r) -> GenericQ r -> GenericQ r
queryRenamedAST n m f x = if isPostTcType x
then n
else foldl m (f x) (gmapQ (queryRenamedAST n m f) x)
where isPostTcType :: GenericQ Bool
isPostTcType = (const False) `extQ`
((const True) :: PostTcType -> Bool)
transformRenamedAST :: (Monad m, Data a) => GenericM m -> a -> m a
transformRenamedAST = everywhereButM isPostTcType
where
isPostTcType :: GenericQ Bool
isPostTcType = extQ (const False) ((const True) :: PostTcType -> Bool)
transformTypecheckedAST :: (Monad m, Data a) => GenericM m -> a -> m a
transformTypecheckedAST = everywhereButM isNameSet
where
isNameSet :: GenericQ Bool
isNameSet = extQ (const False) ((const True) :: NameSet -> Bool)
everywhereButM :: Monad m => GenericQ Bool -> GenericM m -> GenericM m
everywhereButM shouldExclude f x | (not . shouldExclude) x = do x' <- f x; gmapM (everywhereButM shouldExclude f) x'
everywhereButM _ _ x = return (id x)