Copyright | (C) 2014 Richard Eisenberg |
---|---|
License | BSD-style (see LICENSE) |
Maintainer | Richard Eisenberg (eir@cis.upenn.edu) |
Stability | experimental |
Portability | non-portable |
Safe Haskell | None |
Language | Haskell2010 |
Language.Haskell.TH.Desugar
Contents
Description
Desugars full Template Haskell syntax into a smaller core syntax for further processing. The desugared types and constructors are prefixed with a D.
- data DExp
- data DLetDec
- data DPat
- data DType
- type DKind = DType
- type DCxt = [DPred]
- data DPred
- data DTyVarBndr
- data DMatch = DMatch DPat DExp
- data DClause = DClause [DPat] DExp
- data DDec
- = DLetDec DLetDec
- | DDataD NewOrData DCxt Name [DTyVarBndr] [DCon] [DPred]
- | DTySynD Name [DTyVarBndr] DType
- | DClassD DCxt Name [DTyVarBndr] [FunDep] [DDec]
- | DInstanceD (Maybe Overlap) DCxt DType [DDec]
- | DForeignD DForeign
- | DPragmaD DPragma
- | DOpenTypeFamilyD DTypeFamilyHead
- | DClosedTypeFamilyD DTypeFamilyHead [DTySynEqn]
- | DDataFamilyD Name [DTyVarBndr]
- | DDataInstD NewOrData DCxt Name [DType] [DCon] [DPred]
- | DTySynInstD Name DTySynEqn
- | DRoleAnnotD Name [Role]
- | DStandaloneDerivD DCxt DType
- | DDefaultSigD Name DType
- data Overlap :: *
- data NewOrData
- data DTypeFamilyHead = DTypeFamilyHead Name [DTyVarBndr] DFamilyResultSig (Maybe InjectivityAnn)
- data DFamilyResultSig
- data InjectivityAnn :: * = InjectivityAnn Name [Name]
- data DCon = DCon [DTyVarBndr] DCxt Name DConFields (Maybe DType)
- data DConFields
- = DNormalC [DBangType]
- | DRecC [DVarBangType]
- type DBangType = (Bang, DType)
- type DVarBangType = (Name, Bang, DType)
- data Bang :: * = Bang SourceUnpackedness SourceStrictness
- data SourceUnpackedness :: *
- data SourceStrictness :: *
- data DForeign
- data DPragma
- data DRuleBndr
- data DTySynEqn = DTySynEqn [DType] DType
- data DInfo
- type DInstanceDec = DDec
- data Role :: *
- data AnnTarget :: *
- class Desugar th ds | ds -> th where
- dsExp :: DsMonad q => Exp -> q DExp
- dsDecs :: DsMonad q => [Dec] -> q [DDec]
- dsType :: DsMonad q => Type -> q DType
- dsInfo :: DsMonad q => Info -> q DInfo
- dsPatOverExp :: DsMonad q => Pat -> DExp -> q (DPat, DExp)
- dsPatsOverExp :: DsMonad q => [Pat] -> DExp -> q ([DPat], DExp)
- dsPatX :: DsMonad q => Pat -> q (DPat, [(Name, DExp)])
- dsLetDecs :: DsMonad q => [Dec] -> q [DLetDec]
- dsTvb :: DsMonad q => TyVarBndr -> q DTyVarBndr
- dsCxt :: DsMonad q => Cxt -> q DCxt
- dsCon :: DsMonad q => Con -> q [DCon]
- dsForeign :: DsMonad q => Foreign -> q DForeign
- dsPragma :: DsMonad q => Pragma -> q DPragma
- dsRuleBndr :: DsMonad q => RuleBndr -> q DRuleBndr
- type PatM q = WriterT [(Name, DExp)] q
- dsPred :: DsMonad q => Pred -> q DCxt
- dsPat :: DsMonad q => Pat -> PatM q DPat
- dsDec :: DsMonad q => Dec -> q [DDec]
- dsLetDec :: DsMonad q => Dec -> q [DLetDec]
- dsMatches :: DsMonad q => Name -> [Match] -> q [DMatch]
- dsBody :: DsMonad q => Body -> [Dec] -> DExp -> q DExp
- dsGuards :: DsMonad q => [(Guard, Exp)] -> DExp -> q DExp
- dsDoStmts :: DsMonad q => [Stmt] -> q DExp
- dsComp :: DsMonad q => [Stmt] -> q DExp
- dsClauses :: DsMonad q => Name -> [Clause] -> q [DClause]
- dsBangType :: DsMonad q => BangType -> q DBangType
- dsVarBangType :: DsMonad q => VarBangType -> q DVarBangType
- dsTypeFamilyHead :: DsMonad q => TypeFamilyHead -> q DTypeFamilyHead
- dsFamilyResultSig :: DsMonad q => FamilyResultSig -> q DFamilyResultSig
- module Language.Haskell.TH.Desugar.Sweeten
- expand :: (DsMonad q, Data a) => a -> q a
- expandType :: DsMonad q => DType -> q DType
- reifyWithWarning :: Quasi q => Name -> q Info
- withLocalDeclarations :: DsMonad q => [Dec] -> DsM q a -> q a
- dsReify :: DsMonad q => Name -> q (Maybe DInfo)
- reifyWithLocals_maybe :: DsMonad q => Name -> q (Maybe Info)
- reifyWithLocals :: DsMonad q => Name -> q Info
- class Quasi m => DsMonad m where
- data DsM q a
- scExp :: DsMonad q => DExp -> q DExp
- scLetDec :: DsMonad q => DLetDec -> q DLetDec
- applyDExp :: DExp -> [DExp] -> DExp
- applyDType :: DType -> [DType] -> DType
- dPatToDExp :: DPat -> DExp
- removeWilds :: DsMonad q => DPat -> q DPat
- getDataD :: Quasi q => String -> Name -> q ([TyVarBndr], [Con])
- dataConNameToDataName :: Quasi q => Name -> q Name
- dataConNameToCon :: Quasi q => Name -> q Con
- nameOccursIn :: Data a => Name -> a -> Bool
- allNamesIn :: Data a => a -> [Name]
- flattenDValD :: Quasi q => DLetDec -> q [DLetDec]
- getRecordSelectors :: Quasi q => DType -> DCon -> q [DLetDec]
- mkTypeName :: Quasi q => String -> q Name
- mkDataName :: Quasi q => String -> q Name
- newUniqueName :: Quasi q => String -> q Name
- mkTupleDExp :: [DExp] -> DExp
- mkTupleDPat :: [DPat] -> DPat
- maybeDLetE :: [DLetDec] -> DExp -> DExp
- maybeDCaseE :: String -> DExp -> [DMatch] -> DExp
- substTy :: DsMonad q => Map Name DType -> DType -> q DType
- tupleDegree_maybe :: String -> Maybe Int
- tupleNameDegree_maybe :: Name -> Maybe Int
- unboxedTupleDegree_maybe :: String -> Maybe Int
- unboxedTupleNameDegree_maybe :: Name -> Maybe Int
- strictToBang :: Bang -> Bang
- extractBoundNamesStmt :: Stmt -> Set Name
- extractBoundNamesDec :: Dec -> Set Name
- extractBoundNamesPat :: Pat -> Set Name
Desugared data types
Corresponds to TH's Exp
type. Note that DLamE
takes names, not patterns.
Declarations as used in a let
statement.
Corresponds to TH's Pat
type.
Corresponds to TH's Type
type, used to represent
types and kinds.
Corresponds to TH's Pred
Corresponds to TH's Match
type.
Corresponds to TH's Clause
type.
Corresponds to TH's Dec
type.
Constructors
Varieties of allowed instance overlap.
Constructors
Overlappable | May be overlapped by more specific instances |
Overlapping | May overlap a more general instance |
Overlaps | Both |
Incoherent | Both |
Is it a newtype
or a data
type?
data DTypeFamilyHead #
Corresponds to TH's TypeFamilyHead
type
Constructors
DTypeFamilyHead Name [DTyVarBndr] DFamilyResultSig (Maybe InjectivityAnn) |
Instances
data DFamilyResultSig #
Corresponds to TH's FamilyResultSig
type
Constructors
DNoSig | |
DKindSig DKind | |
DTyVarSig DTyVarBndr |
Instances
Corresponds to TH's Con
type.
Constructors
DCon [DTyVarBndr] DCxt Name DConFields (Maybe DType) | A GADT result type, if there is one |
data DConFields #
A list of fields either for a standard data constructor or a record data constructor.
Constructors
DNormalC [DBangType] | |
DRecC [DVarBangType] |
Instances
Data DConFields # | |
Show DConFields # | |
Generic DConFields # | |
type Rep DConFields # | |
type DVarBangType = (Name, Bang, DType) #
Corresponds to TH's VarBangType
type.
Constructors
Bang SourceUnpackedness SourceStrictness | C { {-# UNPACK #-} !}a |
data SourceUnpackedness :: * #
Constructors
NoSourceUnpackedness | C a |
SourceNoUnpack | C { {-# NOUNPACK #-} } a |
SourceUnpack | C { {-# UNPACK #-} } a |
data SourceStrictness :: * #
Constructors
NoSourceStrictness | C a |
SourceLazy | C {~}a |
SourceStrict | C {!}a |
Corresponds to TH's Foreign
type.
Corresponds to TH's Pragma
type.
Corresponds to TH's RuleBndr
type.
Constructors
DRuleVar Name | |
DTypedRuleVar Name DType |
Corresponds to TH's TySynEqn
type (to store type family equations).
Corresponds to TH's Info
type.
Constructors
DTyConI DDec (Maybe [DInstanceDec]) | |
DVarI Name DType (Maybe Name) | The |
DTyVarI Name DKind | |
DPrimTyConI Name Int Bool | The |
type DInstanceDec #
Arguments
= DDec | Guaranteed to be an instance declaration |
Role annotations
Constructors
NominalR | nominal |
RepresentationalR | representational |
PhantomR | phantom |
InferR | _ |
Constructors
ModuleAnnotation | |
TypeAnnotation Name | |
ValueAnnotation Name |
The Desugar
class
Main desugaring functions
dsPatOverExp :: DsMonad q => Pat -> DExp -> q (DPat, DExp) #
Desugar a pattern, along with processing a (desugared) expression that is the entire scope of the variables bound in the pattern.
dsPatsOverExp :: DsMonad q => [Pat] -> DExp -> q ([DPat], DExp) #
Desugar multiple patterns. Like dsPatOverExp
.
dsPatX :: DsMonad q => Pat -> q (DPat, [(Name, DExp)]) #
Desugar a pattern, returning a list of (Name, DExp) pairs of extra variables that must be bound within the scope of the pattern
dsTvb :: DsMonad q => TyVarBndr -> q DTyVarBndr #
Desugar a TyVarBndr
dsRuleBndr :: DsMonad q => RuleBndr -> q DRuleBndr #
Desugar a RuleBndr
.
Secondary desugaring functions
type PatM q = WriterT [(Name, DExp)] q #
Desugaring a pattern also returns the list of variables bound in as-patterns and the values they should be bound to. This variables must be brought into scope in the "body" of the pattern.
dsLetDec :: DsMonad q => Dec -> q [DLetDec] #
Desugar a single Dec
, perhaps producing multiple DLetDec
s
Arguments
:: DsMonad q | |
=> Name | Name of the scrutinee, which must be a bare var |
-> [Match] | Matches of the |
-> q [DMatch] |
Desugar a list of matches for a case
statement
Arguments
:: DsMonad q | |
=> Body | body to desugar |
-> [Dec] | "where" declarations |
-> DExp | what to do if the guards don't match |
-> q DExp |
Desugar a Body
Arguments
:: DsMonad q | |
=> [(Guard, Exp)] | Guarded expressions |
-> DExp | What to do if none of the guards match |
-> q DExp |
Desugar guarded expressions
Desugar clauses to a function definition
dsBangType :: DsMonad q => BangType -> q DBangType #
Desugar a BangType
(or a StrictType
, if you're old-fashioned)
dsVarBangType :: DsMonad q => VarBangType -> q DVarBangType #
Desugar a VarBangType
(or a VarStrictType
, if you're old-fashioned)
dsTypeFamilyHead :: DsMonad q => TypeFamilyHead -> q DTypeFamilyHead #
Desugar a TypeFamilyHead
dsFamilyResultSig :: DsMonad q => FamilyResultSig -> q DFamilyResultSig #
Desugar a FamilyResultSig
Converting desugared AST back to TH AST
Expanding type synonyms
expandType :: DsMonad q => DType -> q DType #
Expands all type synonyms in a desugared type. Also expands open type family applications, as long as the arguments have no free variables. Attempts to expand closed type family applications, but aborts the moment it spots anything strange, like a nested type family application or type variable.
Reification
reifyWithWarning :: Quasi q => Name -> q Info #
Reify a declaration, warning the user about splices if the reify fails. The warning says that reification can fail if you try to reify a type in the same splice as it is declared.
The following definitions allow you to register a list of
Dec
s to be used in reification queries.
withLocalDeclarations :: DsMonad q => [Dec] -> DsM q a -> q a #
Add a list of declarations to be considered when reifying local declarations.
dsReify :: DsMonad q => Name -> q (Maybe DInfo) #
Like reify
, but safer and desugared. Uses local declarations where
available.
reifyWithLocals_maybe :: DsMonad q => Name -> q (Maybe Info) #
Like reify
from Template Haskell, but looks also in any not-yet-typechecked
declarations. To establish this list of not-yet-typechecked declarations,
use withLocalDeclarations
. Returns Nothing
if reification fails.
Note that no inferred type information is available from local declarations;
bottoms may be used if necessary.
reifyWithLocals :: DsMonad q => Name -> q Info #
Like reifyWithLocals_maybe
, but throws an exception upon failure,
warning the user about separating splices.
class Quasi m => DsMonad m where #
A DsMonad
stores some list of declarations that should be considered
in scope. DsM
is the prototypical inhabitant of DsMonad
.
Minimal complete definition
A convenient implementation of the DsMonad
class. Use by calling
withLocalDeclarations
.
Nested pattern flattening
Utility functions
removeWilds :: DsMonad q => DPat -> q DPat #
Remove all wildcards from a pattern, replacing any wildcard with a fresh variable
Arguments
:: Quasi q | |
=> String | Print this out on failure |
-> Name | Name of the datatype ( |
-> q ([TyVarBndr], [Con]) |
Extract the TyVarBndr
s and constructors given the Name
of a type
dataConNameToDataName :: Quasi q => Name -> q Name #
From the name of a data constructor, retrive the datatype definition it is a part of.
dataConNameToCon :: Quasi q => Name -> q Con #
From the name of a data constructor, retrieve its definition as a Con
nameOccursIn :: Data a => Name -> a -> Bool #
Check if a name occurs anywhere within a TH tree.
allNamesIn :: Data a => a -> [Name] #
Extract all Names mentioned in a TH tree.
flattenDValD :: Quasi q => DLetDec -> q [DLetDec] #
If the declaration passed in is a DValD
, creates new, equivalent
declarations such that the DPat
in all DValD
s is just a plain
DVarPa
. Other declarations are passed through unchanged.
Note that the declarations that come out of this function are rather
less efficient than those that come in: they have many more pattern
matches.
mkTypeName :: Quasi q => String -> q Name #
Like TH's lookupTypeName
, but if this name is not bound, then we assume
it is declared in the current module.
mkDataName :: Quasi q => String -> q Name #
Like TH's lookupDataName
, but if this name is not bound, then we assume
it is declared in the current module.
newUniqueName :: Quasi q => String -> q Name #
Like newName, but even more unique (unique across different splices),
and with unique nameBase
s. Precondition: the string is a valid Haskell
alphanumeric identifier (could be upper- or lower-case).
maybeDLetE :: [DLetDec] -> DExp -> DExp #
If decs is non-empty, delcare them in a let:
maybeDCaseE :: String -> DExp -> [DMatch] -> DExp #
If matches is non-empty, make a case statement; otherwise make an error statement
tupleDegree_maybe :: String -> Maybe Int #
Extract the degree of a tuple
tupleNameDegree_maybe :: Name -> Maybe Int #
Extract the degree of a tuple name
unboxedTupleDegree_maybe :: String -> Maybe Int #
Extract the degree of an unboxed tuple
unboxedTupleNameDegree_maybe :: Name -> Maybe Int #
Extract the degree of a tuple name
strictToBang :: Bang -> Bang #
Extracting bound names
extractBoundNamesStmt :: Stmt -> Set Name #
Extract the names bound in a Stmt
extractBoundNamesDec :: Dec -> Set Name #
Extract the names bound in a Dec
that could appear in a let
expression.
extractBoundNamesPat :: Pat -> Set Name #
Extract the names bound in a Pat