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