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