Fix scoped type variables for expression type signatures
[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               sep [ptext SLIT("of") <+> ppr_bndr var, 
169                    char '{' <+> ppr_case_pat con args]
170           ],
171          pprCoreExpr rhs,
172          char '}'
173     ]
174   where
175     ppr_bndr = pprBndr CaseBind
176
177 ppr_expr add_par (Case expr var ty alts)
178   = add_par $
179     sep [sep [ptext SLIT("case")
180                 <+> pprCoreExpr expr
181                 <+> ifPprDebug (braces (ppr ty)),
182               ptext SLIT("of") <+> ppr_bndr var <+> char '{'],
183          nest 2 (sep (punctuate semi (map pprCoreAlt alts))),
184          char '}'
185     ]
186   where
187     ppr_bndr = pprBndr CaseBind
188  
189
190 -- special cases: let ... in let ...
191 -- ("disgusting" SLPJ)
192
193 {-
194 ppr_expr add_par (Let bind@(NonRec val_bdr rhs@(Let _ _)) body)
195   = add_par $
196     vcat [
197       hsep [ptext SLIT("let {"), (pprBndr LetBind val_bdr $$ ppr val_bndr), equals],
198       nest 2 (pprCoreExpr rhs),
199       ptext SLIT("} in"),
200       pprCoreExpr body ]
201 -}
202
203 ppr_expr add_par (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
204   = add_par
205     (hang (ptext SLIT("let {"))
206           2 (hsep [ppr_binding (val_bdr,rhs),
207                    ptext SLIT("} in")])
208      $$
209      pprCoreExpr expr)
210
211 -- general case (recursive case, too)
212 ppr_expr add_par (Let bind expr)
213   = add_par $
214     sep [hang (ptext keyword) 2 (ppr_bind bind),
215          hang (ptext SLIT("} in ")) 2 (pprCoreExpr expr)]
216   where
217     keyword = case bind of
218                 Rec _      -> SLIT("__letrec {")
219                 NonRec _ _ -> SLIT("let {")
220
221 ppr_expr add_par (Note (SCC cc) expr)
222   = add_par (sep [pprCostCentreCore cc, pprCoreExpr expr])
223
224 ppr_expr add_par (Note InlineMe expr)
225   = add_par $ ptext SLIT("__inline_me") <+> pprParendExpr expr
226
227 ppr_expr add_par (Note (CoreNote s) expr)
228   = add_par $ 
229     sep [sep [ptext SLIT("__core_note"), pprHsString (mkFastString s)],
230          pprParendExpr expr]
231
232 pprCoreAlt (con, args, rhs) 
233   = hang (ppr_case_pat con args) 2 (pprCoreExpr rhs)
234
235 ppr_case_pat con@(DataAlt dc) args
236   | isTupleTyCon tc
237   = tupleParens (tupleTyConBoxity tc) (hsep (punctuate comma (map ppr_bndr args))) <+> arrow
238   where
239     ppr_bndr = pprBndr CaseBind
240     tc = dataConTyCon dc
241
242 ppr_case_pat con args
243   = ppr con <+> sep (map ppr_bndr args) <+> arrow
244   where
245     ppr_bndr = pprBndr CaseBind
246
247 pprArg (Type ty) = ptext SLIT("@") <+> pprParendType ty
248 pprArg expr      = pprParendExpr expr
249 \end{code}
250
251 Other printing bits-and-bobs used with the general @pprCoreBinding@
252 and @pprCoreExpr@ functions.
253
254 \begin{code}
255 instance OutputableBndr Var where
256   pprBndr = pprCoreBinder
257
258 pprCoreBinder :: BindingSite -> Var -> SDoc
259 pprCoreBinder LetBind binder
260   = vcat [sig, pprIdDetails binder, pragmas]
261   where
262     sig     = pprTypedBinder binder
263     pragmas = ppIdInfo binder (idInfo binder)
264
265 -- Lambda bound type variables are preceded by "@"
266 pprCoreBinder LambdaBind bndr = parens (pprTypedBinder bndr)
267
268 -- Case bound things don't get a signature or a herald, unless we have debug on
269 pprCoreBinder CaseBind bndr 
270   = getPprStyle $ \ sty ->
271     if debugStyle sty then
272         parens (pprTypedBinder bndr)
273     else
274         pprUntypedBinder bndr
275
276 pprUntypedBinder binder
277   | isTyVar binder = ptext SLIT("@") <+> ppr binder     -- NB: don't print kind
278   | otherwise      = pprIdBndr binder
279
280 pprTypedBinder binder
281   | isTyVar binder  = ptext SLIT("@") <+> pprTyVarBndr binder
282   | otherwise       = pprIdBndr binder <+> dcolon <+> pprType (idType binder)
283
284 pprTyVarBndr :: TyVar -> SDoc
285 pprTyVarBndr tyvar
286   = getPprStyle $ \ sty ->
287     if debugStyle sty then
288         hsep [ppr tyvar, dcolon, pprParendKind kind]
289                 -- See comments with ppDcolon in PprCore.lhs
290     else
291         ppr tyvar
292   where
293     kind = tyVarKind tyvar
294
295 -- pprIdBndr does *not* print the type
296 -- When printing any Id binder in debug mode, we print its inline pragma and one-shot-ness
297 pprIdBndr id = ppr id <+> pprIdBndrInfo (idInfo id)
298
299 pprIdBndrInfo info 
300   = megaSeqIdInfo `seq` doc -- The seq is useful for poking on black holes
301   where
302     prag_info = inlinePragInfo info
303     occ_info  = occInfo info
304     dmd_info  = newDemandInfo info
305     lbv_info  = lbvarInfo info
306
307     no_info = isAlwaysActive prag_info && isNoOcc occ_info && 
308               (case dmd_info of { Nothing -> True; Just d -> isTop d }) &&
309               hasNoLBVarInfo lbv_info
310
311     doc | no_info = empty
312         | otherwise
313         = brackets $ hsep [ppr prag_info, ppr occ_info, 
314                            ppr dmd_info, ppr lbv_info
315 #ifdef OLD_STRICTNESS
316                            , ppr (demandInfo id)
317 #endif
318                           ]
319 \end{code}
320
321
322 \begin{code}
323 pprIdDetails :: Id -> SDoc
324 pprIdDetails id | isGlobalId id     = ppr (globalIdDetails id)
325                 | isExportedId id   = ptext SLIT("[Exported]")
326                 | otherwise         = empty
327
328 ppIdInfo :: Id -> IdInfo -> SDoc
329 ppIdInfo b info
330   = brackets $
331     vcat [  ppArityInfo a,
332             ppWorkerInfo (workerInfo info),
333             ppCafInfo (cafInfo info),
334 #ifdef OLD_STRICTNESS
335             ppStrictnessInfo s,
336             ppCprInfo m,
337 #endif
338             pprNewStrictness (newStrictnessInfo info),
339             if null rules then empty
340             else ptext SLIT("RULES:") <+> vcat (map pprRule rules)
341         -- Inline pragma, occ, demand, lbvar info
342         -- printed out with all binders (when debug is on); 
343         -- see PprCore.pprIdBndr
344         ]
345   where
346     a = arityInfo info
347 #ifdef OLD_STRICTNESS
348     s = strictnessInfo info
349     m = cprInfo info
350 #endif
351     rules = specInfoRules (specInfo info)
352 \end{code}
353
354
355 \begin{code}
356 instance Outputable CoreRule where
357    ppr = pprRule
358
359 pprRules :: [CoreRule] -> SDoc
360 pprRules rules = vcat (map pprRule rules)
361
362 pprRule :: CoreRule -> SDoc
363 pprRule (BuiltinRule { ru_fn = fn, ru_name = name})
364   = ptext SLIT("Built in rule for") <+> ppr fn <> colon <+> doubleQuotes (ftext name)
365
366 pprRule (Rule { ru_name = name, ru_act = act, ru_fn = fn,
367                 ru_bndrs = tpl_vars, ru_args = tpl_args,
368                 ru_rhs = rhs })
369   = doubleQuotes (ftext name) <+> ppr act <+>
370     sep [
371           ptext SLIT("__forall") <+> braces (sep (map pprTypedBinder tpl_vars)),
372           nest 2 (ppr fn <+> sep (map pprArg tpl_args)),
373           nest 2 (ptext SLIT("=") <+> pprCoreExpr rhs)
374     ] <+> semi
375 \end{code}