Copyright | (c) 2010-2011 Patrick Bahr |
---|---|

License | BSD3 |

Maintainer | Patrick Bahr <paba@diku.dk> |

Stability | experimental |

Portability | non-portable (GHC Extensions) |

Safe Haskell | None |

Language | Haskell98 |

This module defines some utility functions for deriving instances for functor based type classes.

## Synopsis

- data DataInfo = DataInfo Cxt Name [TyVarBndr] [Con] [DerivClause]
- abstractNewtypeQ :: Q Info -> Q (Maybe DataInfo)
- abstractNewtype :: Info -> Maybe DataInfo
- normalCon :: Con -> (Name, [StrictType], Maybe Type)
- normalCon' :: Con -> (Name, [Type], Maybe Type)
- normalConExp :: Con -> Q (Name, [Type], Maybe Type)
- normalConStrExp :: Con -> Q (Name, [StrictType], Maybe Type)
- getBinaryFArg :: Type -> Maybe Type -> Type
- getUnaryFArg :: Type -> Maybe Type -> Type
- abstractConType :: Con -> (Name, Int)
- tyVarBndrName :: TyVarBndr -> Name
- containsType :: Type -> Type -> Bool
- containsType' :: Type -> Type -> [Int]
- newNames :: Int -> String -> Q [Name]
- tupleTypes :: Int -> Int -> [Name]
- derive :: [Name -> Q [Dec]] -> [Name] -> Q [Dec]
- mkClassP :: Name -> [Type] -> Type
- isEqualP :: Type -> Maybe (Type, Type)
- mkInstanceD :: Cxt -> Type -> [Dec] -> Dec
- liftSumGen :: Name -> Name -> Name -> Q [Dec]
- findSig :: [Name] -> [Dec] -> Q (Maybe ([Name], [Name]))

# Documentation

abstractNewtypeQ :: Q Info -> Q (Maybe DataInfo) Source #

This is the `Q`

-lifted version of 'abstractNewtype.

abstractNewtype :: Info -> Maybe DataInfo Source #

This function abstracts away `newtype`

declaration, it turns them into
`data`

declarations.

normalCon :: Con -> (Name, [StrictType], Maybe Type) Source #

This function provides the name and the arity of the given data constructor, and if it is a GADT also its type.

normalConExp :: Con -> Q (Name, [Type], Maybe Type) Source #

Same as normalCon' but expands type synonyms.

normalConStrExp :: Con -> Q (Name, [StrictType], Maybe Type) Source #

Same as normalConExp' but retains strictness annotations.

getBinaryFArg :: Type -> Maybe Type -> Type Source #

Auxiliary function to extract the first argument of a binary type
application (the second argument of this function). If the second
argument is `Nothing`

or not of the right shape, the first argument
is returned as a default.

getUnaryFArg :: Type -> Maybe Type -> Type Source #

Auxiliary function to extract the first argument of a type
application (the second argument of this function). If the second
argument is `Nothing`

or not of the right shape, the first argument
is returned as a default.

abstractConType :: Con -> (Name, Int) Source #

This function provides the name and the arity of the given data constructor.

tyVarBndrName :: TyVarBndr -> Name Source #

This function returns the name of a bound type variable

newNames :: Int -> String -> Q [Name] Source #

This function provides a list (of the given length) of new names based on the given string.

derive :: [Name -> Q [Dec]] -> [Name] -> Q [Dec] Source #

Helper function for generating a list of instances for a list of named
signatures. For example, in order to derive instances `Functor`

and
`ShowF`

for a signature `Exp`

, use derive as follows (requires Template
Haskell):

$(derive [makeFunctor, makeShowF] [''Exp])

mkClassP :: Name -> [Type] -> Type Source #

Apply a class name to type arguments to construct a type class constraint.

isEqualP :: Type -> Maybe (Type, Type) Source #

This function checks whether the given type constraint is an equality constraint. If so, the types of the equality constraint are returned.

liftSumGen :: Name -> Name -> Name -> Q [Dec] Source #

This function lifts type class instances over sums
ofsignatures. To this end it assumes that it contains only methods
with types of the form `f t1 .. tn -> t`

where `f`

is the signature
that is used to construct sums. Since this function is generic it
assumes as its first argument the name of the function that is
used to lift methods over sums i.e. a function of type

(f t1 .. tn -> t) -> (g t1 .. tn -> t) -> ((f :+: g) t1 .. tn -> t)

where `:+:`

is the sum type constructor. The second argument to
this function is expected to be the name of that constructor. The
last argument is the name of the class whose instances should be
lifted over sums.