589382aac380ec779d8a00580ffdf339b0f91229
[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 $ sep $ punctuate comma $ map ppr es
132 pprExp _ (UnboxedTupE es) = hashParens $ sep $ punctuate comma $ map ppr 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 $ sep $ punctuate semi $ map ppr 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 $ sep $ punctuate semi $ map ppr 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 <+> (sep $ punctuate comma $ map ppr ss')
165 <> text "]"
166 where s = last ss
167 ss' = init ss
168 pprExp _ (ArithSeqE d) = ppr d
169 pprExp _ (ListE es) = brackets $ sep $ punctuate comma $ map ppr 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
176 pprFields :: [(Name,Exp)] -> Doc
177 pprFields = sep . punctuate comma . map (\(s,e) -> ppr s <+> equals <+> ppr e)
178
179 pprMaybeExp :: Precedence -> Maybe Exp -> Doc
180 pprMaybeExp _ Nothing = empty
181 pprMaybeExp i (Just e) = pprExp i e
182
183 ------------------------------
184 instance Ppr Stmt where
185 ppr (BindS p e) = ppr p <+> text "<-" <+> ppr e
186 ppr (LetS ds) = text "let" <+> ppr ds
187 ppr (NoBindS e) = ppr e
188 ppr (ParS sss) = sep $ punctuate (text "|")
189 $ map (sep . punctuate comma . map ppr) sss
190
191 ------------------------------
192 instance Ppr Match where
193 ppr (Match p rhs ds) = ppr p <+> pprBody False rhs
194 $$ where_clause ds
195
196 ------------------------------
197 pprGuarded :: Doc -> (Guard, Exp) -> Doc
198 pprGuarded eqDoc (guard, expr) = case guard of
199 NormalG guardExpr -> char '|' <+> ppr guardExpr <+> eqDoc <+> ppr expr
200 PatG stmts -> char '|' <+> vcat (punctuate comma $ map ppr stmts) $$
201 nest nestDepth (eqDoc <+> ppr expr)
202
203 ------------------------------
204 pprBody :: Bool -> Body -> Doc
205 pprBody eq body = case body of
206 GuardedB xs -> nest nestDepth $ vcat $ map (pprGuarded eqDoc) xs
207 NormalB e -> eqDoc <+> ppr e
208 where eqDoc | eq = equals
209 | otherwise = arrow
210
211 ------------------------------
212 instance Ppr Lit where
213 ppr = pprLit noPrec
214
215 pprLit :: Precedence -> Lit -> Doc
216 pprLit i (IntPrimL x) = parensIf (i > noPrec && x < 0)
217 (integer x <> char '#')
218 pprLit _ (WordPrimL x) = integer x <> text "##"
219 pprLit i (FloatPrimL x) = parensIf (i > noPrec && x < 0)
220 (float (fromRational x) <> char '#')
221 pprLit i (DoublePrimL x) = parensIf (i > noPrec && x < 0)
222 (double (fromRational x) <> text "##")
223 pprLit i (IntegerL x) = parensIf (i > noPrec && x < 0) (integer x)
224 pprLit _ (CharL c) = text (show c)
225 pprLit _ (CharPrimL c) = text (show c) <> char '#'
226 pprLit _ (StringL s) = pprString s
227 pprLit _ (StringPrimL s) = pprString (bytesToString s) <> char '#'
228 pprLit i (RationalL rat) = parensIf (i > noPrec) $
229 integer (numerator rat) <+> char '/'
230 <+> integer (denominator rat)
231
232 bytesToString :: [Word8] -> String
233 bytesToString = map (chr . fromIntegral)
234
235 pprString :: String -> Doc
236 -- Print newlines as newlines with Haskell string escape notation,
237 -- not as '\n'. For other non-printables use regular escape notation.
238 pprString s = vcat (map text (showMultiLineString s))
239
240 ------------------------------
241 instance Ppr Pat where
242 ppr = pprPat noPrec
243
244 pprPat :: Precedence -> Pat -> Doc
245 pprPat i (LitP l) = pprLit i l
246 pprPat _ (VarP v) = pprName' Applied v
247 pprPat _ (TupP ps) = parens $ sep $ punctuate comma $ map ppr ps
248 pprPat _ (UnboxedTupP ps) = hashParens $ sep $ punctuate comma $ map ppr ps
249 pprPat i (ConP s ps) = parensIf (i >= appPrec) $ pprName' Applied s
250 <+> sep (map (pprPat appPrec) ps)
251 pprPat _ (ParensP p) = parens $ pprPat noPrec p
252 pprPat i (UInfixP p1 n p2)
253 = parensIf (i > unopPrec) (pprPat unopPrec p1 <+>
254 pprName' Infix n <+>
255 pprPat unopPrec p2)
256 pprPat i (InfixP p1 n p2)
257 = parensIf (i >= opPrec) (pprPat opPrec p1 <+>
258 pprName' Infix n <+>
259 pprPat opPrec p2)
260 pprPat i (TildeP p) = parensIf (i > noPrec) $ char '~' <> pprPat appPrec p
261 pprPat i (BangP p) = parensIf (i > noPrec) $ char '!' <> pprPat appPrec p
262 pprPat i (AsP v p) = parensIf (i > noPrec) $ ppr v <> text "@"
263 <> pprPat appPrec p
264 pprPat _ WildP = text "_"
265 pprPat _ (RecP nm fs)
266 = parens $ ppr nm
267 <+> braces (sep $ punctuate comma $
268 map (\(s,p) -> ppr s <+> equals <+> ppr p) fs)
269 pprPat _ (ListP ps) = brackets $ sep $ punctuate comma $ map ppr ps
270 pprPat i (SigP p t) = parensIf (i > noPrec) $ ppr p <+> dcolon <+> ppr t
271 pprPat _ (ViewP e p) = parens $ pprExp noPrec e <+> text "->" <+> pprPat noPrec p
272
273 ------------------------------
274 instance Ppr Dec where
275 ppr = ppr_dec True
276
277 ppr_dec :: Bool -- declaration on the toplevel?
278 -> Dec
279 -> Doc
280 ppr_dec _ (FunD f cs) = vcat $ map (\c -> pprPrefixOcc f <+> ppr c) cs
281 ppr_dec _ (ValD p r ds) = ppr p <+> pprBody True r
282 $$ where_clause ds
283 ppr_dec _ (TySynD t xs rhs)
284 = ppr_tySyn empty t (hsep (map ppr xs)) rhs
285 ppr_dec _ (DataD ctxt t xs cs decs)
286 = ppr_data empty ctxt t (hsep (map ppr xs)) cs decs
287 ppr_dec _ (NewtypeD ctxt t xs c decs)
288 = ppr_newtype empty ctxt t (sep (map ppr xs)) c decs
289 ppr_dec _ (ClassD ctxt c xs fds ds)
290 = text "class" <+> pprCxt ctxt <+> ppr c <+> hsep (map ppr xs) <+> ppr fds
291 $$ where_clause ds
292 ppr_dec _ (InstanceD ctxt i ds) = text "instance" <+> pprCxt ctxt <+> ppr i
293 $$ where_clause ds
294 ppr_dec _ (SigD f t) = pprPrefixOcc f <+> dcolon <+> ppr t
295 ppr_dec _ (ForeignD f) = ppr f
296 ppr_dec _ (InfixD fx n) = pprFixity n fx
297 ppr_dec _ (PragmaD p) = ppr p
298 ppr_dec isTop (DataFamilyD tc tvs kind)
299 = text "data" <+> maybeFamily <+> ppr tc <+> hsep (map ppr tvs) <+> maybeKind
300 where
301 maybeFamily | isTop = text "family"
302 | otherwise = empty
303 maybeKind | (Just k') <- kind = dcolon <+> ppr k'
304 | otherwise = empty
305 ppr_dec isTop (DataInstD ctxt tc tys cs decs)
306 = ppr_data maybeInst ctxt tc (sep (map pprParendType tys)) cs decs
307 where
308 maybeInst | isTop = text "instance"
309 | otherwise = empty
310 ppr_dec isTop (NewtypeInstD ctxt tc tys c decs)
311 = ppr_newtype maybeInst ctxt tc (sep (map pprParendType tys)) c decs
312 where
313 maybeInst | isTop = text "instance"
314 | otherwise = empty
315 ppr_dec isTop (TySynInstD tc (TySynEqn tys rhs))
316 = ppr_tySyn maybeInst tc (sep (map pprParendType tys)) rhs
317 where
318 maybeInst | isTop = text "instance"
319 | otherwise = empty
320 ppr_dec isTop (OpenTypeFamilyD tc tvs res inj)
321 = text "type" <+> maybeFamily <+> ppr tc <+> hsep (map ppr tvs) <+>
322 ppr res <+> maybeInj
323 where
324 maybeFamily | isTop = text "family"
325 | otherwise = empty
326 maybeInj | (Just inj') <- inj = ppr inj'
327 | otherwise = empty
328 ppr_dec _ (ClosedTypeFamilyD tc tvs res inj eqns)
329 = hang (hsep [ text "type family", ppr tc, hsep (map ppr tvs), ppr res
330 , maybeInj, text "where" ])
331 nestDepth (vcat (map ppr_eqn eqns))
332 where
333 maybeInj | (Just inj') <- inj = ppr inj'
334 | otherwise = empty
335 ppr_eqn (TySynEqn lhs rhs)
336 = ppr tc <+> sep (map pprParendType lhs) <+> text "=" <+> ppr rhs
337
338 ppr_dec _ (RoleAnnotD name roles)
339 = hsep [ text "type role", ppr name ] <+> hsep (map ppr roles)
340
341 ppr_dec _ (StandaloneDerivD cxt ty)
342 = hsep [ text "deriving instance", pprCxt cxt, ppr ty ]
343
344 ppr_dec _ (DefaultSigD n ty)
345 = hsep [ text "default", pprPrefixOcc n, dcolon, ppr ty ]
346
347 ppr_data :: Doc -> Cxt -> Name -> Doc -> [Con] -> [Name] -> Doc
348 ppr_data maybeInst ctxt t argsDoc cs decs
349 = sep [text "data" <+> maybeInst
350 <+> pprCxt ctxt
351 <+> ppr t <+> argsDoc,
352 nest nestDepth (sep (pref $ map ppr cs)),
353 if null decs
354 then empty
355 else nest nestDepth
356 $ text "deriving"
357 <+> parens (hsep $ punctuate comma $ map ppr decs)]
358 where
359 pref :: [Doc] -> [Doc]
360 pref [] = [] -- No constructors; can't happen in H98
361 pref (d:ds) = (char '=' <+> d):map (char '|' <+>) ds
362
363 ppr_newtype :: Doc -> Cxt -> Name -> Doc -> Con -> [Name] -> Doc
364 ppr_newtype maybeInst ctxt t argsDoc c decs
365 = sep [text "newtype" <+> maybeInst
366 <+> pprCxt ctxt
367 <+> ppr t <+> argsDoc,
368 nest 2 (char '=' <+> ppr c),
369 if null decs
370 then empty
371 else nest nestDepth
372 $ text "deriving"
373 <+> parens (hsep $ punctuate comma $ map ppr decs)]
374
375 ppr_tySyn :: Doc -> Name -> Doc -> Type -> Doc
376 ppr_tySyn maybeInst t argsDoc rhs
377 = text "type" <+> maybeInst <+> ppr t <+> argsDoc <+> text "=" <+> ppr rhs
378
379 ------------------------------
380 instance Ppr FunDep where
381 ppr (FunDep xs ys) = hsep (map ppr xs) <+> text "->" <+> hsep (map ppr ys)
382 ppr_list [] = empty
383 ppr_list xs = char '|' <+> sep (punctuate (text ", ") (map ppr xs))
384
385 ------------------------------
386 instance Ppr FamFlavour where
387 ppr DataFam = text "data"
388 ppr TypeFam = text "type"
389
390 ------------------------------
391 instance Ppr FamilyResultSig where
392 ppr NoSig = empty
393 ppr (KindSig k) = dcolon <+> ppr k
394 ppr (TyVarSig bndr) = text "=" <+> ppr bndr
395
396 ------------------------------
397 instance Ppr InjectivityAnn where
398 ppr (InjectivityAnn lhs rhs) =
399 char '|' <+> ppr lhs <+> text "->" <+> hsep (map ppr rhs)
400
401 ------------------------------
402 instance Ppr Foreign where
403 ppr (ImportF callconv safety impent as typ)
404 = text "foreign import"
405 <+> showtextl callconv
406 <+> showtextl safety
407 <+> text (show impent)
408 <+> ppr as
409 <+> dcolon <+> ppr typ
410 ppr (ExportF callconv expent as typ)
411 = text "foreign export"
412 <+> showtextl callconv
413 <+> text (show expent)
414 <+> ppr as
415 <+> dcolon <+> ppr typ
416
417 ------------------------------
418 instance Ppr Pragma where
419 ppr (InlineP n inline rm phases)
420 = text "{-#"
421 <+> ppr inline
422 <+> ppr rm
423 <+> ppr phases
424 <+> ppr n
425 <+> text "#-}"
426 ppr (SpecialiseP n ty inline phases)
427 = text "{-# SPECIALISE"
428 <+> maybe empty ppr inline
429 <+> ppr phases
430 <+> sep [ ppr n <+> dcolon
431 , nest 2 $ ppr ty ]
432 <+> text "#-}"
433 ppr (SpecialiseInstP inst)
434 = text "{-# SPECIALISE instance" <+> ppr inst <+> text "#-}"
435 ppr (RuleP n bndrs lhs rhs phases)
436 = sep [ text "{-# RULES" <+> pprString n <+> ppr phases
437 , nest 4 $ ppr_forall <+> ppr lhs
438 , nest 4 $ char '=' <+> ppr rhs <+> text "#-}" ]
439 where ppr_forall | null bndrs = empty
440 | otherwise = text "forall"
441 <+> fsep (map ppr bndrs)
442 <+> char '.'
443 ppr (AnnP tgt expr)
444 = text "{-# ANN" <+> target1 tgt <+> ppr expr <+> text "#-}"
445 where target1 ModuleAnnotation = text "module"
446 target1 (TypeAnnotation t) = text "type" <+> ppr t
447 target1 (ValueAnnotation v) = ppr v
448 ppr (LineP line file)
449 = text "{-# LINE" <+> int line <+> text (show file) <+> text "#-}"
450
451 ------------------------------
452 instance Ppr Inline where
453 ppr NoInline = text "NOINLINE"
454 ppr Inline = text "INLINE"
455 ppr Inlinable = text "INLINABLE"
456
457 ------------------------------
458 instance Ppr RuleMatch where
459 ppr ConLike = text "CONLIKE"
460 ppr FunLike = empty
461
462 ------------------------------
463 instance Ppr Phases where
464 ppr AllPhases = empty
465 ppr (FromPhase i) = brackets $ int i
466 ppr (BeforePhase i) = brackets $ char '~' <> int i
467
468 ------------------------------
469 instance Ppr RuleBndr where
470 ppr (RuleVar n) = ppr n
471 ppr (TypedRuleVar n ty) = parens $ ppr n <+> dcolon <+> ppr ty
472
473 ------------------------------
474 instance Ppr Clause where
475 ppr (Clause ps rhs ds) = hsep (map (pprPat appPrec) ps) <+> pprBody True rhs
476 $$ where_clause ds
477
478 ------------------------------
479 instance Ppr Con where
480 ppr (NormalC c sts) = ppr c <+> sep (map pprStrictType sts)
481 ppr (RecC c vsts)
482 = ppr c <+> braces (sep (punctuate comma $ map pprVarStrictType vsts))
483 ppr (InfixC st1 c st2) = pprStrictType st1
484 <+> pprName' Infix c
485 <+> pprStrictType st2
486 ppr (ForallC ns ctxt con) = text "forall" <+> hsep (map ppr ns)
487 <+> char '.' <+> sep [pprCxt ctxt, ppr con]
488
489 ------------------------------
490 pprVarStrictType :: (Name, Strict, Type) -> Doc
491 -- Slight infelicity: with print non-atomic type with parens
492 pprVarStrictType (v, str, t) = ppr v <+> dcolon <+> pprStrictType (str, t)
493
494 ------------------------------
495 pprStrictType :: (Strict, Type) -> Doc
496 -- Prints with parens if not already atomic
497 pprStrictType (IsStrict, t) = char '!' <> pprParendType t
498 pprStrictType (NotStrict, t) = pprParendType t
499 pprStrictType (Unpacked, t) = text "{-# UNPACK #-} !" <> pprParendType t
500
501 ------------------------------
502 pprParendType :: Type -> Doc
503 pprParendType (VarT v) = ppr v
504 pprParendType (ConT c) = ppr c
505 pprParendType (TupleT 0) = text "()"
506 pprParendType (TupleT n) = parens (hcat (replicate (n-1) comma))
507 pprParendType (UnboxedTupleT n) = hashParens $ hcat $ replicate (n-1) comma
508 pprParendType ArrowT = parens (text "->")
509 pprParendType ListT = text "[]"
510 pprParendType (LitT l) = pprTyLit l
511 pprParendType (PromotedT c) = text "'" <> ppr c
512 pprParendType (PromotedTupleT 0) = text "'()"
513 pprParendType (PromotedTupleT n) = quoteParens (hcat (replicate (n-1) comma))
514 pprParendType PromotedNilT = text "'[]"
515 pprParendType PromotedConsT = text "(':)"
516 pprParendType StarT = char '*'
517 pprParendType ConstraintT = text "Constraint"
518 pprParendType (SigT ty k) = parens (ppr ty <+> text "::" <+> ppr k)
519 pprParendType (WildCardT mbName) = char '_' <> maybe empty ppr mbName
520 pprParendType (InfixT x n y) = parens (ppr x <+> pprName' Infix n <+> ppr y)
521 pprParendType t@(UInfixT {}) = parens (pprUInfixT t)
522 pprParendType (ParensT t) = ppr t
523 pprParendType other = parens (ppr other)
524
525 pprUInfixT :: Type -> Doc
526 pprUInfixT (UInfixT x n y) = pprUInfixT x <+> pprName' Infix n <+> pprUInfixT y
527 pprUInfixT t = ppr t
528
529 instance Ppr Type where
530 ppr (ForallT tvars ctxt ty)
531 = text "forall" <+> hsep (map ppr tvars) <+> text "."
532 <+> sep [pprCxt ctxt, ppr ty]
533 ppr ty = pprTyApp (split ty)
534 -- Works, in a degnerate way, for SigT, and puts parens round (ty :: kind)
535 -- See Note [Pretty-printing kind signatures]
536
537 {- Note [Pretty-printing kind signatures]
538 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
539 GHC's parser only recognises a kind signature in a type when there are
540 parens around it. E.g. the parens are required here:
541 f :: (Int :: *)
542 type instance F Int = (Bool :: *)
543 So we always print a SigT with parens (see Trac #10050). -}
544
545 pprTyApp :: (Type, [Type]) -> Doc
546 pprTyApp (ArrowT, [arg1,arg2]) = sep [pprFunArgType arg1 <+> text "->", ppr arg2]
547 pprTyApp (EqualityT, [arg1, arg2]) =
548 sep [pprFunArgType arg1 <+> text "~", ppr arg2]
549 pprTyApp (ListT, [arg]) = brackets (ppr arg)
550 pprTyApp (TupleT n, args)
551 | length args == n = parens (sep (punctuate comma (map ppr args)))
552 pprTyApp (PromotedTupleT n, args)
553 | length args == n = quoteParens (sep (punctuate comma (map ppr args)))
554 pprTyApp (fun, args) = pprParendType fun <+> sep (map pprParendType args)
555
556 pprFunArgType :: Type -> Doc -- Should really use a precedence argument
557 -- Everything except forall and (->) binds more tightly than (->)
558 pprFunArgType ty@(ForallT {}) = parens (ppr ty)
559 pprFunArgType ty@((ArrowT `AppT` _) `AppT` _) = parens (ppr ty)
560 pprFunArgType ty@(SigT _ _) = parens (ppr ty)
561 pprFunArgType ty = ppr ty
562
563 split :: Type -> (Type, [Type]) -- Split into function and args
564 split t = go t []
565 where go (AppT t1 t2) args = go t1 (t2:args)
566 go ty args = (ty, args)
567
568 pprTyLit :: TyLit -> Doc
569 pprTyLit (NumTyLit n) = integer n
570 pprTyLit (StrTyLit s) = text (show s)
571
572 instance Ppr TyLit where
573 ppr = pprTyLit
574
575 ------------------------------
576 instance Ppr TyVarBndr where
577 ppr (PlainTV nm) = ppr nm
578 ppr (KindedTV nm k) = parens (ppr nm <+> dcolon <+> ppr k)
579
580 instance Ppr Role where
581 ppr NominalR = text "nominal"
582 ppr RepresentationalR = text "representational"
583 ppr PhantomR = text "phantom"
584 ppr InferR = text "_"
585
586 ------------------------------
587 pprCxt :: Cxt -> Doc
588 pprCxt [] = empty
589 pprCxt [t] = ppr t <+> text "=>"
590 pprCxt ts = parens (sep $ punctuate comma $ map ppr ts) <+> text "=>"
591
592 ------------------------------
593 instance Ppr Range where
594 ppr = brackets . pprRange
595 where pprRange :: Range -> Doc
596 pprRange (FromR e) = ppr e <> text ".."
597 pprRange (FromThenR e1 e2) = ppr e1 <> text ","
598 <> ppr e2 <> text ".."
599 pprRange (FromToR e1 e2) = ppr e1 <> text ".." <> ppr e2
600 pprRange (FromThenToR e1 e2 e3) = ppr e1 <> text ","
601 <> ppr e2 <> text ".."
602 <> ppr e3
603
604 ------------------------------
605 where_clause :: [Dec] -> Doc
606 where_clause [] = empty
607 where_clause ds = nest nestDepth $ text "where" <+> vcat (map (ppr_dec False) ds)
608
609 showtextl :: Show a => a -> Doc
610 showtextl = text . map toLower . show
611
612 hashParens :: Doc -> Doc
613 hashParens d = text "(# " <> d <> text " #)"
614
615 quoteParens :: Doc -> Doc
616 quoteParens d = text "'(" <> d <> text ")"
617
618 -----------------------------
619 instance Ppr Loc where
620 ppr (Loc { loc_module = md
621 , loc_package = pkg
622 , loc_start = (start_ln, start_col)
623 , loc_end = (end_ln, end_col) })
624 = hcat [ text pkg, colon, text md, colon
625 , parens $ int start_ln <> comma <> int start_col
626 , text "-"
627 , parens $ int end_ln <> comma <> int end_col ]