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(..),
15 ConDecl(..), ConDetails(..), BangType(..),
17 tyClDeclName, isClassDecl, isSynDecl
19 import RnHsSyn ( RenamedHsDecl, RenamedTyClDecl, listTyCon_name, tupleTyCon_name )
20 import BasicTypes ( RecFlag(..), NewOrData(..), Arity )
23 import Inst ( InstanceMapper )
24 import TcClassDcl ( kcClassDecl, tcClassDecl1 )
25 import TcEnv ( ValueEnv, TcTyThing(..),
28 import TcTyDecls ( tcTyDecl, kcTyDecl )
29 import TcMonoType ( kcHsTyVar )
30 import TcType ( TcKind, newKindVar, newKindVars, kindToTcKind, zonkTcKindToKind )
32 import Type ( mkArrowKind, boxedTypeKind )
33 import Class ( Class, classBigSig )
34 import Var ( tyVarKind )
36 import Digraph ( stronglyConnComp, SCC(..) )
37 import Name ( Name, NamedThing(..), getSrcLoc, isTvOcc, nameOccName )
39 import Maybes ( mapMaybe )
40 import UniqSet ( UniqSet, emptyUniqSet,
41 unitUniqSet, unionUniqSets,
42 unionManyUniqSets, uniqSetToList )
43 import ErrUtils ( Message )
44 import SrcLoc ( SrcLoc )
45 import TyCon ( TyCon )
46 import Unique ( Unique, Uniquable(..) )
47 import UniqFM ( listToUFM, lookupUFM )
53 tcTyAndClassDecls :: ValueEnv -> InstanceMapper -- Knot tying stuff
57 tcTyAndClassDecls unf_env inst_mapper decls
58 = sortByDependency decls `thenTc` \ groups ->
59 tcGroups unf_env inst_mapper groups
61 tcGroups unf_env inst_mapper []
62 = tcGetEnv `thenNF_Tc` \ env ->
65 tcGroups unf_env inst_mapper (group:groups)
66 = tcGroup unf_env inst_mapper group `thenTc` \ env ->
68 tcGroups unf_env inst_mapper groups
75 tcGroup :: ValueEnv -> InstanceMapper -> SCC RenamedTyClDecl -> TcM s TcEnv
76 tcGroup unf_env inst_mapper scc
78 mapNF_Tc getTyBinding1 decls `thenNF_Tc` \ ty_env_stuff1 ->
79 tcExtendTypeEnv ty_env_stuff1 (mapTc kcDecl decls) `thenTc_`
82 -- traceTc (ppr (map fst ty_env_stuff1)) `thenTc_`
83 fixTc ( \ ~(rec_tyclss, _) ->
85 rec_env = listToUFM rec_tyclss
89 mapNF_Tc (getTyBinding2 rec_env) ty_env_stuff1 `thenNF_Tc` \ ty_env_stuff2 ->
90 tcExtendTypeEnv ty_env_stuff2 $
91 mapTc (tcDecl is_rec_group unf_env inst_mapper) decls `thenTc` \ tyclss ->
93 tcGetEnv `thenTc` \ env ->
94 returnTc (tyclss, env)
95 ) `thenTc` \ (_, env) ->
96 -- traceTc (text "done" <+> ppr (map fst ty_env_stuff1)) `thenTc_`
99 is_rec_group = case scc of
100 AcyclicSCC _ -> NonRecursive
101 CyclicSCC _ -> Recursive
104 AcyclicSCC decl -> [decl]
105 CyclicSCC decls -> decls
108 Dealing with one decl
109 ~~~~~~~~~~~~~~~~~~~~~
112 = tcAddDeclCtxt decl $
113 if isClassDecl decl then
118 tcDecl :: RecFlag -- True => recursive group
119 -> ValueEnv -> InstanceMapper
120 -> RenamedTyClDecl -> TcM s (Name, TcTyThing)
122 tcDecl is_rec_group unf_env inst_mapper decl
123 = tcAddDeclCtxt decl $
124 -- traceTc (text "Starting" <+> ppr name) `thenTc_`
125 if isClassDecl decl then
126 tcClassDecl1 unf_env inst_mapper decl `thenTc` \ clas ->
127 -- traceTc (text "Finished" <+> ppr name) `thenTc_`
128 returnTc (getName clas, AClass clas)
130 tcTyDecl is_rec_group decl `thenTc` \ tycon ->
131 -- traceTc (text "Finished" <+> ppr name) `thenTc_`
132 returnTc (getName tycon, ATyCon tycon)
135 name = tyClDeclName 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.
182 getTyBinding1 :: RenamedTyClDecl -> NF_TcM s (Name, (TcKind, Maybe Arity, TcTyThing))
183 getTyBinding1 (TySynonym name tyvars _ _)
184 = mapNF_Tc kcHsTyVar tyvars `thenNF_Tc` \ arg_kinds ->
185 newKindVar `thenNF_Tc` \ result_kind ->
186 returnNF_Tc (name, (foldr mkArrowKind result_kind arg_kinds,
187 Just (length tyvars),
188 ATyCon (pprPanic "ATyCon: syn" (ppr name))))
190 getTyBinding1 (TyData _ _ name tyvars _ _ _ _)
191 = mapNF_Tc kcHsTyVar tyvars `thenNF_Tc` \ arg_kinds ->
192 returnNF_Tc (name, (foldr mkArrowKind boxedTypeKind arg_kinds,
194 ATyCon (error "ATyCon: data")))
196 getTyBinding1 (ClassDecl _ name tyvars _ _ _ _ _ _)
197 = mapNF_Tc kcHsTyVar tyvars `thenNF_Tc` \ arg_kinds ->
198 returnNF_Tc (name, (foldr mkArrowKind boxedTypeKind arg_kinds,
199 Just (length tyvars),
200 AClass (error "AClass")))
202 -- Zonk the kind to its final form, and lookup the
203 -- recursive tycon/class
204 getTyBinding2 rec_env (name, (tc_kind, maybe_arity, thing))
205 = zonkTcKindToKind tc_kind `thenNF_Tc` \ kind ->
206 returnNF_Tc (name, (kind, maybe_arity, mk_thing thing (lookupUFM rec_env name)))
208 mk_thing (ATyCon _) ~(Just (ATyCon tc)) = ATyCon tc
209 mk_thing (AClass _) ~(Just (AClass cls)) = AClass cls
213 %************************************************************************
215 \subsection{Dependency analysis}
217 %************************************************************************
222 sortByDependency :: [RenamedHsDecl] -> TcM s [SCC RenamedTyClDecl]
223 sortByDependency decls
224 = let -- CHECK FOR CLASS CYCLES
225 cls_sccs = stronglyConnComp (mapMaybe mk_cls_edges tycl_decls)
226 cls_cycles = [ decls | CyclicSCC decls <- cls_sccs]
228 checkTc (null cls_cycles) (classCycleErr cls_cycles) `thenTc_`
230 let -- CHECK FOR SYNONYM CYCLES
231 syn_sccs = stronglyConnComp (filter is_syn_decl edges)
232 syn_cycles = [ decls | CyclicSCC decls <- syn_sccs]
235 checkTc (null syn_cycles) (typeCycleErr syn_cycles) `thenTc_`
237 -- DO THE MAIN DEPENDENCY ANALYSIS
239 decl_sccs = stronglyConnComp edges
243 tycl_decls = [d | TyClD d <- decls]
244 edges = map mk_edges tycl_decls
246 is_syn_decl (d, _, _) = isSynDecl d
247 is_cls_decl (d, _, _) = isClassDecl d
250 Edges in Type/Class decls
251 ~~~~~~~~~~~~~~~~~~~~~~~~~
254 ----------------------------------------------------
255 -- mk_cls_edges looks only at the context of class decls
256 -- Its used when we are figuring out if there's a cycle in the
257 -- superclass hierarchy
259 mk_cls_edges :: RenamedTyClDecl -> Maybe (RenamedTyClDecl, Unique, [Unique])
261 mk_cls_edges decl@(ClassDecl ctxt name _ _ _ _ _ _ _)
262 = Just (decl, getUnique name, map (getUnique . fst) ctxt)
263 mk_cls_edges other_decl
266 ----------------------------------------------------
267 mk_edges :: RenamedTyClDecl -> (RenamedTyClDecl, Unique, [Unique])
269 mk_edges decl@(TyData _ ctxt name _ condecls derivs _ _)
270 = (decl, getUnique name, uniqSetToList (get_ctxt ctxt `unionUniqSets`
271 get_cons condecls `unionUniqSets`
274 mk_edges decl@(TySynonym name _ rhs _)
275 = (decl, getUnique name, uniqSetToList (get_ty rhs))
277 mk_edges decl@(ClassDecl ctxt name _ sigs _ _ _ _ _)
278 = (decl, getUnique name, uniqSetToList (get_ctxt ctxt `unionUniqSets`
282 ----------------------------------------------------
283 get_ctxt ctxt = unionManyUniqSets (map (set_name.fst) ctxt)
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 (MonoTyVar name)
309 = if isTvOcc (nameOccName name) then emptyUniqSet else set_name name
310 get_ty (MonoTyApp ty1 ty2)
311 = unionUniqSets (get_ty ty1) (get_ty ty2)
312 get_ty (MonoFunTy ty1 ty2)
313 = unionUniqSets (get_ty ty1) (get_ty ty2)
314 get_ty (MonoListTy ty)
315 = set_name listTyCon_name `unionUniqSets` get_ty ty
316 get_ty (MonoTupleTy tys boxed)
317 = set_name (tupleTyCon_name boxed (length tys)) `unionUniqSets` get_tys tys
318 get_ty (HsForAllTy _ ctxt mty)
319 = get_ctxt ctxt `unionUniqSets` get_ty mty
320 get_ty other = panic "TcTyClsDecls:get_ty"
322 ----------------------------------------------------
324 = unionManyUniqSets (map get_ty tys)
326 ----------------------------------------------------
328 = unionManyUniqSets (map get_sig sigs)
330 get_sig (ClassOpSig _ _ ty _) = get_ty ty
331 get_sig other = panic "TcTyClsDecls:get_sig"
333 ----------------------------------------------------
334 set_name name = unitUniqSet (getUnique name)
335 set_to_bag set = listToBag (uniqSetToList set)
340 typeCycleErr, classCycleErr :: [[RenamedTyClDecl]] -> Message
342 typeCycleErr syn_cycles
343 = vcat (map (pp_cycle "Cycle in type declarations:") syn_cycles)
345 classCycleErr cls_cycles
346 = vcat (map (pp_cycle "Cycle in class declarations:") cls_cycles)
350 4 (vcat (map pp_decl decls))
353 = hsep [quotes (ppr name), ptext SLIT("at"), ppr (getSrcLoc name)]
355 name = tyClDeclName decl