{-# LANGUAGE CPP, NoMonomorphismRestriction, ScopedTypeVariables #-}
module Language.Haskell.TH.Desugar.Expand (
expand, expandType,
expandUnsoundly
) where
import qualified Data.Map as M
import Control.Monad
#if __GLASGOW_HASKELL__ < 709
import Control.Applicative
#endif
import Language.Haskell.TH hiding (cxt)
import Language.Haskell.TH.Syntax ( Quasi(..) )
import Data.Data
import Data.Generics
import qualified Data.Traversable as T
import Language.Haskell.TH.Desugar.AST
import Language.Haskell.TH.Desugar.Core
import Language.Haskell.TH.Desugar.Util
import Language.Haskell.TH.Desugar.Sweeten
import Language.Haskell.TH.Desugar.Reify
import Language.Haskell.TH.Desugar.Subst
expandType :: DsMonad q => DType -> q DType
expandType = expand_type NoIgnore
expand_type :: forall q. DsMonad q => IgnoreKinds -> DType -> q DType
expand_type ign = go []
where
go :: [DType] -> DType -> q DType
go [] (DForallT tvbs cxt ty) =
DForallT tvbs <$> mapM (expand_pred ign) cxt <*> expand_type ign ty
go _ (DForallT {}) =
impossible "A forall type is applied to another type."
go args (DAppT t1 t2) = do
t2' <- expand_type ign t2
go (t2' : args) t1
go args (DSigT ty ki) = do
ty' <- go [] ty
ki' <- go [] ki
finish (DSigT ty' ki') args
go args (DConT n) = expand_con ign n args
go args ty@(DVarT _) = finish ty args
go args ty@DArrowT = finish ty args
go args ty@(DLitT _) = finish ty args
go args ty@DWildCardT = finish ty args
finish :: DType -> [DType] -> q DType
finish ty args = return $ applyDType ty args
expand_pred :: forall q. DsMonad q => IgnoreKinds -> DPred -> q DPred
expand_pred ign = go []
where
go :: [DType] -> DPred -> q DPred
go [] (DForallPr tvbs cxt p) =
DForallPr tvbs <$> mapM (go []) cxt <*> expand_pred ign p
go _ (DForallPr {}) =
impossible "A quantified constraint is applied to another constraint."
go args (DAppPr p t) = do
t' <- expand_type ign t
go (t' : args) p
go args (DSigPr p k) = do
p' <- go [] p
k' <- expand_type ign k
finish (DSigPr p' k') args
go args (DConPr n) = do
ty <- expand_con ign n args
dTypeToDPred ty
go args p@(DVarPr _) = finish p args
go args p@DWildCardPr = finish p args
finish :: DPred -> [DType] -> q DPred
finish p args = return $ foldl DAppPr p args
expand_con :: forall q.
DsMonad q
=> IgnoreKinds
-> Name
-> [DType]
-> q DType
expand_con ign n args = do
info <- reifyWithLocals n
case info of
TyConI (TySynD _ _ StarT)
-> return $ applyDType (DConT typeKindName) args
_ -> go info
where
go :: Info -> q DType
go info = do
dinfo <- dsInfo info
args_ok <- allM no_tyvars_tyfams args
case dinfo of
DTyConI (DTySynD _n tvbs rhs) _
| length args >= length tvbs
-> do
let (syn_args, rest_args) = splitAtList tvbs args
ty <- substTy (M.fromList $ zip (map extractDTvbName tvbs) syn_args) rhs
ty' <- expand_type ign ty
return $ applyDType ty' rest_args
DTyConI (DOpenTypeFamilyD (DTypeFamilyHead _n tvbs _frs _ann)) _
| length args >= length tvbs
#if __GLASGOW_HASKELL__ < 709
, args_ok
#endif
-> do
let (syn_args, rest_args) = splitAtList tvbs args
insts <- qRecover (return []) $
qReifyInstances n (map typeToTH syn_args)
dinsts <- dsDecs insts
case dinsts of
[DTySynInstD _n (DTySynEqn lhs rhs)]
| Just subst <-
unionMaybeSubsts $ zipWith (matchTy ign) lhs syn_args
-> do ty <- substTy subst rhs
ty' <- expand_type ign ty
return $ applyDType ty' rest_args
_ -> give_up
DTyConI (DClosedTypeFamilyD (DTypeFamilyHead _n tvbs _frs _ann) eqns) _
| length args >= length tvbs
, args_ok
-> do
let (syn_args, rest_args) = splitAtList tvbs args
rhss <- mapMaybeM (check_eqn syn_args) eqns
case rhss of
(rhs : _) -> do
rhs' <- expand_type ign rhs
return $ applyDType rhs' rest_args
[] -> give_up
where
check_eqn :: [DType] -> DTySynEqn -> q (Maybe DType)
check_eqn arg_tys (DTySynEqn lhs rhs) = do
let m_subst = unionMaybeSubsts $ zipWith (matchTy ign) lhs arg_tys
T.mapM (flip substTy rhs) m_subst
_ -> give_up
give_up :: q DType
give_up = return $ applyDType (DConT n) args
no_tyvars_tyfams :: Data a => a -> q Bool
no_tyvars_tyfams = everything (liftM2 (&&)) (mkQ (return True) no_tyvar_tyfam)
no_tyvar_tyfam :: DType -> q Bool
no_tyvar_tyfam (DVarT _) = return False
no_tyvar_tyfam (DConT con_name) = do
m_info <- dsReify con_name
return $ case m_info of
Nothing -> False
Just (DTyConI (DOpenTypeFamilyD {}) _) -> False
Just (DTyConI (DDataFamilyD {}) _) -> False
Just (DTyConI (DClosedTypeFamilyD {}) _) -> False
_ -> True
no_tyvar_tyfam t = gmapQl (liftM2 (&&)) (return True) no_tyvars_tyfams t
allM :: Monad m => (a -> m Bool) -> [a] -> m Bool
allM f = foldM (\b x -> (b &&) `liftM` f x) True
extractDTvbName :: DTyVarBndr -> Name
extractDTvbName (DPlainTV n) = n
extractDTvbName (DKindedTV n _) = n
expand :: (DsMonad q, Data a) => a -> q a
expand = expand_ NoIgnore
expandUnsoundly :: (DsMonad q, Data a) => a -> q a
expandUnsoundly = expand_ YesIgnore
expand_ :: (DsMonad q, Data a) => IgnoreKinds -> a -> q a
expand_ ign = everywhereM (mkM (expand_type ign) >=> mkM (expand_pred ign))