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