2 % (c) The AQUA Project, Glasgow University, 1996-1998
4 \section[TcTyDecls]{Typecheck type declarations}
10 mkImplicitDataBinds, mkNewTyConRep
13 #include "HsVersions.h"
15 import HsSyn ( MonoBinds(..),
16 TyClDecl(..), ConDecl(..), ConDetails(..), BangType(..),
17 andMonoBindList, getBangType
19 import RnHsSyn ( RenamedTyClDecl, RenamedConDecl, RenamedContext )
20 import TcHsSyn ( TcMonoBinds, idsToMonoBinds )
21 import BasicTypes ( RecFlag(..), NewOrData(..) )
23 import TcMonoType ( tcHsType, tcHsSigType, tcHsBoxedSigType, kcTyVarScope, tcClassContext,
24 kcHsContext, kcHsSigType, mkImmutTyVars
26 import TcEnv ( tcExtendTyVarEnv, tcLookupTy, tcLookupValueByKey, TyThing(..), TyThingDetails(..) )
28 import TcUnify ( unifyKind )
30 import Class ( Class, ClassContext )
31 import DataCon ( DataCon, mkDataCon,
32 dataConFieldLabels, dataConId, dataConWrapId,
33 markedStrict, notMarkedStrict, markedUnboxed, dataConRepType
35 import MkId ( mkDataConId, mkDataConWrapId, mkRecordSelId )
37 import Var ( Id, TyVar )
38 import Name ( Name, isLocallyDefined, OccName, NamedThing(..), nameUnique )
40 import TyCon ( TyCon, AlgTyConFlavour(..), ArgVrcs, mkSynTyCon, mkAlgTyCon,
41 tyConDataConsIfAvailable, tyConTyVars,
42 isSynTyCon, isNewTyCon
44 import Type ( getTyVar, tyVarsOfTypes, splitFunTy, applyTys,
45 mkTyConApp, mkTyVarTys, mkForAllTys, mkFunTy,
46 mkTyVarTy, splitAlgTyConApp_maybe,
47 mkArrowKind, mkArrowKinds, boxedTypeKind,
48 isUnboxedType, Type, ThetaType, classesOfPreds
50 import TysWiredIn ( unitTy )
51 import Var ( tyVarKind )
52 import VarSet ( intersectVarSet, isEmptyVarSet )
53 import Unique ( unpackCStringIdKey, unpackCStringUtf8IdKey )
54 import Util ( equivClasses )
55 import FiniteMap ( FiniteMap, lookupWithDefaultFM )
58 %************************************************************************
60 \subsection{Type checking}
62 %************************************************************************
65 tcTyDecl1 :: RenamedTyClDecl -> TcM s (Name, TyThingDetails)
66 tcTyDecl1 (TySynonym tycon_name tyvar_names rhs src_loc)
67 = tcLookupTy tycon_name `thenNF_Tc` \ (ATyCon tycon) ->
68 tcExtendTyVarEnv (tyConTyVars tycon) $
69 tcHsType rhs `thenTc` \ rhs_ty ->
70 -- Note tcHsType not tcHsSigType; we allow type synonyms
71 -- that aren't types; e.g. type List = []
73 -- If the RHS mentions tyvars that aren't in scope, we'll
74 -- quantify over them:
76 -- will become type T = forall a. a->a
78 -- With gla-exts that's right, but for H98 we should complain.
79 -- We can now do that here without falling into
80 -- a black hole, we still do it in rnDecl (TySynonym case)
82 returnTc (tycon_name, SynTyDetails rhs_ty)
84 tcTyDecl1 (TyData new_or_data context tycon_name _ con_decls _ derivings _ src_loc)
85 = tcLookupTy tycon_name `thenNF_Tc` \ (ATyCon tycon) ->
87 tyvars = tyConTyVars tycon
89 tcExtendTyVarEnv tyvars $
91 -- Typecheck the pieces
92 tcClassContext context `thenTc` \ ctxt ->
93 tc_derivs derivings `thenTc` \ derived_classes ->
94 mapTc (tcConDecl new_or_data tycon tyvars ctxt) con_decls `thenTc` \ data_cons ->
96 returnTc (tycon_name, DataTyDetails ctxt data_cons derived_classes)
98 tc_derivs Nothing = returnTc []
99 tc_derivs (Just ds) = mapTc tc_deriv ds
101 tc_deriv name = tcLookupTy name `thenTc` \ (AClass clas) ->
106 mkNewTyConRep :: TyCon -> Type
107 -- Find the representation type for this newtype TyCon
108 -- The trick is to to deal correctly with recursive newtypes
109 -- such as newtype T = MkT T
112 = mkForAllTys tvs (loop [] (mkTyConApp tc (mkTyVarTys tvs)))
115 loop tcs ty = case splitAlgTyConApp_maybe ty of {
117 Just (tc, tys, data_cons) | not (isNewTyCon tc) -> ty
118 | tc `elem` tcs -> unitTy
121 case splitFunTy (applyTys (dataConRepType (head data_cons)) tys) of
122 (rep_ty, _) -> loop (tc:tcs) rep_ty
127 %************************************************************************
129 \subsection{Kind and type check constructors}
131 %************************************************************************
134 kcConDetails :: RenamedContext -> ConDetails Name -> TcM s ()
135 kcConDetails ex_ctxt details
136 = kcHsContext ex_ctxt `thenTc_`
137 kc_con_details details
139 kc_con_details (VanillaCon btys) = mapTc_ kc_bty btys
140 kc_con_details (InfixCon bty1 bty2) = mapTc_ kc_bty [bty1,bty2]
141 kc_con_details (RecCon flds) = mapTc_ kc_field flds
143 kc_field (_, bty) = kc_bty bty
145 kc_bty bty = kcHsSigType (getBangType bty)
147 tcConDecl :: NewOrData -> TyCon -> [TyVar] -> ClassContext -> RenamedConDecl -> TcM s DataCon
149 tcConDecl new_or_data tycon tyvars ctxt (ConDecl name wkr_name ex_tvs ex_ctxt details src_loc)
150 = tcAddSrcLoc src_loc $
151 kcTyVarScope ex_tvs (kcConDetails ex_ctxt details) `thenTc` \ ex_tv_kinds ->
153 ex_tyvars = mkImmutTyVars ex_tv_kinds
155 tcExtendTyVarEnv ex_tyvars $
156 tcClassContext ex_ctxt `thenTc` \ ex_theta ->
158 VanillaCon btys -> tc_datacon ex_tyvars ex_theta btys
159 InfixCon bty1 bty2 -> tc_datacon ex_tyvars ex_theta [bty1,bty2]
160 RecCon fields -> tc_rec_con ex_tyvars ex_theta fields
162 tc_sig_type = case new_or_data of
163 DataType -> tcHsSigType
164 NewType -> tcHsBoxedSigType
165 -- Can't allow an unboxed type here, because we're effectively
166 -- going to remove the constructor while coercing it to a boxed type.
168 tc_datacon ex_tyvars ex_theta btys
170 arg_stricts = map getBangStrictness btys
171 tys = map getBangType btys
173 mapTc tc_sig_type tys `thenTc` \ arg_tys ->
174 mk_data_con ex_tyvars ex_theta arg_stricts arg_tys []
176 tc_rec_con ex_tyvars ex_theta fields
177 = checkTc (null ex_tyvars) (exRecConErr name) `thenTc_`
178 mapTc tc_field (fields `zip` allFieldLabelTags) `thenTc` \ field_labels_s ->
180 field_labels = concat field_labels_s
181 arg_stricts = [str | (ns, bty) <- fields,
182 let str = getBangStrictness bty,
183 n <- ns -- One for each. E.g x,y,z :: !Int
186 mk_data_con ex_tyvars ex_theta arg_stricts
187 (map fieldLabelType field_labels) field_labels
189 tc_field ((field_label_names, bty), tag)
190 = tc_sig_type (getBangType bty) `thenTc` \ field_ty ->
191 returnTc [mkFieldLabel (getName name) tycon field_ty tag | name <- field_label_names]
193 mk_data_con ex_tyvars ex_theta arg_stricts arg_tys fields
195 data_con = mkDataCon name arg_stricts fields
196 tyvars (thinContext arg_tys ctxt)
199 tycon data_con_id data_con_wrap_id
201 data_con_id = mkDataConId wkr_name data_con
202 data_con_wrap_id = mkDataConWrapId data_con
206 -- The context for a data constructor should be limited to
207 -- the type variables mentioned in the arg_tys
208 thinContext arg_tys ctxt
209 = filter in_arg_tys ctxt
211 arg_tyvars = tyVarsOfTypes arg_tys
212 in_arg_tys (clas,tys) = not $ isEmptyVarSet $
213 tyVarsOfTypes tys `intersectVarSet` arg_tyvars
215 getBangStrictness (Banged _) = markedStrict
216 getBangStrictness (Unbanged _) = notMarkedStrict
217 getBangStrictness (Unpacked _) = markedUnboxed
222 %************************************************************************
224 \subsection{Generating constructor/selector bindings for data declarations}
226 %************************************************************************
229 mkImplicitDataBinds :: [TyCon] -> TcM s ([Id], TcMonoBinds)
230 mkImplicitDataBinds [] = returnTc ([], EmptyMonoBinds)
231 mkImplicitDataBinds (tycon : tycons)
232 | isSynTyCon tycon = mkImplicitDataBinds tycons
233 | otherwise = mkImplicitDataBinds_one tycon `thenTc` \ (ids1, b1) ->
234 mkImplicitDataBinds tycons `thenTc` \ (ids2, b2) ->
235 returnTc (ids1++ids2, b1 `AndMonoBinds` b2)
237 mkImplicitDataBinds_one tycon
238 = mapTc (mkRecordSelector tycon) groups `thenTc` \ sel_ids ->
240 unf_ids = sel_ids ++ data_con_wrapper_ids
241 all_ids = map dataConId data_cons ++ unf_ids
243 -- For the locally-defined things
244 -- we need to turn the unfoldings inside the selector Ids into bindings,
245 -- and build bindigns for the constructor wrappers
246 binds | isLocallyDefined tycon = idsToMonoBinds unf_ids
247 | otherwise = EmptyMonoBinds
249 returnTc (all_ids, binds)
251 data_cons = tyConDataConsIfAvailable tycon
252 -- Abstract types mean we don't bring the
253 -- data cons into scope, which should be fine
255 data_con_wrapper_ids = map dataConWrapId data_cons
257 fields = [ (con, field) | con <- data_cons,
258 field <- dataConFieldLabels con
261 -- groups is list of fields that share a common name
262 groups = equivClasses cmp_name fields
263 cmp_name (_, field1) (_, field2)
264 = fieldLabelName field1 `compare` fieldLabelName field2
268 mkRecordSelector tycon fields@((first_con, first_field_label) : other_fields)
269 -- These fields all have the same name, but are from
270 -- different constructors in the data type
271 -- Check that all the fields in the group have the same type
272 -- This check assumes that all the constructors of a given
273 -- data type use the same type variables
274 = checkTc (all (== field_ty) other_tys)
275 (fieldTypeMisMatch field_name) `thenTc_`
276 tcLookupValueByKey unpackCStringIdKey `thenTc` \ unpack_id ->
277 tcLookupValueByKey unpackCStringUtf8IdKey `thenTc` \ unpackUtf8_id ->
278 returnTc (mkRecordSelId tycon first_field_label unpack_id unpackUtf8_id)
280 field_ty = fieldLabelType first_field_label
281 field_name = fieldLabelName first_field_label
282 other_tys = [fieldLabelType fl | (_, fl) <- other_fields]
289 fieldTypeMisMatch field_name
290 = sep [ptext SLIT("Declared types differ for field"), quotes (ppr field_name)]
293 = ptext SLIT("Can't combine named fields with locally-quantified type variables")
295 (ptext SLIT("In the declaration of data constructor") <+> ppr name)