2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
7 buildSynTyCon, buildAlgTyCon, buildDataCon,
9 mkAbstractTyConRhs, mkOpenDataTyConRhs, mkOpenNewTyConRhs,
10 mkNewTyConRhs, mkDataTyConRhs
13 #include "HsVersions.h"
15 import IfaceEnv ( newImplicitBinder )
18 import DataCon ( DataCon, isNullarySrcDataCon, dataConUnivTyVars,
19 mkDataCon, dataConFieldLabels, dataConInstOrigArgTys,
21 import Var ( tyVarKind, TyVar, Id )
22 import VarSet ( isEmptyVarSet, intersectVarSet, elemVarSet )
23 import TysWiredIn ( unitTy )
24 import BasicTypes ( RecFlag, StrictnessMark(..) )
26 import OccName ( mkDataConWrapperOcc, mkDataConWorkerOcc, mkClassTyConOcc,
27 mkClassDataConOcc, mkSuperDictSelOcc, mkNewTyCoOcc )
28 import MkId ( mkDataConIds, mkRecordSelId, mkDictSelId )
29 import Class ( mkClass, Class( classTyCon), FunDep, DefMeth(..) )
30 import TyCon ( mkSynTyCon, mkAlgTyCon, visibleDataCons,
31 tyConStupidTheta, tyConDataCons, isNewTyCon,
32 mkClassTyCon, TyCon( tyConTyVars ),
33 isRecursiveTyCon, tyConArity, AlgTyConRhs(..),
34 SynTyConRhs(..), newTyConRhs )
35 import Type ( mkArrowKinds, liftedTypeKind, typeKind,
36 tyVarsOfType, tyVarsOfTypes, tyVarsOfPred,
37 splitTyConApp_maybe, splitAppTy_maybe,
39 mkPredTys, mkTyVarTys, ThetaType, Type, Kind,
41 substTyWith, zipTopTvSubst, substTheta, mkForAllTys,
42 mkTyConApp, mkTyVarTy )
43 import Coercion ( mkNewTypeCoercion )
51 ------------------------------------------------------
52 buildSynTyCon :: Name -> [TyVar] -> SynTyConRhs -> TyCon
53 buildSynTyCon name tvs rhs@(OpenSynTyCon rhs_ki)
54 = mkSynTyCon name kind tvs rhs
56 kind = mkArrowKinds (map tyVarKind tvs) rhs_ki
57 buildSynTyCon name tvs rhs@(SynonymTyCon rhs_ty)
58 = mkSynTyCon name kind tvs rhs
60 kind = mkArrowKinds (map tyVarKind tvs) (typeKind rhs_ty)
63 ------------------------------------------------------
64 buildAlgTyCon :: Name -> [TyVar]
65 -> ThetaType -- Stupid theta
68 -> Bool -- True <=> want generics functions
69 -> Bool -- True <=> was declared in GADT syntax
72 buildAlgTyCon tc_name tvs stupid_theta rhs is_rec want_generics gadt_syn
73 = do { let { tycon = mkAlgTyCon tc_name kind tvs stupid_theta
74 rhs fields is_rec want_generics gadt_syn
75 ; kind = mkArrowKinds (map tyVarKind tvs) liftedTypeKind
76 ; fields = mkTyConSelIds tycon rhs
80 ------------------------------------------------------
81 mkAbstractTyConRhs :: AlgTyConRhs
82 mkAbstractTyConRhs = AbstractTyCon
84 mkOpenDataTyConRhs :: AlgTyConRhs
85 mkOpenDataTyConRhs = OpenDataTyCon
87 mkOpenNewTyConRhs :: AlgTyConRhs
88 mkOpenNewTyConRhs = OpenNewTyCon
90 mkDataTyConRhs :: [DataCon] -> AlgTyConRhs
92 = DataTyCon { data_cons = cons, is_enum = all isNullarySrcDataCon cons }
94 mkNewTyConRhs :: Name -> TyCon -> DataCon -> TcRnIf m n AlgTyConRhs
95 -- Monadic because it makes a Name for the coercion TyCon
96 -- We pass the Name of the parent TyCon, as well as the TyCon itself,
97 -- because the latter is part of a knot, whereas the former is not.
98 mkNewTyConRhs tycon_name tycon con
99 = do { co_tycon_name <- newImplicitBinder tycon_name mkNewTyCoOcc
100 ; let co_tycon = mkNewTypeCoercion co_tycon_name tycon tvs rhs_ty
102 | all_coercions || isRecursiveTyCon tycon
106 ; return (NewTyCon { data_con = con,
108 -- Coreview looks through newtypes with a Nothing
109 -- for nt_co, or uses explicit coercions otherwise
111 nt_etad_rhs = eta_reduce tvs rhs_ty,
112 nt_rep = mkNewTyConRep tycon rhs_ty }) }
114 -- if all_coercions is True then we use coercions for all newtypes
115 -- otherwise we use coercions for recursive newtypes and look through
116 -- non-recursive newtypes
118 tvs = tyConTyVars tycon
119 rhs_ty = head (dataConInstOrigArgTys con (mkTyVarTys tvs))
120 -- Instantiate the data con with the
121 -- type variables from the tycon
123 eta_reduce [] ty = ([], ty)
124 eta_reduce (a:as) ty | null as',
125 Just (fun, arg) <- splitAppTy_maybe ty',
126 Just tv <- getTyVar_maybe arg,
128 not (a `elemVarSet` tyVarsOfType fun)
129 = ([], fun) -- Successful eta reduction
133 (as', ty') = eta_reduce as ty
135 mkNewTyConRep :: TyCon -- The original type constructor
136 -> Type -- The arg type of its constructor
137 -> Type -- Chosen representation type
138 -- The "representation type" is guaranteed not to be another newtype
139 -- at the outermost level; but it might have newtypes in type arguments
141 -- Find the representation type for this newtype TyCon
142 -- Remember that the representation type is the *ultimate* representation
143 -- type, looking through other newtypes.
145 -- splitTyConApp_maybe no longer looks through newtypes, so we must
146 -- deal explicitly with this case
148 -- The trick is to to deal correctly with recursive newtypes
149 -- such as newtype T = MkT T
151 mkNewTyConRep tc rhs_ty
152 | null (tyConDataCons tc) = unitTy
153 -- External Core programs can have newtypes with no data constructors
154 | otherwise = go [tc] rhs_ty
156 -- Invariant: tcs have been seen before
158 = case splitTyConApp_maybe rep_ty of
160 | tc `elem` tcs -> unitTy -- Recursive loop
162 if isRecursiveTyCon tc then
163 go (tc:tcs) (substTyWith tvs tys rhs_ty)
165 substTyWith tvs tys rhs_ty
167 (tvs, rhs_ty) = newTyConRhs tc
171 ------------------------------------------------------
172 buildDataCon :: Name -> Bool
174 -> [Name] -- Field labels
175 -> [TyVar] -> [TyVar] -- Univ and ext
176 -> [(TyVar,Type)] -- Equality spec
177 -> ThetaType -- Does not include the "stupid theta"
178 -- or the GADT equalities
180 -> TcRnIf m n DataCon
181 -- A wrapper for DataCon.mkDataCon that
182 -- a) makes the worker Id
183 -- b) makes the wrapper Id if necessary, including
184 -- allocating its unique (hence monadic)
185 buildDataCon src_name declared_infix arg_stricts field_lbls
186 univ_tvs ex_tvs eq_spec ctxt arg_tys tycon
187 = do { wrap_name <- newImplicitBinder src_name mkDataConWrapperOcc
188 ; work_name <- newImplicitBinder src_name mkDataConWorkerOcc
189 -- This last one takes the name of the data constructor in the source
190 -- code, which (for Haskell source anyway) will be in the DataName name
191 -- space, and puts it into the VarName name space
194 stupid_ctxt = mkDataConStupidTheta tycon arg_tys univ_tvs
195 data_con = mkDataCon src_name declared_infix
196 arg_stricts field_lbls
197 univ_tvs ex_tvs eq_spec ctxt
198 arg_tys tycon stupid_ctxt dc_ids
199 dc_ids = mkDataConIds wrap_name work_name data_con
204 -- The stupid context for a data constructor should be limited to
205 -- the type variables mentioned in the arg_tys
206 -- ToDo: Or functionally dependent on?
207 -- This whole stupid theta thing is, well, stupid.
208 mkDataConStupidTheta tycon arg_tys univ_tvs
209 | null stupid_theta = [] -- The common case
210 | otherwise = filter in_arg_tys stupid_theta
212 tc_subst = zipTopTvSubst (tyConTyVars tycon) (mkTyVarTys univ_tvs)
213 stupid_theta = substTheta tc_subst (tyConStupidTheta tycon)
214 -- Start by instantiating the master copy of the
215 -- stupid theta, taken from the TyCon
217 arg_tyvars = tyVarsOfTypes arg_tys
218 in_arg_tys pred = not $ isEmptyVarSet $
219 tyVarsOfPred pred `intersectVarSet` arg_tyvars
221 ------------------------------------------------------
222 mkTyConSelIds :: TyCon -> AlgTyConRhs -> [Id]
223 mkTyConSelIds tycon rhs
224 = [ mkRecordSelId tycon fld
225 | fld <- nub (concatMap dataConFieldLabels (visibleDataCons rhs)) ]
226 -- We'll check later that fields with the same name
227 -- from different constructors have the same type.
231 ------------------------------------------------------
233 buildClass :: Name -> [TyVar] -> ThetaType
234 -> [FunDep TyVar] -- Functional dependencies
235 -> [TyThing] -- Associated types
236 -> [(Name, DefMeth, Type)] -- Method info
237 -> RecFlag -- Info for type constructor
240 buildClass class_name tvs sc_theta fds ats sig_stuff tc_isrec
241 = do { tycon_name <- newImplicitBinder class_name mkClassTyConOcc
242 ; datacon_name <- newImplicitBinder class_name mkClassDataConOcc
243 -- The class name is the 'parent' for this datacon, not its tycon,
244 -- because one should import the class to get the binding for
246 ; sc_sel_names <- mapM (newImplicitBinder class_name . mkSuperDictSelOcc)
248 -- We number off the superclass selectors, 1, 2, 3 etc so that we
249 -- can construct names for the selectors. Thus
250 -- class (C a, C b) => D a b where ...
251 -- gives superclass selectors
253 -- (We used to call them D_C, but now we can have two different
254 -- superclasses both called C!)
256 ; fixM (\ rec_clas -> do { -- Only name generation inside loop
258 let { rec_tycon = classTyCon rec_clas
259 ; op_tys = [ty | (_,_,ty) <- sig_stuff]
260 ; sc_tys = mkPredTys sc_theta
261 ; dict_component_tys = sc_tys ++ op_tys
262 ; sc_sel_ids = [mkDictSelId sc_name rec_clas | sc_name <- sc_sel_names]
263 ; op_items = [ (mkDictSelId op_name rec_clas, dm_info)
264 | (op_name, dm_info, _) <- sig_stuff ] }
265 -- Build the selector id and default method id
267 ; dict_con <- buildDataCon datacon_name
268 False -- Not declared infix
269 (map (const NotMarkedStrict) dict_component_tys)
270 [{- No labelled fields -}]
271 tvs [{- no existentials -}]
272 [{- No equalities -}] [{-No context-}]
276 ; rhs <- case dict_component_tys of
277 [rep_ty] -> mkNewTyConRhs tycon_name rec_tycon dict_con
278 other -> return (mkDataTyConRhs [dict_con])
280 ; let { clas_kind = mkArrowKinds (map tyVarKind tvs) liftedTypeKind
282 ; tycon = mkClassTyCon tycon_name clas_kind tvs
283 rhs rec_clas tc_isrec
284 -- A class can be recursive, and in the case of newtypes
285 -- this matters. For example
286 -- class C a where { op :: C b => a -> b -> Int }
287 -- Because C has only one operation, it is represented by
288 -- a newtype, and it should be a *recursive* newtype.
289 -- [If we don't make it a recursive newtype, we'll expand the
290 -- newtype like a synonym, but that will lead to an infinite
292 ; atTyCons = [tycon | ATyCon tycon <- ats]
294 ; return (mkClass class_name tvs fds
295 sc_theta sc_sel_ids atTyCons op_items