2 % (c) The AQUA Project, Glasgow University, 1996-1998
4 %************************************************************************
6 \section[PprCore]{Printing of Core syntax, including for interfaces}
8 %************************************************************************
12 pprCoreExpr, pprIfaceUnfolding,
13 pprCoreBinding, pprCoreBindings, pprIdBndr
16 #include "HsVersions.h"
19 import CostCentre ( pprCostCentreCore )
20 import Id ( idType, idInfo, getInlinePragma, getIdDemandInfo, Id )
21 import Var ( isTyVar )
22 import IdInfo ( IdInfo,
23 arityInfo, ppArityInfo,
24 demandInfo, updateInfo, ppUpdateInfo, specInfo,
25 strictnessInfo, ppStrictnessInfo, cafInfo, ppCafInfo,
28 import Const ( Con(..), DataCon )
29 import DataCon ( isTupleCon, isUnboxedTupleCon )
30 import PprType ( pprParendType, pprTyVarBndr )
31 import SpecEnv ( specEnvToList )
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
62 pprCoreBindings = pprTopBinds pprCoreEnv
63 pprCoreBinding = pprTopBind pprCoreEnv
64 pprCoreExpr = ppr_expr pprCoreEnv
66 pprCoreEnv = initCoreEnv pprCoreBinder
69 Printer for unfoldings in interfaces
70 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72 pprIfaceUnfolding :: CoreExpr -> SDoc
73 pprIfaceUnfolding = ppr_expr pprIfaceEnv
75 pprIfaceEnv = initCoreEnv pprIfaceBinder
79 instance Outputable b => Outputable (Bind b) where
80 ppr bind = ppr_bind pprGenericEnv bind
82 instance Outputable b => Outputable (Expr b) where
83 ppr expr = ppr_expr pprGenericEnv expr
85 pprGenericEnv :: Outputable b => PprEnv b
86 pprGenericEnv = initCoreEnv (\site -> ppr)
89 %************************************************************************
91 \subsection{Instance declarations for Core printing}
93 %************************************************************************
99 (Just ppr) -- Constants
100 (Just pprCostCentreCore) -- Cost centres
102 (Just ppr) -- tyvar occs
103 (Just pprParendType) -- types
105 (Just pbdr) (Just ppr) -- value vars
106 -- Use pprIdBndr for this last one as a debugging device.
109 %************************************************************************
111 \subsection{The guts}
113 %************************************************************************
116 pprTopBinds pe binds = vcat (map (pprTopBind pe) binds)
118 pprTopBind pe (NonRec binder expr)
119 = sep [ppr_binding_pe pe (binder,expr)] $$ text ""
121 pprTopBind pe (Rec binds)
122 = vcat [ptext SLIT("Rec {"),
123 vcat (map (ppr_binding_pe pe) binds),
124 ptext SLIT("end Rec }"),
129 ppr_bind :: PprEnv b -> Bind b -> SDoc
131 ppr_bind pe (NonRec val_bdr expr) = ppr_binding_pe pe (val_bdr, expr)
132 ppr_bind pe (Rec binds) = vcat (map pp binds)
134 pp bind = ppr_binding_pe pe bind <> semi
136 ppr_binding_pe :: PprEnv b -> (b, Expr b) -> SDoc
137 ppr_binding_pe pe (val_bdr, expr)
138 = sep [pBndr pe LetBind val_bdr,
139 nest 2 (equals <+> ppr_expr pe expr)]
143 ppr_parend_expr pe expr
144 | no_parens = ppr_expr pe expr
145 | otherwise = parens (ppr_expr pe expr)
147 no_parens = case expr of
150 Con (DataCon dc) _ -> isTupleCon dc
155 ppr_expr :: PprEnv b -> Expr b -> SDoc
157 ppr_expr pe (Type ty) = ptext SLIT("TYPE") <+> ppr ty -- Wierd
159 ppr_expr pe (Var name) = pOcc pe name
161 ppr_expr pe (Con con [])
162 = ppr con -- Nullary constructors too
164 ppr_expr pe (Con (DataCon dc) args)
165 -- Drop the type arguments and print in (a,b,c) notation
167 = parens (sep (punctuate comma (map (ppr_arg pe) (dropWhile isTypeArg args))))
168 | isUnboxedTupleCon dc
170 hsep (punctuate comma (map (ppr_arg pe) (dropWhile isTypeArg args))) <>
173 ppr_expr pe (Con con args)
174 = pCon pe con <+> (braces $ sep (map (ppr_arg pe) args))
176 ppr_expr pe expr@(Lam _ _)
178 (bndrs, body) = collectBinders expr
180 hang (ptext SLIT("\\") <+> sep (map (pBndr pe LambdaBind) bndrs) <+> arrow)
183 ppr_expr pe expr@(App fun arg)
185 (final_fun, final_args) = go fun [arg]
186 go (App fun arg) args_so_far = go fun (arg:args_so_far)
187 go fun args_so_far = (fun, args_so_far)
189 hang (ppr_parend_expr pe final_fun) 4 (sep (map (ppr_arg pe) final_args))
191 ppr_expr pe (Case expr var [(con,args,rhs)])
192 = sep [sep [ptext SLIT("case") <+> ppr_expr pe expr,
193 hsep [ptext SLIT("of"),
196 ppr_case_pat pe con args
202 ppr_bndr = pBndr pe CaseBind
204 ppr_expr pe (Case expr var alts)
205 = sep [sep [ptext SLIT("case") <+> ppr_expr pe expr,
206 ptext SLIT("of") <+> ppr_bndr var <+> char '{'],
207 nest 4 (sep (punctuate semi (map ppr_alt alts))),
211 ppr_bndr = pBndr pe CaseBind
213 ppr_alt (con, args, rhs) = hang (ppr_case_pat pe con args)
216 -- special cases: let ... in let ...
217 -- ("disgusting" SLPJ)
219 ppr_expr pe (Let bind@(NonRec val_bdr rhs@(Let _ _)) body)
221 hsep [ptext SLIT("let {"), pBndr pe LetBind val_bdr, equals],
222 nest 2 (ppr_expr pe rhs),
226 ppr_expr pe (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
227 = hang (ptext SLIT("let {"))
228 2 (hsep [hang (hsep [pBndr pe LetBind val_bdr, equals])
234 -- general case (recursive case, too)
235 ppr_expr pe (Let bind expr)
236 = sep [hang (ptext keyword) 2 (ppr_bind pe bind),
237 hang (ptext SLIT("} in ")) 2 (ppr_expr pe expr)]
239 keyword = case bind of
240 Rec _ -> SLIT("__letrec {")
241 NonRec _ _ -> SLIT("let {")
243 ppr_expr pe (Note (SCC cc) expr)
244 = sep [pSCC pe cc, ppr_expr pe expr]
247 ppr_expr pe (Note (Coerce to_ty from_ty) expr)
249 if debugStyle sty && not (ifaceStyle sty) then
250 sep [hsep [ptext SLIT("__coerce"), pTy pe to_ty, pTy pe from_ty],
251 ppr_parend_expr pe expr] sty
253 sep [hsep [ptext SLIT("__coerce"), pTy pe to_ty],
254 ppr_parend_expr pe expr] sty
256 ppr_expr pe (Note (Coerce to_ty from_ty) expr)
257 = sep [hsep [ptext SLIT("__coerce"), pTy pe to_ty],
258 ppr_parend_expr pe expr]
261 ppr_expr pe (Note InlineCall expr)
262 = ptext SLIT("__inline") <+> ppr_parend_expr pe expr
264 ppr_expr pe (Note (TermUsg u) expr)
266 if ifaceStyle sty then
269 (ppr u <+> ppr_expr pe expr) sty
271 ppr_case_pat pe con@(DataCon dc) args
273 = parens (hsep (punctuate comma (map ppr_bndr args))) <+> arrow
274 | isUnboxedTupleCon dc
275 = hsep [text "(# " <>
276 hsep (punctuate comma (map ppr_bndr args)) <>
280 ppr_bndr = pBndr pe CaseBind
282 ppr_case_pat pe con args
283 = pCon pe con <+> hsep (map ppr_bndr args) <+> arrow
285 ppr_bndr = pBndr pe CaseBind
287 ppr_arg pe (Type ty) = ptext SLIT("@") <+> pTy pe ty
288 ppr_arg pe expr = ppr_parend_expr pe expr
290 arrow = ptext SLIT("->")
293 Other printing bits-and-bobs used with the general @pprCoreBinding@
294 and @pprCoreExpr@ functions.
297 -- Used for printing dump info
298 pprCoreBinder LetBind binder
299 = vcat [sig, pragmas, ppr binder]
301 sig = pprTypedBinder binder
302 pragmas = ppIdInfo (idInfo binder)
304 -- Lambda bound type variables are preceded by "@"
305 pprCoreBinder LambdaBind bndr = pprTypedBinder bndr
307 -- Case bound things don't get a signature or a herald
308 pprCoreBinder CaseBind bndr = pprUntypedBinder bndr
310 -- Used for printing interface-file unfoldings
311 pprIfaceBinder CaseBind binder = pprUntypedBinder binder
312 pprIfaceBinder other binder = pprTypedBinder binder
314 pprUntypedBinder binder
315 | isTyVar binder = pprTyVarBndr binder
316 | otherwise = pprIdBndr binder
318 pprTypedBinder binder
319 | isTyVar binder = ptext SLIT("@") <+> pprTyVarBndr binder
320 | otherwise = pprIdBndr binder <+> dcolon <+> pprParendType (idType binder)
321 -- The space before the :: is important; it helps the lexer
322 -- when reading inferfaces. Otherwise it would lex "a::b" as one thing.
324 -- It's important that the type is parenthesised too, at least when
325 -- printing interfaces, because we get \ x::(a->b) y::(c->d) -> ...
327 -- When printing any Id binder in debug mode, we print its inline pragma
328 pprIdBndr id = ppr id <+> ifPprDebug (ppr (getInlinePragma id) <+> ppr (getIdDemandInfo id))
333 ppIdInfo :: IdInfo -> SDoc
343 -- Inline pragma printed out with all binders; see PprCore.pprIdBndr
348 s = strictnessInfo info
357 = vcat (map pp_item (specEnvToList spec_env))
359 pp_item (tyvars, tys, rhs) = hsep [ptext SLIT("__P"),
360 hsep (map pprParendType tys),
364 (_, body) = collectBinders rhs
365 (head, _) = collectArgs body