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 getBangType, conDetailsTys
19 import RnHsSyn ( RenamedTyClDecl, RenamedConDecl, RenamedContext )
20 import TcHsSyn ( TcMonoBinds, idsToMonoBinds )
21 import BasicTypes ( NewOrData(..), RecFlag )
23 import TcMonoType ( tcHsRecType, tcHsTyVars, tcRecClassContext,
24 kcHsContext, kcHsSigType, kcHsBoxedSigType
26 import TcEnv ( tcExtendTyVarEnv,
27 tcLookupTyCon, tcLookupGlobalId,
32 import Class ( ClassContext )
33 import DataCon ( DataCon, mkDataCon,
34 dataConFieldLabels, dataConId, dataConWrapId,
35 markedStrict, notMarkedStrict, markedUnboxed, dataConRepType
37 import MkId ( mkDataConId, mkDataConWrapId, mkRecordSelId )
39 import Var ( Id, TyVar )
40 import Module ( Module )
41 import Name ( Name, NamedThing(..), isFrom )
43 import TyCon ( TyCon, isSynTyCon, isNewTyCon,
44 tyConDataConsIfAvailable, tyConTyVars, tyConGenIds
46 import Type ( tyVarsOfTypes, splitFunTy, applyTys,
47 mkTyConApp, mkTyVarTys, mkForAllTys,
48 splitAlgTyConApp_maybe, Type
50 import TysWiredIn ( unitTy )
51 import VarSet ( intersectVarSet, isEmptyVarSet )
52 import PrelNames ( unpackCStringName, unpackCStringUtf8Name )
53 import ListSetOps ( equivClasses )
56 %************************************************************************
58 \subsection{Type checking}
60 %************************************************************************
63 tcTyDecl1 :: RecFlag -> RenamedTyClDecl -> TcM (Name, TyThingDetails)
64 tcTyDecl1 is_rec (TySynonym tycon_name tyvar_names rhs src_loc)
65 = tcLookupTyCon tycon_name `thenNF_Tc` \ tycon ->
66 tcExtendTyVarEnv (tyConTyVars tycon) $
67 tcHsRecType is_rec rhs `thenTc` \ rhs_ty ->
68 -- Note tcHsRecType not tcHsRecSigType; we allow type synonyms
69 -- that aren't types; e.g. type List = []
71 -- If the RHS mentions tyvars that aren't in scope, we'll
72 -- quantify over them:
74 -- will become type T = forall a. a->a
76 -- With gla-exts that's right, but for H98 we should complain.
77 -- We can now do that here without falling into
78 -- a black hole, we still do it in rnDecl (TySynonym case)
80 returnTc (tycon_name, SynTyDetails rhs_ty)
82 tcTyDecl1 is_rec (TyData new_or_data context tycon_name _ con_decls _ derivings src_loc name1 name2)
83 = tcLookupTyCon tycon_name `thenNF_Tc` \ tycon ->
85 tyvars = tyConTyVars tycon
87 tcExtendTyVarEnv tyvars $
89 -- Typecheck the pieces
90 tcRecClassContext is_rec context `thenTc` \ ctxt ->
91 mapTc (tcConDecl is_rec new_or_data tycon tyvars ctxt) con_decls `thenTc` \ data_cons ->
92 returnTc (tycon_name, DataTyDetails ctxt data_cons)
96 mkNewTyConRep :: TyCon -> Type
97 -- Find the representation type for this newtype TyCon
98 -- The trick is to to deal correctly with recursive newtypes
99 -- such as newtype T = MkT T
102 = mkForAllTys tvs (loop [] (mkTyConApp tc (mkTyVarTys tvs)))
105 loop tcs ty = case splitAlgTyConApp_maybe ty of {
107 Just (tc, tys, data_cons) | not (isNewTyCon tc) -> ty
108 | tc `elem` tcs -> unitTy
111 case splitFunTy (applyTys (dataConRepType (head data_cons)) tys) of
112 (rep_ty, _) -> loop (tc:tcs) rep_ty
117 %************************************************************************
119 \subsection{Kind and type check constructors}
121 %************************************************************************
124 kcConDetails :: NewOrData -> RenamedContext -> ConDetails Name -> TcM ()
125 kcConDetails new_or_data ex_ctxt details
126 = kcHsContext ex_ctxt `thenTc_`
127 mapTc_ kc_sig_type (conDetailsTys details)
129 kc_sig_type = case new_or_data of
130 DataType -> kcHsSigType
131 NewType -> kcHsBoxedSigType
132 -- Can't allow an unboxed type here, because we're effectively
133 -- going to remove the constructor while coercing it to a boxed type.
136 tcConDecl :: RecFlag -> NewOrData -> TyCon -> [TyVar] -> ClassContext -> RenamedConDecl -> TcM DataCon
138 tcConDecl is_rec new_or_data tycon tyvars ctxt (ConDecl name wkr_name ex_tvs ex_ctxt details src_loc)
139 = tcAddSrcLoc src_loc $
140 tcHsTyVars ex_tvs (kcConDetails new_or_data ex_ctxt details) $ \ ex_tyvars ->
141 tcRecClassContext is_rec ex_ctxt `thenTc` \ ex_theta ->
143 VanillaCon btys -> tc_datacon ex_tyvars ex_theta btys
144 InfixCon bty1 bty2 -> tc_datacon ex_tyvars ex_theta [bty1,bty2]
145 RecCon fields -> tc_rec_con ex_tyvars ex_theta fields
147 tc_datacon ex_tyvars ex_theta btys
149 arg_stricts = map getBangStrictness btys
150 tys = map getBangType btys
152 mapTc (tcHsRecType is_rec) tys `thenTc` \ arg_tys ->
153 mk_data_con ex_tyvars ex_theta arg_stricts arg_tys []
155 tc_rec_con ex_tyvars ex_theta fields
156 = checkTc (null ex_tyvars) (exRecConErr name) `thenTc_`
157 mapTc tc_field (fields `zip` allFieldLabelTags) `thenTc` \ field_labels_s ->
159 field_labels = concat field_labels_s
160 arg_stricts = [str | (ns, bty) <- fields,
161 let str = getBangStrictness bty,
162 n <- ns -- One for each. E.g x,y,z :: !Int
165 mk_data_con ex_tyvars ex_theta arg_stricts
166 (map fieldLabelType field_labels) field_labels
168 tc_field ((field_label_names, bty), tag)
169 = tcHsRecType is_rec (getBangType bty) `thenTc` \ field_ty ->
170 returnTc [mkFieldLabel (getName name) tycon field_ty tag | name <- field_label_names]
172 mk_data_con ex_tyvars ex_theta arg_stricts arg_tys fields
174 data_con = mkDataCon name arg_stricts fields
175 tyvars (thinContext arg_tys ctxt)
178 tycon data_con_id data_con_wrap_id
180 data_con_id = mkDataConId wkr_name data_con
181 data_con_wrap_id = mkDataConWrapId data_con
185 -- The context for a data constructor should be limited to
186 -- the type variables mentioned in the arg_tys
187 thinContext arg_tys ctxt
188 = filter in_arg_tys ctxt
190 arg_tyvars = tyVarsOfTypes arg_tys
191 in_arg_tys (clas,tys) = not $ isEmptyVarSet $
192 tyVarsOfTypes tys `intersectVarSet` arg_tyvars
194 getBangStrictness (Banged _) = markedStrict
195 getBangStrictness (Unbanged _) = notMarkedStrict
196 getBangStrictness (Unpacked _) = markedUnboxed
201 %************************************************************************
203 \subsection{Generating constructor/selector bindings for data declarations}
205 %************************************************************************
208 mkImplicitDataBinds :: Module -> [TyCon] -> TcM ([Id], TcMonoBinds)
209 mkImplicitDataBinds this_mod [] = returnTc ([], EmptyMonoBinds)
210 mkImplicitDataBinds this_mod (tycon : tycons)
211 | isSynTyCon tycon = mkImplicitDataBinds this_mod tycons
212 | otherwise = mkImplicitDataBinds_one this_mod tycon `thenTc` \ (ids1, b1) ->
213 mkImplicitDataBinds this_mod tycons `thenTc` \ (ids2, b2) ->
214 returnTc (ids1++ids2, b1 `AndMonoBinds` b2)
216 mkImplicitDataBinds_one this_mod tycon
217 = mapTc (mkRecordSelector tycon) groups `thenTc` \ sel_ids ->
219 unf_ids = sel_ids ++ data_con_wrapper_ids ++ gen_ids
220 all_ids = map dataConId data_cons ++ unf_ids
222 -- For the locally-defined things
223 -- we need to turn the unfoldings inside the selector Ids into bindings,
224 -- and build bindigns for the constructor wrappers
225 binds | isFrom this_mod tycon = idsToMonoBinds unf_ids
226 | otherwise = EmptyMonoBinds
228 returnTc (all_ids, binds)
230 data_cons = tyConDataConsIfAvailable tycon
231 -- Abstract types mean we don't bring the
232 -- data cons into scope, which should be fine
233 gen_ids = tyConGenIds tycon
234 data_con_wrapper_ids = map dataConWrapId data_cons
236 fields = [ (con, field) | con <- data_cons,
237 field <- dataConFieldLabels con
240 -- groups is list of fields that share a common name
241 groups = equivClasses cmp_name fields
242 cmp_name (_, field1) (_, field2)
243 = fieldLabelName field1 `compare` fieldLabelName field2
247 mkRecordSelector tycon fields@((first_con, first_field_label) : other_fields)
248 -- These fields all have the same name, but are from
249 -- different constructors in the data type
250 -- Check that all the fields in the group have the same type
251 -- This check assumes that all the constructors of a given
252 -- data type use the same type variables
253 = checkTc (all (== field_ty) other_tys)
254 (fieldTypeMisMatch field_name) `thenTc_`
255 tcLookupGlobalId unpackCStringName `thenTc` \ unpack_id ->
256 tcLookupGlobalId unpackCStringUtf8Name `thenTc` \ unpackUtf8_id ->
257 returnTc (mkRecordSelId tycon first_field_label unpack_id unpackUtf8_id)
259 field_ty = fieldLabelType first_field_label
260 field_name = fieldLabelName first_field_label
261 other_tys = [fieldLabelType fl | (_, fl) <- other_fields]
268 fieldTypeMisMatch field_name
269 = sep [ptext SLIT("Declared types differ for field"), quotes (ppr field_name)]
272 = ptext SLIT("Can't combine named fields with locally-quantified type variables")
274 (ptext SLIT("In the declaration of data constructor") <+> ppr name)