1 -- |Compute the representation type for data type constructors.
3 module Vectorise
.Type
.Repr
(
4 CompRepr
(..), ProdRepr
(..), ConRepr
(..), SumRepr
(..),
5 tyConRepr
, sumReprType
, conReprType
, prodReprType
, compReprType
, compOrigType
10 import Vectorise
.Builtins
19 data CompRepr
= Keep Type
20 CoreExpr
-- PR dictionary for the type
23 data ProdRepr
= EmptyProd
25 | Prod
{ repr_tup_tc
:: TyCon
-- representation tuple tycon
26 , repr_ptup_tc
:: TyCon
-- PData representation tycon
27 , repr_comp_tys
:: [Type
] -- representation types of
28 , repr_comps
:: [CompRepr
] -- components
30 data ConRepr
= ConRepr DataCon ProdRepr
32 data SumRepr
= EmptySum
34 | Sum
{ repr_sum_tc
:: TyCon
-- representation sum tycon
35 , repr_psum_tc
:: TyCon
-- PData representation tycon
36 , repr_sel_ty
:: Type
-- type of selector
37 , repr_con_tys
:: [Type
] -- representation types of
38 , repr_cons
:: [ConRepr
] -- components
41 -- |Determine the representation type of a data type constructor.
43 tyConRepr
:: TyCon
-> VM SumRepr
44 tyConRepr tc
= sum_repr
(tyConDataCons tc
)
46 sum_repr
[] = return EmptySum
47 sum_repr
[con
] = liftM UnarySum
(con_repr con
)
49 rs
<- mapM con_repr cons
50 sum_tc
<- builtin
(sumTyCon arity
)
51 tys
<- mapM conReprType rs
52 (psum_tc
, _
) <- pdataReprTyCon
(mkTyConApp sum_tc tys
)
53 sel_ty
<- builtin
(selTy arity
)
54 return $ Sum
{ repr_sum_tc
= sum_tc
55 , repr_psum_tc
= psum_tc
56 , repr_sel_ty
= sel_ty
63 con_repr con
= liftM (ConRepr con
) (prod_repr
(dataConRepArgTys con
))
65 prod_repr
[] = return EmptyProd
66 prod_repr
[ty
] = liftM UnaryProd
(comp_repr ty
)
68 rs
<- mapM comp_repr tys
69 tup_tc
<- builtin
(prodTyCon arity
)
70 tys
' <- mapM compReprType rs
71 (ptup_tc
, _
) <- pdataReprTyCon
(mkTyConApp tup_tc tys
')
72 return $ Prod
{ repr_tup_tc
= tup_tc
73 , repr_ptup_tc
= ptup_tc
74 , repr_comp_tys
= tys
'
80 comp_repr ty
= liftM (Keep ty
) (prDictOfReprType ty
)
81 `orElseV`
return (Wrap ty
)
83 sumReprType
:: SumRepr
-> VM Type
84 sumReprType EmptySum
= voidType
85 sumReprType
(UnarySum r
) = conReprType r
86 sumReprType
(Sum
{ repr_sum_tc
= sum_tc
, repr_con_tys
= tys
})
87 = return $ mkTyConApp sum_tc tys
89 conReprType
:: ConRepr
-> VM Type
90 conReprType
(ConRepr _ r
) = prodReprType r
92 prodReprType
:: ProdRepr
-> VM Type
93 prodReprType EmptyProd
= voidType
94 prodReprType
(UnaryProd r
) = compReprType r
95 prodReprType
(Prod
{ repr_tup_tc
= tup_tc
, repr_comp_tys
= tys
})
96 = return $ mkTyConApp tup_tc tys
98 compReprType
:: CompRepr
-> VM Type
99 compReprType
(Keep ty _
) = return ty
100 compReprType
(Wrap ty
)
101 = do { wrap_tc
<- builtin wrapTyCon
102 ; return $ mkTyConApp wrap_tc
[ty
]
105 compOrigType
:: CompRepr
-> Type
106 compOrigType
(Keep ty _
) = ty
107 compOrigType
(Wrap ty
) = ty