Support new role annotation syntax.
[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
399 ------------------------------
400 instance Ppr Inline where
401 ppr NoInline = text "NOINLINE"
402 ppr Inline = text "INLINE"
403 ppr Inlinable = text "INLINABLE"
404
405 ------------------------------
406 instance Ppr RuleMatch where
407 ppr ConLike = text "CONLIKE"
408 ppr FunLike = empty
409
410 ------------------------------
411 instance Ppr Phases where
412 ppr AllPhases = empty
413 ppr (FromPhase i) = brackets $ int i
414 ppr (BeforePhase i) = brackets $ char '~' <> int i
415
416 ------------------------------
417 instance Ppr RuleBndr where
418 ppr (RuleVar n) = ppr n
419 ppr (TypedRuleVar n ty) = parens $ ppr n <+> text "::" <+> ppr ty
420
421 ------------------------------
422 instance Ppr Clause where
423 ppr (Clause ps rhs ds) = hsep (map (pprPat appPrec) ps) <+> pprBody True rhs
424 $$ where_clause ds
425
426 ------------------------------
427 instance Ppr Con where
428 ppr (NormalC c sts) = ppr c <+> sep (map pprStrictType sts)
429 ppr (RecC c vsts)
430 = ppr c <+> braces (sep (punctuate comma $ map pprVarStrictType vsts))
431 ppr (InfixC st1 c st2) = pprStrictType st1
432 <+> pprName' Infix c
433 <+> pprStrictType st2
434 ppr (ForallC ns ctxt con) = text "forall" <+> hsep (map ppr ns)
435 <+> char '.' <+> sep [pprCxt ctxt, ppr con]
436
437 ------------------------------
438 pprVarStrictType :: (Name, Strict, Type) -> Doc
439 -- Slight infelicity: with print non-atomic type with parens
440 pprVarStrictType (v, str, t) = ppr v <+> text "::" <+> pprStrictType (str, t)
441
442 ------------------------------
443 pprStrictType :: (Strict, Type) -> Doc
444 -- Prints with parens if not already atomic
445 pprStrictType (IsStrict, t) = char '!' <> pprParendType t
446 pprStrictType (NotStrict, t) = pprParendType t
447 pprStrictType (Unpacked, t) = text "{-# UNPACK #-} !" <> pprParendType t
448
449 ------------------------------
450 pprParendType :: Type -> Doc
451 pprParendType (VarT v) = ppr v
452 pprParendType (ConT c) = ppr c
453 pprParendType (TupleT 0) = text "()"
454 pprParendType (TupleT n) = parens (hcat (replicate (n-1) comma))
455 pprParendType (UnboxedTupleT n) = hashParens $ hcat $ replicate (n-1) comma
456 pprParendType ArrowT = parens (text "->")
457 pprParendType ListT = text "[]"
458 pprParendType (LitT l) = pprTyLit l
459 pprParendType (PromotedT c) = text "'" <> ppr c
460 pprParendType (PromotedTupleT 0) = text "'()"
461 pprParendType (PromotedTupleT n) = quoteParens (hcat (replicate (n-1) comma))
462 pprParendType PromotedNilT = text "'[]"
463 pprParendType PromotedConsT = text "(':)"
464 pprParendType StarT = char '*'
465 pprParendType ConstraintT = text "Constraint"
466 pprParendType other = parens (ppr other)
467
468 instance Ppr Type where
469 ppr (ForallT tvars ctxt ty)
470 = text "forall" <+> hsep (map ppr tvars) <+> text "."
471 <+> sep [pprCxt ctxt, ppr ty]
472 ppr (SigT ty k) = ppr ty <+> text "::" <+> ppr k
473 ppr ty = pprTyApp (split ty)
474
475 pprTyApp :: (Type, [Type]) -> Doc
476 pprTyApp (ArrowT, [arg1,arg2]) = sep [pprFunArgType arg1 <+> text "->", ppr arg2]
477 pprTyApp (ListT, [arg]) = brackets (ppr arg)
478 pprTyApp (TupleT n, args)
479 | length args == n = parens (sep (punctuate comma (map ppr args)))
480 pprTyApp (PromotedTupleT n, args)
481 | length args == n = quoteParens (sep (punctuate comma (map ppr args)))
482 pprTyApp (fun, args) = pprParendType fun <+> sep (map pprParendType args)
483
484 pprFunArgType :: Type -> Doc -- Should really use a precedence argument
485 -- Everything except forall and (->) binds more tightly than (->)
486 pprFunArgType ty@(ForallT {}) = parens (ppr ty)
487 pprFunArgType ty@((ArrowT `AppT` _) `AppT` _) = parens (ppr ty)
488 pprFunArgType ty@(SigT _ _) = parens (ppr ty)
489 pprFunArgType ty = ppr ty
490
491 split :: Type -> (Type, [Type]) -- Split into function and args
492 split t = go t []
493 where go (AppT t1 t2) args = go t1 (t2:args)
494 go ty args = (ty, args)
495
496 pprTyLit :: TyLit -> Doc
497 pprTyLit (NumTyLit n) = integer n
498 pprTyLit (StrTyLit s) = text (show s)
499
500 instance Ppr TyLit where
501 ppr = pprTyLit
502
503 ------------------------------
504 instance Ppr TyVarBndr where
505 ppr (PlainTV nm) = ppr nm
506 ppr (KindedTV nm k) = parens (ppr nm <+> text "::" <+> ppr k)
507
508 instance Ppr Role where
509 ppr NominalR = text "nominal"
510 ppr RepresentationalR = text "representational"
511 ppr PhantomR = text "phantom"
512 ppr InferR = text "_"
513
514 ------------------------------
515 pprCxt :: Cxt -> Doc
516 pprCxt [] = empty
517 pprCxt [t] = ppr t <+> text "=>"
518 pprCxt ts = parens (sep $ punctuate comma $ map ppr ts) <+> text "=>"
519
520 ------------------------------
521 instance Ppr Pred where
522 ppr (ClassP cla tys) = ppr cla <+> sep (map pprParendType tys)
523 ppr (EqualP ty1 ty2) = pprFunArgType ty1 <+> char '~' <+> pprFunArgType ty2
524
525 ------------------------------
526 instance Ppr Range where
527 ppr = brackets . pprRange
528 where pprRange :: Range -> Doc
529 pprRange (FromR e) = ppr e <> text ".."
530 pprRange (FromThenR e1 e2) = ppr e1 <> text ","
531 <> ppr e2 <> text ".."
532 pprRange (FromToR e1 e2) = ppr e1 <> text ".." <> ppr e2
533 pprRange (FromThenToR e1 e2 e3) = ppr e1 <> text ","
534 <> ppr e2 <> text ".."
535 <> ppr e3
536
537 ------------------------------
538 where_clause :: [Dec] -> Doc
539 where_clause [] = empty
540 where_clause ds = nest nestDepth $ text "where" <+> vcat (map (ppr_dec False) ds)
541
542 showtextl :: Show a => a -> Doc
543 showtextl = text . map toLower . show
544
545 hashParens :: Doc -> Doc
546 hashParens d = text "(# " <> d <> text " #)"
547
548 quoteParens :: Doc -> Doc
549 quoteParens d = text "'(" <> d <> text ")"
550