HORIZON HASKELLDocslts/ghc-9.14.xb2f3bfd2026-10-11Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · b2f3bfd · 2026-10-11

Moduleghc-lib-parser-9.14.1.20251220GHC2021

GHC.Internal.TH.Syntax

This module is used internally in GHC's integration with Template Haskell and defines the abstract syntax of Template Haskell.

This is not a part of the public API, and as such, there are no API guarantees for this module from version to version.

Import Language.Haskell.TH or Language.Haskell.TH.Syntax instead!

  • 80 types
  • 2 classes
  • 86 values
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.

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

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 (865 + 1) ++ " " ++ show "libraries/ghc-internal/src/GHC/Internal/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.

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.

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.

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.

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.

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.

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)

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.

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)

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

Lift a monadic action producing code into the typed Code representation

valuelocation :: Q Loc
#

The location at which this computation is spliced.

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.

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") |]
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
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
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
 )
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.

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.

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.

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.

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.

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.

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.

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.

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

Deprecated. Use reportError or reportWarning instead

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

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.

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.

valuerunQ :: Quasi m => Q a -> m a
#

"Runs" the Q monad. Normal users of Template Haskell should not need this function, as the splice brackets $( ... ) are the usual way of running a Q computation.

This function is primarily used in GHC internals, and for debugging splices by running them in IO.

Note that many functions in Q, such as reify and other compiler queries, are not supported when running Q in IO; these operations simply fail at runtime. Indeed, the only operations guaranteed to succeed are newName, runIO, reportError and reportWarning.

valuesequenceQ :: Monad m => forall a. [m a] -> m [a]
#

This function is only used in GHC.HsToCore.Quote when desugaring brackets. This is not necessary for the user, who can use the ordinary return and (>>=) operations.

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

Extract the untyped representation from the typed representation

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.

datadata AnnLookup
#

Annotation target for reifyAnnotations

Instances7Eq, Data, Ord, Show, Generic, Binary, …
datadata AnnTarget
#

The target of an ANN pragma

Constructors

Instances7Eq, Data, Ord, Show, Generic, Binary, …
datadata Bang
#

Strictness information in a data constructor's argument.

Constructors

Instances8Eq, Data, Ord, Show, Generic, Binary, …
typetype BangType = (Bang, Type)
#

A type with a strictness annotation, as in data constructors. See Con.

datadata BndrVis
#

Visibility of a type variable. See Inferred vs. specified type variables.

Constructors

Instances8Eq, Data, Ord, Show, Generic, Binary, …
  • Eq BndrVisDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Data BndrVisDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Ord BndrVisDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Show BndrVisDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Generic BndrVisDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Binary BndrVisDefined in ghc-lib-parser-9.14.1.20251220 · GHCi.TH.Binary · orphan
  • PprFlag BndrVisDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Boot.TH.Ppr
  • type Rep BndrVis = D1 ('MetaData "BndrVis" "GHC.Internal.TH.Syntax" "ghc-lib-parser-9.14.1.20251220-2HxJziFvlvq9nNTVQhN5nL" 'False) (C1 ('MetaCons "BndrReq" 'PrefixI 'False) U1 :+: C1 ('MetaCons "BndrInvis" 'PrefixI 'False) U1)Defined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
datadata Body
#

A potentially guarded expression, as in function definitions or case alternatives.

Constructors

Instances7Eq, Data, Ord, Show, Generic, Binary, …
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

Instances7Eq, Data, Ord, Show, Generic, Binary, …
datadata Callconv
#

A calling convention identifier, as in a Foreign declaration.

Instances7Eq, Data, Ord, Show, Generic, Binary, …
datadata Clause
#

A clause consists of patterns, guards, a body expression, and a list of declarations under a where. Clauses are seen in equations for function definitions, case-experssions, explicitly-bidirectional pattern synonyms, etc.

Constructors

Instances8Eq, Data, Ord, Show, Generic, Binary, …
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.Internal.Types.True GHC.Internal.Classes.== "foo"GHC.Internal.Types.True GHC.Internal.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

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

Instances8Eq, Data, Ord, Show, Generic, Binary, …
typetype Cxt = [Pred]
#

A context, as found on the left side of a => in a type.

datadata Dec
#

A single declaration.

Constructors

Instances8Eq, Data, Ord, Show, Generic, Binary, …
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

Instances8Eq, Data, Ord, Show, Generic, Binary, …
datadata DerivClause
#

A single deriving clause at the end of a datatype declaration.

Constructors

Instances7Eq, Data, Ord, Show, Generic, Binary, …
datadata DerivStrategy
#

What the user explicitly requests when deriving an instance with -XDerivingStrategies.

Constructors

Instances7Eq, Data, Ord, Show, Generic, Binary, …
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

Instances7Eq, Data, Ord, Show, Generic, Binary, …
datadata Exp
#

A Haskell expression.

Constructors

Instances8Eq, Data, Ord, Show, Generic, Binary, …
datadata FamilyResultSig
#

Type family result signature

Constructors

Instances8Eq, Data, Ord, Show, Generic, Binary, …
datadata Fixity
#

Fixity, as specified in a infix[lr] n declaration.

Instances7Eq, Data, Ord, Show, Generic, Binary, …
datadata FixityDirection
#

The associativity of an operator, as in an infix declaration.

Instances7Eq, Data, Ord, Show, Generic, Binary, …
datadata Foreign
#

A foreign declaration.

Constructors

Instances8Eq, Data, Ord, Show, Generic, Binary, …
datadata FunDep
#

Functional dependency syntax, as in a class declaration.

Constructors

Instances8Eq, Data, Ord, Show, Generic, Binary, …
datadata Guard
#

A single guard.

Constructors

Instances7Eq, Data, Ord, Show, Generic, Binary, …
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 and is tracked by GHC #14474.

  • 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.

Instances8Eq, Data, Ord, Show, Generic, Binary, …
datadata InjectivityAnn
#

Injectivity annotation as in an injective type family

Constructors

Instances8Eq, Data, Ord, Show, Generic, Binary, …
datadata Inline
#

An inline pragma.

Constructors

Instances8Eq, Data, Ord, Show, Generic, Binary, …
  • Eq InlineDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Data InlineDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Ord InlineDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Show InlineDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Generic InlineDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Binary InlineDefined in ghc-lib-parser-9.14.1.20251220 · GHCi.TH.Binary · orphan
  • Ppr InlineDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Boot.TH.Ppr
  • type Rep Inline = D1 ('MetaData "Inline" "GHC.Internal.TH.Syntax" "ghc-lib-parser-9.14.1.20251220-2HxJziFvlvq9nNTVQhN5nL" 'False) (C1 ('MetaCons "NoInline" 'PrefixI 'False) U1 :+: (C1 ('MetaCons "Inline" 'PrefixI 'False) U1 :+: C1 ('MetaCons "Inlinable" 'PrefixI 'False) U1))Defined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
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 Lit
#

A Haskell literal. Note that the numeric types are all in terms of either Integer or Rational, regardless of the type they represent. The extra precision reflects the textual representation in source code.

Constructors

Instances8Eq, Data, Ord, Show, Generic, Binary, …
datadata Loc
#

A location within a source file.

Instances8Eq, Data, Ord, Show, Generic, Binary, …
datadata Match
#

A case-alternative

Constructors

Instances8Eq, Data, Ord, Show, Generic, Binary, …
newtypenewtype ModName
#

The name of a module.

Constructors

Instances7Eq, Data, Ord, Show, Generic, Binary, …
datadata Module
#

Obtained from reifyModule and thisModule.

Constructors

Instances8Eq, Data, Ord, Show, Generic, Binary, …
datadata ModuleInfo
#

Obtained from reifyModule in the Q Monad.

Constructors

Instances8Eq, Data, Ord, Show, Generic, Binary, …
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.

Instances8Eq, Data, Ord, Show, Generic, Binary, …
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

Instances7Eq, Data, Ord, Show, Generic, Binary, …
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.

Instances7Eq, Data, Ord, Show, Generic, Binary, …
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

Instances7Eq, Data, Ord, Show, Generic, Binary, …
newtypenewtype OccName
#

An "Occurence Name".

Constructors

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

Varieties of allowed instance overlap.

Constructors

Instances7Eq, Data, Ord, Show, Generic, Binary, …
datadata Pat
#

Pattern in Haskell given in {}

Constructors

Instances8Eq, Data, Ord, Show, Generic, Binary, …
datadata PatSynArgs
#

A pattern synonym's argument type.

Constructors

Instances8Eq, Data, Ord, Show, Generic, Binary, …
datadata PatSynDir
#

A pattern synonym's directionality.

Constructors

Instances8Eq, Data, Ord, Show, Generic, Binary, …
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 Phases
#

Phase control syntax.

Constructors

Instances8Eq, Data, Ord, Show, Generic, Binary, …
newtypenewtype PkgName
#

The name of a package.

Constructors

Instances7Eq, Data, Ord, Show, Generic, Binary, …
datadata Pragma
#

Constructors

Instances8Eq, Data, Ord, Show, Generic, Binary, …
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.

newtypenewtype Q a
#

In short, Q provides the Quasi operations in one neat monad for the user.

The longer story, is that Q wraps an arbitrary Quasi-able monad. The perceptive reader notices that Quasi has only two instances, Q itself and IO, neither of which have concrete implementations.Q plays the trick of dependency inversion, providing an abstract interface for the user which is later concretely fufilled by an concrete Quasi instance, internal to GHC.

Constructors

Instances10Monad, Functor, MonadFix, MonadFail, Applicative, MonadIO, …
  • Monad QDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Functor QDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • MonadFix QDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax

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

  • MonadFail QDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Applicative QDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • MonadIO QDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Quasi QDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Quote QDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Semigroup a => Semigroup (Q a)Defined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Monoid a => Monoid (Q a)Defined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
classclass (MonadIO m, MonadFail m) => Quasi (m :: Type -> Type) where
#

Methods

Instances2Quasi
  • Quasi IODefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Quasi QDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
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 ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Quote QDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
datadata Range
#

A list/enum range expression.

Constructors

Instances8Eq, Data, Ord, Show, Generic, Binary, …
datadata Role
#

Role annotations

Constructors

Instances8Eq, Data, Ord, Show, Generic, Binary, …
  • Eq RoleDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Data RoleDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Ord RoleDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Show RoleDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Generic RoleDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Binary RoleDefined in ghc-lib-parser-9.14.1.20251220 · GHCi.TH.Binary · orphan
  • Ppr RoleDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Boot.TH.Ppr
  • type Rep Role = D1 ('MetaData "Role" "GHC.Internal.TH.Syntax" "ghc-lib-parser-9.14.1.20251220-2HxJziFvlvq9nNTVQhN5nL" 'False) ((C1 ('MetaCons "NominalR" 'PrefixI 'False) U1 :+: C1 ('MetaCons "RepresentationalR" 'PrefixI 'False) U1) :+: (C1 ('MetaCons "PhantomR" 'PrefixI 'False) U1 :+: C1 ('MetaCons "InferR" 'PrefixI 'False) U1))Defined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
datadata RuleBndr
#

A binder found in the forall of a RULES pragma.

Constructors

Instances8Eq, Data, Ord, Show, Generic, Binary, …
datadata RuleMatch
#

A CONLIKE modifier, as in one of the various inline pragmas, or lack thereof (FunLike).

Constructors

  • ConLike
    { {-# CONLIKE [inline] ... #-} }
  • FunLike
    { {-# [inline] ... #-} }
Instances8Eq, Data, Ord, Show, Generic, Binary, …
  • Eq RuleMatchDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Data RuleMatchDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Ord RuleMatchDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Show RuleMatchDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Generic RuleMatchDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
  • Binary RuleMatchDefined in ghc-lib-parser-9.14.1.20251220 · GHCi.TH.Binary · orphan
  • Ppr RuleMatchDefined in ghc-lib-parser-9.14.1.20251220 · GHC.Boot.TH.Ppr
  • type Rep RuleMatch = D1 ('MetaData "RuleMatch" "GHC.Internal.TH.Syntax" "ghc-lib-parser-9.14.1.20251220-2HxJziFvlvq9nNTVQhN5nL" 'False) (C1 ('MetaCons "ConLike" 'PrefixI 'False) U1 :+: C1 ('MetaCons "FunLike" 'PrefixI 'False) U1)Defined in ghc-lib-parser-9.14.1.20251220 · GHC.Internal.TH.Syntax
datadata Safety
#

A safety level, as in a Foreign declaration.

Instances7Eq, Data, Ord, Show, Generic, Binary, …
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

Instances8Eq, Data, Ord, Show, Generic, Binary, …
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

Instances8Eq, Data, Ord, Show, Generic, Binary, …
datadata Specificity
#

The specificity of a type variable in a forall ....

Instances8Eq, Data, Ord, Show, Generic, Binary, …
datadata Stmt
#

A single statement, as in do-notation.

Constructors

Instances8Eq, Data, Ord, Show, Generic, Binary, …
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 TyLit
#

Type-level literals.

Constructors

Instances8Eq, Data, Ord, Show, Generic, Binary, …
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

Instances7Eq, Data, Ord, Show, Generic, Binary, …
datadata TyVarBndr flag
#

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

Constructors

Instances11Functor, Foldable, Traversable, Eq, Data, Ord, …
datadata Type
#

A Haskell type.

Constructors

Instances8Eq, Data, Ord, Show, Generic, Binary, …
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.

Instances7Eq, Data, Ord, Show, Generic, Binary, …
typetype Uniq = Integer
#

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

Language extensions

1 declaration
datadata ForeignSrcLang
#

Foreign formats supported by GHC via TH

Constructors

Instances5Eq, Show, Generic, Binary, 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 expression pattern type / 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.