- arity = length reprs
-
-splitSumRepr :: Repr -> [Repr]
-splitSumRepr (SumRepr { sum_components = reprs }) = reprs
-splitSumRepr repr = [repr]
-
-boxRepr :: Repr -> VM Repr
-boxRepr (VoidRepr {}) = boxedProductRepr []
-boxRepr (IdRepr ty) = boxedProductRepr [ty]
-boxRepr repr = return repr
-
-reprType :: Repr -> Type
-reprType (ProdRepr { prod_tycon = tycon, prod_components = tys })
- = mkTyConApp tycon tys
-reprType (SumRepr { sum_tycon = tycon, sum_components = reprs })
- = mkTyConApp tycon (map reprType reprs)
-reprType (IdRepr ty) = ty
-reprType (VoidRepr { void_tycon = tycon }) = mkTyConApp tycon []
-reprType (EnumRepr { enum_tycon = tycon }) = mkTyConApp tycon []
-
-arrReprType :: Repr -> VM Type
-arrReprType = mkPArrayType . reprType
-
-arrShapeTys :: Repr -> VM [Type]
-arrShapeTys (SumRepr {}) = sumShapeTys
-arrShapeTys (ProdRepr {}) = return [intPrimTy]
-arrShapeTys (IdRepr _) = return []
-arrShapeTys (VoidRepr {}) = return [intPrimTy]
-arrShapeTys (EnumRepr {}) = sumShapeTys
-
-sumShapeTys :: VM [Type]
-sumShapeTys = do
- int_arr <- builtin intPrimArrayTy
- return [intPrimTy, int_arr, int_arr]
-
-
-arrShapeVars :: Repr -> VM [Var]
-arrShapeVars repr = mapM (newLocalVar (fsLit "sh")) =<< arrShapeTys repr
-
-replicateShape :: Repr -> CoreExpr -> CoreExpr -> VM [CoreExpr]
-replicateShape (ProdRepr {}) len _ = return [len]
-replicateShape (SumRepr {}) len tag = replicateSumShape len tag
-replicateShape (IdRepr _) _ _ = return []
-replicateShape (VoidRepr {}) len _ = return [len]
-replicateShape (EnumRepr {}) len tag = replicateSumShape len tag
-
-replicateSumShape :: CoreExpr -> CoreExpr -> VM [CoreExpr]
-replicateSumShape len tag
- = do
- rep <- builtin replicatePAIntPrimVar
- up <- builtin upToPAIntPrimVar
- return [len, Var rep `mkApps` [len, tag], Var up `App` len]
-
-arrSelector :: Repr -> [CoreExpr] -> VM (CoreExpr, CoreExpr, CoreExpr)
-arrSelector (SumRepr {}) [len, sel, is] = return (len, sel, is)
-arrSelector (EnumRepr {}) [len, sel, is] = return (len, sel, is)
-arrSelector _ _ = panic "arrSelector"
-
-emptyArrRepr :: Repr -> VM [CoreExpr]
-emptyArrRepr (SumRepr { sum_components = prods })
- = liftM concat $ mapM emptyArrRepr prods
-emptyArrRepr (ProdRepr { prod_components = [] })
- = return [Var unitDataConId]
-emptyArrRepr (ProdRepr { prod_components = tys })
- = mapM emptyPA tys
-emptyArrRepr (IdRepr ty)
- = liftM singleton $ emptyPA ty
-emptyArrRepr (VoidRepr { void_tycon = tycon })
- = liftM singleton $ emptyPA (mkTyConApp tycon [])
-emptyArrRepr (EnumRepr {})
- = return []
-
-arrReprTys :: Repr -> VM [Type]
-arrReprTys (SumRepr { sum_components = reprs })
- = liftM concat $ mapM arrReprTys reprs
-arrReprTys (ProdRepr { prod_components = [] })
- = return [unitTy]
-arrReprTys (ProdRepr { prod_components = tys })
- = mapM mkPArrayType tys
-arrReprTys (IdRepr ty)
- = liftM singleton $ mkPArrayType ty
-arrReprTys (VoidRepr { void_tycon = tycon })
- = liftM singleton $ mkPArrayType (mkTyConApp tycon [])
-arrReprTys (EnumRepr {})
- = return []
-
-arrReprTys' :: Repr -> VM [[Type]]
-arrReprTys' (SumRepr { sum_components = reprs })
- = mapM arrReprTys reprs
-arrReprTys' repr = liftM singleton $ arrReprTys repr
-
-arrReprVars :: Repr -> VM [[Var]]
-arrReprVars repr
- = mapM (mapM (newLocalVar (fsLit "rs"))) =<< arrReprTys' repr
-
-mkRepr :: TyCon -> VM Repr
-mkRepr vect_tc
- | [tys] <- rep_tys = boxedProductRepr tys
- -- removed: | all null rep_tys = enumRepr
- | otherwise = sumRepr =<< mapM unboxedProductRepr rep_tys
- where
- rep_tys = map dataConRepArgTys $ tyConDataCons vect_tc
-
-buildPReprType :: TyCon -> VM Type
-buildPReprType = liftM reprType . mkRepr
-
-buildToPRepr :: Repr -> TyCon -> TyCon -> TyCon -> VM CoreExpr
-buildToPRepr repr vect_tc prepr_tc _
+ sum_repr [] = return EmptySum
+ sum_repr [con] = liftM UnarySum (con_repr con)
+ sum_repr cons = do
+ rs <- mapM con_repr cons
+ sum_tc <- builtin (sumTyCon arity)
+ tys <- mapM conReprType rs
+ (psum_tc, _) <- pdataReprTyCon (mkTyConApp sum_tc tys)
+ sel_ty <- builtin (selTy arity)
+ return $ Sum { repr_sum_tc = sum_tc
+ , repr_psum_tc = psum_tc
+ , repr_sel_ty = sel_ty
+ , repr_con_tys = tys
+ , repr_cons = rs
+ }
+ where
+ arity = length cons
+
+ con_repr con = liftM (ConRepr con) (prod_repr (dataConRepArgTys con))
+
+ prod_repr [] = return EmptyProd
+ prod_repr [ty] = liftM UnaryProd (comp_repr ty)
+ prod_repr tys = do
+ rs <- mapM comp_repr tys
+ tup_tc <- builtin (prodTyCon arity)
+ tys' <- mapM compReprType rs
+ (ptup_tc, _) <- pdataReprTyCon (mkTyConApp tup_tc tys')
+ return $ Prod { repr_tup_tc = tup_tc
+ , repr_ptup_tc = ptup_tc
+ , repr_comp_tys = tys'
+ , repr_comps = rs
+ }
+ where
+ arity = length tys
+
+ comp_repr ty = liftM (Keep ty) (prDictOfType ty)
+ `orElseV` return (Wrap ty)
+
+sumReprType :: SumRepr -> VM Type
+sumReprType EmptySum = voidType
+sumReprType (UnarySum r) = conReprType r
+sumReprType (Sum { repr_sum_tc = sum_tc, repr_con_tys = tys })
+ = return $ mkTyConApp sum_tc tys
+
+conReprType :: ConRepr -> VM Type
+conReprType (ConRepr _ r) = prodReprType r
+
+prodReprType :: ProdRepr -> VM Type
+prodReprType EmptyProd = voidType
+prodReprType (UnaryProd r) = compReprType r
+prodReprType (Prod { repr_tup_tc = tup_tc, repr_comp_tys = tys })
+ = return $ mkTyConApp tup_tc tys
+
+compReprType :: CompRepr -> VM Type
+compReprType (Keep ty _) = return ty
+compReprType (Wrap ty) = do
+ wrap_tc <- builtin wrapTyCon
+ return $ mkTyConApp wrap_tc [ty]
+
+compOrigType :: CompRepr -> Type
+compOrigType (Keep ty _) = ty
+compOrigType (Wrap ty) = ty
+
+buildToPRepr :: TyCon -> TyCon -> TyCon -> SumRepr -> VM CoreExpr
+buildToPRepr vect_tc repr_tc _ repr