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