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(..),
26 tcExtendTypeEnv, getAllEnvTyCons
28 import TcTyDecls ( tcTyDecl, kcTyDecl )
29 import TcMonoType ( kcHsTyVar )
30 import TcType ( TcKind, newKindVar, newKindVars, kindToTcKind, zonkTcKindToKind )
32 import Type ( mkArrowKind, boxedTypeKind )
34 import Class ( Class )
35 import Var ( TyVar, tyVarKind )
39 import Digraph ( stronglyConnComp, SCC(..) )
40 import Name ( Name, NamedThing(..), getSrcLoc, isTvOcc, nameOccName )
42 import Maybes ( mapMaybe, catMaybes, expectJust )
43 import UniqSet ( UniqSet, emptyUniqSet,
44 unitUniqSet, unionUniqSets,
45 unionManyUniqSets, uniqSetToList )
46 import ErrUtils ( Message )
47 import SrcLoc ( SrcLoc )
48 import TyCon ( TyCon, ArgVrcs )
49 import Variance ( calcTyConArgVrcs )
50 import Unique ( Unique, Uniquable(..) )
51 import UniqFM ( listToUFM, lookupUFM )
57 tcTyAndClassDecls :: ValueEnv -> InstanceMapper -- Knot tying stuff
61 tcTyAndClassDecls unf_env inst_mapper decls
62 = sortByDependency decls `thenTc` \ groups ->
63 tcGroups unf_env inst_mapper groups
65 tcGroups unf_env inst_mapper []
66 = tcGetEnv `thenNF_Tc` \ env ->
69 tcGroups unf_env inst_mapper (group:groups)
70 = tcGroup unf_env inst_mapper group `thenTc` \ env ->
72 tcGroups unf_env inst_mapper groups
78 The knot-tying parameters: @rec_tyclss@ is an alist mapping @Name@s to
79 @TcTyThing@s. @rec_vrcs@ is a finite map from @Name@s to @ArgVrcs@s.
82 tcGroup :: ValueEnv -> InstanceMapper -> SCC RenamedTyClDecl -> TcM s TcEnv
83 tcGroup unf_env inst_mapper scc
85 mapNF_Tc getTyBinding1 decls `thenNF_Tc` \ ty_env_stuff1 ->
86 tcExtendTypeEnv ty_env_stuff1 (mapTc kcDecl decls) `thenTc_`
89 -- traceTc (ppr (map fst ty_env_stuff1)) `thenTc_`
90 fixTc ( \ ~(rec_tyclss, rec_vrcs, _) ->
92 rec_env = listToUFM rec_tyclss
96 mapNF_Tc (getTyBinding2 rec_env) ty_env_stuff1 `thenNF_Tc` \ ty_env_stuff2 ->
97 tcExtendTypeEnv ty_env_stuff2 $
98 mapTc (tcDecl is_rec_group unf_env inst_mapper rec_vrcs) decls
101 tcGetEnv `thenTc` \ env ->
103 tycons = getAllEnvTyCons env
104 vrcs = calcTyConArgVrcs tycons
107 returnTc (tyclss, vrcs, env)
108 ) `thenTc` \ (_, _, env) ->
109 -- traceTc (text "done" <+> ppr (map fst ty_env_stuff1)) `thenTc_`
112 is_rec_group = case scc of
113 AcyclicSCC _ -> NonRecursive
114 CyclicSCC _ -> Recursive
117 AcyclicSCC decl -> [decl]
118 CyclicSCC decls -> decls
121 Dealing with one decl
122 ~~~~~~~~~~~~~~~~~~~~~
125 = tcAddDeclCtxt decl $
126 if isClassDecl decl then
131 tcDecl :: RecFlag -- True => recursive group
132 -> ValueEnv -> InstanceMapper -> FiniteMap Name ArgVrcs
133 -> RenamedTyClDecl -> TcM s (Name, TcTyThing)
135 tcDecl is_rec_group unf_env inst_mapper vrcs_env decl
136 = tcAddDeclCtxt decl $
137 if isClassDecl decl then
138 tcClassDecl1 unf_env inst_mapper vrcs_env decl `thenTc` \ clas ->
139 returnTc (getName clas, AClass clas)
141 tcTyDecl is_rec_group vrcs_env decl `thenTc` \ tycon ->
142 returnTc (getName tycon, ATyCon tycon)
145 tcAddDeclCtxt decl thing_inside
152 (ClassDecl _ name _ _ _ _ _ _ _ _ _ loc) -> (name, loc, "class")
153 (TySynonym name _ _ loc) -> (name, loc, "type synonym")
154 (TyData NewType _ name _ _ _ _ loc) -> (name, loc, "data type")
155 (TyData DataType _ name _ _ _ _ loc) -> (name, loc, "newtype")
157 ctxt = hsep [ptext SLIT("In the"), text thing,
158 ptext SLIT("declaration for"), quotes (ppr name)]
164 Extract *binding* names from type and class decls. Type variables are
165 bound in type, data, newtype and class declarations,
166 *and* the polytypes in the class op sigs.
167 *and* the existentially quantified contexts in datacon decls
169 Why do we need to grab all these type variables at once, including
170 those locally-quantified type variables in class op signatures?
172 [Incidentally, this only works because the names are all unique by now.]
174 Because we can only commit to the final kind of a type variable when
175 we've completed the mutually recursive group. For example:
178 op :: D b => a -> b -> b
181 bop :: (Monad c) => ...
183 Here, the kind of the locally-polymorphic type variable "b"
184 depends on *all the uses of class D*. For example, the use of
185 Monad c in bop's type signature means that D must have kind Type->Type.
189 getTyBinding1 :: RenamedTyClDecl -> NF_TcM s (Name, (TcKind, Maybe Arity, TcTyThing))
190 getTyBinding1 (TySynonym name tyvars _ _)
191 = mapNF_Tc kcHsTyVar tyvars `thenNF_Tc` \ arg_kinds ->
192 newKindVar `thenNF_Tc` \ result_kind ->
193 returnNF_Tc (name, (foldr mkArrowKind result_kind arg_kinds,
194 Just (length tyvars),
195 ATyCon (pprPanic "ATyCon: syn" (ppr name))))
197 getTyBinding1 (TyData _ _ name tyvars _ _ _ _)
198 = mapNF_Tc kcHsTyVar tyvars `thenNF_Tc` \ arg_kinds ->
199 returnNF_Tc (name, (foldr mkArrowKind boxedTypeKind arg_kinds,
201 ATyCon (error "ATyCon: data")))
203 getTyBinding1 (ClassDecl _ name tyvars _ _ _ _ _ _ _ _ _)
204 = mapNF_Tc kcHsTyVar tyvars `thenNF_Tc` \ arg_kinds ->
205 returnNF_Tc (name, (foldr mkArrowKind boxedTypeKind arg_kinds,
206 Just (length tyvars),
207 AClass (error "AClass")))
209 -- Zonk the kind to its final form, and lookup the
210 -- recursive tycon/class
211 getTyBinding2 rec_env (name, (tc_kind, maybe_arity, thing))
212 = zonkTcKindToKind tc_kind `thenNF_Tc` \ kind ->
213 returnNF_Tc (name, (kind, maybe_arity, mk_thing thing (lookupUFM rec_env name)))
215 mk_thing (ATyCon _) ~(Just (ATyCon tc)) = ATyCon tc
216 mk_thing (AClass _) ~(Just (AClass cls)) = AClass cls
220 %************************************************************************
222 \subsection{Dependency analysis}
224 %************************************************************************
229 sortByDependency :: [RenamedHsDecl] -> TcM s [SCC RenamedTyClDecl]
230 sortByDependency decls
231 = let -- CHECK FOR CLASS CYCLES
232 cls_sccs = stronglyConnComp (mapMaybe mk_cls_edges tycl_decls)
233 cls_cycles = [ decls | CyclicSCC decls <- cls_sccs]
235 checkTc (null cls_cycles) (classCycleErr cls_cycles) `thenTc_`
237 let -- CHECK FOR SYNONYM CYCLES
238 syn_sccs = stronglyConnComp (filter is_syn_decl edges)
239 syn_cycles = [ decls | CyclicSCC decls <- syn_sccs]
242 checkTc (null syn_cycles) (typeCycleErr syn_cycles) `thenTc_`
244 -- DO THE MAIN DEPENDENCY ANALYSIS
246 decl_sccs = stronglyConnComp edges
250 tycl_decls = [d | TyClD d <- decls]
251 edges = map mk_edges tycl_decls
253 is_syn_decl (d, _, _) = isSynDecl d
256 Edges in Type/Class decls
257 ~~~~~~~~~~~~~~~~~~~~~~~~~
260 ----------------------------------------------------
261 -- mk_cls_edges looks only at the context of class decls
262 -- Its used when we are figuring out if there's a cycle in the
263 -- superclass hierarchy
265 mk_cls_edges :: RenamedTyClDecl -> Maybe (RenamedTyClDecl, Unique, [Unique])
267 mk_cls_edges decl@(ClassDecl ctxt name _ _ _ _ _ _ _ _ _ _)
268 = Just (decl, getUnique name, map getUnique (catMaybes (map get_clas ctxt)))
269 mk_cls_edges other_decl
272 ----------------------------------------------------
273 mk_edges :: RenamedTyClDecl -> (RenamedTyClDecl, Unique, [Unique])
275 mk_edges decl@(TyData _ ctxt name _ condecls derivs _ _)
276 = (decl, getUnique name, uniqSetToList (get_ctxt ctxt `unionUniqSets`
277 get_cons condecls `unionUniqSets`
280 mk_edges decl@(TySynonym name _ rhs _)
281 = (decl, getUnique name, uniqSetToList (get_ty rhs))
283 mk_edges decl@(ClassDecl ctxt name _ _ sigs _ _ _ _ _ _ _)
284 = (decl, getUnique name, uniqSetToList (get_ctxt ctxt `unionUniqSets`
288 ----------------------------------------------------
289 get_ctxt ctxt = unionManyUniqSets (map set_name (catMaybes (map get_clas ctxt)))
290 get_clas (HsPClass clas _) = Just clas
293 ----------------------------------------------------
294 get_deriv Nothing = emptyUniqSet
295 get_deriv (Just clss) = unionManyUniqSets (map set_name clss)
297 ----------------------------------------------------
298 get_cons cons = unionManyUniqSets (map get_con cons)
300 ----------------------------------------------------
301 get_con (ConDecl _ _ _ ctxt details _)
302 = get_ctxt ctxt `unionUniqSets` get_con_details details
304 ----------------------------------------------------
305 get_con_details (VanillaCon btys) = unionManyUniqSets (map get_bty btys)
306 get_con_details (InfixCon bty1 bty2) = unionUniqSets (get_bty bty1) (get_bty bty2)
307 get_con_details (NewCon ty _) = get_ty ty
308 get_con_details (RecCon nbtys) = unionManyUniqSets (map (get_bty.snd) nbtys)
310 ----------------------------------------------------
311 get_bty (Banged ty) = get_ty ty
312 get_bty (Unbanged ty) = get_ty ty
313 get_bty (Unpacked ty) = get_ty ty
315 ----------------------------------------------------
316 get_ty (MonoTyVar name)
317 = if isTvOcc (nameOccName name) then emptyUniqSet else set_name name
318 get_ty (MonoTyApp ty1 ty2)
319 = unionUniqSets (get_ty ty1) (get_ty ty2)
320 get_ty (MonoFunTy ty1 ty2)
321 = unionUniqSets (get_ty ty1) (get_ty ty2)
322 get_ty (MonoListTy ty)
323 = set_name listTyCon_name `unionUniqSets` get_ty ty
324 get_ty (MonoTupleTy tys boxed)
325 = set_name (tupleTyCon_name boxed (length tys)) `unionUniqSets` get_tys tys
326 get_ty (MonoUsgTy _ ty)
328 get_ty (MonoUsgForAllTy _ ty)
330 get_ty (HsForAllTy _ ctxt mty)
331 = get_ctxt ctxt `unionUniqSets` get_ty mty
332 get_ty (MonoDictTy name _)
334 get_ty (MonoIParamTy name _)
337 ----------------------------------------------------
339 = unionManyUniqSets (map get_ty tys)
341 ----------------------------------------------------
343 = unionManyUniqSets (map get_sig sigs)
345 get_sig (ClassOpSig _ _ _ ty _) = get_ty ty
346 get_sig (FixSig _) = emptyUniqSet
347 get_sig other = panic "TcTyClsDecls:get_sig"
349 ----------------------------------------------------
350 set_name name = unitUniqSet (getUnique name)
351 set_to_bag set = listToBag (uniqSetToList set)
356 typeCycleErr, classCycleErr :: [[RenamedTyClDecl]] -> Message
358 typeCycleErr syn_cycles
359 = vcat (map (pp_cycle "Cycle in type declarations:") syn_cycles)
361 classCycleErr cls_cycles
362 = vcat (map (pp_cycle "Cycle in class declarations:") cls_cycles)
366 4 (vcat (map pp_decl decls))
369 = hsep [quotes (ppr name), ptext SLIT("at"), ppr (getSrcLoc name)]
371 name = tyClDeclName decl