2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 Taken quite directly from the Peyton Jones/Lester paper.
8 exprFreeVars, -- CoreExpr -> VarSet -- Find all locally-defined free Ids or tyvars
9 exprsFreeVars, -- [CoreExpr] -> VarSet
11 exprSomeFreeVars, exprsSomeFreeVars,
13 idRuleVars, idFreeVars, idFreeTyVars,
14 ruleRhsFreeVars, ruleLhsFreeNames, ruleLhsFreeIds,
16 CoreExprWithFVs, -- = AnnExpr Id VarSet
17 CoreBindWithFVs, -- = AnnBind Id VarSet
18 freeVars, -- CoreExpr -> CoreExprWithFVs
19 freeVarsOf -- CoreExprWithFVs -> IdSet
22 #include "HsVersions.h"
25 import Id ( Id, idType, idSpecialisation )
28 import Var ( Var, isId, isLocalVar, varName )
29 import Type ( tyVarsOfType )
30 import TcType ( tyClsNamesOfType )
31 import Util ( mapAndUnzip )
36 %************************************************************************
38 \section{Finding the free variables of an expression}
40 %************************************************************************
42 This function simply finds the free variables of an expression.
43 So far as type variables are concerned, it only finds tyvars that are
45 * free in type arguments,
46 * free in the type of a binder,
48 but not those that are free in the type of variable occurrence.
51 exprFreeVars :: CoreExpr -> VarSet -- Find all locally-defined free Ids or tyvars
52 exprFreeVars = exprSomeFreeVars isLocalVar
54 exprsFreeVars :: [CoreExpr] -> VarSet
55 exprsFreeVars = foldr (unionVarSet . exprFreeVars) emptyVarSet
57 exprSomeFreeVars :: InterestingVarFun -- Says which Vars are interesting
60 exprSomeFreeVars fv_cand e = expr_fvs e fv_cand emptyVarSet
62 exprsSomeFreeVars :: InterestingVarFun -- Says which Vars are interesting
65 exprsSomeFreeVars fv_cand = foldr (unionVarSet . exprSomeFreeVars fv_cand) emptyVarSet
67 type InterestingVarFun = Var -> Bool -- True <=> interesting
72 type FV = InterestingVarFun
74 -> VarSet -- Free vars
76 union :: FV -> FV -> FV
77 union fv1 fv2 fv_cand in_scope = fv1 fv_cand in_scope `unionVarSet` fv2 fv_cand in_scope
80 noVars fv_cand in_scope = emptyVarSet
82 -- At a variable occurrence, add in any free variables of its rule rhss
83 -- Curiously, we gather the Id's free *type* variables from its binding
84 -- site, but its free *rule-rhs* variables from its usage sites. This
85 -- is a little weird. The reason is that the former is more efficient,
86 -- but the latter is more fine grained, and a makes a difference when
87 -- a variable mentions itself one of its own rule RHSs
89 oneVar var fv_cand in_scope
91 foldVarSet add_rule_var var_itself_set (idRuleVars var)
93 var_itself_set | keep_it fv_cand in_scope var = unitVarSet var
94 | otherwise = emptyVarSet
95 add_rule_var var set | keep_it fv_cand in_scope var = extendVarSet set var
98 someVars :: VarSet -> FV
99 someVars vars fv_cand in_scope
100 = filterVarSet (keep_it fv_cand in_scope) vars
102 keep_it fv_cand in_scope var
103 | var `elemVarSet` in_scope = False
108 addBndr :: CoreBndr -> FV -> FV
109 addBndr bndr fv fv_cand in_scope
110 | isId bndr = inside_fvs `unionVarSet` someVars (idFreeTyVars bndr) fv_cand in_scope
111 | otherwise = inside_fvs
113 inside_fvs = fv fv_cand (in_scope `extendVarSet` bndr)
115 addBndrs :: [CoreBndr] -> FV -> FV
116 addBndrs bndrs fv = foldr addBndr fv bndrs
121 expr_fvs :: CoreExpr -> FV
123 expr_fvs (Type ty) = someVars (tyVarsOfType ty)
124 expr_fvs (Var var) = oneVar var
125 expr_fvs (Lit lit) = noVars
126 expr_fvs (Note _ expr) = expr_fvs expr
127 expr_fvs (App fun arg) = expr_fvs fun `union` expr_fvs arg
128 expr_fvs (Lam bndr body) = addBndr bndr (expr_fvs body)
131 expr_fvs (Case scrut bndr ty alts)
132 = expr_fvs scrut `union` someVars (tyVarsOfType ty) `union` addBndr bndr
133 (foldr (union . alt_fvs) noVars alts)
135 alt_fvs (con, bndrs, rhs) = addBndrs bndrs (expr_fvs rhs)
137 expr_fvs (Let (NonRec bndr rhs) body)
138 = expr_fvs rhs `union` addBndr bndr (expr_fvs body)
140 expr_fvs (Let (Rec pairs) body)
141 = addBndrs bndrs (foldr (union . expr_fvs) (expr_fvs body) rhss)
143 (bndrs,rhss) = unzip pairs
147 %************************************************************************
151 %************************************************************************
153 exprFreeNames finds the free *names* of an expression, notably
154 including the names of type constructors (which of course do not show
155 up in exprFreeVars). Similarly ruleLhsFreeNames. The latter is used
156 when deciding whether a rule is an orphan. In particular, suppose that
157 T is defined in this module; we want to avoid declaring that a rule like
158 fromIntegral T = fromIntegral_T
159 is an orphan. Of course it isn't, an declaring it an orphan would
160 make the whole module an orphan module, which is bad.
163 ruleLhsFreeNames :: IdCoreRule -> NameSet
164 ruleLhsFreeNames (IdCoreRule fn _ (BuiltinRule _ _)) = unitNameSet (varName fn)
165 ruleLhsFreeNames (IdCoreRule fn _ (Rule _ _ tpl_vars tpl_args rhs))
166 = addOneToNameSet (exprsFreeNames tpl_args `del_binders` tpl_vars) (varName fn)
168 exprFreeNames :: CoreExpr -> NameSet
169 exprFreeNames (Var v) = unitNameSet (varName v)
170 exprFreeNames (Lit _) = emptyNameSet
171 exprFreeNames (Type ty) = tyClsNamesOfType ty -- Don't need free tyvars
172 exprFreeNames (App e1 e2) = exprFreeNames e1 `unionNameSets` exprFreeNames e2
173 exprFreeNames (Lam v e) = exprFreeNames e `delFromNameSet` varName v
174 exprFreeNames (Note n e) = exprFreeNames e
176 exprFreeNames (Let (NonRec b r) e) = (exprFreeNames e `delFromNameSet` varName b)
177 `unionNameSets` exprFreeNames r
179 exprFreeNames (Let (Rec prs) e) = (exprsFreeNames rs `unionNameSets` exprFreeNames e)
185 exprFreeNames (Case e b ty as) = exprFreeNames e `unionNameSets` tyClsNamesOfType ty
187 (unionManyNameSets (map altFreeNames as) `delFromNameSet` varName b)
190 altFreeNames (_,bs,r) = exprFreeNames r `del_binders` bs
192 exprsFreeNames es = foldr (unionNameSets . exprFreeNames) emptyNameSet es
194 del_binders :: NameSet -> [Var] -> NameSet
195 del_binders names bndrs = foldl (\s b -> delFromNameSet s (varName b)) names bndrs
198 %************************************************************************
200 \section[freevars-everywhere]{Attaching free variables to every sub-expression}
202 %************************************************************************
206 ruleRhsFreeVars :: CoreRule -> VarSet
207 ruleRhsFreeVars (BuiltinRule _ _) = noFVs
208 ruleRhsFreeVars (Rule str _ tpl_vars tpl_args rhs)
209 = rule_fvs isLocalVar emptyVarSet
211 rule_fvs = addBndrs tpl_vars (expr_fvs rhs)
213 ruleLhsFreeIds :: CoreRule -> VarSet
214 -- This finds all locally-defined free Ids on the LHS of the rule
215 ruleLhsFreeIds (BuiltinRule _ _) = noFVs
216 ruleLhsFreeIds (Rule _ _ tpl_vars tpl_args rhs)
217 = foldl delVarSet (exprsFreeVars tpl_args) tpl_vars
221 %************************************************************************
223 \section[freevars-everywhere]{Attaching free variables to every sub-expression}
225 %************************************************************************
227 The free variable pass annotates every node in the expression with its
228 NON-GLOBAL free variables and type variables.
231 type CoreBindWithFVs = AnnBind Id VarSet
232 type CoreExprWithFVs = AnnExpr Id VarSet
233 -- Every node annotated with its free variables,
234 -- both Ids and TyVars
236 freeVarsOf :: CoreExprWithFVs -> IdSet
237 freeVarsOf (free_vars, _) = free_vars
240 aFreeVar = unitVarSet
241 unionFVs = unionVarSet
243 delBindersFV :: [Var] -> VarSet -> VarSet
244 delBindersFV bs fvs = foldr delBinderFV fvs bs
246 delBinderFV :: Var -> VarSet -> VarSet
247 -- This way round, so we can do it multiple times using foldr
249 -- (b `delBinderFV` s) removes the binder b from the free variable set s,
251 -- (a) the free variables of b's type
252 -- (b) the idSpecVars of b
254 -- This is really important for some lambdas:
255 -- In (\x::a -> x) the only mention of "a" is in the binder.
258 -- let x::a = b in ...
259 -- we should really note that "a" is free in this expression.
260 -- It'll be pinned inside the /\a by the binding for b, but
261 -- it seems cleaner to make sure that a is in the free-var set
262 -- when it is mentioned.
264 -- This also shows up in recursive bindings. Consider:
265 -- /\a -> letrec x::a = x in E
266 -- Now, there are no explicit free type variables in the RHS of x,
267 -- but nevertheless "a" is free in its definition. So we add in
268 -- the free tyvars of the types of the binders, and include these in the
269 -- free vars of the group, attached to the top level of each RHS.
271 -- This actually happened in the defn of errorIO in IOBase.lhs:
272 -- errorIO (ST io) = case (errorIO# io) of
275 -- bottom = bottom -- Never evaluated
277 delBinderFV b s | isId b = (s `delVarSet` b) `unionFVs` idFreeVars b
278 | otherwise = s `delVarSet` b
280 idFreeVars :: Id -> VarSet
281 idFreeVars id = ASSERT( isId id) idRuleVars id `unionVarSet` idFreeTyVars id
283 idFreeTyVars :: Id -> TyVarSet
284 -- Only local Ids conjured up locally, can have free type variables.
285 -- (During type checking top-level Ids can have free tyvars)
286 idFreeTyVars id = tyVarsOfType (idType id)
287 -- | isLocalId id = tyVarsOfType (idType id)
288 -- | otherwise = emptyVarSet
290 idRuleVars ::Id -> VarSet
291 idRuleVars id = ASSERT( isId id) rulesRhsFreeVars (idSpecialisation id)
295 %************************************************************************
297 \subsection{Free variables (and types)}
299 %************************************************************************
302 freeVars :: CoreExpr -> CoreExprWithFVs
307 -- ToDo: insert motivating example for why we *need*
308 -- to include the idSpecVars in the FV list.
309 -- Actually [June 98] I don't think it's necessary
310 -- fvs = fvs_v `unionVarSet` idSpecVars v
312 fvs | isLocalVar v = aFreeVar v
315 freeVars (Lit lit) = (noFVs, AnnLit lit)
316 freeVars (Lam b body)
317 = (b `delBinderFV` freeVarsOf body', AnnLam b body')
319 body' = freeVars body
321 freeVars (App fun arg)
322 = (freeVarsOf fun2 `unionFVs` freeVarsOf arg2, AnnApp fun2 arg2)
327 freeVars (Case scrut bndr ty alts)
329 = ((bndr `delBinderFV` alts_fvs) `unionFVs` freeVarsOf scrut2 `unionFVs` tyVarsOfType ty,
330 AnnCase scrut2 bndr ty alts2)
332 scrut2 = freeVars scrut
334 (alts_fvs_s, alts2) = mapAndUnzip fv_alt alts
335 alts_fvs = foldr1 unionFVs alts_fvs_s
337 fv_alt (con,args,rhs) = (delBindersFV args (freeVarsOf rhs2),
342 freeVars (Let (NonRec binder rhs) body)
343 = (freeVarsOf rhs2 `unionFVs` body_fvs,
344 AnnLet (AnnNonRec binder rhs2) body2)
347 body2 = freeVars body
348 body_fvs = binder `delBinderFV` freeVarsOf body2
350 freeVars (Let (Rec binds) body)
351 = (foldl delVarSet group_fvs binders,
352 -- The "delBinderFV" part may have added one of the binders
353 -- via the idSpecVars part, so we must delete it again
354 AnnLet (AnnRec (binders `zip` rhss2)) body2)
356 (binders, rhss) = unzip binds
358 rhss2 = map freeVars rhss
359 all_fvs = foldr (unionFVs . fst) body_fvs rhss2
360 group_fvs = delBindersFV binders all_fvs
362 body2 = freeVars body
363 body_fvs = freeVarsOf body2
365 freeVars (Note (Coerce to_ty from_ty) expr)
366 = (freeVarsOf expr2 `unionFVs` tfvs1 `unionFVs` tfvs2,
367 AnnNote (Coerce to_ty from_ty) expr2)
369 expr2 = freeVars expr
370 tfvs1 = tyVarsOfType from_ty
371 tfvs2 = tyVarsOfType to_ty
373 freeVars (Note other_note expr)
374 = (freeVarsOf expr2, AnnNote other_note expr2)
376 expr2 = freeVars expr
378 freeVars (Type ty) = (tyVarsOfType ty, AnnType ty)