Revive External Core typechecker
[ghc-hetmet.git] / utils / ext-core / Printer.hs
1 {-# OPTIONS -Werror -Wall -fno-warn-missing-signatures #-}
2
3 module Printer where
4
5 import Text.PrettyPrint.HughesPJ
6 import Char
7
8 import Core
9 import Encoding
10 import PrimCoercions
11
12 instance Show Module where
13   showsPrec _ m = shows (pmodule m)
14
15 instance Show Tdef where
16   showsPrec _ t = shows (ptdef t)
17
18 instance Show Cdef where
19   showsPrec _ c = shows (pcdef c)
20
21 instance Show Vdefg where
22   showsPrec _ v = shows (pvdefg v)
23
24 instance Show Vdef where
25   showsPrec _ v = shows (pvdef v)
26
27 instance Show Exp where
28   showsPrec _ e = shows (pexp e)
29
30 instance Show Alt where
31   showsPrec _ a = shows (palt a)
32
33 instance Show Ty where
34   showsPrec _ t = shows (pty t)
35
36 instance Show Kind where
37   showsPrec _ k = shows (pkind k)
38
39 instance Show Lit where
40   showsPrec _ l = shows (plit l)
41
42 instance Show CoreLit where
43   showsPrec _ l = shows (pclit l)
44
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 ">") 
50
51 indent = nest 2
52
53 -- seems like this is asking for a type class...
54
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.
61
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)))))
65
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)) <+>
71                                           text "::"
72                                       <+> peqkind k)
73              repclause = case tyopt of
74                            Just ty -> char '=' <+> pty ty 
75                            Nothing -> empty
76
77 pcdef (Constr qdcon tbinds tys)  =
78   (pqname qdcon) <+> (sep [hsep (map pattbind tbinds),sep (map paty tys)])
79
80 pname = text
81
82 pqname (m,v) = pmname m <> pname v
83
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 '.'
90
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
96          -- with no parents.
97              (case parentStrs of
98                 [] -> empty
99                 _  -> hcat (punctuate hierModuleSeparator 
100                         (map pname parents)) 
101                       <> hierModuleSeparator)
102              <> pname name
103
104 -- note that this is not a '.' but a Z-encoded '.':
105 -- GHCziIOBase.IO, not GHC.IOBase.IO.
106 -- What a pain.
107 hierModuleSeparator = text (zEncodeString ".")
108
109 ptbind (t,Klifted) = pname t
110 ptbind (t,k) = parens (pname t <> text "::" <> pkind k)
111
112 pattbind (t,k) = char '@' <> ptbind (t,k)
113
114 pakind (Klifted) = char '*'
115 pakind (Kunlifted) = char '#'
116 pakind (Kopen) = char '?'
117 pakind k = parens (pkind k)
118
119 pkind (Karrow k1 k2) = parens (pakind k1 <> text "->" <> pkind k2)
120 pkind (Keq from to) = peqkind (from,to)
121 pkind k = pakind k
122
123 peqkind (t1, t2) = parens (parens (pty t1) <+> text ":=:" <+> parens (pty t2)) 
124
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)
138
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] 
141 pbty t = paty t
142
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
145 pty t = pbty t
146
147 pappty (Tapp t1 t2) ts = pappty t1 (t2:ts)
148 pappty t ts = sep (map paty (t:ts))
149
150 pforall tbs (Tforall tb t) = pforall (tbs ++ [tb]) t
151 pforall tbs t = hsep (map ptbind tbs) <+> char '.' <+> pty t
152
153 pvdefg (Rec vdefs) = text "%rec" $$ braces (indent (vcat (punctuate (char ';') (map pvdef vdefs))))
154 pvdefg (Nonrec vdef) = pvdef vdef
155
156 pvdef (Vdef (qv,t,e)) = sep [pqname qv <+> text "::" <+> pty t <+> char '=',
157                      indent (pexp e)]
158
159 paexp (Var x) = pqname x
160 paexp (Dcon x) = pqname x
161 paexp (Lit l) = plit l
162 paexp e = parens(pexp e)
163
164 plamexp bs (Lam b e) = plamexp (bs ++ [b]) e
165 plamexp bs e = sep [sep (map pbind bs) <+> text "->",
166                     indent (pexp e)]
167
168 pbind (Tb tb) = char '@' <+> ptbind tb
169 pbind (Vb vb) = pvbind vb
170
171 pfexp (App e1 e2) = pappexp e1 [Left e2]
172 pfexp (Appt e t) = pappexp e [Right t]
173 pfexp e = paexp e
174
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
180
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
190 pexp e = pfexp e
191
192
193 pvbind (x,t) = parens(pname x <> text "::" <> pty t)
194
195 palt (Acon c tbs vbs e) =
196         sep [pqname c, 
197              sep (map pattbind tbs),
198              sep (map pvbind vbs) <+> text "->"]
199         $$ indent (pexp e)
200 palt (Alit l e) = 
201         (plit l <+>  text "->")
202         $$ indent (pexp e)
203 palt (Adefault e) = 
204         (text "%_ ->")
205         $$ indent (pexp e)
206
207 plit (Literal cl t) = parens (pclit cl <> text "::" <> pty t)
208
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
214
215 pstring s = doubleQuotes(text (escape s))
216
217 escape :: String -> String
218 escape s = foldr f [] (map ord s)
219     where 
220      f cv rest
221         | cv > 0xFF = '\\':'x':hs ++ rest
222         | (cv < 0x20 || cv > 0x7e || cv == 0x22 || cv == 0x27 || cv == 0x5c) = 
223          '\\':'x':h1:h0:rest
224            where (q1,r1) = quotRem cv 16
225                  h1 = intToDigit q1
226                  h0 = intToDigit r1
227                  hs = dropWhile (=='0') $ reverse $ mkHex cv
228                  mkHex 0 = ""
229                  mkHex num = intToDigit r : mkHex q
230                     where (q,r) = quotRem num 16
231      f cv rest = (chr cv):rest