add -fsimpleopt-before-flatten
[ghc-hetmet.git] / compiler / coreSyn / PprExternalCore.lhs
1 %
2 % (c) The University of Glasgow 2001-2006
3 %
4
5 \begin{code}
6 module PprExternalCore () where
7
8 import Encoding
9 import ExternalCore
10
11 import Pretty
12 import Data.Char
13 import Data.Ratio
14
15 instance Show Module where
16   showsPrec _ m = shows (pmodule m)
17
18 instance Show Tdef where
19   showsPrec _ t = shows (ptdef t)
20
21 instance Show Cdef where
22   showsPrec _ c = shows (pcdef c)
23
24 instance Show Vdefg where
25   showsPrec _ v = shows (pvdefg 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
43 indent :: Doc -> Doc
44 indent = nest 2
45
46 pmodule :: Module -> Doc
47 pmodule (Module mname tdefs vdefgs) =
48   (text "%module" <+> text mname)
49     $$ indent ((vcat (map ((<> char ';') . ptdef) tdefs))
50                $$ (vcat (map ((<> char ';') . pvdefg) vdefgs)))
51
52 ptdef :: Tdef -> Doc
53 ptdef (Data tcon tbinds cdefs) =
54   (text "%data" <+> pqname tcon <+> (hsep (map ptbind tbinds)) <+> char '=')
55   $$ indent (braces ((vcat (punctuate (char ';') (map pcdef cdefs)))))
56
57 ptdef (Newtype tcon coercion tbinds rep) =
58   text "%newtype" <+> pqname tcon <+> pqname coercion 
59    <+> (hsep (map ptbind tbinds)) $$ indent repclause
60        where repclause = char '=' <+> pty rep
61
62 pcdef :: Cdef -> Doc
63 pcdef (Constr dcon tbinds tys)  =
64   (pqname dcon) <+> (sep [hsep (map pattbind tbinds),sep (map paty tys)])
65 pcdef (GadtConstr dcon ty)  =
66   (pqname dcon) <+> text "::" <+> pty ty
67
68 pname :: Id -> Doc
69 pname id = text (zEncodeString id)
70
71 pqname :: Qual Id -> Doc
72 pqname ("",id) = pname id
73 pqname (m,id)  = text m <> char '.' <> pname id
74
75 ptbind, pattbind :: Tbind -> Doc
76 ptbind (t,Klifted) = pname t
77 ptbind (t,k) = parens (pname t <> text "::" <> pkind k)
78
79 pattbind (t,k) = char '@' <> ptbind (t,k)
80
81 pakind, pkind :: Kind -> Doc
82 pakind (Klifted) = char '*'
83 pakind (Kunlifted) = char '#'
84 pakind (Kopen) = char '?'
85 pakind k = parens (pkind k)
86
87 pkind (Karrow k1 k2) = parens (pakind k1 <> text "->" <> pkind k2)
88 pkind (Keq t1 t2) = parens (parens (pty t1) <+> text ":=:" <+> 
89                             parens (pty t2))
90 pkind k = pakind k
91
92 paty, pbty, pty :: Ty -> Doc
93 paty (Tvar n) = pname n
94 paty (Tcon c) = pqname c
95 paty t = parens (pty t)
96
97 pbty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = parens(fsep [pbty t1, text "->",pty t2])
98 pbty (Tapp t1 t2) = parens $ pappty t1 [t2] 
99 pbty t = paty t
100
101 pty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = fsep [pbty t1, text "->",pty t2]
102 pty (Tforall tb t) = text "%forall" <+> pforall [tb] t
103 pty (TransCoercion t1 t2) =
104   sep [text "%trans", paty t1, paty t2]
105 pty (SymCoercion t) =
106   sep [text "%sym", paty t]
107 pty (UnsafeCoercion t1 t2) =
108   sep [text "%unsafe", paty t1, paty t2]
109 pty (LeftCoercion t) =
110   sep [text "%left", paty t]
111 pty (RightCoercion t) =
112   sep [text "%right", paty t]
113 pty (InstCoercion t1 t2) =
114   sep [text "%inst", paty t1, paty t2]
115 pty t = pbty t
116
117 pappty :: Ty -> [Ty] -> Doc
118 pappty (Tapp t1 t2) ts = pappty t1 (t2:ts)
119 pappty t ts = sep (map paty (t:ts))
120
121 pforall :: [Tbind] -> Ty -> Doc
122 pforall tbs (Tforall tb t) = pforall (tbs ++ [tb]) t
123 pforall tbs t = hsep (map ptbind tbs) <+> char '.' <+> pty t
124
125 pvdefg :: Vdefg -> Doc
126 pvdefg (Rec vdefs) = text "%rec" $$ braces (indent (vcat (punctuate (char ';') (map pvdef vdefs))))
127 pvdefg (Nonrec vdef) = pvdef vdef
128
129 pvdef :: Vdef -> Doc
130 -- TODO: Think about whether %local annotations are actually needed.
131 -- Right now, the local flag is never used, because the Core doc doesn't
132 -- explain the meaning of %local.
133 pvdef (_l,v,t,e) = sep [(pqname v <+> text "::" <+> pty t <+> char '='),
134                     indent (pexp e)]
135
136 paexp, pfexp, pexp :: Exp -> Doc
137 paexp (Var x) = pqname x
138 paexp (Dcon x) = pqname x
139 paexp (Lit l) = plit l
140 paexp e = parens(pexp e)
141
142 plamexp :: [Bind] -> Exp -> Doc
143 plamexp bs (Lam b e) = plamexp (bs ++ [b]) e
144 plamexp bs e = sep [sep (map pbind bs) <+> text "->",
145                     indent (pexp e)]
146
147 pbind :: Bind -> Doc
148 pbind (Tb tb) = char '@' <+> ptbind tb
149 pbind (Vb vb) = pvbind vb
150
151 pfexp (App e1 e2) = pappexp e1 [Left e2]
152 pfexp (Appt e t) = pappexp e [Right t]
153 pfexp e = paexp e
154
155 pappexp :: Exp -> [Either Exp Ty] -> Doc
156 pappexp (App e1 e2) as = pappexp e1 (Left e2:as)
157 pappexp (Appt e t) as = pappexp e (Right t:as)
158 pappexp e as = fsep (paexp e : map pa as)
159            where pa (Left e) = paexp e
160                  pa (Right t) = char '@' <+> paty t
161
162 pexp (Lam b e) = char '\\' <+> plamexp [b] e
163 pexp (Let vd e) = (text "%let" <+> pvdefg vd) $$ (text "%in" <+> pexp e)
164 pexp (Case e vb ty alts) = sep [text "%case" <+> paty ty <+> paexp e,
165                              text "%of" <+> pvbind vb]
166                         $$ (indent (braces (vcat (punctuate (char ';') (map palt alts)))))
167 pexp (Cast e co) = (text "%cast" <+> parens (pexp e)) $$ paty co
168 pexp (Note s e) = (text "%note" <+> pstring s) $$ pexp e
169 pexp (External n cc t) = (text "%external" <+> text cc <+> pstring n) $$ paty t
170 pexp (DynExternal cc t) = (text "%dynexternal" <+> text cc) $$ paty t
171 pexp (Label n) = (text "%label" <+> pstring n)
172 pexp e = pfexp e
173
174 pvbind :: Vbind -> Doc
175 pvbind (x,t) = parens(pname x <> text "::" <> pty t)
176
177 palt :: Alt -> Doc
178 palt (Acon c tbs vbs e) =
179         sep [pqname c, 
180              sep (map pattbind tbs),
181              sep (map pvbind vbs) <+> text "->"]
182         $$ indent (pexp e)
183 palt (Alit l e) = 
184         (plit l <+>  text "->")
185         $$ indent (pexp e)
186 palt (Adefault e) = 
187         (text "%_ ->")
188         $$ indent (pexp e)
189
190 plit :: Lit -> Doc
191 plit (Lint i t) = parens (integer i <> text "::" <> pty t)
192 -- we use (text (show (numerator r))) (and the same for denominator)
193 -- because "(rational r)" was printing out things like "2.0e-2" (which
194 -- isn't External Core), and (text (show r)) was printing out things
195 -- like "((-1)/5)" which isn't either (it should be "(-1/5)").
196 plit (Lrational r t) = parens (text (show (numerator r)) <+> char '%'
197    <+> text (show (denominator r)) <>  text "::" <> pty t)
198 plit (Lchar c t) = parens (text ("\'" ++ escape [c] ++ "\'") <> text "::" <> pty t)
199 plit (Lstring s t) = parens (pstring s <> text "::" <> pty t)
200
201 pstring :: String -> Doc
202 pstring s = doubleQuotes(text (escape s))
203
204 escape :: String -> String
205 escape s = foldr f [] (map ord s)
206     where 
207      f cv rest
208         | cv > 0xFF = '\\':'x':hs ++ rest
209         | (cv < 0x20 || cv > 0x7e || cv == 0x22 || cv == 0x27 || cv == 0x5c) = 
210          '\\':'x':h1:h0:rest
211            where (q1,r1) = quotRem cv 16
212                  h1 = intToDigit q1
213                  h0 = intToDigit r1
214                  hs = dropWhile (=='0') $ reverse $ mkHex cv
215                  mkHex 0 = ""
216                  mkHex cv = intToDigit r : mkHex q
217                     where (q,r) = quotRem cv 16
218      f cv rest = (chr cv):rest
219
220 \end{code}
221
222
223
224