-- |
-- Language.Haskell.TH.Lib contains lots of useful helper functions for
-- generating and manipulating Template Haskell terms

-- Note: this module mostly re-exports functions from
-- Language.Haskell.TH.Lib.Internal, but if a change occurs to Template
-- Haskell which requires breaking the API offered in this module, we opt to
-- copy the old definition here, and make the changes in
-- Language.Haskell.TH.Lib.Internal. This way, we can retain backwards
-- compatibility while still allowing GHC to make changes as it needs.

module Language.Haskell.TH.Lib (
    -- All of the exports from this module should
    -- be "public" functions.  The main module TH
    -- re-exports them all.

    -- * Library functions
    -- ** Abbreviations
        InfoQ, ExpQ, TExpQ, DecQ, DecsQ, ConQ, TypeQ, KindQ, TyVarBndrQ,
        TyLitQ, CxtQ, PredQ, DerivClauseQ, MatchQ, ClauseQ, BodyQ, GuardQ,
        StmtQ, RangeQ, SourceStrictnessQ, SourceUnpackednessQ, BangQ,
        BangTypeQ, VarBangTypeQ, StrictTypeQ, VarStrictTypeQ, FieldExpQ, PatQ,
        FieldPatQ, RuleBndrQ, TySynEqnQ, PatSynDirQ, PatSynArgsQ,
        FamilyResultSigQ, DerivStrategyQ,

    -- ** Constructors lifted to 'Q'
    -- *** Literals
        intPrimL, wordPrimL, floatPrimL, doublePrimL, integerL, rationalL,
        charL, stringL, stringPrimL, charPrimL,
    -- *** Patterns
        litP, varP, tupP, unboxedTupP, unboxedSumP, conP, uInfixP, parensP,
        infixP, tildeP, bangP, asP, wildP, recP,
        listP, sigP, viewP,
        fieldPat,

    -- *** Pattern Guards
        normalB, guardedB, normalG, normalGE, patG, patGE, match, clause,

    -- *** Expressions
        dyn, varE, unboundVarE, labelE, implicitParamVarE, conE, litE, staticE,
        appE, appTypeE, uInfixE, parensE, infixE, infixApp, sectionL, sectionR,
        lamE, lam1E, lamCaseE, tupE, unboxedTupE, unboxedSumE, condE, multiIfE,
        letE, caseE, appsE, listE, sigE, recConE, recUpdE, stringE, fieldExp,
    -- **** Ranges
    fromE, fromThenE, fromToE, fromThenToE,

    -- ***** Ranges with more indirection
    arithSeqE,
    fromR, fromThenR, fromToR, fromThenToR,
    -- **** Statements
    doE, mdoE, compE,
    bindS, letS, noBindS, parS, recS,

    -- *** Types
        forallT, varT, conT, appT, appKindT, arrowT, infixT, uInfixT, parensT,
        equalityT, listT, tupleT, unboxedTupleT, unboxedSumT, sigT, litT,
        wildCardT, promotedT, promotedTupleT, promotedNilT, promotedConsT,
        implicitParamT,
    -- **** Type literals
    numTyLit, strTyLit,
    -- **** Strictness
    noSourceUnpackedness, sourceNoUnpack, sourceUnpack,
    noSourceStrictness, sourceLazy, sourceStrict,
    isStrict, notStrict, unpacked,
    bang, bangType, varBangType, strictType, varStrictType,
    -- **** Class Contexts
    cxt, classP, equalP,
    -- **** Constructors
    normalC, recC, infixC, forallC, gadtC, recGadtC,

    -- *** Kinds
    varK, conK, tupleK, arrowK, listK, appK, starK, constraintK,

    -- *** Type variable binders
    plainTV, kindedTV,

    -- *** Roles
    nominalR, representationalR, phantomR, inferR,

    -- *** Top Level Declarations
    -- **** Data
    valD, funD, tySynD, dataD, newtypeD,
    derivClause, DerivClause(..),
    stockStrategy, anyclassStrategy, newtypeStrategy,
    viaStrategy, DerivStrategy(..),
    -- **** Class
    classD, instanceD, instanceWithOverlapD, Overlap(..),
    sigD, standaloneDerivD, standaloneDerivWithStrategyD, defaultSigD,

    -- **** Role annotations
    roleAnnotD,
    -- **** Type Family / Data Family
    dataFamilyD, openTypeFamilyD, closedTypeFamilyD, dataInstD,
    newtypeInstD, tySynInstD,
    tySynEqn, injectivityAnn, noSig, kindSig, tyVarSig,

    -- **** Fixity
    infixLD, infixRD, infixND,

    -- **** Foreign Function Interface (FFI)
    cCall, stdCall, cApi, prim, javaScript,
    unsafe, safe, interruptible, forImpD,

    -- **** Functional dependencies
    funDep,

    -- **** Pragmas
    ruleVar, typedRuleVar,
    valueAnnotation, typeAnnotation, moduleAnnotation,
    pragInlD, pragSpecD, pragSpecInlD, pragSpecInstD, pragRuleD, pragAnnD,
    pragLineD, pragCompleteD,

    -- **** Pattern Synonyms
    patSynD, patSynSigD, unidir, implBidir, explBidir, prefixPatSyn,
    infixPatSyn, recordPatSyn,

    -- **** Implicit Parameters
    implicitParamBindD,

    -- ** Reify
    thisModule

   ) where

import Language.Haskell.TH.Lib.Internal hiding
  ( tySynD
  , dataD
  , newtypeD
  , classD
  , pragRuleD
  , dataInstD
  , newtypeInstD
  , dataFamilyD
  , openTypeFamilyD
  , closedTypeFamilyD
  , tySynEqn
  , forallC

  , forallT
  , sigT

  , plainTV
  , kindedTV
  , starK
  , constraintK

  , noSig
  , kindSig
  , tyVarSig

  , derivClause
  , standaloneDerivWithStrategyD

  , Role
  , InjectivityAnn
  )
import Language.Haskell.TH.Syntax

import Control.Monad (liftM2)
import Prelude

-- All definitions below represent the "old" API, since their definitions are
-- different in Language.Haskell.TH.Lib.Internal. Please think carefully before
-- deciding to change the APIs of the functions below, as they represent the
-- public API (as opposed to the Internal module, which has no API promises.)

-------------------------------------------------------------------------------
-- *   Dec

tySynD :: Name -> [TyVarBndr] -> TypeQ -> DecQ
tySynD :: Name -> [TyVarBndr] -> TypeQ -> DecQ
tySynD tc :: Name
tc tvs :: [TyVarBndr]
tvs rhs :: TypeQ
rhs = do { Type
rhs1 <- TypeQ
rhs; Dec -> DecQ
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> [TyVarBndr] -> Type -> Dec
TySynD Name
tc [TyVarBndr]
tvs Type
rhs1) }

dataD :: CxtQ -> Name -> [TyVarBndr] -> Maybe Kind -> [ConQ] -> [DerivClauseQ]
      -> DecQ
dataD :: CxtQ
-> Name
-> [TyVarBndr]
-> Maybe Type
-> [ConQ]
-> [DerivClauseQ]
-> DecQ
dataD ctxt :: CxtQ
ctxt tc :: Name
tc tvs :: [TyVarBndr]
tvs ksig :: Maybe Type
ksig cons :: [ConQ]
cons derivs :: [DerivClauseQ]
derivs =
  do
    Cxt
ctxt1 <- CxtQ
ctxt
    [Con]
cons1 <- [ConQ] -> Q [Con]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence [ConQ]
cons
    [DerivClause]
derivs1 <- [DerivClauseQ] -> Q [DerivClause]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence [DerivClauseQ]
derivs
    Dec -> DecQ
forall (m :: * -> *) a. Monad m => a -> m a
return (Cxt
-> Name
-> [TyVarBndr]
-> Maybe Type
-> [Con]
-> [DerivClause]
-> Dec
DataD Cxt
ctxt1 Name
tc [TyVarBndr]
tvs Maybe Type
ksig [Con]
cons1 [DerivClause]
derivs1)

newtypeD :: CxtQ -> Name -> [TyVarBndr] -> Maybe Kind -> ConQ -> [DerivClauseQ]
         -> DecQ
newtypeD :: CxtQ
-> Name
-> [TyVarBndr]
-> Maybe Type
-> ConQ
-> [DerivClauseQ]
-> DecQ
newtypeD ctxt :: CxtQ
ctxt tc :: Name
tc tvs :: [TyVarBndr]
tvs ksig :: Maybe Type
ksig con :: ConQ
con derivs :: [DerivClauseQ]
derivs =
  do
    Cxt
ctxt1 <- CxtQ
ctxt
    Con
con1 <- ConQ
con
    [DerivClause]
derivs1 <- [DerivClauseQ] -> Q [DerivClause]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence [DerivClauseQ]
derivs
    Dec -> DecQ
forall (m :: * -> *) a. Monad m => a -> m a
return (Cxt
-> Name -> [TyVarBndr] -> Maybe Type -> Con -> [DerivClause] -> Dec
NewtypeD Cxt
ctxt1 Name
tc [TyVarBndr]
tvs Maybe Type
ksig Con
con1 [DerivClause]
derivs1)

classD :: CxtQ -> Name -> [TyVarBndr] -> [FunDep] -> [DecQ] -> DecQ
classD :: CxtQ -> Name -> [TyVarBndr] -> [FunDep] -> [DecQ] -> DecQ
classD ctxt :: CxtQ
ctxt cls :: Name
cls tvs :: [TyVarBndr]
tvs fds :: [FunDep]
fds decs :: [DecQ]
decs =
  do
    [Dec]
decs1 <- [DecQ] -> Q [Dec]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence [DecQ]
decs
    Cxt
ctxt1 <- CxtQ
ctxt
    Dec -> DecQ
forall (m :: * -> *) a. Monad m => a -> m a
return (Dec -> DecQ) -> Dec -> DecQ
forall a b. (a -> b) -> a -> b
$ Cxt -> Name -> [TyVarBndr] -> [FunDep] -> [Dec] -> Dec
ClassD Cxt
ctxt1 Name
cls [TyVarBndr]
tvs [FunDep]
fds [Dec]
decs1

pragRuleD :: String -> [RuleBndrQ] -> ExpQ -> ExpQ -> Phases -> DecQ
pragRuleD :: String -> [RuleBndrQ] -> ExpQ -> ExpQ -> Phases -> DecQ
pragRuleD n :: String
n bndrs :: [RuleBndrQ]
bndrs lhs :: ExpQ
lhs rhs :: ExpQ
rhs phases :: Phases
phases
  = do
      [RuleBndr]
bndrs1 <- [RuleBndrQ] -> Q [RuleBndr]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence [RuleBndrQ]
bndrs
      Exp
lhs1   <- ExpQ
lhs
      Exp
rhs1   <- ExpQ
rhs
      Dec -> DecQ
forall (m :: * -> *) a. Monad m => a -> m a
return (Dec -> DecQ) -> Dec -> DecQ
forall a b. (a -> b) -> a -> b
$ Pragma -> Dec
PragmaD (Pragma -> Dec) -> Pragma -> Dec
forall a b. (a -> b) -> a -> b
$ String
-> Maybe [TyVarBndr]
-> [RuleBndr]
-> Exp
-> Exp
-> Phases
-> Pragma
RuleP String
n Maybe [TyVarBndr]
forall a. Maybe a
Nothing [RuleBndr]
bndrs1 Exp
lhs1 Exp
rhs1 Phases
phases

dataInstD :: CxtQ -> Name -> [TypeQ] -> Maybe Kind -> [ConQ] -> [DerivClauseQ]
          -> DecQ
dataInstD :: CxtQ
-> Name
-> [TypeQ]
-> Maybe Type
-> [ConQ]
-> [DerivClauseQ]
-> DecQ
dataInstD ctxt :: CxtQ
ctxt tc :: Name
tc tys :: [TypeQ]
tys ksig :: Maybe Type
ksig cons :: [ConQ]
cons derivs :: [DerivClauseQ]
derivs =
  do
    Cxt
ctxt1 <- CxtQ
ctxt
    Type
ty1 <- (TypeQ -> TypeQ -> TypeQ) -> TypeQ -> [TypeQ] -> TypeQ
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl TypeQ -> TypeQ -> TypeQ
appT (Name -> TypeQ
conT Name
tc) [TypeQ]
tys
    [Con]
cons1 <- [ConQ] -> Q [Con]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence [ConQ]
cons
    [DerivClause]
derivs1 <- [DerivClauseQ] -> Q [DerivClause]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence [DerivClauseQ]
derivs
    Dec -> DecQ
forall (m :: * -> *) a. Monad m => a -> m a
return (Cxt
-> Maybe [TyVarBndr]
-> Type
-> Maybe Type
-> [Con]
-> [DerivClause]
-> Dec
DataInstD Cxt
ctxt1 Maybe [TyVarBndr]
forall a. Maybe a
Nothing Type
ty1 Maybe Type
ksig [Con]
cons1 [DerivClause]
derivs1)

newtypeInstD :: CxtQ -> Name -> [TypeQ] -> Maybe Kind -> ConQ -> [DerivClauseQ]
             -> DecQ
newtypeInstD :: CxtQ
-> Name -> [TypeQ] -> Maybe Type -> ConQ -> [DerivClauseQ] -> DecQ
newtypeInstD ctxt :: CxtQ
ctxt tc :: Name
tc tys :: [TypeQ]
tys ksig :: Maybe Type
ksig con :: ConQ
con derivs :: [DerivClauseQ]
derivs =
  do
    Cxt
ctxt1 <- CxtQ
ctxt
    Type
ty1 <- (TypeQ -> TypeQ -> TypeQ) -> TypeQ -> [TypeQ] -> TypeQ
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl TypeQ -> TypeQ -> TypeQ
appT (Name -> TypeQ
conT Name
tc) [TypeQ]
tys
    Con
con1  <- ConQ
con
    [DerivClause]
derivs1 <- [DerivClauseQ] -> Q [DerivClause]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence [DerivClauseQ]
derivs
    Dec -> DecQ
forall (m :: * -> *) a. Monad m => a -> m a
return (Cxt
-> Maybe [TyVarBndr]
-> Type
-> Maybe Type
-> Con
-> [DerivClause]
-> Dec
NewtypeInstD Cxt
ctxt1 Maybe [TyVarBndr]
forall a. Maybe a
Nothing Type
ty1 Maybe Type
ksig Con
con1 [DerivClause]
derivs1)

dataFamilyD :: Name -> [TyVarBndr] -> Maybe Kind -> DecQ
dataFamilyD :: Name -> [TyVarBndr] -> Maybe Type -> DecQ
dataFamilyD tc :: Name
tc tvs :: [TyVarBndr]
tvs kind :: Maybe Type
kind
    = Dec -> DecQ
forall (m :: * -> *) a. Monad m => a -> m a
return (Dec -> DecQ) -> Dec -> DecQ
forall a b. (a -> b) -> a -> b
$ Name -> [TyVarBndr] -> Maybe Type -> Dec
DataFamilyD Name
tc [TyVarBndr]
tvs Maybe Type
kind

openTypeFamilyD :: Name -> [TyVarBndr] -> FamilyResultSig
                -> Maybe InjectivityAnn -> DecQ
openTypeFamilyD :: Name
-> [TyVarBndr] -> FamilyResultSig -> Maybe InjectivityAnn -> DecQ
openTypeFamilyD tc :: Name
tc tvs :: [TyVarBndr]
tvs res :: FamilyResultSig
res inj :: Maybe InjectivityAnn
inj
    = Dec -> DecQ
forall (m :: * -> *) a. Monad m => a -> m a
return (Dec -> DecQ) -> Dec -> DecQ
forall a b. (a -> b) -> a -> b
$ TypeFamilyHead -> Dec
OpenTypeFamilyD (Name
-> [TyVarBndr]
-> FamilyResultSig
-> Maybe InjectivityAnn
-> TypeFamilyHead
TypeFamilyHead Name
tc [TyVarBndr]
tvs FamilyResultSig
res Maybe InjectivityAnn
inj)

closedTypeFamilyD :: Name -> [TyVarBndr] -> FamilyResultSig
                  -> Maybe InjectivityAnn -> [TySynEqnQ] -> DecQ
closedTypeFamilyD :: Name
-> [TyVarBndr]
-> FamilyResultSig
-> Maybe InjectivityAnn
-> [TySynEqnQ]
-> DecQ
closedTypeFamilyD tc :: Name
tc tvs :: [TyVarBndr]
tvs result :: FamilyResultSig
result injectivity :: Maybe InjectivityAnn
injectivity eqns :: [TySynEqnQ]
eqns =
  do [TySynEqn]
eqns1 <- [TySynEqnQ] -> Q [TySynEqn]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence [TySynEqnQ]
eqns
     Dec -> DecQ
forall (m :: * -> *) a. Monad m => a -> m a
return (TypeFamilyHead -> [TySynEqn] -> Dec
ClosedTypeFamilyD (Name
-> [TyVarBndr]
-> FamilyResultSig
-> Maybe InjectivityAnn
-> TypeFamilyHead
TypeFamilyHead Name
tc [TyVarBndr]
tvs FamilyResultSig
result Maybe InjectivityAnn
injectivity) [TySynEqn]
eqns1)

tySynEqn :: (Maybe [TyVarBndr]) -> TypeQ -> TypeQ -> TySynEqnQ
tySynEqn :: Maybe [TyVarBndr] -> TypeQ -> TypeQ -> TySynEqnQ
tySynEqn tvs :: Maybe [TyVarBndr]
tvs lhs :: TypeQ
lhs rhs :: TypeQ
rhs =
  do
    Type
lhs1 <- TypeQ
lhs
    Type
rhs1 <- TypeQ
rhs
    TySynEqn -> TySynEqnQ
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe [TyVarBndr] -> Type -> Type -> TySynEqn
TySynEqn Maybe [TyVarBndr]
tvs Type
lhs1 Type
rhs1)

forallC :: [TyVarBndr] -> CxtQ -> ConQ -> ConQ
forallC :: [TyVarBndr] -> CxtQ -> ConQ -> ConQ
forallC ns :: [TyVarBndr]
ns ctxt :: CxtQ
ctxt con :: ConQ
con = (Cxt -> Con -> Con) -> CxtQ -> ConQ -> ConQ
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 ([TyVarBndr] -> Cxt -> Con -> Con
ForallC [TyVarBndr]
ns) CxtQ
ctxt ConQ
con

-------------------------------------------------------------------------------
-- *   Type

forallT :: [TyVarBndr] -> CxtQ -> TypeQ -> TypeQ
forallT :: [TyVarBndr] -> CxtQ -> TypeQ -> TypeQ
forallT tvars :: [TyVarBndr]
tvars ctxt :: CxtQ
ctxt ty :: TypeQ
ty = do
    Cxt
ctxt1 <- CxtQ
ctxt
    Type
ty1   <- TypeQ
ty
    Type -> TypeQ
forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> TypeQ) -> Type -> TypeQ
forall a b. (a -> b) -> a -> b
$ [TyVarBndr] -> Cxt -> Type -> Type
ForallT [TyVarBndr]
tvars Cxt
ctxt1 Type
ty1

sigT :: TypeQ -> Kind -> TypeQ
sigT :: TypeQ -> Type -> TypeQ
sigT t :: TypeQ
t k :: Type
k
  = do
      Type
t' <- TypeQ
t
      Type -> TypeQ
forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> TypeQ) -> Type -> TypeQ
forall a b. (a -> b) -> a -> b
$ Type -> Type -> Type
SigT Type
t' Type
k

-------------------------------------------------------------------------------
-- *   Kind

plainTV :: Name -> TyVarBndr
plainTV :: Name -> TyVarBndr
plainTV = Name -> TyVarBndr
PlainTV

kindedTV :: Name -> Kind -> TyVarBndr
kindedTV :: Name -> Type -> TyVarBndr
kindedTV = Name -> Type -> TyVarBndr
KindedTV

starK :: Kind
starK :: Type
starK = Type
StarT

constraintK :: Kind
constraintK :: Type
constraintK = Type
ConstraintT

-------------------------------------------------------------------------------
-- *   Type family result

noSig :: FamilyResultSig
noSig :: FamilyResultSig
noSig = FamilyResultSig
NoSig

kindSig :: Kind -> FamilyResultSig
kindSig :: Type -> FamilyResultSig
kindSig = Type -> FamilyResultSig
KindSig

tyVarSig :: TyVarBndr -> FamilyResultSig
tyVarSig :: TyVarBndr -> FamilyResultSig
tyVarSig = TyVarBndr -> FamilyResultSig
TyVarSig

-------------------------------------------------------------------------------
-- * Top Level Declarations

derivClause :: Maybe DerivStrategy -> [PredQ] -> DerivClauseQ
derivClause :: Maybe DerivStrategy -> [TypeQ] -> DerivClauseQ
derivClause mds :: Maybe DerivStrategy
mds p :: [TypeQ]
p = do
  Cxt
p' <- [TypeQ] -> CxtQ
cxt [TypeQ]
p
  DerivClause -> DerivClauseQ
forall (m :: * -> *) a. Monad m => a -> m a
return (DerivClause -> DerivClauseQ) -> DerivClause -> DerivClauseQ
forall a b. (a -> b) -> a -> b
$ Maybe DerivStrategy -> Cxt -> DerivClause
DerivClause Maybe DerivStrategy
mds Cxt
p'

standaloneDerivWithStrategyD :: Maybe DerivStrategy -> CxtQ -> TypeQ -> DecQ
standaloneDerivWithStrategyD :: Maybe DerivStrategy -> CxtQ -> TypeQ -> DecQ
standaloneDerivWithStrategyD mds :: Maybe DerivStrategy
mds ctxt :: CxtQ
ctxt ty :: TypeQ
ty = do
  Cxt
ctxt' <- CxtQ
ctxt
  Type
ty'   <- TypeQ
ty
  Dec -> DecQ
forall (m :: * -> *) a. Monad m => a -> m a
return (Dec -> DecQ) -> Dec -> DecQ
forall a b. (a -> b) -> a -> b
$ Maybe DerivStrategy -> Cxt -> Type -> Dec
StandaloneDerivD Maybe DerivStrategy
mds Cxt
ctxt' Type
ty'