2 % (c) The AQUA Project, Glasgow University, 1996
4 \section[TcTyClsDecls]{Typecheck type and class declarations}
7 #include "HsVersions.h"
15 import HsSyn ( HsDecl(..), TyDecl(..), ConDecl(..), BangType(..),
16 ClassDecl(..), HsType(..), HsTyVar, DefaultDecl, InstDecl,
17 IfaceSig, Sig(..), MonoBinds, Fake, InPat, HsBinds(..), Bind, HsExpr,
20 import RnHsSyn ( RenamedTyDecl(..), RenamedClassDecl(..), SYN_IE(RenamedHsDecl)
22 import TcHsSyn ( SYN_IE(TcHsBinds), TcIdOcc(..) )
25 import Inst ( SYN_IE(InstanceMapper) )
26 import TcClassDcl ( tcClassDecl1 )
27 import TcEnv ( tcExtendTyConEnv, tcExtendClassEnv )
28 import SpecEnv ( SpecEnv )
29 import TcKind ( TcKind, newKindVars )
30 import TcTyDecls ( tcTyDecl, mkDataBinds )
31 import TcMonoType ( tcTyVarScope )
34 import Class ( SYN_IE(Class), classSelIds )
35 import Digraph ( findSCCs, SCC(..) )
36 import Name ( Name, getSrcLoc, isTvOcc, nameOccName )
39 import UniqSet ( SYN_IE(UniqSet), emptyUniqSet,
40 unitUniqSet, unionUniqSets,
41 unionManyUniqSets, uniqSetToList )
42 import SrcLoc ( SrcLoc )
43 import TyCon ( TyCon )
44 import Unique ( Unique )
45 import Util ( panic{-, pprTrace-} )
52 tcTyAndClassDecls1 :: InstanceMapper
56 tcTyAndClassDecls1 inst_mapper decls
57 = sortByDependency decls `thenTc` \ groups ->
58 tcGroups inst_mapper groups
60 tcGroups inst_mapper []
61 = tcGetEnv `thenNF_Tc` \ env ->
64 tcGroups inst_mapper (group:groups)
65 = tcGroup inst_mapper group `thenTc` \ new_env ->
67 -- Extend the environment using the new tycons and classes
70 -- Do the remaining groups
71 tcGroups inst_mapper groups
77 tcGroup :: InstanceMapper -> Bag RenamedHsDecl -> TcM s (TcEnv s)
78 tcGroup inst_mapper decls
79 = -- pprTrace "tcGroup: " (ppCat (map (fst.fmt_decl) (bagToList decls))) $
82 fixTc ( \ ~(tycons,classes,_) ->
84 -- EXTEND TYPE AND CLASS ENVIRONMENTS
85 -- NB: it's important that the tycons and classes come back in just
86 -- the same order from this fix as from get_binders, so that these
87 -- extend-env things work properly. A bit UGH-ish.
88 tcExtendTyConEnv tycon_names_w_arities tycons $
89 tcExtendClassEnv class_names classes $
91 -- DEAL WITH TYPE VARIABLES
92 tcTyVarScope tyvar_names ( \ tyvars ->
94 -- DEAL WITH THE DEFINITIONS THEMSELVES
95 foldBag combine (tcDecl inst_mapper)
96 (returnTc (emptyBag, emptyBag))
98 ) `thenTc` \ (tycon_bag,class_bag) ->
100 tycons = bagToList tycon_bag
101 classes = bagToList class_bag
104 -- SNAFFLE ENV TO RETURN
105 tcGetEnv `thenNF_Tc` \ final_env ->
107 returnTc (tycons, classes, final_env)
108 ) `thenTc` \ (_, _, final_env) ->
113 (tyvar_names, tycon_names_w_arities, class_names) = get_binders decls
116 = do_a `thenTc` \ (a1,a2) ->
117 do_b `thenTc` \ (b1,b2) ->
118 returnTc (a1 `unionBags` b1, a2 `unionBags` b2)
121 Dealing with one decl
122 ~~~~~~~~~~~~~~~~~~~~~
124 tcDecl :: InstanceMapper
126 -> TcM s (Bag TyCon, Bag Class)
128 tcDecl inst_mapper (TyD decl)
129 = tcTyDecl decl `thenTc` \ tycon ->
130 returnTc (unitBag tycon, emptyBag)
132 tcDecl inst_mapper (ClD decl)
133 = tcClassDecl1 inst_mapper decl `thenTc` \ clas ->
134 returnTc (emptyBag, unitBag clas)
140 sortByDependency :: [RenamedHsDecl] -> TcM s [Bag RenamedHsDecl]
141 sortByDependency decls
142 = let -- CHECK FOR SYNONYM CYCLES
143 syn_sccs = findSCCs mk_edges syn_decls
144 syn_cycles = [ map fmt_decl (bagToList decls)
145 | CyclicSCC decls <- syn_sccs]
148 checkTc (null syn_cycles) (typeCycleErr syn_cycles) `thenTc_`
150 let -- CHECK FOR CLASS CYCLES
151 cls_sccs = findSCCs mk_edges cls_decls
152 cls_cycles = [ map fmt_decl (bagToList decls)
153 | CyclicSCC decls <- cls_sccs]
156 checkTc (null cls_cycles) (classCycleErr cls_cycles) `thenTc_`
158 -- DO THE MAIN DEPENDENCY ANALYSIS
160 decl_sccs = findSCCs mk_edges ty_cls_decls
161 scc_bags = map bag_acyclic decl_sccs
166 syn_decls = listToBag (filter is_syn_decl decls)
167 ty_cls_decls = listToBag (filter is_ty_cls_decl decls)
168 cls_decls = listToBag (filter is_cls_decl decls)
172 bag_acyclic (AcyclicSCC scc) = unitBag scc
173 bag_acyclic (CyclicSCC sccs) = sccs
175 is_syn_decl (TyD (TySynonym _ _ _ _)) = True
176 is_syn_decl _ = False
178 is_ty_cls_decl (TyD _) = True
179 is_ty_cls_decl (ClD _) = True
180 is_ty_cls_decl other = False
182 is_cls_decl (ClD _) = True
183 is_cls_decl other = False
186 = (ppr PprForUser name, getSrcLoc name)
188 name = hsDeclName decl
191 Edges in Type/Class decls
192 ~~~~~~~~~~~~~~~~~~~~~~~~~
194 mk_edges (TyD (TyData ctxt name _ condecls derivs _ _))
195 = (uniqueOf name, set_to_bag (get_ctxt ctxt `unionUniqSets`
196 get_cons condecls `unionUniqSets`
199 mk_edges (TyD (TyNew ctxt name _ condecl derivs _ _))
200 = (uniqueOf name, set_to_bag (get_ctxt ctxt `unionUniqSets`
201 get_con condecl `unionUniqSets`
204 mk_edges (TyD (TySynonym name _ rhs _))
205 = (uniqueOf name, set_to_bag (get_ty rhs))
207 mk_edges (ClD (ClassDecl ctxt name _ sigs _ _ _))
208 = (uniqueOf name, set_to_bag (get_ctxt ctxt `unionUniqSets` get_sigs sigs))
211 = unionManyUniqSets (map (set_name.fst) ctxt)
213 get_deriv Nothing = emptyUniqSet
214 get_deriv (Just clss) = unionManyUniqSets (map set_name clss)
217 = unionManyUniqSets (map get_con cons)
219 get_con (ConDecl _ btys _)
220 = unionManyUniqSets (map get_bty btys)
221 get_con (ConOpDecl bty1 _ bty2 _)
222 = unionUniqSets (get_bty bty1) (get_bty bty2)
223 get_con (NewConDecl _ ty _)
225 get_con (RecConDecl _ nbtys _)
226 = unionManyUniqSets (map (get_bty.snd) nbtys)
228 get_bty (Banged ty) = get_ty ty
229 get_bty (Unbanged ty) = get_ty ty
231 get_ty (MonoTyVar name)
232 = if isTvOcc (nameOccName name) then emptyUniqSet else set_name name
233 get_ty (MonoTyApp ty1 ty2)
234 = unionUniqSets (get_ty ty1) (get_ty ty2)
235 get_ty (MonoFunTy ty1 ty2)
236 = unionUniqSets (get_ty ty1) (get_ty ty2)
237 get_ty (MonoListTy tc ty)
238 = set_name tc `unionUniqSets` get_ty ty
239 get_ty (MonoTupleTy tc tys)
240 = set_name tc `unionUniqSets` get_tys tys
241 get_ty (HsForAllTy _ ctxt mty)
242 = get_ctxt ctxt `unionUniqSets` get_ty mty
243 get_ty other = panic "TcTyClsDecls:get_ty"
246 = unionManyUniqSets (map get_ty tys)
249 = unionManyUniqSets (map get_sig sigs)
251 get_sig (ClassOpSig _ ty _ _) = get_ty ty
252 get_sig other = panic "TcTyClsDecls:get_sig"
254 set_name name = unitUniqSet (uniqueOf name)
256 set_to_bag set = listToBag (uniqSetToList set)
262 Extract *binding* names from type and class decls. Type variables are
263 bound in type, data, newtype and class declarations and the polytypes
264 in the class op sigs.
266 Why do we need to grab all these type variables at once, including
267 those locally-quantified type variables in class op signatures?
268 Because we can only commit to the final kind of a type variable when
269 we've completed the mutually recursive group. For example:
272 op :: D b => a -> b -> b
275 bop :: (Monad c) => ...
277 Here, the kind of the locally-polymorphic type variable "b"
278 depends on *all the uses of class D*. For example, the use of
279 Monad c in bop's type signature means that D must have kind Type->Type.
283 get_binders :: Bag RenamedHsDecl
284 -> ([HsTyVar Name], -- TyVars; no dups
285 [(Name, Maybe Arity)], -- Tycons; no dups; arities for synonyms
286 [Name]) -- Classes; no dups
288 get_binders decls = (bagToList tyvars, bagToList tycons, bagToList classes)
290 (tyvars, tycons, classes) = foldBag union3 get_binders1
291 (emptyBag,emptyBag,emptyBag)
294 union3 (a1,a2,a3) (b1,b2,b3)
295 = (a1 `unionBags` b1, a2 `unionBags` b2, a3 `unionBags` b3)
297 get_binders1 (TyD (TyData _ name tyvars _ _ _ _))
298 = (listToBag tyvars, unitBag (name,Nothing), emptyBag)
299 get_binders1 (TyD (TyNew _ name tyvars _ _ _ _))
300 = (listToBag tyvars, unitBag (name,Nothing), emptyBag)
301 get_binders1 (TyD (TySynonym name tyvars _ _))
302 = (listToBag tyvars, unitBag (name, Just (length tyvars)), emptyBag)
303 get_binders1 (ClD (ClassDecl _ name tyvar sigs _ _ _))
304 = (unitBag tyvar `unionBags` sigs_tvs sigs,
305 emptyBag, unitBag name)
307 sigs_tvs sigs = unionManyBags (map sig_tvs sigs)
309 sig_tvs (ClassOpSig _ ty _ _) = pty_tvs ty
310 pty_tvs (HsForAllTy tvs _ _) = listToBag tvs -- tvs doesn't include the class tyvar
311 pty_tvs other = emptyBag
316 typeCycleErr syn_cycles sty
317 = ppAboves (map (pp_cycle sty "Cycle in type declarations ...") syn_cycles)
319 classCycleErr cls_cycles sty
320 = ppAboves (map (pp_cycle sty "Cycle in class declarations ...") cls_cycles)
322 pp_cycle sty str things
324 4 (ppAboves (map pp_thing things))
326 pp_thing (pp_name, loc)
327 = ppCat [pp_name, ppr sty loc]