2 % (c) The AQUA Project, Glasgow University, 1996
4 %************************************************************************
6 \section[PprCore]{Printing of Core syntax, including for interfaces}
8 %************************************************************************
11 #include "HsVersions.h"
14 pprCoreExpr, pprIfaceUnfolding,
19 -- these are here to make the instances go in 0.26:
20 #if __GLASGOW_HASKELL__ <= 30
21 , GenCoreBinding, GenCoreExpr, GenCoreCaseAlts
22 , GenCoreCaseDefault, GenCoreArg
29 import CostCentre ( showCostCentre )
30 import Id ( idType, getIdInfo, getIdStrictness, isTupleCon,
31 nullIdEnv, SYN_IE(DataCon), GenId{-instances-},
34 import IdInfo ( ppIdInfo, StrictnessInfo(..) )
35 import Literal ( Literal{-instances-} )
36 import Name ( OccName, parenInCode )
37 import Outputable -- quite a few things
39 import PprType ( pprParendGenType, pprTyVarBndr, GenType{-instances-}, GenTyVar{-instance-} )
40 import PprStyle ( PprStyle(..), ifaceStyle )
42 import PrimOp ( PrimOp{-instances-} )
43 import TyVar ( GenTyVar{-instances-} )
44 import Unique ( Unique{-instances-} )
45 import Usage ( GenUsage{-instances-} )
46 import Util ( panic{-ToDo:rm-} )
49 %************************************************************************
51 \subsection{Public interfaces for Core printing (excluding instances)}
53 %************************************************************************
55 @pprCoreBinding@ and @pprCoreExpr@ let you give special printing
56 function for ``major'' val_bdrs (those next to equal signs :-),
57 ``minor'' ones (lambda-bound, case-bound), and bindees. They would
58 usually be called through some intermediary.
60 The binder/occ printers take the default ``homogenized'' (see
61 @PprEnv@...) @Doc@ and the binder/occ. They can either use the
62 homogenized one, or they can ignore it completely. In other words,
63 the things passed in act as ``hooks'', getting the last word on how to
66 @pprParendCoreExpr@ puts parens around non-atomic Core expressions.
69 pprCoreBinding :: PprStyle -> CoreBinding -> Doc
72 :: (Eq tyvar, Outputable tyvar,
73 Eq uvar, Outputable uvar,
77 -> (bndr -> Doc) -- to print "major" val_bdrs
78 -> (bndr -> Doc) -- to print "minor" val_bdrs
79 -> (occ -> Doc) -- to print bindees
80 -> GenCoreBinding bndr occ tyvar uvar
83 pprGenCoreBinding sty pbdr1 pbdr2 pocc bind
84 = ppr_bind (init_ppr_env sty (ppr sty) pbdr1 pbdr2 pocc) bind
86 init_ppr_env sty tvbndr pbdr1 pbdr2 pocc
88 (Just (ppr sty)) -- literals
89 (Just ppr_con) -- data cons
90 (Just ppr_prim) -- primops
91 (Just (\ cc -> text (showCostCentre sty True cc)))
92 (Just tvbndr) -- tyvar binders
93 (Just (ppr sty)) -- tyvar occs
94 (Just (ppr sty)) -- usage vars
95 (Just pbdr1) (Just pbdr2) (Just pocc) -- value vars
96 (Just (pprParendGenType sty)) -- types
97 (Just (ppr sty)) -- usages
100 ppr_con con = ppr sty con
102 {- [We now use Con {a,b,c} for Con expressions. SLPJ March 97.]
103 [We can't treat them as ordinary applications because the Con doesn't have
104 dictionaries in it, whereas the constructor Id does.]
107 -- ppr_con is used when printing Con expressions; we add a "!"
108 -- to distinguish them from ordinary applications. But not when
109 -- printing for interfaces, where they are treated as ordinary applications
110 ppr_con con | ifaceStyle sty = ppr sty con
111 | otherwise = ppr sty con <> char '!'
114 -- We add a "!" to distinguish Primitive applications from ordinary applications.
115 -- But not when printing for interfaces, where they are treated
116 -- as ordinary applications
117 ppr_prim prim | ifaceStyle sty = ppr sty prim
118 | otherwise = ppr sty prim <> char '!'
121 pprCoreBinding sty (NonRec binder expr)
122 = hang (hsep [pprBigCoreBinder sty binder, equals])
123 4 (pprCoreExpr sty (pprBigCoreBinder sty) (pprBabyCoreBinder sty) (ppr sty) expr)
125 pprCoreBinding sty (Rec binds)
126 = vcat [ptext SLIT("Rec {"),
127 vcat (map ppr_bind binds),
128 ptext SLIT("end Rec }")]
130 ppr_bind (binder, expr)
131 = hang (hsep [pprBigCoreBinder sty binder, equals])
132 4 (pprCoreExpr sty (pprBigCoreBinder sty) (pprBabyCoreBinder sty) (ppr sty) expr)
138 -> (Id -> Doc) -- to print "major" val_bdrs
139 -> (Id -> Doc) -- to print "minor" val_bdrs
140 -> (Id -> Doc) -- to print bindees
143 pprCoreExpr = pprGenCoreExpr
145 pprGenCoreExpr, pprParendCoreExpr
146 :: (Eq tyvar, Outputable tyvar,
147 Eq uvar, Outputable uvar,
151 -> (bndr -> Doc) -- to print "major" val_bdrs
152 -> (bndr -> Doc) -- to print "minor" val_bdrs
153 -> (occ -> Doc) -- to print bindees
154 -> GenCoreExpr bndr occ tyvar uvar
157 pprGenCoreExpr sty pbdr1 pbdr2 pocc expr
158 = ppr_expr (init_ppr_env sty (ppr sty) pbdr1 pbdr2 pocc) expr
160 pprParendCoreExpr sty pbdr1 pbdr2 pocc expr
164 Var _ -> id -- leave unchanged
166 _ -> parens -- wraps in parens
168 parenify (pprGenCoreExpr sty pbdr1 pbdr2 pocc expr)
170 -- Printer for unfoldings in interfaces
171 pprIfaceUnfolding :: CoreExpr -> Doc
172 pprIfaceUnfolding = ppr_expr env
174 env = init_ppr_env PprInterface (pprTyVarBndr PprInterface)
175 (pprTypedCoreBinder PprInterface)
179 ppr_core_arg sty pocc arg
180 = ppr_arg (init_ppr_env sty (ppr sty) pocc pocc pocc) arg
182 ppr_core_alts sty pbdr1 pbdr2 pocc alts
183 = ppr_alts (init_ppr_env sty (ppr sty) pbdr1 pbdr2 pocc) alts
185 ppr_core_default sty pbdr1 pbdr2 pocc deflt
186 = ppr_default (init_ppr_env sty (ppr sty) pbdr1 pbdr2 pocc) deflt
189 %************************************************************************
191 \subsection{Instance declarations for Core printing}
193 %************************************************************************
197 (Outputable bndr, Outputable occ, Eq tyvar, Outputable tyvar,
198 Eq uvar, Outputable uvar)
200 Outputable (GenCoreBinding bndr occ tyvar uvar) where
201 ppr sty bind = pprQuote sty $ \sty ->
202 pprGenCoreBinding sty (ppr sty) (ppr sty) (ppr sty) bind
205 (Outputable bndr, Outputable occ, Eq tyvar, Outputable tyvar,
206 Eq uvar, Outputable uvar)
208 Outputable (GenCoreExpr bndr occ tyvar uvar) where
209 ppr sty expr = pprQuote sty $ \sty ->
210 pprGenCoreExpr sty (ppr sty) (ppr sty) (ppr sty) expr
213 (Outputable occ, Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
215 Outputable (GenCoreArg occ tyvar uvar) where
216 ppr sty arg = pprQuote sty $ \sty ->
217 ppr_core_arg sty (ppr sty) arg
220 (Outputable bndr, Outputable occ, Eq tyvar, Outputable tyvar,
221 Eq uvar, Outputable uvar)
223 Outputable (GenCoreCaseAlts bndr occ tyvar uvar) where
224 ppr sty alts = pprQuote sty $ \sty ->
225 ppr_core_alts sty (ppr sty) (ppr sty) (ppr sty) alts
228 (Outputable bndr, Outputable occ, Eq tyvar, Outputable tyvar,
229 Eq uvar, Outputable uvar)
231 Outputable (GenCoreCaseDefault bndr occ tyvar uvar) where
232 ppr sty deflt = pprQuote sty $ \sty ->
233 ppr_core_default sty (ppr sty) (ppr sty) (ppr sty) deflt
236 %************************************************************************
238 \subsection{Workhorse routines (...????...)}
240 %************************************************************************
243 ppr_bind pe (NonRec val_bdr expr)
244 = hang (hsep [pMajBndr pe val_bdr, equals])
247 ppr_bind pe (Rec binds)
248 = vcat (map ppr_pair binds)
250 ppr_pair (val_bdr, expr)
251 = hang (hsep [pMajBndr pe val_bdr, equals])
252 4 (ppr_expr pe expr <> semi)
256 ppr_parend_expr pe expr
260 Var _ -> id -- leave unchanged
262 _ -> parens -- wraps in parens
264 parenify (ppr_expr pe expr)
268 ppr_expr pe (Var name) = pOcc pe name
269 ppr_expr pe (Lit lit) = pLit pe lit
271 ppr_expr pe (Con con args)
273 4 (braces $ sep (map (ppr_arg pe) args))
275 ppr_expr pe (Prim prim args)
276 = hang (pPrim pe prim)
277 4 (sep (map (ppr_arg pe) args))
279 ppr_expr pe expr@(Lam _ _)
281 (uvars, tyvars, vars, body) = collectBinders expr
283 hang (hsep [pp_vars SLIT("/u\\") (pUVar pe) uvars,
284 pp_vars SLIT("_/\\_") (pTyVarB pe) tyvars,
285 pp_vars SLIT("\\") (pMajBndr pe) vars])
288 pp_vars lam pp [] = empty
290 = hsep [ptext lam, hsep (map pp vs), ptext SLIT("->")]
292 ppr_expr pe expr@(App fun arg)
294 (final_fun, final_args) = go fun [arg]
295 go (App fun arg) args_so_far = go fun (arg:args_so_far)
296 go fun args_so_far = (fun, args_so_far)
298 hang (ppr_parend_expr pe final_fun) 4 (sep (map (ppr_arg pe) final_args))
300 ppr_expr pe (Case expr alts)
302 -- johan thinks that single case patterns should be on same line as case,
303 -- and no indent; all sane persons agree with him.
306 ppr_alt (AlgAlts [] (BindDefault n _)) = (<>) (pMinBndr pe n) ppr_arrow
307 ppr_alt (PrimAlts [] (BindDefault n _)) = (<>) (pMinBndr pe n) ppr_arrow
308 ppr_alt (PrimAlts ((l, _):[]) NoDefault)= (<>) (pLit pe l) ppr_arrow
309 ppr_alt (AlgAlts ((con, params, _):[]) NoDefault)
311 hsep (map (pMinBndr pe) params),
314 ppr_rhs (AlgAlts [] (BindDefault _ expr)) = ppr_expr pe expr
315 ppr_rhs (AlgAlts ((_,_,expr):[]) NoDefault) = ppr_expr pe expr
316 ppr_rhs (PrimAlts [] (BindDefault _ expr)) = ppr_expr pe expr
317 ppr_rhs (PrimAlts ((_,expr):[]) NoDefault) = ppr_expr pe expr
320 ppr_arrow = ptext SLIT(" ->")
323 [sep [pp_keyword, nest 4 (ppr_expr pe expr), text "of {", ppr_alt alts],
324 (<>) (ppr_rhs alts) (text ";}")]
326 | otherwise -- default "case" printing
328 [sep [pp_keyword, nest 4 (ppr_expr pe expr), ptext SLIT("of {")],
329 nest 2 (ppr_alts pe alts),
332 pp_keyword = case alts of
333 AlgAlts _ _ -> ptext SLIT("case")
334 PrimAlts _ _ -> ptext SLIT("case#")
336 -- special cases: let ... in let ...
337 -- ("disgusting" SLPJ)
339 ppr_expr pe (Let bind@(NonRec val_bdr rhs@(Let _ _)) body)
341 hsep [ptext SLIT("let {"), pMajBndr pe val_bdr, equals],
342 nest 2 (ppr_expr pe rhs),
346 ppr_expr pe (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
348 (hang (ptext SLIT("let {"))
349 2 (hsep [hang (hsep [pMajBndr pe val_bdr, equals])
351 ptext SLIT("} in")]))
354 -- general case (recursive case, too)
355 ppr_expr pe (Let bind expr)
356 = sep [hang (ptext keyword) 2 (ppr_bind pe bind),
357 hang (ptext SLIT("} in ")) 2 (ppr_expr pe expr)]
359 keyword = case bind of
360 Rec _ -> SLIT("_letrec_ {")
361 NonRec _ _ -> SLIT("let {")
363 ppr_expr pe (SCC cc expr)
364 = sep [hsep [ptext SLIT("_scc_"), pSCC pe cc],
365 ppr_parend_expr pe expr ]
367 ppr_expr pe (Coerce c ty expr)
368 = sep [pp_coerce c, pTy pe ty, ppr_expr pe expr]
370 pp_coerce (CoerceIn v) = (<>) (ptext SLIT("_coerce_in_ ")) (ppr (pStyle pe) v)
371 pp_coerce (CoerceOut v) = (<>) (ptext SLIT("_coerce_out_ ")) (ppr (pStyle pe) v)
373 only_one_alt (AlgAlts [] (BindDefault _ _)) = True
374 only_one_alt (AlgAlts (_:[]) NoDefault) = True
375 only_one_alt (PrimAlts [] (BindDefault _ _)) = True
376 only_one_alt (PrimAlts (_:[]) NoDefault) = True
377 only_one_alt _ = False
381 ppr_alts pe (AlgAlts alts deflt)
382 = vcat [ vcat (map ppr_alt alts), ppr_default pe deflt ]
384 ppr_arrow = ptext SLIT("->")
386 ppr_alt (con, params, expr)
387 = hang (if isTupleCon con then
388 hsep [parens (hsep (punctuate comma (map (pMinBndr pe) params))),
392 hsep (map (pMinBndr pe) params),
395 4 (ppr_expr pe expr <> semi)
397 ppr_alts pe (PrimAlts alts deflt)
398 = vcat [ vcat (map ppr_alt alts), ppr_default pe deflt ]
401 = hang (hsep [pLit pe lit, ptext SLIT("->")])
402 4 (ppr_expr pe expr <> semi)
406 ppr_default pe NoDefault = empty
408 ppr_default pe (BindDefault val_bdr expr)
409 = hang (hsep [pMinBndr pe val_bdr, ptext SLIT("->")])
410 4 (ppr_expr pe expr <> semi)
414 ppr_arg pe (LitArg lit) = pLit pe lit
415 ppr_arg pe (VarArg v) = pOcc pe v
416 ppr_arg pe (TyArg ty) = ptext SLIT("_@_ ") <> pTy pe ty
417 ppr_arg pe (UsageArg use) = pUse pe use
420 Other printing bits-and-bobs used with the general @pprCoreBinding@
421 and @pprCoreExpr@ functions.
424 pprBigCoreBinder sty binder
425 = vcat [sig, pragmas, ppr sty binder]
427 sig = ifnotPprShowAll sty (
428 hang (hsep [ppr sty binder, ppDcolon])
429 4 (ppr sty (idType binder)))
432 (ppIdInfo sty False{-no specs, thanks-} (getIdInfo binder))
434 pprBabyCoreBinder sty binder
435 = hsep [ppr sty binder, pp_strictness]
438 = case (getIdStrictness binder) of
439 NoStrictnessInfo -> empty
440 BottomGuaranteed -> ptext SLIT("{- _!_ -}")
441 StrictnessInfo xx _ ->
442 panic "PprCore:pp_strictness:StrictnessInfo:ToDo"
443 -- text ("{- " ++ (showList xx "") ++ " -}")
445 pprTypedCoreBinder sty binder
446 = hcat [ppr sty binder, ppDcolon, pprParendGenType sty (idType binder)]
448 ppDcolon = ptext SLIT(" :: ")
449 -- The space before the :: is important; it helps the lexer
450 -- when reading inferfaces. Otherwise it would lex "a::b" as one thing.