th-abstraction
Nicer interface for reified information about data types
https://github.com/glguy/th-abstraction
| LTS Haskell 24.16: | 0.7.1.0@rev:1 |
| Stackage Nightly 2025-10-24: | 0.7.1.0@rev:1 |
| Latest on Hackage: | 0.7.1.0@rev:1 |
th-abstraction-0.7.1.0@sha256:3011403d85b0070630e33d66b9370535ccd1e0e672ae58f847550b2b15bb4fd8,2268Module documentation for 0.7.1.0
- Language
- Language.Haskell
- Language.Haskell.TH
- Language.Haskell
th-abstraction
This package provides a consistent interface to a subset of Template Haskell.
Currently the package provides a consistent view of the reified declaration information about datatypes, newtypes, and data family instances. These interfaces abstract away the differences in the normal and GADT syntax used to define these types.
Contact Information
Please contact me via GitHub or on the #haskell IRC channel on irc.libera.chat
Changes
Revision history for th-abstraction
0.7.1.0 – 2024.12.05
- Drop support for pre-8.0 versions of GHC.
0.7.0.0 – 2024.03.17
DatatypeInfonow has an additionaldatatypeReturnKindfield. Most of the time, this will beStarT, but this can also be more exotic kinds such asConT ''UnliftedTypeif dealing with primitive types,UnliftedDatatypes, orUnliftedNewtypes.reifyDatatypeand related functions now support primitive types such asInt#. These will be reified asDatatypeInfos with noConstructorInfos and withDatatypeas thedatatypeVariant.normalizeConnow takes aKindargument representing the return kind of the parent data type. (This is sometimes necessary to determine which type variables in the data constructor are universal or existential, depending on if the variables appear in the return kind.)- Fix a couple of bugs in which
normalizeDecwould return incorrect results for GADTs that useforalls in their return kind.
0.6.0.0 – 2023.07.31
-
Support building with
template-haskell-2.21.0.0(GHC 9.8). -
Adapt to
TyVarBndrs for type-level declarations changing their type fromTyVarBndr ()toTyVarBndr BndrVisintemplate-haskell:Language.Haskell.TH.Datatype.TyVarBndrnow backportstype BndrVis = (), as well asBndrReqandBndrInvispattern synonyms. These make it possible to write code involvingBndrVisthat is somewhat backwards compatible (but do see the caveats in the Haddocks forBndrInvis).Language.Haskell.TH.Datatype.TyVarBndralso backports the following definitions:- The
type TyVarBndrVis = TyVarBndr BndrVistype synonym. - The
DefaultBndrFlagclass, which can be used to write code that is polymorphic overTyVarBndrflags while still allowing the code to return a reasonable default value for the flag. - The
bndrReqandbndrInvisdefinitions, which behave identically toBndrReqandBndrInvis.
- The
Language.Haskell.TH.Datatype.TyVarBndrnow defines the following utility functions, which are not present intemplate-haskell:plainTVReq,plainTVInvis,kindedTVReq, andkindedTVInvisfunctions, which constructPlainTVs andKindedTVs with particularBndrVisflags.- An
elimTVFlag, which behaves likeelimTV, but where the continuation arguments also take aflagargument. (Note that the type of this function is slightly different on old versions oftemplate-haskell. See the Haddocks for more.) - A
tvFlagfunction, which extracts theflagfrom aTyVarBndr. (Note that the type of this function is slightly different on old versions oftemplate-haskell. See the Haddocks for more.)
- The types of the
dataDCompatandnewtypeDCompatfunctions have had their[TyVarBndrUnit]arguments changed to[TyVarBndrVis], matching similar changes toDataDandNewtypeDintemplate-haskell.
Because
BndrVisis a synonym for()on pre-9.8 versions of GHC, this change is unlikely to break any existing code, provided that you build it with GHC 9.6 or earlier. If you build with GHC 9.8 or later, on the other hand, it is likely that you will need to update your existing code. Here are some possible ways that your code might fail to compile with GHC 9.8, along with some migration strategies:-
Your code passes a
TyVarBndrUnitin a place where aTyVarBndrVisis now expected in GHC 9.8, such as in the arguments todataDCompat:import "template-haskell" Language.Haskell.TH import "th-abstraction" Language.Haskell.TH.Datatype (dataDCompat) dec :: DecQ dec = dataDCompat (pure []) d [PlainTV a ()] [] [] where d = mkName "d" a = mkName "a"With GHC 9.8, this will fail to compile with:
error: [GHC-83865] • Couldn't match expected type ‘BndrVis’ with actual type ‘()’ • In the second argument of ‘PlainTV’, namely ‘()’ In the expression: PlainTV a () In the third argument of ‘dataDCompat’, namely ‘[PlainTV a ()]’ | | dec = dataDCompat (pure []) d [PlainTV a ()] [] [] | ^^Some possible ways to migrate this code include:
-
Use the
bndrReqfunction orBndrReqpattern synonym in place of(), making sure to import them fromLanguage.Haskell.TH.Datatype.TyVarBndr:... import "th-abstraction" Language.Haskell.TH.Datatype.TyVarBndr dec :: DecQ dec = dataDCompat (pure []) d [PlainTV a bndrReq] [] [] -- Or, alternatively: {- dec = dataDCompat (pure []) d [PlainTV a BndrReq] [] [] -} where ... -
Use the
plainTVfunction fromLanguage.Haskell.TH.Datatype.TyVarBndr, which is now sufficiently polymorphic to work as both aTyVarBndrUnitand aTyVarBndrVis:... import Language.Haskell.TH.Datatype.TyVarBndr dec :: DecQ dec = dataDCompat (pure []) d [plainTV a] [] [] where ...
-
-
You may have to replace some uses of
TyVarBndrUnitwithTyVarBndrVisin your code. For instance, this will no longer typecheck in GHC 9.8 for similar reasons to the previous example:import "template-haskell" Language.Haskell.TH import "th-abstraction" Language.Haskell.TH.Datatype (dataDCompat) dec :: DecQ dec = dataDCompat (pure []) d tvbs [] [] where tvbs :: [TyVarBndrUnit] tvbs = [plainTV a] d = mkName "d" a = mkName "a"Here is a version that will typecheck with GHC 9.8 and earlier:
... import "th-abstraction" Language.Haskell.TH.Datatype.TyVarBndr dec :: DecQ dec = dataDCompat (pure []) d tvbs [] [] where tvbs :: [TyVarBndrVis] tvbs = [plainTV a] ... -
In some cases, the
TyVarBndrUnits might come from another place in the code, e.g.,import "template-haskell" Language.Haskell.TH import "th-abstraction" Language.Haskell.TH.Datatype (dataDCompat) dec :: [TyVarBndrUnit] -> DecQ dec tvbs = dataDCompat (pure []) d tvbs [] [] where d = mkName "d"If it is not straightforward to change
dec’s type to accept[TyVarBndrVis]as an argument, another viable option is to use thechangeTVFlagsfunction:... import "th-abstraction" Language.Haskell.TH.Datatype.TyVarBndr dec :: [TyVarBndrUnit] -> DecQ dec tvbs = dataDCompat (pure []) d tvbs' [] [] where tvbs' :: [TyVarBndrVis] tvbs' = changeTVFlags bndrReq tvbs ...
This guide, while not comprehensive, should cover most of the common cases one will encounter when migrating their
th-abstractioncode to support GHC 9.8.
0.5.0.0 – 2023.02.27
- Support the
TypeDatalanguage extension added in GHC 9.6. TheDatatypeVariantdata type now has a separateTypeDataconstructor to representtype datadeclarations. - Add a
Liftinstance forth-abstraction’s compatibility shim forSpecificitywhen building with pre-9.0 versions of GHC.
0.4.5.0 – 2022.09.12
- Fix a bug in which data family declarations with interesting return kinds
(e.g.,
data family F :: Type -> Type) would be reified incorrectly when usingreifyDatatype.
0.4.4.0 – 2022.07.23
- Support free variable substitution and infix resolution for
PromotedInfixTandPromotedUInfixTontemplate-haskell-2.19.0.0or later.
0.4.3.0 – 2021.08.30
- Make
applySubstitutionavoid capturing type variable binders when substituting intoforalls. - Fix a bug in which
resolveTypeSynonymswould incorrectly expand type synonyms that are not applied to enough arguments. - Allow the test suite to build with GHC 9.2.
0.4.2.0 – 2020-12-30
- Explicitly mark modules as Safe (or Trustworthy for GHC versions prior to 8.4).
0.4.1.0 – 2020-12-09
- Fix a bug in which
normalizeDecwould give incorrect kind annotations to type variables in quotedDecs.normalizeDecnow leaves the kinds of type variable binders alone.
0.4.0.0 – 2020-09-29
- Adapt to the
TyVarBndrdata type gaining a newflagtype parameter (intemplate-haskell-2.17.0.0) to represent its specificity:- Introduce a new
Language.Haskell.TH.Datatype.TyVarBndrmodule that definesTyVarBndr_, a backwards-compatible type synonym forTyVarBndr, as well as backportingTyVarBndrSpec,TyVarBndrUnit, andSpecificity. This module also defines other useful functions for constructing and manipulatingTyVarBndrs. - The types in
Language.Haskell.TH.Datatypenow useTyVarBndr_,TyVarBndrUnit, andTyVarBndrSpecwhere appropriate. Technically, this is not a breaking change, since all three are simple type synonyms aroundTyVarBndr, but it is likely that you will need to update yourth-abstraction-using code anyway if it involves aTyVarBndr-consuming function.
- Introduce a new
0.3.2.0 – 2020-02-06
- Support substituting into and extracting free variables from
ForallVisTs ontemplate-haskell-2.16.0.0(GHC 8.10) or later. - Fix a bug in which
freeVariablescould report duplicate kind variables when they occur in the kinds of the type variable binders in aForallT. - Fix a bug in which
resolveInfixTwould not resolveUInfixTs occurring in the kinds of type variable binders in aForallT. - Fix a bug in which the
TypeSubstitution ConstructorInfoinstance would not detect free kind variables in theconstructorVars.
0.3.1.0 – 2019-04-28
- Fix a bug which would cause data family information to be reified incorrectly with GHC 8.8+ in some situations.
0.3.0.0 – 2019-04-26
-
Breaking change: the
datatypeVarsfield ofDatatypeInfois now of type[TyVarBndr]instead of[Type], as it now refers to all of the bound type variables in the data type. The olddatatypeVarsfield has been renamed todatatypeInstTypesto better reflect its purpose.In addition, the type of
normalizeConnow has an additional[TyVarBndr]argument, sinceDatatypeInfonow requires it. -
Support
template-haskell-2.15. -
Fix a bug in which
normalizeDecwould not detect existential type variables in a GADT constructor if they were implicitly quantified. -
Fix a bug in which
normalizeDecwould report an incorrect number ofdatatypeVarsfor GADT declarations with explicit return kinds (such asdata Foo :: * -> * where).
0.2.11.0 – 2019-02-26
- Fix a bug in which
freeVariablesWellScopedwould sometimes not preserve the left-to-right ordering ofNames generated withnewName.
0.2.10.0 – 2018-12-20
- Optimization:
quantifyTypenow collapses consecutiveforalls. For instance, callingquantifyTypeonforall b. a -> b -> T anow producesforall a b. a -> b -> T ainstead offorall a. forall b. a -> b -> T a.
0.2.9.0 – 2018-12-20
- Fix a bug in which
resolveTypeSynonymswould not look intoForallTs,SigTs,InfixTs, orParensTs. - Fix a bug in which
quantifyTypewould not respect the dependency order of type variables (e.g.,Proxy (a :: k)would have erroneously been quantified asforall a k. Proxy (a :: k)). - Fix a bug in which
asEqualPredwould return incorrect results with GHC 8.7. - Add a
freeVariablesWellScopedfunction which computes the free variables of a list of types and sorts them according to dependency order. - Add a
resolveKindSynonymsfunction which expands all type synonyms in aKind. This is mostly useful for supporting old GHCs whereTypeandKindwere not the same.
0.2.8.0 – 2018-06-29
- GADT reification is now much more robust with respect to
PolyKinds:-
A bug in which universally quantified kind variables were mistakenly flagged as existential has been fixed.
-
A bug in which the kinds of existentially quantified type variables were not substituted properly has been fixed.
-
More kind equalities are detected than before. For example, in the following data type:
data T (a :: k) where MkT :: forall (a :: Bool). T aWe now catch the
k ~ Boolequality.
-
- Tweak
resolveTypeSynonymsso that failing to reify a type constructor name so longer results in an error. Among other benefits, this makes it possible to pass data types with GADT syntax tonormalizeDec.
0.2.7.0 – 2018-06-17
- Fix bug in which data family instances with duplicate occurrences of type variables in the left-hand side would have redundant equality constraints in their contexts.
0.2.6.0 – 2017-09-04
- Fix bug in which
applySubstitutionandfreeVariableswould ignore type variables in the kinds of type variable binders.
0.2.5.0
- Added
pragLineDCompat,newtypeDCompatandtySynInstDCompat
0.2.4.0 – 2017-07-31
- Fix bug that caused GADT equality constraints to be incorrect in some cases.
- Expose
UnpackednessandStrictness(which were unexported by accident).
0.2.3.0 – 2017-06-26
- Add
resolvePredSynonyms - Add
reifyConstructor, which allows reification ofConstructorInfofrom a constructor name, andlookupByConstructorName, which allows directly looking up aConstructorInfofrom aDatatypeInfovalue for a given constructorName. - Augment
reifyDatatypeto be able to look upDatatypeInfofrom theNameof a record selector for one of its constructors. Also addreifyRecordfor reification of ofConstructorInfofrom a record name, andlookupByRecordName, which allows directly looking up aConstructorInfofrom aDatatypeInfovalue for a given recordName. - Fix bug that caused
th-abstractionto fail on GHC 7.0 and 7.2 when passing a vanilla constructor name toreifyDatatype - Make
normalizeDecandnormalizeConmore robust with respect to data family instances on GHC 7.6 and 7.8
0.2.2.0 – 2017-06-10
- Fix
freeVariableson lists not not produce duplicates.
0.2.1.0 – 2017-06-09
- Add sensible reify defaults and error messages when we can’t backport fixes to old GHC Template Haskell output due to hand-written Decs being processed.
0.2.0.0 – 2017-06-03
- Added
reifyFixityCompat - Added
constructorStrictnessfield toConstructorInfo - Infer more kind signatures when missing on old GHCs
- Added parameter to
normalizeCon - Support GHC back to 7.0.4
0.1.3.0 – 2017-05-27
- Added
resolveInfixTwhich uses reified fixity information to resolveUInfixT - Added
asEqualPredandasClassPred - Fixed data-instance GADTs
0.1.2.1 – 2017-05-21
- Add eta reduction fixes to GHC 7.6
0.1.2.0 – 2017-05-21
- Added
arrowKCompat - Added workaround for GHC 7.8 data instance eta reduction bug
- Added kind signatures to datatypeVars
0.1.1.0 – 2017-05-20
- Better matching of constraints generated for GADTs across GHC versions
- Added
dataDCompat - Support for giving value constructors to reifyDatatype. This enables data families to be reified easily.
0.1.0.0 – 2017-04-26
- First version.