2 % (c) The University of Glasgow 2006
3 % (c) The AQUA Project, Glasgow University, 1996-1998
6 Printing of Core syntax
10 pprCoreExpr, pprParendExpr,
11 pprCoreBinding, pprCoreBindings, pprCoreAlt,
36 %************************************************************************
38 \subsection{Public interfaces for Core printing (excluding instances)}
40 %************************************************************************
42 @pprParendCoreExpr@ puts parens around non-atomic Core expressions.
45 pprCoreBindings :: OutputableBndr b => [Bind b] -> SDoc
46 pprCoreBinding :: OutputableBndr b => Bind b -> SDoc
47 pprCoreExpr :: OutputableBndr b => Expr b -> SDoc
48 pprParendExpr :: OutputableBndr b => Expr b -> SDoc
50 pprCoreBindings = pprTopBinds
51 pprCoreBinding = pprTopBind
53 instance OutputableBndr b => Outputable (Bind b) where
54 ppr bind = ppr_bind bind
56 instance OutputableBndr b => Outputable (Expr b) where
57 ppr expr = pprCoreExpr expr
61 %************************************************************************
65 %************************************************************************
68 pprTopBinds :: OutputableBndr a => [Bind a] -> SDoc
69 pprTopBinds binds = vcat (map pprTopBind binds)
71 pprTopBind :: OutputableBndr a => Bind a -> SDoc
72 pprTopBind (NonRec binder expr)
73 = ppr_binding (binder,expr) $$ text ""
75 pprTopBind (Rec binds)
76 = vcat [ptext (sLit "Rec {"),
77 vcat (map ppr_binding binds),
78 ptext (sLit "end Rec }"),
83 ppr_bind :: OutputableBndr b => Bind b -> SDoc
85 ppr_bind (NonRec val_bdr expr) = ppr_binding (val_bdr, expr)
86 ppr_bind (Rec binds) = vcat (map pp binds)
88 pp bind = ppr_binding bind <> semi
90 ppr_binding :: OutputableBndr b => (b, Expr b) -> SDoc
91 ppr_binding (val_bdr, expr)
92 = pprBndr LetBind val_bdr $$
93 hang (ppr val_bdr <+> equals) 2 (pprCoreExpr expr)
97 pprParendExpr expr = ppr_expr parens expr
98 pprCoreExpr expr = ppr_expr noParens expr
100 noParens :: SDoc -> SDoc
105 ppr_expr :: OutputableBndr b => (SDoc -> SDoc) -> Expr b -> SDoc
106 -- The function adds parens in context that need
107 -- an atomic value (e.g. function args)
109 ppr_expr add_par (Type ty) = add_par (ptext (sLit "TYPE") <+> ppr ty) -- Wierd
111 ppr_expr _ (Var name) = ppr name
112 ppr_expr _ (Lit lit) = ppr lit
114 ppr_expr add_par (Cast expr co)
116 sep [pprParendExpr expr,
117 ptext (sLit "`cast`") <+> parens (pprCo co)]
119 pprCo co = sep [ppr co, dcolon <+> ppr (coercionKindPredTy co)]
122 ppr_expr add_par expr@(Lam _ _)
124 (bndrs, body) = collectBinders expr
127 hang (ptext (sLit "\\") <+> sep (map (pprBndr LambdaBind) bndrs) <+> arrow)
130 ppr_expr add_par expr@(App {})
131 = case collectArgs expr of { (fun, args) ->
133 pp_args = sep (map pprArg args)
134 val_args = dropWhile isTypeArg args -- Drop the type arguments for tuples
135 pp_tup_args = sep (punctuate comma (map pprCoreExpr val_args))
138 Var f -> case isDataConWorkId_maybe f of
139 -- Notice that we print the *worker*
140 -- for tuples in paren'd format.
141 Just dc | saturated && isTupleTyCon tc
142 -> tupleParens (tupleTyConBoxity tc) pp_tup_args
145 saturated = val_args `lengthIs` idArity f
147 _ -> add_par (hang (ppr f) 2 pp_args)
149 _ -> add_par (hang (pprParendExpr fun) 2 pp_args)
152 ppr_expr add_par (Case expr var ty [(con,args,rhs)])
154 sep [sep [ptext (sLit "case") <+> pprCoreExpr expr,
155 ifPprDebug (braces (ppr ty)),
156 sep [ptext (sLit "of") <+> ppr_bndr var,
157 char '{' <+> ppr_case_pat con args]
163 ppr_bndr = pprBndr CaseBind
165 ppr_expr add_par (Case expr var ty alts)
167 sep [sep [ptext (sLit "case")
169 <+> ifPprDebug (braces (ppr ty)),
170 ptext (sLit "of") <+> ppr_bndr var <+> char '{'],
171 nest 2 (sep (punctuate semi (map pprCoreAlt alts))),
175 ppr_bndr = pprBndr CaseBind
178 -- special cases: let ... in let ...
179 -- ("disgusting" SLPJ)
182 ppr_expr add_par (Let bind@(NonRec val_bdr rhs@(Let _ _)) body)
185 hsep [ptext (sLit "let {"), (pprBndr LetBind val_bdr $$ ppr val_bndr), equals],
186 nest 2 (pprCoreExpr rhs),
190 ppr_expr add_par (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
192 (hang (ptext (sLit "let {"))
193 2 (hsep [ppr_binding (val_bdr,rhs),
194 ptext (sLit "} in")])
199 -- General case (recursive case, too)
200 ppr_expr add_par (Let bind expr)
202 sep [hang (ptext keyword) 2 (ppr_bind bind <+> ptext (sLit "} in")),
205 keyword = case bind of
206 Rec _ -> (sLit "letrec {")
207 NonRec _ _ -> (sLit "let {")
209 ppr_expr add_par (Note (SCC cc) expr)
210 = add_par (sep [pprCostCentreCore cc, pprCoreExpr expr])
212 ppr_expr add_par (Note InlineMe expr)
213 = add_par $ ptext (sLit "__inline_me") <+> pprParendExpr expr
215 ppr_expr add_par (Note (CoreNote s) expr)
217 sep [sep [ptext (sLit "__core_note"), pprHsString (mkFastString s)],
220 pprCoreAlt :: OutputableBndr a => (AltCon, [a] , Expr a) -> SDoc
221 pprCoreAlt (con, args, rhs)
222 = hang (ppr_case_pat con args) 2 (pprCoreExpr rhs)
224 ppr_case_pat :: OutputableBndr a => AltCon -> [a] -> SDoc
225 ppr_case_pat (DataAlt dc) args
227 = tupleParens (tupleTyConBoxity tc) (hsep (punctuate comma (map ppr_bndr args))) <+> arrow
229 ppr_bndr = pprBndr CaseBind
232 ppr_case_pat con args
233 = ppr con <+> sep (map ppr_bndr args) <+> arrow
235 ppr_bndr = pprBndr CaseBind
237 pprArg :: OutputableBndr a => Expr a -> SDoc
238 pprArg (Type ty) = ptext (sLit "@") <+> pprParendType ty
239 pprArg expr = pprParendExpr expr
242 Other printing bits-and-bobs used with the general @pprCoreBinding@
243 and @pprCoreExpr@ functions.
246 instance OutputableBndr Var where
247 pprBndr = pprCoreBinder
249 pprCoreBinder :: BindingSite -> Var -> SDoc
250 pprCoreBinder LetBind binder
251 | isTyVar binder = pprTypedBinder binder
253 = vcat [sig, pprIdDetails binder, pragmas]
255 sig = pprTypedBinder binder
256 pragmas = ppIdInfo binder (idInfo binder)
258 -- Lambda bound type variables are preceded by "@"
259 pprCoreBinder LambdaBind bndr = parens (pprTypedBinder bndr)
261 -- Case bound things don't get a signature or a herald, unless we have debug on
262 pprCoreBinder CaseBind bndr
263 = getPprStyle $ \ sty ->
264 if debugStyle sty then
265 parens (pprTypedBinder bndr)
267 pprUntypedBinder bndr
269 pprUntypedBinder :: Var -> SDoc
270 pprUntypedBinder binder
271 | isTyVar binder = ptext (sLit "@") <+> ppr binder -- NB: don't print kind
272 | otherwise = pprIdBndr binder
274 pprTypedBinder :: Var -> SDoc
275 pprTypedBinder binder
276 | isTyVar binder = ptext (sLit "@") <+> pprTyVarBndr binder
277 | otherwise = pprIdBndr binder <+> dcolon <+> pprType (idType binder)
279 pprTyVarBndr :: TyVar -> SDoc
281 = getPprStyle $ \ sty ->
282 if debugStyle sty then
283 hsep [ppr tyvar, dcolon, pprParendKind kind]
284 -- See comments with ppDcolon in PprCore.lhs
288 kind = tyVarKind tyvar
290 -- pprIdBndr does *not* print the type
291 -- When printing any Id binder in debug mode, we print its inline pragma and one-shot-ness
292 pprIdBndr :: Id -> SDoc
293 pprIdBndr id = ppr id <+> pprIdBndrInfo (idInfo id)
295 pprIdBndrInfo :: IdInfo -> SDoc
297 = megaSeqIdInfo info `seq` doc -- The seq is useful for poking on black holes
299 prag_info = inlinePragInfo info
300 occ_info = occInfo info
301 dmd_info = newDemandInfo info
302 lbv_info = lbvarInfo info
304 no_info = isAlwaysActive prag_info && isNoOcc occ_info &&
305 (case dmd_info of { Nothing -> True; Just d -> isTop d }) &&
306 hasNoLBVarInfo lbv_info
308 doc | no_info = empty
310 = brackets $ hsep [ppr prag_info, ppr occ_info,
311 ppr dmd_info, ppr lbv_info
312 #ifdef OLD_STRICTNESS
313 , ppr (demandInfo id)
320 pprIdDetails :: Id -> SDoc
321 pprIdDetails id | isGlobalId id = ppr (globalIdDetails id)
322 | isExportedId id = ptext (sLit "[Exported]")
325 ppIdInfo :: Id -> IdInfo -> SDoc
328 vcat [ ppArityInfo a,
329 ppWorkerInfo (workerInfo info),
330 ppCafInfo (cafInfo info),
331 #ifdef OLD_STRICTNESS
335 pprNewStrictness (newStrictnessInfo info),
336 if null rules then empty
337 else ptext (sLit "RULES:") <+> vcat (map pprRule rules)
338 -- Inline pragma, occ, demand, lbvar info
339 -- printed out with all binders (when debug is on);
340 -- see PprCore.pprIdBndr
344 #ifdef OLD_STRICTNESS
345 s = strictnessInfo info
348 rules = specInfoRules (specInfo info)
353 instance Outputable CoreRule where
356 pprRules :: [CoreRule] -> SDoc
357 pprRules rules = vcat (map pprRule rules)
359 pprRule :: CoreRule -> SDoc
360 pprRule (BuiltinRule { ru_fn = fn, ru_name = name})
361 = ptext (sLit "Built in rule for") <+> ppr fn <> colon <+> doubleQuotes (ftext name)
363 pprRule (Rule { ru_name = name, ru_act = act, ru_fn = fn,
364 ru_bndrs = tpl_vars, ru_args = tpl_args,
366 = hang (doubleQuotes (ftext name) <+> ppr act)
367 4 (sep [ptext (sLit "forall") <+> braces (sep (map pprTypedBinder tpl_vars)),
368 nest 2 (ppr fn <+> sep (map pprArg tpl_args)),
369 nest 2 (ptext (sLit "=") <+> pprCoreExpr rhs)