4db99d8b8c35ad9524fe36442e46a36875fd9303
[ghc.git] / libraries / template-haskell / Language / Haskell / TH / Ppr.hs
1 -- | contains a prettyprinter for the
2 -- Template Haskell datatypes
3
4 module Language.Haskell.TH.Ppr where
5 -- All of the exports from this module should
6 -- be "public" functions. The main module TH
7 -- re-exports them all.
8
9 import Text.PrettyPrint (render)
10 import Language.Haskell.TH.PprLib
11 import Language.Haskell.TH.Syntax
12 import Data.Word ( Word8 )
13 import Data.Char ( toLower, chr, ord, isSymbol )
14 import GHC.Show ( showMultiLineString )
15 import Data.Ratio ( numerator, denominator )
16
17 nestDepth :: Int
18 nestDepth = 4
19
20 type Precedence = Int
21 appPrec, unopPrec, opPrec, noPrec :: Precedence
22 appPrec = 3 -- Argument of a function application
23 opPrec = 2 -- Argument of an infix operator
24 unopPrec = 1 -- Argument of an unresolved infix operator
25 noPrec = 0 -- Others
26
27 parensIf :: Bool -> Doc -> Doc
28 parensIf True d = parens d
29 parensIf False d = d
30
31 ------------------------------
32
33 pprint :: Ppr a => a -> String
34 pprint x = render $ to_HPJ_Doc $ ppr x
35
36 class Ppr a where
37 ppr :: a -> Doc
38 ppr_list :: [a] -> Doc
39 ppr_list = vcat . map ppr
40
41 instance Ppr a => Ppr [a] where
42 ppr x = ppr_list x
43
44 ------------------------------
45 instance Ppr Name where
46 ppr v = pprName v
47
48 ------------------------------
49 instance Ppr Info where
50 ppr (TyConI d) = ppr d
51 ppr (ClassI d is) = ppr d $$ vcat (map ppr is)
52 ppr (FamilyI d is) = ppr d $$ vcat (map ppr is)
53 ppr (PrimTyConI name arity is_unlifted)
54 = text "Primitive"
55 <+> (if is_unlifted then text "unlifted" else empty)
56 <+> text "type constructor" <+> quotes (ppr name)
57 <+> parens (text "arity" <+> int arity)
58 ppr (ClassOpI v ty cls)
59 = text "Class op from" <+> ppr cls <> colon <+> ppr_sig v ty
60 ppr (DataConI v ty tc)
61 = text "Constructor from" <+> ppr tc <> colon <+> ppr_sig v ty
62 ppr (TyVarI v ty)
63 = text "Type variable" <+> ppr v <+> equals <+> ppr ty
64 ppr (VarI v ty mb_d)
65 = vcat [ppr_sig v ty,
66 case mb_d of { Nothing -> empty; Just d -> ppr d }]
67
68 ppr_sig :: Name -> Type -> Doc
69 ppr_sig v ty = ppr v <+> dcolon <+> ppr ty
70
71 pprFixity :: Name -> Fixity -> Doc
72 pprFixity _ f | f == defaultFixity = empty
73 pprFixity v (Fixity i d) = ppr_fix d <+> int i <+> ppr v
74 where ppr_fix InfixR = text "infixr"
75 ppr_fix InfixL = text "infixl"
76 ppr_fix InfixN = text "infix"
77
78
79 ------------------------------
80 instance Ppr Module where
81 ppr (Module pkg m) = text (pkgString pkg) <+> text (modString m)
82
83 instance Ppr ModuleInfo where
84 ppr (ModuleInfo imps) = text "Module" <+> vcat (map ppr imps)
85
86 ------------------------------
87 instance Ppr Exp where
88 ppr = pprExp noPrec
89
90 pprPrefixOcc :: Name -> Doc
91 -- Print operators with parens around them
92 pprPrefixOcc n = parensIf (isSymOcc n) (ppr n)
93
94 isSymOcc :: Name -> Bool
95 isSymOcc n
96 = case nameBase n of
97 [] -> True -- Empty name; weird
98 (c:_) -> isSymbolASCII c || (ord c > 0x7f && isSymbol c)
99 -- c.f. OccName.startsVarSym in GHC itself
100
101 isSymbolASCII :: Char -> Bool
102 isSymbolASCII c = c `elem` "!#$%&*+./<=>?@\\^|~-"
103
104 pprInfixExp :: Exp -> Doc
105 pprInfixExp (VarE v) = pprName' Infix v
106 pprInfixExp (ConE v) = pprName' Infix v
107 pprInfixExp _ = text "<<Non-variable/constructor in infix context>>"
108
109 pprExp :: Precedence -> Exp -> Doc
110 pprExp _ (VarE v) = pprName' Applied v
111 pprExp _ (ConE c) = pprName' Applied c
112 pprExp i (LitE l) = pprLit i l
113 pprExp i (AppE e1 e2) = parensIf (i >= appPrec) $ pprExp opPrec e1
114 <+> pprExp appPrec e2
115 pprExp _ (ParensE e) = parens (pprExp noPrec e)
116 pprExp i (UInfixE e1 op e2)
117 = parensIf (i > unopPrec) $ pprExp unopPrec e1
118 <+> pprInfixExp op
119 <+> pprExp unopPrec e2
120 pprExp i (InfixE (Just e1) op (Just e2))
121 = parensIf (i >= opPrec) $ pprExp opPrec e1
122 <+> pprInfixExp op
123 <+> pprExp opPrec e2
124 pprExp _ (InfixE me1 op me2) = parens $ pprMaybeExp noPrec me1
125 <+> pprInfixExp op
126 <+> pprMaybeExp noPrec me2
127 pprExp i (LamE ps e) = parensIf (i > noPrec) $ char '\\' <> hsep (map (pprPat appPrec) ps)
128 <+> text "->" <+> ppr e
129 pprExp i (LamCaseE ms) = parensIf (i > noPrec)
130 $ text "\\case" $$ nest nestDepth (ppr ms)
131 pprExp _ (TupE es) = parens (commaSep es)
132 pprExp _ (UnboxedTupE es) = hashParens (commaSep es)
133 -- Nesting in Cond is to avoid potential problems in do statments
134 pprExp i (CondE guard true false)
135 = parensIf (i > noPrec) $ sep [text "if" <+> ppr guard,
136 nest 1 $ text "then" <+> ppr true,
137 nest 1 $ text "else" <+> ppr false]
138 pprExp i (MultiIfE alts)
139 = parensIf (i > noPrec) $ vcat $
140 case alts of
141 [] -> [text "if {}"]
142 (alt : alts') -> text "if" <+> pprGuarded arrow alt
143 : map (nest 3 . pprGuarded arrow) alts'
144 pprExp i (LetE ds_ e) = parensIf (i > noPrec) $ text "let" <+> pprDecs ds_
145 $$ text " in" <+> ppr e
146 where
147 pprDecs [] = empty
148 pprDecs [d] = ppr d
149 pprDecs ds = braces (semiSep ds)
150
151 pprExp i (CaseE e ms)
152 = parensIf (i > noPrec) $ text "case" <+> ppr e <+> text "of"
153 $$ nest nestDepth (ppr ms)
154 pprExp i (DoE ss_) = parensIf (i > noPrec) $ text "do" <+> pprStms ss_
155 where
156 pprStms [] = empty
157 pprStms [s] = ppr s
158 pprStms ss = braces (semiSep ss)
159
160 pprExp _ (CompE []) = text "<<Empty CompExp>>"
161 -- This will probably break with fixity declarations - would need a ';'
162 pprExp _ (CompE ss) = text "[" <> ppr s
163 <+> text "|"
164 <+> commaSep ss'
165 <> text "]"
166 where s = last ss
167 ss' = init ss
168 pprExp _ (ArithSeqE d) = ppr d
169 pprExp _ (ListE es) = brackets (commaSep es)
170 pprExp i (SigE e t) = parensIf (i > noPrec) $ ppr e <+> dcolon <+> ppr t
171 pprExp _ (RecConE nm fs) = ppr nm <> braces (pprFields fs)
172 pprExp _ (RecUpdE e fs) = pprExp appPrec e <> braces (pprFields fs)
173 pprExp i (StaticE e) = parensIf (i >= appPrec) $
174 text "static"<+> pprExp appPrec e
175 pprExp _ (UnboundVarE v) = pprName' Applied v
176
177 pprFields :: [(Name,Exp)] -> Doc
178 pprFields = sep . punctuate comma . map (\(s,e) -> ppr s <+> equals <+> ppr e)
179
180 pprMaybeExp :: Precedence -> Maybe Exp -> Doc
181 pprMaybeExp _ Nothing = empty
182 pprMaybeExp i (Just e) = pprExp i e
183
184 ------------------------------
185 instance Ppr Stmt where
186 ppr (BindS p e) = ppr p <+> text "<-" <+> ppr e
187 ppr (LetS ds) = text "let" <+> (braces (semiSep ds))
188 ppr (NoBindS e) = ppr e
189 ppr (ParS sss) = sep $ punctuate (text "|")
190 $ map commaSep sss
191
192 ------------------------------
193 instance Ppr Match where
194 ppr (Match p rhs ds) = ppr p <+> pprBody False rhs
195 $$ where_clause ds
196
197 ------------------------------
198 pprGuarded :: Doc -> (Guard, Exp) -> Doc
199 pprGuarded eqDoc (guard, expr) = case guard of
200 NormalG guardExpr -> char '|' <+> ppr guardExpr <+> eqDoc <+> ppr expr
201 PatG stmts -> char '|' <+> vcat (punctuate comma $ map ppr stmts) $$
202 nest nestDepth (eqDoc <+> ppr expr)
203
204 ------------------------------
205 pprBody :: Bool -> Body -> Doc
206 pprBody eq body = case body of
207 GuardedB xs -> nest nestDepth $ vcat $ map (pprGuarded eqDoc) xs
208 NormalB e -> eqDoc <+> ppr e
209 where eqDoc | eq = equals
210 | otherwise = arrow
211
212 ------------------------------
213 instance Ppr Lit where
214 ppr = pprLit noPrec
215
216 pprLit :: Precedence -> Lit -> Doc
217 pprLit i (IntPrimL x) = parensIf (i > noPrec && x < 0)
218 (integer x <> char '#')
219 pprLit _ (WordPrimL x) = integer x <> text "##"
220 pprLit i (FloatPrimL x) = parensIf (i > noPrec && x < 0)
221 (float (fromRational x) <> char '#')
222 pprLit i (DoublePrimL x) = parensIf (i > noPrec && x < 0)
223 (double (fromRational x) <> text "##")
224 pprLit i (IntegerL x) = parensIf (i > noPrec && x < 0) (integer x)
225 pprLit _ (CharL c) = text (show c)
226 pprLit _ (CharPrimL c) = text (show c) <> char '#'
227 pprLit _ (StringL s) = pprString s
228 pprLit _ (StringPrimL s) = pprString (bytesToString s) <> char '#'
229 pprLit i (RationalL rat) = parensIf (i > noPrec) $
230 integer (numerator rat) <+> char '/'
231 <+> integer (denominator rat)
232
233 bytesToString :: [Word8] -> String
234 bytesToString = map (chr . fromIntegral)
235
236 pprString :: String -> Doc
237 -- Print newlines as newlines with Haskell string escape notation,
238 -- not as '\n'. For other non-printables use regular escape notation.
239 pprString s = vcat (map text (showMultiLineString s))
240
241 ------------------------------
242 instance Ppr Pat where
243 ppr = pprPat noPrec
244
245 pprPat :: Precedence -> Pat -> Doc
246 pprPat i (LitP l) = pprLit i l
247 pprPat _ (VarP v) = pprName' Applied v
248 pprPat _ (TupP ps) = parens (commaSep ps)
249 pprPat _ (UnboxedTupP ps) = hashParens (commaSep ps)
250 pprPat i (ConP s ps) = parensIf (i >= appPrec) $ pprName' Applied s
251 <+> sep (map (pprPat appPrec) ps)
252 pprPat _ (ParensP p) = parens $ pprPat noPrec p
253 pprPat i (UInfixP p1 n p2)
254 = parensIf (i > unopPrec) (pprPat unopPrec p1 <+>
255 pprName' Infix n <+>
256 pprPat unopPrec p2)
257 pprPat i (InfixP p1 n p2)
258 = parensIf (i >= opPrec) (pprPat opPrec p1 <+>
259 pprName' Infix n <+>
260 pprPat opPrec p2)
261 pprPat i (TildeP p) = parensIf (i > noPrec) $ char '~' <> pprPat appPrec p
262 pprPat i (BangP p) = parensIf (i > noPrec) $ char '!' <> pprPat appPrec p
263 pprPat i (AsP v p) = parensIf (i > noPrec) $ ppr v <> text "@"
264 <> pprPat appPrec p
265 pprPat _ WildP = text "_"
266 pprPat _ (RecP nm fs)
267 = parens $ ppr nm
268 <+> braces (sep $ punctuate comma $
269 map (\(s,p) -> ppr s <+> equals <+> ppr p) fs)
270 pprPat _ (ListP ps) = brackets (commaSep ps)
271 pprPat i (SigP p t) = parensIf (i > noPrec) $ ppr p <+> dcolon <+> ppr t
272 pprPat _ (ViewP e p) = parens $ pprExp noPrec e <+> text "->" <+> pprPat noPrec p
273
274 ------------------------------
275 instance Ppr Dec where
276 ppr = ppr_dec True
277
278 ppr_dec :: Bool -- declaration on the toplevel?
279 -> Dec
280 -> Doc
281 ppr_dec _ (FunD f cs) = vcat $ map (\c -> pprPrefixOcc f <+> ppr c) cs
282 ppr_dec _ (ValD p r ds) = ppr p <+> pprBody True r
283 $$ where_clause ds
284 ppr_dec _ (TySynD t xs rhs)
285 = ppr_tySyn empty t (hsep (map ppr xs)) rhs
286 ppr_dec _ (DataD ctxt t xs ksig cs decs)
287 = ppr_data empty ctxt t (hsep (map ppr xs)) ksig cs decs
288 ppr_dec _ (NewtypeD ctxt t xs ksig c decs)
289 = ppr_newtype empty ctxt t (sep (map ppr xs)) ksig c decs
290 ppr_dec _ (ClassD ctxt c xs fds ds)
291 = text "class" <+> pprCxt ctxt <+> ppr c <+> hsep (map ppr xs) <+> ppr fds
292 $$ where_clause ds
293 ppr_dec _ (InstanceD ctxt i ds) = text "instance" <+> pprCxt ctxt <+> ppr i
294 $$ where_clause ds
295 ppr_dec _ (SigD f t) = pprPrefixOcc f <+> dcolon <+> ppr t
296 ppr_dec _ (ForeignD f) = ppr f
297 ppr_dec _ (InfixD fx n) = pprFixity n fx
298 ppr_dec _ (PragmaD p) = ppr p
299 ppr_dec isTop (DataFamilyD tc tvs kind)
300 = text "data" <+> maybeFamily <+> ppr tc <+> hsep (map ppr tvs) <+> maybeKind
301 where
302 maybeFamily | isTop = text "family"
303 | otherwise = empty
304 maybeKind | (Just k') <- kind = dcolon <+> ppr k'
305 | otherwise = empty
306 ppr_dec isTop (DataInstD ctxt tc tys ksig cs decs)
307 = ppr_data maybeInst ctxt tc (sep (map pprParendType tys)) ksig cs decs
308 where
309 maybeInst | isTop = text "instance"
310 | otherwise = empty
311 ppr_dec isTop (NewtypeInstD ctxt tc tys ksig c decs)
312 = ppr_newtype maybeInst ctxt tc (sep (map pprParendType tys)) ksig c decs
313 where
314 maybeInst | isTop = text "instance"
315 | otherwise = empty
316 ppr_dec isTop (TySynInstD tc (TySynEqn tys rhs))
317 = ppr_tySyn maybeInst tc (sep (map pprParendType tys)) rhs
318 where
319 maybeInst | isTop = text "instance"
320 | otherwise = empty
321 ppr_dec isTop (OpenTypeFamilyD tfhead)
322 = text "type" <+> maybeFamily <+> ppr_tf_head tfhead
323 where
324 maybeFamily | isTop = text "family"
325 | otherwise = empty
326 ppr_dec _ (ClosedTypeFamilyD tfhead@(TypeFamilyHead tc _ _ _) eqns)
327 = hang (text "type family" <+> ppr_tf_head tfhead <+> text "where")
328 nestDepth (vcat (map ppr_eqn eqns))
329 where
330 ppr_eqn (TySynEqn lhs rhs)
331 = ppr tc <+> sep (map pprParendType lhs) <+> text "=" <+> ppr rhs
332
333 ppr_dec _ (RoleAnnotD name roles)
334 = hsep [ text "type role", ppr name ] <+> hsep (map ppr roles)
335
336 ppr_dec _ (StandaloneDerivD cxt ty)
337 = hsep [ text "deriving instance", pprCxt cxt, ppr ty ]
338
339 ppr_dec _ (DefaultSigD n ty)
340 = hsep [ text "default", pprPrefixOcc n, dcolon, ppr ty ]
341
342 ppr_data :: Doc -> Cxt -> Name -> Doc -> Maybe Kind -> [Con] -> Cxt -> Doc
343 ppr_data maybeInst ctxt t argsDoc ksig cs decs
344 = sep [text "data" <+> maybeInst
345 <+> pprCxt ctxt
346 <+> ppr t <+> argsDoc <+> ksigDoc <+> maybeWhere,
347 nest nestDepth (sep (pref $ map ppr cs)),
348 if null decs
349 then empty
350 else nest nestDepth
351 $ text "deriving" <+> ppr_cxt_preds decs]
352 where
353 pref :: [Doc] -> [Doc]
354 pref xs | isGadtDecl = xs
355 pref [] = [] -- No constructors; can't happen in H98
356 pref (d:ds) = (char '=' <+> d):map (char '|' <+>) ds
357
358 maybeWhere :: Doc
359 maybeWhere | isGadtDecl = text "where"
360 | otherwise = empty
361
362 isGadtDecl :: Bool
363 isGadtDecl = not (null cs) && all isGadtCon cs
364 where isGadtCon (GadtC _ _ _ _ ) = True
365 isGadtCon (RecGadtC _ _ _ _) = True
366 isGadtCon (ForallC _ _ x ) = isGadtCon x
367 isGadtCon _ = False
368
369 ksigDoc = case ksig of
370 Nothing -> empty
371 Just k -> dcolon <+> ppr k
372
373 ppr_newtype :: Doc -> Cxt -> Name -> Doc -> Maybe Kind -> Con -> Cxt -> Doc
374 ppr_newtype maybeInst ctxt t argsDoc ksig c decs
375 = sep [text "newtype" <+> maybeInst
376 <+> pprCxt ctxt
377 <+> ppr t <+> argsDoc <+> ksigDoc,
378 nest 2 (char '=' <+> ppr c),
379 if null decs
380 then empty
381 else nest nestDepth
382 $ text "deriving" <+> ppr_cxt_preds decs]
383 where
384 ksigDoc = case ksig of
385 Nothing -> empty
386 Just k -> dcolon <+> ppr k
387
388 ppr_tySyn :: Doc -> Name -> Doc -> Type -> Doc
389 ppr_tySyn maybeInst t argsDoc rhs
390 = text "type" <+> maybeInst <+> ppr t <+> argsDoc <+> text "=" <+> ppr rhs
391
392 ppr_tf_head :: TypeFamilyHead -> Doc
393 ppr_tf_head (TypeFamilyHead tc tvs res inj)
394 = ppr tc <+> hsep (map ppr tvs) <+> ppr res <+> maybeInj
395 where
396 maybeInj | (Just inj') <- inj = ppr inj'
397 | otherwise = empty
398
399 ------------------------------
400 instance Ppr FunDep where
401 ppr (FunDep xs ys) = hsep (map ppr xs) <+> text "->" <+> hsep (map ppr ys)
402 ppr_list [] = empty
403 ppr_list xs = char '|' <+> commaSep xs
404
405 ------------------------------
406 instance Ppr FamFlavour where
407 ppr DataFam = text "data"
408 ppr TypeFam = text "type"
409
410 ------------------------------
411 instance Ppr FamilyResultSig where
412 ppr NoSig = empty
413 ppr (KindSig k) = dcolon <+> ppr k
414 ppr (TyVarSig bndr) = text "=" <+> ppr bndr
415
416 ------------------------------
417 instance Ppr InjectivityAnn where
418 ppr (InjectivityAnn lhs rhs) =
419 char '|' <+> ppr lhs <+> text "->" <+> hsep (map ppr rhs)
420
421 ------------------------------
422 instance Ppr Foreign where
423 ppr (ImportF callconv safety impent as typ)
424 = text "foreign import"
425 <+> showtextl callconv
426 <+> showtextl safety
427 <+> text (show impent)
428 <+> ppr as
429 <+> dcolon <+> ppr typ
430 ppr (ExportF callconv expent as typ)
431 = text "foreign export"
432 <+> showtextl callconv
433 <+> text (show expent)
434 <+> ppr as
435 <+> dcolon <+> ppr typ
436
437 ------------------------------
438 instance Ppr Pragma where
439 ppr (InlineP n inline rm phases)
440 = text "{-#"
441 <+> ppr inline
442 <+> ppr rm
443 <+> ppr phases
444 <+> ppr n
445 <+> text "#-}"
446 ppr (SpecialiseP n ty inline phases)
447 = text "{-# SPECIALISE"
448 <+> maybe empty ppr inline
449 <+> ppr phases
450 <+> sep [ ppr n <+> dcolon
451 , nest 2 $ ppr ty ]
452 <+> text "#-}"
453 ppr (SpecialiseInstP inst)
454 = text "{-# SPECIALISE instance" <+> ppr inst <+> text "#-}"
455 ppr (RuleP n bndrs lhs rhs phases)
456 = sep [ text "{-# RULES" <+> pprString n <+> ppr phases
457 , nest 4 $ ppr_forall <+> ppr lhs
458 , nest 4 $ char '=' <+> ppr rhs <+> text "#-}" ]
459 where ppr_forall | null bndrs = empty
460 | otherwise = text "forall"
461 <+> fsep (map ppr bndrs)
462 <+> char '.'
463 ppr (AnnP tgt expr)
464 = text "{-# ANN" <+> target1 tgt <+> ppr expr <+> text "#-}"
465 where target1 ModuleAnnotation = text "module"
466 target1 (TypeAnnotation t) = text "type" <+> ppr t
467 target1 (ValueAnnotation v) = ppr v
468 ppr (LineP line file)
469 = text "{-# LINE" <+> int line <+> text (show file) <+> text "#-}"
470
471 ------------------------------
472 instance Ppr Inline where
473 ppr NoInline = text "NOINLINE"
474 ppr Inline = text "INLINE"
475 ppr Inlinable = text "INLINABLE"
476
477 ------------------------------
478 instance Ppr RuleMatch where
479 ppr ConLike = text "CONLIKE"
480 ppr FunLike = empty
481
482 ------------------------------
483 instance Ppr Phases where
484 ppr AllPhases = empty
485 ppr (FromPhase i) = brackets $ int i
486 ppr (BeforePhase i) = brackets $ char '~' <> int i
487
488 ------------------------------
489 instance Ppr RuleBndr where
490 ppr (RuleVar n) = ppr n
491 ppr (TypedRuleVar n ty) = parens $ ppr n <+> dcolon <+> ppr ty
492
493 ------------------------------
494 instance Ppr Clause where
495 ppr (Clause ps rhs ds) = hsep (map (pprPat appPrec) ps) <+> pprBody True rhs
496 $$ where_clause ds
497
498 ------------------------------
499 instance Ppr Con where
500 ppr (NormalC c sts) = ppr c <+> sep (map pprBangType sts)
501
502 ppr (RecC c vsts)
503 = ppr c <+> braces (sep (punctuate comma $ map pprVarBangType vsts))
504
505 ppr (InfixC st1 c st2) = pprBangType st1
506 <+> pprName' Infix c
507 <+> pprBangType st2
508
509 ppr (ForallC ns ctxt (GadtC c sts ty idx))
510 = commaSep c <+> dcolon <+> pprForall ns ctxt <+> pprGadtRHS sts ty idx
511
512 ppr (ForallC ns ctxt (RecGadtC c vsts ty idx))
513 = commaSep c <+> dcolon <+> pprForall ns ctxt
514 <+> pprRecFields vsts ty idx
515
516 ppr (ForallC ns ctxt con)
517 = pprForall ns ctxt <+> ppr con
518
519 ppr (GadtC c sts ty idx)
520 = commaSep c <+> dcolon <+> pprGadtRHS sts ty idx
521
522 ppr (RecGadtC c vsts ty idx)
523 = commaSep c <+> dcolon <+> pprRecFields vsts ty idx
524
525 pprForall :: [TyVarBndr] -> Cxt -> Doc
526 pprForall ns ctxt
527 = text "forall" <+> hsep (map ppr ns)
528 <+> char '.' <+> pprCxt ctxt
529
530 pprRecFields :: [(Name, Strict, Type)] -> Name -> [Type] -> Doc
531 pprRecFields vsts ty idx
532 = braces (sep (punctuate comma $ map pprVarBangType vsts))
533 <+> arrow <+> ppr ty <+> sep (map ppr idx)
534
535 pprGadtRHS :: [(Strict, Type)] -> Name -> [Type] -> Doc
536 pprGadtRHS [] ty idx
537 = ppr ty <+> sep (map ppr idx)
538 pprGadtRHS sts ty idx
539 = sep (punctuate (space <> arrow) (map pprBangType sts))
540 <+> arrow <+> ppr ty <+> sep (map ppr idx)
541
542 ------------------------------
543 pprVarBangType :: VarBangType -> Doc
544 -- Slight infelicity: with print non-atomic type with parens
545 pprVarBangType (v, bang, t) = ppr v <+> dcolon <+> pprBangType (bang, t)
546
547 ------------------------------
548 pprBangType :: BangType -> Doc
549 -- Make sure we print
550 --
551 -- Con {-# UNPACK #-} a
552 --
553 -- rather than
554 --
555 -- Con {-# UNPACK #-}a
556 --
557 -- when there's no strictness annotation. If there is a strictness annotation,
558 -- it's okay to not put a space between it and the type.
559 pprBangType (bt@(Bang _ NoSourceStrictness), t) = ppr bt <+> pprParendType t
560 pprBangType (bt, t) = ppr bt <> pprParendType t
561
562 ------------------------------
563 instance Ppr Bang where
564 ppr (Bang su ss) = ppr su <+> ppr ss
565
566 ------------------------------
567 instance Ppr SourceUnpackedness where
568 ppr NoSourceUnpackedness = empty
569 ppr SourceNoUnpack = text "{-# NOUNPACK #-}"
570 ppr SourceUnpack = text "{-# UNPACK #-}"
571
572 ------------------------------
573 instance Ppr SourceStrictness where
574 ppr NoSourceStrictness = empty
575 ppr SourceLazy = char '~'
576 ppr SourceStrict = char '!'
577
578 ------------------------------
579 instance Ppr DecidedStrictness where
580 ppr DecidedLazy = empty
581 ppr DecidedStrict = char '!'
582 ppr DecidedUnpack = text "{-# UNPACK #-} !"
583
584 ------------------------------
585 {-# DEPRECATED pprVarStrictType
586 "As of @template-haskell-2.11.0.0@, 'VarStrictType' has been replaced by 'VarBangType'. Please use 'pprVarBangType' instead." #-}
587 pprVarStrictType :: (Name, Strict, Type) -> Doc
588 pprVarStrictType = pprVarBangType
589
590 ------------------------------
591 {-# DEPRECATED pprStrictType
592 "As of @template-haskell-2.11.0.0@, 'StrictType' has been replaced by 'BangType'. Please use 'pprBangType' instead." #-}
593 pprStrictType :: (Strict, Type) -> Doc
594 pprStrictType = pprBangType
595
596 ------------------------------
597 pprParendType :: Type -> Doc
598 pprParendType (VarT v) = ppr v
599 pprParendType (ConT c) = ppr c
600 pprParendType (TupleT 0) = text "()"
601 pprParendType (TupleT n) = parens (hcat (replicate (n-1) comma))
602 pprParendType (UnboxedTupleT n) = hashParens $ hcat $ replicate (n-1) comma
603 pprParendType ArrowT = parens (text "->")
604 pprParendType ListT = text "[]"
605 pprParendType (LitT l) = pprTyLit l
606 pprParendType (PromotedT c) = text "'" <> ppr c
607 pprParendType (PromotedTupleT 0) = text "'()"
608 pprParendType (PromotedTupleT n) = quoteParens (hcat (replicate (n-1) comma))
609 pprParendType PromotedNilT = text "'[]"
610 pprParendType PromotedConsT = text "(':)"
611 pprParendType StarT = char '*'
612 pprParendType ConstraintT = text "Constraint"
613 pprParendType (SigT ty k) = parens (ppr ty <+> text "::" <+> ppr k)
614 pprParendType WildCardT = char '_'
615 pprParendType (InfixT x n y) = parens (ppr x <+> pprName' Infix n <+> ppr y)
616 pprParendType t@(UInfixT {}) = parens (pprUInfixT t)
617 pprParendType (ParensT t) = ppr t
618 pprParendType other = parens (ppr other)
619
620 pprUInfixT :: Type -> Doc
621 pprUInfixT (UInfixT x n y) = pprUInfixT x <+> pprName' Infix n <+> pprUInfixT y
622 pprUInfixT t = ppr t
623
624 instance Ppr Type where
625 ppr (ForallT tvars ctxt ty)
626 = text "forall" <+> hsep (map ppr tvars) <+> text "."
627 <+> sep [pprCxt ctxt, ppr ty]
628 ppr ty = pprTyApp (split ty)
629 -- Works, in a degnerate way, for SigT, and puts parens round (ty :: kind)
630 -- See Note [Pretty-printing kind signatures]
631
632 {- Note [Pretty-printing kind signatures]
633 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
634 GHC's parser only recognises a kind signature in a type when there are
635 parens around it. E.g. the parens are required here:
636 f :: (Int :: *)
637 type instance F Int = (Bool :: *)
638 So we always print a SigT with parens (see Trac #10050). -}
639
640 pprTyApp :: (Type, [Type]) -> Doc
641 pprTyApp (ArrowT, [arg1,arg2]) = sep [pprFunArgType arg1 <+> text "->", ppr arg2]
642 pprTyApp (EqualityT, [arg1, arg2]) =
643 sep [pprFunArgType arg1 <+> text "~", ppr arg2]
644 pprTyApp (ListT, [arg]) = brackets (ppr arg)
645 pprTyApp (TupleT n, args)
646 | length args == n = parens (commaSep args)
647 pprTyApp (PromotedTupleT n, args)
648 | length args == n = quoteParens (commaSep args)
649 pprTyApp (fun, args) = pprParendType fun <+> sep (map pprParendType args)
650
651 pprFunArgType :: Type -> Doc -- Should really use a precedence argument
652 -- Everything except forall and (->) binds more tightly than (->)
653 pprFunArgType ty@(ForallT {}) = parens (ppr ty)
654 pprFunArgType ty@((ArrowT `AppT` _) `AppT` _) = parens (ppr ty)
655 pprFunArgType ty@(SigT _ _) = parens (ppr ty)
656 pprFunArgType ty = ppr ty
657
658 split :: Type -> (Type, [Type]) -- Split into function and args
659 split t = go t []
660 where go (AppT t1 t2) args = go t1 (t2:args)
661 go ty args = (ty, args)
662
663 pprTyLit :: TyLit -> Doc
664 pprTyLit (NumTyLit n) = integer n
665 pprTyLit (StrTyLit s) = text (show s)
666
667 instance Ppr TyLit where
668 ppr = pprTyLit
669
670 ------------------------------
671 instance Ppr TyVarBndr where
672 ppr (PlainTV nm) = ppr nm
673 ppr (KindedTV nm k) = parens (ppr nm <+> dcolon <+> ppr k)
674
675 instance Ppr Role where
676 ppr NominalR = text "nominal"
677 ppr RepresentationalR = text "representational"
678 ppr PhantomR = text "phantom"
679 ppr InferR = text "_"
680
681 ------------------------------
682 pprCxt :: Cxt -> Doc
683 pprCxt [] = empty
684 pprCxt ts = ppr_cxt_preds ts <+> text "=>"
685
686 ppr_cxt_preds :: Cxt -> Doc
687 ppr_cxt_preds [] = empty
688 ppr_cxt_preds [t] = ppr t
689 ppr_cxt_preds ts = parens (commaSep ts)
690
691 ------------------------------
692 instance Ppr Range where
693 ppr = brackets . pprRange
694 where pprRange :: Range -> Doc
695 pprRange (FromR e) = ppr e <> text ".."
696 pprRange (FromThenR e1 e2) = ppr e1 <> text ","
697 <> ppr e2 <> text ".."
698 pprRange (FromToR e1 e2) = ppr e1 <> text ".." <> ppr e2
699 pprRange (FromThenToR e1 e2 e3) = ppr e1 <> text ","
700 <> ppr e2 <> text ".."
701 <> ppr e3
702
703 ------------------------------
704 where_clause :: [Dec] -> Doc
705 where_clause [] = empty
706 where_clause ds = nest nestDepth $ text "where" <+> vcat (map (ppr_dec False) ds)
707
708 showtextl :: Show a => a -> Doc
709 showtextl = text . map toLower . show
710
711 hashParens :: Doc -> Doc
712 hashParens d = text "(# " <> d <> text " #)"
713
714 quoteParens :: Doc -> Doc
715 quoteParens d = text "'(" <> d <> text ")"
716
717 -----------------------------
718 instance Ppr Loc where
719 ppr (Loc { loc_module = md
720 , loc_package = pkg
721 , loc_start = (start_ln, start_col)
722 , loc_end = (end_ln, end_col) })
723 = hcat [ text pkg, colon, text md, colon
724 , parens $ int start_ln <> comma <> int start_col
725 , text "-"
726 , parens $ int end_ln <> comma <> int end_col ]
727
728 -- Takes a list of printable things and prints them separated by commas followed
729 -- by space.
730 commaSep :: Ppr a => [a] -> Doc
731 commaSep = sep . punctuate comma . map ppr
732
733 -- Takes a list of printable things and prints them separated by semicolons
734 -- followed by space.
735 semiSep :: Ppr a => [a] -> Doc
736 semiSep = sep . punctuate semi . map ppr