2 % (c) The University of Glasgow 2001-2006
6 module PprExternalCore () where
14 instance Show Module where
15 showsPrec _ m = shows (pmodule m)
17 instance Show Tdef where
18 showsPrec _ t = shows (ptdef t)
20 instance Show Cdef where
21 showsPrec _ c = shows (pcdef c)
23 instance Show Vdefg where
24 showsPrec _ v = shows (pvdefg v)
26 instance Show Exp where
27 showsPrec _ e = shows (pexp e)
29 instance Show Alt where
30 showsPrec _ a = shows (palt a)
32 instance Show Ty where
33 showsPrec _ t = shows (pty t)
35 instance Show Kind where
36 showsPrec _ k = shows (pkind k)
38 instance Show Lit where
39 showsPrec _ l = shows (plit l)
45 pmodule :: Module -> Doc
46 pmodule (Module mname tdefs vdefgs) =
47 (text "%module" <+> text mname)
48 $$ indent ((vcat (map ((<> char ';') . ptdef) tdefs))
49 $$ (vcat (map ((<> char ';') . pvdefg) vdefgs)))
52 ptdef (Data tcon tbinds cdefs) =
53 (text "%data" <+> pqname tcon <+> (hsep (map ptbind tbinds)) <+> char '=')
54 $$ indent (braces ((vcat (punctuate (char ';') (map pcdef cdefs)))))
56 ptdef (Newtype tcon coercion tbinds rep) =
57 text "%newtype" <+> pqname tcon <+> pqname coercion
58 <+> (hsep (map ptbind tbinds)) $$ indent repclause
59 where repclause = case rep of
60 Just ty -> char '=' <+> pty ty
65 pcdef (Constr dcon tbinds tys) =
66 (pqname dcon) <+> (sep [hsep (map pattbind tbinds),sep (map paty tys)])
67 pcdef (GadtConstr dcon ty) =
68 (pqname dcon) <+> text "::" <+> pty ty
71 pname id = text (zEncodeString id)
73 pqname :: Qual Id -> Doc
74 pqname ("",id) = pname id
75 pqname (m,id) = text m <> char '.' <> pname id
77 ptbind, pattbind :: Tbind -> Doc
78 ptbind (t,Klifted) = pname t
79 ptbind (t,k) = parens (pname t <> text "::" <> pkind k)
81 pattbind (t,k) = char '@' <> ptbind (t,k)
83 pakind, pkind :: Kind -> Doc
84 pakind (Klifted) = char '*'
85 pakind (Kunlifted) = char '#'
86 pakind (Kopen) = char '?'
87 pakind k = parens (pkind k)
89 pkind (Karrow k1 k2) = parens (pakind k1 <> text "->" <> pkind k2)
90 pkind (Keq t1 t2) = parens (parens (pty t1) <+> text ":=:" <+>
94 paty, pbty, pty :: Ty -> Doc
95 paty (Tvar n) = pname n
96 paty (Tcon c) = pqname c
97 paty t = parens (pty t)
99 pbty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = parens(fsep [pbty t1, text "->",pty t2])
100 pbty (Tapp t1 t2) = parens $ pappty t1 [t2]
103 pty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = fsep [pbty t1, text "->",pty t2]
104 pty (Tforall tb t) = text "%forall" <+> pforall [tb] t
105 pty (TransCoercion t1 t2) =
106 sep [text "%trans", paty t1, paty t2]
107 pty (SymCoercion t) =
108 sep [text "%sym", paty t]
109 pty (UnsafeCoercion t1 t2) =
110 sep [text "%unsafe", paty t1, paty t2]
111 pty (LeftCoercion t) =
112 sep [text "%left", paty t]
113 pty (RightCoercion t) =
114 sep [text "%right", paty t]
115 pty (InstCoercion t1 t2) =
116 sep [text "%inst", paty t1, paty t2]
119 pappty :: Ty -> [Ty] -> Doc
120 pappty (Tapp t1 t2) ts = pappty t1 (t2:ts)
121 pappty t ts = sep (map paty (t:ts))
123 pforall :: [Tbind] -> Ty -> Doc
124 pforall tbs (Tforall tb t) = pforall (tbs ++ [tb]) t
125 pforall tbs t = hsep (map ptbind tbs) <+> char '.' <+> pty t
127 pvdefg :: Vdefg -> Doc
128 pvdefg (Rec vdefs) = text "%rec" $$ braces (indent (vcat (punctuate (char ';') (map pvdef vdefs))))
129 pvdefg (Nonrec vdef) = pvdef vdef
132 -- TODO: Think about whether %local annotations are actually needed.
133 -- Right now, the local flag is never used, because the Core doc doesn't
134 -- explain the meaning of %local.
135 pvdef (_l,v,t,e) = sep [(pqname v <+> text "::" <+> pty t <+> char '='),
138 paexp, pfexp, pexp :: Exp -> Doc
139 paexp (Var x) = pqname x
140 paexp (Dcon x) = pqname x
141 paexp (Lit l) = plit l
142 paexp e = parens(pexp e)
144 plamexp :: [Bind] -> Exp -> Doc
145 plamexp bs (Lam b e) = plamexp (bs ++ [b]) e
146 plamexp bs e = sep [sep (map pbind bs) <+> text "->",
150 pbind (Tb tb) = char '@' <+> ptbind tb
151 pbind (Vb vb) = pvbind vb
153 pfexp (App e1 e2) = pappexp e1 [Left e2]
154 pfexp (Appt e t) = pappexp e [Right t]
157 pappexp :: Exp -> [Either Exp Ty] -> Doc
158 pappexp (App e1 e2) as = pappexp e1 (Left e2:as)
159 pappexp (Appt e t) as = pappexp e (Right t:as)
160 pappexp e as = fsep (paexp e : map pa as)
161 where pa (Left e) = paexp e
162 pa (Right t) = char '@' <+> paty t
164 pexp (Lam b e) = char '\\' <+> plamexp [b] e
165 pexp (Let vd e) = (text "%let" <+> pvdefg vd) $$ (text "%in" <+> pexp e)
166 pexp (Case e vb ty alts) = sep [text "%case" <+> paty ty <+> paexp e,
167 text "%of" <+> pvbind vb]
168 $$ (indent (braces (vcat (punctuate (char ';') (map palt alts)))))
169 pexp (Cast e co) = (text "%cast" <+> parens (pexp e)) $$ paty co
170 pexp (Note s e) = (text "%note" <+> pstring s) $$ pexp e
171 pexp (External n cc t) = (text "%external" <+> text cc <+> pstring n) $$ paty t
172 pexp (DynExternal cc t) = (text "%dynexternal" <+> text cc) $$ paty t
173 pexp (Label n) = (text "%label" <+> pstring n)
176 pvbind :: Vbind -> Doc
177 pvbind (x,t) = parens(pname x <> text "::" <> pty t)
180 palt (Acon c tbs vbs e) =
182 sep (map pattbind tbs),
183 sep (map pvbind vbs) <+> text "->"]
186 (plit l <+> text "->")
193 plit (Lint i t) = parens (integer i <> text "::" <> pty t)
194 -- we use (text (show r)) because "(rational r)" was printing out things
195 -- like "2.0e-2" (which isn't External Core)
196 plit (Lrational r t) = parens (text (show r) <> text "::" <> pty t)
197 plit (Lchar c t) = parens (text ("\'" ++ escape [c] ++ "\'") <> text "::" <> pty t)
198 plit (Lstring s t) = parens (pstring s <> text "::" <> pty t)
200 pstring :: String -> Doc
201 pstring s = doubleQuotes(text (escape s))
203 escape :: String -> String
204 escape s = foldr f [] (map ord s)
207 | cv > 0xFF = '\\':'x':hs ++ rest
208 | (cv < 0x20 || cv > 0x7e || cv == 0x22 || cv == 0x27 || cv == 0x5c) =
210 where (q1,r1) = quotRem cv 16
213 hs = dropWhile (=='0') $ reverse $ mkHex cv
215 mkHex cv = intToDigit r : mkHex q
216 where (q,r) = quotRem cv 16
217 f cv rest = (chr cv):rest