6 import Numeric (fromRat)
8 instance Show Module where
9 showsPrec d m = shows (pmodule m)
11 instance Show Tdef where
12 showsPrec d t = shows (ptdef t)
14 instance Show Cdef where
15 showsPrec d c = shows (pcdef c)
17 instance Show Vdefg where
18 showsPrec d v = shows (pvdefg v)
20 instance Show Vdef where
21 showsPrec d v = shows (pvdef v)
23 instance Show Exp where
24 showsPrec d e = shows (pexp e)
26 instance Show Alt where
27 showsPrec d a = shows (palt a)
29 instance Show Ty where
30 showsPrec d t = shows (pty t)
32 instance Show Kind where
33 showsPrec d k = shows (pkind k)
35 instance Show Lit where
36 showsPrec d l = shows (plit l)
41 pmodule (Module mname tdefs vdefgs) =
42 (text "%module" <+> text mname)
43 $$ indent ((vcat (map ((<> char ';') . ptdef) tdefs))
44 $$ (vcat (map ((<> char ';') . pvdefg) vdefgs)))
46 ptdef (Data qtcon tbinds cdefs) =
47 (text "%data" <+> pqname qtcon <+> (hsep (map ptbind tbinds)) <+> char '=')
48 $$ indent (braces ((vcat (punctuate (char ';') (map pcdef cdefs)))))
50 ptdef (Newtype qtcon tbinds tyopt ) =
51 text "%newtype" <+> pqname qtcon <+> (hsep (map ptbind tbinds)) <+>
53 Just ty -> char '=' <+> pty ty
56 pcdef (Constr qdcon tbinds tys) =
57 (pqname qdcon) <+> (sep [hsep (map pattbind tbinds),sep (map paty tys)])
61 pqname ("",id) = pname id
62 pqname (m,id) = pname m <> char '.' <> pname id
64 ptbind (t,Klifted) = pname t
65 ptbind (t,k) = parens (pname t <> text "::" <> pkind k)
67 pattbind (t,k) = char '@' <> ptbind (t,k)
69 pakind (Klifted) = char '*'
70 pakind (Kunlifted) = char '#'
71 pakind (Kopen) = char '?'
72 pakind k = parens (pkind k)
74 pkind (Karrow k1 k2) = parens (pakind k1 <> text "->" <> pkind k2)
77 paty (Tvar n) = pname n
78 paty (Tcon c) = pqname c
79 paty t = parens (pty t)
81 pbty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = parens(fsep [pbty t1, text "->",pty t2])
82 pbty (Tapp t1 t2) = pappty t1 [t2]
85 pty (Tapp(Tapp(Tcon tc) t1) t2) | tc == tcArrow = fsep [pbty t1, text "->",pty t2]
86 pty (Tforall tb t) = text "%forall" <+> pforall [tb] t
89 pappty (Tapp t1 t2) ts = pappty t1 (t2:ts)
90 pappty t ts = sep (map paty (t:ts))
92 pforall tbs (Tforall tb t) = pforall (tbs ++ [tb]) t
93 pforall tbs t = hsep (map ptbind tbs) <+> char '.' <+> pty t
95 pvdefg (Rec vdefs) = text "%rec" $$ braces (indent (vcat (punctuate (char ';') (map pvdef vdefs))))
96 pvdefg (Nonrec vdef) = pvdef vdef
98 pvdef (Vdef (qv,t,e)) = sep [pqname qv <+> text "::" <+> pty t <+> char '=',
101 paexp (Var x) = pqname x
102 paexp (Dcon x) = pqname x
103 paexp (Lit l) = plit l
104 paexp e = parens(pexp e)
106 plamexp bs (Lam b e) = plamexp (bs ++ [b]) e
107 plamexp bs e = sep [sep (map pbind bs) <+> text "->",
110 pbind (Tb tb) = char '@' <+> ptbind tb
111 pbind (Vb vb) = pvbind vb
113 pfexp (App e1 e2) = pappexp e1 [Left e2]
114 pfexp (Appt e t) = pappexp e [Right t]
117 pappexp (App e1 e2) as = pappexp e1 (Left e2:as)
118 pappexp (Appt e t) as = pappexp e (Right t:as)
119 pappexp e as = fsep (paexp e : map pa as)
120 where pa (Left e) = paexp e
121 pa (Right t) = char '@' <+> paty t
123 pexp (Lam b e) = char '\\' <+> plamexp [b] e
124 pexp (Let vd e) = (text "%let" <+> pvdefg vd) $$ (text "%in" <+> pexp e)
125 pexp (Case e vb alts) = sep [text "%case" <+> paexp e,
126 text "%of" <+> pvbind vb]
127 $$ (indent (braces (vcat (punctuate (char ';') (map palt alts)))))
128 pexp (Coerce t e) = (text "%coerce" <+> paty t) $$ pexp e
129 pexp (Note s e) = (text "%note" <+> pstring s) $$ pexp e
130 pexp (External n t) = (text "%extcall" <+> pstring n) $$ paty t
134 pvbind (x,t) = parens(pname x <> text "::" <> pty t)
136 palt (Acon c tbs vbs e) =
138 sep (map pattbind tbs),
139 sep (map pvbind vbs) <+> text "->"]
142 (plit l <+> text "->")
148 plit (Lint i t) = parens (integer i <> text "::" <> pty t)
149 plit (Lrational r t) = parens (text (show (fromRat r)) <> text "::" <> pty t)
150 plit (Lchar c t) = parens (text ("\'" ++ escape [c] ++ "\'") <> text "::" <> pty t)
151 plit (Lstring s t) = parens (pstring s <> text "::" <> pty t)
153 pstring s = doubleQuotes(text (escape s))
155 escape s = foldr f [] (map ord s)
157 f cv rest | (cv < 0x20 || cv > 0x7e || cv == 0x22 || cv == 0x27 || cv == 0x5c) =
159 where (q1,r1) = quotRem cv 16
162 f cv rest = (chr cv):rest