never executed always true always false
1 {-
2 (c) The University of Glasgow 2006
3 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 -}
5
6 module GHC.Types.Var.Env (
7 -- * Var, Id and TyVar environments (maps)
8 VarEnv, IdEnv, TyVarEnv, CoVarEnv, TyCoVarEnv,
9
10 -- ** Manipulating these environments
11 emptyVarEnv, unitVarEnv, mkVarEnv, mkVarEnv_Directly,
12 elemVarEnv, disjointVarEnv,
13 extendVarEnv, extendVarEnv_C, extendVarEnv_Acc,
14 extendVarEnvList,
15 plusVarEnv, plusVarEnv_C, plusVarEnv_CD, plusMaybeVarEnv_C,
16 plusVarEnvList, alterVarEnv,
17 delVarEnvList, delVarEnv,
18 minusVarEnv,
19 lookupVarEnv, lookupVarEnv_NF, lookupWithDefaultVarEnv,
20 mapVarEnv, zipVarEnv,
21 modifyVarEnv, modifyVarEnv_Directly,
22 isEmptyVarEnv,
23 elemVarEnvByKey,
24 filterVarEnv, restrictVarEnv,
25 partitionVarEnv,
26
27 -- * Deterministic Var environments (maps)
28 DVarEnv, DIdEnv, DTyVarEnv,
29
30 -- ** Manipulating these environments
31 emptyDVarEnv, mkDVarEnv,
32 dVarEnvElts,
33 extendDVarEnv, extendDVarEnv_C,
34 extendDVarEnvList,
35 lookupDVarEnv, elemDVarEnv,
36 isEmptyDVarEnv, foldDVarEnv, nonDetStrictFoldDVarEnv,
37 mapDVarEnv, filterDVarEnv,
38 modifyDVarEnv,
39 alterDVarEnv,
40 plusDVarEnv, plusDVarEnv_C,
41 unitDVarEnv,
42 delDVarEnv,
43 delDVarEnvList,
44 minusDVarEnv,
45 partitionDVarEnv,
46 anyDVarEnv,
47
48 -- * The InScopeSet type
49 InScopeSet,
50
51 -- ** Operations on InScopeSets
52 emptyInScopeSet, mkInScopeSet, delInScopeSet,
53 extendInScopeSet, extendInScopeSetList, extendInScopeSetSet,
54 getInScopeVars, lookupInScope, lookupInScope_Directly,
55 unionInScope, elemInScopeSet, uniqAway,
56 varSetInScope,
57 unsafeGetFreshLocalUnique,
58
59 -- * The RnEnv2 type
60 RnEnv2,
61
62 -- ** Operations on RnEnv2s
63 mkRnEnv2, rnBndr2, rnBndrs2, rnBndr2_var,
64 rnOccL, rnOccR, inRnEnvL, inRnEnvR, rnOccL_maybe, rnOccR_maybe,
65 rnBndrL, rnBndrR, nukeRnEnvL, nukeRnEnvR, rnSwap,
66 delBndrL, delBndrR, delBndrsL, delBndrsR,
67 extendRnInScopeSetList,
68 rnEtaL, rnEtaR,
69 rnInScope, rnInScopeSet, lookupRnInScope,
70 rnEnvL, rnEnvR,
71
72 -- * TidyEnv and its operation
73 TidyEnv,
74 emptyTidyEnv, mkEmptyTidyEnv, delTidyEnvList
75 ) where
76
77 import GHC.Prelude
78 import qualified Data.IntMap.Strict as IntMap -- TODO: Move this to UniqFM
79
80 import GHC.Types.Name.Occurrence
81 import GHC.Types.Name
82 import GHC.Types.Var as Var
83 import GHC.Types.Var.Set
84 import GHC.Types.Unique.Set
85 import GHC.Types.Unique.FM
86 import GHC.Types.Unique.DFM
87 import GHC.Types.Unique
88 import GHC.Utils.Misc
89 import GHC.Utils.Panic
90 import GHC.Data.Maybe
91 import GHC.Utils.Outputable
92
93 {-
94 ************************************************************************
95 * *
96 In-scope sets
97 * *
98 ************************************************************************
99 -}
100
101 -- | A set of variables that are in scope at some point.
102 --
103 -- Note that this is a /superset/ of the variables that are currently in scope.
104 -- See Note [The InScopeSet invariant].
105 --
106 -- "Secrets of the Glasgow Haskell Compiler inliner" Section 3.2 provides
107 -- the motivation for this abstraction.
108 newtype InScopeSet = InScope VarSet
109 -- Note [Lookups in in-scope set]
110 -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
111 -- We store a VarSet here, but we use this for lookups rather than just
112 -- membership tests. Typically the InScopeSet contains the canonical
113 -- version of the variable (e.g. with an informative unfolding), so this
114 -- lookup is useful (see, for instance, Note [In-scope set as a
115 -- substitution]).
116
117 -- Note [The InScopeSet invariant]
118 -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
119 -- The InScopeSet must include every in-scope variable, but it may also
120 -- include other variables.
121
122 -- Its principal purpose is to provide a set of variables to be avoided
123 -- when creating a fresh identifier (fresh in the sense that it does not
124 -- "shadow" any in-scope binding). To do this we simply have to find one that
125 -- does not appear in the InScopeSet. This is done by the key function
126 -- GHC.Types.Var.Env.uniqAway.
127
128 -- See "Secrets of the Glasgow Haskell Compiler inliner" Section 3.2
129 -- for more detailed motivation. #20419 has further discussion.
130
131
132 instance Outputable InScopeSet where
133 ppr (InScope s) =
134 text "InScope" <+>
135 braces (fsep (map (ppr . Var.varName) (nonDetEltsUniqSet s)))
136 -- It's OK to use nonDetEltsUniqSet here because it's
137 -- only for pretty printing
138 -- In-scope sets get big, and with -dppr-debug
139 -- the output is overwhelming
140
141 emptyInScopeSet :: InScopeSet
142 emptyInScopeSet = InScope emptyVarSet
143
144 getInScopeVars :: InScopeSet -> VarSet
145 getInScopeVars (InScope vs) = vs
146
147 mkInScopeSet :: VarSet -> InScopeSet
148 mkInScopeSet in_scope = InScope in_scope
149
150 extendInScopeSet :: InScopeSet -> Var -> InScopeSet
151 extendInScopeSet (InScope in_scope) v
152 = InScope (extendVarSet in_scope v)
153
154 extendInScopeSetList :: InScopeSet -> [Var] -> InScopeSet
155 extendInScopeSetList (InScope in_scope) vs
156 = InScope $ foldl' extendVarSet in_scope vs
157
158 extendInScopeSetSet :: InScopeSet -> VarSet -> InScopeSet
159 extendInScopeSetSet (InScope in_scope) vs
160 = InScope (in_scope `unionVarSet` vs)
161
162 delInScopeSet :: InScopeSet -> Var -> InScopeSet
163 delInScopeSet (InScope in_scope) v = InScope (in_scope `delVarSet` v)
164
165 elemInScopeSet :: Var -> InScopeSet -> Bool
166 elemInScopeSet v (InScope in_scope) = v `elemVarSet` in_scope
167
168 -- | Look up a variable the 'InScopeSet'. This lets you map from
169 -- the variable's identity (unique) to its full value.
170 lookupInScope :: InScopeSet -> Var -> Maybe Var
171 lookupInScope (InScope in_scope) v = lookupVarSet in_scope v
172
173 lookupInScope_Directly :: InScopeSet -> Unique -> Maybe Var
174 lookupInScope_Directly (InScope in_scope) uniq
175 = lookupVarSet_Directly in_scope uniq
176
177 unionInScope :: InScopeSet -> InScopeSet -> InScopeSet
178 unionInScope (InScope s1) (InScope s2)
179 = InScope (s1 `unionVarSet` s2)
180
181 varSetInScope :: VarSet -> InScopeSet -> Bool
182 varSetInScope vars (InScope s1) = vars `subVarSet` s1
183
184 {-
185 Note [Local uniques]
186 ~~~~~~~~~~~~~~~~~~~~
187 Sometimes one must create conjure up a unique which is unique in a particular
188 context (but not necessarily globally unique). For instance, one might need to
189 create a fresh local identifier which does not shadow any of the locally
190 in-scope variables. For this we purpose we provide 'uniqAway'.
191
192 'uniqAway' is implemented in terms of the 'unsafeGetFreshLocalUnique'
193 operation, which generates an unclaimed 'Unique' from an 'InScopeSet'. To
194 ensure that we do not conflict with uniques allocated by future allocations
195 from 'UniqSupply's, Uniques generated by 'unsafeGetFreshLocalUnique' are
196 allocated into a dedicated region of the unique space (namely the X tag).
197
198 Note that one must be quite carefully when using uniques generated in this way
199 since they are only locally unique. In particular, two successive calls to
200 'uniqAway' on the same 'InScopeSet' will produce the same unique.
201 -}
202
203 -- | @uniqAway in_scope v@ finds a unique that is not used in the
204 -- in-scope set, and gives that to v. See Note [Local uniques] and
205 -- Note [The InScopeSet invariant].
206 uniqAway :: InScopeSet -> Var -> Var
207 -- It starts with v's current unique, of course, in the hope that it won't
208 -- have to change, and thereafter uses the successor to the last derived unique
209 -- found in the in-scope set.
210 uniqAway in_scope var
211 | var `elemInScopeSet` in_scope = uniqAway' in_scope var -- Make a new one
212 | otherwise = var -- Nothing to do
213
214 uniqAway' :: InScopeSet -> Var -> Var
215 -- This one *always* makes up a new variable
216 uniqAway' in_scope var
217 = setVarUnique var (unsafeGetFreshLocalUnique in_scope)
218
219 -- | @unsafeGetFreshUnique in_scope@ finds a unique that is not in-scope in the
220 -- given 'InScopeSet'. This must be used very carefully since one can very easily
221 -- introduce non-unique 'Unique's this way. See Note [Local uniques].
222 unsafeGetFreshLocalUnique :: InScopeSet -> Unique
223 unsafeGetFreshLocalUnique (InScope set)
224 | Just (uniq,_) <- IntMap.lookupLT (getKey maxLocalUnique) (ufmToIntMap $ getUniqSet set)
225 , let uniq' = mkLocalUnique uniq
226 , not $ uniq' `ltUnique` minLocalUnique
227 = incrUnique uniq'
228
229 | otherwise
230 = minLocalUnique
231
232 {-
233 ************************************************************************
234 * *
235 Dual renaming
236 * *
237 ************************************************************************
238 -}
239
240 -- | Rename Environment 2
241 --
242 -- When we are comparing (or matching) types or terms, we are faced with
243 -- \"going under\" corresponding binders. E.g. when comparing:
244 --
245 -- > \x. e1 ~ \y. e2
246 --
247 -- Basically we want to rename [@x@ -> @y@] or [@y@ -> @x@], but there are lots of
248 -- things we must be careful of. In particular, @x@ might be free in @e2@, or
249 -- y in @e1@. So the idea is that we come up with a fresh binder that is free
250 -- in neither, and rename @x@ and @y@ respectively. That means we must maintain:
251 --
252 -- 1. A renaming for the left-hand expression
253 --
254 -- 2. A renaming for the right-hand expressions
255 --
256 -- 3. An in-scope set
257 --
258 -- Furthermore, when matching, we want to be able to have an 'occurs check',
259 -- to prevent:
260 --
261 -- > \x. f ~ \y. y
262 --
263 -- matching with [@f@ -> @y@]. So for each expression we want to know that set of
264 -- locally-bound variables. That is precisely the domain of the mappings 1.
265 -- and 2., but we must ensure that we always extend the mappings as we go in.
266 --
267 -- All of this information is bundled up in the 'RnEnv2'
268 data RnEnv2
269 = RV2 { envL :: VarEnv Var -- Renaming for Left term
270 , envR :: VarEnv Var -- Renaming for Right term
271 , in_scope :: InScopeSet } -- In scope in left or right terms
272
273 -- The renamings envL and envR are *guaranteed* to contain a binding
274 -- for every variable bound as we go into the term, even if it is not
275 -- renamed. That way we can ask what variables are locally bound
276 -- (inRnEnvL, inRnEnvR)
277
278 mkRnEnv2 :: InScopeSet -> RnEnv2
279 mkRnEnv2 vars = RV2 { envL = emptyVarEnv
280 , envR = emptyVarEnv
281 , in_scope = vars }
282
283 extendRnInScopeSetList :: RnEnv2 -> [Var] -> RnEnv2
284 extendRnInScopeSetList env vs
285 | null vs = env
286 | otherwise = env { in_scope = extendInScopeSetList (in_scope env) vs }
287
288 rnInScope :: Var -> RnEnv2 -> Bool
289 rnInScope x env = x `elemInScopeSet` in_scope env
290
291 rnInScopeSet :: RnEnv2 -> InScopeSet
292 rnInScopeSet = in_scope
293
294 -- | Retrieve the left mapping
295 rnEnvL :: RnEnv2 -> VarEnv Var
296 rnEnvL = envL
297
298 -- | Retrieve the right mapping
299 rnEnvR :: RnEnv2 -> VarEnv Var
300 rnEnvR = envR
301
302 rnBndrs2 :: RnEnv2 -> [Var] -> [Var] -> RnEnv2
303 -- ^ Applies 'rnBndr2' to several variables: the two variable lists must be of equal length
304 rnBndrs2 env bsL bsR = foldl2 rnBndr2 env bsL bsR
305
306 rnBndr2 :: RnEnv2 -> Var -> Var -> RnEnv2
307 -- ^ @rnBndr2 env bL bR@ goes under a binder @bL@ in the Left term,
308 -- and binder @bR@ in the Right term.
309 -- It finds a new binder, @new_b@,
310 -- and returns an environment mapping @bL -> new_b@ and @bR -> new_b@
311 rnBndr2 env bL bR = fst $ rnBndr2_var env bL bR
312
313 rnBndr2_var :: RnEnv2 -> Var -> Var -> (RnEnv2, Var)
314 -- ^ Similar to 'rnBndr2' but returns the new variable as well as the
315 -- new environment
316 rnBndr2_var (RV2 { envL = envL, envR = envR, in_scope = in_scope }) bL bR
317 = (RV2 { envL = extendVarEnv envL bL new_b -- See Note
318 , envR = extendVarEnv envR bR new_b -- [Rebinding]
319 , in_scope = extendInScopeSet in_scope new_b }, new_b)
320 where
321 -- Find a new binder not in scope in either term
322 new_b | not (bL `elemInScopeSet` in_scope) = bL
323 | not (bR `elemInScopeSet` in_scope) = bR
324 | otherwise = uniqAway' in_scope bL
325
326 -- Note [Rebinding]
327 -- If the new var is the same as the old one, note that
328 -- the extendVarEnv *deletes* any current renaming
329 -- E.g. (\x. \x. ...) ~ (\y. \z. ...)
330 --
331 -- Inside \x \y { [x->y], [y->y], {y} }
332 -- \x \z { [x->x], [y->y, z->x], {y,x} }
333
334 rnBndrL :: RnEnv2 -> Var -> (RnEnv2, Var)
335 -- ^ Similar to 'rnBndr2' but used when there's a binder on the left
336 -- side only.
337 rnBndrL (RV2 { envL = envL, envR = envR, in_scope = in_scope }) bL
338 = (RV2 { envL = extendVarEnv envL bL new_b
339 , envR = envR
340 , in_scope = extendInScopeSet in_scope new_b }, new_b)
341 where
342 new_b = uniqAway in_scope bL
343
344 rnBndrR :: RnEnv2 -> Var -> (RnEnv2, Var)
345 -- ^ Similar to 'rnBndr2' but used when there's a binder on the right
346 -- side only.
347 rnBndrR (RV2 { envL = envL, envR = envR, in_scope = in_scope }) bR
348 = (RV2 { envR = extendVarEnv envR bR new_b
349 , envL = envL
350 , in_scope = extendInScopeSet in_scope new_b }, new_b)
351 where
352 new_b = uniqAway in_scope bR
353
354 rnEtaL :: RnEnv2 -> Var -> (RnEnv2, Var)
355 -- ^ Similar to 'rnBndrL' but used for eta expansion
356 -- See Note [Eta expansion]
357 rnEtaL (RV2 { envL = envL, envR = envR, in_scope = in_scope }) bL
358 = (RV2 { envL = extendVarEnv envL bL new_b
359 , envR = extendVarEnv envR new_b new_b -- Note [Eta expansion]
360 , in_scope = extendInScopeSet in_scope new_b }, new_b)
361 where
362 new_b = uniqAway in_scope bL
363
364 rnEtaR :: RnEnv2 -> Var -> (RnEnv2, Var)
365 -- ^ Similar to 'rnBndr2' but used for eta expansion
366 -- See Note [Eta expansion]
367 rnEtaR (RV2 { envL = envL, envR = envR, in_scope = in_scope }) bR
368 = (RV2 { envL = extendVarEnv envL new_b new_b -- Note [Eta expansion]
369 , envR = extendVarEnv envR bR new_b
370 , in_scope = extendInScopeSet in_scope new_b }, new_b)
371 where
372 new_b = uniqAway in_scope bR
373
374 delBndrL, delBndrR :: RnEnv2 -> Var -> RnEnv2
375 delBndrL rn@(RV2 { envL = env, in_scope = in_scope }) v
376 = rn { envL = env `delVarEnv` v, in_scope = in_scope `extendInScopeSet` v }
377 delBndrR rn@(RV2 { envR = env, in_scope = in_scope }) v
378 = rn { envR = env `delVarEnv` v, in_scope = in_scope `extendInScopeSet` v }
379
380 delBndrsL, delBndrsR :: RnEnv2 -> [Var] -> RnEnv2
381 delBndrsL rn@(RV2 { envL = env, in_scope = in_scope }) v
382 = rn { envL = env `delVarEnvList` v, in_scope = in_scope `extendInScopeSetList` v }
383 delBndrsR rn@(RV2 { envR = env, in_scope = in_scope }) v
384 = rn { envR = env `delVarEnvList` v, in_scope = in_scope `extendInScopeSetList` v }
385
386 rnOccL, rnOccR :: RnEnv2 -> Var -> Var
387 -- ^ Look up the renaming of an occurrence in the left or right term
388 rnOccL (RV2 { envL = env }) v = lookupVarEnv env v `orElse` v
389 rnOccR (RV2 { envR = env }) v = lookupVarEnv env v `orElse` v
390
391 rnOccL_maybe, rnOccR_maybe :: RnEnv2 -> Var -> Maybe Var
392 -- ^ Look up the renaming of an occurrence in the left or right term
393 rnOccL_maybe (RV2 { envL = env }) v = lookupVarEnv env v
394 rnOccR_maybe (RV2 { envR = env }) v = lookupVarEnv env v
395
396 inRnEnvL, inRnEnvR :: RnEnv2 -> Var -> Bool
397 -- ^ Tells whether a variable is locally bound
398 inRnEnvL (RV2 { envL = env }) v = v `elemVarEnv` env
399 inRnEnvR (RV2 { envR = env }) v = v `elemVarEnv` env
400
401 lookupRnInScope :: RnEnv2 -> Var -> Var
402 lookupRnInScope env v = lookupInScope (in_scope env) v `orElse` v
403
404 nukeRnEnvL, nukeRnEnvR :: RnEnv2 -> RnEnv2
405 -- ^ Wipe the left or right side renaming
406 nukeRnEnvL env = env { envL = emptyVarEnv }
407 nukeRnEnvR env = env { envR = emptyVarEnv }
408
409 rnSwap :: RnEnv2 -> RnEnv2
410 -- ^ swap the meaning of left and right
411 rnSwap (RV2 { envL = envL, envR = envR, in_scope = in_scope })
412 = RV2 { envL = envR, envR = envL, in_scope = in_scope }
413
414 {-
415 Note [Eta expansion]
416 ~~~~~~~~~~~~~~~~~~~~
417 When matching
418 (\x.M) ~ N
419 we rename x to x' with, where x' is not in scope in
420 either term. Then we want to behave as if we'd seen
421 (\x'.M) ~ (\x'.N x')
422 Since x' isn't in scope in N, the form (\x'. N x') doesn't
423 capture any variables in N. But we must nevertheless extend
424 the envR with a binding [x' -> x'], to support the occurs check.
425 For example, if we don't do this, we can get silly matches like
426 forall a. (\y.a) ~ v
427 succeeding with [a -> v y], which is bogus of course.
428
429
430 ************************************************************************
431 * *
432 Tidying
433 * *
434 ************************************************************************
435 -}
436
437 -- | Tidy Environment
438 --
439 -- When tidying up print names, we keep a mapping of in-scope occ-names
440 -- (the 'TidyOccEnv') and a Var-to-Var of the current renamings
441 type TidyEnv = (TidyOccEnv, VarEnv Var)
442
443 emptyTidyEnv :: TidyEnv
444 emptyTidyEnv = (emptyTidyOccEnv, emptyVarEnv)
445
446 mkEmptyTidyEnv :: TidyOccEnv -> TidyEnv
447 mkEmptyTidyEnv occ_env = (occ_env, emptyVarEnv)
448
449 delTidyEnvList :: TidyEnv -> [Var] -> TidyEnv
450 delTidyEnvList (occ_env, var_env) vs = (occ_env', var_env')
451 where
452 occ_env' = occ_env `delTidyOccEnvList` map (occNameFS . getOccName) vs
453 var_env' = var_env `delVarEnvList` vs
454
455 {-
456 ************************************************************************
457 * *
458 \subsection{@VarEnv@s}
459 * *
460 ************************************************************************
461 -}
462
463 -- We would like this to be `UniqFM Var elt`
464 -- but the code uses various key types.
465 -- So for now make it explicitly untyped
466
467 -- | Variable Environment
468 type VarEnv elt = UniqFM Var elt
469
470 -- | Identifier Environment
471 type IdEnv elt = UniqFM Id elt
472
473 -- | Type Variable Environment
474 type TyVarEnv elt = UniqFM Var elt
475
476 -- | Type or Coercion Variable Environment
477 type TyCoVarEnv elt = UniqFM TyCoVar elt
478
479 -- | Coercion Variable Environment
480 type CoVarEnv elt = UniqFM CoVar elt
481
482 emptyVarEnv :: VarEnv a
483 mkVarEnv :: [(Var, a)] -> VarEnv a
484 mkVarEnv_Directly :: [(Unique, a)] -> VarEnv a
485 zipVarEnv :: [Var] -> [a] -> VarEnv a
486 unitVarEnv :: Var -> a -> VarEnv a
487 alterVarEnv :: (Maybe a -> Maybe a) -> VarEnv a -> Var -> VarEnv a
488 extendVarEnv :: VarEnv a -> Var -> a -> VarEnv a
489 extendVarEnv_C :: (a->a->a) -> VarEnv a -> Var -> a -> VarEnv a
490 extendVarEnv_Acc :: (a->b->b) -> (a->b) -> VarEnv b -> Var -> a -> VarEnv b
491 plusVarEnv :: VarEnv a -> VarEnv a -> VarEnv a
492 plusVarEnvList :: [VarEnv a] -> VarEnv a
493 extendVarEnvList :: VarEnv a -> [(Var, a)] -> VarEnv a
494
495 partitionVarEnv :: (a -> Bool) -> VarEnv a -> (VarEnv a, VarEnv a)
496 restrictVarEnv :: VarEnv a -> VarSet -> VarEnv a
497 delVarEnvList :: VarEnv a -> [Var] -> VarEnv a
498 delVarEnv :: VarEnv a -> Var -> VarEnv a
499 minusVarEnv :: VarEnv a -> VarEnv b -> VarEnv a
500 plusVarEnv_C :: (a -> a -> a) -> VarEnv a -> VarEnv a -> VarEnv a
501 plusVarEnv_CD :: (a -> a -> a) -> VarEnv a -> a -> VarEnv a -> a -> VarEnv a
502 plusMaybeVarEnv_C :: (a -> a -> Maybe a) -> VarEnv a -> VarEnv a -> VarEnv a
503 mapVarEnv :: (a -> b) -> VarEnv a -> VarEnv b
504 modifyVarEnv :: (a -> a) -> VarEnv a -> Var -> VarEnv a
505
506 isEmptyVarEnv :: VarEnv a -> Bool
507 lookupVarEnv :: VarEnv a -> Var -> Maybe a
508 filterVarEnv :: (a -> Bool) -> VarEnv a -> VarEnv a
509 lookupVarEnv_NF :: VarEnv a -> Var -> a
510 lookupWithDefaultVarEnv :: VarEnv a -> a -> Var -> a
511 elemVarEnv :: Var -> VarEnv a -> Bool
512 elemVarEnvByKey :: Unique -> VarEnv a -> Bool
513 disjointVarEnv :: VarEnv a -> VarEnv a -> Bool
514
515 elemVarEnv = elemUFM
516 elemVarEnvByKey = elemUFM_Directly
517 disjointVarEnv = disjointUFM
518 alterVarEnv = alterUFM
519 extendVarEnv = addToUFM
520 extendVarEnv_C = addToUFM_C
521 extendVarEnv_Acc = addToUFM_Acc
522 extendVarEnvList = addListToUFM
523 plusVarEnv_C = plusUFM_C
524 plusVarEnv_CD = plusUFM_CD
525 plusMaybeVarEnv_C = plusMaybeUFM_C
526 delVarEnvList = delListFromUFM
527 delVarEnv = delFromUFM
528 minusVarEnv = minusUFM
529 plusVarEnv = plusUFM
530 plusVarEnvList = plusUFMList
531 lookupVarEnv = lookupUFM
532 filterVarEnv = filterUFM
533 lookupWithDefaultVarEnv = lookupWithDefaultUFM
534 mapVarEnv = mapUFM
535 mkVarEnv = listToUFM
536 mkVarEnv_Directly= listToUFM_Directly
537 emptyVarEnv = emptyUFM
538 unitVarEnv = unitUFM
539 isEmptyVarEnv = isNullUFM
540 partitionVarEnv = partitionUFM
541
542 restrictVarEnv env vs = filterUFM_Directly keep env
543 where
544 keep u _ = u `elemVarSetByKey` vs
545
546 zipVarEnv tyvars tys = mkVarEnv (zipEqual "zipVarEnv" tyvars tys)
547 lookupVarEnv_NF env id = case lookupVarEnv env id of
548 Just xx -> xx
549 Nothing -> panic "lookupVarEnv_NF: Nothing"
550
551 {-
552 @modifyVarEnv@: Look up a thing in the VarEnv,
553 then mash it with the modify function, and put it back.
554 -}
555
556 modifyVarEnv mangle_fn env key
557 = case (lookupVarEnv env key) of
558 Nothing -> env
559 Just xx -> extendVarEnv env key (mangle_fn xx)
560
561 modifyVarEnv_Directly :: (a -> a) -> UniqFM key a -> Unique -> UniqFM key a
562 modifyVarEnv_Directly mangle_fn env key
563 = case (lookupUFM_Directly env key) of
564 Nothing -> env
565 Just xx -> addToUFM_Directly env key (mangle_fn xx)
566
567 -- Deterministic VarEnv
568 -- See Note [Deterministic UniqFM] in GHC.Types.Unique.DFM for explanation why we need
569 -- DVarEnv.
570
571 -- | Deterministic Variable Environment
572 type DVarEnv elt = UniqDFM Var elt
573
574 -- | Deterministic Identifier Environment
575 -- Sadly not always indexed by Id, but it is in the common case.
576 type DIdEnv elt = UniqDFM Var elt
577
578 -- | Deterministic Type Variable Environment
579 type DTyVarEnv elt = UniqDFM TyVar elt
580
581 emptyDVarEnv :: DVarEnv a
582 emptyDVarEnv = emptyUDFM
583
584 dVarEnvElts :: DVarEnv a -> [a]
585 dVarEnvElts = eltsUDFM
586
587 mkDVarEnv :: [(Var, a)] -> DVarEnv a
588 mkDVarEnv = listToUDFM
589
590 extendDVarEnv :: DVarEnv a -> Var -> a -> DVarEnv a
591 extendDVarEnv = addToUDFM
592
593 minusDVarEnv :: DVarEnv a -> DVarEnv a' -> DVarEnv a
594 minusDVarEnv = minusUDFM
595
596 lookupDVarEnv :: DVarEnv a -> Var -> Maybe a
597 lookupDVarEnv = lookupUDFM
598
599 foldDVarEnv :: (a -> b -> b) -> b -> DVarEnv a -> b
600 foldDVarEnv = foldUDFM
601
602 -- See Note [Deterministic UniqFM] to learn about nondeterminism.
603 -- If you use this please provide a justification why it doesn't introduce
604 -- nondeterminism.
605 nonDetStrictFoldDVarEnv :: (a -> b -> b) -> b -> DVarEnv a -> b
606 nonDetStrictFoldDVarEnv = nonDetStrictFoldUDFM
607
608 mapDVarEnv :: (a -> b) -> DVarEnv a -> DVarEnv b
609 mapDVarEnv = mapUDFM
610
611 filterDVarEnv :: (a -> Bool) -> DVarEnv a -> DVarEnv a
612 filterDVarEnv = filterUDFM
613
614 alterDVarEnv :: (Maybe a -> Maybe a) -> DVarEnv a -> Var -> DVarEnv a
615 alterDVarEnv = alterUDFM
616
617 plusDVarEnv :: DVarEnv a -> DVarEnv a -> DVarEnv a
618 plusDVarEnv = plusUDFM
619
620 plusDVarEnv_C :: (a -> a -> a) -> DVarEnv a -> DVarEnv a -> DVarEnv a
621 plusDVarEnv_C = plusUDFM_C
622
623 unitDVarEnv :: Var -> a -> DVarEnv a
624 unitDVarEnv = unitUDFM
625
626 delDVarEnv :: DVarEnv a -> Var -> DVarEnv a
627 delDVarEnv = delFromUDFM
628
629 delDVarEnvList :: DVarEnv a -> [Var] -> DVarEnv a
630 delDVarEnvList = delListFromUDFM
631
632 isEmptyDVarEnv :: DVarEnv a -> Bool
633 isEmptyDVarEnv = isNullUDFM
634
635 elemDVarEnv :: Var -> DVarEnv a -> Bool
636 elemDVarEnv = elemUDFM
637
638 extendDVarEnv_C :: (a -> a -> a) -> DVarEnv a -> Var -> a -> DVarEnv a
639 extendDVarEnv_C = addToUDFM_C
640
641 modifyDVarEnv :: (a -> a) -> DVarEnv a -> Var -> DVarEnv a
642 modifyDVarEnv mangle_fn env key
643 = case (lookupDVarEnv env key) of
644 Nothing -> env
645 Just xx -> extendDVarEnv env key (mangle_fn xx)
646
647 partitionDVarEnv :: (a -> Bool) -> DVarEnv a -> (DVarEnv a, DVarEnv a)
648 partitionDVarEnv = partitionUDFM
649
650 extendDVarEnvList :: DVarEnv a -> [(Var, a)] -> DVarEnv a
651 extendDVarEnvList = addListToUDFM
652
653 anyDVarEnv :: (a -> Bool) -> DVarEnv a -> Bool
654 anyDVarEnv = anyUDFM