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(..), ConDetails(..), BangType(..),
16 ClassDecl(..), HsType(..), HsTyVar, DefaultDecl, InstDecl,
17 IfaceSig, Sig(..), MonoBinds, Fake, InPat, HsBinds(..), HsExpr, NewOrData,
20 import RnHsSyn ( RenamedTyDecl(..), RenamedClassDecl(..), SYN_IE(RenamedHsDecl)
22 import TcHsSyn ( SYN_IE(TcHsBinds) )
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 )
32 import TcType ( TcIdOcc(..) )
35 import Class ( SYN_IE(Class) )
36 import Digraph ( stronglyConnComp, SCC(..) )
37 import Name ( Name, getSrcLoc, isTvOcc, nameOccName )
40 import Maybes ( mapMaybe )
41 import UniqSet ( SYN_IE(UniqSet), emptyUniqSet,
42 unitUniqSet, unionUniqSets,
43 unionManyUniqSets, uniqSetToList )
44 import SrcLoc ( SrcLoc )
45 import TyCon ( TyCon, SYN_IE(Arity) )
46 import Unique ( Unique, Uniquable(..) )
47 import Util ( panic{-, pprTrace-} )
54 tcTyAndClassDecls1 :: TcEnv s -> InstanceMapper -- Knot tying stuff
58 tcTyAndClassDecls1 unf_env inst_mapper decls
59 = sortByDependency decls `thenTc` \ groups ->
60 tcGroups unf_env inst_mapper groups
62 tcGroups unf_env inst_mapper []
63 = tcGetEnv `thenNF_Tc` \ env ->
66 tcGroups unf_env inst_mapper (group:groups)
67 = tcGroup unf_env inst_mapper group `thenTc` \ new_env ->
69 -- Extend the environment using the new tycons and classes
72 -- Do the remaining groups
73 tcGroups unf_env inst_mapper groups
79 tcGroup :: TcEnv s -> InstanceMapper -> Bag RenamedHsDecl -> TcM s (TcEnv s)
80 tcGroup unf_env inst_mapper 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 unf_env 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 :: TcEnv s -> InstanceMapper
126 -> TcM s (Bag TyCon, Bag Class)
128 tcDecl unf_env inst_mapper (TyD decl)
129 = tcTyDecl decl `thenTc` \ tycon ->
130 returnTc (unitBag tycon, emptyBag)
132 tcDecl unf_env inst_mapper (ClD decl)
133 = tcClassDecl1 unf_env 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 = stronglyConnComp (filter is_syn_decl edges)
144 syn_cycles = [ decls | CyclicSCC decls <- syn_sccs]
147 checkTc (null syn_cycles) (typeCycleErr syn_cycles) `thenTc_`
149 let -- CHECK FOR CLASS CYCLES
150 cls_sccs = stronglyConnComp (filter is_cls_decl edges)
151 cls_cycles = [ decls | CyclicSCC decls <- cls_sccs]
154 checkTc (null cls_cycles) (classCycleErr cls_cycles) `thenTc_`
156 -- DO THE MAIN DEPENDENCY ANALYSIS
158 decl_sccs = stronglyConnComp (filter is_ty_cls_decl edges)
159 scc_bags = map bag_acyclic decl_sccs
164 edges = mapMaybe mk_edges decls
166 bag_acyclic (AcyclicSCC scc) = unitBag scc
167 bag_acyclic (CyclicSCC sccs) = listToBag sccs
169 is_syn_decl (TyD (TySynonym _ _ _ _), _, _) = True
170 is_syn_decl _ = False
172 is_ty_cls_decl (TyD _, _, _) = True
173 is_ty_cls_decl (ClD _, _, _) = True
174 is_ty_cls_decl other = False
176 is_cls_decl (ClD _, _, _) = True
177 is_cls_decl other = False
180 Edges in Type/Class decls
181 ~~~~~~~~~~~~~~~~~~~~~~~~~
183 mk_edges decl@(TyD (TyData _ ctxt name _ condecls derivs _ _))
184 = Just (decl, uniqueOf name, uniqSetToList (get_ctxt ctxt `unionUniqSets`
185 get_cons condecls `unionUniqSets`
188 mk_edges decl@(TyD (TySynonym name _ rhs _))
189 = Just (decl, uniqueOf name, uniqSetToList (get_ty rhs))
191 mk_edges decl@(ClD (ClassDecl ctxt name _ sigs _ _ _))
192 = Just (decl, uniqueOf name, uniqSetToList (get_ctxt ctxt `unionUniqSets`
195 mk_edges other_decl = Nothing
197 get_ctxt ctxt = unionManyUniqSets (map (set_name.fst) ctxt)
199 get_deriv Nothing = emptyUniqSet
200 get_deriv (Just clss) = unionManyUniqSets (map set_name clss)
202 get_cons cons = unionManyUniqSets (map get_con cons)
204 get_con (ConDecl _ ctxt details _)
205 = get_ctxt ctxt `unionUniqSets` get_con_details details
207 get_con_details (VanillaCon btys) = unionManyUniqSets (map get_bty btys)
208 get_con_details (InfixCon bty1 bty2) = unionUniqSets (get_bty bty1) (get_bty bty2)
209 get_con_details (NewCon ty) = get_ty ty
210 get_con_details (RecCon nbtys) = unionManyUniqSets (map (get_bty.snd) nbtys)
212 get_bty (Banged ty) = get_ty ty
213 get_bty (Unbanged ty) = get_ty ty
215 get_ty (MonoTyVar name)
216 = if isTvOcc (nameOccName name) then emptyUniqSet else set_name name
217 get_ty (MonoTyApp ty1 ty2)
218 = unionUniqSets (get_ty ty1) (get_ty ty2)
219 get_ty (MonoFunTy ty1 ty2)
220 = unionUniqSets (get_ty ty1) (get_ty ty2)
221 get_ty (MonoListTy tc ty)
222 = set_name tc `unionUniqSets` get_ty ty
223 get_ty (MonoTupleTy tc tys)
224 = set_name tc `unionUniqSets` get_tys tys
225 get_ty (HsForAllTy _ ctxt mty)
226 = get_ctxt ctxt `unionUniqSets` get_ty mty
227 get_ty other = panic "TcTyClsDecls:get_ty"
230 = unionManyUniqSets (map get_ty tys)
233 = unionManyUniqSets (map get_sig sigs)
235 get_sig (ClassOpSig _ _ ty _) = get_ty ty
236 get_sig other = panic "TcTyClsDecls:get_sig"
238 set_name name = unitUniqSet (uniqueOf name)
240 set_to_bag set = listToBag (uniqSetToList set)
246 Extract *binding* names from type and class decls. Type variables are
247 bound in type, data, newtype and class declarations and the polytypes
248 in the class op sigs.
250 Why do we need to grab all these type variables at once, including
251 those locally-quantified type variables in class op signatures?
252 Because we can only commit to the final kind of a type variable when
253 we've completed the mutually recursive group. For example:
256 op :: D b => a -> b -> b
259 bop :: (Monad c) => ...
261 Here, the kind of the locally-polymorphic type variable "b"
262 depends on *all the uses of class D*. For example, the use of
263 Monad c in bop's type signature means that D must have kind Type->Type.
267 get_binders :: Bag RenamedHsDecl
268 -> ([HsTyVar Name], -- TyVars; no dups
269 [(Name, Maybe Arity)], -- Tycons; no dups; arities for synonyms
270 [Name]) -- Classes; no dups
272 get_binders decls = (bagToList tyvars, bagToList tycons, bagToList classes)
274 (tyvars, tycons, classes) = foldBag union3 get_binders1
275 (emptyBag,emptyBag,emptyBag)
278 union3 (a1,a2,a3) (b1,b2,b3)
279 = (a1 `unionBags` b1, a2 `unionBags` b2, a3 `unionBags` b3)
281 get_binders1 (TyD (TyData _ _ name tyvars _ _ _ _))
282 = (listToBag tyvars, unitBag (name,Nothing), emptyBag)
283 get_binders1 (TyD (TySynonym name tyvars _ _))
284 = (listToBag tyvars, unitBag (name, Just (length tyvars)), emptyBag)
285 get_binders1 (ClD (ClassDecl _ name tyvar sigs _ _ _))
286 = (unitBag tyvar `unionBags` sigs_tvs sigs,
287 emptyBag, unitBag name)
289 sigs_tvs sigs = unionManyBags (map sig_tvs sigs)
291 sig_tvs (ClassOpSig _ _ ty _) = pty_tvs ty
292 pty_tvs (HsForAllTy tvs _ _) = listToBag tvs -- tvs doesn't include the class tyvar
293 pty_tvs other = emptyBag
298 typeCycleErr syn_cycles sty
299 = vcat (map (pp_cycle sty "Cycle in type declarations ...") syn_cycles)
301 classCycleErr cls_cycles sty
302 = vcat (map (pp_cycle sty "Cycle in class declarations ...") cls_cycles)
304 pp_cycle sty str decls
306 4 (vcat (map pp_decl decls))
309 = hsep [ppr sty name, ppr sty (getSrcLoc name)]
311 name = hsDeclName decl