2 % (c) The AQUA Project, Glasgow University, 1996-1998
4 %************************************************************************
6 \section[PprCore]{Printing of Core syntax, including for interfaces}
8 %************************************************************************
12 pprCoreExpr, pprParendExpr,
13 pprCoreBinding, pprCoreBindings, pprIdBndr,
14 pprCoreBinding, pprCoreBindings,
15 pprCoreRules, pprCoreRule, pprIdCoreRule
18 #include "HsVersions.h"
21 import CostCentre ( pprCostCentreCore )
22 import Id ( Id, idType, isDataConId_maybe, idLBVarInfo, idArity,
23 idInfo, idInlinePragma, idDemandInfo, idOccInfo
25 import Var ( isTyVar )
26 import IdInfo ( IdInfo, megaSeqIdInfo,
27 arityInfo, ppArityInfo, ppFlavourInfo, flavourInfo,
28 specInfo, cprInfo, ppCprInfo,
29 strictnessInfo, ppStrictnessInfo, cafInfo, ppCafInfo,
30 cprInfo, ppCprInfo, lbvarInfo,
31 workerInfo, ppWorkerInfo,
32 tyGenInfo, ppTyGenInfo
34 import DataCon ( dataConTyCon )
35 import TyCon ( tupleTyConBoxity, isTupleTyCon )
36 import PprType ( pprParendType, pprTyVarBndr )
37 import BasicTypes ( tupleParens )
42 %************************************************************************
44 \subsection{Public interfaces for Core printing (excluding instances)}
46 %************************************************************************
48 @pprCoreBinding@ and @pprCoreExpr@ let you give special printing
49 function for ``major'' val_bdrs (those next to equal signs :-),
50 ``minor'' ones (lambda-bound, case-bound), and bindees. They would
51 usually be called through some intermediary.
53 The binder/occ printers take the default ``homogenized'' (see
54 @PprEnv@...) @Doc@ and the binder/occ. They can either use the
55 homogenized one, or they can ignore it completely. In other words,
56 the things passed in act as ``hooks'', getting the last word on how to
59 @pprParendCoreExpr@ puts parens around non-atomic Core expressions.
61 Un-annotated core dumps
62 ~~~~~~~~~~~~~~~~~~~~~~~
64 pprCoreBindings :: [CoreBind] -> SDoc
65 pprCoreBinding :: CoreBind -> SDoc
66 pprCoreExpr :: CoreExpr -> SDoc
67 pprParendExpr :: CoreExpr -> SDoc
69 pprCoreBindings = pprTopBinds pprCoreEnv
70 pprCoreBinding = pprTopBind pprCoreEnv
71 pprCoreExpr = ppr_noparend_expr pprCoreEnv
72 pprParendExpr = ppr_parend_expr pprCoreEnv
73 pprArg = ppr_arg pprCoreEnv
75 pprCoreEnv = initCoreEnv pprCoreBinder
78 Printer for unfoldings in interfaces
79 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
81 instance Outputable b => Outputable (Bind b) where
82 ppr bind = ppr_bind pprGenericEnv bind
84 instance Outputable b => Outputable (Expr b) where
85 ppr expr = ppr_noparend_expr pprGenericEnv expr
87 pprGenericEnv :: Outputable b => PprEnv b
88 pprGenericEnv = initCoreEnv (\site -> ppr)
91 %************************************************************************
93 \subsection{Instance declarations for Core printing}
95 %************************************************************************
101 (Just pprCostCentreCore) -- Cost centres
103 (Just ppr) -- tyvar occs
104 (Just pprParendType) -- types
106 (Just pbdr) (Just ppr) -- value vars
107 -- Use pprIdBndr for this last one as a debugging device.
110 %************************************************************************
112 \subsection{The guts}
114 %************************************************************************
117 pprTopBinds pe binds = vcat (map (pprTopBind pe) binds)
119 pprTopBind pe (NonRec binder expr)
120 = ppr_binding_pe pe (binder,expr) $$ text ""
122 pprTopBind pe (Rec binds)
123 = vcat [ptext SLIT("Rec {"),
124 vcat (map (ppr_binding_pe pe) binds),
125 ptext SLIT("end Rec }"),
130 ppr_bind :: PprEnv b -> Bind b -> SDoc
132 ppr_bind pe (NonRec val_bdr expr) = ppr_binding_pe pe (val_bdr, expr)
133 ppr_bind pe (Rec binds) = vcat (map pp binds)
135 pp bind = ppr_binding_pe pe bind <> semi
137 ppr_binding_pe :: PprEnv b -> (b, Expr b) -> SDoc
138 ppr_binding_pe pe (val_bdr, expr)
139 = sep [pBndr pe LetBind val_bdr,
140 nest 2 (equals <+> ppr_noparend_expr pe expr)]
144 ppr_parend_expr pe expr = ppr_expr parens pe expr
145 ppr_noparend_expr pe expr = ppr_expr noParens pe expr
147 noParens :: SDoc -> SDoc
152 ppr_expr :: (SDoc -> SDoc) -> PprEnv b -> Expr b -> SDoc
153 -- The function adds parens in context that need
154 -- an atomic value (e.g. function args)
156 ppr_expr add_par pe (Type ty) = add_par (ptext SLIT("TYPE") <+> ppr ty) -- Wierd
158 ppr_expr add_par pe (Var name) = pOcc pe name
159 ppr_expr add_par pe (Lit lit) = ppr lit
161 ppr_expr add_par pe expr@(Lam _ _)
163 (bndrs, body) = collectBinders expr
166 hang (ptext SLIT("\\") <+> sep (map (pBndr pe LambdaBind) bndrs) <+> arrow)
167 4 (ppr_noparend_expr pe body)
169 ppr_expr add_par pe expr@(App fun arg)
170 = case collectArgs expr of { (fun, args) ->
172 pp_args = sep (map (ppr_arg pe) args)
173 val_args = dropWhile isTypeArg args -- Drop the type arguments for tuples
174 pp_tup_args = sep (punctuate comma (map (ppr_arg pe) val_args))
177 Var f -> case isDataConId_maybe f of
178 -- Notice that we print the *worker*
179 -- for tuples in paren'd format.
180 Just dc | saturated && isTupleTyCon tc
181 -> tupleParens (tupleTyConBoxity tc) pp_tup_args
184 saturated = length val_args == idArity f
186 other -> add_par (hang (pOcc pe f) 4 pp_args)
188 other -> add_par (hang (ppr_parend_expr pe fun) 4 pp_args)
191 ppr_expr add_par pe (Case expr var [(con,args,rhs)])
193 sep [sep [ptext SLIT("case") <+> ppr_noparend_expr pe expr,
194 hsep [ptext SLIT("of"),
197 ppr_case_pat pe con args
199 ppr_noparend_expr pe rhs,
203 ppr_bndr = pBndr pe CaseBind
205 ppr_expr add_par pe (Case expr var alts)
207 sep [sep [ptext SLIT("case") <+> ppr_noparend_expr pe expr,
208 ptext SLIT("of") <+> ppr_bndr var <+> char '{'],
209 nest 4 (sep (punctuate semi (map ppr_alt alts))),
213 ppr_bndr = pBndr pe CaseBind
215 ppr_alt (con, args, rhs) = hang (ppr_case_pat pe con args)
216 4 (ppr_noparend_expr pe rhs)
218 -- special cases: let ... in let ...
219 -- ("disgusting" SLPJ)
221 ppr_expr add_par pe (Let bind@(NonRec val_bdr rhs@(Let _ _)) body)
224 hsep [ptext SLIT("let {"), pBndr pe LetBind val_bdr, equals],
225 nest 2 (ppr_noparend_expr pe rhs),
227 ppr_noparend_expr pe body ]
229 ppr_expr add_par pe (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
231 (hang (ptext SLIT("let {"))
232 2 (hsep [hang (hsep [pBndr pe LetBind val_bdr, equals])
233 4 (ppr_noparend_expr pe rhs),
236 ppr_noparend_expr pe expr)
238 -- general case (recursive case, too)
239 ppr_expr add_par pe (Let bind expr)
241 sep [hang (ptext keyword) 2 (ppr_bind pe bind),
242 hang (ptext SLIT("} in ")) 2 (ppr_noparend_expr pe expr)]
244 keyword = case bind of
245 Rec _ -> SLIT("__letrec {")
246 NonRec _ _ -> SLIT("let {")
248 ppr_expr add_par pe (Note (SCC cc) expr)
249 = add_par (sep [pSCC pe cc, ppr_noparend_expr pe expr])
252 ppr_expr add_par pe (Note (Coerce to_ty from_ty) expr)
254 getPprStyle $ \ sty ->
255 if debugStyle sty && not (ifaceStyle sty) then
256 sep [ptext SLIT("__coerce") <+> sep [pTy pe to_ty, pTy pe from_ty],
257 ppr_parend_expr pe expr]
259 sep [hsep [ptext SLIT("__coerce"), pTy pe to_ty],
260 ppr_parend_expr pe expr]
262 ppr_expr add_par pe (Note (Coerce to_ty from_ty) expr)
264 sep [sep [ptext SLIT("__coerce"), nest 4 (pTy pe to_ty)],
265 ppr_parend_expr pe expr]
268 ppr_expr add_par pe (Note InlineCall expr)
269 = add_par (ptext SLIT("__inline_call") <+> ppr_parend_expr pe expr)
271 ppr_expr add_par pe (Note InlineMe expr)
272 = add_par $ ptext SLIT("__inline_me") <+> ppr_parend_expr pe expr
274 ppr_case_pat pe con@(DataAlt dc) args
276 = tupleParens (tupleTyConBoxity tc) (hsep (punctuate comma (map ppr_bndr args))) <+> arrow
278 ppr_bndr = pBndr pe CaseBind
281 ppr_case_pat pe con args
282 = ppr con <+> hsep (map ppr_bndr args) <+> arrow
284 ppr_bndr = pBndr pe CaseBind
286 ppr_arg pe (Type ty) = ptext SLIT("@") <+> pTy pe ty
287 ppr_arg pe expr = ppr_parend_expr pe expr
289 arrow = ptext SLIT("->")
292 Other printing bits-and-bobs used with the general @pprCoreBinding@
293 and @pprCoreExpr@ functions.
296 -- Used for printing dump info
297 pprCoreBinder LetBind binder
298 = vcat [sig, pragmas, ppr binder]
300 sig = pprTypedBinder binder
301 pragmas = ppIdInfo binder (idInfo binder)
303 -- Lambda bound type variables are preceded by "@"
304 pprCoreBinder LambdaBind bndr = pprTypedBinder bndr
306 -- Case bound things don't get a signature or a herald
307 pprCoreBinder CaseBind bndr = pprUntypedBinder bndr
309 pprUntypedBinder binder
310 | isTyVar binder = ptext SLIT("@") <+> pprTyVarBndr binder
311 | otherwise = pprIdBndr binder
313 pprTypedBinder binder
314 | isTyVar binder = ptext SLIT("@") <+> pprTyVarBndr binder
315 | otherwise = pprIdBndr binder <+> dcolon <+> pprParendType (idType binder)
316 -- The space before the :: is important; it helps the lexer
317 -- when reading inferfaces. Otherwise it would lex "a::b" as one thing.
319 -- It's important that the type is parenthesised too, at least when
320 -- printing interfaces, because we get \ x::(a->b) y::(c->d) -> ...
322 -- When printing any Id binder in debug mode, we print its inline pragma and one-shot-ness
323 pprIdBndr id = ppr id <+>
324 (megaSeqIdInfo (idInfo id) `seq`
325 -- Useful for poking on black holes
326 ifPprDebug (ppr (idInlinePragma id) <+> ppr (idOccInfo id) <+>
327 ppr (idDemandInfo id)) <+> ppr (idLBVarInfo id))
332 ppIdInfo :: Id -> IdInfo -> SDoc
335 ppFlavourInfo (flavourInfo info),
338 ppWorkerInfo (workerInfo info),
343 -- Inline pragma, occ, demand, lbvar info
344 -- printed out with all binders (when debug is on);
345 -- see PprCore.pprIdBndr
350 s = strictnessInfo info
358 pprCoreRules :: Id -> CoreRules -> SDoc
359 pprCoreRules var (Rules rules _) = vcat (map (pprCoreRule (ppr var)) rules)
361 pprIdCoreRule :: IdCoreRule -> SDoc
362 pprIdCoreRule (id,rule) = pprCoreRule (ppr id) rule
364 pprCoreRule :: SDoc -> CoreRule -> SDoc
365 pprCoreRule pp_fn (BuiltinRule _)
366 = ifPprDebug (ptext SLIT("A built in rule"))
368 pprCoreRule pp_fn (Rule name tpl_vars tpl_args rhs)
369 = doubleQuotes (ptext name) <+>
371 ptext SLIT("__forall") <+> braces (sep (map pprTypedBinder tpl_vars)),
372 nest 4 (pp_fn <+> sep (map pprArg tpl_args)),
373 nest 4 (ptext SLIT("=") <+> pprCoreExpr rhs)