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 = char '=' <+> pty rep
62 pcdef (Constr dcon tbinds tys) =
63 (pqname dcon) <+> (sep [hsep (map pattbind tbinds),sep (map paty tys)])
64 pcdef (GadtConstr dcon ty) =
65 (pqname dcon) <+> text "::" <+> pty ty
68 pname id = text (zEncodeString id)
70 pqname :: Qual Id -> Doc
71 pqname ("",id) = pname id
72 pqname (m,id) = text m <> char '.' <> pname id
74 ptbind, pattbind :: Tbind -> Doc
75 ptbind (t,Klifted) = pname t
76 ptbind (t,k) = parens (pname t <> text "::" <> pkind k)
78 pattbind (t,k) = char '@' <> ptbind (t,k)
80 pakind, pkind :: Kind -> Doc
81 pakind (Klifted) = char '*'
82 pakind (Kunlifted) = char '#'
83 pakind (Kopen) = char '?'
84 pakind k = parens (pkind k)
86 pkind (Karrow k1 k2) = parens (pakind k1 <> text "->" <> pkind k2)
87 pkind (Keq t1 t2) = parens (parens (pty t1) <+> text ":=:" <+>
91 paty, pbty, pty :: Ty -> Doc
92 paty (Tvar n) = pname n
93 paty (Tcon c) = pqname c
94 paty t = parens (pty t)
96 pbty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = parens(fsep [pbty t1, text "->",pty t2])
97 pbty (Tapp t1 t2) = parens $ pappty t1 [t2]
100 pty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = fsep [pbty t1, text "->",pty t2]
101 pty (Tforall tb t) = text "%forall" <+> pforall [tb] t
102 pty (TransCoercion t1 t2) =
103 sep [text "%trans", paty t1, paty t2]
104 pty (SymCoercion t) =
105 sep [text "%sym", paty t]
106 pty (UnsafeCoercion t1 t2) =
107 sep [text "%unsafe", paty t1, paty t2]
108 pty (LeftCoercion t) =
109 sep [text "%left", paty t]
110 pty (RightCoercion t) =
111 sep [text "%right", paty t]
112 pty (InstCoercion t1 t2) =
113 sep [text "%inst", paty t1, paty t2]
116 pappty :: Ty -> [Ty] -> Doc
117 pappty (Tapp t1 t2) ts = pappty t1 (t2:ts)
118 pappty t ts = sep (map paty (t:ts))
120 pforall :: [Tbind] -> Ty -> Doc
121 pforall tbs (Tforall tb t) = pforall (tbs ++ [tb]) t
122 pforall tbs t = hsep (map ptbind tbs) <+> char '.' <+> pty t
124 pvdefg :: Vdefg -> Doc
125 pvdefg (Rec vdefs) = text "%rec" $$ braces (indent (vcat (punctuate (char ';') (map pvdef vdefs))))
126 pvdefg (Nonrec vdef) = pvdef vdef
129 -- TODO: Think about whether %local annotations are actually needed.
130 -- Right now, the local flag is never used, because the Core doc doesn't
131 -- explain the meaning of %local.
132 pvdef (_l,v,t,e) = sep [(pqname v <+> text "::" <+> pty t <+> char '='),
135 paexp, pfexp, pexp :: Exp -> Doc
136 paexp (Var x) = pqname x
137 paexp (Dcon x) = pqname x
138 paexp (Lit l) = plit l
139 paexp e = parens(pexp e)
141 plamexp :: [Bind] -> Exp -> Doc
142 plamexp bs (Lam b e) = plamexp (bs ++ [b]) e
143 plamexp bs e = sep [sep (map pbind bs) <+> text "->",
147 pbind (Tb tb) = char '@' <+> ptbind tb
148 pbind (Vb vb) = pvbind vb
150 pfexp (App e1 e2) = pappexp e1 [Left e2]
151 pfexp (Appt e t) = pappexp e [Right t]
154 pappexp :: Exp -> [Either Exp Ty] -> Doc
155 pappexp (App e1 e2) as = pappexp e1 (Left e2:as)
156 pappexp (Appt e t) as = pappexp e (Right t:as)
157 pappexp e as = fsep (paexp e : map pa as)
158 where pa (Left e) = paexp e
159 pa (Right t) = char '@' <+> paty t
161 pexp (Lam b e) = char '\\' <+> plamexp [b] e
162 pexp (Let vd e) = (text "%let" <+> pvdefg vd) $$ (text "%in" <+> pexp e)
163 pexp (Case e vb ty alts) = sep [text "%case" <+> paty ty <+> paexp e,
164 text "%of" <+> pvbind vb]
165 $$ (indent (braces (vcat (punctuate (char ';') (map palt alts)))))
166 pexp (Cast e co) = (text "%cast" <+> parens (pexp e)) $$ paty co
167 pexp (Note s e) = (text "%note" <+> pstring s) $$ pexp e
168 pexp (External n cc t) = (text "%external" <+> text cc <+> pstring n) $$ paty t
169 pexp (DynExternal cc t) = (text "%dynexternal" <+> text cc) $$ paty t
170 pexp (Label n) = (text "%label" <+> pstring n)
173 pvbind :: Vbind -> Doc
174 pvbind (x,t) = parens(pname x <> text "::" <> pty t)
177 palt (Acon c tbs vbs e) =
179 sep (map pattbind tbs),
180 sep (map pvbind vbs) <+> text "->"]
183 (plit l <+> text "->")
190 plit (Lint i t) = parens (integer i <> text "::" <> pty t)
191 -- we use (text (show r)) because "(rational r)" was printing out things
192 -- like "2.0e-2" (which isn't External Core)
193 plit (Lrational r t) = parens (text (show r) <> text "::" <> pty t)
194 plit (Lchar c t) = parens (text ("\'" ++ escape [c] ++ "\'") <> text "::" <> pty t)
195 plit (Lstring s t) = parens (pstring s <> text "::" <> pty t)
197 pstring :: String -> Doc
198 pstring s = doubleQuotes(text (escape s))
200 escape :: String -> String
201 escape s = foldr f [] (map ord s)
204 | cv > 0xFF = '\\':'x':hs ++ rest
205 | (cv < 0x20 || cv > 0x7e || cv == 0x22 || cv == 0x27 || cv == 0x5c) =
207 where (q1,r1) = quotRem cv 16
210 hs = dropWhile (=='0') $ reverse $ mkHex cv
212 mkHex cv = intToDigit r : mkHex q
213 where (q,r) = quotRem cv 16
214 f cv rest = (chr cv):rest