Copyright | None |
---|---|
License | MIT (see the file libraries/base/LICENSE) |
Maintainer | [email protected] |
Stability | experimental |
Portability | non-portable |
Safe Haskell | None |
Language | Haskell2010 |
Language.Haskell.TH.Utils
Description
Some auxiliary functions that might be needed when using template Haskell
- rename :: Q Name -> (String -> String) -> Q Name
- rename' :: Name -> (String -> String) -> Name
- rename'' :: Name -> (String -> String) -> Q Name
- nameToExp :: (String -> String) -> Name -> Q Exp
- appExp :: [ExpQ] -> ExpQ
- appExp' :: [Exp] -> Exp
- unappExp :: ExpQ -> Q [Exp]
- unappExp' :: Exp -> [Exp]
- appConT :: [TypeQ] -> TypeQ
- appConT' :: [Type] -> Type
- unappConT :: TypeQ -> Q [Type]
- unappConT' :: Type -> [Type]
- curryType :: [TypeQ] -> TypeQ
- curryType' :: [Type] -> Type
- uncurryType :: TypeQ -> Q [Type]
- uncurryType' :: Type -> [Type]
- genBT :: String -> Int -> Q ([TyVarBndr], [TypeQ])
- genBT' :: String -> Int -> ([TyVarBndr], [Type])
- genPE :: String -> Int -> Q ([PatQ], [ExpQ])
- genPE' :: String -> Int -> ([Pat], [Exp])
- appKind :: [Kind] -> Kind
- unappKind :: Kind -> [Kind]
- curryKind :: [Kind] -> Kind
- uncurryKind :: Kind -> [Kind]
- getTypeNames :: Type -> [Name]
- getTVBName :: TyVarBndr -> Name
- getCompositeType :: Con -> [Name]
- getConName :: Con -> Name
- seqTup2 :: (Q a, Q b) -> Q (a, b)
- seqTup3 :: (Q a, Q b, Q c) -> Q (a, b, c)
- seqTup4 :: (Q a, Q b, Q c, Q d) -> Q (a, b, c, d)
- unsequence :: Q [a] -> Q [Q a]
- printQ :: Show a => Q a -> IO ()
- pprintQ :: Ppr a => Q a -> IO ()
- printiQ :: Out a => Q a -> IO ()
- printi :: Out a => a -> IO ()
- module Text.PrettyPrint.GenericPretty
Documentation
data Foo = Foo { foo :: Int } > $(nameToExp (++"1") 'foo) "foo1"
appExp :: [ExpQ] -> ExpQ Source
Apply a list of expression
[(+), 1, 2] to (+) 1 2 appExp' [VarE '(+) , (LitE (IntegerL 1)), (LitE (IntegerL 2))] >AppE (AppE (VarE GHC.Num.+) (LitE (IntegerL 1))) (LitE (IntegerL 2))
appConT :: [TypeQ] -> TypeQ Source
Apply a type constructor like appExp
> pprint $ appConT' (map ConT [''(,), ''Int , ''Bool]) "GHC.Tuple.(,) GHC.Types.Int GHC.Types.Bool" --i.e. (Int,Bool)
unappConT' :: Type -> [Type] Source
Unapply a constructor application to a list of types
curryType :: [TypeQ] -> TypeQ Source
convert [a, b, c]
to a -> b -> c
> pprint $ curryType' (map ConT [''Int , ''Int , ''Bool]) "GHC.Types.Int -> GHC.Types.Int -> GHC.Types.Bool"
curryType' :: [Type] -> Type Source
uncurryType :: TypeQ -> Q [Type] Source
convert @a -> b -> c@ to @[a,b,c]
> uncurryType' (ForallT [PlainTV (mkName "a")] [] (AppT (AppT ArrowT (VarT (mkName "a"))) (ConT ''Int))) > [VarT a,ConT GHC.Types.Int]
uncurryType' :: Type -> [Type] Source
genBT :: String -> Int -> Q ([TyVarBndr], [TypeQ]) Source
> genBT' "a" 3 ([PlainTV a1,PlainTV a2,PlainTV a3],[VarT a1,VarT a2,VarT a3])
Generate a list of type Bind and Type
genBT' :: String -> Int -> ([TyVarBndr], [Type]) Source
Generate a list of type Bind and Type without Q
genPE :: String -> Int -> Q ([PatQ], [ExpQ]) Source
> genPE' "a" 3 ([VarP a1,VarP a2,VarP a3],[VarE a1,VarE a2,VarE a3])
Generate related patterns and expressions
uncurryKind :: Kind -> [Kind] Source
Like uncurryType
getTypeNames :: Type -> [Name] Source
Get type Names recursively, You can transform it to set if you want.
getTVBName :: TyVarBndr -> Name Source
Get type var bind name
getCompositeType :: Con -> [Name] Source
Get all names recursively from a constructor
getConName :: Con -> Name Source
Get name from constructors
unsequence :: Q [a] -> Q [Q a] Source
Unsequence Q [a]
to [Q a], but you never get rid of the outer Q
printiQ :: Out a => Q a -> IO () Source
Print AST with indentions. There are also other exported functions from genericpretty library. See Out
.
runQ [| (1+1) * 5|] >>= pp -- or use printiQ [| (1+1) * 5|] InfixE (Just InfixE (Just LitE (IntegerL 1)) (VarE (Name (OccName "+") (NameG' VarName (PkgName "base") (ModName "GHC.Num")))) (Just LitE (IntegerL 1))) (VarE (Name (OccName "*") (NameG' VarName (PkgName "base") (ModName "GHC.Num")))) (Just LitE (IntegerL 5))
exported GenericPretty package