IMP_Ubiq(){-uitous-}
-import HsSyn ( TyDecl(..), ConDecl(..), BangType(..),
- ClassDecl(..), MonoType(..), PolyType(..),
- Sig(..), MonoBinds, Fake, InPat, HsBinds(..), Bind, HsExpr )
-import RnHsSyn ( isRnTyCon, RenamedTyDecl(..), RenamedClassDecl(..),
- RnName(..){-instance Uniquable-}
+import HsSyn ( HsDecl(..), TyDecl(..), ConDecl(..), ConDetails(..), BangType(..),
+ ClassDecl(..), HsType(..), HsTyVar, DefaultDecl, InstDecl,
+ IfaceSig, Sig(..), MonoBinds, Fake, InPat, HsBinds(..), HsExpr, NewOrData,
+ hsDeclName
)
-import TcHsSyn ( TcHsBinds(..), TcIdOcc(..) )
+import RnHsSyn ( RenamedTyDecl(..), RenamedClassDecl(..), SYN_IE(RenamedHsDecl)
+ )
+import TcHsSyn ( SYN_IE(TcHsBinds), TcIdOcc(..) )
-import TcMonad hiding ( rnMtoTcM )
-import Inst ( InstanceMapper(..) )
+import TcMonad
+import Inst ( SYN_IE(InstanceMapper) )
import TcClassDcl ( tcClassDecl1 )
-import TcEnv ( tcExtendTyConEnv, tcExtendClassEnv,
- tcTyVarScope, tcGetEnv )
+import TcEnv ( tcExtendTyConEnv, tcExtendClassEnv )
+import SpecEnv ( SpecEnv )
import TcKind ( TcKind, newKindVars )
import TcTyDecls ( tcTyDecl, mkDataBinds )
+import TcMonoType ( tcTyVarScope )
import Bag
-import Class ( SYN_IE(Class), classSelIds )
-import Digraph ( findSCCs, SCC(..) )
-import Name ( getSrcLoc )
-import PprStyle
+import Class ( SYN_IE(Class) )
+import Digraph ( stronglyConnComp, SCC(..) )
+import Name ( Name, getSrcLoc, isTvOcc, nameOccName )
+import Outputable
import Pretty
+import Maybes ( mapMaybe )
import UniqSet ( SYN_IE(UniqSet), emptyUniqSet,
unitUniqSet, unionUniqSets,
unionManyUniqSets, uniqSetToList )
import SrcLoc ( SrcLoc )
-import TyCon ( TyCon )
-import Unique ( Unique )
+import TyCon ( TyCon, SYN_IE(Arity) )
+import Unique ( Unique, Uniquable(..) )
import Util ( panic{-, pprTrace-} )
\end{code}
The main function
~~~~~~~~~~~~~~~~~
\begin{code}
-data Decl = TyD RenamedTyDecl | ClD RenamedClassDecl
-
-tcTyAndClassDecls1 :: InstanceMapper
- -> Bag RenamedTyDecl -> Bag RenamedClassDecl
+tcTyAndClassDecls1 :: TcEnv s -> InstanceMapper -- Knot tying stuff
+ -> [RenamedHsDecl]
-> TcM s (TcEnv s)
-tcTyAndClassDecls1 inst_mapper rnty_decls rncls_decls
- = sortByDependency syn_decls cls_decls decls `thenTc` \ groups ->
- tcGroups inst_mapper groups
- where
- cls_decls = mapBag ClD rncls_decls
- ty_decls = mapBag TyD rnty_decls
- syn_decls = filterBag is_syn_decl ty_decls
- decls = ty_decls `unionBags` cls_decls
-
- is_syn_decl (TyD (TySynonym _ _ _ _)) = True
- is_syn_decl _ = False
+tcTyAndClassDecls1 unf_env inst_mapper decls
+ = sortByDependency decls `thenTc` \ groups ->
+ tcGroups unf_env inst_mapper groups
-tcGroups inst_mapper []
+tcGroups unf_env inst_mapper []
= tcGetEnv `thenNF_Tc` \ env ->
returnTc env
-tcGroups inst_mapper (group:groups)
- = tcGroup inst_mapper group `thenTc` \ new_env ->
+tcGroups unf_env inst_mapper (group:groups)
+ = tcGroup unf_env inst_mapper group `thenTc` \ new_env ->
-- Extend the environment using the new tycons and classes
tcSetEnv new_env $
-- Do the remaining groups
- tcGroups inst_mapper groups
+ tcGroups unf_env inst_mapper groups
\end{code}
Dealing with a group
~~~~~~~~~~~~~~~~~~~~
\begin{code}
-tcGroup :: InstanceMapper -> Bag Decl -> TcM s (TcEnv s)
-tcGroup inst_mapper decls
- = -- pprTrace "tcGroup: " (ppCat (map (fst.fmt_decl) (bagToList decls))) $
-
- -- TIE THE KNOT
+tcGroup :: TcEnv s -> InstanceMapper -> Bag RenamedHsDecl -> TcM s (TcEnv s)
+tcGroup unf_env inst_mapper decls
+ = -- TIE THE KNOT
fixTc ( \ ~(tycons,classes,_) ->
-- EXTEND TYPE AND CLASS ENVIRONMENTS
tcTyVarScope tyvar_names ( \ tyvars ->
-- DEAL WITH THE DEFINITIONS THEMSELVES
- foldBag combine (tcDecl inst_mapper)
+ foldBag combine (tcDecl unf_env inst_mapper)
(returnTc (emptyBag, emptyBag))
decls
) `thenTc` \ (tycon_bag,class_bag) ->
returnTc final_env
where
- (tyvar_rn_names, tycon_names_w_arities, class_names) = get_binders decls
-
- tyvar_names = map de_rn tyvar_rn_names
- de_rn (RnName n) = n
+ (tyvar_names, tycon_names_w_arities, class_names) = get_binders decls
combine do_a do_b
= do_a `thenTc` \ (a1,a2) ->
Dealing with one decl
~~~~~~~~~~~~~~~~~~~~~
\begin{code}
-tcDecl :: InstanceMapper
- -> Decl
+tcDecl :: TcEnv s -> InstanceMapper
+ -> RenamedHsDecl
-> TcM s (Bag TyCon, Bag Class)
-tcDecl inst_mapper (TyD decl)
+tcDecl unf_env inst_mapper (TyD decl)
= tcTyDecl decl `thenTc` \ tycon ->
returnTc (unitBag tycon, emptyBag)
-tcDecl inst_mapper (ClD decl)
- = tcClassDecl1 inst_mapper decl `thenTc` \ clas ->
+tcDecl unf_env inst_mapper (ClD decl)
+ = tcClassDecl1 unf_env inst_mapper decl `thenTc` \ clas ->
returnTc (emptyBag, unitBag clas)
\end{code}
Dependency analysis
~~~~~~~~~~~~~~~~~~~
\begin{code}
-sortByDependency :: Bag Decl -> Bag Decl -> Bag Decl -> TcM s [Bag Decl]
-sortByDependency syn_decls cls_decls decls
+sortByDependency :: [RenamedHsDecl] -> TcM s [Bag RenamedHsDecl]
+sortByDependency decls
= let -- CHECK FOR SYNONYM CYCLES
- syn_sccs = findSCCs mk_edges syn_decls
- syn_cycles = [map fmt_decl (bagToList decls)
- | CyclicSCC decls <- syn_sccs]
+ syn_sccs = stronglyConnComp (filter is_syn_decl edges)
+ syn_cycles = [ decls | CyclicSCC decls <- syn_sccs]
in
checkTc (null syn_cycles) (typeCycleErr syn_cycles) `thenTc_`
let -- CHECK FOR CLASS CYCLES
- cls_sccs = findSCCs mk_edges cls_decls
- cls_cycles = [map fmt_decl (bagToList decls)
- | CyclicSCC decls <- cls_sccs]
+ cls_sccs = stronglyConnComp (filter is_cls_decl edges)
+ cls_cycles = [ decls | CyclicSCC decls <- cls_sccs]
in
checkTc (null cls_cycles) (classCycleErr cls_cycles) `thenTc_`
-- DO THE MAIN DEPENDENCY ANALYSIS
let
- decl_sccs = findSCCs mk_edges decls
+ decl_sccs = stronglyConnComp (filter is_ty_cls_decl edges)
scc_bags = map bag_acyclic decl_sccs
in
returnTc (scc_bags)
-
- where
- bag_acyclic (AcyclicSCC scc) = unitBag scc
- bag_acyclic (CyclicSCC sccs) = sccs
-fmt_decl decl
- = (ppr PprForUser name, getSrcLoc name)
where
- name = get_name decl
- get_name (TyD (TyData _ name _ _ _ _ _)) = name
- get_name (TyD (TyNew _ name _ _ _ _ _)) = name
- get_name (TyD (TySynonym name _ _ _)) = name
- get_name (ClD (ClassDecl _ name _ _ _ _ _)) = name
+ edges = mapMaybe mk_edges decls
+
+bag_acyclic (AcyclicSCC scc) = unitBag scc
+bag_acyclic (CyclicSCC sccs) = listToBag sccs
+
+is_syn_decl (TyD (TySynonym _ _ _ _), _, _) = True
+is_syn_decl _ = False
+
+is_ty_cls_decl (TyD _, _, _) = True
+is_ty_cls_decl (ClD _, _, _) = True
+is_ty_cls_decl other = False
+
+is_cls_decl (ClD _, _, _) = True
+is_cls_decl other = False
\end{code}
Edges in Type/Class decls
~~~~~~~~~~~~~~~~~~~~~~~~~
\begin{code}
-mk_edges (TyD (TyData ctxt name _ condecls derivs _ _))
- = (uniqueOf name, set_to_bag (get_ctxt ctxt `unionUniqSets` get_cons condecls `unionUniqSets` get_deriv derivs))
-mk_edges (TyD (TyNew ctxt name _ condecl derivs _ _))
- = (uniqueOf name, set_to_bag (get_ctxt ctxt `unionUniqSets` get_cons condecl `unionUniqSets` get_deriv derivs))
-mk_edges (TyD (TySynonym name _ rhs _))
- = (uniqueOf name, set_to_bag (get_ty rhs))
-mk_edges (ClD (ClassDecl ctxt name _ sigs _ _ _))
- = (uniqueOf name, set_to_bag (get_ctxt ctxt `unionUniqSets` get_sigs sigs))
-
-get_ctxt ctxt
- = unionManyUniqSets (map (set_name.fst) ctxt)
+mk_edges decl@(TyD (TyData _ ctxt name _ condecls derivs _ _))
+ = Just (decl, uniqueOf name, uniqSetToList (get_ctxt ctxt `unionUniqSets`
+ get_cons condecls `unionUniqSets`
+ get_deriv derivs))
+
+mk_edges decl@(TyD (TySynonym name _ rhs _))
+ = Just (decl, uniqueOf name, uniqSetToList (get_ty rhs))
+
+mk_edges decl@(ClD (ClassDecl ctxt name _ sigs _ _ _))
+ = Just (decl, uniqueOf name, uniqSetToList (get_ctxt ctxt `unionUniqSets`
+ get_sigs sigs))
+
+mk_edges other_decl = Nothing
+
+get_ctxt ctxt = unionManyUniqSets (map (set_name.fst) ctxt)
get_deriv Nothing = emptyUniqSet
get_deriv (Just clss) = unionManyUniqSets (map set_name clss)
-get_cons cons
- = unionManyUniqSets (map get_con cons)
- where
- get_con (ConDecl _ btys _)
- = unionManyUniqSets (map get_bty btys)
- get_con (ConOpDecl bty1 _ bty2 _)
- = unionUniqSets (get_bty bty1) (get_bty bty2)
- get_con (NewConDecl _ ty _)
- = get_ty ty
- get_con (RecConDecl _ nbtys _)
- = unionManyUniqSets (map (get_bty.snd) nbtys)
-
- get_bty (Banged ty) = get_pty ty
- get_bty (Unbanged ty) = get_pty ty
-
-get_ty (MonoTyVar tv)
- = emptyUniqSet
-get_ty (MonoTyApp name tys)
- = (if isRnTyCon name then set_name name else emptyUniqSet)
- `unionUniqSets` get_tys tys
+get_cons cons = unionManyUniqSets (map get_con cons)
+
+get_con (ConDecl _ ctxt details _)
+ = get_ctxt ctxt `unionUniqSets` get_con_details details
+
+get_con_details (VanillaCon btys) = unionManyUniqSets (map get_bty btys)
+get_con_details (InfixCon bty1 bty2) = unionUniqSets (get_bty bty1) (get_bty bty2)
+get_con_details (NewCon ty) = get_ty ty
+get_con_details (RecCon nbtys) = unionManyUniqSets (map (get_bty.snd) nbtys)
+
+get_bty (Banged ty) = get_ty ty
+get_bty (Unbanged ty) = get_ty ty
+
+get_ty (MonoTyVar name)
+ = if isTvOcc (nameOccName name) then emptyUniqSet else set_name name
+get_ty (MonoTyApp ty1 ty2)
+ = unionUniqSets (get_ty ty1) (get_ty ty2)
get_ty (MonoFunTy ty1 ty2)
= unionUniqSets (get_ty ty1) (get_ty ty2)
-get_ty (MonoListTy ty)
- = get_ty ty -- careful when defining [] (,,) etc as
-get_ty (MonoTupleTy tys) -- [ty] (ty,ty,ty) will not give edges!
- = get_tys tys
-get_ty other = panic "TcTyClsDecls:get_ty"
-
-get_pty (HsForAllTy _ ctxt mty)
+get_ty (MonoListTy tc ty)
+ = set_name tc `unionUniqSets` get_ty ty
+get_ty (MonoTupleTy tc tys)
+ = set_name tc `unionUniqSets` get_tys tys
+get_ty (HsForAllTy _ ctxt mty)
= get_ctxt ctxt `unionUniqSets` get_ty mty
-get_pty other = panic "TcTyClsDecls:get_pty"
+get_ty other = panic "TcTyClsDecls:get_ty"
get_tys tys
= unionManyUniqSets (map get_ty tys)
get_sigs sigs
= unionManyUniqSets (map get_sig sigs)
where
- get_sig (ClassOpSig _ ty _ _) = get_pty ty
+ get_sig (ClassOpSig _ _ ty _) = get_ty ty
get_sig other = panic "TcTyClsDecls:get_sig"
set_name name = unitUniqSet (uniqueOf name)
\begin{code}
-get_binders :: Bag Decl
- -> ([RnName], -- TyVars; no dups
- [(RnName, Maybe Arity)],-- Tycons; no dups; arities for synonyms
- [RnName]) -- Classes; no dups
+get_binders :: Bag RenamedHsDecl
+ -> ([HsTyVar Name], -- TyVars; no dups
+ [(Name, Maybe Arity)], -- Tycons; no dups; arities for synonyms
+ [Name]) -- Classes; no dups
get_binders decls = (bagToList tyvars, bagToList tycons, bagToList classes)
where
union3 (a1,a2,a3) (b1,b2,b3)
= (a1 `unionBags` b1, a2 `unionBags` b2, a3 `unionBags` b3)
-get_binders1 (TyD (TyData _ name tyvars _ _ _ _))
- = (listToBag tyvars, unitBag (name,Nothing), emptyBag)
-get_binders1 (TyD (TyNew _ name tyvars _ _ _ _))
+get_binders1 (TyD (TyData _ _ name tyvars _ _ _ _))
= (listToBag tyvars, unitBag (name,Nothing), emptyBag)
get_binders1 (TyD (TySynonym name tyvars _ _))
= (listToBag tyvars, unitBag (name, Just (length tyvars)), emptyBag)
sigs_tvs sigs = unionManyBags (map sig_tvs sigs)
where
- sig_tvs (ClassOpSig _ ty _ _) = pty_tvs ty
- pty_tvs (HsForAllTy tvs _ _) = listToBag tvs -- tvs doesn't include the class tyvar
+ sig_tvs (ClassOpSig _ _ ty _) = pty_tvs ty
+ pty_tvs (HsForAllTy tvs _ _) = listToBag tvs -- tvs doesn't include the class tyvar
+ pty_tvs other = emptyBag
\end{code}
\begin{code}
typeCycleErr syn_cycles sty
- = ppAboves (map (pp_cycle sty "Cycle in type declarations ...") syn_cycles)
+ = vcat (map (pp_cycle sty "Cycle in type declarations ...") syn_cycles)
classCycleErr cls_cycles sty
- = ppAboves (map (pp_cycle sty "Cycle in class declarations ...") cls_cycles)
+ = vcat (map (pp_cycle sty "Cycle in class declarations ...") cls_cycles)
-pp_cycle sty str things
- = ppHang (ppStr str)
- 4 (ppAboves (map pp_thing things))
+pp_cycle sty str decls
+ = hang (text str)
+ 4 (vcat (map pp_decl decls))
where
- pp_thing (pp_name, loc)
- = ppCat [pp_name, ppr sty loc]
+ pp_decl decl
+ = hsep [ppr sty name, ppr sty (getSrcLoc name)]
+ where
+ name = hsDeclName decl
\end{code}