5f4b3f64c8b4121b70a7edcc1beeab9aaa99155f
[ghc-hetmet.git] / ghc / compiler / types / Variance.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1999
3 %
4 \section[Variance]{Variance in @Type@ and @TyCon@}
5
6 \begin{code}
7 module Variance(
8         calcTyConArgVrcs,
9         tyVarVrc
10     ) where
11
12 #include "HsVersions.h"
13
14 import TypeRep          ( Type(..), TyNote(..) )  -- friend
15 import TyCon            ( TyCon, ArgVrcs, tyConArity, tyConDataCons_maybe, tyConDataCons, tyConTyVars,
16                           tyConArgVrcs_maybe, getSynTyConDefn, isSynTyCon, isAlgTyCon )
17 import DataCon          ( dataConRepArgTys )
18
19 import FiniteMap
20 import Var              ( TyVar )
21 import VarSet
22 import Maybes           ( expectJust )
23 import Maybe            ( isNothing )
24 import Outputable
25 \end{code}
26
27
28 Computing the tyConArgVrcs info
29 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30
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.
35
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.
41
42 \begin{code}
43 calcTyConArgVrcs :: [TyCon] -> FiniteMap TyCon ArgVrcs
44
45 calcTyConArgVrcs tycons
46   = tcaoFix initial_oi
47   where
48
49     initial_oi :: FiniteMap TyCon ArgVrcs
50     initial_oi   = foldl (\fm tc -> addToFM fm tc (initial tc)) emptyFM tycons
51     initial tc   = if isAlgTyCon tc && isNothing (tyConDataCons_maybe tc) then
52                          -- make pessimistic assumption (and warn)
53                          abstractVrcs tc
54                        else
55                          replicate (tyConArity tc) (False,False)
56
57     tcaoFix :: FiniteMap TyCon ArgVrcs   -- initial ArgVrcs per tycon
58             -> FiniteMap TyCon ArgVrcs   -- fixpointed ArgVrcs per tycon
59
60     tcaoFix oi = let (changed,oi') = foldFM (\ tc pms
61                                                (changed,oi')
62                                                -> let pms' = tcaoIter oi' tc  -- seq not simult
63                                                   in  (changed || (pms /= pms'),
64                                                        addToFM oi' tc pms'))
65                                             (False,oi)  -- seq not simult for faster fixpting
66                                             oi
67                  in  if changed
68                      then tcaoFix oi'
69                      else oi'
70
71     tcaoIter :: FiniteMap TyCon ArgVrcs  -- reference ArgVrcs (initial)
72              -> TyCon                    -- tycon to update
73              -> ArgVrcs                  -- new ArgVrcs for tycon
74
75     tcaoIter oi tc | isAlgTyCon tc
76       = if null data_cons then
77                 -- Abstract types get uninformative variances
78             abstractVrcs tc
79         else
80             map (\v -> anyVrc (\ty -> vrcInTy myfao v ty) argtys)
81                 vs
82       where
83         data_cons = tyConDataCons tc
84         vs        = tyConTyVars tc
85         argtys    = concatMap dataConRepArgTys data_cons
86         myfao tc  = lookupWithDefaultFM oi (expectJust "tcaoIter(Alg)" $
87                                                    tyConArgVrcs_maybe tc)
88                                            tc
89                          -- we use the already-computed result for tycons not in this SCC
90
91     tcaoIter oi tc | isSynTyCon tc
92       = let (tyvs,ty) = getSynTyConDefn tc
93             myfao tc  = lookupWithDefaultFM oi (expectJust "tcaoIter(Syn)" $
94                                                   tyConArgVrcs_maybe tc)
95                                                tc
96                         -- we use the already-computed result for tycons not in this SCC
97         in  map (\v -> vrcInTy myfao v ty) tyvs
98
99
100 abstractVrcs :: TyCon -> ArgVrcs
101 abstractVrcs tc = 
102 #ifdef DEBUG
103                   pprTrace "Vrc: abstract tycon:" (ppr tc) $
104 #endif
105                   warn_abstract_vrcs `seq` replicate (tyConArity tc) (True,True)
106
107 warn_abstract_vrcs
108 -- we pull the message out as a CAF so the warning only appears *once*
109   = trace ("WARNING: tyConArgVrc info inaccurate due to unavailable constructors.\n"
110         ++ "         Use -fno-prune-tydecls to fix.") $
111                 ()
112 \end{code}
113
114
115 Variance of tyvars in a type
116 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
117
118 A general variance-check function.  We pass a function for determining
119 the @ArgVrc@s of a tycon; when fixpointing this refers to the current
120 value; otherwise this should be looked up from the tycon's own
121 tyConArgVrcs.
122
123 \begin{code}
124 vrcInTy :: (TyCon -> ArgVrcs)  -- function to get argVrcs of a tycon (break out of recursion)
125         -> TyVar               -- tyvar to check Vrcs of
126         -> Type                -- type to check for occ in
127         -> (Bool,Bool)         -- (occurs positively, occurs negatively)
128
129 vrcInTy fao v (NoteTy (SynNote _)   ty) = vrcInTy fao v ty
130                         -- SynTyCon doesn't neccessarily have vrcInfo at this point,
131                         -- so don't try and use it
132
133 vrcInTy fao v (NoteTy (FTVNote ftv) ty) = if elemVarSet v ftv
134                                           then vrcInTy fao v ty
135                                           else (False,False)
136                         -- note that ftv cannot be calculated as occPos||occNeg,
137                         -- since if a tyvar occurs only as unused tyconarg,
138                         -- occPos==occNeg==False, but ftv=True
139
140 vrcInTy fao v (TyVarTy v')              = if v==v'
141                                           then (True,False)
142                                           else (False,False)
143
144 vrcInTy fao v (AppTy ty1 ty2)           = if vrcInTy fao v ty2 /= (False,False)
145                                           then (True,True)
146                                           else vrcInTy fao v ty1
147                         -- ty1 is probably unknown (or it would have been beta-reduced);
148                         -- hence if v occurs in ty2 at all then it could occur with
149                         -- either variance.  Otherwise it occurs as it does in ty1.
150
151 vrcInTy fao v (FunTy ty1 ty2)           = negVrc (vrcInTy fao v ty1)
152                                           `orVrc`
153                                           vrcInTy fao v ty2
154                                          
155 vrcInTy fao v (ForAllTy v' ty)          = if v==v'
156                                           then (False,False)
157                                           else vrcInTy fao v ty
158
159 vrcInTy fao v (TyConApp tc tys)         = let pms1 = map (vrcInTy fao v) tys
160                                               pms2 = fao tc
161                                           in  orVrcs (zipWith timesVrc pms1 pms2)
162 \end{code}
163
164
165 External entry point: assumes tyconargvrcs already computed.
166
167 \begin{code}
168 tyVarVrc :: TyVar               -- tyvar to check Vrc of
169          -> Type                -- type to check for occ in
170          -> (Bool,Bool)         -- (occurs positively, occurs negatively)
171
172 tyVarVrc = vrcInTy (expectJust "tyVarVrcs" . tyConArgVrcs_maybe)
173 \end{code}
174
175
176 Variance algebra
177 ~~~~~~~~~~~~~~~~
178
179 \begin{code}
180 orVrc :: (Bool,Bool) -> (Bool,Bool) -> (Bool,Bool)
181 orVrc (p1,m1) (p2,m2) = (p1||p2,m1||m2)
182
183 orVrcs :: [(Bool,Bool)] -> (Bool,Bool)
184 orVrcs = foldl orVrc (False,False)
185
186 negVrc :: (Bool,Bool) -> (Bool,Bool)
187 negVrc (p1,m1) = (m1,p1)
188
189 anyVrc :: (a -> (Bool,Bool)) -> [a] -> (Bool,Bool)
190 anyVrc p as = foldl (\ pm a -> pm `orVrc` p a)
191                     (False,False) as
192
193 timesVrc :: (Bool,Bool) -> (Bool,Bool) -> (Bool,Bool)
194 timesVrc (p1,m1) (p2,m2) = (p1 && p2 || m1 && m2,
195                             p1 && m2 || m1 && p2)
196 \end{code}