Update External Core docs
[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 ExternalCore
9 import Encoding
10
11 import Pretty
12 import Char
13
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 tbinds (coercion,tbs,k) rep) =
58 -- TODO: I think this is kind of redundant now.
59 -- Here we take apart the newtype tycon in order to get the newtype coercion,
60 -- which needs to be represented in the External Core file because it's not
61 -- straightforward to derive its definition from the newtype declaration alone.
62 -- At the same time, we need the newtype decl to declare the tycon itself.
63 -- Sigh.
64   text "%newtype" <+> pqname tcon <+> (hsep (map ptbind tbinds)) 
65     $$ indent (axiomclause $$ repclause)
66        where  axiomclause = char '^' 
67                  <+> parens (pqname coercion <+> (hsep (map ptbind tbs))
68                               <+> text "::"
69                               <+> pkind k)
70               repclause   = case rep of
71                               Just ty -> char '=' <+> pty ty 
72                               Nothing -> empty
73              
74
75 pcdef :: Cdef -> Doc
76 pcdef (Constr dcon tbinds tys)  =
77   (pqname dcon) <+> (sep [hsep (map pattbind tbinds),sep (map paty tys)])
78 pcdef (GadtConstr dcon ty)  =
79   (pqname dcon) <+> text "::" <+> pty ty
80
81 pname :: Id -> Doc
82 pname id = text (zEncodeString id)
83
84 pqname :: Qual Id -> Doc
85 pqname ("",id) = pname id
86 pqname (m,id)  = text m <> char '.' <> pname id
87
88 ptbind, pattbind :: Tbind -> Doc
89 ptbind (t,Klifted) = pname t
90 ptbind (t,k) = parens (pname t <> text "::" <> pkind k)
91
92 pattbind (t,k) = char '@' <> ptbind (t,k)
93
94 pakind, pkind :: Kind -> Doc
95 pakind (Klifted) = char '*'
96 pakind (Kunlifted) = char '#'
97 pakind (Kopen) = char '?'
98 pakind k = parens (pkind k)
99
100 pkind (Karrow k1 k2) = parens (pakind k1 <> text "->" <> pkind k2)
101 pkind (Keq t1 t2) = parens (parens (pty t1) <+> text ":=:" <+> 
102                             parens (pty t2))
103 pkind k = pakind k
104
105 paty, pbty, pty :: Ty -> Doc
106 paty (Tvar n) = pname n
107 paty (Tcon c) = pqname c
108 paty t = parens (pty t)
109
110 pbty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = parens(fsep [pbty t1, text "->",pty t2])
111 pbty (Tapp t1 t2) = parens $ pappty t1 [t2] 
112 pbty t = paty t
113
114 pty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = fsep [pbty t1, text "->",pty t2]
115 pty (Tforall tb t) = text "%forall" <+> pforall [tb] t
116 pty t = pbty t
117
118 pappty :: Ty -> [Ty] -> Doc
119 pappty (Tapp t1 t2) ts = pappty t1 (t2:ts)
120 pappty t ts = sep (map paty (t:ts))
121
122 pforall :: [Tbind] -> Ty -> Doc
123 pforall tbs (Tforall tb t) = pforall (tbs ++ [tb]) t
124 pforall tbs t = hsep (map ptbind tbs) <+> char '.' <+> pty t
125
126 pvdefg :: Vdefg -> Doc
127 pvdefg (Rec vdefs) = text "%rec" $$ braces (indent (vcat (punctuate (char ';') (map pvdef vdefs))))
128 pvdefg (Nonrec vdef) = pvdef vdef
129
130 pvdef :: Vdef -> Doc
131 -- TODO: Think about whether %local annotations are actually needed.
132 -- Right now, the local flag is never used, because the Core doc doesn't
133 -- explain the meaning of %local.
134 pvdef (_l,v,t,e) = sep [(pqname v <+> text "::" <+> pty t <+> char '='),
135                     indent (pexp e)]
136
137 paexp, pfexp, pexp :: Exp -> Doc
138 paexp (Var x) = pqname x
139 paexp (Dcon x) = pqname x
140 paexp (Lit l) = plit l
141 paexp e = parens(pexp e)
142
143 plamexp :: [Bind] -> Exp -> Doc
144 plamexp bs (Lam b e) = plamexp (bs ++ [b]) e
145 plamexp bs e = sep [sep (map pbind bs) <+> text "->",
146                     indent (pexp e)]
147
148 pbind :: Bind -> Doc
149 pbind (Tb tb) = char '@' <+> ptbind tb
150 pbind (Vb vb) = pvbind vb
151
152 pfexp (App e1 e2) = pappexp e1 [Left e2]
153 pfexp (Appt e t) = pappexp e [Right t]
154 pfexp e = paexp e
155
156 pappexp :: Exp -> [Either Exp Ty] -> Doc
157 pappexp (App e1 e2) as = pappexp e1 (Left e2:as)
158 pappexp (Appt e t) as = pappexp e (Right t:as)
159 pappexp e as = fsep (paexp e : map pa as)
160            where pa (Left e) = paexp e
161                  pa (Right t) = char '@' <+> paty t
162
163 pexp (Lam b e) = char '\\' <+> plamexp [b] e
164 pexp (Let vd e) = (text "%let" <+> pvdefg vd) $$ (text "%in" <+> pexp e)
165 pexp (Case e vb ty alts) = sep [text "%case" <+> paty ty <+> paexp e,
166                              text "%of" <+> pvbind vb]
167                         $$ (indent (braces (vcat (punctuate (char ';') (map palt alts)))))
168 pexp (Cast e co) = (text "%cast" <+> parens (pexp e)) $$ paty co
169 pexp (Note s e) = (text "%note" <+> pstring s) $$ pexp e
170 pexp (External n cc t) = (text "%external" <+> text cc <+> pstring n) $$ paty t
171 pexp (DynExternal cc t) = (text "%dynexternal" <+> text cc) $$ paty t
172 pexp (Label n) = (text "%label" <+> pstring n)
173 pexp e = pfexp e
174
175 pvbind :: Vbind -> Doc
176 pvbind (x,t) = parens(pname x <> text "::" <> pty t)
177
178 palt :: Alt -> Doc
179 palt (Acon c tbs vbs e) =
180         sep [pqname c, 
181              sep (map pattbind tbs),
182              sep (map pvbind vbs) <+> text "->"]
183         $$ indent (pexp e)
184 palt (Alit l e) = 
185         (plit l <+>  text "->")
186         $$ indent (pexp e)
187 palt (Adefault e) = 
188         (text "%_ ->")
189         $$ indent (pexp e)
190
191 plit :: Lit -> Doc
192 plit (Lint i t) = parens (integer i <> text "::" <> pty t)
193 -- we use (text (show r)) because "(rational r)" was printing out things
194 -- like "2.0e-2" (which isn't External Core)
195 plit (Lrational r t) = parens (text (show r) <>  text "::" <> pty t)
196 plit (Lchar c t) = parens (text ("\'" ++ escape [c] ++ "\'") <> text "::" <> pty t)
197 plit (Lstring s t) = parens (pstring s <> text "::" <> pty t)
198
199 pstring :: String -> Doc
200 pstring s = doubleQuotes(text (escape s))
201
202 escape :: String -> String
203 escape s = foldr f [] (map ord s)
204     where 
205      f cv rest
206         | cv > 0xFF = '\\':'x':hs ++ rest
207         | (cv < 0x20 || cv > 0x7e || cv == 0x22 || cv == 0x27 || cv == 0x5c) = 
208          '\\':'x':h1:h0:rest
209            where (q1,r1) = quotRem cv 16
210                  h1 = intToDigit q1
211                  h0 = intToDigit r1
212                  hs = dropWhile (=='0') $ reverse $ mkHex cv
213                  mkHex 0 = ""
214                  mkHex cv = intToDigit r : mkHex q
215                     where (q,r) = quotRem cv 16
216      f cv rest = (chr cv):rest
217
218 \end{code}
219
220
221
222