author | berghofe |
Fri, 10 Dec 2004 16:55:58 +0100 | |
changeset 15398 | 055c01162eaa |
parent 15326 | ff21cddee442 |
child 15531 | 08c8dad8e399 |
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 |
|
18 |
| Pretty of Pretty.T |
|
12452 | 19 |
| Quote of 'a; |
11520 | 20 |
|
12452 | 21 |
type 'a codegen |
22 |
||
23 |
val add_codegen: string -> term codegen -> theory -> theory |
|
24 |
val add_tycodegen: string -> typ codegen -> theory -> theory |
|
14197 | 25 |
val add_attribute: string -> (Args.T list -> theory attribute * Args.T list) -> theory -> theory |
15261 | 26 |
val add_preprocessor: (theory -> thm list -> thm list) -> theory -> theory |
27 |
val preprocess: theory -> thm list -> thm list |
|
11520 | 28 |
val print_codegens: theory -> unit |
29 |
val generate_code: theory -> (string * string) list -> string |
|
30 |
val generate_code_i: theory -> (string * term) list -> string |
|
12452 | 31 |
val assoc_consts: (xstring * string option * term mixfix list) list -> theory -> theory |
32 |
val assoc_consts_i: (xstring * typ option * term mixfix list) list -> theory -> theory |
|
33 |
val assoc_types: (xstring * typ mixfix list) list -> theory -> theory |
|
34 |
val get_assoc_code: theory -> string -> typ -> term mixfix list option |
|
35 |
val get_assoc_type: theory -> string -> typ mixfix list option |
|
36 |
val invoke_codegen: theory -> string -> bool -> |
|
37 |
(exn option * string) Graph.T * term -> (exn option * string) Graph.T * Pretty.T |
|
38 |
val invoke_tycodegen: theory -> string -> bool -> |
|
39 |
(exn option * string) Graph.T * typ -> (exn option * string) Graph.T * Pretty.T |
|
14858 | 40 |
val mk_id: string -> string |
11520 | 41 |
val mk_const_id: Sign.sg -> string -> string |
42 |
val mk_type_id: Sign.sg -> string -> string |
|
43 |
val rename_term: term -> term |
|
15398 | 44 |
val new_names: term -> string list -> string list |
45 |
val new_name: term -> string -> string |
|
11520 | 46 |
val get_defn: theory -> string -> typ -> ((term list * term) * int option) option |
47 |
val is_instance: theory -> typ -> typ -> bool |
|
48 |
val parens: Pretty.T -> Pretty.T |
|
49 |
val mk_app: bool -> Pretty.T -> Pretty.T list -> Pretty.T |
|
50 |
val eta_expand: term -> term list -> int -> term |
|
14105 | 51 |
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
|
52 |
val mk_type: bool -> typ -> Pretty.T |
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
53 |
val mk_term_of: Sign.sg -> bool -> typ -> Pretty.T |
14105 | 54 |
val mk_gen: Sign.sg -> bool -> string list -> string -> typ -> Pretty.T |
55 |
val test_fn: (int -> (string * term) list option) ref |
|
56 |
val test_term: theory -> int -> int -> term -> (string * term) list option |
|
12452 | 57 |
val parse_mixfix: (string -> 'a) -> string -> 'a mixfix list |
11520 | 58 |
val parsers: OuterSyntax.parser list |
59 |
val setup: (theory -> theory) list |
|
60 |
end; |
|
61 |
||
62 |
structure Codegen : CODEGEN = |
|
63 |
struct |
|
64 |
||
65 |
val quiet_mode = ref true; |
|
66 |
fun message s = if !quiet_mode then () else writeln s; |
|
67 |
||
13753
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
68 |
val mode = ref ([] : string list); |
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
69 |
|
13886
0b243f6e257e
Margin for pretty-printing is now a mutable reference.
berghofe
parents:
13753
diff
changeset
|
70 |
val margin = ref 80; |
0b243f6e257e
Margin for pretty-printing is now a mutable reference.
berghofe
parents:
13753
diff
changeset
|
71 |
|
11520 | 72 |
(**** Mixfix syntax ****) |
73 |
||
12452 | 74 |
datatype 'a mixfix = |
11520 | 75 |
Arg |
76 |
| Ignore |
|
77 |
| Pretty of Pretty.T |
|
12452 | 78 |
| Quote of 'a; |
11520 | 79 |
|
80 |
fun is_arg Arg = true |
|
81 |
| is_arg Ignore = true |
|
82 |
| is_arg _ = false; |
|
83 |
||
12452 | 84 |
fun quotes_of [] = [] |
85 |
| quotes_of (Quote q :: ms) = q :: quotes_of ms |
|
86 |
| quotes_of (_ :: ms) = quotes_of ms; |
|
87 |
||
88 |
fun args_of [] xs = ([], xs) |
|
89 |
| args_of (Arg :: ms) (x :: xs) = apfst (cons x) (args_of ms xs) |
|
90 |
| args_of (Ignore :: ms) (_ :: xs) = args_of ms xs |
|
91 |
| args_of (_ :: ms) xs = args_of ms xs; |
|
11520 | 92 |
|
12490 | 93 |
fun num_args x = length (filter is_arg x); |
11520 | 94 |
|
95 |
||
96 |
(**** theory data ****) |
|
97 |
||
14105 | 98 |
(* type of code generators *) |
11520 | 99 |
|
12452 | 100 |
type 'a codegen = theory -> (exn option * string) Graph.T -> |
101 |
string -> bool -> 'a -> ((exn option * string) Graph.T * Pretty.T) option; |
|
102 |
||
14105 | 103 |
(* parameters for random testing *) |
104 |
||
105 |
type test_params = |
|
106 |
{size: int, iterations: int, default_type: typ option}; |
|
107 |
||
108 |
fun merge_test_params |
|
109 |
{size = size1, iterations = iterations1, default_type = default_type1} |
|
110 |
{size = size2, iterations = iterations2, default_type = default_type2} = |
|
111 |
{size = Int.max (size1, size2), |
|
112 |
iterations = Int.max (iterations1, iterations2), |
|
113 |
default_type = case default_type1 of |
|
114 |
None => default_type2 |
|
115 |
| _ => default_type1}; |
|
116 |
||
117 |
val default_test_params : test_params = |
|
118 |
{size = 10, iterations = 100, default_type = None}; |
|
119 |
||
120 |
fun set_size size ({iterations, default_type, ...} : test_params) = |
|
121 |
{size = size, iterations = iterations, default_type = default_type}; |
|
122 |
||
123 |
fun set_iterations iterations ({size, default_type, ...} : test_params) = |
|
124 |
{size = size, iterations = iterations, default_type = default_type}; |
|
125 |
||
126 |
fun set_default_type s sg ({size, iterations, ...} : test_params) = |
|
127 |
{size = size, iterations = iterations, |
|
128 |
default_type = Some (typ_of (read_ctyp sg s))}; |
|
129 |
||
130 |
(* data kind 'Pure/codegen' *) |
|
131 |
||
11520 | 132 |
structure CodegenArgs = |
133 |
struct |
|
134 |
val name = "Pure/codegen"; |
|
135 |
type T = |
|
12452 | 136 |
{codegens : (string * term codegen) list, |
137 |
tycodegens : (string * typ codegen) list, |
|
138 |
consts : ((string * typ) * term mixfix list) list, |
|
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
139 |
types : (string * typ mixfix list) list, |
14197 | 140 |
attrs: (string * (Args.T list -> theory attribute * Args.T list)) list, |
15261 | 141 |
preprocs: (stamp * (theory -> thm list -> thm list)) list, |
14105 | 142 |
test_params: test_params}; |
11520 | 143 |
|
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
144 |
val empty = |
14105 | 145 |
{codegens = [], tycodegens = [], consts = [], types = [], attrs = [], |
15261 | 146 |
preprocs = [], test_params = default_test_params}; |
11520 | 147 |
val copy = I; |
148 |
val prep_ext = I; |
|
149 |
||
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
150 |
fun merge |
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
151 |
({codegens = codegens1, tycodegens = tycodegens1, |
14105 | 152 |
consts = consts1, types = types1, attrs = attrs1, |
15261 | 153 |
preprocs = preprocs1, test_params = test_params1}, |
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
154 |
{codegens = codegens2, tycodegens = tycodegens2, |
14105 | 155 |
consts = consts2, types = types2, attrs = attrs2, |
15261 | 156 |
preprocs = preprocs2, test_params = test_params2}) = |
157 |
{codegens = merge_alists' codegens1 codegens2, |
|
158 |
tycodegens = merge_alists' tycodegens1 tycodegens2, |
|
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
159 |
consts = merge_alists consts1 consts2, |
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
160 |
types = merge_alists types1 types2, |
14105 | 161 |
attrs = merge_alists attrs1 attrs2, |
15261 | 162 |
preprocs = merge_alists' preprocs1 preprocs2, |
14105 | 163 |
test_params = merge_test_params test_params1 test_params2}; |
11520 | 164 |
|
12452 | 165 |
fun print sg ({codegens, tycodegens, ...} : T) = |
166 |
Pretty.writeln (Pretty.chunks |
|
167 |
[Pretty.strs ("term code generators:" :: map fst codegens), |
|
168 |
Pretty.strs ("type code generators:" :: map fst tycodegens)]); |
|
11520 | 169 |
end; |
170 |
||
171 |
structure CodegenData = TheoryDataFun(CodegenArgs); |
|
172 |
val print_codegens = CodegenData.print; |
|
173 |
||
174 |
||
14105 | 175 |
(**** access parameters for random testing ****) |
176 |
||
177 |
fun get_test_params thy = #test_params (CodegenData.get thy); |
|
178 |
||
179 |
fun map_test_params f thy = |
|
15261 | 180 |
let val {codegens, tycodegens, consts, types, attrs, preprocs, test_params} = |
14105 | 181 |
CodegenData.get thy; |
182 |
in CodegenData.put {codegens = codegens, tycodegens = tycodegens, |
|
15261 | 183 |
consts = consts, types = types, attrs = attrs, preprocs = preprocs, |
14105 | 184 |
test_params = f test_params} thy |
185 |
end; |
|
186 |
||
187 |
||
12452 | 188 |
(**** add new code generators to theory ****) |
11520 | 189 |
|
190 |
fun add_codegen name f thy = |
|
15261 | 191 |
let val {codegens, tycodegens, consts, types, attrs, preprocs, test_params} = |
14105 | 192 |
CodegenData.get thy |
11520 | 193 |
in (case assoc (codegens, name) of |
12452 | 194 |
None => CodegenData.put {codegens = (name, f) :: codegens, |
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
195 |
tycodegens = tycodegens, consts = consts, types = types, |
15261 | 196 |
attrs = attrs, preprocs = preprocs, test_params = test_params} thy |
12452 | 197 |
| Some _ => error ("Code generator " ^ name ^ " already declared")) |
198 |
end; |
|
199 |
||
200 |
fun add_tycodegen name f thy = |
|
15261 | 201 |
let val {codegens, tycodegens, consts, types, attrs, preprocs, test_params} = |
14105 | 202 |
CodegenData.get thy |
12452 | 203 |
in (case assoc (tycodegens, name) of |
204 |
None => CodegenData.put {tycodegens = (name, f) :: tycodegens, |
|
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
205 |
codegens = codegens, consts = consts, types = types, |
15261 | 206 |
attrs = attrs, preprocs = preprocs, test_params = test_params} thy |
11520 | 207 |
| Some _ => error ("Code generator " ^ name ^ " already declared")) |
208 |
end; |
|
209 |
||
210 |
||
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
211 |
(**** code attribute ****) |
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
212 |
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
213 |
fun add_attribute name att thy = |
15261 | 214 |
let val {codegens, tycodegens, consts, types, attrs, preprocs, test_params} = |
14105 | 215 |
CodegenData.get thy |
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
216 |
in (case assoc (attrs, name) of |
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
217 |
None => CodegenData.put {tycodegens = tycodegens, |
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
218 |
codegens = codegens, consts = consts, types = types, |
15261 | 219 |
attrs = if name = "" then attrs @ [(name, att)] else (name, att) :: attrs, |
220 |
preprocs = preprocs, |
|
221 |
test_params = test_params} thy |
|
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
222 |
| Some _ => error ("Code attribute " ^ name ^ " already declared")) |
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
223 |
end; |
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
224 |
|
14197 | 225 |
fun mk_parser (a, p) = (if a = "" then Scan.succeed "" else Args.$$$ a) |-- p; |
226 |
||
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
227 |
val code_attr = |
14197 | 228 |
Attrib.syntax (Scan.depend (fn thy => foldr op || (map mk_parser |
229 |
(#attrs (CodegenData.get thy)), Scan.fail) >> pair thy)); |
|
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
230 |
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
231 |
|
15261 | 232 |
(**** preprocessors ****) |
233 |
||
234 |
fun add_preprocessor p thy = |
|
235 |
let val {codegens, tycodegens, consts, types, attrs, preprocs, test_params} = |
|
236 |
CodegenData.get thy |
|
237 |
in CodegenData.put {tycodegens = tycodegens, |
|
238 |
codegens = codegens, consts = consts, types = types, |
|
239 |
attrs = attrs, preprocs = (stamp (), p) :: preprocs, |
|
240 |
test_params = test_params} thy |
|
241 |
end; |
|
242 |
||
243 |
fun preprocess thy ths = |
|
244 |
let val {preprocs, ...} = CodegenData.get thy |
|
245 |
in foldl (fn (ths, (_, f)) => f thy ths) (ths, preprocs) end; |
|
246 |
||
247 |
fun unfold_attr (thy, eqn) = |
|
248 |
let |
|
249 |
val (name, _) = dest_Const (head_of |
|
250 |
(fst (Logic.dest_equals (prop_of eqn)))); |
|
251 |
fun prep thy = map (fn th => |
|
252 |
if name mem term_consts (prop_of th) then |
|
15398 | 253 |
rewrite_rule [eqn] (Thm.transfer thy th) |
15261 | 254 |
else th) |
255 |
in (add_preprocessor prep thy, eqn) end; |
|
256 |
||
257 |
||
11520 | 258 |
(**** associate constants with target language code ****) |
259 |
||
260 |
fun gen_assoc_consts prep_type xs thy = foldl (fn (thy, (s, tyopt, syn)) => |
|
261 |
let |
|
262 |
val sg = sign_of thy; |
|
15261 | 263 |
val {codegens, tycodegens, consts, types, attrs, preprocs, test_params} = |
14105 | 264 |
CodegenData.get thy; |
11520 | 265 |
val cname = Sign.intern_const sg s; |
266 |
in |
|
267 |
(case Sign.const_type sg cname of |
|
268 |
Some T => |
|
269 |
let val T' = (case tyopt of |
|
270 |
None => T |
|
271 |
| Some ty => |
|
272 |
let val U = prep_type sg ty |
|
14769 | 273 |
in if Sign.typ_instance sg (U, T) then U |
11520 | 274 |
else error ("Illegal type constraint for constant " ^ cname) |
275 |
end) |
|
276 |
in (case assoc (consts, (cname, T')) of |
|
277 |
None => CodegenData.put {codegens = codegens, |
|
12452 | 278 |
tycodegens = tycodegens, |
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
279 |
consts = ((cname, T'), syn) :: consts, |
15261 | 280 |
types = types, attrs = attrs, preprocs = preprocs, |
281 |
test_params = test_params} thy |
|
11520 | 282 |
| Some _ => error ("Constant " ^ cname ^ " already associated with code")) |
283 |
end |
|
284 |
| _ => error ("Not a constant: " ^ s)) |
|
285 |
end) (thy, xs); |
|
286 |
||
287 |
val assoc_consts_i = gen_assoc_consts (K I); |
|
288 |
val assoc_consts = gen_assoc_consts (fn sg => typ_of o read_ctyp sg); |
|
289 |
||
290 |
(**** associate types with target language types ****) |
|
291 |
||
292 |
fun assoc_types xs thy = foldl (fn (thy, (s, syn)) => |
|
293 |
let |
|
15261 | 294 |
val {codegens, tycodegens, consts, types, attrs, preprocs, test_params} = |
14105 | 295 |
CodegenData.get thy; |
11520 | 296 |
val tc = Sign.intern_tycon (sign_of thy) s |
297 |
in |
|
298 |
(case assoc (types, tc) of |
|
12452 | 299 |
None => CodegenData.put {codegens = codegens, |
300 |
tycodegens = tycodegens, consts = consts, |
|
14105 | 301 |
types = (tc, syn) :: types, attrs = attrs, |
15261 | 302 |
preprocs = preprocs, test_params = test_params} thy |
11520 | 303 |
| Some _ => error ("Type " ^ tc ^ " already associated with code")) |
304 |
end) (thy, xs); |
|
305 |
||
12452 | 306 |
fun get_assoc_type thy s = assoc (#types (CodegenData.get thy), s); |
11546 | 307 |
|
11520 | 308 |
|
309 |
(**** make valid ML identifiers ****) |
|
310 |
||
14858 | 311 |
fun is_ascii_letdig x = Symbol.is_ascii_letter x orelse |
312 |
Symbol.is_ascii_digit x orelse Symbol.is_ascii_quasi x; |
|
313 |
||
314 |
fun dest_sym s = (case split_last (snd (take_prefix (equal "\\") (explode s))) of |
|
315 |
("<" :: "^" :: xs, ">") => (true, implode xs) |
|
316 |
| ("<" :: xs, ">") => (false, implode xs) |
|
317 |
| _ => sys_error "dest_sym"); |
|
318 |
||
319 |
fun mk_id s = if s = "" then "" else |
|
11520 | 320 |
let |
14858 | 321 |
fun check_str [] = [] |
322 |
| check_str xs = (case take_prefix is_ascii_letdig xs of |
|
323 |
([], " " :: zs) => check_str zs |
|
324 |
| ([], z :: zs) => |
|
325 |
if size z = 1 then string_of_int (ord z) :: check_str zs |
|
326 |
else (case dest_sym z of |
|
327 |
(true, "isub") => check_str zs |
|
328 |
| (true, "isup") => "" :: check_str zs |
|
329 |
| (ctrl, s') => (if ctrl then "ctrl_" ^ s' else s') :: check_str zs) |
|
330 |
| (ys, zs) => implode ys :: check_str zs); |
|
331 |
val s' = space_implode "_" |
|
332 |
(flat (map (check_str o Symbol.explode) (NameSpace.unpack s))) |
|
11520 | 333 |
in |
14858 | 334 |
if Symbol.is_ascii_letter (hd (explode s')) then s' else "id_" ^ s' |
11520 | 335 |
end; |
336 |
||
14858 | 337 |
fun mk_const_id sg s = |
338 |
let val s' = mk_id (Sign.cond_extern sg Sign.constK s) |
|
339 |
in if s' mem ThmDatabase.ml_reserved then s' ^ "_const" else s' end; |
|
13073
cc9d7f403a4b
mk_const_id now checks for clashes with reserved ML identifiers.
berghofe
parents:
13003
diff
changeset
|
340 |
|
14858 | 341 |
fun mk_type_id sg s = |
342 |
let val s' = mk_id (Sign.cond_extern sg Sign.typeK s) |
|
343 |
in if s' mem ThmDatabase.ml_reserved then s' ^ "_type" else s' end; |
|
11520 | 344 |
|
13731
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
345 |
fun rename_terms ts = |
11520 | 346 |
let |
13731
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
347 |
val names = foldr add_term_names |
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
348 |
(ts, map (fst o fst) (Drule.vars_of_terms ts)); |
14858 | 349 |
val reserved = names inter ThmDatabase.ml_reserved; |
350 |
val (illegal, alt_names) = split_list (mapfilter (fn s => |
|
351 |
let val s' = mk_id s in if s = s' then None else Some (s, s') end) names) |
|
352 |
val ps = (reserved @ illegal) ~~ |
|
353 |
variantlist (map (suffix "'") reserved @ alt_names, names); |
|
11520 | 354 |
|
14858 | 355 |
fun rename_id s = if_none (assoc (ps, s)) s; |
356 |
||
357 |
fun rename (Var ((a, i), T)) = Var ((rename_id a, i), T) |
|
358 |
| rename (Free (a, T)) = Free (rename_id a, T) |
|
11520 | 359 |
| rename (Abs (s, T, t)) = Abs (s, T, rename t) |
360 |
| rename (t $ u) = rename t $ rename u |
|
361 |
| rename t = t; |
|
362 |
in |
|
13731
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
363 |
map rename ts |
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
364 |
end; |
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
365 |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
366 |
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
|
367 |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
368 |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
369 |
(**** retrieve definition of constant ****) |
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
370 |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
371 |
fun is_instance thy T1 T2 = |
14769 | 372 |
Sign.typ_instance (sign_of thy) (T1, Type.varifyT T2); |
13731
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
373 |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
374 |
fun get_assoc_code thy s T = apsome snd (find_first (fn ((s', T'), _) => |
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
375 |
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
|
376 |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
377 |
fun get_defn thy s T = |
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
378 |
let |
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
379 |
val axms = flat (map (Symtab.dest o #axioms o Theory.rep_theory) |
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
380 |
(thy :: Theory.ancestors_of thy)); |
15261 | 381 |
fun prep_def def = (case preprocess thy [def] of |
382 |
[def'] => prop_of def' | _ => error "get_defn: bad preprocessor"); |
|
383 |
fun dest t = |
|
384 |
let |
|
385 |
val (lhs, rhs) = Logic.dest_equals t; |
|
386 |
val (c, args) = strip_comb lhs; |
|
387 |
val (s', T') = dest_Const c |
|
388 |
in if s = s' then Some (T', (args, rhs)) else None |
|
389 |
end handle TERM _ => None; |
|
390 |
val defs = mapfilter (fn (name, t) => apsome (pair name) (dest t)) axms; |
|
391 |
val i = find_index (is_instance thy T o fst o snd) defs |
|
13731
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
392 |
in |
15261 | 393 |
if i >= 0 then |
394 |
let val (name, (T', (args, _))) = nth_elem (i, defs) |
|
395 |
in case dest (prep_def (Thm.get_axiom thy name)) of |
|
396 |
None => None |
|
397 |
| Some (T'', p as (args', rhs)) => |
|
398 |
if T' = T'' andalso args = args' then |
|
399 |
Some (split_last (rename_terms (args @ [rhs])), |
|
400 |
if length defs = 1 then None else Some i) |
|
401 |
else None |
|
402 |
end |
|
13731
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
403 |
else None |
11520 | 404 |
end; |
405 |
||
406 |
||
12452 | 407 |
(**** invoke suitable code generator for term / type ****) |
11520 | 408 |
|
12452 | 409 |
fun invoke_codegen thy dep brack (gr, t) = (case get_first |
11520 | 410 |
(fn (_, f) => f thy gr dep brack t) (#codegens (CodegenData.get thy)) of |
411 |
None => error ("Unable to generate code for term:\n" ^ |
|
412 |
Sign.string_of_term (sign_of thy) t ^ "\nrequired by:\n" ^ |
|
413 |
commas (Graph.all_succs gr [dep])) |
|
12452 | 414 |
| Some x => x); |
415 |
||
416 |
fun invoke_tycodegen thy dep brack (gr, T) = (case get_first |
|
417 |
(fn (_, f) => f thy gr dep brack T) (#tycodegens (CodegenData.get thy)) of |
|
418 |
None => error ("Unable to generate code for type:\n" ^ |
|
419 |
Sign.string_of_typ (sign_of thy) T ^ "\nrequired by:\n" ^ |
|
420 |
commas (Graph.all_succs gr [dep])) |
|
421 |
| Some x => x); |
|
11520 | 422 |
|
423 |
||
424 |
(**** code generator for mixfix expressions ****) |
|
425 |
||
426 |
fun parens p = Pretty.block [Pretty.str "(", p, Pretty.str ")"]; |
|
427 |
||
428 |
fun pretty_fn [] p = [p] |
|
429 |
| pretty_fn (x::xs) p = Pretty.str ("fn " ^ x ^ " =>") :: |
|
430 |
Pretty.brk 1 :: pretty_fn xs p; |
|
431 |
||
432 |
fun pretty_mixfix [] [] _ = [] |
|
433 |
| pretty_mixfix (Arg :: ms) (p :: ps) qs = p :: pretty_mixfix ms ps qs |
|
12452 | 434 |
| pretty_mixfix (Ignore :: ms) ps qs = pretty_mixfix ms ps qs |
11520 | 435 |
| pretty_mixfix (Pretty p :: ms) ps qs = p :: pretty_mixfix ms ps qs |
12452 | 436 |
| pretty_mixfix (Quote _ :: ms) ps (q :: qs) = q :: pretty_mixfix ms ps qs; |
11520 | 437 |
|
438 |
||
12452 | 439 |
(**** default code generators ****) |
11520 | 440 |
|
441 |
fun eta_expand t ts i = |
|
442 |
let |
|
443 |
val (Ts, _) = strip_type (fastype_of t); |
|
444 |
val j = i - length ts |
|
445 |
in |
|
446 |
foldr (fn (T, t) => Abs ("x", T, t)) |
|
447 |
(take (j, Ts), list_comb (t, ts @ map Bound (j-1 downto 0))) |
|
448 |
end; |
|
449 |
||
450 |
fun mk_app _ p [] = p |
|
451 |
| mk_app brack p ps = if brack then |
|
452 |
Pretty.block (Pretty.str "(" :: |
|
453 |
separate (Pretty.brk 1) (p :: ps) @ [Pretty.str ")"]) |
|
454 |
else Pretty.block (separate (Pretty.brk 1) (p :: ps)); |
|
455 |
||
14858 | 456 |
fun new_names t xs = variantlist (map mk_id xs, |
11520 | 457 |
map (fst o fst o dest_Var) (term_vars t) union |
458 |
add_term_names (t, ThmDatabase.ml_reserved)); |
|
459 |
||
460 |
fun new_name t x = hd (new_names t [x]); |
|
461 |
||
462 |
fun default_codegen thy gr dep brack t = |
|
463 |
let |
|
464 |
val (u, ts) = strip_comb t; |
|
12452 | 465 |
fun codegens brack = foldl_map (invoke_codegen thy dep brack) |
11520 | 466 |
in (case u of |
14105 | 467 |
Var ((s, i), T) => |
468 |
let |
|
469 |
val (gr', ps) = codegens true (gr, ts); |
|
470 |
val (gr'', _) = invoke_tycodegen thy dep false (gr', T) |
|
471 |
in Some (gr'', mk_app brack (Pretty.str (s ^ |
|
11520 | 472 |
(if i=0 then "" else string_of_int i))) ps) |
473 |
end |
|
474 |
||
14105 | 475 |
| Free (s, T) => |
476 |
let |
|
477 |
val (gr', ps) = codegens true (gr, ts); |
|
478 |
val (gr'', _) = invoke_tycodegen thy dep false (gr', T) |
|
479 |
in Some (gr'', mk_app brack (Pretty.str s) ps) end |
|
11520 | 480 |
|
481 |
| Const (s, T) => |
|
482 |
(case get_assoc_code thy s T of |
|
483 |
Some ms => |
|
484 |
let val i = num_args ms |
|
485 |
in if length ts < i then |
|
486 |
default_codegen thy gr dep brack (eta_expand u ts i) |
|
487 |
else |
|
488 |
let |
|
12452 | 489 |
val (ts1, ts2) = args_of ms ts; |
490 |
val (gr1, ps1) = codegens false (gr, ts1); |
|
491 |
val (gr2, ps2) = codegens true (gr1, ts2); |
|
492 |
val (gr3, ps3) = codegens false (gr2, quotes_of ms); |
|
11520 | 493 |
in |
494 |
Some (gr3, mk_app brack (Pretty.block (pretty_mixfix ms ps1 ps3)) ps2) |
|
495 |
end |
|
496 |
end |
|
497 |
| None => (case get_defn thy s T of |
|
498 |
None => None |
|
499 |
| Some ((args, rhs), k) => |
|
500 |
let |
|
501 |
val id = mk_const_id (sign_of thy) s ^ (case k of |
|
502 |
None => "" | Some i => "_def" ^ string_of_int i); |
|
12452 | 503 |
val (gr', ps) = codegens true (gr, ts); |
11520 | 504 |
in |
505 |
Some (Graph.add_edge (id, dep) gr' handle Graph.UNDEF _ => |
|
506 |
let |
|
507 |
val _ = message ("expanding definition of " ^ s); |
|
508 |
val (Ts, _) = strip_type T; |
|
509 |
val (args', rhs') = |
|
510 |
if not (null args) orelse null Ts then (args, rhs) else |
|
511 |
let val v = Free (new_name rhs "x", hd Ts) |
|
512 |
in ([v], betapply (rhs, v)) end; |
|
12452 | 513 |
val (gr1, p) = invoke_codegen thy id false |
514 |
(Graph.add_edge (id, dep) |
|
515 |
(Graph.new_node (id, (None, "")) gr'), rhs'); |
|
516 |
val (gr2, xs) = codegens false (gr1, args'); |
|
12580
7fdc00bb2a9e
Code generator now adds type constraints to val declarations (to make
berghofe
parents:
12555
diff
changeset
|
517 |
val (gr3, ty) = invoke_tycodegen thy id false (gr2, T); |
14980 | 518 |
in Graph.map_node id (K (None, Pretty.string_of (Pretty.block |
12580
7fdc00bb2a9e
Code generator now adds type constraints to val declarations (to make
berghofe
parents:
12555
diff
changeset
|
519 |
(separate (Pretty.brk 1) (if null args' then |
7fdc00bb2a9e
Code generator now adds type constraints to val declarations (to make
berghofe
parents:
12555
diff
changeset
|
520 |
[Pretty.str ("val " ^ id ^ " :"), ty] |
7fdc00bb2a9e
Code generator now adds type constraints to val declarations (to make
berghofe
parents:
12555
diff
changeset
|
521 |
else Pretty.str ("fun " ^ id) :: xs) @ |
14980 | 522 |
[Pretty.str " =", Pretty.brk 1, p, Pretty.str ";"])) ^ "\n\n")) gr3 |
11520 | 523 |
end, mk_app brack (Pretty.str id) ps) |
524 |
end)) |
|
525 |
||
526 |
| Abs _ => |
|
527 |
let |
|
528 |
val (bs, Ts) = ListPair.unzip (strip_abs_vars u); |
|
529 |
val t = strip_abs_body u |
|
530 |
val bs' = new_names t bs; |
|
12452 | 531 |
val (gr1, ps) = codegens true (gr, ts); |
532 |
val (gr2, p) = invoke_codegen thy dep false |
|
533 |
(gr1, subst_bounds (map Free (rev (bs' ~~ Ts)), t)); |
|
11520 | 534 |
in |
535 |
Some (gr2, mk_app brack (Pretty.block (Pretty.str "(" :: pretty_fn bs' p @ |
|
536 |
[Pretty.str ")"])) ps) |
|
537 |
end |
|
538 |
||
539 |
| _ => None) |
|
540 |
end; |
|
541 |
||
12452 | 542 |
fun default_tycodegen thy gr dep brack (TVar ((s, i), _)) = |
543 |
Some (gr, Pretty.str (s ^ (if i = 0 then "" else string_of_int i))) |
|
544 |
| default_tycodegen thy gr dep brack (TFree (s, _)) = Some (gr, Pretty.str s) |
|
545 |
| default_tycodegen thy gr dep brack (Type (s, Ts)) = |
|
546 |
(case assoc (#types (CodegenData.get thy), s) of |
|
547 |
None => None |
|
548 |
| Some ms => |
|
549 |
let |
|
550 |
val (gr', ps) = foldl_map |
|
551 |
(invoke_tycodegen thy dep false) (gr, fst (args_of ms Ts)); |
|
552 |
val (gr'', qs) = foldl_map |
|
553 |
(invoke_tycodegen thy dep false) (gr', quotes_of ms) |
|
554 |
in Some (gr'', Pretty.block (pretty_mixfix ms ps qs)) end); |
|
555 |
||
11520 | 556 |
|
557 |
fun output_code gr xs = implode (map (snd o Graph.get_node gr) |
|
558 |
(rev (Graph.all_preds gr xs))); |
|
559 |
||
14598
7009f59711e3
Replaced quote by Library.quote, since quote now refers to Symbol.quote
berghofe
parents:
14197
diff
changeset
|
560 |
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
|
561 |
setmp print_mode [] (Pretty.setmp_margin (!margin) (fn xs => |
11520 | 562 |
let |
563 |
val sg = sign_of thy; |
|
564 |
val gr = Graph.new_node ("<Top>", (None, "")) Graph.empty; |
|
565 |
val (gr', ps) = foldl_map (fn (gr, (s, t)) => apsnd (pair s) |
|
12452 | 566 |
(invoke_codegen thy "<Top>" false (gr, t))) |
567 |
(gr, map (apsnd (prep_term sg)) xs) |
|
14818 | 568 |
val code = |
569 |
"structure Generated =\nstruct\n\n" ^ |
|
570 |
output_code gr' ["<Top>"] ^ |
|
571 |
space_implode "\n\n" (map (fn (s', p) => Pretty.string_of (Pretty.block |
|
572 |
[Pretty.str ("val " ^ s' ^ " ="), Pretty.brk 1, p, Pretty.str ";"])) ps) ^ |
|
573 |
"\n\nend;\n\nopen Generated;\n"; |
|
14980 | 574 |
in code end)); |
11520 | 575 |
|
576 |
val generate_code_i = gen_generate_code (K I); |
|
577 |
val generate_code = gen_generate_code |
|
578 |
(fn sg => term_of o read_cterm sg o rpair TypeInfer.logicT); |
|
579 |
||
12452 | 580 |
|
13753
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
581 |
(**** Reflection ****) |
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
582 |
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
583 |
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
|
584 |
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
585 |
fun pretty_list xs = Pretty.block (Pretty.str "[" :: |
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
586 |
flat (separate [Pretty.str ",", Pretty.brk 1] (map single xs)) @ |
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
587 |
[Pretty.str "]"]); |
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
588 |
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
589 |
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
|
590 |
(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
|
591 |
| 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
|
592 |
| 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
|
593 |
[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
|
594 |
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
|
595 |
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
596 |
fun mk_term_of sg 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
|
597 |
(strip_tname s ^ (if i = 0 then "" else string_of_int i) ^ "F") |
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
598 |
| mk_term_of sg p (TFree (s, _)) = Pretty.str (strip_tname s ^ "F") |
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
599 |
| mk_term_of sg 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
|
600 |
(separate (Pretty.brk 1) (Pretty.str ("term_of_" ^ mk_type_id sg s) :: |
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
601 |
flat (map (fn T => [mk_term_of sg true T, mk_type true T]) Ts)))); |
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
602 |
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
603 |
|
14105 | 604 |
(**** Test data generators ****) |
605 |
||
606 |
fun mk_gen sg p xs a (TVar ((s, i), _)) = Pretty.str |
|
607 |
(strip_tname s ^ (if i = 0 then "" else string_of_int i) ^ "G") |
|
608 |
| mk_gen sg p xs a (TFree (s, _)) = Pretty.str (strip_tname s ^ "G") |
|
609 |
| mk_gen sg p xs a (Type (s, Ts)) = (if p then parens else I) (Pretty.block |
|
610 |
(separate (Pretty.brk 1) (Pretty.str ("gen_" ^ mk_type_id sg s ^ |
|
611 |
(if s mem xs then "'" else "")) :: map (mk_gen sg true xs a) Ts @ |
|
612 |
(if s mem xs then [Pretty.str a] else [])))); |
|
613 |
||
614 |
val test_fn : (int -> (string * term) list option) ref = ref (fn _ => None); |
|
615 |
||
15029 | 616 |
fun test_term thy sz i = setmp print_mode [] (fn t => |
14105 | 617 |
let |
618 |
val _ = assert (null (term_tvars t) andalso null (term_tfrees t)) |
|
619 |
"Term to be tested contains type variables"; |
|
620 |
val _ = assert (null (term_vars t)) |
|
621 |
"Term to be tested contains schematic variables"; |
|
622 |
val sg = sign_of thy; |
|
623 |
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
|
624 |
val szname = variant (map fst frees) "i"; |
14105 | 625 |
val s = "structure TestTerm =\nstruct\n\n" ^ |
626 |
setmp mode ["term_of", "test"] (generate_code_i thy) |
|
627 |
[("testf", list_abs_free (frees, t))] ^ |
|
14980 | 628 |
"\n" ^ Pretty.string_of |
14105 | 629 |
(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
|
630 |
Pretty.brk 1, Pretty.str ("(fn " ^ szname ^ " =>"), Pretty.brk 1, |
14105 | 631 |
Pretty.blk (0, [Pretty.str "let", Pretty.brk 1, |
632 |
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
|
633 |
Pretty.block [Pretty.str ("val " ^ mk_id s ^ " ="), Pretty.brk 1, |
14105 | 634 |
mk_gen sg 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
|
635 |
Pretty.str (szname ^ ";")]) frees)), |
14105 | 636 |
Pretty.brk 1, Pretty.str "in", Pretty.brk 1, |
637 |
Pretty.block [Pretty.str "if ", |
|
14886
b792081d2399
mk_id is now also applied to identifiers in test_term.
berghofe
parents:
14858
diff
changeset
|
638 |
mk_app false (Pretty.str "testf") (map (Pretty.str o mk_id o fst) frees), |
14110
c45c94fa16f4
use Library.Some/None instead of just Some/None in generated quickcheck code
kleing
parents:
14105
diff
changeset
|
639 |
Pretty.brk 1, Pretty.str "then Library.None", |
14105 | 640 |
Pretty.brk 1, Pretty.str "else ", |
14110
c45c94fa16f4
use Library.Some/None instead of just Some/None in generated quickcheck code
kleing
parents:
14105
diff
changeset
|
641 |
Pretty.block [Pretty.str "Library.Some ", Pretty.block (Pretty.str "[" :: |
14105 | 642 |
flat (separate [Pretty.str ",", Pretty.brk 1] |
643 |
(map (fn (s, T) => [Pretty.block |
|
15326
ff21cddee442
Made test_term escape special characters in strings that caused the
berghofe
parents:
15261
diff
changeset
|
644 |
[Pretty.str ("(" ^ Library.quote (Symbol.escape s) ^ ","), Pretty.brk 1, |
14105 | 645 |
mk_app false (mk_term_of sg false T) |
14886
b792081d2399
mk_id is now also applied to identifiers in test_term.
berghofe
parents:
14858
diff
changeset
|
646 |
[Pretty.str (mk_id s)], Pretty.str ")"]]) frees)) @ |
14105 | 647 |
[Pretty.str "]"])]], |
14980 | 648 |
Pretty.brk 1, Pretty.str "end"]), Pretty.str ");"]) ^ |
14105 | 649 |
"\n\nend;\n"; |
650 |
val _ = use_text Context.ml_output false s; |
|
651 |
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
|
652 |
else (case (f () handle Match => |
f8a25218b423
test_term now handles Match exception raised in generated code.
berghofe
parents:
14110
diff
changeset
|
653 |
(warning "Exception Match raised in generated code"; None)) of |
f8a25218b423
test_term now handles Match exception raised in generated code.
berghofe
parents:
14110
diff
changeset
|
654 |
None => iter f (k+1) | Some x => Some x); |
14105 | 655 |
fun test k = if k > sz then None |
656 |
else (priority ("Test data size: " ^ string_of_int k); |
|
657 |
case iter (fn () => !test_fn k) 1 of |
|
658 |
None => test (k+1) | Some x => Some x); |
|
15029 | 659 |
in test 0 end); |
14105 | 660 |
|
661 |
fun test_goal ({size, iterations, default_type}, tvinsts) i st = |
|
662 |
let |
|
663 |
val sg = Toplevel.sign_of st; |
|
664 |
fun strip (Const ("all", _) $ Abs (_, _, t)) = strip t |
|
665 |
| strip t = t; |
|
666 |
val (gi, frees) = Logic.goal_params |
|
667 |
(prop_of (snd (snd (Proof.get_goal (Toplevel.proof_of st))))) i; |
|
668 |
val gi' = ObjectLogic.atomize_term sg (map_term_types |
|
669 |
(map_type_tfree (fn p as (s, _) => if_none (assoc (tvinsts, s)) |
|
670 |
(if_none default_type (TFree p)))) (subst_bounds (frees, strip gi))); |
|
671 |
in case test_term (Toplevel.theory_of st) size iterations gi' of |
|
672 |
None => writeln "No counterexamples found." |
|
673 |
| Some cex => writeln ("Counterexample found:\n" ^ |
|
674 |
Pretty.string_of (Pretty.chunks (map (fn (s, t) => |
|
675 |
Pretty.block [Pretty.str (s ^ " ="), Pretty.brk 1, |
|
676 |
Sign.pretty_term sg t]) cex))) |
|
677 |
end; |
|
678 |
||
679 |
||
12452 | 680 |
(**** Interface ****) |
681 |
||
13731
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
682 |
val str = setmp print_mode [] Pretty.str; |
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
683 |
|
11520 | 684 |
fun parse_mixfix rd s = |
685 |
(case Scan.finite Symbol.stopper (Scan.repeat |
|
686 |
( $$ "_" >> K Arg |
|
687 |
|| $$ "?" >> K Ignore |
|
688 |
|| $$ "/" |-- Scan.repeat ($$ " ") >> (Pretty o Pretty.brk o length) |
|
689 |
|| $$ "{" |-- $$ "*" |-- Scan.repeat1 |
|
690 |
( $$ "'" |-- Scan.one Symbol.not_eof |
|
691 |
|| Scan.unless ($$ "*" -- $$ "}") (Scan.one Symbol.not_eof)) --| |
|
12452 | 692 |
$$ "*" --| $$ "}" >> (Quote o rd o implode) |
11520 | 693 |
|| Scan.repeat1 |
694 |
( $$ "'" |-- Scan.one Symbol.not_eof |
|
695 |
|| Scan.unless ($$ "_" || $$ "?" || $$ "/" || $$ "{" |-- $$ "*") |
|
13731
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
696 |
(Scan.one Symbol.not_eof)) >> (Pretty o str o implode))) |
11520 | 697 |
(Symbol.explode s) of |
698 |
(p, []) => p |
|
699 |
| _ => error ("Malformed annotation: " ^ quote s)); |
|
700 |
||
11546 | 701 |
structure P = OuterParse and K = OuterSyntax.Keyword; |
11520 | 702 |
|
703 |
val assoc_typeP = |
|
704 |
OuterSyntax.command "types_code" |
|
11546 | 705 |
"associate types with target language types" K.thy_decl |
11520 | 706 |
(Scan.repeat1 (P.xname --| P.$$$ "(" -- P.string --| P.$$$ ")") >> |
12452 | 707 |
(fn xs => Toplevel.theory (fn thy => assoc_types |
708 |
(map (fn (name, mfx) => (name, parse_mixfix |
|
709 |
(typ_of o read_ctyp (sign_of thy)) mfx)) xs) thy))); |
|
11520 | 710 |
|
711 |
val assoc_constP = |
|
712 |
OuterSyntax.command "consts_code" |
|
11546 | 713 |
"associate constants with target language code" K.thy_decl |
11520 | 714 |
(Scan.repeat1 |
13003 | 715 |
(P.xname -- (Scan.option (P.$$$ "::" |-- P.typ)) --| |
11520 | 716 |
P.$$$ "(" -- P.string --| P.$$$ ")") >> |
717 |
(fn xs => Toplevel.theory (fn thy => assoc_consts |
|
718 |
(map (fn ((name, optype), mfx) => (name, optype, parse_mixfix |
|
719 |
(term_of o read_cterm (sign_of thy) o rpair TypeInfer.logicT) mfx)) |
|
720 |
xs) thy))); |
|
721 |
||
722 |
val generate_codeP = |
|
11546 | 723 |
OuterSyntax.command "generate_code" "generates code for terms" K.thy_decl |
13003 | 724 |
(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
|
725 |
Scan.optional (P.$$$ "[" |-- P.enum "," P.xname --| P.$$$ "]") (!mode) -- |
13003 | 726 |
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
|
727 |
(fn ((opt_fname, mode'), xs) => Toplevel.theory (fn thy => |
11520 | 728 |
((case opt_fname of |
729 |
None => use_text Context.ml_output false |
|
730 |
| Some fname => File.write (Path.unpack fname)) |
|
13753
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
731 |
(setmp mode mode' (generate_code thy) xs); thy)))); |
11520 | 732 |
|
14105 | 733 |
val params = |
734 |
[("size", P.nat >> (K o set_size)), |
|
735 |
("iterations", P.nat >> (K o set_iterations)), |
|
736 |
("default_type", P.typ >> set_default_type)]; |
|
737 |
||
738 |
val parse_test_params = P.short_ident :-- (fn s => |
|
739 |
P.$$$ "=" |-- if_none (assoc (params, s)) Scan.fail) >> snd; |
|
740 |
||
741 |
fun parse_tyinst xs = |
|
742 |
(P.type_ident --| P.$$$ "=" -- P.typ >> (fn (v, s) => fn sg => |
|
743 |
fn (x, ys) => (x, (v, typ_of (read_ctyp sg s)) :: ys))) xs; |
|
744 |
||
745 |
fun app [] x = x |
|
746 |
| app (f :: fs) x = app fs (f x); |
|
747 |
||
748 |
val test_paramsP = |
|
749 |
OuterSyntax.command "quickcheck_params" "set parameters for random testing" K.thy_decl |
|
750 |
(P.$$$ "[" |-- P.list1 parse_test_params --| P.$$$ "]" >> |
|
751 |
(fn fs => Toplevel.theory (fn thy => |
|
752 |
map_test_params (app (map (fn f => f (sign_of thy)) fs)) thy))); |
|
753 |
||
754 |
val testP = |
|
755 |
OuterSyntax.command "quickcheck" "try to find counterexample for subgoal" K.diag |
|
756 |
(Scan.option (P.$$$ "[" |-- P.list1 |
|
757 |
( parse_test_params >> (fn f => fn sg => apfst (f sg)) |
|
758 |
|| parse_tyinst) --| P.$$$ "]") -- Scan.optional P.nat 1 >> |
|
759 |
(fn (ps, g) => Toplevel.keep (fn st => |
|
760 |
test_goal (app (if_none (apsome |
|
761 |
(map (fn f => f (Toplevel.sign_of st))) ps) []) |
|
762 |
(get_test_params (Toplevel.theory_of st), [])) g st))); |
|
763 |
||
764 |
val parsers = [assoc_typeP, assoc_constP, generate_codeP, test_paramsP, testP]; |
|
11520 | 765 |
|
12452 | 766 |
val setup = |
767 |
[CodegenData.init, |
|
768 |
add_codegen "default" default_codegen, |
|
769 |
add_tycodegen "default" default_tycodegen, |
|
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
770 |
assoc_types [("fun", parse_mixfix (K dummyT) "(_ ->/ _)")], |
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
771 |
Attrib.add_attributes [("code", |
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
772 |
(code_attr, K Attrib.undef_local_attribute), |
15261 | 773 |
"declare theorems for code generation")], |
774 |
add_attribute "unfold" (Scan.succeed unfold_attr)]; |
|
11520 | 775 |
|
776 |
end; |
|
777 |
||
778 |
OuterSyntax.add_parsers Codegen.parsers; |