th-utilities-0.2.5.2: Collection of useful functions for use with Template Haskell

Safe Haskell | None
Language | Haskell2010

TH.Utilities

Description

Miscellaneous Template Haskell utilities, added as needed by packages in the th-utilities repo and elsewhere.

Synopsis

- tyVarBndrName :: TyVarBndr_ flag -> Name
- appsT :: Type -> [Type] -> Type
- unAppsT :: Type -> [Type]
- tupT :: [Q Type] -> Q Type
- promotedTupT :: [Q Type] -> Q Type
- typeToNamedCon :: Type -> Maybe (Name, [Type])
- expectTyCon1 :: Name -> Type -> Q Type
- expectTyCon2 :: Name -> Type -> Q (Type, Type)
- proxyE :: TypeQ -> ExpQ
- everywhereButStrings :: Data a => (forall b. Data b => b -> b) -> a -> a
- everywhereButStringsM :: (Data a, Monad m) => GenericM m -> a -> m a
- toSimpleName :: Name -> Name
- dequalify :: Name -> Name
- dequalifyTyVars :: Data a => a -> a
- freeVarsT :: Type -> [Name]
- plainInstanceD :: Cxt -> Type -> [Dec] -> Dec
- fromPlainInstanceD :: Dec -> Maybe (Cxt, Type, [Dec])
- typeRepToType :: TypeRep -> Q Type
- data ExpLifter = ExpLifter (forall (m :: Type -> Type). Quote m => m Exp)
- dumpSplices :: DecsQ -> DecsQ

# Documentation

tyVarBndrName :: TyVarBndr_ flag -> Name Source #

Get the `Name` of a `TyVarBndr`

appsT :: Type -> [Type] -> Type Source #

unAppsT :: Type -> [Type] Source #

Breaks a type application like `A b c` into [A, b, c]. In other words, it descends leftwards down `AppT` constructors, and yields a list of the results.

tupT :: [Q Type] -> Q Type Source #

Given a list of types, produce the type of a tuple of those types. This is analogous to `tupE` and `tupP`.

    tupT [[t|Int|], [t|Char|], [t|Bool]] = [t| (Int, Char, Bool) |]

@since FIXME

promotedTupT :: [Q Type] -> Q Type Source #

Given a list of types, produce the type of a promoted tuple of those types. This is analogous to `tupE` and `tupP`.

    promotedTupT [[t|3|], [t| 'True|], [t|Bool]] = [t| '(3, 'True, Bool) |]

@since FIXME

typeToNamedCon :: Type -> Maybe (Name, [Type]) Source #

Given a `Type`, returns a `Just` value if it's a named type constructor applied to arguments. This value contains the name of the type and a list of arguments.

expectTyCon1 :: Name -> Type -> Q Type Source #

Expect the provided type to be an application of a regular type to one argument, otherwise fail with a message. This will also work if the name is a promoted data constructor (`PromotedT`).

expectTyCon2 :: Name -> Type -> Q (Type, Type) Source #

Expect the provided type to be an application of a regular type to two arguments, otherwise fail with a message. This will also work if the name is a promoted data constructor (`PromotedT`).

proxyE :: TypeQ -> ExpQ Source #

Given a type, construct the expression (Proxy :: Proxy ty).

everywhereButStrings :: Data a => (forall b. Data b => b -> b) -> a -> a Source #

Like the `everywhere` generic traversal strategy, but skips over strings. This can aid performance of TH traversals quite a bit.

everywhereButStringsM :: (Data a, Monad m) => GenericM m -> a -> m a Source #

Like the `everywhereM` generic traversal strategy, but skips over strings. This can aid performance of TH traversals quite a bit.

toSimpleName :: Name -> Name Source #

Make a `Name` with a `NameS` or `NameQ` flavour, from a `Name` with any `NameFlavour`. This may change the meaning of names.

dequalify :: Name -> Name Source #

Construct a plain name (`mkName`) based on the given name. This is useful for cases where TH doesn't expect a unique name.

dequalifyTyVars :: Data a => a -> a Source #

Apply `dequalify` to every type variable.

freeVarsT :: Type -> [Name] Source #

Get the free type variables of a `Type`.

plainInstanceD :: Cxt -> Type -> [Dec] -> Dec Source #

Utility to conveniently handle change to `InstanceD` API in template-haskell-2.11.0

fromPlainInstanceD :: Dec -> Maybe (Cxt, Type, [Dec]) Source #

Utility to conveniently handle change to `InstanceD` API in template-haskell-2.11.0

typeRepToType :: TypeRep -> Q Type Source #

Utility to convert Data.Typeable `TypeRep` to a `Type`. Note that this function is known to not yet work for many cases, but it does work for normal user datatypes. In future versions this function might have better behavior.

data ExpLifter Source #

Hack to enable putting expressions inside `lift`-ed TH data. For example, you could do

        main = print $(lift [ExpLifter [e| 1 + 1 |],  ExpLifter [e| 2 |]])

Here, `lift` is working on a value of type `[ExpLifter]`. The code generated by `lift` constructs a list with the `ExpLifter` expressions providing the element values.

Without `ExpLifter`, `lift` tends to just generate code involving data construction. With `ExpLifter`, you can put more complicated expression into this construction.

Note that this cannot be used in typed quotes, because `liftTyped` will throw an exception. This is because this hack is incompatible with the type of `liftTyped`, as it would require the generated code to have type `ExpLifter`.

Constructors

ExpLifter (forall (m :: Type -> Type). Quote m => m Exp) |

#### Instances

Instances details

Lift ExpLifter Source # |
Instance details Defined in TH.Utilities Methods lift :: Quote m => ExpLifter -> m Exp Source # liftTyped :: forall (m :: Type -> Type). Quote m => ExpLifter -> Code m ExpLifter Source #

dumpSplices :: DecsQ -> DecsQ Source #

Print splices generated by a TH splice (the printing will happen during compilation, as a GHC warning). Useful for debugging.

For instance, you can dump splices generated with `makeLenses` by replacing a top-level invocation of `makeLenses` in your code with:

    dumpSplices $ makeLenses ''Foo