2 % (c) The AQUA Project, Glasgow University, 1996-1998
4 %************************************************************************
6 \section[PprCore]{Printing of Core syntax, including for interfaces}
8 %************************************************************************
12 pprCoreExpr, pprParendExpr, pprIfaceUnfolding,
13 pprCoreBinding, pprCoreBindings, pprIdBndr,
14 pprCoreRules, pprCoreRule
17 #include "HsVersions.h"
20 import CostCentre ( pprCostCentreCore )
21 import Id ( idType, idInfo, getInlinePragma, getIdDemandInfo, Id )
22 import Var ( isTyVar )
23 import IdInfo ( IdInfo,
24 arityInfo, ppArityInfo, ppFlavourInfo, flavourInfo,
25 demandInfo, updateInfo, ppUpdateInfo, specInfo,
26 strictnessInfo, ppStrictnessInfo, cafInfo, ppCafInfo,
29 import Const ( Con(..), DataCon )
30 import DataCon ( isTupleCon, isUnboxedTupleCon )
31 import PprType ( pprParendType, pprTyVarBndr )
36 %************************************************************************
38 \subsection{Public interfaces for Core printing (excluding instances)}
40 %************************************************************************
42 @pprCoreBinding@ and @pprCoreExpr@ let you give special printing
43 function for ``major'' val_bdrs (those next to equal signs :-),
44 ``minor'' ones (lambda-bound, case-bound), and bindees. They would
45 usually be called through some intermediary.
47 The binder/occ printers take the default ``homogenized'' (see
48 @PprEnv@...) @Doc@ and the binder/occ. They can either use the
49 homogenized one, or they can ignore it completely. In other words,
50 the things passed in act as ``hooks'', getting the last word on how to
53 @pprParendCoreExpr@ puts parens around non-atomic Core expressions.
55 Un-annotated core dumps
56 ~~~~~~~~~~~~~~~~~~~~~~~
58 pprCoreBindings :: [CoreBind] -> SDoc
59 pprCoreBinding :: CoreBind -> SDoc
60 pprCoreExpr :: CoreExpr -> SDoc
61 pprParendExpr :: CoreExpr -> SDoc
63 pprCoreBindings = pprTopBinds pprCoreEnv
64 pprCoreBinding = pprTopBind pprCoreEnv
65 pprCoreExpr = ppr_expr pprCoreEnv
66 pprParendExpr = ppr_parend_expr pprCoreEnv
68 pprCoreEnv = initCoreEnv pprCoreBinder
71 Printer for unfoldings in interfaces
72 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74 pprIfaceUnfolding :: CoreExpr -> SDoc
75 pprIfaceUnfolding = ppr_parend_expr pprIfaceEnv
76 -- Notice that it's parenthesised
78 pprIfaceArg = ppr_arg pprIfaceEnv
80 pprIfaceEnv = initCoreEnv pprIfaceBinder
84 instance Outputable b => Outputable (Bind b) where
85 ppr bind = ppr_bind pprGenericEnv bind
87 instance Outputable b => Outputable (Expr b) where
88 ppr expr = ppr_expr pprGenericEnv expr
90 pprGenericEnv :: Outputable b => PprEnv b
91 pprGenericEnv = initCoreEnv (\site -> ppr)
94 %************************************************************************
96 \subsection{Instance declarations for Core printing}
98 %************************************************************************
104 (Just ppr) -- Constants
105 (Just pprCostCentreCore) -- Cost centres
107 (Just ppr) -- tyvar occs
108 (Just pprParendType) -- types
110 (Just pbdr) (Just ppr) -- value vars
111 -- Use pprIdBndr for this last one as a debugging device.
114 %************************************************************************
116 \subsection{The guts}
118 %************************************************************************
121 pprTopBinds pe binds = vcat (map (pprTopBind pe) binds)
123 pprTopBind pe (NonRec binder expr)
124 = sep [ppr_binding_pe pe (binder,expr)] $$ text ""
126 pprTopBind pe (Rec binds)
127 = vcat [ptext SLIT("Rec {"),
128 vcat (map (ppr_binding_pe pe) binds),
129 ptext SLIT("end Rec }"),
134 ppr_bind :: PprEnv b -> Bind b -> SDoc
136 ppr_bind pe (NonRec val_bdr expr) = ppr_binding_pe pe (val_bdr, expr)
137 ppr_bind pe (Rec binds) = vcat (map pp binds)
139 pp bind = ppr_binding_pe pe bind <> semi
141 ppr_binding_pe :: PprEnv b -> (b, Expr b) -> SDoc
142 ppr_binding_pe pe (val_bdr, expr)
143 = sep [pBndr pe LetBind val_bdr,
144 nest 2 (equals <+> ppr_expr pe expr)]
148 ppr_parend_expr pe expr
149 | no_parens = ppr_expr pe expr
150 | otherwise = parens (ppr_expr pe expr)
152 no_parens = case expr of
155 Con (DataCon dc) _ -> isTupleCon dc
160 ppr_expr :: PprEnv b -> Expr b -> SDoc
162 ppr_expr pe (Type ty) = ptext SLIT("TYPE") <+> ppr ty -- Wierd
164 ppr_expr pe (Var name) = pOcc pe name
166 ppr_expr pe (Con con [])
167 = ppr con -- Nullary constructors too
169 ppr_expr pe (Con (DataCon dc) args)
170 -- Drop the type arguments and print in (a,b,c) notation
172 = parens (sep (punctuate comma (map (ppr_arg pe) (dropWhile isTypeArg args))))
173 | isUnboxedTupleCon dc
175 hsep (punctuate comma (map (ppr_arg pe) (dropWhile isTypeArg args))) <>
178 ppr_expr pe (Con con args)
179 = pCon pe con <+> (braces $ sep (map (ppr_arg pe) args))
181 ppr_expr pe expr@(Lam _ _)
183 (bndrs, body) = collectBinders expr
185 hang (ptext SLIT("\\") <+> sep (map (pBndr pe LambdaBind) bndrs) <+> arrow)
188 ppr_expr pe expr@(App fun arg)
190 (final_fun, final_args) = go fun [arg]
191 go (App fun arg) args_so_far = go fun (arg:args_so_far)
192 go fun args_so_far = (fun, args_so_far)
194 hang (ppr_parend_expr pe final_fun) 4 (sep (map (ppr_arg pe) final_args))
196 ppr_expr pe (Case expr var [(con,args,rhs)])
197 = sep [sep [ptext SLIT("case") <+> ppr_expr pe expr,
198 hsep [ptext SLIT("of"),
201 ppr_case_pat pe con args
207 ppr_bndr = pBndr pe CaseBind
209 ppr_expr pe (Case expr var alts)
210 = sep [sep [ptext SLIT("case") <+> ppr_expr pe expr,
211 ptext SLIT("of") <+> ppr_bndr var <+> char '{'],
212 nest 4 (sep (punctuate semi (map ppr_alt alts))),
216 ppr_bndr = pBndr pe CaseBind
218 ppr_alt (con, args, rhs) = hang (ppr_case_pat pe con args)
221 -- special cases: let ... in let ...
222 -- ("disgusting" SLPJ)
224 ppr_expr pe (Let bind@(NonRec val_bdr rhs@(Let _ _)) body)
226 hsep [ptext SLIT("let {"), pBndr pe LetBind val_bdr, equals],
227 nest 2 (ppr_expr pe rhs),
231 ppr_expr pe (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
232 = hang (ptext SLIT("let {"))
233 2 (hsep [hang (hsep [pBndr pe LetBind val_bdr, equals])
239 -- general case (recursive case, too)
240 ppr_expr pe (Let bind expr)
241 = sep [hang (ptext keyword) 2 (ppr_bind pe bind),
242 hang (ptext SLIT("} in ")) 2 (ppr_expr pe expr)]
244 keyword = case bind of
245 Rec _ -> SLIT("__letrec {")
246 NonRec _ _ -> SLIT("let {")
248 ppr_expr pe (Note (SCC cc) expr)
249 = sep [pSCC pe cc, ppr_expr pe expr]
252 ppr_expr 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 pe (Note (Coerce to_ty from_ty) expr)
262 = sep [sep [ptext SLIT("__coerce"), nest 4 (pTy pe to_ty)],
263 ppr_parend_expr pe expr]
266 ppr_expr pe (Note InlineCall expr)
267 = ptext SLIT("__inline_call") <+> ppr_parend_expr pe expr
269 ppr_expr pe (Note InlineMe expr)
270 = ptext SLIT("__inline_me") <+> ppr_parend_expr pe expr
272 ppr_expr pe (Note (TermUsg u) expr)
274 if ifaceStyle sty then
277 (ppr u <+> ppr_expr pe expr) sty
279 ppr_case_pat pe con@(DataCon dc) args
281 = parens (hsep (punctuate comma (map ppr_bndr args))) <+> arrow
282 | isUnboxedTupleCon dc
283 = hsep [text "(# " <>
284 hsep (punctuate comma (map ppr_bndr args)) <>
288 ppr_bndr = pBndr pe CaseBind
290 ppr_case_pat pe con args
291 = pCon pe con <+> hsep (map ppr_bndr args) <+> arrow
293 ppr_bndr = pBndr pe CaseBind
295 ppr_arg pe (Type ty) = ptext SLIT("@") <+> pTy pe ty
296 ppr_arg pe expr = ppr_parend_expr pe expr
298 arrow = ptext SLIT("->")
301 Other printing bits-and-bobs used with the general @pprCoreBinding@
302 and @pprCoreExpr@ functions.
305 -- Used for printing dump info
306 pprCoreBinder LetBind binder
307 = vcat [sig, pragmas, ppr binder]
309 sig = pprTypedBinder binder
310 pragmas = ppIdInfo (idInfo binder)
312 -- Lambda bound type variables are preceded by "@"
313 pprCoreBinder LambdaBind bndr = pprTypedBinder bndr
315 -- Case bound things don't get a signature or a herald
316 pprCoreBinder CaseBind bndr = pprUntypedBinder bndr
318 -- Used for printing interface-file unfoldings
319 pprIfaceBinder CaseBind binder = pprUntypedBinder binder
320 pprIfaceBinder other binder = pprTypedBinder binder
322 pprUntypedBinder binder
323 | isTyVar binder = pprTyVarBndr binder
324 | otherwise = pprIdBndr binder
326 pprTypedBinder binder
327 | isTyVar binder = ptext SLIT("@") <+> pprTyVarBndr binder
328 | otherwise = pprIdBndr binder <+> dcolon <+> pprParendType (idType binder)
329 -- The space before the :: is important; it helps the lexer
330 -- when reading inferfaces. Otherwise it would lex "a::b" as one thing.
332 -- It's important that the type is parenthesised too, at least when
333 -- printing interfaces, because we get \ x::(a->b) y::(c->d) -> ...
335 -- When printing any Id binder in debug mode, we print its inline pragma
336 pprIdBndr id = ppr id <+> ifPprDebug (ppr (getInlinePragma id) <+> ppr (getIdDemandInfo id))
341 ppIdInfo :: IdInfo -> SDoc
344 ppFlavourInfo (flavourInfo info),
352 -- Inline pragma printed out with all binders; see PprCore.pprIdBndr
357 s = strictnessInfo info
366 pprCoreRules :: Id -> CoreRules -> SDoc
367 pprCoreRules var (Rules rules _) = vcat (map (pprCoreRule (Just var)) rules)
369 pprIfaceCoreRules :: CoreRules -> SDoc
370 pprIfaceCoreRules (Rules rules _) = vcat (map (pprCoreRule Nothing) rules)
372 pprCoreRule :: Maybe Id -> CoreRule -> SDoc
373 pprCoreRule maybe_fn (Rule name tpl_vars tpl_args rhs)
374 = doubleQuotes (ptext name) <+>
376 ptext SLIT("__forall") <+> braces (sep (map pprTypedBinder tpl_vars)),
377 nest 4 (pp_fn <+> sep (map pprIfaceArg tpl_args)),
378 nest 4 (ptext SLIT("=") <+> pprIfaceUnfolding rhs)
381 pp_fn = case maybe_fn of
383 Nothing -> empty -- Interface file