[project @ 1996-05-16 09:42:08 by partain]
[ghc-hetmet.git] / ghc / compiler / coreSyn / CoreUnfold.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1994-1996
3 %
4 \section[CoreUnfold]{Core-syntax unfoldings}
5
6 Unfoldings (which can travel across module boundaries) are in Core
7 syntax (namely @CoreExpr@s).
8
9 The type @UnfoldingDetails@ sits ``above'' simply-Core-expressions
10 unfoldings, capturing ``higher-level'' things we know about a binding,
11 usually things that the simplifier found out (e.g., ``it's a
12 literal'').  In the corner of a @GenForm@ unfolding, you will
13 find, unsurprisingly, a Core expression.
14
15 \begin{code}
16 #include "HsVersions.h"
17
18 module CoreUnfold (
19         UnfoldingDetails(..), UnfoldingGuidance(..), -- types
20         FormSummary(..),
21
22         mkFormSummary,
23         mkGenForm,
24         mkMagicUnfolding,
25         modifyUnfoldingDetails,
26         calcUnfoldingGuidance,
27         mentionedInUnfolding
28     ) where
29
30 import Ubiq
31 import IdLoop    -- for paranoia checking;
32                  -- and also to get mkMagicUnfoldingFun
33 import PrelLoop  -- for paranoia checking
34
35 import Bag              ( emptyBag, unitBag, unionBags, Bag )
36 import BinderInfo       ( oneTextualOcc, oneSafeOcc )
37 import CgCompInfo       ( uNFOLDING_CHEAP_OP_COST,
38                           uNFOLDING_DEAR_OP_COST,
39                           uNFOLDING_NOREP_LIT_COST
40                         )
41 import CoreSyn
42 import CoreUtils        ( coreExprType, manifestlyWHNF )
43 import CostCentre       ( ccMentionsId )
44 import Id               ( IdSet(..), GenId{-instances-} )
45 import IdInfo           ( bottomIsGuaranteed )
46 import Literal          ( isNoRepLit, isLitLitLit )
47 import Pretty
48 import PrimOp           ( primOpCanTriggerGC, PrimOp(..) )
49 import TyCon            ( tyConFamilySize )
50 import Type             ( getAppDataTyConExpandingDicts )
51 import UniqSet          ( emptyUniqSet, unitUniqSet, mkUniqSet,
52                           addOneToUniqSet, unionUniqSets
53                         )
54 import Usage            ( UVar(..) )
55 import Util             ( isIn, panic )
56
57 whatsMentionedInId = panic "whatsMentionedInId (CoreUnfold)"
58 getMentionedTyConsAndClassesFromType = panic "getMentionedTyConsAndClassesFromType (CoreUnfold)"
59 \end{code}
60
61 %************************************************************************
62 %*                                                                      *
63 \subsection{@UnfoldingDetails@ and @UnfoldingGuidance@ types}
64 %*                                                                      *
65 %************************************************************************
66
67 (And @FormSummary@, too.)
68
69 \begin{code}
70 data UnfoldingDetails
71   = NoUnfoldingDetails
72
73   | LitForm
74         Literal
75
76   | OtherLitForm
77         [Literal]               -- It is a literal, but definitely not one of these
78
79   | ConForm
80         Id                      -- The constructor
81         [CoreArg]               -- Type/value arguments; NB OutArgs, already cloned
82
83   | OtherConForm
84         [Id]                    -- It definitely isn't one of these constructors
85                                 -- This captures the situation in the default branch of
86                                 -- a case:  case x of
87                                 --              c1 ... -> ...
88                                 --              c2 ... -> ...
89                                 --              v -> default-rhs
90                                 -- Then in default-rhs we know that v isn't c1 or c2.
91                                 --
92                                 -- NB.  In the degenerate: case x of {v -> default-rhs}
93                                 -- x will be bound to
94                                 --      OtherConForm []
95                                 -- which captures the idea that x is eval'd but we don't
96                                 -- know which constructor.
97
98
99   | GenForm
100         Bool                    -- True <=> At most one textual occurrence of the
101                                 --              binder in its scope, *or*
102                                 --              if we are happy to duplicate this
103                                 --              binding.
104         FormSummary             -- Tells whether the template is a WHNF or bottom
105         TemplateOutExpr         -- The template
106         UnfoldingGuidance       -- Tells about the *size* of the template.
107
108   | MagicForm
109         Unique                  -- of the Id whose magic unfolding this is
110         MagicUnfoldingFun
111
112 type TemplateOutExpr = GenCoreExpr (Id, BinderInfo) Id TyVar UVar
113         -- An OutExpr with occurrence info attached.  This is used as
114         -- a template in GeneralForms.
115
116 mkMagicUnfolding :: Unique -> UnfoldingDetails
117 mkMagicUnfolding tag  = MagicForm tag (mkMagicUnfoldingFun tag)
118
119 data FormSummary
120   = WhnfForm            -- Expression is WHNF
121   | BottomForm          -- Expression is guaranteed to be bottom. We're more gung
122                         -- ho about inlining such things, because it can't waste work
123   | OtherForm           -- Anything else
124
125 instance Outputable FormSummary where
126    ppr sty WhnfForm   = ppStr "WHNF"
127    ppr sty BottomForm = ppStr "Bot"
128    ppr sty OtherForm  = ppStr "Other"
129
130 --???mkFormSummary :: StrictnessInfo -> GenCoreExpr bndr Id -> FormSummary
131 mkFormSummary si expr
132   | manifestlyWHNF     expr = WhnfForm
133   | bottomIsGuaranteed si   = BottomForm
134
135   -- Chances are that the Id will be decorated with strictness info
136   -- telling that the RHS is definitely bottom.  This *might* not be the
137   -- case, if it's been a while since strictness analysis, but leaving out
138   -- the test for manifestlyBottom makes things a little more efficient.
139   -- We can always put it back...
140   -- | manifestlyBottom expr  = BottomForm
141
142   | otherwise = OtherForm
143 \end{code}
144
145 \begin{code}
146 data UnfoldingGuidance
147   = UnfoldNever                 -- Don't do it!
148
149   | UnfoldAlways                -- There is no "original" definition,
150                                 -- so you'd better unfold.  Or: something
151                                 -- so cheap to unfold (e.g., 1#) that
152                                 -- you should do it absolutely always.
153
154   | EssentialUnfolding          -- Like UnfoldAlways, but you *must* do
155                                 -- it absolutely always.
156                                 -- This is what we use for data constructors
157                                 -- and PrimOps, because we don't feel like
158                                 -- generating curried versions "just in case".
159
160   | UnfoldIfGoodArgs    Int     -- if "m" type args and "n" value args; and
161                         Int     -- those val args are manifestly data constructors
162                         [Bool]  -- the val-arg positions marked True
163                                 -- (i.e., a simplification will definitely
164                                 -- be possible).
165                         Int     -- The "size" of the unfolding; to be elaborated
166                                 -- later. ToDo
167
168   | BadUnfolding                -- This is used by TcPragmas if the *lazy*
169                                 -- lintUnfolding test fails
170                                 -- It will never escape from the IdInfo as
171                                 -- it is caught by getInfo_UF and converted
172                                 -- to NoUnfoldingDetails
173 \end{code}
174
175 \begin{code}
176 instance Outputable UnfoldingGuidance where
177     ppr sty UnfoldNever         = ppStr "_N_"
178     ppr sty UnfoldAlways        = ppStr "_ALWAYS_"
179     ppr sty EssentialUnfolding  = ppStr "_ESSENTIAL_" -- shouldn't appear in an iface
180     ppr sty (UnfoldIfGoodArgs t v cs size)
181       = ppCat [ppStr "_IF_ARGS_", ppInt t, ppInt v,
182                if null cs       -- always print *something*
183                 then ppChar 'X'
184                 else ppBesides (map pp_c cs),
185                ppInt size ]
186       where
187         pp_c False = ppChar 'X'
188         pp_c True  = ppChar 'C'
189 \end{code}
190
191
192 %************************************************************************
193 %*                                                                      *
194 \subsection{@mkGenForm@ and @modifyUnfoldingDetails@}
195 %*                                                                      *
196 %************************************************************************
197
198 \begin{code}
199 mkGenForm :: Bool               -- Ok to Dup code down different case branches,
200                                 -- because of either a flag saying so,
201                                 -- or alternatively the object is *SMALL*
202           -> BinderInfo         --
203           -> FormSummary
204           -> TemplateOutExpr    -- Template
205           -> UnfoldingGuidance  -- Tells about the *size* of the template.
206           -> UnfoldingDetails
207
208 mkGenForm safe_to_dup occ_info WhnfForm template guidance
209   = GenForm (oneTextualOcc safe_to_dup occ_info) WhnfForm template guidance
210
211 mkGenForm safe_to_dup occ_info form_summary template guidance
212   | oneSafeOcc safe_to_dup occ_info     -- Non-WHNF with only safe occurrences
213   = GenForm True form_summary template guidance
214
215   | otherwise                           -- Not a WHNF, many occurrences
216   = NoUnfoldingDetails
217 \end{code}
218
219 \begin{code}
220 modifyUnfoldingDetails
221         :: Bool         -- OK to dup
222         -> BinderInfo   -- New occurrence info for the thing
223         -> UnfoldingDetails
224         -> UnfoldingDetails
225
226 modifyUnfoldingDetails ok_to_dup occ_info
227         (GenForm only_one form_summary template guidance)
228   | only_one  = mkGenForm ok_to_dup occ_info form_summary template guidance
229
230 modifyUnfoldingDetails ok_to_dup occ_info other = other
231 \end{code}
232
233
234 %************************************************************************
235 %*                                                                      *
236 \subsection[calcUnfoldingGuidance]{Calculate ``unfolding guidance'' for an expression}
237 %*                                                                      *
238 %************************************************************************
239
240 \begin{code}
241 calcUnfoldingGuidance
242         :: Bool             -- True <=> OK if _scc_s appear in expr
243         -> Int              -- bomb out if size gets bigger than this
244         -> CoreExpr    -- expression to look at
245         -> UnfoldingGuidance
246
247 calcUnfoldingGuidance scc_s_OK bOMB_OUT_SIZE expr
248   = let
249         (use_binders, ty_binders, val_binders, body) = collectBinders expr
250     in
251     case (sizeExpr scc_s_OK bOMB_OUT_SIZE val_binders body) of
252
253       Nothing                -> UnfoldNever
254
255       Just (size, cased_args)
256         -> let
257                uf = UnfoldIfGoodArgs
258                         (length ty_binders)
259                         (length val_binders)
260                         [ b `is_elem` cased_args | b <- val_binders ]
261                         size
262            in
263            -- pprTrace "calcUnfold:" (ppAbove (ppr PprDebug uf) (ppr PprDebug expr))
264            uf
265   where
266     is_elem = isIn "calcUnfoldingGuidance"
267 \end{code}
268
269 \begin{code}
270 sizeExpr :: Bool            -- True <=> _scc_s OK
271          -> Int             -- Bomb out if it gets bigger than this
272          -> [Id]            -- Arguments; we're interested in which of these
273                             -- get case'd
274          -> CoreExpr
275          -> Maybe (Int,     -- Size
276                    [Id]     -- Subset of args which are cased
277             )
278
279 sizeExpr scc_s_OK bOMB_OUT_SIZE args expr
280   = size_up expr
281   where
282     size_up (Var v)        = sizeOne
283     size_up (App fun arg)  = size_up fun `addSize` size_up_arg arg
284     size_up (Lit lit)      = if isNoRepLit lit
285                                then sizeN uNFOLDING_NOREP_LIT_COST
286                                else sizeOne
287
288     size_up (SCC _ (Con _ _)) = Nothing -- **** HACK *****
289     size_up (SCC lbl body)
290       = if scc_s_OK then size_up body else Nothing
291
292     size_up (Coerce _ _ body) = size_up body
293
294     size_up (Con con args) = -- 1 + # of val args
295                              sizeN (1 + numValArgs args)
296     size_up (Prim op args) = sizeN op_cost -- NB: no charge for PrimOp args
297       where
298         op_cost = if primOpCanTriggerGC op
299                   then uNFOLDING_DEAR_OP_COST
300                         -- these *tend* to be more expensive;
301                         -- number chosen to avoid unfolding (HACK)
302                   else uNFOLDING_CHEAP_OP_COST
303
304     size_up expr@(Lam _ _)
305       = let
306             (uvars, tyvars, args, body) = collectBinders expr
307         in
308         size_up body `addSizeN` length args
309
310     size_up (Let (NonRec binder rhs) body)
311       = size_up rhs
312                 `addSize`
313         size_up body
314                 `addSizeN`
315         1
316
317     size_up (Let (Rec pairs) body)
318       = foldr addSize sizeZero [size_up rhs | (_,rhs) <- pairs]
319                 `addSize`
320         size_up body
321                 `addSizeN`
322         length pairs
323
324     size_up (Case scrut alts)
325       = size_up_scrut scrut
326                 `addSize`
327         size_up_alts (coreExprType scrut) alts
328             -- We charge for the "case" itself in "size_up_alts"
329
330     ------------
331     size_up_arg arg = if isValArg arg then sizeOne else sizeZero{-it's free-}
332
333     ------------
334     size_up_alts scrut_ty (AlgAlts alts deflt)
335       = foldr (addSize . size_alg_alt) (size_up_deflt deflt) alts
336                 `addSizeN` (tyConFamilySize tycon)
337         -- NB: we charge N for an alg. "case", where N is
338         -- the number of constructors in the thing being eval'd.
339         -- (You'll eventually get a "discount" of N if you
340         -- think the "case" is likely to go away.)
341       where
342         size_alg_alt (con,args,rhs) = size_up rhs
343             -- Don't charge for args, so that wrappers look cheap
344
345         (tycon, _, _) = _trace "CoreUnfold.getAppDataTyConExpandingDicts" $ getAppDataTyConExpandingDicts scrut_ty
346
347     size_up_alts _ (PrimAlts alts deflt)
348       = foldr (addSize . size_prim_alt) (size_up_deflt deflt) alts
349             -- *no charge* for a primitive "case"!
350       where
351         size_prim_alt (lit,rhs) = size_up rhs
352
353     ------------
354     size_up_deflt NoDefault = sizeZero
355     size_up_deflt (BindDefault binder rhs) = size_up rhs
356
357     ------------
358         -- Scrutinees.  There are two things going on here.
359         -- First, we want to record if we're case'ing an argument
360         -- Second, we want to charge nothing for the srutinee if it's just
361         -- a variable.  That way wrapper-like things look cheap.
362     size_up_scrut (Var v) | v `is_elem` args = Just (0, [v])
363                             | otherwise        = Just (0, [])
364     size_up_scrut other                        = size_up other
365
366     is_elem :: Id -> [Id] -> Bool
367     is_elem = isIn "size_up_scrut"
368
369     ------------
370     sizeZero  = Just (0, [])
371     sizeOne   = Just (1, [])
372     sizeN n   = Just (n, [])
373     sizeVar v = Just (0, [v])
374
375     addSizeN Nothing _ = Nothing
376     addSizeN (Just (n, xs)) m
377       | tot < bOMB_OUT_SIZE = Just (tot, xs)
378       | otherwise = Nothing
379       where
380         tot = n+m
381
382     addSize Nothing _ = Nothing
383     addSize _ Nothing = Nothing
384     addSize (Just (n, xs)) (Just (m, ys))
385       | tot < bOMB_OUT_SIZE = Just (tot, xys)
386       | otherwise  = Nothing
387       where
388         tot = n+m
389         xys = xs ++ ys
390 \end{code}
391
392 %************************************************************************
393 %*                                                                      *
394 \subsection[unfoldings-for-ifaces]{Processing unfoldings for interfaces}
395 %*                                                                      *
396 %************************************************************************
397
398 Of course, the main thing we do to unfoldings-for-interfaces is {\em
399 print} them.  But, while we're at it, we collect info about
400 ``mentioned'' Ids, etc., etc.---we're going to need this stuff anyway.
401
402 %************************************************************************
403 %*                                                                      *
404 \subsubsection{Monad stuff for the unfolding-generation game}
405 %*                                                                      *
406 %************************************************************************
407
408 \begin{code}
409 type UnfoldM bndr thing
410         =  IdSet        -- in-scope Ids (passed downwards only)
411         -> (bndr -> Id) -- to extract an Id from a binder (down only)
412
413         -> (Bag Id,     -- mentioned global vars (ditto)
414             Bag TyCon,  -- ditto, tycons
415             Bag Class,  -- ditto, classes
416             Bool)       -- True <=> mentions something litlit-ish
417
418         -> (thing, (Bag Id, Bag TyCon, Bag Class, Bool)) -- accumulated...
419 \end{code}
420
421 A little stuff for in-scopery:
422 \begin{code}
423 no_in_scopes :: IdSet
424 add1         :: IdSet -> Id   -> IdSet
425 add_some     :: IdSet -> [Id] -> IdSet
426
427 no_in_scopes            = emptyUniqSet
428 in_scopes `add1`     x  = addOneToUniqSet in_scopes x
429 in_scopes `add_some` xs = in_scopes `unionUniqSets` mkUniqSet xs
430 \end{code}
431
432 The can-see-inside-monad functions are the usual sorts of things.
433
434 \begin{code}
435 thenUf :: UnfoldM bndr a -> (a -> UnfoldM bndr b) -> UnfoldM bndr b
436 thenUf m k in_scopes get_id mentioneds
437   = case m in_scopes get_id mentioneds of { (v, mentioneds1) ->
438     k v in_scopes get_id mentioneds1 }
439
440 thenUf_ :: UnfoldM bndr a -> UnfoldM bndr b -> UnfoldM bndr b
441 thenUf_ m k in_scopes get_id mentioneds
442   = case m in_scopes get_id mentioneds of { (_, mentioneds1) ->
443     k in_scopes get_id mentioneds1 }
444
445 mapUf :: (a -> UnfoldM bndr b) -> [a] -> UnfoldM bndr [b]
446 mapUf f []     = returnUf []
447 mapUf f (x:xs)
448   = f x         `thenUf` \ r ->
449     mapUf f xs  `thenUf` \ rs ->
450     returnUf (r:rs)
451
452 returnUf :: a -> UnfoldM bndr a
453 returnUf v in_scopes get_id mentioneds = (v, mentioneds)
454
455 addInScopesUf :: [Id] -> UnfoldM bndr a -> UnfoldM bndr a
456 addInScopesUf more_in_scopes m in_scopes get_id mentioneds
457   = m (in_scopes `add_some` more_in_scopes) get_id mentioneds
458
459 getInScopesUf :: UnfoldM bndr IdSet
460 getInScopesUf in_scopes get_id mentioneds = (in_scopes, mentioneds)
461
462 extractIdsUf :: [bndr] -> UnfoldM bndr [Id]
463 extractIdsUf binders in_scopes get_id mentioneds
464   = (map get_id binders, mentioneds)
465
466 consider_Id :: Id -> UnfoldM bndr ()
467 consider_Id var in_scopes get_id (ids, tcs, clss, has_litlit)
468   = let
469         (ids2, tcs2, clss2) = whatsMentionedInId in_scopes var
470     in
471     ((), (ids `unionBags` ids2,
472           tcs `unionBags` tcs2,
473           clss `unionBags`clss2,
474           has_litlit))
475 \end{code}
476
477 \begin{code}
478 addToMentionedIdsUf     :: Id -> UnfoldM bndr ()
479 addToMentionedTyConsUf  :: Bag TyCon -> UnfoldM bndr ()
480 addToMentionedClassesUf :: Bag Class -> UnfoldM bndr ()
481 litlit_oops             :: UnfoldM bndr ()
482
483 addToMentionedIdsUf add_me in_scopes get_id (ids, tcs, clss, has_litlit)
484   = ((), (ids `unionBags` unitBag add_me, tcs, clss, has_litlit))
485
486 addToMentionedTyConsUf add_mes in_scopes get_id (ids, tcs, clss, has_litlit)
487   = ((), (ids, tcs `unionBags` add_mes, clss, has_litlit))
488
489 addToMentionedClassesUf add_mes in_scopes get_id (ids, tcs, clss, has_litlit)
490   = ((), (ids, tcs, clss `unionBags` add_mes, has_litlit))
491
492 litlit_oops in_scopes get_id (ids, tcs, clss, _)
493   = ((), (ids, tcs, clss, True))
494 \end{code}
495
496
497 %************************************************************************
498 %*                                                                      *
499 \subsubsection{Gathering up info for an interface-unfolding}
500 %*                                                                      *
501 %************************************************************************
502
503 \begin{code}
504 {-
505 mentionedInUnfolding
506         :: (bndr -> Id)         -- so we can get Ids out of binders
507         -> GenCoreExpr bndr Id  -- input expression
508         -> (Bag Id, Bag TyCon, Bag Class,
509                                 -- what we found mentioned in the expr
510             Bool                -- True <=> mentions a ``litlit''-ish thing
511                                 -- (the guy on the other side of an interface
512                                 -- may not be able to handle it)
513            )
514 -}
515
516 mentionedInUnfolding get_id expr
517   = case (ment_expr expr no_in_scopes get_id (emptyBag, emptyBag, emptyBag, False)) of
518       (_, (ids_bag, tcs_bag, clss_bag, has_litlit)) ->
519         (ids_bag, tcs_bag, clss_bag, has_litlit)
520 \end{code}
521
522 \begin{code}
523 --ment_expr :: GenCoreExpr bndr Id -> UnfoldM bndr ()
524
525 ment_expr (Var v) = consider_Id  v
526 ment_expr (Lit l) = consider_lit l
527
528 ment_expr expr@(Lam _ _)
529   = let
530         (uvars, tyvars, args, body) = collectBinders expr
531     in
532     extractIdsUf args           `thenUf` \ bs_ids ->
533     addInScopesUf bs_ids (
534         -- this considering is just to extract any mentioned types/classes
535         mapUf consider_Id bs_ids   `thenUf_`
536         ment_expr body
537     )
538
539 ment_expr (App fun arg)
540   = ment_expr fun       `thenUf_`
541     ment_arg  arg
542
543 ment_expr (Con c as)
544   = consider_Id c       `thenUf_`
545     mapUf ment_arg as   `thenUf_`
546     returnUf ()
547
548 ment_expr (Prim op as)
549   = ment_op op          `thenUf_`
550     mapUf ment_arg as   `thenUf_`
551     returnUf ()
552   where
553     ment_op (CCallOp str is_asm may_gc arg_tys res_ty)
554       = mapUf ment_ty arg_tys   `thenUf_`
555         ment_ty res_ty
556     ment_op other_op = returnUf ()
557
558 ment_expr (Case scrutinee alts)
559   = ment_expr scrutinee `thenUf_`
560     ment_alts alts
561
562 ment_expr (Let (NonRec bind rhs) body)
563   = ment_expr rhs       `thenUf_`
564     extractIdsUf [bind] `thenUf` \ bi@[bind_id] ->
565     addInScopesUf bi    (
566     ment_expr body      `thenUf_`
567     consider_Id bind_id )
568
569 ment_expr (Let (Rec pairs) body)
570   = let
571         binders = map fst pairs
572         rhss    = map snd pairs
573     in
574     extractIdsUf binders        `thenUf` \ binder_ids ->
575     addInScopesUf binder_ids (
576         mapUf ment_expr rhss         `thenUf_`
577         mapUf consider_Id binder_ids `thenUf_`
578         ment_expr body )
579
580 ment_expr (SCC cc expr)
581   = (case (ccMentionsId cc) of
582       Just id -> consider_Id id
583       Nothing -> returnUf ()
584     )
585     `thenUf_` ment_expr expr
586
587 ment_expr (Coerce _ _ _) = panic "ment_expr:Coerce"
588
589 -------------
590 ment_ty ty
591   = let
592         (tycons, clss) = getMentionedTyConsAndClassesFromType ty
593     in
594     addToMentionedTyConsUf  tycons  `thenUf_`
595     addToMentionedClassesUf clss
596
597 -------------
598
599 ment_alts alg_alts@(AlgAlts alts deflt)
600   = mapUf ment_alt alts   `thenUf_`
601     ment_deflt deflt
602   where
603     ment_alt alt@(con, params, rhs)
604       = consider_Id con         `thenUf_`
605         extractIdsUf params     `thenUf` \ param_ids ->
606         addInScopesUf param_ids (
607           -- "consider" them so we can chk out their types...
608           mapUf consider_Id param_ids `thenUf_`
609           ment_expr rhs )
610
611 ment_alts (PrimAlts alts deflt)
612   = mapUf ment_alt alts   `thenUf_`
613     ment_deflt deflt
614   where
615     ment_alt alt@(lit, rhs) = ment_expr rhs
616
617 ----------------
618 ment_deflt NoDefault
619   = returnUf ()
620
621 ment_deflt d@(BindDefault b rhs)
622   = extractIdsUf [b]            `thenUf` \ bi@[b_id] ->
623     addInScopesUf bi            (
624         consider_Id b_id `thenUf_`
625         ment_expr rhs )
626
627 -----------
628 ment_arg (VarArg   v)  = consider_Id  v
629 ment_arg (LitArg   l)  = consider_lit l
630 ment_arg (TyArg    ty) = ment_ty ty
631 ment_arg (UsageArg _)  = returnUf ()
632
633 -----------
634 consider_lit lit
635   | isLitLitLit lit = litlit_oops `thenUf_` returnUf ()
636   | otherwise       = returnUf ()
637 \end{code}
638
639 %************************************************************************
640 %*                                                                      *
641 \subsubsection{Printing unfoldings in interfaces}
642 %*                                                                      *
643 %************************************************************************
644
645 Printing Core-expression unfoldings is sufficiently delicate that we
646 give it its own function.
647 \begin{code}
648 {- OLD:
649 pprCoreUnfolding
650         :: CoreExpr
651         -> Pretty
652
653 pprCoreUnfolding expr
654   = let
655         (_, renamed) = instCoreExpr uniqSupply_u expr
656             -- We rename every unfolding with a "steady" unique supply,
657             -- so that the names won't constantly change.
658             -- One place we *MUST NOT* use a splittable UniqueSupply!
659     in
660     ppr_uf_Expr emptyUniqSet renamed
661
662 ppr_Unfolding = PprUnfolding (panic "CoreUnfold:ppr_Unfolding")
663 \end{code}
664
665 \begin{code}
666 ppr_uf_Expr in_scopes (Var v) = pprIdInUnfolding in_scopes v
667 ppr_uf_Expr in_scopes (Lit l) = ppr ppr_Unfolding l
668
669 ppr_uf_Expr in_scopes (Con c as)
670   = ppBesides [ppPStr SLIT("_!_ "), pprIdInUnfolding no_in_scopes c, ppSP,
671            ppLbrack, ppIntersperse pp'SP{-'-} (map (pprParendUniType ppr_Unfolding) ts), ppRbrack,
672            ppSP, ppLbrack, ppIntersperse pp'SP{-'-} (map (ppr_uf_Atom in_scopes) as), ppRbrack]
673 ppr_uf_Expr in_scopes (Prim op as)
674   = ppBesides [ppPStr SLIT("_#_ "), ppr ppr_Unfolding op, ppSP,
675            ppLbrack, ppIntersperse pp'SP{-'-} (map (pprParendUniType ppr_Unfolding) ts), ppRbrack,
676            ppSP, ppLbrack, ppIntersperse pp'SP{-'-} (map (ppr_uf_Atom in_scopes) as), ppRbrack]
677
678 ppr_uf_Expr in_scopes (Lam binder body)
679   = ppCat [ppChar '\\', ppr_uf_Binder binder,
680            ppPStr SLIT("->"), ppr_uf_Expr (in_scopes `add1` binder) body]
681
682 ppr_uf_Expr in_scopes (CoTyLam tyvar expr)
683   = ppCat [ppPStr SLIT("_/\\_"), interppSP ppr_Unfolding (tyvar:tyvars), ppStr "->",
684            ppr_uf_Expr in_scopes body]
685   where
686     (tyvars, body) = collect_tyvars expr
687
688     collect_tyvars (CoTyLam tyv e) = ( tyv:tyvs, e_after )
689       where (tyvs, e_after) = collect_tyvars e
690     collect_tyvars other_e         = ( [], other_e )
691
692 ppr_uf_Expr in_scopes expr@(App fun_expr atom)
693   = let
694         (fun, args) = collect_args expr []
695     in
696     ppCat [ppPStr SLIT("_APP_ "), ppr_uf_Expr in_scopes fun, ppLbrack,
697            ppIntersperse pp'SP{-'-} (map (ppr_uf_Atom in_scopes) args), ppRbrack]
698   where
699     collect_args (App fun arg) args = collect_args fun (arg:args)
700     collect_args fun             args = (fun, args)
701
702 ppr_uf_Expr in_scopes (CoTyApp expr ty)
703   = ppCat [ppPStr SLIT("_TYAPP_ "), ppr_uf_Expr in_scopes expr,
704         ppChar '{', pprParendUniType ppr_Unfolding ty, ppChar '}']
705
706 ppr_uf_Expr in_scopes (Case scrutinee alts)
707   = ppCat [ppPStr SLIT("case"), ppr_uf_Expr in_scopes scrutinee, ppStr "of {",
708            pp_alts alts, ppChar '}']
709   where
710     pp_alts (AlgAlts  alts deflt)
711       = ppCat [ppPStr SLIT("_ALG_"),  ppCat (map pp_alg  alts), pp_deflt deflt]
712     pp_alts (PrimAlts alts deflt)
713       = ppCat [ppPStr SLIT("_PRIM_"), ppCat (map pp_prim alts), pp_deflt deflt]
714
715     pp_alg (con, params, rhs)
716       = ppBesides [pprIdInUnfolding no_in_scopes con, ppSP,
717                    ppIntersperse ppSP (map ppr_uf_Binder params),
718                    ppPStr SLIT(" -> "), ppr_uf_Expr (in_scopes `add_some` params) rhs, ppSemi]
719
720     pp_prim (lit, rhs)
721       = ppBesides [ppr ppr_Unfolding lit,
722                    ppPStr SLIT(" -> "), ppr_uf_Expr in_scopes rhs, ppSemi]
723
724     pp_deflt NoDefault = ppPStr SLIT("_NO_DEFLT_")
725     pp_deflt (BindDefault binder rhs)
726       = ppBesides [ppr_uf_Binder binder, ppPStr SLIT(" -> "),
727                    ppr_uf_Expr (in_scopes `add1` binder) rhs]
728
729 ppr_uf_Expr in_scopes (Let (NonRec binder rhs) body)
730   = ppBesides [ppStr "let {", ppr_uf_Binder binder, ppPStr SLIT(" = "), ppr_uf_Expr in_scopes rhs,
731         ppStr "} in ", ppr_uf_Expr (in_scopes `add1` binder) body]
732
733 ppr_uf_Expr in_scopes (Let (Rec pairs) body)
734   = ppBesides [ppStr "_LETREC_ {", ppIntersperse sep (map pp_pair pairs),
735         ppStr "} in ", ppr_uf_Expr new_in_scopes body]
736   where
737     sep = ppBeside ppSemi ppSP
738     new_in_scopes = in_scopes `add_some` map fst pairs
739
740     pp_pair (b, rhs) = ppCat [ppr_uf_Binder b, ppEquals, ppr_uf_Expr new_in_scopes rhs]
741
742 ppr_uf_Expr in_scopes (SCC cc body)
743   = ASSERT(not (noCostCentreAttached cc))
744     ASSERT(not (currentOrSubsumedCosts cc))
745     ppBesides [ppStr "_scc_ { ", ppStr (showCostCentre ppr_Unfolding False{-not as string-} cc), ppStr " } ",  ppr_uf_Expr in_scopes body]
746
747 ppr_uf_Expr in_scopes (Coerce _ _ _) = panic "ppr_uf_Expr:Coerce"
748 \end{code}
749
750 \begin{code}
751 ppr_uf_Binder :: Id -> Pretty
752 ppr_uf_Binder v
753   = ppBesides [ppLparen, pprIdInUnfolding (unitUniqSet v) v, ppPStr SLIT(" :: "),
754                ppr ppr_Unfolding (idType v), ppRparen]
755
756 ppr_uf_Atom in_scopes (LitArg l) = ppr ppr_Unfolding l
757 ppr_uf_Atom in_scopes (VarArg v) = pprIdInUnfolding in_scopes v
758 END OLD -}
759 \end{code}