HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduletemplate-haskell-2.22.0.0Haskell2010

Language.Haskell.TH.Syntax

Abstract syntax definitions for Template Haskell.

  • 80 types
  • 3 classes
  • 102 values
datadata Foreign
#
Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata Fixity
#
Instances6Eq, Data, Ord, Show, Generic, Rep
datadata Safety
#
Instances6Eq, Data, Ord, Show, Generic, Rep
typetype Kind = Type
#

To avoid duplication between kinds and types, they are defined to be the same. Naturally, you would never have a type be StarT and you would never have a kind be SigT, but many of the other constructors are shared. Note that the kind Bool is denoted with ConT, not PromotedT. Similarly, tuple kinds are made with TupleT, not PromotedTupleT.

datadata Type
#

Constructors

Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata Callconv
#
Instances6Eq, Data, Ord, Show, Generic, Rep
classclass (MonadIO m, MonadFail m) => Quasi (m :: Type -> Type) where
#
Instances2Quasi
  • Quasi IODefined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Quasi QDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
datadata Name
#

An abstract type representing names in the syntax tree.

Names can be constructed in several ways, which come with different name-capture guarantees (see Language.Haskell.TH.Syntax#namecapture for an explanation of name capture):

  • the built-in syntax 'f and ''T can be used to construct names, The expression 'f gives a Name which refers to the value f currently in scope, and ''T gives a Name which refers to the type T currently in scope. These names can never be captured.

  • lookupValueName and lookupTypeName are similar to 'f and ''T respectively, but the Names are looked up at the point where the current splice is being run. These names can never be captured.

  • newName monadically generates a new name, which can never be captured.

  • mkName generates a capturable name.

Names constructed using newName and mkName may be used in bindings (such as let x = ... or x -> ...), but names constructed using lookupValueName, lookupTypeName, 'f, ''T may not.

Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata Info
#

Obtained from reify in the Q Monad.

Constructors

  • ClassI Dec [InstanceDec]

    A class, with a list of its visible instances

  • ClassOpI Name Type ParentName

    A class method

  • TyConI Dec

    A "plain" type constructor. "Fancier" type constructors are returned using PrimTyConI or FamilyI as appropriate. At present, this reified declaration will never have derived instances attached to it (if you wish to check for an instance, see reifyInstances).

  • FamilyI Dec [InstanceDec]

    A type or data family, with a list of its visible instances. A closed type family is returned with 0 instances.

  • PrimTyConI Name Arity Unlifted

    A "primitive" type constructor, which can't be expressed with a Dec. Examples: (->), Int#.

  • DataConI Name Type ParentName

    A data constructor

  • PatSynI Name PatSynType

    A pattern synonym

  • VarI Name Type (Maybe Dec)

    A "value" variable (as opposed to a type variable, see TyVarI).

    The Maybe Dec field contains Just the declaration which defined the variable - including the RHS of the declaration - or else Nothing, in the case where the RHS is unavailable to the compiler. At present, this value is always Nothing: returning the RHS has not yet been implemented because of lack of interest.

  • TyVarI Name Type

    A type variable.

    The Type field contains the type which underlies the variable. At present, this is always VarT theName, but future changes may permit refinement of this.

Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata Dec
#

Constructors

Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata Role
#

Role annotations

Constructors

Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata AnnLookup
#

Annotation target for reifyAnnotations

Instances6Eq, Data, Ord, Show, Generic, Rep
datadata Module
#

Obtained from reifyModule and thisModule.

Constructors

Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata ModuleInfo
#

Obtained from reifyModule in the Q Monad.

Constructors

Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata DecidedStrictness
#

Unlike SourceStrictness and SourceUnpackedness, DecidedStrictness refers to the strictness annotations that the compiler chooses for a data constructor field, which may be different from what is written in source code.

Note that non-unpacked strict fields are assigned DecidedLazy when a bang would be inappropriate, such as the field of a newtype constructor and fields that have an unlifted type.

See reifyConStrictness for more information.

Constructors

Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata Loc
#
Instances7Eq, Data, Ord, Show, Generic, Ppr, …
newtypenewtype Q a
#

Constructors

Instances10Monad, Functor, MonadFix, MonadFail, Applicative, Quote, …
  • Monad QDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Functor QDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • MonadFix QDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax

    If the function passed to mfix inspects its argument, the resulting action will throw a FixIOException.

  • MonadFail QDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Applicative QDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Quote QDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • MonadIO QDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Quasi QDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Semigroup a => Semigroup (Q a)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Monoid a => Monoid (Q a)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
datadata DocLoc
#

A location at which to attach Haddock documentation. Note that adding documentation to a Name defined oustide of the current module will cause an error.

Constructors

Instances6Eq, Data, Ord, Show, Generic, Rep
classclass Monad m => Quote (m :: Type -> Type) where
#

The Quote class implements the minimal interface which is necessary for desugaring quotations.

  • The Monad m superclass is needed to stitch together the different AST fragments.

  • newName is used when desugaring binding structures such as lambdas to generate fresh names.

Therefore the type of an untyped quotation in GHC is `Quote m => m Exp`

For many years the type of a quotation was fixed to be `Q Exp` but by more precisely specifying the minimal interface it enables the Exp to be extracted purely from the quotation without interacting with Q.

Methods

  • newName :: String -> m Name

    Generate a fresh name, which cannot be captured.

    For example, this:

    f = $(do
        nm1 <- newName "x"
        let nm2 = mkName "x"
        return (LamE [VarP nm1] (LamE [VarP nm2] (VarE nm1)))
       )

    will produce the splice

    f = \x0 -> \x -> x0

    In particular, the occurrence VarE nm1 refers to the binding VarP nm1, and is not captured by the binding VarP nm2.

    Although names generated by newName cannot be captured, they can capture other names. For example, this:

    g = $(do
      nm1 <- newName "x"
      let nm2 = mkName "x"
      return (LamE [VarP nm2] (LamE [VarP nm1] (VarE nm2)))
     )

    will produce the splice

    g = \x -> \x0 -> x0

    since the occurrence VarE nm2 is captured by the innermost binding of x, namely VarP nm1.

Instances2Quote
  • Quote IODefined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Quote QDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
typetype Uniq = Integer
#

Uniq is used by GHC to distinguish names from each other.

valuereport :: Bool -> String -> Q ()
#

Deprecated. Use reportError or reportWarning instead

Report an error (True) or warning (False), but carry on; use fail to stop.

valuerunIO :: IO a -> Q a
#

The runIO function lets you run an I/O computation in the Q monad. Take care: you are guaranteed the ordering of calls to runIO within a single Q computation, but not about the order in which splices are run.

Note: for various murky reasons, stdout and stderr handles are not necessarily flushed when the compiler finishes running, so you should flush them yourself.

newtypenewtype TExp (a :: TYPE r)
#

Typed wrapper around an Exp.

This is the typed representation of terms produced by typed quotes.

Representation-polymorphic since template-haskell-2.16.0.0.

Constructors

datadata Exp
#

Constructors

Instances7Eq, Data, Ord, Show, Generic, Ppr, …
valueunTypeQ :: Quote m => m (TExp a) -> m Exp
#

Discard the type annotation and produce a plain Template Haskell expression

Representation-polymorphic since template-haskell-2.16.0.0.

valueunsafeTExpCoerce :: Quote m => m Exp -> m (TExp a)
#

Annotate the Template Haskell expression with a type

This is unsafe because GHC cannot check for you that the expression really does have the type you claim it has.

Representation-polymorphic since template-haskell-2.16.0.0.

newtypenewtype Code (m :: Type -> Type) (a :: TYPE r)
#

Represents an expression which has type a, built in monadic context m. Built on top of TExp, typed expressions allow for type-safe splicing via:

  • typed quotes, written as [|| ... ||] where ... is an expression; if that expression has type a, then the quotation has type Quote m => Code m a

  • typed splices inside of typed quotes, written as $$(...) where ... is an arbitrary expression of type Quote m => Code m a

Traditional expression quotes and splices let us construct ill-typed expressions:

Example2 expressions
fmap ppr $ runQ (unTypeCode [| True == $( [| "foo" |] ) |])GHC.Types.True GHC.Classes.== "foo"GHC.Types.True GHC.Classes.== "foo"<interactive> error:    • Couldn't match expected type ‘Bool’ with actual type ‘[Char]’    • In the second argument of ‘(==)’, namely ‘"foo"’      In the expression: True == "foo"      In an equation for ‘it’: it = True == "foo"

With typed expressions, the type error occurs when constructing the Template Haskell expression:

Example1 expression
fmap ppr $ runQ (unTypeCode [|| True == $$( [|| "foo" ||] ) ||])<interactive> error:    • Couldn't match type ‘[Char]’ with ‘Bool’      Expected type: Code Q Bool        Actual type: Code Q [Char]    • In the Template Haskell quotation [|| "foo" ||]      In the expression: [|| "foo" ||]      In the Template Haskell splice $$([|| "foo" ||])

Constructors

valueliftCode :: m (TExp a) -> Code m a
#

Lift a monadic action producing code into the typed Code representation

valueunTypeCode :: Quote m => Code m a -> m Exp
#

Extract the untyped representation from the typed representation

valuehoistCode :: Monad m => (forall x. m x -> n x) -> Code m a -> Code n a
#

Modify the ambient monad used during code generation. For example, you can use hoistCode to handle a state effect: handleState :: Code (StateT Int Q) a -> Code Q a handleState = hoistCode (flip runState 0)

valuebindCode :: Monad m => m a -> (a -> Code m b) -> Code m b
#

Variant of (>>=) which allows effectful computations to be injected into code generation.

valuebindCode_ :: Monad m => m a -> Code m b -> Code m b
#

Variant of (>>) which allows effectful computations to be injected into code generation.

valuejoinCode :: Monad m => m (Code m a) -> Code m a
#

A useful combinator for embedding monadic actions into Code myCode :: ... => Code m a myCode = joinCode $ do x <- someSideEffect return (makeCodeWith x)

valuereportError :: String -> Q ()
#

Report an error to the user, but allow the current splice's computation to carry on. To abort the computation, use fail.

valuerecover
  1. :: Q a

    handler to invoke on failure

  2. -> Q a

    computation to run

  3. -> Q a
#

Recover from errors raised by reportError or fail.

valuelookupTypeName :: String -> Q (Maybe Name)
#

Look up the given name in the (type namespace of the) current splice's scope. See Language.Haskell.TH.Syntax#namelookup for more details.

valuelookupValueName :: String -> Q (Maybe Name)
#

Look up the given name in the (value namespace of the) current splice's scope. See Language.Haskell.TH.Syntax#namelookup for more details.

valuereify :: Name -> Q Info
#

reify looks up information about the Name. It will fail with a compile error if the Name is not visible. A Name is visible if it is imported or defined in a prior top-level declaration group. See the documentation for newDeclarationGroup for more details.

It is sometimes useful to construct the argument name using lookupTypeName or lookupValueName to ensure that we are reifying from the right namespace. For instance, in this context:

data D = D

which D does reify (mkName "D") return information about? (Answer: D-the-type, but don't rely on it.) To ensure we get information about D-the-value, use lookupValueName:

do
  Just nm <- lookupValueName "D"
  reify nm

and to get information about D-the-type, use lookupTypeName.

valuereifyFixity :: Name -> Q (Maybe Fixity)
#

reifyFixity nm attempts to find a fixity declaration for nm. For example, if the function foo has the fixity declaration infixr 7 foo, then reifyFixity 'foo would return Just (Fixity 7 InfixR). If the function bar does not have a fixity declaration, then reifyFixity 'bar returns Nothing, so you may assume bar has defaultFixity.

valuereifyType :: Name -> Q Type
#

reifyType nm attempts to find the type or kind of nm. For example, reifyType 'not returns Bool -> Bool, and reifyType ''Bool returns Type. This works even if there's no explicit signature and the type or kind is inferred.

valuenewDeclarationGroup :: Q [Dec]
#

Template Haskell is capable of reifying information about types and terms defined in previous declaration groups. Top-level declaration splices break up declaration groups.

For an example, consider this code block. We define a datatype X and then try to call reify on the datatype.

module Check where

data X = X
    deriving Eq

$(do
    info <- reify ''X
    runIO $ print info
 )

This code fails to compile, noting that X is not available for reification at the site of reify. We can fix this by creating a new declaration group using an empty top-level splice:

data X = X
    deriving Eq

$(pure [])

$(do
    info <- reify ''X
    runIO $ print info
 )

We provide newDeclarationGroup as a means of documenting this behavior and providing a name for the pattern.

Since top level splices infer the presence of the $( ... ) brackets, we can also write:

data X = X
    deriving Eq

newDeclarationGroup

$(do
    info <- reify ''X
    runIO $ print info
 )
valuereifyInstances :: Name -> [Type] -> Q [InstanceDec]
#

reifyInstances nm tys returns a list of all visible instances (see below for "visible") of nm tys. That is, if nm is the name of a type class, then all instances of this class at the types tys are returned. Alternatively, if nm is the name of a data family or type family, all instances of this family at the types tys are returned.

Note that this is a "shallow" test; the declarations returned merely have instance heads which unify with nm tys, they need not actually be satisfiable.

  • reifyInstances ''Eq [ TupleT 2 `AppT` ConT ''A `AppT` ConT ''B ] contains the instance (Eq a, Eq b) => Eq (a, b) regardless of whether A and B themselves implement Eq

  • reifyInstances ''Show [ VarT (mkName "a") ] produces every available instance of Show

There is one edge case: reifyInstances ''Typeable tys currently always produces an empty list (no matter what tys are given).

In principle, the *visible* instances are * all instances defined in a prior top-level declaration group (see docs on newDeclarationGroup), or * all instances defined in any module transitively imported by the module being compiled

However, actually searching all modules transitively below the one being compiled is unreasonably expensive, so reifyInstances will report only the instance for modules that GHC has had some cause to visit during this compilation. This is a shortcoming: reifyInstances might fail to report instances for a type that is otherwise unusued, or instances defined in a different component. You can work around this shortcoming by explicitly importing the modules whose instances you want to be visible. GHC issue #20529 has some discussion around this.

valuereifyRoles :: Name -> Q [Role]
#

reifyRoles nm returns the list of roles associated with the parameters (both visible and invisible) of the tycon nm. Fails if nm cannot be found or is not a tycon. The returned list should never contain InferR.

An invisible parameter to a tycon is often a kind parameter. For example, if we have

type Proxy :: forall k. k -> Type
data Proxy a = MkProxy

and reifyRoles Proxy, we will get [NominalR, PhantomR]. The NominalR is the role of the invisible k parameter. Kind parameters are always nominal.

valuereifyAnnotations :: Data a => AnnLookup -> Q [a]
#

reifyAnnotations target returns the list of annotations associated with target. Only the annotations that are appropriately typed is returned. So if you have Int and String annotations for the same target, you have to call this function twice.

reifyConStrictness nm looks up the strictness information for the fields of the constructor with the name nm. Note that the strictness information that reifyConStrictness returns may not correspond to what is written in the source code. For example, in the following data declaration:

data Pair a = Pair a a

reifyConStrictness would return [DecidedLazy, DecidedLazy] under most circumstances, but it would return [DecidedStrict, DecidedStrict] if the -XStrictData language extension was enabled.

valueisInstance :: Name -> [Type] -> Q Bool
#

Is the list of instances returned by reifyInstances nonempty?

If you're confused by an instance not being visible despite being defined in the same module and above the splice in question, see the docs for newDeclarationGroup for a possible explanation.

valuelocation :: Q Loc
#

The location at which this computation is spliced.

Get the package root for the current package which is being compiled. This can be set explicitly with the -package-root flag but is normally just the current working directory.

The motivation for this flag is to provide a principled means to remove the assumption from splices that they will be executed in the directory where the cabal file resides. Projects such as haskell-language-server can't and don't change directory when compiling files but instead set the -package-root flag appropriately.

valueaddDependentFile :: FilePath -> Q ()
#

Record external files that runIO is using (dependent upon). The compiler can then recognize that it should re-compile the Haskell file when an external file changes.

Expects an absolute file path.

Notes:

  • ghc -M does not know about these dependencies - it does not execute TH.

  • The dependency is based on file content, not a modification time

valueaddTempFile :: String -> Q FilePath
#

Obtain a temporary file path with the given suffix. The compiler will delete this file after compilation.

valueaddTopDecls :: [Dec] -> Q ()
#

Add additional top-level declarations. The added declarations will be type checked along with the current declaration group.

Emit a foreign file which will be compiled and linked to the object for the current module. Currently only languages that can be compiled with the C compiler are supported, and the flags passed as part of -optc will be also applied to the C compiler invocation that will compile them.

Note that for non-C languages (for example C++) extern C directives must be used to get symbols that we can access from Haskell.

To get better errors, it is recommended to use #line pragmas when emitting C files, e.g.

{-# LANGUAGE CPP #-}
...
addForeignSource LangC $ unlines
  [ "#line " ++ show (844 + 1) ++ " " ++ show "libraries/template-haskell/Language/Haskell/TH/Syntax.hs"
  , ...
  ]
valueaddModFinalizer :: Q () -> Q ()
#

Add a finalizer that will run in the Q monad after the current module has been type checked. This only makes sense when run within a top-level splice.

The finalizer is given the local type environment at the splice point. Thus reify is able to find the local definitions when executed inside the finalizer.

valueaddCorePlugin :: String -> Q ()
#

Adds a core plugin to the compilation pipeline.

addCorePlugin m has almost the same effect as passing -fplugin=m to ghc in the command line. The major difference is that the plugin module m must not belong to the current package. When TH executes, it is too late to tell the compiler that we needed to compile first a plugin module in the current package.

valuegetQ :: Typeable a => Q (Maybe a)
#

Get state from the Q monad. Note that the state is local to the Haskell module in which the Template Haskell expression is executed.

valueputQ :: Typeable a => a -> Q ()
#

Replace the state in the Q monad. Note that the state is local to the Haskell module in which the Template Haskell expression is executed.

valueputDoc :: DocLoc -> String -> Q ()
#

Add Haddock documentation to the specified location. This will overwrite any documentation at the location if it already exists. This will reify the specified name, so it must be in scope when you call it. If you want to add documentation to something that you are currently splicing, you can use addModFinalizer e.g.

do
  let nm = mkName "x"
  addModFinalizer $ putDoc (DeclDoc nm) "Hello"
  [d| $(varP nm) = 42 |]

The helper functions withDecDoc and withDecsDoc will do this for you, as will the funD_doc and other _doc combinators. You most likely want to have the -haddock flag turned on when using this. Adding documentation to anything outside of the current module will cause an error.

valuegetDoc :: DocLoc -> Q (Maybe String)
#

Retrieves the Haddock documentation at the specified location, if one exists. It can be used to read documentation on things defined outside of the current module, provided that those modules were compiled with the -haddock flag.

classclass Lift (t :: TYPE r) where
#

A Lift instance can have any of its values turned into a Template Haskell expression. This is needed when a value used within a Template Haskell quotation is bound outside the Oxford brackets ([| ... |] or [|| ... ||]) but not at the top level. As an example:

add1 :: Int -> Code Q Int
add1 x = [|| x + 1 ||]

Template Haskell has no way of knowing what value x will take on at splice-time, so it requires the type of x to be an instance of Lift.

A Lift instance must satisfy $(lift x) ≡ x and $$(liftTyped x) ≡ x for all x, where $(...) and $$(...) are Template Haskell splices. It is additionally expected that lift x ≡ unTypeCode (liftTyped x).

Lift instances can be derived automatically by use of the -XDeriveLift GHC language extension:

{-# LANGUAGE DeriveLift #-}
module Foo where

import Language.Haskell.TH.Syntax

data Bar a = Bar1 a (Bar a) | Bar2 String
  deriving Lift

Representation-polymorphic since template-haskell-2.16.0.0.

Methods

  • lift :: Quote m => t -> m Exp

    Turn a value into a Template Haskell expression, suitable for use in a splice.

  • liftTyped :: Quote m => t -> Code m t

    Turn a value into a Template Haskell typed expression, suitable for use in a typed splice.

Instances51Lift, …
  • Lift ByteArrayDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift IntegerDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift NaturalDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift VoidDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift Int16Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift Int32Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift Int64Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift Int8Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift Word16Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift Word32Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift Word64Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift Word8Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift Addr#Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax

    Produces an Addr# literal from the NUL-terminated C-string starting at the given memory address.

  • Lift Char#Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift Double#Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift Float#Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift Int#Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift Word#Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift BoolDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift CharDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift DoubleDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift FloatDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift IntDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift WordDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift ()Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift (# #)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Integral a => Lift (Ratio a)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift a => Lift (NonEmpty a)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift a => Lift (Maybe a)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift a => Lift [a]Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift (Fixed a)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • (Lift a, Lift b) => Lift (Either a b)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • (Lift a, Lift b) => Lift (a, b)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • (Lift a, Lift b, Lift c) => Lift (a, b, c)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • (Lift a, Lift b, Lift c, Lift d) => Lift (a, b, c, d)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • (Lift a, Lift b, Lift c, Lift d, Lift e) => Lift (a, b, c, d, e)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • (Lift a, Lift b, Lift c, Lift d, Lift e, Lift f) => Lift (a, b, c, d, e, f)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • (Lift a, Lift b, Lift c, Lift d, Lift e, Lift f, Lift g) => Lift (a, b, c, d, e, f, g)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • Lift a => Lift (# a #)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • (Lift a, Lift b) => Lift (# a, b #)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • (Lift a, Lift b) => Lift (# a | b #)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • (Lift a, Lift b, Lift c) => Lift (# a, b, c #)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • (Lift a, Lift b, Lift c) => Lift (# a | b | c #)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • (Lift a, Lift b, Lift c, Lift d) => Lift (# a, b, c, d #)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • (Lift a, Lift b, Lift c, Lift d) => Lift (# a | b | c | d #)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • (Lift a, Lift b, Lift c, Lift d, Lift e) => Lift (# a, b, c, d, e #)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • (Lift a, Lift b, Lift c, Lift d, Lift e) => Lift (# a | b | c | d | e #)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • (Lift a, Lift b, Lift c, Lift d, Lift e, Lift f) => Lift (# a, b, c, d, e, f #)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • (Lift a, Lift b, Lift c, Lift d, Lift e, Lift f) => Lift (# a | b | c | d | e | f #)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • (Lift a, Lift b, Lift c, Lift d, Lift e, Lift f, Lift g) => Lift (# a, b, c, d, e, f, g #)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • (Lift a, Lift b, Lift c, Lift d, Lift e, Lift f, Lift g) => Lift (# a | b | c | d | e | f | g #)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
datadata Lit
#

Constructors

Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata Bytes
#

Raw bytes embedded into the binary.

Avoid using Bytes constructor directly as it is likely to change in the future. Use helpers such as mkBytes in Language.Haskell.TH.Lib instead.

Constructors

Instances6Eq, Data, Ord, Show, Generic, Rep
valuedataToQa
  1. :: (Quote m, Data a)
  2. => Name -> k
  3. -> Lit -> m q
  4. -> k -> [m q] -> m q
  5. -> forall b. Data b => b -> Maybe (m q)
  6. -> a
  7. -> m q
#

dataToQa is an internal utility function for constructing generic conversion functions from types with Data instances to various quasi-quoting representations. See the source of dataToExpQ and dataToPatQ for two example usages: mkCon, mkLit and appQ are overloadable to account for different syntax for expressions and patterns; antiQ allows you to override type-specific cases, a common usage is just const Nothing, which results in no overloading.

datadata NameFlavour
#

Constructors

  • NameS

    An unqualified name; dynamically bound

  • NameQ ModName

    A qualified name; dynamically bound

  • NameU !Uniq

    A unique local name

  • NameL !Uniq

    Local name bound outside of the TH AST

  • NameG NameSpace PkgName ModName

    Global name bound outside of the TH AST: An original name (occurrences only, not binders) Need the namespace too to be sure which thing we are naming

Instances6Eq, Data, Ord, Show, Generic, Rep
datadata NameSpace
#

Constructors

  • VarName

    Variables

  • DataName

    Data constructors

  • TcClsName

    Type constructors and classes; Haskell has them in the same name space for now.

  • FldName
    • fldParent :: !String

      The textual name of the parent of the field.

      • For a field of a datatype, this is the name of the first constructor of the datatype (regardless of whether this constructor has this field).

      • For a field of a pattern synonym, this is the name of the pattern synonym.

Instances6Eq, Data, Ord, Show, Generic, Rep
valuedataToExpQ
  1. :: (Quote m, Data a)
  2. => forall b. Data b => b -> Maybe (m Exp)
  3. -> a
  4. -> m Exp
#

dataToExpQ converts a value to a Exp representation of the same value, in the SYB style. It is generalized to take a function override type-specific cases; see liftData for a more commonly used variant.

valuenameSpace :: Name -> Maybe NameSpace
#

Returns whether a name represents an occurrence of a top-level variable (VarName), data constructor (DataName), type constructor, or type class (TcClsName). If we can't be sure, it returns Nothing.

Examples
Example5 expressions
nameSpace 'Prelude.idJust VarNamenameSpace (mkName "id")Nothing -- only works for top-level variable namesnameSpace 'Data.Maybe.JustJust DataNamenameSpace ''Data.Maybe.MaybeJust TcClsNamenameSpace ''Data.Ord.OrdJust TcClsName
valuedataToPatQ
  1. :: (Quote m, Data a)
  2. => forall b. Data b => b -> Maybe (m Pat)
  3. -> a
  4. -> m Pat
#

dataToPatQ converts a value to a Pat representation of the same value, in the SYB style. It takes a function to handle type-specific cases, alternatively, pass const Nothing to get default behavior.

datadata Pat
#

Pattern in Haskell given in {}

Constructors

Instances7Eq, Data, Ord, Show, Generic, Ppr, …
newtypenewtype ModName
#

Constructors

Instances6Eq, Data, Ord, Show, Generic, Rep
newtypenewtype PkgName
#

Constructors

Instances6Eq, Data, Ord, Show, Generic, Rep
newtypenewtype OccName
#

Constructors

Instances6Eq, Data, Ord, Show, Generic, Rep
valuenameBase :: Name -> String
#

The name without its module prefix.

Examples
Example3 expressions
nameBase ''Data.Either.Either"Either"nameBase (mkName "foo")"foo"nameBase (mkName "Module.foo")"foo"
valuenameModule :: Name -> Maybe String
#

Module prefix of a name, if it exists.

Examples
Example3 expressions
nameModule ''Data.Either.EitherJust "Data.Either"nameModule (mkName "foo")NothingnameModule (mkName "Module.foo")Just "Module"
valuenamePackage :: Name -> Maybe String
#

A name's package, if it exists.

Examples
Example3 expressions
namePackage ''Data.Either.EitherJust "base"namePackage (mkName "foo")NothingnamePackage (mkName "Module.foo")Nothing
valuemkName :: String -> Name
#

Generate a capturable name. Occurrences of such names will be resolved according to the Haskell scoping rules at the occurrence site.

For example:

f = [| pi + $(varE (mkName "pi")) |]
...
g = let pi = 3 in $f

In this case, g is desugared to

g = Prelude.pi + 3

Note that mkName may be used with qualified names:

mkName "Prelude.pi"

See also dyn for a useful combinator. The above example could be rewritten using dyn as

f = [| pi + $(dyn "pi") |]
typetype PatSynType = Type
#

A pattern synonym's type. Note that a pattern synonym's fully specified type has a peculiar shape coming with two forall quantifiers and two constraint contexts. For example, consider the pattern synonym

pattern P x1 x2 ... xn = <some-pattern>

P's complete type is of the following form

pattern P :: forall universals.   required constraints
          => forall existentials. provided constraints
          => t1 -> t2 -> ... -> tn -> t

consisting of four parts:

  1. the (possibly empty lists of) universally quantified type variables and required constraints on them.

  2. the (possibly empty lists of) existentially quantified type variables and the provided constraints on them.

  3. the types t1, t2, .., tn of x1, x2, .., xn, respectively

  4. the type t of <some-pattern>, mentioning only universals.

Pattern synonym types interact with TH when (a) reifying a pattern synonym, (b) pretty printing, or (c) specifying a pattern synonym's type signature explicitly:

  • Reification always returns a pattern synonym's fully specified type in abstract syntax.

  • Pretty printing via pprPatSynType abbreviates a pattern synonym's type unambiguously in concrete syntax: The rule of thumb is to print initial empty universals and the required context as () =>, if existentials and a provided context follow. If only universals and their required context, but no existentials are specified, only the universals and their required context are printed. If both or none are specified, so both (or none) are printed.

  • When specifying a pattern synonym's type explicitly with PatSynSigD either one of the universals, the existentials, or their contexts may be left empty.

See the GHC user's guide for more information on pattern synonyms and their types: https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html#pattern-synonyms.

datadata FixityDirection
#
Instances6Eq, Data, Ord, Show, Generic, Rep
datadata Match
#

Constructors

Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata Body
#

Constructors

Instances6Eq, Data, Ord, Show, Generic, Rep
datadata Clause
#

Constructors

Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata Guard
#

Constructors

Instances6Eq, Data, Ord, Show, Generic, Rep
datadata Stmt
#

Constructors

Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata Range
#
Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata TyVarBndr flag
#

The flag type parameter is instantiated to one of the following types:

Constructors

Instances10Functor, Foldable, Traversable, Eq, Data, Ord, …
datadata BndrVis
#

Constructors

Instances8Eq, Data, Ord, Show, Generic, DefaultBndrFlag, …
datadata Con
#

A data constructor.

The constructors for Con can roughly be divided up into two categories: those for constructors with "vanilla" syntax (NormalC, RecC, and InfixC), and those for constructors with GADT syntax (GadtC and RecGadtC). The ForallC constructor, which quantifies additional type variables and class contexts, can surround either variety of constructor. However, the type variables that it quantifies are different depending on what constructor syntax is used:

  • If a ForallC surrounds a constructor with vanilla syntax, then the ForallC will only quantify existential type variables. For example:

  data Foo a = forall b. MkFoo a b
  

In MkFoo, ForallC will quantify b, but not a.

  • If a ForallC surrounds a constructor with GADT syntax, then the ForallC will quantify all type variables used in the constructor. For example:

  data Bar a b where
    MkBar :: (a ~ b) => c -> MkBar a b
  

In MkBar, ForallC will quantify a, b, and c.

Multiplicity annotations for data types are currently not supported in Template Haskell (i.e. all fields represented by Template Haskell will be linear).

Constructors

Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata DerivClause
#

A single deriving clause at the end of a datatype.

Constructors

Instances6Eq, Data, Ord, Show, Generic, Rep
datadata FunDep
#

Constructors

Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata Overlap
#

Varieties of allowed instance overlap.

Constructors

Instances6Eq, Data, Ord, Show, Generic, Rep
datadata NamespaceSpecifier
#

A way to specify a namespace to look in when GHC needs to find a name's source

Constructors

  • NoNamespaceSpecifier

    Name may be everything; If there are two names in different namespaces, then consider both

  • TypeNamespaceSpecifier

    Name should be a type-level entity, such as a data type, type alias, type family, type class, or type variable

  • DataNamespaceSpecifier

    Name should be a term-level entity, such as a function, data constructor, or pattern synonym

Instances6Eq, Data, Ord, Show, Generic, Rep
datadata Pragma
#
Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata TySynEqn
#

One equation of a type family instance or closed type family. The arguments are the left-hand-side type and the right-hand-side result.

For instance, if you had the following type family:

type family Foo (a :: k) :: k where
  forall k (a :: k). Foo @k a = a

The Foo @k a = a equation would be represented as follows:

TySynEqn (Just [PlainTV k, KindedTV a (VarT k)])
           (AppT (AppKindT (ConT ''Foo) (VarT k)) (VarT a))
           (VarT a)

Constructors

Instances6Eq, Data, Ord, Show, Generic, Rep
datadata TypeFamilyHead
#

Common elements of OpenTypeFamilyD and ClosedTypeFamilyD. By analogy with "head" for type classes and type class instances as defined in Type classes: an exploration of the design space, the TypeFamilyHead is defined to be the elements of the declaration between type family and where.

Instances6Eq, Data, Ord, Show, Generic, Rep
datadata DerivStrategy
#

What the user explicitly requests when deriving an instance.

Constructors

Instances6Eq, Data, Ord, Show, Generic, Rep
datadata PatSynArgs
#

A pattern synonym's argument type.

Constructors

Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata PatSynDir
#

A pattern synonym's directionality.

Constructors

Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata FamilyResultSig
#

Type family result signature

Constructors

Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata InjectivityAnn
#

Injectivity annotation

Constructors

Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata Inline
#
Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata RuleMatch
#
Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata Phases
#
Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata RuleBndr
#
Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata AnnTarget
#
Instances6Eq, Data, Ord, Show, Generic, Rep
typetype Pred = Type
#

Since the advent of ConstraintKinds, constraints are really just types. Equality constraints use the EqualityT constructor. Constraints may also be tuples of other constraints.

datadata SourceUnpackedness
#

SourceUnpackedness corresponds to unpack annotations found in the source code.

This may not agree with the annotations returned by reifyConStrictness. See reifyConStrictness for more information.

Constructors

Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata SourceStrictness
#

SourceStrictness corresponds to strictness annotations found in the source code.

This may not agree with the annotations returned by reifyConStrictness. See reifyConStrictness for more information.

Constructors

Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata Specificity
#
Instances8Eq, Data, Ord, Show, Generic, DefaultBndrFlag, …
datadata Bang
#

Constructors

Instances7Eq, Data, Ord, Show, Generic, Ppr, …
datadata TyLit
#

Constructors

Instances7Eq, Data, Ord, Show, Generic, Ppr, …

Language extensions

1 declaration
datadata ForeignSrcLang
#

Foreign formats supported by GHC via TH

Constructors

Instances4Eq, Show, Generic, Rep

Notes

0 declarations

Unresolved Infix

When implementing antiquotation for quasiquoters, one often wants to parse strings into expressions:

parse :: String -> Maybe Exp

But how should we parse a + b * c? If we don't know the fixities of + and *, we don't know whether to parse it as a + (b * c) or (a + b) * c.

In cases like this, use UInfixE, UInfixP, UInfixT, or PromotedUInfixT, which stand for "unresolved infix expressionpatterntype/promoted constructor", respectively. When the compiler is given a splice containing a tree of UInfixE applications such as

UInfixE
  (UInfixE e1 op1 e2)
  op2
  (UInfixE e3 op3 e4)

it will look up and the fixities of the relevant operators and reassociate the tree as necessary.

  • trees will not be reassociated across ParensE, ParensP, or ParensT, which are of use for parsing expressions like

(a + b * c) + d * e
  • InfixE, InfixP, InfixT, and PromotedInfixT expressions are never reassociated.

  • The UInfixE constructor doesn't support sections. Sections such as (a *) have no ambiguity, so InfixE suffices. For longer sections such as (a + b * c -), use an InfixE constructor for the outer-most section, and use UInfixE constructors for all other operators:

InfixE
  Just (UInfixE ...a + b * c...)
  op
  Nothing

Sections such as (a + b +) and ((a + b) +) should be rendered into Exps differently:

(+ a + b)   ---> InfixE Nothing + (Just $ UInfixE a + b)
                   -- will result in a fixity error if (+) is left-infix
(+ (a + b)) ---> InfixE Nothing + (Just $ ParensE $ UInfixE a + b)
                   -- no fixity errors
  • Quoted expressions such as

[| a * b + c |] :: Q Exp
[p| a : b : c |] :: Q Pat
[t| T + T |] :: Q Type

will never contain UInfixE, UInfixP, UInfixT, PromotedUInfixT, InfixT, 'PromotedInfixT, ParensE, ParensP, or ParensT constructors.