-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)
-