0a7f98da7031a1dff0830f4ab962f8001457450b
[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 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 $ sep $ punctuate semi $ map ppr ds)
188 ppr (NoBindS e) = ppr e
189 ppr (ParS sss) = sep $ punctuate (text "|")
190 $ map (sep . punctuate comma . map ppr) 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 $ sep $ punctuate comma $ map ppr ps
249 pprPat _ (UnboxedTupP ps) = hashParens $ sep $ punctuate comma $ map ppr 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 $ sep $ punctuate comma $ map ppr 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 cs decs)
287 = ppr_data empty ctxt t (hsep (map ppr xs)) cs decs
288 ppr_dec _ (NewtypeD ctxt t xs c decs)
289 = ppr_newtype empty ctxt t (sep (map ppr xs)) 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 cs decs)
307 = ppr_data maybeInst ctxt tc (sep (map pprParendType tys)) cs decs
308 where
309 maybeInst | isTop = text "instance"
310 | otherwise = empty
311 ppr_dec isTop (NewtypeInstD ctxt tc tys c decs)
312 = ppr_newtype maybeInst ctxt tc (sep (map pprParendType tys)) 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 -> [Con] -> Cxt -> Doc
343 ppr_data maybeInst ctxt t argsDoc cs decs
344 = sep [text "data" <+> maybeInst
345 <+> pprCxt ctxt
346 <+> ppr t <+> argsDoc,
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 [] = [] -- No constructors; can't happen in H98
355 pref (d:ds) = (char '=' <+> d):map (char '|' <+>) ds
356
357 ppr_newtype :: Doc -> Cxt -> Name -> Doc -> Con -> Cxt -> Doc
358 ppr_newtype maybeInst ctxt t argsDoc c decs
359 = sep [text "newtype" <+> maybeInst
360 <+> pprCxt ctxt
361 <+> ppr t <+> argsDoc,
362 nest 2 (char '=' <+> ppr c),
363 if null decs
364 then empty
365 else nest nestDepth
366 $ text "deriving" <+> ppr_cxt_preds decs]
367
368 ppr_tySyn :: Doc -> Name -> Doc -> Type -> Doc
369 ppr_tySyn maybeInst t argsDoc rhs
370 = text "type" <+> maybeInst <+> ppr t <+> argsDoc <+> text "=" <+> ppr rhs
371
372 ppr_tf_head :: TypeFamilyHead -> Doc
373 ppr_tf_head (TypeFamilyHead tc tvs res inj)
374 = ppr tc <+> hsep (map ppr tvs) <+> ppr res <+> maybeInj
375 where
376 maybeInj | (Just inj') <- inj = ppr inj'
377 | otherwise = empty
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 ts = ppr_cxt_preds ts <+> text "=>"
590
591 ppr_cxt_preds :: Cxt -> Doc
592 ppr_cxt_preds [] = empty
593 ppr_cxt_preds [t] = ppr t
594 ppr_cxt_preds ts = parens (sep $ punctuate comma $ map ppr ts)
595
596 ------------------------------
597 instance Ppr Range where
598 ppr = brackets . pprRange
599 where pprRange :: Range -> Doc
600 pprRange (FromR e) = ppr e <> text ".."
601 pprRange (FromThenR e1 e2) = ppr e1 <> text ","
602 <> ppr e2 <> text ".."
603 pprRange (FromToR e1 e2) = ppr e1 <> text ".." <> ppr e2
604 pprRange (FromThenToR e1 e2 e3) = ppr e1 <> text ","
605 <> ppr e2 <> text ".."
606 <> ppr e3
607
608 ------------------------------
609 where_clause :: [Dec] -> Doc
610 where_clause [] = empty
611 where_clause ds = nest nestDepth $ text "where" <+> vcat (map (ppr_dec False) ds)
612
613 showtextl :: Show a => a -> Doc
614 showtextl = text . map toLower . show
615
616 hashParens :: Doc -> Doc
617 hashParens d = text "(# " <> d <> text " #)"
618
619 quoteParens :: Doc -> Doc
620 quoteParens d = text "'(" <> d <> text ")"
621
622 -----------------------------
623 instance Ppr Loc where
624 ppr (Loc { loc_module = md
625 , loc_package = pkg
626 , loc_start = (start_ln, start_col)
627 , loc_end = (end_ln, end_col) })
628 = hcat [ text pkg, colon, text md, colon
629 , parens $ int start_ln <> comma <> int start_col
630 , text "-"
631 , parens $ int end_ln <> comma <> int end_col ]