25 (** Haskell serializer **) |
25 (** Haskell serializer **) |
26 |
26 |
27 fun print_haskell_stmt labelled_name class_syntax tyco_syntax const_syntax |
27 fun print_haskell_stmt labelled_name class_syntax tyco_syntax const_syntax |
28 reserved deresolve contr_classparam_typs deriving_show = |
28 reserved deresolve contr_classparam_typs deriving_show = |
29 let |
29 let |
30 val deresolve_base = Long_Name.base_name o deresolve; |
|
31 fun class_name class = case class_syntax class |
30 fun class_name class = case class_syntax class |
32 of NONE => deresolve class |
31 of NONE => deresolve class |
33 | SOME class => class; |
32 | SOME class => class; |
34 fun print_typcontext tyvars vs = case maps (fn (v, sort) => map (pair v) sort) vs |
33 fun print_typcontext tyvars vs = case maps (fn (v, sort) => map (pair v) sort) vs |
35 of [] => [] |
34 of [] => [] |
119 fun print_stmt (name, Code_Thingol.Fun (_, (((vs, ty), raw_eqs), _))) = |
118 fun print_stmt (name, Code_Thingol.Fun (_, (((vs, ty), raw_eqs), _))) = |
120 let |
119 let |
121 val tyvars = intro_vars (map fst vs) reserved; |
120 val tyvars = intro_vars (map fst vs) reserved; |
122 fun print_err n = |
121 fun print_err n = |
123 semicolon ( |
122 semicolon ( |
124 (str o deresolve_base) name |
123 (str o deresolve) name |
125 :: map str (replicate n "_") |
124 :: map str (replicate n "_") |
126 @ str "=" |
125 @ str "=" |
127 :: str "error" |
126 :: str "error" |
128 @@ (str o ML_Syntax.print_string |
127 @@ (str o ML_Syntax.print_string |
129 o Long_Name.base_name o Long_Name.qualifier) name |
128 o Long_Name.base_name o Long_Name.qualifier) name |
136 (is_none o const_syntax) deresolve consts |
135 (is_none o const_syntax) deresolve consts |
137 |> intro_vars ((fold o Code_Thingol.fold_varnames) |
136 |> intro_vars ((fold o Code_Thingol.fold_varnames) |
138 (insert (op =)) ts []); |
137 (insert (op =)) ts []); |
139 in |
138 in |
140 semicolon ( |
139 semicolon ( |
141 (str o deresolve_base) name |
140 (str o deresolve) name |
142 :: map (print_term tyvars some_thm vars BR) ts |
141 :: map (print_term tyvars some_thm vars BR) ts |
143 @ str "=" |
142 @ str "=" |
144 @@ print_term tyvars some_thm vars NOBR t |
143 @@ print_term tyvars some_thm vars NOBR t |
145 ) |
144 ) |
146 end; |
145 end; |
147 in |
146 in |
148 Pretty.chunks ( |
147 Pretty.chunks ( |
149 semicolon [ |
148 semicolon [ |
150 (str o suffix " ::" o deresolve_base) name, |
149 (str o suffix " ::" o deresolve) name, |
151 print_typscheme tyvars (vs, ty) |
150 print_typscheme tyvars (vs, ty) |
152 ] |
151 ] |
153 :: (case filter (snd o snd) raw_eqs |
152 :: (case filter (snd o snd) raw_eqs |
154 of [] => [print_err ((length o fst o Code_Thingol.unfold_fun) ty)] |
153 of [] => [print_err ((length o fst o Code_Thingol.unfold_fun) ty)] |
155 | eqs => map print_eqn eqs) |
154 | eqs => map print_eqn eqs) |
159 let |
158 let |
160 val tyvars = intro_vars (map fst vs) reserved; |
159 val tyvars = intro_vars (map fst vs) reserved; |
161 in |
160 in |
162 semicolon [ |
161 semicolon [ |
163 str "data", |
162 str "data", |
164 print_typdecl tyvars (vs, (deresolve_base name, map (ITyVar o fst) vs)) |
163 print_typdecl tyvars (vs, (deresolve name, map (ITyVar o fst) vs)) |
165 ] |
164 ] |
166 end |
165 end |
167 | print_stmt (name, Code_Thingol.Datatype (_, (vs, [((co, _), [ty])]))) = |
166 | print_stmt (name, Code_Thingol.Datatype (_, (vs, [((co, _), [ty])]))) = |
168 let |
167 let |
169 val tyvars = intro_vars (map fst vs) reserved; |
168 val tyvars = intro_vars (map fst vs) reserved; |
170 in |
169 in |
171 semicolon ( |
170 semicolon ( |
172 str "newtype" |
171 str "newtype" |
173 :: print_typdecl tyvars (vs, (deresolve_base name, map (ITyVar o fst) vs)) |
172 :: print_typdecl tyvars (vs, (deresolve name, map (ITyVar o fst) vs)) |
174 :: str "=" |
173 :: str "=" |
175 :: (str o deresolve_base) co |
174 :: (str o deresolve) co |
176 :: print_typ tyvars BR ty |
175 :: print_typ tyvars BR ty |
177 :: (if deriving_show name then [str "deriving (Read, Show)"] else []) |
176 :: (if deriving_show name then [str "deriving (Read, Show)"] else []) |
178 ) |
177 ) |
179 end |
178 end |
180 | print_stmt (name, Code_Thingol.Datatype (_, (vs, co :: cos))) = |
179 | print_stmt (name, Code_Thingol.Datatype (_, (vs, co :: cos))) = |
181 let |
180 let |
182 val tyvars = intro_vars (map fst vs) reserved; |
181 val tyvars = intro_vars (map fst vs) reserved; |
183 fun print_co ((co, _), tys) = |
182 fun print_co ((co, _), tys) = |
184 concat ( |
183 concat ( |
185 (str o deresolve_base) co |
184 (str o deresolve) co |
186 :: map (print_typ tyvars BR) tys |
185 :: map (print_typ tyvars BR) tys |
187 ) |
186 ) |
188 in |
187 in |
189 semicolon ( |
188 semicolon ( |
190 str "data" |
189 str "data" |
191 :: print_typdecl tyvars (vs, (deresolve_base name, map (ITyVar o fst) vs)) |
190 :: print_typdecl tyvars (vs, (deresolve name, map (ITyVar o fst) vs)) |
192 :: str "=" |
191 :: str "=" |
193 :: print_co co |
192 :: print_co co |
194 :: map ((fn p => Pretty.block [str "| ", p]) o print_co) cos |
193 :: map ((fn p => Pretty.block [str "| ", p]) o print_co) cos |
195 @ (if deriving_show name then [str "deriving (Read, Show)"] else []) |
194 @ (if deriving_show name then [str "deriving (Read, Show)"] else []) |
196 ) |
195 ) |
198 | print_stmt (name, Code_Thingol.Class (_, (v, (super_classes, classparams)))) = |
197 | print_stmt (name, Code_Thingol.Class (_, (v, (super_classes, classparams)))) = |
199 let |
198 let |
200 val tyvars = intro_vars [v] reserved; |
199 val tyvars = intro_vars [v] reserved; |
201 fun print_classparam (classparam, ty) = |
200 fun print_classparam (classparam, ty) = |
202 semicolon [ |
201 semicolon [ |
203 (str o deresolve_base) classparam, |
202 (str o deresolve) classparam, |
204 str "::", |
203 str "::", |
205 print_typ tyvars NOBR ty |
204 print_typ tyvars NOBR ty |
206 ] |
205 ] |
207 in |
206 in |
208 Pretty.block_enclose ( |
207 Pretty.block_enclose ( |
209 Pretty.block [ |
208 Pretty.block [ |
210 str "class ", |
209 str "class ", |
211 Pretty.block (print_typcontext tyvars [(v, map fst super_classes)]), |
210 Pretty.block (print_typcontext tyvars [(v, map fst super_classes)]), |
212 str (deresolve_base name ^ " " ^ lookup_var tyvars v), |
211 str (deresolve name ^ " " ^ lookup_var tyvars v), |
213 str " where {" |
212 str " where {" |
214 ], |
213 ], |
215 str "};" |
214 str "};" |
216 ) (map print_classparam classparams) |
215 ) (map print_classparam classparams) |
217 end |
216 end |
218 | print_stmt (_, Code_Thingol.Classinst ((class, (tyco, vs)), (_, (classparam_instances, _)))) = |
217 | print_stmt (_, Code_Thingol.Classinst ((class, (tyco, vs)), (_, (classparam_instances, _)))) = |
219 let |
218 let |
220 val tyvars = intro_vars (map fst vs) reserved; |
219 val tyvars = intro_vars (map fst vs) reserved; |
221 fun requires_args classparam = case const_syntax classparam |
220 fun requires_args classparam = case const_syntax classparam |
222 of NONE => 0 |
221 of NONE => NONE |
223 | SOME (Code_Printer.Plain_const_syntax _) => 0 |
222 | SOME (Code_Printer.Plain_const_syntax _) => SOME 0 |
224 | SOME (Code_Printer.Complex_const_syntax (k,_ )) => k; |
223 | SOME (Code_Printer.Complex_const_syntax (k,_ )) => SOME k; |
225 fun print_classparam_instance ((classparam, const), (thm, _)) = |
224 fun print_classparam_instance ((classparam, const), (thm, _)) = |
226 case requires_args classparam |
225 case requires_args classparam |
227 of 0 => semicolon [ |
226 of NONE => semicolon [ |
228 (str o deresolve_base) classparam, |
227 (str o Long_Name.base_name o deresolve) classparam, |
229 str "=", |
228 str "=", |
230 print_app tyvars (SOME thm) reserved NOBR (const, []) |
229 print_app tyvars (SOME thm) reserved NOBR (const, []) |
231 ] |
230 ] |
232 | k => |
231 | SOME k => |
233 let |
232 let |
234 val (c, (_, tys)) = const; |
233 val (c, (_, tys)) = const; |
235 val (vs, rhs) = (apfst o map) fst |
234 val (vs, rhs) = (apfst o map) fst |
236 (Code_Thingol.unfold_abs (Code_Thingol.eta_expand k (const, []))); |
235 (Code_Thingol.unfold_abs (Code_Thingol.eta_expand k (const, []))); |
237 val s = if (is_some o const_syntax) c |
236 val s = if (is_some o const_syntax) c |