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, setAssocFamilyPermutation
14 #include "HsVersions.h"
36 ------------------------------------------------------
37 buildSynTyCon :: Name -> [TyVar]
39 -> Kind -- Kind of the RHS
40 -> Maybe (TyCon, [Type]) -- family instance if applicable
43 buildSynTyCon tc_name tvs rhs@(OpenSynTyCon {}) rhs_kind _
45 kind = mkArrowKinds (map tyVarKind tvs) rhs_kind
47 return $ mkSynTyCon tc_name kind tvs rhs NoParentTyCon
49 buildSynTyCon tc_name tvs rhs@(SynonymTyCon {}) rhs_kind 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) rhs_kind
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 parent is_rec want_generics gadt_syn
80 ; kind = mkArrowKinds (map tyVarKind tvs) liftedTypeKind
87 -- If a family tycon with instance types is given, the current tycon is an
88 -- instance of that family and we need to
90 -- (1) create a coercion that identifies the family instance type and the
91 -- representation type from Step (1); ie, it is of the form
92 -- `Co tvs :: F ts ~ R tvs', where `Co' is the name of the coercion,
93 -- `F' the family tycon and `R' the (derived) representation tycon,
95 -- (2) produce a `TyConParent' value containing the parent and coercion
98 mkParentInfo :: Maybe (TyCon, [Type])
101 -> TcRnIf m n TyConParent
102 mkParentInfo Nothing _ _ _ =
104 mkParentInfo (Just (family, instTys)) tc_name tvs rep_tycon =
105 do { -- Create the coercion
106 ; co_tycon_name <- newImplicitBinder tc_name mkInstTyCoOcc
107 ; let co_tycon = mkFamInstCoercion co_tycon_name tvs
108 family instTys rep_tycon
109 ; return $ FamilyTyCon family instTys co_tycon
112 ------------------------------------------------------
113 mkAbstractTyConRhs :: AlgTyConRhs
114 mkAbstractTyConRhs = AbstractTyCon
116 mkOpenDataTyConRhs :: AlgTyConRhs
117 mkOpenDataTyConRhs = OpenTyCon Nothing
119 mkDataTyConRhs :: [DataCon] -> AlgTyConRhs
123 is_enum = -- We define datatypes with no constructors to not be
124 -- enumerations; this fixes trac #2578
126 all isNullarySrcDataCon cons
129 mkNewTyConRhs :: Name -> TyCon -> DataCon -> TcRnIf m n AlgTyConRhs
130 -- Monadic because it makes a Name for the coercion TyCon
131 -- We pass the Name of the parent TyCon, as well as the TyCon itself,
132 -- because the latter is part of a knot, whereas the former is not.
133 mkNewTyConRhs tycon_name tycon con
134 = do { co_tycon_name <- newImplicitBinder tycon_name mkNewTyCoOcc
135 ; let co_tycon = mkNewTypeCoercion co_tycon_name tycon etad_tvs etad_rhs
136 cocon_maybe | all_coercions || isRecursiveTyCon tycon
140 ; traceIf (text "mkNewTyConRhs" <+> ppr cocon_maybe)
141 ; return (NewTyCon { data_con = con,
143 nt_etad_rhs = (etad_tvs, etad_rhs),
144 nt_co = cocon_maybe } ) }
145 -- Coreview looks through newtypes with a Nothing
146 -- for nt_co, or uses explicit coercions otherwise
148 -- If all_coercions is True then we use coercions for all newtypes
149 -- otherwise we use coercions for recursive newtypes and look through
150 -- non-recursive newtypes
152 tvs = tyConTyVars tycon
153 inst_con_ty = applyTys (dataConUserType con) (mkTyVarTys tvs)
154 rhs_ty = ASSERT( isFunTy inst_con_ty ) funArgTy inst_con_ty
155 -- Instantiate the data con with the
156 -- type variables from the tycon
157 -- NB: a newtype DataCon has a type that must look like
158 -- forall tvs. <arg-ty> -> T tvs
159 -- Note that we *can't* use dataConInstOrigArgTys here because
160 -- the newtype arising from class Foo a => Bar a where {}
161 -- has a single argument (Foo a) that is a *type class*, so
162 -- dataConInstOrigArgTys returns [].
164 etad_tvs :: [TyVar] -- Matched lazily, so that mkNewTypeCoercion can
165 etad_rhs :: Type -- return a TyCon without pulling on rhs_ty
166 -- See Note [Tricky iface loop] in LoadIface
167 (etad_tvs, etad_rhs) = eta_reduce (reverse tvs) rhs_ty
169 eta_reduce :: [TyVar] -- Reversed
171 -> ([TyVar], Type) -- Eta-reduced version (tyvars in normal order)
172 eta_reduce (a:as) ty | Just (fun, arg) <- splitAppTy_maybe ty,
173 Just tv <- getTyVar_maybe arg,
175 not (a `elemVarSet` tyVarsOfType fun)
177 eta_reduce tvs ty = (reverse tvs, ty)
180 setAssocFamilyPermutation :: [TyVar] -> TyThing -> TyThing
181 setAssocFamilyPermutation clas_tvs (ATyCon tc)
182 = ATyCon (setTyConArgPoss clas_tvs tc)
183 setAssocFamilyPermutation _clas_tvs other
184 = pprPanic "setAssocFamilyPermutation" (ppr other)
187 ------------------------------------------------------
188 buildDataCon :: Name -> Bool
190 -> [Name] -- Field labels
191 -> [TyVar] -> [TyVar] -- Univ and ext
192 -> [(TyVar,Type)] -- Equality spec
193 -> ThetaType -- Does not include the "stupid theta"
194 -- or the GADT equalities
195 -> [Type] -> Type -- Argument and result types
196 -> TyCon -- Rep tycon
197 -> TcRnIf m n DataCon
198 -- A wrapper for DataCon.mkDataCon that
199 -- a) makes the worker Id
200 -- b) makes the wrapper Id if necessary, including
201 -- allocating its unique (hence monadic)
202 buildDataCon src_name declared_infix arg_stricts field_lbls
203 univ_tvs ex_tvs eq_spec ctxt arg_tys res_ty rep_tycon
204 = do { wrap_name <- newImplicitBinder src_name mkDataConWrapperOcc
205 ; work_name <- newImplicitBinder src_name mkDataConWorkerOcc
206 -- This last one takes the name of the data constructor in the source
207 -- code, which (for Haskell source anyway) will be in the DataName name
208 -- space, and puts it into the VarName name space
211 stupid_ctxt = mkDataConStupidTheta rep_tycon arg_tys univ_tvs
212 data_con = mkDataCon src_name declared_infix
213 arg_stricts field_lbls
214 univ_tvs ex_tvs eq_spec ctxt
215 arg_tys res_ty rep_tycon
217 dc_ids = mkDataConIds wrap_name work_name data_con
222 -- The stupid context for a data constructor should be limited to
223 -- the type variables mentioned in the arg_tys
224 -- ToDo: Or functionally dependent on?
225 -- This whole stupid theta thing is, well, stupid.
226 mkDataConStupidTheta :: TyCon -> [Type] -> [TyVar] -> [PredType]
227 mkDataConStupidTheta tycon arg_tys univ_tvs
228 | null stupid_theta = [] -- The common case
229 | otherwise = filter in_arg_tys stupid_theta
231 tc_subst = zipTopTvSubst (tyConTyVars tycon) (mkTyVarTys univ_tvs)
232 stupid_theta = substTheta tc_subst (tyConStupidTheta tycon)
233 -- Start by instantiating the master copy of the
234 -- stupid theta, taken from the TyCon
236 arg_tyvars = tyVarsOfTypes arg_tys
237 in_arg_tys pred = not $ isEmptyVarSet $
238 tyVarsOfPred pred `intersectVarSet` arg_tyvars
242 ------------------------------------------------------
244 buildClass :: Bool -- True <=> do not include unfoldings
246 -- Used when importing a class without -O
247 -> Name -> [TyVar] -> ThetaType
248 -> [FunDep TyVar] -- Functional dependencies
249 -> [TyThing] -- Associated types
250 -> [(Name, DefMeth, Type)] -- Method info
251 -> RecFlag -- Info for type constructor
254 buildClass no_unf class_name tvs sc_theta fds ats sig_stuff tc_isrec
255 = do { traceIf (text "buildClass")
256 ; tycon_name <- newImplicitBinder class_name mkClassTyConOcc
257 ; datacon_name <- newImplicitBinder class_name mkClassDataConOcc
258 -- The class name is the 'parent' for this datacon, not its tycon,
259 -- because one should import the class to get the binding for
262 ; fixM (\ rec_clas -> do { -- Only name generation inside loop
264 let { rec_tycon = classTyCon rec_clas
265 ; op_tys = [ty | (_,_,ty) <- sig_stuff]
266 ; op_names = [op | (op,_,_) <- sig_stuff]
267 ; op_items = [ (mkDictSelId no_unf op_name rec_clas, dm_info)
268 | (op_name, dm_info, _) <- sig_stuff ] }
269 -- Build the selector id and default method id
271 ; let n_value_preds = count (not . isEqPred) sc_theta
272 all_value_preds = n_value_preds == length sc_theta
273 -- We only make selectors for the *value* superclasses,
274 -- not equality predicates
276 ; sc_sel_names <- mapM (newImplicitBinder class_name . mkSuperDictSelOcc)
278 ; let sc_sel_ids = [mkDictSelId no_unf sc_name rec_clas | sc_name <- sc_sel_names]
279 -- We number off the Dict superclass selectors, 1, 2, 3 etc so that we
280 -- can construct names for the selectors. Thus
281 -- class (C a, C b) => D a b where ...
282 -- gives superclass selectors
284 -- (We used to call them D_C, but now we can have two different
285 -- superclasses both called C!)
288 ; let use_newtype = (n_value_preds + length sig_stuff == 1) && all_value_preds
289 -- Use a newtype if the data constructor has
290 -- (a) exactly one value field
291 -- (b) no existential or equality-predicate fields
292 -- i.e. exactly one operation or superclass taken together
293 -- See note [Class newtypes and equality predicates]
295 -- We play a bit fast and loose by treating the superclasses
296 -- as ordinary arguments. That means that in the case of
298 -- we don't get a newtype with no arguments!
299 args = sc_sel_names ++ op_names
300 arg_tys = map mkPredTy sc_theta ++ op_tys
302 ; dict_con <- buildDataCon datacon_name
303 False -- Not declared infix
304 (map (const NotMarkedStrict) args)
306 tvs [{- no existentials -}]
307 [{- No GADT equalities -}] [{- No theta -}]
309 (mkTyConApp rec_tycon (mkTyVarTys tvs))
312 ; rhs <- if use_newtype
313 then mkNewTyConRhs tycon_name rec_tycon dict_con
314 else 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 ; result = mkClass class_name tvs fds
331 sc_theta sc_sel_ids atTyCons
334 ; traceIf (text "buildClass" <+> ppr tycon)
339 Note [Class newtypes and equality predicates]
340 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
342 class (a ~ F b) => C a b where
345 We cannot represent this by a newtype, even though it's not
346 existential, and there's only one value field, because we do
347 capture an equality predicate:
350 MkC :: forall a b. (a ~ F b) => (a->b) -> C a b
352 We need to access this equality predicate when we get passes a C
353 dictionary. See Trac #2238