2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1999
4 \section[Variance]{Variance in @Type@ and @TyCon@}
12 #include "HsVersions.h"
14 import TypeRep ( Type(..), TyNote(..) ) -- friend
15 import TyCon ( TyCon, ArgVrcs, tyConKind, tyConArity, tyConDataConsIfAvailable, tyConTyVars,
16 tyConArgVrcs_maybe, getSynTyConDefn, isSynTyCon, isAlgTyCon )
17 import DataCon ( dataConRepArgTys )
22 import Name ( Name, getName )
23 import Maybes ( expectJust )
28 Computing the tyConArgVrcs info
29 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31 @tyConArgVrcs@ gives a list of (occPos,occNeg) flags, one for each
32 tyvar. For @AlgTyCon@s and @SynTyCon@s, this info must be precomputed
33 separately. Note that this is information about occurrences of type
34 variables, not usages of term variables.
36 The function @calcTyConArgVrcs@ must be passed a list of *algebraic or
37 syntycons only* such that all tycons referred to (by mutual recursion)
38 appear in the list. The fixpointing will be done on this set of
39 tycons as a whole. It returns a list of @tyconVrcInfo@ data, ready to
40 be (knot-tyingly?) stuck back into the appropriate fields.
43 calcTyConArgVrcs :: [TyCon]
44 -> FiniteMap Name ArgVrcs
46 calcTyConArgVrcs tycons
47 = let oi = foldl (\fm tc -> addToFM fm tc (initial tc)) emptyFM tycons
48 initial tc = if isAlgTyCon tc && null (tyConDataConsIfAvailable tc) then
49 -- make pessimistic assumption (and warn)
50 take (tyConArity tc) abstractVrcs
52 replicate (tyConArity tc) (False,False)
54 go (tc,vrcs) = (getName tc,vrcs)
55 in listToFM (map go (fmToList oi''))
59 tcaoFix :: FiniteMap TyCon ArgVrcs -- initial ArgVrcs per tycon
60 -> FiniteMap TyCon ArgVrcs -- fixpointed ArgVrcs per tycon
62 tcaoFix oi = let (changed,oi') = foldFM (\ tc pms
64 -> let pms' = tcaoIter oi' tc -- seq not simult
65 in (changed || (pms /= pms'),
67 (False,oi) -- seq not simult for faster fixpting
73 tcaoIter :: FiniteMap TyCon ArgVrcs -- reference ArgVrcs (initial)
74 -> TyCon -- tycon to update
75 -> ArgVrcs -- new ArgVrcs for tycon
77 tcaoIter oi tc | isAlgTyCon tc
78 = if null data_cons then
79 -- Abstract types get uninformative variances
82 map (\v -> anyVrc (\ty -> vrcInTy myfao v ty) argtys)
85 data_cons = tyConDataConsIfAvailable tc
87 argtys = concatMap dataConRepArgTys data_cons
88 myfao tc = lookupWithDefaultFM oi (expectJust "tcaoIter(Alg)" $
89 tyConArgVrcs_maybe tc)
91 -- we use the already-computed result for tycons not in this SCC
93 tcaoIter oi tc | isSynTyCon tc
94 = let (tyvs,ty) = getSynTyConDefn tc
95 myfao tc = lookupWithDefaultFM oi (expectJust "tcaoIter(Syn)" $
96 tyConArgVrcs_maybe tc)
98 -- we use the already-computed result for tycons not in this SCC
99 in map (\v -> vrcInTy myfao v ty) tyvs
102 abstractVrcs :: ArgVrcs
103 -- we pull this out as a CAF so the warning only appears *once*
104 abstractVrcs = trace ("WARNING: tyConArgVrc info inaccurate due to unavailable constructors.\n"
105 ++ "\tUse -fno-prune-tydecls to fix.") $
110 Variance of tyvars in a type
111 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
113 A general variance-check function. We pass a function for determining
114 the @ArgVrc@s of a tycon; when fixpointing this refers to the current
115 value; otherwise this should be looked up from the tycon's own
119 vrcInTy :: (TyCon -> ArgVrcs) -- function to get argVrcs of a tycon (break out of recursion)
120 -> TyVar -- tyvar to check Vrcs of
121 -> Type -- type to check for occ in
122 -> (Bool,Bool) -- (occurs positively, occurs negatively)
124 vrcInTy fao v (NoteTy (UsgNote _) ty) = vrcInTy fao v ty
126 vrcInTy fao v (NoteTy (UsgForAll _) ty) = vrcInTy fao v ty
128 vrcInTy fao v (NoteTy (SynNote _) ty) = vrcInTy fao v ty
129 -- SynTyCon doesn't neccessarily have vrcInfo at this point,
130 -- so don't try and use it
132 vrcInTy fao v (NoteTy (FTVNote ftv) ty) = if elemVarSet v ftv
133 then vrcInTy fao v ty
135 -- note that ftv cannot be calculated as occPos||occNeg,
136 -- since if a tyvar occurs only as unused tyconarg,
137 -- occPos==occNeg==False, but ftv=True
139 vrcInTy fao v (TyVarTy v') = if v==v'
143 vrcInTy fao v (AppTy ty1 ty2) = if vrcInTy fao v ty2 /= (False,False)
145 else vrcInTy fao v ty1
146 -- ty1 is probably unknown (or it would have been beta-reduced);
147 -- hence if v occurs in ty2 at all then it could occur with
148 -- either variance. Otherwise it occurs as it does in ty1.
150 vrcInTy fao v (FunTy ty1 ty2) = let (p1,m1) = vrcInTy fao v ty1
151 (p2,m2) = vrcInTy fao v ty2
154 vrcInTy fao v (ForAllTy v' ty) = if v==v'
156 else vrcInTy fao v ty
158 vrcInTy fao v (TyConApp tc tys) = let pms1 = map (vrcInTy fao v) tys
160 in orVrcs (zipWith timesVrc pms1 pms2)
164 External entry point: assumes tyconargvrcs already computed.
167 tyVarVrc :: TyVar -- tyvar to check Vrc of
168 -> Type -- type to check for occ in
169 -> (Bool,Bool) -- (occurs positively, occurs negatively)
171 tyVarVrc = vrcInTy (expectJust "tyVarVrcs" . tyConArgVrcs_maybe)
179 orVrc :: (Bool,Bool) -> (Bool,Bool) -> (Bool,Bool)
180 orVrc (p1,m1) (p2,m2) = (p1||p2,m1||m2)
182 orVrcs :: [(Bool,Bool)] -> (Bool,Bool)
183 orVrcs = foldl orVrc (False,False)
185 anyVrc :: (a -> (Bool,Bool)) -> [a] -> (Bool,Bool)
186 anyVrc p as = foldl (\ pm a -> pm `orVrc` p a)
189 timesVrc :: (Bool,Bool) -> (Bool,Bool) -> (Bool,Bool)
190 timesVrc (p1,m1) (p2,m2) = (p1 && p2 || m1 && m2,
191 p1 && m2 || m1 && p2)