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(..),
14 getBangType, getBangStrictness, conDetailsTys
16 import RnHsSyn ( RenamedTyClDecl, RenamedConDecl, RenamedContext )
17 import BasicTypes ( NewOrData(..), RecFlag, isRec )
19 import TcMonoType ( tcHsRecType, tcHsTyVars, tcRecTheta,
20 kcHsContext, kcHsSigType, kcHsLiftedSigType
22 import TcEnv ( tcExtendTyVarEnv,
23 tcLookupTyCon, tcLookupRecId,
24 TyThingDetails(..), RecTcEnv
28 import DataCon ( DataCon, mkDataCon, dataConFieldLabels, dataConRepType )
29 import MkId ( mkDataConId, mkDataConWrapId, mkRecordSelId )
32 import Name ( Name, NamedThing(..) )
34 import TyCon ( TyCon, isNewTyCon, tyConTyVars )
35 import Type ( tyVarsOfTypes, tyVarsOfPred, splitFunTy, applyTys,
36 mkTyConApp, mkTyVarTys, mkForAllTys,
37 splitAlgTyConApp_maybe, Type, ThetaType
39 import TysWiredIn ( unitTy )
40 import VarSet ( intersectVarSet, isEmptyVarSet )
41 import PrelNames ( unpackCStringName, unpackCStringUtf8Name )
42 import ListSetOps ( equivClasses )
45 %************************************************************************
47 \subsection{Type checking}
49 %************************************************************************
52 tcTyDecl1 :: RecFlag -> RecTcEnv -> RenamedTyClDecl -> TcM (Name, TyThingDetails)
53 tcTyDecl1 is_rec unf_env (TySynonym {tcdName = tycon_name, tcdSynRhs = rhs})
54 = tcLookupTyCon tycon_name `thenNF_Tc` \ tycon ->
55 tcExtendTyVarEnv (tyConTyVars tycon) $
56 tcHsRecType is_rec rhs `thenTc` \ rhs_ty ->
57 -- Note tcHsRecType not tcHsRecSigType; we allow type synonyms
58 -- that aren't types; e.g. type List = []
60 -- If the RHS mentions tyvars that aren't in scope, we'll
61 -- quantify over them:
63 -- will become type T = forall a. a->a
65 -- With gla-exts that's right, but for H98 we should complain.
66 -- We can now do that here without falling into
67 -- a black hole, we still do it in rnDecl (TySynonym case)
69 returnTc (tycon_name, SynTyDetails rhs_ty)
71 tcTyDecl1 is_rec unf_env (TyData {tcdND = new_or_data, tcdCtxt = context,
72 tcdName = tycon_name, tcdCons = con_decls})
73 = tcLookupTyCon tycon_name `thenNF_Tc` \ tycon ->
75 tyvars = tyConTyVars tycon
77 tcExtendTyVarEnv tyvars $
79 -- Typecheck the pieces
80 tcRecTheta is_rec context `thenTc` \ ctxt ->
81 mapTc (tcConDecl is_rec new_or_data tycon tyvars ctxt) con_decls `thenTc` \ data_cons ->
82 tcRecordSelectors is_rec unf_env tycon data_cons `thenTc` \ sel_ids ->
83 returnTc (tycon_name, DataTyDetails ctxt data_cons sel_ids)
87 mkNewTyConRep :: TyCon -> Type
88 -- Find the representation type for this newtype TyCon
89 -- The trick is to to deal correctly with recursive newtypes
90 -- such as newtype T = MkT T
93 = mkForAllTys tvs (loop [] (mkTyConApp tc (mkTyVarTys tvs)))
96 loop tcs ty = case splitAlgTyConApp_maybe ty of {
98 Just (tc, tys, data_cons) | not (isNewTyCon tc) -> ty
99 | tc `elem` tcs -> unitTy
102 case splitFunTy (applyTys (dataConRepType (head data_cons)) tys) of
103 (rep_ty, _) -> loop (tc:tcs) rep_ty
108 %************************************************************************
110 \subsection{Kind and type check constructors}
112 %************************************************************************
115 kcConDetails :: NewOrData -> RenamedContext -> ConDetails Name -> TcM ()
116 kcConDetails new_or_data ex_ctxt details
117 = kcHsContext ex_ctxt `thenTc_`
118 mapTc_ kc_sig_type (conDetailsTys details)
120 kc_sig_type = case new_or_data of
121 DataType -> kcHsSigType
122 NewType -> kcHsLiftedSigType
123 -- Can't allow an unlifted type here, because we're effectively
124 -- going to remove the constructor while coercing it to a lifted type.
127 tcConDecl :: RecFlag -> NewOrData -> TyCon -> [TyVar] -> ThetaType -> RenamedConDecl -> TcM DataCon
129 tcConDecl is_rec new_or_data tycon tyvars ctxt (ConDecl name wkr_name ex_tvs ex_ctxt details src_loc)
130 = tcAddSrcLoc src_loc $
131 tcHsTyVars ex_tvs (kcConDetails new_or_data ex_ctxt details) $ \ ex_tyvars ->
132 tcRecTheta is_rec ex_ctxt `thenTc` \ ex_theta ->
134 VanillaCon btys -> tc_datacon ex_tyvars ex_theta btys
135 InfixCon bty1 bty2 -> tc_datacon ex_tyvars ex_theta [bty1,bty2]
136 RecCon fields -> tc_rec_con ex_tyvars ex_theta fields
138 tc_datacon ex_tyvars ex_theta btys
140 arg_stricts = map getBangStrictness btys
141 tys = map getBangType btys
143 mapTc (tcHsRecType is_rec) tys `thenTc` \ arg_tys ->
144 mk_data_con ex_tyvars ex_theta arg_stricts arg_tys []
146 tc_rec_con ex_tyvars ex_theta fields
147 = checkTc (null ex_tyvars) (exRecConErr name) `thenTc_`
148 mapTc tc_field (fields `zip` allFieldLabelTags) `thenTc` \ field_labels_s ->
150 field_labels = concat field_labels_s
151 arg_stricts = [str | (ns, bty) <- fields,
152 let str = getBangStrictness bty,
153 n <- ns -- One for each. E.g x,y,z :: !Int
156 mk_data_con ex_tyvars ex_theta arg_stricts
157 (map fieldLabelType field_labels) field_labels
159 tc_field ((field_label_names, bty), tag)
160 = tcHsRecType is_rec (getBangType bty) `thenTc` \ field_ty ->
161 returnTc [mkFieldLabel (getName name) tycon field_ty tag | name <- field_label_names]
163 mk_data_con ex_tyvars ex_theta arg_stricts arg_tys fields
165 data_con = mkDataCon name arg_stricts fields
166 tyvars (thinContext arg_tys ctxt)
169 tycon data_con_id data_con_wrap_id
171 data_con_id = mkDataConId wkr_name data_con
172 data_con_wrap_id = mkDataConWrapId data_con
176 -- The context for a data constructor should be limited to
177 -- the type variables mentioned in the arg_tys
178 thinContext arg_tys ctxt
179 = filter in_arg_tys ctxt
181 arg_tyvars = tyVarsOfTypes arg_tys
182 in_arg_tys pred = not $ isEmptyVarSet $
183 tyVarsOfPred pred `intersectVarSet` arg_tyvars
187 %************************************************************************
189 \subsection{Record selectors}
191 %************************************************************************
194 tcRecordSelectors is_rec unf_env tycon data_cons
195 -- Omit the check that the fields have consistent types if
196 -- the group is recursive; TcTyClsDecls.tcGroup will repeat
197 -- with NonRecursive once we have tied the knot
198 | isRec is_rec = returnTc sel_ids
199 | otherwise = mapTc check groups `thenTc_`
202 fields = [ field | con <- data_cons
203 , field <- dataConFieldLabels con ]
205 -- groups is list of fields that share a common name
206 groups = equivClasses cmp_name fields
207 cmp_name field1 field2 = fieldLabelName field1 `compare` fieldLabelName field2
209 sel_ids = [ mkRecordSelId tycon field unpack_id unpackUtf8_id
210 | (field : _) <- groups ]
212 check fields@(first_field_label : other_fields)
213 -- These fields all have the same name, but are from
214 -- different constructors in the data type
215 = -- Check that all the fields in the group have the same type
216 -- NB: this check assumes that all the constructors of a given
217 -- data type use the same type variables
218 checkTc (all (== field_ty) other_tys) (fieldTypeMisMatch field_name)
220 field_ty = fieldLabelType first_field_label
221 field_name = fieldLabelName first_field_label
222 other_tys = map fieldLabelType other_fields
224 unpack_id = tcLookupRecId unf_env unpackCStringName
225 unpackUtf8_id = tcLookupRecId unf_env unpackCStringUtf8Name
230 %************************************************************************
232 \subsection{Errors and contexts}
234 %************************************************************************
238 fieldTypeMisMatch field_name
239 = sep [ptext SLIT("Different constructors give different types for field"), quotes (ppr field_name)]
242 = ptext SLIT("Can't combine named fields with locally-quantified type variables")
244 (ptext SLIT("In the declaration of data constructor") <+> ppr name)