| author | haftmann | 
| Tue, 10 Aug 2010 14:53:41 +0200 | |
| changeset 38312 | 9dd57db3c0f2 | 
| parent 37944 | 4b7afae88c57 | 
| child 38329 | 16bb1e60204b | 
| permissions | -rw-r--r-- | 
| 11520 | 1 | (* Title: Pure/codegen.ML | 
| 11539 | 2 | Author: Stefan Berghofer, TU Muenchen | 
| 11520 | 3 | |
| 11539 | 4 | Generic code generator. | 
| 11520 | 5 | *) | 
| 6 | ||
| 7 | signature CODEGEN = | |
| 8 | sig | |
| 32738 | 9 | val quiet_mode : bool Unsynchronized.ref | 
| 11520 | 10 | val message : string -> unit | 
| 32738 | 11 | val mode : string list Unsynchronized.ref | 
| 12 | val margin : int Unsynchronized.ref | |
| 26974 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 13 | val string_of : Pretty.T -> string | 
| 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 14 | val str : string -> Pretty.T | 
| 11520 | 15 | |
| 12452 | 16 | datatype 'a mixfix = | 
| 11520 | 17 | Arg | 
| 18 | | Ignore | |
| 16769 
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
 berghofe parents: 
16649diff
changeset | 19 | | Module | 
| 11520 | 20 | | Pretty of Pretty.T | 
| 12452 | 21 | | Quote of 'a; | 
| 11520 | 22 | |
| 16649 | 23 | type deftab | 
| 17144 | 24 | type node | 
| 16649 | 25 | type codegr | 
| 12452 | 26 | type 'a codegen | 
| 27 | ||
| 28 | val add_codegen: string -> term codegen -> theory -> theory | |
| 29 | val add_tycodegen: string -> typ codegen -> 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 | |
| 22921 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 38 | val assoc_const: string * (term mixfix list * | 
| 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 39 | (string * string) list) -> theory -> theory | 
| 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 40 | val assoc_const_i: (string * typ) * (term mixfix list * | 
| 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 41 | (string * string) list) -> theory -> theory | 
| 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 42 | val assoc_type: xstring * (typ mixfix list * | 
| 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 43 | (string * string) list) -> theory -> theory | 
| 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 44 | val get_assoc_code: theory -> string * typ -> | 
| 16769 
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 -> | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 50 | term -> codegr -> Pretty.T * codegr | 
| 16649 | 51 | val invoke_tycodegen: theory -> deftab -> string -> string -> bool -> | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 52 | typ -> codegr -> Pretty.T * codegr | 
| 14858 | 53 | val mk_id: string -> string | 
| 17144 | 54 | val mk_qual_id: string -> string * string -> string | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 55 | val mk_const_id: string -> string -> codegr -> (string * string) * codegr | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 56 | val get_const_id: codegr -> string -> string * string | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 57 | val mk_type_id: string -> string -> codegr -> (string * string) * codegr | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 58 | val get_type_id: codegr -> string -> string * string | 
| 27398 
768da1da59d6
simplified retrieval of theory names of consts and types
 haftmann parents: 
27353diff
changeset | 59 | val thyname_of_type: theory -> string -> string | 
| 
768da1da59d6
simplified retrieval of theory names of consts and types
 haftmann parents: 
27353diff
changeset | 60 | val thyname_of_const: theory -> string -> string | 
| 16649 | 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 -> | 
| 32085 | 67 | ((typ * (string * thm)) * int option) option | 
| 24908 
c74ad8782eeb
Codegen.is_instance: raw match, ignore sort constraints;
 wenzelm parents: 
24867diff
changeset | 68 | val is_instance: typ -> typ -> bool | 
| 11520 | 69 | val parens: Pretty.T -> Pretty.T | 
| 70 | val mk_app: bool -> Pretty.T -> Pretty.T list -> Pretty.T | |
| 25892 
3ff9d646a66a
Test data generation and conversion to terms are now more closely
 berghofe parents: 
25400diff
changeset | 71 | val mk_tuple: Pretty.T list -> Pretty.T | 
| 
3ff9d646a66a
Test data generation and conversion to terms are now more closely
 berghofe parents: 
25400diff
changeset | 72 | val mk_let: (Pretty.T * Pretty.T) list -> Pretty.T -> Pretty.T | 
| 11520 | 73 | val eta_expand: term -> term list -> int -> term | 
| 14105 | 74 | val strip_tname: string -> string | 
| 13753 
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
 berghofe parents: 
13731diff
changeset | 75 | val mk_type: bool -> typ -> Pretty.T | 
| 17144 | 76 | val mk_term_of: codegr -> string -> bool -> typ -> Pretty.T | 
| 77 | val mk_gen: codegr -> string -> bool -> string list -> string -> typ -> Pretty.T | |
| 32738 | 78 | val test_fn: (int -> term list option) Unsynchronized.ref | 
| 35378 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35232diff
changeset | 79 | val test_term: Proof.context -> bool -> term -> int -> term list option * (bool list * bool) | 
| 32738 | 80 | val eval_result: (unit -> term) Unsynchronized.ref | 
| 17549 | 81 | val eval_term: theory -> term -> term | 
| 20599 | 82 | val evaluation_conv: cterm -> thm | 
| 12452 | 83 | val parse_mixfix: (string -> 'a) -> string -> 'a mixfix list | 
| 18102 | 84 | val quotes_of: 'a mixfix list -> 'a list | 
| 18281 | 85 | val num_args_of: 'a mixfix list -> int | 
| 18102 | 86 | val replace_quotes: 'b list -> 'a mixfix list -> 'b mixfix list | 
| 16649 | 87 | val mk_deftab: theory -> deftab | 
| 32071 | 88 | val map_unfold: (simpset -> simpset) -> theory -> theory | 
| 19341 
3414c04fbc39
added definitional code generator module: codegen_theorems.ML
 haftmann parents: 
19299diff
changeset | 89 | val add_unfold: thm -> theory -> theory | 
| 32071 | 90 | val del_unfold: thm -> theory -> theory | 
| 17549 | 91 | |
| 17144 | 92 | val get_node: codegr -> string -> node | 
| 93 | val add_edge: string * string -> codegr -> codegr | |
| 94 | val add_edge_acyclic: string * string -> codegr -> codegr | |
| 95 | val del_nodes: string list -> codegr -> codegr | |
| 96 | val map_node: string -> (node -> node) -> codegr -> codegr | |
| 97 | val new_node: string * node -> codegr -> codegr | |
| 28227 | 98 | |
| 99 | val setup: theory -> theory | |
| 11520 | 100 | end; | 
| 101 | ||
| 102 | structure Codegen : CODEGEN = | |
| 103 | struct | |
| 104 | ||
| 32738 | 105 | val quiet_mode = Unsynchronized.ref true; | 
| 11520 | 106 | fun message s = if !quiet_mode then () else writeln s; | 
| 107 | ||
| 33222 | 108 | val mode = Unsynchronized.ref ([] : string list); (* FIXME proper functional argument *) | 
| 13753 
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
 berghofe parents: 
13731diff
changeset | 109 | |
| 32738 | 110 | val margin = Unsynchronized.ref 80; | 
| 13886 
0b243f6e257e
Margin for pretty-printing is now a mutable reference.
 berghofe parents: 
13753diff
changeset | 111 | |
| 37146 
f652333bbf8e
renamed structure PrintMode to Print_Mode, keeping the old name as legacy alias for some time;
 wenzelm parents: 
36953diff
changeset | 112 | fun string_of p = Print_Mode.setmp [] (Pretty.string_of_margin (!margin)) p; | 
| 26974 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 113 | |
| 37146 
f652333bbf8e
renamed structure PrintMode to Print_Mode, keeping the old name as legacy alias for some time;
 wenzelm parents: 
36953diff
changeset | 114 | val str = Print_Mode.setmp [] Pretty.str; | 
| 26974 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 115 | |
| 11520 | 116 | (**** Mixfix syntax ****) | 
| 117 | ||
| 12452 | 118 | datatype 'a mixfix = | 
| 11520 | 119 | Arg | 
| 120 | | Ignore | |
| 16769 
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
 berghofe parents: 
16649diff
changeset | 121 | | Module | 
| 11520 | 122 | | Pretty of Pretty.T | 
| 12452 | 123 | | Quote of 'a; | 
| 11520 | 124 | |
| 125 | fun is_arg Arg = true | |
| 126 | | is_arg Ignore = true | |
| 127 | | is_arg _ = false; | |
| 128 | ||
| 12452 | 129 | fun quotes_of [] = [] | 
| 130 | | quotes_of (Quote q :: ms) = q :: quotes_of ms | |
| 131 | | quotes_of (_ :: ms) = quotes_of ms; | |
| 132 | ||
| 133 | fun args_of [] xs = ([], xs) | |
| 134 | | args_of (Arg :: ms) (x :: xs) = apfst (cons x) (args_of ms xs) | |
| 135 | | args_of (Ignore :: ms) (_ :: xs) = args_of ms xs | |
| 136 | | args_of (_ :: ms) xs = args_of ms xs; | |
| 11520 | 137 | |
| 33317 | 138 | fun num_args_of x = length (filter is_arg x); | 
| 11520 | 139 | |
| 140 | ||
| 141 | (**** theory data ****) | |
| 142 | ||
| 16649 | 143 | (* preprocessed definition table *) | 
| 144 | ||
| 145 | type deftab = | |
| 146 | (typ * (* type of constant *) | |
| 147 | (string * (* name of theory containing definition of constant *) | |
| 32085 | 148 | thm)) (* definition theorem *) | 
| 16649 | 149 | list Symtab.table; | 
| 150 | ||
| 151 | (* code dependency graph *) | |
| 152 | ||
| 17144 | 153 | type nametab = (string * string) Symtab.table * unit Symtab.table; | 
| 154 | ||
| 28227 | 155 | fun merge_nametabs ((tab, used) : nametab, (tab', used')) = | 
| 17144 | 156 | (Symtab.merge op = (tab, tab'), Symtab.merge op = (used, used')); | 
| 157 | ||
| 158 | type node = | |
| 16649 | 159 | (exn option * (* slot for arbitrary data *) | 
| 160 | string * (* name of structure containing piece of code *) | |
| 17144 | 161 | string); (* piece of code *) | 
| 162 | ||
| 163 | type codegr = | |
| 164 | node Graph.T * | |
| 165 | (nametab * (* table for assigned constant names *) | |
| 166 | nametab); (* table for assigned type names *) | |
| 167 | ||
| 168 | val emptygr : codegr = (Graph.empty, | |
| 169 | ((Symtab.empty, Symtab.empty), (Symtab.empty, Symtab.empty))); | |
| 16649 | 170 | |
| 14105 | 171 | (* type of code generators *) | 
| 11520 | 172 | |
| 16649 | 173 | type 'a codegen = | 
| 174 | theory -> (* theory in which generate_code was called *) | |
| 175 | deftab -> (* definition table (for efficiency) *) | |
| 176 | string -> (* node name of caller (for recording dependencies) *) | |
| 17144 | 177 | string -> (* module name of caller (for modular code generation) *) | 
| 16649 | 178 | bool -> (* whether to parenthesize generated expression *) | 
| 179 | 'a -> (* item to generate code from *) | |
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 180 | codegr -> (* code dependency graph *) | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 181 | (Pretty.T * codegr) option; | 
| 12452 | 182 | |
| 22846 | 183 | |
| 184 | (* theory data *) | |
| 16458 | 185 | |
| 33522 | 186 | structure CodegenData = Theory_Data | 
| 22846 | 187 | ( | 
| 11520 | 188 | type T = | 
| 12452 | 189 |     {codegens : (string * term codegen) list,
 | 
| 190 | tycodegens : (string * typ codegen) list, | |
| 16769 
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
 berghofe parents: 
16649diff
changeset | 191 | 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 | 192 | types : (string * (typ mixfix list * (string * string) list)) list, | 
| 15261 | 193 | preprocs: (stamp * (theory -> thm list -> thm list)) list, | 
| 28640 
188e9557c572
- removed test_params from CodegenData (now in Pure/Tools/quickcheck.ML)
 berghofe parents: 
28537diff
changeset | 194 | modules: codegr Symtab.table}; | 
| 11520 | 195 | |
| 12555 
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
 berghofe parents: 
12490diff
changeset | 196 | val empty = | 
| 24219 | 197 |     {codegens = [], tycodegens = [], consts = [], types = [],
 | 
| 28640 
188e9557c572
- removed test_params from CodegenData (now in Pure/Tools/quickcheck.ML)
 berghofe parents: 
28537diff
changeset | 198 | preprocs = [], modules = Symtab.empty}; | 
| 16458 | 199 | val extend = I; | 
| 11520 | 200 | |
| 33522 | 201 | fun merge | 
| 12555 
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
 berghofe parents: 
12490diff
changeset | 202 |     ({codegens = codegens1, tycodegens = tycodegens1,
 | 
| 24219 | 203 | consts = consts1, types = types1, | 
| 28640 
188e9557c572
- removed test_params from CodegenData (now in Pure/Tools/quickcheck.ML)
 berghofe parents: 
28537diff
changeset | 204 | preprocs = preprocs1, modules = modules1} : T, | 
| 12555 
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
 berghofe parents: 
12490diff
changeset | 205 |      {codegens = codegens2, tycodegens = tycodegens2,
 | 
| 24219 | 206 | consts = consts2, types = types2, | 
| 33522 | 207 | preprocs = preprocs2, modules = modules2}) : T = | 
| 19119 
dea8d858d37f
abandoned merge_alists' in favour of generic AList.merge
 haftmann parents: 
19046diff
changeset | 208 |     {codegens = AList.merge (op =) (K true) (codegens1, codegens2),
 | 
| 
dea8d858d37f
abandoned merge_alists' in favour of generic AList.merge
 haftmann parents: 
19046diff
changeset | 209 | tycodegens = AList.merge (op =) (K true) (tycodegens1, tycodegens2), | 
| 21098 
d0d8a48ae4e6
switched merge_alists'' to AList.merge'' whenever appropriate
 haftmann parents: 
21056diff
changeset | 210 | consts = AList.merge (op =) (K true) (consts1, consts2), | 
| 
d0d8a48ae4e6
switched merge_alists'' to AList.merge'' whenever appropriate
 haftmann parents: 
21056diff
changeset | 211 | types = AList.merge (op =) (K true) (types1, types2), | 
| 19119 
dea8d858d37f
abandoned merge_alists' in favour of generic AList.merge
 haftmann parents: 
19046diff
changeset | 212 | preprocs = AList.merge (op =) (K true) (preprocs1, preprocs2), | 
| 28640 
188e9557c572
- removed test_params from CodegenData (now in Pure/Tools/quickcheck.ML)
 berghofe parents: 
28537diff
changeset | 213 | modules = Symtab.merge (K true) (modules1, modules2)}; | 
| 22846 | 214 | ); | 
| 11520 | 215 | |
| 22846 | 216 | fun print_codegens thy = | 
| 217 |   let val {codegens, tycodegens, ...} = CodegenData.get thy in
 | |
| 12452 | 218 | Pretty.writeln (Pretty.chunks | 
| 219 |       [Pretty.strs ("term code generators:" :: map fst codegens),
 | |
| 22846 | 220 |        Pretty.strs ("type code generators:" :: map fst tycodegens)])
 | 
| 221 | end; | |
| 11520 | 222 | |
| 223 | ||
| 224 | ||
| 17144 | 225 | (**** access modules ****) | 
| 226 | ||
| 227 | fun get_modules thy = #modules (CodegenData.get thy); | |
| 228 | ||
| 229 | fun map_modules f thy = | |
| 28640 
188e9557c572
- removed test_params from CodegenData (now in Pure/Tools/quickcheck.ML)
 berghofe parents: 
28537diff
changeset | 230 |   let val {codegens, tycodegens, consts, types, preprocs, modules} =
 | 
| 17144 | 231 | CodegenData.get thy; | 
| 232 |   in CodegenData.put {codegens = codegens, tycodegens = tycodegens,
 | |
| 24219 | 233 | consts = consts, types = types, preprocs = preprocs, | 
| 28640 
188e9557c572
- removed test_params from CodegenData (now in Pure/Tools/quickcheck.ML)
 berghofe parents: 
28537diff
changeset | 234 | modules = f modules} thy | 
| 14105 | 235 | end; | 
| 236 | ||
| 237 | ||
| 12452 | 238 | (**** add new code generators to theory ****) | 
| 11520 | 239 | |
| 240 | fun add_codegen name f thy = | |
| 28640 
188e9557c572
- removed test_params from CodegenData (now in Pure/Tools/quickcheck.ML)
 berghofe parents: 
28537diff
changeset | 241 |   let val {codegens, tycodegens, consts, types, preprocs, modules} =
 | 
| 14105 | 242 | CodegenData.get thy | 
| 17521 | 243 | in (case AList.lookup (op =) codegens name of | 
| 15531 | 244 |       NONE => CodegenData.put {codegens = (name, f) :: codegens,
 | 
| 12555 
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
 berghofe parents: 
12490diff
changeset | 245 | tycodegens = tycodegens, consts = consts, types = types, | 
| 28640 
188e9557c572
- removed test_params from CodegenData (now in Pure/Tools/quickcheck.ML)
 berghofe parents: 
28537diff
changeset | 246 | preprocs = preprocs, modules = modules} thy | 
| 15531 | 247 |     | SOME _ => error ("Code generator " ^ name ^ " already declared"))
 | 
| 12452 | 248 | end; | 
| 249 | ||
| 250 | fun add_tycodegen name f thy = | |
| 28640 
188e9557c572
- removed test_params from CodegenData (now in Pure/Tools/quickcheck.ML)
 berghofe parents: 
28537diff
changeset | 251 |   let val {codegens, tycodegens, consts, types, preprocs, modules} =
 | 
| 14105 | 252 | CodegenData.get thy | 
| 17521 | 253 | in (case AList.lookup (op =) tycodegens name of | 
| 15531 | 254 |       NONE => CodegenData.put {tycodegens = (name, f) :: tycodegens,
 | 
| 12555 
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
 berghofe parents: 
12490diff
changeset | 255 | codegens = codegens, consts = consts, types = types, | 
| 28640 
188e9557c572
- removed test_params from CodegenData (now in Pure/Tools/quickcheck.ML)
 berghofe parents: 
28537diff
changeset | 256 | preprocs = preprocs, modules = modules} thy | 
| 15531 | 257 |     | SOME _ => error ("Code generator " ^ name ^ " already declared"))
 | 
| 11520 | 258 | end; | 
| 259 | ||
| 260 | ||
| 15261 | 261 | (**** preprocessors ****) | 
| 262 | ||
| 263 | fun add_preprocessor p thy = | |
| 28640 
188e9557c572
- removed test_params from CodegenData (now in Pure/Tools/quickcheck.ML)
 berghofe parents: 
28537diff
changeset | 264 |   let val {codegens, tycodegens, consts, types, preprocs, modules} =
 | 
| 15261 | 265 | CodegenData.get thy | 
| 266 |   in CodegenData.put {tycodegens = tycodegens,
 | |
| 267 | codegens = codegens, consts = consts, types = types, | |
| 24219 | 268 | preprocs = (stamp (), p) :: preprocs, | 
| 28640 
188e9557c572
- removed test_params from CodegenData (now in Pure/Tools/quickcheck.ML)
 berghofe parents: 
28537diff
changeset | 269 | modules = modules} thy | 
| 15261 | 270 | end; | 
| 271 | ||
| 21836 | 272 | fun preprocess thy = | 
| 15261 | 273 |   let val {preprocs, ...} = CodegenData.get thy
 | 
| 21836 | 274 | in fold (fn (_, f) => f thy) preprocs end; | 
| 15261 | 275 | |
| 17549 | 276 | fun preprocess_term thy t = | 
| 277 | let | |
| 29270 
0eade173f77e
moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
 wenzelm parents: 
29266diff
changeset | 278 | val x = Free (Name.variant (OldTerm.add_term_names (t, [])) "x", fastype_of t); | 
| 17549 | 279 | (* fake definition *) | 
| 32970 
fbd2bb2489a8
operations of structure Skip_Proof (formerly SkipProof) no longer require quick_and_dirty mode;
 wenzelm parents: 
32966diff
changeset | 280 | val eq = Skip_Proof.make_thm thy (Logic.mk_equals (x, t)); | 
| 17549 | 281 | fun err () = error "preprocess_term: bad preprocessor" | 
| 282 | in case map prop_of (preprocess thy [eq]) of | |
| 283 |       [Const ("==", _) $ x' $ t'] => if x = x' then t' else err ()
 | |
| 284 | | _ => err () | |
| 285 | end; | |
| 18281 | 286 | |
| 33522 | 287 | structure UnfoldData = Theory_Data | 
| 28640 
188e9557c572
- removed test_params from CodegenData (now in Pure/Tools/quickcheck.ML)
 berghofe parents: 
28537diff
changeset | 288 | ( | 
| 
188e9557c572
- removed test_params from CodegenData (now in Pure/Tools/quickcheck.ML)
 berghofe parents: 
28537diff
changeset | 289 | type T = simpset; | 
| 
188e9557c572
- removed test_params from CodegenData (now in Pure/Tools/quickcheck.ML)
 berghofe parents: 
28537diff
changeset | 290 | val empty = empty_ss; | 
| 
188e9557c572
- removed test_params from CodegenData (now in Pure/Tools/quickcheck.ML)
 berghofe parents: 
28537diff
changeset | 291 | val extend = I; | 
| 33522 | 292 | val merge = merge_ss; | 
| 28640 
188e9557c572
- removed test_params from CodegenData (now in Pure/Tools/quickcheck.ML)
 berghofe parents: 
28537diff
changeset | 293 | ); | 
| 
188e9557c572
- removed test_params from CodegenData (now in Pure/Tools/quickcheck.ML)
 berghofe parents: 
28537diff
changeset | 294 | |
| 32071 | 295 | val map_unfold = UnfoldData.map; | 
| 296 | val add_unfold = map_unfold o MetaSimplifier.add_simp; | |
| 297 | val del_unfold = map_unfold o MetaSimplifier.del_simp; | |
| 28640 
188e9557c572
- removed test_params from CodegenData (now in Pure/Tools/quickcheck.ML)
 berghofe parents: 
28537diff
changeset | 298 | |
| 
188e9557c572
- removed test_params from CodegenData (now in Pure/Tools/quickcheck.ML)
 berghofe parents: 
28537diff
changeset | 299 | fun unfold_preprocessor thy = | 
| 35232 
f588e1169c8b
renamed Simplifier.theory_context to Simplifier.global_context to emphasize that this is not the real thing;
 wenzelm parents: 
33957diff
changeset | 300 | let val ss = Simplifier.global_context thy (UnfoldData.get thy) | 
| 28640 
188e9557c572
- removed test_params from CodegenData (now in Pure/Tools/quickcheck.ML)
 berghofe parents: 
28537diff
changeset | 301 | in map (Thm.transfer thy #> Simplifier.full_simplify ss) end; | 
| 15261 | 302 | |
| 303 | ||
| 11520 | 304 | (**** associate constants with target language code ****) | 
| 305 | ||
| 22921 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 306 | fun gen_assoc_const prep_const (raw_const, syn) thy = | 
| 11520 | 307 | let | 
| 28640 
188e9557c572
- removed test_params from CodegenData (now in Pure/Tools/quickcheck.ML)
 berghofe parents: 
28537diff
changeset | 308 |     val {codegens, tycodegens, consts, types, preprocs, modules} =
 | 
| 14105 | 309 | CodegenData.get thy; | 
| 22921 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 310 | val (cname, T) = prep_const thy raw_const; | 
| 11520 | 311 | in | 
| 22921 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 312 | if num_args_of (fst syn) > length (binder_types T) then | 
| 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 313 |       error ("More arguments than in corresponding type of " ^ cname)
 | 
| 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 314 | else case AList.lookup (op =) consts (cname, T) of | 
| 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 315 |       NONE => CodegenData.put {codegens = codegens,
 | 
| 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 316 | tycodegens = tycodegens, | 
| 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 317 | consts = ((cname, T), syn) :: consts, | 
| 24219 | 318 | types = types, preprocs = preprocs, | 
| 28640 
188e9557c572
- removed test_params from CodegenData (now in Pure/Tools/quickcheck.ML)
 berghofe parents: 
28537diff
changeset | 319 | modules = modules} thy | 
| 22921 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 320 |     | SOME _ => error ("Constant " ^ cname ^ " already associated with code")
 | 
| 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 321 | end; | 
| 11520 | 322 | |
| 22921 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 323 | val assoc_const_i = gen_assoc_const (K I); | 
| 31153 | 324 | val assoc_const = gen_assoc_const Code.read_bare_const; | 
| 11520 | 325 | |
| 15801 | 326 | |
| 11520 | 327 | (**** associate types with target language types ****) | 
| 328 | ||
| 22921 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 329 | fun assoc_type (s, syn) thy = | 
| 11520 | 330 | let | 
| 28640 
188e9557c572
- removed test_params from CodegenData (now in Pure/Tools/quickcheck.ML)
 berghofe parents: 
28537diff
changeset | 331 |     val {codegens, tycodegens, consts, types, preprocs, modules} =
 | 
| 14105 | 332 | CodegenData.get thy; | 
| 22921 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 333 | val tc = Sign.intern_type thy s; | 
| 11520 | 334 | in | 
| 18320 
ce523820ff75
assoc_consts and assoc_types now check number of arguments in template.
 berghofe parents: 
18281diff
changeset | 335 | case Symtab.lookup (snd (#types (Type.rep_tsig (Sign.tsig_of thy)))) tc of | 
| 33173 
b8ca12f6681a
eliminated obsolete tags for types/consts -- now handled via name space, in strongly typed fashion;
 wenzelm parents: 
33172diff
changeset | 336 | SOME (Type.LogicalType i) => | 
| 18320 
ce523820ff75
assoc_consts and assoc_types now check number of arguments in template.
 berghofe parents: 
18281diff
changeset | 337 | 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 | 338 |           error ("More arguments than corresponding type constructor " ^ s)
 | 
| 33092 
c859019d3ac5
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
33042diff
changeset | 339 | else | 
| 
c859019d3ac5
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
33042diff
changeset | 340 | (case AList.lookup (op =) types tc of | 
| 
c859019d3ac5
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
33042diff
changeset | 341 |             NONE => CodegenData.put {codegens = codegens,
 | 
| 
c859019d3ac5
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
33042diff
changeset | 342 | tycodegens = tycodegens, consts = consts, | 
| 
c859019d3ac5
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
33042diff
changeset | 343 | types = (tc, syn) :: types, | 
| 
c859019d3ac5
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
33042diff
changeset | 344 | preprocs = preprocs, modules = modules} thy | 
| 
c859019d3ac5
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
33042diff
changeset | 345 |           | SOME _ => error ("Type " ^ tc ^ " already associated with code"))
 | 
| 18320 
ce523820ff75
assoc_consts and assoc_types now check number of arguments in template.
 berghofe parents: 
18281diff
changeset | 346 |     | _ => error ("Not a type constructor: " ^ s)
 | 
| 22921 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 347 | end; | 
| 11520 | 348 | |
| 21098 
d0d8a48ae4e6
switched merge_alists'' to AList.merge'' whenever appropriate
 haftmann parents: 
21056diff
changeset | 349 | fun get_assoc_type thy = AList.lookup (op =) ((#types o CodegenData.get) thy); | 
| 11546 | 350 | |
| 11520 | 351 | |
| 352 | (**** make valid ML identifiers ****) | |
| 353 | ||
| 14858 | 354 | fun is_ascii_letdig x = Symbol.is_ascii_letter x orelse | 
| 355 | Symbol.is_ascii_digit x orelse Symbol.is_ascii_quasi x; | |
| 356 | ||
| 28375 | 357 | fun dest_sym s = (case split_last (snd (take_prefix (fn c => c = "\\") (explode s))) of | 
| 14858 | 358 |     ("<" :: "^" :: xs, ">") => (true, implode xs)
 | 
| 359 |   | ("<" :: xs, ">") => (false, implode xs)
 | |
| 360 | | _ => sys_error "dest_sym"); | |
| 16458 | 361 | |
| 14858 | 362 | fun mk_id s = if s = "" then "" else | 
| 11520 | 363 | let | 
| 14858 | 364 | fun check_str [] = [] | 
| 365 | | check_str xs = (case take_prefix is_ascii_letdig xs of | |
| 366 | ([], " " :: zs) => check_str zs | |
| 367 | | ([], z :: zs) => | |
| 368 | if size z = 1 then string_of_int (ord z) :: check_str zs | |
| 369 | else (case dest_sym z of | |
| 370 | (true, "isub") => check_str zs | |
| 371 | | (true, "isup") => "" :: check_str zs | |
| 372 | | (ctrl, s') => (if ctrl then "ctrl_" ^ s' else s') :: check_str zs) | |
| 373 | | (ys, zs) => implode ys :: check_str zs); | |
| 30364 
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
 wenzelm parents: 
30288diff
changeset | 374 | val s' = space_implode "_" (maps (check_str o Symbol.explode) (Long_Name.explode s)) | 
| 11520 | 375 | in | 
| 14858 | 376 | if Symbol.is_ascii_letter (hd (explode s')) then s' else "id_" ^ s' | 
| 11520 | 377 | end; | 
| 378 | ||
| 17144 | 379 | fun mk_long_id (p as (tab, used)) module s = | 
| 16649 | 380 | let | 
| 17144 | 381 | fun find_name [] = sys_error "mk_long_id" | 
| 382 | | find_name (ys :: yss) = | |
| 383 | let | |
| 30364 
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
 wenzelm parents: 
30288diff
changeset | 384 | val s' = Long_Name.implode ys | 
| 
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
 wenzelm parents: 
30288diff
changeset | 385 | val s'' = Long_Name.append module s' | 
| 17412 | 386 | in case Symtab.lookup used s'' of | 
| 17261 | 387 | NONE => ((module, s'), | 
| 17412 | 388 | (Symtab.update_new (s, (module, s')) tab, | 
| 389 | Symtab.update_new (s'', ()) used)) | |
| 17144 | 390 | | SOME _ => find_name yss | 
| 391 | end | |
| 17412 | 392 | in case Symtab.lookup tab s of | 
| 30364 
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
 wenzelm parents: 
30288diff
changeset | 393 | NONE => find_name (Library.suffixes1 (Long_Name.explode s)) | 
| 17144 | 394 | | SOME name => (name, p) | 
| 16649 | 395 | end; | 
| 396 | ||
| 17144 | 397 | (* module: module name for caller *) | 
| 398 | (* module': module name for callee *) | |
| 399 | (* if caller and callee reside in different modules, use qualified access *) | |
| 16649 | 400 | |
| 17144 | 401 | fun mk_qual_id module (module', s) = | 
| 402 | if module = module' orelse module' = "" then s else module' ^ "." ^ s; | |
| 403 | ||
| 404 | fun mk_const_id module cname (gr, (tab1, tab2)) = | |
| 16649 | 405 | let | 
| 17144 | 406 | val ((module, s), tab1') = mk_long_id tab1 module cname | 
| 407 | val s' = mk_id s; | |
| 21478 
a90250b1cf42
moved ML syntax operations to structure ML_Syntax;
 wenzelm parents: 
21098diff
changeset | 408 | val s'' = if ML_Syntax.is_reserved s' then s' ^ "_const" else s' | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 409 | in (((module, s'')), (gr, (tab1', tab2))) end; | 
| 13073 
cc9d7f403a4b
mk_const_id now checks for clashes with reserved ML identifiers.
 berghofe parents: 
13003diff
changeset | 410 | |
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 411 | fun get_const_id (gr, (tab1, tab2)) cname = | 
| 17412 | 412 | case Symtab.lookup (fst tab1) cname of | 
| 17144 | 413 |     NONE => error ("get_const_id: no such constant: " ^ quote cname)
 | 
| 414 | | SOME (module, s) => | |
| 415 | let | |
| 416 | val s' = mk_id s; | |
| 21478 
a90250b1cf42
moved ML syntax operations to structure ML_Syntax;
 wenzelm parents: 
21098diff
changeset | 417 | val s'' = if ML_Syntax.is_reserved s' then s' ^ "_const" else s' | 
| 17144 | 418 | in (module, s'') end; | 
| 419 | ||
| 420 | fun mk_type_id module tyname (gr, (tab1, tab2)) = | |
| 16649 | 421 | let | 
| 17144 | 422 | val ((module, s), tab2') = mk_long_id tab2 module tyname | 
| 423 | val s' = mk_id s; | |
| 21478 
a90250b1cf42
moved ML syntax operations to structure ML_Syntax;
 wenzelm parents: 
21098diff
changeset | 424 | val s'' = if ML_Syntax.is_reserved s' then s' ^ "_type" else s' | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 425 | in ((module, s''), (gr, (tab1, tab2'))) end; | 
| 16649 | 426 | |
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 427 | fun get_type_id (gr, (tab1, tab2)) tyname = | 
| 17412 | 428 | case Symtab.lookup (fst tab2) tyname of | 
| 17144 | 429 |     NONE => error ("get_type_id: no such type: " ^ quote tyname)
 | 
| 430 | | SOME (module, s) => | |
| 431 | let | |
| 432 | val s' = mk_id s; | |
| 21478 
a90250b1cf42
moved ML syntax operations to structure ML_Syntax;
 wenzelm parents: 
21098diff
changeset | 433 | val s'' = if ML_Syntax.is_reserved s' then s' ^ "_type" else s' | 
| 17144 | 434 | in (module, s'') end; | 
| 435 | ||
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 436 | fun get_type_id' f tab tyname = apsnd f (get_type_id tab tyname); | 
| 17144 | 437 | |
| 438 | fun get_node (gr, x) k = Graph.get_node gr k; | |
| 439 | fun add_edge e (gr, x) = (Graph.add_edge e gr, x); | |
| 440 | fun add_edge_acyclic e (gr, x) = (Graph.add_edge_acyclic e gr, x); | |
| 441 | fun del_nodes ks (gr, x) = (Graph.del_nodes ks gr, x); | |
| 442 | fun map_node k f (gr, x) = (Graph.map_node k f gr, x); | |
| 443 | fun new_node p (gr, x) = (Graph.new_node p gr, x); | |
| 16649 | 444 | |
| 33172 | 445 | fun thyname_of_type thy = #theory_name o Name_Space.the_entry (Sign.type_space thy); | 
| 446 | fun thyname_of_const thy = #theory_name o Name_Space.the_entry (Sign.const_space thy); | |
| 11520 | 447 | |
| 13731 
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
 berghofe parents: 
13073diff
changeset | 448 | fun rename_terms ts = | 
| 11520 | 449 | let | 
| 29270 
0eade173f77e
moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
 wenzelm parents: 
29266diff
changeset | 450 | val names = List.foldr OldTerm.add_term_names | 
| 20286 | 451 | (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 | 452 | val reserved = filter ML_Syntax.is_reserved names; | 
| 19482 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19473diff
changeset | 453 | val (illegal, alt_names) = split_list (map_filter (fn s => | 
| 15531 | 454 | let val s' = mk_id s in if s = s' then NONE else SOME (s, s') end) names) | 
| 14858 | 455 | val ps = (reserved @ illegal) ~~ | 
| 20071 
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
 wenzelm parents: 
19806diff
changeset | 456 | Name.variant_list names (map (suffix "'") reserved @ alt_names); | 
| 11520 | 457 | |
| 17521 | 458 | fun rename_id s = AList.lookup (op =) ps s |> the_default s; | 
| 14858 | 459 | |
| 460 | fun rename (Var ((a, i), T)) = Var ((rename_id a, i), T) | |
| 461 | | rename (Free (a, T)) = Free (rename_id a, T) | |
| 11520 | 462 | | rename (Abs (s, T, t)) = Abs (s, T, rename t) | 
| 463 | | rename (t $ u) = rename t $ rename u | |
| 464 | | rename t = t; | |
| 465 | in | |
| 13731 
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
 berghofe parents: 
13073diff
changeset | 466 | map rename ts | 
| 
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
 berghofe parents: 
13073diff
changeset | 467 | end; | 
| 
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
 berghofe parents: 
13073diff
changeset | 468 | |
| 
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
 berghofe parents: 
13073diff
changeset | 469 | val rename_term = hd o rename_terms o single; | 
| 
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
 berghofe parents: 
13073diff
changeset | 470 | |
| 
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
 berghofe parents: 
13073diff
changeset | 471 | |
| 
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
 berghofe parents: 
13073diff
changeset | 472 | (**** retrieve definition of constant ****) | 
| 
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
 berghofe parents: 
13073diff
changeset | 473 | |
| 24908 
c74ad8782eeb
Codegen.is_instance: raw match, ignore sort constraints;
 wenzelm parents: 
24867diff
changeset | 474 | fun is_instance T1 T2 = | 
| 35845 
e5980f0ad025
renamed varify/unvarify operations to varify_global/unvarify_global to emphasize that these only work in a global situation;
 wenzelm parents: 
35378diff
changeset | 475 | Type.raw_instance (T1, if null (OldTerm.typ_tfrees T2) then T2 else Logic.varifyT_global T2); | 
| 13731 
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
 berghofe parents: 
13073diff
changeset | 476 | |
| 22921 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 477 | fun get_assoc_code thy (s, T) = Option.map snd (find_first (fn ((s', T'), _) => | 
| 24908 
c74ad8782eeb
Codegen.is_instance: raw match, ignore sort constraints;
 wenzelm parents: 
24867diff
changeset | 478 | s = s' andalso is_instance T T') (#consts (CodegenData.get thy))); | 
| 13731 
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
 berghofe parents: 
13073diff
changeset | 479 | |
| 19482 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19473diff
changeset | 480 | fun get_aux_code xs = map_filter (fn (m, code) => | 
| 20665 | 481 | 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 | 482 | |
| 32085 | 483 | fun dest_prim_def t = | 
| 484 | let | |
| 485 | val (lhs, rhs) = Logic.dest_equals t; | |
| 486 | val (c, args) = strip_comb lhs; | |
| 487 | val (s, T) = dest_Const c | |
| 488 | in if forall is_Var args then SOME (s, (T, (args, rhs))) else NONE | |
| 489 | end handle TERM _ => NONE; | |
| 490 | ||
| 16649 | 491 | fun mk_deftab thy = | 
| 13731 
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
 berghofe parents: 
13073diff
changeset | 492 | let | 
| 22680 
31a2303f4283
cleaned/simplified Sign.read_typ, Thm.read_cterm etc.;
 wenzelm parents: 
22596diff
changeset | 493 | val axmss = map (fn thy' => (Context.theory_name thy', Theory.axiom_table thy')) | 
| 16649 | 494 | (thy :: Theory.ancestors_of thy); | 
| 32085 | 495 | fun add_def thyname (name, t) = (case dest_prim_def t of | 
| 21056 | 496 | NONE => I | 
| 32085 | 497 | | SOME (s, (T, _)) => Symtab.map_default (s, []) | 
| 498 | (cons (T, (thyname, Thm.axiom thy name)))); | |
| 13731 
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
 berghofe parents: 
13073diff
changeset | 499 | in | 
| 21056 | 500 | fold (fn (thyname, axms) => Symtab.fold (add_def thyname) axms) axmss Symtab.empty | 
| 11520 | 501 | end; | 
| 502 | ||
| 32085 | 503 | fun prep_prim_def thy thm = | 
| 504 | let | |
| 505 | val prop = case preprocess thy [thm] | |
| 506 | of [thm'] => Thm.prop_of thm' | |
| 507 | | _ => error "mk_deftab: bad preprocessor" | |
| 508 | in ((Option.map o apsnd o apsnd) | |
| 509 | (fn (args, rhs) => split_last (rename_terms (args @ [rhs]))) o dest_prim_def) prop | |
| 510 | end; | |
| 511 | ||
| 17412 | 512 | fun get_defn thy defs s T = (case Symtab.lookup defs s of | 
| 16649 | 513 | NONE => NONE | 
| 514 | | SOME ds => | |
| 24908 
c74ad8782eeb
Codegen.is_instance: raw match, ignore sort constraints;
 wenzelm parents: 
24867diff
changeset | 515 | let val i = find_index (is_instance T o fst) ds | 
| 16649 | 516 | in if i >= 0 then | 
| 32085 | 517 | SOME (nth ds i, if length ds = 1 then NONE else SOME i) | 
| 16649 | 518 | else NONE | 
| 519 | end); | |
| 520 | ||
| 11520 | 521 | |
| 12452 | 522 | (**** invoke suitable code generator for term / type ****) | 
| 11520 | 523 | |
| 17144 | 524 | fun codegen_error (gr, _) dep s = | 
| 525 | error (s ^ "\nrequired by:\n" ^ commas (Graph.all_succs gr [dep])); | |
| 526 | ||
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 527 | fun invoke_codegen thy defs dep module brack t gr = (case get_first | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 528 | (fn (_, f) => f thy defs dep module brack t gr) (#codegens (CodegenData.get thy)) of | 
| 17144 | 529 |       NONE => codegen_error gr dep ("Unable to generate code for term:\n" ^
 | 
| 26939 
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
 wenzelm parents: 
26931diff
changeset | 530 | Syntax.string_of_term_global thy t) | 
| 15531 | 531 | | SOME x => x); | 
| 12452 | 532 | |
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 533 | fun invoke_tycodegen thy defs dep module brack T gr = (case get_first | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 534 | (fn (_, f) => f thy defs dep module brack T gr ) (#tycodegens (CodegenData.get thy)) of | 
| 17144 | 535 |       NONE => codegen_error gr dep ("Unable to generate code for type:\n" ^
 | 
| 26939 
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
 wenzelm parents: 
26931diff
changeset | 536 | Syntax.string_of_typ_global thy T) | 
| 15531 | 537 | | SOME x => x); | 
| 11520 | 538 | |
| 539 | ||
| 540 | (**** code generator for mixfix expressions ****) | |
| 541 | ||
| 26974 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 542 | fun parens p = Pretty.block [str "(", p, str ")"];
 | 
| 11520 | 543 | |
| 544 | fun pretty_fn [] p = [p] | |
| 26974 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 545 |   | pretty_fn (x::xs) p = str ("fn " ^ x ^ " =>") ::
 | 
| 11520 | 546 | Pretty.brk 1 :: pretty_fn xs p; | 
| 547 | ||
| 16769 
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
 berghofe parents: 
16649diff
changeset | 548 | fun pretty_mixfix _ _ [] [] _ = [] | 
| 
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
 berghofe parents: 
16649diff
changeset | 549 | | pretty_mixfix module module' (Arg :: ms) (p :: ps) qs = | 
| 
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
 berghofe parents: 
16649diff
changeset | 550 | p :: pretty_mixfix module module' ms ps qs | 
| 
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
 berghofe parents: 
16649diff
changeset | 551 | | pretty_mixfix module module' (Ignore :: ms) ps qs = | 
| 
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
 berghofe parents: 
16649diff
changeset | 552 | pretty_mixfix module module' ms ps qs | 
| 
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
 berghofe parents: 
16649diff
changeset | 553 | | pretty_mixfix module module' (Module :: ms) ps qs = | 
| 17144 | 554 | (if module <> module' | 
| 26974 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 555 | then cons (str (module' ^ ".")) else I) | 
| 16769 
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
 berghofe parents: 
16649diff
changeset | 556 | (pretty_mixfix module module' ms ps qs) | 
| 
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
 berghofe parents: 
16649diff
changeset | 557 | | pretty_mixfix module module' (Pretty p :: ms) ps qs = | 
| 
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
 berghofe parents: 
16649diff
changeset | 558 | p :: pretty_mixfix module module' ms ps qs | 
| 
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
 berghofe parents: 
16649diff
changeset | 559 | | pretty_mixfix module module' (Quote _ :: ms) ps (q :: qs) = | 
| 
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
 berghofe parents: 
16649diff
changeset | 560 | q :: pretty_mixfix module module' ms ps qs; | 
| 11520 | 561 | |
| 18102 | 562 | fun replace_quotes [] [] = [] | 
| 563 | | replace_quotes xs (Arg :: ms) = | |
| 564 | Arg :: replace_quotes xs ms | |
| 565 | | replace_quotes xs (Ignore :: ms) = | |
| 566 | Ignore :: replace_quotes xs ms | |
| 567 | | replace_quotes xs (Module :: ms) = | |
| 568 | Module :: replace_quotes xs ms | |
| 569 | | replace_quotes xs (Pretty p :: ms) = | |
| 570 | Pretty p :: replace_quotes xs ms | |
| 571 | | replace_quotes (x::xs) (Quote _ :: ms) = | |
| 572 | Quote x :: replace_quotes xs ms; | |
| 573 | ||
| 11520 | 574 | |
| 12452 | 575 | (**** default code generators ****) | 
| 11520 | 576 | |
| 577 | fun eta_expand t ts i = | |
| 578 | let | |
| 24456 | 579 | val k = length ts; | 
| 33957 | 580 | val Ts = drop k (binder_types (fastype_of t)); | 
| 24456 | 581 | val j = i - k | 
| 11520 | 582 | in | 
| 23178 | 583 |     List.foldr (fn (T, t) => Abs ("x", T, t))
 | 
| 33957 | 584 | (list_comb (t, ts @ map Bound (j-1 downto 0))) (take j Ts) | 
| 11520 | 585 | end; | 
| 586 | ||
| 587 | fun mk_app _ p [] = p | |
| 588 | | mk_app brack p ps = if brack then | |
| 26974 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 589 |        Pretty.block (str "(" ::
 | 
| 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 590 | separate (Pretty.brk 1) (p :: ps) @ [str ")"]) | 
| 11520 | 591 | else Pretty.block (separate (Pretty.brk 1) (p :: ps)); | 
| 592 | ||
| 20071 
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
 wenzelm parents: 
19806diff
changeset | 593 | fun new_names t xs = Name.variant_list | 
| 33042 | 594 | (union (op =) (map (fst o fst o dest_Var) (OldTerm.term_vars t)) | 
| 595 | (OldTerm.add_term_names (t, ML_Syntax.reserved_names))) (map mk_id xs); | |
| 11520 | 596 | |
| 597 | fun new_name t x = hd (new_names t [x]); | |
| 598 | ||
| 20665 | 599 | fun if_library x y = if member (op =) (!mode) "library" then x else y; | 
| 17144 | 600 | |
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 601 | fun default_codegen thy defs dep module brack t gr = | 
| 11520 | 602 | let | 
| 603 | val (u, ts) = strip_comb t; | |
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 604 | fun codegens brack = fold_map (invoke_codegen thy defs dep module brack) | 
| 11520 | 605 | in (case u of | 
| 14105 | 606 | Var ((s, i), T) => | 
| 607 | let | |
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 608 | val (ps, gr') = codegens true ts gr; | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 609 | val (_, gr'') = invoke_tycodegen thy defs dep module false T gr' | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 610 | in SOME (mk_app brack (str (s ^ | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 611 | (if i=0 then "" else string_of_int i))) ps, gr'') | 
| 11520 | 612 | end | 
| 613 | ||
| 14105 | 614 | | Free (s, T) => | 
| 615 | let | |
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 616 | val (ps, gr') = codegens true ts gr; | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 617 | val (_, gr'') = invoke_tycodegen thy defs dep module false T gr' | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 618 | in SOME (mk_app brack (str s) ps, gr'') end | 
| 11520 | 619 | |
| 620 | | Const (s, T) => | |
| 22921 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 621 | (case get_assoc_code thy (s, T) of | 
| 16769 
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
 berghofe parents: 
16649diff
changeset | 622 | SOME (ms, aux) => | 
| 18281 | 623 | let val i = num_args_of ms | 
| 11520 | 624 | in if length ts < i then | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 625 | default_codegen thy defs dep module brack (eta_expand u ts i) gr | 
| 11520 | 626 | else | 
| 627 | let | |
| 12452 | 628 | val (ts1, ts2) = args_of ms ts; | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 629 | val (ps1, gr1) = codegens false ts1 gr; | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 630 | val (ps2, gr2) = codegens true ts2 gr1; | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 631 | val (ps3, gr3) = codegens false (quotes_of ms) gr2; | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 632 | val (_, gr4) = invoke_tycodegen thy defs dep module false | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 633 | (funpow (length ts) (hd o tl o snd o dest_Type) T) gr3; | 
| 17144 | 634 | val (module', suffix) = (case get_defn thy defs s T of | 
| 27398 
768da1da59d6
simplified retrieval of theory names of consts and types
 haftmann parents: 
27353diff
changeset | 635 | NONE => (if_library (thyname_of_const thy s) module, "") | 
| 17144 | 636 | | SOME ((U, (module', _)), NONE) => | 
| 637 | (if_library module' module, "") | |
| 638 | | SOME ((U, (module', _)), SOME i) => | |
| 639 | (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 | 640 | val node_id = s ^ suffix; | 
| 17144 | 641 | fun p module' = mk_app brack (Pretty.block | 
| 642 | (pretty_mixfix module module' ms ps1 ps3)) ps2 | |
| 17549 | 643 | 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 | 644 | NONE => (case get_aux_code aux of | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 645 | [] => (p module, gr4) | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 646 | | xs => (p module', add_edge (node_id, dep) (new_node | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 647 | (node_id, (NONE, module', cat_lines xs ^ "\n")) gr4))) | 
| 17144 | 648 | | SOME (_, module'', _) => | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 649 | (p module'', add_edge (node_id, dep) gr4)) | 
| 11520 | 650 | end | 
| 651 | end | |
| 16649 | 652 | | NONE => (case get_defn thy defs s T of | 
| 15531 | 653 | NONE => NONE | 
| 32085 | 654 | | SOME ((U, (thyname, thm)), k) => (case prep_prim_def thy thm | 
| 655 | of SOME (_, (_, (args, rhs))) => let | |
| 17144 | 656 | val module' = if_library thyname module; | 
| 657 | val suffix = (case k of NONE => "" | SOME i => " def" ^ string_of_int i); | |
| 16649 | 658 | val node_id = s ^ suffix; | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 659 | val ((ps, def_id), gr') = gr |> codegens true ts | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 660 | ||>> mk_const_id module' (s ^ suffix); | 
| 26974 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 661 | val p = mk_app brack (str (mk_qual_id module def_id)) ps | 
| 17144 | 662 | in SOME (case try (get_node gr') node_id of | 
| 663 | NONE => | |
| 664 | let | |
| 665 |                      val _ = message ("expanding definition of " ^ s);
 | |
| 18788 
4f4ed2a01152
Fixed bug in code generator for primitive definitions that
 berghofe parents: 
18728diff
changeset | 666 | val (Ts, _) = strip_type U; | 
| 17144 | 667 | val (args', rhs') = | 
| 668 | if not (null args) orelse null Ts then (args, rhs) else | |
| 669 | let val v = Free (new_name rhs "x", hd Ts) | |
| 670 | in ([v], betapply (rhs, v)) end; | |
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 671 | val (p', gr1) = invoke_codegen thy defs node_id module' false | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 672 | rhs' (add_edge (node_id, dep) | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 673 | (new_node (node_id, (NONE, "", "")) gr')); | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 674 | val (xs, gr2) = codegens false args' gr1; | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 675 | val (_, gr3) = invoke_tycodegen thy defs dep module false T gr2; | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 676 | val (ty, gr4) = invoke_tycodegen thy defs node_id module' false U gr3; | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 677 | in (p, map_node node_id (K (NONE, module', string_of | 
| 17144 | 678 | (Pretty.block (separate (Pretty.brk 1) | 
| 679 | (if null args' then | |
| 26974 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 680 |                             [str ("val " ^ snd def_id ^ " :"), ty]
 | 
| 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 681 |                           else str ("fun " ^ snd def_id) :: xs) @
 | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 682 | [str " =", Pretty.brk 1, p', str ";"])) ^ "\n\n")) gr4) | 
| 17144 | 683 | end | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 684 | | SOME _ => (p, add_edge (node_id, dep) gr')) | 
| 32085 | 685 | end | 
| 686 | | NONE => NONE))) | |
| 11520 | 687 | |
| 688 | | Abs _ => | |
| 689 | let | |
| 690 | val (bs, Ts) = ListPair.unzip (strip_abs_vars u); | |
| 691 | val t = strip_abs_body u | |
| 692 | val bs' = new_names t bs; | |
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 693 | val (ps, gr1) = codegens true ts gr; | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 694 | val (p, gr2) = invoke_codegen thy defs dep module false | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 695 | (subst_bounds (map Free (rev (bs' ~~ Ts)), t)) gr1; | 
| 11520 | 696 | in | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 697 |         SOME (mk_app brack (Pretty.block (str "(" :: pretty_fn bs' p @
 | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 698 | [str ")"])) ps, gr2) | 
| 11520 | 699 | end | 
| 700 | ||
| 15531 | 701 | | _ => NONE) | 
| 11520 | 702 | end; | 
| 703 | ||
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 704 | fun default_tycodegen thy defs dep module brack (TVar ((s, i), _)) gr = | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 705 | SOME (str (s ^ (if i = 0 then "" else string_of_int i)), gr) | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 706 | | default_tycodegen thy defs dep module brack (TFree (s, _)) gr = | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 707 | SOME (str s, gr) | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 708 | | default_tycodegen thy defs dep module brack (Type (s, Ts)) gr = | 
| 17521 | 709 | (case AList.lookup (op =) ((#types o CodegenData.get) thy) s of | 
| 15531 | 710 | NONE => NONE | 
| 16769 
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
 berghofe parents: 
16649diff
changeset | 711 | | SOME (ms, aux) => | 
| 12452 | 712 | let | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 713 | val (ps, gr') = fold_map | 
| 17144 | 714 | (invoke_tycodegen thy defs dep module false) | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 715 | (fst (args_of ms Ts)) gr; | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 716 | val (qs, gr'') = fold_map | 
| 17144 | 717 | (invoke_tycodegen thy defs dep module false) | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 718 | (quotes_of ms) gr'; | 
| 27398 
768da1da59d6
simplified retrieval of theory names of consts and types
 haftmann parents: 
27353diff
changeset | 719 | val module' = if_library (thyname_of_type thy s) module; | 
| 16769 
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
 berghofe parents: 
16649diff
changeset | 720 | val node_id = s ^ " (type)"; | 
| 17144 | 721 | fun p module' = Pretty.block (pretty_mixfix module module' ms ps qs) | 
| 722 | 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 | 723 | NONE => (case get_aux_code aux of | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 724 | [] => (p module', gr'') | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 725 | | xs => (p module', snd (mk_type_id module' s | 
| 17144 | 726 | (add_edge (node_id, dep) (new_node (node_id, | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 727 | (NONE, module', cat_lines xs ^ "\n")) gr''))))) | 
| 17144 | 728 | | SOME (_, module'', _) => | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 729 | (p module'', add_edge (node_id, dep) gr'')) | 
| 16769 
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
 berghofe parents: 
16649diff
changeset | 730 | end); | 
| 12452 | 731 | |
| 25892 
3ff9d646a66a
Test data generation and conversion to terms are now more closely
 berghofe parents: 
25400diff
changeset | 732 | fun mk_tuple [p] = p | 
| 26974 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 733 |   | mk_tuple ps = Pretty.block (str "(" ::
 | 
| 32952 | 734 | flat (separate [str ",", Pretty.brk 1] (map single ps)) @ [str ")"]); | 
| 25892 
3ff9d646a66a
Test data generation and conversion to terms are now more closely
 berghofe parents: 
25400diff
changeset | 735 | |
| 
3ff9d646a66a
Test data generation and conversion to terms are now more closely
 berghofe parents: 
25400diff
changeset | 736 | fun mk_let bindings body = | 
| 26974 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 737 | Pretty.blk (0, [str "let", Pretty.brk 1, | 
| 25892 
3ff9d646a66a
Test data generation and conversion to terms are now more closely
 berghofe parents: 
25400diff
changeset | 738 | Pretty.blk (0, separate Pretty.fbrk (map (fn (pat, rhs) => | 
| 26974 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 739 | Pretty.block [str "val ", pat, str " =", Pretty.brk 1, | 
| 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 740 | rhs, str ";"]) bindings)), | 
| 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 741 | Pretty.brk 1, str "in", Pretty.brk 1, body, | 
| 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 742 | Pretty.brk 1, str "end"]); | 
| 25892 
3ff9d646a66a
Test data generation and conversion to terms are now more closely
 berghofe parents: 
25400diff
changeset | 743 | |
| 16649 | 744 | fun mk_struct name s = "structure " ^ name ^ " =\nstruct\n\n" ^ s ^ "end;\n"; | 
| 745 | ||
| 28227 | 746 | fun add_to_module name s = AList.map_entry (op =) (name : string) (suffix s); | 
| 16649 | 747 | |
| 17144 | 748 | fun output_code gr module xs = | 
| 16649 | 749 | let | 
| 19482 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19473diff
changeset | 750 | val code = map_filter (fn s => | 
| 17144 | 751 | let val c as (_, module', _) = Graph.get_node gr s | 
| 752 | in if module = "" orelse module = module' then SOME (s, c) else NONE end) | |
| 753 | (rev (Graph.all_preds gr xs)); | |
| 16649 | 754 | fun string_of_cycle (a :: b :: cs) = | 
| 755 | let val SOME (x, y) = get_first (fn (x, (_, a', _)) => | |
| 756 | if a = a' then Option.map (pair x) | |
| 28375 | 757 | (find_first ((fn (_, b', _) => b' = b) o Graph.get_node gr) | 
| 16649 | 758 | (Graph.imm_succs gr x)) | 
| 759 | else NONE) code | |
| 760 | in x ^ " called by " ^ y ^ "\n" ^ string_of_cycle (b :: cs) end | |
| 761 | | string_of_cycle _ = "" | |
| 762 | in | |
| 17144 | 763 | if module = "" then | 
| 16649 | 764 | let | 
| 19046 
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
 wenzelm parents: 
18977diff
changeset | 765 | val modules = distinct (op =) (map (#2 o snd) code); | 
| 23178 | 766 | val mod_gr = fold_rev Graph.add_edge_acyclic | 
| 19482 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19473diff
changeset | 767 | (maps (fn (s, (_, module, _)) => map (pair module) | 
| 28375 | 768 | (filter_out (fn s => s = module) (map (#2 o Graph.get_node gr) | 
| 23178 | 769 | (Graph.imm_succs gr s)))) code) | 
| 770 | (fold_rev (Graph.new_node o rpair ()) modules Graph.empty); | |
| 16649 | 771 | val modules' = | 
| 772 | rev (Graph.all_preds mod_gr (map (#2 o Graph.get_node gr) xs)) | |
| 773 | in | |
| 23178 | 774 | List.foldl (fn ((_, (_, module, s)), ms) => add_to_module module s ms) | 
| 16649 | 775 | (map (rpair "") modules') code | 
| 776 | end handle Graph.CYCLES (cs :: _) => | |
| 777 |         error ("Cyclic dependency of modules:\n" ^ commas cs ^
 | |
| 778 | "\n" ^ string_of_cycle cs) | |
| 17144 | 779 | else [(module, implode (map (#3 o snd) code))] | 
| 16649 | 780 | end; | 
| 11520 | 781 | |
| 26974 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 782 | fun gen_generate_code prep_term thy modules module xs = | 
| 11520 | 783 | let | 
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
21836diff
changeset | 784 | val _ = (module <> "" orelse | 
| 28375 | 785 | member (op =) (!mode) "library" andalso forall (fn (s, _) => s = "") xs) | 
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
21836diff
changeset | 786 | orelse error "missing module name"; | 
| 17144 | 787 | val graphs = get_modules thy; | 
| 16649 | 788 | val defs = mk_deftab thy; | 
| 17144 | 789 |     val gr = new_node ("<Top>", (NONE, module, ""))
 | 
| 23178 | 790 | (List.foldl (fn ((gr, (tab1, tab2)), (gr', (tab1', tab2'))) => | 
| 17144 | 791 | (Graph.merge (fn ((_, module, _), (_, module', _)) => | 
| 792 | module = module') (gr, gr'), | |
| 793 | (merge_nametabs (tab1, tab1'), merge_nametabs (tab2, tab2')))) emptygr | |
| 17412 | 794 | (map (fn s => case Symtab.lookup graphs s of | 
| 17144 | 795 |                 NONE => error ("Undefined code module: " ^ s)
 | 
| 796 | | SOME gr => gr) modules)) | |
| 23655 
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
 wenzelm parents: 
23178diff
changeset | 797 |       handle Graph.DUP k => error ("Duplicate code for " ^ k);
 | 
| 16649 | 798 | fun expand (t as Abs _) = t | 
| 799 | | expand t = (case fastype_of t of | |
| 800 |           Type ("fun", [T, U]) => Abs ("x", T, t $ Bound 0) | _ => t);
 | |
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 801 | val (ps, gr') = fold_map (fn (s, t) => fn gr => apfst (pair s) | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 802 | (invoke_codegen thy defs "<Top>" module false t gr)) | 
| 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 803 | (map (apsnd (expand o preprocess_term thy o prep_term thy)) xs) gr; | 
| 19482 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19473diff
changeset | 804 | val code = map_filter | 
| 17144 | 805 |       (fn ("", _) => NONE
 | 
| 26974 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 806 | | (s', p) => SOME (string_of (Pretty.block | 
| 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 807 |           [str ("val " ^ s' ^ " ="), Pretty.brk 1, p, str ";"]))) ps;
 | 
| 17144 | 808 | val code' = space_implode "\n\n" code ^ "\n\n"; | 
| 809 | val code'' = | |
| 19482 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19473diff
changeset | 810 | map_filter (fn (name, s) => | 
| 20665 | 811 | if member (op =) (!mode) "library" andalso name = module andalso null code | 
| 17144 | 812 | then NONE | 
| 813 | else SOME (name, mk_struct name s)) | |
| 814 | ((if null code then I | |
| 815 | else add_to_module module code') | |
| 816 | (output_code (fst gr') (if_library "" module) ["<Top>"])) | |
| 16649 | 817 | in | 
| 17144 | 818 | (code'', del_nodes ["<Top>"] gr') | 
| 26974 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 819 | end; | 
| 11520 | 820 | |
| 22680 
31a2303f4283
cleaned/simplified Sign.read_typ, Thm.read_cterm etc.;
 wenzelm parents: 
22596diff
changeset | 821 | val generate_code_i = gen_generate_code Sign.cert_term; | 
| 32182 | 822 | val generate_code = | 
| 36610 
bafd82950e24
renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
 wenzelm parents: 
35845diff
changeset | 823 | gen_generate_code (Syntax.read_term o ProofContext.allow_dummies o ProofContext.init_global); | 
| 11520 | 824 | |
| 12452 | 825 | |
| 13753 
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
 berghofe parents: 
13731diff
changeset | 826 | (**** Reflection ****) | 
| 
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
 berghofe parents: 
13731diff
changeset | 827 | |
| 
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
 berghofe parents: 
13731diff
changeset | 828 | 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 | 829 | |
| 26974 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 830 | fun pretty_list xs = Pretty.block (str "[" :: | 
| 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 831 | flat (separate [str ",", Pretty.brk 1] (map single xs)) @ | 
| 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 832 | [str "]"]); | 
| 13753 
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
 berghofe parents: 
13731diff
changeset | 833 | |
| 26974 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 834 | fun mk_type p (TVar ((s, i), _)) = str | 
| 13753 
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
 berghofe parents: 
13731diff
changeset | 835 | (strip_tname s ^ (if i = 0 then "" else string_of_int i) ^ "T") | 
| 26974 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 836 | | mk_type p (TFree (s, _)) = str (strip_tname s ^ "T") | 
| 13753 
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
 berghofe parents: 
13731diff
changeset | 837 | | mk_type p (Type (s, Ts)) = (if p then parens else I) (Pretty.block | 
| 26974 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 838 |       [str "Type", Pretty.brk 1, str ("(\"" ^ s ^ "\","),
 | 
| 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 839 | Pretty.brk 1, pretty_list (map (mk_type false) Ts), str ")"]); | 
| 13753 
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
 berghofe parents: 
13731diff
changeset | 840 | |
| 26974 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 841 | fun mk_term_of gr module p (TVar ((s, i), _)) = str | 
| 13753 
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
 berghofe parents: 
13731diff
changeset | 842 | (strip_tname s ^ (if i = 0 then "" else string_of_int i) ^ "F") | 
| 26974 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 843 | | mk_term_of gr module p (TFree (s, _)) = str (strip_tname s ^ "F") | 
| 17144 | 844 | | mk_term_of gr module p (Type (s, Ts)) = (if p then parens else I) | 
| 16649 | 845 | (Pretty.block (separate (Pretty.brk 1) | 
| 26974 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 846 | (str (mk_qual_id module | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 847 | (get_type_id' (fn s' => "term_of_" ^ s') gr s)) :: | 
| 19482 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19473diff
changeset | 848 | maps (fn T => | 
| 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19473diff
changeset | 849 | [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 | 850 | |
| 
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
 berghofe parents: 
13731diff
changeset | 851 | |
| 14105 | 852 | (**** Test data generators ****) | 
| 853 | ||
| 26974 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 854 | fun mk_gen gr module p xs a (TVar ((s, i), _)) = str | 
| 14105 | 855 | (strip_tname s ^ (if i = 0 then "" else string_of_int i) ^ "G") | 
| 26974 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 856 | | mk_gen gr module p xs a (TFree (s, _)) = str (strip_tname s ^ "G") | 
| 25892 
3ff9d646a66a
Test data generation and conversion to terms are now more closely
 berghofe parents: 
25400diff
changeset | 857 | | mk_gen gr module p xs a (Type (tyc as (s, Ts))) = (if p then parens else I) | 
| 16649 | 858 | (Pretty.block (separate (Pretty.brk 1) | 
| 28537 
1e84256d1a8a
established canonical argument order in SML code generators
 haftmann parents: 
28375diff
changeset | 859 | (str (mk_qual_id module (get_type_id' (fn s' => "gen_" ^ s') gr s) ^ | 
| 20665 | 860 | (if member (op =) xs s then "'" else "")) :: | 
| 25892 
3ff9d646a66a
Test data generation and conversion to terms are now more closely
 berghofe parents: 
25400diff
changeset | 861 | (case tyc of | 
| 
3ff9d646a66a
Test data generation and conversion to terms are now more closely
 berghofe parents: 
25400diff
changeset | 862 |             ("fun", [T, U]) =>
 | 
| 
3ff9d646a66a
Test data generation and conversion to terms are now more closely
 berghofe parents: 
25400diff
changeset | 863 | [mk_term_of gr module true T, mk_type true T, | 
| 
3ff9d646a66a
Test data generation and conversion to terms are now more closely
 berghofe parents: 
25400diff
changeset | 864 | mk_gen gr module true xs a U, mk_type true U] | 
| 
3ff9d646a66a
Test data generation and conversion to terms are now more closely
 berghofe parents: 
25400diff
changeset | 865 | | _ => maps (fn T => | 
| 
3ff9d646a66a
Test data generation and conversion to terms are now more closely
 berghofe parents: 
25400diff
changeset | 866 | [mk_gen gr module true xs a T, mk_type true T]) Ts) @ | 
| 26974 
83adc1eaeaab
Replaced Pretty.str and Pretty.string_of by specific functions that
 berghofe parents: 
26939diff
changeset | 867 | (if member (op =) xs s then [str a] else [])))); | 
| 14105 | 868 | |
| 32738 | 869 | val test_fn : (int -> term list option) Unsynchronized.ref = | 
| 870 | Unsynchronized.ref (fn _ => NONE); | |
| 14105 | 871 | |
| 35378 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35232diff
changeset | 872 | fun test_term ctxt report t = | 
| 28309 | 873 | let | 
| 874 | val thy = ProofContext.theory_of ctxt; | |
| 32966 
5b21661fe618
indicate CRITICAL nature of various setmp combinators;
 wenzelm parents: 
32952diff
changeset | 875 | val (code, gr) = setmp_CRITICAL mode ["term_of", "test"] | 
| 28309 | 876 |       (generate_code_i thy [] "Generated") [("testf", t)];
 | 
| 31145 | 877 | val Ts = map snd (fst (strip_abs t)); | 
| 878 |     val args = map_index (fn (i, T) => ("arg" ^ string_of_int i, T)) Ts;
 | |
| 28309 | 879 | val s = "structure TestTerm =\nstruct\n\n" ^ | 
| 880 | cat_lines (map snd code) ^ | |
| 881 | "\nopen Generated;\n\n" ^ string_of | |
| 882 | (Pretty.block [str "val () = Codegen.test_fn :=", | |
| 883 |           Pretty.brk 1, str ("(fn i =>"), Pretty.brk 1,
 | |
| 31145 | 884 | mk_let (map (fn (s, T) => | 
| 885 | (mk_tuple [str s, str (s ^ "_t")], | |
| 28309 | 886 | Pretty.block [mk_gen gr "Generated" false [] "" T, Pretty.brk 1, | 
| 31145 | 887 | str "i"])) args) | 
| 28309 | 888 | (Pretty.block [str "if ", | 
| 31145 | 889 | mk_app false (str "testf") (map (str o fst) args), | 
| 28309 | 890 | Pretty.brk 1, str "then NONE", | 
| 891 | Pretty.brk 1, str "else ", | |
| 892 | Pretty.block [str "SOME ", Pretty.block (str "[" :: | |
| 31145 | 893 | Pretty.commas (map (fn (s, _) => str (s ^ "_t ()")) args) @ | 
| 28309 | 894 | [str "]"])]]), | 
| 895 | str ");"]) ^ | |
| 896 | "\n\nend;\n"; | |
| 37198 
3af985b10550
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
 wenzelm parents: 
37146diff
changeset | 897 | val _ = ML_Context.eval_text_in (SOME ctxt) false Position.none s; | 
| 35378 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35232diff
changeset | 898 | val dummy_report = ([], false) | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35232diff
changeset | 899 | in (fn size => (! test_fn size, dummy_report)) end; | 
| 28309 | 900 | |
| 24456 | 901 | |
| 14105 | 902 | |
| 17549 | 903 | (**** Evaluator for terms ****) | 
| 904 | ||
| 32738 | 905 | val eval_result = Unsynchronized.ref (fn () => Bound 0); | 
| 17549 | 906 | |
| 25009 
61946780de00
eval_term: moved actual evaluation out of CRITICAL section;
 wenzelm parents: 
24920diff
changeset | 907 | fun eval_term thy t = | 
| 28271 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 908 | let | 
| 36610 
bafd82950e24
renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
 wenzelm parents: 
35845diff
changeset | 909 | val ctxt = ProofContext.init_global thy; | 
| 28271 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 910 | val e = | 
| 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 911 | let | 
| 29266 | 912 | val _ = (null (Term.add_tvars t []) andalso null (Term.add_tfrees t [])) orelse | 
| 28271 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 913 | error "Term to be evaluated contains type variables"; | 
| 29266 | 914 | val _ = (null (Term.add_vars t []) andalso null (Term.add_frees t [])) orelse | 
| 28271 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 915 | error "Term to be evaluated contains variables"; | 
| 32966 
5b21661fe618
indicate CRITICAL nature of various setmp combinators;
 wenzelm parents: 
32952diff
changeset | 916 | val (code, gr) = setmp_CRITICAL mode ["term_of"] | 
| 28271 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 917 | (generate_code_i thy [] "Generated") | 
| 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 918 |           [("result", Abs ("x", TFree ("'a", []), t))];
 | 
| 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 919 | val s = "structure EvalTerm =\nstruct\n\n" ^ | 
| 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 920 | cat_lines (map snd code) ^ | 
| 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 921 | "\nopen Generated;\n\n" ^ string_of | 
| 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 922 | (Pretty.block [str "val () = Codegen.eval_result := (fn () =>", | 
| 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 923 | Pretty.brk 1, | 
| 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 924 | mk_app false (mk_term_of gr "Generated" false (fastype_of t)) | 
| 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 925 | [str "(result ())"], | 
| 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 926 | str ");"]) ^ | 
| 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 927 | "\n\nend;\n"; | 
| 33222 | 928 | val _ = NAMED_CRITICAL "codegen" (fn () => (* FIXME ??? *) | 
| 37198 
3af985b10550
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
 wenzelm parents: 
37146diff
changeset | 929 | ML_Context.eval_text_in (SOME ctxt) false Position.none s); | 
| 28271 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 930 | in !eval_result end; | 
| 25009 
61946780de00
eval_term: moved actual evaluation out of CRITICAL section;
 wenzelm parents: 
24920diff
changeset | 931 | in e () end; | 
| 17549 | 932 | |
| 28293 | 933 | val (_, evaluation_conv) = Context.>>> (Context.map_theory_result | 
| 30288 
a32700e45ab3
Thm.add_oracle interface: replaced old bstring by binding;
 wenzelm parents: 
29270diff
changeset | 934 | (Thm.add_oracle (Binding.name "evaluation", fn ct => | 
| 28293 | 935 | let | 
| 936 | val thy = Thm.theory_of_cterm ct; | |
| 937 | val t = Thm.term_of ct; | |
| 938 | in Thm.cterm_of thy (Logic.mk_equals (t, eval_term thy t)) end))); | |
| 20599 | 939 | |
| 17549 | 940 | |
| 12452 | 941 | (**** Interface ****) | 
| 942 | ||
| 11520 | 943 | fun parse_mixfix rd s = | 
| 944 | (case Scan.finite Symbol.stopper (Scan.repeat | |
| 945 | ( $$ "_" >> K Arg | |
| 946 | || $$ "?" >> K Ignore | |
| 16769 
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
 berghofe parents: 
16649diff
changeset | 947 | || $$ "\\<module>" >> K Module | 
| 11520 | 948 | || $$ "/" |-- Scan.repeat ($$ " ") >> (Pretty o Pretty.brk o length) | 
| 949 |       || $$ "{" |-- $$ "*" |-- Scan.repeat1
 | |
| 23784 
75e6b9dd5336
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
 wenzelm parents: 
23655diff
changeset | 950 | ( $$ "'" |-- Scan.one Symbol.is_regular | 
| 
75e6b9dd5336
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
 wenzelm parents: 
23655diff
changeset | 951 | || Scan.unless ($$ "*" -- $$ "}") (Scan.one Symbol.is_regular)) --| | 
| 12452 | 952 | $$ "*" --| $$ "}" >> (Quote o rd o implode) | 
| 11520 | 953 | || Scan.repeat1 | 
| 23784 
75e6b9dd5336
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
 wenzelm parents: 
23655diff
changeset | 954 | ( $$ "'" |-- Scan.one Symbol.is_regular | 
| 16769 
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
 berghofe parents: 
16649diff
changeset | 955 |             || Scan.unless ($$ "_" || $$ "?" || $$ "\\<module>" || $$ "/" || $$ "{" |-- $$ "*")
 | 
| 23784 
75e6b9dd5336
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
 wenzelm parents: 
23655diff
changeset | 956 | (Scan.one Symbol.is_regular)) >> (Pretty o str o implode))) | 
| 11520 | 957 | (Symbol.explode s) of | 
| 958 | (p, []) => p | |
| 959 |    | _ => error ("Malformed annotation: " ^ quote s));
 | |
| 960 | ||
| 15801 | 961 | |
| 36950 | 962 | val _ = List.app Keyword.keyword ["attach", "file", "contains"]; | 
| 24867 | 963 | |
| 28375 | 964 | fun strip_whitespace s = implode (fst (take_suffix (fn c => c = "\n" orelse c = " ") | 
| 965 | (snd (take_prefix (fn c => c = "\n" orelse c = " ") (explode s))))) ^ "\n"; | |
| 16769 
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
 berghofe parents: 
16649diff
changeset | 966 | |
| 36950 | 967 | val parse_attach = Scan.repeat (Parse.$$$ "attach" |-- | 
| 968 |   Scan.optional (Parse.$$$ "(" |-- Parse.xname --| Parse.$$$ ")") "" --
 | |
| 969 | (Parse.verbatim >> strip_whitespace)); | |
| 16769 
7f188f2127f7
Implemented mechanism for attaching auxiliary code to consts_code and
 berghofe parents: 
16649diff
changeset | 970 | |
| 24867 | 971 | val _ = | 
| 36953 
2af1ad9aa1a3
renamed structure OuterSyntax to Outer_Syntax, keeping the old name as alias for some time;
 wenzelm parents: 
36950diff
changeset | 972 | Outer_Syntax.command "types_code" | 
| 36950 | 973 | "associate types with target language types" Keyword.thy_decl | 
| 974 |     (Scan.repeat1 (Parse.xname --| Parse.$$$ "(" -- Parse.string --| Parse.$$$ ")" -- parse_attach) >>
 | |
| 22921 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 975 | (fn xs => Toplevel.theory (fn thy => fold (assoc_type o | 
| 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 976 | (fn ((name, mfx), aux) => (name, (parse_mixfix | 
| 24707 | 977 | (Syntax.read_typ_global thy) mfx, aux)))) xs thy))); | 
| 11520 | 978 | |
| 24867 | 979 | val _ = | 
| 36953 
2af1ad9aa1a3
renamed structure OuterSyntax to Outer_Syntax, keeping the old name as alias for some time;
 wenzelm parents: 
36950diff
changeset | 980 | Outer_Syntax.command "consts_code" | 
| 36950 | 981 | "associate constants with target language code" Keyword.thy_decl | 
| 11520 | 982 | (Scan.repeat1 | 
| 36950 | 983 | (Parse.term --| | 
| 984 |         Parse.$$$ "(" -- Parse.string --| Parse.$$$ ")" -- parse_attach) >>
 | |
| 25376 
87824cc5ff90
auto_quickcheck ref: set default in ProofGeneral/preferences only
 wenzelm parents: 
25367diff
changeset | 985 | (fn xs => Toplevel.theory (fn thy => fold (assoc_const o | 
| 22921 
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
 haftmann parents: 
22846diff
changeset | 986 | (fn ((const, mfx), aux) => | 
| 28640 
188e9557c572
- removed test_params from CodegenData (now in Pure/Tools/quickcheck.ML)
 berghofe parents: 
28537diff
changeset | 987 | (const, (parse_mixfix (Syntax.read_term_global thy) mfx, aux)))) xs thy))); | 
| 11520 | 988 | |
| 17144 | 989 | fun parse_code lib = | 
| 36950 | 990 |   Scan.optional (Parse.$$$ "(" |-- Parse.enum "," Parse.xname --| Parse.$$$ ")") (!mode) --
 | 
| 991 | (if lib then Scan.optional Parse.name "" else Parse.name) -- | |
| 992 | Scan.option (Parse.$$$ "file" |-- Parse.name) -- | |
| 17144 | 993 | (if lib then Scan.succeed [] | 
| 36950 | 994 | else Scan.optional (Parse.$$$ "imports" |-- Scan.repeat1 Parse.name) []) --| | 
| 995 | Parse.$$$ "contains" -- | |
| 996 | ( Scan.repeat1 (Parse.name --| Parse.$$$ "=" -- Parse.term) | |
| 997 | || Scan.repeat1 (Parse.term >> pair "")) >> | |
| 17144 | 998 | (fn ((((mode', module), opt_fname), modules), xs) => Toplevel.theory (fn thy => | 
| 28271 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 999 | let | 
| 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 1000 | val mode'' = (if lib then insert (op =) "library" else I) (remove (op =) "library" mode'); | 
| 32966 
5b21661fe618
indicate CRITICAL nature of various setmp combinators;
 wenzelm parents: 
32952diff
changeset | 1001 | val (code, gr) = setmp_CRITICAL mode mode'' (generate_code thy modules module) xs; | 
| 28271 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 1002 | val thy' = thy |> Context.theory_map (ML_Context.exec (fn () => | 
| 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 1003 | (case opt_fname of | 
| 37198 
3af985b10550
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
 wenzelm parents: 
37146diff
changeset | 1004 | NONE => ML_Context.eval_text false Position.none (cat_lines (map snd code)) | 
| 28271 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 1005 | | SOME fname => | 
| 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 1006 | if lib then app (fn (name, s) => File.write | 
| 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 1007 | (Path.append (Path.explode fname) (Path.basic (name ^ ".ML"))) s) | 
| 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 1008 |               (("ROOT", implode (map (fn (name, _) =>
 | 
| 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 1009 | "use \"" ^ name ^ ".ML\";\n") code)) :: code) | 
| 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 1010 | else File.write (Path.explode fname) (snd (hd code))))); | 
| 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 1011 | in | 
| 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 1012 | if lib then thy' | 
| 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 1013 | else map_modules (Symtab.update (module, gr)) thy' | 
| 
0e1b07ded55f
simplified ML_Context.eval_in -- expect immutable Proof.context value;
 wenzelm parents: 
28227diff
changeset | 1014 | end)); | 
| 17144 | 1015 | |
| 28227 | 1016 | val setup = add_codegen "default" default_codegen | 
| 1017 | #> add_tycodegen "default" default_tycodegen | |
| 28640 
188e9557c572
- removed test_params from CodegenData (now in Pure/Tools/quickcheck.ML)
 berghofe parents: 
28537diff
changeset | 1018 | #> add_preprocessor unfold_preprocessor; | 
| 28227 | 1019 | |
| 24867 | 1020 | val _ = | 
| 36953 
2af1ad9aa1a3
renamed structure OuterSyntax to Outer_Syntax, keeping the old name as alias for some time;
 wenzelm parents: 
36950diff
changeset | 1021 | Outer_Syntax.command "code_library" | 
| 37944 | 1022 | "generate code for terms (one structure for each theory)" Keyword.thy_decl | 
| 17144 | 1023 | (parse_code true); | 
| 1024 | ||
| 24867 | 1025 | val _ = | 
| 36953 
2af1ad9aa1a3
renamed structure OuterSyntax to Outer_Syntax, keeping the old name as alias for some time;
 wenzelm parents: 
36950diff
changeset | 1026 | Outer_Syntax.command "code_module" | 
| 37944 | 1027 | "generate code for terms (single structure, incremental)" Keyword.thy_decl | 
| 17144 | 1028 | (parse_code false); | 
| 11520 | 1029 | |
| 1030 | end; |