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