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-}
33 import IdInfo ( ppIdInfo, StrictnessInfo(..) )
34 import Literal ( Literal{-instances-} )
35 import Name ( OccName, parenInCode )
36 import Outputable -- quite a few things
38 import PprType ( pprParendGenType, pprTyVarBndr, GenType{-instances-}, GenTyVar{-instance-} )
39 import PprStyle ( PprStyle(..), ifaceStyle )
41 import PrimOp ( PrimOp{-instances-} )
42 import TyVar ( GenTyVar{-instances-} )
43 import Unique ( Unique{-instances-} )
44 import Usage ( GenUsage{-instances-} )
45 import Util ( panic{-ToDo:rm-} )
48 %************************************************************************
50 \subsection{Public interfaces for Core printing (excluding instances)}
52 %************************************************************************
54 @pprCoreBinding@ and @pprCoreExpr@ let you give special printing
55 function for ``major'' val_bdrs (those next to equal signs :-),
56 ``minor'' ones (lambda-bound, case-bound), and bindees. They would
57 usually be called through some intermediary.
59 The binder/occ printers take the default ``homogenized'' (see
60 @PprEnv@...) @Pretty@ and the binder/occ. They can either use the
61 homogenized one, or they can ignore it completely. In other words,
62 the things passed in act as ``hooks'', getting the last word on how to
65 @pprParendCoreExpr@ puts parens around non-atomic Core expressions.
68 pprCoreBinding :: PprStyle -> CoreBinding -> Pretty
71 :: (Eq tyvar, Outputable tyvar,
72 Eq uvar, Outputable uvar,
76 -> (bndr -> Pretty) -- to print "major" val_bdrs
77 -> (bndr -> Pretty) -- to print "minor" val_bdrs
78 -> (occ -> Pretty) -- to print bindees
79 -> GenCoreBinding bndr occ tyvar uvar
82 pprGenCoreBinding sty pbdr1 pbdr2 pocc bind
83 = ppr_bind (init_ppr_env sty (ppr sty) pbdr1 pbdr2 pocc) bind
85 init_ppr_env sty tvbndr pbdr1 pbdr2 pocc
87 (Just (ppr sty)) -- literals
88 (Just ppr_con) -- data cons
89 (Just ppr_prim) -- primops
90 (Just (\ cc -> ppStr (showCostCentre sty True cc)))
91 (Just tvbndr) -- tyvar binders
92 (Just (ppr sty)) -- tyvar occs
93 (Just (ppr sty)) -- usage vars
94 (Just pbdr1) (Just pbdr2) (Just pocc) -- value vars
95 (Just (pprParendGenType sty)) -- types
96 (Just (ppr sty)) -- usages
99 ppr_con con = ppr sty con
101 {- [We now use Con {a,b,c} for Con expressions. SLPJ March 97.]
102 [We can't treat them as ordinary applications because the Con doesn't have
103 dictionaries in it, whereas the constructor Id does.]
106 -- ppr_con is used when printing Con expressions; we add a "!"
107 -- to distinguish them from ordinary applications. But not when
108 -- printing for interfaces, where they are treated as ordinary applications
109 ppr_con con | ifaceStyle sty = ppr sty con
110 | otherwise = ppr sty con `ppBeside` ppChar '!'
113 -- We add a "!" to distinguish Primitive applications from ordinary applications.
114 -- But not when printing for interfaces, where they are treated
115 -- as ordinary applications
116 ppr_prim prim | ifaceStyle sty = ppr sty prim
117 | otherwise = ppr sty prim `ppBeside` ppChar '!'
120 pprCoreBinding sty (NonRec binder expr)
121 = ppHang (ppCat [pprBigCoreBinder sty binder, ppEquals])
122 4 (pprCoreExpr sty (pprBigCoreBinder sty) (pprBabyCoreBinder sty) (ppr sty) expr)
124 pprCoreBinding sty (Rec binds)
125 = ppAboves [ifPprDebug sty (ppPStr SLIT("{- plain Rec -}")),
126 ppAboves (map ppr_bind binds),
127 ifPprDebug sty (ppPStr SLIT("{- end plain Rec -}"))]
129 ppr_bind (binder, expr)
130 = ppHang (ppCat [pprBigCoreBinder sty binder, ppEquals])
131 4 (pprCoreExpr sty (pprBigCoreBinder sty) (pprBabyCoreBinder sty) (ppr sty) expr)
137 -> (Id -> Pretty) -- to print "major" val_bdrs
138 -> (Id -> Pretty) -- to print "minor" val_bdrs
139 -> (Id -> Pretty) -- to print bindees
142 pprCoreExpr = pprGenCoreExpr
144 pprGenCoreExpr, pprParendCoreExpr
145 :: (Eq tyvar, Outputable tyvar,
146 Eq uvar, Outputable uvar,
150 -> (bndr -> Pretty) -- to print "major" val_bdrs
151 -> (bndr -> Pretty) -- to print "minor" val_bdrs
152 -> (occ -> Pretty) -- to print bindees
153 -> GenCoreExpr bndr occ tyvar uvar
156 pprGenCoreExpr sty pbdr1 pbdr2 pocc expr
157 = ppr_expr (init_ppr_env sty (ppr sty) pbdr1 pbdr2 pocc) expr
159 pprParendCoreExpr sty pbdr1 pbdr2 pocc expr
163 Var _ -> id -- leave unchanged
165 _ -> ppParens -- wraps in parens
167 parenify (pprGenCoreExpr sty pbdr1 pbdr2 pocc expr)
169 -- Printer for unfoldings in interfaces
170 pprIfaceUnfolding :: CoreExpr -> Pretty
171 pprIfaceUnfolding = ppr_expr env
173 env = init_ppr_env PprInterface (pprTyVarBndr PprInterface)
174 (pprTypedCoreBinder PprInterface)
178 ppr_core_arg sty pocc arg
179 = ppr_arg (init_ppr_env sty (ppr sty) pocc pocc pocc) arg
181 ppr_core_alts sty pbdr1 pbdr2 pocc alts
182 = ppr_alts (init_ppr_env sty (ppr sty) pbdr1 pbdr2 pocc) alts
184 ppr_core_default sty pbdr1 pbdr2 pocc deflt
185 = ppr_default (init_ppr_env sty (ppr sty) pbdr1 pbdr2 pocc) deflt
188 %************************************************************************
190 \subsection{Instance declarations for Core printing}
192 %************************************************************************
196 (Outputable bndr, Outputable occ, Eq tyvar, Outputable tyvar,
197 Eq uvar, Outputable uvar)
199 Outputable (GenCoreBinding bndr occ tyvar uvar) where
200 ppr sty bind = pprGenCoreBinding sty (ppr sty) (ppr sty) (ppr sty) bind
203 (Outputable bndr, Outputable occ, Eq tyvar, Outputable tyvar,
204 Eq uvar, Outputable uvar)
206 Outputable (GenCoreExpr bndr occ tyvar uvar) where
207 ppr sty expr = pprGenCoreExpr sty (ppr sty) (ppr sty) (ppr sty) expr
210 (Outputable occ, Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
212 Outputable (GenCoreArg occ tyvar uvar) where
213 ppr sty arg = ppr_core_arg sty (ppr sty) arg
216 (Outputable bndr, Outputable occ, Eq tyvar, Outputable tyvar,
217 Eq uvar, Outputable uvar)
219 Outputable (GenCoreCaseAlts bndr occ tyvar uvar) where
220 ppr sty alts = ppr_core_alts sty (ppr sty) (ppr sty) (ppr sty) alts
223 (Outputable bndr, Outputable occ, Eq tyvar, Outputable tyvar,
224 Eq uvar, Outputable uvar)
226 Outputable (GenCoreCaseDefault bndr occ tyvar uvar) where
227 ppr sty deflt = ppr_core_default sty (ppr sty) (ppr sty) (ppr sty) deflt
230 %************************************************************************
232 \subsection{Workhorse routines (...????...)}
234 %************************************************************************
237 ppr_bind pe (NonRec val_bdr expr)
238 = ppHang (ppCat [pMajBndr pe val_bdr, ppEquals])
241 ppr_bind pe (Rec binds)
242 = ppAboves (map ppr_pair binds)
244 ppr_pair (val_bdr, expr)
245 = ppHang (ppCat [pMajBndr pe val_bdr, ppEquals])
246 4 (ppr_expr pe expr `ppBeside` ppSemi)
250 ppr_parend_expr pe expr
254 Var _ -> id -- leave unchanged
256 _ -> ppParens -- wraps in parens
258 parenify (ppr_expr pe expr)
262 ppr_expr pe (Var name) = pOcc pe name
263 ppr_expr pe (Lit lit) = pLit pe lit
265 ppr_expr pe (Con con args)
266 = ppHang (pCon pe con)
267 4 (ppCurlies $ ppSep (map (ppr_arg pe) args))
269 ppr_expr pe (Prim prim args)
270 = ppHang (pPrim pe prim)
271 4 (ppSep (map (ppr_arg pe) args))
273 ppr_expr pe expr@(Lam _ _)
275 (uvars, tyvars, vars, body) = collectBinders expr
277 ppHang (ppCat [pp_vars SLIT("/u\\") (pUVar pe) uvars,
278 pp_vars SLIT("_/\\_") (pTyVarB pe) tyvars,
279 pp_vars SLIT("\\") (pMajBndr pe) vars])
282 pp_vars lam pp [] = ppNil
284 = ppCat [ppPStr lam, ppInterleave ppSP (map pp vs), ppPStr SLIT("->")]
286 ppr_expr pe expr@(App fun arg)
288 (final_fun, final_args) = go fun [arg]
289 go (App fun arg) args_so_far = go fun (arg:args_so_far)
290 go fun args_so_far = (fun, args_so_far)
292 ppHang (ppr_parend_expr pe final_fun) 4 (ppSep (map (ppr_arg pe) final_args))
294 ppr_expr pe (Case expr alts)
296 -- johan thinks that single case patterns should be on same line as case,
297 -- and no indent; all sane persons agree with him.
300 ppr_alt (AlgAlts [] (BindDefault n _)) = ppBeside (pMinBndr pe n) ppr_arrow
301 ppr_alt (PrimAlts [] (BindDefault n _)) = ppBeside (pMinBndr pe n) ppr_arrow
302 ppr_alt (PrimAlts ((l, _):[]) NoDefault)= ppBeside (pLit pe l) ppr_arrow
303 ppr_alt (AlgAlts ((con, params, _):[]) NoDefault)
304 = ppCat [pCon pe con,
305 ppInterleave ppSP (map (pMinBndr pe) params),
308 ppr_rhs (AlgAlts [] (BindDefault _ expr)) = ppr_expr pe expr
309 ppr_rhs (AlgAlts ((_,_,expr):[]) NoDefault) = ppr_expr pe expr
310 ppr_rhs (PrimAlts [] (BindDefault _ expr)) = ppr_expr pe expr
311 ppr_rhs (PrimAlts ((_,expr):[]) NoDefault) = ppr_expr pe expr
314 ppr_arrow = ppPStr SLIT(" ->")
317 [ppSep [pp_keyword, ppNest 4 (ppr_expr pe expr), ppStr "of {", ppr_alt alts],
318 ppBeside (ppr_rhs alts) (ppStr ";}")]
320 | otherwise -- default "case" printing
322 [ppSep [pp_keyword, ppNest 4 (ppr_expr pe expr), ppPStr SLIT("of {")],
323 ppNest 2 (ppr_alts pe alts),
326 pp_keyword = case alts of
327 AlgAlts _ _ -> ppPStr SLIT("case")
328 PrimAlts _ _ -> ppPStr SLIT("case#")
330 -- special cases: let ... in let ...
331 -- ("disgusting" SLPJ)
333 ppr_expr pe (Let bind@(NonRec val_bdr rhs@(Let _ _)) body)
335 ppCat [ppPStr SLIT("let {"), pMajBndr pe val_bdr, ppEquals],
336 ppNest 2 (ppr_expr pe rhs),
340 ppr_expr pe (Let bind@(NonRec val_bdr rhs) expr@(Let _ _))
342 (ppHang (ppPStr SLIT("let {"))
343 2 (ppCat [ppHang (ppCat [pMajBndr pe val_bdr, ppEquals])
345 ppPStr SLIT("} in")]))
348 -- general case (recursive case, too)
349 ppr_expr pe (Let bind expr)
350 = ppSep [ppHang (ppPStr keyword) 2 (ppr_bind pe bind),
351 ppHang (ppPStr SLIT("} in ")) 2 (ppr_expr pe expr)]
353 keyword = case bind of
354 Rec _ -> SLIT("letrec {")
355 NonRec _ _ -> SLIT("let {")
357 ppr_expr pe (SCC cc expr)
358 = ppSep [ppCat [ppPStr SLIT("_scc_"), pSCC pe cc],
359 ppr_parend_expr pe expr ]
361 ppr_expr pe (Coerce c ty expr)
362 = ppSep [pp_coerce c, pTy pe ty, ppr_expr pe expr]
364 pp_coerce (CoerceIn v) = ppBeside (ppPStr SLIT("_coerce_in_ ")) (ppr (pStyle pe) v)
365 pp_coerce (CoerceOut v) = ppBeside (ppPStr SLIT("_coerce_out_ ")) (ppr (pStyle pe) v)
367 only_one_alt (AlgAlts [] (BindDefault _ _)) = True
368 only_one_alt (AlgAlts (_:[]) NoDefault) = True
369 only_one_alt (PrimAlts [] (BindDefault _ _)) = True
370 only_one_alt (PrimAlts (_:[]) NoDefault) = True
371 only_one_alt _ = False
375 ppr_alts pe (AlgAlts alts deflt)
376 = ppAboves [ ppAboves (map ppr_alt alts), ppr_default pe deflt ]
378 ppr_arrow = ppPStr SLIT("->")
380 ppr_alt (con, params, expr)
381 = ppHang (if isTupleCon con then
382 ppCat [ppParens (ppInterleave ppComma (map (pMinBndr pe) params)),
386 ppInterleave ppSP (map (pMinBndr pe) params),
389 4 (ppr_expr pe expr `ppBeside` ppSemi)
391 ppr_alts pe (PrimAlts alts deflt)
392 = ppAboves [ ppAboves (map ppr_alt alts), ppr_default pe deflt ]
395 = ppHang (ppCat [pLit pe lit, ppPStr SLIT("->")])
396 4 (ppr_expr pe expr `ppBeside` ppSemi)
400 ppr_default pe NoDefault = ppNil
402 ppr_default pe (BindDefault val_bdr expr)
403 = ppHang (ppCat [pMinBndr pe val_bdr, ppPStr SLIT("->")])
404 4 (ppr_expr pe expr `ppBeside` ppSemi)
408 ppr_arg pe (LitArg lit) = pLit pe lit
409 ppr_arg pe (VarArg v) = pOcc pe v
410 ppr_arg pe (TyArg ty) = ppPStr SLIT("_@_ ") `ppBeside` pTy pe ty
411 ppr_arg pe (UsageArg use) = pUse pe use
414 Other printing bits-and-bobs used with the general @pprCoreBinding@
415 and @pprCoreExpr@ functions.
418 pprBigCoreBinder sty binder
419 = ppAboves [sig, pragmas, ppr sty binder]
421 sig = ifnotPprShowAll sty (
422 ppHang (ppCat [ppr sty binder, ppDcolon])
423 4 (ppr sty (idType binder)))
426 (ppIdInfo sty False{-no specs, thanks-} (getIdInfo binder))
428 pprBabyCoreBinder sty binder
429 = ppCat [ppr sty binder, pp_strictness]
432 = case (getIdStrictness binder) of
433 NoStrictnessInfo -> ppNil
434 BottomGuaranteed -> ppPStr SLIT("{- _!_ -}")
435 StrictnessInfo xx _ ->
436 panic "PprCore:pp_strictness:StrictnessInfo:ToDo"
437 -- ppStr ("{- " ++ (showList xx "") ++ " -}")
439 pprTypedCoreBinder sty binder
440 = ppBesides [ppr sty binder, ppDcolon, pprParendGenType sty (idType binder)]
442 ppDcolon = ppPStr SLIT(" :: ")
443 -- The space before the :: is important; it helps the lexer
444 -- when reading inferfaces. Otherwise it would lex "a::b" as one thing.