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