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