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