author | wenzelm |
Mon, 24 Oct 2016 15:00:13 +0200 | |
changeset 64375 | 74a2af7c5145 |
parent 63303 | 7cffe366d333 |
child 66325 | fd28cb6e6f2c |
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 |
end; |
12 |
||
13 |
structure Code_ML : CODE_ML = |
|
14 |
struct |
|
15 |
||
55150 | 16 |
open Basic_Code_Symbol; |
28054 | 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) |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
31 |
| ML_Instance of (string * class) * { class: class, tyco: string, vs: (vname * sort) list, |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
32 |
superinsts: (class * dict list list) list, |
52519
598addf65209
explicit hint for domain of class parameters in instance statements
haftmann
parents:
52435
diff
changeset
|
33 |
inst_params: ((string * (const * int)) * (thm * bool)) list, |
598addf65209
explicit hint for domain of class parameters in instance statements
haftmann
parents:
52435
diff
changeset
|
34 |
superinst_params: ((string * (const * int)) * (thm * bool)) list }; |
28054 | 35 |
|
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
36 |
datatype ml_stmt = |
33992 | 37 |
ML_Exc of string * (typscheme * int) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
38 |
| ML_Val of ml_binding |
55684
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
39 |
| ML_Funs of (Code_Namespace.export * ml_binding) list * Code_Symbol.T list |
48003
1d11af40b106
dropped sort constraints on datatype specifications
haftmann
parents:
47609
diff
changeset
|
40 |
| ML_Datas of (string * (vname list * ((string * vname list) * itype list) list)) list |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
41 |
| ML_Class of string * (vname * ((class * class) list * (string * itype) list)); |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
42 |
|
33992 | 43 |
fun print_product _ [] = NONE |
44 |
| 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
|
45 |
| print_product print xs = (SOME o enum " *" "" "") (map print xs); |
28054 | 46 |
|
38922 | 47 |
fun tuplify _ _ [] = NONE |
48 |
| tuplify print fxy [x] = SOME (print fxy x) |
|
49 |
| tuplify print _ xs = SOME (enum "," "(" ")" (map (print NOBR) xs)); |
|
28054 | 50 |
|
33992 | 51 |
|
52 |
(** SML serializer **) |
|
53 |
||
59456
180555df34ea
string printing conformant to both (S)ML and Isabelle/ML
haftmann
parents:
59323
diff
changeset
|
54 |
fun print_char_any_ml s = |
180555df34ea
string printing conformant to both (S)ML and Isabelle/ML
haftmann
parents:
59323
diff
changeset
|
55 |
if Symbol.is_char s then ML_Syntax.print_char s else "\\092" ^ unprefix "\\" s; |
180555df34ea
string printing conformant to both (S)ML and Isabelle/ML
haftmann
parents:
59323
diff
changeset
|
56 |
|
180555df34ea
string printing conformant to both (S)ML and Isabelle/ML
haftmann
parents:
59323
diff
changeset
|
57 |
val print_string_any_ml = quote o implode o map print_char_any_ml o Symbol.explode; |
180555df34ea
string printing conformant to both (S)ML and Isabelle/ML
haftmann
parents:
59323
diff
changeset
|
58 |
|
50625 | 59 |
fun print_sml_stmt tyco_syntax const_syntax reserved is_constr deresolve = |
28054 | 60 |
let |
55150 | 61 |
val deresolve_const = deresolve o Constant; |
62 |
val deresolve_class = deresolve o Type_Class; |
|
63 |
val deresolve_classrel = deresolve o Class_Relation; |
|
64 |
val deresolve_inst = deresolve o Class_Instance; |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
65 |
fun print_tyco_expr (sym, []) = (str o deresolve) sym |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
66 |
| print_tyco_expr (sym, [ty]) = |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
67 |
concat [print_typ BR ty, (str o deresolve) sym] |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
68 |
| print_tyco_expr (sym, tys) = |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
69 |
concat [enum "," "(" ")" (map (print_typ BR) tys), (str o deresolve) sym] |
38923 | 70 |
and print_typ fxy (tyco `%% tys) = (case tyco_syntax tyco |
55150 | 71 |
of NONE => print_tyco_expr (Type_Constructor tyco, tys) |
47609 | 72 |
| SOME (_, print) => print print_typ fxy tys) |
33992 | 73 |
| print_typ fxy (ITyVar v) = str ("'" ^ v); |
55150 | 74 |
fun print_dicttyp (class, ty) = print_tyco_expr (Type_Class class, [ty]); |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
75 |
fun print_typscheme_prefix (vs, p) = enum " ->" "" "" |
33992 | 76 |
(map_filter (fn (v, sort) => |
77 |
(print_product (fn class => print_dicttyp (class, ITyVar v)) sort)) vs @| p); |
|
78 |
fun print_typscheme (vs, ty) = print_typscheme_prefix (vs, print_typ NOBR ty); |
|
79 |
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
|
80 |
fun print_classrels fxy [] ps = brackify fxy ps |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
81 |
| print_classrels fxy [classrel] ps = brackify fxy [(str o deresolve_classrel) classrel, brackify BR ps] |
41100
6c0940392fb4
dictionary constants must permit explicit weakening of classes;
haftmann
parents:
40627
diff
changeset
|
82 |
| print_classrels fxy classrels ps = |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
83 |
brackify fxy [enum " o" "(" ")" (map (str o deresolve_classrel) classrels), brackify BR ps] |
41118
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
84 |
fun print_dict is_pseudo_fun fxy (Dict (classrels, x)) = |
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
85 |
print_classrels fxy classrels (print_plain_dict is_pseudo_fun fxy x) |
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
86 |
and print_plain_dict is_pseudo_fun fxy (Dict_Const (inst, dss)) = |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
87 |
((str o deresolve_inst) inst :: |
55150 | 88 |
(if is_pseudo_fun (Class_Instance inst) then [str "()"] |
33992 | 89 |
else map_filter (print_dicts is_pseudo_fun BR) dss)) |
63303 | 90 |
| print_plain_dict is_pseudo_fun fxy (Dict_Var { var, index, length, ... }) = |
62539 | 91 |
[str (if length = 1 then Name.enforce_case true var ^ "_" |
92 |
else Name.enforce_case true var ^ string_of_int (index + 1) ^ "_")] |
|
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 |
63303 | 95 |
(map_index (fn (i, _) => Dict ([], |
96 |
Dict_Var { var = v, index = i, length = length sort, class = nth sort i, unique = true })) sort)); |
|
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
97 |
fun print_term is_pseudo_fun some_thm vars fxy (IConst const) = |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
98 |
print_app is_pseudo_fun some_thm vars fxy (const, []) |
35228 | 99 |
| print_term is_pseudo_fun some_thm vars fxy (IVar NONE) = |
31889 | 100 |
str "_" |
35228 | 101 |
| 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
|
102 |
str (lookup_var vars v) |
35228 | 103 |
| print_term is_pseudo_fun some_thm vars fxy (t as t1 `$ t2) = |
28054 | 104 |
(case Code_Thingol.unfold_const_app t |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
105 |
of SOME app => print_app is_pseudo_fun some_thm vars fxy app |
35228 | 106 |
| NONE => brackify fxy [print_term is_pseudo_fun some_thm vars NOBR t1, |
107 |
print_term is_pseudo_fun some_thm vars BR t2]) |
|
108 |
| print_term is_pseudo_fun some_thm vars fxy (t as _ `|=> _) = |
|
28054 | 109 |
let |
31874 | 110 |
val (binds, t') = Code_Thingol.unfold_pat_abs t; |
33992 | 111 |
fun print_abs (pat, ty) = |
35228 | 112 |
print_bind is_pseudo_fun some_thm NOBR pat |
28054 | 113 |
#>> (fn p => concat [str "fn", p, str "=>"]); |
33992 | 114 |
val (ps, vars') = fold_map print_abs binds vars; |
35228 | 115 |
in brackets (ps @ [print_term is_pseudo_fun some_thm vars' NOBR t']) end |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
116 |
| print_term is_pseudo_fun some_thm vars fxy (ICase case_expr) = |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
117 |
(case Code_Thingol.unfold_const_app (#primitive case_expr) |
55150 | 118 |
of SOME (app as ({ sym = Constant const, ... }, _)) => |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
119 |
if is_none (const_syntax const) |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
120 |
then print_case is_pseudo_fun some_thm vars fxy case_expr |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
121 |
else print_app is_pseudo_fun some_thm vars fxy app |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
122 |
| NONE => print_case is_pseudo_fun some_thm vars fxy case_expr) |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
123 |
and print_app_expr is_pseudo_fun some_thm vars (app as ({ sym, dicts = dss, dom = dom, ... }, ts)) = |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
124 |
if is_constr sym then |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
125 |
let val k = length dom in |
33992 | 126 |
if k < 2 orelse length ts = k |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
127 |
then (str o deresolve) sym |
38922 | 128 |
:: the_list (tuplify (print_term is_pseudo_fun some_thm vars) BR ts) |
35228 | 129 |
else [print_term is_pseudo_fun some_thm vars BR (Code_Thingol.eta_expand k app)] |
33992 | 130 |
end |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
131 |
else if is_pseudo_fun sym |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
132 |
then (str o deresolve) sym @@ str "()" |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
133 |
else (str o deresolve) sym :: map_filter (print_dicts is_pseudo_fun BR) dss |
35228 | 134 |
@ map (print_term is_pseudo_fun some_thm vars BR) ts |
135 |
and print_app is_pseudo_fun some_thm vars = gen_print_app (print_app_expr is_pseudo_fun) |
|
38923 | 136 |
(print_term is_pseudo_fun) const_syntax some_thm vars |
33992 | 137 |
and print_bind is_pseudo_fun = gen_print_bind (print_term is_pseudo_fun) |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
138 |
and print_case is_pseudo_fun some_thm vars fxy { clauses = [], ... } = |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
139 |
(concat o map str) ["raise", "Fail", "\"empty case\""] |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
140 |
| print_case is_pseudo_fun some_thm vars fxy (case_expr as { clauses = [_], ... }) = |
28054 | 141 |
let |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
142 |
val (binds, body) = Code_Thingol.unfold_let (ICase case_expr); |
47609 | 143 |
fun print_match ((pat, _), t) vars = |
28054 | 144 |
vars |
35228 | 145 |
|> print_bind is_pseudo_fun some_thm NOBR pat |
33992 | 146 |
|>> (fn p => semicolon [str "val", p, str "=", |
35228 | 147 |
print_term is_pseudo_fun some_thm vars NOBR t]) |
33992 | 148 |
val (ps, vars') = fold_map print_match binds vars; |
28054 | 149 |
in |
150 |
Pretty.chunks [ |
|
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
151 |
Pretty.block [str "let", Pretty.fbrk, Pretty.chunks ps], |
35228 | 152 |
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
|
153 |
str "end" |
28054 | 154 |
] |
155 |
end |
|
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
156 |
| print_case is_pseudo_fun some_thm vars fxy { term = t, typ = ty, clauses = clause :: clauses, ... } = |
28054 | 157 |
let |
33992 | 158 |
fun print_select delim (pat, body) = |
28054 | 159 |
let |
35228 | 160 |
val (p, vars') = print_bind is_pseudo_fun some_thm NOBR pat vars; |
28054 | 161 |
in |
35228 | 162 |
concat [str delim, p, str "=>", print_term is_pseudo_fun some_thm vars' NOBR body] |
28054 | 163 |
end; |
164 |
in |
|
31665 | 165 |
brackets ( |
28054 | 166 |
str "case" |
35228 | 167 |
:: print_term is_pseudo_fun some_thm vars NOBR t |
33992 | 168 |
:: print_select "of" clause |
169 |
:: map (print_select "|") clauses |
|
28054 | 170 |
) |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
171 |
end; |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
172 |
fun print_val_decl print_typscheme (sym, typscheme) = concat |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
173 |
[str "val", str (deresolve sym), str ":", print_typscheme typscheme]; |
33992 | 174 |
fun print_datatype_decl definer (tyco, (vs, cos)) = |
175 |
let |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
176 |
fun print_co ((co, _), []) = str (deresolve_const co) |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
177 |
| print_co ((co, _), tys) = concat [str (deresolve_const co), str "of", |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
178 |
enum " *" "" "" (map (print_typ (INFX (2, X))) tys)]; |
33992 | 179 |
in |
180 |
concat ( |
|
181 |
str definer |
|
55150 | 182 |
:: print_tyco_expr (Type_Constructor tyco, map ITyVar vs) |
33992 | 183 |
:: str "=" |
184 |
:: separate (str "|") (map print_co cos) |
|
185 |
) |
|
186 |
end; |
|
187 |
fun print_def is_pseudo_fun needs_typ definer |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
188 |
(ML_Function (const, (vs_ty as (vs, ty), eq :: eqs))) = |
29189 | 189 |
let |
35228 | 190 |
fun print_eqn definer ((ts, t), (some_thm, _)) = |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
191 |
let |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
192 |
val vars = reserved |
55145
2bb3cd36bcf7
more abstract declaration of unqualified constant names in code printing context
haftmann
parents:
52519
diff
changeset
|
193 |
|> intro_base_names_for (is_none o const_syntax) |
2bb3cd36bcf7
more abstract declaration of unqualified constant names in code printing context
haftmann
parents:
52519
diff
changeset
|
194 |
deresolve (t :: ts) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
195 |
|> intro_vars ((fold o Code_Thingol.fold_varnames) |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
196 |
(insert (op =)) ts []); |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
197 |
val prolog = if needs_typ then |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
198 |
concat [str definer, (str o deresolve_const) const, str ":", print_typ NOBR ty] |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
199 |
else (concat o map str) [definer, deresolve_const const]; |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
200 |
in |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
201 |
concat ( |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
202 |
prolog |
55150 | 203 |
:: (if is_pseudo_fun (Constant const) then [str "()"] |
33992 | 204 |
else print_dict_args vs |
35228 | 205 |
@ 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
|
206 |
@ str "=" |
35228 | 207 |
@@ print_term is_pseudo_fun some_thm vars NOBR t |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
208 |
) |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
209 |
end |
33992 | 210 |
val shift = if null eqs then I else |
211 |
map (Pretty.block o single o Pretty.block o single); |
|
212 |
in pair |
|
55150 | 213 |
(print_val_decl print_typscheme (Constant const, vs_ty)) |
33992 | 214 |
((Pretty.block o Pretty.fbreaks o shift) ( |
215 |
print_eqn definer eq |
|
216 |
:: map (print_eqn "|") eqs |
|
217 |
)) |
|
29189 | 218 |
end |
33992 | 219 |
| print_def is_pseudo_fun _ definer |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
220 |
(ML_Instance (inst as (tyco, class), { vs, superinsts, inst_params, ... })) = |
29189 | 221 |
let |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
222 |
fun print_super_instance (super_class, x) = |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
223 |
concat [ |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
224 |
(str o Long_Name.base_name o deresolve_classrel) (class, super_class), |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
225 |
str "=", |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
226 |
print_dict is_pseudo_fun NOBR (Dict ([], Dict_Const ((tyco, super_class), x))) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
227 |
]; |
52519
598addf65209
explicit hint for domain of class parameters in instance statements
haftmann
parents:
52435
diff
changeset
|
228 |
fun print_classparam_instance ((classparam, (const, _)), (thm, _)) = |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
229 |
concat [ |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
230 |
(str o Long_Name.base_name o deresolve_const) classparam, |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
231 |
str "=", |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
232 |
print_app (K false) (SOME thm) reserved NOBR (const, []) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
233 |
]; |
33992 | 234 |
in pair |
235 |
(print_val_decl print_dicttypscheme |
|
55150 | 236 |
(Class_Instance inst, (vs, (class, tyco `%% map (ITyVar o fst) vs)))) |
33992 | 237 |
(concat ( |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
238 |
str definer |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
239 |
:: (str o deresolve_inst) inst |
55150 | 240 |
:: (if is_pseudo_fun (Class_Instance inst) then [str "()"] |
33992 | 241 |
else print_dict_args vs) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
242 |
@ str "=" |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
243 |
:: enum "," "{" "}" |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
244 |
(map print_super_instance superinsts |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
245 |
@ map print_classparam_instance inst_params) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
246 |
:: str ":" |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
247 |
@@ print_dicttyp (class, tyco `%% map (ITyVar o fst) vs) |
33992 | 248 |
)) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
249 |
end; |
55681 | 250 |
fun print_stmt _ (ML_Exc (const, (vs_ty, n))) = pair |
55150 | 251 |
[print_val_decl print_typscheme (Constant const, vs_ty)] |
33992 | 252 |
((semicolon o map str) ( |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
253 |
(if n = 0 then "val" else "fun") |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
254 |
:: deresolve_const const |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
255 |
:: replicate n "_" |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
256 |
@ "=" |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
257 |
:: "raise" |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
258 |
:: "Fail" |
59456
180555df34ea
string printing conformant to both (S)ML and Isabelle/ML
haftmann
parents:
59323
diff
changeset
|
259 |
@@ print_string_any_ml const |
33992 | 260 |
)) |
55681 | 261 |
| print_stmt _ (ML_Val binding) = |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
262 |
let |
33992 | 263 |
val (sig_p, p) = print_def (K false) true "val" binding |
264 |
in pair |
|
265 |
[sig_p] |
|
266 |
(semicolon [p]) |
|
267 |
end |
|
55684
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
268 |
| print_stmt _ (ML_Funs ((export, binding) :: exports_bindings, pseudo_funs)) = |
33992 | 269 |
let |
270 |
val print_def' = print_def (member (op =) pseudo_funs) false; |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
271 |
fun print_pseudo_fun sym = concat [ |
29189 | 272 |
str "val", |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
273 |
(str o deresolve) sym, |
29189 | 274 |
str "=", |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
275 |
(str o deresolve) sym, |
29189 | 276 |
str "();" |
277 |
]; |
|
33992 | 278 |
val (sig_ps, (ps, p)) = (apsnd split_last o split_list) |
55684
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
279 |
(print_def' "fun" binding :: map (print_def' "and" o snd) exports_bindings); |
33992 | 280 |
val pseudo_ps = map print_pseudo_fun pseudo_funs; |
281 |
in pair |
|
55684
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
282 |
(map_filter (fn (export, p) => if Code_Namespace.not_private export then SOME p else NONE) |
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
283 |
((export :: map fst exports_bindings) ~~ sig_ps)) |
33992 | 284 |
(Pretty.chunks (ps @ semicolon [p] :: pseudo_ps)) |
285 |
end |
|
55681 | 286 |
| print_stmt _ (ML_Datas [(tyco, (vs, []))]) = |
33992 | 287 |
let |
55150 | 288 |
val ty_p = print_tyco_expr (Type_Constructor tyco, map ITyVar vs); |
33992 | 289 |
in |
290 |
pair |
|
291 |
[concat [str "type", ty_p]] |
|
55681 | 292 |
(semicolon [str "datatype", ty_p, str "=", str "EMPTY__"]) |
33992 | 293 |
end |
55681 | 294 |
| print_stmt export (ML_Datas (data :: datas)) = |
28054 | 295 |
let |
55681 | 296 |
val decl_ps = print_datatype_decl "datatype" data |
33992 | 297 |
:: map (print_datatype_decl "and") datas; |
55681 | 298 |
val (ps, p) = split_last decl_ps; |
33992 | 299 |
in pair |
55681 | 300 |
(if Code_Namespace.is_public export |
301 |
then decl_ps |
|
302 |
else map (fn (tyco, (vs, _)) => |
|
303 |
concat [str "type", print_tyco_expr (Type_Constructor tyco, map ITyVar vs)]) |
|
304 |
(data :: datas)) |
|
33992 | 305 |
(Pretty.chunks (ps @| semicolon [p])) |
306 |
end |
|
55681 | 307 |
| print_stmt export (ML_Class (class, (v, (classrels, classparams)))) = |
28054 | 308 |
let |
33992 | 309 |
fun print_field s p = concat [str s, str ":", p]; |
310 |
fun print_proj s p = semicolon |
|
311 |
(map str ["val", s, "=", "#" ^ s, ":"] @| p); |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
312 |
fun print_super_class_decl (classrel as (_, super_class)) = |
33992 | 313 |
print_val_decl print_dicttypscheme |
55150 | 314 |
(Class_Relation classrel, ([(v, [class])], (super_class, ITyVar v))); |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
315 |
fun print_super_class_field (classrel as (_, super_class)) = |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
316 |
print_field (deresolve_classrel classrel) (print_dicttyp (super_class, ITyVar v)); |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
317 |
fun print_super_class_proj (classrel as (_, super_class)) = |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
318 |
print_proj (deresolve_classrel classrel) |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
319 |
(print_dicttypscheme ([(v, [class])], (super_class, ITyVar v))); |
33992 | 320 |
fun print_classparam_decl (classparam, ty) = |
321 |
print_val_decl print_typscheme |
|
55150 | 322 |
(Constant classparam, ([(v, [class])], ty)); |
33992 | 323 |
fun print_classparam_field (classparam, ty) = |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
324 |
print_field (deresolve_const classparam) (print_typ NOBR ty); |
33992 | 325 |
fun print_classparam_proj (classparam, ty) = |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
326 |
print_proj (deresolve_const classparam) |
33992 | 327 |
(print_typscheme ([(v, [class])], ty)); |
328 |
in pair |
|
329 |
(concat [str "type", print_dicttyp (class, ITyVar v)] |
|
55681 | 330 |
:: (if Code_Namespace.is_public export |
331 |
then map print_super_class_decl classrels |
|
332 |
@ map print_classparam_decl classparams |
|
333 |
else [])) |
|
33992 | 334 |
(Pretty.chunks ( |
28054 | 335 |
concat [ |
55681 | 336 |
str "type", |
337 |
print_dicttyp (class, ITyVar v), |
|
28054 | 338 |
str "=", |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
339 |
enum "," "{" "};" ( |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
340 |
map print_super_class_field classrels |
33992 | 341 |
@ map print_classparam_field classparams |
28054 | 342 |
) |
343 |
] |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
344 |
:: map print_super_class_proj classrels |
33992 | 345 |
@ map print_classparam_proj classparams |
346 |
)) |
|
28054 | 347 |
end; |
33992 | 348 |
in print_stmt end; |
28054 | 349 |
|
55677 | 350 |
fun print_sml_module name decls body = |
38933
bd77e092f67c
avoid strange special treatment of empty module names
haftmann
parents:
38928
diff
changeset
|
351 |
Pretty.chunks2 ( |
55677 | 352 |
Pretty.chunks [ |
353 |
str ("structure " ^ name ^ " : sig"), |
|
354 |
(indent 2 o Pretty.chunks) decls, |
|
355 |
str "end = struct" |
|
356 |
] |
|
33992 | 357 |
:: body |
358 |
@| str ("end; (*struct " ^ name ^ "*)") |
|
28054 | 359 |
); |
360 |
||
28064 | 361 |
val literals_sml = Literals { |
362 |
literal_char = prefix "#" o quote o ML_Syntax.print_char, |
|
59456
180555df34ea
string printing conformant to both (S)ML and Isabelle/ML
haftmann
parents:
59323
diff
changeset
|
363 |
literal_string = print_string_any_ml, |
34944
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
364 |
literal_numeral = fn k => "(" ^ string_of_int k ^ " : IntInf.int)", |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
365 |
literal_list = enum "," "[" "]", |
28064 | 366 |
infix_cons = (7, "::") |
367 |
}; |
|
368 |
||
28054 | 369 |
|
370 |
(** OCaml serializer **) |
|
371 |
||
50625 | 372 |
fun print_ocaml_stmt tyco_syntax const_syntax reserved is_constr deresolve = |
28054 | 373 |
let |
55150 | 374 |
val deresolve_const = deresolve o Constant; |
375 |
val deresolve_class = deresolve o Type_Class; |
|
376 |
val deresolve_classrel = deresolve o Class_Relation; |
|
377 |
val deresolve_inst = deresolve o Class_Instance; |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
378 |
fun print_tyco_expr (sym, []) = (str o deresolve) sym |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
379 |
| print_tyco_expr (sym, [ty]) = |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
380 |
concat [print_typ BR ty, (str o deresolve) sym] |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
381 |
| print_tyco_expr (sym, tys) = |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
382 |
concat [enum "," "(" ")" (map (print_typ BR) tys), (str o deresolve) sym] |
38923 | 383 |
and print_typ fxy (tyco `%% tys) = (case tyco_syntax tyco |
55150 | 384 |
of NONE => print_tyco_expr (Type_Constructor tyco, tys) |
47576 | 385 |
| SOME (_, print) => print print_typ fxy tys) |
33992 | 386 |
| print_typ fxy (ITyVar v) = str ("'" ^ v); |
55150 | 387 |
fun print_dicttyp (class, ty) = print_tyco_expr (Type_Class class, [ty]); |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
388 |
fun print_typscheme_prefix (vs, p) = enum " ->" "" "" |
33992 | 389 |
(map_filter (fn (v, sort) => |
390 |
(print_product (fn class => print_dicttyp (class, ITyVar v)) sort)) vs @| p); |
|
391 |
fun print_typscheme (vs, ty) = print_typscheme_prefix (vs, print_typ NOBR ty); |
|
392 |
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
|
393 |
val print_classrels = |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
394 |
fold_rev (fn classrel => fn p => Pretty.block [p, str ".", (str o deresolve_classrel) classrel]) |
41118
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
395 |
fun print_dict is_pseudo_fun fxy (Dict (classrels, x)) = |
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
396 |
print_plain_dict is_pseudo_fun fxy x |
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
397 |
|> print_classrels classrels |
b290841cd3b0
separated dictionary weakning into separate type
haftmann
parents:
41100
diff
changeset
|
398 |
and print_plain_dict is_pseudo_fun fxy (Dict_Const (inst, dss)) = |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
399 |
brackify BR ((str o deresolve_inst) inst :: |
55150 | 400 |
(if is_pseudo_fun (Class_Instance inst) then [str "()"] |
33992 | 401 |
else map_filter (print_dicts is_pseudo_fun BR) dss)) |
63303 | 402 |
| print_plain_dict is_pseudo_fun fxy (Dict_Var { var, index, length, ... }) = |
62539 | 403 |
str (if length = 1 then "_" ^ Name.enforce_case true var |
404 |
else "_" ^ Name.enforce_case true var ^ string_of_int (index + 1)) |
|
38922 | 405 |
and print_dicts is_pseudo_fun = tuplify (print_dict is_pseudo_fun); |
33992 | 406 |
val print_dict_args = map_filter (fn (v, sort) => print_dicts (K false) BR |
63303 | 407 |
(map_index (fn (i, _) => Dict ([], |
408 |
Dict_Var { var = v, index = i, length = length sort, class = nth sort i, unique = true })) sort)); |
|
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
409 |
fun print_term is_pseudo_fun some_thm vars fxy (IConst const) = |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
410 |
print_app is_pseudo_fun some_thm vars fxy (const, []) |
35228 | 411 |
| print_term is_pseudo_fun some_thm vars fxy (IVar NONE) = |
31889 | 412 |
str "_" |
35228 | 413 |
| 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
|
414 |
str (lookup_var vars v) |
35228 | 415 |
| print_term is_pseudo_fun some_thm vars fxy (t as t1 `$ t2) = |
28054 | 416 |
(case Code_Thingol.unfold_const_app t |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
417 |
of SOME app => print_app is_pseudo_fun some_thm vars fxy app |
35228 | 418 |
| NONE => brackify fxy [print_term is_pseudo_fun some_thm vars NOBR t1, |
419 |
print_term is_pseudo_fun some_thm vars BR t2]) |
|
420 |
| print_term is_pseudo_fun some_thm vars fxy (t as _ `|=> _) = |
|
28054 | 421 |
let |
31874 | 422 |
val (binds, t') = Code_Thingol.unfold_pat_abs t; |
35228 | 423 |
val (ps, vars') = fold_map (print_bind is_pseudo_fun some_thm BR o fst) binds vars; |
424 |
in brackets (str "fun" :: ps @ str "->" @@ print_term is_pseudo_fun some_thm vars' NOBR t') end |
|
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
425 |
| print_term is_pseudo_fun some_thm vars fxy (ICase case_expr) = |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
426 |
(case Code_Thingol.unfold_const_app (#primitive case_expr) |
55150 | 427 |
of SOME (app as ({ sym = Constant const, ... }, _)) => |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
428 |
if is_none (const_syntax const) |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
429 |
then print_case is_pseudo_fun some_thm vars fxy case_expr |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
430 |
else print_app is_pseudo_fun some_thm vars fxy app |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
431 |
| NONE => print_case is_pseudo_fun some_thm vars fxy case_expr) |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
432 |
and print_app_expr is_pseudo_fun some_thm vars (app as ({ sym, dicts = dss, dom = dom, ... }, ts)) = |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
433 |
if is_constr sym then |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
434 |
let val k = length dom in |
33992 | 435 |
if length ts = k |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
436 |
then (str o deresolve) sym |
38922 | 437 |
:: the_list (tuplify (print_term is_pseudo_fun some_thm vars) BR ts) |
35228 | 438 |
else [print_term is_pseudo_fun some_thm vars BR (Code_Thingol.eta_expand k app)] |
33992 | 439 |
end |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
440 |
else if is_pseudo_fun sym |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
441 |
then (str o deresolve) sym @@ str "()" |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
442 |
else (str o deresolve) sym :: map_filter (print_dicts is_pseudo_fun BR) dss |
35228 | 443 |
@ map (print_term is_pseudo_fun some_thm vars BR) ts |
444 |
and print_app is_pseudo_fun some_thm vars = gen_print_app (print_app_expr is_pseudo_fun) |
|
38923 | 445 |
(print_term is_pseudo_fun) const_syntax some_thm vars |
33992 | 446 |
and print_bind is_pseudo_fun = gen_print_bind (print_term is_pseudo_fun) |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
447 |
and print_case is_pseudo_fun some_thm vars fxy { clauses = [], ... } = |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
448 |
(concat o map str) ["failwith", "\"empty case\""] |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
449 |
| print_case is_pseudo_fun some_thm vars fxy (case_expr as { clauses = [_], ... }) = |
28054 | 450 |
let |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
451 |
val (binds, body) = Code_Thingol.unfold_let (ICase case_expr); |
47576 | 452 |
fun print_let ((pat, _), t) vars = |
28054 | 453 |
vars |
35228 | 454 |
|> print_bind is_pseudo_fun some_thm NOBR pat |
28054 | 455 |
|>> (fn p => concat |
35228 | 456 |
[str "let", p, str "=", print_term is_pseudo_fun some_thm vars NOBR t, str "in"]) |
33992 | 457 |
val (ps, vars') = fold_map print_let binds vars; |
31665 | 458 |
in |
61129
774752af4a1f
parenthesing let-expressions in OCaml similar to case expressions avoids precendence problems due to ambiguous scope;
haftmann
parents:
59456
diff
changeset
|
459 |
brackets [Pretty.chunks ps, print_term is_pseudo_fun some_thm vars' NOBR body] |
31665 | 460 |
end |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
461 |
| print_case is_pseudo_fun some_thm vars fxy { term = t, typ = ty, clauses = clause :: clauses, ... } = |
28054 | 462 |
let |
33992 | 463 |
fun print_select delim (pat, body) = |
28054 | 464 |
let |
35228 | 465 |
val (p, vars') = print_bind is_pseudo_fun some_thm NOBR pat vars; |
466 |
in concat [str delim, p, str "->", print_term is_pseudo_fun some_thm vars' NOBR body] end; |
|
28054 | 467 |
in |
31665 | 468 |
brackets ( |
28054 | 469 |
str "match" |
35228 | 470 |
:: print_term is_pseudo_fun some_thm vars NOBR t |
33992 | 471 |
:: print_select "with" clause |
472 |
:: map (print_select "|") clauses |
|
28054 | 473 |
) |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
474 |
end; |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
475 |
fun print_val_decl print_typscheme (sym, typscheme) = concat |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
476 |
[str "val", str (deresolve sym), str ":", print_typscheme typscheme]; |
33992 | 477 |
fun print_datatype_decl definer (tyco, (vs, cos)) = |
478 |
let |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
479 |
fun print_co ((co, _), []) = str (deresolve_const co) |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
480 |
| print_co ((co, _), tys) = concat [str (deresolve_const co), str "of", |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
481 |
enum " *" "" "" (map (print_typ (INFX (2, X))) tys)]; |
33992 | 482 |
in |
483 |
concat ( |
|
484 |
str definer |
|
55150 | 485 |
:: print_tyco_expr (Type_Constructor tyco, map ITyVar vs) |
33992 | 486 |
:: str "=" |
487 |
:: separate (str "|") (map print_co cos) |
|
488 |
) |
|
489 |
end; |
|
490 |
fun print_def is_pseudo_fun needs_typ definer |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
491 |
(ML_Function (const, (vs_ty as (vs, ty), eqs))) = |
28054 | 492 |
let |
35228 | 493 |
fun print_eqn ((ts, t), (some_thm, _)) = |
28054 | 494 |
let |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
495 |
val vars = reserved |
55145
2bb3cd36bcf7
more abstract declaration of unqualified constant names in code printing context
haftmann
parents:
52519
diff
changeset
|
496 |
|> intro_base_names_for (is_none o const_syntax) |
2bb3cd36bcf7
more abstract declaration of unqualified constant names in code printing context
haftmann
parents:
52519
diff
changeset
|
497 |
deresolve (t :: ts) |
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 concat [ |
|
38778 | 501 |
(Pretty.block o commas) |
35228 | 502 |
(map (print_term is_pseudo_fun some_thm vars NOBR) ts), |
28054 | 503 |
str "->", |
35228 | 504 |
print_term is_pseudo_fun some_thm vars NOBR t |
28054 | 505 |
] end; |
35228 | 506 |
fun print_eqns is_pseudo [((ts, t), (some_thm, _))] = |
28054 | 507 |
let |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
508 |
val vars = reserved |
55145
2bb3cd36bcf7
more abstract declaration of unqualified constant names in code printing context
haftmann
parents:
52519
diff
changeset
|
509 |
|> intro_base_names_for (is_none o const_syntax) |
2bb3cd36bcf7
more abstract declaration of unqualified constant names in code printing context
haftmann
parents:
52519
diff
changeset
|
510 |
deresolve (t :: ts) |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
511 |
|> intro_vars ((fold o Code_Thingol.fold_varnames) |
28054 | 512 |
(insert (op =)) ts []); |
513 |
in |
|
514 |
concat ( |
|
29189 | 515 |
(if is_pseudo then [str "()"] |
35228 | 516 |
else map (print_term is_pseudo_fun some_thm vars BR) ts) |
28054 | 517 |
@ str "=" |
35228 | 518 |
@@ print_term is_pseudo_fun some_thm vars NOBR t |
28054 | 519 |
) |
520 |
end |
|
33992 | 521 |
| print_eqns _ ((eq as (([_], _), _)) :: eqs) = |
28054 | 522 |
Pretty.block ( |
523 |
str "=" |
|
524 |
:: Pretty.brk 1 |
|
525 |
:: str "function" |
|
526 |
:: Pretty.brk 1 |
|
33992 | 527 |
:: print_eqn eq |
28054 | 528 |
:: maps (append [Pretty.fbrk, str "|", Pretty.brk 1] |
33992 | 529 |
o single o print_eqn) eqs |
28054 | 530 |
) |
33992 | 531 |
| print_eqns _ (eqs as eq :: eqs') = |
28054 | 532 |
let |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
533 |
val vars = reserved |
55145
2bb3cd36bcf7
more abstract declaration of unqualified constant names in code printing context
haftmann
parents:
52519
diff
changeset
|
534 |
|> intro_base_names_for (is_none o const_syntax) |
2bb3cd36bcf7
more abstract declaration of unqualified constant names in code printing context
haftmann
parents:
52519
diff
changeset
|
535 |
deresolve (map (snd o fst) eqs) |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
536 |
val dummy_parms = (map str o aux_params vars o map (fst o fst)) eqs; |
28054 | 537 |
in |
538 |
Pretty.block ( |
|
539 |
Pretty.breaks dummy_parms |
|
540 |
@ Pretty.brk 1 |
|
541 |
:: str "=" |
|
542 |
:: Pretty.brk 1 |
|
543 |
:: str "match" |
|
544 |
:: Pretty.brk 1 |
|
38778 | 545 |
:: (Pretty.block o commas) dummy_parms |
28054 | 546 |
:: Pretty.brk 1 |
547 |
:: str "with" |
|
548 |
:: Pretty.brk 1 |
|
33992 | 549 |
:: print_eqn eq |
28054 | 550 |
:: maps (append [Pretty.fbrk, str "|", Pretty.brk 1] |
33992 | 551 |
o single o print_eqn) eqs' |
28054 | 552 |
) |
553 |
end; |
|
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
554 |
val prolog = if needs_typ then |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
555 |
concat [str definer, (str o deresolve_const) const, str ":", print_typ NOBR ty] |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
556 |
else (concat o map str) [definer, deresolve_const const]; |
33992 | 557 |
in pair |
55150 | 558 |
(print_val_decl print_typscheme (Constant const, vs_ty)) |
33992 | 559 |
(concat ( |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
560 |
prolog |
33992 | 561 |
:: print_dict_args vs |
55150 | 562 |
@| print_eqns (is_pseudo_fun (Constant const)) eqs |
33992 | 563 |
)) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
564 |
end |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
565 |
| print_def is_pseudo_fun _ definer |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
566 |
(ML_Instance (inst as (tyco, class), { vs, superinsts, inst_params, ... })) = |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
567 |
let |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
568 |
fun print_super_instance (super_class, x) = |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
569 |
concat [ |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
570 |
(str o deresolve_classrel) (class, super_class), |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
571 |
str "=", |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
572 |
print_dict is_pseudo_fun NOBR (Dict ([], Dict_Const ((tyco, super_class), x))) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
573 |
]; |
52519
598addf65209
explicit hint for domain of class parameters in instance statements
haftmann
parents:
52435
diff
changeset
|
574 |
fun print_classparam_instance ((classparam, (const, _)), (thm, _)) = |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
575 |
concat [ |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
576 |
(str o deresolve_const) classparam, |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
577 |
str "=", |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
578 |
print_app (K false) (SOME thm) reserved NOBR (const, []) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
579 |
]; |
33992 | 580 |
in pair |
581 |
(print_val_decl print_dicttypscheme |
|
55150 | 582 |
(Class_Instance inst, (vs, (class, tyco `%% map (ITyVar o fst) vs)))) |
33992 | 583 |
(concat ( |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
584 |
str definer |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
585 |
:: (str o deresolve_inst) inst |
55150 | 586 |
:: (if is_pseudo_fun (Class_Instance inst) then [str "()"] |
43343
e83695ea0e0a
resolving an issue with class instances that are pseudo functions in the OCaml code serializer
bulwahn
parents:
42599
diff
changeset
|
587 |
else print_dict_args vs) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
588 |
@ str "=" |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
589 |
@@ brackets [ |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
590 |
enum_default "()" ";" "{" "}" (map print_super_instance superinsts |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
591 |
@ map print_classparam_instance inst_params), |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
592 |
str ":", |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
593 |
print_dicttyp (class, tyco `%% map (ITyVar o fst) vs) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
594 |
] |
33992 | 595 |
)) |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
596 |
end; |
55681 | 597 |
fun print_stmt _ (ML_Exc (const, (vs_ty, n))) = pair |
55150 | 598 |
[print_val_decl print_typscheme (Constant const, vs_ty)] |
33992 | 599 |
((doublesemicolon o map str) ( |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
600 |
"let" |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
601 |
:: deresolve_const const |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
602 |
:: replicate n "_" |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
603 |
@ "=" |
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
604 |
:: "failwith" |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
605 |
@@ ML_Syntax.print_string const |
33992 | 606 |
)) |
55681 | 607 |
| print_stmt _ (ML_Val binding) = |
33636
a9bb3f063773
accomplish mutual recursion between fun and inst
haftmann
parents:
33519
diff
changeset
|
608 |
let |
33992 | 609 |
val (sig_p, p) = print_def (K false) true "let" binding |
610 |
in pair |
|
611 |
[sig_p] |
|
612 |
(doublesemicolon [p]) |
|
613 |
end |
|
55684
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
614 |
| print_stmt _ (ML_Funs ((export, binding) :: exports_bindings, pseudo_funs)) = |
33992 | 615 |
let |
616 |
val print_def' = print_def (member (op =) pseudo_funs) false; |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
617 |
fun print_pseudo_fun sym = concat [ |
29189 | 618 |
str "let", |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
619 |
(str o deresolve) sym, |
29189 | 620 |
str "=", |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
621 |
(str o deresolve) sym, |
29189 | 622 |
str "();;" |
623 |
]; |
|
33992 | 624 |
val (sig_ps, (ps, p)) = (apsnd split_last o split_list) |
55684
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
625 |
(print_def' "let rec" binding :: map (print_def' "and" o snd) exports_bindings); |
33992 | 626 |
val pseudo_ps = map print_pseudo_fun pseudo_funs; |
627 |
in pair |
|
55684
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
628 |
(map_filter (fn (export, p) => if Code_Namespace.not_private export then SOME p else NONE) |
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
629 |
((export :: map fst exports_bindings) ~~ sig_ps)) |
33992 | 630 |
(Pretty.chunks (ps @ doublesemicolon [p] :: pseudo_ps)) |
631 |
end |
|
55681 | 632 |
| print_stmt _ (ML_Datas [(tyco, (vs, []))]) = |
33992 | 633 |
let |
55150 | 634 |
val ty_p = print_tyco_expr (Type_Constructor tyco, map ITyVar vs); |
33992 | 635 |
in |
636 |
pair |
|
637 |
[concat [str "type", ty_p]] |
|
55681 | 638 |
(doublesemicolon [str "type", ty_p, str "=", str "EMPTY__"]) |
33992 | 639 |
end |
55681 | 640 |
| print_stmt export (ML_Datas (data :: datas)) = |
28054 | 641 |
let |
55681 | 642 |
val decl_ps = print_datatype_decl "type" data |
33992 | 643 |
:: map (print_datatype_decl "and") datas; |
55681 | 644 |
val (ps, p) = split_last decl_ps; |
33992 | 645 |
in pair |
55681 | 646 |
(if Code_Namespace.is_public export |
647 |
then decl_ps |
|
648 |
else map (fn (tyco, (vs, _)) => |
|
649 |
concat [str "type", print_tyco_expr (Type_Constructor tyco, map ITyVar vs)]) |
|
650 |
(data :: datas)) |
|
33992 | 651 |
(Pretty.chunks (ps @| doublesemicolon [p])) |
652 |
end |
|
55681 | 653 |
| print_stmt export (ML_Class (class, (v, (classrels, classparams)))) = |
28054 | 654 |
let |
33992 | 655 |
fun print_field s p = concat [str s, str ":", p]; |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
656 |
fun print_super_class_field (classrel as (_, super_class)) = |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
657 |
print_field (deresolve_classrel classrel) (print_dicttyp (super_class, ITyVar v)); |
33992 | 658 |
fun print_classparam_decl (classparam, ty) = |
659 |
print_val_decl print_typscheme |
|
55150 | 660 |
(Constant classparam, ([(v, [class])], ty)); |
33992 | 661 |
fun print_classparam_field (classparam, ty) = |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
662 |
print_field (deresolve_const classparam) (print_typ NOBR ty); |
56812 | 663 |
val w = "_" ^ Name.enforce_case true v; |
33992 | 664 |
fun print_classparam_proj (classparam, _) = |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
665 |
(concat o map str) ["let", deresolve_const classparam, w, "=", |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
666 |
w ^ "." ^ deresolve_const classparam ^ ";;"]; |
33992 | 667 |
val type_decl_p = concat [ |
55682 | 668 |
str "type", |
669 |
print_dicttyp (class, ITyVar v), |
|
28054 | 670 |
str "=", |
33992 | 671 |
enum_default "unit" ";" "{" "}" ( |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
672 |
map print_super_class_field classrels |
33992 | 673 |
@ map print_classparam_field classparams |
674 |
) |
|
28054 | 675 |
]; |
33992 | 676 |
in pair |
55684
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
677 |
(if Code_Namespace.is_public export |
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
678 |
then type_decl_p :: map print_classparam_decl classparams |
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
679 |
else [concat [str "type", print_dicttyp (class, ITyVar v)]]) |
33992 | 680 |
(Pretty.chunks ( |
681 |
doublesemicolon [type_decl_p] |
|
682 |
:: map print_classparam_proj classparams |
|
683 |
)) |
|
684 |
end; |
|
685 |
in print_stmt end; |
|
28054 | 686 |
|
55677 | 687 |
fun print_ocaml_module name decls body = |
38933
bd77e092f67c
avoid strange special treatment of empty module names
haftmann
parents:
38928
diff
changeset
|
688 |
Pretty.chunks2 ( |
55677 | 689 |
Pretty.chunks [ |
690 |
str ("module " ^ name ^ " : sig"), |
|
691 |
(indent 2 o Pretty.chunks) decls, |
|
692 |
str "end = struct" |
|
693 |
] |
|
33992 | 694 |
:: body |
695 |
@| str ("end;; (*struct " ^ name ^ "*)") |
|
28054 | 696 |
); |
697 |
||
28064 | 698 |
val literals_ocaml = let |
699 |
fun chr i = |
|
700 |
let |
|
701 |
val xs = string_of_int i; |
|
40627
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents:
39148
diff
changeset
|
702 |
val ys = replicate_string (3 - length (raw_explode xs)) "0"; |
28064 | 703 |
in "\\" ^ ys ^ xs end; |
704 |
fun char_ocaml c = |
|
705 |
let |
|
706 |
val i = ord c; |
|
707 |
val s = if i < 32 orelse i = 34 orelse i = 39 orelse i = 92 orelse i > 126 |
|
708 |
then chr i else c |
|
709 |
in s end; |
|
34944
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
710 |
fun numeral_ocaml k = if k < 0 |
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
711 |
then "(Big_int.minus_big_int " ^ numeral_ocaml (~ k) ^ ")" |
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
712 |
else if k <= 1073741823 |
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
713 |
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
|
714 |
else "(Big_int.big_int_of_string " ^ quote (string_of_int k) ^ ")" |
28064 | 715 |
in Literals { |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
716 |
literal_char = Library.enclose "'" "'" o char_ocaml, |
28064 | 717 |
literal_string = quote o translate_string char_ocaml, |
34944
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34246
diff
changeset
|
718 |
literal_numeral = numeral_ocaml, |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34071
diff
changeset
|
719 |
literal_list = enum ";" "[" "]", |
28064 | 720 |
infix_cons = (6, "::") |
721 |
} end; |
|
722 |
||
723 |
||
28054 | 724 |
|
725 |
(** SML/OCaml generic part **) |
|
726 |
||
55681 | 727 |
fun ml_program_of_program ctxt module_name reserved identifiers = |
39028
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
728 |
let |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
729 |
fun namify_const upper base (nsp_const, nsp_type) = |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
730 |
let |
56826
ba18bd41e510
enforce case of identifiers only to accomodate strict language requirements (or clear separation of constructors from variables in the case of SML)
haftmann
parents:
56812
diff
changeset
|
731 |
val (base', nsp_const') = Name.variant (Name.enforce_case upper base) nsp_const |
39028
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
732 |
in (base', (nsp_const', nsp_type)) end; |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
733 |
fun namify_type base (nsp_const, nsp_type) = |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
734 |
let |
56826
ba18bd41e510
enforce case of identifiers only to accomodate strict language requirements (or clear separation of constructors from variables in the case of SML)
haftmann
parents:
56812
diff
changeset
|
735 |
val (base', nsp_type') = Name.variant (Name.enforce_case false base) nsp_type |
39028
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
736 |
in (base', (nsp_const, nsp_type')) end; |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
737 |
fun namify_stmt (Code_Thingol.Fun _) = namify_const false |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
738 |
| namify_stmt (Code_Thingol.Datatype _) = namify_type |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
739 |
| namify_stmt (Code_Thingol.Datatypecons _) = namify_const true |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
740 |
| namify_stmt (Code_Thingol.Class _) = namify_type |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
741 |
| namify_stmt (Code_Thingol.Classrel _) = namify_const false |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
742 |
| namify_stmt (Code_Thingol.Classparam _) = namify_const false |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
743 |
| namify_stmt (Code_Thingol.Classinst _) = namify_const false; |
55684
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
744 |
fun ml_binding_of_stmt (sym as Constant const, (export, Code_Thingol.Fun ((tysm as (vs, ty), raw_eqs), _))) = |
39028
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
745 |
let |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
746 |
val eqs = filter (snd o snd) raw_eqs; |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
747 |
val (eqs', some_sym) = if null (filter_out (null o snd) vs) then case eqs |
39028
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
748 |
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
|
749 |
then ([(([IVar (SOME "x")], t `$ IVar (SOME "x")), some_thm)], NONE) |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
750 |
else (eqs, SOME (sym, member (op =) (Code_Thingol.add_constsyms t []) sym)) |
39028
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
751 |
| _ => (eqs, NONE) |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
752 |
else (eqs, NONE) |
55684
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
753 |
in ((export, ML_Function (const, (tysm, eqs'))), some_sym) end |
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
754 |
| ml_binding_of_stmt (sym as Class_Instance inst, (export, Code_Thingol.Classinst (stmt as { vs, ... }))) = |
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
755 |
((export, ML_Instance (inst, stmt)), |
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
756 |
if forall (null o snd) vs then SOME (sym, false) else NONE) |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
757 |
| ml_binding_of_stmt (sym, _) = |
52138
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
51143
diff
changeset
|
758 |
error ("Binding block containing illegal statement: " ^ |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
759 |
Code_Symbol.quote ctxt sym) |
55684
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
760 |
fun modify_fun (sym, (export, stmt)) = |
39028
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
761 |
let |
55684
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
762 |
val ((export', binding), some_value_sym) = ml_binding_of_stmt (sym, (export, stmt)); |
39028
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
763 |
val ml_stmt = case binding |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
764 |
of ML_Function (const, ((vs, ty), [])) => |
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
765 |
ML_Exc (const, ((vs, ty), |
39028
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
766 |
(length o filter_out (null o snd)) vs + (length o fst o Code_Thingol.unfold_fun) ty)) |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
767 |
| _ => case some_value_sym |
55684
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
768 |
of NONE => ML_Funs ([(export', binding)], []) |
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
769 |
| SOME (sym, true) => ML_Funs ([(export, binding)], [sym]) |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
770 |
| SOME (sym, false) => ML_Val binding |
55684
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
771 |
in SOME (export, ml_stmt) end; |
39031 | 772 |
fun modify_funs stmts = single (SOME |
55684
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
773 |
(Code_Namespace.Opaque, ML_Funs (map_split ml_binding_of_stmt stmts |> (apsnd o map_filter o Option.map) fst))) |
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
774 |
fun modify_datatypes stmts = |
63174
57c0d60e491c
do not export abstract constructors in code_reflect
haftmann
parents:
63024
diff
changeset
|
775 |
let |
57c0d60e491c
do not export abstract constructors in code_reflect
haftmann
parents:
63024
diff
changeset
|
776 |
val datas = map_filter |
57c0d60e491c
do not export abstract constructors in code_reflect
haftmann
parents:
63024
diff
changeset
|
777 |
(fn (Type_Constructor tyco, (export, Code_Thingol.Datatype stmt)) => SOME (export, (tyco, stmt)) | _ => NONE) stmts |
57c0d60e491c
do not export abstract constructors in code_reflect
haftmann
parents:
63024
diff
changeset
|
778 |
in |
57c0d60e491c
do not export abstract constructors in code_reflect
haftmann
parents:
63024
diff
changeset
|
779 |
if null datas then [] (*for abstract types wrt. code_reflect*) |
57c0d60e491c
do not export abstract constructors in code_reflect
haftmann
parents:
63024
diff
changeset
|
780 |
else datas |
57c0d60e491c
do not export abstract constructors in code_reflect
haftmann
parents:
63024
diff
changeset
|
781 |
|> split_list |
57c0d60e491c
do not export abstract constructors in code_reflect
haftmann
parents:
63024
diff
changeset
|
782 |
|> apfst Code_Namespace.join_exports |
57c0d60e491c
do not export abstract constructors in code_reflect
haftmann
parents:
63024
diff
changeset
|
783 |
|> apsnd ML_Datas |
57c0d60e491c
do not export abstract constructors in code_reflect
haftmann
parents:
63024
diff
changeset
|
784 |
|> SOME |
57c0d60e491c
do not export abstract constructors in code_reflect
haftmann
parents:
63024
diff
changeset
|
785 |
|> single |
57c0d60e491c
do not export abstract constructors in code_reflect
haftmann
parents:
63024
diff
changeset
|
786 |
end; |
55684
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
787 |
fun modify_class stmts = |
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
788 |
the_single (map_filter |
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
789 |
(fn (Type_Class class, (export, Code_Thingol.Class stmt)) => SOME (export, (class, stmt)) | _ => NONE) stmts) |
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
790 |
|> apsnd ML_Class |
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
791 |
|> SOME |
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
792 |
|> single; |
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
793 |
fun modify_stmts ([stmt as (_, (_, stmt' as Code_Thingol.Fun _))]) = |
39059
3a11a667af75
restored and added surpression of case combinators
haftmann
parents:
39058
diff
changeset
|
794 |
if Code_Thingol.is_case stmt' then [] else [modify_fun stmt] |
55684
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
795 |
| modify_stmts ((stmts as (_, (_, Code_Thingol.Fun _)) :: _)) = |
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
796 |
modify_funs (filter_out (Code_Thingol.is_case o snd o snd) stmts) |
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
797 |
| modify_stmts ((stmts as (_, (_, Code_Thingol.Datatypecons _)) :: _)) = |
39028
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
798 |
modify_datatypes stmts |
55684
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
799 |
| modify_stmts ((stmts as (_, (_, Code_Thingol.Datatype _)) :: _)) = |
39028
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
800 |
modify_datatypes stmts |
55684
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
801 |
| modify_stmts ((stmts as (_, (_, Code_Thingol.Class _)) :: _)) = |
39028
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
802 |
modify_class stmts |
55684
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
803 |
| modify_stmts ((stmts as (_, (_, Code_Thingol.Classrel _)) :: _)) = |
39028
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
804 |
modify_class stmts |
55684
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
805 |
| modify_stmts ((stmts as (_, (_, Code_Thingol.Classparam _)) :: _)) = |
39028
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
806 |
modify_class stmts |
55684
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
807 |
| modify_stmts ([stmt as (_, (_, Code_Thingol.Classinst _))]) = |
39028
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
808 |
[modify_fun stmt] |
55684
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
809 |
| modify_stmts ((stmts as (_, (_, Code_Thingol.Classinst _)) :: _)) = |
39028
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
810 |
modify_funs stmts |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
811 |
| modify_stmts stmts = error ("Illegal mutual dependencies: " ^ |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
812 |
(Library.commas o map (Code_Symbol.quote ctxt o fst)) stmts); |
39028
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
813 |
in |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
814 |
Code_Namespace.hierarchical_program ctxt { |
52138
e21426f244aa
bookkeeping and input syntax for exact specification of names of symbols in generated code
haftmann
parents:
51143
diff
changeset
|
815 |
module_name = module_name, reserved = reserved, identifiers = identifiers, |
39028
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
816 |
empty_nsp = (reserved, reserved), namify_module = pair, namify_stmt = namify_stmt, |
55776
7dd1971b39c1
amended some slips, rolling back currently dysfunctional export minimimalisation for Scala
haftmann
parents:
55684
diff
changeset
|
817 |
cyclic_modules = false, class_transitive = true, |
55684
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
818 |
class_relation_public = true, empty_data = (), |
ee49b4f7edc8
keep only identifiers public which are explicitly requested or demanded by dependencies
haftmann
parents:
55683
diff
changeset
|
819 |
memorize_data = K I, modify_stmts = modify_stmts } |
39028
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
820 |
end; |
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
821 |
|
55677 | 822 |
fun serialize_ml print_ml_module print_ml_stmt ctxt |
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
823 |
{ module_name, reserved_syms, identifiers, includes, |
55683 | 824 |
class_syntax, tyco_syntax, const_syntax } exports program = |
28054 | 825 |
let |
39147 | 826 |
|
827 |
(* build program *) |
|
39028
0dd6c5a0beef
use code_namespace module for SML and OCaml code
haftmann
parents:
38966
diff
changeset
|
828 |
val { deresolver, hierarchical_program = ml_program } = |
55681 | 829 |
ml_program_of_program ctxt module_name (Name.make_context reserved_syms) |
55683 | 830 |
identifiers exports program; |
39147 | 831 |
|
832 |
(* print statements *) |
|
55679 | 833 |
fun print_stmt prefix_fragments (_, (export, stmt)) = print_ml_stmt |
47576 | 834 |
tyco_syntax const_syntax (make_vars reserved_syms) |
55681 | 835 |
(Code_Thingol.is_constr program) (deresolver prefix_fragments) export stmt |
836 |
|> apfst (fn decl => if Code_Namespace.not_private export then SOME decl else NONE); |
|
39147 | 837 |
|
838 |
(* print modules *) |
|
47576 | 839 |
fun print_module _ base _ xs = |
39147 | 840 |
let |
841 |
val (raw_decls, body) = split_list xs; |
|
55677 | 842 |
val decls = maps these raw_decls |
39147 | 843 |
in (NONE, print_ml_module base decls body) end; |
844 |
||
845 |
(* serialization *) |
|
846 |
val p = Pretty.chunks2 (map snd includes |
|
847 |
@ map snd (Code_Namespace.print_hierarchical { |
|
848 |
print_module = print_module, print_stmt = print_stmt, |
|
849 |
lift_markup = apsnd } ml_program)); |
|
39056 | 850 |
fun write width NONE = writeln o format [] width |
851 |
| write width (SOME p) = File.write p o format [] width; |
|
55147
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
haftmann
parents:
55145
diff
changeset
|
852 |
fun prepare syms width p = ([("", format syms width p)], try (deresolver [])); |
28054 | 853 |
in |
48568 | 854 |
Code_Target.serialization write prepare p |
28054 | 855 |
end; |
856 |
||
38966 | 857 |
val serializer_sml : Code_Target.serializer = |
55677 | 858 |
Code_Target.parse_args (Scan.succeed ()) #> K (serialize_ml print_sml_module print_sml_stmt); |
28054 | 859 |
|
38966 | 860 |
val serializer_ocaml : Code_Target.serializer = |
55677 | 861 |
Code_Target.parse_args (Scan.succeed ()) #> K (serialize_ml print_ocaml_module print_ocaml_stmt); |
28054 | 862 |
|
38966 | 863 |
|
864 |
(** Isar setup **) |
|
865 |
||
52435
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52138
diff
changeset
|
866 |
fun fun_syntax print_typ fxy [ty1, ty2] = |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52138
diff
changeset
|
867 |
brackify_infix (1, R) fxy ( |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52138
diff
changeset
|
868 |
print_typ (INFX (1, X)) ty1, |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52138
diff
changeset
|
869 |
str "->", |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52138
diff
changeset
|
870 |
print_typ (INFX (1, R)) ty2 |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52138
diff
changeset
|
871 |
); |
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52138
diff
changeset
|
872 |
|
59323 | 873 |
val _ = Theory.setup |
874 |
(Code_Target.add_language |
|
38966 | 875 |
(target_SML, { serializer = serializer_sml, literals = literals_sml, |
63024
adeac19dd410
environment variable check has become pointless after 771b8ad5c7fc
haftmann
parents:
62966
diff
changeset
|
876 |
check = { env_var = "", |
41940 | 877 |
make_destination = fn p => Path.append p (Path.explode "ROOT.ML"), |
50022
286dfcab9833
restored SML code check which got unintentionally broken: must explicitly check for error during compilation;
haftmann
parents:
48568
diff
changeset
|
878 |
make_command = fn _ => |
62966 | 879 |
"isabelle process -e 'datatype ref = datatype Unsynchronized.ref' -f 'ROOT.ML' -l Pure" } }) |
59104 | 880 |
#> Code_Target.add_language |
38966 | 881 |
(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
|
882 |
check = { env_var = "ISABELLE_OCAML", |
41940 | 883 |
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
|
884 |
make_command = fn _ => "\"$ISABELLE_OCAML\" -w pu nums.cma ROOT.ocaml" } }) |
55150 | 885 |
#> Code_Target.set_printings (Type_Constructor ("fun", |
52435
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann
parents:
52138
diff
changeset
|
886 |
[(target_SML, SOME (2, fun_syntax)), (target_OCaml, SOME (2, fun_syntax))])) |
28054 | 887 |
#> fold (Code_Target.add_reserved target_SML) ML_Syntax.reserved_names |
888 |
#> fold (Code_Target.add_reserved target_SML) |
|
38070 | 889 |
["ref" (*rebinding is illegal*), "o" (*dictionary projections use it already*), |
890 |
"Fail", "div", "mod" (*standard infixes*), "IntInf"] |
|
28054 | 891 |
#> fold (Code_Target.add_reserved target_OCaml) [ |
892 |
"and", "as", "assert", "begin", "class", |
|
893 |
"constraint", "do", "done", "downto", "else", "end", "exception", |
|
894 |
"external", "false", "for", "fun", "function", "functor", "if", |
|
895 |
"in", "include", "inherit", "initializer", "lazy", "let", "match", "method", |
|
896 |
"module", "mutable", "new", "object", "of", "open", "or", "private", "rec", |
|
897 |
"sig", "struct", "then", "to", "true", "try", "type", "val", |
|
898 |
"virtual", "when", "while", "with" |
|
899 |
] |
|
59323 | 900 |
#> fold (Code_Target.add_reserved target_OCaml) ["failwith", "mod", "Big_int"]); |
28054 | 901 |
|
902 |
end; (*struct*) |