2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
7 buildSynTyCon, buildAlgTyCon, buildDataCon,
9 mkAbstractTyConRhs, mkNewTyConRhs, mkDataTyConRhs
12 #include "HsVersions.h"
14 import IfaceEnv ( newImplicitBinder )
17 import Util ( zipLazy )
18 import DataCon ( DataCon, isNullarySrcDataCon,
19 mkDataCon, dataConFieldLabels, dataConOrigArgTys )
20 import Var ( tyVarKind, TyVar, Id )
21 import VarSet ( isEmptyVarSet, intersectVarSet )
22 import TysWiredIn ( unitTy )
23 import BasicTypes ( RecFlag, StrictnessMark(..) )
25 import OccName ( mkDataConWrapperOcc, mkDataConWorkerOcc, mkClassTyConOcc,
26 mkClassDataConOcc, mkSuperDictSelOcc )
27 import MkId ( mkDataConIds, mkRecordSelId, mkDictSelId )
28 import Class ( mkClass, Class( classTyCon), FunDep, DefMeth(..) )
29 import TyCon ( FieldLabel, mkSynTyCon, mkAlgTyCon, visibleDataCons, tyConStupidTheta,
30 tyConDataCons, isNewTyCon, mkClassTyCon, TyCon( tyConTyVars ),
31 ArgVrcs, AlgTyConRhs(..), newTyConRhs, visibleDataCons )
32 import Type ( mkArrowKinds, liftedTypeKind, typeKind, tyVarsOfTypes, tyVarsOfPred,
33 splitTyConApp_maybe, mkPredTys, mkTyVarTys, ThetaType, Type,
34 substTyWith, zipTopTvSubst, substTheta )
42 ------------------------------------------------------
43 buildSynTyCon name tvs rhs_ty arg_vrcs
44 = mkSynTyCon name kind tvs rhs_ty arg_vrcs
46 kind = mkArrowKinds (map tyVarKind tvs) (typeKind rhs_ty)
49 ------------------------------------------------------
50 buildAlgTyCon :: Name -> [TyVar]
53 -> Bool -- True <=> want generics functions
56 buildAlgTyCon tc_name tvs rhs arg_vrcs is_rec want_generics
57 = do { let { tycon = mkAlgTyCon tc_name kind tvs arg_vrcs
58 rhs fields is_rec want_generics
59 ; kind = mkArrowKinds (map tyVarKind tvs) liftedTypeKind
60 ; fields = mkTyConFields tycon rhs
64 ------------------------------------------------------
65 mkAbstractTyConRhs :: AlgTyConRhs
66 mkAbstractTyConRhs = AbstractTyCon
68 mkDataTyConRhs :: Maybe ThetaType -> [DataCon] -> AlgTyConRhs
69 mkDataTyConRhs mb_theta cons
70 = DataTyCon mb_theta cons (all isNullarySrcDataCon cons)
72 mkNewTyConRhs :: TyCon -> DataCon -> AlgTyConRhs
73 mkNewTyConRhs tycon con
74 = NewTyCon con rhs_ty (mkNewTyConRep tycon)
76 rhs_ty = head (dataConOrigArgTys con)
77 -- Newtypes are guaranteed vanilla, so OrigArgTys will do
79 mkNewTyConRep :: TyCon -- The original type constructor
80 -> Type -- Chosen representation type
81 -- (guaranteed not to be another newtype)
83 -- Find the representation type for this newtype TyCon
84 -- Remember that the representation type is the *ultimate* representation
85 -- type, looking through other newtypes.
87 -- The non-recursive newtypes are easy, because they look transparent
88 -- to splitTyConApp_maybe, but recursive ones really are represented as
89 -- TyConApps (see TypeRep).
91 -- The trick is to to deal correctly with recursive newtypes
92 -- such as newtype T = MkT T
95 | null (tyConDataCons tc) = unitTy
96 -- External Core programs can have newtypes with no data constructors
97 | otherwise = go [] tc
99 -- Invariant: tc is a NewTyCon
100 -- tcs have been seen before
102 | tc `elem` tcs = unitTy
104 = case splitTyConApp_maybe rhs_ty of
105 Just (tc', tys) | isNewTyCon tc'
106 -> substTyWith tc_tvs tys (go (tc:tcs) tc')
109 (tc_tvs, rhs_ty) = newTyConRhs tc
112 ------------------------------------------------------
113 buildDataCon :: Name -> Bool -> Bool
115 -> [Name] -- Field labels
116 -> [TyVar] -> ThetaType
117 -> [Type] -> TyCon -> [Type]
118 -> TcRnIf m n DataCon
119 -- A wrapper for DataCon.mkDataCon that
120 -- a) makes the worker Id
121 -- b) makes the wrapper Id if necessary, including
122 -- allocating its unique (hence monadic)
123 buildDataCon src_name declared_infix vanilla arg_stricts field_lbls
124 tyvars ctxt arg_tys tycon res_tys
125 = do { wrap_name <- newImplicitBinder src_name mkDataConWrapperOcc
126 ; work_name <- newImplicitBinder src_name mkDataConWorkerOcc
127 -- This last one takes the name of the data constructor in the source
128 -- code, which (for Haskell source anyway) will be in the SrcDataName name
129 -- space, and makes it into a "real data constructor name"
132 stupid_ctxt = mkDataConStupidTheta tycon arg_tys res_tys
133 data_con = mkDataCon src_name declared_infix vanilla
134 arg_stricts field_lbls
135 tyvars stupid_ctxt ctxt
136 arg_tys tycon res_tys dc_ids
137 dc_ids = mkDataConIds wrap_name work_name data_con
142 -- The stupid context for a data constructor should be limited to
143 -- the type variables mentioned in the arg_tys
144 mkDataConStupidTheta tycon arg_tys res_tys
145 | null stupid_theta = [] -- The common case
146 | otherwise = filter in_arg_tys stupid_theta
148 tc_subst = zipTopTvSubst (tyConTyVars tycon) res_tys
149 stupid_theta = substTheta tc_subst (tyConStupidTheta tycon)
150 arg_tyvars = tyVarsOfTypes arg_tys
151 in_arg_tys pred = not $ isEmptyVarSet $
152 tyVarsOfPred pred `intersectVarSet` arg_tyvars
154 ------------------------------------------------------
155 mkTyConFields :: TyCon -> AlgTyConRhs -> [(FieldLabel,Type,Id)]
156 mkTyConFields tycon rhs
157 = -- We'll check later that fields with the same name
158 -- from different constructors have the same type.
159 [ (fld, ty, mkRecordSelId tycon fld ty)
160 | (fld, ty) <- nubBy eq_fld all_fld_tys ]
162 all_fld_tys = concatMap fld_tys_of (visibleDataCons rhs)
163 fld_tys_of con = dataConFieldLabels con `zipLazy`
164 dataConOrigArgTys con
165 -- The laziness means that the type isn't sucked in prematurely
166 -- Only vanilla datacons have fields at all, and they
167 -- share the tycon's type variables => datConOrigArgTys will do
169 eq_fld (f1,_) (f2,_) = f1 == f2
173 ------------------------------------------------------
175 buildClass :: Name -> [TyVar] -> ThetaType
176 -> [FunDep TyVar] -- Functional dependencies
177 -> [(Name, DefMeth, Type)] -- Method info
178 -> RecFlag -> ArgVrcs -- Info for type constructor
181 buildClass class_name tvs sc_theta fds sig_stuff tc_isrec tc_vrcs
182 = do { tycon_name <- newImplicitBinder class_name mkClassTyConOcc
183 ; datacon_name <- newImplicitBinder class_name mkClassDataConOcc
184 -- The class name is the 'parent' for this datacon, not its tycon,
185 -- because one should import the class to get the binding for
187 ; sc_sel_names <- mapM (newImplicitBinder class_name . mkSuperDictSelOcc)
189 -- We number off the superclass selectors, 1, 2, 3 etc so that we
190 -- can construct names for the selectors. Thus
191 -- class (C a, C b) => D a b where ...
192 -- gives superclass selectors
194 -- (We used to call them D_C, but now we can have two different
195 -- superclasses both called C!)
197 ; fixM (\ clas -> do { -- Only name generation inside loop
199 let { op_tys = [ty | (_,_,ty) <- sig_stuff]
200 ; sc_tys = mkPredTys sc_theta
201 ; dict_component_tys = sc_tys ++ op_tys
202 ; sc_sel_ids = [mkDictSelId sc_name clas | sc_name <- sc_sel_names]
203 ; op_items = [ (mkDictSelId op_name clas, dm_info)
204 | (op_name, dm_info, _) <- sig_stuff ] }
205 -- Build the selector id and default method id
207 ; dict_con <- buildDataCon datacon_name
208 False -- Not declared infix
209 True -- Is vanilla; tyvars same as tycon
210 (map (const NotMarkedStrict) dict_component_tys)
211 [{- No labelled fields -}]
212 tvs [{-No context-}] dict_component_tys
213 (classTyCon clas) (mkTyVarTys tvs)
215 ; let { clas = mkClass class_name tvs fds
216 sc_theta sc_sel_ids op_items
219 ; tycon = mkClassTyCon tycon_name clas_kind tvs
220 tc_vrcs rhs clas tc_isrec
221 -- A class can be recursive, and in the case of newtypes
222 -- this matters. For example
223 -- class C a where { op :: C b => a -> b -> Int }
224 -- Because C has only one operation, it is represented by
225 -- a newtype, and it should be a *recursive* newtype.
226 -- [If we don't make it a recursive newtype, we'll expand the
227 -- newtype like a synonym, but that will lead to an infinite type]
229 ; clas_kind = mkArrowKinds (map tyVarKind tvs) liftedTypeKind
231 ; rhs = case dict_component_tys of
232 [rep_ty] -> mkNewTyConRhs tycon dict_con
233 other -> mkDataTyConRhs Nothing [dict_con]