1 {-# OPTIONS -Werror -Wall -fno-warn-missing-signatures #-}
5 import Text.PrettyPrint.HughesPJ
12 instance Show Module where
13 showsPrec _ m = shows (pmodule m)
15 instance Show Tdef where
16 showsPrec _ t = shows (ptdef t)
18 instance Show Cdef where
19 showsPrec _ c = shows (pcdef c)
21 instance Show Vdefg where
22 showsPrec _ v = shows (pvdefg v)
24 instance Show Vdef where
25 showsPrec _ v = shows (pvdef v)
27 instance Show Exp where
28 showsPrec _ e = shows (pexp e)
30 instance Show Alt where
31 showsPrec _ a = shows (palt a)
33 instance Show Ty where
34 showsPrec _ t = shows (pty t)
36 instance Show Kind where
37 showsPrec _ k = shows (pkind k)
39 instance Show Lit where
40 showsPrec _ l = shows (plit l)
42 instance Show CoreLit where
43 showsPrec _ l = shows (pclit l)
45 instance Show KindOrCoercion where
46 showsPrec _ (Kind k) = shows (text "<K" <+> pkind k <> text ">")
47 showsPrec _ (Coercion (DefinedCoercion tbs (t1,t2))) =
48 shows (text "<C" <+> hsep (map ptbind tbs) <+>
49 parens (pkind (Keq t1 t2)) <> text ">")
53 -- seems like this is asking for a type class...
55 pmodule (Module mname tdefs vdefgs) =
56 (text "%module" <+> panmname mname)
57 $$ indent ((vcat (map ((<> char ';') . ptdef) tdefs))
58 $$ (vcat (map ((<> char ';') . pvdefg) vdefgs)))
59 <> (if ((not.null) tdefs) || ((not.null) vdefgs) then char '\n' else empty)
60 -- add final newline; sigh.
62 ptdef (Data qtcon tbinds cdefs) =
63 (text "%data" <+> pqname qtcon <+> (hsep (map ptbind tbinds)) <+> char '=')
64 $$ indent (braces ((vcat (punctuate (char ';') (map pcdef cdefs)))))
66 ptdef (Newtype qtcon tbinds (coercion,cTbs,k) tyopt) =
67 text "%newtype" <+> pqname qtcon <+> (hsep (map ptbind tbinds))
68 $$ indent (axiomclause $$ repclause)
69 where axiomclause = char '^' <+> parens (pqname coercion <+>
70 (hsep (map ptbind cTbs)) <+>
73 repclause = case tyopt of
74 Just ty -> char '=' <+> pty ty
77 pcdef (Constr qdcon tbinds tys) =
78 (pqname qdcon) <+> (sep [hsep (map pattbind tbinds),sep (map paty tys)])
82 pqname (m,v) = pmname m <> pname v
84 -- be sure to print the '.' here so we don't print out
85 -- ".foo" for unqualified foo...
86 pmname Nothing = empty
87 -- Notice that we print the "^" here; this is so that
88 -- "%module foo" doesn't get printed as "%module ^foo"
89 pmname (Just m) = char '^' <> panmname m <> char '.'
91 panmname (pkgName, parents, name) =
92 let parentStrs = map pname parents in
93 pname pkgName <> char ':' <>
94 -- This is to be sure to not print out:
95 -- main:.Main for when there's a single module name
99 _ -> hcat (punctuate hierModuleSeparator
101 <> hierModuleSeparator)
104 -- note that this is not a '.' but a Z-encoded '.':
105 -- GHCziIOBase.IO, not GHC.IOBase.IO.
107 hierModuleSeparator = text (zEncodeString ".")
109 ptbind (t,Klifted) = pname t
110 ptbind (t,k) = parens (pname t <> text "::" <> pkind k)
112 pattbind (t,k) = char '@' <> ptbind (t,k)
114 pakind (Klifted) = char '*'
115 pakind (Kunlifted) = char '#'
116 pakind (Kopen) = char '?'
117 pakind k = parens (pkind k)
119 pkind (Karrow k1 k2) = parens (pakind k1 <> text "->" <> pkind k2)
120 pkind (Keq from to) = peqkind (from,to)
123 peqkind (t1, t2) = parens (parens (pty t1) <+> text ":=:" <+> parens (pty t2))
125 paty (Tvar n) = pname n
126 paty (Tcon c) = pqname c
127 paty (TransCoercion t1 t2) =
128 parens (sep ([pqname transCoercion, pbty t1, pbty t2]))
129 paty (SymCoercion t) =
130 parens (sep [pqname symCoercion, paty t])
131 paty (UnsafeCoercion t1 t2) =
132 parens (sep [pqname unsafeCoercion, pbty t1, pbty t2])
133 paty (LeftCoercion t) =
134 parens (pqname leftCoercion <+> paty t)
135 paty (RightCoercion t) =
136 parens (pqname rightCoercion <+> paty t)
137 paty t = parens (pty t)
139 pbty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = parens(fsep [pbty t1, text "->",pty t2])
140 pbty (Tapp t1 t2) = pappty t1 [t2]
143 pty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = fsep [pbty t1, text "->",pty t2]
144 pty (Tforall tb t) = text "%forall" <+> pforall [tb] t
147 pappty (Tapp t1 t2) ts = pappty t1 (t2:ts)
148 pappty t ts = sep (map paty (t:ts))
150 pforall tbs (Tforall tb t) = pforall (tbs ++ [tb]) t
151 pforall tbs t = hsep (map ptbind tbs) <+> char '.' <+> pty t
153 pvdefg (Rec vdefs) = text "%rec" $$ braces (indent (vcat (punctuate (char ';') (map pvdef vdefs))))
154 pvdefg (Nonrec vdef) = pvdef vdef
156 pvdef (Vdef (qv,t,e)) = sep [pqname qv <+> text "::" <+> pty t <+> char '=',
159 paexp (Var x) = pqname x
160 paexp (Dcon x) = pqname x
161 paexp (Lit l) = plit l
162 paexp e = parens(pexp e)
164 plamexp bs (Lam b e) = plamexp (bs ++ [b]) e
165 plamexp bs e = sep [sep (map pbind bs) <+> text "->",
168 pbind (Tb tb) = char '@' <+> ptbind tb
169 pbind (Vb vb) = pvbind vb
171 pfexp (App e1 e2) = pappexp e1 [Left e2]
172 pfexp (Appt e t) = pappexp e [Right t]
175 pappexp (App e1 e2) as = pappexp e1 (Left e2:as)
176 pappexp (Appt e t) as = pappexp e (Right t:as)
177 pappexp e as = fsep (paexp e : map pa as)
178 where pa (Left ex) = paexp ex
179 pa (Right t) = char '@' <+> paty t
181 pexp (Lam b e) = char '\\' <+> plamexp [b] e
182 pexp (Let vd e) = (text "%let" <+> pvdefg vd) $$ (text "%in" <+> pexp e)
183 pexp (Case e vb t alts) = sep [text "%case" <+> paty t <+> paexp e,
184 text "%of" <+> pvbind vb]
185 $$ (indent (braces (vcat (punctuate (char ';') (map palt alts)))))
186 pexp (Cast e t) = (text "%cast" <+> parens (pexp e)) $$ paty t
187 pexp (Note s e) = (text "%note" <+> pstring s) $$ pexp e
188 -- TODO: ccall shouldn't really be there
189 pexp (External n t) = (text "%external ccall" <+> pstring n) $$ paty t
193 pvbind (x,t) = parens(pname x <> text "::" <> pty t)
195 palt (Acon c tbs vbs e) =
197 sep (map pattbind tbs),
198 sep (map pvbind vbs) <+> text "->"]
201 (plit l <+> text "->")
207 plit (Literal cl t) = parens (pclit cl <> text "::" <> pty t)
209 pclit (Lint i) = integer i
210 -- makes sure to print it out as n % d
211 pclit (Lrational r) = text (show r)
212 pclit (Lchar c) = text ("\'" ++ escape [c] ++ "\'")
213 pclit (Lstring s) = pstring s
215 pstring s = doubleQuotes(text (escape s))
217 escape :: String -> String
218 escape s = foldr f [] (map ord s)
221 | cv > 0xFF = '\\':'x':hs ++ rest
222 | (cv < 0x20 || cv > 0x7e || cv == 0x22 || cv == 0x27 || cv == 0x5c) =
224 where (q1,r1) = quotRem cv 16
227 hs = dropWhile (=='0') $ reverse $ mkHex cv
229 mkHex num = intToDigit r : mkHex q
230 where (q,r) = quotRem num 16
231 f cv rest = (chr cv):rest