2 % (c) The AQUA Project, Glasgow University, 1996-1998
4 \section[TcTyClsDecls]{Typecheck type and class declarations}
11 #include "HsVersions.h"
13 import HsSyn ( HsDecl(..), TyClDecl(..),
14 HsType(..), HsTyVarBndr,
15 ConDecl(..), ConDetails(..), BangType(..),
16 Sig(..), HsPred(..), HsTupCon(..),
17 tyClDeclName, isClassDecl, isSynDecl
19 import RnHsSyn ( RenamedHsDecl, RenamedTyClDecl, listTyCon_name )
20 import BasicTypes ( RecFlag(..), NewOrData(..), Arity )
23 import TcClassDcl ( kcClassDecl, tcClassDecl1 )
24 import TcEnv ( ValueEnv, TcTyThing(..),
25 tcExtendTypeEnv, getEnvAllTyCons
27 import TcTyDecls ( tcTyDecl, kcTyDecl )
28 import TcMonoType ( kcHsTyVar )
29 import TcType ( TcKind, newKindVar, newKindVars, kindToTcKind, zonkTcKindToKind )
31 import Type ( mkArrowKind, boxedTypeKind )
33 import Class ( Class )
34 import Var ( TyVar, tyVarKind )
38 import Digraph ( stronglyConnComp, SCC(..) )
39 import Name ( Name, NamedThing(..), getSrcLoc, isTvOcc, nameOccName )
41 import Maybes ( mapMaybe, catMaybes, expectJust )
42 import UniqSet ( UniqSet, emptyUniqSet,
43 unitUniqSet, unionUniqSets,
44 unionManyUniqSets, uniqSetToList )
45 import ErrUtils ( Message )
46 import SrcLoc ( SrcLoc )
47 import TyCon ( TyCon, ArgVrcs )
48 import Variance ( calcTyConArgVrcs )
49 import Unique ( Unique, Uniquable(..) )
50 import UniqFM ( listToUFM, lookupUFM )
56 tcTyAndClassDecls :: ValueEnv -- Knot tying stuff
60 tcTyAndClassDecls unf_env decls
61 = sortByDependency decls `thenTc` \ groups ->
62 tcGroups unf_env groups
65 = tcGetEnv `thenNF_Tc` \ env ->
68 tcGroups unf_env (group:groups)
69 = tcGroup unf_env group `thenTc` \ env ->
71 tcGroups unf_env groups
77 The knot-tying parameters: @rec_tyclss@ is an alist mapping @Name@s to
78 @TcTyThing@s. @rec_vrcs@ is a finite map from @Name@s to @ArgVrcs@s.
81 tcGroup :: ValueEnv -> SCC RenamedTyClDecl -> TcM s TcEnv
84 mapNF_Tc getTyBinding1 decls `thenNF_Tc` \ ty_env_stuff1 ->
85 tcExtendTypeEnv ty_env_stuff1 (mapTc kcDecl decls) `thenTc_`
88 -- traceTc (ppr (map fst ty_env_stuff1)) `thenTc_`
89 fixTc ( \ ~(rec_tyclss, _) ->
91 rec_env = listToUFM rec_tyclss
92 rec_tycons = getEnvAllTyCons rec_tyclss
93 rec_vrcs = calcTyConArgVrcs rec_tycons
97 mapNF_Tc (getTyBinding2 rec_env) ty_env_stuff1 `thenNF_Tc` \ ty_env_stuff2 ->
98 tcExtendTypeEnv ty_env_stuff2 $
99 mapTc (tcDecl is_rec_group unf_env rec_vrcs) decls `thenTc` \ tyclss ->
101 tcGetEnv `thenTc` \ env ->
102 returnTc (tyclss, env)
103 ) `thenTc` \ (_, env) ->
104 -- traceTc (text "done" <+> ppr (map fst ty_env_stuff1)) `thenTc_`
107 is_rec_group = case scc of
108 AcyclicSCC _ -> NonRecursive
109 CyclicSCC _ -> Recursive
112 AcyclicSCC decl -> [decl]
113 CyclicSCC decls -> decls
116 Dealing with one decl
117 ~~~~~~~~~~~~~~~~~~~~~
120 = tcAddDeclCtxt decl $
121 if isClassDecl decl then
126 tcDecl :: RecFlag -- True => recursive group
127 -> ValueEnv -> FiniteMap Name ArgVrcs
128 -> RenamedTyClDecl -> TcM s (Name, TcTyThing)
130 tcDecl is_rec_group unf_env vrcs_env decl
131 = tcAddDeclCtxt decl $
132 if isClassDecl decl then
133 tcClassDecl1 unf_env vrcs_env decl
135 tcTyDecl is_rec_group vrcs_env decl
138 tcAddDeclCtxt decl thing_inside
145 (ClassDecl _ name _ _ _ _ _ _ _ _ _ loc) -> (name, loc, "class")
146 (TySynonym name _ _ loc) -> (name, loc, "type synonym")
147 (TyData NewType _ name _ _ _ _ _ loc) -> (name, loc, "data type")
148 (TyData DataType _ name _ _ _ _ _ loc) -> (name, loc, "newtype")
150 ctxt = hsep [ptext SLIT("In the"), text thing,
151 ptext SLIT("declaration for"), quotes (ppr name)]
157 Extract *binding* names from type and class decls. Type variables are
158 bound in type, data, newtype and class declarations,
159 *and* the polytypes in the class op sigs.
160 *and* the existentially quantified contexts in datacon decls
162 Why do we need to grab all these type variables at once, including
163 those locally-quantified type variables in class op signatures?
165 [Incidentally, this only works because the names are all unique by now.]
167 Because we can only commit to the final kind of a type variable when
168 we've completed the mutually recursive group. For example:
171 op :: D b => a -> b -> b
174 bop :: (Monad c) => ...
176 Here, the kind of the locally-polymorphic type variable "b"
177 depends on *all the uses of class D*. For example, the use of
178 Monad c in bop's type signature means that D must have kind Type->Type.
180 [April 00: looks as if we've dropped this subtlety; I'm not sure when]
183 getTyBinding1 :: RenamedTyClDecl -> NF_TcM s (Name, (TcKind, TcTyThing))
184 getTyBinding1 (TySynonym name tyvars _ _)
185 = mapNF_Tc kcHsTyVar tyvars `thenNF_Tc` \ arg_kinds ->
186 newKindVar `thenNF_Tc` \ result_kind ->
187 returnNF_Tc (name, (foldr mkArrowKind result_kind arg_kinds,
188 ASynTyCon (pprPanic "ATyCon: syn" (ppr name)) (length tyvars)))
190 getTyBinding1 (TyData _ _ name tyvars _ _ _ _ _)
191 = mapNF_Tc kcHsTyVar tyvars `thenNF_Tc` \ arg_kinds ->
192 returnNF_Tc (name, (foldr mkArrowKind boxedTypeKind arg_kinds,
193 ADataTyCon (error "ATyCon: data")))
195 getTyBinding1 (ClassDecl _ name tyvars _ _ _ _ _ _ _ _ _)
196 = mapNF_Tc kcHsTyVar tyvars `thenNF_Tc` \ arg_kinds ->
197 returnNF_Tc (name, (foldr mkArrowKind boxedTypeKind arg_kinds,
198 AClass (pprPanic "AClass" (ppr name)) (length tyvars)))
200 -- Zonk the kind to its final form, and lookup the
201 -- recursive tycon/class
202 getTyBinding2 rec_env (name, (tc_kind, thing))
203 = zonkTcKindToKind tc_kind `thenNF_Tc` \ kind ->
204 returnNF_Tc (name, (kind, mk_thing thing (lookupUFM rec_env name)))
206 mk_thing (ADataTyCon _) ~(Just (ADataTyCon tc)) = ADataTyCon tc
207 mk_thing (ASynTyCon _ arity) ~(Just (ASynTyCon tc _)) = ASynTyCon tc arity
208 mk_thing (AClass _ arity) ~(Just (AClass cls _)) = AClass cls arity
212 %************************************************************************
214 \subsection{Dependency analysis}
216 %************************************************************************
221 sortByDependency :: [RenamedHsDecl] -> TcM s [SCC RenamedTyClDecl]
222 sortByDependency decls
223 = let -- CHECK FOR CLASS CYCLES
224 cls_sccs = stronglyConnComp (mapMaybe mk_cls_edges tycl_decls)
225 cls_cycles = [ decls | CyclicSCC decls <- cls_sccs]
227 checkTc (null cls_cycles) (classCycleErr cls_cycles) `thenTc_`
229 let -- CHECK FOR SYNONYM CYCLES
230 syn_sccs = stronglyConnComp (filter is_syn_decl edges)
231 syn_cycles = [ decls | CyclicSCC decls <- syn_sccs]
234 checkTc (null syn_cycles) (typeCycleErr syn_cycles) `thenTc_`
236 -- DO THE MAIN DEPENDENCY ANALYSIS
238 decl_sccs = stronglyConnComp edges
242 tycl_decls = [d | TyClD d <- decls]
243 edges = map mk_edges tycl_decls
245 is_syn_decl (d, _, _) = isSynDecl d
248 Edges in Type/Class decls
249 ~~~~~~~~~~~~~~~~~~~~~~~~~
252 ----------------------------------------------------
253 -- mk_cls_edges looks only at the context of class decls
254 -- Its used when we are figuring out if there's a cycle in the
255 -- superclass hierarchy
257 mk_cls_edges :: RenamedTyClDecl -> Maybe (RenamedTyClDecl, Unique, [Unique])
259 mk_cls_edges decl@(ClassDecl ctxt name _ _ _ _ _ _ _ _ _ _)
260 = Just (decl, getUnique name, map getUnique (catMaybes (map get_clas ctxt)))
261 mk_cls_edges other_decl
264 ----------------------------------------------------
265 mk_edges :: RenamedTyClDecl -> (RenamedTyClDecl, Unique, [Unique])
267 mk_edges decl@(TyData _ ctxt name _ condecls _ derivs _ _)
268 = (decl, getUnique name, uniqSetToList (get_ctxt ctxt `unionUniqSets`
269 get_cons condecls `unionUniqSets`
272 mk_edges decl@(TySynonym name _ rhs _)
273 = (decl, getUnique name, uniqSetToList (get_ty rhs))
275 mk_edges decl@(ClassDecl ctxt name _ _ sigs _ _ _ _ _ _ _)
276 = (decl, getUnique name, uniqSetToList (get_ctxt ctxt `unionUniqSets`
280 ----------------------------------------------------
281 get_ctxt ctxt = unionManyUniqSets (map set_name (catMaybes (map get_clas ctxt)))
282 get_clas (HsPClass clas _) = Just clas
285 ----------------------------------------------------
286 get_deriv Nothing = emptyUniqSet
287 get_deriv (Just clss) = unionManyUniqSets (map set_name clss)
289 ----------------------------------------------------
290 get_cons cons = unionManyUniqSets (map get_con cons)
292 ----------------------------------------------------
293 get_con (ConDecl _ _ _ ctxt details _)
294 = get_ctxt ctxt `unionUniqSets` get_con_details details
296 ----------------------------------------------------
297 get_con_details (VanillaCon btys) = unionManyUniqSets (map get_bty btys)
298 get_con_details (InfixCon bty1 bty2) = unionUniqSets (get_bty bty1) (get_bty bty2)
299 get_con_details (NewCon ty _) = get_ty ty
300 get_con_details (RecCon nbtys) = unionManyUniqSets (map (get_bty.snd) nbtys)
302 ----------------------------------------------------
303 get_bty (Banged ty) = get_ty ty
304 get_bty (Unbanged ty) = get_ty ty
305 get_bty (Unpacked ty) = get_ty ty
307 ----------------------------------------------------
308 get_ty (HsTyVar name) | isTvOcc (nameOccName name) = emptyUniqSet
309 | otherwise = set_name name
310 get_ty (HsAppTy ty1 ty2) = unionUniqSets (get_ty ty1) (get_ty ty2)
311 get_ty (HsFunTy ty1 ty2) = unionUniqSets (get_ty ty1) (get_ty ty2)
312 get_ty (HsListTy ty) = set_name listTyCon_name `unionUniqSets` get_ty ty
313 get_ty (HsTupleTy (HsTupCon n _) tys) = set_name n `unionUniqSets` get_tys tys
314 get_ty (HsUsgTy _ ty) = get_ty ty
315 get_ty (HsUsgForAllTy _ ty) = get_ty ty
316 get_ty (HsForAllTy _ ctxt mty) = get_ctxt ctxt `unionUniqSets` get_ty mty
317 get_ty (HsPredTy (HsPClass name _)) = set_name name
318 get_ty (HsPredTy (HsPIParam _ _)) = emptyUniqSet -- I think
320 ----------------------------------------------------
321 get_tys tys = unionManyUniqSets (map get_ty tys)
323 ----------------------------------------------------
325 = unionManyUniqSets (map get_sig sigs)
327 get_sig (ClassOpSig _ _ _ ty _) = get_ty ty
328 get_sig (FixSig _) = emptyUniqSet
329 get_sig other = panic "TcTyClsDecls:get_sig"
331 ----------------------------------------------------
332 set_name name = unitUniqSet (getUnique name)
333 set_to_bag set = listToBag (uniqSetToList set)
338 typeCycleErr, classCycleErr :: [[RenamedTyClDecl]] -> Message
340 typeCycleErr syn_cycles
341 = vcat (map (pp_cycle "Cycle in type declarations:") syn_cycles)
343 classCycleErr cls_cycles
344 = vcat (map (pp_cycle "Cycle in class declarations:") cls_cycles)
348 4 (vcat (map pp_decl decls))
351 = hsep [quotes (ppr name), ptext SLIT("at"), ppr (getSrcLoc name)]
353 name = tyClDeclName decl