module Data.Origami.Internal.THUtils(
appEs,
appTs,
funcT,
funcTs,
nms,
unAppTs,
upperName,
varEs,
varEs',
varPs,
varPs'
) where
import Data.Char(toUpper)
import Language.Haskell.TH.Syntax
appEs :: [Exp] -> Exp
appEs = foldl1 AppE
appTs :: [Type] -> Type
appTs = foldl1 AppT
funcT :: Type -> Type -> Type
funcT lhs rhs = appTs [ArrowT, lhs, rhs]
funcTs :: [Type] -> Type
funcTs = foldr1 funcT
nms :: [Name]
nms = map x [1..]
where
x :: Int -> Name
x n = mkName ('x' : show n)
nms' :: [Name]
nms' = map x' [1..]
where
x' :: Int -> Name
x' n = mkName ('x' : show n ++ "'")
varEs :: [Exp]
varEs = map VarE nms
varEs' :: [Exp]
varEs' = map VarE nms'
varPs :: [Pat]
varPs = map VarP nms
varPs' :: [Pat]
varPs' = map VarP nms'
unAppTs :: Type -> [Type]
unAppTs = reverse . go
where
go (AppT t1 t2) = t2 : go t1
go t = [t]
upperName :: Name -> String
upperName nm = toUpper c : cs
where
(c : cs) = nameBase nm