author | haftmann |
Mon, 07 Nov 2005 09:34:51 +0100 | |
changeset 18098 | 227ecb2cfa3d |
parent 17666 | 4708ab4626a5 |
child 18102 | 60220e935287 |
permissions | -rw-r--r-- |
11520 | 1 |
(* Title: Pure/codegen.ML |
2 |
ID: $Id$ |
|
11539 | 3 |
Author: Stefan Berghofer, TU Muenchen |
11520 | 4 |
|
11539 | 5 |
Generic code generator. |
11520 | 6 |
*) |
7 |
||
8 |
signature CODEGEN = |
|
9 |
sig |
|
10 |
val quiet_mode : bool ref |
|
11 |
val message : string -> unit |
|
13753
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
12 |
val mode : string list ref |
13886
0b243f6e257e
Margin for pretty-printing is now a mutable reference.
berghofe
parents:
13753
diff
changeset
|
13 |
val margin : int ref |
11520 | 14 |
|
12452 | 15 |
datatype 'a mixfix = |
11520 | 16 |
Arg |
17 |
| Ignore |
|
16769
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
18 |
| Module |
11520 | 19 |
| Pretty of Pretty.T |
12452 | 20 |
| Quote of 'a; |
11520 | 21 |
|
16649 | 22 |
type deftab |
17144 | 23 |
type node |
16649 | 24 |
type codegr |
12452 | 25 |
type 'a codegen |
26 |
||
27 |
val add_codegen: string -> term codegen -> theory -> theory |
|
28 |
val add_tycodegen: string -> typ codegen -> theory -> theory |
|
14197 | 29 |
val add_attribute: string -> (Args.T list -> theory attribute * Args.T list) -> theory -> theory |
15261 | 30 |
val add_preprocessor: (theory -> thm list -> thm list) -> theory -> theory |
31 |
val preprocess: theory -> thm list -> thm list |
|
17549 | 32 |
val preprocess_term: theory -> term -> term |
11520 | 33 |
val print_codegens: theory -> unit |
17144 | 34 |
val generate_code: theory -> string list -> string -> (string * string) list -> |
35 |
(string * string) list * codegr |
|
36 |
val generate_code_i: theory -> string list -> string -> (string * term) list -> |
|
37 |
(string * string) list * codegr |
|
16769
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
38 |
val assoc_consts: (xstring * string option * (term mixfix list * |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
39 |
(string * string) list)) list -> theory -> theory |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
40 |
val assoc_consts_i: (xstring * typ option * (term mixfix list * |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
41 |
(string * string) list)) list -> theory -> theory |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
42 |
val assoc_types: (xstring * (typ mixfix list * |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
43 |
(string * string) list)) list -> theory -> theory |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
44 |
val get_assoc_code: theory -> string -> typ -> |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
45 |
(term mixfix list * (string * string) list) option |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
46 |
val get_assoc_type: theory -> string -> |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
47 |
(typ mixfix list * (string * string) list) option |
17144 | 48 |
val codegen_error: codegr -> string -> string -> 'a |
16649 | 49 |
val invoke_codegen: theory -> deftab -> string -> string -> bool -> |
50 |
codegr * term -> codegr * Pretty.T |
|
51 |
val invoke_tycodegen: theory -> deftab -> string -> string -> bool -> |
|
52 |
codegr * typ -> codegr * Pretty.T |
|
14858 | 53 |
val mk_id: string -> string |
17144 | 54 |
val mk_qual_id: string -> string * string -> string |
55 |
val mk_const_id: string -> string -> codegr -> codegr * (string * string) |
|
56 |
val get_const_id: string -> codegr -> string * string |
|
57 |
val mk_type_id: string -> string -> codegr -> codegr * (string * string) |
|
58 |
val get_type_id: string -> codegr -> string * string |
|
16649 | 59 |
val thyname_of_type: string -> theory -> string |
60 |
val thyname_of_const: string -> theory -> string |
|
61 |
val rename_terms: term list -> term list |
|
11520 | 62 |
val rename_term: term -> term |
15398 | 63 |
val new_names: term -> string list -> string list |
64 |
val new_name: term -> string -> string |
|
17144 | 65 |
val if_library: 'a -> 'a -> 'a |
16649 | 66 |
val get_defn: theory -> deftab -> string -> typ -> |
67 |
((typ * (string * (term list * term))) * int option) option |
|
11520 | 68 |
val is_instance: theory -> typ -> typ -> bool |
69 |
val parens: Pretty.T -> Pretty.T |
|
70 |
val mk_app: bool -> Pretty.T -> Pretty.T list -> Pretty.T |
|
71 |
val eta_expand: term -> term list -> int -> term |
|
14105 | 72 |
val strip_tname: string -> string |
13753
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
73 |
val mk_type: bool -> typ -> Pretty.T |
17144 | 74 |
val mk_term_of: codegr -> string -> bool -> typ -> Pretty.T |
75 |
val mk_gen: codegr -> string -> bool -> string list -> string -> typ -> Pretty.T |
|
14105 | 76 |
val test_fn: (int -> (string * term) list option) ref |
77 |
val test_term: theory -> int -> int -> term -> (string * term) list option |
|
17549 | 78 |
val eval_result: term ref |
79 |
val eval_term: theory -> term -> term |
|
12452 | 80 |
val parse_mixfix: (string -> 'a) -> string -> 'a mixfix list |
18098 | 81 |
val fillin_mixfix': 'a mixfix list -> Pretty.T list -> string list * Pretty.T |
16649 | 82 |
val mk_deftab: theory -> deftab |
17549 | 83 |
|
17144 | 84 |
val get_node: codegr -> string -> node |
85 |
val add_edge: string * string -> codegr -> codegr |
|
86 |
val add_edge_acyclic: string * string -> codegr -> codegr |
|
87 |
val del_nodes: string list -> codegr -> codegr |
|
88 |
val map_node: string -> (node -> node) -> codegr -> codegr |
|
89 |
val new_node: string * node -> codegr -> codegr |
|
11520 | 90 |
end; |
91 |
||
92 |
structure Codegen : CODEGEN = |
|
93 |
struct |
|
94 |
||
95 |
val quiet_mode = ref true; |
|
96 |
fun message s = if !quiet_mode then () else writeln s; |
|
97 |
||
13753
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
98 |
val mode = ref ([] : string list); |
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
99 |
|
13886
0b243f6e257e
Margin for pretty-printing is now a mutable reference.
berghofe
parents:
13753
diff
changeset
|
100 |
val margin = ref 80; |
0b243f6e257e
Margin for pretty-printing is now a mutable reference.
berghofe
parents:
13753
diff
changeset
|
101 |
|
11520 | 102 |
(**** Mixfix syntax ****) |
103 |
||
12452 | 104 |
datatype 'a mixfix = |
11520 | 105 |
Arg |
106 |
| Ignore |
|
16769
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
107 |
| Module |
11520 | 108 |
| Pretty of Pretty.T |
12452 | 109 |
| Quote of 'a; |
11520 | 110 |
|
111 |
fun is_arg Arg = true |
|
112 |
| is_arg Ignore = true |
|
113 |
| is_arg _ = false; |
|
114 |
||
12452 | 115 |
fun quotes_of [] = [] |
116 |
| quotes_of (Quote q :: ms) = q :: quotes_of ms |
|
117 |
| quotes_of (_ :: ms) = quotes_of ms; |
|
118 |
||
119 |
fun args_of [] xs = ([], xs) |
|
120 |
| args_of (Arg :: ms) (x :: xs) = apfst (cons x) (args_of ms xs) |
|
121 |
| args_of (Ignore :: ms) (_ :: xs) = args_of ms xs |
|
122 |
| args_of (_ :: ms) xs = args_of ms xs; |
|
11520 | 123 |
|
15570 | 124 |
fun num_args x = length (List.filter is_arg x); |
11520 | 125 |
|
126 |
||
127 |
(**** theory data ****) |
|
128 |
||
16649 | 129 |
(* preprocessed definition table *) |
130 |
||
131 |
type deftab = |
|
132 |
(typ * (* type of constant *) |
|
133 |
(string * (* name of theory containing definition of constant *) |
|
134 |
(term list * (* parameters *) |
|
135 |
term))) (* right-hand side *) |
|
136 |
list Symtab.table; |
|
137 |
||
138 |
(* code dependency graph *) |
|
139 |
||
17144 | 140 |
type nametab = (string * string) Symtab.table * unit Symtab.table; |
141 |
||
142 |
fun merge_nametabs ((tab, used), (tab', used')) = |
|
143 |
(Symtab.merge op = (tab, tab'), Symtab.merge op = (used, used')); |
|
144 |
||
145 |
type node = |
|
16649 | 146 |
(exn option * (* slot for arbitrary data *) |
147 |
string * (* name of structure containing piece of code *) |
|
17144 | 148 |
string); (* piece of code *) |
149 |
||
150 |
type codegr = |
|
151 |
node Graph.T * |
|
152 |
(nametab * (* table for assigned constant names *) |
|
153 |
nametab); (* table for assigned type names *) |
|
154 |
||
155 |
val emptygr : codegr = (Graph.empty, |
|
156 |
((Symtab.empty, Symtab.empty), (Symtab.empty, Symtab.empty))); |
|
16649 | 157 |
|
14105 | 158 |
(* type of code generators *) |
11520 | 159 |
|
16649 | 160 |
type 'a codegen = |
161 |
theory -> (* theory in which generate_code was called *) |
|
162 |
deftab -> (* definition table (for efficiency) *) |
|
163 |
codegr -> (* code dependency graph *) |
|
164 |
string -> (* node name of caller (for recording dependencies) *) |
|
17144 | 165 |
string -> (* module name of caller (for modular code generation) *) |
16649 | 166 |
bool -> (* whether to parenthesize generated expression *) |
167 |
'a -> (* item to generate code from *) |
|
168 |
(codegr * Pretty.T) option; |
|
12452 | 169 |
|
14105 | 170 |
(* parameters for random testing *) |
171 |
||
172 |
type test_params = |
|
173 |
{size: int, iterations: int, default_type: typ option}; |
|
174 |
||
175 |
fun merge_test_params |
|
176 |
{size = size1, iterations = iterations1, default_type = default_type1} |
|
177 |
{size = size2, iterations = iterations2, default_type = default_type2} = |
|
178 |
{size = Int.max (size1, size2), |
|
179 |
iterations = Int.max (iterations1, iterations2), |
|
180 |
default_type = case default_type1 of |
|
15531 | 181 |
NONE => default_type2 |
14105 | 182 |
| _ => default_type1}; |
183 |
||
184 |
val default_test_params : test_params = |
|
15531 | 185 |
{size = 10, iterations = 100, default_type = NONE}; |
14105 | 186 |
|
187 |
fun set_size size ({iterations, default_type, ...} : test_params) = |
|
188 |
{size = size, iterations = iterations, default_type = default_type}; |
|
189 |
||
190 |
fun set_iterations iterations ({size, default_type, ...} : test_params) = |
|
191 |
{size = size, iterations = iterations, default_type = default_type}; |
|
192 |
||
16458 | 193 |
fun set_default_type s thy ({size, iterations, ...} : test_params) = |
14105 | 194 |
{size = size, iterations = iterations, |
16458 | 195 |
default_type = SOME (typ_of (read_ctyp thy s))}; |
14105 | 196 |
|
197 |
(* data kind 'Pure/codegen' *) |
|
16458 | 198 |
|
199 |
structure CodegenData = TheoryDataFun |
|
200 |
(struct |
|
11520 | 201 |
val name = "Pure/codegen"; |
202 |
type T = |
|
12452 | 203 |
{codegens : (string * term codegen) list, |
204 |
tycodegens : (string * typ codegen) list, |
|
16769
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
205 |
consts : ((string * typ) * (term mixfix list * (string * string) list)) list, |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
206 |
types : (string * (typ mixfix list * (string * string) list)) list, |
14197 | 207 |
attrs: (string * (Args.T list -> theory attribute * Args.T list)) list, |
15261 | 208 |
preprocs: (stamp * (theory -> thm list -> thm list)) list, |
17144 | 209 |
modules: codegr Symtab.table, |
14105 | 210 |
test_params: test_params}; |
11520 | 211 |
|
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
212 |
val empty = |
14105 | 213 |
{codegens = [], tycodegens = [], consts = [], types = [], attrs = [], |
17144 | 214 |
preprocs = [], modules = Symtab.empty, test_params = default_test_params}; |
11520 | 215 |
val copy = I; |
16458 | 216 |
val extend = I; |
11520 | 217 |
|
16458 | 218 |
fun merge _ |
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
219 |
({codegens = codegens1, tycodegens = tycodegens1, |
14105 | 220 |
consts = consts1, types = types1, attrs = attrs1, |
17144 | 221 |
preprocs = preprocs1, modules = modules1, test_params = test_params1}, |
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
222 |
{codegens = codegens2, tycodegens = tycodegens2, |
14105 | 223 |
consts = consts2, types = types2, attrs = attrs2, |
17144 | 224 |
preprocs = preprocs2, modules = modules2, test_params = test_params2}) = |
15261 | 225 |
{codegens = merge_alists' codegens1 codegens2, |
226 |
tycodegens = merge_alists' tycodegens1 tycodegens2, |
|
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
227 |
consts = merge_alists consts1 consts2, |
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
228 |
types = merge_alists types1 types2, |
14105 | 229 |
attrs = merge_alists attrs1 attrs2, |
15261 | 230 |
preprocs = merge_alists' preprocs1 preprocs2, |
17144 | 231 |
modules = Symtab.merge (K true) (modules1, modules2), |
14105 | 232 |
test_params = merge_test_params test_params1 test_params2}; |
11520 | 233 |
|
16458 | 234 |
fun print _ ({codegens, tycodegens, ...} : T) = |
12452 | 235 |
Pretty.writeln (Pretty.chunks |
236 |
[Pretty.strs ("term code generators:" :: map fst codegens), |
|
237 |
Pretty.strs ("type code generators:" :: map fst tycodegens)]); |
|
16458 | 238 |
end); |
11520 | 239 |
|
15801 | 240 |
val _ = Context.add_setup [CodegenData.init]; |
11520 | 241 |
val print_codegens = CodegenData.print; |
242 |
||
243 |
||
14105 | 244 |
(**** access parameters for random testing ****) |
245 |
||
246 |
fun get_test_params thy = #test_params (CodegenData.get thy); |
|
247 |
||
248 |
fun map_test_params f thy = |
|
17144 | 249 |
let val {codegens, tycodegens, consts, types, attrs, preprocs, modules, test_params} = |
14105 | 250 |
CodegenData.get thy; |
251 |
in CodegenData.put {codegens = codegens, tycodegens = tycodegens, |
|
15261 | 252 |
consts = consts, types = types, attrs = attrs, preprocs = preprocs, |
17144 | 253 |
modules = modules, test_params = f test_params} thy |
254 |
end; |
|
255 |
||
256 |
||
257 |
(**** access modules ****) |
|
258 |
||
259 |
fun get_modules thy = #modules (CodegenData.get thy); |
|
260 |
||
261 |
fun map_modules f thy = |
|
262 |
let val {codegens, tycodegens, consts, types, attrs, preprocs, modules, test_params} = |
|
263 |
CodegenData.get thy; |
|
264 |
in CodegenData.put {codegens = codegens, tycodegens = tycodegens, |
|
265 |
consts = consts, types = types, attrs = attrs, preprocs = preprocs, |
|
266 |
modules = f modules, test_params = test_params} thy |
|
14105 | 267 |
end; |
268 |
||
269 |
||
12452 | 270 |
(**** add new code generators to theory ****) |
11520 | 271 |
|
272 |
fun add_codegen name f thy = |
|
17144 | 273 |
let val {codegens, tycodegens, consts, types, attrs, preprocs, modules, test_params} = |
14105 | 274 |
CodegenData.get thy |
17521 | 275 |
in (case AList.lookup (op =) codegens name of |
15531 | 276 |
NONE => CodegenData.put {codegens = (name, f) :: codegens, |
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
277 |
tycodegens = tycodegens, consts = consts, types = types, |
17144 | 278 |
attrs = attrs, preprocs = preprocs, modules = modules, |
279 |
test_params = test_params} thy |
|
15531 | 280 |
| SOME _ => error ("Code generator " ^ name ^ " already declared")) |
12452 | 281 |
end; |
282 |
||
283 |
fun add_tycodegen name f thy = |
|
17144 | 284 |
let val {codegens, tycodegens, consts, types, attrs, preprocs, modules, test_params} = |
14105 | 285 |
CodegenData.get thy |
17521 | 286 |
in (case AList.lookup (op =) tycodegens name of |
15531 | 287 |
NONE => CodegenData.put {tycodegens = (name, f) :: tycodegens, |
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
288 |
codegens = codegens, consts = consts, types = types, |
17144 | 289 |
attrs = attrs, preprocs = preprocs, modules = modules, |
290 |
test_params = test_params} thy |
|
15531 | 291 |
| SOME _ => error ("Code generator " ^ name ^ " already declared")) |
11520 | 292 |
end; |
293 |
||
294 |
||
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
295 |
(**** code attribute ****) |
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
296 |
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
297 |
fun add_attribute name att thy = |
17144 | 298 |
let val {codegens, tycodegens, consts, types, attrs, preprocs, modules, test_params} = |
14105 | 299 |
CodegenData.get thy |
17521 | 300 |
in (case AList.lookup (op =) attrs name of |
15531 | 301 |
NONE => CodegenData.put {tycodegens = tycodegens, |
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
302 |
codegens = codegens, consts = consts, types = types, |
15261 | 303 |
attrs = if name = "" then attrs @ [(name, att)] else (name, att) :: attrs, |
17144 | 304 |
preprocs = preprocs, modules = modules, |
15261 | 305 |
test_params = test_params} thy |
15531 | 306 |
| SOME _ => error ("Code attribute " ^ name ^ " already declared")) |
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
307 |
end; |
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
308 |
|
14197 | 309 |
fun mk_parser (a, p) = (if a = "" then Scan.succeed "" else Args.$$$ a) |-- p; |
310 |
||
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
311 |
val code_attr = |
15664 | 312 |
Attrib.syntax (Scan.peek (fn thy => foldr op || Scan.fail (map mk_parser |
313 |
(#attrs (CodegenData.get thy))))); |
|
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
314 |
|
15801 | 315 |
val _ = Context.add_setup |
316 |
[Attrib.add_attributes |
|
317 |
[("code", (code_attr, K Attrib.undef_local_attribute), |
|
318 |
"declare theorems for code generation")]]; |
|
319 |
||
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
320 |
|
15261 | 321 |
(**** preprocessors ****) |
322 |
||
323 |
fun add_preprocessor p thy = |
|
17144 | 324 |
let val {codegens, tycodegens, consts, types, attrs, preprocs, modules, test_params} = |
15261 | 325 |
CodegenData.get thy |
326 |
in CodegenData.put {tycodegens = tycodegens, |
|
327 |
codegens = codegens, consts = consts, types = types, |
|
328 |
attrs = attrs, preprocs = (stamp (), p) :: preprocs, |
|
17144 | 329 |
modules = modules, test_params = test_params} thy |
15261 | 330 |
end; |
331 |
||
332 |
fun preprocess thy ths = |
|
333 |
let val {preprocs, ...} = CodegenData.get thy |
|
15570 | 334 |
in Library.foldl (fn (ths, (_, f)) => f thy ths) (ths, preprocs) end; |
15261 | 335 |
|
17549 | 336 |
fun preprocess_term thy t = |
337 |
let |
|
338 |
val x = Free (variant (add_term_names (t, [])) "x", fastype_of t); |
|
339 |
(* fake definition *) |
|
340 |
val eq = setmp quick_and_dirty true (SkipProof.make_thm thy) |
|
341 |
(Logic.mk_equals (x, t)); |
|
342 |
fun err () = error "preprocess_term: bad preprocessor" |
|
343 |
in case map prop_of (preprocess thy [eq]) of |
|
344 |
[Const ("==", _) $ x' $ t'] => if x = x' then t' else err () |
|
345 |
| _ => err () |
|
346 |
end; |
|
347 |
||
15261 | 348 |
fun unfold_attr (thy, eqn) = |
349 |
let |
|
17666 | 350 |
val names = term_consts |
351 |
(fst (Logic.dest_equals (prop_of eqn))); |
|
15261 | 352 |
fun prep thy = map (fn th => |
17666 | 353 |
let val prop = prop_of th |
354 |
in |
|
355 |
if forall (fn name => exists_Const (equal name o fst) prop) names |
|
356 |
then rewrite_rule [eqn] (Thm.transfer thy th) |
|
357 |
else th |
|
358 |
end) |
|
15261 | 359 |
in (add_preprocessor prep thy, eqn) end; |
360 |
||
15801 | 361 |
val _ = Context.add_setup [add_attribute "unfold" (Scan.succeed unfold_attr)]; |
362 |
||
15261 | 363 |
|
11520 | 364 |
(**** associate constants with target language code ****) |
365 |
||
15570 | 366 |
fun gen_assoc_consts prep_type xs thy = Library.foldl (fn (thy, (s, tyopt, syn)) => |
11520 | 367 |
let |
17144 | 368 |
val {codegens, tycodegens, consts, types, attrs, preprocs, modules, test_params} = |
14105 | 369 |
CodegenData.get thy; |
16458 | 370 |
val cname = Sign.intern_const thy s; |
11520 | 371 |
in |
16458 | 372 |
(case Sign.const_type thy cname of |
15531 | 373 |
SOME T => |
11520 | 374 |
let val T' = (case tyopt of |
15531 | 375 |
NONE => T |
376 |
| SOME ty => |
|
16458 | 377 |
let val U = prep_type thy ty |
378 |
in if Sign.typ_instance thy (U, T) then U |
|
11520 | 379 |
else error ("Illegal type constraint for constant " ^ cname) |
380 |
end) |
|
17521 | 381 |
in (case AList.lookup (op =) consts (cname, T') of |
15531 | 382 |
NONE => CodegenData.put {codegens = codegens, |
12452 | 383 |
tycodegens = tycodegens, |
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
384 |
consts = ((cname, T'), syn) :: consts, |
15261 | 385 |
types = types, attrs = attrs, preprocs = preprocs, |
17144 | 386 |
modules = modules, test_params = test_params} thy |
15531 | 387 |
| SOME _ => error ("Constant " ^ cname ^ " already associated with code")) |
11520 | 388 |
end |
389 |
| _ => error ("Not a constant: " ^ s)) |
|
390 |
end) (thy, xs); |
|
391 |
||
392 |
val assoc_consts_i = gen_assoc_consts (K I); |
|
16458 | 393 |
val assoc_consts = gen_assoc_consts (typ_of oo read_ctyp); |
11520 | 394 |
|
15801 | 395 |
|
11520 | 396 |
(**** associate types with target language types ****) |
397 |
||
15570 | 398 |
fun assoc_types xs thy = Library.foldl (fn (thy, (s, syn)) => |
11520 | 399 |
let |
17144 | 400 |
val {codegens, tycodegens, consts, types, attrs, preprocs, modules, test_params} = |
14105 | 401 |
CodegenData.get thy; |
16649 | 402 |
val tc = Sign.intern_type thy s |
11520 | 403 |
in |
17521 | 404 |
(case AList.lookup (op =) types tc of |
15531 | 405 |
NONE => CodegenData.put {codegens = codegens, |
12452 | 406 |
tycodegens = tycodegens, consts = consts, |
14105 | 407 |
types = (tc, syn) :: types, attrs = attrs, |
17144 | 408 |
preprocs = preprocs, modules = modules, test_params = test_params} thy |
15531 | 409 |
| SOME _ => error ("Type " ^ tc ^ " already associated with code")) |
11520 | 410 |
end) (thy, xs); |
411 |
||
17521 | 412 |
fun get_assoc_type thy s = AList.lookup (op =) ((#types o CodegenData.get) thy) s; |
11546 | 413 |
|
11520 | 414 |
|
415 |
(**** make valid ML identifiers ****) |
|
416 |
||
14858 | 417 |
fun is_ascii_letdig x = Symbol.is_ascii_letter x orelse |
418 |
Symbol.is_ascii_digit x orelse Symbol.is_ascii_quasi x; |
|
419 |
||
420 |
fun dest_sym s = (case split_last (snd (take_prefix (equal "\\") (explode s))) of |
|
421 |
("<" :: "^" :: xs, ">") => (true, implode xs) |
|
422 |
| ("<" :: xs, ">") => (false, implode xs) |
|
423 |
| _ => sys_error "dest_sym"); |
|
16458 | 424 |
|
14858 | 425 |
fun mk_id s = if s = "" then "" else |
11520 | 426 |
let |
14858 | 427 |
fun check_str [] = [] |
428 |
| check_str xs = (case take_prefix is_ascii_letdig xs of |
|
429 |
([], " " :: zs) => check_str zs |
|
430 |
| ([], z :: zs) => |
|
431 |
if size z = 1 then string_of_int (ord z) :: check_str zs |
|
432 |
else (case dest_sym z of |
|
433 |
(true, "isub") => check_str zs |
|
434 |
| (true, "isup") => "" :: check_str zs |
|
435 |
| (ctrl, s') => (if ctrl then "ctrl_" ^ s' else s') :: check_str zs) |
|
436 |
| (ys, zs) => implode ys :: check_str zs); |
|
437 |
val s' = space_implode "_" |
|
15570 | 438 |
(List.concat (map (check_str o Symbol.explode) (NameSpace.unpack s))) |
11520 | 439 |
in |
14858 | 440 |
if Symbol.is_ascii_letter (hd (explode s')) then s' else "id_" ^ s' |
11520 | 441 |
end; |
442 |
||
17144 | 443 |
fun mk_long_id (p as (tab, used)) module s = |
16649 | 444 |
let |
17144 | 445 |
fun find_name [] = sys_error "mk_long_id" |
446 |
| find_name (ys :: yss) = |
|
447 |
let |
|
448 |
val s' = NameSpace.pack ys |
|
449 |
val s'' = NameSpace.append module s' |
|
17412 | 450 |
in case Symtab.lookup used s'' of |
17261 | 451 |
NONE => ((module, s'), |
17412 | 452 |
(Symtab.update_new (s, (module, s')) tab, |
453 |
Symtab.update_new (s'', ()) used)) |
|
17144 | 454 |
| SOME _ => find_name yss |
455 |
end |
|
17412 | 456 |
in case Symtab.lookup tab s of |
17144 | 457 |
NONE => find_name (Library.suffixes1 (NameSpace.unpack s)) |
458 |
| SOME name => (name, p) |
|
16649 | 459 |
end; |
460 |
||
17144 | 461 |
(* module: module name for caller *) |
462 |
(* module': module name for callee *) |
|
463 |
(* if caller and callee reside in different modules, use qualified access *) |
|
16649 | 464 |
|
17144 | 465 |
fun mk_qual_id module (module', s) = |
466 |
if module = module' orelse module' = "" then s else module' ^ "." ^ s; |
|
467 |
||
468 |
fun mk_const_id module cname (gr, (tab1, tab2)) = |
|
16649 | 469 |
let |
17144 | 470 |
val ((module, s), tab1') = mk_long_id tab1 module cname |
471 |
val s' = mk_id s; |
|
16649 | 472 |
val s'' = if s' mem ThmDatabase.ml_reserved then s' ^ "_const" else s' |
17144 | 473 |
in ((gr, (tab1', tab2)), (module, s'')) end; |
13073
cc9d7f403a4b
mk_const_id now checks for clashes with reserved ML identifiers.
berghofe
parents:
13003
diff
changeset
|
474 |
|
17144 | 475 |
fun get_const_id cname (gr, (tab1, tab2)) = |
17412 | 476 |
case Symtab.lookup (fst tab1) cname of |
17144 | 477 |
NONE => error ("get_const_id: no such constant: " ^ quote cname) |
478 |
| SOME (module, s) => |
|
479 |
let |
|
480 |
val s' = mk_id s; |
|
481 |
val s'' = if s' mem ThmDatabase.ml_reserved then s' ^ "_const" else s' |
|
482 |
in (module, s'') end; |
|
483 |
||
484 |
fun mk_type_id module tyname (gr, (tab1, tab2)) = |
|
16649 | 485 |
let |
17144 | 486 |
val ((module, s), tab2') = mk_long_id tab2 module tyname |
487 |
val s' = mk_id s; |
|
488 |
val s'' = if s' mem ThmDatabase.ml_reserved then s' ^ "_type" else s' |
|
489 |
in ((gr, (tab1, tab2')), (module, s'')) end; |
|
16649 | 490 |
|
17144 | 491 |
fun get_type_id tyname (gr, (tab1, tab2)) = |
17412 | 492 |
case Symtab.lookup (fst tab2) tyname of |
17144 | 493 |
NONE => error ("get_type_id: no such type: " ^ quote tyname) |
494 |
| SOME (module, s) => |
|
495 |
let |
|
496 |
val s' = mk_id s; |
|
497 |
val s'' = if s' mem ThmDatabase.ml_reserved then s' ^ "_type" else s' |
|
498 |
in (module, s'') end; |
|
499 |
||
500 |
fun get_type_id' f tyname tab = apsnd f (get_type_id tyname tab); |
|
501 |
||
502 |
fun get_node (gr, x) k = Graph.get_node gr k; |
|
503 |
fun add_edge e (gr, x) = (Graph.add_edge e gr, x); |
|
504 |
fun add_edge_acyclic e (gr, x) = (Graph.add_edge_acyclic e gr, x); |
|
505 |
fun del_nodes ks (gr, x) = (Graph.del_nodes ks gr, x); |
|
506 |
fun map_node k f (gr, x) = (Graph.map_node k f gr, x); |
|
507 |
fun new_node p (gr, x) = (Graph.new_node p gr, x); |
|
16649 | 508 |
|
509 |
fun theory_of_type s thy = |
|
510 |
if Sign.declared_tyname thy s |
|
511 |
then SOME (if_none (get_first (theory_of_type s) (Theory.parents_of thy)) thy) |
|
512 |
else NONE; |
|
513 |
||
514 |
fun theory_of_const s thy = |
|
515 |
if Sign.declared_const thy s |
|
516 |
then SOME (if_none (get_first (theory_of_const s) (Theory.parents_of thy)) thy) |
|
517 |
else NONE; |
|
518 |
||
519 |
fun thyname_of_type s thy = (case theory_of_type s thy of |
|
520 |
NONE => error ("thyname_of_type: no such type: " ^ quote s) |
|
521 |
| SOME thy' => Context.theory_name thy'); |
|
522 |
||
523 |
fun thyname_of_const s thy = (case theory_of_const s thy of |
|
524 |
NONE => error ("thyname_of_const: no such constant: " ^ quote s) |
|
525 |
| SOME thy' => Context.theory_name thy'); |
|
11520 | 526 |
|
13731
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
527 |
fun rename_terms ts = |
11520 | 528 |
let |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
529 |
val names = foldr add_term_names |
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
530 |
(map (fst o fst) (Drule.vars_of_terms ts)) ts; |
14858 | 531 |
val reserved = names inter ThmDatabase.ml_reserved; |
15570 | 532 |
val (illegal, alt_names) = split_list (List.mapPartial (fn s => |
15531 | 533 |
let val s' = mk_id s in if s = s' then NONE else SOME (s, s') end) names) |
14858 | 534 |
val ps = (reserved @ illegal) ~~ |
535 |
variantlist (map (suffix "'") reserved @ alt_names, names); |
|
11520 | 536 |
|
17521 | 537 |
fun rename_id s = AList.lookup (op =) ps s |> the_default s; |
14858 | 538 |
|
539 |
fun rename (Var ((a, i), T)) = Var ((rename_id a, i), T) |
|
540 |
| rename (Free (a, T)) = Free (rename_id a, T) |
|
11520 | 541 |
| rename (Abs (s, T, t)) = Abs (s, T, rename t) |
542 |
| rename (t $ u) = rename t $ rename u |
|
543 |
| rename t = t; |
|
544 |
in |
|
13731
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
545 |
map rename ts |
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
546 |
end; |
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
547 |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
548 |
val rename_term = hd o rename_terms o single; |
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
549 |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
550 |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
551 |
(**** retrieve definition of constant ****) |
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
552 |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
553 |
fun is_instance thy T1 T2 = |
16649 | 554 |
Sign.typ_instance thy (T1, Type.varifyT T2); |
13731
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
555 |
|
18098 | 556 |
fun get_assoc_code thy s T = Option.map snd (Library.find_first (fn ((s', T'), _) => |
13731
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
557 |
s = s' andalso is_instance thy T T') (#consts (CodegenData.get thy))); |
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
558 |
|
16769
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
559 |
fun get_aux_code xs = List.mapPartial (fn (m, code) => |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
560 |
if m = "" orelse m mem !mode then SOME code else NONE) xs; |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
561 |
|
16649 | 562 |
fun mk_deftab thy = |
13731
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
563 |
let |
16649 | 564 |
val axmss = map (fn thy' => |
565 |
(Context.theory_name thy', snd (#axioms (Theory.rep_theory thy')))) |
|
566 |
(thy :: Theory.ancestors_of thy); |
|
15261 | 567 |
fun prep_def def = (case preprocess thy [def] of |
16649 | 568 |
[def'] => prop_of def' | _ => error "mk_deftab: bad preprocessor"); |
15261 | 569 |
fun dest t = |
570 |
let |
|
571 |
val (lhs, rhs) = Logic.dest_equals t; |
|
572 |
val (c, args) = strip_comb lhs; |
|
16649 | 573 |
val (s, T) = dest_Const c |
574 |
in if forall is_Var args then SOME (s, (T, (args, rhs))) else NONE |
|
15531 | 575 |
end handle TERM _ => NONE; |
16649 | 576 |
fun add_def thyname (defs, (name, t)) = (case dest t of |
577 |
NONE => defs |
|
578 |
| SOME _ => (case dest (prep_def (Thm.get_axiom thy name)) of |
|
579 |
NONE => defs |
|
17412 | 580 |
| SOME (s, (T, (args, rhs))) => Symtab.update |
17261 | 581 |
(s, (T, (thyname, split_last (rename_terms (args @ [rhs])))) :: |
17412 | 582 |
if_none (Symtab.lookup defs s) []) defs)) |
13731
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
583 |
in |
16649 | 584 |
foldl (fn ((thyname, axms), defs) => |
585 |
Symtab.foldl (add_def thyname) (defs, axms)) Symtab.empty axmss |
|
11520 | 586 |
end; |
587 |
||
17412 | 588 |
fun get_defn thy defs s T = (case Symtab.lookup defs s of |
16649 | 589 |
NONE => NONE |
590 |
| SOME ds => |
|
591 |
let val i = find_index (is_instance thy T o fst) ds |
|
592 |
in if i >= 0 then |
|
593 |
SOME (List.nth (ds, i), if length ds = 1 then NONE else SOME i) |
|
594 |
else NONE |
|
595 |
end); |
|
596 |
||
11520 | 597 |
|
12452 | 598 |
(**** invoke suitable code generator for term / type ****) |
11520 | 599 |
|
17144 | 600 |
fun codegen_error (gr, _) dep s = |
601 |
error (s ^ "\nrequired by:\n" ^ commas (Graph.all_succs gr [dep])); |
|
602 |
||
603 |
fun invoke_codegen thy defs dep module brack (gr, t) = (case get_first |
|
604 |
(fn (_, f) => f thy defs gr dep module brack t) (#codegens (CodegenData.get thy)) of |
|
605 |
NONE => codegen_error gr dep ("Unable to generate code for term:\n" ^ |
|
606 |
Sign.string_of_term thy t) |
|
15531 | 607 |
| SOME x => x); |
12452 | 608 |
|
17144 | 609 |
fun invoke_tycodegen thy defs dep module brack (gr, T) = (case get_first |
610 |
(fn (_, f) => f thy defs gr dep module brack T) (#tycodegens (CodegenData.get thy)) of |
|
611 |
NONE => codegen_error gr dep ("Unable to generate code for type:\n" ^ |
|
612 |
Sign.string_of_typ thy T) |
|
15531 | 613 |
| SOME x => x); |
11520 | 614 |
|
615 |
||
616 |
(**** code generator for mixfix expressions ****) |
|
617 |
||
618 |
fun parens p = Pretty.block [Pretty.str "(", p, Pretty.str ")"]; |
|
619 |
||
620 |
fun pretty_fn [] p = [p] |
|
621 |
| pretty_fn (x::xs) p = Pretty.str ("fn " ^ x ^ " =>") :: |
|
622 |
Pretty.brk 1 :: pretty_fn xs p; |
|
623 |
||
16769
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
624 |
fun pretty_mixfix _ _ [] [] _ = [] |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
625 |
| pretty_mixfix module module' (Arg :: ms) (p :: ps) qs = |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
626 |
p :: pretty_mixfix module module' ms ps qs |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
627 |
| pretty_mixfix module module' (Ignore :: ms) ps qs = |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
628 |
pretty_mixfix module module' ms ps qs |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
629 |
| pretty_mixfix module module' (Module :: ms) ps qs = |
17144 | 630 |
(if module <> module' |
16769
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
631 |
then cons (Pretty.str (module' ^ ".")) else I) |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
632 |
(pretty_mixfix module module' ms ps qs) |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
633 |
| pretty_mixfix module module' (Pretty p :: ms) ps qs = |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
634 |
p :: pretty_mixfix module module' ms ps qs |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
635 |
| pretty_mixfix module module' (Quote _ :: ms) ps (q :: qs) = |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
636 |
q :: pretty_mixfix module module' ms ps qs; |
11520 | 637 |
|
18098 | 638 |
fun fillin_mixfix' ms args = |
639 |
let |
|
640 |
fun fillin [] [] vars = (vars, []) |
|
641 |
| fillin (Arg :: ms) [] vars = |
|
642 |
let |
|
643 |
val v = "a_" ^ (string_of_int o length) vars; |
|
644 |
in fillin ms args (v::vars) ||> (cons o Pretty.str) v end |
|
645 |
| fillin (Arg :: ms) (a :: args) vars = |
|
646 |
fillin ms args vars ||> cons a |
|
647 |
| fillin (Ignore :: ms) args vars = |
|
648 |
fillin ms args vars |
|
649 |
| fillin (Module :: ms) args vars = |
|
650 |
fillin ms args vars |
|
651 |
| fillin (Pretty p :: ms) args vars = |
|
652 |
fillin ms args vars ||> cons p |
|
653 |
| fillin (Quote _ :: ms) args vars = |
|
654 |
error ("no quotes currently supported in codegen patterns"); |
|
655 |
in fillin ms args [] ||> Pretty.block end; |
|
656 |
||
11520 | 657 |
|
12452 | 658 |
(**** default code generators ****) |
11520 | 659 |
|
660 |
fun eta_expand t ts i = |
|
661 |
let |
|
662 |
val (Ts, _) = strip_type (fastype_of t); |
|
663 |
val j = i - length ts |
|
664 |
in |
|
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
665 |
foldr (fn (T, t) => Abs ("x", T, t)) |
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
666 |
(list_comb (t, ts @ map Bound (j-1 downto 0))) (Library.take (j, Ts)) |
11520 | 667 |
end; |
668 |
||
669 |
fun mk_app _ p [] = p |
|
670 |
| mk_app brack p ps = if brack then |
|
671 |
Pretty.block (Pretty.str "(" :: |
|
672 |
separate (Pretty.brk 1) (p :: ps) @ [Pretty.str ")"]) |
|
673 |
else Pretty.block (separate (Pretty.brk 1) (p :: ps)); |
|
674 |
||
14858 | 675 |
fun new_names t xs = variantlist (map mk_id xs, |
11520 | 676 |
map (fst o fst o dest_Var) (term_vars t) union |
677 |
add_term_names (t, ThmDatabase.ml_reserved)); |
|
678 |
||
679 |
fun new_name t x = hd (new_names t [x]); |
|
680 |
||
17144 | 681 |
fun if_library x y = if "library" mem !mode then x else y; |
682 |
||
683 |
fun default_codegen thy defs gr dep module brack t = |
|
11520 | 684 |
let |
685 |
val (u, ts) = strip_comb t; |
|
17144 | 686 |
fun codegens brack = foldl_map (invoke_codegen thy defs dep module brack) |
11520 | 687 |
in (case u of |
14105 | 688 |
Var ((s, i), T) => |
689 |
let |
|
690 |
val (gr', ps) = codegens true (gr, ts); |
|
17144 | 691 |
val (gr'', _) = invoke_tycodegen thy defs dep module false (gr', T) |
15531 | 692 |
in SOME (gr'', mk_app brack (Pretty.str (s ^ |
11520 | 693 |
(if i=0 then "" else string_of_int i))) ps) |
694 |
end |
|
695 |
||
14105 | 696 |
| Free (s, T) => |
697 |
let |
|
698 |
val (gr', ps) = codegens true (gr, ts); |
|
17144 | 699 |
val (gr'', _) = invoke_tycodegen thy defs dep module false (gr', T) |
15531 | 700 |
in SOME (gr'', mk_app brack (Pretty.str s) ps) end |
11520 | 701 |
|
702 |
| Const (s, T) => |
|
703 |
(case get_assoc_code thy s T of |
|
16769
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
704 |
SOME (ms, aux) => |
11520 | 705 |
let val i = num_args ms |
706 |
in if length ts < i then |
|
17144 | 707 |
default_codegen thy defs gr dep module brack (eta_expand u ts i) |
11520 | 708 |
else |
709 |
let |
|
12452 | 710 |
val (ts1, ts2) = args_of ms ts; |
711 |
val (gr1, ps1) = codegens false (gr, ts1); |
|
712 |
val (gr2, ps2) = codegens true (gr1, ts2); |
|
713 |
val (gr3, ps3) = codegens false (gr2, quotes_of ms); |
|
17549 | 714 |
val (gr4, _) = invoke_tycodegen thy defs dep module false |
715 |
(gr3, funpow (length ts) (hd o tl o snd o dest_Type) T); |
|
17144 | 716 |
val (module', suffix) = (case get_defn thy defs s T of |
717 |
NONE => (if_library (thyname_of_const s thy) module, "") |
|
718 |
| SOME ((U, (module', _)), NONE) => |
|
719 |
(if_library module' module, "") |
|
720 |
| SOME ((U, (module', _)), SOME i) => |
|
721 |
(if_library module' module, " def" ^ string_of_int i)); |
|
16769
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
722 |
val node_id = s ^ suffix; |
17144 | 723 |
fun p module' = mk_app brack (Pretty.block |
724 |
(pretty_mixfix module module' ms ps1 ps3)) ps2 |
|
17549 | 725 |
in SOME (case try (get_node gr4) node_id of |
16769
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
726 |
NONE => (case get_aux_code aux of |
17549 | 727 |
[] => (gr4, p module) |
17144 | 728 |
| xs => (add_edge (node_id, dep) (new_node |
17549 | 729 |
(node_id, (NONE, module', space_implode "\n" xs ^ "\n")) gr4), |
17144 | 730 |
p module')) |
731 |
| SOME (_, module'', _) => |
|
17549 | 732 |
(add_edge (node_id, dep) gr4, p module'')) |
11520 | 733 |
end |
734 |
end |
|
16649 | 735 |
| NONE => (case get_defn thy defs s T of |
15531 | 736 |
NONE => NONE |
17144 | 737 |
| SOME ((U, (thyname, (args, rhs))), k) => |
11520 | 738 |
let |
17144 | 739 |
val module' = if_library thyname module; |
740 |
val suffix = (case k of NONE => "" | SOME i => " def" ^ string_of_int i); |
|
16649 | 741 |
val node_id = s ^ suffix; |
17144 | 742 |
val (gr', (ps, def_id)) = codegens true (gr, ts) |>>> |
743 |
mk_const_id module' (s ^ suffix); |
|
744 |
val p = mk_app brack (Pretty.str (mk_qual_id module def_id)) ps |
|
745 |
in SOME (case try (get_node gr') node_id of |
|
746 |
NONE => |
|
747 |
let |
|
748 |
val _ = message ("expanding definition of " ^ s); |
|
749 |
val (Ts, _) = strip_type T; |
|
750 |
val (args', rhs') = |
|
751 |
if not (null args) orelse null Ts then (args, rhs) else |
|
752 |
let val v = Free (new_name rhs "x", hd Ts) |
|
753 |
in ([v], betapply (rhs, v)) end; |
|
754 |
val (gr1, p') = invoke_codegen thy defs node_id module' false |
|
755 |
(add_edge (node_id, dep) |
|
756 |
(new_node (node_id, (NONE, "", "")) gr'), rhs'); |
|
757 |
val (gr2, xs) = codegens false (gr1, args'); |
|
758 |
val (gr3, _) = invoke_tycodegen thy defs dep module false (gr2, T); |
|
759 |
val (gr4, ty) = invoke_tycodegen thy defs node_id module' false (gr3, U); |
|
760 |
in (map_node node_id (K (NONE, module', Pretty.string_of |
|
761 |
(Pretty.block (separate (Pretty.brk 1) |
|
762 |
(if null args' then |
|
763 |
[Pretty.str ("val " ^ snd def_id ^ " :"), ty] |
|
764 |
else Pretty.str ("fun " ^ snd def_id) :: xs) @ |
|
765 |
[Pretty.str " =", Pretty.brk 1, p', Pretty.str ";"])) ^ "\n\n")) gr4, |
|
766 |
p) |
|
767 |
end |
|
768 |
| SOME _ => (add_edge (node_id, dep) gr', p)) |
|
11520 | 769 |
end)) |
770 |
||
771 |
| Abs _ => |
|
772 |
let |
|
773 |
val (bs, Ts) = ListPair.unzip (strip_abs_vars u); |
|
774 |
val t = strip_abs_body u |
|
775 |
val bs' = new_names t bs; |
|
12452 | 776 |
val (gr1, ps) = codegens true (gr, ts); |
17144 | 777 |
val (gr2, p) = invoke_codegen thy defs dep module false |
12452 | 778 |
(gr1, subst_bounds (map Free (rev (bs' ~~ Ts)), t)); |
11520 | 779 |
in |
15531 | 780 |
SOME (gr2, mk_app brack (Pretty.block (Pretty.str "(" :: pretty_fn bs' p @ |
11520 | 781 |
[Pretty.str ")"])) ps) |
782 |
end |
|
783 |
||
15531 | 784 |
| _ => NONE) |
11520 | 785 |
end; |
786 |
||
17144 | 787 |
fun default_tycodegen thy defs gr dep module brack (TVar ((s, i), _)) = |
15531 | 788 |
SOME (gr, Pretty.str (s ^ (if i = 0 then "" else string_of_int i))) |
17144 | 789 |
| default_tycodegen thy defs gr dep module brack (TFree (s, _)) = |
16649 | 790 |
SOME (gr, Pretty.str s) |
17144 | 791 |
| default_tycodegen thy defs gr dep module brack (Type (s, Ts)) = |
17521 | 792 |
(case AList.lookup (op =) ((#types o CodegenData.get) thy) s of |
15531 | 793 |
NONE => NONE |
16769
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
794 |
| SOME (ms, aux) => |
12452 | 795 |
let |
796 |
val (gr', ps) = foldl_map |
|
17144 | 797 |
(invoke_tycodegen thy defs dep module false) |
16649 | 798 |
(gr, fst (args_of ms Ts)); |
12452 | 799 |
val (gr'', qs) = foldl_map |
17144 | 800 |
(invoke_tycodegen thy defs dep module false) |
16769
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
801 |
(gr', quotes_of ms); |
17144 | 802 |
val module' = if_library (thyname_of_type s thy) module; |
16769
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
803 |
val node_id = s ^ " (type)"; |
17144 | 804 |
fun p module' = Pretty.block (pretty_mixfix module module' ms ps qs) |
805 |
in SOME (case try (get_node gr'') node_id of |
|
16769
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
806 |
NONE => (case get_aux_code aux of |
17144 | 807 |
[] => (gr'', p module') |
808 |
| xs => (fst (mk_type_id module' s |
|
809 |
(add_edge (node_id, dep) (new_node (node_id, |
|
810 |
(NONE, module', space_implode "\n" xs ^ "\n")) gr''))), |
|
811 |
p module')) |
|
812 |
| SOME (_, module'', _) => |
|
813 |
(add_edge (node_id, dep) gr'', p module'')) |
|
16769
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
814 |
end); |
12452 | 815 |
|
15801 | 816 |
val _ = Context.add_setup |
817 |
[add_codegen "default" default_codegen, |
|
818 |
add_tycodegen "default" default_tycodegen]; |
|
819 |
||
11520 | 820 |
|
16649 | 821 |
fun mk_struct name s = "structure " ^ name ^ " =\nstruct\n\n" ^ s ^ "end;\n"; |
822 |
||
17521 | 823 |
fun add_to_module name s = AList.map_entry (op =) name (suffix s); |
16649 | 824 |
|
17144 | 825 |
fun output_code gr module xs = |
16649 | 826 |
let |
17144 | 827 |
val code = List.mapPartial (fn s => |
828 |
let val c as (_, module', _) = Graph.get_node gr s |
|
829 |
in if module = "" orelse module = module' then SOME (s, c) else NONE end) |
|
830 |
(rev (Graph.all_preds gr xs)); |
|
16649 | 831 |
fun string_of_cycle (a :: b :: cs) = |
832 |
let val SOME (x, y) = get_first (fn (x, (_, a', _)) => |
|
833 |
if a = a' then Option.map (pair x) |
|
18098 | 834 |
(Library.find_first (equal b o #2 o Graph.get_node gr) |
16649 | 835 |
(Graph.imm_succs gr x)) |
836 |
else NONE) code |
|
837 |
in x ^ " called by " ^ y ^ "\n" ^ string_of_cycle (b :: cs) end |
|
838 |
| string_of_cycle _ = "" |
|
839 |
in |
|
17144 | 840 |
if module = "" then |
16649 | 841 |
let |
842 |
val modules = distinct (map (#2 o snd) code); |
|
843 |
val mod_gr = foldr (uncurry Graph.add_edge_acyclic) |
|
844 |
(foldr (uncurry (Graph.new_node o rpair ())) Graph.empty modules) |
|
17144 | 845 |
(List.concat (map (fn (s, (_, module, _)) => map (pair module) |
846 |
(filter_out (equal module) (map (#2 o Graph.get_node gr) |
|
16649 | 847 |
(Graph.imm_succs gr s)))) code)); |
848 |
val modules' = |
|
849 |
rev (Graph.all_preds mod_gr (map (#2 o Graph.get_node gr) xs)) |
|
850 |
in |
|
17144 | 851 |
foldl (fn ((_, (_, module, s)), ms) => add_to_module module s ms) |
16649 | 852 |
(map (rpair "") modules') code |
853 |
end handle Graph.CYCLES (cs :: _) => |
|
854 |
error ("Cyclic dependency of modules:\n" ^ commas cs ^ |
|
855 |
"\n" ^ string_of_cycle cs) |
|
17144 | 856 |
else [(module, implode (map (#3 o snd) code))] |
16649 | 857 |
end; |
11520 | 858 |
|
17144 | 859 |
fun gen_generate_code prep_term thy modules module = |
14598
7009f59711e3
Replaced quote by Library.quote, since quote now refers to Symbol.quote
berghofe
parents:
14197
diff
changeset
|
860 |
setmp print_mode [] (Pretty.setmp_margin (!margin) (fn xs => |
11520 | 861 |
let |
17144 | 862 |
val _ = assert (module <> "" orelse |
863 |
"library" mem !mode andalso forall (equal "" o fst) xs) |
|
864 |
"missing module name"; |
|
865 |
val graphs = get_modules thy; |
|
16649 | 866 |
val defs = mk_deftab thy; |
17144 | 867 |
val gr = new_node ("<Top>", (NONE, module, "")) |
868 |
(foldl (fn ((gr, (tab1, tab2)), (gr', (tab1', tab2'))) => |
|
869 |
(Graph.merge (fn ((_, module, _), (_, module', _)) => |
|
870 |
module = module') (gr, gr'), |
|
871 |
(merge_nametabs (tab1, tab1'), merge_nametabs (tab2, tab2')))) emptygr |
|
17412 | 872 |
(map (fn s => case Symtab.lookup graphs s of |
17144 | 873 |
NONE => error ("Undefined code module: " ^ s) |
874 |
| SOME gr => gr) modules)) |
|
875 |
handle Graph.DUPS ks => error ("Duplicate code for " ^ commas ks); |
|
16649 | 876 |
fun expand (t as Abs _) = t |
877 |
| expand t = (case fastype_of t of |
|
878 |
Type ("fun", [T, U]) => Abs ("x", T, t $ Bound 0) | _ => t); |
|
11520 | 879 |
val (gr', ps) = foldl_map (fn (gr, (s, t)) => apsnd (pair s) |
17144 | 880 |
(invoke_codegen thy defs "<Top>" module false (gr, t))) |
17549 | 881 |
(gr, map (apsnd (expand o preprocess_term thy o prep_term thy)) xs); |
17144 | 882 |
val code = List.mapPartial |
883 |
(fn ("", _) => NONE |
|
884 |
| (s', p) => SOME (Pretty.string_of (Pretty.block |
|
885 |
[Pretty.str ("val " ^ s' ^ " ="), Pretty.brk 1, p, Pretty.str ";"]))) ps; |
|
886 |
val code' = space_implode "\n\n" code ^ "\n\n"; |
|
887 |
val code'' = |
|
888 |
List.mapPartial (fn (name, s) => |
|
889 |
if "library" mem !mode andalso name = module andalso null code |
|
890 |
then NONE |
|
891 |
else SOME (name, mk_struct name s)) |
|
892 |
((if null code then I |
|
893 |
else add_to_module module code') |
|
894 |
(output_code (fst gr') (if_library "" module) ["<Top>"])) |
|
16649 | 895 |
in |
17144 | 896 |
(code'', del_nodes ["<Top>"] gr') |
16649 | 897 |
end)); |
11520 | 898 |
|
899 |
val generate_code_i = gen_generate_code (K I); |
|
900 |
val generate_code = gen_generate_code |
|
16458 | 901 |
(fn thy => term_of o read_cterm thy o rpair TypeInfer.logicT); |
11520 | 902 |
|
12452 | 903 |
|
13753
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
904 |
(**** Reflection ****) |
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
905 |
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
906 |
val strip_tname = implode o tl o explode; |
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
907 |
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
908 |
fun pretty_list xs = Pretty.block (Pretty.str "[" :: |
15570 | 909 |
List.concat (separate [Pretty.str ",", Pretty.brk 1] (map single xs)) @ |
13753
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
910 |
[Pretty.str "]"]); |
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
911 |
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
912 |
fun mk_type p (TVar ((s, i), _)) = Pretty.str |
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
913 |
(strip_tname s ^ (if i = 0 then "" else string_of_int i) ^ "T") |
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
914 |
| mk_type p (TFree (s, _)) = Pretty.str (strip_tname s ^ "T") |
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
915 |
| mk_type p (Type (s, Ts)) = (if p then parens else I) (Pretty.block |
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
916 |
[Pretty.str "Type", Pretty.brk 1, Pretty.str ("(\"" ^ s ^ "\","), |
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
917 |
Pretty.brk 1, pretty_list (map (mk_type false) Ts), Pretty.str ")"]); |
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
918 |
|
17144 | 919 |
fun mk_term_of gr module p (TVar ((s, i), _)) = Pretty.str |
13753
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
920 |
(strip_tname s ^ (if i = 0 then "" else string_of_int i) ^ "F") |
17144 | 921 |
| mk_term_of gr module p (TFree (s, _)) = Pretty.str (strip_tname s ^ "F") |
922 |
| mk_term_of gr module p (Type (s, Ts)) = (if p then parens else I) |
|
16649 | 923 |
(Pretty.block (separate (Pretty.brk 1) |
17144 | 924 |
(Pretty.str (mk_qual_id module |
925 |
(get_type_id' (fn s' => "term_of_" ^ s') s gr)) :: |
|
16649 | 926 |
List.concat (map (fn T => |
17144 | 927 |
[mk_term_of gr module true T, mk_type true T]) Ts)))); |
13753
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
928 |
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
929 |
|
14105 | 930 |
(**** Test data generators ****) |
931 |
||
17144 | 932 |
fun mk_gen gr module p xs a (TVar ((s, i), _)) = Pretty.str |
14105 | 933 |
(strip_tname s ^ (if i = 0 then "" else string_of_int i) ^ "G") |
17144 | 934 |
| mk_gen gr module p xs a (TFree (s, _)) = Pretty.str (strip_tname s ^ "G") |
935 |
| mk_gen gr module p xs a (Type (s, Ts)) = (if p then parens else I) |
|
16649 | 936 |
(Pretty.block (separate (Pretty.brk 1) |
17144 | 937 |
(Pretty.str (mk_qual_id module (get_type_id' (fn s' => "gen_" ^ s') s gr) ^ |
16649 | 938 |
(if s mem xs then "'" else "")) :: |
17144 | 939 |
map (mk_gen gr module true xs a) Ts @ |
16649 | 940 |
(if s mem xs then [Pretty.str a] else [])))); |
14105 | 941 |
|
15531 | 942 |
val test_fn : (int -> (string * term) list option) ref = ref (fn _ => NONE); |
14105 | 943 |
|
17638 | 944 |
fun test_term thy sz i t = |
14105 | 945 |
let |
946 |
val _ = assert (null (term_tvars t) andalso null (term_tfrees t)) |
|
947 |
"Term to be tested contains type variables"; |
|
948 |
val _ = assert (null (term_vars t)) |
|
949 |
"Term to be tested contains schematic variables"; |
|
950 |
val frees = map dest_Free (term_frees t); |
|
17549 | 951 |
val frees' = frees ~~ |
952 |
map (fn i => "arg" ^ string_of_int i) (1 upto length frees); |
|
17144 | 953 |
val (code, gr) = setmp mode ["term_of", "test"] |
954 |
(generate_code_i thy [] "Generated") [("testf", list_abs_free (frees, t))]; |
|
17638 | 955 |
val s = setmp print_mode [] (fn () => "structure TestTerm =\nstruct\n\n" ^ |
17144 | 956 |
space_implode "\n" (map snd code) ^ |
16649 | 957 |
"\nopen Generated;\n\n" ^ Pretty.string_of |
14105 | 958 |
(Pretty.block [Pretty.str "val () = Codegen.test_fn :=", |
17549 | 959 |
Pretty.brk 1, Pretty.str ("(fn i =>"), Pretty.brk 1, |
14105 | 960 |
Pretty.blk (0, [Pretty.str "let", Pretty.brk 1, |
17549 | 961 |
Pretty.blk (0, separate Pretty.fbrk (map (fn ((s, T), s') => |
962 |
Pretty.block [Pretty.str ("val " ^ s' ^ " ="), Pretty.brk 1, |
|
17144 | 963 |
mk_gen gr "Generated" false [] "" T, Pretty.brk 1, |
17549 | 964 |
Pretty.str "i;"]) frees')), |
14105 | 965 |
Pretty.brk 1, Pretty.str "in", Pretty.brk 1, |
966 |
Pretty.block [Pretty.str "if ", |
|
17549 | 967 |
mk_app false (Pretty.str "testf") (map (Pretty.str o snd) frees'), |
15531 | 968 |
Pretty.brk 1, Pretty.str "then NONE", |
14105 | 969 |
Pretty.brk 1, Pretty.str "else ", |
15531 | 970 |
Pretty.block [Pretty.str "SOME ", Pretty.block (Pretty.str "[" :: |
15570 | 971 |
List.concat (separate [Pretty.str ",", Pretty.brk 1] |
17549 | 972 |
(map (fn ((s, T), s') => [Pretty.block |
15326
ff21cddee442
Made test_term escape special characters in strings that caused the
berghofe
parents:
15261
diff
changeset
|
973 |
[Pretty.str ("(" ^ Library.quote (Symbol.escape s) ^ ","), Pretty.brk 1, |
17144 | 974 |
mk_app false (mk_term_of gr "Generated" false T) |
17549 | 975 |
[Pretty.str s'], Pretty.str ")"]]) frees')) @ |
14105 | 976 |
[Pretty.str "]"])]], |
14980 | 977 |
Pretty.brk 1, Pretty.str "end"]), Pretty.str ");"]) ^ |
17638 | 978 |
"\n\nend;\n") (); |
14105 | 979 |
val _ = use_text Context.ml_output false s; |
15531 | 980 |
fun iter f k = if k > i then NONE |
14135
f8a25218b423
test_term now handles Match exception raised in generated code.
berghofe
parents:
14110
diff
changeset
|
981 |
else (case (f () handle Match => |
15531 | 982 |
(warning "Exception Match raised in generated code"; NONE)) of |
983 |
NONE => iter f (k+1) | SOME x => SOME x); |
|
984 |
fun test k = if k > sz then NONE |
|
14105 | 985 |
else (priority ("Test data size: " ^ string_of_int k); |
986 |
case iter (fn () => !test_fn k) 1 of |
|
15531 | 987 |
NONE => test (k+1) | SOME x => SOME x); |
17638 | 988 |
in test 0 end; |
14105 | 989 |
|
990 |
fun test_goal ({size, iterations, default_type}, tvinsts) i st = |
|
991 |
let |
|
16458 | 992 |
val thy = Toplevel.theory_of st; |
14105 | 993 |
fun strip (Const ("all", _) $ Abs (_, _, t)) = strip t |
994 |
| strip t = t; |
|
995 |
val (gi, frees) = Logic.goal_params |
|
996 |
(prop_of (snd (snd (Proof.get_goal (Toplevel.proof_of st))))) i; |
|
16458 | 997 |
val gi' = ObjectLogic.atomize_term thy (map_term_types |
17521 | 998 |
(map_type_tfree (fn p as (s, _) => getOpt (AList.lookup (op =) tvinsts s, |
999 |
getOpt (default_type, TFree p)))) (subst_bounds (frees, strip gi))); |
|
14105 | 1000 |
in case test_term (Toplevel.theory_of st) size iterations gi' of |
15531 | 1001 |
NONE => writeln "No counterexamples found." |
1002 |
| SOME cex => writeln ("Counterexample found:\n" ^ |
|
14105 | 1003 |
Pretty.string_of (Pretty.chunks (map (fn (s, t) => |
1004 |
Pretty.block [Pretty.str (s ^ " ="), Pretty.brk 1, |
|
16458 | 1005 |
Sign.pretty_term thy t]) cex))) |
14105 | 1006 |
end; |
1007 |
||
1008 |
||
17549 | 1009 |
(**** Evaluator for terms ****) |
1010 |
||
1011 |
val eval_result = ref (Bound 0); |
|
1012 |
||
1013 |
fun eval_term thy = setmp print_mode [] (fn t => |
|
1014 |
let |
|
1015 |
val _ = assert (null (term_tvars t) andalso null (term_tfrees t)) |
|
1016 |
"Term to be evaluated contains type variables"; |
|
1017 |
val _ = assert (null (term_vars t) andalso null (term_frees t)) |
|
1018 |
"Term to be evaluated contains variables"; |
|
1019 |
val (code, gr) = setmp mode ["term_of"] |
|
1020 |
(generate_code_i thy [] "Generated") [("result", t)]; |
|
1021 |
val s = "structure EvalTerm =\nstruct\n\n" ^ |
|
1022 |
space_implode "\n" (map snd code) ^ |
|
1023 |
"\nopen Generated;\n\n" ^ Pretty.string_of |
|
1024 |
(Pretty.block [Pretty.str "val () = Codegen.eval_result :=", |
|
1025 |
Pretty.brk 1, |
|
1026 |
mk_app false (mk_term_of gr "Generated" false (fastype_of t)) |
|
1027 |
[Pretty.str "result"], |
|
1028 |
Pretty.str ";"]) ^ |
|
1029 |
"\n\nend;\n"; |
|
1030 |
val _ = use_text Context.ml_output false s |
|
1031 |
in !eval_result end); |
|
1032 |
||
1033 |
fun print_evaluated_term s = Toplevel.keep (fn state => |
|
1034 |
let |
|
1035 |
val state' = Toplevel.enter_forward_proof state; |
|
1036 |
val ctxt = Proof.context_of state'; |
|
1037 |
val t = eval_term (Proof.theory_of state') (ProofContext.read_term ctxt s); |
|
1038 |
val T = Term.type_of t; |
|
1039 |
in |
|
1040 |
writeln (Pretty.string_of |
|
1041 |
(Pretty.block [Pretty.quote (ProofContext.pretty_term ctxt t), Pretty.fbrk, |
|
1042 |
Pretty.str "::", Pretty.brk 1, Pretty.quote (ProofContext.pretty_typ ctxt T)])) |
|
1043 |
end); |
|
1044 |
||
1045 |
||
12452 | 1046 |
(**** Interface ****) |
1047 |
||
13731
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
1048 |
val str = setmp print_mode [] Pretty.str; |
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
1049 |
|
11520 | 1050 |
fun parse_mixfix rd s = |
1051 |
(case Scan.finite Symbol.stopper (Scan.repeat |
|
1052 |
( $$ "_" >> K Arg |
|
1053 |
|| $$ "?" >> K Ignore |
|
16769
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
1054 |
|| $$ "\\<module>" >> K Module |
11520 | 1055 |
|| $$ "/" |-- Scan.repeat ($$ " ") >> (Pretty o Pretty.brk o length) |
1056 |
|| $$ "{" |-- $$ "*" |-- Scan.repeat1 |
|
1057 |
( $$ "'" |-- Scan.one Symbol.not_eof |
|
1058 |
|| Scan.unless ($$ "*" -- $$ "}") (Scan.one Symbol.not_eof)) --| |
|
12452 | 1059 |
$$ "*" --| $$ "}" >> (Quote o rd o implode) |
11520 | 1060 |
|| Scan.repeat1 |
1061 |
( $$ "'" |-- Scan.one Symbol.not_eof |
|
16769
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
1062 |
|| Scan.unless ($$ "_" || $$ "?" || $$ "\\<module>" || $$ "/" || $$ "{" |-- $$ "*") |
13731
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
1063 |
(Scan.one Symbol.not_eof)) >> (Pretty o str o implode))) |
11520 | 1064 |
(Symbol.explode s) of |
1065 |
(p, []) => p |
|
1066 |
| _ => error ("Malformed annotation: " ^ quote s)); |
|
1067 |
||
15801 | 1068 |
val _ = Context.add_setup |
16769
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
1069 |
[assoc_types [("fun", (parse_mixfix (K dummyT) "(_ ->/ _)", |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
1070 |
[("term_of", |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
1071 |
"fun term_of_fun_type _ T _ U _ = Free (\"<function>\", T --> U);\n"), |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
1072 |
("test", |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
1073 |
"fun gen_fun_type _ G i =\n\ |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
1074 |
\ let\n\ |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
1075 |
\ val f = ref (fn x => raise ERROR);\n\ |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
1076 |
\ val _ = (f := (fn x =>\n\ |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
1077 |
\ let\n\ |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
1078 |
\ val y = G i;\n\ |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
1079 |
\ val f' = !f\n\ |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
1080 |
\ in (f := (fn x' => if x = x' then y else f' x'); y) end))\n\ |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
1081 |
\ in (fn x => !f x) end;\n")]))]]; |
15801 | 1082 |
|
1083 |
||
17057 | 1084 |
structure P = OuterParse and K = OuterKeyword; |
11520 | 1085 |
|
17144 | 1086 |
fun strip_whitespace s = implode (fst (take_suffix (equal "\n" orf equal " ") |
1087 |
(snd (take_prefix (equal "\n" orf equal " ") (explode s))))) ^ "\n"; |
|
16769
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
1088 |
|
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
1089 |
val parse_attach = Scan.repeat (P.$$$ "attach" |-- |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
1090 |
Scan.optional (P.$$$ "(" |-- P.xname --| P.$$$ ")") "" -- |
17144 | 1091 |
(P.verbatim >> strip_whitespace)); |
16769
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
1092 |
|
11520 | 1093 |
val assoc_typeP = |
1094 |
OuterSyntax.command "types_code" |
|
11546 | 1095 |
"associate types with target language types" K.thy_decl |
16769
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
1096 |
(Scan.repeat1 (P.xname --| P.$$$ "(" -- P.string --| P.$$$ ")" -- parse_attach) >> |
12452 | 1097 |
(fn xs => Toplevel.theory (fn thy => assoc_types |
16769
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
1098 |
(map (fn ((name, mfx), aux) => (name, (parse_mixfix |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
1099 |
(typ_of o read_ctyp thy) mfx, aux))) xs) thy))); |
11520 | 1100 |
|
1101 |
val assoc_constP = |
|
1102 |
OuterSyntax.command "consts_code" |
|
11546 | 1103 |
"associate constants with target language code" K.thy_decl |
11520 | 1104 |
(Scan.repeat1 |
13003 | 1105 |
(P.xname -- (Scan.option (P.$$$ "::" |-- P.typ)) --| |
16769
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
1106 |
P.$$$ "(" -- P.string --| P.$$$ ")" -- parse_attach) >> |
11520 | 1107 |
(fn xs => Toplevel.theory (fn thy => assoc_consts |
16769
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
1108 |
(map (fn (((name, optype), mfx), aux) => (name, optype, (parse_mixfix |
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
1109 |
(term_of o read_cterm thy o rpair TypeInfer.logicT) mfx, aux))) |
11520 | 1110 |
xs) thy))); |
1111 |
||
17144 | 1112 |
fun parse_code lib = |
1113 |
Scan.optional (P.$$$ "(" |-- P.enum "," P.xname --| P.$$$ ")") (!mode) -- |
|
1114 |
(if lib then Scan.optional P.name "" else P.name) -- |
|
1115 |
Scan.option (P.$$$ "file" |-- P.name) -- |
|
1116 |
(if lib then Scan.succeed [] |
|
1117 |
else Scan.optional (P.$$$ "imports" |-- Scan.repeat1 P.name) []) --| |
|
1118 |
P.$$$ "contains" -- |
|
1119 |
( Scan.repeat1 (P.name --| P.$$$ "=" -- P.term) |
|
1120 |
|| Scan.repeat1 (P.term >> pair "")) >> |
|
1121 |
(fn ((((mode', module), opt_fname), modules), xs) => Toplevel.theory (fn thy => |
|
1122 |
let |
|
1123 |
val mode'' = if lib then "library" ins (mode' \ "library") |
|
1124 |
else mode' \ "library"; |
|
1125 |
val (code, gr) = setmp mode mode'' (generate_code thy modules module) xs |
|
1126 |
in ((case opt_fname of |
|
1127 |
NONE => use_text Context.ml_output false |
|
1128 |
(space_implode "\n" (map snd code)) |
|
1129 |
| SOME fname => |
|
1130 |
if lib then |
|
1131 |
app (fn (name, s) => File.write |
|
1132 |
(Path.append (Path.unpack fname) (Path.basic (name ^ ".ML"))) s) |
|
1133 |
(("ROOT", implode (map (fn (name, _) => |
|
1134 |
"use \"" ^ name ^ ".ML\";\n") code)) :: code) |
|
1135 |
else File.write (Path.unpack fname) (snd (hd code))); |
|
1136 |
if lib then thy |
|
17412 | 1137 |
else map_modules (Symtab.update (module, gr)) thy) |
17144 | 1138 |
end)); |
1139 |
||
1140 |
val code_libraryP = |
|
1141 |
OuterSyntax.command "code_library" |
|
1142 |
"generates code for terms (one structure for each theory)" K.thy_decl |
|
1143 |
(parse_code true); |
|
1144 |
||
1145 |
val code_moduleP = |
|
1146 |
OuterSyntax.command "code_module" |
|
1147 |
"generates code for terms (single structure, incremental)" K.thy_decl |
|
1148 |
(parse_code false); |
|
11520 | 1149 |
|
14105 | 1150 |
val params = |
1151 |
[("size", P.nat >> (K o set_size)), |
|
1152 |
("iterations", P.nat >> (K o set_iterations)), |
|
1153 |
("default_type", P.typ >> set_default_type)]; |
|
1154 |
||
1155 |
val parse_test_params = P.short_ident :-- (fn s => |
|
17521 | 1156 |
P.$$$ "=" |-- (AList.lookup (op =) params s |> the_default Scan.fail)) >> snd; |
14105 | 1157 |
|
1158 |
fun parse_tyinst xs = |
|
16458 | 1159 |
(P.type_ident --| P.$$$ "=" -- P.typ >> (fn (v, s) => fn thy => |
1160 |
fn (x, ys) => (x, (v, typ_of (read_ctyp thy s)) :: ys))) xs; |
|
14105 | 1161 |
|
1162 |
fun app [] x = x |
|
1163 |
| app (f :: fs) x = app fs (f x); |
|
1164 |
||
1165 |
val test_paramsP = |
|
1166 |
OuterSyntax.command "quickcheck_params" "set parameters for random testing" K.thy_decl |
|
1167 |
(P.$$$ "[" |-- P.list1 parse_test_params --| P.$$$ "]" >> |
|
1168 |
(fn fs => Toplevel.theory (fn thy => |
|
16649 | 1169 |
map_test_params (app (map (fn f => f thy) fs)) thy))); |
14105 | 1170 |
|
1171 |
val testP = |
|
1172 |
OuterSyntax.command "quickcheck" "try to find counterexample for subgoal" K.diag |
|
1173 |
(Scan.option (P.$$$ "[" |-- P.list1 |
|
16458 | 1174 |
( parse_test_params >> (fn f => fn thy => apfst (f thy)) |
14105 | 1175 |
|| parse_tyinst) --| P.$$$ "]") -- Scan.optional P.nat 1 >> |
1176 |
(fn (ps, g) => Toplevel.keep (fn st => |
|
15570 | 1177 |
test_goal (app (getOpt (Option.map |
1178 |
(map (fn f => f (Toplevel.sign_of st))) ps, [])) |
|
14105 | 1179 |
(get_test_params (Toplevel.theory_of st), [])) g st))); |
1180 |
||
17549 | 1181 |
val valueP = |
1182 |
OuterSyntax.improper_command "value" "read, evaluate and print term" K.diag |
|
1183 |
(P.term >> (Toplevel.no_timing oo print_evaluated_term)); |
|
1184 |
||
17144 | 1185 |
val _ = OuterSyntax.add_keywords ["attach", "file", "contains"]; |
16769
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
berghofe
parents:
16649
diff
changeset
|
1186 |
|
15801 | 1187 |
val _ = OuterSyntax.add_parsers |
17549 | 1188 |
[assoc_typeP, assoc_constP, code_libraryP, code_moduleP, test_paramsP, testP, valueP]; |
11520 | 1189 |
|
1190 |
end; |