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, ruleSomeFreeVars, ruleSomeLhsFreeVars,
12 CoreExprWithFVs, CoreBindWithFVs, freeVars, freeVarsOf,
15 #include "HsVersions.h"
18 import Id ( Id, idFreeTyVars, idSpecialisation )
20 import Var ( Var, isId )
21 import Name ( isLocallyDefined )
22 import Type ( tyVarsOfType, Type )
23 import Util ( mapAndUnzip )
26 %************************************************************************
28 \section{Finding the free variables of an expression}
30 %************************************************************************
32 This function simply finds the free variables of an expression.
33 So far as type variables are concerned, it only finds tyvars that are
35 * free in type arguments,
36 * free in the type of a binder,
38 but not those that are free in the type of variable occurrence.
41 exprFreeVars :: CoreExpr -> VarSet -- Find all locally-defined free Ids or tyvars
42 exprFreeVars = exprSomeFreeVars isLocallyDefined
44 exprsFreeVars :: [CoreExpr] -> VarSet
45 exprsFreeVars = foldr (unionVarSet . exprFreeVars) emptyVarSet
47 exprSomeFreeVars :: InterestingVarFun -- Says which Vars are interesting
50 exprSomeFreeVars fv_cand e = expr_fvs e fv_cand emptyVarSet
52 exprsSomeFreeVars :: InterestingVarFun -- Says which Vars are interesting
55 exprsSomeFreeVars fv_cand = foldr (unionVarSet . exprSomeFreeVars fv_cand) emptyVarSet
57 type InterestingVarFun = Var -> Bool -- True <=> interesting
62 type FV = InterestingVarFun
64 -> VarSet -- Free vars
66 union :: FV -> FV -> FV
67 union fv1 fv2 fv_cand in_scope = fv1 fv_cand in_scope `unionVarSet` fv2 fv_cand in_scope
70 noVars fv_cand in_scope = emptyVarSet
72 -- At a variable occurrence, add in any free variables of its rule rhss
73 -- Curiously, we gather the Id's free *type* variables from its binding
74 -- site, but its free *rule-rhs* variables from its usage sites. This
75 -- is a little weird. The reason is that the former is more efficient,
76 -- but the latter is more fine grained, and a makes a difference when
77 -- a variable mentions itself one of its own rule RHSs
79 oneVar var fv_cand in_scope
80 = foldVarSet add_rule_var var_itself_set (idRuleVars var)
82 var_itself_set | keep_it fv_cand in_scope var = unitVarSet var
83 | otherwise = emptyVarSet
84 add_rule_var var set | keep_it fv_cand in_scope var = extendVarSet set var
87 someVars :: VarSet -> FV
88 someVars vars fv_cand in_scope
89 = filterVarSet (keep_it fv_cand in_scope) vars
91 keep_it fv_cand in_scope var
92 | var `elemVarSet` in_scope = False
97 addBndr :: CoreBndr -> FV -> FV
98 addBndr bndr fv fv_cand in_scope
99 | isId bndr = inside_fvs `unionVarSet` someVars (idFreeTyVars bndr) fv_cand in_scope
100 | otherwise = inside_fvs
102 inside_fvs = fv fv_cand (in_scope `extendVarSet` bndr)
104 addBndrs :: [CoreBndr] -> FV -> FV
105 addBndrs bndrs fv = foldr addBndr fv bndrs
110 expr_fvs :: CoreExpr -> FV
112 expr_fvs (Type ty) = someVars (tyVarsOfType ty)
113 expr_fvs (Var var) = oneVar var
114 expr_fvs (Lit lit) = noVars
115 expr_fvs (Note _ expr) = expr_fvs expr
116 expr_fvs (App fun arg) = expr_fvs fun `union` expr_fvs arg
117 expr_fvs (Lam bndr body) = addBndr bndr (expr_fvs body)
119 expr_fvs (Case scrut bndr alts)
120 = expr_fvs scrut `union` addBndr bndr (foldr (union . alt_fvs) noVars alts)
122 alt_fvs (con, bndrs, rhs) = addBndrs bndrs (expr_fvs rhs)
124 expr_fvs (Let (NonRec bndr rhs) body)
125 = expr_fvs rhs `union` addBndr bndr (expr_fvs body)
127 expr_fvs (Let (Rec pairs) body)
128 = addBndrs bndrs (foldr (union . expr_fvs) (expr_fvs body) rhss)
130 (bndrs,rhss) = unzip pairs
136 idRuleVars ::Id -> VarSet
137 idRuleVars id = rulesRhsFreeVars (idSpecialisation id)
139 idFreeVars :: Id -> VarSet
140 idFreeVars id = idRuleVars id `unionVarSet` idFreeTyVars id
142 rulesSomeFreeVars :: InterestingVarFun -> CoreRules -> VarSet
143 rulesSomeFreeVars interesting (Rules rules _)
144 = foldr (unionVarSet . ruleSomeFreeVars interesting) emptyVarSet rules
146 ruleSomeFreeVars :: InterestingVarFun -> CoreRule -> VarSet
147 ruleSomeFreeVars interesting (BuiltinRule _) = noFVs
148 ruleSomeFreeVars interesting (Rule _ tpl_vars tpl_args rhs)
149 = rule_fvs interesting emptyVarSet
151 rule_fvs = addBndrs tpl_vars $
152 foldr (union . expr_fvs) (expr_fvs rhs) tpl_args
154 ruleSomeLhsFreeVars :: InterestingVarFun -> CoreRule -> VarSet
155 ruleSomeLhsFreeVars fn (BuiltinRule _) = noFVs
156 ruleSomeLhsFreeVars fn (Rule _ tpl_vars tpl_args rhs)
157 = foldl delVarSet (exprsSomeFreeVars fn tpl_args) tpl_vars
161 %************************************************************************
163 \section[freevars-everywhere]{Attaching free variables to every sub-expression}
165 %************************************************************************
167 The free variable pass annotates every node in the expression with its
168 NON-GLOBAL free variables and type variables.
171 type CoreBindWithFVs = AnnBind Id VarSet
172 type CoreExprWithFVs = AnnExpr Id VarSet
173 -- Every node annotated with its free variables,
174 -- both Ids and TyVars
176 freeVarsOf :: CoreExprWithFVs -> IdSet
177 freeVarsOf (free_vars, _) = free_vars
180 aFreeVar = unitVarSet
181 unionFVs = unionVarSet
183 filters :: Var -> VarSet -> VarSet
185 -- (b `filters` s) removes the binder b from the free variable set s,
187 -- (a) the free variables of b's type
188 -- (b) the idSpecVars of b
190 -- This is really important for some lambdas:
191 -- In (\x::a -> x) the only mention of "a" is in the binder.
194 -- let x::a = b in ...
195 -- we should really note that "a" is free in this expression.
196 -- It'll be pinned inside the /\a by the binding for b, but
197 -- it seems cleaner to make sure that a is in the free-var set
198 -- when it is mentioned.
200 -- This also shows up in recursive bindings. Consider:
201 -- /\a -> letrec x::a = x in E
202 -- Now, there are no explicit free type variables in the RHS of x,
203 -- but nevertheless "a" is free in its definition. So we add in
204 -- the free tyvars of the types of the binders, and include these in the
205 -- free vars of the group, attached to the top level of each RHS.
207 -- This actually happened in the defn of errorIO in IOBase.lhs:
208 -- errorIO (ST io) = case (errorIO# io) of
211 -- bottom = bottom -- Never evaluated
213 filters b s | isId b = (s `delVarSet` b) `unionFVs` idFreeVars b
214 | otherwise = s `delVarSet` b
218 %************************************************************************
220 \subsection{Free variables (and types)}
222 %************************************************************************
225 freeVars :: CoreExpr -> CoreExprWithFVs
230 -- ToDo: insert motivating example for why we *need*
231 -- to include the idSpecVars in the FV list.
232 -- Actually [June 98] I don't think it's necessary
233 -- fvs = fvs_v `unionVarSet` idSpecVars v
235 fvs | isLocallyDefined v = aFreeVar v
238 freeVars (Lit lit) = (noFVs, AnnLit lit)
239 freeVars (Lam b body)
240 = (b `filters` freeVarsOf body', AnnLam b body')
242 body' = freeVars body
244 freeVars (App fun arg)
245 = (freeVarsOf fun2 `unionFVs` freeVarsOf arg2, AnnApp fun2 arg2)
250 freeVars (Case scrut bndr alts)
251 = ((bndr `filters` alts_fvs) `unionFVs` freeVarsOf scrut2,
252 AnnCase scrut2 bndr alts2)
254 scrut2 = freeVars scrut
256 (alts_fvs_s, alts2) = mapAndUnzip fv_alt alts
257 alts_fvs = foldr1 unionFVs alts_fvs_s
259 fv_alt (con,args,rhs) = (foldr filters (freeVarsOf rhs2) args,
264 freeVars (Let (NonRec binder rhs) body)
265 = (freeVarsOf rhs2 `unionFVs` body_fvs,
266 AnnLet (AnnNonRec binder rhs2) body2)
269 body2 = freeVars body
270 body_fvs = binder `filters` freeVarsOf body2
272 freeVars (Let (Rec binds) body)
273 = (foldl delVarSet group_fvs binders,
274 -- The "filters" part may have added one of the binders
275 -- via the idSpecVars part, so we must delete it again
276 AnnLet (AnnRec (binders `zip` rhss2)) body2)
278 (binders, rhss) = unzip binds
280 rhss2 = map freeVars rhss
281 all_fvs = foldr (unionFVs . fst) body_fvs rhss2
282 group_fvs = foldr filters all_fvs binders
284 body2 = freeVars body
285 body_fvs = freeVarsOf body2
287 freeVars (Note (Coerce to_ty from_ty) expr)
288 = (freeVarsOf expr2 `unionFVs` tfvs1 `unionFVs` tfvs2,
289 AnnNote (Coerce to_ty from_ty) expr2)
291 expr2 = freeVars expr
292 tfvs1 = tyVarsOfType from_ty
293 tfvs2 = tyVarsOfType to_ty
295 freeVars (Note other_note expr)
296 = (freeVarsOf expr2, AnnNote other_note expr2)
298 expr2 = freeVars expr
300 freeVars (Type ty) = (tyVarsOfType ty, AnnType ty)