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