author | haftmann |
Mon, 28 May 2012 13:38:07 +0200 | |
changeset 48003 | 1d11af40b106 |
parent 47609 | b3dab1892cda |
child 48072 | ace701efe203 |
permissions | -rw-r--r-- |
37745
6315b6426200
checking generated code for various target languages
haftmann
parents:
37449
diff
changeset
|
1 |
(* Title: Tools/Code/code_ml.ML |
28054 | 2 |
Author: Florian Haftmann, TU Muenchen |
3 |
||
4 |
Serializer for SML and OCaml. |
|
5 |
*) |
|
6 |
||
7 |
signature CODE_ML = |
|
8 |
sig |
|
34028
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
haftmann
parents:
33992
diff
changeset
|
9 |
val target_SML: string |
37745
6315b6426200
checking generated code for various target languages
haftmann
parents:
37449
diff
changeset
|
10 |
val target_OCaml: string |
28054 | 11 |
val setup: theory -> theory |
12 |
end; |
|
13 |
||
14 |
structure Code_ML : CODE_ML = |
|
15 |
struct |
|
16 |
||
17 |
open Basic_Code_Thingol; |
|
18 |
open Code_Printer; |
|
19 |
||
20 |
infixr 5 @@; |
|
21 |
infixr 5 @|; |
|
22 |
||
33992 | 23 |
|
24 |
(** generic **) |
|
25 |
||
28054 | 26 |
val target_SML = "SML"; |
27 |
val target_OCaml = "OCaml"; |
|
28 |
||
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
29 |
datatype ml_binding = |
35228 | 30 |
ML_Function of string * (typscheme * ((iterm list * iterm) * (thm option * bool)) list) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
31 |
| ML_Instance of string * ((class * (string * (vname * sort) list)) |
37445
e372fa3c7239
dropped obscure type argument weakening mapping -- was only a misunderstanding
haftmann
parents:
37439
diff
changeset
|
32 |
* ((class * (string * (string * dict list list))) list |
37449 | 33 |
* (((string * const) * (thm * bool)) list * ((string * const) * (thm * bool)) list))); |
28054 | 34 |
|
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
35 |
datatype ml_stmt = |
33992 | 36 |
ML_Exc of string * (typscheme * int) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
37 |
| ML_Val of ml_binding |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
38 |
| ML_Funs of ml_binding list * string list |
48003
1d11af40b106
dropped sort constraints on datatype specifications
haftmann
parents:
47609
diff
changeset
|
39 |
| ML_Datas of (string * (vname list * ((string * vname list) * itype list) list)) list |
37447
ad3e04f289b6
transitive superclasses were also only a misunderstanding
haftmann
parents:
37446
diff
changeset
|
40 |
| ML_Class of string * (vname * ((class * string) list * (string * itype) list)); |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
41 |
|
33992 | 42 |
fun print_product _ [] = NONE |
43 |
| print_product print [x] = SOME (print x) |
|
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
44 |
| print_product print xs = (SOME o enum " *" "" "") (map print xs); |
28054 | 45 |
|
38922 | 46 |
fun tuplify _ _ [] = NONE |
47 |
| tuplify print fxy [x] = SOME (print fxy x) |
|
48 |
| tuplify print _ xs = SOME (enum "," "(" ")" (map (print NOBR) xs)); |
|
28054 | 49 |
|
33992 | 50 |
|
51 |
(** SML serializer **) |
|
52 |
||
47576 | 53 |
fun print_sml_stmt tyco_syntax const_syntax reserved is_cons deresolve = |
28054 | 54 |
let |
47609 | 55 |
fun print_tyco_expr (tyco, []) = (str o deresolve) tyco |
56 |
| print_tyco_expr (tyco, [ty]) = |
|
33992 | 57 |
concat [print_typ BR ty, (str o deresolve) tyco] |
47609 | 58 |
| print_tyco_expr (tyco, tys) = |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
59 |
concat [enum "," "(" ")" (map (print_typ BR) tys), (str o deresolve) tyco] |
38923 | 60 |
and print_typ fxy (tyco `%% tys) = (case tyco_syntax tyco |
47609 | 61 |
of NONE => print_tyco_expr (tyco, tys) |
62 |
| SOME (_, print) => print print_typ fxy tys) |
|
33992 | 63 |
| print_typ fxy (ITyVar v) = str ("'" ^ v); |
47609 | 64 |
fun print_dicttyp (class, ty) = print_tyco_expr (class, [ty]); |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
65 |
fun print_typscheme_prefix (vs, p) = enum " ->" "" "" |
33992 | 66 |
(map_filter (fn (v, sort) => |
67 |
(print_product (fn class => print_dicttyp (class, ITyVar v)) sort)) vs @| p); |
|
68 |
fun print_typscheme (vs, ty) = print_typscheme_prefix (vs, print_typ NOBR ty); |
|
69 |
fun print_dicttypscheme (vs, class_ty) = print_typscheme_prefix (vs, print_dicttyp class_ty); |
|
41100
6c0940392fb4
dictionary constants must permit explicit weakening of classes;
haftmann
parents:
40627
diff
changeset
|
70 |
fun print_classrels fxy [] ps = brackify fxy ps |
6c0940392fb4
dictionary constants must permit explicit weakening of classes;
haftmann
parents:
40627
diff
changeset
|
71 |
| print_classrels fxy [classrel] ps = brackify fxy [(str o deresolve) classrel, brackify BR ps] |
6c0940392fb4
dictionary constants must permit explicit weakening of classes;
haftmann
parents:
40627
diff
changeset
|
72 |
| print_classrels fxy classrels ps = |
6c0940392fb4
dictionary constants must permit explicit weakening of classes;
haftmann
parents:
40627
diff
changeset
|
73 |
brackify fxy [enum " o" "(" ")" (map (str o deresolve) classrels), brackify BR ps] |
41118
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
74 |
fun print_dict is_pseudo_fun fxy (Dict (classrels, x)) = |
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
75 |
print_classrels fxy classrels (print_plain_dict is_pseudo_fun fxy x) |
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
76 |
and print_plain_dict is_pseudo_fun fxy (Dict_Const (inst, dss)) = |
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
77 |
((str o deresolve) inst :: |
33992 | 78 |
(if is_pseudo_fun inst then [str "()"] |
79 |
else map_filter (print_dicts is_pseudo_fun BR) dss)) |
|
41118
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
80 |
| print_plain_dict is_pseudo_fun fxy (Dict_Var (v, (i, k))) = |
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
81 |
[str (if k = 1 then first_upper v ^ "_" |
41100
6c0940392fb4
dictionary constants must permit explicit weakening of classes;
haftmann
parents:
40627
diff
changeset
|
82 |
else first_upper v ^ string_of_int (i+1) ^ "_")] |
38922 | 83 |
and print_dicts is_pseudo_fun = tuplify (print_dict is_pseudo_fun); |
33992 | 84 |
val print_dict_args = map_filter (fn (v, sort) => print_dicts (K false) BR |
41118
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
85 |
(map_index (fn (i, _) => Dict ([], Dict_Var (v, (i, length sort)))) sort)); |
35228 | 86 |
fun print_term is_pseudo_fun some_thm vars fxy (IConst c) = |
87 |
print_app is_pseudo_fun some_thm vars fxy (c, []) |
|
88 |
| print_term is_pseudo_fun some_thm vars fxy (IVar NONE) = |
|
31889 | 89 |
str "_" |
35228 | 90 |
| print_term is_pseudo_fun some_thm vars fxy (IVar (SOME v)) = |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
91 |
str (lookup_var vars v) |
35228 | 92 |
| print_term is_pseudo_fun some_thm vars fxy (t as t1 `$ t2) = |
28054 | 93 |
(case Code_Thingol.unfold_const_app t |
35228 | 94 |
of SOME c_ts => print_app is_pseudo_fun some_thm vars fxy c_ts |
95 |
| NONE => brackify fxy [print_term is_pseudo_fun some_thm vars NOBR t1, |
|
96 |
print_term is_pseudo_fun some_thm vars BR t2]) |
|
97 |
| print_term is_pseudo_fun some_thm vars fxy (t as _ `|=> _) = |
|
28054 | 98 |
let |
31874 | 99 |
val (binds, t') = Code_Thingol.unfold_pat_abs t; |
33992 | 100 |
fun print_abs (pat, ty) = |
35228 | 101 |
print_bind is_pseudo_fun some_thm NOBR pat |
28054 | 102 |
#>> (fn p => concat [str "fn", p, str "=>"]); |
33992 | 103 |
val (ps, vars') = fold_map print_abs binds vars; |
35228 | 104 |
in brackets (ps @ [print_term is_pseudo_fun some_thm vars' NOBR t']) end |
105 |
| print_term is_pseudo_fun some_thm vars fxy (ICase (cases as (_, t0))) = |
|
28054 | 106 |
(case Code_Thingol.unfold_const_app t0 |
38923 | 107 |
of SOME (c_ts as ((c, _), _)) => if is_none (const_syntax c) |
35228 | 108 |
then print_case is_pseudo_fun some_thm vars fxy cases |
109 |
else print_app is_pseudo_fun some_thm vars fxy c_ts |
|
110 |
| NONE => print_case is_pseudo_fun some_thm vars fxy cases) |
|
45009
99e1965f9c21
syntactic improvements and tuning names in the code generator due to Florian's code review
bulwahn
parents:
44789
diff
changeset
|
111 |
and print_app_expr is_pseudo_fun some_thm vars (app as ((c, (((_, iss), (arg_tys, _)), _)), ts)) = |
29175 | 112 |
if is_cons c then |
45009
99e1965f9c21
syntactic improvements and tuning names in the code generator due to Florian's code review
bulwahn
parents:
44789
diff
changeset
|
113 |
let val k = length arg_tys in |
33992 | 114 |
if k < 2 orelse length ts = k |
115 |
then (str o deresolve) c |
|
38922 | 116 |
:: the_list (tuplify (print_term is_pseudo_fun some_thm vars) BR ts) |
35228 | 117 |
else [print_term is_pseudo_fun some_thm vars BR (Code_Thingol.eta_expand k app)] |
33992 | 118 |
end |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
119 |
else if is_pseudo_fun c |
29175 | 120 |
then (str o deresolve) c @@ str "()" |
33992 | 121 |
else (str o deresolve) c :: map_filter (print_dicts is_pseudo_fun BR) iss |
35228 | 122 |
@ map (print_term is_pseudo_fun some_thm vars BR) ts |
123 |
and print_app is_pseudo_fun some_thm vars = gen_print_app (print_app_expr is_pseudo_fun) |
|
38923 | 124 |
(print_term is_pseudo_fun) const_syntax some_thm vars |
33992 | 125 |
and print_bind is_pseudo_fun = gen_print_bind (print_term is_pseudo_fun) |
35228 | 126 |
and print_case is_pseudo_fun some_thm vars fxy (cases as ((_, [_]), _)) = |
28054 | 127 |
let |
29952
9aed85067721
unified variable names in case expressions; no exponential fork in translation of case expressions
haftmann
parents:
29264
diff
changeset
|
128 |
val (binds, body) = Code_Thingol.unfold_let (ICase cases); |
47609 | 129 |
fun print_match ((pat, _), t) vars = |
28054 | 130 |
vars |
35228 | 131 |
|> print_bind is_pseudo_fun some_thm NOBR pat |
33992 | 132 |
|>> (fn p => semicolon [str "val", p, str "=", |
35228 | 133 |
print_term is_pseudo_fun some_thm vars NOBR t]) |
33992 | 134 |
val (ps, vars') = fold_map print_match binds vars; |
28054 | 135 |
in |
136 |
Pretty.chunks [ |
|
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
137 |
Pretty.block [str "let", Pretty.fbrk, Pretty.chunks ps], |
35228 | 138 |
Pretty.block [str "in", Pretty.fbrk, print_term is_pseudo_fun some_thm vars' NOBR body], |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
139 |
str "end" |
28054 | 140 |
] |
141 |
end |
|
35228 | 142 |
| print_case is_pseudo_fun some_thm vars fxy (((t, ty), clause :: clauses), _) = |
28054 | 143 |
let |
33992 | 144 |
fun print_select delim (pat, body) = |
28054 | 145 |
let |
35228 | 146 |
val (p, vars') = print_bind is_pseudo_fun some_thm NOBR pat vars; |
28054 | 147 |
in |
35228 | 148 |
concat [str delim, p, str "=>", print_term is_pseudo_fun some_thm vars' NOBR body] |
28054 | 149 |
end; |
150 |
in |
|
31665 | 151 |
brackets ( |
28054 | 152 |
str "case" |
35228 | 153 |
:: print_term is_pseudo_fun some_thm vars NOBR t |
33992 | 154 |
:: print_select "of" clause |
155 |
:: map (print_select "|") clauses |
|
28054 | 156 |
) |
157 |
end |
|
35228 | 158 |
| print_case is_pseudo_fun some_thm vars fxy ((_, []), _) = |
31121 | 159 |
(concat o map str) ["raise", "Fail", "\"empty case\""]; |
33992 | 160 |
fun print_val_decl print_typscheme (name, typscheme) = concat |
161 |
[str "val", str (deresolve name), str ":", print_typscheme typscheme]; |
|
162 |
fun print_datatype_decl definer (tyco, (vs, cos)) = |
|
163 |
let |
|
37449 | 164 |
fun print_co ((co, _), []) = str (deresolve co) |
165 |
| print_co ((co, _), tys) = concat [str (deresolve co), str "of", |
|
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
166 |
enum " *" "" "" (map (print_typ (INFX (2, X))) tys)]; |
33992 | 167 |
in |
168 |
concat ( |
|
169 |
str definer |
|
48003
1d11af40b106
dropped sort constraints on datatype specifications
haftmann
parents:
47609
diff
changeset
|
170 |
:: print_tyco_expr (tyco, map ITyVar vs) |
33992 | 171 |
:: str "=" |
172 |
:: separate (str "|") (map print_co cos) |
|
173 |
) |
|
174 |
end; |
|
175 |
fun print_def is_pseudo_fun needs_typ definer |
|
176 |
(ML_Function (name, (vs_ty as (vs, ty), eq :: eqs))) = |
|
29189 | 177 |
let |
35228 | 178 |
fun print_eqn definer ((ts, t), (some_thm, _)) = |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
179 |
let |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
180 |
val consts = fold Code_Thingol.add_constnames (t :: ts) []; |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
181 |
val vars = reserved |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
182 |
|> intro_base_names |
38923 | 183 |
(is_none o const_syntax) deresolve consts |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
184 |
|> intro_vars ((fold o Code_Thingol.fold_varnames) |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
185 |
(insert (op =)) ts []); |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
186 |
val prolog = if needs_typ then |
33992 | 187 |
concat [str definer, (str o deresolve) name, str ":", print_typ NOBR ty] |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
188 |
else (concat o map str) [definer, deresolve name]; |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
189 |
in |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
190 |
concat ( |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
191 |
prolog |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
192 |
:: (if is_pseudo_fun name then [str "()"] |
33992 | 193 |
else print_dict_args vs |
35228 | 194 |
@ map (print_term is_pseudo_fun some_thm vars BR) ts) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
195 |
@ str "=" |
35228 | 196 |
@@ print_term is_pseudo_fun some_thm vars NOBR t |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
197 |
) |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
198 |
end |
33992 | 199 |
val shift = if null eqs then I else |
200 |
map (Pretty.block o single o Pretty.block o single); |
|
201 |
in pair |
|
202 |
(print_val_decl print_typscheme (name, vs_ty)) |
|
203 |
((Pretty.block o Pretty.fbreaks o shift) ( |
|
204 |
print_eqn definer eq |
|
205 |
:: map (print_eqn "|") eqs |
|
206 |
)) |
|
29189 | 207 |
end |
33992 | 208 |
| print_def is_pseudo_fun _ definer |
37449 | 209 |
(ML_Instance (inst, ((class, (tyco, vs)), (super_instances, (classparam_instances, _))))) = |
29189 | 210 |
let |
41100
6c0940392fb4
dictionary constants must permit explicit weakening of classes;
haftmann
parents:
40627
diff
changeset
|
211 |
fun print_super_instance (_, (classrel, x)) = |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
212 |
concat [ |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
213 |
(str o Long_Name.base_name o deresolve) classrel, |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
214 |
str "=", |
41118
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
215 |
print_dict is_pseudo_fun NOBR (Dict ([], Dict_Const x)) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
216 |
]; |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
217 |
fun print_classparam_instance ((classparam, const), (thm, _)) = |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
218 |
concat [ |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
219 |
(str o Long_Name.base_name o deresolve) classparam, |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
220 |
str "=", |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
221 |
print_app (K false) (SOME thm) reserved NOBR (const, []) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
222 |
]; |
33992 | 223 |
in pair |
224 |
(print_val_decl print_dicttypscheme |
|
225 |
(inst, (vs, (class, tyco `%% map (ITyVar o fst) vs)))) |
|
226 |
(concat ( |
|
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
227 |
str definer |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
228 |
:: (str o deresolve) inst |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
229 |
:: (if is_pseudo_fun inst then [str "()"] |
33992 | 230 |
else print_dict_args vs) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
231 |
@ str "=" |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
232 |
:: enum "," "{" "}" |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
233 |
(map print_super_instance super_instances |
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
234 |
@ map print_classparam_instance classparam_instances) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
235 |
:: str ":" |
47609 | 236 |
@@ print_tyco_expr (class, [tyco `%% map (ITyVar o fst) vs]) |
33992 | 237 |
)) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
238 |
end; |
33992 | 239 |
fun print_stmt (ML_Exc (name, (vs_ty, n))) = pair |
240 |
[print_val_decl print_typscheme (name, vs_ty)] |
|
241 |
((semicolon o map str) ( |
|
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
242 |
(if n = 0 then "val" else "fun") |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
243 |
:: deresolve name |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
244 |
:: replicate n "_" |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
245 |
@ "=" |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
246 |
:: "raise" |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
247 |
:: "Fail" |
33992 | 248 |
@@ (ML_Syntax.print_string o Long_Name.base_name o Long_Name.qualifier) name |
249 |
)) |
|
250 |
| print_stmt (ML_Val binding) = |
|
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
251 |
let |
33992 | 252 |
val (sig_p, p) = print_def (K false) true "val" binding |
253 |
in pair |
|
254 |
[sig_p] |
|
255 |
(semicolon [p]) |
|
256 |
end |
|
257 |
| print_stmt (ML_Funs (binding :: bindings, pseudo_funs)) = |
|
258 |
let |
|
259 |
val print_def' = print_def (member (op =) pseudo_funs) false; |
|
260 |
fun print_pseudo_fun name = concat [ |
|
29189 | 261 |
str "val", |
262 |
(str o deresolve) name, |
|
263 |
str "=", |
|
264 |
(str o deresolve) name, |
|
265 |
str "();" |
|
266 |
]; |
|
33992 | 267 |
val (sig_ps, (ps, p)) = (apsnd split_last o split_list) |
268 |
(print_def' "fun" binding :: map (print_def' "and") bindings); |
|
269 |
val pseudo_ps = map print_pseudo_fun pseudo_funs; |
|
270 |
in pair |
|
271 |
sig_ps |
|
272 |
(Pretty.chunks (ps @ semicolon [p] :: pseudo_ps)) |
|
273 |
end |
|
274 |
| print_stmt (ML_Datas [(tyco, (vs, []))]) = |
|
275 |
let |
|
48003
1d11af40b106
dropped sort constraints on datatype specifications
haftmann
parents:
47609
diff
changeset
|
276 |
val ty_p = print_tyco_expr (tyco, map ITyVar vs); |
33992 | 277 |
in |
278 |
pair |
|
279 |
[concat [str "type", ty_p]] |
|
280 |
(concat [str "datatype", ty_p, str "=", str "EMPTY__"]) |
|
281 |
end |
|
282 |
| print_stmt (ML_Datas (data :: datas)) = |
|
28054 | 283 |
let |
33992 | 284 |
val sig_ps = print_datatype_decl "datatype" data |
285 |
:: map (print_datatype_decl "and") datas; |
|
286 |
val (ps, p) = split_last sig_ps; |
|
287 |
in pair |
|
288 |
sig_ps |
|
289 |
(Pretty.chunks (ps @| semicolon [p])) |
|
290 |
end |
|
37447
ad3e04f289b6
transitive superclasses were also only a misunderstanding
haftmann
parents:
37446
diff
changeset
|
291 |
| print_stmt (ML_Class (class, (v, (super_classes, classparams)))) = |
28054 | 292 |
let |
33992 | 293 |
fun print_field s p = concat [str s, str ":", p]; |
294 |
fun print_proj s p = semicolon |
|
295 |
(map str ["val", s, "=", "#" ^ s, ":"] @| p); |
|
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
296 |
fun print_super_class_decl (super_class, classrel) = |
33992 | 297 |
print_val_decl print_dicttypscheme |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
298 |
(classrel, ([(v, [class])], (super_class, ITyVar v))); |
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
299 |
fun print_super_class_field (super_class, classrel) = |
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
300 |
print_field (deresolve classrel) (print_dicttyp (super_class, ITyVar v)); |
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
301 |
fun print_super_class_proj (super_class, classrel) = |
33992 | 302 |
print_proj (deresolve classrel) |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
303 |
(print_dicttypscheme ([(v, [class])], (super_class, ITyVar v))); |
33992 | 304 |
fun print_classparam_decl (classparam, ty) = |
305 |
print_val_decl print_typscheme |
|
306 |
(classparam, ([(v, [class])], ty)); |
|
307 |
fun print_classparam_field (classparam, ty) = |
|
308 |
print_field (deresolve classparam) (print_typ NOBR ty); |
|
309 |
fun print_classparam_proj (classparam, ty) = |
|
310 |
print_proj (deresolve classparam) |
|
311 |
(print_typscheme ([(v, [class])], ty)); |
|
312 |
in pair |
|
313 |
(concat [str "type", print_dicttyp (class, ITyVar v)] |
|
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
314 |
:: map print_super_class_decl super_classes |
33992 | 315 |
@ map print_classparam_decl classparams) |
316 |
(Pretty.chunks ( |
|
28054 | 317 |
concat [ |
318 |
str ("type '" ^ v), |
|
319 |
(str o deresolve) class, |
|
320 |
str "=", |
|
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
321 |
enum "," "{" "};" ( |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
322 |
map print_super_class_field super_classes |
33992 | 323 |
@ map print_classparam_field classparams |
28054 | 324 |
) |
325 |
] |
|
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
326 |
:: map print_super_class_proj super_classes |
33992 | 327 |
@ map print_classparam_proj classparams |
328 |
)) |
|
28054 | 329 |
end; |
33992 | 330 |
in print_stmt end; |
28054 | 331 |
|
38933
bd77e092f67c
avoid strange special treatment of empty module names
haftmann
parents:
38928
diff
changeset
|
332 |
fun print_sml_module name some_decls body = |
bd77e092f67c
avoid strange special treatment of empty module names
haftmann
parents:
38928
diff
changeset
|
333 |
Pretty.chunks2 ( |
33992 | 334 |
Pretty.chunks ( |
335 |
str ("structure " ^ name ^ (if is_some some_decls then " : sig" else " =")) |
|
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
336 |
:: (the_list o Option.map (indent 2 o Pretty.chunks)) some_decls |
33992 | 337 |
@| (if is_some some_decls then str "end = struct" else str "struct") |
338 |
) |
|
339 |
:: body |
|
340 |
@| str ("end; (*struct " ^ name ^ "*)") |
|
28054 | 341 |
); |
342 |
||
28064 | 343 |
val literals_sml = Literals { |
344 |
literal_char = prefix "#" o quote o ML_Syntax.print_char, |
|
345 |
literal_string = quote o translate_string ML_Syntax.print_char, |
|
34944
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
346 |
literal_numeral = fn k => "(" ^ string_of_int k ^ " : IntInf.int)", |
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
347 |
literal_positive_numeral = fn k => "(" ^ string_of_int k ^ " : IntInf.int)", |
37958
9728342bcd56
another refinement chapter in the neverending numeral story
haftmann
parents:
37822
diff
changeset
|
348 |
literal_alternative_numeral = fn k => "(" ^ string_of_int k ^ " : IntInf.int)", |
34944
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
349 |
literal_naive_numeral = string_of_int, |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
350 |
literal_list = enum "," "[" "]", |
28064 | 351 |
infix_cons = (7, "::") |
352 |
}; |
|
353 |
||
28054 | 354 |
|
355 |
(** OCaml serializer **) |
|
356 |
||
47576 | 357 |
fun print_ocaml_stmt tyco_syntax const_syntax reserved is_cons deresolve = |
28054 | 358 |
let |
47609 | 359 |
fun print_tyco_expr (tyco, []) = (str o deresolve) tyco |
360 |
| print_tyco_expr (tyco, [ty]) = |
|
33992 | 361 |
concat [print_typ BR ty, (str o deresolve) tyco] |
47609 | 362 |
| print_tyco_expr (tyco, tys) = |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
363 |
concat [enum "," "(" ")" (map (print_typ BR) tys), (str o deresolve) tyco] |
38923 | 364 |
and print_typ fxy (tyco `%% tys) = (case tyco_syntax tyco |
47609 | 365 |
of NONE => print_tyco_expr (tyco, tys) |
47576 | 366 |
| SOME (_, print) => print print_typ fxy tys) |
33992 | 367 |
| print_typ fxy (ITyVar v) = str ("'" ^ v); |
47609 | 368 |
fun print_dicttyp (class, ty) = print_tyco_expr (class, [ty]); |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
369 |
fun print_typscheme_prefix (vs, p) = enum " ->" "" "" |
33992 | 370 |
(map_filter (fn (v, sort) => |
371 |
(print_product (fn class => print_dicttyp (class, ITyVar v)) sort)) vs @| p); |
|
372 |
fun print_typscheme (vs, ty) = print_typscheme_prefix (vs, print_typ NOBR ty); |
|
373 |
fun print_dicttypscheme (vs, class_ty) = print_typscheme_prefix (vs, print_dicttyp class_ty); |
|
41100
6c0940392fb4
dictionary constants must permit explicit weakening of classes;
haftmann
parents:
40627
diff
changeset
|
374 |
val print_classrels = |
6c0940392fb4
dictionary constants must permit explicit weakening of classes;
haftmann
parents:
40627
diff
changeset
|
375 |
fold_rev (fn classrel => fn p => Pretty.block [p, str ".", (str o deresolve) classrel]) |
41118
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
376 |
fun print_dict is_pseudo_fun fxy (Dict (classrels, x)) = |
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
377 |
print_plain_dict is_pseudo_fun fxy x |
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
378 |
|> print_classrels classrels |
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
379 |
and print_plain_dict is_pseudo_fun fxy (Dict_Const (inst, dss)) = |
41100
6c0940392fb4
dictionary constants must permit explicit weakening of classes;
haftmann
parents:
40627
diff
changeset
|
380 |
brackify BR ((str o deresolve) inst :: |
33992 | 381 |
(if is_pseudo_fun inst then [str "()"] |
382 |
else map_filter (print_dicts is_pseudo_fun BR) dss)) |
|
41118
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
383 |
| print_plain_dict is_pseudo_fun fxy (Dict_Var (v, (i, k))) = |
33992 | 384 |
str (if k = 1 then "_" ^ first_upper v |
385 |
else "_" ^ first_upper v ^ string_of_int (i+1)) |
|
38922 | 386 |
and print_dicts is_pseudo_fun = tuplify (print_dict is_pseudo_fun); |
33992 | 387 |
val print_dict_args = map_filter (fn (v, sort) => print_dicts (K false) BR |
41118
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
388 |
(map_index (fn (i, _) => Dict ([], Dict_Var (v, (i, length sort)))) sort)); |
35228 | 389 |
fun print_term is_pseudo_fun some_thm vars fxy (IConst c) = |
390 |
print_app is_pseudo_fun some_thm vars fxy (c, []) |
|
391 |
| print_term is_pseudo_fun some_thm vars fxy (IVar NONE) = |
|
31889 | 392 |
str "_" |
35228 | 393 |
| print_term is_pseudo_fun some_thm vars fxy (IVar (SOME v)) = |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
394 |
str (lookup_var vars v) |
35228 | 395 |
| print_term is_pseudo_fun some_thm vars fxy (t as t1 `$ t2) = |
28054 | 396 |
(case Code_Thingol.unfold_const_app t |
35228 | 397 |
of SOME c_ts => print_app is_pseudo_fun some_thm vars fxy c_ts |
398 |
| NONE => brackify fxy [print_term is_pseudo_fun some_thm vars NOBR t1, |
|
399 |
print_term is_pseudo_fun some_thm vars BR t2]) |
|
400 |
| print_term is_pseudo_fun some_thm vars fxy (t as _ `|=> _) = |
|
28054 | 401 |
let |
31874 | 402 |
val (binds, t') = Code_Thingol.unfold_pat_abs t; |
35228 | 403 |
val (ps, vars') = fold_map (print_bind is_pseudo_fun some_thm BR o fst) binds vars; |
404 |
in brackets (str "fun" :: ps @ str "->" @@ print_term is_pseudo_fun some_thm vars' NOBR t') end |
|
405 |
| print_term is_pseudo_fun some_thm vars fxy (ICase (cases as (_, t0))) = |
|
33992 | 406 |
(case Code_Thingol.unfold_const_app t0 |
38923 | 407 |
of SOME (c_ts as ((c, _), _)) => if is_none (const_syntax c) |
35228 | 408 |
then print_case is_pseudo_fun some_thm vars fxy cases |
409 |
else print_app is_pseudo_fun some_thm vars fxy c_ts |
|
410 |
| NONE => print_case is_pseudo_fun some_thm vars fxy cases) |
|
44789
5a062c23c7db
adding the body type as well to the code generation for constants as it is required for type annotations of constants
bulwahn
parents:
44788
diff
changeset
|
411 |
and print_app_expr is_pseudo_fun some_thm vars (app as ((c, (((_, iss), (tys, _)), _)), ts)) = |
28054 | 412 |
if is_cons c then |
33992 | 413 |
let val k = length tys in |
414 |
if length ts = k |
|
415 |
then (str o deresolve) c |
|
38922 | 416 |
:: the_list (tuplify (print_term is_pseudo_fun some_thm vars) BR ts) |
35228 | 417 |
else [print_term is_pseudo_fun some_thm vars BR (Code_Thingol.eta_expand k app)] |
33992 | 418 |
end |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
419 |
else if is_pseudo_fun c |
29175 | 420 |
then (str o deresolve) c @@ str "()" |
33992 | 421 |
else (str o deresolve) c :: map_filter (print_dicts is_pseudo_fun BR) iss |
35228 | 422 |
@ map (print_term is_pseudo_fun some_thm vars BR) ts |
423 |
and print_app is_pseudo_fun some_thm vars = gen_print_app (print_app_expr is_pseudo_fun) |
|
38923 | 424 |
(print_term is_pseudo_fun) const_syntax some_thm vars |
33992 | 425 |
and print_bind is_pseudo_fun = gen_print_bind (print_term is_pseudo_fun) |
35228 | 426 |
and print_case is_pseudo_fun some_thm vars fxy (cases as ((_, [_]), _)) = |
28054 | 427 |
let |
29952
9aed85067721
unified variable names in case expressions; no exponential fork in translation of case expressions
haftmann
parents:
29264
diff
changeset
|
428 |
val (binds, body) = Code_Thingol.unfold_let (ICase cases); |
47576 | 429 |
fun print_let ((pat, _), t) vars = |
28054 | 430 |
vars |
35228 | 431 |
|> print_bind is_pseudo_fun some_thm NOBR pat |
28054 | 432 |
|>> (fn p => concat |
35228 | 433 |
[str "let", p, str "=", print_term is_pseudo_fun some_thm vars NOBR t, str "in"]) |
33992 | 434 |
val (ps, vars') = fold_map print_let binds vars; |
31665 | 435 |
in |
436 |
brackify_block fxy (Pretty.chunks ps) [] |
|
35228 | 437 |
(print_term is_pseudo_fun some_thm vars' NOBR body) |
31665 | 438 |
end |
35228 | 439 |
| print_case is_pseudo_fun some_thm vars fxy (((t, ty), clause :: clauses), _) = |
28054 | 440 |
let |
33992 | 441 |
fun print_select delim (pat, body) = |
28054 | 442 |
let |
35228 | 443 |
val (p, vars') = print_bind is_pseudo_fun some_thm NOBR pat vars; |
444 |
in concat [str delim, p, str "->", print_term is_pseudo_fun some_thm vars' NOBR body] end; |
|
28054 | 445 |
in |
31665 | 446 |
brackets ( |
28054 | 447 |
str "match" |
35228 | 448 |
:: print_term is_pseudo_fun some_thm vars NOBR t |
33992 | 449 |
:: print_select "with" clause |
450 |
:: map (print_select "|") clauses |
|
28054 | 451 |
) |
452 |
end |
|
35228 | 453 |
| print_case is_pseudo_fun some_thm vars fxy ((_, []), _) = |
31121 | 454 |
(concat o map str) ["failwith", "\"empty case\""]; |
33992 | 455 |
fun print_val_decl print_typscheme (name, typscheme) = concat |
456 |
[str "val", str (deresolve name), str ":", print_typscheme typscheme]; |
|
457 |
fun print_datatype_decl definer (tyco, (vs, cos)) = |
|
458 |
let |
|
37449 | 459 |
fun print_co ((co, _), []) = str (deresolve co) |
460 |
| print_co ((co, _), tys) = concat [str (deresolve co), str "of", |
|
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
461 |
enum " *" "" "" (map (print_typ (INFX (2, X))) tys)]; |
33992 | 462 |
in |
463 |
concat ( |
|
464 |
str definer |
|
48003
1d11af40b106
dropped sort constraints on datatype specifications
haftmann
parents:
47609
diff
changeset
|
465 |
:: print_tyco_expr (tyco, map ITyVar vs) |
33992 | 466 |
:: str "=" |
467 |
:: separate (str "|") (map print_co cos) |
|
468 |
) |
|
469 |
end; |
|
470 |
fun print_def is_pseudo_fun needs_typ definer |
|
471 |
(ML_Function (name, (vs_ty as (vs, ty), eqs))) = |
|
28054 | 472 |
let |
35228 | 473 |
fun print_eqn ((ts, t), (some_thm, _)) = |
28054 | 474 |
let |
32913 | 475 |
val consts = fold Code_Thingol.add_constnames (t :: ts) []; |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
476 |
val vars = reserved |
42599
1a82b0400b2a
improving naming of fresh variables in OCaml serializer
bulwahn
parents:
41952
diff
changeset
|
477 |
|> intro_base_names |
1a82b0400b2a
improving naming of fresh variables in OCaml serializer
bulwahn
parents:
41952
diff
changeset
|
478 |
(is_none o const_syntax) deresolve consts |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
479 |
|> intro_vars ((fold o Code_Thingol.fold_varnames) |
28054 | 480 |
(insert (op =)) ts []); |
481 |
in concat [ |
|
38778 | 482 |
(Pretty.block o commas) |
35228 | 483 |
(map (print_term is_pseudo_fun some_thm vars NOBR) ts), |
28054 | 484 |
str "->", |
35228 | 485 |
print_term is_pseudo_fun some_thm vars NOBR t |
28054 | 486 |
] end; |
35228 | 487 |
fun print_eqns is_pseudo [((ts, t), (some_thm, _))] = |
28054 | 488 |
let |
32913 | 489 |
val consts = fold Code_Thingol.add_constnames (t :: ts) []; |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
490 |
val vars = reserved |
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
491 |
|> intro_base_names |
38923 | 492 |
(is_none o const_syntax) deresolve consts |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
493 |
|> intro_vars ((fold o Code_Thingol.fold_varnames) |
28054 | 494 |
(insert (op =)) ts []); |
495 |
in |
|
496 |
concat ( |
|
29189 | 497 |
(if is_pseudo then [str "()"] |
35228 | 498 |
else map (print_term is_pseudo_fun some_thm vars BR) ts) |
28054 | 499 |
@ str "=" |
35228 | 500 |
@@ print_term is_pseudo_fun some_thm vars NOBR t |
28054 | 501 |
) |
502 |
end |
|
33992 | 503 |
| print_eqns _ ((eq as (([_], _), _)) :: eqs) = |
28054 | 504 |
Pretty.block ( |
505 |
str "=" |
|
506 |
:: Pretty.brk 1 |
|
507 |
:: str "function" |
|
508 |
:: Pretty.brk 1 |
|
33992 | 509 |
:: print_eqn eq |
28054 | 510 |
:: maps (append [Pretty.fbrk, str "|", Pretty.brk 1] |
33992 | 511 |
o single o print_eqn) eqs |
28054 | 512 |
) |
33992 | 513 |
| print_eqns _ (eqs as eq :: eqs') = |
28054 | 514 |
let |
32913 | 515 |
val consts = fold Code_Thingol.add_constnames (map (snd o fst) eqs) []; |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
516 |
val vars = reserved |
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
517 |
|> intro_base_names |
38923 | 518 |
(is_none o const_syntax) deresolve consts; |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
519 |
val dummy_parms = (map str o aux_params vars o map (fst o fst)) eqs; |
28054 | 520 |
in |
521 |
Pretty.block ( |
|
522 |
Pretty.breaks dummy_parms |
|
523 |
@ Pretty.brk 1 |
|
524 |
:: str "=" |
|
525 |
:: Pretty.brk 1 |
|
526 |
:: str "match" |
|
527 |
:: Pretty.brk 1 |
|
38778 | 528 |
:: (Pretty.block o commas) dummy_parms |
28054 | 529 |
:: Pretty.brk 1 |
530 |
:: str "with" |
|
531 |
:: Pretty.brk 1 |
|
33992 | 532 |
:: print_eqn eq |
28054 | 533 |
:: maps (append [Pretty.fbrk, str "|", Pretty.brk 1] |
33992 | 534 |
o single o print_eqn) eqs' |
28054 | 535 |
) |
536 |
end; |
|
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
537 |
val prolog = if needs_typ then |
33992 | 538 |
concat [str definer, (str o deresolve) name, str ":", print_typ NOBR ty] |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
539 |
else (concat o map str) [definer, deresolve name]; |
33992 | 540 |
in pair |
541 |
(print_val_decl print_typscheme (name, vs_ty)) |
|
542 |
(concat ( |
|
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
543 |
prolog |
33992 | 544 |
:: print_dict_args vs |
545 |
@| print_eqns (is_pseudo_fun name) eqs |
|
546 |
)) |
|
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
547 |
end |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
548 |
| print_def is_pseudo_fun _ definer |
37449 | 549 |
(ML_Instance (inst, ((class, (tyco, vs)), (super_instances, (classparam_instances, _))))) = |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
550 |
let |
41100
6c0940392fb4
dictionary constants must permit explicit weakening of classes;
haftmann
parents:
40627
diff
changeset
|
551 |
fun print_super_instance (_, (classrel, x)) = |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
552 |
concat [ |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
553 |
(str o deresolve) classrel, |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
554 |
str "=", |
41118
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
555 |
print_dict is_pseudo_fun NOBR (Dict ([], Dict_Const x)) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
556 |
]; |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
557 |
fun print_classparam_instance ((classparam, const), (thm, _)) = |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
558 |
concat [ |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
559 |
(str o deresolve) classparam, |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
560 |
str "=", |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
561 |
print_app (K false) (SOME thm) reserved NOBR (const, []) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
562 |
]; |
33992 | 563 |
in pair |
564 |
(print_val_decl print_dicttypscheme |
|
565 |
(inst, (vs, (class, tyco `%% map (ITyVar o fst) vs)))) |
|
566 |
(concat ( |
|
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
567 |
str definer |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
568 |
:: (str o deresolve) inst |
43343
e83695ea0e0a
resolving an issue with class instances that are pseudo functions in the OCaml code serializer
bulwahn
parents:
42599
diff
changeset
|
569 |
:: (if is_pseudo_fun inst then [str "()"] |
e83695ea0e0a
resolving an issue with class instances that are pseudo functions in the OCaml code serializer
bulwahn
parents:
42599
diff
changeset
|
570 |
else print_dict_args vs) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
571 |
@ str "=" |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
572 |
@@ brackets [ |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
573 |
enum_default "()" ";" "{" "}" (map print_super_instance super_instances |
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
574 |
@ map print_classparam_instance classparam_instances), |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
575 |
str ":", |
47609 | 576 |
print_tyco_expr (class, [tyco `%% map (ITyVar o fst) vs]) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
577 |
] |
33992 | 578 |
)) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
579 |
end; |
33992 | 580 |
fun print_stmt (ML_Exc (name, (vs_ty, n))) = pair |
581 |
[print_val_decl print_typscheme (name, vs_ty)] |
|
582 |
((doublesemicolon o map str) ( |
|
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
583 |
"let" |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
584 |
:: deresolve name |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
585 |
:: replicate n "_" |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
586 |
@ "=" |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
587 |
:: "failwith" |
33992 | 588 |
@@ (ML_Syntax.print_string o Long_Name.base_name o Long_Name.qualifier) name |
589 |
)) |
|
590 |
| print_stmt (ML_Val binding) = |
|
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
591 |
let |
33992 | 592 |
val (sig_p, p) = print_def (K false) true "let" binding |
593 |
in pair |
|
594 |
[sig_p] |
|
595 |
(doublesemicolon [p]) |
|
596 |
end |
|
597 |
| print_stmt (ML_Funs (binding :: bindings, pseudo_funs)) = |
|
598 |
let |
|
599 |
val print_def' = print_def (member (op =) pseudo_funs) false; |
|
600 |
fun print_pseudo_fun name = concat [ |
|
29189 | 601 |
str "let", |
602 |
(str o deresolve) name, |
|
603 |
str "=", |
|
604 |
(str o deresolve) name, |
|
605 |
str "();;" |
|
606 |
]; |
|
33992 | 607 |
val (sig_ps, (ps, p)) = (apsnd split_last o split_list) |
608 |
(print_def' "let rec" binding :: map (print_def' "and") bindings); |
|
609 |
val pseudo_ps = map print_pseudo_fun pseudo_funs; |
|
610 |
in pair |
|
611 |
sig_ps |
|
612 |
(Pretty.chunks (ps @ doublesemicolon [p] :: pseudo_ps)) |
|
613 |
end |
|
614 |
| print_stmt (ML_Datas [(tyco, (vs, []))]) = |
|
615 |
let |
|
48003
1d11af40b106
dropped sort constraints on datatype specifications
haftmann
parents:
47609
diff
changeset
|
616 |
val ty_p = print_tyco_expr (tyco, map ITyVar vs); |
33992 | 617 |
in |
618 |
pair |
|
619 |
[concat [str "type", ty_p]] |
|
620 |
(concat [str "type", ty_p, str "=", str "EMPTY__"]) |
|
621 |
end |
|
622 |
| print_stmt (ML_Datas (data :: datas)) = |
|
28054 | 623 |
let |
33992 | 624 |
val sig_ps = print_datatype_decl "type" data |
625 |
:: map (print_datatype_decl "and") datas; |
|
626 |
val (ps, p) = split_last sig_ps; |
|
627 |
in pair |
|
628 |
sig_ps |
|
629 |
(Pretty.chunks (ps @| doublesemicolon [p])) |
|
630 |
end |
|
37447
ad3e04f289b6
transitive superclasses were also only a misunderstanding
haftmann
parents:
37446
diff
changeset
|
631 |
| print_stmt (ML_Class (class, (v, (super_classes, classparams)))) = |
28054 | 632 |
let |
33992 | 633 |
fun print_field s p = concat [str s, str ":", p]; |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
634 |
fun print_super_class_field (super_class, classrel) = |
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
635 |
print_field (deresolve classrel) (print_dicttyp (super_class, ITyVar v)); |
33992 | 636 |
fun print_classparam_decl (classparam, ty) = |
637 |
print_val_decl print_typscheme |
|
638 |
(classparam, ([(v, [class])], ty)); |
|
639 |
fun print_classparam_field (classparam, ty) = |
|
640 |
print_field (deresolve classparam) (print_typ NOBR ty); |
|
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
641 |
val w = "_" ^ first_upper v; |
33992 | 642 |
fun print_classparam_proj (classparam, _) = |
643 |
(concat o map str) ["let", deresolve classparam, w, "=", |
|
644 |
w ^ "." ^ deresolve classparam ^ ";;"]; |
|
645 |
val type_decl_p = concat [ |
|
646 |
str ("type '" ^ v), |
|
647 |
(str o deresolve) class, |
|
28054 | 648 |
str "=", |
33992 | 649 |
enum_default "unit" ";" "{" "}" ( |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
650 |
map print_super_class_field super_classes |
33992 | 651 |
@ map print_classparam_field classparams |
652 |
) |
|
28054 | 653 |
]; |
33992 | 654 |
in pair |
655 |
(type_decl_p :: map print_classparam_decl classparams) |
|
656 |
(Pretty.chunks ( |
|
657 |
doublesemicolon [type_decl_p] |
|
658 |
:: map print_classparam_proj classparams |
|
659 |
)) |
|
660 |
end; |
|
661 |
in print_stmt end; |
|
28054 | 662 |
|
38933
bd77e092f67c
avoid strange special treatment of empty module names
haftmann
parents:
38928
diff
changeset
|
663 |
fun print_ocaml_module name some_decls body = |
bd77e092f67c
avoid strange special treatment of empty module names
haftmann
parents:
38928
diff
changeset
|
664 |
Pretty.chunks2 ( |
33992 | 665 |
Pretty.chunks ( |
666 |
str ("module " ^ name ^ (if is_some some_decls then " : sig" else " =")) |
|
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
667 |
:: (the_list o Option.map (indent 2 o Pretty.chunks)) some_decls |
33992 | 668 |
@| (if is_some some_decls then str "end = struct" else str "struct") |
669 |
) |
|
670 |
:: body |
|
671 |
@| str ("end;; (*struct " ^ name ^ "*)") |
|
28054 | 672 |
); |
673 |
||
28064 | 674 |
val literals_ocaml = let |
675 |
fun chr i = |
|
676 |
let |
|
677 |
val xs = string_of_int i; |
|
40627
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents:
39148
diff
changeset
|
678 |
val ys = replicate_string (3 - length (raw_explode xs)) "0"; |
28064 | 679 |
in "\\" ^ ys ^ xs end; |
680 |
fun char_ocaml c = |
|
681 |
let |
|
682 |
val i = ord c; |
|
683 |
val s = if i < 32 orelse i = 34 orelse i = 39 orelse i = 92 orelse i > 126 |
|
684 |
then chr i else c |
|
685 |
in s end; |
|
34944
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
686 |
fun numeral_ocaml k = if k < 0 |
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
687 |
then "(Big_int.minus_big_int " ^ numeral_ocaml (~ k) ^ ")" |
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
688 |
else if k <= 1073741823 |
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
689 |
then "(Big_int.big_int_of_int " ^ string_of_int k ^ ")" |
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
690 |
else "(Big_int.big_int_of_string " ^ quote (string_of_int k) ^ ")" |
28064 | 691 |
in Literals { |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
692 |
literal_char = Library.enclose "'" "'" o char_ocaml, |
28064 | 693 |
literal_string = quote o translate_string char_ocaml, |
34944
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
694 |
literal_numeral = numeral_ocaml, |
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
695 |
literal_positive_numeral = numeral_ocaml, |
37958
9728342bcd56
another refinement chapter in the neverending numeral story
haftmann
parents:
37822
diff
changeset
|
696 |
literal_alternative_numeral = numeral_ocaml, |
34944
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
697 |
literal_naive_numeral = numeral_ocaml, |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
698 |
literal_list = enum ";" "[" "]", |
28064 | 699 |
infix_cons = (6, "::") |
700 |
} end; |
|
701 |
||
702 |
||
28054 | 703 |
|
704 |
(** SML/OCaml generic part **) |
|
705 |
||
39028
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
706 |
fun ml_program_of_program labelled_name reserved module_alias program = |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
707 |
let |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
708 |
fun namify_const upper base (nsp_const, nsp_type) = |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
709 |
let |
43326
47cf4bc789aa
simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents:
42599
diff
changeset
|
710 |
val (base', nsp_const') = |
47cf4bc789aa
simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents:
42599
diff
changeset
|
711 |
Name.variant (if upper then first_upper base else base) nsp_const |
39028
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
712 |
in (base', (nsp_const', nsp_type)) end; |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
713 |
fun namify_type base (nsp_const, nsp_type) = |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
714 |
let |
43326
47cf4bc789aa
simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents:
42599
diff
changeset
|
715 |
val (base', nsp_type') = Name.variant base nsp_type |
39028
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
716 |
in (base', (nsp_const, nsp_type')) end; |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
717 |
fun namify_stmt (Code_Thingol.Fun _) = namify_const false |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
718 |
| namify_stmt (Code_Thingol.Datatype _) = namify_type |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
719 |
| namify_stmt (Code_Thingol.Datatypecons _) = namify_const true |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
720 |
| namify_stmt (Code_Thingol.Class _) = namify_type |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
721 |
| namify_stmt (Code_Thingol.Classrel _) = namify_const false |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
722 |
| namify_stmt (Code_Thingol.Classparam _) = namify_const false |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
723 |
| namify_stmt (Code_Thingol.Classinst _) = namify_const false; |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
724 |
fun ml_binding_of_stmt (name, Code_Thingol.Fun (_, ((tysm as (vs, ty), raw_eqs), _))) = |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
725 |
let |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
726 |
val eqs = filter (snd o snd) raw_eqs; |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
727 |
val (eqs', some_value_name) = if null (filter_out (null o snd) vs) then case eqs |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
728 |
of [(([], t), some_thm)] => if (not o null o fst o Code_Thingol.unfold_fun) ty |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
729 |
then ([(([IVar (SOME "x")], t `$ IVar (SOME "x")), some_thm)], NONE) |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
730 |
else (eqs, SOME (name, member (op =) (Code_Thingol.add_constnames t []) name)) |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
731 |
| _ => (eqs, NONE) |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
732 |
else (eqs, NONE) |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
733 |
in (ML_Function (name, (tysm, eqs')), some_value_name) end |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
734 |
| ml_binding_of_stmt (name, Code_Thingol.Classinst (stmt as ((_, (_, vs)), _))) = |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
735 |
(ML_Instance (name, stmt), if forall (null o snd) vs then SOME (name, false) else NONE) |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
736 |
| ml_binding_of_stmt (name, _) = |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
737 |
error ("Binding block containing illegal statement: " ^ labelled_name name) |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
738 |
fun modify_fun (name, stmt) = |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
739 |
let |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
740 |
val (binding, some_value_name) = ml_binding_of_stmt (name, stmt); |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
741 |
val ml_stmt = case binding |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
742 |
of ML_Function (name, ((vs, ty), [])) => |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
743 |
ML_Exc (name, ((vs, ty), |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
744 |
(length o filter_out (null o snd)) vs + (length o fst o Code_Thingol.unfold_fun) ty)) |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
745 |
| _ => case some_value_name |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
746 |
of NONE => ML_Funs ([binding], []) |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
747 |
| SOME (name, true) => ML_Funs ([binding], [name]) |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
748 |
| SOME (name, false) => ML_Val binding |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
749 |
in SOME ml_stmt end; |
39031 | 750 |
fun modify_funs stmts = single (SOME |
751 |
(ML_Funs (map_split ml_binding_of_stmt stmts |> (apsnd o map_filter o Option.map) fst))) |
|
752 |
fun modify_datatypes stmts = single (SOME |
|
753 |
(ML_Datas (map_filter |
|
754 |
(fn (name, Code_Thingol.Datatype (_, stmt)) => SOME (name, stmt) | _ => NONE) stmts))) |
|
755 |
fun modify_class stmts = single (SOME |
|
756 |
(ML_Class (the_single (map_filter |
|
757 |
(fn (name, Code_Thingol.Class (_, stmt)) => SOME (name, stmt) | _ => NONE) stmts)))) |
|
47576 | 758 |
fun modify_stmts ([stmt as (_, stmt' as Code_Thingol.Fun _)]) = |
39059
3a11a667af75
restored and added surpression of case combinators
haftmann
parents:
39058
diff
changeset
|
759 |
if Code_Thingol.is_case stmt' then [] else [modify_fun stmt] |
39028
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
760 |
| modify_stmts ((stmts as (_, Code_Thingol.Fun _)::_)) = |
39061 | 761 |
modify_funs (filter_out (Code_Thingol.is_case o snd) stmts) |
39028
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
762 |
| modify_stmts ((stmts as (_, Code_Thingol.Datatypecons _)::_)) = |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
763 |
modify_datatypes stmts |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
764 |
| modify_stmts ((stmts as (_, Code_Thingol.Datatype _)::_)) = |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
765 |
modify_datatypes stmts |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
766 |
| modify_stmts ((stmts as (_, Code_Thingol.Class _)::_)) = |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
767 |
modify_class stmts |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
768 |
| modify_stmts ((stmts as (_, Code_Thingol.Classrel _)::_)) = |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
769 |
modify_class stmts |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
770 |
| modify_stmts ((stmts as (_, Code_Thingol.Classparam _)::_)) = |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
771 |
modify_class stmts |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
772 |
| modify_stmts ([stmt as (_, Code_Thingol.Classinst _)]) = |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
773 |
[modify_fun stmt] |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
774 |
| modify_stmts ((stmts as (_, Code_Thingol.Classinst _)::_)) = |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
775 |
modify_funs stmts |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
776 |
| modify_stmts stmts = error ("Illegal mutual dependencies: " ^ |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
777 |
(Library.commas o map (labelled_name o fst)) stmts); |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
778 |
in |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
779 |
Code_Namespace.hierarchical_program labelled_name { module_alias = module_alias, reserved = reserved, |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
780 |
empty_nsp = (reserved, reserved), namify_module = pair, namify_stmt = namify_stmt, |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
781 |
cyclic_modules = false, empty_data = (), memorize_data = K I, modify_stmts = modify_stmts } program |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
782 |
end; |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
783 |
|
47576 | 784 |
fun serialize_ml print_ml_module print_ml_stmt with_signatures |
39142 | 785 |
{ labelled_name, reserved_syms, includes, module_alias, |
41343 | 786 |
class_syntax, tyco_syntax, const_syntax } program = |
28054 | 787 |
let |
39147 | 788 |
|
789 |
(* build program *) |
|
39028
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
790 |
val { deresolver, hierarchical_program = ml_program } = |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
791 |
ml_program_of_program labelled_name (Name.make_context reserved_syms) module_alias program; |
39147 | 792 |
|
793 |
(* print statements *) |
|
794 |
fun print_stmt prefix_fragments (_, stmt) = print_ml_stmt |
|
47576 | 795 |
tyco_syntax const_syntax (make_vars reserved_syms) |
39147 | 796 |
(Code_Thingol.is_cons program) (deresolver prefix_fragments) stmt |
797 |
|> apfst SOME; |
|
798 |
||
799 |
(* print modules *) |
|
47576 | 800 |
fun print_module _ base _ xs = |
39147 | 801 |
let |
802 |
val (raw_decls, body) = split_list xs; |
|
803 |
val decls = if with_signatures then SOME (maps these raw_decls) else NONE |
|
804 |
in (NONE, print_ml_module base decls body) end; |
|
805 |
||
806 |
(* serialization *) |
|
807 |
val p = Pretty.chunks2 (map snd includes |
|
808 |
@ map snd (Code_Namespace.print_hierarchical { |
|
809 |
print_module = print_module, print_stmt = print_stmt, |
|
810 |
lift_markup = apsnd } ml_program)); |
|
39056 | 811 |
fun write width NONE = writeln o format [] width |
812 |
| write width (SOME p) = File.write p o format [] width; |
|
28054 | 813 |
in |
39102 | 814 |
Code_Target.serialization write (rpair (try (deresolver [])) ooo format) p |
28054 | 815 |
end; |
816 |
||
38966 | 817 |
val serializer_sml : Code_Target.serializer = |
33992 | 818 |
Code_Target.parse_args (Scan.optional (Args.$$$ "no_signatures" >> K false) true |
47576 | 819 |
>> (fn with_signatures => serialize_ml print_sml_module print_sml_stmt with_signatures)); |
28054 | 820 |
|
38966 | 821 |
val serializer_ocaml : Code_Target.serializer = |
33992 | 822 |
Code_Target.parse_args (Scan.optional (Args.$$$ "no_signatures" >> K false) true |
47576 | 823 |
>> (fn with_signatures => serialize_ml print_ocaml_module print_ocaml_stmt with_signatures)); |
28054 | 824 |
|
38966 | 825 |
|
826 |
(** Isar setup **) |
|
827 |
||
28054 | 828 |
val setup = |
37821 | 829 |
Code_Target.add_target |
38966 | 830 |
(target_SML, { serializer = serializer_sml, literals = literals_sml, |
41940 | 831 |
check = { env_var = "ISABELLE_PROCESS", |
832 |
make_destination = fn p => Path.append p (Path.explode "ROOT.ML"), |
|
833 |
make_command = fn _ => "\"$ISABELLE_PROCESS\" -r -q -u Pure" } }) |
|
37821 | 834 |
#> Code_Target.add_target |
38966 | 835 |
(target_OCaml, { serializer = serializer_ocaml, literals = literals_ocaml, |
41952
c7297638599b
cleanup of former settings GHC_PATH, EXEC_GHC, EXEC_OCAML, EXEC_SWIPL, EXEC_YAP -- discontinued implicit detection;
wenzelm
parents:
41940
diff
changeset
|
836 |
check = { env_var = "ISABELLE_OCAML", |
41940 | 837 |
make_destination = fn p => Path.append p (Path.explode "ROOT.ocaml"), |
41952
c7297638599b
cleanup of former settings GHC_PATH, EXEC_GHC, EXEC_OCAML, EXEC_SWIPL, EXEC_YAP -- discontinued implicit detection;
wenzelm
parents:
41940
diff
changeset
|
838 |
make_command = fn _ => "\"$ISABELLE_OCAML\" -w pu nums.cma ROOT.ocaml" } }) |
38923 | 839 |
#> Code_Target.add_tyco_syntax target_SML "fun" (SOME (2, fn print_typ => fn fxy => fn [ty1, ty2] => |
37242
97097e589715
brackify_infix etc.: no break before infix operator -- eases survival in Scala
haftmann
parents:
36536
diff
changeset
|
840 |
brackify_infix (1, R) fxy ( |
33992 | 841 |
print_typ (INFX (1, X)) ty1, |
28054 | 842 |
str "->", |
33992 | 843 |
print_typ (INFX (1, R)) ty2 |
37242
97097e589715
brackify_infix etc.: no break before infix operator -- eases survival in Scala
haftmann
parents:
36536
diff
changeset
|
844 |
))) |
38923 | 845 |
#> Code_Target.add_tyco_syntax target_OCaml "fun" (SOME (2, fn print_typ => fn fxy => fn [ty1, ty2] => |
37242
97097e589715
brackify_infix etc.: no break before infix operator -- eases survival in Scala
haftmann
parents:
36536
diff
changeset
|
846 |
brackify_infix (1, R) fxy ( |
33992 | 847 |
print_typ (INFX (1, X)) ty1, |
28054 | 848 |
str "->", |
33992 | 849 |
print_typ (INFX (1, R)) ty2 |
37242
97097e589715
brackify_infix etc.: no break before infix operator -- eases survival in Scala
haftmann
parents:
36536
diff
changeset
|
850 |
))) |
28054 | 851 |
#> fold (Code_Target.add_reserved target_SML) ML_Syntax.reserved_names |
852 |
#> fold (Code_Target.add_reserved target_SML) |
|
38070 | 853 |
["ref" (*rebinding is illegal*), "o" (*dictionary projections use it already*), |
854 |
"Fail", "div", "mod" (*standard infixes*), "IntInf"] |
|
28054 | 855 |
#> fold (Code_Target.add_reserved target_OCaml) [ |
856 |
"and", "as", "assert", "begin", "class", |
|
857 |
"constraint", "do", "done", "downto", "else", "end", "exception", |
|
858 |
"external", "false", "for", "fun", "function", "functor", "if", |
|
859 |
"in", "include", "inherit", "initializer", "lazy", "let", "match", "method", |
|
860 |
"module", "mutable", "new", "object", "of", "open", "or", "private", "rec", |
|
861 |
"sig", "struct", "then", "to", "true", "try", "type", "val", |
|
862 |
"virtual", "when", "while", "with" |
|
863 |
] |
|
34944
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
864 |
#> fold (Code_Target.add_reserved target_OCaml) ["failwith", "mod", "Big_int"]; |
28054 | 865 |
|
866 |
end; (*struct*) |