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