Add typed holes support in Template Haskell.
[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" <+> 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 tc tvs res inj)
322 = text "type" <+> maybeFamily <+> ppr tc <+> hsep (map ppr tvs) <+>
323 ppr res <+> maybeInj
324 where
325 maybeFamily | isTop = text "family"
326 | otherwise = empty
327 maybeInj | (Just inj') <- inj = ppr inj'
328 | otherwise = empty
329 ppr_dec _ (ClosedTypeFamilyD tc tvs res inj eqns)
330 = hang (hsep [ text "type family", ppr tc, hsep (map ppr tvs), ppr res
331 , maybeInj, text "where" ])
332 nestDepth (vcat (map ppr_eqn eqns))
333 where
334 maybeInj | (Just inj') <- inj = ppr inj'
335 | otherwise = empty
336 ppr_eqn (TySynEqn lhs rhs)
337 = ppr tc <+> sep (map pprParendType lhs) <+> text "=" <+> ppr rhs
338
339 ppr_dec _ (RoleAnnotD name roles)
340 = hsep [ text "type role", ppr name ] <+> hsep (map ppr roles)
341
342 ppr_dec _ (StandaloneDerivD cxt ty)
343 = hsep [ text "deriving instance", pprCxt cxt, ppr ty ]
344
345 ppr_dec _ (DefaultSigD n ty)
346 = hsep [ text "default", pprPrefixOcc n, dcolon, ppr ty ]
347
348 ppr_data :: Doc -> Cxt -> Name -> Doc -> [Con] -> [Name] -> Doc
349 ppr_data maybeInst ctxt t argsDoc cs decs
350 = sep [text "data" <+> maybeInst
351 <+> pprCxt ctxt
352 <+> ppr t <+> argsDoc,
353 nest nestDepth (sep (pref $ map ppr cs)),
354 if null decs
355 then empty
356 else nest nestDepth
357 $ text "deriving"
358 <+> parens (hsep $ punctuate comma $ map ppr decs)]
359 where
360 pref :: [Doc] -> [Doc]
361 pref [] = [] -- No constructors; can't happen in H98
362 pref (d:ds) = (char '=' <+> d):map (char '|' <+>) ds
363
364 ppr_newtype :: Doc -> Cxt -> Name -> Doc -> Con -> [Name] -> Doc
365 ppr_newtype maybeInst ctxt t argsDoc c decs
366 = sep [text "newtype" <+> maybeInst
367 <+> pprCxt ctxt
368 <+> ppr t <+> argsDoc,
369 nest 2 (char '=' <+> ppr c),
370 if null decs
371 then empty
372 else nest nestDepth
373 $ text "deriving"
374 <+> parens (hsep $ punctuate comma $ map ppr decs)]
375
376 ppr_tySyn :: Doc -> Name -> Doc -> Type -> Doc
377 ppr_tySyn maybeInst t argsDoc rhs
378 = text "type" <+> maybeInst <+> ppr t <+> argsDoc <+> text "=" <+> ppr rhs
379
380 ------------------------------
381 instance Ppr FunDep where
382 ppr (FunDep xs ys) = hsep (map ppr xs) <+> text "->" <+> hsep (map ppr ys)
383 ppr_list [] = empty
384 ppr_list xs = char '|' <+> sep (punctuate (text ", ") (map ppr xs))
385
386 ------------------------------
387 instance Ppr FamFlavour where
388 ppr DataFam = text "data"
389 ppr TypeFam = text "type"
390
391 ------------------------------
392 instance Ppr FamilyResultSig where
393 ppr NoSig = empty
394 ppr (KindSig k) = dcolon <+> ppr k
395 ppr (TyVarSig bndr) = text "=" <+> ppr bndr
396
397 ------------------------------
398 instance Ppr InjectivityAnn where
399 ppr (InjectivityAnn lhs rhs) =
400 char '|' <+> ppr lhs <+> text "->" <+> hsep (map ppr rhs)
401
402 ------------------------------
403 instance Ppr Foreign where
404 ppr (ImportF callconv safety impent as typ)
405 = text "foreign import"
406 <+> showtextl callconv
407 <+> showtextl safety
408 <+> text (show impent)
409 <+> ppr as
410 <+> dcolon <+> ppr typ
411 ppr (ExportF callconv expent as typ)
412 = text "foreign export"
413 <+> showtextl callconv
414 <+> text (show expent)
415 <+> ppr as
416 <+> dcolon <+> ppr typ
417
418 ------------------------------
419 instance Ppr Pragma where
420 ppr (InlineP n inline rm phases)
421 = text "{-#"
422 <+> ppr inline
423 <+> ppr rm
424 <+> ppr phases
425 <+> ppr n
426 <+> text "#-}"
427 ppr (SpecialiseP n ty inline phases)
428 = text "{-# SPECIALISE"
429 <+> maybe empty ppr inline
430 <+> ppr phases
431 <+> sep [ ppr n <+> dcolon
432 , nest 2 $ ppr ty ]
433 <+> text "#-}"
434 ppr (SpecialiseInstP inst)
435 = text "{-# SPECIALISE instance" <+> ppr inst <+> text "#-}"
436 ppr (RuleP n bndrs lhs rhs phases)
437 = sep [ text "{-# RULES" <+> pprString n <+> ppr phases
438 , nest 4 $ ppr_forall <+> ppr lhs
439 , nest 4 $ char '=' <+> ppr rhs <+> text "#-}" ]
440 where ppr_forall | null bndrs = empty
441 | otherwise = text "forall"
442 <+> fsep (map ppr bndrs)
443 <+> char '.'
444 ppr (AnnP tgt expr)
445 = text "{-# ANN" <+> target1 tgt <+> ppr expr <+> text "#-}"
446 where target1 ModuleAnnotation = text "module"
447 target1 (TypeAnnotation t) = text "type" <+> ppr t
448 target1 (ValueAnnotation v) = ppr v
449 ppr (LineP line file)
450 = text "{-# LINE" <+> int line <+> text (show file) <+> text "#-}"
451
452 ------------------------------
453 instance Ppr Inline where
454 ppr NoInline = text "NOINLINE"
455 ppr Inline = text "INLINE"
456 ppr Inlinable = text "INLINABLE"
457
458 ------------------------------
459 instance Ppr RuleMatch where
460 ppr ConLike = text "CONLIKE"
461 ppr FunLike = empty
462
463 ------------------------------
464 instance Ppr Phases where
465 ppr AllPhases = empty
466 ppr (FromPhase i) = brackets $ int i
467 ppr (BeforePhase i) = brackets $ char '~' <> int i
468
469 ------------------------------
470 instance Ppr RuleBndr where
471 ppr (RuleVar n) = ppr n
472 ppr (TypedRuleVar n ty) = parens $ ppr n <+> dcolon <+> ppr ty
473
474 ------------------------------
475 instance Ppr Clause where
476 ppr (Clause ps rhs ds) = hsep (map (pprPat appPrec) ps) <+> pprBody True rhs
477 $$ where_clause ds
478
479 ------------------------------
480 instance Ppr Con where
481 ppr (NormalC c sts) = ppr c <+> sep (map pprStrictType sts)
482 ppr (RecC c vsts)
483 = ppr c <+> braces (sep (punctuate comma $ map pprVarStrictType vsts))
484 ppr (InfixC st1 c st2) = pprStrictType st1
485 <+> pprName' Infix c
486 <+> pprStrictType st2
487 ppr (ForallC ns ctxt con) = text "forall" <+> hsep (map ppr ns)
488 <+> char '.' <+> sep [pprCxt ctxt, ppr con]
489
490 ------------------------------
491 pprVarStrictType :: (Name, Strict, Type) -> Doc
492 -- Slight infelicity: with print non-atomic type with parens
493 pprVarStrictType (v, str, t) = ppr v <+> dcolon <+> pprStrictType (str, t)
494
495 ------------------------------
496 pprStrictType :: (Strict, Type) -> Doc
497 -- Prints with parens if not already atomic
498 pprStrictType (IsStrict, t) = char '!' <> pprParendType t
499 pprStrictType (NotStrict, t) = pprParendType t
500 pprStrictType (Unpacked, t) = text "{-# UNPACK #-} !" <> pprParendType t
501
502 ------------------------------
503 pprParendType :: Type -> Doc
504 pprParendType (VarT v) = ppr v
505 pprParendType (ConT c) = ppr c
506 pprParendType (TupleT 0) = text "()"
507 pprParendType (TupleT n) = parens (hcat (replicate (n-1) comma))
508 pprParendType (UnboxedTupleT n) = hashParens $ hcat $ replicate (n-1) comma
509 pprParendType ArrowT = parens (text "->")
510 pprParendType ListT = text "[]"
511 pprParendType (LitT l) = pprTyLit l
512 pprParendType (PromotedT c) = text "'" <> ppr c
513 pprParendType (PromotedTupleT 0) = text "'()"
514 pprParendType (PromotedTupleT n) = quoteParens (hcat (replicate (n-1) comma))
515 pprParendType PromotedNilT = text "'[]"
516 pprParendType PromotedConsT = text "(':)"
517 pprParendType StarT = char '*'
518 pprParendType ConstraintT = text "Constraint"
519 pprParendType (SigT ty k) = parens (ppr ty <+> text "::" <+> ppr k)
520 pprParendType (WildCardT mbName) = char '_' <> maybe empty ppr mbName
521 pprParendType (InfixT x n y) = parens (ppr x <+> pprName' Infix n <+> ppr y)
522 pprParendType t@(UInfixT {}) = parens (pprUInfixT t)
523 pprParendType (ParensT t) = ppr t
524 pprParendType other = parens (ppr other)
525
526 pprUInfixT :: Type -> Doc
527 pprUInfixT (UInfixT x n y) = pprUInfixT x <+> pprName' Infix n <+> pprUInfixT y
528 pprUInfixT t = ppr t
529
530 instance Ppr Type where
531 ppr (ForallT tvars ctxt ty)
532 = text "forall" <+> hsep (map ppr tvars) <+> text "."
533 <+> sep [pprCxt ctxt, ppr ty]
534 ppr ty = pprTyApp (split ty)
535 -- Works, in a degnerate way, for SigT, and puts parens round (ty :: kind)
536 -- See Note [Pretty-printing kind signatures]
537
538 {- Note [Pretty-printing kind signatures]
539 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
540 GHC's parser only recognises a kind signature in a type when there are
541 parens around it. E.g. the parens are required here:
542 f :: (Int :: *)
543 type instance F Int = (Bool :: *)
544 So we always print a SigT with parens (see Trac #10050). -}
545
546 pprTyApp :: (Type, [Type]) -> Doc
547 pprTyApp (ArrowT, [arg1,arg2]) = sep [pprFunArgType arg1 <+> text "->", ppr arg2]
548 pprTyApp (EqualityT, [arg1, arg2]) =
549 sep [pprFunArgType arg1 <+> text "~", ppr arg2]
550 pprTyApp (ListT, [arg]) = brackets (ppr arg)
551 pprTyApp (TupleT n, args)
552 | length args == n = parens (sep (punctuate comma (map ppr args)))
553 pprTyApp (PromotedTupleT n, args)
554 | length args == n = quoteParens (sep (punctuate comma (map ppr args)))
555 pprTyApp (fun, args) = pprParendType fun <+> sep (map pprParendType args)
556
557 pprFunArgType :: Type -> Doc -- Should really use a precedence argument
558 -- Everything except forall and (->) binds more tightly than (->)
559 pprFunArgType ty@(ForallT {}) = parens (ppr ty)
560 pprFunArgType ty@((ArrowT `AppT` _) `AppT` _) = parens (ppr ty)
561 pprFunArgType ty@(SigT _ _) = parens (ppr ty)
562 pprFunArgType ty = ppr ty
563
564 split :: Type -> (Type, [Type]) -- Split into function and args
565 split t = go t []
566 where go (AppT t1 t2) args = go t1 (t2:args)
567 go ty args = (ty, args)
568
569 pprTyLit :: TyLit -> Doc
570 pprTyLit (NumTyLit n) = integer n
571 pprTyLit (StrTyLit s) = text (show s)
572
573 instance Ppr TyLit where
574 ppr = pprTyLit
575
576 ------------------------------
577 instance Ppr TyVarBndr where
578 ppr (PlainTV nm) = ppr nm
579 ppr (KindedTV nm k) = parens (ppr nm <+> dcolon <+> ppr k)
580
581 instance Ppr Role where
582 ppr NominalR = text "nominal"
583 ppr RepresentationalR = text "representational"
584 ppr PhantomR = text "phantom"
585 ppr InferR = text "_"
586
587 ------------------------------
588 pprCxt :: Cxt -> Doc
589 pprCxt [] = empty
590 pprCxt [t] = ppr t <+> text "=>"
591 pprCxt ts = parens (sep $ punctuate comma $ map ppr ts) <+> text "=>"
592
593 ------------------------------
594 instance Ppr Range where
595 ppr = brackets . pprRange
596 where pprRange :: Range -> Doc
597 pprRange (FromR e) = ppr e <> text ".."
598 pprRange (FromThenR e1 e2) = ppr e1 <> text ","
599 <> ppr e2 <> text ".."
600 pprRange (FromToR e1 e2) = ppr e1 <> text ".." <> ppr e2
601 pprRange (FromThenToR e1 e2 e3) = ppr e1 <> text ","
602 <> ppr e2 <> text ".."
603 <> ppr e3
604
605 ------------------------------
606 where_clause :: [Dec] -> Doc
607 where_clause [] = empty
608 where_clause ds = nest nestDepth $ text "where" <+> vcat (map (ppr_dec False) ds)
609
610 showtextl :: Show a => a -> Doc
611 showtextl = text . map toLower . show
612
613 hashParens :: Doc -> Doc
614 hashParens d = text "(# " <> d <> text " #)"
615
616 quoteParens :: Doc -> Doc
617 quoteParens d = text "'(" <> d <> text ")"
618
619 -----------------------------
620 instance Ppr Loc where
621 ppr (Loc { loc_module = md
622 , loc_package = pkg
623 , loc_start = (start_ln, start_col)
624 , loc_end = (end_ln, end_col) })
625 = hcat [ text pkg, colon, text md, colon
626 , parens $ int start_ln <> comma <> int start_col
627 , text "-"
628 , parens $ int end_ln <> comma <> int end_col ]