module VectUtils (
collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg,
collectAnnValBinders,
+ mkDataConTag,
splitClosureTy,
+ mkPReprType, mkPReprAlts,
mkPADictType, mkPArrayType,
+ parrayReprTyCon, parrayReprDataCon, mkVScrut,
paDictArgType, paDictOfType, paDFunType,
paMethod, lengthPA, replicatePA, emptyPA, liftPA,
polyAbstract, polyApply, polyVApply,
- lookupPArrayFamInst,
hoistBinding, hoistExpr, hoistPolyVExpr, takeHoisted,
buildClosure, buildClosures,
mkClosureApp
import Type
import TypeRep
import TyCon
-import DataCon ( dataConWrapId )
+import DataCon ( DataCon, dataConWrapId, dataConTag )
import Var
import Id ( mkWildId )
import MkId ( unwrapFamInstScrut )
+import Name ( Name )
import PrelNames
import TysWiredIn
import BasicTypes ( Boxity(..) )
isAnnTypeArg (_, AnnType t) = True
isAnnTypeArg _ = False
-isClosureTyCon :: TyCon -> Bool
-isClosureTyCon tc = tyConName tc == closureTyConName
+mkDataConTag :: DataCon -> CoreExpr
+mkDataConTag dc = mkConApp intDataCon [mkIntLitInt $ dataConTag dc]
-splitClosureTy :: Type -> (Type, Type)
-splitClosureTy ty
- | Just (tc, [arg_ty, res_ty]) <- splitTyConApp_maybe ty
- , isClosureTyCon tc
- = (arg_ty, res_ty)
+splitUnTy :: String -> Name -> Type -> Type
+splitUnTy s name ty
+ | Just (tc, [ty']) <- splitTyConApp_maybe ty
+ , tyConName tc == name
+ = ty'
+
+ | otherwise = pprPanic s (ppr ty)
+
+splitBinTy :: String -> Name -> Type -> (Type, Type)
+splitBinTy s name ty
+ | Just (tc, [ty1, ty2]) <- splitTyConApp_maybe ty
+ , tyConName tc == name
+ = (ty1, ty2)
+
+ | otherwise = pprPanic s (ppr ty)
- | otherwise = pprPanic "splitClosureTy" (ppr ty)
+splitCrossTy :: Type -> (Type, Type)
+splitCrossTy = splitBinTy "splitCrossTy" ndpCrossTyConName
-isPArrayTyCon :: TyCon -> Bool
-isPArrayTyCon tc = tyConName tc == parrayTyConName
+splitPlusTy :: Type -> (Type, Type)
+splitPlusTy = splitBinTy "splitSumTy" ndpPlusTyConName
+
+splitEmbedTy :: Type -> Type
+splitEmbedTy = splitUnTy "splitEmbedTy" embedTyConName
+
+splitClosureTy :: Type -> (Type, Type)
+splitClosureTy = splitBinTy "splitClosureTy" closureTyConName
splitPArrayTy :: Type -> Type
-splitPArrayTy ty
- | Just (tc, [arg_ty]) <- splitTyConApp_maybe ty
- , isPArrayTyCon tc
- = arg_ty
+splitPArrayTy = splitUnTy "splitPArrayTy" parrayTyConName
- | otherwise = pprPanic "splitPArrayTy" (ppr ty)
+mkBuiltinTyConApp :: (Builtins -> TyCon) -> [Type] -> VM Type
+mkBuiltinTyConApp get_tc tys
+ = do
+ tc <- builtin get_tc
+ return $ mkTyConApp tc tys
-mkClosureType :: Type -> Type -> VM Type
-mkClosureType arg_ty res_ty
+mkBuiltinTyConApps :: (Builtins -> TyCon) -> [Type] -> Type -> VM Type
+mkBuiltinTyConApps get_tc tys ty
= do
- tc <- builtin closureTyCon
- return $ mkTyConApp tc [arg_ty, res_ty]
+ tc <- builtin get_tc
+ return $ foldr (mk tc) ty tys
+ where
+ mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
-mkClosureTypes :: [Type] -> Type -> VM Type
-mkClosureTypes arg_tys res_ty
+mkBuiltinTyConApps1 :: (Builtins -> TyCon) -> Type -> [Type] -> VM Type
+mkBuiltinTyConApps1 get_tc dft [] = return dft
+mkBuiltinTyConApps1 get_tc dft tys
= do
- tc <- builtin closureTyCon
- return $ foldr (mk tc) res_ty arg_tys
+ tc <- builtin get_tc
+ case tys of
+ [] -> pprPanic "mkBuiltinTyConApps1" (ppr tc)
+ _ -> return $ foldr1 (mk tc) tys
where
- mk tc arg_ty res_ty = mkTyConApp tc [arg_ty, res_ty]
+ mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2]
-mkPADictType :: Type -> VM Type
-mkPADictType ty
+mkPReprType :: [[Type]] -> VM Type
+mkPReprType [] = return unitTy
+mkPReprType tys
+ = do
+ embed <- builtin embedTyCon
+ cross <- builtin crossTyCon
+ plus <- builtin plusTyCon
+
+ let mk_embed ty = mkTyConApp embed [ty]
+ mk_cross ty1 ty2 = mkTyConApp cross [ty1, ty2]
+ mk_plus ty1 ty2 = mkTyConApp plus [ty1, ty2]
+
+ mk_tup [] = unitTy
+ mk_tup tys = foldr1 mk_cross tys
+
+ mk_sum [] = unitTy
+ mk_sum tys = foldr1 mk_plus tys
+
+ return . mk_sum
+ . map (mk_tup . map mk_embed)
+ $ tys
+
+mkPReprAlts :: [[CoreExpr]] -> VM ([CoreExpr], Type)
+mkPReprAlts ess
= do
- tc <- builtin paTyCon
- return $ TyConApp tc [ty]
+ embed_tc <- builtin embedTyCon
+ embed_dc <- builtin embedDataCon
+ cross_tc <- builtin crossTyCon
+ cross_dc <- builtin crossDataCon
+ plus_tc <- builtin plusTyCon
+ left_dc <- builtin leftDataCon
+ right_dc <- builtin rightDataCon
+
+ let mk_embed (expr, ty, pa)
+ = (mkConApp embed_dc [Type ty, pa, expr],
+ mkTyConApp embed_tc [ty])
+
+ mk_cross (expr1, ty1) (expr2, ty2)
+ = (mkConApp cross_dc [Type ty1, Type ty2, expr1, expr2],
+ mkTyConApp cross_tc [ty1, ty2])
+
+ mk_tup [] = (Var unitDataConId, unitTy)
+ mk_tup es = foldr1 mk_cross es
+
+ mk_sum [] = ([Var unitDataConId], unitTy)
+ mk_sum [(expr, ty)] = ([expr], ty)
+ mk_sum ((expr, lty) : es)
+ = let (alts, rty) = mk_sum es
+ in
+ (mkConApp left_dc [Type lty, Type rty, expr]
+ : [mkConApp right_dc [Type lty, Type rty, alt] | alt <- alts],
+ mkTyConApp plus_tc [lty, rty])
+
+ liftM (mk_sum . map (mk_tup . map mk_embed))
+ (mapM (mapM init) ess)
+ where
+ init expr = let ty = exprType expr
+ in do
+ pa <- paDictOfType ty
+ return (expr, ty, pa)
+
+mkClosureType :: Type -> Type -> VM Type
+mkClosureType arg_ty res_ty = mkBuiltinTyConApp closureTyCon [arg_ty, res_ty]
+
+mkClosureTypes :: [Type] -> Type -> VM Type
+mkClosureTypes = mkBuiltinTyConApps closureTyCon
+
+mkPADictType :: Type -> VM Type
+mkPADictType ty = mkBuiltinTyConApp paTyCon [ty]
mkPArrayType :: Type -> VM Type
-mkPArrayType ty
+mkPArrayType ty = mkBuiltinTyConApp parrayTyCon [ty]
+
+parrayReprTyCon :: Type -> VM (TyCon, [Type])
+parrayReprTyCon ty = builtin parrayTyCon >>= (`lookupFamInst` [ty])
+
+parrayReprDataCon :: Type -> VM (DataCon, [Type])
+parrayReprDataCon ty
= do
- tc <- builtin parrayTyCon
- return $ TyConApp tc [ty]
+ (tc, arg_tys) <- parrayReprTyCon ty
+ let [dc] = tyConDataCons tc
+ return (dc, arg_tys)
+
+mkVScrut :: VExpr -> VM (VExpr, TyCon, [Type])
+mkVScrut (ve, le)
+ = do
+ (tc, arg_tys) <- parrayReprTyCon (exprType ve)
+ return ((ve, unwrapFamInstScrut tc arg_tys le), tc, arg_tys)
paDictArgType :: TyVar -> VM (Maybe Type)
paDictArgType tv = go (TyVarTy tv) (tyVarKind tv)
dicts <- mapM paDictOfType tys
return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr
-lookupPArrayFamInst :: Type -> VM (TyCon, [Type])
-lookupPArrayFamInst ty = builtin parrayTyCon >>= (`lookupFamInst` [ty])
-
hoistBinding :: Var -> CoreExpr -> VM ()
hoistBinding v e = updGEnv $ \env ->
env { global_bindings = (v,e) : global_bindings env }
(arg_ty, res_ty) = splitClosureTy (exprType vclo)
buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
+buildClosures tvs vars [] res_ty mk_body
+ = mk_body
buildClosures tvs vars [arg_ty] res_ty mk_body
= buildClosure tvs vars arg_ty res_ty mk_body
buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body
-- NOTE: this transparently deals with empty environments
mkLiftEnv lc tys vs
= do
- (env_tc, env_tyargs) <- lookupPArrayFamInst vty
+ (env_tc, env_tyargs) <- parrayReprTyCon vty
let [env_con] = tyConDataCons env_tc
env = Var (dataConWrapId env_con)