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, idsToMonoBinds )
21 import BasicTypes ( RecFlag(..), NewOrData(..) )
23 import TcMonoType ( tcExtendTopTyVarScope, tcExtendTyVarScope,
24 tcHsTypeKind, kcHsType, 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, dataConWrapId,
35 markedStrict, notMarkedStrict, markedUnboxed
37 import MkId ( mkDataConId, mkDataConWrapId, mkRecordSelId )
39 import Var ( Id, TyVar )
40 import Name ( Name, isLocallyDefined, OccName, NamedThing(..), nameUnique )
42 import TyCon ( TyCon, ArgVrcs, mkSynTyCon, mkAlgTyCon,
43 isSynTyCon, tyConDataCons, isNewTyCon
45 import Type ( getTyVar, tyVarsOfTypes,
46 mkTyConApp, mkTyVarTys, mkForAllTys, mkFunTy,
47 mkTyVarTy, splitForAllTys, isForAllTy,
48 mkArrowKind, mkArrowKinds, boxedTypeKind,
49 isUnboxedType, Type, ThetaType, classesOfPreds
51 import Var ( tyVarKind )
52 import VarSet ( intersectVarSet, isEmptyVarSet )
53 import Util ( equivClasses )
54 import FiniteMap ( FiniteMap, lookupWithDefaultFM )
55 import CmdLineOpts ( opt_GlasgowExts )
58 %************************************************************************
60 \subsection{Kind checking}
62 %************************************************************************
65 kcTyDecl :: RenamedTyClDecl -> TcM s ()
67 kcTyDecl (TySynonym name tyvar_names rhs src_loc)
68 = tcLookupTy name `thenNF_Tc` \ (kind, _, _) ->
69 tcExtendTopTyVarScope kind tyvar_names $ \ _ result_kind ->
70 tcHsTypeKind rhs `thenTc` \ (rhs_kind, _) ->
71 unifyKind result_kind rhs_kind
73 kcTyDecl (TyData _ context tycon_name tyvar_names con_decls _ _ src_loc)
74 = tcLookupTy tycon_name `thenNF_Tc` \ (kind, _, _) ->
75 tcExtendTopTyVarScope kind tyvar_names $ \ result_kind _ ->
76 tcContext context `thenTc_`
77 mapTc kcConDecl con_decls `thenTc_`
80 kcConDecl (ConDecl _ _ ex_tvs ex_ctxt details loc)
82 tcExtendTyVarScope ex_tvs ( \ tyvars ->
83 tcContext ex_ctxt `thenTc_`
84 kc_con details `thenTc_`
88 kc_con (VanillaCon btys) = mapTc kc_bty btys `thenTc_` returnTc ()
89 kc_con (InfixCon bty1 bty2) = mapTc kc_bty [bty1,bty2] `thenTc_` returnTc ()
90 kc_con (NewCon ty _) = kcHsType ty
91 kc_con (RecCon flds) = mapTc kc_field flds `thenTc_` returnTc ()
93 kc_bty (Banged ty) = kcHsType ty
94 kc_bty (Unbanged ty) = kcHsType ty
95 kc_bty (Unpacked ty) = kcHsType ty
97 kc_field (_, bty) = kc_bty bty
101 %************************************************************************
103 \subsection{Type checking}
105 %************************************************************************
108 tcTyDecl :: RecFlag -> FiniteMap Name ArgVrcs -> RenamedTyClDecl -> TcM s TyCon
110 tcTyDecl is_rec rec_vrcs (TySynonym tycon_name tyvar_names rhs src_loc)
111 = tcLookupTy tycon_name `thenNF_Tc` \ (tycon_kind, Just arity, _) ->
112 tcExtendTopTyVarScope tycon_kind tyvar_names $ \ tyvars _ ->
113 tcHsTopTypeKind rhs `thenTc` \ (_, rhs_ty) ->
114 -- If the RHS mentions tyvars that aren't in scope, we'll
115 -- quantify over them. With gla-exts that's right, but for H98
116 -- we should complain. We can't do that here without falling into
117 -- a black hole, so we do it in rnDecl (TySynonym case)
119 -- Construct the tycon
120 argvrcs = lookupWithDefaultFM rec_vrcs (pprPanic "tcTyDecl: argvrcs:" $ ppr tycon_name)
122 tycon = mkSynTyCon tycon_name tycon_kind arity tyvars rhs_ty argvrcs
127 tcTyDecl is_rec rec_vrcs (TyData data_or_new context tycon_name tyvar_names con_decls derivings pragmas src_loc)
128 = -- Lookup the pieces
129 tcLookupTy tycon_name `thenNF_Tc` \ (tycon_kind, _, ATyCon rec_tycon) ->
130 tcExtendTopTyVarScope tycon_kind tyvar_names $ \ tyvars _ ->
132 -- Typecheck the pieces
133 tcContext context `thenTc` \ ctxt ->
134 let ctxt' = classesOfPreds ctxt in
135 mapTc (tcConDecl rec_tycon tyvars ctxt') con_decls `thenTc` \ data_cons ->
136 tc_derivs derivings `thenTc` \ derived_classes ->
139 -- Construct the tycon
140 real_data_or_new = case data_or_new of
142 DataType | all isNullaryDataCon data_cons -> EnumType
143 | otherwise -> DataType
145 argvrcs = lookupWithDefaultFM rec_vrcs (pprPanic "tcTyDecl: argvrcs:" $ ppr tycon_name)
148 tycon = mkAlgTyCon tycon_name tycon_kind tyvars ctxt' argvrcs
151 Nothing -- Not a dictionary
152 real_data_or_new is_rec
156 tc_derivs Nothing = returnTc []
157 tc_derivs (Just ds) = mapTc tc_deriv ds
159 tc_deriv name = tcLookupTy name `thenTc` \ (_, _, AClass clas) ->
164 %************************************************************************
166 \subsection{Type check constructors}
168 %************************************************************************
171 tcConDecl :: TyCon -> [TyVar] -> [(Class,[Type])] -> RenamedConDecl -> TcM s DataCon
173 tcConDecl tycon tyvars ctxt (ConDecl name wkr_name ex_tvs ex_ctxt details src_loc)
174 = tcAddSrcLoc src_loc $
175 tcExtendTyVarScope ex_tvs $ \ ex_tyvars ->
176 tcContext ex_ctxt `thenTc` \ ex_theta ->
178 ex_ctxt' = classesOfPreds ex_theta
180 tc_con_decl_help tycon tyvars ctxt name wkr_name ex_tyvars ex_ctxt' details
182 tc_con_decl_help tycon tyvars ctxt name wkr_name ex_tyvars ex_theta details
184 VanillaCon btys -> tc_datacon btys
185 InfixCon bty1 bty2 -> tc_datacon [bty1,bty2]
186 NewCon ty mb_f -> tc_newcon ty mb_f
187 RecCon fields -> tc_rec_con fields
191 arg_stricts = map get_strictness btys
192 tys = map get_pty btys
194 mapTc tcHsTopType tys `thenTc` \ arg_tys ->
195 mk_data_con arg_stricts arg_tys []
198 = tcHsTopBoxedType ty `thenTc` \ arg_ty ->
199 -- can't allow an unboxed type here, because we're effectively
200 -- going to remove the constructor while coercing it to a boxed type.
205 Just f -> [mkFieldLabel (getName f) arg_ty (head allFieldLabelTags)]
207 mk_data_con [notMarkedStrict] [arg_ty] field_label
210 = checkTc (null ex_tyvars) (exRecConErr name) `thenTc_`
211 mapTc tc_field fields `thenTc` \ field_label_infos_s ->
213 field_label_infos = concat field_label_infos_s
214 arg_stricts = [strict | (_, _, strict) <- field_label_infos]
215 arg_tys = [ty | (_, ty, _) <- field_label_infos]
217 field_labels = [ mkFieldLabel (getName name) ty tag
218 | ((name, ty, _), tag) <- field_label_infos `zip` allFieldLabelTags ]
220 mk_data_con arg_stricts arg_tys field_labels
222 tc_field (field_label_names, bty)
223 = tcHsTopType (get_pty bty) `thenTc` \ field_ty ->
224 returnTc [(name, field_ty, get_strictness bty) | name <- field_label_names]
226 mk_data_con arg_stricts arg_tys fields
227 = -- Now we've checked all the field types we must
228 -- zonk the existential tyvars to finish the kind
229 -- inference on their kinds, and commit them to being
230 -- immutable type variables. (The top-level tyvars are
231 -- already fixed, by the preceding kind-inference pass.)
232 mapNF_Tc zonkTcTyVarToTyVar ex_tyvars `thenNF_Tc` \ ex_tyvars' ->
233 zonkTcClassConstraints ex_theta `thenNF_Tc` \ ex_theta' ->
235 data_con = mkDataCon name arg_stricts fields
236 tyvars (thinContext arg_tys ctxt)
239 tycon data_con_id data_con_wrap_id
240 data_con_id = mkDataConId wkr_name data_con
241 data_con_wrap_id = mkDataConWrapId data_con
245 -- The context for a data constructor should be limited to
246 -- the type variables mentioned in the arg_tys
247 thinContext arg_tys ctxt
248 = filter in_arg_tys ctxt
250 arg_tyvars = tyVarsOfTypes arg_tys
251 in_arg_tys (clas,tys) = not $ isEmptyVarSet $
252 tyVarsOfTypes tys `intersectVarSet` arg_tyvars
254 get_strictness (Banged _) = markedStrict
255 get_strictness (Unbanged _) = notMarkedStrict
256 get_strictness (Unpacked _) = markedUnboxed
258 get_pty (Banged ty) = ty
259 get_pty (Unbanged ty) = ty
260 get_pty (Unpacked ty) = ty
265 %************************************************************************
267 \subsection{Generating constructor/selector bindings for data declarations}
269 %************************************************************************
272 mkImplicitDataBinds :: [TyCon] -> TcM s ([Id], TcMonoBinds)
273 mkImplicitDataBinds [] = returnTc ([], EmptyMonoBinds)
274 mkImplicitDataBinds (tycon : tycons)
275 | isSynTyCon tycon = mkImplicitDataBinds tycons
276 | otherwise = mkImplicitDataBinds_one tycon `thenTc` \ (ids1, b1) ->
277 mkImplicitDataBinds tycons `thenTc` \ (ids2, b2) ->
278 returnTc (ids1++ids2, b1 `AndMonoBinds` b2)
280 mkImplicitDataBinds_one tycon
281 = mapTc (mkRecordSelector tycon) groups `thenTc` \ sel_ids ->
283 unf_ids = sel_ids ++ data_con_wrapper_ids
284 all_ids = map dataConId data_cons ++ unf_ids
286 -- For the locally-defined things
287 -- we need to turn the unfoldings inside the selector Ids into bindings,
288 -- and build bindigns for the constructor wrappers
289 binds | isLocallyDefined tycon = idsToMonoBinds unf_ids
290 | otherwise = EmptyMonoBinds
292 returnTc (all_ids, binds)
294 data_cons = tyConDataCons tycon
296 data_con_wrapper_ids = map dataConWrapId data_cons
298 fields = [ (con, field) | con <- data_cons,
299 field <- dataConFieldLabels con
302 -- groups is list of fields that share a common name
303 groups = equivClasses cmp_name fields
304 cmp_name (_, field1) (_, field2)
305 = fieldLabelName field1 `compare` fieldLabelName field2
309 mkRecordSelector tycon fields@((first_con, first_field_label) : other_fields)
310 -- These fields all have the same name, but are from
311 -- different constructors in the data type
312 -- Check that all the fields in the group have the same type
313 -- This check assumes that all the constructors of a given
314 -- data type use the same type variables
315 = checkTc (all (== field_ty) other_tys)
316 (fieldTypeMisMatch field_name) `thenTc_`
317 returnTc (mkRecordSelId tycon first_field_label)
319 field_ty = fieldLabelType first_field_label
320 field_name = fieldLabelName first_field_label
321 other_tys = [fieldLabelType fl | (_, fl) <- other_fields]
328 fieldTypeMisMatch field_name
329 = sep [ptext SLIT("Declared types differ for field"), quotes (ppr field_name)]
332 = ptext SLIT("Can't combine named fields with locally-quantified type variables")
334 (ptext SLIT("In the declaration of data constructor") <+> ppr name)