author | haftmann |
Wed, 11 Oct 2006 20:35:54 +0200 | |
changeset 20976 | e324808e9f1f |
parent 20940 | 2526ef41a189 |
child 21015 | 425883e01fe0 |
permissions | -rw-r--r-- |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
1 |
(* Title: Pure/Tools/codegen_serializer.ML |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
2 |
ID: $Id$ |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
3 |
Author: Florian Haftmann, TU Muenchen |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
4 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
5 |
Serializer from intermediate language ("Thin-gol") to |
20699 | 6 |
target languages (like SML or Haskell). |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
7 |
*) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
8 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
9 |
signature CODEGEN_SERIALIZER = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
10 |
sig |
20456 | 11 |
include BASIC_CODEGEN_THINGOL; |
20931 | 12 |
|
20699 | 13 |
val add_pretty_list: string -> string -> string -> (Pretty.T list -> Pretty.T) |
14 |
-> ((string -> string) * (string -> string)) option -> int * string |
|
15 |
-> theory -> theory; |
|
16 |
val add_pretty_ml_string: string -> string -> string -> string |
|
17 |
-> (string -> string) -> (string -> string) -> string -> theory -> theory; |
|
18 |
val add_undefined: string -> string -> string -> theory -> theory; |
|
20896 | 19 |
|
20 |
type serializer; |
|
21 |
val add_serializer : string * serializer -> theory -> theory; |
|
22 |
val ml_from_thingol: serializer; |
|
23 |
val hs_from_thingol: serializer; |
|
20931 | 24 |
val get_serializer: theory -> string * Args.T list |
25 |
-> string list option -> CodegenThingol.code -> unit; |
|
26 |
||
27 |
val const_has_serialization: theory -> string list -> string -> bool; |
|
28 |
val tyco_has_serialization: theory -> string list -> string -> bool; |
|
29 |
||
30 |
val eval_verbose: bool ref; |
|
31 |
val eval_term: theory -> |
|
32 |
(string * 'a option ref) * CodegenThingol.iterm -> CodegenThingol.code |
|
33 |
-> 'a; |
|
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
34 |
end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
35 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
36 |
structure CodegenSerializer: CODEGEN_SERIALIZER = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
37 |
struct |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
38 |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
39 |
open BasicCodegenThingol; |
20845 | 40 |
val tracing = CodegenThingol.tracing; |
18850 | 41 |
|
20896 | 42 |
(** syntax **) |
43 |
||
44 |
(* basics *) |
|
18702 | 45 |
|
18216 | 46 |
datatype lrx = L | R | X; |
47 |
||
18516 | 48 |
datatype fixity = |
18216 | 49 |
BR |
50 |
| NOBR |
|
51 |
| INFX of (int * lrx); |
|
52 |
||
20699 | 53 |
type 'a pretty_syntax = int * (fixity -> (fixity -> 'a -> Pretty.T) |
18865 | 54 |
-> 'a list -> Pretty.T); |
18516 | 55 |
|
18216 | 56 |
fun eval_lrx L L = false |
57 |
| eval_lrx R R = false |
|
58 |
| eval_lrx _ _ = true; |
|
59 |
||
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
60 |
fun eval_fxy NOBR _ = false |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
61 |
| eval_fxy _ BR = true |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
62 |
| eval_fxy _ NOBR = false |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
63 |
| eval_fxy (INFX (pr, lr)) (INFX (pr_ctxt, lr_ctxt)) = |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
64 |
pr < pr_ctxt |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
65 |
orelse pr = pr_ctxt |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
66 |
andalso eval_lrx lr lr_ctxt |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
67 |
| eval_fxy _ (INFX _) = false; |
18216 | 68 |
|
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
69 |
fun gen_brackify _ [p] = p |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
70 |
| gen_brackify true (ps as _::_) = Pretty.enclose "(" ")" ps |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
71 |
| gen_brackify false (ps as _::_) = Pretty.block ps; |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
72 |
|
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
73 |
fun brackify fxy_ctxt ps = |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
74 |
gen_brackify (eval_fxy BR fxy_ctxt) (Pretty.breaks ps); |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
75 |
|
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
76 |
fun brackify_infix infx fxy_ctxt ps = |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
77 |
gen_brackify (eval_fxy (INFX infx) fxy_ctxt) (Pretty.breaks ps); |
18216 | 78 |
|
20976 | 79 |
fun mk_app mk_app' from_expr const_syntax fxy (app as ((c, (_, ty)), ts)) = |
20191 | 80 |
case const_syntax c |
20976 | 81 |
of NONE => brackify fxy (mk_app' app) |
20699 | 82 |
| SOME (i, pr) => |
83 |
let |
|
84 |
val k = if i < 0 then (length o fst o CodegenThingol.unfold_fun) ty else i |
|
20976 | 85 |
in if k <= length ts |
86 |
then case chop i ts of (ts1, ts2) => |
|
87 |
brackify fxy (pr fxy from_expr ts1 :: map (from_expr BR) ts2) |
|
88 |
else from_expr fxy (CodegenThingol.eta_expand app k) |
|
20699 | 89 |
end; |
90 |
||
20896 | 91 |
val first_upper = implode o nth_map 0 Symbol.to_ascii_upper o explode; |
20699 | 92 |
|
93 |
||
20896 | 94 |
(* user-defined syntax *) |
20699 | 95 |
|
20896 | 96 |
val str = setmp print_mode [] Pretty.str; |
20699 | 97 |
|
98 |
val (atomK, infixK, infixlK, infixrK) = |
|
99 |
("target_atom", "infix", "infixl", "infixr"); |
|
100 |
val _ = OuterSyntax.add_keywords [atomK, infixK, infixlK, infixrK]; |
|
101 |
||
102 |
datatype 'a mixfix = |
|
103 |
Arg of fixity |
|
20931 | 104 |
| Pretty of Pretty.T; |
18865 | 105 |
|
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
106 |
fun fillin_mixfix fxy_this ms fxy_ctxt pr args = |
18702 | 107 |
let |
108 |
fun fillin [] [] = |
|
19008 | 109 |
[] |
18702 | 110 |
| fillin (Arg fxy :: ms) (a :: args) = |
111 |
pr fxy a :: fillin ms args |
|
112 |
| fillin (Pretty p :: ms) args = |
|
113 |
p :: fillin ms args |
|
19008 | 114 |
| fillin [] _ = |
20389 | 115 |
error ("Inconsistent mixfix: too many arguments") |
19008 | 116 |
| fillin _ [] = |
20389 | 117 |
error ("Inconsistent mixfix: too less arguments"); |
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
118 |
in gen_brackify (eval_fxy fxy_this fxy_ctxt) (fillin ms args) end; |
18702 | 119 |
|
120 |
fun parse_infix (fixity as INFX (i, x)) s = |
|
121 |
let |
|
122 |
val l = case x of L => fixity |
|
123 |
| _ => INFX (i, X); |
|
124 |
val r = case x of R => fixity |
|
125 |
| _ => INFX (i, X); |
|
126 |
in |
|
20931 | 127 |
[Arg l, (Pretty o Pretty.brk) 1, (Pretty o str) s, (Pretty o Pretty.brk) 1, Arg r] |
18702 | 128 |
end; |
129 |
||
20931 | 130 |
fun parse_mixfix s = |
18335 | 131 |
let |
20931 | 132 |
val sym_any = Scan.one Symbol.not_eof; |
18702 | 133 |
val parse = Scan.repeat ( |
20931 | 134 |
($$ "_" -- $$ "_" >> K (Arg NOBR)) |
135 |
|| ($$ "_" >> K (Arg BR)) |
|
136 |
|| ($$ "/" |-- Scan.repeat ($$ " ") >> (Pretty o Pretty.brk o length)) |
|
18702 | 137 |
|| (Scan.repeat1 |
20931 | 138 |
( $$ "'" |-- sym_any |
139 |
|| Scan.unless ($$ "_" || $$ "/") |
|
18702 | 140 |
sym_any) >> (Pretty o str o implode))); |
20931 | 141 |
in case Scan.finite Symbol.stopper parse (Symbol.explode s) |
142 |
of (p, []) => p |
|
18702 | 143 |
| _ => error ("Malformed mixfix annotation: " ^ quote s) |
144 |
end; |
|
145 |
||
20931 | 146 |
fun parse_syntax num_args = |
18335 | 147 |
let |
18702 | 148 |
fun is_arg (Arg _) = true |
149 |
| is_arg _ = false; |
|
20931 | 150 |
fun parse_nonatomic s = |
151 |
case parse_mixfix s |
|
152 |
of [Pretty _] => |
|
20439 | 153 |
error ("Mixfix contains just one pretty element; either declare as " |
154 |
^ quote atomK ^ " or consider adding a break") |
|
155 |
| x => x; |
|
20931 | 156 |
fun mk fixity mfx ctxt = |
157 |
let |
|
158 |
val i = (length o List.filter is_arg) mfx; |
|
159 |
val _ = if i > num_args ctxt then error "Too many arguments in code syntax" else (); |
|
160 |
in (i, fillin_mixfix fixity mfx) end; |
|
20456 | 161 |
val parse = ( |
20439 | 162 |
OuterParse.$$$ infixK |-- OuterParse.nat |
163 |
>> (fn i => (parse_infix (INFX (i, X)), INFX (i, X))) |
|
164 |
|| OuterParse.$$$ infixlK |-- OuterParse.nat |
|
165 |
>> (fn i => (parse_infix (INFX (i, L)), INFX (i, L))) |
|
166 |
|| OuterParse.$$$ infixrK |-- OuterParse.nat |
|
167 |
>> (fn i => (parse_infix (INFX (i, R)), INFX (i, R))) |
|
20931 | 168 |
|| OuterParse.$$$ atomK |-- pair (parse_mixfix, NOBR) |
20439 | 169 |
|| pair (parse_nonatomic, BR) |
20456 | 170 |
) -- OuterParse.string >> (fn ((p, fxy), s) => (p s, fxy)); |
18702 | 171 |
in |
20931 | 172 |
parse #-> (fn (mfx, fixity) => pair (mk fixity mfx)) |
18702 | 173 |
end; |
174 |
||
175 |
||
20896 | 176 |
(* list and string serializer *) |
20401 | 177 |
|
178 |
fun implode_list c_nil c_cons e = |
|
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
179 |
let |
19202 | 180 |
fun dest_cons (IConst (c, _) `$ e1 `$ e2) = |
20401 | 181 |
if c = c_cons |
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
182 |
then SOME (e1, e2) |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
183 |
else NONE |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
184 |
| dest_cons _ = NONE; |
20401 | 185 |
val (es, e') = CodegenThingol.unfoldr dest_cons e; |
186 |
in case e' |
|
187 |
of IConst (c, _) => if c = c_nil then SOME es else NONE |
|
188 |
| _ => NONE |
|
189 |
end; |
|
190 |
||
191 |
fun implode_string mk_char mk_string es = |
|
192 |
if forall (fn IChar _ => true | _ => false) es |
|
193 |
then (SOME o str o mk_string o implode o map (fn IChar (c, _) => mk_char c)) es |
|
194 |
else NONE; |
|
195 |
||
196 |
fun pretty_ml_string c_nil c_cons mk_char mk_string target_implode = |
|
197 |
let |
|
198 |
fun pretty fxy pr [e] = |
|
199 |
case implode_list c_nil c_cons e |
|
200 |
of SOME es => (case implode_string mk_char mk_string es |
|
201 |
of SOME p => p |
|
202 |
| NONE => Pretty.block [str target_implode, Pretty.brk 1, pr BR e]) |
|
203 |
| NONE => Pretty.block [str target_implode, Pretty.brk 1, pr BR e] |
|
20699 | 204 |
in (1, pretty) end; |
20401 | 205 |
|
206 |
fun pretty_list c_nil c_cons mk_list mk_char_string (target_fxy, target_cons) = |
|
207 |
let |
|
208 |
fun default fxy pr e1 e2 = |
|
209 |
brackify_infix (target_fxy, R) fxy [ |
|
210 |
pr (INFX (target_fxy, X)) e1, |
|
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
211 |
str target_cons, |
20401 | 212 |
pr (INFX (target_fxy, R)) e2 |
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
213 |
]; |
20401 | 214 |
fun pretty fxy pr [e1, e2] = |
215 |
case Option.map (cons e1) (implode_list c_nil c_cons e2) |
|
216 |
of SOME es => |
|
217 |
(case mk_char_string |
|
218 |
of SOME (mk_char, mk_string) => |
|
219 |
(case implode_string mk_char mk_string es |
|
220 |
of SOME p => p |
|
221 |
| NONE => mk_list (map (pr NOBR) es)) |
|
222 |
| NONE => mk_list (map (pr NOBR) es)) |
|
223 |
| NONE => default fxy pr e1 e2; |
|
20699 | 224 |
in (2, pretty) end; |
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
225 |
|
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
226 |
|
20896 | 227 |
(* variable name contexts *) |
18216 | 228 |
|
20896 | 229 |
(*FIXME could name.ML do th whole job?*) |
230 |
fun make_vars names = (fold (fn name => Symtab.update_new (name, name)) names Symtab.empty, |
|
231 |
Name.make_context names); |
|
18216 | 232 |
|
20896 | 233 |
fun intro_vars names (namemap, namectxt) = |
234 |
let |
|
235 |
val (names', namectxt') = Name.variants names namectxt; |
|
236 |
val namemap' = fold2 (curry Symtab.update) names names' namemap; |
|
237 |
in (namemap', namectxt') end; |
|
19150 | 238 |
|
20896 | 239 |
fun lookup_var (namemap, _) name = case Symtab.lookup namemap name |
240 |
of SOME name' => name' |
|
241 |
| NONE => error ("invalid name in context: " ^ quote name); |
|
242 |
||
243 |
fun constructive_fun (name, (eqs, ty)) = |
|
18216 | 244 |
let |
20699 | 245 |
val is_cons = CodegenNames.has_nsp CodegenNames.nsp_dtco; |
20896 | 246 |
fun is_pat (IConst (c, _)) = is_cons c |
247 |
| is_pat (IVar _) = true |
|
248 |
| is_pat (t1 `$ t2) = |
|
249 |
is_pat t1 andalso is_pat t2 |
|
250 |
| is_pat (INum _) = true |
|
251 |
| is_pat (IChar _) = true |
|
252 |
| is_pat _ = false; |
|
253 |
fun check_eq (eq as (lhs, rhs)) = |
|
254 |
if forall is_pat lhs |
|
255 |
then SOME eq |
|
256 |
else (warning ("In function " ^ quote name ^ ", throwing away one " |
|
257 |
^ "non-executable function clause"); NONE) |
|
258 |
in case map_filter check_eq eqs |
|
259 |
of [] => error ("In function " ^ quote name ^ ", no " |
|
260 |
^ "executable function clauses found") |
|
261 |
| eqs => (name, (eqs, ty)) |
|
262 |
end; |
|
263 |
||
264 |
||
265 |
(** SML serializer **) |
|
266 |
||
267 |
datatype ml_def = |
|
268 |
MLFuns of (string * ((iterm list * iterm) list * typscheme)) list |
|
269 |
| MLDatas of (string * ((vname * sort) list * (string * itype list) list)) list |
|
270 |
| MLClass of string * (class list * (vname * (string * itype) list)) |
|
271 |
| MLClassinst of string * ((class * (string * (vname * sort) list)) |
|
272 |
* ((class * (string * inst list list)) list |
|
273 |
* (string * iterm) list)); |
|
274 |
||
275 |
fun pr_sml_def tyco_syntax const_syntax keyword_vars deresolv ml_def = |
|
276 |
let |
|
277 |
val is_cons = CodegenNames.has_nsp CodegenNames.nsp_dtco; |
|
278 |
fun dictvar v = |
|
20699 | 279 |
first_upper v ^ "_"; |
20896 | 280 |
val label = translate_string (fn "." => "__" | c => c) |
281 |
o NameSpace.pack o op @ o apsnd single o apfst (fst o split_last) o split_last o NameSpace.unpack; |
|
282 |
fun pr_tyvar (v, []) = |
|
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19936
diff
changeset
|
283 |
str "()" |
20896 | 284 |
| pr_tyvar (v, sort) = |
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19936
diff
changeset
|
285 |
let |
20896 | 286 |
fun pr_class class = |
287 |
str ("'" ^ v ^ " " ^ deresolv class); |
|
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19936
diff
changeset
|
288 |
in |
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19936
diff
changeset
|
289 |
Pretty.block [ |
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19936
diff
changeset
|
290 |
str "(", |
20896 | 291 |
(str o dictvar) v, |
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19936
diff
changeset
|
292 |
str ":", |
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19936
diff
changeset
|
293 |
case sort |
20896 | 294 |
of [class] => pr_class class |
295 |
| _ => Pretty.enum " *" "" "" (map pr_class sort), |
|
296 |
str ")" |
|
19953
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19936
diff
changeset
|
297 |
] |
2f54a51f1801
class package refinements, slight code generation refinements
haftmann
parents:
19936
diff
changeset
|
298 |
end; |
20896 | 299 |
fun pr_insts fxy iys = |
18885 | 300 |
let |
20896 | 301 |
fun pr_proj s = str ("#" ^ s); |
302 |
fun pr_lookup [] p = |
|
20401 | 303 |
p |
20896 | 304 |
| pr_lookup [p'] p = |
305 |
brackify BR [p', p] |
|
306 |
| pr_lookup (ps as _ :: _) p = |
|
307 |
brackify BR [Pretty.enum " o" "(" ")" ps, p]; |
|
308 |
fun pr_inst fxy (Instance (inst, iss)) = |
|
18885 | 309 |
brackify fxy ( |
20896 | 310 |
(str o deresolv) inst |
311 |
:: map (pr_insts BR) iss |
|
18885 | 312 |
) |
20896 | 313 |
| pr_inst fxy (Context (classes, (v, i))) = |
314 |
pr_lookup (map (pr_proj o label) classes |
|
315 |
@ (if i = ~1 then [] else [(pr_proj o string_of_int) (i+1)]) |
|
316 |
) ((str o dictvar) v); |
|
317 |
in case iys |
|
19253 | 318 |
of [] => str "()" |
20896 | 319 |
| [iy] => pr_inst fxy iy |
320 |
| _ :: _ => (Pretty.list "(" ")" o map (pr_inst NOBR)) iys |
|
18885 | 321 |
end; |
20896 | 322 |
fun pr_tycoexpr fxy (tyco, tys) = |
18963 | 323 |
let |
20896 | 324 |
val tyco' = (str o deresolv) tyco |
325 |
in case map (pr_typ BR) tys |
|
18963 | 326 |
of [] => tyco' |
327 |
| [p] => Pretty.block [p, Pretty.brk 1, tyco'] |
|
328 |
| (ps as _::_) => Pretty.block [Pretty.list "(" ")" ps, Pretty.brk 1, tyco'] |
|
329 |
end |
|
20896 | 330 |
and pr_typ fxy (tyco `%% tys) = |
18702 | 331 |
(case tyco_syntax tyco |
20896 | 332 |
of NONE => pr_tycoexpr fxy (tyco, tys) |
20699 | 333 |
| SOME (i, pr) => |
334 |
if not (i = length tys) |
|
20389 | 335 |
then error ("Number of argument mismatch in customary serialization: " |
18865 | 336 |
^ (string_of_int o length) tys ^ " given, " |
20699 | 337 |
^ string_of_int i ^ " expected") |
20896 | 338 |
else pr fxy pr_typ tys) |
339 |
| pr_typ fxy (t1 `-> t2) = |
|
340 |
(gen_brackify (case fxy of BR => false | _ => eval_fxy (INFX (1, R)) fxy) |
|
341 |
o Pretty.breaks) [ |
|
342 |
pr_typ (INFX (1, X)) t1, |
|
18702 | 343 |
str "->", |
20896 | 344 |
pr_typ (INFX (1, R)) t2 |
18702 | 345 |
] |
20896 | 346 |
| pr_typ fxy (ITyVar v) = |
18885 | 347 |
str ("'" ^ v); |
20896 | 348 |
fun pr_term vars fxy (IConst c) = |
349 |
pr_app vars fxy (c, []) |
|
350 |
| pr_term vars fxy (IVar v) = |
|
351 |
str (lookup_var vars v) |
|
352 |
| pr_term vars fxy (t as t1 `$ t2) = |
|
353 |
(case CodegenThingol.unfold_const_app t |
|
354 |
of SOME c_ts => pr_app vars fxy c_ts |
|
18865 | 355 |
| NONE => |
20976 | 356 |
brackify fxy [pr_term vars NOBR t1, pr_term vars BR t2]) |
20896 | 357 |
| pr_term vars fxy (t as _ `|-> _) = |
20105 | 358 |
let |
20896 | 359 |
val (ts, t') = CodegenThingol.unfold_abs t; |
20976 | 360 |
fun pr (p, _) vars = |
361 |
let |
|
362 |
val vs = CodegenThingol.fold_varnames (insert (op =)) p []; |
|
363 |
val vars' = intro_vars vs vars; |
|
364 |
in |
|
365 |
((Pretty.block o Pretty.breaks) [str "fn", pr_term vars' NOBR p, str "=>"], vars') |
|
366 |
end; |
|
367 |
val (ps, vars') = fold_map pr ts vars; |
|
368 |
in brackify BR (ps @ [pr_term vars' NOBR t']) end |
|
20896 | 369 |
| pr_term vars fxy (INum (n, _)) = |
370 |
brackify BR [(str o IntInf.toString) n, str ":", str "IntInf.int"] |
|
371 |
| pr_term vars _ (IChar (c, _)) = |
|
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
372 |
(str o prefix "#" o quote) |
20203 | 373 |
(let val i = ord c |
20191 | 374 |
in if i < 32 |
20105 | 375 |
then prefix "\\" (string_of_int i) |
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
376 |
else c |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
377 |
end) |
20896 | 378 |
| pr_term vars fxy (t as ICase ((_, [_]), _)) = |
18216 | 379 |
let |
20976 | 380 |
val (ts, t') = CodegenThingol.unfold_let t; |
381 |
fun mk ((p, _), t'') vars = |
|
20699 | 382 |
let |
20896 | 383 |
val vs = CodegenThingol.fold_varnames (insert (op =)) p []; |
384 |
val vars' = intro_vars vs vars; |
|
20699 | 385 |
in |
386 |
(Pretty.block [ |
|
387 |
(Pretty.block o Pretty.breaks) [ |
|
388 |
str "val", |
|
20896 | 389 |
pr_term vars' NOBR p, |
20699 | 390 |
str "=", |
20976 | 391 |
pr_term vars NOBR t'' |
20699 | 392 |
], |
393 |
str ";" |
|
20896 | 394 |
], vars') |
20699 | 395 |
end |
20896 | 396 |
val (binds, vars') = fold_map mk ts vars; |
397 |
in |
|
398 |
Pretty.chunks [ |
|
399 |
[str ("let"), Pretty.fbrk, binds |> Pretty.chunks] |> Pretty.block, |
|
20976 | 400 |
[str ("in"), Pretty.fbrk, pr_term vars' NOBR t'] |> Pretty.block, |
20896 | 401 |
str ("end") |
402 |
] end |
|
403 |
| pr_term vars fxy (ICase (((td, ty), b::bs), _)) = |
|
18216 | 404 |
let |
20896 | 405 |
fun pr definer (p, t) = |
20699 | 406 |
let |
20896 | 407 |
val vs = CodegenThingol.fold_varnames (insert (op =)) p []; |
408 |
val vars' = intro_vars vs vars; |
|
20699 | 409 |
in |
410 |
(Pretty.block o Pretty.breaks) [ |
|
411 |
str definer, |
|
20896 | 412 |
pr_term vars' NOBR p, |
20699 | 413 |
str "=>", |
20896 | 414 |
pr_term vars' NOBR t |
20699 | 415 |
] |
20896 | 416 |
end; |
417 |
in |
|
418 |
(Pretty.enclose "(" ")" o single o brackify fxy) ( |
|
419 |
str "case" |
|
420 |
:: pr_term vars NOBR td |
|
421 |
:: pr "of" b |
|
422 |
:: map (pr "|") bs |
|
423 |
) |
|
424 |
end |
|
20976 | 425 |
and pr_app' vars (app as ((c, (iss, ty)), ts)) = |
426 |
if is_cons c then let |
|
427 |
val k = (length o fst o CodegenThingol.unfold_fun) ty |
|
428 |
in if k < 2 then |
|
429 |
(str o deresolv) c :: map (pr_term vars BR) ts |
|
430 |
else if k = length ts then |
|
431 |
[(str o deresolv) c, Pretty.enum "," "(" ")" (map (pr_term vars NOBR) ts)] |
|
432 |
else [pr_term vars BR (CodegenThingol.eta_expand app k)] end else |
|
433 |
(str o deresolv) c :: map (pr_term vars BR) ts |
|
20896 | 434 |
and pr_app vars fxy (app as ((c, (iss, ty)), ts)) = |
435 |
case if is_cons c then [] else (map (pr_insts BR) o filter_out null) iss |
|
18885 | 436 |
of [] => |
20896 | 437 |
mk_app (pr_app' vars) (pr_term vars) const_syntax fxy app |
438 |
| ps => |
|
20456 | 439 |
if (is_none o const_syntax) c then |
20896 | 440 |
brackify fxy ((str o deresolv) c :: (ps @ map (pr_term vars BR) ts)) |
441 |
else |
|
442 |
error ("Cannot apply user defined serialization for function expecting dictionaries: " ^ quote c) |
|
443 |
fun eta_expand_poly_fun (funn as (_, (_::_, _))) = |
|
444 |
funn |
|
445 |
| eta_expand_poly_fun (funn as (_, ([_], ([], _)))) = |
|
446 |
funn |
|
447 |
| eta_expand_poly_fun (funn as (_, ([(_::_, _)], _))) = |
|
448 |
funn |
|
449 |
| eta_expand_poly_fun (funn as (_, ([(_, _ `|-> _)], _))) = |
|
450 |
funn |
|
451 |
| eta_expand_poly_fun (funn as (name, ([([], t)], tysm as (vs, ty)))) = |
|
452 |
if (null o fst o CodegenThingol.unfold_fun) ty |
|
453 |
orelse (not o null o filter_out (null o snd)) vs |
|
454 |
then funn |
|
455 |
else (name, ([([IVar "x"], t `$ IVar "x")], tysm)); |
|
456 |
fun pr_def (MLFuns raw_funns) = |
|
457 |
let |
|
458 |
val funns as (funn :: funns') = map (eta_expand_poly_fun o constructive_fun) raw_funns; |
|
459 |
val definer = |
|
460 |
let |
|
461 |
fun mk [] [] = "val" |
|
462 |
| mk (_::_) _ = "fun" |
|
463 |
| mk [] vs = if (null o filter_out (null o snd)) vs then "val" else "fun"; |
|
464 |
fun chk (_, ((ts, _) :: _, (vs, _))) NONE = SOME (mk ts vs) |
|
465 |
| chk (_, ((ts, _) :: _, (vs, _))) (SOME defi) = |
|
466 |
if defi = mk ts vs then SOME defi |
|
467 |
else error ("Mixing simultaneous vals and funs not implemented"); |
|
468 |
in the (fold chk funns NONE) end; |
|
469 |
fun pr_funn definer (name, (eqs as eq::eqs', (raw_vs, ty))) = |
|
470 |
let |
|
471 |
val vs = filter_out (null o snd) raw_vs; |
|
472 |
val shift = if null eqs' then I else |
|
473 |
map (Pretty.block o single o Pretty.block o single); |
|
474 |
fun pr_eq definer (ts, t) = |
|
475 |
let |
|
476 |
val consts = map_filter |
|
477 |
(fn c => if (is_some o const_syntax) c |
|
478 |
then NONE else (SOME o NameSpace.base o deresolv) c) |
|
479 |
((fold o CodegenThingol.fold_constnames) (insert (op =)) (t :: ts) []); |
|
480 |
val vars = keyword_vars |
|
481 |
|> intro_vars consts |
|
482 |
|> intro_vars ((fold o CodegenThingol.fold_unbound_varnames) (insert (op =)) (t :: ts) []); |
|
483 |
in |
|
484 |
(Pretty.block o Pretty.breaks) ( |
|
485 |
[str definer, (str o deresolv) name] |
|
486 |
@ (if null ts andalso null vs |
|
487 |
andalso not (ty = ITyVar "_")(*for evaluation*) |
|
488 |
then [str ":", pr_typ NOBR ty] |
|
489 |
else |
|
490 |
map pr_tyvar vs |
|
491 |
@ map (pr_term vars BR) ts) |
|
492 |
@ [str "=", pr_term vars NOBR t] |
|
493 |
) |
|
494 |
end |
|
495 |
in |
|
496 |
(Pretty.block o Pretty.fbreaks o shift) ( |
|
497 |
pr_eq definer eq |
|
498 |
:: map (pr_eq "|") eqs' |
|
499 |
) |
|
500 |
end; |
|
501 |
val (ps, p) = split_last (pr_funn definer funn :: map (pr_funn "and") funns'); |
|
502 |
in Pretty.chunks (ps @ [Pretty.block ([p, str ";"])]) end |
|
503 |
| pr_def (MLDatas (datas as (data :: datas'))) = |
|
504 |
let |
|
505 |
fun pr_co (co, []) = |
|
506 |
str (deresolv co) |
|
507 |
| pr_co (co, tys) = |
|
508 |
(Pretty.block o Pretty.breaks) [ |
|
509 |
str (deresolv co), |
|
510 |
str "of", |
|
511 |
Pretty.enum " *" "" "" (map (pr_typ (INFX (2, L))) tys) |
|
512 |
]; |
|
513 |
fun pr_data definer (tyco, (vs, cos)) = |
|
514 |
(Pretty.block o Pretty.breaks) ( |
|
515 |
str definer |
|
516 |
:: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs) |
|
517 |
:: str "=" |
|
518 |
:: separate (str "|") (map pr_co cos) |
|
519 |
); |
|
520 |
val (ps, p) = split_last (pr_data "datatype" data :: map (pr_data "and") datas'); |
|
521 |
in Pretty.chunks (ps @ [Pretty.block ([p, str ";"])]) end |
|
522 |
| pr_def (MLClass (class, (superclasses, (v, classops)))) = |
|
523 |
let |
|
524 |
val w = dictvar v; |
|
525 |
fun pr_superclass class = |
|
526 |
(Pretty.block o Pretty.breaks o map str) [ |
|
527 |
label class, ":", "'" ^ v, deresolv class |
|
528 |
]; |
|
529 |
fun pr_classop (classop, ty) = |
|
530 |
(Pretty.block o Pretty.breaks) [ |
|
531 |
(str o suffix "_" o NameSpace.base) classop, str ":", pr_typ NOBR ty |
|
532 |
]; |
|
533 |
fun pr_classop_fun (classop, _) = |
|
534 |
(Pretty.block o Pretty.breaks) [ |
|
535 |
str "fun", |
|
536 |
(str o deresolv) classop, |
|
537 |
Pretty.enclose "(" ")" [str (w ^ ":'" ^ v ^ " " ^ deresolv class)], |
|
538 |
str "=", |
|
539 |
str ("#" ^ (suffix "_" o NameSpace.base) classop), |
|
540 |
str (w ^ ";") |
|
541 |
]; |
|
542 |
in |
|
543 |
Pretty.chunks ( |
|
544 |
(Pretty.block o Pretty.breaks) [ |
|
545 |
str ("type '" ^ v), |
|
546 |
(str o deresolv) class, |
|
547 |
str "=", |
|
548 |
Pretty.enum "," "{" "};" ( |
|
549 |
map pr_superclass superclasses @ map pr_classop classops |
|
550 |
) |
|
551 |
] |
|
552 |
:: map pr_classop_fun classops |
|
553 |
) |
|
554 |
end |
|
555 |
| pr_def (MLClassinst (inst, ((class, (tyco, arity)), (superarities, classop_defs)))) = |
|
556 |
let |
|
557 |
fun pr_superclass (superclass, superinst_iss) = |
|
558 |
(Pretty.block o Pretty.breaks) [ |
|
559 |
(str o label) superclass, |
|
560 |
str "=", |
|
561 |
pr_insts NOBR [Instance superinst_iss] |
|
562 |
]; |
|
563 |
fun pr_classop_def (classop, t) = |
|
564 |
let |
|
565 |
val consts = map_filter |
|
566 |
(fn c => if (is_some o const_syntax) c |
|
567 |
then NONE else (SOME o NameSpace.base o deresolv) c) |
|
568 |
(CodegenThingol.fold_constnames (insert (op =)) t []); |
|
569 |
val vars = keyword_vars |
|
570 |
|> intro_vars consts; |
|
571 |
in |
|
572 |
(Pretty.block o Pretty.breaks) [ |
|
573 |
(str o suffix "_" o NameSpace.base) classop, |
|
574 |
str "=", |
|
575 |
pr_term vars NOBR t |
|
576 |
] |
|
577 |
end; |
|
578 |
in |
|
579 |
(Pretty.block o Pretty.breaks) ([ |
|
580 |
str (if null arity then "val" else "fun"), |
|
581 |
(str o deresolv) inst ] @ |
|
582 |
map pr_tyvar arity @ [ |
|
583 |
str "=", |
|
584 |
Pretty.enum "," "{" "}" (map pr_superclass superarities @ map pr_classop_def classop_defs), |
|
585 |
str ":", |
|
586 |
pr_tycoexpr NOBR (class, [tyco `%% map (ITyVar o fst) arity]) |
|
587 |
]) |
|
588 |
end; |
|
589 |
in pr_def ml_def end; |
|
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
590 |
|
20896 | 591 |
|
592 |
||
593 |
(** Haskell serializer **) |
|
594 |
||
595 |
fun pr_haskell class_syntax tyco_syntax const_syntax keyword_vars deresolv_here deresolv def = |
|
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
596 |
let |
20699 | 597 |
val is_cons = CodegenNames.has_nsp CodegenNames.nsp_dtco; |
20896 | 598 |
fun class_name class = case class_syntax class |
599 |
of NONE => deresolv class |
|
600 |
| SOME (class, _) => class; |
|
601 |
fun classop_name class classop = case class_syntax class |
|
602 |
of NONE => NameSpace.base classop |
|
603 |
| SOME (_, classop_syntax) => case classop_syntax classop |
|
604 |
of NONE => NameSpace.base classop |
|
605 |
| SOME classop => classop |
|
606 |
fun pr_typparms tyvars vs = |
|
607 |
case maps (fn (v, sort) => map (pair v) sort) vs |
|
608 |
of [] => str "" |
|
609 |
| xs => Pretty.block [ |
|
610 |
Pretty.enum "," "(" ")" ( |
|
611 |
map (fn (v, class) => str |
|
612 |
(class_name class ^ " " ^ lookup_var tyvars v)) xs |
|
613 |
), |
|
614 |
str " => " |
|
615 |
]; |
|
616 |
fun pr_tycoexpr tyvars fxy (tyco, tys) = |
|
617 |
brackify fxy (str tyco :: map (pr_typ tyvars BR) tys) |
|
618 |
and pr_typ tyvars fxy (tycoexpr as tyco `%% tys) = |
|
619 |
(case tyco_syntax tyco |
|
620 |
of NONE => |
|
621 |
pr_tycoexpr tyvars fxy (deresolv tyco, tys) |
|
622 |
| SOME (i, pr) => |
|
623 |
if not (i = length tys) |
|
624 |
then error ("Number of argument mismatch in customary serialization: " |
|
625 |
^ (string_of_int o length) tys ^ " given, " |
|
626 |
^ string_of_int i ^ " expected") |
|
627 |
else pr fxy (pr_typ tyvars) tys) |
|
628 |
| pr_typ tyvars fxy (t1 `-> t2) = |
|
629 |
brackify_infix (1, R) fxy [ |
|
630 |
pr_typ tyvars (INFX (1, X)) t1, |
|
631 |
str "->", |
|
632 |
pr_typ tyvars (INFX (1, R)) t2 |
|
633 |
] |
|
634 |
| pr_typ tyvars fxy (ITyVar v) = |
|
635 |
(str o lookup_var tyvars) v; |
|
636 |
fun pr_typscheme_expr tyvars (vs, tycoexpr) = |
|
637 |
Pretty.block [pr_typparms tyvars vs, pr_tycoexpr tyvars NOBR tycoexpr]; |
|
638 |
fun pr_typscheme tyvars (vs, ty) = |
|
639 |
Pretty.block [pr_typparms tyvars vs, pr_typ tyvars NOBR ty]; |
|
640 |
fun pr_term vars fxy (IConst c) = |
|
641 |
pr_app vars fxy (c, []) |
|
642 |
| pr_term vars fxy (t as (t1 `$ t2)) = |
|
643 |
(case CodegenThingol.unfold_const_app t |
|
644 |
of SOME app => pr_app vars fxy app |
|
645 |
| _ => |
|
646 |
brackify fxy [ |
|
647 |
pr_term vars NOBR t1, |
|
648 |
pr_term vars BR t2 |
|
649 |
]) |
|
650 |
| pr_term vars fxy (IVar v) = |
|
651 |
(str o lookup_var vars) v |
|
652 |
| pr_term vars fxy (t as _ `|-> _) = |
|
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
653 |
let |
20896 | 654 |
val (ts, t') = CodegenThingol.unfold_abs t; |
20976 | 655 |
fun pr (p, _) vars = |
656 |
let |
|
657 |
val vs = CodegenThingol.fold_varnames (insert (op =)) p []; |
|
658 |
val vars' = intro_vars vs vars; |
|
659 |
in (pr_term vars' BR p, vars') end; |
|
660 |
val (ps, vars') = fold_map pr ts vars; |
|
20896 | 661 |
in |
662 |
brackify BR ( |
|
663 |
str "\\" |
|
20976 | 664 |
:: ps @ [ |
20896 | 665 |
str "->", |
666 |
pr_term vars' NOBR t' |
|
667 |
]) |
|
668 |
end |
|
669 |
| pr_term vars fxy (INum (n, _)) = |
|
670 |
if n > 0 then |
|
671 |
(str o IntInf.toString) n |
|
672 |
else |
|
673 |
brackify BR [(str o Library.prefix "-" o IntInf.toString o IntInf.~) n] |
|
674 |
| pr_term vars fxy (IChar (c, _)) = |
|
675 |
(str o enclose "'" "'") |
|
676 |
(let val i = (Char.ord o the o Char.fromString) c |
|
677 |
in if i < 32 |
|
678 |
then Library.prefix "\\" (string_of_int i) |
|
679 |
else c |
|
680 |
end) |
|
681 |
| pr_term vars fxy (t as ICase ((_, [_]), _)) = |
|
18216 | 682 |
let |
20896 | 683 |
val (ts, t) = CodegenThingol.unfold_let t; |
684 |
fun pr ((p, _), t) vars = |
|
685 |
let |
|
686 |
val vs = CodegenThingol.fold_varnames (insert (op =)) p []; |
|
687 |
val vars' = intro_vars vs vars; |
|
688 |
in |
|
689 |
((Pretty.block o Pretty.breaks) [ |
|
690 |
pr_term vars' BR p, |
|
691 |
str "=", |
|
692 |
pr_term vars NOBR t |
|
693 |
], vars') |
|
694 |
end; |
|
695 |
val (binds, vars') = fold_map pr ts vars; |
|
696 |
in Pretty.chunks [ |
|
697 |
[str ("let"), Pretty.fbrk, binds |> Pretty.chunks] |> Pretty.block, |
|
698 |
[str ("in "), pr_term vars' NOBR t] |> Pretty.block |
|
699 |
] end |
|
700 |
| pr_term vars fxy (ICase (((td, _), bs), _)) = |
|
701 |
let |
|
702 |
fun pr (p, t) = |
|
703 |
let |
|
704 |
val vs = CodegenThingol.fold_varnames (insert (op =)) p []; |
|
705 |
val vars' = intro_vars vs vars; |
|
706 |
in |
|
707 |
(Pretty.block o Pretty.breaks) [ |
|
708 |
pr_term vars' NOBR p, |
|
709 |
str "->", |
|
710 |
pr_term vars' NOBR t |
|
711 |
] |
|
712 |
end |
|
713 |
in |
|
714 |
(Pretty.enclose "(" ")" o Pretty.breaks) [ |
|
715 |
str "case", |
|
716 |
pr_term vars NOBR td, |
|
717 |
str "of", |
|
718 |
(Pretty.chunks o map pr) bs |
|
719 |
] |
|
720 |
end |
|
20976 | 721 |
and pr_app' vars ((c, _), ts) = |
20896 | 722 |
(str o deresolv) c :: map (pr_term vars BR) ts |
723 |
and pr_app vars fxy = |
|
724 |
mk_app (pr_app' vars) (pr_term vars) const_syntax fxy; |
|
725 |
fun pr_def (name, CodegenThingol.Fun (funn as (eqs, (vs, ty)))) = |
|
726 |
let |
|
727 |
val tyvars = intro_vars (map fst vs) keyword_vars; |
|
728 |
fun pr_eq (ts, t) = |
|
20699 | 729 |
let |
730 |
val consts = map_filter |
|
731 |
(fn c => if (is_some o const_syntax) c |
|
20896 | 732 |
then NONE else (SOME o NameSpace.base o deresolv) c) |
733 |
((fold o CodegenThingol.fold_constnames) (insert (op =)) (t :: ts) []); |
|
734 |
val vars = keyword_vars |
|
735 |
|> intro_vars consts |
|
736 |
|> intro_vars ((fold o CodegenThingol.fold_unbound_varnames) (insert (op =)) (t :: ts) []); |
|
20699 | 737 |
in |
738 |
(Pretty.block o Pretty.breaks) ( |
|
20896 | 739 |
(str o deresolv_here) name |
740 |
:: map (pr_term vars BR) ts |
|
741 |
@ [str "=", pr_term vars NOBR t] |
|
20699 | 742 |
) |
20896 | 743 |
end; |
744 |
in |
|
745 |
Pretty.chunks ( |
|
746 |
Pretty.block [ |
|
747 |
(str o suffix " ::" o deresolv_here) name, |
|
748 |
Pretty.brk 1, |
|
749 |
pr_typscheme tyvars (vs, ty) |
|
750 |
] |
|
751 |
:: (map pr_eq o fst o snd o constructive_fun) (name, funn) |
|
752 |
) |
|
753 |
end |> SOME |
|
754 |
| pr_def (name, CodegenThingol.Datatype (vs, [(co, [ty])])) = |
|
755 |
let |
|
756 |
val tyvars = intro_vars (map fst vs) keyword_vars; |
|
18216 | 757 |
in |
20896 | 758 |
(Pretty.block o Pretty.breaks) [ |
759 |
str "newtype", |
|
760 |
pr_typscheme_expr tyvars (vs, (deresolv_here name, map (ITyVar o fst) vs)), |
|
761 |
str "=", |
|
762 |
(str o deresolv_here) co, |
|
763 |
pr_typ tyvars BR ty |
|
764 |
] |
|
765 |
end |> SOME |
|
766 |
| pr_def (name, CodegenThingol.Datatype (vs, co :: cos)) = |
|
767 |
let |
|
768 |
val tyvars = intro_vars (map fst vs) keyword_vars; |
|
769 |
fun pr_co (co, tys) = |
|
770 |
(Pretty.block o Pretty.breaks) ( |
|
771 |
(str o deresolv_here) co |
|
772 |
:: map (pr_typ tyvars BR) tys |
|
773 |
) |
|
774 |
in |
|
775 |
(Pretty.block o Pretty.breaks) ( |
|
776 |
str "data" |
|
777 |
:: pr_typscheme_expr tyvars (vs, (deresolv_here name, map (ITyVar o fst) vs)) |
|
778 |
:: str "=" |
|
779 |
:: pr_co co |
|
780 |
:: map ((fn p => Pretty.block [str "| ", p]) o pr_co) cos |
|
18912 | 781 |
) |
20896 | 782 |
end |> SOME |
783 |
| pr_def (_, CodegenThingol.Datatypecons _) = |
|
784 |
NONE |
|
785 |
| pr_def (name, CodegenThingol.Class (superclasss, (v, classops))) = |
|
786 |
let |
|
787 |
val tyvars = intro_vars [v] keyword_vars; |
|
788 |
fun pr_classop (classop, ty) = |
|
19136 | 789 |
Pretty.block [ |
20896 | 790 |
str (deresolv_here classop ^ " ::"), |
19136 | 791 |
Pretty.brk 1, |
20896 | 792 |
pr_typ tyvars NOBR ty |
19136 | 793 |
] |
20896 | 794 |
in |
795 |
Pretty.block [ |
|
796 |
str "class ", |
|
797 |
pr_typparms tyvars [(v, superclasss)], |
|
798 |
str (deresolv_here name ^ " " ^ v), |
|
799 |
str " where", |
|
800 |
Pretty.fbrk, |
|
801 |
Pretty.chunks (map pr_classop classops) |
|
802 |
] |
|
803 |
end |> SOME |
|
804 |
| pr_def (_, CodegenThingol.Classmember _) = |
|
805 |
NONE |
|
806 |
| pr_def (_, CodegenThingol.Classinst ((class, (tyco, vs)), (_, classop_defs))) = |
|
807 |
let |
|
808 |
val tyvars = intro_vars (map fst vs) keyword_vars; |
|
809 |
in |
|
810 |
Pretty.block [ |
|
811 |
str "instance ", |
|
812 |
pr_typparms tyvars vs, |
|
813 |
str (class_name class ^ " "), |
|
814 |
pr_typ tyvars BR (tyco `%% map (ITyVar o fst) vs), |
|
815 |
str " where", |
|
816 |
Pretty.fbrk, |
|
817 |
Pretty.chunks (map (fn (classop, t) => |
|
818 |
let |
|
819 |
val consts = map_filter |
|
820 |
(fn c => if (is_some o const_syntax) c |
|
821 |
then NONE else (SOME o NameSpace.base o deresolv) c) |
|
822 |
(CodegenThingol.fold_constnames (insert (op =)) t []); |
|
823 |
val vars = keyword_vars |
|
824 |
|> intro_vars consts; |
|
825 |
in |
|
826 |
(Pretty.block o Pretty.breaks) [ |
|
827 |
(str o classop_name class) classop, |
|
828 |
str "=", |
|
829 |
pr_term vars NOBR t |
|
830 |
] |
|
831 |
end |
|
832 |
) classop_defs) |
|
833 |
] |
|
834 |
end |> SOME |
|
20940 | 835 |
in pr_def def end; |
20896 | 836 |
|
837 |
||
838 |
(** generic abstract serializer **) |
|
839 |
||
840 |
structure NameMangler = NameManglerFun ( |
|
841 |
type ctxt = (string * string -> string) * (string -> string option); |
|
842 |
type src = string * string; |
|
843 |
val ord = prod_ord string_ord string_ord; |
|
844 |
fun mk (postprocess, validate) ((shallow, name), 0) = |
|
845 |
let |
|
846 |
val name' = postprocess (shallow, name); |
|
847 |
in case validate name' |
|
848 |
of NONE => name' |
|
849 |
| _ => mk (postprocess, validate) ((shallow, name), 1) |
|
850 |
end |
|
851 |
| mk (postprocess, validate) (("", name), i) = |
|
852 |
postprocess ("", name ^ replicate_string i "'") |
|
853 |
|> perhaps validate |
|
854 |
| mk (postprocess, validate) ((shallow, name), 1) = |
|
855 |
postprocess (shallow, shallow ^ "_" ^ name) |
|
856 |
|> perhaps validate |
|
857 |
| mk (postprocess, validate) ((shallow, name), i) = |
|
858 |
postprocess (shallow, name ^ replicate_string i "'") |
|
859 |
|> perhaps validate; |
|
860 |
fun is_valid _ _ = true; |
|
861 |
fun maybe_unique _ _ = NONE; |
|
862 |
fun re_mangle _ dst = error ("No such definition name: " ^ quote dst); |
|
863 |
); |
|
864 |
||
865 |
(*FIXME refactor this properly*) |
|
866 |
fun code_serialize seri_defs seri_module validate postprocess nsp_conn name_root |
|
867 |
(code : CodegenThingol.code) = |
|
868 |
let |
|
869 |
datatype node = Def of CodegenThingol.def | Module of node Graph.T; |
|
870 |
fun dest_modl (Module m) = m; |
|
871 |
fun dest_name name = |
|
872 |
let |
|
873 |
val (names, name_base) = (split_last o NameSpace.unpack) name; |
|
874 |
val (names_mod, name_shallow) = split_last names; |
|
875 |
in (names_mod, NameSpace.pack [name_shallow, name_base]) end; |
|
876 |
fun mk_deresolver module nsp_conn postprocess validate = |
|
877 |
let |
|
878 |
datatype tabnode = N of string * tabnode Symtab.table option; |
|
879 |
fun mk module manglers tab = |
|
880 |
let |
|
881 |
fun mk_name name = |
|
882 |
case NameSpace.unpack name |
|
883 |
of [n] => ("", n) |
|
884 |
| [s, n] => (s, n); |
|
885 |
fun in_conn (shallow, conn) = |
|
886 |
member (op = : string * string -> bool) conn shallow; |
|
887 |
fun add_name name = |
|
888 |
let |
|
889 |
val n as (shallow, _) = mk_name name; |
|
890 |
in |
|
891 |
AList.map_entry_yield in_conn shallow ( |
|
892 |
NameMangler.declare (postprocess, validate) n |
|
893 |
#-> (fn n' => pair (name, n')) |
|
894 |
) #> apfst the |
|
895 |
end; |
|
896 |
val (renamings, manglers') = |
|
897 |
fold_map add_name (Graph.keys module) manglers; |
|
898 |
fun extend_tab (n, n') = |
|
899 |
if (length o NameSpace.unpack) n = 1 |
|
900 |
then |
|
901 |
Symtab.update_new |
|
902 |
(n, N (n', SOME (mk ((dest_modl o Graph.get_node module) n) manglers' Symtab.empty))) |
|
903 |
else |
|
904 |
Symtab.update_new (n, N (n', NONE)); |
|
905 |
in fold extend_tab renamings tab end; |
|
906 |
fun get_path_name [] tab = |
|
907 |
([], SOME tab) |
|
908 |
| get_path_name [p] tab = |
|
909 |
let |
|
910 |
val SOME (N (p', tab')) = Symtab.lookup tab p |
|
911 |
in ([p'], tab') end |
|
912 |
| get_path_name [p1, p2] tab = |
|
913 |
(case Symtab.lookup tab p1 |
|
914 |
of SOME (N (p', SOME tab')) => |
|
915 |
let |
|
916 |
val (ps', tab'') = get_path_name [p2] tab' |
|
917 |
in (p' :: ps', tab'') end |
|
918 |
| NONE => |
|
919 |
let |
|
920 |
val SOME (N (p', NONE)) = Symtab.lookup tab (NameSpace.pack [p1, p2]) |
|
921 |
in ([p'], NONE) end) |
|
922 |
| get_path_name (p::ps) tab = |
|
923 |
let |
|
924 |
val SOME (N (p', SOME tab')) = Symtab.lookup tab p |
|
925 |
val (ps', tab'') = get_path_name ps tab' |
|
926 |
in (p' :: ps', tab'') end; |
|
927 |
fun deresolv tab prefix name = |
|
928 |
let |
|
929 |
val (common, (_, rem)) = chop_prefix (op =) (prefix, NameSpace.unpack name); |
|
930 |
val (_, SOME tab') = get_path_name common tab; |
|
931 |
val (name', _) = get_path_name rem tab'; |
|
932 |
in NameSpace.pack name' end handle BIND => (error ("Missing name: " ^ quote name ^ ", in " ^ quote (NameSpace.pack prefix))); |
|
933 |
in deresolv (mk module (AList.make (K NameMangler.empty) nsp_conn) Symtab.empty) end; |
|
934 |
fun allimports_of modl = |
|
935 |
let |
|
936 |
fun imps_of prfx (Module modl) imps tab = |
|
937 |
let |
|
938 |
val this = NameSpace.pack prfx; |
|
939 |
val name_con = (rev o Graph.strong_conn) modl; |
|
940 |
in |
|
941 |
tab |
|
942 |
|> pair [] |
|
943 |
|> fold (fn names => fn (imps', tab) => |
|
944 |
tab |
|
945 |
|> fold_map (fn name => |
|
946 |
imps_of (prfx @ [name]) (Graph.get_node modl name) (imps' @ imps)) names |
|
947 |
|-> (fn imps'' => pair (flat imps'' @ imps'))) name_con |
|
948 |
|-> (fn imps' => |
|
949 |
Symtab.update_new (this, imps' @ imps) |
|
950 |
#> pair (this :: imps')) |
|
951 |
end |
|
952 |
| imps_of prfx (Def _) imps tab = |
|
953 |
([], tab); |
|
954 |
in snd (imps_of [] (Module modl) [] Symtab.empty) end; |
|
955 |
fun add_def ((names_mod, name_id), def) = |
|
956 |
let |
|
957 |
fun add [] = |
|
958 |
Graph.new_node (name_id, Def def) |
|
959 |
| add (m::ms) = |
|
960 |
Graph.default_node (m, Module Graph.empty) |
|
961 |
#> Graph.map_node m (Module o add ms o dest_modl) |
|
962 |
in add names_mod end; |
|
963 |
fun add_dep (name1, name2) = |
|
964 |
if name1 = name2 then I |
|
965 |
else |
|
966 |
let |
|
967 |
val m1 = dest_name name1 |> apsnd single |> (op @); |
|
968 |
val m2 = dest_name name2 |> apsnd single |> (op @); |
|
969 |
val (ms, (r1, r2)) = chop_prefix (op =) (m1, m2); |
|
970 |
val (ms, (s1::r1, s2::r2)) = chop_prefix (op =) (m1, m2); |
|
971 |
val add_edge = |
|
972 |
if null r1 andalso null r2 |
|
973 |
then Graph.add_edge |
|
974 |
else fn edge => fn gr => (Graph.add_edge_acyclic edge gr |
|
975 |
handle Graph.CYCLES _ => |
|
976 |
error ("Module dependency " |
|
977 |
^ quote name1 ^ " -> " ^ quote name2 ^ " would result in module dependency cycle")) |
|
978 |
fun add [] node = |
|
979 |
node |
|
980 |
|> add_edge (s1, s2) |
|
981 |
| add (m::ms) node = |
|
982 |
node |
|
983 |
|> Graph.map_node m (Module o add ms o dest_modl); |
|
984 |
in add ms end; |
|
985 |
val root_module = |
|
986 |
Graph.empty |
|
987 |
|> Graph.fold (fn (name, (def, _)) => add_def (dest_name name, def)) code |
|
988 |
|> Graph.fold (fn (name, (_, (_, deps))) => |
|
989 |
fold (curry add_dep name) deps) code; |
|
990 |
val names = map fst (Graph.dest root_module); |
|
991 |
val imptab = allimports_of root_module; |
|
992 |
val resolver = mk_deresolver root_module nsp_conn postprocess validate; |
|
993 |
fun sresolver s = (resolver o NameSpace.unpack) s |
|
994 |
fun mk_name prfx name = |
|
995 |
let |
|
996 |
val name_qual = NameSpace.pack (prfx @ [name]) |
|
997 |
in (name_qual, resolver prfx name_qual) end; |
|
998 |
fun is_bot (_, (Def Bot)) = true |
|
999 |
| is_bot _ = false; |
|
1000 |
fun mk_contents prfx module = |
|
1001 |
map_filter (seri prfx) |
|
1002 |
((map (AList.make (Graph.get_node module)) o rev o Graph.strong_conn) module) |
|
1003 |
and seri prfx [(name, Module modl)] = |
|
1004 |
seri_module (resolver []) (map (resolver []) ((the o Symtab.lookup imptab) (NameSpace.pack (prfx @ [name])))) |
|
1005 |
(mk_name prfx name, mk_contents (prfx @ [name]) modl) |
|
1006 |
| seri prfx ds = |
|
1007 |
seri_defs sresolver (NameSpace.pack prfx) |
|
1008 |
(map (fn (name, Def def) => (fst (mk_name prfx name), def)) ds) |
|
1009 |
in |
|
1010 |
seri_module (resolver []) (map (resolver []) ((the o Symtab.lookup imptab) "")) |
|
1011 |
(("", name_root), (mk_contents [] root_module)) |
|
1012 |
end; |
|
1013 |
||
1014 |
fun abstract_serializer (target, nspgrp) name_root (from_defs, from_module, validator, postproc) |
|
1015 |
postprocess (class_syntax, tyco_syntax, const_syntax) |
|
1016 |
(drop, select) code = |
|
1017 |
let |
|
1018 |
fun project NONE code = code |
|
1019 |
| project (SOME names) code = |
|
1020 |
let |
|
1021 |
fun check name = if member (op =) drop name |
|
1022 |
then error ("shadowed definition " ^ quote name ^ " selected for serialization") |
|
1023 |
else if can (Graph.get_node code) name |
|
1024 |
then () |
|
1025 |
else error ("dropped definition " ^ quote name ^ " selected for serialization") |
|
1026 |
val names' = (map o tap) check names; |
|
1027 |
in CodegenThingol.project_code names code end; |
|
1028 |
fun from_module' resolv imps ((name_qual, name), defs) = |
|
1029 |
from_module resolv imps ((name_qual, name), defs) |
|
1030 |
|> postprocess (resolv name_qual); |
|
1031 |
in |
|
1032 |
code |
|
1033 |
|> tracing (fn _ => "dropping shadowed definitions...") |
|
1034 |
|> CodegenThingol.delete_garbage drop |
|
1035 |
|> tracing (fn _ => "projecting...") |
|
1036 |
|> project select |
|
1037 |
|> tracing (fn _ => "serializing...") |
|
1038 |
|> code_serialize (from_defs (class_syntax, tyco_syntax, const_syntax)) |
|
1039 |
from_module' validator postproc nspgrp name_root |
|
1040 |
|> K () |
|
1041 |
end; |
|
1042 |
||
1043 |
fun abstract_validator keywords name = |
|
1044 |
let |
|
1045 |
fun replace_invalid c = (*FIXME*) |
|
1046 |
if Symbol.is_ascii_letter c orelse Symbol.is_ascii_digit c orelse c = "'" |
|
1047 |
andalso not (NameSpace.separator = c) |
|
1048 |
then c |
|
1049 |
else "_" |
|
1050 |
fun suffix_it name= |
|
1051 |
name |
|
1052 |
|> member (op =) keywords ? suffix "'" |
|
1053 |
|> (fn name' => if name = name' then name else suffix_it name') |
|
1054 |
in |
|
1055 |
name |
|
1056 |
|> translate_string replace_invalid |
|
1057 |
|> suffix_it |
|
1058 |
|> (fn name' => if name = name' then NONE else SOME name') |
|
1059 |
end; |
|
1060 |
||
1061 |
fun write_file mkdir path p = ( |
|
1062 |
if mkdir |
|
1063 |
then |
|
1064 |
File.mkdir (Path.dir path) |
|
1065 |
else (); |
|
1066 |
File.write path (Pretty.output p ^ "\n"); |
|
1067 |
p |
|
1068 |
); |
|
1069 |
||
1070 |
fun mk_module_file postprocess_module ext path name p = |
|
1071 |
let |
|
1072 |
val prfx = Path.dir path; |
|
1073 |
val name' = case name |
|
1074 |
of "" => Path.base path |
|
1075 |
| _ => (Path.ext ext o Path.unpack o implode o separate "/" o NameSpace.unpack) name; |
|
1076 |
in |
|
1077 |
p |
|
20940 | 1078 |
|> Pretty.setmp_margin 999999 (write_file true (Path.append prfx name')) |
20896 | 1079 |
|> postprocess_module name |
1080 |
end; |
|
1081 |
||
1082 |
fun parse_args f args = |
|
1083 |
case f args |
|
1084 |
of (x, []) => x |
|
1085 |
| (_, _) => error "bad serializer arguments"; |
|
1086 |
||
1087 |
fun parse_single_file serializer = |
|
1088 |
parse_args (Args.name |
|
1089 |
>> (fn path => serializer |
|
1090 |
(fn "" => write_file false (Path.unpack path) #> K NONE |
|
1091 |
| _ => SOME))); |
|
1092 |
||
1093 |
fun parse_multi_file postprocess_module ext serializer = |
|
1094 |
parse_args (Args.name |
|
1095 |
>> (fn path => (serializer o mk_module_file postprocess_module ext) (Path.unpack path))); |
|
1096 |
||
1097 |
fun parse_internal serializer = |
|
1098 |
parse_args (Args.name |
|
1099 |
>> (fn "-" => serializer |
|
20926 | 1100 |
(fn "" => (fn p => (use_text Output.ml_output false (Pretty.output p); NONE)) |
20896 | 1101 |
| _ => SOME) |
1102 |
| _ => Scan.fail ())); |
|
1103 |
||
1104 |
fun parse_stdout serializer = |
|
1105 |
parse_args (Args.name |
|
1106 |
>> (fn "_" => serializer |
|
1107 |
(fn "" => (fn p => (Pretty.writeln p; NONE)) |
|
1108 |
| _ => SOME) |
|
1109 |
| _ => Scan.fail ())); |
|
1110 |
||
1111 |
val nsp_module = CodegenNames.nsp_module; |
|
1112 |
val nsp_class = CodegenNames.nsp_class; |
|
1113 |
val nsp_tyco = CodegenNames.nsp_tyco; |
|
1114 |
val nsp_inst = CodegenNames.nsp_inst; |
|
1115 |
val nsp_fun = CodegenNames.nsp_fun; |
|
1116 |
val nsp_classop = CodegenNames.nsp_classop; |
|
1117 |
val nsp_dtco = CodegenNames.nsp_dtco; |
|
1118 |
val nsp_eval = CodegenNames.nsp_eval; |
|
1119 |
||
1120 |
||
1121 |
(** ML serializer **) |
|
1122 |
||
1123 |
local |
|
1124 |
||
1125 |
val reserved_ml' = [ |
|
1126 |
"bool", "int", "list", "unit", "option", "true", "false", "not", |
|
1127 |
"NONE", "SOME", "o", "string", "char", "String", "Term" |
|
1128 |
]; |
|
1129 |
||
1130 |
fun ml_from_defs (_, tyco_syntax, const_syntax) deresolver prefx defs = |
|
1131 |
let |
|
1132 |
val seri = pr_sml_def tyco_syntax const_syntax |
|
1133 |
(make_vars (ThmDatabase.ml_reserved @ reserved_ml')) |
|
1134 |
(deresolver prefx) #> SOME; |
|
1135 |
val filter_funs = |
|
1136 |
map |
|
1137 |
(fn (name, CodegenThingol.Fun info) => (name, info) |
|
1138 |
| (name, def) => error ("Function block containing illegal def: " ^ quote name) |
|
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1139 |
) |
20896 | 1140 |
#> MLFuns; |
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1141 |
val filter_datatype = |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19466
diff
changeset
|
1142 |
map_filter |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
1143 |
(fn (name, CodegenThingol.Datatype info) => SOME (name, info) |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
1144 |
| (name, CodegenThingol.Datatypecons _) => NONE |
20896 | 1145 |
| (name, def) => error ("Datatype block containing illegal def: " ^ quote name) |
1146 |
) |
|
1147 |
#> MLDatas; |
|
20191 | 1148 |
fun filter_class defs = |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19466
diff
changeset
|
1149 |
case map_filter |
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
1150 |
(fn (name, CodegenThingol.Class info) => SOME (name, info) |
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
1151 |
| (name, CodegenThingol.Classmember _) => NONE |
20896 | 1152 |
| (name, def) => error ("Class block containing illegal def: " ^ quote name) |
1153 |
) defs |
|
1154 |
of [class] => MLClass class |
|
20389 | 1155 |
| _ => error ("Class block without class: " ^ (commas o map (quote o fst)) defs) |
18850 | 1156 |
in case defs |
20896 | 1157 |
of (_, CodegenThingol.Fun _)::_ => (seri o filter_funs) defs |
1158 |
| (_, CodegenThingol.Datatypecons _)::_ => (seri o filter_datatype) defs |
|
1159 |
| (_, CodegenThingol.Datatype _)::_ => (seri o filter_datatype) defs |
|
1160 |
| (_, CodegenThingol.Class _)::_ => (seri o filter_class) defs |
|
1161 |
| (_, CodegenThingol.Classmember _)::_ => (seri o filter_class) defs |
|
1162 |
| [(inst, CodegenThingol.Classinst info)] => seri (MLClassinst (inst, info)) |
|
20389 | 1163 |
| defs => error ("Illegal mutual dependencies: " ^ (commas o map fst) defs) |
18216 | 1164 |
end; |
1165 |
||
20699 | 1166 |
fun ml_serializer root_name target nspgrp = |
18216 | 1167 |
let |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
1168 |
fun ml_from_module resolv _ ((_, name), ps) = |
18756 | 1169 |
Pretty.chunks ([ |
18702 | 1170 |
str ("structure " ^ name ^ " = "), |
1171 |
str "struct", |
|
1172 |
str "" |
|
1173 |
] @ separate (str "") ps @ [ |
|
1174 |
str "", |
|
1175 |
str ("end; (* struct " ^ name ^ " *)") |
|
18756 | 1176 |
]); |
20401 | 1177 |
fun postproc (shallow, n) = |
1178 |
let |
|
1179 |
fun ch_first f = String.implode o nth_map 0 f o String.explode; |
|
20699 | 1180 |
in if shallow = CodegenNames.nsp_dtco |
20401 | 1181 |
then ch_first Char.toUpper n |
1182 |
else n |
|
1183 |
end; |
|
20191 | 1184 |
in abstract_serializer (target, nspgrp) |
20896 | 1185 |
root_name (ml_from_defs, ml_from_module, |
20699 | 1186 |
abstract_validator (ThmDatabase.ml_reserved @ reserved_ml'), postproc) end; |
20191 | 1187 |
|
1188 |
in |
|
1189 |
||
20896 | 1190 |
fun ml_from_thingol target args = |
20699 | 1191 |
let |
1192 |
val serializer = ml_serializer "ROOT" target [[nsp_module], [nsp_class, nsp_tyco], |
|
1193 |
[nsp_fun, nsp_dtco, nsp_class, nsp_classop, nsp_inst]] |
|
18850 | 1194 |
val parse_multi = |
20896 | 1195 |
Args.name |
20191 | 1196 |
#-> (fn "dir" => |
18850 | 1197 |
parse_multi_file |
1198 |
(K o SOME o str o suffix ";" o prefix "val _ = use " |
|
1199 |
o quote o suffix ".ML" o translate_string (fn "." => "/" | s => s)) "ML" serializer |
|
1200 |
| _ => Scan.fail ()); |
|
18282 | 1201 |
in |
20896 | 1202 |
(parse_multi |
19884 | 1203 |
|| parse_internal serializer |
20105 | 1204 |
|| parse_stdout serializer |
20896 | 1205 |
|| parse_single_file serializer) args |
18216 | 1206 |
end; |
1207 |
||
20389 | 1208 |
val eval_verbose = ref false; |
1209 |
||
20896 | 1210 |
fun eval_term_proto thy data hidden ((ref_name, reff), e) code = |
20191 | 1211 |
let |
20699 | 1212 |
val (val_name, code') = CodegenThingol.add_eval_def (nsp_eval, e) code; |
20191 | 1213 |
val struct_name = "EVAL"; |
20401 | 1214 |
fun output p = if !eval_verbose then (Pretty.writeln p; Pretty.output p) |
1215 |
else Pretty.output p; |
|
20699 | 1216 |
val serializer = ml_serializer struct_name "SML" [[nsp_module], [nsp_class, nsp_tyco], |
1217 |
[nsp_fun, nsp_dtco, nsp_class, nsp_classop, nsp_inst], [nsp_eval]] |
|
20926 | 1218 |
(fn "" => (fn p => (use_text Output.ml_output (!eval_verbose) (output p); NONE)) |
20896 | 1219 |
| _ => SOME) data |
1220 |
(hidden, SOME [NameSpace.pack [nsp_eval, val_name]]); |
|
20699 | 1221 |
val _ = serializer code'; |
20191 | 1222 |
val val_name_struct = NameSpace.append struct_name val_name; |
20600 | 1223 |
val _ = reff := NONE; |
20926 | 1224 |
val _ = use_text Output.ml_output (!eval_verbose) ("val _ = (" ^ ref_name ^ " := SOME (" ^ val_name_struct ^ "))"); |
20600 | 1225 |
in case !reff |
1226 |
of NONE => error ("Could not retrieve value of ML reference " ^ quote ref_name |
|
1227 |
^ " (reference probably has been shadowed)") |
|
1228 |
| SOME value => value |
|
1229 |
end; |
|
20191 | 1230 |
|
20699 | 1231 |
structure NameMangler = NameManglerFun ( |
1232 |
type ctxt = string list; |
|
1233 |
type src = string; |
|
1234 |
val ord = string_ord; |
|
1235 |
fun mk reserved_ml (name, i) = |
|
1236 |
(Symbol.alphanum o NameSpace.base) name ^ replicate_string i "'"; |
|
1237 |
fun is_valid (reserved_ml : string list) = not o member (op =) reserved_ml; |
|
1238 |
fun maybe_unique _ _ = NONE; |
|
1239 |
fun re_mangle _ dst = error ("No such definition name: " ^ quote dst); |
|
1240 |
); |
|
1241 |
||
19150 | 1242 |
fun mk_flat_ml_resolver names = |
1243 |
let |
|
1244 |
val mangler = |
|
1245 |
NameMangler.empty |
|
20699 | 1246 |
|> fold_map (NameMangler.declare (ThmDatabase.ml_reserved @ reserved_ml')) names |
19150 | 1247 |
|-> (fn _ => I) |
20699 | 1248 |
in NameMangler.get (ThmDatabase.ml_reserved @ reserved_ml') mangler end; |
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1249 |
|
18216 | 1250 |
end; (* local *) |
1251 |
||
20191 | 1252 |
|
1253 |
(** haskell serializer **) |
|
1254 |
||
20896 | 1255 |
fun hs_from_thingol target args = |
18282 | 1256 |
let |
20896 | 1257 |
fun hs_from_defs keyword_vars (class_syntax, tyco_syntax, const_syntax) deresolver prefix defs = |
18282 | 1258 |
let |
20896 | 1259 |
val deresolv = deresolver ""; |
1260 |
val deresolv_here = deresolver prefix; |
|
1261 |
val hs_from_def = pr_haskell class_syntax tyco_syntax const_syntax |
|
1262 |
keyword_vars deresolv_here deresolv; |
|
1263 |
in case map_filter hs_from_def defs |
|
1264 |
of [] => NONE |
|
1265 |
| ps => (SOME o Pretty.chunks o separate (str "")) ps |
|
18282 | 1266 |
end; |
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
1267 |
val reserved_hs = [ |
18702 | 1268 |
"hiding", "deriving", "where", "case", "of", "infix", "infixl", "infixr", |
1269 |
"import", "default", "forall", "let", "in", "class", "qualified", "data", |
|
1270 |
"newtype", "instance", "if", "then", "else", "type", "as", "do", "module" |
|
1271 |
] @ [ |
|
20401 | 1272 |
"Bool", "Integer", "Maybe", "True", "False", "Nothing", "Just", "not", "negate", |
1273 |
"String", "Char" |
|
18702 | 1274 |
]; |
19341
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
haftmann
parents:
19280
diff
changeset
|
1275 |
fun hs_from_module resolv imps ((_, name), ps) = |
19038 | 1276 |
(Pretty.chunks) ( |
1277 |
str ("module " ^ name ^ " where") |
|
1278 |
:: map (str o prefix "import qualified ") imps @ ( |
|
1279 |
str "" |
|
1280 |
:: separate (str "") ps |
|
1281 |
)); |
|
18919 | 1282 |
fun postproc (shallow, n) = |
1283 |
let |
|
1284 |
fun ch_first f = String.implode o nth_map 0 f o String.explode; |
|
20699 | 1285 |
in if member (op =) [nsp_module, nsp_class, nsp_tyco, nsp_dtco] shallow |
18919 | 1286 |
then ch_first Char.toUpper n |
1287 |
else ch_first Char.toLower n |
|
1288 |
end; |
|
20699 | 1289 |
val serializer = abstract_serializer (target, [[nsp_module], |
1290 |
[nsp_class], [nsp_tyco], [nsp_fun, nsp_classop], [nsp_dtco], [nsp_inst]]) |
|
20896 | 1291 |
"Main" (hs_from_defs (make_vars reserved_hs), hs_from_module, abstract_validator reserved_hs, postproc); |
18282 | 1292 |
in |
20896 | 1293 |
(parse_multi_file ((K o K) NONE) "hs" serializer) args |
18282 | 1294 |
end; |
1295 |
||
20896 | 1296 |
|
20699 | 1297 |
|
20896 | 1298 |
(** theory data **) |
1299 |
||
1300 |
datatype syntax_expr = SyntaxExpr of { |
|
1301 |
class: ((string * (string -> string option)) * serial) Symtab.table, |
|
1302 |
inst: unit Symtab.table, |
|
1303 |
tyco: (itype pretty_syntax * serial) Symtab.table, |
|
1304 |
const: (iterm pretty_syntax * serial) Symtab.table |
|
1305 |
}; |
|
18702 | 1306 |
|
20896 | 1307 |
fun mk_syntax_expr ((class, inst), (tyco, const)) = |
1308 |
SyntaxExpr { class = class, inst = inst, tyco = tyco, const = const }; |
|
1309 |
fun map_syntax_expr f (SyntaxExpr { class, inst, tyco, const }) = |
|
1310 |
mk_syntax_expr (f ((class, inst), (tyco, const))); |
|
1311 |
fun merge_syntax_expr (SyntaxExpr { class = class1, inst = inst1, tyco = tyco1, const = const1 }, |
|
1312 |
SyntaxExpr { class = class2, inst = inst2, tyco = tyco2, const = const2 }) = |
|
1313 |
mk_syntax_expr ( |
|
1314 |
(Symtab.merge (eq_snd (op =)) (class1, class2), |
|
1315 |
Symtab.merge (op =) (inst1, inst2)), |
|
1316 |
(Symtab.merge (eq_snd (op =)) (tyco1, tyco2), |
|
1317 |
Symtab.merge (eq_snd (op =)) (const1, const2)) |
|
1318 |
); |
|
1319 |
||
1320 |
datatype syntax_modl = SyntaxModl of { |
|
1321 |
merge: string Symtab.table, |
|
1322 |
prolog: Pretty.T Symtab.table |
|
1323 |
}; |
|
20699 | 1324 |
|
20896 | 1325 |
fun mk_syntax_modl (merge, prolog) = |
1326 |
SyntaxModl { merge = merge, prolog = prolog }; |
|
1327 |
fun map_syntax_modl f (SyntaxModl { merge, prolog }) = |
|
1328 |
mk_syntax_modl (f (merge, prolog)); |
|
1329 |
fun merge_syntax_modl (SyntaxModl { merge = merge1, prolog = prolog1 }, |
|
1330 |
SyntaxModl { merge = merge2, prolog = prolog2 }) = |
|
1331 |
mk_syntax_modl ( |
|
1332 |
Symtab.merge (op =) (merge1, merge2), |
|
1333 |
Symtab.merge (op =) (prolog1, prolog2) |
|
1334 |
); |
|
20699 | 1335 |
|
20896 | 1336 |
type serializer = string -> Args.T list |
1337 |
-> (string -> (string * (string -> string option)) option) |
|
1338 |
* (string |
|
1339 |
-> (int |
|
1340 |
* (fixity |
|
1341 |
-> (fixity |
|
1342 |
-> itype -> Pretty.T) |
|
1343 |
-> itype list -> Pretty.T)) |
|
1344 |
option) |
|
1345 |
* (string |
|
1346 |
-> (int |
|
1347 |
* (fixity |
|
1348 |
-> (fixity |
|
1349 |
-> iterm -> Pretty.T) |
|
1350 |
-> iterm list -> Pretty.T)) |
|
1351 |
option) |
|
1352 |
-> string list * string list option |
|
1353 |
-> CodegenThingol.code -> unit; |
|
1354 |
||
1355 |
datatype target = Target of { |
|
1356 |
serial: serial, |
|
1357 |
serializer: serializer, |
|
1358 |
syntax_expr: syntax_expr, |
|
1359 |
syntax_modl: syntax_modl |
|
1360 |
}; |
|
20699 | 1361 |
|
20896 | 1362 |
fun mk_target (serial, (serializer, (syntax_expr, syntax_modl))) = |
1363 |
Target { serial = serial, serializer = serializer, syntax_expr = syntax_expr, syntax_modl = syntax_modl }; |
|
1364 |
fun map_target f ( Target { serial, serializer, syntax_expr, syntax_modl } ) = |
|
1365 |
mk_target (f (serial, (serializer, (syntax_expr, syntax_modl)))); |
|
1366 |
fun merge_target target (Target { serial = serial1, serializer = serializer, syntax_expr = syntax_expr1, syntax_modl = syntax_modl1 }, |
|
1367 |
Target { serial = serial2, serializer = _, syntax_expr = syntax_expr2, syntax_modl = syntax_modl2 }) = |
|
1368 |
if serial1 = serial2 then |
|
1369 |
mk_target (serial1, (serializer, |
|
1370 |
(merge_syntax_expr (syntax_expr1, syntax_expr2), |
|
1371 |
merge_syntax_modl (syntax_modl1, syntax_modl2)) |
|
1372 |
)) |
|
1373 |
else |
|
1374 |
error ("Incompatible serializers: " ^ quote target); |
|
1375 |
||
1376 |
structure CodegenSerializerData = TheoryDataFun |
|
1377 |
(struct |
|
1378 |
val name = "Pure/codegen_serializer"; |
|
1379 |
type T = target Symtab.table; |
|
1380 |
val empty = Symtab.empty; |
|
1381 |
val copy = I; |
|
1382 |
val extend = I; |
|
1383 |
fun merge _ = Symtab.join merge_target; |
|
1384 |
fun print _ _ = (); |
|
1385 |
end); |
|
1386 |
||
1387 |
fun the_serializer (Target { serializer, ... }) = serializer; |
|
1388 |
fun the_syntax_expr (Target { syntax_expr = SyntaxExpr x, ... }) = x; |
|
1389 |
fun the_syntax_modl (Target { syntax_modl = SyntaxModl x, ... }) = x; |
|
1390 |
||
1391 |
fun add_serializer (target, seri) thy = |
|
18702 | 1392 |
let |
20896 | 1393 |
val _ = case Symtab.lookup (CodegenSerializerData.get thy) target |
1394 |
of SOME _ => warning ("overwriting existing serializer " ^ quote target) |
|
1395 |
| NONE => (); |
|
20699 | 1396 |
in |
20896 | 1397 |
thy |
1398 |
|> (CodegenSerializerData.map oo Symtab.map_default) |
|
1399 |
(target, mk_target (serial (), (seri, |
|
1400 |
(mk_syntax_expr ((Symtab.empty, Symtab.empty), (Symtab.empty, Symtab.empty)), |
|
1401 |
mk_syntax_modl (Symtab.empty, Symtab.empty))))) |
|
1402 |
(map_target (fn (serial, (_, syntax)) => (serial, (seri, syntax)))) |
|
20699 | 1403 |
end; |
1404 |
||
20896 | 1405 |
val _ = Context.add_setup ( |
1406 |
CodegenSerializerData.init |
|
1407 |
#> add_serializer ("SML", ml_from_thingol) |
|
1408 |
#> add_serializer ("Haskell", hs_from_thingol) |
|
1409 |
); |
|
1410 |
||
1411 |
fun get_serializer thy (target, args) cs = |
|
1412 |
let |
|
1413 |
val data = case Symtab.lookup (CodegenSerializerData.get thy) target |
|
1414 |
of SOME data => data |
|
1415 |
| NONE => error ("Unknown code target language: " ^ quote target); |
|
1416 |
val seri = the_serializer data; |
|
1417 |
val { class, inst, tyco, const } = the_syntax_expr data; |
|
1418 |
fun fun_of sys = (Option.map fst oo Symtab.lookup) sys; |
|
1419 |
in |
|
1420 |
seri target args (fun_of class, fun_of tyco, fun_of const) |
|
1421 |
(Symtab.keys class @ Symtab.keys inst @ Symtab.keys tyco @ Symtab.keys const, cs) |
|
1422 |
end; |
|
1423 |
||
1424 |
fun has_serialization f thy targets name = |
|
1425 |
forall ( |
|
1426 |
is_some o (fn tab => Symtab.lookup tab name) o f o the_syntax_expr o the |
|
1427 |
o (Symtab.lookup ((CodegenSerializerData.get) thy)) |
|
1428 |
) targets; |
|
1429 |
||
1430 |
val tyco_has_serialization = has_serialization #tyco; |
|
1431 |
val const_has_serialization = has_serialization #const; |
|
1432 |
||
1433 |
fun eval_term thy = |
|
1434 |
let |
|
1435 |
val target = "SML"; |
|
1436 |
val data = case Symtab.lookup (CodegenSerializerData.get thy) target |
|
1437 |
of SOME data => data |
|
1438 |
| NONE => error ("Unknown code target language: " ^ quote target); |
|
1439 |
val { class, inst, tyco, const } = the_syntax_expr data; |
|
1440 |
fun fun_of sys = (Option.map fst oo Symtab.lookup) sys; |
|
1441 |
in |
|
1442 |
eval_term_proto thy (fun_of class, fun_of tyco, fun_of const) |
|
1443 |
(Symtab.keys class @ Symtab.keys inst @ Symtab.keys tyco @ Symtab.keys const) |
|
1444 |
end; |
|
1445 |
||
1446 |
||
20699 | 1447 |
|
20931 | 1448 |
(** ML and toplevel interface **) |
20699 | 1449 |
|
1450 |
local |
|
1451 |
||
20896 | 1452 |
fun map_syntax_exprs target f thy = |
1453 |
let |
|
1454 |
val _ = if is_some (Symtab.lookup (CodegenSerializerData.get thy) target) |
|
1455 |
then () |
|
1456 |
else error ("Unknown code target language: " ^ quote target); |
|
1457 |
in |
|
1458 |
thy |
|
1459 |
|> (CodegenSerializerData.map o Symtab.map_entry target o map_target |
|
1460 |
o apsnd o apsnd o apfst o map_syntax_expr) f |
|
1461 |
end; |
|
1462 |
||
20699 | 1463 |
fun gen_add_syntax_class prep_class prep_const target raw_class (syntax, raw_ops) thy = |
1464 |
let |
|
1465 |
val cls = prep_class thy raw_class |
|
1466 |
val class = CodegenNames.class thy cls; |
|
20896 | 1467 |
fun mk_classop (const as (c, _)) = case AxClass.class_of_param thy c |
1468 |
of SOME class' => if cls = class' then CodegenNames.const thy const |
|
20699 | 1469 |
else error ("Not a class operation for class " ^ quote class ^ ": " ^ quote c) |
1470 |
| NONE => error ("Not a class operation: " ^ quote c) |
|
1471 |
val ops = (map o apfst) (mk_classop o prep_const thy) raw_ops; |
|
1472 |
val syntax_ops = AList.lookup (op =) ops; |
|
1473 |
in |
|
1474 |
thy |
|
20896 | 1475 |
|> (map_syntax_exprs target o apfst o apfst) |
1476 |
(Symtab.update (class, ((syntax, syntax_ops), serial ()))) |
|
20699 | 1477 |
end; |
1478 |
||
1479 |
fun gen_add_syntax_inst prep_class prep_tyco target (raw_tyco, raw_class) thy = |
|
1480 |
let |
|
1481 |
val inst = CodegenNames.instance thy (prep_class thy raw_class, prep_tyco thy raw_tyco); |
|
1482 |
in |
|
1483 |
thy |
|
20896 | 1484 |
|> (map_syntax_exprs target o apfst o apsnd) |
1485 |
(Symtab.update (inst, ())) |
|
20699 | 1486 |
end; |
1487 |
||
1488 |
fun gen_add_syntax_tyco prep_tyco raw_tyco target syntax thy = |
|
1489 |
let |
|
1490 |
val tyco = (CodegenNames.tyco thy o prep_tyco thy) raw_tyco; |
|
1491 |
in |
|
1492 |
thy |
|
20896 | 1493 |
|> (map_syntax_exprs target o apsnd o apfst) |
1494 |
(Symtab.update (tyco, (syntax, serial ()))) |
|
20699 | 1495 |
end; |
1496 |
||
1497 |
fun gen_add_syntax_const prep_const raw_c target syntax thy = |
|
1498 |
let |
|
1499 |
val c' = prep_const thy raw_c; |
|
1500 |
val c'' = CodegenNames.const thy c'; |
|
1501 |
in |
|
1502 |
thy |
|
20896 | 1503 |
|> (map_syntax_exprs target o apsnd o apsnd) |
1504 |
(Symtab.update (c'', (syntax, serial ()))) |
|
20699 | 1505 |
end; |
1506 |
||
1507 |
fun read_type thy raw_tyco = |
|
1508 |
let |
|
1509 |
val tyco = Sign.intern_type thy raw_tyco; |
|
1510 |
val _ = if Sign.declared_tyname thy tyco then () |
|
1511 |
else error ("No such type constructor: " ^ quote raw_tyco); |
|
1512 |
in tyco end; |
|
1513 |
||
1514 |
fun idfs_of_const_names thy cs = |
|
1515 |
let |
|
1516 |
val cs' = AList.make (fn c => Sign.the_const_type thy c) cs; |
|
1517 |
val cs'' = map (CodegenConsts.norm_of_typ thy) cs'; |
|
1518 |
in AList.make (CodegenNames.const thy) cs'' end; |
|
1519 |
||
20931 | 1520 |
fun parse_quote num_of consts_of target get_init adder = |
1521 |
parse_syntax num_of #-> (fn mfx => pair (fn thy => adder target (mfx thy) thy)); |
|
1522 |
||
1523 |
fun zip_list (x::xs) f g = |
|
1524 |
f x #-> (fn y => fold_map (fn x => g |-- f x >> pair x) xs |
|
1525 |
#-> (fn xys => pair ((x, y) :: xys))); |
|
20699 | 1526 |
|
20931 | 1527 |
structure P = OuterParse |
1528 |
and K = OuterKeyword |
|
20699 | 1529 |
|
20931 | 1530 |
fun parse_multi_syntax parse_thing parse_syntax = |
1531 |
P.and_list1 parse_thing |
|
1532 |
#-> (fn things => Scan.repeat1 (P.$$$ "(" |-- P.name :-- |
|
1533 |
(fn target => zip_list things (parse_syntax target) |
|
1534 |
(P.$$$ "and")) --| P.$$$ ")")) |
|
20699 | 1535 |
|
1536 |
val add_syntax_class = gen_add_syntax_class ClassPackage.read_class CodegenConsts.read_const; |
|
1537 |
val add_syntax_inst = gen_add_syntax_inst ClassPackage.read_class read_type; |
|
1538 |
||
20931 | 1539 |
fun parse_syntax_tyco target raw_tyco = |
20699 | 1540 |
let |
1541 |
fun intern thy = read_type thy raw_tyco; |
|
1542 |
fun num_of thy = Sign.arity_number thy (intern thy); |
|
1543 |
fun idf_of thy = CodegenNames.tyco thy (intern thy); |
|
1544 |
fun read_typ thy = |
|
1545 |
Sign.read_typ (thy, K NONE); |
|
1546 |
in |
|
20931 | 1547 |
parse_quote num_of ((K o K) ([], [])) target idf_of |
20699 | 1548 |
(gen_add_syntax_tyco read_type raw_tyco) |
1549 |
end; |
|
1550 |
||
20931 | 1551 |
fun parse_syntax_const target raw_const = |
20699 | 1552 |
let |
1553 |
fun intern thy = CodegenConsts.read_const thy raw_const; |
|
1554 |
fun num_of thy = (length o fst o strip_type o Sign.the_const_type thy o fst o intern) thy; |
|
1555 |
fun idf_of thy = (CodegenNames.const thy o intern) thy; |
|
1556 |
in |
|
20931 | 1557 |
parse_quote num_of CodegenConsts.consts_of target idf_of |
20699 | 1558 |
(gen_add_syntax_const CodegenConsts.read_const raw_const) |
1559 |
end; |
|
1560 |
||
20931 | 1561 |
val (code_classK, code_instanceK, code_typeK, code_constK) = |
1562 |
("code_class", "code_instance", "code_type", "code_const"); |
|
1563 |
||
1564 |
in |
|
1565 |
||
1566 |
val code_classP = |
|
1567 |
OuterSyntax.command code_classK "define code syntax for class" K.thy_decl ( |
|
1568 |
parse_multi_syntax P.xname |
|
1569 |
(fn _ => fn _ => P.string -- Scan.optional (P.$$$ "where" |-- Scan.repeat1 |
|
1570 |
(P.term --| (P.$$$ "\\<equiv>" || P.$$$ "==") -- P.string)) []) |
|
1571 |
>> (Toplevel.theory oo fold) (fn (target, syns) => |
|
1572 |
fold (fn (raw_class, syn) => add_syntax_class target raw_class syn) syns) |
|
1573 |
); |
|
1574 |
||
1575 |
val code_instanceP = |
|
1576 |
OuterSyntax.command code_instanceK "define code syntax for instance" K.thy_decl ( |
|
1577 |
parse_multi_syntax (P.xname --| P.$$$ "::" -- P.xname) |
|
1578 |
(fn _ => fn _ => P.name #-> |
|
1579 |
(fn "-" => Scan.succeed () | _ => Scan.fail_with (fn _ => "\"-\" expected") ())) |
|
1580 |
>> (Toplevel.theory oo fold) (fn (target, syns) => |
|
1581 |
fold (fn (raw_inst, ()) => add_syntax_inst target raw_inst) syns) |
|
1582 |
); |
|
1583 |
||
1584 |
val code_typeP = |
|
1585 |
OuterSyntax.command code_typeK "define code syntax for type constructor" K.thy_decl ( |
|
1586 |
Scan.repeat1 ( |
|
1587 |
parse_multi_syntax P.xname parse_syntax_tyco |
|
1588 |
) |
|
1589 |
>> (Toplevel.theory o (fold o fold) (fold snd o snd)) |
|
1590 |
); |
|
1591 |
||
1592 |
val code_constP = |
|
1593 |
OuterSyntax.command code_constK "define code syntax for constant" K.thy_decl ( |
|
1594 |
Scan.repeat1 ( |
|
1595 |
parse_multi_syntax P.term parse_syntax_const |
|
1596 |
) |
|
1597 |
>> (Toplevel.theory o (fold o fold) (fold snd o snd)) |
|
1598 |
); |
|
1599 |
||
1600 |
val _ = OuterSyntax.add_parsers [code_classP, code_instanceP, code_typeP, code_constP]; |
|
1601 |
||
20699 | 1602 |
fun add_pretty_list target nill cons mk_list mk_char_string target_cons thy = |
1603 |
let |
|
1604 |
val [(_, nil''), (cons', cons'')] = idfs_of_const_names thy [nill, cons]; |
|
1605 |
val pr = pretty_list nil'' cons'' mk_list mk_char_string target_cons; |
|
1606 |
in |
|
1607 |
thy |
|
1608 |
|> gen_add_syntax_const (K I) cons' target pr |
|
1609 |
end; |
|
1610 |
||
1611 |
fun add_pretty_ml_string target nill cons str mk_char mk_string target_implode thy = |
|
1612 |
let |
|
1613 |
val [(_, nil''), (_, cons''), (str', _)] = idfs_of_const_names thy [nill, cons, str]; |
|
1614 |
val pr = pretty_ml_string nil'' cons'' mk_char mk_string target_implode; |
|
1615 |
in |
|
1616 |
thy |
|
1617 |
|> gen_add_syntax_const (K I) str' target pr |
|
1618 |
end; |
|
1619 |
||
1620 |
fun add_undefined target undef target_undefined thy = |
|
1621 |
let |
|
1622 |
val [(undef', _)] = idfs_of_const_names thy [undef]; |
|
1623 |
fun pretty _ _ _ = str target_undefined; |
|
1624 |
in |
|
1625 |
thy |
|
1626 |
|> gen_add_syntax_const (K I) undef' target (~1, pretty) |
|
1627 |
end; |
|
1628 |
||
1629 |
end; (*local*) |
|
18702 | 1630 |
|
18216 | 1631 |
end; (* struct *) |