clean up Coercion kinding functions, rename coercionKindTyConApp
[ghc-hetmet.git] / compiler / coreSyn / PprCore.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1996-1998
3 %
4 %************************************************************************
5 %*                                                                      *
6 \section[PprCore]{Printing of Core syntax, including for interfaces}
7 %*                                                                      *
8 %************************************************************************
9
10 \begin{code}
11 module PprCore (
12         pprCoreExpr, pprParendExpr,
13         pprCoreBinding, pprCoreBindings, pprCoreAlt,
14         pprRules
15     ) where
16
17 #include "HsVersions.h"
18
19 import CoreSyn
20 import CostCentre       ( pprCostCentreCore )
21 import Var              ( Var )
22 import Id               ( Id, idType, isDataConWorkId_maybe, idArity,
23                           idInfo, globalIdDetails, isGlobalId, isExportedId 
24                         )
25 import Var              ( TyVar, isTyVar, tyVarKind )
26 import IdInfo           ( IdInfo, megaSeqIdInfo, 
27                           inlinePragInfo, occInfo, newDemandInfo, 
28                           lbvarInfo, hasNoLBVarInfo,
29                           arityInfo, ppArityInfo, 
30                           specInfo, pprNewStrictness,
31                           workerInfo, ppWorkerInfo,
32                           newStrictnessInfo, cafInfo, ppCafInfo, specInfoRules
33                         )
34 import NewDemand        ( isTop )
35 #ifdef OLD_STRICTNESS
36 import Id               ( idDemandInfo )
37 import IdInfo           ( cprInfo, ppCprInfo, strictnessInfo, ppStrictnessInfo ) 
38 #endif
39
40 import DataCon          ( dataConTyCon )
41 import TyCon            ( tupleTyConBoxity, isTupleTyCon )
42 import Type             ( pprParendType, pprType, pprParendKind )
43 import Coercion         ( coercionKindPredTy )
44 import BasicTypes       ( tupleParens, isNoOcc, isAlwaysActive )
45 import Util             ( lengthIs )
46 import Outputable
47 import FastString       ( mkFastString )
48 \end{code}
49
50 %************************************************************************
51 %*                                                                      *
52 \subsection{Public interfaces for Core printing (excluding instances)}
53 %*                                                                      *
54 %************************************************************************
55
56 @pprParendCoreExpr@ puts parens around non-atomic Core expressions.
57
58 \begin{code}
59 pprCoreBindings :: OutputableBndr b => [Bind b] -> SDoc
60 pprCoreBinding  :: OutputableBndr b => Bind b  -> SDoc
61 pprCoreExpr     :: OutputableBndr b => Expr b  -> SDoc
62 pprParendExpr   :: OutputableBndr b => Expr b  -> SDoc
63
64 pprCoreBindings = pprTopBinds
65 pprCoreBinding  = pprTopBind 
66
67 instance OutputableBndr b => Outputable (Bind b) where
68     ppr bind = ppr_bind bind
69
70 instance OutputableBndr b => Outputable (Expr b) where
71     ppr expr = pprCoreExpr expr
72 \end{code}
73
74
75 %************************************************************************
76 %*                                                                      *
77 \subsection{The guts}
78 %*                                                                      *
79 %************************************************************************
80
81 \begin{code}
82 pprTopBinds binds = vcat (map pprTopBind binds)
83
84 pprTopBind (NonRec binder expr)
85  = ppr_binding (binder,expr) $$ text ""
86
87 pprTopBind (Rec binds)
88   = vcat [ptext SLIT("Rec {"),
89           vcat (map ppr_binding binds),
90           ptext SLIT("end Rec }"),
91           text ""]
92 \end{code}
93
94 \begin{code}
95 ppr_bind :: OutputableBndr b => Bind b -> SDoc
96
97 ppr_bind (NonRec val_bdr expr) = ppr_binding (val_bdr, expr)
98 ppr_bind (Rec binds)           = vcat (map pp binds)
99                                where
100                                  pp bind = ppr_binding bind <> semi
101
102 ppr_binding :: OutputableBndr b => (b, Expr b) -> SDoc
103 ppr_binding (val_bdr, expr)
104   = pprBndr LetBind val_bdr $$ 
105     hang (ppr val_bdr <+> equals) 2 (pprCoreExpr expr)
106 \end{code}
107
108 \begin{code}
109 pprParendExpr   expr = ppr_expr parens expr
110 pprCoreExpr expr = ppr_expr noParens expr
111
112 noParens :: SDoc -> SDoc
113 noParens pp = pp
114 \end{code}
115
116 \begin{code}
117 ppr_expr :: OutputableBndr b => (SDoc -> SDoc) -> Expr b -> SDoc
118         -- The function adds parens in context that need
119         -- an atomic value (e.g. function args)
120
121 ppr_expr add_par (Type ty)  = add_par (ptext SLIT("TYPE") <+> ppr ty)   -- Wierd
122                    
123 ppr_expr add_par (Var name) = ppr name
124 ppr_expr add_par (Lit lit)  = ppr lit
125
126 ppr_expr add_par (Cast expr co) 
127   = add_par $
128     sep [pprParendExpr expr, 
129          ptext SLIT("`cast`") <+> parens (pprCo co)]
130   where
131     pprCo co = sep [ppr co, dcolon <+> ppr (coercionKindPredTy co)]
132          
133
134 ppr_expr add_par expr@(Lam _ _)
135   = let
136         (bndrs, body) = collectBinders expr
137     in
138     add_par $
139     hang (ptext SLIT("\\") <+> sep (map (pprBndr LambdaBind) bndrs) <+> arrow)
140          2 (pprCoreExpr body)
141
142 ppr_expr add_par expr@(App fun arg)
143   = case collectArgs expr of { (fun, args) -> 
144     let
145         pp_args     = sep (map pprArg args)
146         val_args    = dropWhile isTypeArg args   -- Drop the type arguments for tuples
147         pp_tup_args = sep (punctuate comma (map pprArg val_args))
148     in
149     case fun of
150         Var f -> case isDataConWorkId_maybe f of
151                         -- Notice that we print the *worker*
152                         -- for tuples in paren'd format.
153                    Just dc | saturated && isTupleTyCon tc
154                            -> tupleParens (tupleTyConBoxity tc) pp_tup_args
155                            where
156                              tc        = dataConTyCon dc
157                              saturated = val_args `lengthIs` idArity f
158
159                    other -> add_par (hang (ppr f) 2 pp_args)
160
161         other -> add_par (hang (pprParendExpr fun) 2 pp_args)
162     }
163
164 ppr_expr add_par (Case expr var ty [(con,args,rhs)])
165   = add_par $
166     sep [sep [ptext SLIT("case") <+> pprCoreExpr expr,
167               ifPprDebug (braces (ppr ty)),
168               hsep [ptext SLIT("of"),
169                     ppr_bndr var, 
170                     char '{',
171                     ppr_case_pat con args
172           ]],
173          pprCoreExpr rhs,
174          char '}'
175     ]
176   where
177     ppr_bndr = pprBndr CaseBind
178
179 ppr_expr add_par (Case expr var ty alts)
180   = add_par $
181     sep [sep [ptext SLIT("case")
182                 <+> pprCoreExpr expr
183                 <+> ifPprDebug (braces (ppr ty)),
184               ptext SLIT("of") <+> ppr_bndr var <+> char '{'],
185          nest 2 (sep (punctuate semi (map pprCoreAlt alts))),
186          char '}'
187     ]
188   where
189     ppr_bndr = pprBndr CaseBind
190  
191
192 -- special cases: let ... in let ...
193 -- ("disgusting" SLPJ)
194
195 {-
196 ppr_expr add_par (Let bind@(NonRec val_bdr rhs@(Let _ _)) body)
197   = add_par $
198     vcat [
199       hsep [ptext SLIT("let {"), (pprBndr LetBind val_bdr $$ ppr val_bndr), equals],
200       nest 2 (pprCoreExpr rhs),
201       ptext SLIT("} in"),
202       pprCoreExpr body ]
203 -}
204
205 ppr_expr add_par (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
206   = add_par
207     (hang (ptext SLIT("let {"))
208           2 (hsep [ppr_binding (val_bdr,rhs),
209                    ptext SLIT("} in")])
210      $$
211      pprCoreExpr expr)
212
213 -- general case (recursive case, too)
214 ppr_expr add_par (Let bind expr)
215   = add_par $
216     sep [hang (ptext keyword) 2 (ppr_bind bind),
217          hang (ptext SLIT("} in ")) 2 (pprCoreExpr expr)]
218   where
219     keyword = case bind of
220                 Rec _      -> SLIT("__letrec {")
221                 NonRec _ _ -> SLIT("let {")
222
223 ppr_expr add_par (Note (SCC cc) expr)
224   = add_par (sep [pprCostCentreCore cc, pprCoreExpr expr])
225
226 ppr_expr add_par (Note InlineMe expr)
227   = add_par $ ptext SLIT("__inline_me") <+> pprParendExpr expr
228
229 ppr_expr add_par (Note (CoreNote s) expr)
230   = add_par $ 
231     sep [sep [ptext SLIT("__core_note"), pprHsString (mkFastString s)],
232          pprParendExpr expr]
233
234 pprCoreAlt (con, args, rhs) 
235   = hang (ppr_case_pat con args) 2 (pprCoreExpr rhs)
236
237 ppr_case_pat con@(DataAlt dc) args
238   | isTupleTyCon tc
239   = tupleParens (tupleTyConBoxity tc) (hsep (punctuate comma (map ppr_bndr args))) <+> arrow
240   where
241     ppr_bndr = pprBndr CaseBind
242     tc = dataConTyCon dc
243
244 ppr_case_pat con args
245   = ppr con <+> hsep (map ppr_bndr args) <+> arrow
246   where
247     ppr_bndr = pprBndr CaseBind
248
249 pprArg (Type ty) = ptext SLIT("@") <+> pprParendType ty
250 pprArg expr      = pprParendExpr expr
251 \end{code}
252
253 Other printing bits-and-bobs used with the general @pprCoreBinding@
254 and @pprCoreExpr@ functions.
255
256 \begin{code}
257 instance OutputableBndr Var where
258   pprBndr = pprCoreBinder
259
260 pprCoreBinder :: BindingSite -> Var -> SDoc
261 pprCoreBinder LetBind binder
262   = vcat [sig, pprIdDetails binder, pragmas]
263   where
264     sig     = pprTypedBinder binder
265     pragmas = ppIdInfo binder (idInfo binder)
266
267 -- Lambda bound type variables are preceded by "@"
268 pprCoreBinder LambdaBind bndr = parens (pprTypedBinder bndr)
269
270 -- Case bound things don't get a signature or a herald, unless we have debug on
271 pprCoreBinder CaseBind bndr 
272   = getPprStyle $ \ sty ->
273     if debugStyle sty then
274         parens (pprTypedBinder bndr)
275     else
276         pprUntypedBinder bndr
277
278 pprUntypedBinder binder
279   | isTyVar binder = ptext SLIT("@") <+> ppr binder     -- NB: don't print kind
280   | otherwise      = pprIdBndr binder
281
282 pprTypedBinder binder
283   | isTyVar binder  = ptext SLIT("@") <+> pprTyVarBndr binder
284   | otherwise       = pprIdBndr binder <+> dcolon <+> pprType (idType binder)
285
286 pprTyVarBndr :: TyVar -> SDoc
287 pprTyVarBndr tyvar
288   = getPprStyle $ \ sty ->
289     if debugStyle sty then
290         hsep [ppr tyvar, dcolon, pprParendKind kind]
291                 -- See comments with ppDcolon in PprCore.lhs
292     else
293         ppr tyvar
294   where
295     kind = tyVarKind tyvar
296
297 -- pprIdBndr does *not* print the type
298 -- When printing any Id binder in debug mode, we print its inline pragma and one-shot-ness
299 pprIdBndr id = ppr id <+> pprIdBndrInfo (idInfo id)
300
301 pprIdBndrInfo info 
302   = megaSeqIdInfo `seq` doc -- The seq is useful for poking on black holes
303   where
304     prag_info = inlinePragInfo info
305     occ_info  = occInfo info
306     dmd_info  = newDemandInfo info
307     lbv_info  = lbvarInfo info
308
309     no_info = isAlwaysActive prag_info && isNoOcc occ_info && 
310               (case dmd_info of { Nothing -> True; Just d -> isTop d }) &&
311               hasNoLBVarInfo lbv_info
312
313     doc | no_info = empty
314         | otherwise
315         = brackets $ hsep [ppr prag_info, ppr occ_info, 
316                            ppr dmd_info, ppr lbv_info
317 #ifdef OLD_STRICTNESS
318                            , ppr (demandInfo id)
319 #endif
320                           ]
321 \end{code}
322
323
324 \begin{code}
325 pprIdDetails :: Id -> SDoc
326 pprIdDetails id | isGlobalId id     = ppr (globalIdDetails id)
327                 | isExportedId id   = ptext SLIT("[Exported]")
328                 | otherwise         = empty
329
330 ppIdInfo :: Id -> IdInfo -> SDoc
331 ppIdInfo b info
332   = brackets $
333     vcat [  ppArityInfo a,
334             ppWorkerInfo (workerInfo info),
335             ppCafInfo (cafInfo info),
336 #ifdef OLD_STRICTNESS
337             ppStrictnessInfo s,
338             ppCprInfo m,
339 #endif
340             pprNewStrictness (newStrictnessInfo info),
341             if null rules then empty
342             else ptext SLIT("RULES:") <+> vcat (map pprRule rules)
343         -- Inline pragma, occ, demand, lbvar info
344         -- printed out with all binders (when debug is on); 
345         -- see PprCore.pprIdBndr
346         ]
347   where
348     a = arityInfo info
349 #ifdef OLD_STRICTNESS
350     s = strictnessInfo info
351     m = cprInfo info
352 #endif
353     rules = specInfoRules (specInfo info)
354 \end{code}
355
356
357 \begin{code}
358 instance Outputable CoreRule where
359    ppr = pprRule
360
361 pprRules :: [CoreRule] -> SDoc
362 pprRules rules = vcat (map pprRule rules)
363
364 pprRule :: CoreRule -> SDoc
365 pprRule (BuiltinRule { ru_fn = fn, ru_name = name})
366   = ptext SLIT("Built in rule for") <+> ppr fn <> colon <+> doubleQuotes (ftext name)
367
368 pprRule (Rule { ru_name = name, ru_act = act, ru_fn = fn,
369                 ru_bndrs = tpl_vars, ru_args = tpl_args,
370                 ru_rhs = rhs })
371   = doubleQuotes (ftext name) <+> ppr act <+>
372     sep [
373           ptext SLIT("__forall") <+> braces (sep (map pprTypedBinder tpl_vars)),
374           nest 2 (ppr fn <+> sep (map pprArg tpl_args)),
375           nest 2 (ptext SLIT("=") <+> pprCoreExpr rhs)
376     ] <+> semi
377 \end{code}