2 % (c) The AQUA Project, Glasgow University, 1996-1998
4 \section[TcTyDecls]{Typecheck type declarations}
8 tcTyDecl1, kcConDetails, mkNewTyConRep
11 #include "HsVersions.h"
13 import HsSyn ( TyClDecl(..), ConDecl(..), ConDetails(..), BangType(..),
14 getBangType, conDetailsTys
16 import RnHsSyn ( RenamedTyClDecl, RenamedConDecl, RenamedContext )
17 import BasicTypes ( NewOrData(..), RecFlag, isRec )
19 import TcMonoType ( tcHsRecType, tcHsTyVars, tcRecClassContext,
20 kcHsContext, kcHsSigType, kcHsBoxedSigType
22 import TcEnv ( tcExtendTyVarEnv,
23 tcLookupTyCon, tcLookupRecId,
24 TyThingDetails(..), RecTcEnv
28 import Class ( ClassContext )
29 import DataCon ( DataCon, mkDataCon, dataConFieldLabels, markedStrict,
30 notMarkedStrict, markedUnboxed, dataConRepType
32 import MkId ( mkDataConId, mkDataConWrapId, mkRecordSelId )
35 import Name ( Name, NamedThing(..) )
37 import TyCon ( TyCon, isNewTyCon, tyConTyVars )
38 import Type ( tyVarsOfTypes, splitFunTy, applyTys,
39 mkTyConApp, mkTyVarTys, mkForAllTys,
40 splitAlgTyConApp_maybe, Type
42 import TysWiredIn ( unitTy )
43 import VarSet ( intersectVarSet, isEmptyVarSet )
44 import PrelNames ( unpackCStringName, unpackCStringUtf8Name )
45 import ListSetOps ( equivClasses )
48 %************************************************************************
50 \subsection{Type checking}
52 %************************************************************************
55 tcTyDecl1 :: RecFlag -> RecTcEnv -> RenamedTyClDecl -> TcM (Name, TyThingDetails)
56 tcTyDecl1 is_rec unf_env (TySynonym tycon_name tyvar_names rhs src_loc)
57 = tcLookupTyCon tycon_name `thenNF_Tc` \ tycon ->
58 tcExtendTyVarEnv (tyConTyVars tycon) $
59 tcHsRecType is_rec rhs `thenTc` \ rhs_ty ->
60 -- Note tcHsRecType not tcHsRecSigType; we allow type synonyms
61 -- that aren't types; e.g. type List = []
63 -- If the RHS mentions tyvars that aren't in scope, we'll
64 -- quantify over them:
66 -- will become type T = forall a. a->a
68 -- With gla-exts that's right, but for H98 we should complain.
69 -- We can now do that here without falling into
70 -- a black hole, we still do it in rnDecl (TySynonym case)
72 returnTc (tycon_name, SynTyDetails rhs_ty)
74 tcTyDecl1 is_rec unf_env (TyData new_or_data context tycon_name _ con_decls _ derivings src_loc name1 name2)
75 = tcLookupTyCon tycon_name `thenNF_Tc` \ tycon ->
77 tyvars = tyConTyVars tycon
79 tcExtendTyVarEnv tyvars $
81 -- Typecheck the pieces
82 tcRecClassContext is_rec context `thenTc` \ ctxt ->
83 mapTc (tcConDecl is_rec new_or_data tycon tyvars ctxt) con_decls `thenTc` \ data_cons ->
84 tcRecordSelectors is_rec unf_env tycon data_cons `thenTc` \ sel_ids ->
85 returnTc (tycon_name, DataTyDetails ctxt data_cons sel_ids)
89 mkNewTyConRep :: TyCon -> Type
90 -- Find the representation type for this newtype TyCon
91 -- The trick is to to deal correctly with recursive newtypes
92 -- such as newtype T = MkT T
95 = mkForAllTys tvs (loop [] (mkTyConApp tc (mkTyVarTys tvs)))
98 loop tcs ty = case splitAlgTyConApp_maybe ty of {
100 Just (tc, tys, data_cons) | not (isNewTyCon tc) -> ty
101 | tc `elem` tcs -> unitTy
104 case splitFunTy (applyTys (dataConRepType (head data_cons)) tys) of
105 (rep_ty, _) -> loop (tc:tcs) rep_ty
110 %************************************************************************
112 \subsection{Kind and type check constructors}
114 %************************************************************************
117 kcConDetails :: NewOrData -> RenamedContext -> ConDetails Name -> TcM ()
118 kcConDetails new_or_data ex_ctxt details
119 = kcHsContext ex_ctxt `thenTc_`
120 mapTc_ kc_sig_type (conDetailsTys details)
122 kc_sig_type = case new_or_data of
123 DataType -> kcHsSigType
124 NewType -> kcHsBoxedSigType
125 -- Can't allow an unboxed type here, because we're effectively
126 -- going to remove the constructor while coercing it to a boxed type.
129 tcConDecl :: RecFlag -> NewOrData -> TyCon -> [TyVar] -> ClassContext -> RenamedConDecl -> TcM DataCon
131 tcConDecl is_rec new_or_data tycon tyvars ctxt (ConDecl name wkr_name ex_tvs ex_ctxt details src_loc)
132 = tcAddSrcLoc src_loc $
133 tcHsTyVars ex_tvs (kcConDetails new_or_data ex_ctxt details) $ \ ex_tyvars ->
134 tcRecClassContext is_rec ex_ctxt `thenTc` \ ex_theta ->
136 VanillaCon btys -> tc_datacon ex_tyvars ex_theta btys
137 InfixCon bty1 bty2 -> tc_datacon ex_tyvars ex_theta [bty1,bty2]
138 RecCon fields -> tc_rec_con ex_tyvars ex_theta fields
140 tc_datacon ex_tyvars ex_theta btys
142 arg_stricts = map getBangStrictness btys
143 tys = map getBangType btys
145 mapTc (tcHsRecType is_rec) tys `thenTc` \ arg_tys ->
146 mk_data_con ex_tyvars ex_theta arg_stricts arg_tys []
148 tc_rec_con ex_tyvars ex_theta fields
149 = checkTc (null ex_tyvars) (exRecConErr name) `thenTc_`
150 mapTc tc_field (fields `zip` allFieldLabelTags) `thenTc` \ field_labels_s ->
152 field_labels = concat field_labels_s
153 arg_stricts = [str | (ns, bty) <- fields,
154 let str = getBangStrictness bty,
155 n <- ns -- One for each. E.g x,y,z :: !Int
158 mk_data_con ex_tyvars ex_theta arg_stricts
159 (map fieldLabelType field_labels) field_labels
161 tc_field ((field_label_names, bty), tag)
162 = tcHsRecType is_rec (getBangType bty) `thenTc` \ field_ty ->
163 returnTc [mkFieldLabel (getName name) tycon field_ty tag | name <- field_label_names]
165 mk_data_con ex_tyvars ex_theta arg_stricts arg_tys fields
167 data_con = mkDataCon name arg_stricts fields
168 tyvars (thinContext arg_tys ctxt)
171 tycon data_con_id data_con_wrap_id
173 data_con_id = mkDataConId wkr_name data_con
174 data_con_wrap_id = mkDataConWrapId data_con
178 -- The context for a data constructor should be limited to
179 -- the type variables mentioned in the arg_tys
180 thinContext arg_tys ctxt
181 = filter in_arg_tys ctxt
183 arg_tyvars = tyVarsOfTypes arg_tys
184 in_arg_tys (clas,tys) = not $ isEmptyVarSet $
185 tyVarsOfTypes tys `intersectVarSet` arg_tyvars
187 getBangStrictness (Banged _) = markedStrict
188 getBangStrictness (Unbanged _) = notMarkedStrict
189 getBangStrictness (Unpacked _) = markedUnboxed
193 %************************************************************************
195 \subsection{Record selectors}
197 %************************************************************************
200 tcRecordSelectors is_rec unf_env tycon data_cons
201 -- Omit the check that the fields have consistent types if
202 -- the group is recursive; TcTyClsDecls.tcGroup will repeat
203 -- with NonRecursive once we have tied the knot
204 | isRec is_rec = returnTc sel_ids
205 | otherwise = mapTc check groups `thenTc_`
208 fields = [ field | con <- data_cons
209 , field <- dataConFieldLabels con ]
211 -- groups is list of fields that share a common name
212 groups = equivClasses cmp_name fields
213 cmp_name field1 field2 = fieldLabelName field1 `compare` fieldLabelName field2
215 sel_ids = [ mkRecordSelId tycon field unpack_id unpackUtf8_id
216 | (field : _) <- groups ]
218 check fields@(first_field_label : other_fields)
219 -- These fields all have the same name, but are from
220 -- different constructors in the data type
221 = -- Check that all the fields in the group have the same type
222 -- NB: this check assumes that all the constructors of a given
223 -- data type use the same type variables
224 checkTc (all (== field_ty) other_tys) (fieldTypeMisMatch field_name)
226 field_ty = fieldLabelType first_field_label
227 field_name = fieldLabelName first_field_label
228 other_tys = map fieldLabelType other_fields
230 unpack_id = tcLookupRecId unf_env unpackCStringName
231 unpackUtf8_id = tcLookupRecId unf_env unpackCStringUtf8Name
236 %************************************************************************
238 \subsection{Errors and contexts}
240 %************************************************************************
244 fieldTypeMisMatch field_name
245 = sep [ptext SLIT("Different constructors give different types for field"), quotes (ppr field_name)]
248 = ptext SLIT("Can't combine named fields with locally-quantified type variables")
250 (ptext SLIT("In the declaration of data constructor") <+> ppr name)