import Inst ( InstanceMapper )
import TcClassDcl ( kcClassDecl, tcClassDecl1 )
import TcEnv ( ValueEnv, TcTyThing(..),
- tcExtendTypeEnv
+ tcExtendTypeEnv, getAllEnvTyCons
)
import TcTyDecls ( tcTyDecl, kcTyDecl )
import TcMonoType ( kcHsTyVar )
import TcType ( TcKind, newKindVar, newKindVars, kindToTcKind, zonkTcKindToKind )
-import Type ( mkArrowKind, boxedTypeKind )
+import Type ( mkArrowKind, boxedTypeKind, mkDictTy )
+ -- next two imports for usage stuff only
+import TyCon ( ArgVrcs, tyConKind, tyConArity, tyConDataCons, tyConTyVars,
+ tyConArgVrcs_maybe, getSynTyConDefn, isSynTyCon, isAlgTyCon )
+import DataCon ( dataConRawArgTys, dataConSig )
+
import Class ( Class, classBigSig )
-import Var ( tyVarKind )
+import Type ( Type(..), TyNote(..), tyVarsOfTypes )
+import Var ( TyVar, tyVarKind )
+import FiniteMap
import Bag
+import VarSet
import Digraph ( stronglyConnComp, SCC(..) )
import Name ( Name, NamedThing(..), getSrcLoc, isTvOcc, nameOccName )
import Outputable
-import Maybes ( mapMaybe )
+import Maybes ( mapMaybe, expectJust )
import UniqSet ( UniqSet, emptyUniqSet,
unitUniqSet, unionUniqSets,
unionManyUniqSets, uniqSetToList )
Dealing with a group
~~~~~~~~~~~~~~~~~~~~
+The knot-tying parameters: @rec_tyclss@ is an alist mapping @Name@s to
+@TcTyThing@s. @rec_vrcs@ is a finite map from @Name@s to @ArgVrcs@s.
+
\begin{code}
tcGroup :: ValueEnv -> InstanceMapper -> SCC RenamedTyClDecl -> TcM s TcEnv
tcGroup unf_env inst_mapper scc
-- Tie the knot
-- traceTc (ppr (map fst ty_env_stuff1)) `thenTc_`
- fixTc ( \ ~(rec_tyclss, _) ->
+ fixTc ( \ ~(rec_tyclss, rec_vrcs, _) ->
let
rec_env = listToUFM rec_tyclss
in
-- Do type checking
mapNF_Tc (getTyBinding2 rec_env) ty_env_stuff1 `thenNF_Tc` \ ty_env_stuff2 ->
tcExtendTypeEnv ty_env_stuff2 $
- mapTc (tcDecl is_rec_group unf_env inst_mapper) decls `thenTc` \ tyclss ->
+ mapTc (tcDecl is_rec_group unf_env inst_mapper rec_vrcs) decls
+ `thenTc` \ tyclss ->
tcGetEnv `thenTc` \ env ->
- returnTc (tyclss, env)
- ) `thenTc` \ (_, env) ->
+ let
+ tycons = getAllEnvTyCons env
+ vrcs = calcTyConArgVrcs tycons
+ in
+
+ returnTc (tyclss, vrcs, env)
+ ) `thenTc` \ (_, _, env) ->
-- traceTc (text "done" <+> ppr (map fst ty_env_stuff1)) `thenTc_`
returnTc env
where
kcTyDecl decl
tcDecl :: RecFlag -- True => recursive group
- -> ValueEnv -> InstanceMapper
+ -> ValueEnv -> InstanceMapper -> FiniteMap Name ArgVrcs
-> RenamedTyClDecl -> TcM s (Name, TcTyThing)
-tcDecl is_rec_group unf_env inst_mapper decl
+tcDecl is_rec_group unf_env inst_mapper vrcs_env decl
= tcAddDeclCtxt decl $
-- traceTc (text "Starting" <+> ppr name) `thenTc_`
if isClassDecl decl then
- tcClassDecl1 unf_env inst_mapper decl `thenTc` \ clas ->
+ tcClassDecl1 unf_env inst_mapper vrcs_env decl `thenTc` \ clas ->
-- traceTc (text "Finished" <+> ppr name) `thenTc_`
returnTc (getName clas, AClass clas)
else
- tcTyDecl is_rec_group decl `thenTc` \ tycon ->
+ tcTyDecl is_rec_group vrcs_env decl `thenTc` \ tycon ->
-- traceTc (text "Finished" <+> ppr name) `thenTc_`
returnTc (getName tycon, ATyCon tycon)
= set_name listTyCon_name `unionUniqSets` get_ty ty
get_ty (MonoTupleTy tys boxed)
= set_name (tupleTyCon_name boxed (length tys)) `unionUniqSets` get_tys tys
+get_ty (MonoUsgTy _ ty)
+ = get_ty ty
get_ty (HsForAllTy _ ctxt mty)
= get_ctxt ctxt `unionUniqSets` get_ty mty
-get_ty other = panic "TcTyClsDecls:get_ty"
+get_ty (MonoDictTy name _)
+ = set_name name
----------------------------------------------------
get_tys tys
where
name = tyClDeclName decl
\end{code}
+
+
+Computing the tyConArgVrcs info
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+@tyConArgVrcs@ gives a list of (occPos,occNeg) flags, one for each
+tyvar. For @AlgTyCon@s and @SynTyCon@s, this info must be precomputed
+separately. Note that this is information about occurrences of type
+variables, not usages of term variables.
+
+The function @calcTyConArgVrcs@ must be passed a list of *algebraic or
+syntycons only* such that all tycons referred to (by mutual recursion)
+appear in the list. The fixpointing will be done on this set of
+tycons as a whole. It returns a list of @tyconVrcInfo@ data, ready to
+be (knot-tyingly?) stuck back into the appropriate fields.
+
+\begin{code}
+calcTyConArgVrcs :: [TyCon]
+ -> FiniteMap Name ArgVrcs
+
+calcTyConArgVrcs tycons
+ = let oi = foldl (\fm tc -> addToFM fm tc (initial tc)) emptyFM tycons
+ initial tc = if isAlgTyCon tc && null (tyConDataCons tc) then
+ -- make pessimistic assumption (and warn)
+ take (tyConArity tc) abstractVrcs
+ else
+ replicate (tyConArity tc) (False,False)
+ oi'' = tcaoFix oi
+ go (tc,vrcs) = (getName tc,vrcs)
+ in listToFM (map go (fmToList oi''))
+
+ where
+
+ tcaoFix :: FiniteMap TyCon ArgVrcs -- initial ArgVrcs per tycon
+ -> FiniteMap TyCon ArgVrcs -- fixpointed ArgVrcs per tycon
+
+ tcaoFix oi = let (changed,oi') = foldFM (\ tc pms
+ (changed,oi')
+ -> let pms' = tcaoIter oi' tc -- seq not simult
+ in (changed || (pms /= pms'),
+ addToFM oi' tc pms'))
+ (False,oi) -- seq not simult for faster fixpting
+ oi
+ in if changed
+ then tcaoFix oi'
+ else oi'
+
+ tcaoIter :: FiniteMap TyCon ArgVrcs -- reference ArgVrcs (initial)
+ -> TyCon -- tycon to update
+ -> ArgVrcs -- new ArgVrcs for tycon
+
+ tcaoIter oi tc | isAlgTyCon tc
+ = let cs = tyConDataCons tc
+ vs = tyConTyVars tc
+ argtys = concatMap dataConRawArgTys cs
+ exdicttys = concatMap ((\ (_,_,_,exth,_,_) -> map (uncurry mkDictTy) exth)
+ . dataConSig) cs
+ myfao tc = lookupWithDefaultFM oi (expectJust "tcaoIter(Alg)" $
+ tyConArgVrcs_maybe tc)
+ tc
+ -- we use the already-computed result for tycons not in this SCC
+ in map (\v -> anyVrc (\ty -> vrcInTy myfao v ty) (exdicttys ++ argtys))
+ vs
+
+ tcaoIter oi tc | isSynTyCon tc
+ = let (tyvs,ty) = getSynTyConDefn tc
+ myfao tc = lookupWithDefaultFM oi (expectJust "tcaoIter(Syn)" $
+ tyConArgVrcs_maybe tc)
+ tc
+ -- we use the already-computed result for tycons not in this SCC
+ in map (\v -> vrcInTy myfao v ty) tyvs
+
+
+abstractVrcs :: ArgVrcs
+-- we pull this out as a CAF so the warning only appears *once*
+abstractVrcs = trace ("WARNING: tyConArgVrc info inaccurate due to unavailable constructors.\n"
+ ++ "\tUse -fno-prune-tydecls to fix.") $
+ repeat (True,True)
+\end{code}
+
+And a general variance-check function. We pass a function for
+determining the @ArgVrc@s of a tycon; when fixpointing this refers to
+the current value; otherwise this should be looked up from the tycon's
+own tyConArgVrcs.
+
+\begin{code}
+vrcInTy :: (TyCon -> ArgVrcs) -- function to get argVrcs of a tycon (break out of recursion)
+ -> TyVar -- tyvar to check Vrcs of
+ -> Type -- type to check for occ in
+ -> (Bool,Bool) -- (occurs positively, occurs negatively)
+
+vrcInTy fao v (NoteTy (UsgNote _) ty) = vrcInTy fao v ty
+
+vrcInTy fao v (NoteTy (SynNote _) ty) = vrcInTy fao v ty
+ -- SynTyCon doesn't neccessarily have vrcInfo at this point,
+ -- so don't try and use it
+
+vrcInTy fao v (NoteTy (FTVNote ftv) ty) = if elemVarSet v ftv
+ then vrcInTy fao v ty
+ else (False,False)
+ -- note that ftv cannot be calculated as occPos||occNeg,
+ -- since if a tyvar occurs only as unused tyconarg,
+ -- occPos==occNeg==False, but ftv=True
+
+vrcInTy fao v (TyVarTy v') = if v==v'
+ then (True,False)
+ else (False,False)
+
+vrcInTy fao v (AppTy ty1 ty2) = if vrcInTy fao v ty2 /= (False,False)
+ then (True,True)
+ else vrcInTy fao v ty1
+ -- ty1 is probably unknown (or it would have been beta-reduced);
+ -- hence if v occurs in ty2 at all then it could occur with
+ -- either variance. Otherwise it occurs as it does in ty1.
+
+vrcInTy fao v (FunTy ty1 ty2) = let (p1,m1) = vrcInTy fao v ty1
+ (p2,m2) = vrcInTy fao v ty2
+ in (m1||p2,p1||m2)
+
+vrcInTy fao v (ForAllTy v' ty) = if v==v'
+ then (False,False)
+ else vrcInTy fao v ty
+
+vrcInTy fao v (TyConApp tc tys) = let pms1 = map (vrcInTy fao v) tys
+ pms2 = fao tc
+ in orVrcs (zipWith timesVrc pms1 pms2)
+
+orVrc :: (Bool,Bool) -> (Bool,Bool) -> (Bool,Bool)
+orVrc (p1,m1) (p2,m2) = (p1||p2,m1||m2)
+
+orVrcs :: [(Bool,Bool)] -> (Bool,Bool)
+orVrcs = foldl orVrc (False,False)
+
+anyVrc :: (a -> (Bool,Bool)) -> [a] -> (Bool,Bool)
+anyVrc p as = foldl (\pm a -> pm `orVrc` p a) (False,False) as
+
+timesVrc :: (Bool,Bool) -> (Bool,Bool) -> (Bool,Bool)
+timesVrc (p1,m1) (p2,m2) = (p1 && p2 || m1 && m2,
+ p1 && m2 || m1 && p2)
+\end{code}
+
+