2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 Taken quite directly from the Peyton Jones/Lester paper.
8 exprFreeVars, exprsFreeVars,
9 exprSomeFreeVars, exprsSomeFreeVars,
10 idRuleVars, idFreeVars,
11 ruleSomeFreeVars, ruleSomeLhsFreeVars, ruleRhsFreeVars,
15 CoreExprWithFVs, CoreBindWithFVs, freeVars, freeVarsOf,
18 #include "HsVersions.h"
21 import Id ( Id, idFreeTyVars, hasNoBinding, idSpecialisation )
23 import Var ( Var, isId )
24 import Type ( tyVarsOfType )
25 import Util ( mapAndUnzip )
30 %************************************************************************
32 \section{Finding the free variables of an expression}
34 %************************************************************************
36 This function simply finds the free variables of an expression.
37 So far as type variables are concerned, it only finds tyvars that are
39 * free in type arguments,
40 * free in the type of a binder,
42 but not those that are free in the type of variable occurrence.
45 exprFreeVars :: CoreExpr -> VarSet -- Find all locally-defined free Ids or tyvars
46 exprFreeVars = exprSomeFreeVars isLocalVar
48 exprsFreeVars :: [CoreExpr] -> VarSet
49 exprsFreeVars = foldr (unionVarSet . exprFreeVars) emptyVarSet
51 exprSomeFreeVars :: InterestingVarFun -- Says which Vars are interesting
54 exprSomeFreeVars fv_cand e = expr_fvs e fv_cand emptyVarSet
56 exprsSomeFreeVars :: InterestingVarFun -- Says which Vars are interesting
59 exprsSomeFreeVars fv_cand = foldr (unionVarSet . exprSomeFreeVars fv_cand) emptyVarSet
61 type InterestingVarFun = Var -> Bool -- True <=> interesting
66 type FV = InterestingVarFun
68 -> VarSet -- Free vars
70 union :: FV -> FV -> FV
71 union fv1 fv2 fv_cand in_scope = fv1 fv_cand in_scope `unionVarSet` fv2 fv_cand in_scope
74 noVars fv_cand in_scope = emptyVarSet
76 -- At a variable occurrence, add in any free variables of its rule rhss
77 -- Curiously, we gather the Id's free *type* variables from its binding
78 -- site, but its free *rule-rhs* variables from its usage sites. This
79 -- is a little weird. The reason is that the former is more efficient,
80 -- but the latter is more fine grained, and a makes a difference when
81 -- a variable mentions itself one of its own rule RHSs
83 oneVar var fv_cand in_scope
85 foldVarSet add_rule_var var_itself_set (idRuleVars var)
87 var_itself_set | keep_it fv_cand in_scope var = unitVarSet var
88 | otherwise = emptyVarSet
89 add_rule_var var set | keep_it fv_cand in_scope var = extendVarSet set var
92 someVars :: VarSet -> FV
93 someVars vars fv_cand in_scope
94 = filterVarSet (keep_it fv_cand in_scope) vars
96 keep_it fv_cand in_scope var
97 | var `elemVarSet` in_scope = False
102 addBndr :: CoreBndr -> FV -> FV
103 addBndr bndr fv fv_cand in_scope
104 | isId bndr = inside_fvs `unionVarSet` someVars (idFreeTyVars bndr) fv_cand in_scope
105 | otherwise = inside_fvs
107 inside_fvs = fv fv_cand (in_scope `extendVarSet` bndr)
109 addBndrs :: [CoreBndr] -> FV -> FV
110 addBndrs bndrs fv = foldr addBndr fv bndrs
115 expr_fvs :: CoreExpr -> FV
117 expr_fvs (Type ty) = someVars (tyVarsOfType ty)
118 expr_fvs (Var var) = oneVar var
119 expr_fvs (Lit lit) = noVars
120 expr_fvs (Note _ expr) = expr_fvs expr
121 expr_fvs (App fun arg) = expr_fvs fun `union` expr_fvs arg
122 expr_fvs (Lam bndr body) = addBndr bndr (expr_fvs body)
124 expr_fvs (Case scrut bndr alts)
125 = expr_fvs scrut `union` addBndr bndr (foldr (union . alt_fvs) noVars alts)
127 alt_fvs (con, bndrs, rhs) = addBndrs bndrs (expr_fvs rhs)
129 expr_fvs (Let (NonRec bndr rhs) body)
130 = expr_fvs rhs `union` addBndr bndr (expr_fvs body)
132 expr_fvs (Let (Rec pairs) body)
133 = addBndrs bndrs (foldr (union . expr_fvs) (expr_fvs body) rhss)
135 (bndrs,rhss) = unzip pairs
141 idRuleVars ::Id -> VarSet
142 idRuleVars id = ASSERT( isId id) rulesRhsFreeVars (idSpecialisation id)
144 idFreeVars :: Id -> VarSet
145 idFreeVars id = ASSERT( isId id) idRuleVars id `unionVarSet` idFreeTyVars id
147 rulesSomeFreeVars :: InterestingVarFun -> CoreRules -> VarSet
148 rulesSomeFreeVars interesting (Rules rules _)
149 = foldr (unionVarSet . ruleSomeFreeVars interesting) emptyVarSet rules
151 ruleRhsFreeVars :: CoreRule -> VarSet
152 ruleRhsFreeVars (BuiltinRule _) = noFVs
153 ruleRhsFreeVars (Rule str tpl_vars tpl_args rhs)
154 = rule_fvs isLocalVar emptyVarSet
156 rule_fvs = addBndrs tpl_vars (expr_fvs rhs)
158 ruleSomeFreeVars :: InterestingVarFun -> CoreRule -> VarSet
159 ruleSomeFreeVars interesting (BuiltinRule _) = noFVs
160 ruleSomeFreeVars interesting (Rule _ tpl_vars tpl_args rhs)
161 = rule_fvs interesting emptyVarSet
163 rule_fvs = addBndrs tpl_vars $
164 foldr (union . expr_fvs) (expr_fvs rhs) tpl_args
166 ruleSomeLhsFreeVars :: InterestingVarFun -> CoreRule -> VarSet
167 ruleSomeLhsFreeVars fn (BuiltinRule _) = noFVs
168 ruleSomeLhsFreeVars fn (Rule _ tpl_vars tpl_args rhs)
169 = foldl delVarSet (exprsSomeFreeVars fn tpl_args) tpl_vars
173 %************************************************************************
175 \section[freevars-everywhere]{Attaching free variables to every sub-expression}
177 %************************************************************************
179 The free variable pass annotates every node in the expression with its
180 NON-GLOBAL free variables and type variables.
183 type CoreBindWithFVs = AnnBind Id VarSet
184 type CoreExprWithFVs = AnnExpr Id VarSet
185 -- Every node annotated with its free variables,
186 -- both Ids and TyVars
188 freeVarsOf :: CoreExprWithFVs -> IdSet
189 freeVarsOf (free_vars, _) = free_vars
192 aFreeVar = unitVarSet
193 unionFVs = unionVarSet
195 filters :: Var -> VarSet -> VarSet
197 -- (b `filters` s) removes the binder b from the free variable set s,
199 -- (a) the free variables of b's type
200 -- (b) the idSpecVars of b
202 -- This is really important for some lambdas:
203 -- In (\x::a -> x) the only mention of "a" is in the binder.
206 -- let x::a = b in ...
207 -- we should really note that "a" is free in this expression.
208 -- It'll be pinned inside the /\a by the binding for b, but
209 -- it seems cleaner to make sure that a is in the free-var set
210 -- when it is mentioned.
212 -- This also shows up in recursive bindings. Consider:
213 -- /\a -> letrec x::a = x in E
214 -- Now, there are no explicit free type variables in the RHS of x,
215 -- but nevertheless "a" is free in its definition. So we add in
216 -- the free tyvars of the types of the binders, and include these in the
217 -- free vars of the group, attached to the top level of each RHS.
219 -- This actually happened in the defn of errorIO in IOBase.lhs:
220 -- errorIO (ST io) = case (errorIO# io) of
223 -- bottom = bottom -- Never evaluated
225 filters b s | isId b = (s `delVarSet` b) `unionFVs` idFreeVars b
226 | otherwise = s `delVarSet` b
230 %************************************************************************
232 \subsection{Free variables (and types)}
234 %************************************************************************
237 freeVars :: CoreExpr -> CoreExprWithFVs
242 -- ToDo: insert motivating example for why we *need*
243 -- to include the idSpecVars in the FV list.
244 -- Actually [June 98] I don't think it's necessary
245 -- fvs = fvs_v `unionVarSet` idSpecVars v
247 fvs | isLocalVar v = aFreeVar v
250 freeVars (Lit lit) = (noFVs, AnnLit lit)
251 freeVars (Lam b body)
252 = (b `filters` freeVarsOf body', AnnLam b body')
254 body' = freeVars body
256 freeVars (App fun arg)
257 = (freeVarsOf fun2 `unionFVs` freeVarsOf arg2, AnnApp fun2 arg2)
262 freeVars (Case scrut bndr alts)
263 = ((bndr `filters` alts_fvs) `unionFVs` freeVarsOf scrut2,
264 AnnCase scrut2 bndr alts2)
266 scrut2 = freeVars scrut
268 (alts_fvs_s, alts2) = mapAndUnzip fv_alt alts
269 alts_fvs = foldr1 unionFVs alts_fvs_s
271 fv_alt (con,args,rhs) = (foldr filters (freeVarsOf rhs2) args,
276 freeVars (Let (NonRec binder rhs) body)
277 = (freeVarsOf rhs2 `unionFVs` body_fvs,
278 AnnLet (AnnNonRec binder rhs2) body2)
281 body2 = freeVars body
282 body_fvs = binder `filters` freeVarsOf body2
284 freeVars (Let (Rec binds) body)
285 = (foldl delVarSet group_fvs binders,
286 -- The "filters" part may have added one of the binders
287 -- via the idSpecVars part, so we must delete it again
288 AnnLet (AnnRec (binders `zip` rhss2)) body2)
290 (binders, rhss) = unzip binds
292 rhss2 = map freeVars rhss
293 all_fvs = foldr (unionFVs . fst) body_fvs rhss2
294 group_fvs = foldr filters all_fvs binders
296 body2 = freeVars body
297 body_fvs = freeVarsOf body2
299 freeVars (Note (Coerce to_ty from_ty) expr)
300 = (freeVarsOf expr2 `unionFVs` tfvs1 `unionFVs` tfvs2,
301 AnnNote (Coerce to_ty from_ty) expr2)
303 expr2 = freeVars expr
304 tfvs1 = tyVarsOfType from_ty
305 tfvs2 = tyVarsOfType to_ty
307 freeVars (Note other_note expr)
308 = (freeVarsOf expr2, AnnNote other_note expr2)
310 expr2 = freeVars expr
312 freeVars (Type ty) = (tyVarsOfType ty, AnnType ty)