Add support for LINE pragma in template-haskell
[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_data :: Doc -> Cxt -> Name -> Doc -> [Con] -> [Name] -> Doc
331 ppr_data maybeInst ctxt t argsDoc cs decs
332 = sep [text "data" <+> maybeInst
333 <+> pprCxt ctxt
334 <+> ppr t <+> argsDoc,
335 nest nestDepth (sep (pref $ map ppr cs)),
336 if null decs
337 then empty
338 else nest nestDepth
339 $ text "deriving"
340 <+> parens (hsep $ punctuate comma $ map ppr decs)]
341 where
342 pref :: [Doc] -> [Doc]
343 pref [] = [] -- No constructors; can't happen in H98
344 pref (d:ds) = (char '=' <+> d):map (char '|' <+>) ds
345
346 ppr_newtype :: Doc -> Cxt -> Name -> Doc -> Con -> [Name] -> Doc
347 ppr_newtype maybeInst ctxt t argsDoc c decs
348 = sep [text "newtype" <+> maybeInst
349 <+> pprCxt ctxt
350 <+> ppr t <+> argsDoc,
351 nest 2 (char '=' <+> ppr c),
352 if null decs
353 then empty
354 else nest nestDepth
355 $ text "deriving"
356 <+> parens (hsep $ punctuate comma $ map ppr decs)]
357
358 ppr_tySyn :: Doc -> Name -> Doc -> Type -> Doc
359 ppr_tySyn maybeInst t argsDoc rhs
360 = text "type" <+> maybeInst <+> ppr t <+> argsDoc <+> text "=" <+> ppr rhs
361
362 ------------------------------
363 instance Ppr FunDep where
364 ppr (FunDep xs ys) = hsep (map ppr xs) <+> text "->" <+> hsep (map ppr ys)
365 ppr_list [] = empty
366 ppr_list xs = char '|' <+> sep (punctuate (text ", ") (map ppr xs))
367
368 ------------------------------
369 instance Ppr FamFlavour where
370 ppr DataFam = text "data"
371 ppr TypeFam = text "type"
372
373 ------------------------------
374 instance Ppr Foreign where
375 ppr (ImportF callconv safety impent as typ)
376 = text "foreign import"
377 <+> showtextl callconv
378 <+> showtextl safety
379 <+> text (show impent)
380 <+> ppr as
381 <+> text "::" <+> ppr typ
382 ppr (ExportF callconv expent as typ)
383 = text "foreign export"
384 <+> showtextl callconv
385 <+> text (show expent)
386 <+> ppr as
387 <+> text "::" <+> ppr typ
388
389 ------------------------------
390 instance Ppr Pragma where
391 ppr (InlineP n inline rm phases)
392 = text "{-#"
393 <+> ppr inline
394 <+> ppr rm
395 <+> ppr phases
396 <+> ppr n
397 <+> text "#-}"
398 ppr (SpecialiseP n ty inline phases)
399 = text "{-# SPECIALISE"
400 <+> maybe empty ppr inline
401 <+> ppr phases
402 <+> sep [ ppr n <+> text "::"
403 , nest 2 $ ppr ty ]
404 <+> text "#-}"
405 ppr (SpecialiseInstP inst)
406 = text "{-# SPECIALISE instance" <+> ppr inst <+> text "#-}"
407 ppr (RuleP n bndrs lhs rhs phases)
408 = sep [ text "{-# RULES" <+> pprString n <+> ppr phases
409 , nest 4 $ ppr_forall <+> ppr lhs
410 , nest 4 $ char '=' <+> ppr rhs <+> text "#-}" ]
411 where ppr_forall | null bndrs = empty
412 | otherwise = text "forall"
413 <+> fsep (map ppr bndrs)
414 <+> char '.'
415 ppr (AnnP tgt expr)
416 = text "{-# ANN" <+> target1 tgt <+> ppr expr <+> text "#-}"
417 where target1 ModuleAnnotation = text "module"
418 target1 (TypeAnnotation t) = text "type" <+> ppr t
419 target1 (ValueAnnotation v) = ppr v
420 ppr (LineP line file)
421 = text "{-# LINE" <+> int line <+> text (show file) <+> text "#-}"
422
423 ------------------------------
424 instance Ppr Inline where
425 ppr NoInline = text "NOINLINE"
426 ppr Inline = text "INLINE"
427 ppr Inlinable = text "INLINABLE"
428
429 ------------------------------
430 instance Ppr RuleMatch where
431 ppr ConLike = text "CONLIKE"
432 ppr FunLike = empty
433
434 ------------------------------
435 instance Ppr Phases where
436 ppr AllPhases = empty
437 ppr (FromPhase i) = brackets $ int i
438 ppr (BeforePhase i) = brackets $ char '~' <> int i
439
440 ------------------------------
441 instance Ppr RuleBndr where
442 ppr (RuleVar n) = ppr n
443 ppr (TypedRuleVar n ty) = parens $ ppr n <+> text "::" <+> ppr ty
444
445 ------------------------------
446 instance Ppr Clause where
447 ppr (Clause ps rhs ds) = hsep (map (pprPat appPrec) ps) <+> pprBody True rhs
448 $$ where_clause ds
449
450 ------------------------------
451 instance Ppr Con where
452 ppr (NormalC c sts) = ppr c <+> sep (map pprStrictType sts)
453 ppr (RecC c vsts)
454 = ppr c <+> braces (sep (punctuate comma $ map pprVarStrictType vsts))
455 ppr (InfixC st1 c st2) = pprStrictType st1
456 <+> pprName' Infix c
457 <+> pprStrictType st2
458 ppr (ForallC ns ctxt con) = text "forall" <+> hsep (map ppr ns)
459 <+> char '.' <+> sep [pprCxt ctxt, ppr con]
460
461 ------------------------------
462 pprVarStrictType :: (Name, Strict, Type) -> Doc
463 -- Slight infelicity: with print non-atomic type with parens
464 pprVarStrictType (v, str, t) = ppr v <+> text "::" <+> pprStrictType (str, t)
465
466 ------------------------------
467 pprStrictType :: (Strict, Type) -> Doc
468 -- Prints with parens if not already atomic
469 pprStrictType (IsStrict, t) = char '!' <> pprParendType t
470 pprStrictType (NotStrict, t) = pprParendType t
471 pprStrictType (Unpacked, t) = text "{-# UNPACK #-} !" <> pprParendType t
472
473 ------------------------------
474 pprParendType :: Type -> Doc
475 pprParendType (VarT v) = ppr v
476 pprParendType (ConT c) = ppr c
477 pprParendType (TupleT 0) = text "()"
478 pprParendType (TupleT n) = parens (hcat (replicate (n-1) comma))
479 pprParendType (UnboxedTupleT n) = hashParens $ hcat $ replicate (n-1) comma
480 pprParendType ArrowT = parens (text "->")
481 pprParendType ListT = text "[]"
482 pprParendType (LitT l) = pprTyLit l
483 pprParendType (PromotedT c) = text "'" <> ppr c
484 pprParendType (PromotedTupleT 0) = text "'()"
485 pprParendType (PromotedTupleT n) = quoteParens (hcat (replicate (n-1) comma))
486 pprParendType PromotedNilT = text "'[]"
487 pprParendType PromotedConsT = text "(':)"
488 pprParendType StarT = char '*'
489 pprParendType ConstraintT = text "Constraint"
490 pprParendType other = parens (ppr other)
491
492 instance Ppr Type where
493 ppr (ForallT tvars ctxt ty)
494 = text "forall" <+> hsep (map ppr tvars) <+> text "."
495 <+> sep [pprCxt ctxt, ppr ty]
496 ppr (SigT ty k) = ppr ty <+> text "::" <+> ppr k
497 ppr ty = pprTyApp (split ty)
498
499 pprTyApp :: (Type, [Type]) -> Doc
500 pprTyApp (ArrowT, [arg1,arg2]) = sep [pprFunArgType arg1 <+> text "->", ppr arg2]
501 pprTyApp (EqualityT, [arg1, arg2]) =
502 sep [pprFunArgType arg1 <+> text "~", ppr arg2]
503 pprTyApp (ListT, [arg]) = brackets (ppr arg)
504 pprTyApp (TupleT n, args)
505 | length args == n = parens (sep (punctuate comma (map ppr args)))
506 pprTyApp (PromotedTupleT n, args)
507 | length args == n = quoteParens (sep (punctuate comma (map ppr args)))
508 pprTyApp (fun, args) = pprParendType fun <+> sep (map pprParendType args)
509
510 pprFunArgType :: Type -> Doc -- Should really use a precedence argument
511 -- Everything except forall and (->) binds more tightly than (->)
512 pprFunArgType ty@(ForallT {}) = parens (ppr ty)
513 pprFunArgType ty@((ArrowT `AppT` _) `AppT` _) = parens (ppr ty)
514 pprFunArgType ty@(SigT _ _) = parens (ppr ty)
515 pprFunArgType ty = ppr ty
516
517 split :: Type -> (Type, [Type]) -- Split into function and args
518 split t = go t []
519 where go (AppT t1 t2) args = go t1 (t2:args)
520 go ty args = (ty, args)
521
522 pprTyLit :: TyLit -> Doc
523 pprTyLit (NumTyLit n) = integer n
524 pprTyLit (StrTyLit s) = text (show s)
525
526 instance Ppr TyLit where
527 ppr = pprTyLit
528
529 ------------------------------
530 instance Ppr TyVarBndr where
531 ppr (PlainTV nm) = ppr nm
532 ppr (KindedTV nm k) = parens (ppr nm <+> text "::" <+> ppr k)
533
534 instance Ppr Role where
535 ppr NominalR = text "nominal"
536 ppr RepresentationalR = text "representational"
537 ppr PhantomR = text "phantom"
538 ppr InferR = text "_"
539
540 ------------------------------
541 pprCxt :: Cxt -> Doc
542 pprCxt [] = empty
543 pprCxt [t] = ppr t <+> text "=>"
544 pprCxt ts = parens (sep $ punctuate comma $ map ppr ts) <+> text "=>"
545
546 ------------------------------
547 instance Ppr Range where
548 ppr = brackets . pprRange
549 where pprRange :: Range -> Doc
550 pprRange (FromR e) = ppr e <> text ".."
551 pprRange (FromThenR e1 e2) = ppr e1 <> text ","
552 <> ppr e2 <> text ".."
553 pprRange (FromToR e1 e2) = ppr e1 <> text ".." <> ppr e2
554 pprRange (FromThenToR e1 e2 e3) = ppr e1 <> text ","
555 <> ppr e2 <> text ".."
556 <> ppr e3
557
558 ------------------------------
559 where_clause :: [Dec] -> Doc
560 where_clause [] = empty
561 where_clause ds = nest nestDepth $ text "where" <+> vcat (map (ppr_dec False) ds)
562
563 showtextl :: Show a => a -> Doc
564 showtextl = text . map toLower . show
565
566 hashParens :: Doc -> Doc
567 hashParens d = text "(# " <> d <> text " #)"
568
569 quoteParens :: Doc -> Doc
570 quoteParens d = text "'(" <> d <> text ")"