PageRenderTime 40ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/compiler/GHC/Types/Var/Env.hs

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