2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
8 buildSynTyCon, buildAlgTyCon, buildDataCon,
10 mkAbstractTyConRhs, mkOpenDataTyConRhs,
11 mkNewTyConRhs, mkDataTyConRhs
14 #include "HsVersions.h"
37 ------------------------------------------------------
38 buildSynTyCon :: Name -> [TyVar]
40 -> Maybe (TyCon, [Type]) -- family instance if applicable
43 buildSynTyCon tc_name tvs rhs@(OpenSynTyCon rhs_ki _) _
45 kind = mkArrowKinds (map tyVarKind tvs) rhs_ki
47 return $ mkSynTyCon tc_name kind tvs rhs NoParentTyCon
49 buildSynTyCon tc_name tvs rhs@(SynonymTyCon rhs_ty) mb_family
50 = do { -- We need to tie a knot as the coercion of a data instance depends
51 -- on the instance representation tycon and vice versa.
52 ; tycon <- fixM (\ tycon_rec -> do
53 { parent <- mkParentInfo mb_family tc_name tvs tycon_rec
54 ; let { tycon = mkSynTyCon tc_name kind tvs rhs parent
55 ; kind = mkArrowKinds (map tyVarKind tvs) (typeKind rhs_ty)
62 ------------------------------------------------------
63 buildAlgTyCon :: Name -> [TyVar]
64 -> ThetaType -- Stupid theta
67 -> Bool -- True <=> want generics functions
68 -> Bool -- True <=> was declared in GADT syntax
69 -> Maybe (TyCon, [Type]) -- family instance if applicable
72 buildAlgTyCon tc_name tvs stupid_theta rhs is_rec want_generics gadt_syn
74 = do { -- We need to tie a knot as the coercion of a data instance depends
75 -- on the instance representation tycon and vice versa.
76 ; tycon <- fixM (\ tycon_rec -> do
77 { parent <- mkParentInfo mb_family tc_name tvs tycon_rec
78 ; let { tycon = mkAlgTyCon tc_name kind tvs stupid_theta rhs
79 fields parent is_rec want_generics gadt_syn
80 ; kind = mkArrowKinds (map tyVarKind tvs) liftedTypeKind
81 ; fields = mkTyConSelIds tycon rhs
88 -- If a family tycon with instance types is given, the current tycon is an
89 -- instance of that family and we need to
91 -- (1) create a coercion that identifies the family instance type and the
92 -- representation type from Step (1); ie, it is of the form
93 -- `Co tvs :: F ts :=: R tvs', where `Co' is the name of the coercion,
94 -- `F' the family tycon and `R' the (derived) representation tycon,
96 -- (2) produce a `TyConParent' value containing the parent and coercion
99 mkParentInfo :: Maybe (TyCon, [Type])
102 -> TcRnIf m n TyConParent
103 mkParentInfo Nothing _ _ _ =
105 mkParentInfo (Just (family, instTys)) tc_name tvs rep_tycon =
106 do { -- Create the coercion
107 ; co_tycon_name <- newImplicitBinder tc_name mkInstTyCoOcc
108 ; let co_tycon = mkFamInstCoercion co_tycon_name tvs
109 family instTys rep_tycon
110 ; return $ FamilyTyCon family instTys co_tycon
113 ------------------------------------------------------
114 mkAbstractTyConRhs :: AlgTyConRhs
115 mkAbstractTyConRhs = AbstractTyCon
117 mkOpenDataTyConRhs :: AlgTyConRhs
118 mkOpenDataTyConRhs = OpenTyCon Nothing
120 mkDataTyConRhs :: [DataCon] -> AlgTyConRhs
122 = DataTyCon { data_cons = cons, is_enum = all isNullarySrcDataCon cons }
124 mkNewTyConRhs :: Name -> TyCon -> DataCon -> TcRnIf m n AlgTyConRhs
125 -- Monadic because it makes a Name for the coercion TyCon
126 -- We pass the Name of the parent TyCon, as well as the TyCon itself,
127 -- because the latter is part of a knot, whereas the former is not.
128 mkNewTyConRhs tycon_name tycon con
129 = do { co_tycon_name <- newImplicitBinder tycon_name mkNewTyCoOcc
130 ; let co_tycon = mkNewTypeCoercion co_tycon_name tycon etad_tvs etad_rhs
131 cocon_maybe | all_coercions || isRecursiveTyCon tycon
135 ; return (NewTyCon { data_con = con,
137 nt_etad_rhs = (etad_tvs, etad_rhs),
139 -- Coreview looks through newtypes with a Nothing
140 -- for nt_co, or uses explicit coercions otherwise
141 nt_rep = mkNewTyConRep tycon rhs_ty }) }
143 -- If all_coercions is True then we use coercions for all newtypes
144 -- otherwise we use coercions for recursive newtypes and look through
145 -- non-recursive newtypes
147 tvs = tyConTyVars tycon
148 rhs_ty = head (dataConInstOrigArgTys con (mkTyVarTys tvs))
149 -- Instantiate the data con with the
150 -- type variables from the tycon
151 -- NB: a newtype DataCon has no existentials; hence the
152 -- call to dataConInstOrigArgTys has the right type args
154 etad_tvs :: [TyVar] -- Matched lazily, so that mkNewTypeCoercion can
155 etad_rhs :: Type -- return a TyCon without pulling on rhs_ty
156 -- See Note [Tricky iface loop] in LoadIface
157 (etad_tvs, etad_rhs) = eta_reduce (reverse tvs) rhs_ty
159 eta_reduce :: [TyVar] -- Reversed
161 -> ([TyVar], Type) -- Eta-reduced version (tyvars in normal order)
162 eta_reduce (a:as) ty | Just (fun, arg) <- splitAppTy_maybe ty,
163 Just tv <- getTyVar_maybe arg,
165 not (a `elemVarSet` tyVarsOfType fun)
167 eta_reduce tvs ty = (reverse tvs, ty)
170 mkNewTyConRep :: TyCon -- The original type constructor
171 -> Type -- The arg type of its constructor
172 -> Type -- Chosen representation type
173 -- The "representation type" is guaranteed not to be another newtype
174 -- at the outermost level; but it might have newtypes in type arguments
176 -- Find the representation type for this newtype TyCon
177 -- Remember that the representation type is the *ultimate* representation
178 -- type, looking through other newtypes.
180 -- splitTyConApp_maybe no longer looks through newtypes, so we must
181 -- deal explicitly with this case
183 -- The trick is to to deal correctly with recursive newtypes
184 -- such as newtype T = MkT T
186 mkNewTyConRep tc rhs_ty
187 | null (tyConDataCons tc) = unitTy
188 -- External Core programs can have newtypes with no data constructors
189 | otherwise = go [tc] rhs_ty
191 -- Invariant: tcs have been seen before
193 = case splitTyConApp_maybe rep_ty of
195 | tc `elem` tcs -> unitTy -- Recursive loop
197 if isRecursiveTyCon tc then
198 go (tc:tcs) (substTyWith tvs tys rhs_ty)
200 substTyWith tvs tys rhs_ty
202 (tvs, rhs_ty) = newTyConRhs tc
206 ------------------------------------------------------
207 buildDataCon :: Name -> Bool
209 -> [Name] -- Field labels
210 -> [TyVar] -> [TyVar] -- Univ and ext
211 -> [(TyVar,Type)] -- Equality spec
212 -> ThetaType -- Does not include the "stupid theta"
213 -- or the GADT equalities
215 -> TcRnIf m n DataCon
216 -- A wrapper for DataCon.mkDataCon that
217 -- a) makes the worker Id
218 -- b) makes the wrapper Id if necessary, including
219 -- allocating its unique (hence monadic)
220 buildDataCon src_name declared_infix arg_stricts field_lbls
221 univ_tvs ex_tvs eq_spec ctxt arg_tys tycon
222 = do { wrap_name <- newImplicitBinder src_name mkDataConWrapperOcc
223 ; work_name <- newImplicitBinder src_name mkDataConWorkerOcc
224 -- This last one takes the name of the data constructor in the source
225 -- code, which (for Haskell source anyway) will be in the DataName name
226 -- space, and puts it into the VarName name space
229 stupid_ctxt = mkDataConStupidTheta tycon arg_tys univ_tvs
230 data_con = mkDataCon src_name declared_infix
231 arg_stricts field_lbls
232 univ_tvs ex_tvs eq_spec ctxt
235 dc_ids = mkDataConIds wrap_name work_name data_con
240 -- The stupid context for a data constructor should be limited to
241 -- the type variables mentioned in the arg_tys
242 -- ToDo: Or functionally dependent on?
243 -- This whole stupid theta thing is, well, stupid.
244 mkDataConStupidTheta tycon arg_tys univ_tvs
245 | null stupid_theta = [] -- The common case
246 | otherwise = filter in_arg_tys stupid_theta
248 tc_subst = zipTopTvSubst (tyConTyVars tycon) (mkTyVarTys univ_tvs)
249 stupid_theta = substTheta tc_subst (tyConStupidTheta tycon)
250 -- Start by instantiating the master copy of the
251 -- stupid theta, taken from the TyCon
253 arg_tyvars = tyVarsOfTypes arg_tys
254 in_arg_tys pred = not $ isEmptyVarSet $
255 tyVarsOfPred pred `intersectVarSet` arg_tyvars
257 ------------------------------------------------------
258 mkTyConSelIds :: TyCon -> AlgTyConRhs -> [Id]
259 mkTyConSelIds tycon rhs
260 = [ mkRecordSelId tycon fld
261 | fld <- nub (concatMap dataConFieldLabels (visibleDataCons rhs)) ]
262 -- We'll check later that fields with the same name
263 -- from different constructors have the same type.
267 ------------------------------------------------------
269 buildClass :: Name -> [TyVar] -> ThetaType
270 -> [FunDep TyVar] -- Functional dependencies
271 -> [TyThing] -- Associated types
272 -> [(Name, DefMeth, Type)] -- Method info
273 -> RecFlag -- Info for type constructor
276 buildClass class_name tvs sc_theta fds ats sig_stuff tc_isrec
277 = do { tycon_name <- newImplicitBinder class_name mkClassTyConOcc
278 ; datacon_name <- newImplicitBinder class_name mkClassDataConOcc
279 -- The class name is the 'parent' for this datacon, not its tycon,
280 -- because one should import the class to get the binding for
282 ; sc_sel_names <- mapM (newImplicitBinder class_name . mkSuperDictSelOcc)
284 -- We number off the superclass selectors, 1, 2, 3 etc so that we
285 -- can construct names for the selectors. Thus
286 -- class (C a, C b) => D a b where ...
287 -- gives superclass selectors
289 -- (We used to call them D_C, but now we can have two different
290 -- superclasses both called C!)
292 ; fixM (\ rec_clas -> do { -- Only name generation inside loop
294 let { rec_tycon = classTyCon rec_clas
295 ; op_tys = [ty | (_,_,ty) <- sig_stuff]
296 ; sc_tys = mkPredTys sc_theta
297 ; dict_component_tys = sc_tys ++ op_tys
298 ; sc_sel_ids = [mkDictSelId sc_name rec_clas | sc_name <- sc_sel_names]
299 ; op_items = [ (mkDictSelId op_name rec_clas, dm_info)
300 | (op_name, dm_info, _) <- sig_stuff ] }
301 -- Build the selector id and default method id
303 ; dict_con <- buildDataCon datacon_name
304 False -- Not declared infix
305 (map (const NotMarkedStrict) dict_component_tys)
306 [{- No labelled fields -}]
307 tvs [{- no existentials -}]
308 [{- No equalities -}] [{-No context-}]
312 ; rhs <- case dict_component_tys of
313 [rep_ty] -> mkNewTyConRhs tycon_name rec_tycon dict_con
314 other -> return (mkDataTyConRhs [dict_con])
316 ; let { clas_kind = mkArrowKinds (map tyVarKind tvs) liftedTypeKind
318 ; tycon = mkClassTyCon tycon_name clas_kind tvs
319 rhs rec_clas tc_isrec
320 -- A class can be recursive, and in the case of newtypes
321 -- this matters. For example
322 -- class C a where { op :: C b => a -> b -> Int }
323 -- Because C has only one operation, it is represented by
324 -- a newtype, and it should be a *recursive* newtype.
325 -- [If we don't make it a recursive newtype, we'll expand the
326 -- newtype like a synonym, but that will lead to an infinite
328 ; atTyCons = [tycon | ATyCon tycon <- ats]
330 ; return (mkClass class_name tvs fds
331 sc_theta sc_sel_ids atTyCons op_items