Copyright | Eric Mertens 2017 |
---|---|
License | ISC |
Maintainer | emertens@gmail.com |
Safe Haskell | None |
Language | Haskell2010 |
This module provides a flattened view of information about data types and newtypes that can be supported uniformly across multiple verisons of the template-haskell package.
Sample output for reifyDatatype
''Maybe
DatatypeInfo
{datatypeContext
= [] ,datatypeName
= GHC.Base.Maybe ,datatypeVars
= [VarT
a_3530822107858468866 ] ,datatypeVariant
=Datatype
,datatypeCons
= [ConstructorInfo
{constructorName
= GHC.Base.Nothing ,constructorVars
= [] ,constructorContext
= [] ,constructorFields
= [] ,constructorVariant
=NormalConstructor
} ,ConstructorInfo
{constructorName
= GHC.Base.Just ,constructorVars
= [] ,constructorContext
= [] ,constructorFields
= [VarT
a_3530822107858468866 ] ,constructorVariant
=NormalConstructor
} ] }
Datatypes declared with GADT syntax are normalized to constructors with existentially quantified type variables and equality constraints.
- data DatatypeInfo = DatatypeInfo {}
- data ConstructorInfo = ConstructorInfo {}
- data DatatypeVariant
- data ConstructorVariant
- reifyDatatype :: Name -> Q DatatypeInfo
- normalizeInfo :: Info -> Q DatatypeInfo
- normalizeDec :: Dec -> Q DatatypeInfo
- normalizeCon :: Name -> [Name] -> Con -> Q [ConstructorInfo]
- class TypeSubstitution a where
- quantifyType :: Type -> Type
- freshenFreeVariables :: Type -> Q Type
- equalPred :: Type -> Type -> Pred
- classPred :: Name -> [Type] -> Pred
- resolveTypeSynonyms :: Type -> Q Type
- unifyTypes :: [Type] -> Q (Map Name Type)
- tvName :: TyVarBndr -> Name
- datatypeType :: DatatypeInfo -> Type
Types
data DatatypeInfo Source #
Normalized information about newtypes and data types.
DatatypeInfo | |
|
data ConstructorInfo Source #
Normalized information about constructors associated with newtypes and data types.
ConstructorInfo | |
|
data DatatypeVariant Source #
Possible variants of data type declarations.
Datatype | Type declared with |
Newtype | Type declared with |
DataInstance | Type declared with |
NewtypeInstance | Type declared with |
data ConstructorVariant Source #
Possible variants of data constructors.
NormalConstructor | Constructor without field names |
RecordConstructor [Name] | Constructor with field names |
Normalization functions
:: Name | type constructor |
-> Q DatatypeInfo |
Compute a normalized view of the metadata about a data type or newtype given a type constructor.
normalizeInfo :: Info -> Q DatatypeInfo Source #
Normalize Info
for a newtype or datatype into a DatatypeInfo
.
Fail in Q
otherwise.
normalizeDec :: Dec -> Q DatatypeInfo Source #
Normalize Dec
for a newtype or datatype into a DatatypeInfo
.
Fail in Q
otherwise.
:: Name | Type constructor |
-> [Name] | Type parameters |
-> Con | Constructor |
-> Q [ConstructorInfo] |
Normalize a Con
into a ConstructorInfo
. This requires knowledge of
the type and parameters of the constructor as extracted from the outer
Dec
.
Type variable manipulation
class TypeSubstitution a where Source #
Class for types that support type variable substitution.
applySubstitution :: Map Name Type -> a -> a Source #
Apply a type variable substitution
freeVariables :: a -> [Name] Source #
Compute the free type variables
quantifyType :: Type -> Type Source #
Add universal quantifier for all free variables in the type. This is
useful when constructing a type signature for a declaration.
This code is careful to ensure that the order of the variables quantified
is determined by their order of appearance in the type singnature. (In
contrast with being dependent upon the Ord instance for Name
)
freshenFreeVariables :: Type -> Q Type Source #
Substitute all of the free variables in a type with fresh ones
Pred
functions
equalPred :: Type -> Type -> Pred Source #
Construct an equality constraint. The implementation of Pred
varies
across versions of Template Haskell.
Construct a typeclass constraint. The implementation of Pred
varies
across versions of Template Haskell.
Convenience functions
unifyTypes :: [Type] -> Q (Map Name Type) Source #
Compute the type variable substitution that unifies a list of types,
or fail in Q
.
tvName :: TyVarBndr -> Name Source #
Extract the type variable name from a TyVarBndr
ignoring the
kind signature if one exists.
datatypeType :: DatatypeInfo -> Type Source #
Construct a Type using the datatype's type constructor and type parameteters.