[project @ 2000-05-23 11:35:36 by simonpj]
[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, tyConKind, tyConArity, 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 Name             ( Name, getName )
23 import Maybes           ( expectJust )
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]
44                  -> FiniteMap Name ArgVrcs
45
46 calcTyConArgVrcs tycons
47   = let oi           = foldl (\fm tc -> addToFM fm tc (initial tc)) emptyFM tycons
48         initial tc   = if isAlgTyCon tc && null (tyConDataCons tc) then
49                          -- make pessimistic assumption (and warn)
50                          take (tyConArity tc) abstractVrcs
51                        else
52                          replicate (tyConArity tc) (False,False)
53         oi''         = tcaoFix oi
54         go (tc,vrcs) = (getName tc,vrcs)
55     in  listToFM (map go (fmToList oi''))
56         
57   where
58
59     tcaoFix :: FiniteMap TyCon ArgVrcs   -- initial ArgVrcs per tycon
60             -> FiniteMap TyCon ArgVrcs   -- fixpointed ArgVrcs per tycon
61
62     tcaoFix oi = let (changed,oi') = foldFM (\ tc pms
63                                                (changed,oi')
64                                                -> let pms' = tcaoIter oi' tc  -- seq not simult
65                                                   in  (changed || (pms /= pms'),
66                                                        addToFM oi' tc pms'))
67                                             (False,oi)  -- seq not simult for faster fixpting
68                                             oi
69                  in  if changed
70                      then tcaoFix oi'
71                      else oi'
72
73     tcaoIter :: FiniteMap TyCon ArgVrcs  -- reference ArgVrcs (initial)
74              -> TyCon                    -- tycon to update
75              -> ArgVrcs                  -- new ArgVrcs for tycon
76
77     tcaoIter oi tc | isAlgTyCon tc
78       = let cs        = tyConDataCons tc
79             vs        = tyConTyVars tc
80             argtys    = concatMap dataConRepArgTys cs
81             myfao tc  = lookupWithDefaultFM oi (expectJust "tcaoIter(Alg)" $
82                                                   tyConArgVrcs_maybe tc)
83                                                tc
84                         -- we use the already-computed result for tycons not in this SCC
85         in  map (\v -> anyVrc (\ty -> vrcInTy myfao v ty) argtys)
86                 vs
87
88     tcaoIter oi tc | isSynTyCon tc
89       = let (tyvs,ty) = getSynTyConDefn tc
90             myfao tc  = lookupWithDefaultFM oi (expectJust "tcaoIter(Syn)" $
91                                                   tyConArgVrcs_maybe tc)
92                                                tc
93                         -- we use the already-computed result for tycons not in this SCC
94         in  map (\v -> vrcInTy myfao v ty) tyvs
95
96
97 abstractVrcs :: ArgVrcs
98 -- we pull this out as a CAF so the warning only appears *once*
99 abstractVrcs = trace ("WARNING: tyConArgVrc info inaccurate due to unavailable constructors.\n"
100                       ++ "\tUse -fno-prune-tydecls to fix.") $
101                  repeat (True,True)
102 \end{code}
103
104
105 Variance of tyvars in a type
106 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
107
108 A general variance-check function.  We pass a function for determining
109 the @ArgVrc@s of a tycon; when fixpointing this refers to the current
110 value; otherwise this should be looked up from the tycon's own
111 tyConArgVrcs.
112
113 \begin{code}
114 vrcInTy :: (TyCon -> ArgVrcs)  -- function to get argVrcs of a tycon (break out of recursion)
115         -> TyVar               -- tyvar to check Vrcs of
116         -> Type                -- type to check for occ in
117         -> (Bool,Bool)         -- (occurs positively, occurs negatively)
118
119 vrcInTy fao v (NoteTy (UsgNote _)   ty) = vrcInTy fao v ty
120
121 vrcInTy fao v (NoteTy (UsgForAll _) ty) = vrcInTy fao v ty
122
123 vrcInTy fao v (NoteTy (SynNote _)   ty) = vrcInTy fao v ty
124                         -- SynTyCon doesn't neccessarily have vrcInfo at this point,
125                         -- so don't try and use it
126
127 vrcInTy fao v (NoteTy (FTVNote ftv) ty) = if elemVarSet v ftv
128                                           then vrcInTy fao v ty
129                                           else (False,False)
130                         -- note that ftv cannot be calculated as occPos||occNeg,
131                         -- since if a tyvar occurs only as unused tyconarg,
132                         -- occPos==occNeg==False, but ftv=True
133
134 vrcInTy fao v (TyVarTy v')              = if v==v'
135                                           then (True,False)
136                                           else (False,False)
137
138 vrcInTy fao v (AppTy ty1 ty2)           = if vrcInTy fao v ty2 /= (False,False)
139                                           then (True,True)
140                                           else vrcInTy fao v ty1
141                         -- ty1 is probably unknown (or it would have been beta-reduced);
142                         -- hence if v occurs in ty2 at all then it could occur with
143                         -- either variance.  Otherwise it occurs as it does in ty1.
144
145 vrcInTy fao v (FunTy ty1 ty2)           = let (p1,m1) = vrcInTy fao v ty1
146                                               (p2,m2) = vrcInTy fao v ty2
147                                           in (m1||p2,p1||m2)
148                                          
149 vrcInTy fao v (ForAllTy v' ty)          = if v==v'
150                                           then (False,False)
151                                           else vrcInTy fao v ty
152
153 vrcInTy fao v (TyConApp tc tys)         = let pms1 = map (vrcInTy fao v) tys
154                                               pms2 = fao tc
155                                           in  orVrcs (zipWith timesVrc pms1 pms2)
156 \end{code}
157
158
159 External entry point: assumes tyconargvrcs already computed.
160
161 \begin{code}
162 tyVarVrc :: TyVar               -- tyvar to check Vrc of
163          -> Type                -- type to check for occ in
164          -> (Bool,Bool)         -- (occurs positively, occurs negatively)
165
166 tyVarVrc = vrcInTy (expectJust "tyVarVrcs" . tyConArgVrcs_maybe)
167 \end{code}
168
169
170 Variance algebra
171 ~~~~~~~~~~~~~~~~
172
173 \begin{code}
174 orVrc :: (Bool,Bool) -> (Bool,Bool) -> (Bool,Bool)
175 orVrc (p1,m1) (p2,m2) = (p1||p2,m1||m2)
176
177 orVrcs :: [(Bool,Bool)] -> (Bool,Bool)
178 orVrcs = foldl orVrc (False,False)
179
180 anyVrc :: (a -> (Bool,Bool)) -> [a] -> (Bool,Bool)
181 anyVrc p as = foldl (\ pm a -> pm `orVrc` p a)
182                     (False,False) as
183
184 timesVrc :: (Bool,Bool) -> (Bool,Bool) -> (Bool,Bool)
185 timesVrc (p1,m1) (p2,m2) = (p1 && p2 || m1 && m2,
186                             p1 && m2 || m1 && p2)
187 \end{code}