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