X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=compiler%2Fvectorise%2FVectUtils.hs;h=8cb0a11a99251000380db7234b1c11e97f97c9c2;hb=151b1170c26a325cc93b2ae151804d5a714021a3;hp=cbca02f49e54317c119b0efe3bf54fc085b7d7a7;hpb=fede30f987edef37bc62a07bdcdf3e7b5b951475;p=ghc-hetmet.git diff --git a/compiler/vectorise/VectUtils.hs b/compiler/vectorise/VectUtils.hs index cbca02f..8cb0a11 100644 --- a/compiler/vectorise/VectUtils.hs +++ b/compiler/vectorise/VectUtils.hs @@ -1,28 +1,40 @@ module VectUtils ( collectAnnTypeBinders, collectAnnTypeArgs, isAnnTypeArg, + collectAnnValBinders, + mkDataConTag, splitClosureTy, - mkPADictType, mkPArrayType, - paDictArgType, paDictOfType, - paMethod, lengthPA, replicatePA, emptyPA, - polyAbstract, polyApply, - lookupPArrayFamInst, - hoistExpr, takeHoisted + + TyConRepr(..), mkTyConRepr, + mkToPRepr, mkToArrPRepr, mkFromPRepr, mkFromArrPRepr, + mkPADictType, mkPArrayType, mkPReprType, + + parrayCoerce, parrayReprTyCon, parrayReprDataCon, mkVScrut, + prDictOfType, prCoerce, + paDictArgType, paDictOfType, paDFunType, + paMethod, lengthPA, replicatePA, emptyPA, liftPA, + polyAbstract, polyApply, polyVApply, + hoistBinding, hoistExpr, hoistPolyVExpr, takeHoisted, + buildClosure, buildClosures, + mkClosureApp ) where #include "HsVersions.h" +import VectCore import VectMonad import DsUtils import CoreSyn import CoreUtils +import Coercion import Type import TypeRep import TyCon -import DataCon ( dataConWrapId ) +import DataCon import Var import Id ( mkWildId ) import MkId ( unwrapFamInstScrut ) +import Name ( Name ) import PrelNames import TysWiredIn import BasicTypes ( Boxity(..) ) @@ -30,7 +42,8 @@ import BasicTypes ( Boxity(..) ) import Outputable import FastString -import Control.Monad ( liftM, zipWithM_ ) +import Data.List ( zipWith4 ) +import Control.Monad ( liftM, liftM2, zipWithM_ ) collectAnnTypeArgs :: AnnExpr b ann -> (AnnExpr b ann, [Type]) collectAnnTypeArgs expr = go expr [] @@ -44,43 +57,288 @@ collectAnnTypeBinders expr = go [] expr go bs (_, AnnLam b e) | isTyVar b = go (b:bs) e go bs e = (reverse bs, e) +collectAnnValBinders :: AnnExpr Var ann -> ([Var], AnnExpr Var ann) +collectAnnValBinders expr = go [] expr + where + go bs (_, AnnLam b e) | isId b = go (b:bs) e + go bs e = (reverse bs, e) + isAnnTypeArg :: AnnExpr b ann -> Bool 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 "splitClosureTy" (ppr ty) + | otherwise = pprPanic s (ppr ty) -isPArrayTyCon :: TyCon -> Bool -isPArrayTyCon tc = tyConName tc == parrayTyConName +splitFixedTyConApp :: TyCon -> Type -> [Type] +splitFixedTyConApp tc ty + | Just (tc', tys) <- splitTyConApp_maybe ty + , tc == tc' + = tys + + | otherwise = pprPanic "splitFixedTyConApp" (ppr tc <+> ppr ty) + +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 -mkPADictType :: Type -> VM Type -mkPADictType ty +mkBuiltinTyConApps :: (Builtins -> TyCon) -> [Type] -> Type -> VM Type +mkBuiltinTyConApps get_tc tys ty = do - tc <- builtin paDictTyCon - return $ TyConApp tc [ty] + tc <- builtin get_tc + return $ foldr (mk tc) ty tys + where + mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2] + +mkBuiltinTyConApps1 :: (Builtins -> TyCon) -> Type -> [Type] -> VM Type +mkBuiltinTyConApps1 get_tc dft [] = return dft +mkBuiltinTyConApps1 get_tc dft tys + = do + tc <- builtin get_tc + case tys of + [] -> pprPanic "mkBuiltinTyConApps1" (ppr tc) + _ -> return $ foldr1 (mk tc) tys + where + mk tc ty1 ty2 = mkTyConApp tc [ty1,ty2] + +data TyConRepr = TyConRepr { + repr_tyvars :: [TyVar] + , repr_tys :: [[Type]] + + , repr_prod_tycons :: [Maybe TyCon] + , repr_prod_tys :: [Type] + , repr_sum_tycon :: Maybe TyCon + , repr_type :: Type + } + +mkTyConRepr :: TyCon -> VM TyConRepr +mkTyConRepr vect_tc + = do + prod_tycons <- mapM (mk_tycon prodTyCon) rep_tys + let prod_tys = zipWith mk_tc_app_maybe prod_tycons rep_tys + sum_tycon <- mk_tycon sumTyCon prod_tys + + return $ TyConRepr { + repr_tyvars = tyvars + , repr_tys = rep_tys + + , repr_prod_tycons = prod_tycons + , repr_prod_tys = prod_tys + , repr_sum_tycon = sum_tycon + , repr_type = mk_tc_app_maybe sum_tycon prod_tys + } + where + tyvars = tyConTyVars vect_tc + data_cons = tyConDataCons vect_tc + rep_tys = map dataConRepArgTys data_cons + + mk_tycon get_tc tys + | n > 1 = builtin (Just . get_tc n) + | otherwise = return Nothing + where n = length tys + + mk_tc_app_maybe Nothing [] = unitTy + mk_tc_app_maybe Nothing [ty] = ty + mk_tc_app_maybe (Just tc) tys = mkTyConApp tc tys + +mkToPRepr :: TyConRepr -> [[CoreExpr]] -> [CoreExpr] +mkToPRepr (TyConRepr { + repr_tys = repr_tys + , repr_prod_tycons = prod_tycons + , repr_prod_tys = prod_tys + , repr_sum_tycon = repr_sum_tycon + }) + = mk_sum . zipWith3 mk_prod prod_tycons repr_tys + where + Just sum_tycon = repr_sum_tycon + + mk_sum [] = [Var unitDataConId] + mk_sum [expr] = [expr] + mk_sum exprs = zipWith (mk_alt prod_tys) (tyConDataCons sum_tycon) exprs + + mk_alt tys dc expr = mk_con_app dc tys [expr] + + mk_prod _ _ [] = Var unitDataConId + mk_prod _ _ [expr] = expr + mk_prod (Just tc) tys exprs = mk_con_app dc tys exprs + where + [dc] = tyConDataCons tc + + mk_con_app dc tys exprs = mkConApp dc (map Type tys ++ exprs) + +mkToArrPRepr :: CoreExpr -> CoreExpr -> [[CoreExpr]] -> VM CoreExpr +mkToArrPRepr len sel ess + = do + let mk_sum [(expr, ty)] = return (expr, ty) + mk_sum es + = do + sum_tc <- builtin . sumTyCon $ length es + (sum_rtc, _) <- parrayReprTyCon (mkTyConApp sum_tc tys) + let [sum_rdc] = tyConDataCons sum_rtc + + return (mkConApp sum_rdc (map Type tys ++ (len : sel : exprs)), + mkTyConApp sum_tc tys) + where + (exprs, tys) = unzip es + + mk_prod [expr] = return (expr, splitPArrayTy (exprType expr)) + mk_prod exprs + = do + prod_tc <- builtin . prodTyCon $ length exprs + (prod_rtc, _) <- parrayReprTyCon (mkTyConApp prod_tc tys) + let [prod_rdc] = tyConDataCons prod_rtc + + return (mkConApp prod_rdc (map Type tys ++ (len : exprs)), + mkTyConApp prod_tc tys) + where + tys = map (splitPArrayTy . exprType) exprs + + liftM fst (mk_sum =<< mapM mk_prod ess) + +mkFromPRepr :: CoreExpr -> Type -> [([Var], CoreExpr)] -> VM CoreExpr +mkFromPRepr scrut res_ty alts + = do + sum_tcs <- builtins sumTyCon + prod_tcs <- builtins prodTyCon + + let un_sum expr ty [(vars, res)] = un_prod expr ty vars res + un_sum expr ty bs + = do + ps <- mapM (newLocalVar FSLIT("p")) tys + bodies <- sequence + $ zipWith4 un_prod (map Var ps) tys vars rs + return . Case expr (mkWildId ty) res_ty + $ zipWith3 mk_alt sum_dcs ps bodies + where + (vars, rs) = unzip bs + tys = splitFixedTyConApp sum_tc ty + sum_tc = sum_tcs $ length bs + sum_dcs = tyConDataCons sum_tc + + mk_alt dc p body = (DataAlt dc, [p], body) + + un_prod expr ty [] r = return r + un_prod expr ty [var] r = return $ Let (NonRec var expr) r + un_prod expr ty vars r + = return $ Case expr (mkWildId ty) res_ty + [(DataAlt prod_dc, vars, r)] + where + prod_tc = prod_tcs $ length vars + [prod_dc] = tyConDataCons prod_tc + + un_sum scrut (exprType scrut) alts + +mkFromArrPRepr :: CoreExpr -> Type -> Var -> Var -> [[Var]] -> CoreExpr + -> VM CoreExpr +mkFromArrPRepr scrut res_ty len sel vars res + = return (Var unitDataConId) + +mkClosureType :: Type -> Type -> VM Type +mkClosureType arg_ty res_ty = mkBuiltinTyConApp closureTyCon [arg_ty, res_ty] + +mkClosureTypes :: [Type] -> Type -> VM Type +mkClosureTypes = mkBuiltinTyConApps closureTyCon + +mkPReprType :: Type -> VM Type +mkPReprType ty = mkBuiltinTyConApp preprTyCon [ty] + +mkPADictType :: Type -> VM Type +mkPADictType ty = mkBuiltinTyConApp paTyCon [ty] mkPArrayType :: Type -> VM Type -mkPArrayType ty +mkPArrayType ty = mkBuiltinTyConApp parrayTyCon [ty] + +parrayCoerce :: TyCon -> [Type] -> CoreExpr -> VM CoreExpr +parrayCoerce repr_tc args expr + | Just arg_co <- tyConFamilyCoercion_maybe repr_tc = do - tc <- builtin parrayTyCon - return $ TyConApp tc [ty] + parray <- builtin parrayTyCon + + let co = mkAppCoercion (mkTyConApp parray []) + (mkSymCoercion (mkTyConApp arg_co args)) + + return $ mkCoerce co expr + +parrayReprTyCon :: Type -> VM (TyCon, [Type]) +parrayReprTyCon ty = builtin parrayTyCon >>= (`lookupFamInst` [ty]) + +parrayReprDataCon :: Type -> VM (DataCon, [Type]) +parrayReprDataCon ty + = do + (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) + +prDictOfType :: Type -> VM CoreExpr +prDictOfType orig_ty + | Just (tycon, ty_args) <- splitTyConApp_maybe orig_ty + = do + dfun <- traceMaybeV "prDictOfType" (ppr tycon) (lookupTyConPR tycon) + prDFunApply (Var dfun) ty_args + +prDFunApply :: CoreExpr -> [Type] -> VM CoreExpr +prDFunApply dfun tys + = do + args <- mapM mkDFunArg arg_tys + return $ mkApps mono_dfun args + where + mono_dfun = mkTyApps dfun tys + (arg_tys, _) = splitFunTys (exprType mono_dfun) + +mkDFunArg :: Type -> VM CoreExpr +mkDFunArg ty + | Just (tycon, [arg]) <- splitTyConApp_maybe ty + + = let name = tyConName tycon + + get_dict | name == paTyConName = paDictOfType + | name == prTyConName = prDictOfType + | otherwise = pprPanic "mkDFunArg" (ppr ty) + + in get_dict arg + +mkDFunArg ty = pprPanic "mkDFunArg" (ppr ty) + +prCoerce :: TyCon -> [Type] -> CoreExpr -> VM CoreExpr +prCoerce repr_tc args expr + | Just arg_co <- tyConFamilyCoercion_maybe repr_tc + = do + pr_tc <- builtin prTyCon + + let co = mkAppCoercion (mkTyConApp pr_tc []) + (mkSymCoercion (mkTyConApp arg_co args)) + + return $ mkCoerce co expr paDictArgType :: TyVar -> VM (Maybe Type) paDictArgType tv = go (TyVarTy tv) (tyVarKind tv) @@ -116,11 +374,21 @@ paDictOfTyApp (TyVarTy tv) ty_args paDFunApply dfun ty_args paDictOfTyApp (TyConApp tc _) ty_args = do - pa_class <- builtin paClass - (dfun, ty_args') <- lookupInst pa_class [TyConApp tc ty_args] - paDFunApply (Var dfun) ty_args' + dfun <- traceMaybeV "paDictOfTyApp" (ppr tc) (lookupTyConPA tc) + paDFunApply (Var dfun) ty_args paDictOfTyApp ty ty_args = pprPanic "paDictOfTyApp" (ppr ty) +paDFunType :: TyCon -> VM Type +paDFunType tc + = do + margs <- mapM paDictArgType tvs + res <- mkPADictType (mkTyConApp tc arg_tys) + return . mkForAllTys tvs + $ mkFunTys [arg | Just arg <- margs] res + where + tvs = tyConTyVars tc + arg_tys = mkTyVarTys tvs + paDFunApply :: CoreExpr -> [Type] -> VM CoreExpr paDFunApply dfun tys = do @@ -134,6 +402,9 @@ paMethod method ty dict <- paDictOfType ty return $ mkApps (Var fn) [Type ty, dict] +mkPR :: Type -> VM CoreExpr +mkPR = paMethod mkPRVar + lengthPA :: CoreExpr -> VM CoreExpr lengthPA x = liftM (`App` x) (paMethod lengthPAVar ty) where @@ -146,18 +417,11 @@ replicatePA len x = liftM (`mkApps` [len,x]) emptyPA :: Type -> VM CoreExpr emptyPA = paMethod emptyPAVar -type Vect a = (a,a) -type VVar = Vect Var -type VExpr = Vect CoreExpr - -vectorised :: Vect a -> a -vectorised = fst - -lifted :: Vect a -> a -lifted = snd - -mapVect :: (a -> b) -> Vect a -> Vect b -mapVect f (x,y) = (f x, f y) +liftPA :: CoreExpr -> VM CoreExpr +liftPA x + = do + lc <- builtin liftingContext + replicatePA (Var lc) x newLocalVVar :: FastString -> Type -> VM VVar newLocalVVar fs vty @@ -167,19 +431,6 @@ newLocalVVar fs vty lv <- newLocalVar fs lty return (vv,lv) -vVar :: VVar -> VExpr -vVar = mapVect Var - -mkVLams :: [VVar] -> VExpr -> VExpr -mkVLams vvs (ve,le) = (mkLams vs ve, mkLams ls le) - where - (vs,ls) = unzip vvs - -mkVVarApps :: Var -> VExpr -> [VVar] -> VExpr -mkVVarApps lc (ve, le) vvs = (ve `mkVarApps` vs, le `mkVarApps` (lc : ls)) - where - (vs,ls) = unzip vvs - polyAbstract :: [TyVar] -> ((CoreExpr -> CoreExpr) -> VM a) -> VM a polyAbstract tvs p = localV @@ -202,30 +453,38 @@ polyApply expr tys dicts <- mapM paDictOfType tys return $ expr `mkTyApps` tys `mkApps` dicts -lookupPArrayFamInst :: Type -> VM (TyCon, [Type]) -lookupPArrayFamInst ty = builtin parrayTyCon >>= (`lookupFamInst` [ty]) +polyVApply :: VExpr -> [Type] -> VM VExpr +polyVApply expr tys + = do + dicts <- mapM paDictOfType tys + return $ mapVect (\e -> e `mkTyApps` tys `mkApps` dicts) expr + +hoistBinding :: Var -> CoreExpr -> VM () +hoistBinding v e = updGEnv $ \env -> + env { global_bindings = (v,e) : global_bindings env } hoistExpr :: FastString -> CoreExpr -> VM Var hoistExpr fs expr = do var <- newLocalVar fs (exprType expr) - updGEnv $ \env -> - env { global_bindings = (var, expr) : global_bindings env } + hoistBinding var expr return var -hoistPolyExpr :: FastString -> [TyVar] -> CoreExpr -> VM CoreExpr -hoistPolyExpr fs tvs expr +hoistVExpr :: VExpr -> VM VVar +hoistVExpr (ve, le) = do - poly_expr <- closedV . polyAbstract tvs $ \abstract -> return (abstract expr) - fn <- hoistExpr fs poly_expr - polyApply (Var fn) (mkTyVarTys tvs) + fs <- getBindName + vv <- hoistExpr ('v' `consFS` fs) ve + lv <- hoistExpr ('l' `consFS` fs) le + return (vv, lv) -hoistPolyVExpr :: FastString -> [TyVar] -> VExpr -> VM VExpr -hoistPolyVExpr fs tvs (ve, le) +hoistPolyVExpr :: [TyVar] -> VM VExpr -> VM VExpr +hoistPolyVExpr tvs p = do - ve' <- hoistPolyExpr ('v' `consFS` fs) tvs ve - le' <- hoistPolyExpr ('l' `consFS` fs) tvs le - return (ve',le') + expr <- closedV . polyAbstract tvs $ \abstract -> + liftM (mapVect abstract) p + fn <- hoistVExpr expr + polyVApply (vVar fn) (mkTyVarTys tvs) takeHoisted :: VM [(Var, CoreExpr)] takeHoisted @@ -234,7 +493,6 @@ takeHoisted setGEnv $ env { global_bindings = [] } return $ global_bindings env - mkClosure :: Type -> Type -> Type -> VExpr -> VExpr -> VM VExpr mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv) = do @@ -244,36 +502,66 @@ mkClosure arg_ty res_ty env_ty (vfn,lfn) (venv,lenv) return (Var mkv `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, venv], Var mkl `mkTyApps` [arg_ty, res_ty, env_ty] `mkApps` [dict, vfn, lfn, lenv]) +mkClosureApp :: VExpr -> VExpr -> VM VExpr +mkClosureApp (vclo, lclo) (varg, larg) + = do + vapply <- builtin applyClosureVar + lapply <- builtin applyClosurePVar + return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg], + Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [lclo, larg]) + where + (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 + = do + res_ty' <- mkClosureTypes arg_tys res_ty + arg <- newLocalVVar FSLIT("x") arg_ty + buildClosure tvs vars arg_ty res_ty' + . hoistPolyVExpr tvs + $ do + lc <- builtin liftingContext + clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body + return $ vLams lc (vars ++ [arg]) clo + -- (clo , aclo (Arr lc xs1 ... xsn) ) -- where -- f = \env v -> case env of -> e x1 ... xn v -- f^ = \env v -> case env of Arr l xs1 ... xsn -> e^ l x1 ... xn v - -buildClosure :: [TyVar] -> Var -> [VVar] -> VVar -> VExpr -> VM VExpr -buildClosure tvs lv vars arg body +-- +buildClosure :: [TyVar] -> [VVar] -> Type -> Type -> VM VExpr -> VM VExpr +buildClosure tvs vars arg_ty res_ty mk_body = do - (env_ty, env, bind) <- buildEnv lv vars - env_bndr <- newLocalVVar FSLIT("env") env_ty - - fn <- hoistPolyVExpr FSLIT("fn") tvs - . mkVLams [env_bndr, arg] - . bind (vVar env_bndr) - $ mkVVarApps lv body (vars ++ [arg]) + (env_ty, env, bind) <- buildEnv vars + env_bndr <- newLocalVVar FSLIT("env") env_ty + arg_bndr <- newLocalVVar FSLIT("arg") arg_ty + + fn <- hoistPolyVExpr tvs + $ do + lc <- builtin liftingContext + body <- mk_body + body' <- bind (vVar env_bndr) + (vVarApps lc body (vars ++ [arg_bndr])) + return (vLamsWithoutLC [env_bndr, arg_bndr] body') mkClosure arg_ty res_ty env_ty fn env - where - arg_ty = idType (vectorised arg) - res_ty = exprType (vectorised body) - - -buildEnv :: Var -> [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VExpr) -buildEnv lv vvs +buildEnv :: [VVar] -> VM (Type, VExpr, VExpr -> VExpr -> VM VExpr) +buildEnv vvs = do + lc <- builtin liftingContext let (ty, venv, vbind) = mkVectEnv tys vs - (lenv, lbind) <- mkLiftEnv lv tys ls + (lenv, lbind) <- mkLiftEnv lc tys ls return (ty, (venv, lenv), - \(venv,lenv) (vbody,lbody) -> (vbind venv vbody, lbind lenv lbody)) + \(venv,lenv) (vbody,lbody) -> + do + let vbody' = vbind venv vbody + lbody' <- lbind lenv lbody + return (vbody', lbody')) where (vs,ls) = unzip vvs tys = map idType vs @@ -287,28 +575,33 @@ mkVectEnv tys vs = (ty, mkCoreTup (map Var vs), where ty = mkCoreTupTy tys -mkLiftEnv :: Var -> [Type] -> [Var] -> VM (CoreExpr, CoreExpr -> CoreExpr -> CoreExpr) -mkLiftEnv lv [ty] [v] - = do - len <- lengthPA (Var v) - return (Var v, \env body -> Let (NonRec v env) - $ Case len lv (exprType body) [(DEFAULT, [], body)]) +mkLiftEnv :: Var -> [Type] -> [Var] -> VM (CoreExpr, CoreExpr -> CoreExpr -> VM CoreExpr) +mkLiftEnv lc [ty] [v] + = return (Var v, \env body -> + do + len <- lengthPA (Var v) + return . Let (NonRec v env) + $ Case len lc (exprType body) [(DEFAULT, [], body)]) -- NOTE: this transparently deals with empty environments -mkLiftEnv lv tys vs +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) `mkTyApps` env_tyargs - `mkVarApps` (lv : vs) + `mkVarApps` (lc : vs) bind env body = let scrut = unwrapFamInstScrut env_tc env_tyargs env in - Case scrut (mkWildId (exprType scrut)) (exprType body) - [(DataAlt env_con, lv : vs, body)] + return $ Case scrut (mkWildId (exprType scrut)) + (exprType body) + [(DataAlt env_con, lc : bndrs, body)] return (env, bind) where vty = mkCoreTupTy tys + bndrs | null vs = [mkWildId unitTy] + | otherwise = vs +