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 ( ppIdInfo )
23 import Const ( Con(..), DataCon )
24 import DataCon ( isTupleCon, isUnboxedTupleCon )
25 import PprType ( pprParendType, pprTyVarBndr )
30 %************************************************************************
32 \subsection{Public interfaces for Core printing (excluding instances)}
34 %************************************************************************
36 @pprCoreBinding@ and @pprCoreExpr@ let you give special printing
37 function for ``major'' val_bdrs (those next to equal signs :-),
38 ``minor'' ones (lambda-bound, case-bound), and bindees. They would
39 usually be called through some intermediary.
41 The binder/occ printers take the default ``homogenized'' (see
42 @PprEnv@...) @Doc@ and the binder/occ. They can either use the
43 homogenized one, or they can ignore it completely. In other words,
44 the things passed in act as ``hooks'', getting the last word on how to
47 @pprParendCoreExpr@ puts parens around non-atomic Core expressions.
49 Un-annotated core dumps
50 ~~~~~~~~~~~~~~~~~~~~~~~
52 pprCoreBindings :: [CoreBind] -> SDoc
53 pprCoreBinding :: CoreBind -> SDoc
54 pprCoreExpr :: CoreExpr -> SDoc
56 pprCoreBindings = pprTopBinds pprCoreEnv
57 pprCoreBinding = pprTopBind pprCoreEnv
58 pprCoreExpr = ppr_expr pprCoreEnv
60 pprCoreEnv = initCoreEnv pprCoreBinder
63 Printer for unfoldings in interfaces
64 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
66 pprIfaceUnfolding :: CoreExpr -> SDoc
67 pprIfaceUnfolding = ppr_expr pprIfaceEnv
69 pprIfaceEnv = initCoreEnv pprIfaceBinder
73 instance Outputable b => Outputable (Bind b) where
74 ppr bind = ppr_bind pprGenericEnv bind
76 instance Outputable b => Outputable (Expr b) where
77 ppr expr = ppr_expr pprGenericEnv expr
79 pprGenericEnv :: Outputable b => PprEnv b
80 pprGenericEnv = initCoreEnv (\site -> ppr)
83 %************************************************************************
85 \subsection{Instance declarations for Core printing}
87 %************************************************************************
93 (Just ppr) -- Constants
94 (Just pprCostCentreCore) -- Cost centres
96 (Just ppr) -- tyvar occs
97 (Just pprParendType) -- types
99 (Just pbdr) (Just pprIdBndr) -- value vars
100 -- The pprIdBndr part here is a temporary debugging aid
101 -- Revert to ppr if it gets tiresome
104 %************************************************************************
106 \subsection{The guts}
108 %************************************************************************
111 pprTopBinds pe binds = vcat (map (pprTopBind pe) binds)
113 pprTopBind pe (NonRec binder expr)
114 = sep [ppr_binding_pe pe (binder,expr)] $$ text ""
116 pprTopBind pe (Rec binds)
117 = vcat [ptext SLIT("Rec {"),
118 vcat (map (ppr_binding_pe pe) binds),
119 ptext SLIT("end Rec }"),
124 ppr_bind :: PprEnv b -> Bind b -> SDoc
126 ppr_bind pe (NonRec val_bdr expr) = ppr_binding_pe pe (val_bdr, expr)
127 ppr_bind pe (Rec binds) = vcat (map pp binds)
129 pp bind = ppr_binding_pe pe bind <> semi
131 ppr_binding_pe :: PprEnv b -> (b, Expr b) -> SDoc
132 ppr_binding_pe pe (val_bdr, expr)
133 = sep [pBndr pe LetBind val_bdr,
134 nest 2 (equals <+> ppr_expr pe expr)]
138 ppr_parend_expr pe expr
139 | no_parens = ppr_expr pe expr
140 | otherwise = parens (ppr_expr pe expr)
142 no_parens = case expr of
145 Con (DataCon dc) _ -> isTupleCon dc
150 ppr_expr :: PprEnv b -> Expr b -> SDoc
152 ppr_expr pe (Type ty) = ptext SLIT("TYPE") <+> ppr ty -- Wierd
154 ppr_expr pe (Var name) = pOcc pe name
156 ppr_expr pe (Con con [])
157 = ppr con -- Nullary constructors too
159 ppr_expr pe (Con (DataCon dc) args)
160 -- Drop the type arguments and print in (a,b,c) notation
162 = parens (sep (punctuate comma (map (ppr_arg pe) (dropWhile isTypeArg args))))
163 | isUnboxedTupleCon dc
165 hsep (punctuate comma (map (ppr_arg pe) (dropWhile isTypeArg args))) <>
168 ppr_expr pe (Con con args)
169 = pCon pe con <+> (braces $ sep (map (ppr_arg pe) args))
171 ppr_expr pe expr@(Lam _ _)
173 (bndrs, body) = collectBinders expr
175 hang (ptext SLIT("\\") <+> sep (map (pBndr pe LambdaBind) bndrs) <+> arrow)
178 ppr_expr pe expr@(App fun arg)
180 (final_fun, final_args) = go fun [arg]
181 go (App fun arg) args_so_far = go fun (arg:args_so_far)
182 go fun args_so_far = (fun, args_so_far)
184 hang (ppr_parend_expr pe final_fun) 4 (sep (map (ppr_arg pe) final_args))
186 ppr_expr pe (Case expr var [(con,args,rhs)])
187 = sep [sep [ptext SLIT("case") <+> ppr_expr pe expr,
188 hsep [ptext SLIT("of"),
191 ppr_case_pat pe con args
197 ppr_bndr = pBndr pe CaseBind
199 ppr_expr pe (Case expr var alts)
200 = sep [sep [ptext SLIT("case") <+> ppr_expr pe expr,
201 ptext SLIT("of") <+> ppr_bndr var <+> char '{'],
202 nest 4 (sep (punctuate semi (map ppr_alt alts))),
206 ppr_bndr = pBndr pe CaseBind
208 ppr_alt (con, args, rhs) = hang (ppr_case_pat pe con args)
211 -- special cases: let ... in let ...
212 -- ("disgusting" SLPJ)
214 ppr_expr pe (Let bind@(NonRec val_bdr rhs@(Let _ _)) body)
216 hsep [ptext SLIT("let {"), pBndr pe LetBind val_bdr, equals],
217 nest 2 (ppr_expr pe rhs),
221 ppr_expr pe (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
222 = hang (ptext SLIT("let {"))
223 2 (hsep [hang (hsep [pBndr pe LetBind val_bdr, equals])
229 -- general case (recursive case, too)
230 ppr_expr pe (Let bind expr)
231 = sep [hang (ptext keyword) 2 (ppr_bind pe bind),
232 hang (ptext SLIT("} in ")) 2 (ppr_expr pe expr)]
234 keyword = case bind of
235 Rec _ -> SLIT("__letrec {")
236 NonRec _ _ -> SLIT("let {")
238 ppr_expr pe (Note (SCC cc) expr)
239 = sep [pSCC pe cc, ppr_expr pe expr]
242 ppr_expr pe (Note (Coerce to_ty from_ty) expr)
244 if debugStyle sty && not (ifaceStyle sty) then
245 sep [hsep [ptext SLIT("__coerce"), pTy pe to_ty, pTy pe from_ty],
246 ppr_parend_expr pe expr] sty
248 sep [hsep [ptext SLIT("__coerce"), pTy pe to_ty],
249 ppr_parend_expr pe expr] sty
251 ppr_expr pe (Note (Coerce to_ty from_ty) expr)
252 = sep [hsep [ptext SLIT("__coerce"), pTy pe to_ty],
253 ppr_parend_expr pe expr]
256 ppr_expr pe (Note InlineCall expr)
257 = ptext SLIT("__inline") <+> ppr_parend_expr pe expr
259 ppr_case_pat pe con@(DataCon dc) args
261 = parens (hsep (punctuate comma (map ppr_bndr args))) <+> arrow
262 | isUnboxedTupleCon dc
263 = hsep [text "(# " <>
264 hsep (punctuate comma (map ppr_bndr args)) <>
268 ppr_bndr = pBndr pe CaseBind
270 ppr_case_pat pe con args
271 = pCon pe con <+> hsep (map ppr_bndr args) <+> arrow
273 ppr_bndr = pBndr pe CaseBind
275 ppr_arg pe (Type ty) = ptext SLIT("@") <+> pTy pe ty
276 ppr_arg pe expr = ppr_parend_expr pe expr
278 arrow = ptext SLIT("->")
281 Other printing bits-and-bobs used with the general @pprCoreBinding@
282 and @pprCoreExpr@ functions.
285 -- Used for printing dump info
286 pprCoreBinder LetBind binder
287 = vcat [sig, pragmas, ppr binder]
289 sig = pprTypedBinder binder
290 pragmas = ppIdInfo (idInfo binder)
292 -- Lambda bound type variables are preceded by "@"
293 pprCoreBinder LambdaBind bndr = pprTypedBinder bndr
295 -- Case bound things don't get a signature or a herald
296 pprCoreBinder CaseBind bndr = pprUntypedBinder bndr
298 -- Used for printing interface-file unfoldings
299 pprIfaceBinder CaseBind binder = pprUntypedBinder binder
300 pprIfaceBinder other binder = pprTypedBinder binder
302 pprUntypedBinder binder
303 | isTyVar binder = pprTyVarBndr binder
304 | otherwise = pprIdBndr binder
306 pprTypedBinder binder
307 | isTyVar binder = ptext SLIT("@") <+> pprTyVarBndr binder
308 | otherwise = pprIdBndr binder <+> dcolon <+> pprParendType (idType binder)
309 -- The space before the :: is important; it helps the lexer
310 -- when reading inferfaces. Otherwise it would lex "a::b" as one thing.
312 -- It's important that the type is parenthesised too, at least when
313 -- printing interfaces, because we get \ x::(a->b) y::(c->d) -> ...
315 -- When printing any Id binder in debug mode, we print its inline pragma
316 pprIdBndr id = ppr id <+> ifPprDebug (ppr (getInlinePragma id) <+> ppr (getIdDemandInfo id))