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, pprCoreAlt,
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,
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
74 pprCoreAlt = ppr_alt pprCoreEnv
76 pprCoreEnv = initCoreEnv pprCoreBinder
79 Printer for unfoldings in interfaces
80 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
82 instance Outputable b => Outputable (Bind b) where
83 ppr bind = ppr_bind pprGenericEnv bind
85 instance Outputable b => Outputable (Expr b) where
86 ppr expr = ppr_noparend_expr pprGenericEnv expr
88 pprGenericEnv :: Outputable b => PprEnv b
89 pprGenericEnv = initCoreEnv (\site -> ppr)
92 %************************************************************************
94 \subsection{Instance declarations for Core printing}
96 %************************************************************************
102 (Just pprCostCentreCore) -- Cost centres
104 (Just ppr) -- tyvar occs
105 (Just pprParendType) -- types
107 (Just pbdr) (Just ppr) -- value vars
108 -- Use pprIdBndr for this last one as a debugging device.
111 %************************************************************************
113 \subsection{The guts}
115 %************************************************************************
118 pprTopBinds pe binds = vcat (map (pprTopBind pe) binds)
120 pprTopBind pe (NonRec binder expr)
121 = ppr_binding_pe pe (binder,expr) $$ text ""
123 pprTopBind pe (Rec binds)
124 = vcat [ptext SLIT("Rec {"),
125 vcat (map (ppr_binding_pe pe) binds),
126 ptext SLIT("end Rec }"),
131 ppr_bind :: PprEnv b -> Bind b -> SDoc
133 ppr_bind pe (NonRec val_bdr expr) = ppr_binding_pe pe (val_bdr, expr)
134 ppr_bind pe (Rec binds) = vcat (map pp binds)
136 pp bind = ppr_binding_pe pe bind <> semi
138 ppr_binding_pe :: PprEnv b -> (b, Expr b) -> SDoc
139 ppr_binding_pe pe (val_bdr, expr)
140 = sep [pBndr pe LetBind val_bdr,
141 nest 2 (equals <+> ppr_noparend_expr pe expr)]
145 ppr_parend_expr pe expr = ppr_expr parens pe expr
146 ppr_noparend_expr pe expr = ppr_expr noParens pe expr
148 noParens :: SDoc -> SDoc
153 ppr_expr :: (SDoc -> SDoc) -> PprEnv b -> Expr b -> SDoc
154 -- The function adds parens in context that need
155 -- an atomic value (e.g. function args)
157 ppr_expr add_par pe (Type ty) = add_par (ptext SLIT("TYPE") <+> ppr ty) -- Wierd
159 ppr_expr add_par pe (Var name) = pOcc pe name
160 ppr_expr add_par pe (Lit lit) = ppr lit
162 ppr_expr add_par pe expr@(Lam _ _)
164 (bndrs, body) = collectBinders expr
167 hang (ptext SLIT("\\") <+> sep (map (pBndr pe LambdaBind) bndrs) <+> arrow)
168 4 (ppr_noparend_expr pe body)
170 ppr_expr add_par pe expr@(App fun arg)
171 = case collectArgs expr of { (fun, args) ->
173 pp_args = sep (map (ppr_arg pe) args)
174 val_args = dropWhile isTypeArg args -- Drop the type arguments for tuples
175 pp_tup_args = sep (punctuate comma (map (ppr_arg pe) val_args))
178 Var f -> case isDataConId_maybe f of
179 -- Notice that we print the *worker*
180 -- for tuples in paren'd format.
181 Just dc | saturated && isTupleTyCon tc
182 -> tupleParens (tupleTyConBoxity tc) pp_tup_args
185 saturated = length val_args == idArity f
187 other -> add_par (hang (pOcc pe f) 4 pp_args)
189 other -> add_par (hang (ppr_parend_expr pe fun) 4 pp_args)
192 ppr_expr add_par pe (Case expr var [(con,args,rhs)])
194 sep [sep [ptext SLIT("case") <+> ppr_noparend_expr pe expr,
195 hsep [ptext SLIT("of"),
198 ppr_case_pat pe con args
200 ppr_noparend_expr pe rhs,
204 ppr_bndr = pBndr pe CaseBind
206 ppr_expr add_par pe (Case expr var alts)
208 sep [sep [ptext SLIT("case") <+> ppr_noparend_expr pe expr,
209 ptext SLIT("of") <+> ppr_bndr var <+> char '{'],
210 nest 4 (sep (punctuate semi (map (ppr_alt pe) alts))),
214 ppr_bndr = pBndr pe CaseBind
217 -- special cases: let ... in let ...
218 -- ("disgusting" SLPJ)
220 ppr_expr add_par pe (Let bind@(NonRec val_bdr rhs@(Let _ _)) body)
223 hsep [ptext SLIT("let {"), pBndr pe LetBind val_bdr, equals],
224 nest 2 (ppr_noparend_expr pe rhs),
226 ppr_noparend_expr pe body ]
228 ppr_expr add_par pe (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
230 (hang (ptext SLIT("let {"))
231 2 (hsep [hang (hsep [pBndr pe LetBind val_bdr, equals])
232 4 (ppr_noparend_expr pe rhs),
235 ppr_noparend_expr pe expr)
237 -- general case (recursive case, too)
238 ppr_expr add_par pe (Let bind expr)
240 sep [hang (ptext keyword) 2 (ppr_bind pe bind),
241 hang (ptext SLIT("} in ")) 2 (ppr_noparend_expr pe expr)]
243 keyword = case bind of
244 Rec _ -> SLIT("__letrec {")
245 NonRec _ _ -> SLIT("let {")
247 ppr_expr add_par pe (Note (SCC cc) expr)
248 = add_par (sep [pSCC pe cc, ppr_noparend_expr pe expr])
251 ppr_expr add_par pe (Note (Coerce to_ty from_ty) expr)
253 getPprStyle $ \ sty ->
254 if debugStyle sty && not (ifaceStyle sty) then
255 sep [ptext SLIT("__coerce") <+> sep [pTy pe to_ty, pTy pe from_ty],
256 ppr_parend_expr pe expr]
258 sep [hsep [ptext SLIT("__coerce"), pTy pe to_ty],
259 ppr_parend_expr pe expr]
261 ppr_expr add_par pe (Note (Coerce to_ty from_ty) expr)
263 sep [sep [ptext SLIT("__coerce"), nest 4 (pTy pe to_ty)],
264 ppr_parend_expr pe expr]
267 ppr_expr add_par pe (Note InlineCall expr)
268 = add_par (ptext SLIT("__inline_call") <+> ppr_parend_expr pe expr)
270 ppr_expr add_par pe (Note InlineMe expr)
271 = add_par $ ptext SLIT("__inline_me") <+> ppr_parend_expr pe expr
273 ppr_alt pe (con, args, rhs)
274 = hang (ppr_case_pat pe con args) 4 (ppr_noparend_expr pe rhs)
276 ppr_case_pat pe con@(DataAlt dc) args
278 = tupleParens (tupleTyConBoxity tc) (hsep (punctuate comma (map ppr_bndr args))) <+> arrow
280 ppr_bndr = pBndr pe CaseBind
283 ppr_case_pat pe con args
284 = ppr con <+> hsep (map ppr_bndr args) <+> arrow
286 ppr_bndr = pBndr pe CaseBind
288 ppr_arg pe (Type ty) = ptext SLIT("@") <+> pTy pe ty
289 ppr_arg pe expr = ppr_parend_expr pe expr
291 arrow = ptext SLIT("->")
294 Other printing bits-and-bobs used with the general @pprCoreBinding@
295 and @pprCoreExpr@ functions.
298 -- Used for printing dump info
299 pprCoreBinder LetBind binder
300 = vcat [sig, pragmas, ppr binder]
302 sig = pprTypedBinder binder
303 pragmas = ppIdInfo binder (idInfo binder)
305 -- Lambda bound type variables are preceded by "@"
306 pprCoreBinder LambdaBind bndr = pprTypedBinder bndr
308 -- Case bound things don't get a signature or a herald
309 pprCoreBinder CaseBind bndr = pprUntypedBinder bndr
311 pprUntypedBinder binder
312 | isTyVar binder = ptext SLIT("@") <+> pprTyVarBndr binder
313 | otherwise = pprIdBndr binder
315 pprTypedBinder binder
316 | isTyVar binder = ptext SLIT("@") <+> pprTyVarBndr binder
317 | otherwise = pprIdBndr binder <+> dcolon <+> pprParendType (idType binder)
318 -- The space before the :: is important; it helps the lexer
319 -- when reading inferfaces. Otherwise it would lex "a::b" as one thing.
321 -- It's important that the type is parenthesised too, at least when
322 -- printing interfaces, because we get \ x::(a->b) y::(c->d) -> ...
324 -- When printing any Id binder in debug mode, we print its inline pragma and one-shot-ness
325 pprIdBndr id = ppr id <+>
326 (megaSeqIdInfo (idInfo id) `seq`
327 -- Useful for poking on black holes
328 ifPprDebug (ppr (idInlinePragma id) <+> ppr (idOccInfo id) <+>
329 ppr (idDemandInfo id)) <+> ppr (idLBVarInfo id))
334 ppIdInfo :: Id -> IdInfo -> SDoc
337 ppFlavourInfo (flavourInfo info),
340 ppWorkerInfo (workerInfo info),
345 -- Inline pragma, occ, demand, lbvar info
346 -- printed out with all binders (when debug is on);
347 -- see PprCore.pprIdBndr
352 s = strictnessInfo info
360 pprCoreRules :: Id -> CoreRules -> SDoc
361 pprCoreRules var (Rules rules _) = vcat (map (pprCoreRule (ppr var)) rules)
363 pprIdCoreRule :: IdCoreRule -> SDoc
364 pprIdCoreRule (id,rule) = pprCoreRule (ppr id) rule
366 pprCoreRule :: SDoc -> CoreRule -> SDoc
367 pprCoreRule pp_fn (BuiltinRule _)
368 = ifPprDebug (ptext SLIT("A built in rule"))
370 pprCoreRule pp_fn (Rule name tpl_vars tpl_args rhs)
371 = doubleQuotes (ptext name) <+>
373 ptext SLIT("__forall") <+> braces (sep (map pprTypedBinder tpl_vars)),
374 nest 4 (pp_fn <+> sep (map pprArg tpl_args)),
375 nest 4 (ptext SLIT("=") <+> pprCoreExpr rhs)