CoreSubst.hs 30.9 KB
Newer Older
Austin Seipp's avatar
Austin Seipp committed
1 2 3 4
{-
(c) The University of Glasgow 2006
(c) The GRASP/AQUA Project, Glasgow University, 1992-1998

Simon Marlow's avatar
Simon Marlow committed
5 6

Utility functions on @Core@ syntax
Austin Seipp's avatar
Austin Seipp committed
7
-}
8

9
{-# LANGUAGE CPP #-}
10
module CoreSubst (
11 12 13
        -- * Main data types
        Subst(..), -- Implementation exported for supercompiler's Renaming.hs only
        TvSubstEnv, IdSubstEnv, InScopeSet,
14

batterseapower's avatar
batterseapower committed
15
        -- ** Substituting into expressions and related types
16 17
        deShadowBinds, substSpec, substRulesForImportedIds,
        substTy, substCo, substExpr, substExprSC, substBind, substBindSC,
18
        substUnfolding, substUnfoldingSC,
19
        lookupIdSubst, lookupTCvSubst, substIdOcc,
20
        substTickish, substDVarSet, substIdInfo,
21

batterseapower's avatar
batterseapower committed
22
        -- ** Operations on substitutions
23
        emptySubst, mkEmptySubst, mkSubst, mkOpenSubst, substInScope, isEmptySubst,
24
        extendIdSubst, extendIdSubstList, extendTCvSubst, extendTvSubstList,
25
        extendSubst, extendSubstList, extendSubstWithVar, zapSubstEnv,
26
        addInScopeSet, extendInScope, extendInScopeList, extendInScopeIds,
27
        isInScope, setInScope, getTCvSubst, extendTvSubst, extendCvSubst,
28
        delBndr, delBndrs,
29

30
        -- ** Substituting and cloning binders
31
        substBndr, substBndrs, substRecBndrs, substTyVarBndr, substCoVarBndr,
32
        cloneBndr, cloneBndrs, cloneIdBndr, cloneIdBndrs, cloneRecIdBndrs,
33

34 35 36 37
    ) where

#include "HsVersions.h"

lukemaurer's avatar
lukemaurer committed
38

Simon Marlow's avatar
Simon Marlow committed
39 40
import CoreSyn
import CoreFVs
41
import CoreSeq
Simon Marlow's avatar
Simon Marlow committed
42 43
import CoreUtils
import qualified Type
44 45
import qualified Coercion

46
        -- We are defining local versions
47
import Type     hiding ( substTy, extendTvSubst, extendCvSubst, extendTvSubstList
48
                       , isInScope, substTyVarBndr, cloneTyVarBndr )
49
import Coercion hiding ( substCo, substCoVarBndr )
50

eir@cis.upenn.edu's avatar
eir@cis.upenn.edu committed
51
import PrelNames
52 53
import VarSet
import VarEnv
Simon Marlow's avatar
Simon Marlow committed
54
import Id
55
import Name     ( Name )
56
import Var
Simon Marlow's avatar
Simon Marlow committed
57 58 59
import IdInfo
import UniqSupply
import Maybes
60
import Util
61
import Outputable
62
import PprCore          ()              -- Instances
63
import Data.List
64

65

66

Austin Seipp's avatar
Austin Seipp committed
67 68 69
{-
************************************************************************
*                                                                      *
70
\subsection{Substitutions}
Austin Seipp's avatar
Austin Seipp committed
71 72 73
*                                                                      *
************************************************************************
-}
74

75 76
-- | A substitution environment, containing 'Id', 'TyVar', and 'CoVar'
-- substitutions.
batterseapower's avatar
batterseapower committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
--
-- Some invariants apply to how you use the substitution:
--
-- 1. #in_scope_invariant# The in-scope set contains at least those 'Id's and 'TyVar's that will be in scope /after/
-- applying the substitution to a term. Precisely, the in-scope set must be a superset of the free vars of the
-- substitution range that might possibly clash with locally-bound variables in the thing being substituted in.
--
-- 2. #apply_once# You may apply the substitution only /once/
--
-- There are various ways of setting up the in-scope set such that the first of these invariants hold:
--
-- * Arrange that the in-scope set really is all the things in scope
--
-- * Arrange that it's the free vars of the range of the substitution
--
-- * Make it empty, if you know that all the free vars of the substitution are fresh, and hence can't possibly clash
93
data Subst
Thomas Schilling's avatar
Thomas Schilling committed
94 95
  = Subst InScopeSet  -- Variables in in scope (both Ids and TyVars) /after/
                      -- applying the substitution
96
          IdSubstEnv  -- Substitution from NcIds to CoreExprs
97
          TvSubstEnv  -- Substitution from TyVars to Types
98
          CvSubstEnv  -- Substitution from CoVars to Coercions
99

100 101 102 103 104 105 106 107
        -- INVARIANT 1: See #in_scope_invariant#
        -- This is what lets us deal with name capture properly
        -- It's a hard invariant to check...
        --
        -- INVARIANT 2: The substitution is apply-once; see Note [Apply once] with
        --              Types.TvSubstEnv
        --
        -- INVARIANT 3: See Note [Extending the Subst]
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
108

Austin Seipp's avatar
Austin Seipp committed
109
{-
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
110 111 112
Note [Extending the Subst]
~~~~~~~~~~~~~~~~~~~~~~~~~~
For a core Subst, which binds Ids as well, we make a different choice for Ids
113
than we do for TyVars.
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
114

115
For TyVars, see Note [Extending the TCvSubst] with Type.TvSubstEnv
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
116 117

For Ids, we have a different invariant
118 119
        The IdSubstEnv is extended *only* when the Unique on an Id changes
        Otherwise, we just extend the InScopeSet
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
120 121 122

In consequence:

123
* If all subst envs are empty, substExpr would be a
124 125 126 127 128
  no-op, so substExprSC ("short cut") does nothing.

  However, substExpr still goes ahead and substitutes.  Reason: we may
  want to replace existing Ids with new ones from the in-scope set, to
  avoid space leaks.
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
129

130
* In substIdBndr, we extend the IdSubstEnv only when the unique changes
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
131

132 133 134 135 136
* If the CvSubstEnv, TvSubstEnv and IdSubstEnv are all empty,
  substExpr does nothing (Note that the above rule for substIdBndr
  maintains this property.  If the incoming envts are both empty, then
  substituting the type and IdInfo can't change anything.)

simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
137 138
* In lookupIdSubst, we *must* look up the Id in the in-scope set, because
  it may contain non-trivial changes.  Example:
139
        (/\a. \x:a. ...x...) Int
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
140 141 142 143
  We extend the TvSubstEnv with [a |-> Int]; but x's unique does not change
  so we only extend the in-scope set.  Then we must look up in the in-scope
  set when we find the occurrence of x.

144
* The requirement to look up the Id in the in-scope set means that we
145
  must NOT take no-op short cut when the IdSubst is empty.
146 147 148 149 150
  We must still look up every Id in the in-scope set.

* (However, we don't need to do so for expressions found in the IdSubst
  itself, whose range is assumed to be correct wrt the in-scope set.)

151 152
Why do we make a different choice for the IdSubstEnv than the
TvSubstEnv and CvSubstEnv?
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
153 154 155 156 157

* For Ids, we change the IdInfo all the time (e.g. deleting the
  unfolding), and adding it back later, so using the TyVar convention
  would entail extending the substitution almost all the time

158
* The simplifier wants to look up in the in-scope set anyway, in case it
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
159 160
  can see a better unfolding from an enclosing case expression

161
* For TyVars, only coercion variables can possibly change, and they are
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
162
  easy to spot
Austin Seipp's avatar
Austin Seipp committed
163
-}
164

batterseapower's avatar
batterseapower committed
165
-- | An environment for substituting for 'Id's
166
type IdSubstEnv = IdEnv CoreExpr   -- Domain is NcIds, i.e. not coercions
167 168 169

----------------------------
isEmptySubst :: Subst -> Bool
170
isEmptySubst (Subst _ id_env tv_env cv_env)
171
  = isEmptyVarEnv id_env && isEmptyVarEnv tv_env && isEmptyVarEnv cv_env
172 173

emptySubst :: Subst
174
emptySubst = Subst emptyInScopeSet emptyVarEnv emptyVarEnv emptyVarEnv
175 176

mkEmptySubst :: InScopeSet -> Subst
177
mkEmptySubst in_scope = Subst in_scope emptyVarEnv emptyVarEnv emptyVarEnv
178

179 180
mkSubst :: InScopeSet -> TvSubstEnv -> CvSubstEnv -> IdSubstEnv -> Subst
mkSubst in_scope tvs cvs ids = Subst in_scope ids tvs cvs
181

batterseapower's avatar
batterseapower committed
182
-- | Find the in-scope set: see "CoreSubst#in_scope_invariant"
183
substInScope :: Subst -> InScopeSet
184
substInScope (Subst in_scope _ _ _) = in_scope
185

batterseapower's avatar
batterseapower committed
186 187
-- | Remove all substitutions for 'Id's and 'Var's that might have been built up
-- while preserving the in-scope set
188
zapSubstEnv :: Subst -> Subst
189
zapSubstEnv (Subst in_scope _ _ _) = Subst in_scope emptyVarEnv emptyVarEnv emptyVarEnv
190

batterseapower's avatar
batterseapower committed
191 192
-- | Add a substitution for an 'Id' to the 'Subst': you must ensure that the in-scope set is
-- such that the "CoreSubst#in_scope_invariant" is true after extending the substitution like this
193
extendIdSubst :: Subst -> Id -> CoreExpr -> Subst
batterseapower's avatar
batterseapower committed
194
-- ToDo: add an ASSERT that fvs(subst-result) is already in the in-scope set
195 196 197
extendIdSubst (Subst in_scope ids tvs cvs) v r
  = ASSERT2( isNonCoVarId v, ppr v $$ ppr r )
    Subst in_scope (extendVarEnv ids v r) tvs cvs
198

batterseapower's avatar
batterseapower committed
199
-- | Adds multiple 'Id' substitutions to the 'Subst': see also 'extendIdSubst'
200
extendIdSubstList :: Subst -> [(Id, CoreExpr)] -> Subst
201 202 203
extendIdSubstList (Subst in_scope ids tvs cvs) prs
  = ASSERT( all (isNonCoVarId . fst) prs )
    Subst in_scope (extendVarEnvList ids prs) tvs cvs
204

205 206 207 208 209 210 211
-- | Add a substitution for a 'TyVar' to the 'Subst'
-- The 'TyVar' *must* be a real TyVar, and not a CoVar
-- You must ensure that the in-scope set is such that
-- the "CoreSubst#in_scope_invariant" is true after extending
-- the substitution like this.
extendTvSubst :: Subst -> TyVar -> Type -> Subst
extendTvSubst (Subst in_scope ids tvs cvs) tv ty
212 213 214
  = ASSERT( isTyVar tv )
    Subst in_scope ids (extendVarEnv tvs tv ty) cvs

215 216 217
-- | Adds multiple 'TyVar' substitutions to the 'Subst': see also 'extendTvSubst'
extendTvSubstList :: Subst -> [(TyVar,Type)] -> Subst
extendTvSubstList subst vrs
218
  = foldl' extend subst vrs
219 220
  where
    extend subst (v, r) = extendTvSubst subst v r
221

222
-- | Add a substitution from a 'CoVar' to a 'Coercion' to the 'Subst': you must ensure that the in-scope set is
223
-- such that the "CoreSubst#in_scope_invariant" is true after extending the substitution like this
224
extendCvSubst :: Subst -> CoVar -> Coercion -> Subst
225 226 227
extendCvSubst (Subst in_scope ids tvs cvs) v r
  = ASSERT( isCoVar v )
    Subst in_scope ids tvs (extendVarEnv cvs v r)
228 229 230

-- | Add a substitution appropriate to the thing being substituted
--   (whether an expression, type, or coercion). See also
231
--   'extendIdSubst', 'extendTvSubst', 'extendCvSubst'
232 233 234
extendSubst :: Subst -> Var -> CoreArg -> Subst
extendSubst subst var arg
  = case arg of
235
      Type ty     -> ASSERT( isTyVar var ) extendTvSubst subst var ty
236 237 238 239 240
      Coercion co -> ASSERT( isCoVar var ) extendCvSubst subst var co
      _           -> ASSERT( isId    var ) extendIdSubst subst var arg

extendSubstWithVar :: Subst -> Var -> Var -> Subst
extendSubstWithVar subst v1 v2
241
  | isTyVar v1 = ASSERT( isTyVar v2 ) extendTvSubst subst v1 (mkTyVarTy v2)
242 243 244 245 246 247
  | isCoVar v1 = ASSERT( isCoVar v2 ) extendCvSubst subst v1 (mkCoVarCo v2)
  | otherwise  = ASSERT( isId    v2 ) extendIdSubst subst v1 (Var v2)

-- | Add a substitution as appropriate to each of the terms being
--   substituted (whether expressions, types, or coercions). See also
--   'extendSubst'.
batterseapower's avatar
batterseapower committed
248
extendSubstList :: Subst -> [(Var,CoreArg)] -> Subst
249
extendSubstList subst []              = subst
batterseapower's avatar
batterseapower committed
250 251 252
extendSubstList subst ((var,rhs):prs) = extendSubstList (extendSubst subst var rhs) prs

-- | Find the substitution for an 'Id' in the 'Subst'
253
lookupIdSubst :: SDoc -> Subst -> Id -> CoreExpr
254
lookupIdSubst doc (Subst in_scope ids _ _) v
255
  | not (isLocalId v) = Var v
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
256 257
  | Just e  <- lookupVarEnv ids       v = e
  | Just v' <- lookupInScope in_scope v = Var v'
258
        -- Vital! See Note [Extending the Subst]
259
  | otherwise = WARN( True, text "CoreSubst.lookupIdSubst" <+> doc <+> ppr v
260 261
                            $$ ppr in_scope)
                Var v
262

batterseapower's avatar
batterseapower committed
263
-- | Find the substitution for a 'TyVar' in the 'Subst'
264 265 266 267 268 269
lookupTCvSubst :: Subst -> TyVar -> Type
lookupTCvSubst (Subst _ _ tvs cvs) v
  | isTyVar v
  = lookupVarEnv tvs v `orElse` Type.mkTyVarTy v
  | otherwise
  = mkCoercionTy $ lookupVarEnv cvs v `orElse` mkCoVarCo v
270

271
delBndr :: Subst -> Var -> Subst
272 273 274 275
delBndr (Subst in_scope ids tvs cvs) v
  | isCoVar v = Subst in_scope ids tvs (delVarEnv cvs v)
  | isTyVar v = Subst in_scope ids (delVarEnv tvs v) cvs
  | otherwise = Subst in_scope (delVarEnv ids v) tvs cvs
276 277

delBndrs :: Subst -> [Var] -> Subst
278 279
delBndrs (Subst in_scope ids tvs cvs) vs
  = Subst in_scope (delVarEnvList ids vs) (delVarEnvList tvs vs) (delVarEnvList cvs vs)
280
      -- Easiest thing is just delete all from all!
281

282 283 284 285
-- | Simultaneously substitute for a bunch of variables
--   No left-right shadowing
--   ie the substitution for   (\x \y. e) a1 a2
--      so neither x nor y scope over a1 a2
286 287
mkOpenSubst :: InScopeSet -> [(Var,CoreArg)] -> Subst
mkOpenSubst in_scope pairs = Subst in_scope
288 289
                                   (mkVarEnv [(id,e)  | (id, e) <- pairs, isId id])
                                   (mkVarEnv [(tv,ty) | (tv, Type ty) <- pairs])
290
                                   (mkVarEnv [(v,co)  | (v, Coercion co) <- pairs])
291

292 293
------------------------------
isInScope :: Var -> Subst -> Bool
294
isInScope v (Subst in_scope _ _ _) = v `elemInScopeSet` in_scope
295

296 297 298
-- | Add the 'Var' to the in-scope set, but do not remove
-- any existing substitutions for it
addInScopeSet :: Subst -> VarSet -> Subst
299 300
addInScopeSet (Subst in_scope ids tvs cvs) vs
  = Subst (in_scope `extendInScopeSetSet` vs) ids tvs cvs
301 302 303

-- | Add the 'Var' to the in-scope set: as a side effect,
-- and remove any existing substitutions for it
304
extendInScope :: Subst -> Var -> Subst
305
extendInScope (Subst in_scope ids tvs cvs) v
306 307
  = Subst (in_scope `extendInScopeSet` v)
          (ids `delVarEnv` v) (tvs `delVarEnv` v) (cvs `delVarEnv` v)
308

batterseapower's avatar
batterseapower committed
309
-- | Add the 'Var's to the in-scope set: see also 'extendInScope'
310
extendInScopeList :: Subst -> [Var] -> Subst
311
extendInScopeList (Subst in_scope ids tvs cvs) vs
312 313
  = Subst (in_scope `extendInScopeSetList` vs)
          (ids `delVarEnvList` vs) (tvs `delVarEnvList` vs) (cvs `delVarEnvList` vs)
314

315
-- | Optimized version of 'extendInScopeList' that can be used if you are certain
316
-- all the things being added are 'Id's and hence none are 'TyVar's or 'CoVar's
317
extendInScopeIds :: Subst -> [Id] -> Subst
318 319 320
extendInScopeIds (Subst in_scope ids tvs cvs) vs
  = Subst (in_scope `extendInScopeSetList` vs)
          (ids `delVarEnvList` vs) tvs cvs
321 322

setInScope :: Subst -> InScopeSet -> Subst
323
setInScope (Subst _ ids tvs cvs) in_scope = Subst in_scope ids tvs cvs
324

Austin Seipp's avatar
Austin Seipp committed
325
-- Pretty printing, for debugging only
326 327

instance Outputable Subst where
328
  ppr (Subst in_scope ids tvs cvs)
329
        =  text "<InScope =" <+> in_scope_doc
330 331 332
        $$ text " IdSubst   =" <+> ppr ids
        $$ text " TvSubst   =" <+> ppr tvs
        $$ text " CvSubst   =" <+> ppr cvs
333
         <> char '>'
334 335
    where
    in_scope_doc = pprVarSet (getInScopeVars in_scope) (braces . fsep . map ppr)
336

Austin Seipp's avatar
Austin Seipp committed
337 338 339
{-
************************************************************************
*                                                                      *
340
        Substituting expressions
Austin Seipp's avatar
Austin Seipp committed
341 342 343
*                                                                      *
************************************************************************
-}
344

345
-- | Apply a substitution to an entire 'CoreExpr'. Remember, you may only
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
346
-- apply the substitution /once/: see "CoreSubst#apply_once"
347 348 349
--
-- Do *not* attempt to short-cut in the case of an empty substitution!
-- See Note [Extending the Subst]
350
substExprSC :: SDoc -> Subst -> CoreExpr -> CoreExpr
351
substExprSC doc subst orig_expr
352 353
  | isEmptySubst subst = orig_expr
  | otherwise          = -- pprTrace "enter subst-expr" (doc $$ ppr orig_expr) $
354
                         subst_expr doc subst orig_expr
355 356

substExpr :: SDoc -> Subst -> CoreExpr -> CoreExpr
357
substExpr doc subst orig_expr = subst_expr doc subst orig_expr
358

359 360
subst_expr :: SDoc -> Subst -> CoreExpr -> CoreExpr
subst_expr doc subst expr
361 362
  = go expr
  where
363
    go (Var v)         = lookupIdSubst (doc $$ text "subst_expr") subst v
364
    go (Type ty)       = Type (substTy subst ty)
365
    go (Coercion co)   = Coercion (substCo subst co)
366 367
    go (Lit lit)       = Lit lit
    go (App fun arg)   = App (go fun) (go arg)
Peter Wortmann's avatar
Peter Wortmann committed
368
    go (Tick tickish e) = mkTick (substTickish subst tickish) (go e)
369
    go (Cast e co)     = Cast (go e) (substCo subst co)
370 371 372 373 374
       -- Do not optimise even identity coercions
       -- Reason: substitution applies to the LHS of RULES, and
       --         if you "optimise" an identity coercion, you may
       --         lose a binder. We optimise the LHS of rules at
       --         construction time
375

376
    go (Lam bndr body) = Lam bndr' (subst_expr doc subst' body)
377 378
                       where
                         (subst', bndr') = substBndr subst bndr
379

380
    go (Let bind body) = Let bind' (subst_expr doc subst' body)
381 382
                       where
                         (subst', bind') = substBind subst bind
383 384

    go (Case scrut bndr ty alts) = Case (go scrut) bndr' (substTy subst ty) (map (go_alt subst') alts)
385 386
                                 where
                                 (subst', bndr') = substBndr subst bndr
387

388
    go_alt subst (con, bndrs, rhs) = (con, bndrs', subst_expr doc subst' rhs)
389 390
                                 where
                                   (subst', bndrs') = substBndrs subst bndrs
391

392 393
-- | Apply a substitution to an entire 'CoreBind', additionally returning an updated 'Subst'
-- that should be used by subsequent substitutions.
394 395
substBind, substBindSC :: Subst -> CoreBind -> (Subst, CoreBind)

396
substBindSC subst bind    -- Short-cut if the substitution is empty
397 398 399 400 401 402
  | not (isEmptySubst subst)
  = substBind subst bind
  | otherwise
  = case bind of
       NonRec bndr rhs -> (subst', NonRec bndr' rhs)
          where
403
            (subst', bndr') = substBndr subst bndr
404 405 406
       Rec pairs -> (subst', Rec (bndrs' `zip` rhss'))
          where
            (bndrs, rhss)    = unzip pairs
407
            (subst', bndrs') = substRecBndrs subst bndrs
408 409 410 411
            rhss' | isEmptySubst subst'
                  = rhss
                  | otherwise
                  = map (subst_expr (text "substBindSC") subst') rhss
412

413 414 415 416
substBind subst (NonRec bndr rhs)
  = (subst', NonRec bndr' (subst_expr (text "substBind") subst rhs))
  where
    (subst', bndr') = substBndr subst bndr
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
417

418 419 420 421 422 423
substBind subst (Rec pairs)
   = (subst', Rec (bndrs' `zip` rhss'))
   where
       (bndrs, rhss)    = unzip pairs
       (subst', bndrs') = substRecBndrs subst bndrs
       rhss' = map (subst_expr (text "substBind") subst') rhss
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
424

batterseapower's avatar
batterseapower committed
425
-- | De-shadowing the program is sometimes a useful pre-pass. It can be done simply
Gabor Greif's avatar
typos  
Gabor Greif committed
426
-- by running over the bindings with an empty substitution, because substitution
batterseapower's avatar
batterseapower committed
427 428
-- returns a result that has no-shadowing guaranteed.
--
429
-- (Actually, within a single /type/ there might still be shadowing, because
batterseapower's avatar
batterseapower committed
430
-- 'substTy' is a no-op for the empty substitution, but that's probably OK.)
431
--
432
-- [Aug 09] This function is not used in GHC at the moment, but seems so
433
--          short and simple that I'm going to leave it here
434
deShadowBinds :: CoreProgram -> CoreProgram
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
435
deShadowBinds binds = snd (mapAccumL substBind emptySubst binds)
436

Austin Seipp's avatar
Austin Seipp committed
437 438 439
{-
************************************************************************
*                                                                      *
440
        Substituting binders
Austin Seipp's avatar
Austin Seipp committed
441 442
*                                                                      *
************************************************************************
443 444 445 446 447

Remember that substBndr and friends are used when doing expression
substitution only.  Their only business is substitution, so they
preserve all IdInfo (suitably substituted).  For example, we *want* to
preserve occ info in rules.
Austin Seipp's avatar
Austin Seipp committed
448
-}
449

batterseapower's avatar
batterseapower committed
450
-- | Substitutes a 'Var' for another one according to the 'Subst' given, returning
451
-- the result and an updated 'Subst' that should be used by subsequent substitutions.
batterseapower's avatar
batterseapower committed
452
-- 'IdInfo' is preserved by this process, although it is substituted into appropriately.
453 454
substBndr :: Subst -> Var -> (Subst, Var)
substBndr subst bndr
455 456 457
  | isTyVar bndr  = substTyVarBndr subst bndr
  | isCoVar bndr  = substCoVarBndr subst bndr
  | otherwise     = substIdBndr (text "var-bndr") subst subst bndr
458

batterseapower's avatar
batterseapower committed
459
-- | Applies 'substBndr' to a number of 'Var's, accumulating a new 'Subst' left-to-right
460 461 462
substBndrs :: Subst -> [Var] -> (Subst, [Var])
substBndrs subst bndrs = mapAccumL substBndr subst bndrs

batterseapower's avatar
batterseapower committed
463
-- | Substitute in a mutually recursive group of 'Id's
464
substRecBndrs :: Subst -> [Id] -> (Subst, [Id])
465
substRecBndrs subst bndrs
466
  = (new_subst, new_bndrs)
467
  where         -- Here's the reason we need to pass rec_subst to subst_id
468
    (new_subst, new_bndrs) = mapAccumL (substIdBndr (text "rec-bndr") new_subst) subst bndrs
469

470 471 472 473 474
substIdBndr :: SDoc
            -> Subst            -- ^ Substitution to use for the IdInfo
            -> Subst -> Id      -- ^ Substitution and Id to transform
            -> (Subst, Id)      -- ^ Transformed pair
                                -- NB: unfolding may be zapped
475

476
substIdBndr _doc rec_subst subst@(Subst in_scope env tvs cvs) old_id
477
  = -- pprTrace "substIdBndr" (doc $$ ppr old_id $$ ppr in_scope) $
478
    (Subst (in_scope `extendInScopeSet` new_id) new_env tvs cvs, new_id)
479
  where
480
    id1 = uniqAway in_scope old_id      -- id1 is cloned if necessary
481
    id2 | no_type_change = id1
482
        | otherwise      = setIdType id1 (substTy subst old_ty)
483 484

    old_ty = idType old_id
485
    no_type_change = (isEmptyVarEnv tvs && isEmptyVarEnv cvs) ||
Richard Eisenberg's avatar
Richard Eisenberg committed
486
                     noFreeVarsOfType old_ty
487

488 489 490
        -- new_id has the right IdInfo
        -- The lazy-set is because we're in a loop here, with
        -- rec_subst, when dealing with a mutually-recursive group
491
    new_id = maybeModifyIdInfo mb_new_info id2
492
    mb_new_info = substIdInfo rec_subst id2 (idInfo id2)
493
        -- NB: unfolding info may be zapped
494

495 496
        -- Extend the substitution if the unique has changed
        -- See the notes with substTyVarBndr for the delVarEnv
497
    new_env | no_change = delVarEnv env old_id
498
            | otherwise = extendVarEnv env old_id (Var new_id)
499

simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
500
    no_change = id1 == old_id
501 502
        -- See Note [Extending the Subst]
        -- it's /not/ necessary to check mb_new_info and no_type_change
503

Austin Seipp's avatar
Austin Seipp committed
504
{-
505 506
Now a variant that unconditionally allocates a new unique.
It also unconditionally zaps the OccInfo.
Austin Seipp's avatar
Austin Seipp committed
507
-}
508

batterseapower's avatar
batterseapower committed
509
-- | Very similar to 'substBndr', but it always allocates a new 'Unique' for
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
510
-- each variable in its output.  It substitutes the IdInfo though.
511 512 513 514
cloneIdBndr :: Subst -> UniqSupply -> Id -> (Subst, Id)
cloneIdBndr subst us old_id
  = clone_id subst subst (old_id, uniqFromSupply us)

batterseapower's avatar
batterseapower committed
515 516
-- | Applies 'cloneIdBndr' to a number of 'Id's, accumulating a final
-- substitution from left to right
517 518 519 520
cloneIdBndrs :: Subst -> UniqSupply -> [Id] -> (Subst, [Id])
cloneIdBndrs subst us ids
  = mapAccumL (clone_id subst) subst (ids `zip` uniqsFromSupply us)

521 522 523 524
cloneBndrs :: Subst -> UniqSupply -> [Var] -> (Subst, [Var])
-- Works for all kinds of variables (typically case binders)
-- not just Ids
cloneBndrs subst us vs
525 526 527 528
  = mapAccumL (\subst (v, u) -> cloneBndr subst u v) subst (vs `zip` uniqsFromSupply us)

cloneBndr :: Subst -> Unique -> Var -> (Subst, Var)
cloneBndr subst uniq v
Simon Peyton Jones's avatar
Simon Peyton Jones committed
529 530
  | isTyVar v = cloneTyVarBndr subst v uniq
  | otherwise = clone_id subst subst (v,uniq)  -- Works for coercion variables too
531

batterseapower's avatar
batterseapower committed
532
-- | Clone a mutually recursive group of 'Id's
533 534 535 536 537
cloneRecIdBndrs :: Subst -> UniqSupply -> [Id] -> (Subst, [Id])
cloneRecIdBndrs subst us ids
  = (subst', ids')
  where
    (subst', ids') = mapAccumL (clone_id subst') subst
538
                               (ids `zip` uniqsFromSupply us)
539 540 541

-- Just like substIdBndr, except that it always makes a new unique
-- It is given the unique to use
542 543 544
clone_id    :: Subst                    -- Substitution for the IdInfo
            -> Subst -> (Id, Unique)    -- Substitution and Id to transform
            -> (Subst, Id)              -- Transformed pair
545

546 547
clone_id rec_subst subst@(Subst in_scope idvs tvs cvs) (old_id, uniq)
  = (Subst (in_scope `extendInScopeSet` new_id) new_idvs tvs new_cvs, new_id)
548
  where
549
    id1     = setVarUnique old_id uniq
550
    id2     = substIdType subst id1
551
    new_id  = maybeModifyIdInfo (substIdInfo rec_subst id2 (idInfo old_id)) id2
552 553
    (new_idvs, new_cvs) | isCoVar old_id = (idvs, extendVarEnv cvs old_id (mkCoVarCo new_id))
                        | otherwise      = (extendVarEnv idvs old_id (Var new_id), cvs)
554

Austin Seipp's avatar
Austin Seipp committed
555 556 557
{-
************************************************************************
*                                                                      *
558
                Types and Coercions
Austin Seipp's avatar
Austin Seipp committed
559 560
*                                                                      *
************************************************************************
561

562 563
For types and coercions we just call the corresponding functions in
Type and Coercion, but we have to repackage the substitution, from a
564
Subst to a TCvSubst.
Austin Seipp's avatar
Austin Seipp committed
565
-}
566 567

substTyVarBndr :: Subst -> TyVar -> (Subst, TyVar)
568
substTyVarBndr (Subst in_scope id_env tv_env cv_env) tv
569 570 571
  = case Type.substTyVarBndr (TCvSubst in_scope tv_env cv_env) tv of
        (TCvSubst in_scope' tv_env' cv_env', tv')
           -> (Subst in_scope' id_env tv_env' cv_env', tv')
572

573 574
cloneTyVarBndr :: Subst -> TyVar -> Unique -> (Subst, TyVar)
cloneTyVarBndr (Subst in_scope id_env tv_env cv_env) tv uniq
575 576 577
  = case Type.cloneTyVarBndr (TCvSubst in_scope tv_env cv_env) tv uniq of
        (TCvSubst in_scope' tv_env' cv_env', tv')
           -> (Subst in_scope' id_env tv_env' cv_env', tv')
578

579 580
substCoVarBndr :: Subst -> TyVar -> (Subst, TyVar)
substCoVarBndr (Subst in_scope id_env tv_env cv_env) cv
581 582
  = case Coercion.substCoVarBndr (TCvSubst in_scope tv_env cv_env) cv of
        (TCvSubst in_scope' tv_env' cv_env', cv')
583
           -> (Subst in_scope' id_env tv_env' cv_env', cv')
584

batterseapower's avatar
batterseapower committed
585
-- | See 'Type.substTy'
586
substTy :: Subst -> Type -> Type
587
substTy subst ty = Type.substTyUnchecked (getTCvSubst subst) ty
588

589 590
getTCvSubst :: Subst -> TCvSubst
getTCvSubst (Subst in_scope _ tenv cenv) = TCvSubst in_scope tenv cenv
591 592 593

-- | See 'Coercion.substCo'
substCo :: Subst -> Coercion -> Coercion
594
substCo subst co = Coercion.substCo (getTCvSubst subst) co
595

Austin Seipp's avatar
Austin Seipp committed
596 597 598
{-
************************************************************************
*                                                                      *
599
\section{IdInfo substitution}
Austin Seipp's avatar
Austin Seipp committed
600 601 602
*                                                                      *
************************************************************************
-}
603 604

substIdType :: Subst -> Id -> Id
605
substIdType subst@(Subst _ _ tv_env cv_env) id
Richard Eisenberg's avatar
Richard Eisenberg committed
606
  | (isEmptyVarEnv tv_env && isEmptyVarEnv cv_env) || noFreeVarsOfType old_ty = id
607
  | otherwise   = setIdType id (substTy subst old_ty)
608
                -- The tyCoVarsOfType is cheaper than it looks
609 610
                -- because we cache the free tyvars of the type
                -- in a Note in the id's type itself
611 612 613 614
  where
    old_ty = idType id

------------------
batterseapower's avatar
batterseapower committed
615 616
-- | Substitute into some 'IdInfo' with regard to the supplied new 'Id'.
substIdInfo :: Subst -> Id -> IdInfo -> Maybe IdInfo
617
substIdInfo subst new_id info
618
  | nothing_to_do = Nothing
619
  | otherwise     = Just (info `setRuleInfo`      substSpec subst new_id old_rules
620
                               `setUnfoldingInfo` substUnfolding subst old_unf)
621
  where
622
    old_rules     = ruleInfo info
623
    old_unf       = unfoldingInfo info
624
    nothing_to_do = isEmptyRuleInfo old_rules && not (isFragileUnfolding old_unf)
625 626

------------------
627
-- | Substitutes for the 'Id's within an unfolding
628
substUnfolding, substUnfoldingSC :: Subst -> Unfolding -> Unfolding
629 630
        -- Seq'ing on the returned Unfolding is enough to cause
        -- all the substitutions to happen completely
631

632
substUnfoldingSC subst unf       -- Short-cut version
633 634 635
  | isEmptySubst subst = unf
  | otherwise          = substUnfolding subst unf

636 637
substUnfolding subst df@(DFunUnfolding { df_bndrs = bndrs, df_args = args })
  = df { df_bndrs = bndrs', df_args = args' }
638
  where
639 640
    (subst',bndrs') = substBndrs subst bndrs
    args'           = map (substExpr (text "subst-unf:dfun") subst') args
641

642
substUnfolding subst unf@(CoreUnfolding { uf_tmpl = tmpl, uf_src = src })
643
        -- Retain an InlineRule!
644
  | not (isStableSource src)  -- Zap an unstable unfolding, to save substitution work
645
  = NoUnfolding
646
  | otherwise                 -- But keep a stable one!
647 648
  = seqExpr new_tmpl `seq`
    unf { uf_tmpl = new_tmpl }
649
  where
650
    new_tmpl = substExpr (text "subst-unf") subst tmpl
651

652
substUnfolding _ unf = unf      -- NoUnfolding, OtherCon
653 654 655 656

------------------
substIdOcc :: Subst -> Id -> Id
-- These Ids should not be substituted to non-Ids
657
substIdOcc subst v = case lookupIdSubst (text "substIdOcc") subst v of
658 659
                        Var v' -> v'
                        other  -> pprPanic "substIdOcc" (vcat [ppr v <+> ppr other, ppr subst])
660 661

------------------
batterseapower's avatar
batterseapower committed
662
-- | Substitutes for the 'Id's within the 'WorkerInfo' given the new function 'Id'
663 664 665
substSpec :: Subst -> Id -> RuleInfo -> RuleInfo
substSpec subst new_id (RuleInfo rules rhs_fvs)
  = seqRuleInfo new_spec `seq` new_spec
666
  where
667
    subst_ru_fn = const (idName new_id)
668
    new_spec = RuleInfo (map (substRule subst subst_ru_fn) rules)
669
                        (substDVarSet subst rhs_fvs)
670 671 672

------------------
substRulesForImportedIds :: Subst -> [CoreRule] -> [CoreRule]
673
substRulesForImportedIds subst rules
674 675 676
  = map (substRule subst not_needed) rules
  where
    not_needed name = pprPanic "substRulesForImportedIds" (ppr name)
677 678 679 680 681 682 683 684

------------------
substRule :: Subst -> (Name -> Name) -> CoreRule -> CoreRule

-- The subst_ru_fn argument is applied to substitute the ru_fn field
-- of the rule:
--    - Rules for *imported* Ids never change ru_fn
--    - Rules for *local* Ids are in the IdInfo for that Id,
685
--      and the ru_fn field is simply replaced by the new name
686
--      of the Id
687 688
substRule _ _ rule@(BuiltinRule {}) = rule
substRule subst subst_ru_fn rule@(Rule { ru_bndrs = bndrs, ru_args = args
689 690
                                       , ru_fn = fn_name, ru_rhs = rhs
                                       , ru_local = is_local })
691 692
  = rule { ru_bndrs = bndrs'
         , ru_fn    = if is_local
693
                        then subst_ru_fn fn_name
694 695 696 697 698
                        else fn_name
         , ru_args  = map (substExpr doc subst') args
         , ru_rhs   = substExpr (text "foo") subst' rhs }
           -- Do NOT optimise the RHS (previously we did simplOptExpr here)
           -- See Note [Substitute lazily]
699
  where
700
    doc = text "subst-rule" <+> ppr fn_name
701
    (subst', bndrs') = substBndrs subst bndrs
702 703

------------------
704 705 706
substDVarSet :: Subst -> DVarSet -> DVarSet
substDVarSet subst fvs
  = mkDVarSet $ fst $ foldr (subst_fv subst) ([], emptyVarSet) $ dVarSetElems fvs
707
  where
708 709
  subst_fv subst fv acc
     | isId fv = expr_fvs (lookupIdSubst (text "substDVarSet") subst fv) isLocalVar emptyVarSet $! acc
niteria's avatar
niteria committed
710
     | otherwise = tyCoFVsOfType (lookupTCvSubst subst fv) (const True) emptyVarSet $! acc
711 712 713

------------------
substTickish :: Subst -> Tickish Id -> Tickish Id
714 715 716 717
substTickish subst (Breakpoint n ids)
   = Breakpoint n (map do_one ids)
 where
    do_one = getIdFromTrivialExpr . lookupIdSubst (text "subst_tickish") subst
718 719
substTickish _subst other = other

720 721
{- Note [Substitute lazily]
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Gabor Greif's avatar
Gabor Greif committed
722
The functions that substitute over IdInfo must be pretty lazy, because
723
they are knot-tied by substRecBndrs.
724

725 726 727 728 729 730 731 732 733 734 735
One case in point was Trac #10627 in which a rule for a function 'f'
referred to 'f' (at a differnet type) on the RHS.  But instead of just
substituting in the rhs of the rule, we were calling simpleOptExpr, which
looked at the idInfo for 'f'; result <<loop>>.

In any case we don't need to optimise the RHS of rules, or unfoldings,
because the simplifier will do that.


Note [substTickish]
~~~~~~~~~~~~~~~~~~~~~~
736 737 738 739 740 741 742 743 744 745 746 747 748
A Breakpoint contains a list of Ids.  What happens if we ever want to
substitute an expression for one of these Ids?

First, we ensure that we only ever substitute trivial expressions for
these Ids, by marking them as NoOccInfo in the occurrence analyser.
Then, when substituting for the Id, we unwrap any type applications
and abstractions to get back to an Id, with getIdFromTrivialExpr.

Second, we have to ensure that we never try to substitute a literal
for an Id in a breakpoint.  We ensure this by never storing an Id with
an unlifted type in a Breakpoint - see Coverage.mkTickish.
Breakpoints can't handle free variables with unlifted types anyway.
-}
749

Austin Seipp's avatar
Austin Seipp committed
750
{-
751 752 753
Note [Worker inlining]
~~~~~~~~~~~~~~~~~~~~~~
A worker can get sustituted away entirely.
754 755 756
        - it might be trivial
        - it might simply be very small
We do not treat an InlWrapper as an 'occurrence' in the occurrence
757 758 759 760
analyser, so it's possible that the worker is not even in scope any more.

In all all these cases we simply drop the special case, returning to
InlVanilla.  The WARN is just so I can see if it happens a lot.
Austin Seipp's avatar
Austin Seipp committed
761
-}
762