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, tyConArity, tyConDataConsIfAvailable, tyConTyVars,
16 tyConArgVrcs_maybe, getSynTyConDefn, isSynTyCon, isAlgTyCon )
17 import DataCon ( dataConRepArgTys )
22 import Maybes ( expectJust )
27 Computing the tyConArgVrcs info
28 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30 @tyConArgVrcs@ gives a list of (occPos,occNeg) flags, one for each
31 tyvar. For @AlgTyCon@s and @SynTyCon@s, this info must be precomputed
32 separately. Note that this is information about occurrences of type
33 variables, not usages of term variables.
35 The function @calcTyConArgVrcs@ must be passed a list of *algebraic or
36 syntycons only* such that all tycons referred to (by mutual recursion)
37 appear in the list. The fixpointing will be done on this set of
38 tycons as a whole. It returns a list of @tyconVrcInfo@ data, ready to
39 be (knot-tyingly?) stuck back into the appropriate fields.
42 calcTyConArgVrcs :: [TyCon] -> FiniteMap TyCon ArgVrcs
44 calcTyConArgVrcs tycons
48 initial_oi :: FiniteMap TyCon ArgVrcs
49 initial_oi = foldl (\fm tc -> addToFM fm tc (initial tc)) emptyFM tycons
50 initial tc = if isAlgTyCon tc && null (tyConDataConsIfAvailable tc) then
51 -- make pessimistic assumption (and warn)
52 take (tyConArity tc) abstractVrcs
54 replicate (tyConArity tc) (False,False)
56 tcaoFix :: FiniteMap TyCon ArgVrcs -- initial ArgVrcs per tycon
57 -> FiniteMap TyCon ArgVrcs -- fixpointed ArgVrcs per tycon
59 tcaoFix oi = let (changed,oi') = foldFM (\ tc pms
61 -> let pms' = tcaoIter oi' tc -- seq not simult
62 in (changed || (pms /= pms'),
64 (False,oi) -- seq not simult for faster fixpting
70 tcaoIter :: FiniteMap TyCon ArgVrcs -- reference ArgVrcs (initial)
71 -> TyCon -- tycon to update
72 -> ArgVrcs -- new ArgVrcs for tycon
74 tcaoIter oi tc | isAlgTyCon tc
75 = if null data_cons then
76 -- Abstract types get uninformative variances
79 map (\v -> anyVrc (\ty -> vrcInTy myfao v ty) argtys)
82 data_cons = tyConDataConsIfAvailable tc
84 argtys = concatMap dataConRepArgTys data_cons
85 myfao tc = lookupWithDefaultFM oi (expectJust "tcaoIter(Alg)" $
86 tyConArgVrcs_maybe tc)
88 -- we use the already-computed result for tycons not in this SCC
90 tcaoIter oi tc | isSynTyCon tc
91 = let (tyvs,ty) = getSynTyConDefn tc
92 myfao tc = lookupWithDefaultFM oi (expectJust "tcaoIter(Syn)" $
93 tyConArgVrcs_maybe tc)
95 -- we use the already-computed result for tycons not in this SCC
96 in map (\v -> vrcInTy myfao v ty) tyvs
99 abstractVrcs :: ArgVrcs
100 -- we pull this out as a CAF so the warning only appears *once*
101 abstractVrcs = trace ("WARNING: tyConArgVrc info inaccurate due to unavailable constructors.\n"
102 ++ "\tUse -fno-prune-tydecls to fix.") $
107 Variance of tyvars in a type
108 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110 A general variance-check function. We pass a function for determining
111 the @ArgVrc@s of a tycon; when fixpointing this refers to the current
112 value; otherwise this should be looked up from the tycon's own
116 vrcInTy :: (TyCon -> ArgVrcs) -- function to get argVrcs of a tycon (break out of recursion)
117 -> TyVar -- tyvar to check Vrcs of
118 -> Type -- type to check for occ in
119 -> (Bool,Bool) -- (occurs positively, occurs negatively)
121 vrcInTy fao v (NoteTy (UsgNote _) ty) = vrcInTy fao v ty
123 vrcInTy fao v (NoteTy (UsgForAll _) ty) = vrcInTy fao v ty
125 vrcInTy fao v (NoteTy (SynNote _) ty) = vrcInTy fao v ty
126 -- SynTyCon doesn't neccessarily have vrcInfo at this point,
127 -- so don't try and use it
129 vrcInTy fao v (NoteTy (FTVNote ftv) ty) = if elemVarSet v ftv
130 then vrcInTy fao v ty
132 -- note that ftv cannot be calculated as occPos||occNeg,
133 -- since if a tyvar occurs only as unused tyconarg,
134 -- occPos==occNeg==False, but ftv=True
136 vrcInTy fao v (TyVarTy v') = if v==v'
140 vrcInTy fao v (AppTy ty1 ty2) = if vrcInTy fao v ty2 /= (False,False)
142 else vrcInTy fao v ty1
143 -- ty1 is probably unknown (or it would have been beta-reduced);
144 -- hence if v occurs in ty2 at all then it could occur with
145 -- either variance. Otherwise it occurs as it does in ty1.
147 vrcInTy fao v (FunTy ty1 ty2) = let (p1,m1) = vrcInTy fao v ty1
148 (p2,m2) = vrcInTy fao v ty2
151 vrcInTy fao v (ForAllTy v' ty) = if v==v'
153 else vrcInTy fao v ty
155 vrcInTy fao v (TyConApp tc tys) = let pms1 = map (vrcInTy fao v) tys
157 in orVrcs (zipWith timesVrc pms1 pms2)
161 External entry point: assumes tyconargvrcs already computed.
164 tyVarVrc :: TyVar -- tyvar to check Vrc of
165 -> Type -- type to check for occ in
166 -> (Bool,Bool) -- (occurs positively, occurs negatively)
168 tyVarVrc = vrcInTy (expectJust "tyVarVrcs" . tyConArgVrcs_maybe)
176 orVrc :: (Bool,Bool) -> (Bool,Bool) -> (Bool,Bool)
177 orVrc (p1,m1) (p2,m2) = (p1||p2,m1||m2)
179 orVrcs :: [(Bool,Bool)] -> (Bool,Bool)
180 orVrcs = foldl orVrc (False,False)
182 anyVrc :: (a -> (Bool,Bool)) -> [a] -> (Bool,Bool)
183 anyVrc p as = foldl (\ pm a -> pm `orVrc` p a)
186 timesVrc :: (Bool,Bool) -> (Bool,Bool) -> (Bool,Bool)
187 timesVrc (p1,m1) (p2,m2) = (p1 && p2 || m1 && m2,
188 p1 && m2 || m1 && p2)