2 % (c) The AQUA Project, Glasgow University, 1996-1998
4 \section[TcTyDecls]{Typecheck type declarations}
13 #include "HsVersions.h"
15 import HsSyn ( MonoBinds(..),
16 TyClDecl(..), ConDecl(..), ConDetails(..), BangType(..),
19 import RnHsSyn ( RenamedTyClDecl, RenamedConDecl )
20 import TcHsSyn ( TcMonoBinds )
21 import BasicTypes ( RecFlag(..), NewOrData(..) )
23 import TcMonoType ( tcExtendTopTyVarScope, tcExtendTyVarScope,
24 tcHsTypeKind, tcHsType, tcHsTopType, tcHsTopBoxedType,
25 tcContext, tcHsTopTypeKind
27 import TcType ( zonkTcTyVarToTyVar, zonkTcClassConstraints )
28 import TcEnv ( tcLookupTy, TcTyThing(..) )
30 import TcUnify ( unifyKind )
32 import Class ( Class )
33 import DataCon ( DataCon, dataConSig, mkDataCon, isNullaryDataCon,
34 dataConFieldLabels, dataConId,
35 markedStrict, notMarkedStrict, markedUnboxed
37 import MkId ( mkDataConId, mkRecordSelId, mkNewTySelId )
38 import Id ( getIdUnfolding )
39 import CoreUnfold ( unfoldingTemplate )
41 import Var ( Id, TyVar )
42 import Name ( Name, isLocallyDefined, OccName, NamedThing(..), nameUnique )
44 import TyCon ( TyCon, ArgVrcs, mkSynTyCon, mkAlgTyCon, isAlgTyCon,
45 isSynTyCon, tyConDataCons, isNewTyCon
47 import Type ( getTyVar, tyVarsOfTypes,
48 mkTyConApp, mkTyVarTys, mkForAllTys, mkFunTy,
50 mkArrowKind, mkArrowKinds, boxedTypeKind,
51 isUnboxedType, Type, ThetaType, classesOfPreds
53 import Var ( tyVarKind )
54 import VarSet ( intersectVarSet, isEmptyVarSet )
55 import Util ( equivClasses )
56 import FiniteMap ( FiniteMap, lookupWithDefaultFM )
59 %************************************************************************
61 \subsection{Kind checking}
63 %************************************************************************
66 kcTyDecl :: RenamedTyClDecl -> TcM s ()
68 kcTyDecl (TySynonym name tyvar_names rhs src_loc)
69 = tcLookupTy name `thenNF_Tc` \ (kind, _, _) ->
70 tcExtendTopTyVarScope kind tyvar_names $ \ _ result_kind ->
71 tcHsTypeKind rhs `thenTc` \ (rhs_kind, _) ->
72 unifyKind result_kind rhs_kind
74 kcTyDecl (TyData _ context tycon_name tyvar_names con_decls _ _ src_loc)
75 = tcLookupTy tycon_name `thenNF_Tc` \ (kind, _, _) ->
76 tcExtendTopTyVarScope kind tyvar_names $ \ result_kind _ ->
77 tcContext context `thenTc_`
78 mapTc kcConDecl con_decls `thenTc_`
81 kcConDecl (ConDecl _ ex_tvs ex_ctxt details loc)
83 tcExtendTyVarScope ex_tvs ( \ tyvars ->
84 tcContext ex_ctxt `thenTc_`
85 kc_con details `thenTc_`
89 kc_con (VanillaCon btys) = mapTc kc_bty btys `thenTc_` returnTc ()
90 kc_con (InfixCon bty1 bty2) = mapTc kc_bty [bty1,bty2] `thenTc_` returnTc ()
91 kc_con (NewCon ty _) = tcHsType ty `thenTc_` returnTc ()
92 kc_con (RecCon flds) = mapTc kc_field flds `thenTc_` returnTc ()
94 kc_bty (Banged ty) = tcHsType ty
95 kc_bty (Unbanged ty) = tcHsType ty
96 kc_bty (Unpacked ty) = tcHsType ty
98 kc_field (_, bty) = kc_bty bty
102 %************************************************************************
104 \subsection{Type checking}
106 %************************************************************************
109 tcTyDecl :: RecFlag -> FiniteMap Name ArgVrcs -> RenamedTyClDecl -> TcM s TyCon
111 tcTyDecl is_rec rec_vrcs (TySynonym tycon_name tyvar_names rhs src_loc)
112 = tcLookupTy tycon_name `thenNF_Tc` \ (tycon_kind, Just arity, _) ->
113 tcExtendTopTyVarScope tycon_kind tyvar_names $ \ tyvars _ ->
114 tcHsTopTypeKind rhs `thenTc` \ (_, rhs_ty) ->
116 -- Construct the tycon
117 argvrcs = lookupWithDefaultFM rec_vrcs (pprPanic "tcTyDecl: argvrcs:" $ ppr tycon_name)
119 tycon = mkSynTyCon tycon_name tycon_kind arity tyvars rhs_ty argvrcs
124 tcTyDecl is_rec rec_vrcs (TyData data_or_new context tycon_name tyvar_names con_decls derivings pragmas src_loc)
125 = -- Lookup the pieces
126 tcLookupTy tycon_name `thenNF_Tc` \ (tycon_kind, _, ATyCon rec_tycon) ->
127 tcExtendTopTyVarScope tycon_kind tyvar_names $ \ tyvars _ ->
129 -- Typecheck the pieces
130 tcContext context `thenTc` \ ctxt ->
131 let ctxt' = classesOfPreds ctxt in
132 mapTc (tcConDecl rec_tycon tyvars ctxt') con_decls `thenTc` \ data_cons ->
133 tc_derivs derivings `thenTc` \ derived_classes ->
136 -- Construct the tycon
137 real_data_or_new = case data_or_new of
139 DataType | all isNullaryDataCon data_cons -> EnumType
140 | otherwise -> DataType
142 argvrcs = lookupWithDefaultFM rec_vrcs (pprPanic "tcTyDecl: argvrcs:" $ ppr tycon_name)
145 tycon = mkAlgTyCon tycon_name tycon_kind tyvars ctxt' argvrcs
148 Nothing -- Not a dictionary
149 real_data_or_new is_rec
153 tc_derivs Nothing = returnTc []
154 tc_derivs (Just ds) = mapTc tc_deriv ds
156 tc_deriv name = tcLookupTy name `thenTc` \ (_, _, AClass clas) ->
161 %************************************************************************
163 \subsection{Type check constructors}
165 %************************************************************************
168 tcConDecl :: TyCon -> [TyVar] -> [(Class,[Type])] -> RenamedConDecl -> TcM s DataCon
170 tcConDecl tycon tyvars ctxt (ConDecl name ex_tvs ex_ctxt details src_loc)
171 = tcAddSrcLoc src_loc $
172 tcExtendTyVarScope ex_tvs $ \ ex_tyvars ->
173 tcContext ex_ctxt `thenTc` \ ex_theta ->
174 let ex_ctxt' = classesOfPreds ex_theta in
175 tc_con_decl_help tycon tyvars ctxt name ex_tyvars ex_ctxt' details
177 tc_con_decl_help tycon tyvars ctxt name ex_tyvars ex_theta details
179 VanillaCon btys -> tc_datacon btys
180 InfixCon bty1 bty2 -> tc_datacon [bty1,bty2]
181 NewCon ty mb_f -> tc_newcon ty mb_f
182 RecCon fields -> tc_rec_con fields
186 arg_stricts = map get_strictness btys
187 tys = map get_pty btys
189 mapTc tcHsTopType tys `thenTc` \ arg_tys ->
190 mk_data_con arg_stricts arg_tys []
193 = tcHsTopBoxedType ty `thenTc` \ arg_ty ->
194 -- can't allow an unboxed type here, because we're effectively
195 -- going to remove the constructor while coercing it to a boxed type.
200 Just f -> [mkFieldLabel (getName f) arg_ty (head allFieldLabelTags)]
202 mk_data_con [notMarkedStrict] [arg_ty] field_label
205 = checkTc (null ex_tyvars) (exRecConErr name) `thenTc_`
206 mapTc tc_field fields `thenTc` \ field_label_infos_s ->
208 field_label_infos = concat field_label_infos_s
209 arg_stricts = [strict | (_, _, strict) <- field_label_infos]
210 arg_tys = [ty | (_, ty, _) <- field_label_infos]
212 field_labels = [ mkFieldLabel (getName name) ty tag
213 | ((name, ty, _), tag) <- field_label_infos `zip` allFieldLabelTags ]
215 mk_data_con arg_stricts arg_tys field_labels
217 tc_field (field_label_names, bty)
218 = tcHsTopType (get_pty bty) `thenTc` \ field_ty ->
219 returnTc [(name, field_ty, get_strictness bty) | name <- field_label_names]
221 mk_data_con arg_stricts arg_tys fields
222 = -- Now we've checked all the field types we must
223 -- zonk the existential tyvars to finish the kind
224 -- inference on their kinds, and commit them to being
225 -- immutable type variables. (The top-level tyvars are
226 -- already fixed, by the preceding kind-inference pass.)
227 mapNF_Tc zonkTcTyVarToTyVar ex_tyvars `thenNF_Tc` \ ex_tyvars' ->
228 zonkTcClassConstraints ex_theta `thenNF_Tc` \ ex_theta' ->
230 data_con = mkDataCon name arg_stricts fields
231 tyvars (thinContext arg_tys ctxt)
235 data_con_id = mkDataConId data_con
239 -- The context for a data constructor should be limited to
240 -- the type variables mentioned in the arg_tys
241 thinContext arg_tys ctxt
242 = filter in_arg_tys ctxt
244 arg_tyvars = tyVarsOfTypes arg_tys
245 in_arg_tys (clas,tys) = not $ isEmptyVarSet $
246 tyVarsOfTypes tys `intersectVarSet` arg_tyvars
248 get_strictness (Banged _) = markedStrict
249 get_strictness (Unbanged _) = notMarkedStrict
250 get_strictness (Unpacked _) = markedUnboxed
252 get_pty (Banged ty) = ty
253 get_pty (Unbanged ty) = ty
254 get_pty (Unpacked ty) = ty
259 %************************************************************************
261 \subsection{Generating constructor/selector bindings for data declarations}
263 %************************************************************************
266 mkDataBinds :: [TyCon] -> TcM s ([Id], TcMonoBinds)
267 mkDataBinds [] = returnTc ([], EmptyMonoBinds)
268 mkDataBinds (tycon : tycons)
269 | isSynTyCon tycon = mkDataBinds tycons
270 | otherwise = mkDataBinds_one tycon `thenTc` \ (ids1, b1) ->
271 mkDataBinds tycons `thenTc` \ (ids2, b2) ->
272 returnTc (ids1++ids2, b1 `AndMonoBinds` b2)
274 mkDataBinds_one tycon
275 = mapTc (mkRecordSelector tycon) groups `thenTc` \ sel_ids ->
277 data_ids = map dataConId data_cons ++ sel_ids
279 -- For the locally-defined things
280 -- we need to turn the unfoldings inside the Ids into bindings,
281 binds | isLocallyDefined tycon
282 = [ CoreMonoBind data_id (unfoldingTemplate (getIdUnfolding data_id))
283 | data_id <- data_ids, isLocallyDefined data_id
288 returnTc (data_ids, andMonoBindList binds)
290 data_cons = tyConDataCons tycon
291 fields = [ (con, field) | con <- data_cons,
292 field <- dataConFieldLabels con
295 -- groups is list of fields that share a common name
296 groups = equivClasses cmp_name fields
297 cmp_name (_, field1) (_, field2)
298 = fieldLabelName field1 `compare` fieldLabelName field2
302 mkRecordSelector tycon fields@((first_con, first_field_label) : other_fields)
303 -- These fields all have the same name, but are from
304 -- different constructors in the data type
305 -- Check that all the fields in the group have the same type
306 -- This check assumes that all the constructors of a given
307 -- data type use the same type variables
308 = checkTc (all (== field_ty) other_tys)
309 (fieldTypeMisMatch field_name) `thenTc_`
312 field_ty = fieldLabelType first_field_label
313 field_name = fieldLabelName first_field_label
314 other_tys = [fieldLabelType fl | (_, fl) <- other_fields]
315 (tyvars, _, _, _, _, _) = dataConSig first_con
316 data_ty = mkTyConApp tycon (mkTyVarTys tyvars)
317 -- tyvars of first_con may be free in field_ty
318 -- Now build the selector
321 selector_ty = mkForAllTys tyvars $
327 | isNewTyCon tycon = mkNewTySelId first_field_label selector_ty
328 | otherwise = mkRecordSelId first_field_label selector_ty
335 fieldTypeMisMatch field_name
336 = sep [ptext SLIT("Declared types differ for field"), quotes (ppr field_name)]
339 = ptext SLIT("Can't combine named fields with locally-quantified type variables")
341 (ptext SLIT("In the declaration of data constructor") <+> ppr name)