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,
37 %************************************************************************
39 \subsection{Public interfaces for Core printing (excluding instances)}
41 %************************************************************************
43 @pprParendCoreExpr@ puts parens around non-atomic Core expressions.
46 pprCoreBindings :: OutputableBndr b => [Bind b] -> SDoc
47 pprCoreBinding :: OutputableBndr b => Bind b -> SDoc
48 pprCoreExpr :: OutputableBndr b => Expr b -> SDoc
49 pprParendExpr :: OutputableBndr b => Expr b -> SDoc
51 pprCoreBindings = pprTopBinds
52 pprCoreBinding = pprTopBind
54 instance OutputableBndr b => Outputable (Bind b) where
55 ppr bind = ppr_bind bind
57 instance OutputableBndr b => Outputable (Expr b) where
58 ppr expr = pprCoreExpr expr
62 %************************************************************************
66 %************************************************************************
69 pprTopBinds :: OutputableBndr a => [Bind a] -> SDoc
70 pprTopBinds binds = vcat (map pprTopBind binds)
72 pprTopBind :: OutputableBndr a => Bind a -> SDoc
73 pprTopBind (NonRec binder expr)
74 = ppr_binding (binder,expr) $$ text ""
76 pprTopBind (Rec binds)
77 = vcat [ptext (sLit "Rec {"),
78 vcat (map ppr_binding binds),
79 ptext (sLit "end Rec }"),
84 ppr_bind :: OutputableBndr b => Bind b -> SDoc
86 ppr_bind (NonRec val_bdr expr) = ppr_binding (val_bdr, expr)
87 ppr_bind (Rec binds) = vcat (map pp binds)
89 pp bind = ppr_binding bind <> semi
91 ppr_binding :: OutputableBndr b => (b, Expr b) -> SDoc
92 ppr_binding (val_bdr, expr)
93 = pprBndr LetBind val_bdr $$
94 hang (ppr val_bdr <+> equals) 2 (pprCoreExpr expr)
98 pprParendExpr expr = ppr_expr parens expr
99 pprCoreExpr expr = ppr_expr noParens expr
101 noParens :: SDoc -> SDoc
106 ppr_expr :: OutputableBndr b => (SDoc -> SDoc) -> Expr b -> SDoc
107 -- The function adds parens in context that need
108 -- an atomic value (e.g. function args)
110 ppr_expr add_par (Type ty) = add_par (ptext (sLit "TYPE") <+> ppr ty) -- Wierd
112 ppr_expr _ (Var name) = ppr name
113 ppr_expr _ (Lit lit) = ppr lit
115 ppr_expr add_par (Cast expr co)
117 sep [pprParendExpr expr,
118 ptext (sLit "`cast`") <+> pprCo co]
120 pprCo co | opt_SuppressCoercions = ptext (sLit "...")
122 $ sep [ppr co, dcolon <+> ppr (coercionKindPredTy co)]
125 ppr_expr add_par expr@(Lam _ _)
127 (bndrs, body) = collectBinders expr
130 hang (ptext (sLit "\\") <+> sep (map (pprBndr LambdaBind) bndrs) <+> arrow)
133 ppr_expr add_par expr@(App {})
134 = case collectArgs expr of { (fun, args) ->
136 pp_args = sep (map pprArg args)
137 val_args = dropWhile isTypeArg args -- Drop the type arguments for tuples
138 pp_tup_args = sep (punctuate comma (map pprCoreExpr val_args))
141 Var f -> case isDataConWorkId_maybe f of
142 -- Notice that we print the *worker*
143 -- for tuples in paren'd format.
144 Just dc | saturated && isTupleTyCon tc
145 -> tupleParens (tupleTyConBoxity tc) pp_tup_args
148 saturated = val_args `lengthIs` idArity f
150 _ -> add_par (hang (ppr f) 2 pp_args)
152 _ -> add_par (hang (pprParendExpr fun) 2 pp_args)
155 ppr_expr add_par (Case expr var ty [(con,args,rhs)])
157 sep [sep [ptext (sLit "case") <+> pprCoreExpr expr,
158 ifPprDebug (braces (ppr ty)),
159 sep [ptext (sLit "of") <+> ppr_bndr var,
160 char '{' <+> ppr_case_pat con args]
166 ppr_bndr = pprBndr CaseBind
168 ppr_expr add_par (Case expr var ty alts)
170 sep [sep [ptext (sLit "case")
172 <+> ifPprDebug (braces (ppr ty)),
173 ptext (sLit "of") <+> ppr_bndr var <+> char '{'],
174 nest 2 (sep (punctuate semi (map pprCoreAlt alts))),
178 ppr_bndr = pprBndr CaseBind
181 -- special cases: let ... in let ...
182 -- ("disgusting" SLPJ)
185 ppr_expr add_par (Let bind@(NonRec val_bdr rhs@(Let _ _)) body)
188 hsep [ptext (sLit "let {"), (pprBndr LetBind val_bdr $$ ppr val_bndr), equals],
189 nest 2 (pprCoreExpr rhs),
193 ppr_expr add_par (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
195 (hang (ptext (sLit "let {"))
196 2 (hsep [ppr_binding (val_bdr,rhs),
197 ptext (sLit "} in")])
202 -- General case (recursive case, too)
203 ppr_expr add_par (Let bind expr)
205 sep [hang (ptext keyword) 2 (ppr_bind bind <+> ptext (sLit "} in")),
208 keyword = case bind of
209 Rec _ -> (sLit "letrec {")
210 NonRec _ _ -> (sLit "let {")
212 ppr_expr add_par (Note (SCC cc) expr)
213 = add_par (sep [pprCostCentreCore cc, pprCoreExpr expr])
215 ppr_expr add_par (Note InlineMe expr)
216 = add_par $ ptext (sLit "__inline_me") <+> pprParendExpr expr
218 ppr_expr add_par (Note (CoreNote s) expr)
220 sep [sep [ptext (sLit "__core_note"), pprHsString (mkFastString s)],
223 pprCoreAlt :: OutputableBndr a => (AltCon, [a] , Expr a) -> SDoc
224 pprCoreAlt (con, args, rhs)
225 = hang (ppr_case_pat con args) 2 (pprCoreExpr rhs)
227 ppr_case_pat :: OutputableBndr a => AltCon -> [a] -> SDoc
228 ppr_case_pat (DataAlt dc) args
230 = tupleParens (tupleTyConBoxity tc) (hsep (punctuate comma (map ppr_bndr args))) <+> arrow
232 ppr_bndr = pprBndr CaseBind
235 ppr_case_pat con args
236 = ppr con <+> sep (map ppr_bndr args) <+> arrow
238 ppr_bndr = pprBndr CaseBind
240 pprArg :: OutputableBndr a => Expr a -> SDoc
241 pprArg (Type ty) = ptext (sLit "@") <+> pprParendType ty
242 pprArg expr = pprParendExpr expr
245 Other printing bits-and-bobs used with the general @pprCoreBinding@
246 and @pprCoreExpr@ functions.
249 instance OutputableBndr Var where
250 pprBndr = pprCoreBinder
252 pprCoreBinder :: BindingSite -> Var -> SDoc
253 pprCoreBinder LetBind binder
254 | isTyVar binder = pprKindedTyVarBndr binder
256 = vcat [sig, pprIdExtras binder, pragmas]
258 sig = pprTypedBinder binder
259 pragmas = ppIdInfo binder (idInfo binder)
261 -- Lambda bound type variables are preceded by "@"
262 pprCoreBinder LambdaBind bndr
264 = getPprStyle $ \ sty ->
265 if debugStyle sty then
266 parens (pprTypedBinder bndr)
270 = parens (pprTypedBinder bndr)
272 -- Case bound things don't get a signature or a herald, unless we have debug on
273 pprCoreBinder CaseBind bndr
274 = getPprStyle $ \ sty ->
275 if debugStyle sty then
276 parens (pprTypedBinder bndr)
278 if isDeadBinder bndr then char '_'
279 else pprUntypedBinder bndr
281 pprUntypedBinder :: Var -> SDoc
282 pprUntypedBinder binder
283 | isTyVar binder = ptext (sLit "@") <+> ppr binder -- NB: don't print kind
284 | otherwise = pprIdBndr binder
286 pprTypedBinder :: Var -> SDoc
287 -- Print binder with a type or kind signature (not paren'd)
288 pprTypedBinder binder
289 | isTyVar binder = pprKindedTyVarBndr binder
290 | otherwise = pprIdBndr binder <+> dcolon <+> pprType (idType binder)
292 pprKindedTyVarBndr :: TyVar -> SDoc
293 -- Print a type variable binder with its kind (but not if *)
294 pprKindedTyVarBndr tyvar
295 = ptext (sLit "@") <+> ppr tyvar <> opt_kind
297 opt_kind -- Print the kind if not *
298 | isLiftedTypeKind kind = empty
299 | otherwise = dcolon <> pprKind kind
300 kind = tyVarKind tyvar
302 -- pprIdBndr does *not* print the type
303 -- When printing any Id binder in debug mode, we print its inline pragma and one-shot-ness
304 pprIdBndr :: Id -> SDoc
305 pprIdBndr id = ppr id <+> pprIdBndrInfo (idInfo id)
307 pprIdBndrInfo :: IdInfo -> SDoc
309 = megaSeqIdInfo info `seq` doc -- The seq is useful for poking on black holes
311 prag_info = inlinePragInfo info
312 occ_info = occInfo info
313 dmd_info = newDemandInfo info
314 lbv_info = lbvarInfo info
316 no_info = isDefaultInlinePragma prag_info && isNoOcc occ_info &&
317 (case dmd_info of { Nothing -> True; Just d -> isTop d }) &&
318 hasNoLBVarInfo lbv_info
320 doc | no_info = empty
322 = brackets $ hsep [ppr prag_info, ppr occ_info,
323 ppr dmd_info, ppr lbv_info
324 #ifdef OLD_STRICTNESS
325 , ppr (demandInfo id)
332 pprIdExtras :: Id -> SDoc
333 pprIdExtras id = pp_scope <> ppr (idDetails id)
335 pp_scope | isGlobalId id = ptext (sLit "GblId")
336 | isExportedId id = ptext (sLit "LclIdX")
337 | otherwise = ptext (sLit "LclId")
339 ppIdInfo :: Id -> IdInfo -> SDoc
342 vcat [ ppArityInfo a,
343 ppWorkerInfo (workerInfo info),
344 ppCafInfo (cafInfo info),
345 #ifdef OLD_STRICTNESS
349 pprNewStrictness (newStrictnessInfo info),
350 if null rules then empty
351 else ptext (sLit "RULES:") <+> vcat (map pprRule rules)
352 -- Inline pragma, occ, demand, lbvar info
353 -- printed out with all binders (when debug is on);
354 -- see PprCore.pprIdBndr
358 #ifdef OLD_STRICTNESS
359 s = strictnessInfo info
362 rules = specInfoRules (specInfo info)
367 instance Outputable CoreRule where
370 pprRules :: [CoreRule] -> SDoc
371 pprRules rules = vcat (map pprRule rules)
373 pprRule :: CoreRule -> SDoc
374 pprRule (BuiltinRule { ru_fn = fn, ru_name = name})
375 = ptext (sLit "Built in rule for") <+> ppr fn <> colon <+> doubleQuotes (ftext name)
377 pprRule (Rule { ru_name = name, ru_act = act, ru_fn = fn,
378 ru_bndrs = tpl_vars, ru_args = tpl_args,
380 = hang (doubleQuotes (ftext name) <+> ppr act)
381 4 (sep [ptext (sLit "forall") <+> braces (sep (map pprTypedBinder tpl_vars)),
382 nest 2 (ppr fn <+> sep (map pprArg tpl_args)),
383 nest 2 (ptext (sLit "=") <+> pprCoreExpr rhs)