ext-core library: Change syntax for primitive coercions
[ghc-hetmet.git] / utils / ext-core / Language / Core / Printer.hs
1 {-# OPTIONS -Werror -Wall -fno-warn-missing-signatures #-}
2
3 module Language.Core.Printer where
4
5 import Text.PrettyPrint.HughesPJ
6 import Data.Char
7
8 import Language.Core.Core
9 import Language.Core.Encoding
10
11 instance Show Module where
12   showsPrec _ m = shows (pmodule m)
13
14 instance Show Tdef where
15   showsPrec _ t = shows (ptdef t)
16
17 instance Show Cdef where
18   showsPrec _ c = shows (pcdef c)
19
20 instance Show Vdefg where
21   showsPrec _ v = shows (pvdefg v)
22
23 instance Show Vdef where
24   showsPrec _ v = shows (pvdef v)
25
26 instance Show Exp where
27   showsPrec _ e = shows (pexp e)
28
29 instance Show Alt where
30   showsPrec _ a = shows (palt a)
31
32 instance Show Ty where
33   showsPrec _ t = shows (pty t)
34
35 instance Show Kind where
36   showsPrec _ k = shows (pkind k)
37
38 instance Show Lit where
39   showsPrec _ l = shows (plit l)
40
41 instance Show CoreLit where
42   showsPrec _ l = shows (pclit l)
43
44 instance Show KindOrCoercion where
45   showsPrec _ (Kind k) = shows (text "<K" <+> pkind k <> text ">")
46   showsPrec _ (Coercion (DefinedCoercion tbs (t1,t2))) = 
47      shows (text "<C" <+> hsep (map ptbind tbs) <+>
48               parens (pkind (Keq t1 t2)) <> text ">") 
49
50 instance Show AnMname where
51   showsPrec _ mn = shows (panmname mn)
52
53 indent = nest 2
54
55 -- seems like this is asking for a type class...
56
57 pmodule (Module mname tdefs vdefgs) =
58   (text "%module" <+> panmname mname)
59   $$ indent ((vcat (map ((<> char ';') . ptdef) tdefs))
60              $$ (vcat (map ((<> char ';') . pvdefg) vdefgs)))
61   <> (if ((not.null) tdefs) || ((not.null) vdefgs) then char '\n' else empty)
62          -- add final newline; sigh.
63
64 ptdef (Data qtcon tbinds cdefs) =
65   (text "%data" <+> pqname qtcon <+> (hsep (map ptbind tbinds)) <+> char '=')
66   $$ indent (braces ((vcat (punctuate (char ';') (map pcdef cdefs)))))
67
68 ptdef (Newtype qtcon coercion tbinds tyopt) =
69   text "%newtype" <+> pqname qtcon <+> pqname coercion 
70     <+> (hsep (map ptbind tbinds)) $$ indent repclause
71        where repclause = char '=' <+> pty tyopt
72
73 pcdef (Constr qdcon tbinds tys)  =
74   (pqname qdcon) <+> (sep [hsep (map pattbind tbinds),sep (map paty tys)])
75
76 pname = text
77
78 pqname (m,v) = pmname m <> pname v
79
80 -- be sure to print the '.' here so we don't print out
81 -- ".foo" for unqualified foo...
82 pmname Nothing = empty
83 pmname (Just m) = panmname m <> char '.'
84
85 panmname (M (P pkgName, parents, name)) =
86   let parentStrs = map pname parents in
87          pname pkgName <> char ':' <>
88          -- This is to be sure to not print out:
89          -- main:.Main for when there's a single module name
90          -- with no parents.
91              (case parentStrs of
92                 [] -> empty
93                 _  -> hcat (punctuate hierModuleSeparator 
94                         (map pname parents)) 
95                       <> hierModuleSeparator)
96              <> pname name
97
98 -- note that this is not a '.' but a Z-encoded '.':
99 -- GHCziIOBase.IO, not GHC.IOBase.IO.
100 -- What a pain.
101 hierModuleSeparator = text (zEncodeString ".")
102
103 ptbind (t,Klifted) = pname t
104 ptbind (t,k) = parens (pname t <> text "::" <> pkind k)
105
106 pattbind (t,k) = char '@' <> ptbind (t,k)
107
108 pakind (Klifted) = char '*'
109 pakind (Kunlifted) = char '#'
110 pakind (Kopen) = char '?'
111 pakind k = parens (pkind k)
112
113 pkind (Karrow k1 k2) = parens (pakind k1 <> text "->" <> pkind k2)
114 pkind (Keq from to) = peqkind (from,to)
115 pkind k = pakind k
116
117 peqkind (t1, t2) = parens (parens (pty t1) <+> text ":=:" <+> parens (pty t2)) 
118
119 paty (Tvar n) = pname n
120 paty (Tcon c) = pqname c
121 paty t = parens (pty t)
122
123 pbty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = parens(fsep [pbty t1, text "->",pty t2])
124 pbty (Tapp t1 t2) = pappty t1 [t2] 
125 pbty t = paty t
126
127 pty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = fsep [pbty t1, text "->",pty t2]
128 pty (Tforall tb t) = text "%forall" <+> pforall [tb] t
129 pty (TransCoercion t1 t2) = 
130     (sep ([text "%trans", paty t1, paty t2]))
131 pty (SymCoercion t) = 
132     (sep [text "%sym", paty t])
133 pty (UnsafeCoercion t1 t2) = 
134     (sep [text "%unsafe", paty t1, paty t2])
135 pty (LeftCoercion t) = 
136     (text "%left" <+> paty t)
137 pty (RightCoercion t) = 
138     (text "%right" <+> paty t)
139 pty (InstCoercion t1 t2) = 
140     (sep [text "%inst", paty t1, paty t2])
141 pty t = pbty t
142
143 pappty (Tapp t1 t2) ts = pappty t1 (t2:ts)
144 pappty t ts = sep (map paty (t:ts))
145
146 pforall tbs (Tforall tb t) = pforall (tbs ++ [tb]) t
147 pforall tbs t = hsep (map ptbind tbs) <+> char '.' <+> pty t
148
149 pvdefg (Rec vdefs) = text "%rec" $$ braces (indent (vcat (punctuate (char ';') (map pvdef vdefs))))
150 pvdefg (Nonrec vdef) = pvdef vdef
151
152 pvdef (Vdef (qv,t,e)) = sep [pqname qv <+> text "::" <+> pty t <+> char '=',
153                      indent (pexp e)]
154
155 paexp (Var x) = pqname x
156 paexp (Dcon x) = pqname x
157 paexp (Lit l) = plit l
158 paexp e = parens(pexp e)
159
160 plamexp bs (Lam b e) = plamexp (bs ++ [b]) e
161 plamexp bs e = sep [sep (map pbind bs) <+> text "->",
162                     indent (pexp e)]
163
164 pbind (Tb tb) = char '@' <+> ptbind tb
165 pbind (Vb vb) = pvbind vb
166
167 pfexp (App e1 e2) = pappexp e1 [Left e2]
168 pfexp (Appt e t) = pappexp e [Right t]
169 pfexp e = paexp e
170
171 pappexp (App e1 e2) as = pappexp e1 (Left e2:as)
172 pappexp (Appt e t) as = pappexp e (Right t:as)
173 pappexp e as = fsep (paexp e : map pa as)
174            where pa (Left ex) = paexp ex
175                  pa (Right t) = char '@' <+> paty t
176
177 pexp (Lam b e) = char '\\' <+> plamexp [b] e
178 pexp (Let vd e) = (text "%let" <+> pvdefg vd) $$ (text "%in" <+> pexp e)
179 pexp (Case e vb t alts) = sep [text "%case" <+> paty t <+> paexp e,
180                              text "%of" <+> pvbind vb]
181                         $$ (indent (braces (vcat (punctuate (char ';') (map palt alts)))))
182 pexp (Cast e t) = (text "%cast" <+> parens (pexp e)) $$ paty t
183 pexp (Note s e) = (text "%note" <+> pstring s) $$ pexp e
184 -- TODO: ccall shouldn't really be there
185 pexp (External n t) = (text "%external ccall" <+> pstring n) $$ paty t
186 pexp e = pfexp e
187
188
189 pvbind (x,t) = parens(pname x <> text "::" <> pty t)
190
191 palt (Acon c tbs vbs e) =
192         sep [pqname c, 
193              sep (map pattbind tbs),
194              sep (map pvbind vbs) <+> text "->"]
195         $$ indent (pexp e)
196 palt (Alit l e) = 
197         (plit l <+>  text "->")
198         $$ indent (pexp e)
199 palt (Adefault e) = 
200         (text "%_ ->")
201         $$ indent (pexp e)
202
203 plit (Literal cl t) = parens (pclit cl <> text "::" <> pty t)
204
205 pclit (Lint i) = integer i
206 -- makes sure to print it out as n % d
207 pclit (Lrational r) = text (show r)
208 pclit (Lchar c) = text ("\'" ++ escape [c] ++ "\'")
209 pclit (Lstring s) = pstring s
210
211 pstring s = doubleQuotes(text (escape s))
212
213 escape :: String -> String
214 escape s = foldr f [] (map ord s)
215     where 
216      f cv rest
217         | cv > 0xFF = '\\':'x':hs ++ rest
218         | (cv < 0x20 || cv > 0x7e || cv == 0x22 || cv == 0x27 || cv == 0x5c) = 
219          '\\':'x':h1:h0:rest
220            where (q1,r1) = quotRem cv 16
221                  h1 = intToDigit q1
222                  h0 = intToDigit r1
223                  hs = dropWhile (=='0') $ reverse $ mkHex cv
224                  mkHex 0 = ""
225                  mkHex num = intToDigit r : mkHex q
226                     where (q,r) = quotRem num 16
227      f cv rest = (chr cv):rest