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