th-desugar-1.4.2.1: Functions to desugar Template Haskell

Copyright(C) 2014 Richard Eisenberg
LicenseBSD-style (see LICENSE)
MaintainerRichard Eisenberg (eir@cis.upenn.edu)
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

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.

Synopsis

Desugared data types

data DExp Source

Corresponds to TH's Exp type. Note that DLamE takes names, not patterns.

data DLetDec Source

Declarations as used in a let statement.

data DPat Source

Corresponds to TH's Pat type.

data DKind Source

Corresponds to TH's Kind type, which is a synonym for Type. DKind, though, only contains constructors that make sense for kinds.

type DCxt = [DPred] Source

Corresponds to TH's Cxt

data DPred Source

Corresponds to TH's Pred

data DTyVarBndr Source

Corresponds to TH's TyVarBndr. Note that PlainTV x and KindedTV x StarT are distinct, so we retain that distinction here.

data DMatch Source

Corresponds to TH's Match type.

Constructors

DMatch DPat DExp 

data DClause Source

Corresponds to TH's Clause type.

Constructors

DClause [DPat] DExp 

data NewOrData Source

Is it a newtype or a data type?

Constructors

Newtype 
Data 

data DCon Source

Corresponds to TH's Con type.

data DConFields Source

A list of fields either for a standard data constructor or a record data constructor.

type DStrictType = (Strict, DType) Source

Corresponds to TH's StrictType type.

type DVarStrictType = (Name, Strict, DType) Source

Corresponds to TH's VarStrictType type.

data DRuleBndr Source

Corresponds to TH's RuleBndr type.

data DTySynEqn Source

Corresponds to TH's TySynEqn type (to store type family equations).

Constructors

DTySynEqn [DType] DType 

data DInfo Source

Corresponds to TH's Info type.

Constructors

DTyConI DDec (Maybe [DInstanceDec]) 
DVarI Name DType (Maybe Name) Fixity

The Maybe Name stores the name of the enclosing definition (datatype, for a data constructor; class, for a method), if any

DTyVarI Name DKind 
DPrimTyConI Name Int Bool

The Int is the arity; the Bool is whether this tycon is unlifted.

type DInstanceDec Source

Arguments

 = DDec

Guaranteed to be an instance declaration

data Role :: *

Role annotations

Constructors

NominalR
nominal
RepresentationalR
representational
PhantomR
phantom
InferR
_

The Desugar class

class Desugar th ds | ds -> th where Source

This class relates a TH type with its th-desugar type and allows conversions back and forth. The functional dependency goes only one way because Type and Kind are type synonyms, but they desugar to different types.

Methods

desugar :: Quasi q => th -> q ds Source

sweeten :: ds -> th Source

Main desugaring functions

dsExp :: Quasi q => Exp -> q DExp Source

Desugar an expression

dsDecs :: Quasi q => [Dec] -> q [DDec] Source

Desugar arbitrary Decs

dsType :: Quasi q => Type -> q DType Source

Desugar a type

dsKind :: Quasi q => Kind -> q DKind Source

Desugar a kind

dsInfo :: Quasi q => Info -> q DInfo Source

Desugar Info

dsPatOverExp :: Quasi q => Pat -> DExp -> q (DPat, DExp) Source

Desugar a pattern, along with processing a (desugared) expression that is the entire scope of the variables bound in the pattern.

dsPatsOverExp :: Quasi q => [Pat] -> DExp -> q ([DPat], DExp) Source

Desugar multiple patterns. Like dsPatOverExp.

dsPatX :: Quasi q => Pat -> q (DPat, [(Name, DExp)]) Source

Desugar a pattern, returning a list of (Name, DExp) pairs of extra variables that must be bound within the scope of the pattern

dsLetDecs :: Quasi q => [Dec] -> q [DLetDec] Source

Desugar Decs that can appear in a let expression

dsTvb :: Quasi q => TyVarBndr -> q DTyVarBndr Source

Desugar a TyVarBndr

dsCxt :: Quasi q => Cxt -> q DCxt Source

Desugar a Cxt

dsCon :: Quasi q => Con -> q DCon Source

Desugar a single Con.

dsForeign :: Quasi q => Foreign -> q DForeign Source

Desugar a Foreign.

dsPragma :: Quasi q => Pragma -> q DPragma Source

Desugar a Pragma.

dsRuleBndr :: Quasi q => RuleBndr -> q DRuleBndr Source

Desugar a RuleBndr.

Secondary desugaring functions

type PatM q = WriterT [(Name, DExp)] q Source

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.

dsPred :: Quasi q => Pred -> q DCxt Source

Desugar a Pred, flattening any internal tuples

dsPat :: Quasi q => Pat -> PatM q DPat Source

Desugar a pattern.

dsDec :: Quasi q => Dec -> q [DDec] Source

Desugar a single Dec, perhaps producing multiple DDecs

dsLetDec :: Quasi q => Dec -> q [DLetDec] Source

Desugar a single Dec, perhaps producing multiple DLetDecs

dsMatches Source

Arguments

:: Quasi q 
=> Name

Name of the scrutinee, which must be a bare var

-> [Match]

Matches of the case statement

-> q [DMatch] 

Desugar a list of matches for a case statement

dsBody Source

Arguments

:: Quasi q 
=> Body

body to desugar

-> [Dec]

"where" declarations

-> DExp

what to do if the guards don't match

-> q DExp 

Desugar a Body

dsGuards Source

Arguments

:: Quasi q 
=> [(Guard, Exp)]

Guarded expressions

-> DExp

What to do if none of the guards match

-> q DExp 

Desugar guarded expressions

dsDoStmts :: Quasi q => [Stmt] -> q DExp Source

Desugar the Stmts in a do expression

dsComp :: Quasi q => [Stmt] -> q DExp Source

Desugar the Stmts in a list or monad comprehension

dsClauses Source

Arguments

:: Quasi q 
=> Name

Name of the function

-> [Clause]

Clauses to desugar

-> q [DClause] 

Desugar clauses to a function definition

Utility functions

applyDExp :: DExp -> [DExp] -> DExp Source

Apply one DExp to a list of arguments

applyDType :: DType -> [DType] -> DType Source

Apply one DType to a list of arguments

dPatToDExp :: DPat -> DExp Source

Convert a DPat to a DExp. Fails on DWildP.

removeWilds :: Quasi q => DPat -> q DPat Source

Remove all wildcards from a pattern, replacing any wildcard with a fresh variable

reifyWithWarning :: Quasi q => Name -> q Info Source

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.

getDataD Source

Arguments

:: Quasi q 
=> String

Print this out on failure

-> Name

Name of the datatype (data or newtype) of interest

-> q ([TyVarBndr], [Con]) 

Extract the TyVarBndrs and constructors given the Name of a type

dataConNameToDataName :: Quasi q => Name -> q Name Source

From the name of a data constructor, retrive the datatype definition it is a part of.

dataConNameToCon :: Quasi q => Name -> q Con Source

From the name of a data constructor, retrieve its definition as a Con

nameOccursIn :: Data a => Name -> a -> Bool Source

Check if a name occurs anywhere within a TH tree.

allNamesIn :: Data a => a -> [Name] Source

Extract all Names mentioned in a TH tree.

flattenDValD :: Quasi q => DLetDec -> q [DLetDec] Source

If the declaration passed in is a DValD, creates new, equivalent declarations such that the DPat in all DValDs 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.

getRecordSelectors Source

Arguments

:: Quasi q 
=> DType

the type of the argument

-> DCon 
-> q [DLetDec] 

Produces DLetDecs representing the record selector functions from the provided DCon.

mkTypeName :: Quasi q => String -> q Name Source

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 Source

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 Source

Like newName, but even more unique (unique across different splices), and with unique nameBases.

mkTupleDExp :: [DExp] -> DExp Source

Make a tuple DExp from a list of DExps. Avoids using a 1-tuple.

mkTupleDPat :: [DPat] -> DPat Source

Make a tuple DPat from a list of DPats. Avoids using a 1-tuple.

maybeDLetE :: [DLetDec] -> DExp -> DExp Source

If decs is non-empty, delcare them in a let:

maybeDCaseE :: String -> DExp -> [DMatch] -> DExp Source

If matches is non-empty, make a case statement; otherwise make an error statement

Extracting bound names

extractBoundNamesStmt :: Stmt -> Set Name Source

Extract the names bound in a Stmt

extractBoundNamesDec :: Dec -> Set Name Source

Extract the names bound in a Dec that could appear in a let expression.

extractBoundNamesPat :: Pat -> Set Name Source

Extract the names bound in a Pat