+mkPRepr :: [[Type]] -> VM Type
+mkPRepr [] = return unitTy
+mkPRepr 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
+
+mkToPRepr :: [[CoreExpr]] -> VM ([CoreExpr], Type)
+mkToPRepr ess
+ = do
+ 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)
+
+mkFromPRepr :: CoreExpr -> Type -> [([Var], CoreExpr)] -> VM CoreExpr
+mkFromPRepr scrut res_ty alts
+ = do
+ embed_dc <- builtin embedDataCon
+ cross_dc <- builtin crossDataCon
+ left_dc <- builtin leftDataCon
+ right_dc <- builtin rightDataCon
+ pa_tc <- builtin paTyCon
+
+ let un_embed expr ty var res
+ = do
+ pa <- newLocalVar FSLIT("pa") (mkTyConApp pa_tc [idType var])
+ return $ Case expr (mkWildId ty) res_ty
+ [(DataAlt embed_dc, [pa, var], res)]
+
+ un_cross expr ty var1 var2 res
+ = Case expr (mkWildId ty) res_ty
+ [(DataAlt cross_dc, [var1, var2], res)]
+
+ un_tup expr ty [] res = return res
+ un_tup expr ty [var] res = un_embed expr ty var res
+ un_tup expr ty (var : vars) res
+ = do
+ lv <- newLocalVar FSLIT("x") lty
+ rv <- newLocalVar FSLIT("y") rty
+ liftM (un_cross expr ty lv rv)
+ (un_embed (Var lv) lty var
+ =<< un_tup (Var rv) rty vars res)
+ where
+ (lty, rty) = splitCrossTy ty
+
+ un_plus expr ty var1 var2 res1 res2
+ = Case expr (mkWildId ty) res_ty
+ [(DataAlt left_dc, [var1], res1),
+ (DataAlt right_dc, [var2], res2)]
+
+ un_sum expr ty [(vars, res)] = un_tup expr ty vars res
+ un_sum expr ty ((vars, res) : alts)
+ = do
+ lv <- newLocalVar FSLIT("l") lty
+ rv <- newLocalVar FSLIT("r") rty
+ liftM2 (un_plus expr ty lv rv)
+ (un_tup (Var lv) lty vars res)
+ (un_sum (Var rv) rty alts)
+ where
+ (lty, rty) = splitPlusTy ty
+
+ un_sum scrut (exprType scrut) alts
+