2 % (c) The AQUA Project, Glasgow University, 1996-1998
4 \section[TcTyDecls]{Typecheck type declarations}
13 #include "HsVersions.h"
15 import HsSyn ( MonoBinds(..),
16 TyDecl(..), ConDecl(..), ConDetails(..), BangType(..),
19 import RnHsSyn ( RenamedTyDecl, RenamedConDecl )
20 import TcHsSyn ( TcMonoBinds )
21 import BasicTypes ( RecFlag(..), NewOrData(..), StrictnessMark(..) )
23 import Inst ( InstOrigin(..) )
24 import TcMonoType ( tcHsTypeKind, tcHsType, tcContext )
25 import TcEnv ( TcIdOcc(..),
26 tcLookupTyCon, tcLookupClass,
30 import TcUnify ( unifyKind )
32 import Class ( Class )
33 import DataCon ( DataCon, dataConSig, mkDataCon, isNullaryDataCon,
34 dataConFieldLabels, dataConId
36 import MkId ( mkDataConId, mkRecordSelId )
37 import Id ( getIdUnfolding )
38 import CoreUnfold ( getUnfoldingTemplate )
40 import Var ( Id, TyVar )
41 import Name ( isLocallyDefined, OccName(..), NamedThing(..) )
43 import TyCon ( TyCon, mkSynTyCon, mkAlgTyCon, isAlgTyCon,
44 isSynTyCon, tyConDataCons
46 import Type ( typeKind, getTyVar, tyVarsOfTypes,
47 mkTyConApp, mkTyVarTys, mkForAllTys, mkFunTy,
49 mkArrowKind, mkArrowKinds, boxedTypeKind,
50 isUnboxedType, Type, ThetaType
52 import Var ( tyVarKind )
53 import VarSet ( intersectVarSet, isEmptyVarSet )
54 import Util ( equivClasses, panic, assertPanic )
58 tcTyDecl :: RecFlag -> RenamedTyDecl -> TcM s TyCon
65 tcTyDecl is_rec (TySynonym tycon_name tyvar_names rhs src_loc)
66 = tcAddSrcLoc src_loc $
67 tcAddErrCtxt (tySynCtxt tycon_name) $
70 tcLookupTyCon tycon_name `thenTc` \ (tycon_kind, _, rec_tycon) ->
71 tcLookupTyVarBndrs tyvar_names `thenNF_Tc` \ (tyvar_kinds, rec_tyvars) ->
74 tcHsTypeKind rhs `thenTc` \ (rhs_kind, rhs_ty) ->
76 -- Unify tycon kind with (k1->...->kn->rhs)
77 unifyKind tycon_kind (mkArrowKinds tyvar_kinds rhs_kind) `thenTc_`
79 -- Construct the tycon
80 kind = mkArrowKinds (map tyVarKind rec_tyvars) (typeKind rhs_ty)
81 tycon = mkSynTyCon (getName tycon_name)
90 Algebraic data and newtype decls
91 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
94 tcTyDecl is_rec (TyData data_or_new context tycon_name tyvar_names con_decls derivings pragmas src_loc)
95 = tcAddSrcLoc src_loc $
96 let ctxt = case data_or_new of
97 NewType -> tyNewCtxt tycon_name
98 DataType -> tyDataCtxt tycon_name
103 tcLookupTyCon tycon_name `thenTc` \ (tycon_kind, _, rec_tycon) ->
104 tcLookupTyVarBndrs tyvar_names `thenNF_Tc` \ (tyvar_kinds, rec_tyvars) ->
105 tc_derivs derivings `thenTc` \ derived_classes ->
107 -- Typecheck the context
108 tcContext context `thenTc` \ ctxt ->
110 -- Unify tycon kind with (k1->...->kn->Type)
111 unifyKind tycon_kind (mkArrowKinds tyvar_kinds boxedTypeKind) `thenTc_`
114 mapTc (tcConDecl rec_tycon rec_tyvars ctxt) con_decls
115 `thenTc` \ data_cons ->
117 -- Construct the tycon
118 real_data_or_new = case data_or_new of
120 DataType -> if all isNullaryDataCon data_cons then
125 kind = foldr (mkArrowKind . tyVarKind) boxedTypeKind rec_tyvars
126 tycon = mkAlgTyCon (getName tycon_name)
132 Nothing -- Not a dictionary
138 tc_derivs Nothing = returnTc []
139 tc_derivs (Just ds) = mapTc tc_deriv ds
142 = tcLookupClass name `thenTc` \ (_, clas) ->
146 Generating constructor/selector bindings for data declarations
147 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
150 mkDataBinds :: [TyCon] -> TcM s ([Id], TcMonoBinds s)
151 mkDataBinds [] = returnTc ([], EmptyMonoBinds)
152 mkDataBinds (tycon : tycons)
153 | isSynTyCon tycon = mkDataBinds tycons
154 | otherwise = mkDataBinds_one tycon `thenTc` \ (ids1, b1) ->
155 mkDataBinds tycons `thenTc` \ (ids2, b2) ->
156 returnTc (ids1++ids2, b1 `AndMonoBinds` b2)
158 mkDataBinds_one tycon
159 = ASSERT( isAlgTyCon tycon )
160 mapTc (mkRecordSelector tycon) groups `thenTc` \ sel_ids ->
162 data_ids = map dataConId data_cons ++ sel_ids
164 -- For the locally-defined things
165 -- we need to turn the unfoldings inside the Ids into bindings,
166 binds = [ CoreMonoBind (RealId data_id) (getUnfoldingTemplate (getIdUnfolding data_id))
167 | data_id <- data_ids, isLocallyDefined data_id
170 returnTc (data_ids, andMonoBindList binds)
172 data_cons = tyConDataCons tycon
173 fields = [ (con, field) | con <- data_cons,
174 field <- dataConFieldLabels con
177 -- groups is list of fields that share a common name
178 groups = equivClasses cmp_name fields
179 cmp_name (_, field1) (_, field2)
180 = fieldLabelName field1 `compare` fieldLabelName field2
184 mkRecordSelector tycon fields@((first_con, first_field_label) : other_fields)
185 -- These fields all have the same name, but are from
186 -- different constructors in the data type
187 -- Check that all the fields in the group have the same type
188 -- This check assumes that all the constructors of a given
189 -- data type use the same type variables
190 = checkTc (all (== field_ty) other_tys)
191 (fieldTypeMisMatch field_name) `thenTc_`
194 field_ty = fieldLabelType first_field_label
195 field_name = fieldLabelName first_field_label
196 other_tys = [fieldLabelType fl | (_, fl) <- other_fields]
197 (tyvars, _, _, _, _, _) = dataConSig first_con
198 data_ty = mkTyConApp tycon (mkTyVarTys tyvars)
199 -- tyvars of first_con may be free in field_ty
200 -- Now build the selector
203 selector_ty = mkForAllTys tyvars $
208 selector_id = mkRecordSelId first_field_label selector_ty
214 tcConDecl :: TyCon -> [TyVar] -> ThetaType -> RenamedConDecl -> TcM s DataCon
216 tcConDecl tycon tyvars ctxt (ConDecl name ex_tvs ex_ctxt details src_loc)
217 = tcAddSrcLoc src_loc $
218 tcLookupTyVarBndrs ex_tvs `thenNF_Tc` \ (kinds, ex_tyvars) ->
219 tcContext ex_ctxt `thenTc` \ ex_theta ->
220 tc_con_help tycon tyvars ctxt name ex_tyvars ex_theta details
222 tc_con_help tycon tyvars ctxt name ex_tyvars ex_theta (VanillaCon btys)
223 = tc_datacon_help tycon tyvars ctxt name ex_tyvars ex_theta btys
225 tc_con_help tycon tyvars ctxt name ex_tyvars ex_theta (InfixCon bty1 bty2)
226 = tc_datacon_help tycon tyvars ctxt name ex_tyvars ex_theta [bty1,bty2]
228 tc_con_help tycon tyvars ctxt name ex_tyvars ex_theta (NewCon ty)
229 = tcHsType ty `thenTc` \ arg_ty ->
230 -- can't allow an unboxed type here, because we're effectively
231 -- going to remove the constructor while coercing it to a boxed type.
232 checkTc (not (isUnboxedType arg_ty)) (newTypeUnboxedField ty) `thenTc_`
234 data_con = mkDataCon (getName name)
236 [{- No labelled fields -}]
242 data_con_id = mkDataConId data_con
246 tc_con_help tycon tyvars ctxt name ex_tyvars ex_theta (RecCon fields)
247 = checkTc (null ex_tyvars) (exRecConErr name) `thenTc_`
248 mapTc tcField fields `thenTc` \ field_label_infos_s ->
250 field_label_infos = concat field_label_infos_s
251 arg_stricts = [strict | (_, _, strict) <- field_label_infos]
252 arg_tys = [ty | (_, ty, _) <- field_label_infos]
254 field_labels = [ mkFieldLabel (getName name) ty tag
255 | ((name, ty, _), tag) <- field_label_infos `zip` allFieldLabelTags ]
257 data_con = mkDataCon (getName name)
261 (thinContext arg_tys ctxt)
265 data_con_id = mkDataConId data_con
269 tcField (field_label_names, bty)
270 = tcHsType (get_pty bty) `thenTc` \ field_ty ->
271 returnTc [(name, field_ty, get_strictness bty) | name <- field_label_names]
273 tc_datacon_help tycon tyvars ctxt name ex_tyvars ex_theta btys
275 arg_stricts = map get_strictness btys
276 tys = map get_pty btys
278 mapTc tcHsType tys `thenTc` \ arg_tys ->
280 data_con = mkDataCon (getName name)
282 [{- No field labels -}]
284 (thinContext arg_tys ctxt)
288 data_con_id = mkDataConId data_con
292 -- The context for a data constructor should be limited to
293 -- the type variables mentioned in the arg_tys
294 thinContext arg_tys ctxt
295 = filter in_arg_tys ctxt
297 arg_tyvars = tyVarsOfTypes arg_tys
298 in_arg_tys (clas,tys) = not $ isEmptyVarSet $
299 tyVarsOfTypes tys `intersectVarSet` arg_tyvars
301 get_strictness (Banged _) = MarkedStrict
302 get_strictness (Unbanged _) = NotMarkedStrict
304 get_pty (Banged ty) = ty
305 get_pty (Unbanged ty) = ty
314 = hsep [ptext SLIT("In the type declaration for"), quotes (ppr tycon_name)]
316 tyDataCtxt tycon_name
317 = hsep [ptext SLIT("In the data declaration for"), quotes (ppr tycon_name)]
320 = hsep [ptext SLIT("In the newtype declaration for"), quotes (ppr tycon_name)]
322 fieldTypeMisMatch field_name
323 = sep [ptext SLIT("Declared types differ for field"), quotes (ppr field_name)]
325 newTypeUnboxedField ty
326 = sep [ptext SLIT("Newtype constructor field has an unboxed type:"),
330 = ptext SLIT("Can't combine named fields with locally-quantified type variables")
332 (ptext SLIT("In the declaration of data constructor") <+> ppr name)