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