haftmann@24219
|
1 |
(* Title: Tools/code/code_target.ML
|
haftmann@24219
|
2 |
ID: $Id$
|
haftmann@24219
|
3 |
Author: Florian Haftmann, TU Muenchen
|
haftmann@24219
|
4 |
|
haftmann@24219
|
5 |
Serializer from intermediate language ("Thin-gol")
|
haftmann@24219
|
6 |
to target languages (like SML or Haskell).
|
haftmann@24219
|
7 |
*)
|
haftmann@24219
|
8 |
|
haftmann@24219
|
9 |
signature CODE_TARGET =
|
haftmann@24219
|
10 |
sig
|
haftmann@24219
|
11 |
include BASIC_CODE_THINGOL;
|
haftmann@24219
|
12 |
|
haftmann@24219
|
13 |
val add_syntax_class: string -> class
|
haftmann@24423
|
14 |
-> (string * (string * string) list) option -> theory -> theory;
|
haftmann@24219
|
15 |
val add_syntax_inst: string -> string * class -> bool -> theory -> theory;
|
haftmann@24219
|
16 |
val add_syntax_tycoP: string -> string -> OuterParse.token list
|
haftmann@24219
|
17 |
-> (theory -> theory) * OuterParse.token list;
|
haftmann@24219
|
18 |
val add_syntax_constP: string -> string -> OuterParse.token list
|
haftmann@24219
|
19 |
-> (theory -> theory) * OuterParse.token list;
|
haftmann@24219
|
20 |
|
haftmann@24219
|
21 |
val add_undefined: string -> string -> string -> theory -> theory;
|
haftmann@24219
|
22 |
val add_pretty_list: string -> string -> string -> theory -> theory;
|
haftmann@24219
|
23 |
val add_pretty_list_string: string -> string -> string
|
haftmann@24219
|
24 |
-> string -> string list -> theory -> theory;
|
haftmann@24219
|
25 |
val add_pretty_char: string -> string -> string list -> theory -> theory
|
huffman@26086
|
26 |
val add_pretty_numeral: string -> bool -> bool -> string -> string -> string
|
haftmann@24219
|
27 |
-> string -> string -> theory -> theory;
|
haftmann@24992
|
28 |
val add_pretty_message: string -> string -> string list -> string
|
haftmann@24219
|
29 |
-> string -> string -> theory -> theory;
|
haftmann@24219
|
30 |
|
haftmann@24841
|
31 |
val allow_exception: string -> theory -> theory;
|
haftmann@24841
|
32 |
|
haftmann@27000
|
33 |
type serialization;
|
haftmann@24219
|
34 |
type serializer;
|
haftmann@24219
|
35 |
val add_serializer: string * serializer -> theory -> theory;
|
haftmann@26998
|
36 |
val assert_serializer: theory -> string -> string;
|
haftmann@27000
|
37 |
val serialize: theory -> string -> bool -> string option -> Args.T list
|
haftmann@27000
|
38 |
-> CodeThingol.code -> string list option -> serialization;
|
haftmann@27000
|
39 |
val compile: serialization -> unit;
|
haftmann@27001
|
40 |
val write: serialization -> unit;
|
haftmann@27000
|
41 |
val file: Path.T -> serialization -> unit;
|
haftmann@27000
|
42 |
val string: serialization -> string;
|
haftmann@27000
|
43 |
val sml_of: theory -> CodeThingol.code -> string list -> string;
|
haftmann@26010
|
44 |
val eval: theory -> (string * (unit -> 'a) option ref)
|
haftmann@24918
|
45 |
-> CodeThingol.code -> CodeThingol.iterm * CodeThingol.itype -> string list -> 'a;
|
haftmann@27024
|
46 |
val target_code_width: int ref;
|
haftmann@24219
|
47 |
|
haftmann@24219
|
48 |
val setup: theory -> theory;
|
haftmann@24219
|
49 |
end;
|
haftmann@24219
|
50 |
|
haftmann@24219
|
51 |
structure CodeTarget : CODE_TARGET =
|
haftmann@24219
|
52 |
struct
|
haftmann@24219
|
53 |
|
haftmann@24219
|
54 |
open BasicCodeThingol;
|
haftmann@24219
|
55 |
|
haftmann@24219
|
56 |
(** basics **)
|
haftmann@24219
|
57 |
|
haftmann@24219
|
58 |
infixr 5 @@;
|
haftmann@24219
|
59 |
infixr 5 @|;
|
haftmann@24219
|
60 |
fun x @@ y = [x, y];
|
haftmann@24219
|
61 |
fun xs @| y = xs @ [y];
|
wenzelm@24634
|
62 |
val str = PrintMode.setmp [] Pretty.str;
|
haftmann@24219
|
63 |
val concat = Pretty.block o Pretty.breaks;
|
haftmann@24219
|
64 |
val brackets = Pretty.enclose "(" ")" o Pretty.breaks;
|
haftmann@24219
|
65 |
fun semicolon ps = Pretty.block [concat ps, str ";"];
|
haftmann@25771
|
66 |
fun enum_default default sep opn cls [] = str default
|
haftmann@25771
|
67 |
| enum_default default sep opn cls xs = Pretty.enum sep opn cls xs;
|
haftmann@24219
|
68 |
|
haftmann@27024
|
69 |
datatype destination = Compile | Write | File of Path.T | String;
|
haftmann@27024
|
70 |
type serialization = destination -> string option;
|
haftmann@27014
|
71 |
|
haftmann@27024
|
72 |
val target_code_width = ref 80; (*FIXME after Pretty module no longer depends on print mode*)
|
haftmann@27024
|
73 |
fun target_code_setmp f = PrintMode.setmp [] (Pretty.setmp_margin (!target_code_width) f);
|
haftmann@27024
|
74 |
fun target_code_of_pretty p = target_code_setmp Pretty.string_of p ^ "\n";
|
haftmann@27024
|
75 |
fun target_code_writeln p = Pretty.setmp_margin (!target_code_width) Pretty.writeln p;
|
haftmann@26998
|
76 |
|
haftmann@27024
|
77 |
(*FIXME why another target_code_setmp?*)
|
haftmann@27024
|
78 |
fun compile f = (target_code_setmp f Compile; ());
|
haftmann@27024
|
79 |
fun write f = (target_code_setmp f Write; ());
|
haftmann@27024
|
80 |
fun file p f = (target_code_setmp f (File p); ());
|
haftmann@27024
|
81 |
fun string f = the (target_code_setmp f String);
|
haftmann@27014
|
82 |
|
haftmann@24219
|
83 |
|
haftmann@27000
|
84 |
(** generic syntax **)
|
haftmann@24219
|
85 |
|
haftmann@24219
|
86 |
datatype lrx = L | R | X;
|
haftmann@24219
|
87 |
|
haftmann@24219
|
88 |
datatype fixity =
|
haftmann@24219
|
89 |
BR
|
haftmann@24219
|
90 |
| NOBR
|
haftmann@24219
|
91 |
| INFX of (int * lrx);
|
haftmann@24219
|
92 |
|
haftmann@24219
|
93 |
val APP = INFX (~1, L);
|
haftmann@24219
|
94 |
|
haftmann@26010
|
95 |
fun fixity_lrx L L = false
|
haftmann@26010
|
96 |
| fixity_lrx R R = false
|
haftmann@26010
|
97 |
| fixity_lrx _ _ = true;
|
haftmann@24219
|
98 |
|
haftmann@27024
|
99 |
fun fixity NOBR _ = false
|
haftmann@27024
|
100 |
| fixity _ NOBR = false
|
haftmann@26010
|
101 |
| fixity (INFX (pr, lr)) (INFX (pr_ctxt, lr_ctxt)) =
|
haftmann@24219
|
102 |
pr < pr_ctxt
|
haftmann@24219
|
103 |
orelse pr = pr_ctxt
|
haftmann@26010
|
104 |
andalso fixity_lrx lr lr_ctxt
|
haftmann@24219
|
105 |
orelse pr_ctxt = ~1
|
haftmann@27024
|
106 |
| fixity BR (INFX _) = false
|
haftmann@26010
|
107 |
| fixity _ _ = true;
|
haftmann@24219
|
108 |
|
haftmann@24219
|
109 |
fun gen_brackify _ [p] = p
|
haftmann@24219
|
110 |
| gen_brackify true (ps as _::_) = Pretty.enclose "(" ")" ps
|
haftmann@24219
|
111 |
| gen_brackify false (ps as _::_) = Pretty.block ps;
|
haftmann@24219
|
112 |
|
haftmann@27024
|
113 |
fun brackify fxy_ctxt =
|
haftmann@27024
|
114 |
gen_brackify (fixity BR fxy_ctxt) o Pretty.breaks;
|
haftmann@24219
|
115 |
|
haftmann@27024
|
116 |
fun brackify_infix infx fxy_ctxt =
|
haftmann@27024
|
117 |
gen_brackify (fixity (INFX infx) fxy_ctxt) o Pretty.breaks;
|
haftmann@24219
|
118 |
|
haftmann@24219
|
119 |
type class_syntax = string * (string -> string option);
|
haftmann@24219
|
120 |
type typ_syntax = int * ((fixity -> itype -> Pretty.T)
|
haftmann@24219
|
121 |
-> fixity -> itype list -> Pretty.T);
|
haftmann@24219
|
122 |
type term_syntax = int * ((CodeName.var_ctxt -> fixity -> iterm -> Pretty.T)
|
haftmann@24219
|
123 |
-> CodeName.var_ctxt -> fixity -> (iterm * itype) list -> Pretty.T);
|
haftmann@24219
|
124 |
|
haftmann@24219
|
125 |
|
haftmann@27000
|
126 |
(** theory data **)
|
haftmann@24219
|
127 |
|
haftmann@27000
|
128 |
val target_diag = "diag";
|
haftmann@27000
|
129 |
val target_SML = "SML";
|
haftmann@27000
|
130 |
val target_OCaml = "OCaml";
|
haftmann@27000
|
131 |
val target_Haskell = "Haskell";
|
haftmann@24219
|
132 |
|
haftmann@27024
|
133 |
datatype name_syntax_table = NameSyntaxTable of {
|
haftmann@27000
|
134 |
class: (string * (string -> string option)) Symtab.table,
|
haftmann@27000
|
135 |
inst: unit Symtab.table,
|
haftmann@27000
|
136 |
tyco: typ_syntax Symtab.table,
|
haftmann@27000
|
137 |
const: term_syntax Symtab.table
|
haftmann@27000
|
138 |
};
|
haftmann@27000
|
139 |
|
haftmann@27024
|
140 |
fun mk_name_syntax_table ((class, inst), (tyco, const)) =
|
haftmann@27024
|
141 |
NameSyntaxTable { class = class, inst = inst, tyco = tyco, const = const };
|
haftmann@27024
|
142 |
fun map_name_syntax_table f (NameSyntaxTable { class, inst, tyco, const }) =
|
haftmann@27024
|
143 |
mk_name_syntax_table (f ((class, inst), (tyco, const)));
|
haftmann@27024
|
144 |
fun merge_name_syntax_table (NameSyntaxTable { class = class1, inst = inst1, tyco = tyco1, const = const1 },
|
haftmann@27024
|
145 |
NameSyntaxTable { class = class2, inst = inst2, tyco = tyco2, const = const2 }) =
|
haftmann@27024
|
146 |
mk_name_syntax_table (
|
haftmann@27000
|
147 |
(Symtab.join (K snd) (class1, class2),
|
haftmann@27000
|
148 |
Symtab.join (K snd) (inst1, inst2)),
|
haftmann@27000
|
149 |
(Symtab.join (K snd) (tyco1, tyco2),
|
haftmann@27000
|
150 |
Symtab.join (K snd) (const1, const2))
|
haftmann@27000
|
151 |
);
|
haftmann@27000
|
152 |
|
haftmann@27024
|
153 |
type serializer = (*FIXME order of arguments*)
|
haftmann@27024
|
154 |
string option (*module name*)
|
haftmann@27024
|
155 |
-> Args.T list (*arguments*)
|
haftmann@27024
|
156 |
-> (string -> string) (*labelled_name*)
|
haftmann@27024
|
157 |
-> string list (*reserved symbols*)
|
haftmann@27024
|
158 |
-> (string * Pretty.T) list (*includes*)
|
haftmann@27024
|
159 |
-> (string -> string option) (*module aliasses*)
|
haftmann@27000
|
160 |
-> (string -> class_syntax option)
|
haftmann@27000
|
161 |
-> (string -> typ_syntax option)
|
haftmann@27000
|
162 |
-> (string -> term_syntax option)
|
haftmann@27024
|
163 |
-> CodeThingol.code (*program*)
|
haftmann@27024
|
164 |
-> string list (*selected statements*)
|
haftmann@27024
|
165 |
-> serialization;
|
haftmann@27000
|
166 |
|
haftmann@27000
|
167 |
datatype target = Target of {
|
haftmann@27000
|
168 |
serial: serial,
|
haftmann@27000
|
169 |
serializer: serializer,
|
haftmann@27000
|
170 |
reserved: string list,
|
haftmann@27000
|
171 |
includes: Pretty.T Symtab.table,
|
haftmann@27024
|
172 |
name_syntax_table: name_syntax_table,
|
haftmann@27000
|
173 |
module_alias: string Symtab.table
|
haftmann@27000
|
174 |
};
|
haftmann@27000
|
175 |
|
haftmann@27024
|
176 |
fun mk_target ((serial, serializer), ((reserved, includes), (name_syntax_table, module_alias))) =
|
haftmann@27000
|
177 |
Target { serial = serial, serializer = serializer, reserved = reserved,
|
haftmann@27024
|
178 |
includes = includes, name_syntax_table = name_syntax_table, module_alias = module_alias };
|
haftmann@27024
|
179 |
fun map_target f ( Target { serial, serializer, reserved, includes, name_syntax_table, module_alias } ) =
|
haftmann@27024
|
180 |
mk_target (f ((serial, serializer), ((reserved, includes), (name_syntax_table, module_alias))));
|
haftmann@27000
|
181 |
fun merge_target target (Target { serial = serial1, serializer = serializer,
|
haftmann@27000
|
182 |
reserved = reserved1, includes = includes1,
|
haftmann@27024
|
183 |
name_syntax_table = name_syntax_table1, module_alias = module_alias1 },
|
haftmann@27000
|
184 |
Target { serial = serial2, serializer = _,
|
haftmann@27000
|
185 |
reserved = reserved2, includes = includes2,
|
haftmann@27024
|
186 |
name_syntax_table = name_syntax_table2, module_alias = module_alias2 }) =
|
haftmann@27000
|
187 |
if serial1 = serial2 then
|
haftmann@27000
|
188 |
mk_target ((serial1, serializer),
|
haftmann@27000
|
189 |
((merge (op =) (reserved1, reserved2), Symtab.merge (op =) (includes1, includes2)),
|
haftmann@27024
|
190 |
(merge_name_syntax_table (name_syntax_table1, name_syntax_table2),
|
haftmann@27000
|
191 |
Symtab.join (K snd) (module_alias1, module_alias2))
|
haftmann@27000
|
192 |
))
|
haftmann@27000
|
193 |
else
|
haftmann@27000
|
194 |
error ("Incompatible serializers: " ^ quote target);
|
haftmann@27000
|
195 |
|
haftmann@27000
|
196 |
structure CodeTargetData = TheoryDataFun
|
haftmann@27000
|
197 |
(
|
haftmann@27000
|
198 |
type T = target Symtab.table * string list;
|
haftmann@27000
|
199 |
val empty = (Symtab.empty, []);
|
haftmann@27000
|
200 |
val copy = I;
|
haftmann@27000
|
201 |
val extend = I;
|
haftmann@27000
|
202 |
fun merge _ ((target1, exc1) : T, (target2, exc2)) =
|
haftmann@27000
|
203 |
(Symtab.join merge_target (target1, target2), Library.merge (op =) (exc1, exc2));
|
haftmann@27000
|
204 |
);
|
haftmann@27000
|
205 |
|
haftmann@27000
|
206 |
fun the_serializer (Target { serializer, ... }) = serializer;
|
haftmann@27000
|
207 |
fun the_reserved (Target { reserved, ... }) = reserved;
|
haftmann@27000
|
208 |
fun the_includes (Target { includes, ... }) = includes;
|
haftmann@27024
|
209 |
fun the_name_syntax (Target { name_syntax_table = NameSyntaxTable x, ... }) = x;
|
haftmann@27000
|
210 |
fun the_module_alias (Target { module_alias , ... }) = module_alias;
|
haftmann@27000
|
211 |
|
haftmann@27000
|
212 |
fun assert_serializer thy target =
|
haftmann@27000
|
213 |
case Symtab.lookup (fst (CodeTargetData.get thy)) target
|
haftmann@27000
|
214 |
of SOME data => target
|
haftmann@27000
|
215 |
| NONE => error ("Unknown code target language: " ^ quote target);
|
haftmann@27000
|
216 |
|
haftmann@27000
|
217 |
fun add_serializer (target, seri) thy =
|
haftmann@24219
|
218 |
let
|
haftmann@27000
|
219 |
val _ = case Symtab.lookup (fst (CodeTargetData.get thy)) target
|
haftmann@27024
|
220 |
of SOME _ => warning ("Overwriting existing serializer " ^ quote target)
|
haftmann@27000
|
221 |
| NONE => ();
|
haftmann@24219
|
222 |
in
|
haftmann@27000
|
223 |
thy
|
haftmann@27000
|
224 |
|> (CodeTargetData.map o apfst oo Symtab.map_default)
|
haftmann@27000
|
225 |
(target, mk_target ((serial (), seri), (([], Symtab.empty),
|
haftmann@27024
|
226 |
(mk_name_syntax_table ((Symtab.empty, Symtab.empty), (Symtab.empty, Symtab.empty)),
|
haftmann@27000
|
227 |
Symtab.empty))))
|
haftmann@27000
|
228 |
((map_target o apfst o apsnd o K) seri)
|
haftmann@24219
|
229 |
end;
|
haftmann@24219
|
230 |
|
haftmann@27000
|
231 |
fun map_seri_data target f thy =
|
haftmann@24219
|
232 |
let
|
haftmann@27000
|
233 |
val _ = assert_serializer thy target;
|
haftmann@24219
|
234 |
in
|
haftmann@27000
|
235 |
thy
|
haftmann@27000
|
236 |
|> (CodeTargetData.map o apfst o Symtab.map_entry target o map_target) f
|
haftmann@24219
|
237 |
end;
|
haftmann@24219
|
238 |
|
haftmann@27000
|
239 |
fun map_adaptions target =
|
haftmann@27000
|
240 |
map_seri_data target o apsnd o apfst;
|
haftmann@27024
|
241 |
fun map_name_syntax target =
|
haftmann@27024
|
242 |
map_seri_data target o apsnd o apsnd o apfst o map_name_syntax_table;
|
haftmann@27000
|
243 |
fun map_module_alias target =
|
haftmann@27000
|
244 |
map_seri_data target o apsnd o apsnd o apsnd;
|
haftmann@27000
|
245 |
|
haftmann@27000
|
246 |
fun get_serializer get_seri thy target permissive =
|
haftmann@24219
|
247 |
let
|
haftmann@27000
|
248 |
val (seris, exc) = CodeTargetData.get thy;
|
haftmann@27000
|
249 |
val data = case Symtab.lookup seris target
|
haftmann@27000
|
250 |
of SOME data => data
|
haftmann@27000
|
251 |
| NONE => error ("Unknown code target language: " ^ quote target);
|
haftmann@27000
|
252 |
val seri = get_seri data;
|
haftmann@27000
|
253 |
val reserved = the_reserved data;
|
haftmann@27000
|
254 |
val includes = Symtab.dest (the_includes data);
|
haftmann@27000
|
255 |
val alias = the_module_alias data;
|
haftmann@27024
|
256 |
val { class, inst, tyco, const } = the_name_syntax data;
|
haftmann@27000
|
257 |
val project = if target = target_diag then K I
|
haftmann@27000
|
258 |
else CodeThingol.project_code permissive
|
haftmann@27000
|
259 |
(Symtab.keys class @ Symtab.keys inst @ Symtab.keys tyco @ Symtab.keys const);
|
haftmann@27000
|
260 |
fun check_empty_funs code = case filter_out (member (op =) exc)
|
haftmann@27000
|
261 |
(CodeThingol.empty_funs code)
|
haftmann@27000
|
262 |
of [] => code
|
haftmann@27000
|
263 |
| names => error ("No defining equations for "
|
haftmann@27000
|
264 |
^ commas (map (CodeName.labelled_name thy) names));
|
haftmann@27000
|
265 |
in fn module => fn args => fn code => fn cs =>
|
haftmann@27000
|
266 |
seri module args (CodeName.labelled_name thy) reserved includes
|
haftmann@27000
|
267 |
(Symtab.lookup alias) (Symtab.lookup class)
|
haftmann@27000
|
268 |
(Symtab.lookup tyco) (Symtab.lookup const)
|
haftmann@27000
|
269 |
((check_empty_funs o project cs) code) (these cs)
|
haftmann@24219
|
270 |
end;
|
haftmann@24219
|
271 |
|
haftmann@27000
|
272 |
val serialize = get_serializer the_serializer;
|
haftmann@27000
|
273 |
|
haftmann@24219
|
274 |
fun parse_args f args =
|
haftmann@24219
|
275 |
case Scan.read Args.stopper f args
|
haftmann@24219
|
276 |
of SOME x => x
|
haftmann@24219
|
277 |
| NONE => error "Bad serializer arguments";
|
haftmann@24219
|
278 |
|
haftmann@24219
|
279 |
|
haftmann@27000
|
280 |
(** generic output combinators **)
|
haftmann@27000
|
281 |
|
haftmann@27000
|
282 |
(* applications and bindings *)
|
haftmann@24219
|
283 |
|
haftmann@24219
|
284 |
fun gen_pr_app pr_app' pr_term const_syntax labelled_name is_cons
|
haftmann@24219
|
285 |
lhs vars fxy (app as ((c, (_, tys)), ts)) =
|
haftmann@24219
|
286 |
case const_syntax c
|
haftmann@24219
|
287 |
of NONE => if lhs andalso not (is_cons c) then
|
haftmann@27024
|
288 |
error ("Non-constructor on left hand side of equation: " ^ labelled_name c)
|
haftmann@24219
|
289 |
else brackify fxy (pr_app' lhs vars app)
|
haftmann@24219
|
290 |
| SOME (i, pr) =>
|
haftmann@24219
|
291 |
let
|
haftmann@24219
|
292 |
val k = if i < 0 then length tys else i;
|
haftmann@24219
|
293 |
fun pr' fxy ts = pr (pr_term lhs) vars fxy (ts ~~ curry Library.take k tys);
|
haftmann@24219
|
294 |
in if k = length ts
|
haftmann@24219
|
295 |
then pr' fxy ts
|
haftmann@24219
|
296 |
else if k < length ts
|
haftmann@24219
|
297 |
then case chop k ts of (ts1, ts2) =>
|
haftmann@24219
|
298 |
brackify fxy (pr' APP ts1 :: map (pr_term lhs vars BR) ts2)
|
haftmann@24219
|
299 |
else pr_term lhs vars fxy (CodeThingol.eta_expand app k)
|
haftmann@24219
|
300 |
end;
|
haftmann@24219
|
301 |
|
haftmann@24219
|
302 |
fun gen_pr_bind pr_bind' pr_term fxy ((v, pat), ty) vars =
|
haftmann@24219
|
303 |
let
|
haftmann@24219
|
304 |
val vs = case pat
|
haftmann@24219
|
305 |
of SOME pat => CodeThingol.fold_varnames (insert (op =)) pat []
|
haftmann@24219
|
306 |
| NONE => [];
|
haftmann@24219
|
307 |
val vars' = CodeName.intro_vars (the_list v) vars;
|
haftmann@24219
|
308 |
val vars'' = CodeName.intro_vars vs vars';
|
haftmann@24219
|
309 |
val v' = Option.map (CodeName.lookup_var vars') v;
|
haftmann@24284
|
310 |
val pat' = Option.map (pr_term true vars'' fxy) pat;
|
haftmann@24219
|
311 |
in (pr_bind' ((v', pat'), ty), vars'') end;
|
haftmann@24219
|
312 |
|
haftmann@24219
|
313 |
|
haftmann@24219
|
314 |
(* list, char, string, numeral and monad abstract syntax transformations *)
|
haftmann@24219
|
315 |
|
haftmann@24219
|
316 |
fun implode_list c_nil c_cons t =
|
haftmann@24219
|
317 |
let
|
haftmann@24219
|
318 |
fun dest_cons (IConst (c, _) `$ t1 `$ t2) =
|
haftmann@24219
|
319 |
if c = c_cons
|
haftmann@24219
|
320 |
then SOME (t1, t2)
|
haftmann@24219
|
321 |
else NONE
|
haftmann@24219
|
322 |
| dest_cons _ = NONE;
|
haftmann@24219
|
323 |
val (ts, t') = CodeThingol.unfoldr dest_cons t;
|
haftmann@24219
|
324 |
in case t'
|
haftmann@24219
|
325 |
of IConst (c, _) => if c = c_nil then SOME ts else NONE
|
haftmann@24219
|
326 |
| _ => NONE
|
haftmann@24219
|
327 |
end;
|
haftmann@24219
|
328 |
|
haftmann@24219
|
329 |
fun decode_char c_nibbles (IConst (c1, _), IConst (c2, _)) =
|
haftmann@24219
|
330 |
let
|
haftmann@24219
|
331 |
fun idx c = find_index (curry (op =) c) c_nibbles;
|
haftmann@24219
|
332 |
fun decode ~1 _ = NONE
|
haftmann@24219
|
333 |
| decode _ ~1 = NONE
|
haftmann@24219
|
334 |
| decode n m = SOME (chr (n * 16 + m));
|
haftmann@24219
|
335 |
in decode (idx c1) (idx c2) end
|
haftmann@24219
|
336 |
| decode_char _ _ = NONE;
|
haftmann@24219
|
337 |
|
haftmann@24219
|
338 |
fun implode_string c_char c_nibbles mk_char mk_string ts =
|
haftmann@24219
|
339 |
let
|
haftmann@24219
|
340 |
fun implode_char (IConst (c, _) `$ t1 `$ t2) =
|
haftmann@24219
|
341 |
if c = c_char then decode_char c_nibbles (t1, t2) else NONE
|
haftmann@24219
|
342 |
| implode_char _ = NONE;
|
haftmann@24219
|
343 |
val ts' = map implode_char ts;
|
haftmann@24219
|
344 |
in if forall is_some ts'
|
haftmann@24219
|
345 |
then (SOME o str o mk_string o implode o map_filter I) ts'
|
haftmann@24219
|
346 |
else NONE
|
haftmann@24219
|
347 |
end;
|
haftmann@24219
|
348 |
|
huffman@26086
|
349 |
fun implode_numeral negative c_pls c_min c_bit0 c_bit1 =
|
haftmann@24219
|
350 |
let
|
haftmann@25936
|
351 |
fun dest_bit (IConst (c, _)) = if c = c_bit0 then 0
|
haftmann@25936
|
352 |
else if c = c_bit1 then 1
|
haftmann@25936
|
353 |
else error "Illegal numeral expression: illegal bit"
|
haftmann@25936
|
354 |
| dest_bit _ = error "Illegal numeral expression: illegal bit";
|
wenzelm@24630
|
355 |
fun dest_numeral (IConst (c, _)) = if c = c_pls then SOME 0
|
haftmann@25936
|
356 |
else if c = c_min then
|
haftmann@25936
|
357 |
if negative then SOME ~1 else NONE
|
haftmann@25969
|
358 |
else error "Illegal numeral expression: illegal leading digit"
|
huffman@26086
|
359 |
| dest_numeral (t1 `$ t2) =
|
huffman@26086
|
360 |
let val (n, b) = (dest_numeral t2, dest_bit t1)
|
huffman@26086
|
361 |
in case n of SOME n => SOME (2 * n + b) | NONE => NONE end
|
haftmann@25969
|
362 |
| dest_numeral _ = error "Illegal numeral expression: illegal constant";
|
haftmann@25936
|
363 |
in dest_numeral #> the_default 0 end;
|
haftmann@24219
|
364 |
|
haftmann@24992
|
365 |
fun implode_monad c_bind t =
|
haftmann@24219
|
366 |
let
|
haftmann@24219
|
367 |
fun dest_monad (IConst (c, _) `$ t1 `$ t2) =
|
haftmann@24992
|
368 |
if c = c_bind
|
haftmann@24219
|
369 |
then case CodeThingol.split_abs t2
|
haftmann@24992
|
370 |
of SOME (((v, pat), ty), t') =>
|
haftmann@24992
|
371 |
SOME ((SOME (((SOME v, pat), ty), true), t1), t')
|
haftmann@24219
|
372 |
| NONE => NONE
|
haftmann@24219
|
373 |
else NONE
|
haftmann@24219
|
374 |
| dest_monad t = case CodeThingol.split_let t
|
haftmann@24992
|
375 |
of SOME (((pat, ty), tbind), t') =>
|
haftmann@24992
|
376 |
SOME ((SOME (((NONE, SOME pat), ty), false), tbind), t')
|
haftmann@24219
|
377 |
| NONE => NONE;
|
haftmann@24219
|
378 |
in CodeThingol.unfoldr dest_monad t end;
|
haftmann@24219
|
379 |
|
haftmann@24219
|
380 |
|
haftmann@27000
|
381 |
(* name auxiliary *)
|
haftmann@24219
|
382 |
|
haftmann@24219
|
383 |
val first_upper = implode o nth_map 0 Symbol.to_ascii_upper o explode;
|
haftmann@24219
|
384 |
val first_lower = implode o nth_map 0 Symbol.to_ascii_lower o explode;
|
haftmann@24219
|
385 |
|
haftmann@24219
|
386 |
val dest_name =
|
haftmann@24219
|
387 |
apfst NameSpace.implode o split_last o fst o split_last o NameSpace.explode;
|
haftmann@24219
|
388 |
|
haftmann@24219
|
389 |
fun mk_modl_name_tab init_names prefix module_alias code =
|
haftmann@24219
|
390 |
let
|
haftmann@24219
|
391 |
fun nsp_map f = NameSpace.explode #> map f #> NameSpace.implode;
|
haftmann@24219
|
392 |
fun mk_alias name =
|
haftmann@24219
|
393 |
case module_alias name
|
haftmann@24219
|
394 |
of SOME name' => name'
|
haftmann@24219
|
395 |
| NONE => nsp_map (fn name => (the_single o fst)
|
haftmann@24219
|
396 |
(Name.variants [name] init_names)) name;
|
haftmann@24219
|
397 |
fun mk_prefix name =
|
haftmann@24219
|
398 |
case prefix
|
haftmann@24219
|
399 |
of SOME prefix => NameSpace.append prefix name
|
haftmann@24219
|
400 |
| NONE => name;
|
haftmann@24219
|
401 |
val tab =
|
haftmann@24219
|
402 |
Symtab.empty
|
haftmann@24219
|
403 |
|> Graph.fold ((fn name => Symtab.default (name, (mk_alias #> mk_prefix) name))
|
haftmann@24219
|
404 |
o fst o dest_name o fst)
|
haftmann@24219
|
405 |
code
|
haftmann@24219
|
406 |
in fn name => (the o Symtab.lookup tab) name end;
|
haftmann@24219
|
407 |
|
haftmann@24219
|
408 |
|
haftmann@24219
|
409 |
|
haftmann@24219
|
410 |
(** SML/OCaml serializer **)
|
haftmann@24219
|
411 |
|
haftmann@24219
|
412 |
datatype ml_def =
|
haftmann@24381
|
413 |
MLFuns of (string * (typscheme * (iterm list * iterm) list)) list
|
haftmann@24219
|
414 |
| MLDatas of (string * ((vname * sort) list * (string * itype list) list)) list
|
haftmann@24811
|
415 |
| MLClass of string * (vname * ((class * string) list * (string * itype) list))
|
haftmann@24219
|
416 |
| MLClassinst of string * ((class * (string * (vname * sort) list))
|
haftmann@24219
|
417 |
* ((class * (string * (string * dict list list))) list
|
haftmann@24591
|
418 |
* (string * const) list));
|
haftmann@24219
|
419 |
|
haftmann@24841
|
420 |
fun pr_sml tyco_syntax const_syntax labelled_name init_syms deresolv is_cons ml_def =
|
haftmann@24219
|
421 |
let
|
haftmann@24992
|
422 |
val pr_label_classrel = translate_string (fn "." => "__" | c => c)
|
haftmann@24992
|
423 |
o NameSpace.qualifier;
|
haftmann@24841
|
424 |
val pr_label_classparam = NameSpace.base o NameSpace.qualifier;
|
haftmann@24219
|
425 |
fun pr_dicts fxy ds =
|
haftmann@24219
|
426 |
let
|
haftmann@24219
|
427 |
fun pr_dictvar (v, (_, 1)) = first_upper v ^ "_"
|
haftmann@24219
|
428 |
| pr_dictvar (v, (i, _)) = first_upper v ^ string_of_int (i+1) ^ "_";
|
haftmann@24219
|
429 |
fun pr_proj [] p =
|
haftmann@24219
|
430 |
p
|
haftmann@24219
|
431 |
| pr_proj [p'] p =
|
haftmann@24219
|
432 |
brackets [p', p]
|
haftmann@24219
|
433 |
| pr_proj (ps as _ :: _) p =
|
haftmann@24219
|
434 |
brackets [Pretty.enum " o" "(" ")" ps, p];
|
haftmann@24219
|
435 |
fun pr_dictc fxy (DictConst (inst, dss)) =
|
haftmann@24219
|
436 |
brackify fxy ((str o deresolv) inst :: map (pr_dicts BR) dss)
|
haftmann@24219
|
437 |
| pr_dictc fxy (DictVar (classrels, v)) =
|
haftmann@24219
|
438 |
pr_proj (map (str o deresolv) classrels) ((str o pr_dictvar) v)
|
haftmann@24219
|
439 |
in case ds
|
haftmann@24219
|
440 |
of [] => str "()"
|
haftmann@24219
|
441 |
| [d] => pr_dictc fxy d
|
haftmann@24219
|
442 |
| _ :: _ => (Pretty.list "(" ")" o map (pr_dictc NOBR)) ds
|
haftmann@24219
|
443 |
end;
|
haftmann@24219
|
444 |
fun pr_tyvars vs =
|
haftmann@24219
|
445 |
vs
|
haftmann@24992
|
446 |
|> map (fn (v, sort) => map_index (fn (i, _) =>
|
haftmann@24992
|
447 |
DictVar ([], (v, (i, length sort)))) sort)
|
haftmann@24219
|
448 |
|> map (pr_dicts BR);
|
haftmann@24219
|
449 |
fun pr_tycoexpr fxy (tyco, tys) =
|
haftmann@24219
|
450 |
let
|
haftmann@24219
|
451 |
val tyco' = (str o deresolv) tyco
|
haftmann@24219
|
452 |
in case map (pr_typ BR) tys
|
haftmann@24219
|
453 |
of [] => tyco'
|
haftmann@24219
|
454 |
| [p] => Pretty.block [p, Pretty.brk 1, tyco']
|
haftmann@24219
|
455 |
| (ps as _::_) => Pretty.block [Pretty.list "(" ")" ps, Pretty.brk 1, tyco']
|
haftmann@24219
|
456 |
end
|
haftmann@24219
|
457 |
and pr_typ fxy (tyco `%% tys) =
|
haftmann@24219
|
458 |
(case tyco_syntax tyco
|
haftmann@24219
|
459 |
of NONE => pr_tycoexpr fxy (tyco, tys)
|
haftmann@24219
|
460 |
| SOME (i, pr) =>
|
haftmann@24219
|
461 |
if not (i = length tys)
|
haftmann@24219
|
462 |
then error ("Number of argument mismatch in customary serialization: "
|
haftmann@24219
|
463 |
^ (string_of_int o length) tys ^ " given, "
|
haftmann@24219
|
464 |
^ string_of_int i ^ " expected")
|
haftmann@24219
|
465 |
else pr pr_typ fxy tys)
|
haftmann@24219
|
466 |
| pr_typ fxy (ITyVar v) =
|
haftmann@24219
|
467 |
str ("'" ^ v);
|
haftmann@24219
|
468 |
fun pr_term lhs vars fxy (IConst c) =
|
haftmann@24219
|
469 |
pr_app lhs vars fxy (c, [])
|
haftmann@24219
|
470 |
| pr_term lhs vars fxy (IVar v) =
|
haftmann@24219
|
471 |
str (CodeName.lookup_var vars v)
|
haftmann@24219
|
472 |
| pr_term lhs vars fxy (t as t1 `$ t2) =
|
haftmann@24219
|
473 |
(case CodeThingol.unfold_const_app t
|
haftmann@24219
|
474 |
of SOME c_ts => pr_app lhs vars fxy c_ts
|
haftmann@24219
|
475 |
| NONE =>
|
haftmann@24219
|
476 |
brackify fxy [pr_term lhs vars NOBR t1, pr_term lhs vars BR t2])
|
haftmann@24219
|
477 |
| pr_term lhs vars fxy (t as _ `|-> _) =
|
haftmann@24219
|
478 |
let
|
haftmann@24219
|
479 |
val (binds, t') = CodeThingol.unfold_abs t;
|
haftmann@24219
|
480 |
fun pr ((v, pat), ty) =
|
haftmann@24219
|
481 |
pr_bind NOBR ((SOME v, pat), ty)
|
haftmann@24219
|
482 |
#>> (fn p => concat [str "fn", p, str "=>"]);
|
haftmann@24219
|
483 |
val (ps, vars') = fold_map pr binds vars;
|
haftmann@24219
|
484 |
in brackets (ps @ [pr_term lhs vars' NOBR t']) end
|
haftmann@24992
|
485 |
| pr_term lhs vars fxy (ICase (cases as (_, t0))) =
|
haftmann@24992
|
486 |
(case CodeThingol.unfold_const_app t0
|
haftmann@24219
|
487 |
of SOME (c_ts as ((c, _), _)) => if is_none (const_syntax c)
|
haftmann@24219
|
488 |
then pr_case vars fxy cases
|
haftmann@24219
|
489 |
else pr_app lhs vars fxy c_ts
|
haftmann@24219
|
490 |
| NONE => pr_case vars fxy cases)
|
haftmann@24219
|
491 |
and pr_app' lhs vars (app as ((c, (iss, tys)), ts)) =
|
haftmann@24219
|
492 |
if is_cons c then let
|
haftmann@24219
|
493 |
val k = length tys
|
haftmann@24219
|
494 |
in if k < 2 then
|
haftmann@24219
|
495 |
(str o deresolv) c :: map (pr_term lhs vars BR) ts
|
haftmann@24219
|
496 |
else if k = length ts then
|
haftmann@24219
|
497 |
[(str o deresolv) c, Pretty.enum "," "(" ")" (map (pr_term lhs vars NOBR) ts)]
|
haftmann@24219
|
498 |
else [pr_term lhs vars BR (CodeThingol.eta_expand app k)] end else
|
haftmann@24219
|
499 |
(str o deresolv) c
|
haftmann@24219
|
500 |
:: (map (pr_dicts BR) o filter_out null) iss @ map (pr_term lhs vars BR) ts
|
haftmann@24992
|
501 |
and pr_app lhs vars = gen_pr_app pr_app' pr_term const_syntax
|
haftmann@24992
|
502 |
labelled_name is_cons lhs vars
|
haftmann@24219
|
503 |
and pr_bind' ((NONE, NONE), _) = str "_"
|
haftmann@24219
|
504 |
| pr_bind' ((SOME v, NONE), _) = str v
|
haftmann@24219
|
505 |
| pr_bind' ((NONE, SOME p), _) = p
|
haftmann@24219
|
506 |
| pr_bind' ((SOME v, SOME p), _) = concat [str v, str "as", p]
|
haftmann@24284
|
507 |
and pr_bind fxy = gen_pr_bind pr_bind' pr_term fxy
|
haftmann@24219
|
508 |
and pr_case vars fxy (cases as ((_, [_]), _)) =
|
haftmann@24219
|
509 |
let
|
haftmann@24219
|
510 |
val (binds, t') = CodeThingol.unfold_let (ICase cases);
|
haftmann@24219
|
511 |
fun pr ((pat, ty), t) vars =
|
haftmann@24219
|
512 |
vars
|
haftmann@24219
|
513 |
|> pr_bind NOBR ((NONE, SOME pat), ty)
|
haftmann@24219
|
514 |
|>> (fn p => semicolon [str "val", p, str "=", pr_term false vars NOBR t])
|
haftmann@24219
|
515 |
val (ps, vars') = fold_map pr binds vars;
|
haftmann@24219
|
516 |
in
|
haftmann@24219
|
517 |
Pretty.chunks [
|
haftmann@24219
|
518 |
[str ("let"), Pretty.fbrk, Pretty.chunks ps] |> Pretty.block,
|
haftmann@24219
|
519 |
[str ("in"), Pretty.fbrk, pr_term false vars' NOBR t'] |> Pretty.block,
|
haftmann@24219
|
520 |
str ("end")
|
haftmann@24219
|
521 |
]
|
haftmann@24219
|
522 |
end
|
haftmann@24219
|
523 |
| pr_case vars fxy (((td, ty), b::bs), _) =
|
haftmann@24219
|
524 |
let
|
haftmann@24219
|
525 |
fun pr delim (pat, t) =
|
haftmann@24219
|
526 |
let
|
haftmann@24219
|
527 |
val (p, vars') = pr_bind NOBR ((NONE, SOME pat), ty) vars;
|
haftmann@24219
|
528 |
in
|
haftmann@24219
|
529 |
concat [str delim, p, str "=>", pr_term false vars' NOBR t]
|
haftmann@24219
|
530 |
end;
|
haftmann@24219
|
531 |
in
|
haftmann@24219
|
532 |
(Pretty.enclose "(" ")" o single o brackify fxy) (
|
haftmann@24219
|
533 |
str "case"
|
haftmann@24219
|
534 |
:: pr_term false vars NOBR td
|
haftmann@24219
|
535 |
:: pr "of" b
|
haftmann@24219
|
536 |
:: map (pr "|") bs
|
haftmann@24219
|
537 |
)
|
haftmann@24219
|
538 |
end
|
haftmann@24219
|
539 |
| pr_case vars fxy ((_, []), _) = str "raise Fail \"empty case\""
|
haftmann@24219
|
540 |
fun pr_def (MLFuns (funns as (funn :: funns'))) =
|
haftmann@24219
|
541 |
let
|
haftmann@24219
|
542 |
val definer =
|
haftmann@24219
|
543 |
let
|
haftmann@24841
|
544 |
fun no_args _ ((ts, _) :: _) = length ts
|
haftmann@24841
|
545 |
| no_args ty [] = (length o fst o CodeThingol.unfold_fun) ty;
|
haftmann@24841
|
546 |
fun mk 0 [] = "val"
|
haftmann@24992
|
547 |
| mk 0 vs = if (null o filter_out (null o snd)) vs
|
haftmann@24992
|
548 |
then "val" else "fun"
|
haftmann@24841
|
549 |
| mk k _ = "fun";
|
haftmann@24841
|
550 |
fun chk (_, ((vs, ty), eqs)) NONE = SOME (mk (no_args ty eqs) vs)
|
haftmann@24841
|
551 |
| chk (_, ((vs, ty), eqs)) (SOME defi) =
|
haftmann@24841
|
552 |
if defi = mk (no_args ty eqs) vs then SOME defi
|
haftmann@24219
|
553 |
else error ("Mixing simultaneous vals and funs not implemented: "
|
haftmann@24219
|
554 |
^ commas (map (labelled_name o fst) funns));
|
haftmann@24219
|
555 |
in the (fold chk funns NONE) end;
|
haftmann@24841
|
556 |
fun pr_funn definer (name, ((raw_vs, ty), [])) =
|
haftmann@24219
|
557 |
let
|
haftmann@24841
|
558 |
val vs = filter_out (null o snd) raw_vs;
|
haftmann@24841
|
559 |
val n = length vs + (length o fst o CodeThingol.unfold_fun) ty;
|
haftmann@24992
|
560 |
val exc_str =
|
haftmann@24992
|
561 |
(ML_Syntax.print_string o NameSpace.base o NameSpace.qualifier) name;
|
haftmann@24219
|
562 |
in
|
haftmann@24219
|
563 |
concat (
|
haftmann@24841
|
564 |
str definer
|
haftmann@24841
|
565 |
:: (str o deresolv) name
|
haftmann@24841
|
566 |
:: map str (replicate n "_")
|
haftmann@24841
|
567 |
@ str "="
|
haftmann@24841
|
568 |
:: str "raise"
|
haftmann@24841
|
569 |
:: str "(Fail"
|
haftmann@24992
|
570 |
:: str exc_str
|
haftmann@24841
|
571 |
@@ str ")"
|
haftmann@24219
|
572 |
)
|
haftmann@24219
|
573 |
end
|
haftmann@24841
|
574 |
| pr_funn definer (name, ((raw_vs, ty), eqs as eq :: eqs')) =
|
haftmann@24841
|
575 |
let
|
haftmann@24841
|
576 |
val vs = filter_out (null o snd) raw_vs;
|
haftmann@24841
|
577 |
val shift = if null eqs' then I else
|
haftmann@24841
|
578 |
map (Pretty.block o single o Pretty.block o single);
|
haftmann@24841
|
579 |
fun pr_eq definer (ts, t) =
|
haftmann@24841
|
580 |
let
|
haftmann@24841
|
581 |
val consts = map_filter
|
haftmann@24841
|
582 |
(fn c => if (is_some o const_syntax) c
|
haftmann@24841
|
583 |
then NONE else (SOME o NameSpace.base o deresolv) c)
|
haftmann@24841
|
584 |
((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []);
|
haftmann@24841
|
585 |
val vars = init_syms
|
haftmann@24841
|
586 |
|> CodeName.intro_vars consts
|
haftmann@24841
|
587 |
|> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames)
|
haftmann@24841
|
588 |
(insert (op =)) ts []);
|
haftmann@24841
|
589 |
in
|
haftmann@24841
|
590 |
concat (
|
haftmann@24841
|
591 |
[str definer, (str o deresolv) name]
|
haftmann@24841
|
592 |
@ (if null ts andalso null vs
|
haftmann@24841
|
593 |
then [str ":", pr_typ NOBR ty]
|
haftmann@24841
|
594 |
else
|
haftmann@24841
|
595 |
pr_tyvars vs
|
haftmann@24841
|
596 |
@ map (pr_term true vars BR) ts)
|
haftmann@24841
|
597 |
@ [str "=", pr_term false vars NOBR t]
|
haftmann@24841
|
598 |
)
|
haftmann@24841
|
599 |
end
|
haftmann@24841
|
600 |
in
|
haftmann@24841
|
601 |
(Pretty.block o Pretty.fbreaks o shift) (
|
haftmann@24841
|
602 |
pr_eq definer eq
|
haftmann@24841
|
603 |
:: map (pr_eq "|") eqs'
|
haftmann@24841
|
604 |
)
|
haftmann@24841
|
605 |
end;
|
haftmann@24219
|
606 |
val (ps, p) = split_last (pr_funn definer funn :: map (pr_funn "and") funns');
|
haftmann@24219
|
607 |
in Pretty.chunks (ps @ [Pretty.block ([p, str ";"])]) end
|
haftmann@24219
|
608 |
| pr_def (MLDatas (datas as (data :: datas'))) =
|
haftmann@24219
|
609 |
let
|
haftmann@24219
|
610 |
fun pr_co (co, []) =
|
haftmann@24219
|
611 |
str (deresolv co)
|
haftmann@24219
|
612 |
| pr_co (co, tys) =
|
haftmann@24219
|
613 |
concat [
|
haftmann@24219
|
614 |
str (deresolv co),
|
haftmann@24219
|
615 |
str "of",
|
haftmann@24219
|
616 |
Pretty.enum " *" "" "" (map (pr_typ (INFX (2, X))) tys)
|
haftmann@24219
|
617 |
];
|
haftmann@24219
|
618 |
fun pr_data definer (tyco, (vs, [])) =
|
haftmann@24219
|
619 |
concat (
|
haftmann@24219
|
620 |
str definer
|
haftmann@24219
|
621 |
:: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs)
|
haftmann@24219
|
622 |
:: str "="
|
haftmann@24219
|
623 |
@@ str "EMPTY__"
|
haftmann@24219
|
624 |
)
|
haftmann@24219
|
625 |
| pr_data definer (tyco, (vs, cos)) =
|
haftmann@24219
|
626 |
concat (
|
haftmann@24219
|
627 |
str definer
|
haftmann@24219
|
628 |
:: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs)
|
haftmann@24219
|
629 |
:: str "="
|
haftmann@24219
|
630 |
:: separate (str "|") (map pr_co cos)
|
haftmann@24219
|
631 |
);
|
haftmann@24992
|
632 |
val (ps, p) = split_last
|
haftmann@24992
|
633 |
(pr_data "datatype" data :: map (pr_data "and") datas');
|
haftmann@24219
|
634 |
in Pretty.chunks (ps @ [Pretty.block ([p, str ";"])]) end
|
haftmann@24841
|
635 |
| pr_def (MLClass (class, (v, (superclasses, classparams)))) =
|
haftmann@24219
|
636 |
let
|
haftmann@24219
|
637 |
val w = first_upper v ^ "_";
|
haftmann@24219
|
638 |
fun pr_superclass_field (class, classrel) =
|
haftmann@24219
|
639 |
(concat o map str) [
|
haftmann@24219
|
640 |
pr_label_classrel classrel, ":", "'" ^ v, deresolv class
|
haftmann@24219
|
641 |
];
|
haftmann@24841
|
642 |
fun pr_classparam_field (classparam, ty) =
|
haftmann@24219
|
643 |
concat [
|
haftmann@24841
|
644 |
(str o pr_label_classparam) classparam, str ":", pr_typ NOBR ty
|
haftmann@24219
|
645 |
];
|
haftmann@24841
|
646 |
fun pr_classparam_proj (classparam, _) =
|
haftmann@24219
|
647 |
semicolon [
|
haftmann@24219
|
648 |
str "fun",
|
haftmann@24841
|
649 |
(str o deresolv) classparam,
|
haftmann@24219
|
650 |
Pretty.enclose "(" ")" [str (w ^ ":'" ^ v ^ " " ^ deresolv class)],
|
haftmann@24219
|
651 |
str "=",
|
haftmann@24841
|
652 |
str ("#" ^ pr_label_classparam classparam),
|
haftmann@24219
|
653 |
str w
|
haftmann@24219
|
654 |
];
|
haftmann@24219
|
655 |
fun pr_superclass_proj (_, classrel) =
|
haftmann@24219
|
656 |
semicolon [
|
haftmann@24219
|
657 |
str "fun",
|
haftmann@24219
|
658 |
(str o deresolv) classrel,
|
haftmann@24219
|
659 |
Pretty.enclose "(" ")" [str (w ^ ":'" ^ v ^ " " ^ deresolv class)],
|
haftmann@24219
|
660 |
str "=",
|
haftmann@24219
|
661 |
str ("#" ^ pr_label_classrel classrel),
|
haftmann@24219
|
662 |
str w
|
haftmann@24219
|
663 |
];
|
haftmann@24219
|
664 |
in
|
haftmann@24219
|
665 |
Pretty.chunks (
|
haftmann@24219
|
666 |
concat [
|
haftmann@24219
|
667 |
str ("type '" ^ v),
|
haftmann@24219
|
668 |
(str o deresolv) class,
|
haftmann@24219
|
669 |
str "=",
|
haftmann@24219
|
670 |
Pretty.enum "," "{" "};" (
|
haftmann@24841
|
671 |
map pr_superclass_field superclasses @ map pr_classparam_field classparams
|
haftmann@24219
|
672 |
)
|
haftmann@24219
|
673 |
]
|
haftmann@24219
|
674 |
:: map pr_superclass_proj superclasses
|
haftmann@24841
|
675 |
@ map pr_classparam_proj classparams
|
haftmann@24219
|
676 |
)
|
haftmann@24219
|
677 |
end
|
haftmann@24841
|
678 |
| pr_def (MLClassinst (inst, ((class, (tyco, arity)), (superarities, classparam_insts)))) =
|
haftmann@24219
|
679 |
let
|
haftmann@24219
|
680 |
fun pr_superclass (_, (classrel, dss)) =
|
haftmann@24219
|
681 |
concat [
|
haftmann@24219
|
682 |
(str o pr_label_classrel) classrel,
|
haftmann@24219
|
683 |
str "=",
|
haftmann@24219
|
684 |
pr_dicts NOBR [DictConst dss]
|
haftmann@24219
|
685 |
];
|
haftmann@24841
|
686 |
fun pr_classparam (classparam, c_inst) =
|
haftmann@24591
|
687 |
concat [
|
haftmann@24841
|
688 |
(str o pr_label_classparam) classparam,
|
haftmann@24591
|
689 |
str "=",
|
haftmann@24591
|
690 |
pr_app false init_syms NOBR (c_inst, [])
|
haftmann@24591
|
691 |
];
|
haftmann@24219
|
692 |
in
|
haftmann@24219
|
693 |
semicolon ([
|
haftmann@24219
|
694 |
str (if null arity then "val" else "fun"),
|
haftmann@24219
|
695 |
(str o deresolv) inst ] @
|
haftmann@24219
|
696 |
pr_tyvars arity @ [
|
haftmann@24219
|
697 |
str "=",
|
haftmann@24992
|
698 |
Pretty.enum "," "{" "}"
|
haftmann@24992
|
699 |
(map pr_superclass superarities @ map pr_classparam classparam_insts),
|
haftmann@24219
|
700 |
str ":",
|
haftmann@24219
|
701 |
pr_tycoexpr NOBR (class, [tyco `%% map (ITyVar o fst) arity])
|
haftmann@24219
|
702 |
])
|
haftmann@24219
|
703 |
end;
|
haftmann@24219
|
704 |
in pr_def ml_def end;
|
haftmann@24219
|
705 |
|
haftmann@24219
|
706 |
fun pr_sml_modl name content =
|
haftmann@24219
|
707 |
Pretty.chunks ([
|
haftmann@24219
|
708 |
str ("structure " ^ name ^ " = "),
|
haftmann@24219
|
709 |
str "struct",
|
haftmann@24219
|
710 |
str ""
|
haftmann@24219
|
711 |
] @ content @ [
|
haftmann@24219
|
712 |
str "",
|
haftmann@24219
|
713 |
str ("end; (*struct " ^ name ^ "*)")
|
haftmann@24219
|
714 |
]);
|
haftmann@24219
|
715 |
|
haftmann@24841
|
716 |
fun pr_ocaml tyco_syntax const_syntax labelled_name
|
haftmann@24621
|
717 |
init_syms deresolv is_cons ml_def =
|
haftmann@24219
|
718 |
let
|
haftmann@24219
|
719 |
fun pr_dicts fxy ds =
|
haftmann@24219
|
720 |
let
|
haftmann@24219
|
721 |
fun pr_dictvar (v, (_, 1)) = "_" ^ first_upper v
|
haftmann@24219
|
722 |
| pr_dictvar (v, (i, _)) = "_" ^ first_upper v ^ string_of_int (i+1);
|
haftmann@24219
|
723 |
fun pr_proj ps p =
|
haftmann@24219
|
724 |
fold_rev (fn p2 => fn p1 => Pretty.block [p1, str ".", str p2]) ps p
|
haftmann@24219
|
725 |
fun pr_dictc fxy (DictConst (inst, dss)) =
|
haftmann@24219
|
726 |
brackify fxy ((str o deresolv) inst :: map (pr_dicts BR) dss)
|
haftmann@24219
|
727 |
| pr_dictc fxy (DictVar (classrels, v)) =
|
haftmann@24219
|
728 |
pr_proj (map deresolv classrels) ((str o pr_dictvar) v)
|
haftmann@24219
|
729 |
in case ds
|
haftmann@24219
|
730 |
of [] => str "()"
|
haftmann@24219
|
731 |
| [d] => pr_dictc fxy d
|
haftmann@24219
|
732 |
| _ :: _ => (Pretty.list "(" ")" o map (pr_dictc NOBR)) ds
|
haftmann@24219
|
733 |
end;
|
haftmann@24219
|
734 |
fun pr_tyvars vs =
|
haftmann@24219
|
735 |
vs
|
haftmann@24992
|
736 |
|> map (fn (v, sort) => map_index (fn (i, _) =>
|
haftmann@24992
|
737 |
DictVar ([], (v, (i, length sort)))) sort)
|
haftmann@24219
|
738 |
|> map (pr_dicts BR);
|
haftmann@24219
|
739 |
fun pr_tycoexpr fxy (tyco, tys) =
|
haftmann@24219
|
740 |
let
|
haftmann@24219
|
741 |
val tyco' = (str o deresolv) tyco
|
haftmann@24219
|
742 |
in case map (pr_typ BR) tys
|
haftmann@24219
|
743 |
of [] => tyco'
|
haftmann@24219
|
744 |
| [p] => Pretty.block [p, Pretty.brk 1, tyco']
|
haftmann@24219
|
745 |
| (ps as _::_) => Pretty.block [Pretty.list "(" ")" ps, Pretty.brk 1, tyco']
|
haftmann@24219
|
746 |
end
|
haftmann@24219
|
747 |
and pr_typ fxy (tyco `%% tys) =
|
haftmann@24219
|
748 |
(case tyco_syntax tyco
|
haftmann@24219
|
749 |
of NONE => pr_tycoexpr fxy (tyco, tys)
|
haftmann@24219
|
750 |
| SOME (i, pr) =>
|
haftmann@24219
|
751 |
if not (i = length tys)
|
haftmann@24219
|
752 |
then error ("Number of argument mismatch in customary serialization: "
|
haftmann@24219
|
753 |
^ (string_of_int o length) tys ^ " given, "
|
haftmann@24219
|
754 |
^ string_of_int i ^ " expected")
|
haftmann@24219
|
755 |
else pr pr_typ fxy tys)
|
haftmann@24219
|
756 |
| pr_typ fxy (ITyVar v) =
|
haftmann@24219
|
757 |
str ("'" ^ v);
|
haftmann@24219
|
758 |
fun pr_term lhs vars fxy (IConst c) =
|
haftmann@24219
|
759 |
pr_app lhs vars fxy (c, [])
|
haftmann@24219
|
760 |
| pr_term lhs vars fxy (IVar v) =
|
haftmann@24219
|
761 |
str (CodeName.lookup_var vars v)
|
haftmann@24219
|
762 |
| pr_term lhs vars fxy (t as t1 `$ t2) =
|
haftmann@24219
|
763 |
(case CodeThingol.unfold_const_app t
|
haftmann@24219
|
764 |
of SOME c_ts => pr_app lhs vars fxy c_ts
|
haftmann@24219
|
765 |
| NONE =>
|
haftmann@24219
|
766 |
brackify fxy [pr_term lhs vars NOBR t1, pr_term lhs vars BR t2])
|
haftmann@24219
|
767 |
| pr_term lhs vars fxy (t as _ `|-> _) =
|
haftmann@24219
|
768 |
let
|
haftmann@24219
|
769 |
val (binds, t') = CodeThingol.unfold_abs t;
|
haftmann@24219
|
770 |
fun pr ((v, pat), ty) = pr_bind BR ((SOME v, pat), ty);
|
haftmann@24219
|
771 |
val (ps, vars') = fold_map pr binds vars;
|
haftmann@24219
|
772 |
in brackets (str "fun" :: ps @ str "->" @@ pr_term lhs vars' NOBR t') end
|
haftmann@24219
|
773 |
| pr_term lhs vars fxy (ICase (cases as (_, t0))) = (case CodeThingol.unfold_const_app t0
|
haftmann@24219
|
774 |
of SOME (c_ts as ((c, _), _)) => if is_none (const_syntax c)
|
haftmann@24219
|
775 |
then pr_case vars fxy cases
|
haftmann@24219
|
776 |
else pr_app lhs vars fxy c_ts
|
haftmann@24219
|
777 |
| NONE => pr_case vars fxy cases)
|
haftmann@24219
|
778 |
and pr_app' lhs vars (app as ((c, (iss, tys)), ts)) =
|
haftmann@24219
|
779 |
if is_cons c then
|
haftmann@24219
|
780 |
if length tys = length ts
|
haftmann@24219
|
781 |
then case ts
|
haftmann@24219
|
782 |
of [] => [(str o deresolv) c]
|
haftmann@24219
|
783 |
| [t] => [(str o deresolv) c, pr_term lhs vars BR t]
|
haftmann@24992
|
784 |
| _ => [(str o deresolv) c, Pretty.enum "," "(" ")"
|
haftmann@24992
|
785 |
(map (pr_term lhs vars NOBR) ts)]
|
haftmann@24219
|
786 |
else [pr_term lhs vars BR (CodeThingol.eta_expand app (length tys))]
|
haftmann@24219
|
787 |
else (str o deresolv) c
|
haftmann@24219
|
788 |
:: ((map (pr_dicts BR) o filter_out null) iss @ map (pr_term lhs vars BR) ts)
|
haftmann@24992
|
789 |
and pr_app lhs vars = gen_pr_app pr_app' pr_term const_syntax
|
haftmann@24992
|
790 |
labelled_name is_cons lhs vars
|
haftmann@24219
|
791 |
and pr_bind' ((NONE, NONE), _) = str "_"
|
haftmann@24219
|
792 |
| pr_bind' ((SOME v, NONE), _) = str v
|
haftmann@24219
|
793 |
| pr_bind' ((NONE, SOME p), _) = p
|
haftmann@24219
|
794 |
| pr_bind' ((SOME v, SOME p), _) = brackets [p, str "as", str v]
|
haftmann@24284
|
795 |
and pr_bind fxy = gen_pr_bind pr_bind' pr_term fxy
|
haftmann@24219
|
796 |
and pr_case vars fxy (cases as ((_, [_]), _)) =
|
haftmann@24219
|
797 |
let
|
haftmann@24219
|
798 |
val (binds, t') = CodeThingol.unfold_let (ICase cases);
|
haftmann@24219
|
799 |
fun pr ((pat, ty), t) vars =
|
haftmann@24219
|
800 |
vars
|
haftmann@24219
|
801 |
|> pr_bind NOBR ((NONE, SOME pat), ty)
|
haftmann@24992
|
802 |
|>> (fn p => concat
|
haftmann@24992
|
803 |
[str "let", p, str "=", pr_term false vars NOBR t, str "in"])
|
haftmann@24219
|
804 |
val (ps, vars') = fold_map pr binds vars;
|
haftmann@24219
|
805 |
in Pretty.chunks (ps @| pr_term false vars' NOBR t') end
|
haftmann@24219
|
806 |
| pr_case vars fxy (((td, ty), b::bs), _) =
|
haftmann@24219
|
807 |
let
|
haftmann@24219
|
808 |
fun pr delim (pat, t) =
|
haftmann@24219
|
809 |
let
|
haftmann@24219
|
810 |
val (p, vars') = pr_bind NOBR ((NONE, SOME pat), ty) vars;
|
haftmann@24219
|
811 |
in concat [str delim, p, str "->", pr_term false vars' NOBR t] end;
|
haftmann@24219
|
812 |
in
|
haftmann@24219
|
813 |
(Pretty.enclose "(" ")" o single o brackify fxy) (
|
haftmann@24219
|
814 |
str "match"
|
haftmann@24219
|
815 |
:: pr_term false vars NOBR td
|
haftmann@24219
|
816 |
:: pr "with" b
|
haftmann@24219
|
817 |
:: map (pr "|") bs
|
haftmann@24219
|
818 |
)
|
haftmann@24219
|
819 |
end
|
haftmann@24219
|
820 |
| pr_case vars fxy ((_, []), _) = str "failwith \"empty case\"";
|
haftmann@24219
|
821 |
fun pr_def (MLFuns (funns as funn :: funns')) =
|
haftmann@24219
|
822 |
let
|
haftmann@24219
|
823 |
fun fish_parm _ (w as SOME _) = w
|
haftmann@24219
|
824 |
| fish_parm (IVar v) NONE = SOME v
|
haftmann@24219
|
825 |
| fish_parm _ NONE = NONE;
|
haftmann@24219
|
826 |
fun fillup_parm _ (_, SOME v) = v
|
haftmann@24219
|
827 |
| fillup_parm x (i, NONE) = x ^ string_of_int i;
|
haftmann@24219
|
828 |
fun fish_parms vars eqs =
|
haftmann@24219
|
829 |
let
|
haftmann@24294
|
830 |
val fished1 = fold (map2 fish_parm) eqs (replicate (length (hd eqs)) NONE);
|
haftmann@24294
|
831 |
val x = Name.variant (map_filter I fished1) "x";
|
haftmann@24294
|
832 |
val fished2 = map_index (fillup_parm x) fished1;
|
haftmann@24294
|
833 |
val (fished3, _) = Name.variants fished2 Name.context;
|
haftmann@24294
|
834 |
val vars' = CodeName.intro_vars fished3 vars;
|
haftmann@24294
|
835 |
in map (CodeName.lookup_var vars') fished3 end;
|
haftmann@24219
|
836 |
fun pr_eq (ts, t) =
|
haftmann@24219
|
837 |
let
|
haftmann@24219
|
838 |
val consts = map_filter
|
haftmann@24219
|
839 |
(fn c => if (is_some o const_syntax) c
|
haftmann@24219
|
840 |
then NONE else (SOME o NameSpace.base o deresolv) c)
|
haftmann@24219
|
841 |
((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []);
|
haftmann@24219
|
842 |
val vars = init_syms
|
haftmann@24219
|
843 |
|> CodeName.intro_vars consts
|
haftmann@24219
|
844 |
|> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames)
|
haftmann@24219
|
845 |
(insert (op =)) ts []);
|
haftmann@24219
|
846 |
in concat [
|
haftmann@24219
|
847 |
(Pretty.block o Pretty.commas) (map (pr_term true vars NOBR) ts),
|
haftmann@24219
|
848 |
str "->",
|
haftmann@24219
|
849 |
pr_term false vars NOBR t
|
haftmann@24219
|
850 |
] end;
|
haftmann@24841
|
851 |
fun pr_eqs name ty [] =
|
haftmann@24841
|
852 |
let
|
haftmann@24841
|
853 |
val n = (length o fst o CodeThingol.unfold_fun) ty;
|
haftmann@24992
|
854 |
val exc_str =
|
haftmann@24992
|
855 |
(ML_Syntax.print_string o NameSpace.base o NameSpace.qualifier) name;
|
haftmann@24841
|
856 |
in
|
haftmann@24841
|
857 |
concat (
|
haftmann@24841
|
858 |
map str (replicate n "_")
|
haftmann@24841
|
859 |
@ str "="
|
haftmann@24841
|
860 |
:: str "failwith"
|
haftmann@24992
|
861 |
@@ str exc_str
|
haftmann@24841
|
862 |
)
|
haftmann@24841
|
863 |
end
|
haftmann@24841
|
864 |
| pr_eqs _ _ [(ts, t)] =
|
haftmann@24219
|
865 |
let
|
haftmann@24219
|
866 |
val consts = map_filter
|
haftmann@24219
|
867 |
(fn c => if (is_some o const_syntax) c
|
haftmann@24219
|
868 |
then NONE else (SOME o NameSpace.base o deresolv) c)
|
haftmann@24219
|
869 |
((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []);
|
haftmann@24219
|
870 |
val vars = init_syms
|
haftmann@24219
|
871 |
|> CodeName.intro_vars consts
|
haftmann@24219
|
872 |
|> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames)
|
haftmann@24219
|
873 |
(insert (op =)) ts []);
|
haftmann@24219
|
874 |
in
|
haftmann@24219
|
875 |
concat (
|
haftmann@24219
|
876 |
map (pr_term true vars BR) ts
|
haftmann@24219
|
877 |
@ str "="
|
haftmann@24219
|
878 |
@@ pr_term false vars NOBR t
|
haftmann@24219
|
879 |
)
|
haftmann@24219
|
880 |
end
|
haftmann@24841
|
881 |
| pr_eqs _ _ (eqs as (eq as ([_], _)) :: eqs') =
|
haftmann@24219
|
882 |
Pretty.block (
|
haftmann@24219
|
883 |
str "="
|
haftmann@24219
|
884 |
:: Pretty.brk 1
|
haftmann@24219
|
885 |
:: str "function"
|
haftmann@24219
|
886 |
:: Pretty.brk 1
|
haftmann@24219
|
887 |
:: pr_eq eq
|
haftmann@24992
|
888 |
:: maps (append [Pretty.fbrk, str "|", Pretty.brk 1]
|
haftmann@24992
|
889 |
o single o pr_eq) eqs'
|
haftmann@24219
|
890 |
)
|
haftmann@24841
|
891 |
| pr_eqs _ _ (eqs as eq :: eqs') =
|
haftmann@24219
|
892 |
let
|
haftmann@24219
|
893 |
val consts = map_filter
|
haftmann@24219
|
894 |
(fn c => if (is_some o const_syntax) c
|
haftmann@24219
|
895 |
then NONE else (SOME o NameSpace.base o deresolv) c)
|
haftmann@24992
|
896 |
((fold o CodeThingol.fold_constnames)
|
haftmann@24992
|
897 |
(insert (op =)) (map snd eqs) []);
|
haftmann@24219
|
898 |
val vars = init_syms
|
haftmann@24219
|
899 |
|> CodeName.intro_vars consts;
|
haftmann@24219
|
900 |
val dummy_parms = (map str o fish_parms vars o map fst) eqs;
|
haftmann@24219
|
901 |
in
|
haftmann@24219
|
902 |
Pretty.block (
|
haftmann@24219
|
903 |
Pretty.breaks dummy_parms
|
haftmann@24219
|
904 |
@ Pretty.brk 1
|
haftmann@24219
|
905 |
:: str "="
|
haftmann@24219
|
906 |
:: Pretty.brk 1
|
haftmann@24219
|
907 |
:: str "match"
|
haftmann@24219
|
908 |
:: Pretty.brk 1
|
haftmann@24219
|
909 |
:: (Pretty.block o Pretty.commas) dummy_parms
|
haftmann@24219
|
910 |
:: Pretty.brk 1
|
haftmann@24219
|
911 |
:: str "with"
|
haftmann@24219
|
912 |
:: Pretty.brk 1
|
haftmann@24219
|
913 |
:: pr_eq eq
|
haftmann@24992
|
914 |
:: maps (append [Pretty.fbrk, str "|", Pretty.brk 1]
|
haftmann@24992
|
915 |
o single o pr_eq) eqs'
|
haftmann@24219
|
916 |
)
|
haftmann@24219
|
917 |
end;
|
haftmann@24381
|
918 |
fun pr_funn definer (name, ((vs, ty), eqs)) =
|
haftmann@24219
|
919 |
concat (
|
haftmann@24219
|
920 |
str definer
|
haftmann@24219
|
921 |
:: (str o deresolv) name
|
haftmann@24219
|
922 |
:: pr_tyvars (filter_out (null o snd) vs)
|
haftmann@24841
|
923 |
@| pr_eqs name ty eqs
|
haftmann@24219
|
924 |
);
|
haftmann@24992
|
925 |
val (ps, p) = split_last
|
haftmann@24992
|
926 |
(pr_funn "let rec" funn :: map (pr_funn "and") funns');
|
haftmann@24219
|
927 |
in Pretty.chunks (ps @ [Pretty.block ([p, str ";;"])]) end
|
haftmann@24219
|
928 |
| pr_def (MLDatas (datas as (data :: datas'))) =
|
haftmann@24219
|
929 |
let
|
haftmann@24219
|
930 |
fun pr_co (co, []) =
|
haftmann@24219
|
931 |
str (deresolv co)
|
haftmann@24219
|
932 |
| pr_co (co, tys) =
|
haftmann@24219
|
933 |
concat [
|
haftmann@24219
|
934 |
str (deresolv co),
|
haftmann@24219
|
935 |
str "of",
|
haftmann@24219
|
936 |
Pretty.enum " *" "" "" (map (pr_typ (INFX (2, X))) tys)
|
haftmann@24219
|
937 |
];
|
haftmann@24219
|
938 |
fun pr_data definer (tyco, (vs, [])) =
|
haftmann@24219
|
939 |
concat (
|
haftmann@24219
|
940 |
str definer
|
haftmann@24219
|
941 |
:: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs)
|
haftmann@24219
|
942 |
:: str "="
|
haftmann@24219
|
943 |
@@ str "EMPTY_"
|
haftmann@24219
|
944 |
)
|
haftmann@24219
|
945 |
| pr_data definer (tyco, (vs, cos)) =
|
haftmann@24219
|
946 |
concat (
|
haftmann@24219
|
947 |
str definer
|
haftmann@24219
|
948 |
:: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs)
|
haftmann@24219
|
949 |
:: str "="
|
haftmann@24219
|
950 |
:: separate (str "|") (map pr_co cos)
|
haftmann@24219
|
951 |
);
|
haftmann@24992
|
952 |
val (ps, p) = split_last
|
haftmann@24992
|
953 |
(pr_data "type" data :: map (pr_data "and") datas');
|
haftmann@24219
|
954 |
in Pretty.chunks (ps @ [Pretty.block ([p, str ";;"])]) end
|
haftmann@24841
|
955 |
| pr_def (MLClass (class, (v, (superclasses, classparams)))) =
|
haftmann@24219
|
956 |
let
|
haftmann@24219
|
957 |
val w = "_" ^ first_upper v;
|
haftmann@24219
|
958 |
fun pr_superclass_field (class, classrel) =
|
haftmann@24219
|
959 |
(concat o map str) [
|
haftmann@24219
|
960 |
deresolv classrel, ":", "'" ^ v, deresolv class
|
haftmann@24219
|
961 |
];
|
haftmann@24841
|
962 |
fun pr_classparam_field (classparam, ty) =
|
haftmann@24219
|
963 |
concat [
|
haftmann@24841
|
964 |
(str o deresolv) classparam, str ":", pr_typ NOBR ty
|
haftmann@24219
|
965 |
];
|
haftmann@24841
|
966 |
fun pr_classparam_proj (classparam, _) =
|
haftmann@24219
|
967 |
concat [
|
haftmann@24219
|
968 |
str "let",
|
haftmann@24841
|
969 |
(str o deresolv) classparam,
|
haftmann@24219
|
970 |
str w,
|
haftmann@24219
|
971 |
str "=",
|
haftmann@24841
|
972 |
str (w ^ "." ^ deresolv classparam ^ ";;")
|
haftmann@24219
|
973 |
];
|
haftmann@24219
|
974 |
in Pretty.chunks (
|
haftmann@24219
|
975 |
concat [
|
haftmann@24219
|
976 |
str ("type '" ^ v),
|
haftmann@24219
|
977 |
(str o deresolv) class,
|
haftmann@24219
|
978 |
str "=",
|
haftmann@25771
|
979 |
enum_default "();;" ";" "{" "};;" (
|
haftmann@24992
|
980 |
map pr_superclass_field superclasses
|
haftmann@24992
|
981 |
@ map pr_classparam_field classparams
|
haftmann@24219
|
982 |
)
|
haftmann@24219
|
983 |
]
|
haftmann@24841
|
984 |
:: map pr_classparam_proj classparams
|
haftmann@24219
|
985 |
) end
|
haftmann@24841
|
986 |
| pr_def (MLClassinst (inst, ((class, (tyco, arity)), (superarities, classparam_insts)))) =
|
haftmann@24219
|
987 |
let
|
haftmann@24219
|
988 |
fun pr_superclass (_, (classrel, dss)) =
|
haftmann@24219
|
989 |
concat [
|
haftmann@24219
|
990 |
(str o deresolv) classrel,
|
haftmann@24219
|
991 |
str "=",
|
haftmann@24219
|
992 |
pr_dicts NOBR [DictConst dss]
|
haftmann@24219
|
993 |
];
|
haftmann@24841
|
994 |
fun pr_classparam_inst (classparam, c_inst) =
|
haftmann@24591
|
995 |
concat [
|
haftmann@24841
|
996 |
(str o deresolv) classparam,
|
haftmann@24591
|
997 |
str "=",
|
haftmann@24591
|
998 |
pr_app false init_syms NOBR (c_inst, [])
|
haftmann@24591
|
999 |
];
|
haftmann@24219
|
1000 |
in
|
haftmann@24219
|
1001 |
concat (
|
haftmann@24219
|
1002 |
str "let"
|
haftmann@24219
|
1003 |
:: (str o deresolv) inst
|
haftmann@24219
|
1004 |
:: pr_tyvars arity
|
haftmann@24219
|
1005 |
@ str "="
|
haftmann@24219
|
1006 |
@@ (Pretty.enclose "(" ");;" o Pretty.breaks) [
|
haftmann@25771
|
1007 |
enum_default "()" ";" "{" "}" (map pr_superclass superarities
|
haftmann@24992
|
1008 |
@ map pr_classparam_inst classparam_insts),
|
haftmann@24219
|
1009 |
str ":",
|
haftmann@24219
|
1010 |
pr_tycoexpr NOBR (class, [tyco `%% map (ITyVar o fst) arity])
|
haftmann@24219
|
1011 |
]
|
haftmann@24219
|
1012 |
)
|
haftmann@24219
|
1013 |
end;
|
haftmann@24219
|
1014 |
in pr_def ml_def end;
|
haftmann@24219
|
1015 |
|
haftmann@24219
|
1016 |
fun pr_ocaml_modl name content =
|
haftmann@24219
|
1017 |
Pretty.chunks ([
|
haftmann@24219
|
1018 |
str ("module " ^ name ^ " = "),
|
haftmann@24219
|
1019 |
str "struct",
|
haftmann@24219
|
1020 |
str ""
|
haftmann@24219
|
1021 |
] @ content @ [
|
haftmann@24219
|
1022 |
str "",
|
haftmann@24219
|
1023 |
str ("end;; (*struct " ^ name ^ "*)")
|
haftmann@24219
|
1024 |
]);
|
haftmann@24219
|
1025 |
|
haftmann@27000
|
1026 |
fun seri_ml internal output_cont pr_def pr_modl module labelled_name reserved_syms includes raw_module_alias
|
haftmann@27000
|
1027 |
(_ : string -> class_syntax option) tyco_syntax const_syntax code cs seri_dest =
|
haftmann@24219
|
1028 |
let
|
haftmann@24219
|
1029 |
val module_alias = if is_some module then K module else raw_module_alias;
|
haftmann@24219
|
1030 |
val is_cons = CodeThingol.is_cons code;
|
haftmann@24219
|
1031 |
datatype node =
|
haftmann@24219
|
1032 |
Def of string * ml_def option
|
haftmann@24219
|
1033 |
| Module of string * ((Name.context * Name.context) * node Graph.T);
|
haftmann@24219
|
1034 |
val init_names = Name.make_context reserved_syms;
|
haftmann@24219
|
1035 |
val init_module = ((init_names, init_names), Graph.empty);
|
haftmann@24219
|
1036 |
fun map_node [] f = f
|
haftmann@24219
|
1037 |
| map_node (m::ms) f =
|
haftmann@24219
|
1038 |
Graph.default_node (m, Module (m, init_module))
|
haftmann@24992
|
1039 |
#> Graph.map_node m (fn (Module (dmodlname, (nsp, nodes))) =>
|
haftmann@24992
|
1040 |
Module (dmodlname, (nsp, map_node ms f nodes)));
|
haftmann@24219
|
1041 |
fun map_nsp_yield [] f (nsp, nodes) =
|
haftmann@24219
|
1042 |
let
|
haftmann@24219
|
1043 |
val (x, nsp') = f nsp
|
haftmann@24219
|
1044 |
in (x, (nsp', nodes)) end
|
haftmann@24219
|
1045 |
| map_nsp_yield (m::ms) f (nsp, nodes) =
|
haftmann@24219
|
1046 |
let
|
haftmann@24219
|
1047 |
val (x, nodes') =
|
haftmann@24219
|
1048 |
nodes
|
haftmann@24219
|
1049 |
|> Graph.default_node (m, Module (m, init_module))
|
haftmann@24219
|
1050 |
|> Graph.map_node_yield m (fn Module (dmodlname, nsp_nodes) =>
|
haftmann@24219
|
1051 |
let
|
haftmann@24219
|
1052 |
val (x, nsp_nodes') = map_nsp_yield ms f nsp_nodes
|
haftmann@24219
|
1053 |
in (x, Module (dmodlname, nsp_nodes')) end)
|
haftmann@24219
|
1054 |
in (x, (nsp, nodes')) end;
|
haftmann@24219
|
1055 |
val init_syms = CodeName.make_vars reserved_syms;
|
haftmann@24219
|
1056 |
val name_modl = mk_modl_name_tab init_names NONE module_alias code;
|
haftmann@24219
|
1057 |
fun name_def upper name nsp =
|
haftmann@24219
|
1058 |
let
|
haftmann@24219
|
1059 |
val (_, base) = dest_name name;
|
haftmann@24219
|
1060 |
val base' = if upper then first_upper base else base;
|
haftmann@24219
|
1061 |
val ([base''], nsp') = Name.variants [base'] nsp;
|
haftmann@24219
|
1062 |
in (base'', nsp') end;
|
haftmann@24219
|
1063 |
fun map_nsp_fun f (nsp_fun, nsp_typ) =
|
haftmann@24219
|
1064 |
let
|
haftmann@24219
|
1065 |
val (x, nsp_fun') = f nsp_fun
|
haftmann@24219
|
1066 |
in (x, (nsp_fun', nsp_typ)) end;
|
haftmann@24219
|
1067 |
fun map_nsp_typ f (nsp_fun, nsp_typ) =
|
haftmann@24219
|
1068 |
let
|
haftmann@24219
|
1069 |
val (x, nsp_typ') = f nsp_typ
|
haftmann@24219
|
1070 |
in (x, (nsp_fun, nsp_typ')) end;
|
haftmann@24219
|
1071 |
fun mk_funs defs =
|
haftmann@24219
|
1072 |
fold_map
|
haftmann@24219
|
1073 |
(fn (name, CodeThingol.Fun info) =>
|
haftmann@24992
|
1074 |
map_nsp_fun (name_def false name) >>
|
haftmann@24992
|
1075 |
(fn base => (base, (name, (apsnd o map) fst info)))
|
haftmann@24992
|
1076 |
| (name, def) =>
|
haftmann@27024
|
1077 |
error ("Function block containing illegal statement: " ^ labelled_name name)
|
haftmann@24219
|
1078 |
) defs
|
haftmann@24219
|
1079 |
>> (split_list #> apsnd MLFuns);
|
haftmann@24219
|
1080 |
fun mk_datatype defs =
|
haftmann@24219
|
1081 |
fold_map
|
haftmann@24219
|
1082 |
(fn (name, CodeThingol.Datatype info) =>
|
haftmann@24219
|
1083 |
map_nsp_typ (name_def false name) >> (fn base => (base, SOME (name, info)))
|
haftmann@24219
|
1084 |
| (name, CodeThingol.Datatypecons _) =>
|
haftmann@24219
|
1085 |
map_nsp_fun (name_def true name) >> (fn base => (base, NONE))
|
haftmann@24992
|
1086 |
| (name, def) =>
|
haftmann@27024
|
1087 |
error ("Datatype block containing illegal statement: " ^ labelled_name name)
|
haftmann@24219
|
1088 |
) defs
|
haftmann@24219
|
1089 |
>> (split_list #> apsnd (map_filter I
|
haftmann@27024
|
1090 |
#> (fn [] => error ("Datatype block without data statement: "
|
haftmann@24992
|
1091 |
^ (commas o map (labelled_name o fst)) defs)
|
haftmann@24219
|
1092 |
| infos => MLDatas infos)));
|
haftmann@24219
|
1093 |
fun mk_class defs =
|
haftmann@24219
|
1094 |
fold_map
|
haftmann@24219
|
1095 |
(fn (name, CodeThingol.Class info) =>
|
haftmann@24219
|
1096 |
map_nsp_typ (name_def false name) >> (fn base => (base, SOME (name, info)))
|
haftmann@24219
|
1097 |
| (name, CodeThingol.Classrel _) =>
|
haftmann@24219
|
1098 |
map_nsp_fun (name_def false name) >> (fn base => (base, NONE))
|
haftmann@24841
|
1099 |
| (name, CodeThingol.Classparam _) =>
|
haftmann@24219
|
1100 |
map_nsp_fun (name_def false name) >> (fn base => (base, NONE))
|
haftmann@24992
|
1101 |
| (name, def) =>
|
haftmann@27024
|
1102 |
error ("Class block containing illegal statement: " ^ labelled_name name)
|
haftmann@24219
|
1103 |
) defs
|
haftmann@24219
|
1104 |
>> (split_list #> apsnd (map_filter I
|
haftmann@27024
|
1105 |
#> (fn [] => error ("Class block without class statement: "
|
haftmann@24992
|
1106 |
^ (commas o map (labelled_name o fst)) defs)
|
haftmann@24219
|
1107 |
| [info] => MLClass info)));
|
haftmann@24219
|
1108 |
fun mk_inst [(name, CodeThingol.Classinst info)] =
|
haftmann@24219
|
1109 |
map_nsp_fun (name_def false name)
|
haftmann@24591
|
1110 |
>> (fn base => ([base], MLClassinst (name, (apsnd o apsnd o map) fst info)));
|
haftmann@24219
|
1111 |
fun add_group mk defs nsp_nodes =
|
haftmann@24219
|
1112 |
let
|
haftmann@24219
|
1113 |
val names as (name :: names') = map fst defs;
|
haftmann@24219
|
1114 |
val deps =
|
haftmann@24219
|
1115 |
[]
|
haftmann@24219
|
1116 |
|> fold (fold (insert (op =)) o Graph.imm_succs code) names
|
haftmann@24219
|
1117 |
|> subtract (op =) names;
|
haftmann@24219
|
1118 |
val (modls, _) = (split_list o map dest_name) names;
|
haftmann@24219
|
1119 |
val modl' = (the_single o distinct (op =) o map name_modl) modls
|
haftmann@24219
|
1120 |
handle Empty =>
|
haftmann@24811
|
1121 |
error ("Different namespace prefixes for mutual dependencies:\n"
|
haftmann@24811
|
1122 |
^ commas (map labelled_name names)
|
haftmann@24811
|
1123 |
^ "\n"
|
haftmann@24811
|
1124 |
^ commas (map (NameSpace.qualifier o NameSpace.qualifier) names));
|
haftmann@24219
|
1125 |
val modl_explode = NameSpace.explode modl';
|
haftmann@24219
|
1126 |
fun add_dep name name'' =
|
haftmann@24219
|
1127 |
let
|
haftmann@24219
|
1128 |
val modl'' = (name_modl o fst o dest_name) name'';
|
haftmann@24219
|
1129 |
in if modl' = modl'' then
|
haftmann@24219
|
1130 |
map_node modl_explode
|
haftmann@24219
|
1131 |
(Graph.add_edge (name, name''))
|
haftmann@24219
|
1132 |
else let
|
haftmann@24219
|
1133 |
val (common, (diff1::_, diff2::_)) = chop_prefix (op =)
|
haftmann@24219
|
1134 |
(modl_explode, NameSpace.explode modl'');
|
haftmann@24219
|
1135 |
in
|
haftmann@24219
|
1136 |
map_node common
|
haftmann@24219
|
1137 |
(fn gr => Graph.add_edge_acyclic (diff1, diff2) gr
|
haftmann@24219
|
1138 |
handle Graph.CYCLES _ => error ("Dependency "
|
haftmann@24219
|
1139 |
^ quote name ^ " -> " ^ quote name''
|
haftmann@24219
|
1140 |
^ " would result in module dependency cycle"))
|
haftmann@24219
|
1141 |
end end;
|
haftmann@24219
|
1142 |
in
|
haftmann@24219
|
1143 |
nsp_nodes
|
haftmann@24219
|
1144 |
|> map_nsp_yield modl_explode (mk defs)
|
haftmann@24219
|
1145 |
|-> (fn (base' :: bases', def') =>
|
haftmann@24219
|
1146 |
apsnd (map_node modl_explode (Graph.new_node (name, (Def (base', SOME def')))
|
haftmann@24992
|
1147 |
#> fold2 (fn name' => fn base' =>
|
haftmann@24992
|
1148 |
Graph.new_node (name', (Def (base', NONE)))) names' bases')))
|
haftmann@24219
|
1149 |
|> apsnd (fold (fn name => fold (add_dep name) deps) names)
|
haftmann@25538
|
1150 |
|> apsnd (fold_product (curry (map_node modl_explode o Graph.add_edge)) names names)
|
haftmann@24219
|
1151 |
end;
|
haftmann@27024
|
1152 |
fun group_defs ((defs as (_, CodeThingol.Fun _)::_)) =
|
haftmann@24219
|
1153 |
add_group mk_funs defs
|
haftmann@24219
|
1154 |
| group_defs ((defs as (_, CodeThingol.Datatypecons _)::_)) =
|
haftmann@24219
|
1155 |
add_group mk_datatype defs
|
haftmann@24219
|
1156 |
| group_defs ((defs as (_, CodeThingol.Datatype _)::_)) =
|
haftmann@24219
|
1157 |
add_group mk_datatype defs
|
haftmann@24219
|
1158 |
| group_defs ((defs as (_, CodeThingol.Class _)::_)) =
|
haftmann@24219
|
1159 |
add_group mk_class defs
|
haftmann@24219
|
1160 |
| group_defs ((defs as (_, CodeThingol.Classrel _)::_)) =
|
haftmann@24219
|
1161 |
add_group mk_class defs
|
haftmann@24841
|
1162 |
| group_defs ((defs as (_, CodeThingol.Classparam _)::_)) =
|
haftmann@24219
|
1163 |
add_group mk_class defs
|
haftmann@24219
|
1164 |
| group_defs ((defs as [(_, CodeThingol.Classinst _)])) =
|
haftmann@24219
|
1165 |
add_group mk_inst defs
|
haftmann@24219
|
1166 |
| group_defs defs = error ("Illegal mutual dependencies: " ^
|
haftmann@24219
|
1167 |
(commas o map (labelled_name o fst)) defs)
|
haftmann@24219
|
1168 |
val (_, nodes) =
|
haftmann@24219
|
1169 |
init_module
|
haftmann@24219
|
1170 |
|> fold group_defs (map (AList.make (Graph.get_node code))
|
haftmann@24219
|
1171 |
(rev (Graph.strong_conn code)))
|
haftmann@24219
|
1172 |
fun deresolver prefix name =
|
haftmann@24219
|
1173 |
let
|
haftmann@24219
|
1174 |
val modl = (fst o dest_name) name;
|
haftmann@24219
|
1175 |
val modl' = (NameSpace.explode o name_modl) modl;
|
haftmann@24219
|
1176 |
val (_, (_, remainder)) = chop_prefix (op =) (prefix, modl');
|
haftmann@24219
|
1177 |
val defname' =
|
haftmann@24219
|
1178 |
nodes
|
haftmann@24219
|
1179 |
|> fold (fn m => fn g => case Graph.get_node g m
|
haftmann@24219
|
1180 |
of Module (_, (_, g)) => g) modl'
|
haftmann@24219
|
1181 |
|> (fn g => case Graph.get_node g name of Def (defname, _) => defname);
|
haftmann@24219
|
1182 |
in
|
haftmann@24219
|
1183 |
NameSpace.implode (remainder @ [defname'])
|
haftmann@24219
|
1184 |
end handle Graph.UNDEF _ =>
|
haftmann@27024
|
1185 |
error ("Unknown statement name: " ^ labelled_name name);
|
haftmann@24219
|
1186 |
fun pr_node prefix (Def (_, NONE)) =
|
haftmann@24219
|
1187 |
NONE
|
haftmann@24219
|
1188 |
| pr_node prefix (Def (_, SOME def)) =
|
haftmann@24841
|
1189 |
SOME (pr_def tyco_syntax const_syntax labelled_name init_syms
|
haftmann@24219
|
1190 |
(deresolver prefix) is_cons def)
|
haftmann@24219
|
1191 |
| pr_node prefix (Module (dmodlname, (_, nodes))) =
|
haftmann@24992
|
1192 |
SOME (pr_modl dmodlname (
|
haftmann@24992
|
1193 |
separate (str "")
|
haftmann@24992
|
1194 |
((map_filter (pr_node (prefix @ [dmodlname]) o Graph.get_node nodes)
|
haftmann@24219
|
1195 |
o rev o flat o Graph.strong_conn) nodes)));
|
haftmann@24992
|
1196 |
val p = Pretty.chunks (separate (str "") (map snd includes @ (map_filter
|
haftmann@26113
|
1197 |
(pr_node [] o Graph.get_node nodes) o rev o flat o Graph.strong_conn) nodes));
|
haftmann@26113
|
1198 |
val deresolv = try (deresolver (if is_some module then the_list module else []));
|
haftmann@27000
|
1199 |
val output = case seri_dest
|
haftmann@27024
|
1200 |
of Compile => K NONE o internal o target_code_of_pretty
|
haftmann@27024
|
1201 |
| Write => K NONE o target_code_writeln
|
haftmann@27024
|
1202 |
| File file => K NONE o File.write file o target_code_of_pretty
|
haftmann@27024
|
1203 |
| String => SOME o target_code_of_pretty;
|
haftmann@27000
|
1204 |
in output_cont (map_filter deresolv cs, output p) end;
|
haftmann@24219
|
1205 |
|
haftmann@27000
|
1206 |
fun isar_seri_sml module =
|
haftmann@27000
|
1207 |
parse_args (Scan.succeed ())
|
haftmann@27000
|
1208 |
#> (fn () => seri_ml (use_text (1, "generated code") Output.ml_output false) snd pr_sml pr_sml_modl module);
|
haftmann@24219
|
1209 |
|
haftmann@27000
|
1210 |
fun isar_seri_ocaml module =
|
haftmann@27000
|
1211 |
parse_args (Scan.succeed ())
|
haftmann@27000
|
1212 |
#> (fn () => seri_ml (fn _ => error "OCaml: no internal compilation") snd pr_ocaml pr_ocaml_modl module)
|
haftmann@24219
|
1213 |
|
haftmann@24219
|
1214 |
|
haftmann@24219
|
1215 |
(** Haskell serializer **)
|
haftmann@24219
|
1216 |
|
haftmann@24219
|
1217 |
local
|
haftmann@24219
|
1218 |
|
haftmann@24219
|
1219 |
fun pr_bind' ((NONE, NONE), _) = str "_"
|
haftmann@24219
|
1220 |
| pr_bind' ((SOME v, NONE), _) = str v
|
haftmann@24219
|
1221 |
| pr_bind' ((NONE, SOME p), _) = p
|
haftmann@24219
|
1222 |
| pr_bind' ((SOME v, SOME p), _) = brackets [str v, str "@", p]
|
haftmann@24219
|
1223 |
|
haftmann@24219
|
1224 |
val pr_bind_haskell = gen_pr_bind pr_bind';
|
haftmann@24219
|
1225 |
|
haftmann@24219
|
1226 |
in
|
haftmann@24219
|
1227 |
|
haftmann@24841
|
1228 |
fun pr_haskell class_syntax tyco_syntax const_syntax labelled_name
|
haftmann@25621
|
1229 |
init_syms deresolv_here deresolv is_cons contr_classparam_typs deriving_show def =
|
haftmann@24219
|
1230 |
let
|
haftmann@24219
|
1231 |
fun class_name class = case class_syntax class
|
haftmann@24219
|
1232 |
of NONE => deresolv class
|
haftmann@24219
|
1233 |
| SOME (class, _) => class;
|
haftmann@24841
|
1234 |
fun classparam_name class classparam = case class_syntax class
|
haftmann@24841
|
1235 |
of NONE => deresolv_here classparam
|
haftmann@24841
|
1236 |
| SOME (_, classparam_syntax) => case classparam_syntax classparam
|
haftmann@24841
|
1237 |
of NONE => (snd o dest_name) classparam
|
haftmann@25621
|
1238 |
| SOME classparam => classparam;
|
haftmann@25621
|
1239 |
fun pr_typcontext tyvars vs = case maps (fn (v, sort) => map (pair v) sort) vs
|
haftmann@25621
|
1240 |
of [] => []
|
haftmann@25621
|
1241 |
| classbinds => Pretty.enum "," "(" ")" (
|
haftmann@25621
|
1242 |
map (fn (v, class) =>
|
haftmann@25621
|
1243 |
str (class_name class ^ " " ^ CodeName.lookup_var tyvars v)) classbinds)
|
haftmann@25621
|
1244 |
@@ str " => ";
|
haftmann@25621
|
1245 |
fun pr_typforall tyvars vs = case map fst vs
|
haftmann@25621
|
1246 |
of [] => []
|
haftmann@25621
|
1247 |
| vnames => str "forall " :: Pretty.breaks
|
haftmann@25621
|
1248 |
(map (str o CodeName.lookup_var tyvars) vnames) @ str "." @@ Pretty.brk 1;
|
haftmann@24219
|
1249 |
fun pr_tycoexpr tyvars fxy (tyco, tys) =
|
haftmann@24219
|
1250 |
brackify fxy (str tyco :: map (pr_typ tyvars BR) tys)
|
haftmann@24219
|
1251 |
and pr_typ tyvars fxy (tycoexpr as tyco `%% tys) =
|
haftmann@24219
|
1252 |
(case tyco_syntax tyco
|
haftmann@24219
|
1253 |
of NONE =>
|
haftmann@24219
|
1254 |
pr_tycoexpr tyvars fxy (deresolv tyco, tys)
|
haftmann@24219
|
1255 |
| SOME (i, pr) =>
|
haftmann@24219
|
1256 |
if not (i = length tys)
|
haftmann@24219
|
1257 |
then error ("Number of argument mismatch in customary serialization: "
|
haftmann@24219
|
1258 |
^ (string_of_int o length) tys ^ " given, "
|
haftmann@24219
|
1259 |
^ string_of_int i ^ " expected")
|
haftmann@24219
|
1260 |
else pr (pr_typ tyvars) fxy tys)
|
haftmann@24219
|
1261 |
| pr_typ tyvars fxy (ITyVar v) =
|
haftmann@24219
|
1262 |
(str o CodeName.lookup_var tyvars) v;
|
haftmann@25621
|
1263 |
fun pr_typdecl tyvars (vs, tycoexpr) =
|
haftmann@25621
|
1264 |
Pretty.block (pr_typcontext tyvars vs @| pr_tycoexpr tyvars NOBR tycoexpr);
|
haftmann@24219
|
1265 |
fun pr_typscheme tyvars (vs, ty) =
|
haftmann@25621
|
1266 |
Pretty.block (pr_typforall tyvars vs @ pr_typcontext tyvars vs @| pr_typ tyvars NOBR ty);
|
haftmann@25621
|
1267 |
fun pr_term tyvars lhs vars fxy (IConst c) =
|
haftmann@25621
|
1268 |
pr_app tyvars lhs vars fxy (c, [])
|
haftmann@25621
|
1269 |
| pr_term tyvars lhs vars fxy (t as (t1 `$ t2)) =
|
haftmann@24219
|
1270 |
(case CodeThingol.unfold_const_app t
|
haftmann@25621
|
1271 |
of SOME app => pr_app tyvars lhs vars fxy app
|
haftmann@24219
|
1272 |
| _ =>
|
haftmann@24219
|
1273 |
brackify fxy [
|
haftmann@25621
|
1274 |
pr_term tyvars lhs vars NOBR t1,
|
haftmann@25621
|
1275 |
pr_term tyvars lhs vars BR t2
|
haftmann@24219
|
1276 |
])
|
haftmann@25621
|
1277 |
| pr_term tyvars lhs vars fxy (IVar v) =
|
haftmann@24219
|
1278 |
(str o CodeName.lookup_var vars) v
|
haftmann@25621
|
1279 |
| pr_term tyvars lhs vars fxy (t as _ `|-> _) =
|
haftmann@24219
|
1280 |
let
|
haftmann@24219
|
1281 |
val (binds, t') = CodeThingol.unfold_abs t;
|
haftmann@25621
|
1282 |
fun pr ((v, pat), ty) = pr_bind tyvars BR ((SOME v, pat), ty);
|
haftmann@24219
|
1283 |
val (ps, vars') = fold_map pr binds vars;
|
haftmann@25621
|
1284 |
in brackets (str "\\" :: ps @ str "->" @@ pr_term tyvars lhs vars' NOBR t') end
|
haftmann@25621
|
1285 |
| pr_term tyvars lhs vars fxy (ICase (cases as (_, t0))) =
|
haftmann@24992
|
1286 |
(case CodeThingol.unfold_const_app t0
|
haftmann@24219
|
1287 |
of SOME (c_ts as ((c, _), _)) => if is_none (const_syntax c)
|
haftmann@25621
|
1288 |
then pr_case tyvars vars fxy cases
|
haftmann@25621
|
1289 |
else pr_app tyvars lhs vars fxy c_ts
|
haftmann@25621
|
1290 |
| NONE => pr_case tyvars vars fxy cases)
|
haftmann@25621
|
1291 |
and pr_app' tyvars lhs vars ((c, (_, tys)), ts) = case contr_classparam_typs c
|
haftmann@25621
|
1292 |
of [] => (str o deresolv) c :: map (pr_term tyvars lhs vars BR) ts
|
haftmann@25621
|
1293 |
| fingerprint => let
|
haftmann@25621
|
1294 |
val ts_fingerprint = ts ~~ curry Library.take (length ts) fingerprint;
|
haftmann@25621
|
1295 |
val needs_annotation = forall (fn (_, NONE) => true | (t, SOME _) =>
|
haftmann@25621
|
1296 |
(not o CodeThingol.locally_monomorphic) t) ts_fingerprint;
|
haftmann@25621
|
1297 |
fun pr_term_anno (t, NONE) _ = pr_term tyvars lhs vars BR t
|
haftmann@25621
|
1298 |
| pr_term_anno (t, SOME _) ty =
|
haftmann@25621
|
1299 |
brackets [pr_term tyvars lhs vars NOBR t, str "::", pr_typ tyvars NOBR ty];
|
haftmann@25621
|
1300 |
in
|
haftmann@25621
|
1301 |
if needs_annotation then
|
haftmann@25621
|
1302 |
(str o deresolv) c :: map2 pr_term_anno ts_fingerprint (curry Library.take (length ts) tys)
|
haftmann@25621
|
1303 |
else (str o deresolv) c :: map (pr_term tyvars lhs vars BR) ts
|
haftmann@25621
|
1304 |
end
|
haftmann@25621
|
1305 |
and pr_app tyvars lhs vars = gen_pr_app (pr_app' tyvars) (pr_term tyvars) const_syntax
|
haftmann@24992
|
1306 |
labelled_name is_cons lhs vars
|
haftmann@25621
|
1307 |
and pr_bind tyvars = pr_bind_haskell (pr_term tyvars)
|
haftmann@25621
|
1308 |
and pr_case tyvars vars fxy (cases as ((_, [_]), _)) =
|
haftmann@24219
|
1309 |
let
|
haftmann@24219
|
1310 |
val (binds, t) = CodeThingol.unfold_let (ICase cases);
|
haftmann@24219
|
1311 |
fun pr ((pat, ty), t) vars =
|
haftmann@24219
|
1312 |
vars
|
haftmann@25621
|
1313 |
|> pr_bind tyvars BR ((NONE, SOME pat), ty)
|
haftmann@25621
|
1314 |
|>> (fn p => semicolon [p, str "=", pr_term tyvars false vars NOBR t])
|
haftmann@24219
|
1315 |
val (ps, vars') = fold_map pr binds vars;
|
haftmann@24219
|
1316 |
in
|
haftmann@24219
|
1317 |
Pretty.block_enclose (
|
haftmann@24219
|
1318 |
str "let {",
|
haftmann@25621
|
1319 |
concat [str "}", str "in", pr_term tyvars false vars' NOBR t]
|
haftmann@24219
|
1320 |
) ps
|
haftmann@24219
|
1321 |
end
|
haftmann@25621
|
1322 |
| pr_case tyvars vars fxy (((td, ty), bs as _ :: _), _) =
|
haftmann@24219
|
1323 |
let
|
haftmann@24219
|
1324 |
fun pr (pat, t) =
|
haftmann@24219
|
1325 |
let
|
haftmann@25621
|
1326 |
val (p, vars') = pr_bind tyvars NOBR ((NONE, SOME pat), ty) vars;
|
haftmann@25621
|
1327 |
in semicolon [p, str "->", pr_term tyvars false vars' NOBR t] end;
|
haftmann@24219
|
1328 |
in
|
haftmann@24219
|
1329 |
Pretty.block_enclose (
|
haftmann@25621
|
1330 |
concat [str "(case", pr_term tyvars false vars NOBR td, str "of", str "{"],
|
haftmann@24219
|
1331 |
str "})"
|
haftmann@24219
|
1332 |
) (map pr bs)
|
haftmann@24219
|
1333 |
end
|
haftmann@25621
|
1334 |
| pr_case tyvars vars fxy ((_, []), _) = str "error \"empty case\"";
|
haftmann@24841
|
1335 |
fun pr_def (name, CodeThingol.Fun ((vs, ty), [])) =
|
haftmann@24841
|
1336 |
let
|
haftmann@24841
|
1337 |
val tyvars = CodeName.intro_vars (map fst vs) init_syms;
|
haftmann@24841
|
1338 |
val n = (length o fst o CodeThingol.unfold_fun) ty;
|
haftmann@24841
|
1339 |
in
|
haftmann@24841
|
1340 |
Pretty.chunks [
|
haftmann@24841
|
1341 |
Pretty.block [
|
haftmann@24841
|
1342 |
(str o suffix " ::" o deresolv_here) name,
|
haftmann@24841
|
1343 |
Pretty.brk 1,
|
haftmann@24841
|
1344 |
pr_typscheme tyvars (vs, ty),
|
haftmann@24841
|
1345 |
str ";"
|
haftmann@24841
|
1346 |
],
|
haftmann@24841
|
1347 |
concat (
|
haftmann@24841
|
1348 |
(str o deresolv_here) name
|
haftmann@24841
|
1349 |
:: map str (replicate n "_")
|
haftmann@24841
|
1350 |
@ str "="
|
haftmann@24841
|
1351 |
:: str "error"
|
haftmann@24992
|
1352 |
@@ (str o (fn s => s ^ ";") o ML_Syntax.print_string
|
haftmann@24992
|
1353 |
o NameSpace.base o NameSpace.qualifier) name
|
haftmann@24841
|
1354 |
)
|
haftmann@24841
|
1355 |
]
|
haftmann@24841
|
1356 |
end
|
haftmann@24841
|
1357 |
| pr_def (name, CodeThingol.Fun ((vs, ty), eqs)) =
|
haftmann@24219
|
1358 |
let
|
haftmann@24219
|
1359 |
val tyvars = CodeName.intro_vars (map fst vs) init_syms;
|
haftmann@24591
|
1360 |
fun pr_eq ((ts, t), _) =
|
haftmann@24219
|
1361 |
let
|
haftmann@24219
|
1362 |
val consts = map_filter
|
haftmann@24219
|
1363 |
(fn c => if (is_some o const_syntax) c
|
haftmann@24219
|
1364 |
then NONE else (SOME o NameSpace.base o deresolv) c)
|
haftmann@24219
|
1365 |
((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []);
|
haftmann@24219
|
1366 |
val vars = init_syms
|
haftmann@24219
|
1367 |
|> CodeName.intro_vars consts
|
haftmann@24219
|
1368 |
|> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames)
|
haftmann@24219
|
1369 |
(insert (op =)) ts []);
|
haftmann@24219
|
1370 |
in
|
haftmann@24219
|
1371 |
semicolon (
|
haftmann@24219
|
1372 |
(str o deresolv_here) name
|
haftmann@25621
|
1373 |
:: map (pr_term tyvars true vars BR) ts
|
haftmann@24219
|
1374 |
@ str "="
|
haftmann@25621
|
1375 |
@@ pr_term tyvars false vars NOBR t
|
haftmann@24219
|
1376 |
)
|
haftmann@24219
|
1377 |
end;
|
haftmann@24219
|
1378 |
in
|
haftmann@24219
|
1379 |
Pretty.chunks (
|
haftmann@24219
|
1380 |
Pretty.block [
|
haftmann@24219
|
1381 |
(str o suffix " ::" o deresolv_here) name,
|
haftmann@24219
|
1382 |
Pretty.brk 1,
|
haftmann@24219
|
1383 |
pr_typscheme tyvars (vs, ty),
|
haftmann@24219
|
1384 |
str ";"
|
haftmann@24219
|
1385 |
]
|
haftmann@24219
|
1386 |
:: map pr_eq eqs
|
haftmann@24219
|
1387 |
)
|
haftmann@24219
|
1388 |
end
|
haftmann@24219
|
1389 |
| pr_def (name, CodeThingol.Datatype (vs, [])) =
|
haftmann@24219
|
1390 |
let
|
haftmann@24219
|
1391 |
val tyvars = CodeName.intro_vars (map fst vs) init_syms;
|
haftmann@24219
|
1392 |
in
|
haftmann@24219
|
1393 |
semicolon [
|
haftmann@24219
|
1394 |
str "data",
|
haftmann@25621
|
1395 |
pr_typdecl tyvars (vs, (deresolv_here name, map (ITyVar o fst) vs))
|
haftmann@24219
|
1396 |
]
|
haftmann@24219
|
1397 |
end
|
haftmann@24219
|
1398 |
| pr_def (name, CodeThingol.Datatype (vs, [(co, [ty])])) =
|
haftmann@24219
|
1399 |
let
|
haftmann@24219
|
1400 |
val tyvars = CodeName.intro_vars (map fst vs) init_syms;
|
haftmann@24219
|
1401 |
in
|
haftmann@24219
|
1402 |
semicolon (
|
haftmann@24219
|
1403 |
str "newtype"
|
haftmann@25621
|
1404 |
:: pr_typdecl tyvars (vs, (deresolv_here name, map (ITyVar o fst) vs))
|
haftmann@24219
|
1405 |
:: str "="
|
haftmann@24219
|
1406 |
:: (str o deresolv_here) co
|
haftmann@24219
|
1407 |
:: pr_typ tyvars BR ty
|
haftmann@24219
|
1408 |
:: (if deriving_show name then [str "deriving (Read, Show)"] else [])
|
haftmann@24219
|
1409 |
)
|
haftmann@24219
|
1410 |
end
|
haftmann@24219
|
1411 |
| pr_def (name, CodeThingol.Datatype (vs, co :: cos)) =
|
haftmann@24219
|
1412 |
let
|
haftmann@24219
|
1413 |
val tyvars = CodeName.intro_vars (map fst vs) init_syms;
|
haftmann@24219
|
1414 |
fun pr_co (co, tys) =
|
haftmann@24219
|
1415 |
concat (
|
haftmann@24219
|
1416 |
(str o deresolv_here) co
|
haftmann@24219
|
1417 |
:: map (pr_typ tyvars BR) tys
|
haftmann@24219
|
1418 |
)
|
haftmann@24219
|
1419 |
in
|
haftmann@24219
|
1420 |
semicolon (
|
haftmann@24219
|
1421 |
str "data"
|
haftmann@25621
|
1422 |
:: pr_typdecl tyvars (vs, (deresolv_here name, map (ITyVar o fst) vs))
|
haftmann@24219
|
1423 |
:: str "="
|
haftmann@24219
|
1424 |
:: pr_co co
|
haftmann@24219
|
1425 |
:: map ((fn p => Pretty.block [str "| ", p]) o pr_co) cos
|
haftmann@24219
|
1426 |
@ (if deriving_show name then [str "deriving (Read, Show)"] else [])
|
haftmann@24219
|
1427 |
)
|
haftmann@24219
|
1428 |
end
|
haftmann@24841
|
1429 |
| pr_def (name, CodeThingol.Class (v, (superclasses, classparams))) =
|
haftmann@24219
|
1430 |
let
|
haftmann@24219
|
1431 |
val tyvars = CodeName.intro_vars [v] init_syms;
|
haftmann@24841
|
1432 |
fun pr_classparam (classparam, ty) =
|
haftmann@24219
|
1433 |
semicolon [
|
haftmann@24841
|
1434 |
(str o classparam_name name) classparam,
|
haftmann@24219
|
1435 |
str "::",
|
haftmann@24219
|
1436 |
pr_typ tyvars NOBR ty
|
haftmann@24219
|
1437 |
]
|
haftmann@24219
|
1438 |
in
|
haftmann@24219
|
1439 |
Pretty.block_enclose (
|
haftmann@24219
|
1440 |
Pretty.block [
|
haftmann@24219
|
1441 |
str "class ",
|
haftmann@25621
|
1442 |
Pretty.block (pr_typcontext tyvars [(v, map fst superclasses)]),
|
haftmann@24219
|
1443 |
str (deresolv_here name ^ " " ^ CodeName.lookup_var tyvars v),
|
haftmann@24219
|
1444 |
str " where {"
|
haftmann@24219
|
1445 |
],
|
haftmann@24219
|
1446 |
str "};"
|
haftmann@24841
|
1447 |
) (map pr_classparam classparams)
|
haftmann@24219
|
1448 |
end
|
haftmann@24841
|
1449 |
| pr_def (_, CodeThingol.Classinst ((class, (tyco, vs)), (_, classparam_insts))) =
|
haftmann@24219
|
1450 |
let
|
haftmann@24219
|
1451 |
val tyvars = CodeName.intro_vars (map fst vs) init_syms;
|
haftmann@24841
|
1452 |
fun pr_instdef ((classparam, c_inst), _) =
|
haftmann@24591
|
1453 |
semicolon [
|
haftmann@24841
|
1454 |
(str o classparam_name class) classparam,
|
haftmann@24591
|
1455 |
str "=",
|
haftmann@25621
|
1456 |
pr_app tyvars false init_syms NOBR (c_inst, [])
|
haftmann@24591
|
1457 |
];
|
haftmann@24219
|
1458 |
in
|
haftmann@24219
|
1459 |
Pretty.block_enclose (
|
haftmann@24219
|
1460 |
Pretty.block [
|
haftmann@24219
|
1461 |
str "instance ",
|
haftmann@25621
|
1462 |
Pretty.block (pr_typcontext tyvars vs),
|
haftmann@24219
|
1463 |
str (class_name class ^ " "),
|
haftmann@24219
|
1464 |
pr_typ tyvars BR (tyco `%% map (ITyVar o fst) vs),
|
haftmann@24219
|
1465 |
str " where {"
|
haftmann@24219
|
1466 |
],
|
haftmann@24219
|
1467 |
str "};"
|
haftmann@24841
|
1468 |
) (map pr_instdef classparam_insts)
|
haftmann@24219
|
1469 |
end;
|
haftmann@24219
|
1470 |
in pr_def def end;
|
haftmann@24219
|
1471 |
|
haftmann@24992
|
1472 |
fun pretty_haskell_monad c_bind =
|
haftmann@24219
|
1473 |
let
|
haftmann@24219
|
1474 |
fun pretty pr vars fxy [(t, _)] =
|
haftmann@24219
|
1475 |
let
|
haftmann@24284
|
1476 |
val pr_bind = pr_bind_haskell (K pr);
|
haftmann@24992
|
1477 |
fun pr_monad (NONE, t) vars =
|
haftmann@24219
|
1478 |
(semicolon [pr vars NOBR t], vars)
|
haftmann@24992
|
1479 |
| pr_monad (SOME (bind, true), t) vars = vars
|
haftmann@24219
|
1480 |
|> pr_bind NOBR bind
|
haftmann@24219
|
1481 |
|>> (fn p => semicolon [p, str "<-", pr vars NOBR t])
|
haftmann@24992
|
1482 |
| pr_monad (SOME (bind, false), t) vars = vars
|
haftmann@24219
|
1483 |
|> pr_bind NOBR bind
|
haftmann@24219
|
1484 |
|>> (fn p => semicolon [str "let", p, str "=", pr vars NOBR t]);
|
haftmann@24992
|
1485 |
val (binds, t) = implode_monad c_bind t;
|
haftmann@24992
|
1486 |
val (ps, vars') = fold_map pr_monad binds vars;
|
haftmann@26010
|
1487 |
fun brack p = if fixity BR fxy then Pretty.block [str "(", p, str ")"] else p;
|
haftmann@24219
|
1488 |
in (brack o Pretty.block_enclose (str "do {", str "}")) (ps @| pr vars' NOBR t) end;
|
haftmann@24219
|
1489 |
in (1, pretty) end;
|
haftmann@24219
|
1490 |
|
haftmann@24219
|
1491 |
end; (*local*)
|
haftmann@24219
|
1492 |
|
haftmann@27000
|
1493 |
fun seri_haskell module_prefix module string_classes labelled_name
|
haftmann@24992
|
1494 |
reserved_syms includes raw_module_alias
|
haftmann@27000
|
1495 |
class_syntax tyco_syntax const_syntax code cs seri_dest =
|
haftmann@24219
|
1496 |
let
|
haftmann@24219
|
1497 |
val is_cons = CodeThingol.is_cons code;
|
haftmann@25621
|
1498 |
val contr_classparam_typs = CodeThingol.contr_classparam_typs code;
|
haftmann@24219
|
1499 |
val module_alias = if is_some module then K module else raw_module_alias;
|
haftmann@24219
|
1500 |
val init_names = Name.make_context reserved_syms;
|
haftmann@24219
|
1501 |
val name_modl = mk_modl_name_tab init_names module_prefix module_alias code;
|
haftmann@24219
|
1502 |
fun add_def (name, (def, deps)) =
|
haftmann@24219
|
1503 |
let
|
haftmann@24219
|
1504 |
val (modl, base) = dest_name name;
|
haftmann@25147
|
1505 |
val name_def = yield_singleton Name.variants;
|
haftmann@24219
|
1506 |
fun add_fun upper (nsp_fun, nsp_typ) =
|
haftmann@24219
|
1507 |
let
|
haftmann@24992
|
1508 |
val (base', nsp_fun') =
|
haftmann@24992
|
1509 |
name_def (if upper then first_upper base else base) nsp_fun
|
haftmann@24219
|
1510 |
in (base', (nsp_fun', nsp_typ)) end;
|
haftmann@24219
|
1511 |
fun add_typ (nsp_fun, nsp_typ) =
|
haftmann@24219
|
1512 |
let
|
haftmann@24219
|
1513 |
val (base', nsp_typ') = name_def (first_upper base) nsp_typ
|
haftmann@24219
|
1514 |
in (base', (nsp_fun, nsp_typ')) end;
|
haftmann@24219
|
1515 |
val add_name =
|
haftmann@24219
|
1516 |
case def
|
haftmann@27024
|
1517 |
of CodeThingol.Fun _ => add_fun false
|
haftmann@24219
|
1518 |
| CodeThingol.Datatype _ => add_typ
|
haftmann@24219
|
1519 |
| CodeThingol.Datatypecons _ => add_fun true
|
haftmann@24219
|
1520 |
| CodeThingol.Class _ => add_typ
|
haftmann@24219
|
1521 |
| CodeThingol.Classrel _ => pair base
|
haftmann@24841
|
1522 |
| CodeThingol.Classparam _ => add_fun false
|
haftmann@24219
|
1523 |
| CodeThingol.Classinst _ => pair base;
|
haftmann@24219
|
1524 |
val modlname' = name_modl modl;
|
haftmann@24219
|
1525 |
fun add_def base' =
|
haftmann@24219
|
1526 |
case def
|
haftmann@27024
|
1527 |
of CodeThingol.Datatypecons _ =>
|
haftmann@24219
|
1528 |
cons (name, ((NameSpace.append modlname' base', base'), NONE))
|
haftmann@24219
|
1529 |
| CodeThingol.Classrel _ => I
|
haftmann@24841
|
1530 |
| CodeThingol.Classparam _ =>
|
haftmann@24219
|
1531 |
cons (name, ((NameSpace.append modlname' base', base'), NONE))
|
haftmann@24219
|
1532 |
| _ => cons (name, ((NameSpace.append modlname' base', base'), SOME def));
|
haftmann@24219
|
1533 |
in
|
haftmann@24219
|
1534 |
Symtab.map_default (modlname', ([], ([], (init_names, init_names))))
|
haftmann@24219
|
1535 |
(apfst (fold (insert (op = : string * string -> bool)) deps))
|
haftmann@24219
|
1536 |
#> `(fn code => add_name ((snd o snd o the o Symtab.lookup code) modlname'))
|
haftmann@24219
|
1537 |
#-> (fn (base', names) =>
|
haftmann@24219
|
1538 |
(Symtab.map_entry modlname' o apsnd) (fn (defs, _) =>
|
haftmann@24219
|
1539 |
(add_def base' defs, names)))
|
haftmann@24219
|
1540 |
end;
|
haftmann@24219
|
1541 |
val code' =
|
haftmann@24992
|
1542 |
fold add_def (AList.make (fn name =>
|
haftmann@24992
|
1543 |
(Graph.get_node code name, Graph.imm_succs code name))
|
haftmann@24219
|
1544 |
(Graph.strong_conn code |> flat)) Symtab.empty;
|
haftmann@24219
|
1545 |
val init_syms = CodeName.make_vars reserved_syms;
|
haftmann@24219
|
1546 |
fun deresolv name =
|
haftmann@24219
|
1547 |
(fst o fst o the o AList.lookup (op =) ((fst o snd o the
|
haftmann@24219
|
1548 |
o Symtab.lookup code') ((name_modl o fst o dest_name) name))) name
|
haftmann@27024
|
1549 |
handle Option => error ("Unknown statement name: " ^ labelled_name name);
|
haftmann@24219
|
1550 |
fun deresolv_here name =
|
haftmann@24219
|
1551 |
(snd o fst o the o AList.lookup (op =) ((fst o snd o the
|
haftmann@24219
|
1552 |
o Symtab.lookup code') ((name_modl o fst o dest_name) name))) name
|
haftmann@27024
|
1553 |
handle Option => error ("Unknown statement name: " ^ labelled_name name);
|
haftmann@24219
|
1554 |
fun deriving_show tyco =
|
haftmann@24219
|
1555 |
let
|
haftmann@24219
|
1556 |
fun deriv _ "fun" = false
|
haftmann@24219
|
1557 |
| deriv tycos tyco = member (op =) tycos tyco orelse
|
haftmann@27024
|
1558 |
case try (Graph.get_node code) tyco
|
haftmann@27024
|
1559 |
of SOME (CodeThingol.Datatype (_, cs)) => forall (deriv' (tyco :: tycos))
|
haftmann@24219
|
1560 |
(maps snd cs)
|
haftmann@27024
|
1561 |
| NONE => true
|
haftmann@24219
|
1562 |
and deriv' tycos (tyco `%% tys) = deriv tycos tyco
|
haftmann@24219
|
1563 |
andalso forall (deriv' tycos) tys
|
haftmann@24219
|
1564 |
| deriv' _ (ITyVar _) = true
|
haftmann@24219
|
1565 |
in deriv [] tyco end;
|
haftmann@24841
|
1566 |
fun seri_def qualified = pr_haskell class_syntax tyco_syntax
|
haftmann@24621
|
1567 |
const_syntax labelled_name init_syms
|
haftmann@24219
|
1568 |
deresolv_here (if qualified then deresolv else deresolv_here) is_cons
|
haftmann@25621
|
1569 |
contr_classparam_typs
|
haftmann@24219
|
1570 |
(if string_classes then deriving_show else K false);
|
haftmann@27000
|
1571 |
fun assemble_module (modulename, content) =
|
haftmann@27001
|
1572 |
(modulename, Pretty.chunks [
|
haftmann@24992
|
1573 |
str ("module " ^ modulename ^ " where {"),
|
haftmann@24992
|
1574 |
str "",
|
haftmann@24992
|
1575 |
content,
|
haftmann@24992
|
1576 |
str "",
|
haftmann@24992
|
1577 |
str "}"
|
haftmann@27001
|
1578 |
]);
|
haftmann@24219
|
1579 |
fun seri_module (modlname', (imports, (defs, _))) =
|
haftmann@24219
|
1580 |
let
|
haftmann@24219
|
1581 |
val imports' =
|
haftmann@24219
|
1582 |
imports
|
haftmann@24219
|
1583 |
|> map (name_modl o fst o dest_name)
|
haftmann@24219
|
1584 |
|> distinct (op =)
|
haftmann@24219
|
1585 |
|> remove (op =) modlname';
|
haftmann@27024
|
1586 |
val qualified = is_none module andalso
|
haftmann@24423
|
1587 |
imports @ map fst defs
|
haftmann@25147
|
1588 |
|> distinct (op =)
|
haftmann@24219
|
1589 |
|> map_filter (try deresolv)
|
haftmann@24219
|
1590 |
|> map NameSpace.base
|
haftmann@24219
|
1591 |
|> has_duplicates (op =);
|
haftmann@24219
|
1592 |
val mk_import = str o (if qualified
|
haftmann@24219
|
1593 |
then prefix "import qualified "
|
haftmann@24219
|
1594 |
else prefix "import ") o suffix ";";
|
haftmann@24992
|
1595 |
fun import_include (name, _) = str ("import " ^ name ^ ";");
|
haftmann@24992
|
1596 |
val content = Pretty.chunks (
|
haftmann@24992
|
1597 |
map mk_import imports'
|
haftmann@24992
|
1598 |
@ map import_include includes
|
haftmann@24992
|
1599 |
@ str ""
|
haftmann@24992
|
1600 |
:: separate (str "") (map_filter
|
haftmann@24992
|
1601 |
(fn (name, (_, SOME def)) => SOME (seri_def qualified (name, def))
|
haftmann@24992
|
1602 |
| (_, (_, NONE)) => NONE) defs)
|
haftmann@24992
|
1603 |
)
|
haftmann@27000
|
1604 |
in assemble_module (modlname', content) end;
|
haftmann@27000
|
1605 |
fun write_module destination (modlname, content) =
|
haftmann@27000
|
1606 |
let
|
haftmann@27000
|
1607 |
val filename = case modlname
|
haftmann@27000
|
1608 |
of "" => Path.explode "Main.hs"
|
haftmann@27000
|
1609 |
| _ => (Path.ext "hs" o Path.explode o implode o separate "/"
|
haftmann@27000
|
1610 |
o NameSpace.explode) modlname;
|
haftmann@27000
|
1611 |
val pathname = Path.append destination filename;
|
haftmann@27000
|
1612 |
val _ = File.mkdir (Path.dir pathname);
|
haftmann@27024
|
1613 |
in File.write pathname (target_code_of_pretty content) end
|
haftmann@27000
|
1614 |
val output = case seri_dest
|
haftmann@27000
|
1615 |
of Compile => error ("Haskell: no internal compilation")
|
haftmann@27024
|
1616 |
| Write => K NONE o map (target_code_writeln o snd)
|
haftmann@27000
|
1617 |
| File destination => K NONE o map (write_module destination)
|
haftmann@27024
|
1618 |
| String => SOME o cat_lines o map (target_code_of_pretty o snd);
|
haftmann@27000
|
1619 |
in output (map assemble_module includes
|
haftmann@27000
|
1620 |
@ map seri_module (Symtab.dest code'))
|
haftmann@27000
|
1621 |
end;
|
haftmann@24219
|
1622 |
|
haftmann@27000
|
1623 |
fun isar_seri_haskell module =
|
haftmann@27000
|
1624 |
parse_args (Scan.option (Args.$$$ "root" -- Args.colon |-- Args.name)
|
haftmann@27000
|
1625 |
-- Scan.optional (Args.$$$ "string_classes" >> K true) false
|
haftmann@27000
|
1626 |
>> (fn (module_prefix, string_classes) =>
|
haftmann@27000
|
1627 |
seri_haskell module_prefix module string_classes));
|
haftmann@24219
|
1628 |
|
haftmann@24219
|
1629 |
|
haftmann@24219
|
1630 |
(** diagnosis serializer **)
|
haftmann@24219
|
1631 |
|
haftmann@27000
|
1632 |
fun seri_diagnosis labelled_name _ _ _ _ _ _ code _ _ =
|
haftmann@24219
|
1633 |
let
|
haftmann@24219
|
1634 |
val init_names = CodeName.make_vars [];
|
haftmann@24219
|
1635 |
fun pr_fun "fun" = SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] =>
|
haftmann@24219
|
1636 |
brackify_infix (1, R) fxy [
|
haftmann@24219
|
1637 |
pr_typ (INFX (1, X)) ty1,
|
haftmann@24219
|
1638 |
str "->",
|
haftmann@24219
|
1639 |
pr_typ (INFX (1, R)) ty2
|
haftmann@24219
|
1640 |
])
|
haftmann@24219
|
1641 |
| pr_fun _ = NONE
|
haftmann@24992
|
1642 |
val pr = pr_haskell (K NONE) pr_fun (K NONE) labelled_name init_names
|
haftmann@25621
|
1643 |
I I (K false) (K []) (K false);
|
haftmann@24219
|
1644 |
in
|
haftmann@24219
|
1645 |
[]
|
haftmann@24992
|
1646 |
|> Graph.fold (fn (name, (def, _)) =>
|
haftmann@24992
|
1647 |
case try pr (name, def) of SOME p => cons p | NONE => I) code
|
haftmann@24219
|
1648 |
|> Pretty.chunks2
|
haftmann@27024
|
1649 |
|> target_code_of_pretty
|
haftmann@27000
|
1650 |
|> writeln
|
haftmann@27000
|
1651 |
|> K NONE
|
haftmann@24219
|
1652 |
end;
|
haftmann@24219
|
1653 |
|
haftmann@24219
|
1654 |
|
haftmann@24219
|
1655 |
(** optional pretty serialization **)
|
haftmann@24219
|
1656 |
|
haftmann@24219
|
1657 |
local
|
haftmann@24219
|
1658 |
|
haftmann@26448
|
1659 |
fun ocaml_char c =
|
haftmann@26448
|
1660 |
let
|
haftmann@26448
|
1661 |
fun chr i =
|
haftmann@26448
|
1662 |
let
|
haftmann@26448
|
1663 |
val xs = string_of_int i;
|
haftmann@26448
|
1664 |
val ys = replicate_string (3 - length (explode xs)) "0";
|
haftmann@26448
|
1665 |
in "\\" ^ ys ^ xs end;
|
haftmann@26448
|
1666 |
val i = ord c;
|
haftmann@26448
|
1667 |
val s = if i < 32 orelse i = 34 orelse i = 39 orelse i = 92 orelse i > 126
|
haftmann@26448
|
1668 |
then chr i else c
|
haftmann@26448
|
1669 |
in s end;
|
haftmann@26448
|
1670 |
|
haftmann@26448
|
1671 |
fun haskell_char c =
|
haftmann@26448
|
1672 |
let
|
haftmann@26448
|
1673 |
val s = ML_Syntax.print_char c;
|
haftmann@26448
|
1674 |
in if s = "'" then "\\'" else s end;
|
haftmann@26448
|
1675 |
|
haftmann@24219
|
1676 |
val pretty : (string * {
|
haftmann@24219
|
1677 |
pretty_char: string -> string,
|
haftmann@24219
|
1678 |
pretty_string: string -> string,
|
wenzelm@24630
|
1679 |
pretty_numeral: bool -> int -> string,
|
haftmann@24219
|
1680 |
pretty_list: Pretty.T list -> Pretty.T,
|
haftmann@24219
|
1681 |
infix_cons: int * string
|
haftmann@24219
|
1682 |
}) list = [
|
haftmann@24219
|
1683 |
("SML", { pretty_char = prefix "#" o quote o ML_Syntax.print_char,
|
haftmann@26448
|
1684 |
pretty_string = quote o translate_string ML_Syntax.print_char,
|
haftmann@24219
|
1685 |
pretty_numeral = fn unbounded => fn k =>
|
haftmann@24750
|
1686 |
if unbounded then "(" ^ string_of_int k ^ " : IntInf.int)"
|
wenzelm@24630
|
1687 |
else string_of_int k,
|
haftmann@24219
|
1688 |
pretty_list = Pretty.enum "," "[" "]",
|
haftmann@24219
|
1689 |
infix_cons = (7, "::")}),
|
haftmann@26448
|
1690 |
("OCaml", { pretty_char = enclose "'" "'" o ocaml_char,
|
haftmann@26448
|
1691 |
pretty_string = quote o translate_string ocaml_char,
|
wenzelm@24630
|
1692 |
pretty_numeral = fn unbounded => fn k => if k >= 0 then
|
haftmann@24219
|
1693 |
if unbounded then
|
wenzelm@24630
|
1694 |
"(Big_int.big_int_of_int " ^ string_of_int k ^ ")"
|
wenzelm@24630
|
1695 |
else string_of_int k
|
haftmann@24219
|
1696 |
else
|
haftmann@24219
|
1697 |
if unbounded then
|
haftmann@24219
|
1698 |
"(Big_int.big_int_of_int " ^ (enclose "(" ")" o prefix "-"
|
wenzelm@24630
|
1699 |
o string_of_int o op ~) k ^ ")"
|
wenzelm@24630
|
1700 |
else (enclose "(" ")" o prefix "-" o string_of_int o op ~) k,
|
haftmann@24219
|
1701 |
pretty_list = Pretty.enum ";" "[" "]",
|
haftmann@24219
|
1702 |
infix_cons = (6, "::")}),
|
haftmann@26448
|
1703 |
("Haskell", { pretty_char = enclose "'" "'" o haskell_char,
|
haftmann@26448
|
1704 |
pretty_string = quote o translate_string haskell_char,
|
wenzelm@24630
|
1705 |
pretty_numeral = fn unbounded => fn k => if k >= 0 then string_of_int k
|
wenzelm@24630
|
1706 |
else enclose "(" ")" (signed_string_of_int k),
|
haftmann@24219
|
1707 |
pretty_list = Pretty.enum "," "[" "]",
|
haftmann@24219
|
1708 |
infix_cons = (5, ":")})
|
haftmann@24219
|
1709 |
];
|
haftmann@24219
|
1710 |
|
haftmann@24219
|
1711 |
in
|
haftmann@24219
|
1712 |
|
haftmann@24219
|
1713 |
fun pr_pretty target = case AList.lookup (op =) pretty target
|
haftmann@24219
|
1714 |
of SOME x => x
|
haftmann@24219
|
1715 |
| NONE => error ("Unknown code target language: " ^ quote target);
|
haftmann@24219
|
1716 |
|
haftmann@24219
|
1717 |
fun default_list (target_fxy, target_cons) pr fxy t1 t2 =
|
haftmann@24219
|
1718 |
brackify_infix (target_fxy, R) fxy [
|
haftmann@24219
|
1719 |
pr (INFX (target_fxy, X)) t1,
|
haftmann@24219
|
1720 |
str target_cons,
|
haftmann@24219
|
1721 |
pr (INFX (target_fxy, R)) t2
|
haftmann@24219
|
1722 |
];
|
haftmann@24219
|
1723 |
|
haftmann@24219
|
1724 |
fun pretty_list c_nil c_cons target =
|
haftmann@24219
|
1725 |
let
|
haftmann@24219
|
1726 |
val pretty_ops = pr_pretty target;
|
haftmann@24219
|
1727 |
val mk_list = #pretty_list pretty_ops;
|
haftmann@24219
|
1728 |
fun pretty pr vars fxy [(t1, _), (t2, _)] =
|
haftmann@24219
|
1729 |
case Option.map (cons t1) (implode_list c_nil c_cons t2)
|
haftmann@24219
|
1730 |
of SOME ts => mk_list (map (pr vars NOBR) ts)
|
haftmann@24219
|
1731 |
| NONE => default_list (#infix_cons pretty_ops) (pr vars) fxy t1 t2;
|
haftmann@24219
|
1732 |
in (2, pretty) end;
|
haftmann@24219
|
1733 |
|
haftmann@24219
|
1734 |
fun pretty_list_string c_nil c_cons c_char c_nibbles target =
|
haftmann@24219
|
1735 |
let
|
haftmann@24219
|
1736 |
val pretty_ops = pr_pretty target;
|
haftmann@24219
|
1737 |
val mk_list = #pretty_list pretty_ops;
|
haftmann@24219
|
1738 |
val mk_char = #pretty_char pretty_ops;
|
haftmann@24219
|
1739 |
val mk_string = #pretty_string pretty_ops;
|
haftmann@24219
|
1740 |
fun pretty pr vars fxy [(t1, _), (t2, _)] =
|
haftmann@24219
|
1741 |
case Option.map (cons t1) (implode_list c_nil c_cons t2)
|
haftmann@26448
|
1742 |
of SOME ts => (case implode_string c_char c_nibbles mk_char mk_string ts
|
haftmann@24219
|
1743 |
of SOME p => p
|
haftmann@26448
|
1744 |
| NONE => mk_list (map (pr vars NOBR) ts))
|
haftmann@24219
|
1745 |
| NONE => default_list (#infix_cons pretty_ops) (pr vars) fxy t1 t2;
|
haftmann@24219
|
1746 |
in (2, pretty) end;
|
haftmann@24219
|
1747 |
|
haftmann@24219
|
1748 |
fun pretty_char c_char c_nibbles target =
|
haftmann@24219
|
1749 |
let
|
haftmann@24219
|
1750 |
val mk_char = #pretty_char (pr_pretty target);
|
haftmann@24219
|
1751 |
fun pretty _ _ _ [(t1, _), (t2, _)] =
|
haftmann@24219
|
1752 |
case decode_char c_nibbles (t1, t2)
|
haftmann@24219
|
1753 |
of SOME c => (str o mk_char) c
|
haftmann@24219
|
1754 |
| NONE => error "Illegal character expression";
|
haftmann@24219
|
1755 |
in (2, pretty) end;
|
haftmann@24219
|
1756 |
|
huffman@26086
|
1757 |
fun pretty_numeral unbounded negative c_pls c_min c_bit0 c_bit1 target =
|
haftmann@24219
|
1758 |
let
|
haftmann@24219
|
1759 |
val mk_numeral = #pretty_numeral (pr_pretty target);
|
haftmann@24219
|
1760 |
fun pretty _ _ _ [(t, _)] =
|
huffman@26086
|
1761 |
(str o mk_numeral unbounded o implode_numeral negative c_pls c_min c_bit0 c_bit1) t;
|
haftmann@24219
|
1762 |
in (1, pretty) end;
|
haftmann@24219
|
1763 |
|
haftmann@24992
|
1764 |
fun pretty_message c_char c_nibbles c_nil c_cons target =
|
haftmann@24219
|
1765 |
let
|
haftmann@24219
|
1766 |
val pretty_ops = pr_pretty target;
|
haftmann@24219
|
1767 |
val mk_char = #pretty_char pretty_ops;
|
haftmann@24219
|
1768 |
val mk_string = #pretty_string pretty_ops;
|
haftmann@24219
|
1769 |
fun pretty pr vars fxy [(t, _)] =
|
haftmann@24219
|
1770 |
case implode_list c_nil c_cons t
|
haftmann@24219
|
1771 |
of SOME ts => (case implode_string c_char c_nibbles mk_char mk_string ts
|
haftmann@24219
|
1772 |
of SOME p => p
|
haftmann@24992
|
1773 |
| NONE => error "Illegal message expression")
|
haftmann@24992
|
1774 |
| NONE => error "Illegal message expression";
|
haftmann@24219
|
1775 |
in (1, pretty) end;
|
haftmann@24219
|
1776 |
|
haftmann@26753
|
1777 |
fun pretty_imperative_monad_bind bind' return' unit' =
|
haftmann@24219
|
1778 |
let
|
haftmann@26752
|
1779 |
val dummy_name = "";
|
haftmann@26752
|
1780 |
val dummy_type = ITyVar dummy_name;
|
haftmann@26752
|
1781 |
val dummy_case_term = IVar dummy_name;
|
haftmann@26752
|
1782 |
(*assumption: dummy values are not relevant for serialization*)
|
haftmann@26752
|
1783 |
val unitt = IConst (unit', ([], []));
|
haftmann@24702
|
1784 |
fun dest_abs ((v, ty) `|-> t, _) = ((v, ty), t)
|
haftmann@24702
|
1785 |
| dest_abs (t, ty) =
|
haftmann@24219
|
1786 |
let
|
haftmann@24702
|
1787 |
val vs = CodeThingol.fold_varnames cons t [];
|
haftmann@24219
|
1788 |
val v = Name.variant vs "x";
|
haftmann@24702
|
1789 |
val ty' = (hd o fst o CodeThingol.unfold_fun) ty;
|
haftmann@24702
|
1790 |
in ((v, ty'), t `$ IVar v) end;
|
haftmann@26753
|
1791 |
fun force (t as IConst (c, _) `$ t') = if c = return'
|
haftmann@26753
|
1792 |
then t' else t `$ unitt
|
haftmann@26753
|
1793 |
| force t = t `$ unitt;
|
haftmann@26752
|
1794 |
fun tr_bind' [(t1, _), (t2, ty2)] =
|
haftmann@24702
|
1795 |
let
|
haftmann@24702
|
1796 |
val ((v, ty), t) = dest_abs (t2, ty2);
|
haftmann@26753
|
1797 |
in ICase (((force t1, ty), [(IVar v, tr_bind'' t)]), dummy_case_term) end
|
haftmann@26753
|
1798 |
and tr_bind'' t = case CodeThingol.unfold_app t
|
haftmann@24702
|
1799 |
of (IConst (c, (_, ty1 :: ty2 :: _)), [x1, x2]) => if c = bind'
|
haftmann@26752
|
1800 |
then tr_bind' [(x1, ty1), (x2, ty2)]
|
haftmann@26753
|
1801 |
else force t
|
haftmann@26753
|
1802 |
| _ => force t;
|
haftmann@26752
|
1803 |
fun tr_bind ts = (dummy_name, dummy_type)
|
haftmann@26752
|
1804 |
`|-> ICase (((IVar dummy_name, dummy_type), [(unitt, tr_bind' ts)]), dummy_case_term);
|
haftmann@24702
|
1805 |
fun pretty pr vars fxy ts = pr vars fxy (tr_bind ts);
|
haftmann@24219
|
1806 |
in (2, pretty) end;
|
haftmann@24219
|
1807 |
|
haftmann@24992
|
1808 |
fun no_bindings x = (Option.map o apsnd)
|
haftmann@24992
|
1809 |
(fn pretty => fn pr => fn vars => pretty (pr vars)) x;
|
haftmann@24992
|
1810 |
|
haftmann@24219
|
1811 |
end; (*local*)
|
haftmann@24219
|
1812 |
|
haftmann@27000
|
1813 |
|
haftmann@27000
|
1814 |
(** user interfaces **)
|
haftmann@27000
|
1815 |
|
haftmann@27000
|
1816 |
(* evaluation *)
|
haftmann@27000
|
1817 |
|
haftmann@27000
|
1818 |
fun eval_seri module args =
|
haftmann@27000
|
1819 |
seri_ml (use_text (1, "generated code") Output.ml_output false)
|
haftmann@27000
|
1820 |
(fn (cs, SOME s) => "let\n" ^ s ^ "\nin fn () => " ^ enclose "(" ")" (commas cs) ^ " end")
|
haftmann@27000
|
1821 |
pr_sml (K Pretty.chunks) (SOME "Isabelle_Eval");
|
haftmann@27000
|
1822 |
|
haftmann@27000
|
1823 |
fun sml_of thy code cs = get_serializer (K eval_seri) thy target_SML false NONE [] code (SOME cs) String;
|
haftmann@27000
|
1824 |
|
haftmann@27000
|
1825 |
fun eval thy (ref_name, reff) code (t, ty) args =
|
haftmann@27000
|
1826 |
let
|
haftmann@27000
|
1827 |
val _ = if CodeThingol.contains_dictvar t then
|
haftmann@27000
|
1828 |
error "Term to be evaluated constains free dictionaries" else ();
|
haftmann@27000
|
1829 |
val code' = CodeThingol.add_value_stmt (t, ty) code;
|
haftmann@27000
|
1830 |
val value_code = sml_of thy code' [CodeName.value_name] ;
|
haftmann@27000
|
1831 |
val sml_code = space_implode " " (value_code :: "()" :: map (enclose "(" ")") args);
|
haftmann@27000
|
1832 |
in ML_Context.evaluate Output.ml_output false (ref_name, reff) sml_code end;
|
haftmann@27000
|
1833 |
|
haftmann@27000
|
1834 |
|
haftmann@27000
|
1835 |
(* infix syntax *)
|
haftmann@27000
|
1836 |
|
haftmann@27000
|
1837 |
datatype 'a mixfix =
|
haftmann@27000
|
1838 |
Arg of fixity
|
haftmann@27000
|
1839 |
| Pretty of Pretty.T;
|
haftmann@27000
|
1840 |
|
haftmann@27000
|
1841 |
fun mk_mixfix prep_arg (fixity_this, mfx) =
|
haftmann@27000
|
1842 |
let
|
haftmann@27000
|
1843 |
fun is_arg (Arg _) = true
|
haftmann@27000
|
1844 |
| is_arg _ = false;
|
haftmann@27000
|
1845 |
val i = (length o filter is_arg) mfx;
|
haftmann@27000
|
1846 |
fun fillin _ [] [] =
|
haftmann@27000
|
1847 |
[]
|
haftmann@27000
|
1848 |
| fillin pr (Arg fxy :: mfx) (a :: args) =
|
haftmann@27000
|
1849 |
(pr fxy o prep_arg) a :: fillin pr mfx args
|
haftmann@27000
|
1850 |
| fillin pr (Pretty p :: mfx) args =
|
haftmann@27000
|
1851 |
p :: fillin pr mfx args
|
haftmann@27000
|
1852 |
| fillin _ [] _ =
|
haftmann@27000
|
1853 |
error ("Inconsistent mixfix: too many arguments")
|
haftmann@27000
|
1854 |
| fillin _ _ [] =
|
haftmann@27000
|
1855 |
error ("Inconsistent mixfix: too less arguments");
|
haftmann@27000
|
1856 |
in
|
haftmann@27000
|
1857 |
(i, fn pr => fn fixity_ctxt => fn args =>
|
haftmann@27000
|
1858 |
gen_brackify (fixity fixity_this fixity_ctxt) (fillin pr mfx args))
|
haftmann@27000
|
1859 |
end;
|
haftmann@27000
|
1860 |
|
haftmann@27000
|
1861 |
fun parse_infix prep_arg (x, i) s =
|
haftmann@27000
|
1862 |
let
|
haftmann@27000
|
1863 |
val l = case x of L => INFX (i, L) | _ => INFX (i, X);
|
haftmann@27000
|
1864 |
val r = case x of R => INFX (i, R) | _ => INFX (i, X);
|
haftmann@27000
|
1865 |
in
|
haftmann@27000
|
1866 |
mk_mixfix prep_arg (INFX (i, x),
|
haftmann@27000
|
1867 |
[Arg l, (Pretty o Pretty.brk) 1, (Pretty o str) s, (Pretty o Pretty.brk) 1, Arg r])
|
haftmann@27000
|
1868 |
end;
|
haftmann@27000
|
1869 |
|
haftmann@27000
|
1870 |
|
haftmann@27000
|
1871 |
(* data access *)
|
haftmann@24219
|
1872 |
|
haftmann@24219
|
1873 |
local
|
haftmann@24219
|
1874 |
|
haftmann@24992
|
1875 |
fun cert_class thy class =
|
haftmann@24992
|
1876 |
let
|
haftmann@24992
|
1877 |
val _ = AxClass.get_info thy class;
|
haftmann@24992
|
1878 |
in class end;
|
haftmann@24992
|
1879 |
|
haftmann@24992
|
1880 |
fun read_class thy raw_class =
|
haftmann@24992
|
1881 |
let
|
haftmann@24992
|
1882 |
val class = Sign.intern_class thy raw_class;
|
haftmann@24992
|
1883 |
val _ = AxClass.get_info thy class;
|
haftmann@24992
|
1884 |
in class end;
|
haftmann@24992
|
1885 |
|
haftmann@24992
|
1886 |
fun cert_tyco thy tyco =
|
haftmann@24992
|
1887 |
let
|
haftmann@24992
|
1888 |
val _ = if Sign.declared_tyname thy tyco then ()
|
haftmann@24992
|
1889 |
else error ("No such type constructor: " ^ quote tyco);
|
haftmann@24992
|
1890 |
in tyco end;
|
haftmann@24992
|
1891 |
|
haftmann@24992
|
1892 |
fun read_tyco thy raw_tyco =
|
haftmann@24992
|
1893 |
let
|
haftmann@24992
|
1894 |
val tyco = Sign.intern_type thy raw_tyco;
|
haftmann@24992
|
1895 |
val _ = if Sign.declared_tyname thy tyco then ()
|
haftmann@24992
|
1896 |
else error ("No such type constructor: " ^ quote raw_tyco);
|
haftmann@24992
|
1897 |
in tyco end;
|
haftmann@24219
|
1898 |
|
haftmann@24219
|
1899 |
fun gen_add_syntax_class prep_class prep_const target raw_class raw_syn thy =
|
haftmann@24219
|
1900 |
let
|
haftmann@24841
|
1901 |
val class = prep_class thy raw_class;
|
haftmann@24841
|
1902 |
val class' = CodeName.class thy class;
|
haftmann@24841
|
1903 |
fun mk_classparam c = case AxClass.class_of_param thy c
|
haftmann@24841
|
1904 |
of SOME class'' => if class = class'' then CodeName.const thy c
|
haftmann@24219
|
1905 |
else error ("Not a class operation for class " ^ quote class ^ ": " ^ quote c)
|
haftmann@24219
|
1906 |
| NONE => error ("Not a class operation: " ^ quote c);
|
haftmann@24841
|
1907 |
fun mk_syntax_params raw_params = AList.lookup (op =)
|
haftmann@24841
|
1908 |
((map o apfst) (mk_classparam o prep_const thy) raw_params);
|
haftmann@24219
|
1909 |
in case raw_syn
|
haftmann@24841
|
1910 |
of SOME (syntax, raw_params) =>
|
haftmann@24219
|
1911 |
thy
|
haftmann@27024
|
1912 |
|> (map_name_syntax target o apfst o apfst)
|
haftmann@24841
|
1913 |
(Symtab.update (class', (syntax, mk_syntax_params raw_params)))
|
haftmann@24219
|
1914 |
| NONE =>
|
haftmann@24219
|
1915 |
thy
|
haftmann@27024
|
1916 |
|> (map_name_syntax target o apfst o apfst)
|
haftmann@24841
|
1917 |
(Symtab.delete_safe class')
|
haftmann@24219
|
1918 |
end;
|
haftmann@24219
|
1919 |
|
haftmann@24219
|
1920 |
fun gen_add_syntax_inst prep_class prep_tyco target (raw_tyco, raw_class) add_del thy =
|
haftmann@24219
|
1921 |
let
|
haftmann@24219
|
1922 |
val inst = CodeName.instance thy (prep_class thy raw_class, prep_tyco thy raw_tyco);
|
haftmann@24219
|
1923 |
in if add_del then
|
haftmann@24219
|
1924 |
thy
|
haftmann@27024
|
1925 |
|> (map_name_syntax target o apfst o apsnd)
|
haftmann@24219
|
1926 |
(Symtab.update (inst, ()))
|
haftmann@24219
|
1927 |
else
|
haftmann@24219
|
1928 |
thy
|
haftmann@27024
|
1929 |
|> (map_name_syntax target o apfst o apsnd)
|
haftmann@24219
|
1930 |
(Symtab.delete_safe inst)
|
haftmann@24219
|
1931 |
end;
|
haftmann@24219
|
1932 |
|
haftmann@24219
|
1933 |
fun gen_add_syntax_tyco prep_tyco target raw_tyco raw_syn thy =
|
haftmann@24219
|
1934 |
let
|
haftmann@24219
|
1935 |
val tyco = prep_tyco thy raw_tyco;
|
haftmann@24219
|
1936 |
val tyco' = if tyco = "fun" then "fun" else CodeName.tyco thy tyco;
|
haftmann@24219
|
1937 |
fun check_args (syntax as (n, _)) = if n <> Sign.arity_number thy tyco
|
haftmann@24219
|
1938 |
then error ("Number of arguments mismatch in syntax for type constructor " ^ quote tyco)
|
haftmann@24219
|
1939 |
else syntax
|
haftmann@24219
|
1940 |
in case raw_syn
|
haftmann@24219
|
1941 |
of SOME syntax =>
|
haftmann@24219
|
1942 |
thy
|
haftmann@27024
|
1943 |
|> (map_name_syntax target o apsnd o apfst)
|
haftmann@24219
|
1944 |
(Symtab.update (tyco', check_args syntax))
|
haftmann@24219
|
1945 |
| NONE =>
|
haftmann@24219
|
1946 |
thy
|
haftmann@27024
|
1947 |
|> (map_name_syntax target o apsnd o apfst)
|
haftmann@24219
|
1948 |
(Symtab.delete_safe tyco')
|
haftmann@24219
|
1949 |
end;
|
haftmann@24219
|
1950 |
|
haftmann@24219
|
1951 |
fun gen_add_syntax_const prep_const target raw_c raw_syn thy =
|
haftmann@24219
|
1952 |
let
|
haftmann@24219
|
1953 |
val c = prep_const thy raw_c;
|
haftmann@24219
|
1954 |
val c' = CodeName.const thy c;
|
haftmann@24423
|
1955 |
fun check_args (syntax as (n, _)) = if n > CodeUnit.no_args thy c
|
haftmann@24423
|
1956 |
then error ("Too many arguments in syntax for constant " ^ quote c)
|
haftmann@24219
|
1957 |
else syntax;
|
haftmann@24219
|
1958 |
in case raw_syn
|
haftmann@24219
|
1959 |
of SOME syntax =>
|
haftmann@24219
|
1960 |
thy
|
haftmann@27024
|
1961 |
|> (map_name_syntax target o apsnd o apsnd)
|
haftmann@24219
|
1962 |
(Symtab.update (c', check_args syntax))
|
haftmann@24219
|
1963 |
| NONE =>
|
haftmann@24219
|
1964 |
thy
|
haftmann@27024
|
1965 |
|> (map_name_syntax target o apsnd o apsnd)
|
haftmann@24219
|
1966 |
(Symtab.delete_safe c')
|
haftmann@24219
|
1967 |
end;
|
haftmann@24219
|
1968 |
|
haftmann@24219
|
1969 |
fun add_reserved target =
|
haftmann@24219
|
1970 |
let
|
haftmann@24219
|
1971 |
fun add sym syms = if member (op =) syms sym
|
haftmann@24219
|
1972 |
then error ("Reserved symbol " ^ quote sym ^ " already declared")
|
haftmann@24219
|
1973 |
else insert (op =) sym syms
|
haftmann@24992
|
1974 |
in map_adaptions target o apfst o add end;
|
haftmann@24992
|
1975 |
|
haftmann@24992
|
1976 |
fun add_include target =
|
haftmann@24992
|
1977 |
let
|
haftmann@24992
|
1978 |
fun add (name, SOME content) incls =
|
haftmann@24992
|
1979 |
let
|
haftmann@24992
|
1980 |
val _ = if Symtab.defined incls name
|
haftmann@24992
|
1981 |
then warning ("Overwriting existing include " ^ name)
|
haftmann@24992
|
1982 |
else ();
|
haftmann@24992
|
1983 |
in Symtab.update (name, str content) incls end
|
haftmann@24992
|
1984 |
| add (name, NONE) incls =
|
haftmann@24992
|
1985 |
Symtab.delete name incls;
|
haftmann@24992
|
1986 |
in map_adaptions target o apsnd o add end;
|
haftmann@24219
|
1987 |
|
haftmann@24219
|
1988 |
fun add_modl_alias target =
|
haftmann@24992
|
1989 |
map_module_alias target o Symtab.update o apsnd CodeName.check_modulename;
|
haftmann@24219
|
1990 |
|
haftmann@26753
|
1991 |
fun add_monad target c_run c_bind c_return_unit thy =
|
haftmann@24992
|
1992 |
let
|
haftmann@24992
|
1993 |
val c_run' = CodeUnit.read_const thy c_run;
|
haftmann@24992
|
1994 |
val c_bind' = CodeUnit.read_const thy c_bind;
|
haftmann@24992
|
1995 |
val c_bind'' = CodeName.const thy c_bind';
|
haftmann@26753
|
1996 |
val c_return_unit'' = (Option.map o pairself)
|
haftmann@26753
|
1997 |
(CodeName.const thy o CodeUnit.read_const thy) c_return_unit;
|
haftmann@26752
|
1998 |
val is_haskell = target = target_Haskell;
|
haftmann@26753
|
1999 |
val _ = if is_haskell andalso is_some c_return_unit''
|
haftmann@26752
|
2000 |
then error ("No unit entry may be given for Haskell monad")
|
haftmann@26752
|
2001 |
else ();
|
haftmann@26753
|
2002 |
val _ = if not is_haskell andalso is_none c_return_unit''
|
haftmann@26752
|
2003 |
then error ("Unit entry must be given for SML/OCaml monad")
|
haftmann@26752
|
2004 |
else ();
|
haftmann@24992
|
2005 |
in if target = target_Haskell then
|
haftmann@24992
|
2006 |
thy
|
haftmann@24992
|
2007 |
|> gen_add_syntax_const (K I) target_Haskell c_run'
|
haftmann@24992
|
2008 |
(SOME (pretty_haskell_monad c_bind''))
|
haftmann@24992
|
2009 |
|> gen_add_syntax_const (K I) target_Haskell c_bind'
|
haftmann@24992
|
2010 |
(no_bindings (SOME (parse_infix fst (L, 1) ">>=")))
|
haftmann@24992
|
2011 |
else
|
haftmann@24992
|
2012 |
thy
|
haftmann@24992
|
2013 |
|> gen_add_syntax_const (K I) target c_bind'
|
haftmann@26753
|
2014 |
(SOME (pretty_imperative_monad_bind c_bind''
|
haftmann@26753
|
2015 |
((fst o the) c_return_unit'') ((snd o the) c_return_unit'')))
|
haftmann@24992
|
2016 |
end;
|
haftmann@24219
|
2017 |
|
haftmann@24841
|
2018 |
fun gen_allow_exception prep_cs raw_c thy =
|
haftmann@24841
|
2019 |
let
|
haftmann@24841
|
2020 |
val c = prep_cs thy raw_c;
|
haftmann@24841
|
2021 |
val c' = CodeName.const thy c;
|
haftmann@24841
|
2022 |
in thy |> (CodeTargetData.map o apsnd) (insert (op =) c') end;
|
haftmann@24841
|
2023 |
|
haftmann@24219
|
2024 |
fun zip_list (x::xs) f g =
|
haftmann@24219
|
2025 |
f
|
haftmann@24219
|
2026 |
#-> (fn y =>
|
haftmann@24219
|
2027 |
fold_map (fn x => g |-- f >> pair x) xs
|
haftmann@24219
|
2028 |
#-> (fn xys => pair ((x, y) :: xys)));
|
haftmann@24219
|
2029 |
|
haftmann@27000
|
2030 |
|
haftmann@27000
|
2031 |
(* conrete syntax *)
|
haftmann@27000
|
2032 |
|
haftmann@24219
|
2033 |
structure P = OuterParse
|
haftmann@24219
|
2034 |
and K = OuterKeyword
|
haftmann@24219
|
2035 |
|
haftmann@24219
|
2036 |
fun parse_multi_syntax parse_thing parse_syntax =
|
haftmann@24219
|
2037 |
P.and_list1 parse_thing
|
haftmann@24219
|
2038 |
#-> (fn things => Scan.repeat1 (P.$$$ "(" |-- P.name --
|
haftmann@24219
|
2039 |
(zip_list things parse_syntax (P.$$$ "and")) --| P.$$$ ")"));
|
haftmann@24219
|
2040 |
|
haftmann@24219
|
2041 |
val (infixK, infixlK, infixrK) = ("infix", "infixl", "infixr");
|
haftmann@24219
|
2042 |
|
haftmann@27000
|
2043 |
fun parse_mixfix prep_arg s =
|
haftmann@27000
|
2044 |
let
|
haftmann@27000
|
2045 |
val sym_any = Scan.one Symbol.is_regular;
|
haftmann@27000
|
2046 |
val parse = Scan.optional ($$ "!" >> K true) false -- Scan.repeat (
|
haftmann@27000
|
2047 |
($$ "(" -- $$ "_" -- $$ ")" >> K (Arg NOBR))
|
haftmann@27000
|
2048 |
|| ($$ "_" >> K (Arg BR))
|
haftmann@27000
|
2049 |
|| ($$ "/" |-- Scan.repeat ($$ " ") >> (Pretty o Pretty.brk o length))
|
haftmann@27000
|
2050 |
|| (Scan.repeat1
|
haftmann@27000
|
2051 |
( $$ "'" |-- sym_any
|
haftmann@27000
|
2052 |
|| Scan.unless ($$ "_" || $$ "/" || $$ "(" |-- $$ "_" |-- $$ ")")
|
haftmann@27000
|
2053 |
sym_any) >> (Pretty o str o implode)));
|
haftmann@27000
|
2054 |
in case Scan.finite Symbol.stopper parse (Symbol.explode s)
|
haftmann@27000
|
2055 |
of ((_, p as [_]), []) => mk_mixfix prep_arg (NOBR, p)
|
haftmann@27000
|
2056 |
| ((b, p as _ :: _ :: _), []) => mk_mixfix prep_arg (if b then NOBR else BR, p)
|
haftmann@27000
|
2057 |
| _ => Scan.!!
|
haftmann@27000
|
2058 |
(the_default ("malformed mixfix annotation: " ^ quote s) o snd) Scan.fail ()
|
haftmann@27000
|
2059 |
end;
|
haftmann@27000
|
2060 |
|
haftmann@24219
|
2061 |
fun parse_syntax prep_arg xs =
|
haftmann@24219
|
2062 |
Scan.option ((
|
haftmann@24219
|
2063 |
((P.$$$ infixK >> K X)
|
haftmann@24219
|
2064 |
|| (P.$$$ infixlK >> K L)
|
haftmann@24219
|
2065 |
|| (P.$$$ infixrK >> K R))
|
haftmann@24219
|
2066 |
-- P.nat >> parse_infix prep_arg
|
haftmann@24219
|
2067 |
|| Scan.succeed (parse_mixfix prep_arg))
|
haftmann@24219
|
2068 |
-- P.string
|
haftmann@24219
|
2069 |
>> (fn (parse, s) => parse s)) xs;
|
haftmann@24219
|
2070 |
|
haftmann@24219
|
2071 |
in
|
haftmann@24219
|
2072 |
|
haftmann@24219
|
2073 |
val parse_syntax = parse_syntax;
|
haftmann@24219
|
2074 |
|
haftmann@24219
|
2075 |
val add_syntax_class = gen_add_syntax_class cert_class (K I);
|
haftmann@24219
|
2076 |
val add_syntax_inst = gen_add_syntax_inst cert_class cert_tyco;
|
haftmann@24219
|
2077 |
val add_syntax_tyco = gen_add_syntax_tyco cert_tyco;
|
haftmann@24219
|
2078 |
val add_syntax_const = gen_add_syntax_const (K I);
|
haftmann@24841
|
2079 |
val allow_exception = gen_allow_exception (K I);
|
haftmann@24219
|
2080 |
|
haftmann@24219
|
2081 |
val add_syntax_class_cmd = gen_add_syntax_class read_class CodeUnit.read_const;
|
haftmann@24219
|
2082 |
val add_syntax_inst_cmd = gen_add_syntax_inst read_class read_tyco;
|
haftmann@24219
|
2083 |
val add_syntax_tyco_cmd = gen_add_syntax_tyco read_tyco;
|
haftmann@24219
|
2084 |
val add_syntax_const_cmd = gen_add_syntax_const CodeUnit.read_const;
|
haftmann@24841
|
2085 |
val allow_exception_cmd = gen_allow_exception CodeUnit.read_const;
|
haftmann@24219
|
2086 |
|
haftmann@24219
|
2087 |
fun add_syntax_tycoP target tyco = parse_syntax I >> add_syntax_tyco_cmd target tyco;
|
haftmann@24219
|
2088 |
fun add_syntax_constP target c = parse_syntax fst >> (add_syntax_const_cmd target c o no_bindings);
|
haftmann@24219
|
2089 |
|
haftmann@24219
|
2090 |
fun add_undefined target undef target_undefined thy =
|
haftmann@24219
|
2091 |
let
|
haftmann@24219
|
2092 |
fun pr _ _ _ _ = str target_undefined;
|
haftmann@24219
|
2093 |
in
|
haftmann@24219
|
2094 |
thy
|
haftmann@24423
|
2095 |
|> add_syntax_const target undef (SOME (~1, pr))
|
haftmann@24219
|
2096 |
end;
|
haftmann@24219
|
2097 |
|
haftmann@24219
|
2098 |
fun add_pretty_list target nill cons thy =
|
haftmann@24219
|
2099 |
let
|
haftmann@24423
|
2100 |
val nil' = CodeName.const thy nill;
|
haftmann@24423
|
2101 |
val cons' = CodeName.const thy cons;
|
haftmann@24423
|
2102 |
val pr = pretty_list nil' cons' target;
|
haftmann@24219
|
2103 |
in
|
haftmann@24219
|
2104 |
thy
|
haftmann@24423
|
2105 |
|> add_syntax_const target cons (SOME pr)
|
haftmann@24219
|
2106 |
end;
|
haftmann@24219
|
2107 |
|
haftmann@24219
|
2108 |
fun add_pretty_list_string target nill cons charr nibbles thy =
|
haftmann@24219
|
2109 |
let
|
haftmann@24423
|
2110 |
val nil' = CodeName.const thy nill;
|
haftmann@24423
|
2111 |
val cons' = CodeName.const thy cons;
|
haftmann@24423
|
2112 |
val charr' = CodeName.const thy charr;
|
haftmann@24423
|
2113 |
val nibbles' = map (CodeName.const thy) nibbles;
|
haftmann@24423
|
2114 |
val pr = pretty_list_string nil' cons' charr' nibbles' target;
|
haftmann@24219
|
2115 |
in
|
haftmann@24219
|
2116 |
thy
|
haftmann@24423
|
2117 |
|> add_syntax_const target cons (SOME pr)
|
haftmann@24219
|
2118 |
end;
|
haftmann@24219
|
2119 |
|
haftmann@24219
|
2120 |
fun add_pretty_char target charr nibbles thy =
|
haftmann@24219
|
2121 |
let
|
haftmann@24423
|
2122 |
val charr' = CodeName.const thy charr;
|
haftmann@24423
|
2123 |
val nibbles' = map (CodeName.const thy) nibbles;
|
haftmann@24423
|
2124 |
val pr = pretty_char charr' nibbles' target;
|
haftmann@24219
|
2125 |
in
|
haftmann@24219
|
2126 |
thy
|
haftmann@24423
|
2127 |
|> add_syntax_const target charr (SOME pr)
|
haftmann@24219
|
2128 |
end;
|
haftmann@24219
|
2129 |
|
huffman@26086
|
2130 |
fun add_pretty_numeral target unbounded negative number_of pls min bit0 bit1 thy =
|
haftmann@24219
|
2131 |
let
|
haftmann@24423
|
2132 |
val pls' = CodeName.const thy pls;
|
haftmann@24423
|
2133 |
val min' = CodeName.const thy min;
|
huffman@26086
|
2134 |
val bit0' = CodeName.const thy bit0;
|
huffman@26086
|
2135 |
val bit1' = CodeName.const thy bit1;
|
huffman@26086
|
2136 |
val pr = pretty_numeral unbounded negative pls' min' bit0' bit1' target;
|
haftmann@24219
|
2137 |
in
|
haftmann@24219
|
2138 |
thy
|
haftmann@24423
|
2139 |
|> add_syntax_const target number_of (SOME pr)
|
haftmann@24219
|
2140 |
end;
|
haftmann@24219
|
2141 |
|
haftmann@24992
|
2142 |
fun add_pretty_message target charr nibbles nill cons str thy =
|
haftmann@24219
|
2143 |
let
|
haftmann@24423
|
2144 |
val charr' = CodeName.const thy charr;
|
haftmann@24423
|
2145 |
val nibbles' = map (CodeName.const thy) nibbles;
|
haftmann@24423
|
2146 |
val nil' = CodeName.const thy nill;
|
haftmann@24423
|
2147 |
val cons' = CodeName.const thy cons;
|
haftmann@24992
|
2148 |
val pr = pretty_message charr' nibbles' nil' cons' target;
|
haftmann@24219
|
2149 |
in
|
haftmann@24219
|
2150 |
thy
|
haftmann@24423
|
2151 |
|> add_syntax_const target str (SOME pr)
|
haftmann@24219
|
2152 |
end;
|
haftmann@24219
|
2153 |
|
wenzelm@24867
|
2154 |
|
haftmann@27000
|
2155 |
(* Isar commands *)
|
haftmann@27000
|
2156 |
|
wenzelm@24867
|
2157 |
val _ = OuterSyntax.keywords [infixK, infixlK, infixrK];
|
wenzelm@24867
|
2158 |
|
wenzelm@24867
|
2159 |
val _ =
|
haftmann@24992
|
2160 |
OuterSyntax.command "code_class" "define code syntax for class" K.thy_decl (
|
haftmann@24219
|
2161 |
parse_multi_syntax P.xname
|
haftmann@24219
|
2162 |
(Scan.option (P.string -- Scan.optional (P.$$$ "where" |-- Scan.repeat1
|
wenzelm@25985
|
2163 |
(P.term --| (P.$$$ "\<equiv>" || P.$$$ "==") -- P.string)) []))
|
haftmann@24219
|
2164 |
>> (Toplevel.theory oo fold) (fn (target, syns) =>
|
haftmann@24219
|
2165 |
fold (fn (raw_class, syn) => add_syntax_class_cmd target raw_class syn) syns)
|
haftmann@24219
|
2166 |
);
|
haftmann@24219
|
2167 |
|
wenzelm@24867
|
2168 |
val _ =
|
haftmann@24992
|
2169 |
OuterSyntax.command "code_instance" "define code syntax for instance" K.thy_decl (
|
haftmann@24219
|
2170 |
parse_multi_syntax (P.xname --| P.$$$ "::" -- P.xname)
|
haftmann@24219
|
2171 |
((P.minus >> K true) || Scan.succeed false)
|
haftmann@24219
|
2172 |
>> (Toplevel.theory oo fold) (fn (target, syns) =>
|
haftmann@24219
|
2173 |
fold (fn (raw_inst, add_del) => add_syntax_inst_cmd target raw_inst add_del) syns)
|
haftmann@24219
|
2174 |
);
|
haftmann@24219
|
2175 |
|
wenzelm@24867
|
2176 |
val _ =
|
haftmann@24992
|
2177 |
OuterSyntax.command "code_type" "define code syntax for type constructor" K.thy_decl (
|
haftmann@24219
|
2178 |
parse_multi_syntax P.xname (parse_syntax I)
|
haftmann@24219
|
2179 |
>> (Toplevel.theory oo fold) (fn (target, syns) =>
|
haftmann@24219
|
2180 |
fold (fn (raw_tyco, syn) => add_syntax_tyco_cmd target raw_tyco syn) syns)
|
haftmann@24219
|
2181 |
);
|
haftmann@24219
|
2182 |
|
wenzelm@24867
|
2183 |
val _ =
|
haftmann@24992
|
2184 |
OuterSyntax.command "code_const" "define code syntax for constant" K.thy_decl (
|
haftmann@24219
|
2185 |
parse_multi_syntax P.term (parse_syntax fst)
|
haftmann@24219
|
2186 |
>> (Toplevel.theory oo fold) (fn (target, syns) =>
|
haftmann@24219
|
2187 |
fold (fn (raw_const, syn) => add_syntax_const_cmd target raw_const (no_bindings syn)) syns)
|
haftmann@24219
|
2188 |
);
|
haftmann@24219
|
2189 |
|
wenzelm@24867
|
2190 |
val _ =
|
haftmann@24992
|
2191 |
OuterSyntax.command "code_monad" "define code syntax for monads" K.thy_decl (
|
haftmann@26753
|
2192 |
P.term -- P.term -- ((P.term -- P.term >> SOME) -- Scan.repeat1 P.name
|
haftmann@26752
|
2193 |
|| Scan.succeed NONE -- Scan.repeat1 P.name)
|
haftmann@26753
|
2194 |
>> (fn ((raw_run, raw_bind), (raw_unit_return, targets)) => Toplevel.theory
|
haftmann@26753
|
2195 |
(fold (fn target => add_monad target raw_run raw_bind raw_unit_return) targets))
|
haftmann@24219
|
2196 |
);
|
haftmann@24219
|
2197 |
|
wenzelm@24867
|
2198 |
val _ =
|
haftmann@24992
|
2199 |
OuterSyntax.command "code_reserved" "declare words as reserved for target language" K.thy_decl (
|
haftmann@24219
|
2200 |
P.name -- Scan.repeat1 P.name
|
haftmann@24219
|
2201 |
>> (fn (target, reserveds) => (Toplevel.theory o fold (add_reserved target)) reserveds)
|
haftmann@24841
|
2202 |
);
|
haftmann@24219
|
2203 |
|
wenzelm@24867
|
2204 |
val _ =
|
haftmann@24992
|
2205 |
OuterSyntax.command "code_include" "declare piece of code to be included in generated code" K.thy_decl (
|
haftmann@24992
|
2206 |
P.name -- P.name -- (P.text >> (fn "-" => NONE | s => SOME s))
|
haftmann@24992
|
2207 |
>> (fn ((target, name), content) => (Toplevel.theory o add_include target)
|
haftmann@24992
|
2208 |
(name, content))
|
haftmann@24992
|
2209 |
);
|
haftmann@24992
|
2210 |
|
haftmann@24992
|
2211 |
val _ =
|
haftmann@24992
|
2212 |
OuterSyntax.command "code_modulename" "alias module to other name" K.thy_decl (
|
haftmann@24219
|
2213 |
P.name -- Scan.repeat1 (P.name -- P.name)
|
haftmann@24219
|
2214 |
>> (fn (target, modlnames) => (Toplevel.theory o fold (add_modl_alias target)) modlnames)
|
haftmann@24841
|
2215 |
);
|
haftmann@24219
|
2216 |
|
wenzelm@24867
|
2217 |
val _ =
|
haftmann@24992
|
2218 |
OuterSyntax.command "code_exception" "permit exceptions for constant" K.thy_decl (
|
haftmann@24841
|
2219 |
Scan.repeat1 P.term >> (Toplevel.theory o fold allow_exception_cmd)
|
haftmann@24841
|
2220 |
);
|
haftmann@24219
|
2221 |
|
haftmann@24219
|
2222 |
|
haftmann@27000
|
2223 |
(* serializer setup, including serializer defaults *)
|
haftmann@27000
|
2224 |
|
haftmann@24219
|
2225 |
val setup =
|
haftmann@24219
|
2226 |
add_serializer (target_SML, isar_seri_sml)
|
haftmann@24219
|
2227 |
#> add_serializer (target_OCaml, isar_seri_ocaml)
|
haftmann@24219
|
2228 |
#> add_serializer (target_Haskell, isar_seri_haskell)
|
haftmann@27000
|
2229 |
#> add_serializer (target_diag, fn _ => fn _=> seri_diagnosis)
|
haftmann@24219
|
2230 |
#> add_syntax_tyco "SML" "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] =>
|
haftmann@27024
|
2231 |
brackify_infix (1, R) fxy [
|
haftmann@24219
|
2232 |
pr_typ (INFX (1, X)) ty1,
|
haftmann@24219
|
2233 |
str "->",
|
haftmann@24219
|
2234 |
pr_typ (INFX (1, R)) ty2
|
haftmann@24219
|
2235 |
]))
|
haftmann@24219
|
2236 |
#> add_syntax_tyco "OCaml" "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] =>
|
haftmann@27024
|
2237 |
brackify_infix (1, R) fxy [
|
haftmann@24219
|
2238 |
pr_typ (INFX (1, X)) ty1,
|
haftmann@24219
|
2239 |
str "->",
|
haftmann@24219
|
2240 |
pr_typ (INFX (1, R)) ty2
|
haftmann@24219
|
2241 |
]))
|
haftmann@24219
|
2242 |
#> add_syntax_tyco "Haskell" "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] =>
|
haftmann@24219
|
2243 |
brackify_infix (1, R) fxy [
|
haftmann@24219
|
2244 |
pr_typ (INFX (1, X)) ty1,
|
haftmann@24219
|
2245 |
str "->",
|
haftmann@24219
|
2246 |
pr_typ (INFX (1, R)) ty2
|
haftmann@24219
|
2247 |
]))
|
haftmann@24219
|
2248 |
#> fold (add_reserved "SML") ML_Syntax.reserved_names
|
haftmann@24219
|
2249 |
#> fold (add_reserved "SML")
|
haftmann@24219
|
2250 |
["o" (*dictionary projections use it already*), "Fail", "div", "mod" (*standard infixes*)]
|
haftmann@24219
|
2251 |
#> fold (add_reserved "OCaml") [
|
haftmann@24219
|
2252 |
"and", "as", "assert", "begin", "class",
|
haftmann@24219
|
2253 |
"constraint", "do", "done", "downto", "else", "end", "exception",
|
haftmann@24219
|
2254 |
"external", "false", "for", "fun", "function", "functor", "if",
|
haftmann@24219
|
2255 |
"in", "include", "inherit", "initializer", "lazy", "let", "match", "method",
|
haftmann@24219
|
2256 |
"module", "mutable", "new", "object", "of", "open", "or", "private", "rec",
|
haftmann@24219
|
2257 |
"sig", "struct", "then", "to", "true", "try", "type", "val",
|
haftmann@24219
|
2258 |
"virtual", "when", "while", "with"
|
haftmann@24219
|
2259 |
]
|
haftmann@24219
|
2260 |
#> fold (add_reserved "OCaml") ["failwith", "mod"]
|
haftmann@24219
|
2261 |
#> fold (add_reserved "Haskell") [
|
haftmann@24219
|
2262 |
"hiding", "deriving", "where", "case", "of", "infix", "infixl", "infixr",
|
haftmann@24219
|
2263 |
"import", "default", "forall", "let", "in", "class", "qualified", "data",
|
haftmann@24219
|
2264 |
"newtype", "instance", "if", "then", "else", "type", "as", "do", "module"
|
haftmann@24219
|
2265 |
]
|
haftmann@24219
|
2266 |
#> fold (add_reserved "Haskell") [
|
haftmann@24219
|
2267 |
"Prelude", "Main", "Bool", "Maybe", "Either", "Ordering", "Char", "String", "Int",
|
haftmann@24219
|
2268 |
"Integer", "Float", "Double", "Rational", "IO", "Eq", "Ord", "Enum", "Bounded",
|
haftmann@24219
|
2269 |
"Num", "Real", "Integral", "Fractional", "Floating", "RealFloat", "Monad", "Functor",
|
haftmann@24219
|
2270 |
"AlreadyExists", "ArithException", "ArrayException", "AssertionFailed", "AsyncException",
|
haftmann@24219
|
2271 |
"BlockedOnDeadMVar", "Deadlock", "Denormal", "DivideByZero", "DotNetException", "DynException",
|
haftmann@24219
|
2272 |
"Dynamic", "EOF", "EQ", "EmptyRec", "ErrorCall", "ExitException", "ExitFailure",
|
haftmann@24219
|
2273 |
"ExitSuccess", "False", "GT", "HeapOverflow",
|
haftmann@24219
|
2274 |
"IOError", "IOException", "IllegalOperation",
|
haftmann@24219
|
2275 |
"IndexOutOfBounds", "Just", "Key", "LT", "Left", "LossOfPrecision", "NoMethodError",
|
haftmann@24219
|
2276 |
"NoSuchThing", "NonTermination", "Nothing", "Obj", "OtherError", "Overflow",
|
haftmann@24219
|
2277 |
"PatternMatchFail", "PermissionDenied", "ProtocolError", "RecConError", "RecSelError",
|
haftmann@24219
|
2278 |
"RecUpdError", "ResourceBusy", "ResourceExhausted", "Right", "StackOverflow",
|
haftmann@24219
|
2279 |
"ThreadKilled", "True", "TyCon", "TypeRep", "UndefinedElement", "Underflow",
|
haftmann@24219
|
2280 |
"UnsupportedOperation", "UserError", "abs", "absReal", "acos", "acosh", "all",
|
haftmann@24219
|
2281 |
"and", "any", "appendFile", "asTypeOf", "asciiTab", "asin", "asinh", "atan",
|
haftmann@24219
|
2282 |
"atan2", "atanh", "basicIORun", "blockIO", "boundedEnumFrom", "boundedEnumFromThen",
|
haftmann@24219
|
2283 |
"boundedEnumFromThenTo", "boundedEnumFromTo", "boundedPred", "boundedSucc", "break",
|
haftmann@24219
|
2284 |
"catch", "catchException", "ceiling", "compare", "concat", "concatMap", "const",
|
haftmann@24219
|
2285 |
"cos", "cosh", "curry", "cycle", "decodeFloat", "denominator", "div", "divMod",
|
haftmann@24219
|
2286 |
"doubleToRatio", "doubleToRational", "drop", "dropWhile", "either", "elem",
|
haftmann@24219
|
2287 |
"emptyRec", "encodeFloat", "enumFrom", "enumFromThen", "enumFromThenTo",
|
haftmann@24219
|
2288 |
"enumFromTo", "error", "even", "exp", "exponent", "fail", "filter", "flip",
|
haftmann@24219
|
2289 |
"floatDigits", "floatProperFraction", "floatRadix", "floatRange", "floatToRational",
|
haftmann@24219
|
2290 |
"floor", "fmap", "foldl", "foldl'", "foldl1", "foldr", "foldr1", "fromDouble",
|
haftmann@24219
|
2291 |
"fromEnum", "fromEnum_0", "fromInt", "fromInteger", "fromIntegral", "fromObj",
|
haftmann@24219
|
2292 |
"fromRational", "fst", "gcd", "getChar", "getContents", "getLine", "head",
|
haftmann@24219
|
2293 |
"id", "inRange", "index", "init", "intToRatio", "interact", "ioError", "isAlpha",
|
haftmann@24219
|
2294 |
"isAlphaNum", "isDenormalized", "isDigit", "isHexDigit", "isIEEE", "isInfinite",
|
haftmann@24219
|
2295 |
"isLower", "isNaN", "isNegativeZero", "isOctDigit", "isSpace", "isUpper", "iterate", "iterate'",
|
haftmann@24219
|
2296 |
"last", "lcm", "length", "lex", "lexDigits", "lexLitChar", "lexmatch", "lines", "log",
|
haftmann@24219
|
2297 |
"logBase", "lookup", "loop", "map", "mapM", "mapM_", "max", "maxBound", "maximum",
|
haftmann@24219
|
2298 |
"maybe", "min", "minBound", "minimum", "mod", "negate", "nonnull", "not", "notElem",
|
haftmann@24219
|
2299 |
"null", "numerator", "numericEnumFrom", "numericEnumFromThen", "numericEnumFromThenTo",
|
haftmann@24219
|
2300 |
"numericEnumFromTo", "odd", "or", "otherwise", "pi", "pred",
|
haftmann@24219
|
2301 |
"print", "product", "properFraction", "protectEsc", "putChar", "putStr", "putStrLn",
|
haftmann@24219
|
2302 |
"quot", "quotRem", "range", "rangeSize", "rationalToDouble", "rationalToFloat",
|
haftmann@24219
|
2303 |
"rationalToRealFloat", "read", "readDec", "readField", "readFieldName", "readFile",
|
haftmann@24219
|
2304 |
"readFloat", "readHex", "readIO", "readInt", "readList", "readLitChar", "readLn",
|
haftmann@24219
|
2305 |
"readOct", "readParen", "readSigned", "reads", "readsPrec", "realFloatToRational",
|
haftmann@24219
|
2306 |
"realToFrac", "recip", "reduce", "rem", "repeat", "replicate", "return", "reverse",
|
haftmann@24219
|
2307 |
"round", "scaleFloat", "scanl", "scanl1", "scanr", "scanr1", "seq", "sequence",
|
haftmann@24219
|
2308 |
"sequence_", "show", "showChar", "showException", "showField", "showList",
|
haftmann@24219
|
2309 |
"showLitChar", "showParen", "showString", "shows", "showsPrec", "significand",
|
haftmann@24219
|
2310 |
"signum", "signumReal", "sin", "sinh", "snd", "span", "splitAt", "sqrt", "subtract",
|
haftmann@24219
|
2311 |
"succ", "sum", "tail", "take", "takeWhile", "takeWhile1", "tan", "tanh", "threadToIOResult",
|
haftmann@24219
|
2312 |
"throw", "toEnum", "toInt", "toInteger", "toObj", "toRational", "truncate", "uncurry",
|
haftmann@24219
|
2313 |
"undefined", "unlines", "unsafeCoerce", "unsafeIndex", "unsafeRangeSize", "until", "unwords",
|
haftmann@24219
|
2314 |
"unzip", "unzip3", "userError", "words", "writeFile", "zip", "zip3", "zipWith", "zipWith3"
|
haftmann@24219
|
2315 |
] (*due to weird handling of ':', we can't do anything else than to import *all* prelude symbols*);
|
haftmann@24219
|
2316 |
|
haftmann@24219
|
2317 |
end; (*local*)
|
haftmann@24219
|
2318 |
|
haftmann@24219
|
2319 |
end; (*struct*)
|