Correctly pretty-print let and do expressions.
[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 Exp where
83 ppr = pprExp noPrec
84
85 pprPrefixOcc :: Name -> Doc
86 -- Print operators with parens around them
87 pprPrefixOcc n = parensIf (isSymOcc n) (ppr n)
88
89 isSymOcc :: Name -> Bool
90 isSymOcc n
91 = case nameBase n of
92 [] -> True -- Empty name; weird
93 (c:_) -> isSymbolASCII c || (ord c > 0x7f && isSymbol c)
94 -- c.f. OccName.startsVarSym in GHC itself
95
96 isSymbolASCII :: Char -> Bool
97 isSymbolASCII c = c `elem` "!#$%&*+./<=>?@\\^|~-"
98
99 pprInfixExp :: Exp -> Doc
100 pprInfixExp (VarE v) = pprName' Infix v
101 pprInfixExp (ConE v) = pprName' Infix v
102 pprInfixExp _ = text "<<Non-variable/constructor in infix context>>"
103
104 pprExp :: Precedence -> Exp -> Doc
105 pprExp _ (VarE v) = pprName' Applied v
106 pprExp _ (ConE c) = pprName' Applied c
107 pprExp i (LitE l) = pprLit i l
108 pprExp i (AppE e1 e2) = parensIf (i >= appPrec) $ pprExp opPrec e1
109 <+> pprExp appPrec e2
110 pprExp _ (ParensE e) = parens (pprExp noPrec e)
111 pprExp i (UInfixE e1 op e2)
112 = parensIf (i > unopPrec) $ pprExp unopPrec e1
113 <+> pprInfixExp op
114 <+> pprExp unopPrec e2
115 pprExp i (InfixE (Just e1) op (Just e2))
116 = parensIf (i >= opPrec) $ pprExp opPrec e1
117 <+> pprInfixExp op
118 <+> pprExp opPrec e2
119 pprExp _ (InfixE me1 op me2) = parens $ pprMaybeExp noPrec me1
120 <+> pprInfixExp op
121 <+> pprMaybeExp noPrec me2
122 pprExp i (LamE ps e) = parensIf (i > noPrec) $ char '\\' <> hsep (map (pprPat appPrec) ps)
123 <+> text "->" <+> ppr e
124 pprExp i (LamCaseE ms) = parensIf (i > noPrec)
125 $ text "\\case" $$ nest nestDepth (ppr ms)
126 pprExp _ (TupE es) = parens $ sep $ punctuate comma $ map ppr es
127 pprExp _ (UnboxedTupE es) = hashParens $ sep $ punctuate comma $ map ppr es
128 -- Nesting in Cond is to avoid potential problems in do statments
129 pprExp i (CondE guard true false)
130 = parensIf (i > noPrec) $ sep [text "if" <+> ppr guard,
131 nest 1 $ text "then" <+> ppr true,
132 nest 1 $ text "else" <+> ppr false]
133 pprExp i (MultiIfE alts)
134 = parensIf (i > noPrec) $ vcat $
135 case alts of
136 [] -> [text "if {}"]
137 (alt : alts') -> text "if" <+> pprGuarded arrow alt
138 : map (nest 3 . pprGuarded arrow) alts'
139 pprExp i (LetE ds_ e) = parensIf (i > noPrec) $ text "let" <+> pprDecs ds_
140 $$ text " in" <+> ppr e
141 where
142 pprDecs [] = empty
143 pprDecs [d] = ppr d
144 pprDecs ds = braces $ sep $ punctuate semi $ map ppr ds
145
146 pprExp i (CaseE e ms)
147 = parensIf (i > noPrec) $ text "case" <+> ppr e <+> text "of"
148 $$ nest nestDepth (ppr ms)
149 pprExp i (DoE ss_) = parensIf (i > noPrec) $ text "do" <+> pprStms ss_
150 where
151 pprStms [] = empty
152 pprStms [s] = ppr s
153 pprStms ss = braces $ sep $ punctuate semi $ map ppr ss
154
155 pprExp _ (CompE []) = text "<<Empty CompExp>>"
156 -- This will probably break with fixity declarations - would need a ';'
157 pprExp _ (CompE ss) = text "[" <> ppr s
158 <+> text "|"
159 <+> (sep $ punctuate comma $ map ppr ss')
160 <> text "]"
161 where s = last ss
162 ss' = init ss
163 pprExp _ (ArithSeqE d) = ppr d
164 pprExp _ (ListE es) = brackets $ sep $ punctuate comma $ map ppr es
165 pprExp i (SigE e t) = parensIf (i > noPrec) $ ppr e <+> text "::" <+> ppr t
166 pprExp _ (RecConE nm fs) = ppr nm <> braces (pprFields fs)
167 pprExp _ (RecUpdE e fs) = pprExp appPrec e <> braces (pprFields fs)
168
169 pprFields :: [(Name,Exp)] -> Doc
170 pprFields = sep . punctuate comma . map (\(s,e) -> ppr s <+> equals <+> ppr e)
171
172 pprMaybeExp :: Precedence -> Maybe Exp -> Doc
173 pprMaybeExp _ Nothing = empty
174 pprMaybeExp i (Just e) = pprExp i e
175
176 ------------------------------
177 instance Ppr Stmt where
178 ppr (BindS p e) = ppr p <+> text "<-" <+> ppr e
179 ppr (LetS ds) = text "let" <+> ppr ds
180 ppr (NoBindS e) = ppr e
181 ppr (ParS sss) = sep $ punctuate (text "|")
182 $ map (sep . punctuate comma . map ppr) sss
183
184 ------------------------------
185 instance Ppr Match where
186 ppr (Match p rhs ds) = ppr p <+> pprBody False rhs
187 $$ where_clause ds
188
189 ------------------------------
190 pprGuarded :: Doc -> (Guard, Exp) -> Doc
191 pprGuarded eqDoc (guard, expr) = case guard of
192 NormalG guardExpr -> char '|' <+> ppr guardExpr <+> eqDoc <+> ppr expr
193 PatG stmts -> char '|' <+> vcat (punctuate comma $ map ppr stmts) $$
194 nest nestDepth (eqDoc <+> ppr expr)
195
196 ------------------------------
197 pprBody :: Bool -> Body -> Doc
198 pprBody eq body = case body of
199 GuardedB xs -> nest nestDepth $ vcat $ map (pprGuarded eqDoc) xs
200 NormalB e -> eqDoc <+> ppr e
201 where eqDoc | eq = equals
202 | otherwise = arrow
203
204 ------------------------------
205 pprLit :: Precedence -> Lit -> Doc
206 pprLit i (IntPrimL x) = parensIf (i > noPrec && x < 0)
207 (integer x <> char '#')
208 pprLit _ (WordPrimL x) = integer x <> text "##"
209 pprLit i (FloatPrimL x) = parensIf (i > noPrec && x < 0)
210 (float (fromRational x) <> char '#')
211 pprLit i (DoublePrimL x) = parensIf (i > noPrec && x < 0)
212 (double (fromRational x) <> text "##")
213 pprLit i (IntegerL x) = parensIf (i > noPrec && x < 0) (integer x)
214 pprLit _ (CharL c) = text (show c)
215 pprLit _ (StringL s) = pprString s
216 pprLit _ (StringPrimL s) = pprString (bytesToString s) <> char '#'
217 pprLit i (RationalL rat) = parensIf (i > noPrec) $
218 integer (numerator rat) <+> char '/'
219 <+> integer (denominator rat)
220
221 bytesToString :: [Word8] -> String
222 bytesToString = map (chr . fromIntegral)
223
224 pprString :: String -> Doc
225 -- Print newlines as newlines with Haskell string escape notation,
226 -- not as '\n'. For other non-printables use regular escape notation.
227 pprString s = vcat (map text (showMultiLineString s))
228
229 ------------------------------
230 instance Ppr Pat where
231 ppr = pprPat noPrec
232
233 pprPat :: Precedence -> Pat -> Doc
234 pprPat i (LitP l) = pprLit i l
235 pprPat _ (VarP v) = pprName' Applied v
236 pprPat _ (TupP ps) = parens $ sep $ punctuate comma $ map ppr ps
237 pprPat _ (UnboxedTupP ps) = hashParens $ sep $ punctuate comma $ map ppr ps
238 pprPat i (ConP s ps) = parensIf (i >= appPrec) $ pprName' Applied s
239 <+> sep (map (pprPat appPrec) ps)
240 pprPat _ (ParensP p) = parens $ pprPat noPrec p
241 pprPat i (UInfixP p1 n p2)
242 = parensIf (i > unopPrec) (pprPat unopPrec p1 <+>
243 pprName' Infix n <+>
244 pprPat unopPrec p2)
245 pprPat i (InfixP p1 n p2)
246 = parensIf (i >= opPrec) (pprPat opPrec p1 <+>
247 pprName' Infix n <+>
248 pprPat opPrec p2)
249 pprPat i (TildeP p) = parensIf (i > noPrec) $ char '~' <> pprPat appPrec p
250 pprPat i (BangP p) = parensIf (i > noPrec) $ char '!' <> pprPat appPrec p
251 pprPat i (AsP v p) = parensIf (i > noPrec) $ ppr v <> text "@"
252 <> pprPat appPrec p
253 pprPat _ WildP = text "_"
254 pprPat _ (RecP nm fs)
255 = parens $ ppr nm
256 <+> braces (sep $ punctuate comma $
257 map (\(s,p) -> ppr s <+> equals <+> ppr p) fs)
258 pprPat _ (ListP ps) = brackets $ sep $ punctuate comma $ map ppr ps
259 pprPat i (SigP p t) = parensIf (i > noPrec) $ ppr p <+> text "::" <+> ppr t
260 pprPat _ (ViewP e p) = parens $ pprExp noPrec e <+> text "->" <+> pprPat noPrec p
261
262 ------------------------------
263 instance Ppr Dec where
264 ppr = ppr_dec True
265
266 ppr_dec :: Bool -- declaration on the toplevel?
267 -> Dec
268 -> Doc
269 ppr_dec _ (FunD f cs) = vcat $ map (\c -> pprPrefixOcc f <+> ppr c) cs
270 ppr_dec _ (ValD p r ds) = ppr p <+> pprBody True r
271 $$ where_clause ds
272 ppr_dec _ (TySynD t xs rhs)
273 = ppr_tySyn empty t (hsep (map ppr xs)) rhs
274 ppr_dec _ (DataD ctxt t xs cs decs)
275 = ppr_data empty ctxt t (hsep (map ppr xs)) cs decs
276 ppr_dec _ (NewtypeD ctxt t xs c decs)
277 = ppr_newtype empty ctxt t (sep (map ppr xs)) c decs
278 ppr_dec _ (ClassD ctxt c xs fds ds)
279 = text "class" <+> pprCxt ctxt <+> ppr c <+> hsep (map ppr xs) <+> ppr fds
280 $$ where_clause ds
281 ppr_dec _ (InstanceD ctxt i ds) = text "instance" <+> pprCxt ctxt <+> ppr i
282 $$ where_clause ds
283 ppr_dec _ (SigD f t) = pprPrefixOcc f <+> text "::" <+> ppr t
284 ppr_dec _ (ForeignD f) = ppr f
285 ppr_dec _ (InfixD fx n) = pprFixity n fx
286 ppr_dec _ (PragmaD p) = ppr p
287 ppr_dec isTop (FamilyD flav tc tvs k)
288 = ppr flav <+> maybeFamily <+> ppr tc <+> hsep (map ppr tvs) <+> maybeKind
289 where
290 maybeFamily | isTop = text "family"
291 | otherwise = empty
292
293 maybeKind | (Just k') <- k = text "::" <+> ppr k'
294 | otherwise = empty
295 ppr_dec isTop (DataInstD ctxt tc tys cs decs)
296 = ppr_data maybeInst ctxt tc (sep (map pprParendType tys)) cs decs
297 where
298 maybeInst | isTop = text "instance"
299 | otherwise = empty
300 ppr_dec isTop (NewtypeInstD ctxt tc tys c decs)
301 = ppr_newtype maybeInst ctxt tc (sep (map pprParendType tys)) c decs
302 where
303 maybeInst | isTop = text "instance"
304 | otherwise = empty
305 ppr_dec isTop (TySynInstD tc (TySynEqn tys rhs))
306 = ppr_tySyn maybeInst tc (sep (map pprParendType tys)) rhs
307 where
308 maybeInst | isTop = text "instance"
309 | otherwise = empty
310 ppr_dec _ (ClosedTypeFamilyD tc tvs mkind eqns)
311 = hang (hsep [ text "type family", ppr tc, hsep (map ppr tvs), maybeKind
312 , text "where" ])
313 nestDepth (vcat (map ppr_eqn eqns))
314 where
315 maybeKind | (Just k') <- mkind = text "::" <+> ppr k'
316 | otherwise = empty
317 ppr_eqn (TySynEqn lhs rhs)
318 = ppr tc <+> sep (map pprParendType lhs) <+> text "=" <+> ppr rhs
319
320 ppr_dec _ (RoleAnnotD name roles)
321 = hsep [ text "type role", ppr name ] <+> hsep (map ppr roles)
322
323 ppr_data :: Doc -> Cxt -> Name -> Doc -> [Con] -> [Name] -> Doc
324 ppr_data maybeInst ctxt t argsDoc cs decs
325 = sep [text "data" <+> maybeInst
326 <+> pprCxt ctxt
327 <+> ppr t <+> argsDoc,
328 nest nestDepth (sep (pref $ map ppr cs)),
329 if null decs
330 then empty
331 else nest nestDepth
332 $ text "deriving"
333 <+> parens (hsep $ punctuate comma $ map ppr decs)]
334 where
335 pref :: [Doc] -> [Doc]
336 pref [] = [] -- No constructors; can't happen in H98
337 pref (d:ds) = (char '=' <+> d):map (char '|' <+>) ds
338
339 ppr_newtype :: Doc -> Cxt -> Name -> Doc -> Con -> [Name] -> Doc
340 ppr_newtype maybeInst ctxt t argsDoc c decs
341 = sep [text "newtype" <+> maybeInst
342 <+> pprCxt ctxt
343 <+> ppr t <+> argsDoc,
344 nest 2 (char '=' <+> ppr c),
345 if null decs
346 then empty
347 else nest nestDepth
348 $ text "deriving"
349 <+> parens (hsep $ punctuate comma $ map ppr decs)]
350
351 ppr_tySyn :: Doc -> Name -> Doc -> Type -> Doc
352 ppr_tySyn maybeInst t argsDoc rhs
353 = text "type" <+> maybeInst <+> ppr t <+> argsDoc <+> text "=" <+> ppr rhs
354
355 ------------------------------
356 instance Ppr FunDep where
357 ppr (FunDep xs ys) = hsep (map ppr xs) <+> text "->" <+> hsep (map ppr ys)
358 ppr_list [] = empty
359 ppr_list xs = char '|' <+> sep (punctuate (text ", ") (map ppr xs))
360
361 ------------------------------
362 instance Ppr FamFlavour where
363 ppr DataFam = text "data"
364 ppr TypeFam = text "type"
365
366 ------------------------------
367 instance Ppr Foreign where
368 ppr (ImportF callconv safety impent as typ)
369 = text "foreign import"
370 <+> showtextl callconv
371 <+> showtextl safety
372 <+> text (show impent)
373 <+> ppr as
374 <+> text "::" <+> ppr typ
375 ppr (ExportF callconv expent as typ)
376 = text "foreign export"
377 <+> showtextl callconv
378 <+> text (show expent)
379 <+> ppr as
380 <+> text "::" <+> ppr typ
381
382 ------------------------------
383 instance Ppr Pragma where
384 ppr (InlineP n inline rm phases)
385 = text "{-#"
386 <+> ppr inline
387 <+> ppr rm
388 <+> ppr phases
389 <+> ppr n
390 <+> text "#-}"
391 ppr (SpecialiseP n ty inline phases)
392 = text "{-# SPECIALISE"
393 <+> maybe empty ppr inline
394 <+> ppr phases
395 <+> sep [ ppr n <+> text "::"
396 , nest 2 $ ppr ty ]
397 <+> text "#-}"
398 ppr (SpecialiseInstP inst)
399 = text "{-# SPECIALISE instance" <+> ppr inst <+> text "#-}"
400 ppr (RuleP n bndrs lhs rhs phases)
401 = sep [ text "{-# RULES" <+> pprString n <+> ppr phases
402 , nest 4 $ ppr_forall <+> ppr lhs
403 , nest 4 $ char '=' <+> ppr rhs <+> text "#-}" ]
404 where ppr_forall | null bndrs = empty
405 | otherwise = text "forall"
406 <+> fsep (map ppr bndrs)
407 <+> char '.'
408 ppr (AnnP tgt expr)
409 = text "{-# ANN" <+> target1 tgt <+> ppr expr <+> text "#-}"
410 where target1 ModuleAnnotation = text "module"
411 target1 (TypeAnnotation t) = text "type" <+> ppr t
412 target1 (ValueAnnotation v) = ppr v
413
414 ------------------------------
415 instance Ppr Inline where
416 ppr NoInline = text "NOINLINE"
417 ppr Inline = text "INLINE"
418 ppr Inlinable = text "INLINABLE"
419
420 ------------------------------
421 instance Ppr RuleMatch where
422 ppr ConLike = text "CONLIKE"
423 ppr FunLike = empty
424
425 ------------------------------
426 instance Ppr Phases where
427 ppr AllPhases = empty
428 ppr (FromPhase i) = brackets $ int i
429 ppr (BeforePhase i) = brackets $ char '~' <> int i
430
431 ------------------------------
432 instance Ppr RuleBndr where
433 ppr (RuleVar n) = ppr n
434 ppr (TypedRuleVar n ty) = parens $ ppr n <+> text "::" <+> ppr ty
435
436 ------------------------------
437 instance Ppr Clause where
438 ppr (Clause ps rhs ds) = hsep (map (pprPat appPrec) ps) <+> pprBody True rhs
439 $$ where_clause ds
440
441 ------------------------------
442 instance Ppr Con where
443 ppr (NormalC c sts) = ppr c <+> sep (map pprStrictType sts)
444 ppr (RecC c vsts)
445 = ppr c <+> braces (sep (punctuate comma $ map pprVarStrictType vsts))
446 ppr (InfixC st1 c st2) = pprStrictType st1
447 <+> pprName' Infix c
448 <+> pprStrictType st2
449 ppr (ForallC ns ctxt con) = text "forall" <+> hsep (map ppr ns)
450 <+> char '.' <+> sep [pprCxt ctxt, ppr con]
451
452 ------------------------------
453 pprVarStrictType :: (Name, Strict, Type) -> Doc
454 -- Slight infelicity: with print non-atomic type with parens
455 pprVarStrictType (v, str, t) = ppr v <+> text "::" <+> pprStrictType (str, t)
456
457 ------------------------------
458 pprStrictType :: (Strict, Type) -> Doc
459 -- Prints with parens if not already atomic
460 pprStrictType (IsStrict, t) = char '!' <> pprParendType t
461 pprStrictType (NotStrict, t) = pprParendType t
462 pprStrictType (Unpacked, t) = text "{-# UNPACK #-} !" <> pprParendType t
463
464 ------------------------------
465 pprParendType :: Type -> Doc
466 pprParendType (VarT v) = ppr v
467 pprParendType (ConT c) = ppr c
468 pprParendType (TupleT 0) = text "()"
469 pprParendType (TupleT n) = parens (hcat (replicate (n-1) comma))
470 pprParendType (UnboxedTupleT n) = hashParens $ hcat $ replicate (n-1) comma
471 pprParendType ArrowT = parens (text "->")
472 pprParendType ListT = text "[]"
473 pprParendType (LitT l) = pprTyLit l
474 pprParendType (PromotedT c) = text "'" <> ppr c
475 pprParendType (PromotedTupleT 0) = text "'()"
476 pprParendType (PromotedTupleT n) = quoteParens (hcat (replicate (n-1) comma))
477 pprParendType PromotedNilT = text "'[]"
478 pprParendType PromotedConsT = text "(':)"
479 pprParendType StarT = char '*'
480 pprParendType ConstraintT = text "Constraint"
481 pprParendType other = parens (ppr other)
482
483 instance Ppr Type where
484 ppr (ForallT tvars ctxt ty)
485 = text "forall" <+> hsep (map ppr tvars) <+> text "."
486 <+> sep [pprCxt ctxt, ppr ty]
487 ppr (SigT ty k) = ppr ty <+> text "::" <+> ppr k
488 ppr ty = pprTyApp (split ty)
489
490 pprTyApp :: (Type, [Type]) -> Doc
491 pprTyApp (ArrowT, [arg1,arg2]) = sep [pprFunArgType arg1 <+> text "->", ppr arg2]
492 pprTyApp (ListT, [arg]) = brackets (ppr arg)
493 pprTyApp (TupleT n, args)
494 | length args == n = parens (sep (punctuate comma (map ppr args)))
495 pprTyApp (PromotedTupleT n, args)
496 | length args == n = quoteParens (sep (punctuate comma (map ppr args)))
497 pprTyApp (fun, args) = pprParendType fun <+> sep (map pprParendType args)
498
499 pprFunArgType :: Type -> Doc -- Should really use a precedence argument
500 -- Everything except forall and (->) binds more tightly than (->)
501 pprFunArgType ty@(ForallT {}) = parens (ppr ty)
502 pprFunArgType ty@((ArrowT `AppT` _) `AppT` _) = parens (ppr ty)
503 pprFunArgType ty@(SigT _ _) = parens (ppr ty)
504 pprFunArgType ty = ppr ty
505
506 split :: Type -> (Type, [Type]) -- Split into function and args
507 split t = go t []
508 where go (AppT t1 t2) args = go t1 (t2:args)
509 go ty args = (ty, args)
510
511 pprTyLit :: TyLit -> Doc
512 pprTyLit (NumTyLit n) = integer n
513 pprTyLit (StrTyLit s) = text (show s)
514
515 instance Ppr TyLit where
516 ppr = pprTyLit
517
518 ------------------------------
519 instance Ppr TyVarBndr where
520 ppr (PlainTV nm) = ppr nm
521 ppr (KindedTV nm k) = parens (ppr nm <+> text "::" <+> ppr k)
522
523 instance Ppr Role where
524 ppr NominalR = text "nominal"
525 ppr RepresentationalR = text "representational"
526 ppr PhantomR = text "phantom"
527 ppr InferR = text "_"
528
529 ------------------------------
530 pprCxt :: Cxt -> Doc
531 pprCxt [] = empty
532 pprCxt [t] = ppr t <+> text "=>"
533 pprCxt ts = parens (sep $ punctuate comma $ map ppr ts) <+> text "=>"
534
535 ------------------------------
536 instance Ppr Pred where
537 ppr (ClassP cla tys) = ppr cla <+> sep (map pprParendType tys)
538 ppr (EqualP ty1 ty2) = pprFunArgType ty1 <+> char '~' <+> pprFunArgType ty2
539
540 ------------------------------
541 instance Ppr Range where
542 ppr = brackets . pprRange
543 where pprRange :: Range -> Doc
544 pprRange (FromR e) = ppr e <> text ".."
545 pprRange (FromThenR e1 e2) = ppr e1 <> text ","
546 <> ppr e2 <> text ".."
547 pprRange (FromToR e1 e2) = ppr e1 <> text ".." <> ppr e2
548 pprRange (FromThenToR e1 e2 e3) = ppr e1 <> text ","
549 <> ppr e2 <> text ".."
550 <> ppr e3
551
552 ------------------------------
553 where_clause :: [Dec] -> Doc
554 where_clause [] = empty
555 where_clause ds = nest nestDepth $ text "where" <+> vcat (map (ppr_dec False) ds)
556
557 showtextl :: Show a => a -> Doc
558 showtextl = text . map toLower . show
559
560 hashParens :: Doc -> Doc
561 hashParens d = text "(# " <> d <> text " #)"
562
563 quoteParens :: Doc -> Doc
564 quoteParens d = text "'(" <> d <> text ")"
565