author | haftmann |
Wed, 01 Sep 2010 08:52:49 +0200 | |
changeset 38966 | 68853347ba37 |
parent 38933 | bd77e092f67c |
child 39028 | 0dd6c5a0beef |
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 |
37449 | 39 |
| ML_Datas of (string * ((vname * sort) 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 |
|
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
42 |
fun stmt_name_of_binding (ML_Function (name, _)) = name |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
43 |
| stmt_name_of_binding (ML_Instance (name, _)) = name; |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
44 |
|
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
45 |
fun stmt_names_of (ML_Exc (name, _)) = [name] |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
46 |
| stmt_names_of (ML_Val binding) = [stmt_name_of_binding binding] |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
47 |
| stmt_names_of (ML_Funs (bindings, _)) = map stmt_name_of_binding bindings |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
48 |
| stmt_names_of (ML_Datas ds) = map fst ds |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
49 |
| stmt_names_of (ML_Class (name, _)) = [name]; |
28054 | 50 |
|
33992 | 51 |
fun print_product _ [] = NONE |
52 |
| 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
|
53 |
| print_product print xs = (SOME o enum " *" "" "") (map print xs); |
28054 | 54 |
|
38922 | 55 |
fun tuplify _ _ [] = NONE |
56 |
| tuplify print fxy [x] = SOME (print fxy x) |
|
57 |
| tuplify print _ xs = SOME (enum "," "(" ")" (map (print NOBR) xs)); |
|
28054 | 58 |
|
33992 | 59 |
|
60 |
(** SML serializer **) |
|
61 |
||
38923 | 62 |
fun print_sml_stmt labelled_name tyco_syntax const_syntax reserved is_cons deresolve = |
28054 | 63 |
let |
33992 | 64 |
fun print_tyco_expr fxy (tyco, []) = (str o deresolve) tyco |
65 |
| print_tyco_expr fxy (tyco, [ty]) = |
|
66 |
concat [print_typ BR ty, (str o deresolve) tyco] |
|
67 |
| print_tyco_expr fxy (tyco, tys) = |
|
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
68 |
concat [enum "," "(" ")" (map (print_typ BR) tys), (str o deresolve) tyco] |
38923 | 69 |
and print_typ fxy (tyco `%% tys) = (case tyco_syntax tyco |
33992 | 70 |
of NONE => print_tyco_expr fxy (tyco, tys) |
71 |
| SOME (i, print) => print print_typ fxy tys) |
|
72 |
| print_typ fxy (ITyVar v) = str ("'" ^ v); |
|
73 |
fun print_dicttyp (class, ty) = print_tyco_expr NOBR (class, [ty]); |
|
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
74 |
fun print_typscheme_prefix (vs, p) = enum " ->" "" "" |
33992 | 75 |
(map_filter (fn (v, sort) => |
76 |
(print_product (fn class => print_dicttyp (class, ITyVar v)) sort)) vs @| p); |
|
77 |
fun print_typscheme (vs, ty) = print_typscheme_prefix (vs, print_typ NOBR ty); |
|
78 |
fun print_dicttypscheme (vs, class_ty) = print_typscheme_prefix (vs, print_dicttyp class_ty); |
|
79 |
fun print_dict is_pseudo_fun fxy (DictConst (inst, dss)) = |
|
80 |
brackify fxy ((str o deresolve) inst :: |
|
81 |
(if is_pseudo_fun inst then [str "()"] |
|
82 |
else map_filter (print_dicts is_pseudo_fun BR) dss)) |
|
83 |
| print_dict is_pseudo_fun fxy (DictVar (classrels, (v, (i, k)))) = |
|
84 |
let |
|
85 |
val v_p = str (if k = 1 then first_upper v ^ "_" |
|
86 |
else first_upper v ^ string_of_int (i+1) ^ "_"); |
|
87 |
in case classrels |
|
88 |
of [] => v_p |
|
89 |
| [classrel] => brackets [(str o deresolve) classrel, v_p] |
|
90 |
| classrels => brackets |
|
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
91 |
[enum " o" "(" ")" (map (str o deresolve) classrels), v_p] |
33992 | 92 |
end |
38922 | 93 |
and print_dicts is_pseudo_fun = tuplify (print_dict is_pseudo_fun); |
33992 | 94 |
val print_dict_args = map_filter (fn (v, sort) => print_dicts (K false) BR |
95 |
(map_index (fn (i, _) => DictVar ([], (v, (i, length sort)))) sort)); |
|
35228 | 96 |
fun print_term is_pseudo_fun some_thm vars fxy (IConst c) = |
97 |
print_app is_pseudo_fun some_thm vars fxy (c, []) |
|
98 |
| print_term is_pseudo_fun some_thm vars fxy (IVar NONE) = |
|
31889 | 99 |
str "_" |
35228 | 100 |
| 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
|
101 |
str (lookup_var vars v) |
35228 | 102 |
| print_term is_pseudo_fun some_thm vars fxy (t as t1 `$ t2) = |
28054 | 103 |
(case Code_Thingol.unfold_const_app t |
35228 | 104 |
of SOME c_ts => print_app is_pseudo_fun some_thm vars fxy c_ts |
105 |
| NONE => brackify fxy [print_term is_pseudo_fun some_thm vars NOBR t1, |
|
106 |
print_term is_pseudo_fun some_thm vars BR t2]) |
|
107 |
| print_term is_pseudo_fun some_thm vars fxy (t as _ `|=> _) = |
|
28054 | 108 |
let |
31874 | 109 |
val (binds, t') = Code_Thingol.unfold_pat_abs t; |
33992 | 110 |
fun print_abs (pat, ty) = |
35228 | 111 |
print_bind is_pseudo_fun some_thm NOBR pat |
28054 | 112 |
#>> (fn p => concat [str "fn", p, str "=>"]); |
33992 | 113 |
val (ps, vars') = fold_map print_abs binds vars; |
35228 | 114 |
in brackets (ps @ [print_term is_pseudo_fun some_thm vars' NOBR t']) end |
115 |
| print_term is_pseudo_fun some_thm vars fxy (ICase (cases as (_, t0))) = |
|
28054 | 116 |
(case Code_Thingol.unfold_const_app t0 |
38923 | 117 |
of SOME (c_ts as ((c, _), _)) => if is_none (const_syntax c) |
35228 | 118 |
then print_case is_pseudo_fun some_thm vars fxy cases |
119 |
else print_app is_pseudo_fun some_thm vars fxy c_ts |
|
120 |
| NONE => print_case is_pseudo_fun some_thm vars fxy cases) |
|
37449 | 121 |
and print_app_expr is_pseudo_fun some_thm vars (app as ((c, ((_, iss), function_typs)), ts)) = |
29175 | 122 |
if is_cons c then |
37449 | 123 |
let val k = length function_typs in |
33992 | 124 |
if k < 2 orelse length ts = k |
125 |
then (str o deresolve) c |
|
38922 | 126 |
:: the_list (tuplify (print_term is_pseudo_fun some_thm vars) BR ts) |
35228 | 127 |
else [print_term is_pseudo_fun some_thm vars BR (Code_Thingol.eta_expand k app)] |
33992 | 128 |
end |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
129 |
else if is_pseudo_fun c |
29175 | 130 |
then (str o deresolve) c @@ str "()" |
33992 | 131 |
else (str o deresolve) c :: map_filter (print_dicts is_pseudo_fun BR) iss |
35228 | 132 |
@ map (print_term is_pseudo_fun some_thm vars BR) ts |
133 |
and print_app is_pseudo_fun some_thm vars = gen_print_app (print_app_expr is_pseudo_fun) |
|
38923 | 134 |
(print_term is_pseudo_fun) const_syntax some_thm vars |
33992 | 135 |
and print_bind is_pseudo_fun = gen_print_bind (print_term is_pseudo_fun) |
35228 | 136 |
and print_case is_pseudo_fun some_thm vars fxy (cases as ((_, [_]), _)) = |
28054 | 137 |
let |
29952
9aed85067721
unified variable names in case expressions; no exponential fork in translation of case expressions
haftmann
parents:
29264
diff
changeset
|
138 |
val (binds, body) = Code_Thingol.unfold_let (ICase cases); |
33992 | 139 |
fun print_match ((pat, ty), t) vars = |
28054 | 140 |
vars |
35228 | 141 |
|> print_bind is_pseudo_fun some_thm NOBR pat |
33992 | 142 |
|>> (fn p => semicolon [str "val", p, str "=", |
35228 | 143 |
print_term is_pseudo_fun some_thm vars NOBR t]) |
33992 | 144 |
val (ps, vars') = fold_map print_match binds vars; |
28054 | 145 |
in |
146 |
Pretty.chunks [ |
|
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
147 |
Pretty.block [str "let", Pretty.fbrk, Pretty.chunks ps], |
35228 | 148 |
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
|
149 |
str "end" |
28054 | 150 |
] |
151 |
end |
|
35228 | 152 |
| print_case is_pseudo_fun some_thm vars fxy (((t, ty), clause :: clauses), _) = |
28054 | 153 |
let |
33992 | 154 |
fun print_select delim (pat, body) = |
28054 | 155 |
let |
35228 | 156 |
val (p, vars') = print_bind is_pseudo_fun some_thm NOBR pat vars; |
28054 | 157 |
in |
35228 | 158 |
concat [str delim, p, str "=>", print_term is_pseudo_fun some_thm vars' NOBR body] |
28054 | 159 |
end; |
160 |
in |
|
31665 | 161 |
brackets ( |
28054 | 162 |
str "case" |
35228 | 163 |
:: print_term is_pseudo_fun some_thm vars NOBR t |
33992 | 164 |
:: print_select "of" clause |
165 |
:: map (print_select "|") clauses |
|
28054 | 166 |
) |
167 |
end |
|
35228 | 168 |
| print_case is_pseudo_fun some_thm vars fxy ((_, []), _) = |
31121 | 169 |
(concat o map str) ["raise", "Fail", "\"empty case\""]; |
33992 | 170 |
fun print_val_decl print_typscheme (name, typscheme) = concat |
171 |
[str "val", str (deresolve name), str ":", print_typscheme typscheme]; |
|
172 |
fun print_datatype_decl definer (tyco, (vs, cos)) = |
|
173 |
let |
|
37449 | 174 |
fun print_co ((co, _), []) = str (deresolve co) |
175 |
| 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
|
176 |
enum " *" "" "" (map (print_typ (INFX (2, X))) tys)]; |
33992 | 177 |
in |
178 |
concat ( |
|
179 |
str definer |
|
180 |
:: print_tyco_expr NOBR (tyco, map (ITyVar o fst) vs) |
|
181 |
:: str "=" |
|
182 |
:: separate (str "|") (map print_co cos) |
|
183 |
) |
|
184 |
end; |
|
185 |
fun print_def is_pseudo_fun needs_typ definer |
|
186 |
(ML_Function (name, (vs_ty as (vs, ty), eq :: eqs))) = |
|
29189 | 187 |
let |
35228 | 188 |
fun print_eqn definer ((ts, t), (some_thm, _)) = |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
189 |
let |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
190 |
val consts = fold Code_Thingol.add_constnames (t :: ts) []; |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
191 |
val vars = reserved |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
192 |
|> intro_base_names |
38923 | 193 |
(is_none o const_syntax) deresolve consts |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
194 |
|> intro_vars ((fold o Code_Thingol.fold_varnames) |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
195 |
(insert (op =)) ts []); |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
196 |
val prolog = if needs_typ then |
33992 | 197 |
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
|
198 |
else (concat o map str) [definer, deresolve name]; |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
199 |
in |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
200 |
concat ( |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
201 |
prolog |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
202 |
:: (if is_pseudo_fun name then [str "()"] |
33992 | 203 |
else print_dict_args vs |
35228 | 204 |
@ 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
|
205 |
@ str "=" |
35228 | 206 |
@@ print_term is_pseudo_fun some_thm vars NOBR t |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
207 |
) |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
208 |
end |
33992 | 209 |
val shift = if null eqs then I else |
210 |
map (Pretty.block o single o Pretty.block o single); |
|
211 |
in pair |
|
212 |
(print_val_decl print_typscheme (name, vs_ty)) |
|
213 |
((Pretty.block o Pretty.fbreaks o shift) ( |
|
214 |
print_eqn definer eq |
|
215 |
:: map (print_eqn "|") eqs |
|
216 |
)) |
|
29189 | 217 |
end |
33992 | 218 |
| print_def is_pseudo_fun _ definer |
37449 | 219 |
(ML_Instance (inst, ((class, (tyco, vs)), (super_instances, (classparam_instances, _))))) = |
29189 | 220 |
let |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
221 |
fun print_super_instance (_, (classrel, dss)) = |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
222 |
concat [ |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
223 |
(str o Long_Name.base_name o deresolve) classrel, |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
224 |
str "=", |
33992 | 225 |
print_dict is_pseudo_fun NOBR (DictConst dss) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
226 |
]; |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
227 |
fun print_classparam_instance ((classparam, const), (thm, _)) = |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
228 |
concat [ |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
229 |
(str o Long_Name.base_name o deresolve) classparam, |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
230 |
str "=", |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
231 |
print_app (K false) (SOME thm) reserved NOBR (const, []) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
232 |
]; |
33992 | 233 |
in pair |
234 |
(print_val_decl print_dicttypscheme |
|
235 |
(inst, (vs, (class, tyco `%% map (ITyVar o fst) vs)))) |
|
236 |
(concat ( |
|
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
237 |
str definer |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
238 |
:: (str o deresolve) inst |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
239 |
:: (if is_pseudo_fun inst then [str "()"] |
33992 | 240 |
else print_dict_args vs) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
241 |
@ str "=" |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
242 |
:: enum "," "{" "}" |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
243 |
(map print_super_instance super_instances |
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
244 |
@ map print_classparam_instance classparam_instances) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
245 |
:: str ":" |
33992 | 246 |
@@ print_tyco_expr NOBR (class, [tyco `%% map (ITyVar o fst) vs]) |
247 |
)) |
|
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
248 |
end; |
33992 | 249 |
fun print_stmt (ML_Exc (name, (vs_ty, n))) = pair |
250 |
[print_val_decl print_typscheme (name, vs_ty)] |
|
251 |
((semicolon o map str) ( |
|
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
252 |
(if n = 0 then "val" else "fun") |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
253 |
:: deresolve name |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
254 |
:: replicate n "_" |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
255 |
@ "=" |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
256 |
:: "raise" |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
257 |
:: "Fail" |
33992 | 258 |
@@ (ML_Syntax.print_string o Long_Name.base_name o Long_Name.qualifier) name |
259 |
)) |
|
260 |
| print_stmt (ML_Val binding) = |
|
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
261 |
let |
33992 | 262 |
val (sig_p, p) = print_def (K false) true "val" binding |
263 |
in pair |
|
264 |
[sig_p] |
|
265 |
(semicolon [p]) |
|
266 |
end |
|
267 |
| print_stmt (ML_Funs (binding :: bindings, pseudo_funs)) = |
|
268 |
let |
|
269 |
val print_def' = print_def (member (op =) pseudo_funs) false; |
|
270 |
fun print_pseudo_fun name = concat [ |
|
29189 | 271 |
str "val", |
272 |
(str o deresolve) name, |
|
273 |
str "=", |
|
274 |
(str o deresolve) name, |
|
275 |
str "();" |
|
276 |
]; |
|
33992 | 277 |
val (sig_ps, (ps, p)) = (apsnd split_last o split_list) |
278 |
(print_def' "fun" binding :: map (print_def' "and") bindings); |
|
279 |
val pseudo_ps = map print_pseudo_fun pseudo_funs; |
|
280 |
in pair |
|
281 |
sig_ps |
|
282 |
(Pretty.chunks (ps @ semicolon [p] :: pseudo_ps)) |
|
283 |
end |
|
284 |
| print_stmt (ML_Datas [(tyco, (vs, []))]) = |
|
285 |
let |
|
286 |
val ty_p = print_tyco_expr NOBR (tyco, map (ITyVar o fst) vs); |
|
287 |
in |
|
288 |
pair |
|
289 |
[concat [str "type", ty_p]] |
|
290 |
(concat [str "datatype", ty_p, str "=", str "EMPTY__"]) |
|
291 |
end |
|
292 |
| print_stmt (ML_Datas (data :: datas)) = |
|
28054 | 293 |
let |
33992 | 294 |
val sig_ps = print_datatype_decl "datatype" data |
295 |
:: map (print_datatype_decl "and") datas; |
|
296 |
val (ps, p) = split_last sig_ps; |
|
297 |
in pair |
|
298 |
sig_ps |
|
299 |
(Pretty.chunks (ps @| semicolon [p])) |
|
300 |
end |
|
37447
ad3e04f289b6
transitive superclasses were also only a misunderstanding
haftmann
parents:
37446
diff
changeset
|
301 |
| print_stmt (ML_Class (class, (v, (super_classes, classparams)))) = |
28054 | 302 |
let |
33992 | 303 |
fun print_field s p = concat [str s, str ":", p]; |
304 |
fun print_proj s p = semicolon |
|
305 |
(map str ["val", s, "=", "#" ^ s, ":"] @| p); |
|
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
306 |
fun print_super_class_decl (super_class, classrel) = |
33992 | 307 |
print_val_decl print_dicttypscheme |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
308 |
(classrel, ([(v, [class])], (super_class, ITyVar v))); |
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
309 |
fun print_super_class_field (super_class, classrel) = |
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
310 |
print_field (deresolve classrel) (print_dicttyp (super_class, ITyVar v)); |
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
311 |
fun print_super_class_proj (super_class, classrel) = |
33992 | 312 |
print_proj (deresolve classrel) |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
313 |
(print_dicttypscheme ([(v, [class])], (super_class, ITyVar v))); |
33992 | 314 |
fun print_classparam_decl (classparam, ty) = |
315 |
print_val_decl print_typscheme |
|
316 |
(classparam, ([(v, [class])], ty)); |
|
317 |
fun print_classparam_field (classparam, ty) = |
|
318 |
print_field (deresolve classparam) (print_typ NOBR ty); |
|
319 |
fun print_classparam_proj (classparam, ty) = |
|
320 |
print_proj (deresolve classparam) |
|
321 |
(print_typscheme ([(v, [class])], ty)); |
|
322 |
in pair |
|
323 |
(concat [str "type", print_dicttyp (class, ITyVar v)] |
|
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
324 |
:: map print_super_class_decl super_classes |
33992 | 325 |
@ map print_classparam_decl classparams) |
326 |
(Pretty.chunks ( |
|
28054 | 327 |
concat [ |
328 |
str ("type '" ^ v), |
|
329 |
(str o deresolve) class, |
|
330 |
str "=", |
|
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
331 |
enum "," "{" "};" ( |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
332 |
map print_super_class_field super_classes |
33992 | 333 |
@ map print_classparam_field classparams |
28054 | 334 |
) |
335 |
] |
|
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
336 |
:: map print_super_class_proj super_classes |
33992 | 337 |
@ map print_classparam_proj classparams |
338 |
)) |
|
28054 | 339 |
end; |
33992 | 340 |
in print_stmt end; |
28054 | 341 |
|
38933
bd77e092f67c
avoid strange special treatment of empty module names
haftmann
parents:
38928
diff
changeset
|
342 |
fun print_sml_module name some_decls body = |
bd77e092f67c
avoid strange special treatment of empty module names
haftmann
parents:
38928
diff
changeset
|
343 |
Pretty.chunks2 ( |
33992 | 344 |
Pretty.chunks ( |
345 |
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
|
346 |
:: (the_list o Option.map (indent 2 o Pretty.chunks)) some_decls |
33992 | 347 |
@| (if is_some some_decls then str "end = struct" else str "struct") |
348 |
) |
|
349 |
:: body |
|
350 |
@| str ("end; (*struct " ^ name ^ "*)") |
|
28054 | 351 |
); |
352 |
||
28064 | 353 |
val literals_sml = Literals { |
354 |
literal_char = prefix "#" o quote o ML_Syntax.print_char, |
|
355 |
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
|
356 |
literal_numeral = fn k => "(" ^ string_of_int k ^ " : IntInf.int)", |
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
357 |
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
|
358 |
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
|
359 |
literal_naive_numeral = string_of_int, |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
360 |
literal_list = enum "," "[" "]", |
28064 | 361 |
infix_cons = (7, "::") |
362 |
}; |
|
363 |
||
28054 | 364 |
|
365 |
(** OCaml serializer **) |
|
366 |
||
38923 | 367 |
fun print_ocaml_stmt labelled_name tyco_syntax const_syntax reserved is_cons deresolve = |
28054 | 368 |
let |
33992 | 369 |
fun print_tyco_expr fxy (tyco, []) = (str o deresolve) tyco |
370 |
| print_tyco_expr fxy (tyco, [ty]) = |
|
371 |
concat [print_typ BR ty, (str o deresolve) tyco] |
|
372 |
| print_tyco_expr fxy (tyco, tys) = |
|
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
373 |
concat [enum "," "(" ")" (map (print_typ BR) tys), (str o deresolve) tyco] |
38923 | 374 |
and print_typ fxy (tyco `%% tys) = (case tyco_syntax tyco |
33992 | 375 |
of NONE => print_tyco_expr fxy (tyco, tys) |
376 |
| SOME (i, print) => print print_typ fxy tys) |
|
377 |
| print_typ fxy (ITyVar v) = str ("'" ^ v); |
|
378 |
fun print_dicttyp (class, ty) = print_tyco_expr NOBR (class, [ty]); |
|
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
379 |
fun print_typscheme_prefix (vs, p) = enum " ->" "" "" |
33992 | 380 |
(map_filter (fn (v, sort) => |
381 |
(print_product (fn class => print_dicttyp (class, ITyVar v)) sort)) vs @| p); |
|
382 |
fun print_typscheme (vs, ty) = print_typscheme_prefix (vs, print_typ NOBR ty); |
|
383 |
fun print_dicttypscheme (vs, class_ty) = print_typscheme_prefix (vs, print_dicttyp class_ty); |
|
384 |
fun print_dict is_pseudo_fun fxy (DictConst (inst, dss)) = |
|
385 |
brackify fxy ((str o deresolve) inst :: |
|
386 |
(if is_pseudo_fun inst then [str "()"] |
|
387 |
else map_filter (print_dicts is_pseudo_fun BR) dss)) |
|
388 |
| print_dict is_pseudo_fun fxy (DictVar (classrels, (v, (i, k)))) = |
|
389 |
str (if k = 1 then "_" ^ first_upper v |
|
390 |
else "_" ^ first_upper v ^ string_of_int (i+1)) |
|
391 |
|> fold_rev (fn classrel => fn p => |
|
392 |
Pretty.block [p, str ".", (str o deresolve) classrel]) classrels |
|
38922 | 393 |
and print_dicts is_pseudo_fun = tuplify (print_dict is_pseudo_fun); |
33992 | 394 |
val print_dict_args = map_filter (fn (v, sort) => print_dicts (K false) BR |
395 |
(map_index (fn (i, _) => DictVar ([], (v, (i, length sort)))) sort)); |
|
35228 | 396 |
fun print_term is_pseudo_fun some_thm vars fxy (IConst c) = |
397 |
print_app is_pseudo_fun some_thm vars fxy (c, []) |
|
398 |
| print_term is_pseudo_fun some_thm vars fxy (IVar NONE) = |
|
31889 | 399 |
str "_" |
35228 | 400 |
| 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
|
401 |
str (lookup_var vars v) |
35228 | 402 |
| print_term is_pseudo_fun some_thm vars fxy (t as t1 `$ t2) = |
28054 | 403 |
(case Code_Thingol.unfold_const_app t |
35228 | 404 |
of SOME c_ts => print_app is_pseudo_fun some_thm vars fxy c_ts |
405 |
| NONE => brackify fxy [print_term is_pseudo_fun some_thm vars NOBR t1, |
|
406 |
print_term is_pseudo_fun some_thm vars BR t2]) |
|
407 |
| print_term is_pseudo_fun some_thm vars fxy (t as _ `|=> _) = |
|
28054 | 408 |
let |
31874 | 409 |
val (binds, t') = Code_Thingol.unfold_pat_abs t; |
35228 | 410 |
val (ps, vars') = fold_map (print_bind is_pseudo_fun some_thm BR o fst) binds vars; |
411 |
in brackets (str "fun" :: ps @ str "->" @@ print_term is_pseudo_fun some_thm vars' NOBR t') end |
|
412 |
| print_term is_pseudo_fun some_thm vars fxy (ICase (cases as (_, t0))) = |
|
33992 | 413 |
(case Code_Thingol.unfold_const_app t0 |
38923 | 414 |
of SOME (c_ts as ((c, _), _)) => if is_none (const_syntax c) |
35228 | 415 |
then print_case is_pseudo_fun some_thm vars fxy cases |
416 |
else print_app is_pseudo_fun some_thm vars fxy c_ts |
|
417 |
| NONE => print_case is_pseudo_fun some_thm vars fxy cases) |
|
418 |
and print_app_expr is_pseudo_fun some_thm vars (app as ((c, ((_, iss), tys)), ts)) = |
|
28054 | 419 |
if is_cons c then |
33992 | 420 |
let val k = length tys in |
421 |
if length ts = k |
|
422 |
then (str o deresolve) c |
|
38922 | 423 |
:: the_list (tuplify (print_term is_pseudo_fun some_thm vars) BR ts) |
35228 | 424 |
else [print_term is_pseudo_fun some_thm vars BR (Code_Thingol.eta_expand k app)] |
33992 | 425 |
end |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
426 |
else if is_pseudo_fun c |
29175 | 427 |
then (str o deresolve) c @@ str "()" |
33992 | 428 |
else (str o deresolve) c :: map_filter (print_dicts is_pseudo_fun BR) iss |
35228 | 429 |
@ map (print_term is_pseudo_fun some_thm vars BR) ts |
430 |
and print_app is_pseudo_fun some_thm vars = gen_print_app (print_app_expr is_pseudo_fun) |
|
38923 | 431 |
(print_term is_pseudo_fun) const_syntax some_thm vars |
33992 | 432 |
and print_bind is_pseudo_fun = gen_print_bind (print_term is_pseudo_fun) |
35228 | 433 |
and print_case is_pseudo_fun some_thm vars fxy (cases as ((_, [_]), _)) = |
28054 | 434 |
let |
29952
9aed85067721
unified variable names in case expressions; no exponential fork in translation of case expressions
haftmann
parents:
29264
diff
changeset
|
435 |
val (binds, body) = Code_Thingol.unfold_let (ICase cases); |
33992 | 436 |
fun print_let ((pat, ty), t) vars = |
28054 | 437 |
vars |
35228 | 438 |
|> print_bind is_pseudo_fun some_thm NOBR pat |
28054 | 439 |
|>> (fn p => concat |
35228 | 440 |
[str "let", p, str "=", print_term is_pseudo_fun some_thm vars NOBR t, str "in"]) |
33992 | 441 |
val (ps, vars') = fold_map print_let binds vars; |
31665 | 442 |
in |
443 |
brackify_block fxy (Pretty.chunks ps) [] |
|
35228 | 444 |
(print_term is_pseudo_fun some_thm vars' NOBR body) |
31665 | 445 |
end |
35228 | 446 |
| print_case is_pseudo_fun some_thm vars fxy (((t, ty), clause :: clauses), _) = |
28054 | 447 |
let |
33992 | 448 |
fun print_select delim (pat, body) = |
28054 | 449 |
let |
35228 | 450 |
val (p, vars') = print_bind is_pseudo_fun some_thm NOBR pat vars; |
451 |
in concat [str delim, p, str "->", print_term is_pseudo_fun some_thm vars' NOBR body] end; |
|
28054 | 452 |
in |
31665 | 453 |
brackets ( |
28054 | 454 |
str "match" |
35228 | 455 |
:: print_term is_pseudo_fun some_thm vars NOBR t |
33992 | 456 |
:: print_select "with" clause |
457 |
:: map (print_select "|") clauses |
|
28054 | 458 |
) |
459 |
end |
|
35228 | 460 |
| print_case is_pseudo_fun some_thm vars fxy ((_, []), _) = |
31121 | 461 |
(concat o map str) ["failwith", "\"empty case\""]; |
33992 | 462 |
fun print_val_decl print_typscheme (name, typscheme) = concat |
463 |
[str "val", str (deresolve name), str ":", print_typscheme typscheme]; |
|
464 |
fun print_datatype_decl definer (tyco, (vs, cos)) = |
|
465 |
let |
|
37449 | 466 |
fun print_co ((co, _), []) = str (deresolve co) |
467 |
| 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
|
468 |
enum " *" "" "" (map (print_typ (INFX (2, X))) tys)]; |
33992 | 469 |
in |
470 |
concat ( |
|
471 |
str definer |
|
472 |
:: print_tyco_expr NOBR (tyco, map (ITyVar o fst) vs) |
|
473 |
:: str "=" |
|
474 |
:: separate (str "|") (map print_co cos) |
|
475 |
) |
|
476 |
end; |
|
477 |
fun print_def is_pseudo_fun needs_typ definer |
|
478 |
(ML_Function (name, (vs_ty as (vs, ty), eqs))) = |
|
28054 | 479 |
let |
35228 | 480 |
fun print_eqn ((ts, t), (some_thm, _)) = |
28054 | 481 |
let |
32913 | 482 |
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
|
483 |
val vars = reserved |
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
484 |
|> intro_vars ((fold o Code_Thingol.fold_varnames) |
28054 | 485 |
(insert (op =)) ts []); |
486 |
in concat [ |
|
38778 | 487 |
(Pretty.block o commas) |
35228 | 488 |
(map (print_term is_pseudo_fun some_thm vars NOBR) ts), |
28054 | 489 |
str "->", |
35228 | 490 |
print_term is_pseudo_fun some_thm vars NOBR t |
28054 | 491 |
] end; |
35228 | 492 |
fun print_eqns is_pseudo [((ts, t), (some_thm, _))] = |
28054 | 493 |
let |
32913 | 494 |
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
|
495 |
val vars = reserved |
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
496 |
|> intro_base_names |
38923 | 497 |
(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
|
498 |
|> intro_vars ((fold o Code_Thingol.fold_varnames) |
28054 | 499 |
(insert (op =)) ts []); |
500 |
in |
|
501 |
concat ( |
|
29189 | 502 |
(if is_pseudo then [str "()"] |
35228 | 503 |
else map (print_term is_pseudo_fun some_thm vars BR) ts) |
28054 | 504 |
@ str "=" |
35228 | 505 |
@@ print_term is_pseudo_fun some_thm vars NOBR t |
28054 | 506 |
) |
507 |
end |
|
33992 | 508 |
| print_eqns _ ((eq as (([_], _), _)) :: eqs) = |
28054 | 509 |
Pretty.block ( |
510 |
str "=" |
|
511 |
:: Pretty.brk 1 |
|
512 |
:: str "function" |
|
513 |
:: Pretty.brk 1 |
|
33992 | 514 |
:: print_eqn eq |
28054 | 515 |
:: maps (append [Pretty.fbrk, str "|", Pretty.brk 1] |
33992 | 516 |
o single o print_eqn) eqs |
28054 | 517 |
) |
33992 | 518 |
| print_eqns _ (eqs as eq :: eqs') = |
28054 | 519 |
let |
32913 | 520 |
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
|
521 |
val vars = reserved |
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
522 |
|> intro_base_names |
38923 | 523 |
(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
|
524 |
val dummy_parms = (map str o aux_params vars o map (fst o fst)) eqs; |
28054 | 525 |
in |
526 |
Pretty.block ( |
|
527 |
Pretty.breaks dummy_parms |
|
528 |
@ Pretty.brk 1 |
|
529 |
:: str "=" |
|
530 |
:: Pretty.brk 1 |
|
531 |
:: str "match" |
|
532 |
:: Pretty.brk 1 |
|
38778 | 533 |
:: (Pretty.block o commas) dummy_parms |
28054 | 534 |
:: Pretty.brk 1 |
535 |
:: str "with" |
|
536 |
:: Pretty.brk 1 |
|
33992 | 537 |
:: print_eqn eq |
28054 | 538 |
:: maps (append [Pretty.fbrk, str "|", Pretty.brk 1] |
33992 | 539 |
o single o print_eqn) eqs' |
28054 | 540 |
) |
541 |
end; |
|
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
542 |
val prolog = if needs_typ then |
33992 | 543 |
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
|
544 |
else (concat o map str) [definer, deresolve name]; |
33992 | 545 |
in pair |
546 |
(print_val_decl print_typscheme (name, vs_ty)) |
|
547 |
(concat ( |
|
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
548 |
prolog |
33992 | 549 |
:: print_dict_args vs |
550 |
@| print_eqns (is_pseudo_fun name) eqs |
|
551 |
)) |
|
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
552 |
end |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
553 |
| print_def is_pseudo_fun _ definer |
37449 | 554 |
(ML_Instance (inst, ((class, (tyco, vs)), (super_instances, (classparam_instances, _))))) = |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
555 |
let |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
556 |
fun print_super_instance (_, (classrel, dss)) = |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
557 |
concat [ |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
558 |
(str o deresolve) classrel, |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
559 |
str "=", |
33992 | 560 |
print_dict is_pseudo_fun NOBR (DictConst dss) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
561 |
]; |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
562 |
fun print_classparam_instance ((classparam, const), (thm, _)) = |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
563 |
concat [ |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
564 |
(str o deresolve) classparam, |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
565 |
str "=", |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
566 |
print_app (K false) (SOME thm) reserved NOBR (const, []) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
567 |
]; |
33992 | 568 |
in pair |
569 |
(print_val_decl print_dicttypscheme |
|
570 |
(inst, (vs, (class, tyco `%% map (ITyVar o fst) vs)))) |
|
571 |
(concat ( |
|
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
572 |
str definer |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
573 |
:: (str o deresolve) inst |
33992 | 574 |
:: print_dict_args vs |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
575 |
@ str "=" |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
576 |
@@ brackets [ |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
577 |
enum_default "()" ";" "{" "}" (map print_super_instance super_instances |
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
578 |
@ map print_classparam_instance classparam_instances), |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
579 |
str ":", |
33992 | 580 |
print_tyco_expr NOBR (class, [tyco `%% map (ITyVar o fst) vs]) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
581 |
] |
33992 | 582 |
)) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
583 |
end; |
33992 | 584 |
fun print_stmt (ML_Exc (name, (vs_ty, n))) = pair |
585 |
[print_val_decl print_typscheme (name, vs_ty)] |
|
586 |
((doublesemicolon o map str) ( |
|
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
587 |
"let" |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
588 |
:: deresolve name |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
589 |
:: replicate n "_" |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
590 |
@ "=" |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
591 |
:: "failwith" |
33992 | 592 |
@@ (ML_Syntax.print_string o Long_Name.base_name o Long_Name.qualifier) name |
593 |
)) |
|
594 |
| print_stmt (ML_Val binding) = |
|
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
595 |
let |
33992 | 596 |
val (sig_p, p) = print_def (K false) true "let" binding |
597 |
in pair |
|
598 |
[sig_p] |
|
599 |
(doublesemicolon [p]) |
|
600 |
end |
|
601 |
| print_stmt (ML_Funs (binding :: bindings, pseudo_funs)) = |
|
602 |
let |
|
603 |
val print_def' = print_def (member (op =) pseudo_funs) false; |
|
604 |
fun print_pseudo_fun name = concat [ |
|
29189 | 605 |
str "let", |
606 |
(str o deresolve) name, |
|
607 |
str "=", |
|
608 |
(str o deresolve) name, |
|
609 |
str "();;" |
|
610 |
]; |
|
33992 | 611 |
val (sig_ps, (ps, p)) = (apsnd split_last o split_list) |
612 |
(print_def' "let rec" binding :: map (print_def' "and") bindings); |
|
613 |
val pseudo_ps = map print_pseudo_fun pseudo_funs; |
|
614 |
in pair |
|
615 |
sig_ps |
|
616 |
(Pretty.chunks (ps @ doublesemicolon [p] :: pseudo_ps)) |
|
617 |
end |
|
618 |
| print_stmt (ML_Datas [(tyco, (vs, []))]) = |
|
619 |
let |
|
620 |
val ty_p = print_tyco_expr NOBR (tyco, map (ITyVar o fst) vs); |
|
621 |
in |
|
622 |
pair |
|
623 |
[concat [str "type", ty_p]] |
|
624 |
(concat [str "type", ty_p, str "=", str "EMPTY__"]) |
|
625 |
end |
|
626 |
| print_stmt (ML_Datas (data :: datas)) = |
|
28054 | 627 |
let |
33992 | 628 |
val sig_ps = print_datatype_decl "type" data |
629 |
:: map (print_datatype_decl "and") datas; |
|
630 |
val (ps, p) = split_last sig_ps; |
|
631 |
in pair |
|
632 |
sig_ps |
|
633 |
(Pretty.chunks (ps @| doublesemicolon [p])) |
|
634 |
end |
|
37447
ad3e04f289b6
transitive superclasses were also only a misunderstanding
haftmann
parents:
37446
diff
changeset
|
635 |
| print_stmt (ML_Class (class, (v, (super_classes, classparams)))) = |
28054 | 636 |
let |
33992 | 637 |
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
|
638 |
fun print_super_class_field (super_class, classrel) = |
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
639 |
print_field (deresolve classrel) (print_dicttyp (super_class, ITyVar v)); |
33992 | 640 |
fun print_classparam_decl (classparam, ty) = |
641 |
print_val_decl print_typscheme |
|
642 |
(classparam, ([(v, [class])], ty)); |
|
643 |
fun print_classparam_field (classparam, ty) = |
|
644 |
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
|
645 |
val w = "_" ^ first_upper v; |
33992 | 646 |
fun print_classparam_proj (classparam, _) = |
647 |
(concat o map str) ["let", deresolve classparam, w, "=", |
|
648 |
w ^ "." ^ deresolve classparam ^ ";;"]; |
|
649 |
val type_decl_p = concat [ |
|
650 |
str ("type '" ^ v), |
|
651 |
(str o deresolve) class, |
|
28054 | 652 |
str "=", |
33992 | 653 |
enum_default "unit" ";" "{" "}" ( |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
654 |
map print_super_class_field super_classes |
33992 | 655 |
@ map print_classparam_field classparams |
656 |
) |
|
28054 | 657 |
]; |
33992 | 658 |
in pair |
659 |
(type_decl_p :: map print_classparam_decl classparams) |
|
660 |
(Pretty.chunks ( |
|
661 |
doublesemicolon [type_decl_p] |
|
662 |
:: map print_classparam_proj classparams |
|
663 |
)) |
|
664 |
end; |
|
665 |
in print_stmt end; |
|
28054 | 666 |
|
38933
bd77e092f67c
avoid strange special treatment of empty module names
haftmann
parents:
38928
diff
changeset
|
667 |
fun print_ocaml_module name some_decls body = |
bd77e092f67c
avoid strange special treatment of empty module names
haftmann
parents:
38928
diff
changeset
|
668 |
Pretty.chunks2 ( |
33992 | 669 |
Pretty.chunks ( |
670 |
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
|
671 |
:: (the_list o Option.map (indent 2 o Pretty.chunks)) some_decls |
33992 | 672 |
@| (if is_some some_decls then str "end = struct" else str "struct") |
673 |
) |
|
674 |
:: body |
|
675 |
@| str ("end;; (*struct " ^ name ^ "*)") |
|
28054 | 676 |
); |
677 |
||
28064 | 678 |
val literals_ocaml = let |
679 |
fun chr i = |
|
680 |
let |
|
681 |
val xs = string_of_int i; |
|
682 |
val ys = replicate_string (3 - length (explode xs)) "0"; |
|
683 |
in "\\" ^ ys ^ xs end; |
|
684 |
fun char_ocaml c = |
|
685 |
let |
|
686 |
val i = ord c; |
|
687 |
val s = if i < 32 orelse i = 34 orelse i = 39 orelse i = 92 orelse i > 126 |
|
688 |
then chr i else c |
|
689 |
in s end; |
|
34944
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
690 |
fun numeral_ocaml k = if k < 0 |
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
691 |
then "(Big_int.minus_big_int " ^ numeral_ocaml (~ k) ^ ")" |
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
692 |
else if k <= 1073741823 |
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
693 |
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
|
694 |
else "(Big_int.big_int_of_string " ^ quote (string_of_int k) ^ ")" |
28064 | 695 |
in Literals { |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
696 |
literal_char = Library.enclose "'" "'" o char_ocaml, |
28064 | 697 |
literal_string = quote o translate_string char_ocaml, |
34944
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
698 |
literal_numeral = numeral_ocaml, |
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
699 |
literal_positive_numeral = numeral_ocaml, |
37958
9728342bcd56
another refinement chapter in the neverending numeral story
haftmann
parents:
37822
diff
changeset
|
700 |
literal_alternative_numeral = numeral_ocaml, |
34944
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
701 |
literal_naive_numeral = numeral_ocaml, |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
702 |
literal_list = enum ";" "[" "]", |
28064 | 703 |
infix_cons = (6, "::") |
704 |
} end; |
|
705 |
||
706 |
||
28054 | 707 |
|
708 |
(** SML/OCaml generic part **) |
|
709 |
||
710 |
local |
|
711 |
||
712 |
datatype ml_node = |
|
713 |
Dummy of string |
|
714 |
| Stmt of string * ml_stmt |
|
715 |
| Module of string * ((Name.context * Name.context) * ml_node Graph.T); |
|
716 |
||
717 |
in |
|
718 |
||
38924 | 719 |
fun ml_node_of_program labelled_name reserved module_alias program = |
28054 | 720 |
let |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
721 |
val reserved = Name.make_context reserved; |
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
722 |
val empty_module = ((reserved, reserved), Graph.empty); |
28054 | 723 |
fun map_node [] f = f |
724 |
| map_node (m::ms) f = |
|
725 |
Graph.default_node (m, Module (m, empty_module)) |
|
726 |
#> Graph.map_node m (fn (Module (module_name, (nsp, nodes))) => |
|
727 |
Module (module_name, (nsp, map_node ms f nodes))); |
|
728 |
fun map_nsp_yield [] f (nsp, nodes) = |
|
729 |
let |
|
730 |
val (x, nsp') = f nsp |
|
731 |
in (x, (nsp', nodes)) end |
|
732 |
| map_nsp_yield (m::ms) f (nsp, nodes) = |
|
733 |
let |
|
734 |
val (x, nodes') = |
|
735 |
nodes |
|
736 |
|> Graph.default_node (m, Module (m, empty_module)) |
|
737 |
|> Graph.map_node_yield m (fn Module (d_module_name, nsp_nodes) => |
|
738 |
let |
|
739 |
val (x, nsp_nodes') = map_nsp_yield ms f nsp_nodes |
|
740 |
in (x, Module (d_module_name, nsp_nodes')) end) |
|
741 |
in (x, (nsp, nodes')) end; |
|
742 |
fun map_nsp_fun_yield f (nsp_fun, nsp_typ) = |
|
743 |
let |
|
744 |
val (x, nsp_fun') = f nsp_fun |
|
745 |
in (x, (nsp_fun', nsp_typ)) end; |
|
746 |
fun map_nsp_typ_yield f (nsp_fun, nsp_typ) = |
|
747 |
let |
|
748 |
val (x, nsp_typ') = f nsp_typ |
|
749 |
in (x, (nsp_fun, nsp_typ')) end; |
|
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
750 |
val mk_name_module = mk_name_module reserved NONE module_alias program; |
28054 | 751 |
fun mk_name_stmt upper name nsp = |
752 |
let |
|
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
753 |
val (_, base) = dest_name name; |
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
754 |
val base' = if upper then first_upper base else base; |
28054 | 755 |
val ([base''], nsp') = Name.variants [base'] nsp; |
756 |
in (base'', nsp') end; |
|
37439 | 757 |
fun deps_of names = |
758 |
[] |
|
759 |
|> fold (fold (insert (op =)) o Graph.imm_succs program) names |
|
760 |
|> subtract (op =) names |
|
761 |
|> filter_out (Code_Thingol.is_case o Graph.get_node program); |
|
37437 | 762 |
fun ml_binding_of_stmt (name, Code_Thingol.Fun (_, ((tysm as (vs, ty), raw_eqs), _))) = |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
763 |
let |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
764 |
val eqs = filter (snd o snd) raw_eqs; |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
765 |
val (eqs', is_value) = if null (filter_out (null o snd) vs) then case eqs |
35228 | 766 |
of [(([], t), some_thm)] => if (not o null o fst o Code_Thingol.unfold_fun) ty |
767 |
then ([(([IVar (SOME "x")], t `$ IVar (SOME "x")), some_thm)], NONE) |
|
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
768 |
else (eqs, SOME (name, member (op =) (Code_Thingol.add_constnames t []) name)) |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
769 |
| _ => (eqs, NONE) |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
770 |
else (eqs, NONE) |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
771 |
in (ML_Function (name, (tysm, eqs')), is_value) end |
33992 | 772 |
| ml_binding_of_stmt (name, Code_Thingol.Classinst (stmt as ((_, (_, vs)), _))) = |
773 |
(ML_Instance (name, stmt), if forall (null o snd) vs then SOME (name, false) else NONE) |
|
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
774 |
| ml_binding_of_stmt (name, _) = |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
775 |
error ("Binding block containing illegal statement: " ^ labelled_name name) |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
776 |
fun add_fun (name, stmt) = |
29189 | 777 |
let |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
778 |
val (binding, some_value_name) = ml_binding_of_stmt (name, stmt); |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
779 |
val ml_stmt = case binding |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
780 |
of ML_Function (name, ((vs, ty), [])) => |
33992 | 781 |
ML_Exc (name, ((vs, ty), |
782 |
(length o filter_out (null o snd)) vs + (length o fst o Code_Thingol.unfold_fun) ty)) |
|
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
783 |
| _ => case some_value_name |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
784 |
of NONE => ML_Funs ([binding], []) |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
785 |
| SOME (name, true) => ML_Funs ([binding], [name]) |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
786 |
| SOME (name, false) => ML_Val binding |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
787 |
in |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
788 |
map_nsp_fun_yield (mk_name_stmt false name) |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
789 |
#>> (fn name' => ([name'], ml_stmt)) |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
790 |
end; |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
791 |
fun add_funs stmts = |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
792 |
let |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
793 |
val ml_stmt = ML_Funs (map_split ml_binding_of_stmt stmts |> (apsnd o map_filter o Option.map) fst); |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
794 |
in |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
795 |
fold_map (fn (name, _) => map_nsp_fun_yield (mk_name_stmt false name)) stmts |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
796 |
#>> rpair ml_stmt |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
797 |
end; |
28054 | 798 |
fun add_datatypes stmts = |
799 |
fold_map |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
800 |
(fn (name, Code_Thingol.Datatype (_, stmt)) => |
28054 | 801 |
map_nsp_typ_yield (mk_name_stmt false name) #>> rpair (SOME (name, stmt)) |
802 |
| (name, Code_Thingol.Datatypecons _) => |
|
803 |
map_nsp_fun_yield (mk_name_stmt true name) #>> rpair NONE |
|
804 |
| (name, _) => |
|
805 |
error ("Datatype block containing illegal statement: " ^ labelled_name name) |
|
806 |
) stmts |
|
807 |
#>> (split_list #> apsnd (map_filter I |
|
808 |
#> (fn [] => error ("Datatype block without data statement: " |
|
38778 | 809 |
^ (Library.commas o map (labelled_name o fst)) stmts) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
810 |
| stmts => ML_Datas stmts))); |
28054 | 811 |
fun add_class stmts = |
812 |
fold_map |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
813 |
(fn (name, Code_Thingol.Class (_, stmt)) => |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
814 |
map_nsp_typ_yield (mk_name_stmt false name) #>> rpair (SOME (name, stmt)) |
28054 | 815 |
| (name, Code_Thingol.Classrel _) => |
816 |
map_nsp_fun_yield (mk_name_stmt false name) #>> rpair NONE |
|
817 |
| (name, Code_Thingol.Classparam _) => |
|
818 |
map_nsp_fun_yield (mk_name_stmt false name) #>> rpair NONE |
|
819 |
| (name, _) => |
|
820 |
error ("Class block containing illegal statement: " ^ labelled_name name) |
|
821 |
) stmts |
|
822 |
#>> (split_list #> apsnd (map_filter I |
|
823 |
#> (fn [] => error ("Class block without class statement: " |
|
38778 | 824 |
^ (Library.commas o map (labelled_name o fst)) stmts) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
825 |
| [stmt] => ML_Class stmt))); |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
826 |
fun add_stmts ([stmt as (name, Code_Thingol.Fun _)]) = |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
827 |
add_fun stmt |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
828 |
| add_stmts ((stmts as (_, Code_Thingol.Fun _)::_)) = |
28054 | 829 |
add_funs stmts |
830 |
| add_stmts ((stmts as (_, Code_Thingol.Datatypecons _)::_)) = |
|
831 |
add_datatypes stmts |
|
832 |
| add_stmts ((stmts as (_, Code_Thingol.Datatype _)::_)) = |
|
833 |
add_datatypes stmts |
|
834 |
| add_stmts ((stmts as (_, Code_Thingol.Class _)::_)) = |
|
835 |
add_class stmts |
|
836 |
| add_stmts ((stmts as (_, Code_Thingol.Classrel _)::_)) = |
|
837 |
add_class stmts |
|
838 |
| add_stmts ((stmts as (_, Code_Thingol.Classparam _)::_)) = |
|
839 |
add_class stmts |
|
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
840 |
| add_stmts ([stmt as (_, Code_Thingol.Classinst _)]) = |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
841 |
add_fun stmt |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
842 |
| add_stmts ((stmts as (_, Code_Thingol.Classinst _)::_)) = |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
843 |
add_funs stmts |
28054 | 844 |
| add_stmts stmts = error ("Illegal mutual dependencies: " ^ |
38778 | 845 |
(Library.commas o map (labelled_name o fst)) stmts); |
28054 | 846 |
fun add_stmts' stmts nsp_nodes = |
847 |
let |
|
848 |
val names as (name :: names') = map fst stmts; |
|
37439 | 849 |
val deps = deps_of names; |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
850 |
val (module_names, _) = (split_list o map dest_name) names; |
28054 | 851 |
val module_name = (the_single o distinct (op =) o map mk_name_module) module_names |
852 |
handle Empty => |
|
853 |
error ("Different namespace prefixes for mutual dependencies:\n" |
|
38778 | 854 |
^ Library.commas (map labelled_name names) |
28054 | 855 |
^ "\n" |
38778 | 856 |
^ Library.commas module_names); |
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30280
diff
changeset
|
857 |
val module_name_path = Long_Name.explode module_name; |
28054 | 858 |
fun add_dep name name' = |
859 |
let |
|
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
860 |
val module_name' = (mk_name_module o fst o dest_name) name'; |
28054 | 861 |
in if module_name = module_name' then |
862 |
map_node module_name_path (Graph.add_edge (name, name')) |
|
863 |
else let |
|
28705 | 864 |
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
|
865 |
(module_name_path, Long_Name.explode module_name'); |
28054 | 866 |
in |
867 |
map_node common |
|
868 |
(fn node => Graph.add_edge_acyclic (diff1, diff2) node |
|
869 |
handle Graph.CYCLES _ => error ("Dependency " |
|
870 |
^ quote name ^ " -> " ^ quote name' |
|
871 |
^ " would result in module dependency cycle")) |
|
872 |
end end; |
|
873 |
in |
|
874 |
nsp_nodes |
|
875 |
|> map_nsp_yield module_name_path (add_stmts stmts) |
|
876 |
|-> (fn (base' :: bases', stmt') => |
|
877 |
apsnd (map_node module_name_path (Graph.new_node (name, (Stmt (base', stmt'))) |
|
878 |
#> fold2 (fn name' => fn base' => |
|
879 |
Graph.new_node (name', (Dummy base'))) names' bases'))) |
|
880 |
|> apsnd (fold (fn name => fold (add_dep name) deps) names) |
|
881 |
|> apsnd (fold_product (curry (map_node module_name_path o Graph.add_edge)) names names) |
|
882 |
end; |
|
37439 | 883 |
val stmts = map (AList.make (Graph.get_node program)) (rev (Graph.strong_conn program)) |
884 |
|> filter_out (fn [(_, stmt)] => Code_Thingol.is_case stmt | _ => false); |
|
885 |
val (_, nodes) = fold add_stmts' stmts empty_module; |
|
28054 | 886 |
fun deresolver prefix name = |
887 |
let |
|
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
888 |
val module_name = (fst o dest_name) name; |
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30280
diff
changeset
|
889 |
val module_name' = (Long_Name.explode o mk_name_module) module_name; |
28054 | 890 |
val (_, (_, remainder)) = chop_prefix (op =) (prefix, module_name'); |
891 |
val stmt_name = |
|
892 |
nodes |
|
893 |
|> fold (fn name => fn node => case Graph.get_node node name |
|
894 |
of Module (_, (_, node)) => node) module_name' |
|
895 |
|> (fn node => case Graph.get_node node name of Stmt (stmt_name, _) => stmt_name |
|
896 |
| Dummy stmt_name => stmt_name); |
|
897 |
in |
|
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30280
diff
changeset
|
898 |
Long_Name.implode (remainder @ [stmt_name]) |
28054 | 899 |
end handle Graph.UNDEF _ => |
900 |
error ("Unknown statement name: " ^ labelled_name name); |
|
901 |
in (deresolver, nodes) end; |
|
902 |
||
38926 | 903 |
fun serialize_ml target print_module print_stmt with_signatures { labelled_name, |
38928 | 904 |
reserved_syms, includes, module_alias, class_syntax, tyco_syntax, |
38926 | 905 |
const_syntax, program, names, presentation_names } = |
28054 | 906 |
let |
907 |
val is_cons = Code_Thingol.is_cons program; |
|
38926 | 908 |
val is_presentation = not (null presentation_names); |
38924 | 909 |
val (deresolver, nodes) = ml_node_of_program labelled_name |
38926 | 910 |
reserved_syms module_alias program; |
911 |
val reserved = make_vars reserved_syms; |
|
33992 | 912 |
fun print_node prefix (Dummy _) = |
28054 | 913 |
NONE |
33992 | 914 |
| print_node prefix (Stmt (_, stmt)) = if is_presentation andalso |
38926 | 915 |
(null o filter (member (op =) presentation_names) o stmt_names_of) stmt |
30962 | 916 |
then NONE |
38923 | 917 |
else SOME (print_stmt labelled_name tyco_syntax const_syntax reserved is_cons (deresolver prefix) stmt) |
33992 | 918 |
| print_node prefix (Module (module_name, (_, nodes))) = |
919 |
let |
|
920 |
val (decls, body) = print_nodes (prefix @ [module_name]) nodes; |
|
921 |
val p = if is_presentation then Pretty.chunks2 body |
|
922 |
else print_module module_name (if with_signatures then SOME decls else NONE) body; |
|
923 |
in SOME ([], p) end |
|
924 |
and print_nodes prefix nodes = (map_filter (print_node prefix o Graph.get_node nodes) |
|
925 |
o rev o flat o Graph.strong_conn) nodes |
|
926 |
|> split_list |
|
927 |
|> (fn (decls, body) => (flat decls, body)) |
|
38928 | 928 |
val names' = map (try (deresolver [])) names; |
33992 | 929 |
val p = Pretty.chunks2 (map snd includes @ snd (print_nodes [] nodes)); |
38915 | 930 |
fun write width NONE = writeln_pretty width |
931 |
| write width (SOME p) = File.write p o string_of_pretty width; |
|
28054 | 932 |
in |
38926 | 933 |
Code_Target.serialization write (fn width => (rpair names' o string_of_pretty width)) p |
28054 | 934 |
end; |
935 |
||
936 |
end; (*local*) |
|
937 |
||
38966 | 938 |
val serializer_sml : Code_Target.serializer = |
33992 | 939 |
Code_Target.parse_args (Scan.optional (Args.$$$ "no_signatures" >> K false) true |
940 |
>> (fn with_signatures => serialize_ml target_SML |
|
38924 | 941 |
print_sml_module print_sml_stmt with_signatures)); |
28054 | 942 |
|
38966 | 943 |
val serializer_ocaml : Code_Target.serializer = |
33992 | 944 |
Code_Target.parse_args (Scan.optional (Args.$$$ "no_signatures" >> K false) true |
37748 | 945 |
>> (fn with_signatures => serialize_ml target_OCaml |
38924 | 946 |
print_ocaml_module print_ocaml_stmt with_signatures)); |
28054 | 947 |
|
38966 | 948 |
|
949 |
(** Isar setup **) |
|
950 |
||
28054 | 951 |
val setup = |
37821 | 952 |
Code_Target.add_target |
38966 | 953 |
(target_SML, { serializer = serializer_sml, literals = literals_sml, |
37822
cf3588177676
use generic description slot for formal code checking
haftmann
parents:
37821
diff
changeset
|
954 |
check = { env_var = "ISABELLE_PROCESS", make_destination = fn p => Path.append p (Path.explode "ROOT.ML"), |
38863
9070a7c356c9
code checking: compiler invocation happens in same directory as generated file -- avoid problem with different path representations on cygwin
haftmann
parents:
38784
diff
changeset
|
955 |
make_command = fn isabelle => fn _ => isabelle ^ " -r -q -u Pure" } }) |
37821 | 956 |
#> Code_Target.add_target |
38966 | 957 |
(target_OCaml, { serializer = serializer_ocaml, literals = literals_ocaml, |
37822
cf3588177676
use generic description slot for formal code checking
haftmann
parents:
37821
diff
changeset
|
958 |
check = { env_var = "EXEC_OCAML", make_destination = fn p => Path.append p (Path.explode "ROOT.ocaml"), |
38863
9070a7c356c9
code checking: compiler invocation happens in same directory as generated file -- avoid problem with different path representations on cygwin
haftmann
parents:
38784
diff
changeset
|
959 |
make_command = fn ocaml => fn _ => ocaml ^ " -w pu nums.cma ROOT.ocaml" } }) |
38923 | 960 |
#> 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
|
961 |
brackify_infix (1, R) fxy ( |
33992 | 962 |
print_typ (INFX (1, X)) ty1, |
28054 | 963 |
str "->", |
33992 | 964 |
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
|
965 |
))) |
38923 | 966 |
#> 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
|
967 |
brackify_infix (1, R) fxy ( |
33992 | 968 |
print_typ (INFX (1, X)) ty1, |
28054 | 969 |
str "->", |
33992 | 970 |
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
|
971 |
))) |
28054 | 972 |
#> fold (Code_Target.add_reserved target_SML) ML_Syntax.reserved_names |
973 |
#> fold (Code_Target.add_reserved target_SML) |
|
38070 | 974 |
["ref" (*rebinding is illegal*), "o" (*dictionary projections use it already*), |
975 |
"Fail", "div", "mod" (*standard infixes*), "IntInf"] |
|
28054 | 976 |
#> fold (Code_Target.add_reserved target_OCaml) [ |
977 |
"and", "as", "assert", "begin", "class", |
|
978 |
"constraint", "do", "done", "downto", "else", "end", "exception", |
|
979 |
"external", "false", "for", "fun", "function", "functor", "if", |
|
980 |
"in", "include", "inherit", "initializer", "lazy", "let", "match", "method", |
|
981 |
"module", "mutable", "new", "object", "of", "open", "or", "private", "rec", |
|
982 |
"sig", "struct", "then", "to", "true", "try", "type", "val", |
|
983 |
"virtual", "when", "while", "with" |
|
984 |
] |
|
34944
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
985 |
#> fold (Code_Target.add_reserved target_OCaml) ["failwith", "mod", "Big_int"]; |
28054 | 986 |
|
987 |
end; (*struct*) |