author | haftmann |
Thu, 13 Nov 2008 15:59:36 +0100 | |
changeset 28743 | eda4a5f64f2e |
parent 28724 | 4656aacba2bc |
child 28971 | 300ec36a19af |
permissions | -rw-r--r-- |
28054 | 1 |
(* Title: Tools/code/code_ml.ML |
2 |
ID: $Id$ |
|
3 |
Author: Florian Haftmann, TU Muenchen |
|
4 |
||
5 |
Serializer for SML and OCaml. |
|
6 |
*) |
|
7 |
||
8 |
signature CODE_ML = |
|
9 |
sig |
|
10 |
val eval_term: string * (unit -> 'a) option ref |
|
28724 | 11 |
-> theory -> term -> string list -> 'a |
28054 | 12 |
val setup: theory -> theory |
13 |
end; |
|
14 |
||
15 |
structure Code_ML : CODE_ML = |
|
16 |
struct |
|
17 |
||
18 |
open Basic_Code_Thingol; |
|
19 |
open Code_Printer; |
|
20 |
||
21 |
infixr 5 @@; |
|
22 |
infixr 5 @|; |
|
23 |
||
24 |
val target_SML = "SML"; |
|
25 |
val target_OCaml = "OCaml"; |
|
26 |
||
27 |
datatype ml_stmt = |
|
28350 | 28 |
MLFuns of (string * (typscheme * ((iterm list * iterm) * (thm * bool)) list)) list |
28054 | 29 |
| MLDatas of (string * ((vname * sort) list * (string * itype list) list)) list |
30 |
| MLClass of string * (vname * ((class * string) list * (string * itype) list)) |
|
31 |
| MLClassinst of string * ((class * (string * (vname * sort) list)) |
|
32 |
* ((class * (string * (string * dict list list))) list |
|
28350 | 33 |
* ((string * const) * (thm * bool)) list)); |
28054 | 34 |
|
35 |
fun stmt_names_of (MLFuns fs) = map fst fs |
|
36 |
| stmt_names_of (MLDatas ds) = map fst ds |
|
37 |
| stmt_names_of (MLClass (c, _)) = [c] |
|
38 |
| stmt_names_of (MLClassinst (i, _)) = [i]; |
|
39 |
||
40 |
||
41 |
(** SML serailizer **) |
|
42 |
||
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
43 |
fun pr_sml_stmt naming labelled_name syntax_tyco syntax_const reserved_names deresolve is_cons = |
28054 | 44 |
let |
45 |
val pr_label_classrel = translate_string (fn "." => "__" | c => c) |
|
46 |
o NameSpace.qualifier; |
|
47 |
val pr_label_classparam = NameSpace.base o NameSpace.qualifier; |
|
48 |
fun pr_dicts fxy ds = |
|
49 |
let |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
50 |
fun pr_dictvar (v, (_, 1)) = Code_Name.first_upper v ^ "_" |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
51 |
| pr_dictvar (v, (i, _)) = Code_Name.first_upper v ^ string_of_int (i+1) ^ "_"; |
28054 | 52 |
fun pr_proj [] p = |
53 |
p |
|
54 |
| pr_proj [p'] p = |
|
55 |
brackets [p', p] |
|
56 |
| pr_proj (ps as _ :: _) p = |
|
57 |
brackets [Pretty.enum " o" "(" ")" ps, p]; |
|
58 |
fun pr_dict fxy (DictConst (inst, dss)) = |
|
59 |
brackify fxy ((str o deresolve) inst :: map (pr_dicts BR) dss) |
|
60 |
| pr_dict fxy (DictVar (classrels, v)) = |
|
61 |
pr_proj (map (str o deresolve) classrels) ((str o pr_dictvar) v) |
|
62 |
in case ds |
|
63 |
of [] => str "()" |
|
64 |
| [d] => pr_dict fxy d |
|
65 |
| _ :: _ => (Pretty.list "(" ")" o map (pr_dict NOBR)) ds |
|
66 |
end; |
|
67 |
fun pr_tyvar_dicts vs = |
|
68 |
vs |
|
69 |
|> map (fn (v, sort) => map_index (fn (i, _) => |
|
70 |
DictVar ([], (v, (i, length sort)))) sort) |
|
71 |
|> map (pr_dicts BR); |
|
72 |
fun pr_tycoexpr fxy (tyco, tys) = |
|
73 |
let |
|
74 |
val tyco' = (str o deresolve) tyco |
|
75 |
in case map (pr_typ BR) tys |
|
76 |
of [] => tyco' |
|
77 |
| [p] => Pretty.block [p, Pretty.brk 1, tyco'] |
|
78 |
| (ps as _::_) => Pretty.block [Pretty.list "(" ")" ps, Pretty.brk 1, tyco'] |
|
79 |
end |
|
80 |
and pr_typ fxy (tyco `%% tys) = (case syntax_tyco tyco |
|
81 |
of NONE => pr_tycoexpr fxy (tyco, tys) |
|
82 |
| SOME (i, pr) => pr pr_typ fxy tys) |
|
83 |
| pr_typ fxy (ITyVar v) = str ("'" ^ v); |
|
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
84 |
fun pr_term thm vars fxy (IConst c) = |
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
85 |
pr_app thm vars fxy (c, []) |
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
86 |
| pr_term thm vars fxy (IVar v) = |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
87 |
str (Code_Name.lookup_var vars v) |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
88 |
| pr_term thm vars fxy (t as t1 `$ t2) = |
28054 | 89 |
(case Code_Thingol.unfold_const_app t |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
90 |
of SOME c_ts => pr_app thm vars fxy c_ts |
28054 | 91 |
| NONE => |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
92 |
brackify fxy [pr_term thm vars NOBR t1, pr_term thm vars BR t2]) |
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
93 |
| pr_term thm vars fxy (t as _ `|-> _) = |
28054 | 94 |
let |
95 |
val (binds, t') = Code_Thingol.unfold_abs t; |
|
96 |
fun pr ((v, pat), ty) = |
|
97 |
pr_bind thm NOBR ((SOME v, pat), ty) |
|
98 |
#>> (fn p => concat [str "fn", p, str "=>"]); |
|
99 |
val (ps, vars') = fold_map pr binds vars; |
|
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
100 |
in brackets (ps @ [pr_term thm vars' NOBR t']) end |
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
101 |
| pr_term thm vars fxy (ICase (cases as (_, t0))) = |
28054 | 102 |
(case Code_Thingol.unfold_const_app t0 |
103 |
of SOME (c_ts as ((c, _), _)) => if is_none (syntax_const c) |
|
104 |
then pr_case thm vars fxy cases |
|
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
105 |
else pr_app thm vars fxy c_ts |
28054 | 106 |
| NONE => pr_case thm vars fxy cases) |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
107 |
and pr_app' thm vars (app as ((c, (iss, tys)), ts)) = |
28054 | 108 |
if is_cons c then let |
109 |
val k = length tys |
|
110 |
in if k < 2 then |
|
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
111 |
(str o deresolve) c :: map (pr_term thm vars BR) ts |
28054 | 112 |
else if k = length ts then |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
113 |
[(str o deresolve) c, Pretty.enum "," "(" ")" (map (pr_term thm vars NOBR) ts)] |
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
114 |
else [pr_term thm vars BR (Code_Thingol.eta_expand k app)] end else |
28054 | 115 |
(str o deresolve) c |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
116 |
:: (map (pr_dicts BR) o filter_out null) iss @ map (pr_term thm vars BR) ts |
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
117 |
and pr_app thm vars = gen_pr_app pr_app' pr_term syntax_const naming thm vars |
28054 | 118 |
and pr_bind' ((NONE, NONE), _) = str "_" |
119 |
| pr_bind' ((SOME v, NONE), _) = str v |
|
120 |
| pr_bind' ((NONE, SOME p), _) = p |
|
121 |
| pr_bind' ((SOME v, SOME p), _) = concat [str v, str "as", p] |
|
122 |
and pr_bind thm = gen_pr_bind pr_bind' pr_term thm |
|
123 |
and pr_case thm vars fxy (cases as ((_, [_]), _)) = |
|
124 |
let |
|
125 |
val (binds, t') = Code_Thingol.unfold_let (ICase cases); |
|
126 |
fun pr ((pat, ty), t) vars = |
|
127 |
vars |
|
128 |
|> pr_bind thm NOBR ((NONE, SOME pat), ty) |
|
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
129 |
|>> (fn p => semicolon [str "val", p, str "=", pr_term thm vars NOBR t]) |
28054 | 130 |
val (ps, vars') = fold_map pr binds vars; |
131 |
in |
|
132 |
Pretty.chunks [ |
|
133 |
[str ("let"), Pretty.fbrk, Pretty.chunks ps] |> Pretty.block, |
|
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
134 |
[str ("in"), Pretty.fbrk, pr_term thm vars' NOBR t'] |> Pretty.block, |
28054 | 135 |
str ("end") |
136 |
] |
|
137 |
end |
|
138 |
| pr_case thm vars fxy (((td, ty), b::bs), _) = |
|
139 |
let |
|
140 |
fun pr delim (pat, t) = |
|
141 |
let |
|
142 |
val (p, vars') = pr_bind thm NOBR ((NONE, SOME pat), ty) vars; |
|
143 |
in |
|
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
144 |
concat [str delim, p, str "=>", pr_term thm vars' NOBR t] |
28054 | 145 |
end; |
146 |
in |
|
147 |
(Pretty.enclose "(" ")" o single o brackify fxy) ( |
|
148 |
str "case" |
|
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
149 |
:: pr_term thm vars NOBR td |
28054 | 150 |
:: pr "of" b |
151 |
:: map (pr "|") bs |
|
152 |
) |
|
153 |
end |
|
154 |
| pr_case thm vars fxy ((_, []), _) = str "raise Fail \"empty case\""; |
|
155 |
fun pr_stmt (MLFuns (funns as (funn :: funns'))) = |
|
156 |
let |
|
157 |
val definer = |
|
158 |
let |
|
159 |
fun no_args _ (((ts, _), _) :: _) = length ts |
|
160 |
| no_args ty [] = (length o fst o Code_Thingol.unfold_fun) ty; |
|
161 |
fun mk 0 [] = "val" |
|
162 |
| mk 0 vs = if (null o filter_out (null o snd)) vs |
|
163 |
then "val" else "fun" |
|
164 |
| mk k _ = "fun"; |
|
165 |
fun chk (_, ((vs, ty), eqs)) NONE = SOME (mk (no_args ty eqs) vs) |
|
166 |
| chk (_, ((vs, ty), eqs)) (SOME defi) = |
|
167 |
if defi = mk (no_args ty eqs) vs then SOME defi |
|
168 |
else error ("Mixing simultaneous vals and funs not implemented: " |
|
169 |
^ commas (map (labelled_name o fst) funns)); |
|
170 |
in the (fold chk funns NONE) end; |
|
171 |
fun pr_funn definer (name, ((vs, ty), [])) = |
|
172 |
let |
|
173 |
val vs_dict = filter_out (null o snd) vs; |
|
174 |
val n = length vs_dict + (length o fst o Code_Thingol.unfold_fun) ty; |
|
175 |
val exc_str = |
|
176 |
(ML_Syntax.print_string o NameSpace.base o NameSpace.qualifier) name; |
|
177 |
in |
|
178 |
concat ( |
|
179 |
str definer |
|
180 |
:: (str o deresolve) name |
|
181 |
:: map str (replicate n "_") |
|
182 |
@ str "=" |
|
183 |
:: str "raise" |
|
184 |
:: str "(Fail" |
|
185 |
@@ str (exc_str ^ ")") |
|
186 |
) |
|
187 |
end |
|
188 |
| pr_funn definer (name, ((vs, ty), eqs as eq :: eqs')) = |
|
189 |
let |
|
190 |
val vs_dict = filter_out (null o snd) vs; |
|
191 |
val shift = if null eqs' then I else |
|
192 |
map (Pretty.block o single o Pretty.block o single); |
|
28350 | 193 |
fun pr_eq definer ((ts, t), (thm, _)) = |
28054 | 194 |
let |
195 |
val consts = map_filter |
|
196 |
(fn c => if (is_some o syntax_const) c |
|
197 |
then NONE else (SOME o NameSpace.base o deresolve) c) |
|
198 |
((fold o Code_Thingol.fold_constnames) (insert (op =)) (t :: ts) []); |
|
199 |
val vars = reserved_names |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
200 |
|> Code_Name.intro_vars consts |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
201 |
|> Code_Name.intro_vars ((fold o Code_Thingol.fold_unbound_varnames) |
28054 | 202 |
(insert (op =)) ts []); |
203 |
in |
|
204 |
concat ( |
|
205 |
[str definer, (str o deresolve) name] |
|
206 |
@ (if null ts andalso null vs_dict |
|
207 |
then [str ":", pr_typ NOBR ty] |
|
208 |
else |
|
209 |
pr_tyvar_dicts vs_dict |
|
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
210 |
@ map (pr_term thm vars BR) ts) |
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
211 |
@ [str "=", pr_term thm vars NOBR t] |
28054 | 212 |
) |
213 |
end |
|
214 |
in |
|
215 |
(Pretty.block o Pretty.fbreaks o shift) ( |
|
216 |
pr_eq definer eq |
|
217 |
:: map (pr_eq "|") eqs' |
|
218 |
) |
|
219 |
end; |
|
220 |
val (ps, p) = split_last (pr_funn definer funn :: map (pr_funn "and") funns'); |
|
221 |
in Pretty.chunks (ps @ [Pretty.block ([p, str ";"])]) end |
|
222 |
| pr_stmt (MLDatas (datas as (data :: datas'))) = |
|
223 |
let |
|
224 |
fun pr_co (co, []) = |
|
225 |
str (deresolve co) |
|
226 |
| pr_co (co, tys) = |
|
227 |
concat [ |
|
228 |
str (deresolve co), |
|
229 |
str "of", |
|
230 |
Pretty.enum " *" "" "" (map (pr_typ (INFX (2, X))) tys) |
|
231 |
]; |
|
232 |
fun pr_data definer (tyco, (vs, [])) = |
|
233 |
concat ( |
|
234 |
str definer |
|
235 |
:: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs) |
|
236 |
:: str "=" |
|
237 |
@@ str "EMPTY__" |
|
238 |
) |
|
239 |
| pr_data definer (tyco, (vs, cos)) = |
|
240 |
concat ( |
|
241 |
str definer |
|
242 |
:: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs) |
|
243 |
:: str "=" |
|
244 |
:: separate (str "|") (map pr_co cos) |
|
245 |
); |
|
246 |
val (ps, p) = split_last |
|
247 |
(pr_data "datatype" data :: map (pr_data "and") datas'); |
|
248 |
in Pretty.chunks (ps @ [Pretty.block ([p, str ";"])]) end |
|
249 |
| pr_stmt (MLClass (class, (v, (superclasses, classparams)))) = |
|
250 |
let |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
251 |
val w = Code_Name.first_upper v ^ "_"; |
28054 | 252 |
fun pr_superclass_field (class, classrel) = |
253 |
(concat o map str) [ |
|
254 |
pr_label_classrel classrel, ":", "'" ^ v, deresolve class |
|
255 |
]; |
|
256 |
fun pr_classparam_field (classparam, ty) = |
|
257 |
concat [ |
|
258 |
(str o pr_label_classparam) classparam, str ":", pr_typ NOBR ty |
|
259 |
]; |
|
260 |
fun pr_classparam_proj (classparam, _) = |
|
261 |
semicolon [ |
|
262 |
str "fun", |
|
263 |
(str o deresolve) classparam, |
|
264 |
Pretty.enclose "(" ")" [str (w ^ ":'" ^ v ^ " " ^ deresolve class)], |
|
265 |
str "=", |
|
266 |
str ("#" ^ pr_label_classparam classparam), |
|
267 |
str w |
|
268 |
]; |
|
269 |
fun pr_superclass_proj (_, classrel) = |
|
270 |
semicolon [ |
|
271 |
str "fun", |
|
272 |
(str o deresolve) classrel, |
|
273 |
Pretty.enclose "(" ")" [str (w ^ ":'" ^ v ^ " " ^ deresolve class)], |
|
274 |
str "=", |
|
275 |
str ("#" ^ pr_label_classrel classrel), |
|
276 |
str w |
|
277 |
]; |
|
278 |
in |
|
279 |
Pretty.chunks ( |
|
280 |
concat [ |
|
281 |
str ("type '" ^ v), |
|
282 |
(str o deresolve) class, |
|
283 |
str "=", |
|
284 |
Pretty.enum "," "{" "};" ( |
|
285 |
map pr_superclass_field superclasses @ map pr_classparam_field classparams |
|
286 |
) |
|
287 |
] |
|
288 |
:: map pr_superclass_proj superclasses |
|
289 |
@ map pr_classparam_proj classparams |
|
290 |
) |
|
291 |
end |
|
292 |
| pr_stmt (MLClassinst (inst, ((class, (tyco, arity)), (superarities, classparam_insts)))) = |
|
293 |
let |
|
294 |
fun pr_superclass (_, (classrel, dss)) = |
|
295 |
concat [ |
|
296 |
(str o pr_label_classrel) classrel, |
|
297 |
str "=", |
|
298 |
pr_dicts NOBR [DictConst dss] |
|
299 |
]; |
|
28350 | 300 |
fun pr_classparam ((classparam, c_inst), (thm, _)) = |
28054 | 301 |
concat [ |
302 |
(str o pr_label_classparam) classparam, |
|
303 |
str "=", |
|
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
304 |
pr_app thm reserved_names NOBR (c_inst, []) |
28054 | 305 |
]; |
306 |
in |
|
307 |
semicolon ([ |
|
308 |
str (if null arity then "val" else "fun"), |
|
309 |
(str o deresolve) inst ] @ |
|
310 |
pr_tyvar_dicts arity @ [ |
|
311 |
str "=", |
|
312 |
Pretty.enum "," "{" "}" |
|
313 |
(map pr_superclass superarities @ map pr_classparam classparam_insts), |
|
314 |
str ":", |
|
315 |
pr_tycoexpr NOBR (class, [tyco `%% map (ITyVar o fst) arity]) |
|
316 |
]) |
|
317 |
end; |
|
318 |
in pr_stmt end; |
|
319 |
||
320 |
fun pr_sml_module name content = |
|
321 |
Pretty.chunks ( |
|
322 |
str ("structure " ^ name ^ " = ") |
|
323 |
:: str "struct" |
|
324 |
:: str "" |
|
325 |
:: content |
|
326 |
@ str "" |
|
327 |
@@ str ("end; (*struct " ^ name ^ "*)") |
|
328 |
); |
|
329 |
||
28064 | 330 |
val literals_sml = Literals { |
331 |
literal_char = prefix "#" o quote o ML_Syntax.print_char, |
|
332 |
literal_string = quote o translate_string ML_Syntax.print_char, |
|
333 |
literal_numeral = fn unbounded => fn k => |
|
334 |
if unbounded then "(" ^ string_of_int k ^ " : IntInf.int)" |
|
335 |
else string_of_int k, |
|
336 |
literal_list = Pretty.enum "," "[" "]", |
|
337 |
infix_cons = (7, "::") |
|
338 |
}; |
|
339 |
||
28054 | 340 |
|
341 |
(** OCaml serializer **) |
|
342 |
||
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
343 |
fun pr_ocaml_stmt naming labelled_name syntax_tyco syntax_const reserved_names deresolve is_cons = |
28054 | 344 |
let |
345 |
fun pr_dicts fxy ds = |
|
346 |
let |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
347 |
fun pr_dictvar (v, (_, 1)) = "_" ^ Code_Name.first_upper v |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
348 |
| pr_dictvar (v, (i, _)) = "_" ^ Code_Name.first_upper v ^ string_of_int (i+1); |
28054 | 349 |
fun pr_proj ps p = |
350 |
fold_rev (fn p2 => fn p1 => Pretty.block [p1, str ".", str p2]) ps p |
|
351 |
fun pr_dict fxy (DictConst (inst, dss)) = |
|
352 |
brackify fxy ((str o deresolve) inst :: map (pr_dicts BR) dss) |
|
353 |
| pr_dict fxy (DictVar (classrels, v)) = |
|
354 |
pr_proj (map deresolve classrels) ((str o pr_dictvar) v) |
|
355 |
in case ds |
|
356 |
of [] => str "()" |
|
357 |
| [d] => pr_dict fxy d |
|
358 |
| _ :: _ => (Pretty.list "(" ")" o map (pr_dict NOBR)) ds |
|
359 |
end; |
|
360 |
fun pr_tyvar_dicts vs = |
|
361 |
vs |
|
362 |
|> map (fn (v, sort) => map_index (fn (i, _) => |
|
363 |
DictVar ([], (v, (i, length sort)))) sort) |
|
364 |
|> map (pr_dicts BR); |
|
365 |
fun pr_tycoexpr fxy (tyco, tys) = |
|
366 |
let |
|
367 |
val tyco' = (str o deresolve) tyco |
|
368 |
in case map (pr_typ BR) tys |
|
369 |
of [] => tyco' |
|
370 |
| [p] => Pretty.block [p, Pretty.brk 1, tyco'] |
|
371 |
| (ps as _::_) => Pretty.block [Pretty.list "(" ")" ps, Pretty.brk 1, tyco'] |
|
372 |
end |
|
373 |
and pr_typ fxy (tyco `%% tys) = (case syntax_tyco tyco |
|
374 |
of NONE => pr_tycoexpr fxy (tyco, tys) |
|
375 |
| SOME (i, pr) => pr pr_typ fxy tys) |
|
376 |
| pr_typ fxy (ITyVar v) = str ("'" ^ v); |
|
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
377 |
fun pr_term thm vars fxy (IConst c) = |
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
378 |
pr_app thm vars fxy (c, []) |
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
379 |
| pr_term thm vars fxy (IVar v) = |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
380 |
str (Code_Name.lookup_var vars v) |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
381 |
| pr_term thm vars fxy (t as t1 `$ t2) = |
28054 | 382 |
(case Code_Thingol.unfold_const_app t |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
383 |
of SOME c_ts => pr_app thm vars fxy c_ts |
28054 | 384 |
| NONE => |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
385 |
brackify fxy [pr_term thm vars NOBR t1, pr_term thm vars BR t2]) |
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
386 |
| pr_term thm vars fxy (t as _ `|-> _) = |
28054 | 387 |
let |
388 |
val (binds, t') = Code_Thingol.unfold_abs t; |
|
389 |
fun pr ((v, pat), ty) = pr_bind thm BR ((SOME v, pat), ty); |
|
390 |
val (ps, vars') = fold_map pr binds vars; |
|
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
391 |
in brackets (str "fun" :: ps @ str "->" @@ pr_term thm vars' NOBR t') end |
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
392 |
| pr_term thm vars fxy (ICase (cases as (_, t0))) = (case Code_Thingol.unfold_const_app t0 |
28054 | 393 |
of SOME (c_ts as ((c, _), _)) => if is_none (syntax_const c) |
394 |
then pr_case thm vars fxy cases |
|
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
395 |
else pr_app thm vars fxy c_ts |
28054 | 396 |
| NONE => pr_case thm vars fxy cases) |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
397 |
and pr_app' thm vars (app as ((c, (iss, tys)), ts)) = |
28054 | 398 |
if is_cons c then |
399 |
if length tys = length ts |
|
400 |
then case ts |
|
401 |
of [] => [(str o deresolve) c] |
|
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
402 |
| [t] => [(str o deresolve) c, pr_term thm vars BR t] |
28054 | 403 |
| _ => [(str o deresolve) c, Pretty.enum "," "(" ")" |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
404 |
(map (pr_term thm vars NOBR) ts)] |
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
405 |
else [pr_term thm vars BR (Code_Thingol.eta_expand (length tys) app)] |
28054 | 406 |
else (str o deresolve) c |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
407 |
:: ((map (pr_dicts BR) o filter_out null) iss @ map (pr_term thm vars BR) ts) |
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
408 |
and pr_app thm vars = gen_pr_app pr_app' pr_term syntax_const naming thm vars |
28054 | 409 |
and pr_bind' ((NONE, NONE), _) = str "_" |
410 |
| pr_bind' ((SOME v, NONE), _) = str v |
|
411 |
| pr_bind' ((NONE, SOME p), _) = p |
|
412 |
| pr_bind' ((SOME v, SOME p), _) = brackets [p, str "as", str v] |
|
413 |
and pr_bind thm = gen_pr_bind pr_bind' pr_term thm |
|
414 |
and pr_case thm vars fxy (cases as ((_, [_]), _)) = |
|
415 |
let |
|
416 |
val (binds, t') = Code_Thingol.unfold_let (ICase cases); |
|
417 |
fun pr ((pat, ty), t) vars = |
|
418 |
vars |
|
419 |
|> pr_bind thm NOBR ((NONE, SOME pat), ty) |
|
420 |
|>> (fn p => concat |
|
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
421 |
[str "let", p, str "=", pr_term thm vars NOBR t, str "in"]) |
28054 | 422 |
val (ps, vars') = fold_map pr binds vars; |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
423 |
in Pretty.chunks (ps @| pr_term thm vars' NOBR t') end |
28054 | 424 |
| pr_case thm vars fxy (((td, ty), b::bs), _) = |
425 |
let |
|
426 |
fun pr delim (pat, t) = |
|
427 |
let |
|
428 |
val (p, vars') = pr_bind thm NOBR ((NONE, SOME pat), ty) vars; |
|
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
429 |
in concat [str delim, p, str "->", pr_term thm vars' NOBR t] end; |
28054 | 430 |
in |
431 |
(Pretty.enclose "(" ")" o single o brackify fxy) ( |
|
432 |
str "match" |
|
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
433 |
:: pr_term thm vars NOBR td |
28054 | 434 |
:: pr "with" b |
435 |
:: map (pr "|") bs |
|
436 |
) |
|
437 |
end |
|
438 |
| pr_case thm vars fxy ((_, []), _) = str "failwith \"empty case\""; |
|
439 |
fun fish_params vars eqs = |
|
440 |
let |
|
441 |
fun fish_param _ (w as SOME _) = w |
|
442 |
| fish_param (IVar v) NONE = SOME v |
|
443 |
| fish_param _ NONE = NONE; |
|
444 |
fun fillup_param _ (_, SOME v) = v |
|
445 |
| fillup_param x (i, NONE) = x ^ string_of_int i; |
|
446 |
val fished1 = fold (map2 fish_param) eqs (replicate (length (hd eqs)) NONE); |
|
447 |
val x = Name.variant (map_filter I fished1) "x"; |
|
448 |
val fished2 = map_index (fillup_param x) fished1; |
|
449 |
val (fished3, _) = Name.variants fished2 Name.context; |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
450 |
val vars' = Code_Name.intro_vars fished3 vars; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
451 |
in map (Code_Name.lookup_var vars') fished3 end; |
28054 | 452 |
fun pr_stmt (MLFuns (funns as funn :: funns')) = |
453 |
let |
|
28350 | 454 |
fun pr_eq ((ts, t), (thm, _)) = |
28054 | 455 |
let |
456 |
val consts = map_filter |
|
457 |
(fn c => if (is_some o syntax_const) c |
|
458 |
then NONE else (SOME o NameSpace.base o deresolve) c) |
|
459 |
((fold o Code_Thingol.fold_constnames) (insert (op =)) (t :: ts) []); |
|
460 |
val vars = reserved_names |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
461 |
|> Code_Name.intro_vars consts |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
462 |
|> Code_Name.intro_vars ((fold o Code_Thingol.fold_unbound_varnames) |
28054 | 463 |
(insert (op =)) ts []); |
464 |
in concat [ |
|
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
465 |
(Pretty.block o Pretty.commas) (map (pr_term thm vars NOBR) ts), |
28054 | 466 |
str "->", |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
467 |
pr_term thm vars NOBR t |
28054 | 468 |
] end; |
469 |
fun pr_eqs name ty [] = |
|
470 |
let |
|
471 |
val n = (length o fst o Code_Thingol.unfold_fun) ty; |
|
472 |
val exc_str = |
|
473 |
(ML_Syntax.print_string o NameSpace.base o NameSpace.qualifier) name; |
|
474 |
in |
|
475 |
concat ( |
|
476 |
map str (replicate n "_") |
|
477 |
@ str "=" |
|
478 |
:: str "failwith" |
|
479 |
@@ str exc_str |
|
480 |
) |
|
481 |
end |
|
28350 | 482 |
| pr_eqs _ _ [((ts, t), (thm, _))] = |
28054 | 483 |
let |
484 |
val consts = map_filter |
|
485 |
(fn c => if (is_some o syntax_const) c |
|
486 |
then NONE else (SOME o NameSpace.base o deresolve) c) |
|
487 |
((fold o Code_Thingol.fold_constnames) (insert (op =)) (t :: ts) []); |
|
488 |
val vars = reserved_names |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
489 |
|> Code_Name.intro_vars consts |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
490 |
|> Code_Name.intro_vars ((fold o Code_Thingol.fold_unbound_varnames) |
28054 | 491 |
(insert (op =)) ts []); |
492 |
in |
|
493 |
concat ( |
|
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
494 |
map (pr_term thm vars BR) ts |
28054 | 495 |
@ str "=" |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
496 |
@@ pr_term thm vars NOBR t |
28054 | 497 |
) |
498 |
end |
|
499 |
| pr_eqs _ _ (eqs as (eq as (([_], _), _)) :: eqs') = |
|
500 |
Pretty.block ( |
|
501 |
str "=" |
|
502 |
:: Pretty.brk 1 |
|
503 |
:: str "function" |
|
504 |
:: Pretty.brk 1 |
|
505 |
:: pr_eq eq |
|
506 |
:: maps (append [Pretty.fbrk, str "|", Pretty.brk 1] |
|
507 |
o single o pr_eq) eqs' |
|
508 |
) |
|
509 |
| pr_eqs _ _ (eqs as eq :: eqs') = |
|
510 |
let |
|
511 |
val consts = map_filter |
|
512 |
(fn c => if (is_some o syntax_const) c |
|
513 |
then NONE else (SOME o NameSpace.base o deresolve) c) |
|
514 |
((fold o Code_Thingol.fold_constnames) |
|
515 |
(insert (op =)) (map (snd o fst) eqs) []); |
|
516 |
val vars = reserved_names |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
517 |
|> Code_Name.intro_vars consts; |
28054 | 518 |
val dummy_parms = (map str o fish_params vars o map (fst o fst)) eqs; |
519 |
in |
|
520 |
Pretty.block ( |
|
521 |
Pretty.breaks dummy_parms |
|
522 |
@ Pretty.brk 1 |
|
523 |
:: str "=" |
|
524 |
:: Pretty.brk 1 |
|
525 |
:: str "match" |
|
526 |
:: Pretty.brk 1 |
|
527 |
:: (Pretty.block o Pretty.commas) dummy_parms |
|
528 |
:: Pretty.brk 1 |
|
529 |
:: str "with" |
|
530 |
:: Pretty.brk 1 |
|
531 |
:: pr_eq eq |
|
532 |
:: maps (append [Pretty.fbrk, str "|", Pretty.brk 1] |
|
533 |
o single o pr_eq) eqs' |
|
534 |
) |
|
535 |
end; |
|
536 |
fun pr_funn definer (name, ((vs, ty), eqs)) = |
|
537 |
concat ( |
|
538 |
str definer |
|
539 |
:: (str o deresolve) name |
|
540 |
:: pr_tyvar_dicts (filter_out (null o snd) vs) |
|
541 |
@| pr_eqs name ty eqs |
|
542 |
); |
|
543 |
val (ps, p) = split_last |
|
544 |
(pr_funn "let rec" funn :: map (pr_funn "and") funns'); |
|
545 |
in Pretty.chunks (ps @ [Pretty.block ([p, str ";;"])]) end |
|
546 |
| pr_stmt (MLDatas (datas as (data :: datas'))) = |
|
547 |
let |
|
548 |
fun pr_co (co, []) = |
|
549 |
str (deresolve co) |
|
550 |
| pr_co (co, tys) = |
|
551 |
concat [ |
|
552 |
str (deresolve co), |
|
553 |
str "of", |
|
554 |
Pretty.enum " *" "" "" (map (pr_typ (INFX (2, X))) tys) |
|
555 |
]; |
|
556 |
fun pr_data definer (tyco, (vs, [])) = |
|
557 |
concat ( |
|
558 |
str definer |
|
559 |
:: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs) |
|
560 |
:: str "=" |
|
561 |
@@ str "EMPTY_" |
|
562 |
) |
|
563 |
| pr_data definer (tyco, (vs, cos)) = |
|
564 |
concat ( |
|
565 |
str definer |
|
566 |
:: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs) |
|
567 |
:: str "=" |
|
568 |
:: separate (str "|") (map pr_co cos) |
|
569 |
); |
|
570 |
val (ps, p) = split_last |
|
571 |
(pr_data "type" data :: map (pr_data "and") datas'); |
|
572 |
in Pretty.chunks (ps @ [Pretty.block ([p, str ";;"])]) end |
|
573 |
| pr_stmt (MLClass (class, (v, (superclasses, classparams)))) = |
|
574 |
let |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
575 |
val w = "_" ^ Code_Name.first_upper v; |
28054 | 576 |
fun pr_superclass_field (class, classrel) = |
577 |
(concat o map str) [ |
|
578 |
deresolve classrel, ":", "'" ^ v, deresolve class |
|
579 |
]; |
|
580 |
fun pr_classparam_field (classparam, ty) = |
|
581 |
concat [ |
|
582 |
(str o deresolve) classparam, str ":", pr_typ NOBR ty |
|
583 |
]; |
|
584 |
fun pr_classparam_proj (classparam, _) = |
|
585 |
concat [ |
|
586 |
str "let", |
|
587 |
(str o deresolve) classparam, |
|
588 |
str w, |
|
589 |
str "=", |
|
590 |
str (w ^ "." ^ deresolve classparam ^ ";;") |
|
591 |
]; |
|
592 |
in Pretty.chunks ( |
|
593 |
concat [ |
|
594 |
str ("type '" ^ v), |
|
595 |
(str o deresolve) class, |
|
596 |
str "=", |
|
597 |
enum_default "();;" ";" "{" "};;" ( |
|
598 |
map pr_superclass_field superclasses |
|
599 |
@ map pr_classparam_field classparams |
|
600 |
) |
|
601 |
] |
|
602 |
:: map pr_classparam_proj classparams |
|
603 |
) end |
|
604 |
| pr_stmt (MLClassinst (inst, ((class, (tyco, arity)), (superarities, classparam_insts)))) = |
|
605 |
let |
|
606 |
fun pr_superclass (_, (classrel, dss)) = |
|
607 |
concat [ |
|
608 |
(str o deresolve) classrel, |
|
609 |
str "=", |
|
610 |
pr_dicts NOBR [DictConst dss] |
|
611 |
]; |
|
28350 | 612 |
fun pr_classparam_inst ((classparam, c_inst), (thm, _)) = |
28054 | 613 |
concat [ |
614 |
(str o deresolve) classparam, |
|
615 |
str "=", |
|
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28705
diff
changeset
|
616 |
pr_app thm reserved_names NOBR (c_inst, []) |
28054 | 617 |
]; |
618 |
in |
|
619 |
concat ( |
|
620 |
str "let" |
|
621 |
:: (str o deresolve) inst |
|
622 |
:: pr_tyvar_dicts arity |
|
623 |
@ str "=" |
|
624 |
@@ (Pretty.enclose "(" ");;" o Pretty.breaks) [ |
|
625 |
enum_default "()" ";" "{" "}" (map pr_superclass superarities |
|
626 |
@ map pr_classparam_inst classparam_insts), |
|
627 |
str ":", |
|
628 |
pr_tycoexpr NOBR (class, [tyco `%% map (ITyVar o fst) arity]) |
|
629 |
] |
|
630 |
) |
|
631 |
end; |
|
632 |
in pr_stmt end; |
|
633 |
||
634 |
fun pr_ocaml_module name content = |
|
635 |
Pretty.chunks ( |
|
636 |
str ("module " ^ name ^ " = ") |
|
637 |
:: str "struct" |
|
638 |
:: str "" |
|
639 |
:: content |
|
640 |
@ str "" |
|
641 |
@@ str ("end;; (*struct " ^ name ^ "*)") |
|
642 |
); |
|
643 |
||
28064 | 644 |
val literals_ocaml = let |
645 |
fun chr i = |
|
646 |
let |
|
647 |
val xs = string_of_int i; |
|
648 |
val ys = replicate_string (3 - length (explode xs)) "0"; |
|
649 |
in "\\" ^ ys ^ xs end; |
|
650 |
fun char_ocaml c = |
|
651 |
let |
|
652 |
val i = ord c; |
|
653 |
val s = if i < 32 orelse i = 34 orelse i = 39 orelse i = 92 orelse i > 126 |
|
654 |
then chr i else c |
|
655 |
in s end; |
|
656 |
in Literals { |
|
657 |
literal_char = enclose "'" "'" o char_ocaml, |
|
658 |
literal_string = quote o translate_string char_ocaml, |
|
659 |
literal_numeral = fn unbounded => fn k => if k >= 0 then |
|
660 |
if unbounded then |
|
661 |
"(Big_int.big_int_of_int " ^ string_of_int k ^ ")" |
|
662 |
else string_of_int k |
|
663 |
else |
|
664 |
if unbounded then |
|
665 |
"(Big_int.big_int_of_int " ^ (enclose "(" ")" o prefix "-" |
|
666 |
o string_of_int o op ~) k ^ ")" |
|
667 |
else (enclose "(" ")" o prefix "-" o string_of_int o op ~) k, |
|
668 |
literal_list = Pretty.enum ";" "[" "]", |
|
669 |
infix_cons = (6, "::") |
|
670 |
} end; |
|
671 |
||
672 |
||
28054 | 673 |
|
674 |
(** SML/OCaml generic part **) |
|
675 |
||
676 |
local |
|
677 |
||
678 |
datatype ml_node = |
|
679 |
Dummy of string |
|
680 |
| Stmt of string * ml_stmt |
|
681 |
| Module of string * ((Name.context * Name.context) * ml_node Graph.T); |
|
682 |
||
683 |
in |
|
684 |
||
685 |
fun ml_node_of_program labelled_name module_name reserved_names raw_module_alias program = |
|
686 |
let |
|
687 |
val module_alias = if is_some module_name then K module_name else raw_module_alias; |
|
688 |
val reserved_names = Name.make_context reserved_names; |
|
689 |
val empty_module = ((reserved_names, reserved_names), Graph.empty); |
|
690 |
fun map_node [] f = f |
|
691 |
| map_node (m::ms) f = |
|
692 |
Graph.default_node (m, Module (m, empty_module)) |
|
693 |
#> Graph.map_node m (fn (Module (module_name, (nsp, nodes))) => |
|
694 |
Module (module_name, (nsp, map_node ms f nodes))); |
|
695 |
fun map_nsp_yield [] f (nsp, nodes) = |
|
696 |
let |
|
697 |
val (x, nsp') = f nsp |
|
698 |
in (x, (nsp', nodes)) end |
|
699 |
| map_nsp_yield (m::ms) f (nsp, nodes) = |
|
700 |
let |
|
701 |
val (x, nodes') = |
|
702 |
nodes |
|
703 |
|> Graph.default_node (m, Module (m, empty_module)) |
|
704 |
|> Graph.map_node_yield m (fn Module (d_module_name, nsp_nodes) => |
|
705 |
let |
|
706 |
val (x, nsp_nodes') = map_nsp_yield ms f nsp_nodes |
|
707 |
in (x, Module (d_module_name, nsp_nodes')) end) |
|
708 |
in (x, (nsp, nodes')) end; |
|
709 |
fun map_nsp_fun_yield f (nsp_fun, nsp_typ) = |
|
710 |
let |
|
711 |
val (x, nsp_fun') = f nsp_fun |
|
712 |
in (x, (nsp_fun', nsp_typ)) end; |
|
713 |
fun map_nsp_typ_yield f (nsp_fun, nsp_typ) = |
|
714 |
let |
|
715 |
val (x, nsp_typ') = f nsp_typ |
|
716 |
in (x, (nsp_fun, nsp_typ')) end; |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
717 |
val mk_name_module = Code_Name.mk_name_module reserved_names NONE module_alias program; |
28054 | 718 |
fun mk_name_stmt upper name nsp = |
719 |
let |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
720 |
val (_, base) = Code_Name.dest_name name; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
721 |
val base' = if upper then Code_Name.first_upper base else base; |
28054 | 722 |
val ([base''], nsp') = Name.variants [base'] nsp; |
723 |
in (base'', nsp') end; |
|
724 |
fun add_funs stmts = |
|
725 |
fold_map |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
726 |
(fn (name, Code_Thingol.Fun (_, stmt)) => |
28054 | 727 |
map_nsp_fun_yield (mk_name_stmt false name) #>> |
28350 | 728 |
rpair (name, stmt |> apsnd (filter (snd o snd))) |
28054 | 729 |
| (name, _) => |
730 |
error ("Function block containing illegal statement: " ^ labelled_name name) |
|
731 |
) stmts |
|
732 |
#>> (split_list #> apsnd MLFuns); |
|
733 |
fun add_datatypes stmts = |
|
734 |
fold_map |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
735 |
(fn (name, Code_Thingol.Datatype (_, stmt)) => |
28054 | 736 |
map_nsp_typ_yield (mk_name_stmt false name) #>> rpair (SOME (name, stmt)) |
737 |
| (name, Code_Thingol.Datatypecons _) => |
|
738 |
map_nsp_fun_yield (mk_name_stmt true name) #>> rpair NONE |
|
739 |
| (name, _) => |
|
740 |
error ("Datatype block containing illegal statement: " ^ labelled_name name) |
|
741 |
) stmts |
|
742 |
#>> (split_list #> apsnd (map_filter I |
|
743 |
#> (fn [] => error ("Datatype block without data statement: " |
|
744 |
^ (commas o map (labelled_name o fst)) stmts) |
|
745 |
| stmts => MLDatas stmts))); |
|
746 |
fun add_class stmts = |
|
747 |
fold_map |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
748 |
(fn (name, Code_Thingol.Class (_, stmt)) => |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
749 |
map_nsp_typ_yield (mk_name_stmt false name) #>> rpair (SOME (name, stmt)) |
28054 | 750 |
| (name, Code_Thingol.Classrel _) => |
751 |
map_nsp_fun_yield (mk_name_stmt false name) #>> rpair NONE |
|
752 |
| (name, Code_Thingol.Classparam _) => |
|
753 |
map_nsp_fun_yield (mk_name_stmt false name) #>> rpair NONE |
|
754 |
| (name, _) => |
|
755 |
error ("Class block containing illegal statement: " ^ labelled_name name) |
|
756 |
) stmts |
|
757 |
#>> (split_list #> apsnd (map_filter I |
|
758 |
#> (fn [] => error ("Class block without class statement: " |
|
759 |
^ (commas o map (labelled_name o fst)) stmts) |
|
760 |
| [stmt] => MLClass stmt))); |
|
761 |
fun add_inst [(name, Code_Thingol.Classinst stmt)] = |
|
762 |
map_nsp_fun_yield (mk_name_stmt false name) |
|
763 |
#>> (fn base => ([base], MLClassinst (name, stmt))); |
|
764 |
fun add_stmts ((stmts as (_, Code_Thingol.Fun _)::_)) = |
|
765 |
add_funs stmts |
|
766 |
| add_stmts ((stmts as (_, Code_Thingol.Datatypecons _)::_)) = |
|
767 |
add_datatypes stmts |
|
768 |
| add_stmts ((stmts as (_, Code_Thingol.Datatype _)::_)) = |
|
769 |
add_datatypes stmts |
|
770 |
| add_stmts ((stmts as (_, Code_Thingol.Class _)::_)) = |
|
771 |
add_class stmts |
|
772 |
| add_stmts ((stmts as (_, Code_Thingol.Classrel _)::_)) = |
|
773 |
add_class stmts |
|
774 |
| add_stmts ((stmts as (_, Code_Thingol.Classparam _)::_)) = |
|
775 |
add_class stmts |
|
776 |
| add_stmts ((stmts as [(_, Code_Thingol.Classinst _)])) = |
|
777 |
add_inst stmts |
|
778 |
| add_stmts stmts = error ("Illegal mutual dependencies: " ^ |
|
779 |
(commas o map (labelled_name o fst)) stmts); |
|
780 |
fun add_stmts' stmts nsp_nodes = |
|
781 |
let |
|
782 |
val names as (name :: names') = map fst stmts; |
|
783 |
val deps = |
|
784 |
[] |
|
785 |
|> fold (fold (insert (op =)) o Graph.imm_succs program) names |
|
786 |
|> subtract (op =) names; |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
787 |
val (module_names, _) = (split_list o map Code_Name.dest_name) names; |
28054 | 788 |
val module_name = (the_single o distinct (op =) o map mk_name_module) module_names |
789 |
handle Empty => |
|
790 |
error ("Different namespace prefixes for mutual dependencies:\n" |
|
791 |
^ commas (map labelled_name names) |
|
792 |
^ "\n" |
|
793 |
^ commas module_names); |
|
794 |
val module_name_path = NameSpace.explode module_name; |
|
795 |
fun add_dep name name' = |
|
796 |
let |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
797 |
val module_name' = (mk_name_module o fst o Code_Name.dest_name) name'; |
28054 | 798 |
in if module_name = module_name' then |
799 |
map_node module_name_path (Graph.add_edge (name, name')) |
|
800 |
else let |
|
28705 | 801 |
val (common, (diff1 :: _, diff2 :: _)) = chop_prefix (op =) |
28054 | 802 |
(module_name_path, NameSpace.explode module_name'); |
803 |
in |
|
804 |
map_node common |
|
805 |
(fn node => Graph.add_edge_acyclic (diff1, diff2) node |
|
806 |
handle Graph.CYCLES _ => error ("Dependency " |
|
807 |
^ quote name ^ " -> " ^ quote name' |
|
808 |
^ " would result in module dependency cycle")) |
|
809 |
end end; |
|
810 |
in |
|
811 |
nsp_nodes |
|
812 |
|> map_nsp_yield module_name_path (add_stmts stmts) |
|
813 |
|-> (fn (base' :: bases', stmt') => |
|
814 |
apsnd (map_node module_name_path (Graph.new_node (name, (Stmt (base', stmt'))) |
|
815 |
#> fold2 (fn name' => fn base' => |
|
816 |
Graph.new_node (name', (Dummy base'))) names' bases'))) |
|
817 |
|> apsnd (fold (fn name => fold (add_dep name) deps) names) |
|
818 |
|> apsnd (fold_product (curry (map_node module_name_path o Graph.add_edge)) names names) |
|
819 |
end; |
|
820 |
val (_, nodes) = empty_module |
|
821 |
|> fold add_stmts' (map (AList.make (Graph.get_node program)) |
|
822 |
(rev (Graph.strong_conn program))); |
|
823 |
fun deresolver prefix name = |
|
824 |
let |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
825 |
val module_name = (fst o Code_Name.dest_name) name; |
28054 | 826 |
val module_name' = (NameSpace.explode o mk_name_module) module_name; |
827 |
val (_, (_, remainder)) = chop_prefix (op =) (prefix, module_name'); |
|
828 |
val stmt_name = |
|
829 |
nodes |
|
830 |
|> fold (fn name => fn node => case Graph.get_node node name |
|
831 |
of Module (_, (_, node)) => node) module_name' |
|
832 |
|> (fn node => case Graph.get_node node name of Stmt (stmt_name, _) => stmt_name |
|
833 |
| Dummy stmt_name => stmt_name); |
|
834 |
in |
|
835 |
NameSpace.implode (remainder @ [stmt_name]) |
|
836 |
end handle Graph.UNDEF _ => |
|
837 |
error ("Unknown statement name: " ^ labelled_name name); |
|
838 |
in (deresolver, nodes) end; |
|
839 |
||
840 |
fun serialize_ml target compile pr_module pr_stmt raw_module_name labelled_name reserved_names includes raw_module_alias |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
841 |
_ syntax_tyco syntax_const naming program cs destination = |
28054 | 842 |
let |
843 |
val is_cons = Code_Thingol.is_cons program; |
|
844 |
val stmt_names = Code_Target.stmt_names_of_destination destination; |
|
845 |
val module_name = if null stmt_names then raw_module_name else SOME "Code"; |
|
846 |
val (deresolver, nodes) = ml_node_of_program labelled_name module_name |
|
847 |
reserved_names raw_module_alias program; |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
848 |
val reserved_names = Code_Name.make_vars reserved_names; |
28054 | 849 |
fun pr_node prefix (Dummy _) = |
850 |
NONE |
|
851 |
| pr_node prefix (Stmt (_, stmt)) = if null stmt_names orelse |
|
852 |
(not o null o filter (member (op =) stmt_names) o stmt_names_of) stmt then SOME |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
853 |
(pr_stmt naming labelled_name syntax_tyco syntax_const reserved_names |
28054 | 854 |
(deresolver prefix) is_cons stmt) |
855 |
else NONE |
|
856 |
| pr_node prefix (Module (module_name, (_, nodes))) = |
|
857 |
separate (str "") |
|
858 |
((map_filter (pr_node (prefix @ [module_name]) o Graph.get_node nodes) |
|
859 |
o rev o flat o Graph.strong_conn) nodes) |
|
860 |
|> (if null stmt_names then pr_module module_name else Pretty.chunks) |
|
861 |
|> SOME; |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
862 |
val cs' = (map o try) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
863 |
(deresolver (if is_some module_name then the_list module_name else [])) cs; |
28054 | 864 |
val p = Pretty.chunks (separate (str "") (map snd includes @ (map_filter |
865 |
(pr_node [] o Graph.get_node nodes) o rev o flat o Graph.strong_conn) nodes)); |
|
866 |
in |
|
867 |
Code_Target.mk_serialization target |
|
868 |
(case compile of SOME compile => SOME (compile o Code_Target.code_of_pretty) | NONE => NONE) |
|
869 |
(fn NONE => Code_Target.code_writeln | SOME file => File.write file o Code_Target.code_of_pretty) |
|
870 |
(rpair cs' o Code_Target.code_of_pretty) p destination |
|
871 |
end; |
|
872 |
||
873 |
end; (*local*) |
|
874 |
||
875 |
||
876 |
(** ML (system language) code for evaluation and instrumentalization **) |
|
877 |
||
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
878 |
fun ml_code_of thy = Code_Target.serialize_custom thy (target_SML, |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
879 |
(fn _ => fn [] => serialize_ml target_SML (SOME (K ())) (K Pretty.chunks) pr_sml_stmt (SOME ""), |
28064 | 880 |
literals_sml)); |
28054 | 881 |
|
882 |
||
883 |
(* evaluation *) |
|
884 |
||
885 |
fun eval eval'' term_of reff thy ct args = |
|
886 |
let |
|
28275
8dab53900e8c
ML_Context.evaluate: proper context (for ML environment);
wenzelm
parents:
28064
diff
changeset
|
887 |
val ctxt = ProofContext.init thy; |
28054 | 888 |
val _ = if null (term_frees (term_of ct)) then () else error ("Term " |
889 |
^ quote (Syntax.string_of_term_global thy (term_of ct)) |
|
890 |
^ " to be evaluated contains free variables"); |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
891 |
fun eval' naming program ((vs, ty), t) deps = |
28054 | 892 |
let |
893 |
val _ = if Code_Thingol.contains_dictvar t then |
|
28724 | 894 |
error "Term to be evaluated contains free dictionaries" else (); |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
895 |
val value_name = "Value.VALUE.value" |
28054 | 896 |
val program' = program |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
897 |
|> Graph.new_node (value_name, |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
898 |
Code_Thingol.Fun (Term.dummy_patternN, (([], ty), [(([], t), (Drule.dummy_thm, true))]))) |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
899 |
|> fold (curry Graph.add_edge value_name) deps; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
900 |
val (value_code, [SOME value_name']) = ml_code_of thy naming program' [value_name]; |
28054 | 901 |
val sml_code = "let\n" ^ value_code ^ "\nin " ^ value_name' |
902 |
^ space_implode " " (map (enclose "(" ")") args) ^ " end"; |
|
28275
8dab53900e8c
ML_Context.evaluate: proper context (for ML environment);
wenzelm
parents:
28064
diff
changeset
|
903 |
in ML_Context.evaluate ctxt Output.ml_output false reff sml_code end; |
28724 | 904 |
in eval'' thy (rpair eval') ct end; |
28054 | 905 |
|
906 |
fun eval_term reff = eval Code_Thingol.eval_term I reff; |
|
907 |
||
908 |
||
909 |
(* instrumentalization by antiquotation *) |
|
910 |
||
911 |
local |
|
912 |
||
913 |
structure CodeAntiqData = ProofDataFun |
|
914 |
( |
|
28673
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents:
28663
diff
changeset
|
915 |
type T = string list * (bool * (string * (string * (string * string) list) Lazy.T)); |
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents:
28663
diff
changeset
|
916 |
fun init _ = ([], (true, ("", Lazy.value ("", [])))); |
28054 | 917 |
); |
918 |
||
919 |
val is_first_occ = fst o snd o CodeAntiqData.get; |
|
920 |
||
921 |
fun delayed_code thy consts () = |
|
922 |
let |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
923 |
val (consts', (naming, program)) = Code_Thingol.consts_program thy consts; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
924 |
val (ml_code, consts'') = ml_code_of thy naming program consts'; |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
925 |
val const_tab = map2 (fn const => fn NONE => |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
926 |
error ("Constant " ^ (quote o Code_Unit.string_of_const thy) const |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
927 |
^ "\nhas a user-defined serialization") |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
928 |
| SOME const' => (const, const')) consts consts'' |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
929 |
in (ml_code, const_tab) end; |
28054 | 930 |
|
931 |
fun register_const const ctxt = |
|
932 |
let |
|
933 |
val (consts, (_, (struct_name, _))) = CodeAntiqData.get ctxt; |
|
934 |
val consts' = insert (op =) const consts; |
|
935 |
val (struct_name', ctxt') = if struct_name = "" |
|
936 |
then ML_Antiquote.variant "Code" ctxt |
|
937 |
else (struct_name, ctxt); |
|
28673
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents:
28663
diff
changeset
|
938 |
val acc_code = Lazy.lazy (delayed_code (ProofContext.theory_of ctxt) consts'); |
28054 | 939 |
in CodeAntiqData.put (consts', (false, (struct_name', acc_code))) ctxt' end; |
940 |
||
941 |
fun print_code struct_name is_first const ctxt = |
|
942 |
let |
|
943 |
val (consts, (_, (struct_code_name, acc_code))) = CodeAntiqData.get ctxt; |
|
28673
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents:
28663
diff
changeset
|
944 |
val (raw_ml_code, consts_map) = Lazy.force acc_code; |
28054 | 945 |
val const'' = NameSpace.append (NameSpace.append struct_name struct_code_name) |
946 |
((the o AList.lookup (op =) consts_map) const); |
|
947 |
val ml_code = if is_first then "\nstructure " ^ struct_code_name |
|
948 |
^ " =\nstruct\n\n" ^ raw_ml_code ^ "\nend;\n\n" |
|
949 |
else ""; |
|
950 |
in (ml_code, const'') end; |
|
951 |
||
952 |
in |
|
953 |
||
954 |
fun ml_code_antiq raw_const {struct_name, background} = |
|
955 |
let |
|
956 |
val const = Code_Unit.check_const (ProofContext.theory_of background) raw_const; |
|
957 |
val is_first = is_first_occ background; |
|
958 |
val background' = register_const const background; |
|
959 |
in (print_code struct_name is_first const, background') end; |
|
960 |
||
961 |
end; (*local*) |
|
962 |
||
963 |
||
964 |
(** Isar setup **) |
|
965 |
||
966 |
val _ = ML_Context.add_antiq "code" (fn _ => Args.term >> ml_code_antiq); |
|
967 |
||
968 |
fun isar_seri_sml module_name = |
|
969 |
Code_Target.parse_args (Scan.succeed ()) |
|
28275
8dab53900e8c
ML_Context.evaluate: proper context (for ML environment);
wenzelm
parents:
28064
diff
changeset
|
970 |
#> (fn () => serialize_ml target_SML |
8dab53900e8c
ML_Context.evaluate: proper context (for ML environment);
wenzelm
parents:
28064
diff
changeset
|
971 |
(SOME (use_text ML_Context.name_space (1, "generated code") Output.ml_output false)) |
28054 | 972 |
pr_sml_module pr_sml_stmt module_name); |
973 |
||
974 |
fun isar_seri_ocaml module_name = |
|
975 |
Code_Target.parse_args (Scan.succeed ()) |
|
976 |
#> (fn () => serialize_ml target_OCaml NONE |
|
977 |
pr_ocaml_module pr_ocaml_stmt module_name); |
|
978 |
||
979 |
val setup = |
|
28064 | 980 |
Code_Target.add_target (target_SML, (isar_seri_sml, literals_sml)) |
981 |
#> Code_Target.add_target (target_OCaml, (isar_seri_ocaml, literals_ocaml)) |
|
28054 | 982 |
#> Code_Target.add_syntax_tyco target_SML "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] => |
983 |
brackify_infix (1, R) fxy [ |
|
984 |
pr_typ (INFX (1, X)) ty1, |
|
985 |
str "->", |
|
986 |
pr_typ (INFX (1, R)) ty2 |
|
987 |
])) |
|
988 |
#> Code_Target.add_syntax_tyco target_OCaml "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] => |
|
989 |
brackify_infix (1, R) fxy [ |
|
990 |
pr_typ (INFX (1, X)) ty1, |
|
991 |
str "->", |
|
992 |
pr_typ (INFX (1, R)) ty2 |
|
993 |
])) |
|
994 |
#> fold (Code_Target.add_reserved target_SML) ML_Syntax.reserved_names |
|
995 |
#> fold (Code_Target.add_reserved target_SML) |
|
996 |
["o" (*dictionary projections use it already*), "Fail", "div", "mod" (*standard infixes*)] |
|
997 |
#> fold (Code_Target.add_reserved target_OCaml) [ |
|
998 |
"and", "as", "assert", "begin", "class", |
|
999 |
"constraint", "do", "done", "downto", "else", "end", "exception", |
|
1000 |
"external", "false", "for", "fun", "function", "functor", "if", |
|
1001 |
"in", "include", "inherit", "initializer", "lazy", "let", "match", "method", |
|
1002 |
"module", "mutable", "new", "object", "of", "open", "or", "private", "rec", |
|
1003 |
"sig", "struct", "then", "to", "true", "try", "type", "val", |
|
1004 |
"virtual", "when", "while", "with" |
|
1005 |
] |
|
1006 |
#> fold (Code_Target.add_reserved target_OCaml) ["failwith", "mod"]; |
|
1007 |
||
1008 |
end; (*struct*) |