- return . mk_sum
- . map (mk_prod . map mk_embed)
- $ tys
--}
-
-mkToPRepr :: [[CoreExpr]] -> VM ([CoreExpr], Type)
-mkToPRepr ess
- = do
- embed_tc <- builtin embedTyCon
- embed_dc <- builtin embedDataCon
- sum_tcs <- builtins sumTyCon
- prod_tcs <- builtins prodTyCon
-
- let mk_sum [] = ([Var unitDataConId], unitTy)
- mk_sum [(expr, ty)] = ([expr], ty)
- mk_sum es = (zipWith mk_alt (tyConDataCons sum_tc) exprs,
- mkTyConApp sum_tc tys)
- where
- (exprs, tys) = unzip es
- sum_tc = sum_tcs (length es)
- mk_alt dc expr = mkConApp dc (map Type tys ++ [expr])
-
- mk_prod [] = (Var unitDataConId, unitTy)
- mk_prod [(expr, ty)] = (expr, ty)
- mk_prod es = (mkConApp prod_dc (map Type tys ++ exprs),
- mkTyConApp prod_tc tys)
- where
- (exprs, tys) = unzip es
- prod_tc = prod_tcs (length es)
- [prod_dc] = tyConDataCons prod_tc
-
- mk_embed expr = (mkConApp embed_dc [Type ty, expr],
- mkTyConApp embed_tc [ty])
- where ty = exprType expr
-
- return . mk_sum $ map (mk_prod . map mk_embed) ess
-
-mkToArrPRepr :: CoreExpr -> CoreExpr -> [[CoreExpr]] -> VM CoreExpr
-mkToArrPRepr len sel ess
- = do
- embed_tc <- builtin embedTyCon
- (embed_rtc, _) <- parrayReprTyCon (mkTyConApp embed_tc [unitTy])
- let [embed_rdc] = tyConDataCons embed_rtc
-
- 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, ty)] = return (expr, ty)
- mk_prod es
- = do
- prod_tc <- builtin . prodTyCon $ length es
- (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
- (exprs, tys) = unzip es
-
- mk_embed expr = (mkConApp embed_rdc [Type ty, expr],
- mkTyConApp embed_tc [ty])
- where ty = splitPArrayTy (exprType expr)
-
- liftM fst (mk_sum =<< mapM (mk_prod . map mk_embed) ess)
-
-mkFromPRepr :: CoreExpr -> Type -> [([Var], CoreExpr)] -> VM CoreExpr
-mkFromPRepr scrut res_ty alts
- = do
- embed_dc <- builtin embedDataCon
- 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 $ un_embed expr ty var r
- un_prod expr ty vars r
- = do
- xs <- mapM (newLocalVar FSLIT("x")) tys
- let body = foldr (\(e,t,v) r -> un_embed e t v r) r
- $ zip3 (map Var xs) tys vars
- return $ Case expr (mkWildId ty) res_ty
- [(DataAlt prod_dc, xs, body)]
- where
- tys = splitFixedTyConApp prod_tc ty
- prod_tc = prod_tcs $ length vars
- [prod_dc] = tyConDataCons prod_tc
-
- un_embed expr ty var r
- = Case expr (mkWildId ty) res_ty
- [(DataAlt embed_dc, [var], r)]
-
- 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)
-
-mkEmbedType :: Type -> VM Type
-mkEmbedType ty = mkBuiltinTyConApp embedTyCon [ty]
-
-mkClosureType :: Type -> Type -> VM Type
-mkClosureType arg_ty res_ty = mkBuiltinTyConApp closureTyCon [arg_ty, res_ty]