Improve pretty-printing for Template Haskell
[ghc.git] / libraries / template-haskell / Language / Haskell / TH / Ppr.hs
1 -- TH.Ppr 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.HughesPJ (render)
10 import Language.Haskell.TH.PprLib
11 import Language.Haskell.TH.Syntax
12 import Data.Char ( toLower )
13
14 nestDepth :: Int
15 nestDepth = 4
16
17 type Precedence = Int
18 appPrec, opPrec, noPrec :: Precedence
19 appPrec = 2 -- Argument of a function application
20 opPrec = 1 -- Argument of an infix operator
21 noPrec = 0 -- Others
22
23 parensIf :: Bool -> Doc -> Doc
24 parensIf True d = parens d
25 parensIf False d = d
26
27 ------------------------------
28
29 pprint :: Ppr a => a -> String
30 pprint x = render $ to_HPJ_Doc $ ppr x
31
32 class Ppr a where
33 ppr :: a -> Doc
34 ppr_list :: [a] -> Doc
35 ppr_list = vcat . map ppr
36
37 instance Ppr a => Ppr [a] where
38 ppr x = ppr_list x
39
40 ------------------------------
41 instance Ppr Name where
42 ppr v = pprName v -- text (show v)
43
44 ------------------------------
45 instance Ppr Info where
46 ppr (ClassI d) = ppr d
47 ppr (TyConI d) = ppr d
48 ppr (PrimTyConI name arity is_unlifted)
49 = text "Primitive"
50 <+> (if is_unlifted then text "unlifted" else empty)
51 <+> text "type construtor" <+> quotes (ppr name)
52 <+> parens (text "arity" <+> int arity)
53 ppr (ClassOpI v ty cls fix)
54 = text "Class op from" <+> ppr cls <> colon <+>
55 vcat [ppr_sig v ty, pprFixity v fix]
56 ppr (DataConI v ty tc fix)
57 = text "Constructor from" <+> ppr tc <> colon <+>
58 vcat [ppr_sig v ty, pprFixity v fix]
59 ppr (TyVarI v ty)
60 = text "Type variable" <+> ppr v <+> equals <+> ppr ty
61 ppr (VarI v ty mb_d fix)
62 = vcat [ppr_sig v ty, pprFixity v fix,
63 case mb_d of { Nothing -> empty; Just d -> ppr d }]
64
65 ppr_sig v ty = ppr v <+> text "::" <+> ppr ty
66
67 pprFixity :: Name -> Fixity -> Doc
68 pprFixity v f | f == defaultFixity = empty
69 pprFixity v (Fixity i d) = ppr_fix d <+> int i <+> ppr v
70 where ppr_fix InfixR = text "infixr"
71 ppr_fix InfixL = text "infixl"
72 ppr_fix InfixN = text "infix"
73
74
75 ------------------------------
76 instance Ppr Exp where
77 ppr = pprExp noPrec
78
79 pprExp :: Precedence -> Exp -> Doc
80 pprExp _ (VarE v) = ppr v
81 pprExp _ (ConE c) = ppr c
82 pprExp i (LitE l) = pprLit i l
83 pprExp i (AppE e1 e2) = parensIf (i >= appPrec) $ pprExp opPrec e1
84 <+> pprExp appPrec e2
85 pprExp i (InfixE (Just e1) op (Just e2))
86 = parensIf (i >= opPrec) $ pprExp opPrec e1
87 <+> ppr op
88 <+> pprExp opPrec e2
89 pprExp _ (InfixE me1 op me2) = parens $ pprMaybeExp noPrec me1
90 <+> ppr op
91 <+> pprMaybeExp noPrec me2
92 pprExp i (LamE ps e) = parensIf (i > noPrec) $ char '\\' <> hsep (map ppr ps)
93 <+> text "->" <+> ppr e
94 pprExp _ (TupE es) = parens $ sep $ punctuate comma $ map ppr es
95 -- Nesting in Cond is to avoid potential problems in do statments
96 pprExp i (CondE guard true false)
97 = parensIf (i > noPrec) $ sep [text "if" <+> ppr guard,
98 nest 1 $ text "then" <+> ppr true,
99 nest 1 $ text "else" <+> ppr false]
100 pprExp i (LetE ds e) = parensIf (i > noPrec) $ text "let" <+> ppr ds
101 $$ text " in" <+> ppr e
102 pprExp i (CaseE e ms)
103 = parensIf (i > noPrec) $ text "case" <+> ppr e <+> text "of"
104 $$ nest nestDepth (ppr ms)
105 pprExp i (DoE ss) = parensIf (i > noPrec) $ text "do" <+> ppr ss
106 pprExp _ (CompE []) = error "Can't happen: pprExp (CompExp [])"
107 -- This will probably break with fixity declarations - would need a ';'
108 pprExp _ (CompE ss) = text "[" <> ppr s
109 <+> text "|"
110 <+> (sep $ punctuate comma $ map ppr ss')
111 <> text "]"
112 where s = last ss
113 ss' = init ss
114 pprExp _ (ArithSeqE d) = ppr d
115 pprExp _ (ListE es) = brackets $ sep $ punctuate comma $ map ppr es
116 pprExp i (SigE e t) = parensIf (i > noPrec) $ ppr e <+> text "::" <+> ppr t
117 pprExp _ (RecConE nm fs) = ppr nm <> braces (pprFields fs)
118 pprExp _ (RecUpdE e fs) = pprExp appPrec e <> braces (pprFields fs)
119
120 pprFields :: [(Name,Exp)] -> Doc
121 pprFields = sep . punctuate comma . map (\(s,e) -> ppr s <+> equals <+> ppr e)
122
123 pprMaybeExp :: Precedence -> Maybe Exp -> Doc
124 pprMaybeExp _ Nothing = empty
125 pprMaybeExp i (Just e) = pprExp i e
126
127 ------------------------------
128 instance Ppr Stmt where
129 ppr (BindS p e) = ppr p <+> text "<-" <+> ppr e
130 ppr (LetS ds) = text "let" <+> ppr ds
131 ppr (NoBindS e) = ppr e
132 ppr (ParS sss) = sep $ punctuate (text "|")
133 $ map (sep . punctuate comma . map ppr) sss
134
135 ------------------------------
136 instance Ppr Match where
137 ppr (Match p rhs ds) = ppr p <+> pprBody False rhs
138 $$ where_clause ds
139
140 ------------------------------
141 pprBody :: Bool -> Body -> Doc
142 pprBody eq (GuardedB xs) = nest nestDepth $ vcat $ map do_guard xs
143 where eqd = if eq then text "=" else text "->"
144 do_guard (NormalG g, e) = text "|" <+> ppr g <+> eqd <+> ppr e
145 do_guard (PatG ss, e) = text "|" <+> vcat (map ppr ss)
146 $$ nest nestDepth (eqd <+> ppr e)
147 pprBody eq (NormalB e) = (if eq then text "=" else text "->") <+> ppr e
148
149 ------------------------------
150 pprLit :: Precedence -> Lit -> Doc
151 pprLit i (IntPrimL x) = parensIf (i > noPrec && x < 0)
152 (integer x <> char '#')
153 pprLit i (FloatPrimL x) = parensIf (i > noPrec && x < 0)
154 (float (fromRational x) <> char '#')
155 pprLit i (DoublePrimL x) = parensIf (i > noPrec && x < 0)
156 (double (fromRational x) <> text "##")
157 pprLit i (IntegerL x) = parensIf (i > noPrec && x < 0) (integer x)
158 pprLit _ (CharL c) = text (show c)
159 pprLit _ (StringL s) = text (show s)
160 pprLit i (RationalL rat) = parensIf (i > noPrec) $ rational rat
161
162 ------------------------------
163 instance Ppr Pat where
164 ppr = pprPat noPrec
165
166 pprPat :: Precedence -> Pat -> Doc
167 pprPat i (LitP l) = pprLit i l
168 pprPat _ (VarP v) = ppr v
169 pprPat _ (TupP ps) = parens $ sep $ punctuate comma $ map ppr ps
170 pprPat i (ConP s ps) = parensIf (i > noPrec) $ ppr s
171 <+> sep (map (pprPat appPrec) ps)
172 pprPat i (InfixP p1 n p2)
173 = parensIf (i > noPrec)
174 $ pprPat opPrec p1 <+> ppr n <+> pprPat opPrec p2
175 pprPat i (TildeP p) = parensIf (i > noPrec) $ pprPat appPrec p
176 pprPat i (AsP v p) = parensIf (i > noPrec) $ ppr v <> text "@"
177 <> pprPat appPrec p
178 pprPat _ WildP = text "_"
179 pprPat _ (RecP nm fs)
180 = parens $ ppr nm
181 <+> braces (sep $ punctuate comma $
182 map (\(s,p) -> ppr s <+> equals <+> ppr p) fs)
183 pprPat _ (ListP ps) = brackets $ sep $ punctuate comma $ map ppr ps
184 pprPat i (SigP p t) = parensIf (i > noPrec) $ ppr p <+> text "::" <+> ppr t
185
186 ------------------------------
187 instance Ppr Dec where
188 ppr (FunD f cs) = vcat $ map (\c -> ppr f <+> ppr c) cs
189 ppr (ValD p r ds) = ppr p <+> pprBody True r
190 $$ where_clause ds
191 ppr (TySynD t xs rhs) = text "type" <+> ppr t <+> hsep (map ppr xs)
192 <+> text "=" <+> ppr rhs
193 ppr (DataD ctxt t xs cs decs)
194 = text "data"
195 <+> pprCxt ctxt
196 <+> ppr t <+> hsep (map ppr xs)
197 <+> sep (pref $ map ppr cs)
198 $$ if null decs
199 then empty
200 else nest nestDepth
201 $ text "deriving"
202 <+> parens (hsep $ punctuate comma $ map ppr decs)
203 where pref :: [Doc] -> [Doc]
204 pref [] = [char '='] -- Can't happen in H98
205 pref (d:ds) = (char '=' <+> d):map (char '|' <+>) ds
206 ppr (NewtypeD ctxt t xs c decs)
207 = text "newtype"
208 <+> pprCxt ctxt
209 <+> ppr t <+> hsep (map ppr xs)
210 <+> char '=' <+> ppr c
211 $$ if null decs
212 then empty
213 else nest nestDepth
214 $ text "deriving"
215 <+> parens (hsep $ punctuate comma $ map ppr decs)
216 ppr (ClassD ctxt c xs fds ds) = text "class" <+> pprCxt ctxt
217 <+> ppr c <+> hsep (map ppr xs) <+> ppr fds
218 $$ where_clause ds
219 ppr (InstanceD ctxt i ds) = text "instance" <+> pprCxt ctxt <+> ppr i
220 $$ where_clause ds
221 ppr (SigD f t) = ppr f <+> text "::" <+> ppr t
222 ppr (ForeignD f) = ppr f
223
224 ------------------------------
225 instance Ppr FunDep where
226 ppr (FunDep xs ys) = hsep (map ppr xs) <+> text "->" <+> hsep (map ppr ys)
227 ppr_list xs = char '|' <+> sep (punctuate (text ", ") (map ppr xs))
228
229 ------------------------------
230 instance Ppr Foreign where
231 ppr (ImportF callconv safety impent as typ)
232 = text "foreign import"
233 <+> showtextl callconv
234 <+> showtextl safety
235 <+> text (show impent)
236 <+> ppr as
237 <+> text "::" <+> ppr typ
238 ppr (ExportF callconv expent as typ)
239 = text "foreign export"
240 <+> showtextl callconv
241 <+> text (show expent)
242 <+> ppr as
243 <+> text "::" <+> ppr typ
244
245 ------------------------------
246 instance Ppr Clause where
247 ppr (Clause ps rhs ds) = hsep (map (pprPat appPrec) ps) <+> pprBody True rhs
248 $$ where_clause ds
249
250 ------------------------------
251 instance Ppr Con where
252 ppr (NormalC c sts) = ppr c <+> sep (map pprStrictType sts)
253 ppr (RecC c vsts)
254 = ppr c <+> braces (sep (punctuate comma $ map pprVarStrictType vsts))
255 ppr (InfixC st1 c st2) = pprStrictType st1 <+> ppr c <+> pprStrictType st2
256 ppr (ForallC ns ctxt con) = text "forall" <+> hsep (map ppr ns)
257 <+> char '.' <+> pprCxt ctxt <+> ppr con
258
259 ------------------------------
260 pprVarStrictType :: (Name, Strict, Type) -> Doc
261 -- Slight infelicity: with print non-atomic type with parens
262 pprVarStrictType (v, str, t) = ppr v <+> text "::" <+> pprStrictType (str, t)
263
264 ------------------------------
265 pprStrictType :: (Strict, Type) -> Doc
266 -- Prints with parens if not already atomic
267 pprStrictType (IsStrict, t) = char '!' <> pprParendType t
268 pprStrictType (NotStrict, t) = pprParendType t
269
270 ------------------------------
271 pprParendType :: Type -> Doc
272 pprParendType (VarT v) = ppr v
273 pprParendType (ConT c) = ppr c
274 pprParendType (TupleT 0) = text "()"
275 pprParendType (TupleT n) = parens (hcat (replicate (n-1) comma))
276 pprParendType ArrowT = parens (text "->")
277 pprParendType ListT = text "[]"
278 pprParendType other = parens (ppr other)
279
280 instance Ppr Type where
281 ppr (ForallT tvars ctxt ty) =
282 text "forall" <+> hsep (map ppr tvars) <+> text "."
283 <+> pprCxt ctxt <+> ppr ty
284 ppr ty = pprTyApp (split ty)
285
286 pprTyApp :: (Type, [Type]) -> Doc
287 pprTyApp (ArrowT, [arg1,arg2]) = sep [ppr arg1 <+> text "->", ppr arg2]
288 pprTyApp (ListT, [arg]) = brackets (ppr arg)
289 pprTyApp (TupleT n, args)
290 | length args == n = parens (sep (punctuate comma (map ppr args)))
291 pprTyApp (fun, args) = pprParendType fun <+> sep (map pprParendType args)
292
293 split :: Type -> (Type, [Type]) -- Split into function and args
294 split t = go t []
295 where go (AppT t1 t2) args = go t1 (t2:args)
296 go ty args = (ty, args)
297
298 ------------------------------
299 pprCxt :: Cxt -> Doc
300 pprCxt [] = empty
301 pprCxt [t] = ppr t <+> text "=>"
302 pprCxt ts = parens (hsep $ punctuate comma $ map ppr ts) <+> text "=>"
303
304 ------------------------------
305 instance Ppr Range where
306 ppr = brackets . pprRange
307 where pprRange :: Range -> Doc
308 pprRange (FromR e) = ppr e <> text ".."
309 pprRange (FromThenR e1 e2) = ppr e1 <> text ","
310 <> ppr e2 <> text ".."
311 pprRange (FromToR e1 e2) = ppr e1 <> text ".." <> ppr e2
312 pprRange (FromThenToR e1 e2 e3) = ppr e1 <> text ","
313 <> ppr e2 <> text ".."
314 <> ppr e3
315
316 ------------------------------
317 where_clause :: [Dec] -> Doc
318 where_clause [] = empty
319 where_clause ds = nest nestDepth $ text "where" <+> vcat (map ppr ds)
320
321 showtextl :: Show a => a -> Doc
322 showtextl = text . map toLower . show
323