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