Put parens around (ty :: kind) when pretty-printing TH syntax
[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 fix)
59 = text "Class op from" <+> ppr cls <> colon <+>
60 vcat [ppr_sig v ty, pprFixity v fix]
61 ppr (DataConI v ty tc fix)
62 = text "Constructor from" <+> ppr tc <> colon <+>
63 vcat [ppr_sig v ty, pprFixity v fix]
64 ppr (TyVarI v ty)
65 = text "Type variable" <+> ppr v <+> equals <+> ppr ty
66 ppr (VarI v ty mb_d fix)
67 = vcat [ppr_sig v ty, pprFixity v fix,
68 case mb_d of { Nothing -> empty; Just d -> ppr d }]
69
70 ppr_sig :: Name -> Type -> Doc
71 ppr_sig v ty = ppr v <+> text "::" <+> ppr ty
72
73 pprFixity :: Name -> Fixity -> Doc
74 pprFixity _ f | f == defaultFixity = empty
75 pprFixity v (Fixity i d) = ppr_fix d <+> int i <+> ppr v
76 where ppr_fix InfixR = text "infixr"
77 ppr_fix InfixL = text "infixl"
78 ppr_fix InfixN = text "infix"
79
80
81 ------------------------------
82 instance Ppr Module where
83 ppr (Module pkg m) = text (pkgString pkg) <+> text (modString m)
84
85 instance Ppr ModuleInfo where
86 ppr (ModuleInfo imps) = text "Module" <+> vcat (map ppr imps)
87
88 ------------------------------
89 instance Ppr Exp where
90 ppr = pprExp noPrec
91
92 pprPrefixOcc :: Name -> Doc
93 -- Print operators with parens around them
94 pprPrefixOcc n = parensIf (isSymOcc n) (ppr n)
95
96 isSymOcc :: Name -> Bool
97 isSymOcc n
98 = case nameBase n of
99 [] -> True -- Empty name; weird
100 (c:_) -> isSymbolASCII c || (ord c > 0x7f && isSymbol c)
101 -- c.f. OccName.startsVarSym in GHC itself
102
103 isSymbolASCII :: Char -> Bool
104 isSymbolASCII c = c `elem` "!#$%&*+./<=>?@\\^|~-"
105
106 pprInfixExp :: Exp -> Doc
107 pprInfixExp (VarE v) = pprName' Infix v
108 pprInfixExp (ConE v) = pprName' Infix v
109 pprInfixExp _ = text "<<Non-variable/constructor in infix context>>"
110
111 pprExp :: Precedence -> Exp -> Doc
112 pprExp _ (VarE v) = pprName' Applied v
113 pprExp _ (ConE c) = pprName' Applied c
114 pprExp i (LitE l) = pprLit i l
115 pprExp i (AppE e1 e2) = parensIf (i >= appPrec) $ pprExp opPrec e1
116 <+> pprExp appPrec e2
117 pprExp _ (ParensE e) = parens (pprExp noPrec e)
118 pprExp i (UInfixE e1 op e2)
119 = parensIf (i > unopPrec) $ pprExp unopPrec e1
120 <+> pprInfixExp op
121 <+> pprExp unopPrec e2
122 pprExp i (InfixE (Just e1) op (Just e2))
123 = parensIf (i >= opPrec) $ pprExp opPrec e1
124 <+> pprInfixExp op
125 <+> pprExp opPrec e2
126 pprExp _ (InfixE me1 op me2) = parens $ pprMaybeExp noPrec me1
127 <+> pprInfixExp op
128 <+> pprMaybeExp noPrec me2
129 pprExp i (LamE ps e) = parensIf (i > noPrec) $ char '\\' <> hsep (map (pprPat appPrec) ps)
130 <+> text "->" <+> ppr e
131 pprExp i (LamCaseE ms) = parensIf (i > noPrec)
132 $ text "\\case" $$ nest nestDepth (ppr ms)
133 pprExp _ (TupE es) = parens $ sep $ punctuate comma $ map ppr es
134 pprExp _ (UnboxedTupE es) = hashParens $ sep $ punctuate comma $ map ppr es
135 -- Nesting in Cond is to avoid potential problems in do statments
136 pprExp i (CondE guard true false)
137 = parensIf (i > noPrec) $ sep [text "if" <+> ppr guard,
138 nest 1 $ text "then" <+> ppr true,
139 nest 1 $ text "else" <+> ppr false]
140 pprExp i (MultiIfE alts)
141 = parensIf (i > noPrec) $ vcat $
142 case alts of
143 [] -> [text "if {}"]
144 (alt : alts') -> text "if" <+> pprGuarded arrow alt
145 : map (nest 3 . pprGuarded arrow) alts'
146 pprExp i (LetE ds_ e) = parensIf (i > noPrec) $ text "let" <+> pprDecs ds_
147 $$ text " in" <+> ppr e
148 where
149 pprDecs [] = empty
150 pprDecs [d] = ppr d
151 pprDecs ds = braces $ sep $ punctuate semi $ map ppr ds
152
153 pprExp i (CaseE e ms)
154 = parensIf (i > noPrec) $ text "case" <+> ppr e <+> text "of"
155 $$ nest nestDepth (ppr ms)
156 pprExp i (DoE ss_) = parensIf (i > noPrec) $ text "do" <+> pprStms ss_
157 where
158 pprStms [] = empty
159 pprStms [s] = ppr s
160 pprStms ss = braces $ sep $ punctuate semi $ map ppr ss
161
162 pprExp _ (CompE []) = text "<<Empty CompExp>>"
163 -- This will probably break with fixity declarations - would need a ';'
164 pprExp _ (CompE ss) = text "[" <> ppr s
165 <+> text "|"
166 <+> (sep $ punctuate comma $ map ppr ss')
167 <> text "]"
168 where s = last ss
169 ss' = init ss
170 pprExp _ (ArithSeqE d) = ppr d
171 pprExp _ (ListE es) = brackets $ sep $ punctuate comma $ map ppr es
172 pprExp i (SigE e t) = parensIf (i > noPrec) $ ppr e <+> text "::" <+> ppr t
173 pprExp _ (RecConE nm fs) = ppr nm <> braces (pprFields fs)
174 pprExp _ (RecUpdE e fs) = pprExp appPrec e <> braces (pprFields fs)
175 pprExp i (StaticE e) = parensIf (i >= appPrec) $
176 text "static"<+> pprExp appPrec e
177
178 pprFields :: [(Name,Exp)] -> Doc
179 pprFields = sep . punctuate comma . map (\(s,e) -> ppr s <+> equals <+> ppr e)
180
181 pprMaybeExp :: Precedence -> Maybe Exp -> Doc
182 pprMaybeExp _ Nothing = empty
183 pprMaybeExp i (Just e) = pprExp i e
184
185 ------------------------------
186 instance Ppr Stmt where
187 ppr (BindS p e) = ppr p <+> text "<-" <+> ppr e
188 ppr (LetS ds) = text "let" <+> ppr ds
189 ppr (NoBindS e) = ppr e
190 ppr (ParS sss) = sep $ punctuate (text "|")
191 $ map (sep . punctuate comma . map ppr) sss
192
193 ------------------------------
194 instance Ppr Match where
195 ppr (Match p rhs ds) = ppr p <+> pprBody False rhs
196 $$ where_clause ds
197
198 ------------------------------
199 pprGuarded :: Doc -> (Guard, Exp) -> Doc
200 pprGuarded eqDoc (guard, expr) = case guard of
201 NormalG guardExpr -> char '|' <+> ppr guardExpr <+> eqDoc <+> ppr expr
202 PatG stmts -> char '|' <+> vcat (punctuate comma $ map ppr stmts) $$
203 nest nestDepth (eqDoc <+> ppr expr)
204
205 ------------------------------
206 pprBody :: Bool -> Body -> Doc
207 pprBody eq body = case body of
208 GuardedB xs -> nest nestDepth $ vcat $ map (pprGuarded eqDoc) xs
209 NormalB e -> eqDoc <+> ppr e
210 where eqDoc | eq = equals
211 | otherwise = arrow
212
213 ------------------------------
214 instance Ppr Lit where
215 ppr = pprLit noPrec
216
217 pprLit :: Precedence -> Lit -> Doc
218 pprLit i (IntPrimL x) = parensIf (i > noPrec && x < 0)
219 (integer x <> char '#')
220 pprLit _ (WordPrimL x) = integer x <> text "##"
221 pprLit i (FloatPrimL x) = parensIf (i > noPrec && x < 0)
222 (float (fromRational x) <> char '#')
223 pprLit i (DoublePrimL x) = parensIf (i > noPrec && x < 0)
224 (double (fromRational x) <> text "##")
225 pprLit i (IntegerL x) = parensIf (i > noPrec && x < 0) (integer x)
226 pprLit _ (CharL c) = text (show c)
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 <+> text "::" <+> 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 <+> text "::" <+> 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 (FamilyD flav tc tvs k)
300 = ppr flav <+> maybeFamily <+> ppr tc <+> hsep (map ppr tvs) <+> maybeKind
301 where
302 maybeFamily | isTop = text "family"
303 | otherwise = empty
304
305 maybeKind | (Just k') <- k = text "::" <+> ppr k'
306 | otherwise = empty
307 ppr_dec isTop (DataInstD ctxt tc tys cs decs)
308 = ppr_data maybeInst ctxt tc (sep (map pprParendType tys)) cs decs
309 where
310 maybeInst | isTop = text "instance"
311 | otherwise = empty
312 ppr_dec isTop (NewtypeInstD ctxt tc tys c decs)
313 = ppr_newtype maybeInst ctxt tc (sep (map pprParendType tys)) c decs
314 where
315 maybeInst | isTop = text "instance"
316 | otherwise = empty
317 ppr_dec isTop (TySynInstD tc (TySynEqn tys rhs))
318 = ppr_tySyn maybeInst tc (sep (map pprParendType tys)) rhs
319 where
320 maybeInst | isTop = text "instance"
321 | otherwise = empty
322 ppr_dec _ (ClosedTypeFamilyD tc tvs mkind eqns)
323 = hang (hsep [ text "type family", ppr tc, hsep (map ppr tvs), maybeKind
324 , text "where" ])
325 nestDepth (vcat (map ppr_eqn eqns))
326 where
327 maybeKind | (Just k') <- mkind = text "::" <+> ppr k'
328 | otherwise = empty
329 ppr_eqn (TySynEqn lhs rhs)
330 = ppr tc <+> sep (map pprParendType lhs) <+> text "=" <+> ppr rhs
331
332 ppr_dec _ (RoleAnnotD name roles)
333 = hsep [ text "type role", ppr name ] <+> hsep (map ppr roles)
334
335 ppr_dec _ (StandaloneDerivD cxt ty)
336 = hsep [ text "deriving instance", pprCxt cxt, ppr ty ]
337
338 ppr_dec _ (DefaultSigD n ty)
339 = hsep [ text "default", pprPrefixOcc n, text "::", ppr ty ]
340
341 ppr_data :: Doc -> Cxt -> Name -> Doc -> [Con] -> [Name] -> Doc
342 ppr_data maybeInst ctxt t argsDoc cs decs
343 = sep [text "data" <+> maybeInst
344 <+> pprCxt ctxt
345 <+> ppr t <+> argsDoc,
346 nest nestDepth (sep (pref $ map ppr cs)),
347 if null decs
348 then empty
349 else nest nestDepth
350 $ text "deriving"
351 <+> parens (hsep $ punctuate comma $ map ppr 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 -> [Name] -> 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"
367 <+> parens (hsep $ punctuate comma $ map ppr decs)]
368
369 ppr_tySyn :: Doc -> Name -> Doc -> Type -> Doc
370 ppr_tySyn maybeInst t argsDoc rhs
371 = text "type" <+> maybeInst <+> ppr t <+> argsDoc <+> text "=" <+> ppr rhs
372
373 ------------------------------
374 instance Ppr FunDep where
375 ppr (FunDep xs ys) = hsep (map ppr xs) <+> text "->" <+> hsep (map ppr ys)
376 ppr_list [] = empty
377 ppr_list xs = char '|' <+> sep (punctuate (text ", ") (map ppr xs))
378
379 ------------------------------
380 instance Ppr FamFlavour where
381 ppr DataFam = text "data"
382 ppr TypeFam = text "type"
383
384 ------------------------------
385 instance Ppr Foreign where
386 ppr (ImportF callconv safety impent as typ)
387 = text "foreign import"
388 <+> showtextl callconv
389 <+> showtextl safety
390 <+> text (show impent)
391 <+> ppr as
392 <+> text "::" <+> ppr typ
393 ppr (ExportF callconv expent as typ)
394 = text "foreign export"
395 <+> showtextl callconv
396 <+> text (show expent)
397 <+> ppr as
398 <+> text "::" <+> ppr typ
399
400 ------------------------------
401 instance Ppr Pragma where
402 ppr (InlineP n inline rm phases)
403 = text "{-#"
404 <+> ppr inline
405 <+> ppr rm
406 <+> ppr phases
407 <+> ppr n
408 <+> text "#-}"
409 ppr (SpecialiseP n ty inline phases)
410 = text "{-# SPECIALISE"
411 <+> maybe empty ppr inline
412 <+> ppr phases
413 <+> sep [ ppr n <+> text "::"
414 , nest 2 $ ppr ty ]
415 <+> text "#-}"
416 ppr (SpecialiseInstP inst)
417 = text "{-# SPECIALISE instance" <+> ppr inst <+> text "#-}"
418 ppr (RuleP n bndrs lhs rhs phases)
419 = sep [ text "{-# RULES" <+> pprString n <+> ppr phases
420 , nest 4 $ ppr_forall <+> ppr lhs
421 , nest 4 $ char '=' <+> ppr rhs <+> text "#-}" ]
422 where ppr_forall | null bndrs = empty
423 | otherwise = text "forall"
424 <+> fsep (map ppr bndrs)
425 <+> char '.'
426 ppr (AnnP tgt expr)
427 = text "{-# ANN" <+> target1 tgt <+> ppr expr <+> text "#-}"
428 where target1 ModuleAnnotation = text "module"
429 target1 (TypeAnnotation t) = text "type" <+> ppr t
430 target1 (ValueAnnotation v) = ppr v
431 ppr (LineP line file)
432 = text "{-# LINE" <+> int line <+> text (show file) <+> text "#-}"
433
434 ------------------------------
435 instance Ppr Inline where
436 ppr NoInline = text "NOINLINE"
437 ppr Inline = text "INLINE"
438 ppr Inlinable = text "INLINABLE"
439
440 ------------------------------
441 instance Ppr RuleMatch where
442 ppr ConLike = text "CONLIKE"
443 ppr FunLike = empty
444
445 ------------------------------
446 instance Ppr Phases where
447 ppr AllPhases = empty
448 ppr (FromPhase i) = brackets $ int i
449 ppr (BeforePhase i) = brackets $ char '~' <> int i
450
451 ------------------------------
452 instance Ppr RuleBndr where
453 ppr (RuleVar n) = ppr n
454 ppr (TypedRuleVar n ty) = parens $ ppr n <+> text "::" <+> ppr ty
455
456 ------------------------------
457 instance Ppr Clause where
458 ppr (Clause ps rhs ds) = hsep (map (pprPat appPrec) ps) <+> pprBody True rhs
459 $$ where_clause ds
460
461 ------------------------------
462 instance Ppr Con where
463 ppr (NormalC c sts) = ppr c <+> sep (map pprStrictType sts)
464 ppr (RecC c vsts)
465 = ppr c <+> braces (sep (punctuate comma $ map pprVarStrictType vsts))
466 ppr (InfixC st1 c st2) = pprStrictType st1
467 <+> pprName' Infix c
468 <+> pprStrictType st2
469 ppr (ForallC ns ctxt con) = text "forall" <+> hsep (map ppr ns)
470 <+> char '.' <+> sep [pprCxt ctxt, ppr con]
471
472 ------------------------------
473 pprVarStrictType :: (Name, Strict, Type) -> Doc
474 -- Slight infelicity: with print non-atomic type with parens
475 pprVarStrictType (v, str, t) = ppr v <+> text "::" <+> pprStrictType (str, t)
476
477 ------------------------------
478 pprStrictType :: (Strict, Type) -> Doc
479 -- Prints with parens if not already atomic
480 pprStrictType (IsStrict, t) = char '!' <> pprParendType t
481 pprStrictType (NotStrict, t) = pprParendType t
482 pprStrictType (Unpacked, t) = text "{-# UNPACK #-} !" <> pprParendType t
483
484 ------------------------------
485 pprParendType :: Type -> Doc
486 pprParendType (VarT v) = ppr v
487 pprParendType (ConT c) = ppr c
488 pprParendType (TupleT 0) = text "()"
489 pprParendType (TupleT n) = parens (hcat (replicate (n-1) comma))
490 pprParendType (UnboxedTupleT n) = hashParens $ hcat $ replicate (n-1) comma
491 pprParendType ArrowT = parens (text "->")
492 pprParendType ListT = text "[]"
493 pprParendType (LitT l) = pprTyLit l
494 pprParendType (PromotedT c) = text "'" <> ppr c
495 pprParendType (PromotedTupleT 0) = text "'()"
496 pprParendType (PromotedTupleT n) = quoteParens (hcat (replicate (n-1) comma))
497 pprParendType PromotedNilT = text "'[]"
498 pprParendType PromotedConsT = text "(':)"
499 pprParendType StarT = char '*'
500 pprParendType ConstraintT = text "Constraint"
501 pprParendType (SigT ty k) = parens (ppr ty <+> text "::" <+> ppr k)
502 pprParendType other = parens (ppr other)
503
504 instance Ppr Type where
505 ppr (ForallT tvars ctxt ty)
506 = text "forall" <+> hsep (map ppr tvars) <+> text "."
507 <+> sep [pprCxt ctxt, ppr ty]
508 ppr ty = pprTyApp (split ty)
509 -- Works, in a degnerate way, for SigT, and puts parens round (ty :: kind)
510 -- See Note [Pretty-printing kind signatures]
511
512 {- Note [Pretty-printing kind signatures]
513 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
514 GHC's parser only recognises a kind signature in a type when there are
515 parens around it. E.g. the parens are required here:
516 f :: (Int :: *)
517 type instance F Int = (Bool :: *)
518 So we always print a SigT with parens (see Trac #10050). -}
519
520
521 pprTyApp :: (Type, [Type]) -> Doc
522 pprTyApp (ArrowT, [arg1,arg2]) = sep [pprFunArgType arg1 <+> text "->", ppr arg2]
523 pprTyApp (EqualityT, [arg1, arg2]) =
524 sep [pprFunArgType arg1 <+> text "~", ppr arg2]
525 pprTyApp (ListT, [arg]) = brackets (ppr arg)
526 pprTyApp (TupleT n, args)
527 | length args == n = parens (sep (punctuate comma (map ppr args)))
528 pprTyApp (PromotedTupleT n, args)
529 | length args == n = quoteParens (sep (punctuate comma (map ppr args)))
530 pprTyApp (fun, args) = pprParendType fun <+> sep (map pprParendType args)
531
532 pprFunArgType :: Type -> Doc -- Should really use a precedence argument
533 -- Everything except forall and (->) binds more tightly than (->)
534 pprFunArgType ty@(ForallT {}) = parens (ppr ty)
535 pprFunArgType ty@((ArrowT `AppT` _) `AppT` _) = parens (ppr ty)
536 pprFunArgType ty@(SigT _ _) = parens (ppr ty)
537 pprFunArgType ty = ppr ty
538
539 split :: Type -> (Type, [Type]) -- Split into function and args
540 split t = go t []
541 where go (AppT t1 t2) args = go t1 (t2:args)
542 go ty args = (ty, args)
543
544 pprTyLit :: TyLit -> Doc
545 pprTyLit (NumTyLit n) = integer n
546 pprTyLit (StrTyLit s) = text (show s)
547
548 instance Ppr TyLit where
549 ppr = pprTyLit
550
551 ------------------------------
552 instance Ppr TyVarBndr where
553 ppr (PlainTV nm) = ppr nm
554 ppr (KindedTV nm k) = parens (ppr nm <+> text "::" <+> ppr k)
555
556 instance Ppr Role where
557 ppr NominalR = text "nominal"
558 ppr RepresentationalR = text "representational"
559 ppr PhantomR = text "phantom"
560 ppr InferR = text "_"
561
562 ------------------------------
563 pprCxt :: Cxt -> Doc
564 pprCxt [] = empty
565 pprCxt [t] = ppr t <+> text "=>"
566 pprCxt ts = parens (sep $ punctuate comma $ map ppr ts) <+> text "=>"
567
568 ------------------------------
569 instance Ppr Range where
570 ppr = brackets . pprRange
571 where pprRange :: Range -> Doc
572 pprRange (FromR e) = ppr e <> text ".."
573 pprRange (FromThenR e1 e2) = ppr e1 <> text ","
574 <> ppr e2 <> text ".."
575 pprRange (FromToR e1 e2) = ppr e1 <> text ".." <> ppr e2
576 pprRange (FromThenToR e1 e2 e3) = ppr e1 <> text ","
577 <> ppr e2 <> text ".."
578 <> ppr e3
579
580 ------------------------------
581 where_clause :: [Dec] -> Doc
582 where_clause [] = empty
583 where_clause ds = nest nestDepth $ text "where" <+> vcat (map (ppr_dec False) ds)
584
585 showtextl :: Show a => a -> Doc
586 showtextl = text . map toLower . show
587
588 hashParens :: Doc -> Doc
589 hashParens d = text "(# " <> d <> text " #)"
590
591 quoteParens :: Doc -> Doc
592 quoteParens d = text "'(" <> d <> text ")"
593
594 -----------------------------
595 instance Ppr Loc where
596 ppr (Loc { loc_module = md
597 , loc_package = pkg
598 , loc_start = (start_ln, start_col)
599 , loc_end = (end_ln, end_col) })
600 = hcat [ text pkg, colon, text md, colon
601 , parens $ int start_ln <> comma <> int start_col
602 , text "-"
603 , parens $ int end_ln <> comma <> int end_col ]