---------------------------------------------------------------------------------- |-- Module : Data.Comp.Derive.Utils-- Copyright : (c) 2010-2011 Patrick Bahr-- License : BSD3-- Maintainer : Patrick Bahr <paba@diku.dk>-- Stability : experimental-- Portability : non-portable (GHC Extensions)---- This module defines some utility functions for deriving instances-- for functor based type classes.----------------------------------------------------------------------------------moduleData.Comp.Derive.UtilswhereimportLanguage.Haskell.THimportLanguage.Haskell.TH.SyntaximportControl.MonadimportLanguage.Haskell.TH.ExpandSyns{-|
This is the @Q@-lifted version of 'abstractNewtypeQ.
-}abstractNewtypeQ::QInfo->QInfoabstractNewtypeQ=liftMabstractNewtype{-|
This function abstracts away @newtype@ declaration, it turns them into
@data@ declarations.
-}abstractNewtype::Info->InfoabstractNewtype(TyConI(NewtypeDcxtnameargsconstrderive))=TyConI(DataDcxtnameargs[constr]derive)abstractNewtypeowise=owise{-|
This function provides the name and the arity of the given data constructor.
-}normalCon::Con->(Name,[StrictType])normalCon(NormalCconstrargs)=(constr,args)normalCon(RecCconstrargs)=(constr,map(\(_,s,t)->(s,t))args)normalCon(InfixCaconstrb)=(constr,[a,b])normalCon(ForallC__constr)=normalConconstrnormalCon'::Con->(Name,[Type])normalCon'=fmap(mapsnd).normalCon-- | Same as normalCon' but expands type synonyms.normalConExp::Con->Q(Name,[Type])normalConExpc=dolet(n,ts)=normalCon'cts'<-mapMexpandSynstsreturn(n,ts'){-|
This function provides the name and the arity of the given data constructor.
-}abstractConType::Con->(Name,Int)abstractConType(NormalCconstrargs)=(constr,lengthargs)abstractConType(RecCconstrargs)=(constr,lengthargs)abstractConType(InfixC_constr_)=(constr,2)abstractConType(ForallC__constr)=abstractConTypeconstr{-|
This function returns the name of a bound type variable
-}tyVarBndrName(PlainTVn)=ntyVarBndrName(KindedTVn_)=ncontainsType::Type->Type->BoolcontainsTypest|s==t=True|otherwise=casesofForallT__s'->containsTypes'tAppTs1s2->containsTypes1t||containsTypes2tSigTs'_->containsTypes't_->FalsecontainsType'::Type->Type->[Int]containsType'=run0whererunnst|s==t=[n]|otherwise=casesofForallT__s'->runns't-- only going through the right-hand side counts!AppTs1s2->runns1t++run(n+1)s2tSigTs'_->runns't_->[]{-|
This function provides a list (of the given length) of new names based
on the given string.
-}newNames::Int->String->Q[Name]newNamesnname=replicateMn(newNamename)tupleTypesnm=maptupleTypeName[n..m]{-| 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])
-}derive::[Name->Q[Dec]]->[Name]->Q[Dec]derivedersnames=liftMconcat$sequence[dername|der<-ders,name<-names]