template-haskell-util-0.1.1.0: Some utilities for template Haskell.

CopyrightNone
LicenseMIT (see the file libraries/base/LICENSE)
MaintainerHaskell.Zhang.Song@hotmail.com
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Language.Haskell.TH.Utils

Description

Some auxiliary functions that might be needed when using template Haskell

Synopsis

Documentation

rename :: Q Name -> (String -> String) -> Q Name Source

Rename a Name

nameToExp Source

Arguments

:: (String -> String)

Function to change name.

-> Name 
-> Q Exp 
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 :: TypeQ -> Q [Type] Source

Unapply a constructor application to a list of types

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"

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]

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

genPE' :: String -> Int -> ([Pat], [Exp]) Source

Generate related patterns and expressions without Q

appKind :: [Kind] -> Kind Source

Apply a list of kinds, like appConT

curryKind :: [Kind] -> Kind Source

Like curryType but on kind level

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

seqTup2 :: (Q a, Q b) -> Q (a, b) Source

sequence-like functons on tuples

seqTup3 :: (Q a, Q b, Q c) -> Q (a, b, c) Source

seqTup4 :: (Q a, Q b, Q c, Q d) -> Q (a, b, c, d) Source

unsequence :: Q [a] -> Q [Q a] Source

Unsequence Q [a] to [Q a], but you never get rid of the outer Q

printQ :: Show a => Q a -> IO () Source

Print AST of the code.

pprintQ :: Ppr a => Q a -> IO () Source

Pretty print spliced code

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))

printi :: Out a => a -> IO () Source

Print AST with identions

exported GenericPretty package