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
27 import Const ( Con(..), DataCon )
28 import DataCon ( isTupleCon, isUnboxedTupleCon )
29 import PprType ( pprParendType, pprTyVarBndr )
30 import SpecEnv ( specEnvToList )
35 %************************************************************************
37 \subsection{Public interfaces for Core printing (excluding instances)}
39 %************************************************************************
41 @pprCoreBinding@ and @pprCoreExpr@ let you give special printing
42 function for ``major'' val_bdrs (those next to equal signs :-),
43 ``minor'' ones (lambda-bound, case-bound), and bindees. They would
44 usually be called through some intermediary.
46 The binder/occ printers take the default ``homogenized'' (see
47 @PprEnv@...) @Doc@ and the binder/occ. They can either use the
48 homogenized one, or they can ignore it completely. In other words,
49 the things passed in act as ``hooks'', getting the last word on how to
52 @pprParendCoreExpr@ puts parens around non-atomic Core expressions.
54 Un-annotated core dumps
55 ~~~~~~~~~~~~~~~~~~~~~~~
57 pprCoreBindings :: [CoreBind] -> SDoc
58 pprCoreBinding :: CoreBind -> SDoc
59 pprCoreExpr :: CoreExpr -> SDoc
61 pprCoreBindings = pprTopBinds pprCoreEnv
62 pprCoreBinding = pprTopBind pprCoreEnv
63 pprCoreExpr = ppr_expr pprCoreEnv
65 pprCoreEnv = initCoreEnv pprCoreBinder
68 Printer for unfoldings in interfaces
69 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
71 pprIfaceUnfolding :: CoreExpr -> SDoc
72 pprIfaceUnfolding = ppr_expr pprIfaceEnv
74 pprIfaceEnv = initCoreEnv pprIfaceBinder
78 instance Outputable b => Outputable (Bind b) where
79 ppr bind = ppr_bind pprGenericEnv bind
81 instance Outputable b => Outputable (Expr b) where
82 ppr expr = ppr_expr pprGenericEnv expr
84 pprGenericEnv :: Outputable b => PprEnv b
85 pprGenericEnv = initCoreEnv (\site -> ppr)
88 %************************************************************************
90 \subsection{Instance declarations for Core printing}
92 %************************************************************************
98 (Just ppr) -- Constants
99 (Just pprCostCentreCore) -- Cost centres
101 (Just ppr) -- tyvar occs
102 (Just pprParendType) -- types
104 (Just pbdr) (Just ppr) -- value vars
105 -- Use pprIdBndr for this last one as a debugging device.
108 %************************************************************************
110 \subsection{The guts}
112 %************************************************************************
115 pprTopBinds pe binds = vcat (map (pprTopBind pe) binds)
117 pprTopBind pe (NonRec binder expr)
118 = sep [ppr_binding_pe pe (binder,expr)] $$ text ""
120 pprTopBind pe (Rec binds)
121 = vcat [ptext SLIT("Rec {"),
122 vcat (map (ppr_binding_pe pe) binds),
123 ptext SLIT("end Rec }"),
128 ppr_bind :: PprEnv b -> Bind b -> SDoc
130 ppr_bind pe (NonRec val_bdr expr) = ppr_binding_pe pe (val_bdr, expr)
131 ppr_bind pe (Rec binds) = vcat (map pp binds)
133 pp bind = ppr_binding_pe pe bind <> semi
135 ppr_binding_pe :: PprEnv b -> (b, Expr b) -> SDoc
136 ppr_binding_pe pe (val_bdr, expr)
137 = sep [pBndr pe LetBind val_bdr,
138 nest 2 (equals <+> ppr_expr pe expr)]
142 ppr_parend_expr pe expr
143 | no_parens = ppr_expr pe expr
144 | otherwise = parens (ppr_expr pe expr)
146 no_parens = case expr of
149 Con (DataCon dc) _ -> isTupleCon dc
154 ppr_expr :: PprEnv b -> Expr b -> SDoc
156 ppr_expr pe (Type ty) = ptext SLIT("TYPE") <+> ppr ty -- Wierd
158 ppr_expr pe (Var name) = pOcc pe name
160 ppr_expr pe (Con con [])
161 = ppr con -- Nullary constructors too
163 ppr_expr pe (Con (DataCon dc) args)
164 -- Drop the type arguments and print in (a,b,c) notation
166 = parens (sep (punctuate comma (map (ppr_arg pe) (dropWhile isTypeArg args))))
167 | isUnboxedTupleCon dc
169 hsep (punctuate comma (map (ppr_arg pe) (dropWhile isTypeArg args))) <>
172 ppr_expr pe (Con con args)
173 = pCon pe con <+> (braces $ sep (map (ppr_arg pe) args))
175 ppr_expr pe expr@(Lam _ _)
177 (bndrs, body) = collectBinders expr
179 hang (ptext SLIT("\\") <+> sep (map (pBndr pe LambdaBind) bndrs) <+> arrow)
182 ppr_expr pe expr@(App fun arg)
184 (final_fun, final_args) = go fun [arg]
185 go (App fun arg) args_so_far = go fun (arg:args_so_far)
186 go fun args_so_far = (fun, args_so_far)
188 hang (ppr_parend_expr pe final_fun) 4 (sep (map (ppr_arg pe) final_args))
190 ppr_expr pe (Case expr var [(con,args,rhs)])
191 = sep [sep [ptext SLIT("case") <+> ppr_expr pe expr,
192 hsep [ptext SLIT("of"),
195 ppr_case_pat pe con args
201 ppr_bndr = pBndr pe CaseBind
203 ppr_expr pe (Case expr var alts)
204 = sep [sep [ptext SLIT("case") <+> ppr_expr pe expr,
205 ptext SLIT("of") <+> ppr_bndr var <+> char '{'],
206 nest 4 (sep (punctuate semi (map ppr_alt alts))),
210 ppr_bndr = pBndr pe CaseBind
212 ppr_alt (con, args, rhs) = hang (ppr_case_pat pe con args)
215 -- special cases: let ... in let ...
216 -- ("disgusting" SLPJ)
218 ppr_expr pe (Let bind@(NonRec val_bdr rhs@(Let _ _)) body)
220 hsep [ptext SLIT("let {"), pBndr pe LetBind val_bdr, equals],
221 nest 2 (ppr_expr pe rhs),
225 ppr_expr pe (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
226 = hang (ptext SLIT("let {"))
227 2 (hsep [hang (hsep [pBndr pe LetBind val_bdr, equals])
233 -- general case (recursive case, too)
234 ppr_expr pe (Let bind expr)
235 = sep [hang (ptext keyword) 2 (ppr_bind pe bind),
236 hang (ptext SLIT("} in ")) 2 (ppr_expr pe expr)]
238 keyword = case bind of
239 Rec _ -> SLIT("__letrec {")
240 NonRec _ _ -> SLIT("let {")
242 ppr_expr pe (Note (SCC cc) expr)
243 = sep [pSCC pe cc, ppr_expr pe expr]
246 ppr_expr pe (Note (Coerce to_ty from_ty) expr)
248 if debugStyle sty && not (ifaceStyle sty) then
249 sep [hsep [ptext SLIT("__coerce"), pTy pe to_ty, pTy pe from_ty],
250 ppr_parend_expr pe expr] sty
252 sep [hsep [ptext SLIT("__coerce"), pTy pe to_ty],
253 ppr_parend_expr pe expr] sty
255 ppr_expr pe (Note (Coerce to_ty from_ty) expr)
256 = sep [hsep [ptext SLIT("__coerce"), pTy pe to_ty],
257 ppr_parend_expr pe expr]
260 ppr_expr pe (Note InlineCall expr)
261 = ptext SLIT("__inline") <+> ppr_parend_expr pe expr
263 ppr_case_pat pe con@(DataCon dc) args
265 = parens (hsep (punctuate comma (map ppr_bndr args))) <+> arrow
266 | isUnboxedTupleCon dc
267 = hsep [text "(# " <>
268 hsep (punctuate comma (map ppr_bndr args)) <>
272 ppr_bndr = pBndr pe CaseBind
274 ppr_case_pat pe con args
275 = pCon pe con <+> hsep (map ppr_bndr args) <+> arrow
277 ppr_bndr = pBndr pe CaseBind
279 ppr_arg pe (Type ty) = ptext SLIT("@") <+> pTy pe ty
280 ppr_arg pe expr = ppr_parend_expr pe expr
282 arrow = ptext SLIT("->")
285 Other printing bits-and-bobs used with the general @pprCoreBinding@
286 and @pprCoreExpr@ functions.
289 -- Used for printing dump info
290 pprCoreBinder LetBind binder
291 = vcat [sig, pragmas, ppr binder]
293 sig = pprTypedBinder binder
294 pragmas = ppIdInfo (idInfo binder)
296 -- Lambda bound type variables are preceded by "@"
297 pprCoreBinder LambdaBind bndr = pprTypedBinder bndr
299 -- Case bound things don't get a signature or a herald
300 pprCoreBinder CaseBind bndr = pprUntypedBinder bndr
302 -- Used for printing interface-file unfoldings
303 pprIfaceBinder CaseBind binder = pprUntypedBinder binder
304 pprIfaceBinder other binder = pprTypedBinder binder
306 pprUntypedBinder binder
307 | isTyVar binder = pprTyVarBndr binder
308 | otherwise = pprIdBndr binder
310 pprTypedBinder binder
311 | isTyVar binder = ptext SLIT("@") <+> pprTyVarBndr binder
312 | otherwise = pprIdBndr binder <+> dcolon <+> pprParendType (idType binder)
313 -- The space before the :: is important; it helps the lexer
314 -- when reading inferfaces. Otherwise it would lex "a::b" as one thing.
316 -- It's important that the type is parenthesised too, at least when
317 -- printing interfaces, because we get \ x::(a->b) y::(c->d) -> ...
319 -- When printing any Id binder in debug mode, we print its inline pragma
320 pprIdBndr id = ppr id <+> ifPprDebug (ppr (getInlinePragma id) <+> ppr (getIdDemandInfo id))
325 ppIdInfo :: IdInfo -> SDoc
334 -- Inline pragma printed out with all binders; see PprCore.pprIdBndr
339 s = strictnessInfo info
347 = vcat (map pp_item (specEnvToList spec_env))
349 pp_item (tyvars, tys, rhs) = hsep [ptext SLIT("__P"),
350 hsep (map pprParendType tys),
354 (_, body) = collectBinders rhs
355 (head, _) = collectArgs body