d7ab114f4eeb804278ca82fed73b1f36fa1d98e1
[ghc-hetmet.git] / ghc / compiler / coreSyn / CoreTidy.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section{Tidying up Core}
5
6 \begin{code}
7 module CoreTidy (
8         tidyCorePgm, tidyExpr, tidyCoreExpr,
9         tidyBndr, tidyBndrs
10     ) where
11
12 #include "HsVersions.h"
13
14 import CmdLineOpts      ( DynFlags, DynFlag(..), opt_OmitInterfacePragmas )
15 import CoreSyn
16 import CoreUnfold       ( noUnfolding, mkTopUnfolding, okToUnfoldInHiFile )
17 import CoreFVs          ( ruleSomeFreeVars, exprSomeFreeVars )
18 import CoreLint         ( showPass, endPass )
19 import VarEnv
20 import VarSet
21 import Var              ( Id, Var )
22 import Id               ( idType, idInfo, idName, isExportedId, 
23                           idSpecialisation, idUnique, isDataConWrapId,
24                           mkVanillaGlobal, isLocalId, isRecordSelector,
25                           setIdUnfolding, hasNoBinding, mkUserLocal
26                         ) 
27 import IdInfo           {- loads of stuff -}
28 import Name             ( getOccName, nameOccName, globaliseName, setNameOcc, 
29                           localiseName, isGlobalName, setNameUnique
30                         )
31 import NameEnv          ( filterNameEnv )
32 import OccName          ( TidyOccEnv, initTidyOccEnv, tidyOccName )
33 import Type             ( tidyTopType, tidyType, tidyTyVar )
34 import Module           ( Module, moduleName )
35 import HscTypes         ( PersistentCompilerState( pcs_PRS ), 
36                           PersistentRenamerState( prsOrig ),
37                           NameSupply( nsNames, nsUniqs ),
38                           TypeEnv, extendTypeEnvList, 
39                           ModDetails(..), TyThing(..)
40                         )
41 import FiniteMap        ( lookupFM, addToFM )
42 import Maybes           ( maybeToBool, orElse )
43 import ErrUtils         ( showPass )
44 import SrcLoc           ( noSrcLoc )
45 import UniqFM           ( mapUFM )
46 import UniqSupply       ( splitUniqSupply, uniqFromSupply )
47 import List             ( partition )
48 import Util             ( mapAccumL )
49 import Outputable
50 \end{code}
51
52
53
54 %************************************************************************
55 %*                                                                      *
56 \subsection{What goes on}
57 %*                                                                      * 
58 %************************************************************************
59
60 [SLPJ: 19 Nov 00]
61
62 The plan is this.  
63
64 Step 1: Figure out external Ids
65 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
66 First we figure out which Ids are "external" Ids.  An
67 "external" Id is one that is visible from outside the compilation
68 unit.  These are
69         a) the user exported ones
70         b) ones mentioned in the unfoldings, workers, 
71            or rules of externally-visible ones 
72 This exercise takes a sweep of the bindings bottom to top.  Actually,
73 in Step 2 we're also going to need to know which Ids should be
74 exported with their unfoldings, so we produce not an IdSet but an
75 IdEnv Bool
76
77
78 Step 2: Tidy the program
79 ~~~~~~~~~~~~~~~~~~~~~~~~
80 Next we traverse the bindings top to bottom.  For each *top-level*
81 binder
82
83  1. Make it into a GlobalId
84
85  2. Give it a system-wide Unique.
86     [Even non-exported things need system-wide Uniques because the
87     byte-code generator builds a single Name->BCO symbol table.]
88
89     We use the NameSupply kept in the PersistentRenamerState as the
90     source of such system-wide uniques.
91
92     For external Ids, use the original-name cache in the NameSupply 
93     to ensure that the unique assigned is the same as the Id had 
94     in any previous compilation run.
95   
96  3. If it's an external Id, make it have a global Name and vice versa.
97     This is used by the code generator to decide whether
98     to make the label externally visible
99
100  4. Give external Ids a "tidy" occurrence name.  This means
101     we can print them in interface files without confusing 
102     "x" (unique 5) with "x" (unique 10).
103   
104  5. Give it its UTTERLY FINAL IdInfo; in ptic, 
105         * Its IdDetails becomes VanillaGlobal, reflecting the fact that
106           from now on we regard it as a global, not local, Id
107
108         * its unfolding, if it should have one
109         
110         * its arity, computed from the number of visible lambdas
111
112         * its CAF info, computed from what is free in its RHS
113
114                 
115 Finally, substitute these new top-level binders consistently
116 throughout, including in unfoldings.  We also tidy binders in
117 RHSs, so that they print nicely in interfaces.
118
119 \begin{code}
120 tidyCorePgm :: DynFlags -> Module
121             -> PersistentCompilerState
122             -> CgInfoEnv                -- Information from the back end,
123                                         -- to be splatted into the IdInfo
124             -> ModDetails
125             -> IO (PersistentCompilerState, ModDetails)
126
127 tidyCorePgm dflags mod pcs cg_info_env
128             (ModDetails { md_types = env_tc, md_insts = insts_tc, 
129                           md_binds = binds_in, md_rules = orphans_in })
130   = do  { showPass dflags "Tidy Core"
131
132         ; let ext_ids   = findExternalSet   binds_in orphans_in
133         ; let ext_rules = findExternalRules binds_in orphans_in ext_ids
134
135         -- We also make sure to avoid any exported binders.  Consider
136         --      f{-u1-} = 1     -- Local decl
137         --      ...
138         --      f{-u2-} = 2     -- Exported decl
139         --
140         -- The second exported decl must 'get' the name 'f', so we
141         -- have to put 'f' in the avoids list before we get to the first
142         -- decl.  tidyTopId then does a no-op on exported binders.
143         ; let   prs           = pcs_PRS pcs
144                 orig_ns       = prsOrig prs
145
146                 init_tidy_env = (orig_ns, initTidyOccEnv avoids, emptyVarEnv)
147                 avoids        = [getOccName bndr | bndr <- bindersOfBinds binds_in,
148                                                    isGlobalName (idName bndr)]
149
150         ; let ((orig_ns', occ_env, subst_env), tidy_binds) 
151                         = mapAccumL (tidyTopBind mod ext_ids cg_info_env) 
152                                     init_tidy_env binds_in
153
154         ; let tidy_rules = tidyIdRules (occ_env,subst_env) ext_rules
155
156         ; let prs' = prs { prsOrig = orig_ns' }
157               pcs' = pcs { pcs_PRS = prs' }
158
159         ; let final_ids  = [ id | bind <- tidy_binds
160                            , id <- bindersOf bind
161                            , isGlobalName (idName id)]
162
163                 -- Dfuns are local Ids that might have
164                 -- changed their unique during tidying
165         ; let lookup_dfun_id id = lookupVarEnv subst_env id `orElse` 
166                                   pprPanic "lookup_dfun_id" (ppr id)
167
168
169         ; let tidy_type_env = mkFinalTypeEnv env_tc final_ids
170               tidy_dfun_ids = map lookup_dfun_id insts_tc
171
172         ; let tidy_details = ModDetails { md_types = tidy_type_env,
173                                           md_rules = tidy_rules,
174                                           md_insts = tidy_dfun_ids,
175                                           md_binds = tidy_binds }
176
177         ; endPass dflags "Tidy Core" Opt_D_dump_simpl tidy_binds
178
179         ; return (pcs', tidy_details)
180         }
181
182 tidyCoreExpr :: CoreExpr -> IO CoreExpr
183 tidyCoreExpr expr = return (tidyExpr emptyTidyEnv expr)
184 \end{code}
185
186
187 %************************************************************************
188 %*                                                                      *
189 \subsection{Write a new interface file}
190 %*                                                                      *
191 %************************************************************************
192
193 \begin{code}
194 mkFinalTypeEnv :: TypeEnv       -- From typechecker
195                -> [Id]          -- Final Ids
196                -> TypeEnv
197
198 mkFinalTypeEnv type_env final_ids
199   = extendTypeEnvList (filterNameEnv keep_it type_env)
200                       (map AnId final_ids)
201   where
202         -- The competed type environment is gotten from
203         --      a) keeping the types and classes
204         --      b) removing all Ids, 
205         --      c) adding Ids with correct IdInfo, including unfoldings,
206         --              gotten from the bindings
207         -- From (c) we keep only those Ids with Global names;
208         --          the CoreTidy pass makes sure these are all and only
209         --          the externally-accessible ones
210         -- This truncates the type environment to include only the 
211         -- exported Ids and things needed from them, which saves space
212         --
213         -- However, we do keep things like constructors, which should not appear 
214         -- in interface files, because they are needed by importing modules when
215         -- using the compilation manager
216
217         -- We keep "hasNoBinding" Ids, notably constructor workers, 
218         -- because they won't appear in the bindings from which final_ids are derived!
219     keep_it (AnId id) = hasNoBinding id -- Remove all Ids except constructor workers
220     keep_it other     = True            -- Keep all TyCons and Classes
221 \end{code}
222
223 \begin{code}
224 findExternalRules :: [CoreBind]
225                   -> [IdCoreRule] -- Orphan rules
226                   -> IdEnv a      -- Ids that are exported, so we need their rules
227                   -> [IdCoreRule]
228   -- The complete rules are gotten by combining
229   --    a) the orphan rules
230   --    b) rules embedded in the top-level Ids
231 findExternalRules binds orphan_rules ext_ids
232   | opt_OmitInterfacePragmas = []
233   | otherwise
234   = orphan_rules ++ local_rules
235   where
236     local_rules  = [ (id, rule)
237                    | id <- bindersOfBinds binds,
238                      id `elemVarEnv` ext_ids,
239                      rule <- rulesRules (idSpecialisation id),
240                      not (isBuiltinRule rule)
241                         -- We can't print builtin rules in interface files
242                         -- Since they are built in, an importing module
243                         -- will have access to them anyway
244                  ]
245 \end{code}
246
247 %************************************************************************
248 %*                                                                      *
249 \subsection{Step 1: finding externals}
250 %*                                                                      * 
251 %************************************************************************
252
253 \begin{code}
254 findExternalSet :: [CoreBind] -> [IdCoreRule]
255                 -> IdEnv Bool   -- In domain => external
256                                 -- Range = True <=> show unfolding
257         -- Step 1 from the notes above
258 findExternalSet binds orphan_rules
259   = foldr find init_needed binds
260   where
261     orphan_rule_ids :: IdSet
262     orphan_rule_ids = unionVarSets [ ruleSomeFreeVars isLocalId rule 
263                                    | (_, rule) <- orphan_rules]
264     init_needed :: IdEnv Bool
265     init_needed = mapUFM (\_ -> False) orphan_rule_ids
266         -- The mapUFM is a bit cheesy.  It is a cheap way
267         -- to turn the set of orphan_rule_ids, which we use to initialise
268         -- the sweep, into a mapping saying 'don't expose unfolding'    
269         -- (When we come to the binding site we may change our mind, of course.)
270
271     find (NonRec id rhs) needed
272         | need_id needed id = addExternal (id,rhs) needed
273         | otherwise         = needed
274     find (Rec prs) needed   = find_prs prs needed
275
276         -- For a recursive group we have to look for a fixed point
277     find_prs prs needed 
278         | null needed_prs = needed
279         | otherwise       = find_prs other_prs new_needed
280         where
281           (needed_prs, other_prs) = partition (need_pr needed) prs
282           new_needed = foldr addExternal needed needed_prs
283
284         -- The 'needed' set contains the Ids that are needed by earlier
285         -- interface file emissions.  If the Id isn't in this set, and isn't
286         -- exported, there's no need to emit anything
287     need_id needed_set id       = id `elemVarEnv` needed_set || isExportedId id 
288     need_pr needed_set (id,rhs) = need_id needed_set id
289
290 addExternal :: (Id,CoreExpr) -> IdEnv Bool -> IdEnv Bool
291 -- The Id is needed; extend the needed set
292 -- with it and its dependents (free vars etc)
293 addExternal (id,rhs) needed
294   = extendVarEnv (foldVarSet add_occ needed new_needed_ids)
295                  id show_unfold
296   where
297     add_occ id needed = extendVarEnv needed id False
298         -- "False" because we don't know we need the Id's unfolding
299         -- We'll override it later when we find the binding site
300
301     new_needed_ids | opt_OmitInterfacePragmas = emptyVarSet
302                    | otherwise                = worker_ids      `unionVarSet`
303                                                 unfold_ids      `unionVarSet`
304                                                 spec_ids
305
306     idinfo         = idInfo id
307     dont_inline    = isNeverInlinePrag (inlinePragInfo idinfo)
308     loop_breaker   = isLoopBreaker (occInfo idinfo)
309     bottoming_fn   = isBottomingStrictness (strictnessInfo idinfo)
310     spec_ids       = rulesRhsFreeVars (specInfo idinfo)
311     worker_info    = workerInfo idinfo
312
313         -- Stuff to do with the Id's unfolding
314         -- The simplifier has put an up-to-date unfolding
315         -- in the IdInfo, but the RHS will do just as well
316     unfolding    = unfoldingInfo idinfo
317     rhs_is_small = not (neverUnfold unfolding)
318
319         -- We leave the unfolding there even if there is a worker
320         -- In GHCI the unfolding is used by importers
321         -- When writing an interface file, we omit the unfolding 
322         -- if there is a worker
323     show_unfold = not bottoming_fn       &&     -- Not necessary
324                   not dont_inline        &&
325                   not loop_breaker       &&
326                   rhs_is_small           &&     -- Small enough
327                   okToUnfoldInHiFile rhs        -- No casms etc
328
329     unfold_ids | show_unfold = exprSomeFreeVars isLocalId rhs
330                | otherwise   = emptyVarSet
331
332     worker_ids = case worker_info of
333                    HasWorker work_id _ -> unitVarSet work_id
334                    otherwise           -> emptyVarSet
335 \end{code}
336
337
338 %************************************************************************
339 %*                                                                      *
340 \subsection{Step 2: top-level tidying}
341 %*                                                                      *
342 %************************************************************************
343
344
345 \begin{code}
346 type TopTidyEnv = (NameSupply, TidyOccEnv, VarEnv Var)
347
348 -- TopTidyEnv: when tidying we need to know
349 --   * ns: The NameSupply, containing a unique supply and any pre-ordained Names.  
350 --        These may have arisen because the
351 --        renamer read in an interface file mentioning M.$wf, say,
352 --        and assigned it unique r77.  If, on this compilation, we've
353 --        invented an Id whose name is $wf (but with a different unique)
354 --        we want to rename it to have unique r77, so that we can do easy
355 --        comparisons with stuff from the interface file
356 --
357 --   * occ_env: The TidyOccEnv, which tells us which local occurrences 
358 --     are 'used'
359 --
360 --   * subst_env: A Var->Var mapping that substitutes the new Var for the old
361 \end{code}
362
363
364 \begin{code}
365 tidyTopBind :: Module
366             -> IdEnv Bool       -- Domain = Ids that should be external
367                                 -- True <=> their unfolding is external too
368             -> CgInfoEnv
369             -> TopTidyEnv -> CoreBind
370             -> (TopTidyEnv, CoreBind)
371
372 tidyTopBind mod ext_ids cg_info_env top_tidy_env (NonRec bndr rhs)
373   = ((orig,occ,subst) , NonRec bndr' rhs')
374   where
375     ((orig,occ,subst), bndr')
376          = tidyTopBinder mod ext_ids cg_info_env rec_tidy_env rhs' top_tidy_env bndr
377     rec_tidy_env = (occ,subst)
378     rhs' = tidyExpr rec_tidy_env rhs
379
380 tidyTopBind mod ext_ids cg_info_env top_tidy_env (Rec prs)
381   = (final_env, Rec prs')
382   where
383     (final_env@(_,occ,subst), prs') = mapAccumL do_one top_tidy_env prs
384     rec_tidy_env = (occ,subst)
385
386     do_one top_tidy_env (bndr,rhs) 
387         = ((orig,occ,subst), (bndr',rhs'))
388         where
389         ((orig,occ,subst), bndr')
390            = tidyTopBinder mod ext_ids cg_info_env 
391                 rec_tidy_env rhs' top_tidy_env bndr
392
393         rhs' = tidyExpr rec_tidy_env rhs
394
395 tidyTopBinder :: Module -> IdEnv Bool
396               -> CgInfoEnv
397               -> TidyEnv -> CoreExpr
398                         -- The TidyEnv is used to tidy the IdInfo
399                         -- The expr is the already-tided RHS
400                         -- Both are knot-tied: don't look at them!
401               -> TopTidyEnv -> Id -> (TopTidyEnv, Id)
402   -- NB: tidyTopBinder doesn't affect the unique supply
403
404 tidyTopBinder mod ext_ids cg_info_env tidy_env rhs
405               env@(ns2, occ_env2, subst_env2) id
406
407   | isDataConWrapId id  -- Don't tidy constructor wrappers
408   = (env, id)           -- The Id is stored in the TyCon, so it would be bad
409                         -- if anything changed
410
411 -- HACK ALERT: we *do* tidy record selectors.  Reason: they mention error
412 -- messages, which may be floated out:
413 --      x_field pt = case pt of
414 --                      Rect x y -> y
415 --                      Pol _ _  -> error "buggle wuggle"
416 -- The error message will be floated out so we'll get
417 --      lvl5 = error "buggle wuggle"
418 --      x_field pt = case pt of
419 --                      Rect x y -> y
420 --                      Pol _ _  -> lvl5
421 --
422 -- When this happens, it's vital that the Id exposed to importing modules
423 -- (by ghci) mentions lvl5 in its unfolding, not the un-tidied version.
424 -- 
425 -- What about the Id in the TyCon?  It probably shouldn't be in the TyCon at
426 -- all, but in any case it will have the error message inline so it won't matter.
427
428
429   | isRecordSelector id -- We can't use the "otherwise" case, because that
430                         -- forgets the IdDetails, which forgets that this is
431                         -- a record selector, which confuses an importing module
432   = (env, id `setIdUnfolding` unfold_info)
433
434   | otherwise
435         -- This function is the heart of Step 2
436         -- The second env is the one to use for the IdInfo
437         -- It's necessary because when we are dealing with a recursive
438         -- group, a variable late in the group might be mentioned
439         -- in the IdInfo of one early in the group
440
441         -- The rhs is already tidied
442         
443   = ((orig_env', occ_env', subst_env'), id')
444   where
445     (orig_env', occ_env', name') = tidyTopName mod ns2 occ_env2
446                                                is_external
447                                                (idName id)
448     ty'     = tidyTopType (idType id)
449     cg_info = lookupCgInfo cg_info_env name'
450     idinfo' = tidyIdInfo tidy_env is_external unfold_info cg_info id
451
452     id'        = mkVanillaGlobal name' ty' idinfo'
453     subst_env' = extendVarEnv subst_env2 id id'
454
455     maybe_external = lookupVarEnv ext_ids id
456     is_external    = maybeToBool maybe_external
457
458     -- Expose an unfolding if ext_ids tells us to
459     show_unfold = maybe_external `orElse` False
460     unfold_info | show_unfold = mkTopUnfolding rhs
461                 | otherwise   = noUnfolding
462
463
464 tidyIdInfo tidy_env is_external unfold_info cg_info id
465   | opt_OmitInterfacePragmas || not is_external
466         -- No IdInfo if the Id isn't external, or if we don't have -O
467   = vanillaIdInfo 
468         `setCgInfo`         cg_info
469         `setStrictnessInfo` strictnessInfo core_idinfo
470         -- Keep strictness; it's used by CorePrep
471
472   | otherwise
473   =  vanillaIdInfo 
474         `setCgInfo`         cg_info
475         `setCprInfo`        cprInfo core_idinfo
476         `setStrictnessInfo` strictnessInfo core_idinfo
477         `setInlinePragInfo` inlinePragInfo core_idinfo
478         `setUnfoldingInfo`  unfold_info
479         `setWorkerInfo`     tidyWorker tidy_env (workerInfo core_idinfo)
480         -- NB: we throw away the Rules
481         -- They have already been extracted by findExternalRules
482   where
483     core_idinfo = idInfo id
484
485
486 -- This is where we set names to local/global based on whether they really are 
487 -- externally visible (see comment at the top of this module).  If the name
488 -- was previously local, we have to give it a unique occurrence name if
489 -- we intend to globalise it.
490 tidyTopName mod ns occ_env external name
491   | global && internal = (ns, occ_env, localiseName name)
492
493   | global && external = (ns, occ_env, name)
494         -- Global names are assumed to have been allocated by the renamer,
495         -- so they already have the "right" unique
496         -- And it's a system-wide unique too
497
498   | local  && internal = (ns { nsUniqs = us2 }, occ_env', unique_name)
499         -- Even local, internal names must get a unique occurrence, because
500         -- if we do -split-objs we globalise the name later, in the code generator
501         --
502         -- Similarly, we must make sure it has a system-wide Unique, because
503         -- the byte-code generator builds a system-wide Name->BCO symbol table
504
505   | local  && external = case lookupFM ns_names key of
506                            Just orig -> (ns,                                        occ_env', orig)
507                            Nothing   -> (ns { nsUniqs = us2, nsNames = ns_names' }, occ_env', global_name)
508         -- If we want to globalise a currently-local name, check
509         -- whether we have already assigned a unique for it.
510         -- If so, use it; if not, extend the table
511
512   where
513     global           = isGlobalName name
514     local            = not global
515     internal         = not external
516
517     (occ_env', occ') = tidyOccName occ_env (nameOccName name)
518     key              = (moduleName mod, occ')
519     ns_names         = nsNames ns
520     ns_uniqs         = nsUniqs ns
521     (us1, us2)       = splitUniqSupply ns_uniqs
522     unique_name      = setNameUnique (setNameOcc name occ') (uniqFromSupply us1)
523     global_name      = globaliseName unique_name mod
524     ns_names'        = addToFM ns_names key global_name
525
526
527 ------------  Worker  --------------
528 tidyWorker tidy_env (HasWorker work_id wrap_arity) 
529   = HasWorker (tidyVarOcc tidy_env work_id) wrap_arity
530 tidyWorker tidy_env other
531   = NoWorker
532
533 ------------  Rules  --------------
534 tidyIdRules :: TidyEnv -> [IdCoreRule] -> [IdCoreRule]
535 tidyIdRules env [] = []
536 tidyIdRules env ((fn,rule) : rules)
537   = tidyRule env rule           =: \ rule ->
538     tidyIdRules env rules       =: \ rules ->
539      ((tidyVarOcc env fn, rule) : rules)
540
541 tidyRule :: TidyEnv -> CoreRule -> CoreRule
542 tidyRule env rule@(BuiltinRule _) = rule
543 tidyRule env (Rule name vars tpl_args rhs)
544   = tidyBndrs env vars                  =: \ (env', vars) ->
545     map (tidyExpr env') tpl_args        =: \ tpl_args ->
546      (Rule name vars tpl_args (tidyExpr env' rhs))
547 \end{code}
548
549 %************************************************************************
550 %*                                                                      *
551 \subsection{Step 2: inner tidying
552 %*                                                                      *
553 %************************************************************************
554
555 \begin{code}
556 tidyBind :: TidyEnv
557          -> CoreBind
558          ->  (TidyEnv, CoreBind)
559
560 tidyBind env (NonRec bndr rhs)
561   = tidyBndrWithRhs env (bndr,rhs) =: \ (env', bndr') ->
562     (env', NonRec bndr' (tidyExpr env' rhs))
563
564 tidyBind env (Rec prs)
565   = mapAccumL tidyBndrWithRhs env prs   =: \ (env', bndrs') ->
566     map (tidyExpr env') (map snd prs)   =: \ rhss' ->
567     (env', Rec (zip bndrs' rhss'))
568
569
570 tidyExpr env (Var v)    =  Var (tidyVarOcc env v)
571 tidyExpr env (Type ty)  =  Type (tidyType env ty)
572 tidyExpr env (Lit lit)  =  Lit lit
573 tidyExpr env (App f a)  =  App (tidyExpr env f) (tidyExpr env a)
574 tidyExpr env (Note n e) =  Note (tidyNote env n) (tidyExpr env e)
575
576 tidyExpr env (Let b e) 
577   = tidyBind env b      =: \ (env', b') ->
578     Let b' (tidyExpr env' e)
579
580 tidyExpr env (Case e b alts)
581   = tidyBndr env b      =: \ (env', b) ->
582     Case (tidyExpr env e) b (map (tidyAlt env') alts)
583
584 tidyExpr env (Lam b e)
585   = tidyBndr env b      =: \ (env', b) ->
586     Lam b (tidyExpr env' e)
587
588
589 tidyAlt env (con, vs, rhs)
590   = tidyBndrs env vs    =: \ (env', vs) ->
591     (con, vs, tidyExpr env' rhs)
592
593 tidyNote env (Coerce t1 t2)  = Coerce (tidyType env t1) (tidyType env t2)
594 tidyNote env note            = note
595 \end{code}
596
597
598 %************************************************************************
599 %*                                                                      *
600 \subsection{Tidying up non-top-level binders}
601 %*                                                                      *
602 %************************************************************************
603
604 \begin{code}
605 tidyVarOcc (_, var_env) v = case lookupVarEnv var_env v of
606                                   Just v' -> v'
607                                   Nothing -> v
608
609 -- tidyBndr is used for lambda and case binders
610 tidyBndr :: TidyEnv -> Var -> (TidyEnv, Var)
611 tidyBndr env var
612   | isTyVar var = tidyTyVar env var
613   | otherwise   = tidyId env var
614
615 tidyBndrs :: TidyEnv -> [Var] -> (TidyEnv, [Var])
616 tidyBndrs env vars = mapAccumL tidyBndr env vars
617
618 -- tidyBndrWithRhs is used for let binders
619 tidyBndrWithRhs :: TidyEnv -> (Id, CoreExpr) -> (TidyEnv, Var)
620 tidyBndrWithRhs env (id,rhs) = tidyId env id
621
622 tidyId :: TidyEnv -> Id -> (TidyEnv, Id)
623 tidyId env@(tidy_env, var_env) id
624   =     -- Non-top-level variables
625     let 
626         -- Give the Id a fresh print-name, *and* rename its type
627         -- The SrcLoc isn't important now, 
628         -- though we could extract it from the Id
629         -- 
630         -- All local Ids now have the same IdInfo, which should save some
631         -- space.
632         (tidy_env', occ') = tidyOccName tidy_env (getOccName id)
633         ty'               = tidyType (tidy_env,var_env) (idType id)
634         id'               = mkUserLocal occ' (idUnique id) ty' noSrcLoc
635         var_env'          = extendVarEnv var_env id id'
636     in
637      ((tidy_env', var_env'), id')
638 \end{code}
639
640 \begin{code}
641 m =: k = m `seq` k m
642 \end{code}