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