author | wenzelm |
Fri, 20 Mar 2009 20:21:38 +0100 | |
changeset 30613 | b22d35d9ef28 |
parent 30528 | 7173bf123335 |
child 30722 | 623d4831c8cf |
permissions | -rw-r--r-- |
24219 | 1 |
(* Title: Pure/Isar/code.ML |
2 |
Author: Florian Haftmann, TU Muenchen |
|
3 |
||
4 |
Abstract executable content of theory. Management of data dependent on |
|
25485 | 5 |
executable content. Cache assumes non-concurrent processing of a single theory. |
24219 | 6 |
*) |
7 |
||
8 |
signature CODE = |
|
9 |
sig |
|
28368 | 10 |
val add_eqn: thm -> theory -> theory |
11 |
val add_nonlinear_eqn: thm -> theory -> theory |
|
12 |
val add_default_eqn: thm -> theory -> theory |
|
28703 | 13 |
val add_default_eqn_attribute: attribute |
14 |
val add_default_eqn_attrib: Attrib.src |
|
28368 | 15 |
val del_eqn: thm -> theory -> theory |
16 |
val del_eqns: string -> theory -> theory |
|
28971 | 17 |
val add_eqnl: string * (thm * bool) list lazy -> theory -> theory |
29302 | 18 |
val map_pre: (simpset -> simpset) -> theory -> theory |
19 |
val map_post: (simpset -> simpset) -> theory -> theory |
|
24219 | 20 |
val add_inline: thm -> theory -> theory |
28423 | 21 |
val add_functrans: string * (theory -> (thm * bool) list -> (thm * bool) list option) -> theory -> theory |
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
22 |
val del_functrans: string -> theory -> theory |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
23 |
val add_datatype: (string * typ) list -> theory -> theory |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
24 |
val add_datatype_cmd: string list -> theory -> theory |
25485 | 25 |
val type_interpretation: |
26 |
(string * ((string * sort) list * (string * typ list) list) |
|
27 |
-> theory -> theory) -> theory -> theory |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
28 |
val add_case: thm -> theory -> theory |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
29 |
val add_undefined: string -> theory -> theory |
27675 | 30 |
val purge_data: theory -> theory |
24219 | 31 |
|
32 |
val coregular_algebra: theory -> Sorts.algebra |
|
33 |
val operational_algebra: theory -> (sort -> sort) * Sorts.algebra |
|
28368 | 34 |
val these_eqns: theory -> string -> (thm * bool) list |
28525 | 35 |
val these_raw_eqns: theory -> string -> (thm * bool) list |
24219 | 36 |
val get_datatype: theory -> string -> ((string * sort) list * (string * typ list) list) |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
37 |
val get_datatype_of_constr: theory -> string -> string option |
30023
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
38 |
val get_case_scheme: theory -> string -> (int * (int * string list)) option |
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
39 |
val is_undefined: theory -> string -> bool |
28423 | 40 |
val default_typscheme: theory -> string -> (string * sort) list * typ |
24219 | 41 |
|
28423 | 42 |
val preprocess_conv: theory -> cterm -> thm |
24837 | 43 |
val preprocess_term: theory -> term -> term |
28423 | 44 |
val postprocess_conv: theory -> cterm -> thm |
24837 | 45 |
val postprocess_term: theory -> term -> term |
24219 | 46 |
|
30513 | 47 |
val add_attribute: string * attribute parser -> theory -> theory |
24219 | 48 |
|
49 |
val print_codesetup: theory -> unit |
|
50 |
end; |
|
51 |
||
52 |
signature CODE_DATA_ARGS = |
|
53 |
sig |
|
54 |
type T |
|
55 |
val empty: T |
|
27609 | 56 |
val purge: theory -> string list -> T -> T |
24219 | 57 |
end; |
58 |
||
59 |
signature CODE_DATA = |
|
60 |
sig |
|
61 |
type T |
|
62 |
val get: theory -> T |
|
63 |
val change: theory -> (T -> T) -> T |
|
64 |
val change_yield: theory -> (T -> 'a * T) -> 'a * T |
|
65 |
end; |
|
66 |
||
67 |
signature PRIVATE_CODE = |
|
68 |
sig |
|
69 |
include CODE |
|
27609 | 70 |
val declare_data: Object.T -> (theory -> string list -> Object.T -> Object.T) |
71 |
-> serial |
|
24219 | 72 |
val get_data: serial * ('a -> Object.T) * (Object.T -> 'a) |
73 |
-> theory -> 'a |
|
74 |
val change_data: serial * ('a -> Object.T) * (Object.T -> 'a) |
|
75 |
-> theory -> ('a -> 'a) -> 'a |
|
76 |
val change_yield_data: serial * ('a -> Object.T) * (Object.T -> 'a) |
|
77 |
-> theory -> ('a -> 'b * 'a) -> 'b * 'a |
|
78 |
end; |
|
79 |
||
80 |
structure Code : PRIVATE_CODE = |
|
81 |
struct |
|
82 |
||
25312 | 83 |
(** code attributes **) |
84 |
||
85 |
structure CodeAttr = TheoryDataFun ( |
|
30513 | 86 |
type T = (string * attribute parser) list; |
25312 | 87 |
val empty = []; |
88 |
val copy = I; |
|
89 |
val extend = I; |
|
26970 | 90 |
fun merge _ = AList.merge (op = : string * string -> bool) (K true); |
25312 | 91 |
); |
24219 | 92 |
|
25312 | 93 |
fun add_attribute (attr as (name, _)) = |
94 |
let |
|
28562 | 95 |
fun add_parser ("", parser) attrs = attrs |> rev |> AList.update (op =) ("", parser) |> rev |
25312 | 96 |
| add_parser (name, parser) attrs = (name, Args.$$$ name |-- parser) :: attrs; |
28562 | 97 |
in CodeAttr.map (fn attrs => if not (name = "") andalso AList.defined (op =) attrs name |
98 |
then error ("Code attribute " ^ name ^ " already declared") else add_parser attr attrs) |
|
25312 | 99 |
end; |
100 |
||
101 |
val _ = |
|
102 |
let |
|
30528 | 103 |
val code_attr = Scan.peek (fn context => |
104 |
List.foldr op || Scan.fail (map snd (CodeAttr.get (Context.theory_of context)))); |
|
25312 | 105 |
in |
26463 | 106 |
Context.>> (Context.map_theory |
30528 | 107 |
(Attrib.setup (Binding.name "code") code_attr "declare theorems for code generation")) |
25312 | 108 |
end; |
109 |
||
110 |
||
28143 | 111 |
(** logical and syntactical specification of executable code **) |
24219 | 112 |
|
30023
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
113 |
(* code equations *) |
28695 | 114 |
|
28971 | 115 |
type eqns = bool * (thm * bool) list lazy; |
28695 | 116 |
(*default flag, theorems with linear flag (perhaps lazy)*) |
24219 | 117 |
|
28673
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents:
28672
diff
changeset
|
118 |
fun pretty_lthms ctxt r = case Lazy.peek r |
28672 | 119 |
of SOME thms => map (ProofContext.pretty_thm ctxt o fst) (Exn.release thms) |
24219 | 120 |
| NONE => [Pretty.str "[...]"]; |
121 |
||
122 |
fun certificate thy f r = |
|
28673
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents:
28672
diff
changeset
|
123 |
case Lazy.peek r |
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents:
28672
diff
changeset
|
124 |
of SOME thms => (Lazy.value o f thy) (Exn.release thms) |
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
125 |
| NONE => let |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
126 |
val thy_ref = Theory.check_thy thy; |
28673
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents:
28672
diff
changeset
|
127 |
in Lazy.lazy (fn () => (f (Theory.deref thy_ref) o Lazy.force) r) end; |
24219 | 128 |
|
28423 | 129 |
fun add_drop_redundant thy (thm, linear) thms = |
24219 | 130 |
let |
131 |
val args_of = snd o strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of; |
|
132 |
val args = args_of thm; |
|
28403 | 133 |
val incr_idx = Logic.incr_indexes ([], Thm.maxidx_of thm + 1); |
28350 | 134 |
fun matches_args args' = length args <= length args' andalso |
28403 | 135 |
Pattern.matchess thy (args, (map incr_idx o curry Library.take (length args)) args'); |
28359 | 136 |
fun drop (thm', linear') = if (linear orelse not linear') |
137 |
andalso matches_args (args_of thm') then |
|
30023
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
138 |
(warning ("Code generator: dropping redundant code equation\n" ^ Display.string_of_thm thm'); true) |
28350 | 139 |
else false; |
140 |
in (thm, linear) :: filter_out drop thms end; |
|
24219 | 141 |
|
28673
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents:
28672
diff
changeset
|
142 |
fun add_thm thy _ thm (false, thms) = (false, Lazy.map_force (add_drop_redundant thy thm) thms) |
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents:
28672
diff
changeset
|
143 |
| add_thm thy true thm (true, thms) = (true, Lazy.map_force (fn thms => thms @ [thm]) thms) |
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents:
28672
diff
changeset
|
144 |
| add_thm thy false thm (true, thms) = (false, Lazy.value [thm]); |
24219 | 145 |
|
28143 | 146 |
fun add_lthms lthms _ = (false, lthms); |
25312 | 147 |
|
28673
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
wenzelm
parents:
28672
diff
changeset
|
148 |
fun del_thm thm = (apsnd o Lazy.map_force) (remove (eq_fst Thm.eq_thm_prop) (thm, true)); |
25312 | 149 |
|
28143 | 150 |
|
151 |
(* specification data *) |
|
152 |
||
24219 | 153 |
datatype spec = Spec of { |
28695 | 154 |
concluded_history: bool, |
155 |
eqns: ((bool * eqns) * (serial * eqns) list) Symtab.table |
|
156 |
(*with explicit history*), |
|
157 |
dtyps: ((serial * ((string * sort) list * (string * typ list) list)) list) Symtab.table |
|
158 |
(*with explicit history*), |
|
30076 | 159 |
cases: (int * (int * string list)) Symtab.table * unit Symtab.table |
24219 | 160 |
}; |
161 |
||
28695 | 162 |
fun mk_spec ((concluded_history, eqns), (dtyps, cases)) = |
163 |
Spec { concluded_history = concluded_history, eqns = eqns, dtyps = dtyps, cases = cases }; |
|
164 |
fun map_spec f (Spec { concluded_history = concluded_history, eqns = eqns, |
|
165 |
dtyps = dtyps, cases = cases }) = |
|
166 |
mk_spec (f ((concluded_history, eqns), (dtyps, cases))); |
|
167 |
fun merge_spec (Spec { concluded_history = _, eqns = eqns1, dtyps = dtyps1, cases = (cases1, undefs1) }, |
|
168 |
Spec { concluded_history = _, eqns = eqns2, dtyps = dtyps2, cases = (cases2, undefs2) }) = |
|
24219 | 169 |
let |
28695 | 170 |
fun merge_eqns ((_, history1), (_, history2)) = |
171 |
let |
|
172 |
val raw_history = AList.merge (op =) (K true) (history1, history2) |
|
173 |
val filtered_history = filter_out (fst o snd) raw_history |
|
174 |
val history = if null filtered_history |
|
175 |
then raw_history else filtered_history; |
|
176 |
in ((false, (snd o hd) history), history) end; |
|
177 |
val eqns = Symtab.join (K merge_eqns) (eqns1, eqns2); |
|
178 |
val dtyps = Symtab.join (K (AList.merge (op =) (K true))) (dtyps1, dtyps2); |
|
27609 | 179 |
val cases = (Symtab.merge (K true) (cases1, cases2), |
180 |
Symtab.merge (K true) (undefs1, undefs2)); |
|
28695 | 181 |
in mk_spec ((false, eqns), (dtyps, cases)) end; |
24219 | 182 |
|
25312 | 183 |
|
184 |
(* pre- and postprocessor *) |
|
185 |
||
186 |
datatype thmproc = Thmproc of { |
|
29302 | 187 |
pre: simpset, |
188 |
post: simpset, |
|
28423 | 189 |
functrans: (string * (serial * (theory -> (thm * bool) list -> (thm * bool) list option))) list |
25312 | 190 |
}; |
191 |
||
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
192 |
fun mk_thmproc ((pre, post), functrans) = |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
193 |
Thmproc { pre = pre, post = post, functrans = functrans }; |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
194 |
fun map_thmproc f (Thmproc { pre, post, functrans }) = |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
195 |
mk_thmproc (f ((pre, post), functrans)); |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
196 |
fun merge_thmproc (Thmproc { pre = pre1, post = post1, functrans = functrans1 }, |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
197 |
Thmproc { pre = pre2, post = post2, functrans = functrans2 }) = |
25312 | 198 |
let |
29302 | 199 |
val pre = Simplifier.merge_ss (pre1, pre2); |
200 |
val post = Simplifier.merge_ss (post1, post2); |
|
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
201 |
val functrans = AList.merge (op =) (eq_fst (op =)) (functrans1, functrans2); |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
202 |
in mk_thmproc ((pre, post), functrans) end; |
25312 | 203 |
|
24219 | 204 |
datatype exec = Exec of { |
205 |
thmproc: thmproc, |
|
206 |
spec: spec |
|
207 |
}; |
|
208 |
||
28143 | 209 |
|
210 |
(* code setup data *) |
|
211 |
||
24219 | 212 |
fun mk_exec (thmproc, spec) = |
213 |
Exec { thmproc = thmproc, spec = spec }; |
|
214 |
fun map_exec f (Exec { thmproc = thmproc, spec = spec }) = |
|
215 |
mk_exec (f (thmproc, spec)); |
|
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
216 |
fun merge_exec (Exec { thmproc = thmproc1, spec = spec1 }, |
24219 | 217 |
Exec { thmproc = thmproc2, spec = spec2 }) = |
218 |
let |
|
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
219 |
val thmproc = merge_thmproc (thmproc1, thmproc2); |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
220 |
val spec = merge_spec (spec1, spec2); |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
221 |
in mk_exec (thmproc, spec) end; |
29302 | 222 |
val empty_exec = mk_exec (mk_thmproc ((Simplifier.empty_ss, Simplifier.empty_ss), []), |
28695 | 223 |
mk_spec ((false, Symtab.empty), (Symtab.empty, (Symtab.empty, Symtab.empty)))); |
24219 | 224 |
|
25312 | 225 |
fun the_thmproc (Exec { thmproc = Thmproc x, ...}) = x; |
24219 | 226 |
fun the_spec (Exec { spec = Spec x, ...}) = x; |
28368 | 227 |
val the_eqns = #eqns o the_spec; |
24219 | 228 |
val the_dtyps = #dtyps o the_spec; |
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
229 |
val the_cases = #cases o the_spec; |
24219 | 230 |
val map_thmproc = map_exec o apfst o map_thmproc; |
28695 | 231 |
val map_concluded_history = map_exec o apsnd o map_spec o apfst o apfst; |
232 |
val map_eqns = map_exec o apsnd o map_spec o apfst o apsnd; |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
233 |
val map_dtyps = map_exec o apsnd o map_spec o apsnd o apfst; |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
234 |
val map_cases = map_exec o apsnd o map_spec o apsnd o apsnd; |
24219 | 235 |
|
236 |
||
237 |
(* data slots dependent on executable content *) |
|
238 |
||
239 |
(*private copy avoids potential conflict of table exceptions*) |
|
240 |
structure Datatab = TableFun(type key = int val ord = int_ord); |
|
241 |
||
242 |
local |
|
243 |
||
244 |
type kind = { |
|
245 |
empty: Object.T, |
|
27609 | 246 |
purge: theory -> string list -> Object.T -> Object.T |
24219 | 247 |
}; |
248 |
||
249 |
val kinds = ref (Datatab.empty: kind Datatab.table); |
|
250 |
val kind_keys = ref ([]: serial list); |
|
251 |
||
252 |
fun invoke f k = case Datatab.lookup (! kinds) k |
|
253 |
of SOME kind => f kind |
|
254 |
| NONE => sys_error "Invalid code data identifier"; |
|
255 |
||
256 |
in |
|
257 |
||
27609 | 258 |
fun declare_data empty purge = |
24219 | 259 |
let |
260 |
val k = serial (); |
|
27609 | 261 |
val kind = {empty = empty, purge = purge}; |
24219 | 262 |
val _ = change kinds (Datatab.update (k, kind)); |
263 |
val _ = change kind_keys (cons k); |
|
264 |
in k end; |
|
265 |
||
27609 | 266 |
fun invoke_init k = invoke (fn kind => #empty kind) k; |
24219 | 267 |
|
27609 | 268 |
fun invoke_purge_all thy cs = |
24219 | 269 |
fold (fn k => Datatab.map_entry k |
27609 | 270 |
(invoke (fn kind => #purge kind thy cs) k)) (! kind_keys); |
24219 | 271 |
|
272 |
end; (*local*) |
|
273 |
||
274 |
||
25312 | 275 |
(** theory store **) |
24219 | 276 |
|
277 |
local |
|
278 |
||
279 |
type data = Object.T Datatab.table; |
|
27609 | 280 |
val empty_data = Datatab.empty : data; |
24219 | 281 |
|
282 |
structure CodeData = TheoryDataFun |
|
283 |
( |
|
284 |
type T = exec * data ref; |
|
27609 | 285 |
val empty = (empty_exec, ref empty_data); |
24219 | 286 |
fun copy (exec, data) = (exec, ref (! data)); |
287 |
val extend = copy; |
|
288 |
fun merge pp ((exec1, data1), (exec2, data2)) = |
|
27609 | 289 |
(merge_exec (exec1, exec2), ref empty_data); |
24219 | 290 |
); |
291 |
||
292 |
fun thy_data f thy = f ((snd o CodeData.get) thy); |
|
293 |
||
294 |
fun get_ensure_init kind data_ref = |
|
295 |
case Datatab.lookup (! data_ref) kind |
|
296 |
of SOME x => x |
|
27609 | 297 |
| NONE => let val y = invoke_init kind |
24219 | 298 |
in (change data_ref (Datatab.update (kind, y)); y) end; |
299 |
||
300 |
in |
|
301 |
||
302 |
(* access to executable content *) |
|
303 |
||
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
304 |
val the_exec = fst o CodeData.get; |
24219 | 305 |
|
27983 | 306 |
fun complete_class_params thy cs = |
307 |
fold (fn c => case AxClass.inst_of_param thy c |
|
308 |
of NONE => insert (op =) c |
|
309 |
| SOME (c', _) => insert (op =) c' #> insert (op =) c) cs []; |
|
310 |
||
24219 | 311 |
fun map_exec_purge touched f thy = |
27609 | 312 |
CodeData.map (fn (exec, data) => (f exec, ref (case touched |
27983 | 313 |
of SOME cs => invoke_purge_all thy (complete_class_params thy cs) (! data) |
27609 | 314 |
| NONE => empty_data))) thy; |
24219 | 315 |
|
27675 | 316 |
val purge_data = (CodeData.map o apsnd) (K (ref empty_data)); |
317 |
||
24219 | 318 |
|
28695 | 319 |
(* tackling equation history *) |
320 |
||
321 |
fun get_eqns thy c = |
|
322 |
Symtab.lookup ((the_eqns o the_exec) thy) c |
|
323 |
|> Option.map (Lazy.force o snd o snd o fst) |
|
324 |
|> these; |
|
325 |
||
326 |
fun continue_history thy = if (#concluded_history o the_spec o the_exec) thy |
|
327 |
then thy |
|
328 |
|> (CodeData.map o apfst o map_concluded_history) (K false) |
|
329 |
|> SOME |
|
330 |
else NONE; |
|
331 |
||
332 |
fun conclude_history thy = if (#concluded_history o the_spec o the_exec) thy |
|
333 |
then NONE |
|
334 |
else thy |
|
335 |
|> (CodeData.map o apfst) |
|
336 |
((map_eqns o Symtab.map) (fn ((changed, current), history) => |
|
337 |
((false, current), |
|
338 |
if changed then (serial (), current) :: history else history)) |
|
339 |
#> map_concluded_history (K true)) |
|
340 |
|> SOME; |
|
341 |
||
342 |
val _ = Context.>> (Context.map_theory (CodeData.init |
|
343 |
#> Theory.at_begin continue_history |
|
344 |
#> Theory.at_end conclude_history)); |
|
345 |
||
346 |
||
24219 | 347 |
(* access to data dependent on abstract executable content *) |
348 |
||
349 |
fun get_data (kind, _, dest) = thy_data (get_ensure_init kind #> dest); |
|
350 |
||
351 |
fun change_data (kind, mk, dest) = |
|
352 |
let |
|
353 |
fun chnge data_ref f = |
|
354 |
let |
|
355 |
val data = get_ensure_init kind data_ref; |
|
356 |
val data' = f (dest data); |
|
357 |
in (change data_ref (Datatab.update (kind, mk data')); data') end; |
|
358 |
in thy_data chnge end; |
|
359 |
||
360 |
fun change_yield_data (kind, mk, dest) = |
|
361 |
let |
|
362 |
fun chnge data_ref f = |
|
363 |
let |
|
364 |
val data = get_ensure_init kind data_ref; |
|
365 |
val (x, data') = f (dest data); |
|
366 |
in (x, (change data_ref (Datatab.update (kind, mk data')); data')) end; |
|
367 |
in thy_data chnge end; |
|
368 |
||
369 |
end; (*local*) |
|
370 |
||
371 |
||
372 |
(* print executable content *) |
|
373 |
||
374 |
fun print_codesetup thy = |
|
375 |
let |
|
376 |
val ctxt = ProofContext.init thy; |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
377 |
val exec = the_exec thy; |
28368 | 378 |
fun pretty_eqn (s, (_, lthms)) = |
24219 | 379 |
(Pretty.block o Pretty.fbreaks) ( |
28143 | 380 |
Pretty.str s :: pretty_lthms ctxt lthms |
24219 | 381 |
); |
382 |
fun pretty_dtyp (s, []) = |
|
383 |
Pretty.str s |
|
384 |
| pretty_dtyp (s, cos) = |
|
385 |
(Pretty.block o Pretty.breaks) ( |
|
386 |
Pretty.str s |
|
387 |
:: Pretty.str "=" |
|
28403 | 388 |
:: separate (Pretty.str "|") (map (fn (c, []) => Pretty.str (Code_Unit.string_of_const thy c) |
24219 | 389 |
| (c, tys) => |
390 |
(Pretty.block o Pretty.breaks) |
|
28054 | 391 |
(Pretty.str (Code_Unit.string_of_const thy c) |
26947
133905a0c493
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26928
diff
changeset
|
392 |
:: Pretty.str "of" |
133905a0c493
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26928
diff
changeset
|
393 |
:: map (Pretty.quote o Syntax.pretty_typ_global thy) tys)) cos) |
24219 | 394 |
); |
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
395 |
val pre = (#pre o the_thmproc) exec; |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
396 |
val post = (#post o the_thmproc) exec; |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
397 |
val functrans = (map fst o #functrans o the_thmproc) exec; |
28368 | 398 |
val eqns = the_eqns exec |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
399 |
|> Symtab.dest |
28054 | 400 |
|> (map o apfst) (Code_Unit.string_of_const thy) |
28695 | 401 |
|> (map o apsnd) (snd o fst) |
24219 | 402 |
|> sort (string_ord o pairself fst); |
403 |
val dtyps = the_dtyps exec |
|
404 |
|> Symtab.dest |
|
28695 | 405 |
|> map (fn (dtco, (_, (vs, cos)) :: _) => |
26947
133905a0c493
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26928
diff
changeset
|
406 |
(Syntax.string_of_typ_global thy (Type (dtco, map TFree vs)), cos)) |
24219 | 407 |
|> sort (string_ord o pairself fst) |
408 |
in |
|
409 |
(Pretty.writeln o Pretty.chunks) [ |
|
410 |
Pretty.block ( |
|
30023
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
411 |
Pretty.str "code equations:" |
24219 | 412 |
:: Pretty.fbrk |
28368 | 413 |
:: (Pretty.fbreaks o map pretty_eqn) eqns |
24219 | 414 |
), |
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
415 |
Pretty.block [ |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
416 |
Pretty.str "preprocessing simpset:", |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
417 |
Pretty.fbrk, |
30357 | 418 |
Simplifier.pretty_ss ctxt pre |
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
419 |
], |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
420 |
Pretty.block [ |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
421 |
Pretty.str "postprocessing simpset:", |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
422 |
Pretty.fbrk, |
30357 | 423 |
Simplifier.pretty_ss ctxt post |
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
424 |
], |
24219 | 425 |
Pretty.block ( |
27609 | 426 |
Pretty.str "function transformers:" |
24219 | 427 |
:: Pretty.fbrk |
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
428 |
:: (Pretty.fbreaks o map Pretty.str) functrans |
25968 | 429 |
), |
430 |
Pretty.block ( |
|
24219 | 431 |
Pretty.str "datatypes:" |
432 |
:: Pretty.fbrk |
|
433 |
:: (Pretty.fbreaks o map pretty_dtyp) dtyps |
|
434 |
) |
|
435 |
] |
|
436 |
end; |
|
437 |
||
438 |
||
439 |
(** theorem transformation and certification **) |
|
440 |
||
28423 | 441 |
fun common_typ_eqns thy [] = [] |
442 |
| common_typ_eqns thy [thm] = [thm] |
|
443 |
| common_typ_eqns thy (thms as thm :: _) = (*FIXME is too general*) |
|
24219 | 444 |
let |
445 |
fun incr_thm thm max = |
|
446 |
let |
|
447 |
val thm' = incr_indexes max thm; |
|
448 |
val max' = Thm.maxidx_of thm' + 1; |
|
449 |
in (thm', max') end; |
|
450 |
val (thms', maxidx) = fold_map incr_thm thms 0; |
|
28423 | 451 |
val ty1 :: tys = map (snd o Code_Unit.const_typ_eqn) thms'; |
24219 | 452 |
fun unify ty env = Sign.typ_unify thy (ty1, ty) env |
453 |
handle Type.TUNIFY => |
|
30023
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
454 |
error ("Type unificaton failed, while unifying code equations\n" |
24219 | 455 |
^ (cat_lines o map Display.string_of_thm) thms |
456 |
^ "\nwith types\n" |
|
28054 | 457 |
^ (cat_lines o map (Code_Unit.string_of_typ thy)) (ty1 :: tys)); |
24219 | 458 |
val (env, _) = fold unify tys (Vartab.empty, maxidx) |
459 |
val instT = Vartab.fold (fn (x_i, (sort, ty)) => |
|
460 |
cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env []; |
|
461 |
in map (Thm.instantiate (instT, [])) thms' end; |
|
462 |
||
28423 | 463 |
fun check_linear (eqn as (thm, linear)) = |
464 |
if linear then eqn else Code_Unit.bad_thm |
|
30023
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
465 |
("Duplicate variables on left hand side of code equation:\n" |
28423 | 466 |
^ Display.string_of_thm thm); |
467 |
||
468 |
fun mk_eqn thy linear = |
|
469 |
Code_Unit.error_thm ((if linear then check_linear else I) o Code_Unit.mk_eqn thy); |
|
470 |
fun mk_syntactic_eqn thy = Code_Unit.warning_thm (Code_Unit.mk_eqn thy); |
|
471 |
fun mk_default_eqn thy = Code_Unit.try_thm (check_linear o Code_Unit.mk_eqn thy); |
|
24219 | 472 |
|
473 |
||
474 |
(** operational sort algebra and class discipline **) |
|
475 |
||
476 |
local |
|
477 |
||
28423 | 478 |
fun arity_constraints thy algebra (class, tyco) = |
24219 | 479 |
let |
28423 | 480 |
val base_constraints = Sorts.mg_domain algebra tyco [class]; |
481 |
val classparam_constraints = Sorts.complete_sort algebra [class] |
|
482 |
|> maps (map fst o these o try (#params o AxClass.get_info thy)) |
|
25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25501
diff
changeset
|
483 |
|> map_filter (fn c => try (AxClass.param_of_inst thy) (c, tyco)) |
28695 | 484 |
|> maps (map fst o get_eqns thy) |
28423 | 485 |
|> map (map (snd o dest_TVar) o Sign.const_typargs thy o Code_Unit.const_typ_eqn); |
486 |
val inter_sorts = map2 (curry (Sorts.inter_sort algebra)); |
|
487 |
in fold inter_sorts classparam_constraints base_constraints end; |
|
24219 | 488 |
|
489 |
fun retrieve_algebra thy operational = |
|
26947
133905a0c493
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26928
diff
changeset
|
490 |
Sorts.subalgebra (Syntax.pp_global thy) operational |
30060 | 491 |
(SOME o arity_constraints thy (Sign.classes_of thy)) |
24219 | 492 |
(Sign.classes_of thy); |
493 |
||
494 |
in |
|
495 |
||
496 |
fun coregular_algebra thy = retrieve_algebra thy (K true) |> snd; |
|
497 |
fun operational_algebra thy = |
|
498 |
let |
|
499 |
fun add_iff_operational class = |
|
24928
3419943838f5
renamed AxClass.get_definition to AxClass.get_info (again);
wenzelm
parents:
24848
diff
changeset
|
500 |
can (AxClass.get_info thy) class ? cons class; |
24219 | 501 |
val operational_classes = fold add_iff_operational (Sign.all_classes thy) [] |
502 |
in retrieve_algebra thy (member (op =) operational_classes) end; |
|
503 |
||
28143 | 504 |
end; (*local*) |
24219 | 505 |
|
506 |
||
507 |
(** interfaces and attributes **) |
|
508 |
||
24624
b8383b1bbae3
distinction between regular and default code theorems
haftmann
parents:
24585
diff
changeset
|
509 |
fun delete_force msg key xs = |
b8383b1bbae3
distinction between regular and default code theorems
haftmann
parents:
24585
diff
changeset
|
510 |
if AList.defined (op =) xs key then AList.delete (op =) key xs |
b8383b1bbae3
distinction between regular and default code theorems
haftmann
parents:
24585
diff
changeset
|
511 |
else error ("No such " ^ msg ^ ": " ^ quote key); |
b8383b1bbae3
distinction between regular and default code theorems
haftmann
parents:
24585
diff
changeset
|
512 |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
513 |
fun get_datatype thy tyco = |
28695 | 514 |
case these (Symtab.lookup ((the_dtyps o the_exec) thy) tyco) |
515 |
of (_, spec) :: _ => spec |
|
516 |
| [] => Sign.arity_number thy tyco |
|
24848 | 517 |
|> Name.invents Name.context Name.aT |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
518 |
|> map (rpair []) |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
519 |
|> rpair []; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
520 |
|
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
521 |
fun get_datatype_of_constr thy c = |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
522 |
case (snd o strip_type o Sign.the_const_type thy) c |
28695 | 523 |
of Type (tyco, _) => if member (op =) ((map fst o snd o get_datatype thy) tyco) c |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
524 |
then SOME tyco else NONE |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
525 |
| _ => NONE; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
526 |
|
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28703
diff
changeset
|
527 |
fun recheck_eqn thy = Code_Unit.error_thm |
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28703
diff
changeset
|
528 |
(Code_Unit.assert_linear (is_some o get_datatype_of_constr thy) o apfst (Code_Unit.assert_eqn thy)); |
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28703
diff
changeset
|
529 |
|
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28703
diff
changeset
|
530 |
fun recheck_eqns_const thy c eqns = |
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28703
diff
changeset
|
531 |
let |
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28703
diff
changeset
|
532 |
fun cert (eqn as (thm, _)) = if c = Code_Unit.const_eqn thm |
30023
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
533 |
then eqn else error ("Wrong head of code equation,\nexpected constant " |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28703
diff
changeset
|
534 |
^ Code_Unit.string_of_const thy c ^ "\n" ^ Display.string_of_thm thm) |
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28703
diff
changeset
|
535 |
in map (cert o recheck_eqn thy) eqns end; |
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28703
diff
changeset
|
536 |
|
28695 | 537 |
fun change_eqns delete c f = (map_exec_purge (SOME [c]) o map_eqns |
538 |
o (if delete then Symtab.map_entry c else Symtab.map_default (c, ((false, (true, Lazy.value [])), []))) |
|
539 |
o apfst) (fn (_, eqns) => (true, f eqns)); |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
540 |
|
28695 | 541 |
fun gen_add_eqn linear default thm thy = |
542 |
case (if default then mk_default_eqn thy else SOME o mk_eqn thy linear) thm |
|
28368 | 543 |
of SOME (thm, _) => |
28143 | 544 |
let |
28423 | 545 |
val c = Code_Unit.const_eqn thm; |
28695 | 546 |
val _ = if not default andalso (is_some o AxClass.class_of_param thy) c |
30023
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
547 |
then error ("Rejected polymorphic code equation for overloaded constant:\n" |
28143 | 548 |
^ Display.string_of_thm thm) |
549 |
else (); |
|
28695 | 550 |
val _ = if not default andalso (is_some o get_datatype_of_constr thy) c |
30023
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
551 |
then error ("Rejected code equation for datatype constructor:\n" |
28368 | 552 |
^ Display.string_of_thm thm) |
28143 | 553 |
else (); |
28695 | 554 |
in change_eqns false c (add_thm thy default (thm, linear)) thy end |
24624
b8383b1bbae3
distinction between regular and default code theorems
haftmann
parents:
24585
diff
changeset
|
555 |
| NONE => thy; |
b8383b1bbae3
distinction between regular and default code theorems
haftmann
parents:
24585
diff
changeset
|
556 |
|
28695 | 557 |
val add_eqn = gen_add_eqn true false; |
558 |
val add_default_eqn = gen_add_eqn true true; |
|
559 |
val add_nonlinear_eqn = gen_add_eqn false false; |
|
26021 | 560 |
|
28368 | 561 |
fun add_eqnl (c, lthms) thy = |
24219 | 562 |
let |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28703
diff
changeset
|
563 |
val lthms' = certificate thy (fn thy => recheck_eqns_const thy c) lthms; |
28695 | 564 |
in change_eqns false c (add_lthms lthms') thy end; |
24219 | 565 |
|
28703 | 566 |
val add_default_eqn_attribute = Thm.declaration_attribute |
567 |
(fn thm => Context.mapping (add_default_eqn thm) I); |
|
568 |
val add_default_eqn_attrib = Attrib.internal (K add_default_eqn_attribute); |
|
24219 | 569 |
|
28695 | 570 |
fun del_eqn thm thy = case mk_syntactic_eqn thy thm |
571 |
of SOME (thm, _) => change_eqns true (Code_Unit.const_eqn thm) (del_thm thm) thy |
|
572 |
| NONE => thy; |
|
573 |
||
574 |
fun del_eqns c = change_eqns true c (K (false, Lazy.value [])); |
|
575 |
||
30076 | 576 |
fun get_case_scheme thy = Symtab.lookup ((fst o the_cases o the_exec) thy); |
28695 | 577 |
|
578 |
val is_undefined = Symtab.defined o snd o the_cases o the_exec; |
|
579 |
||
25485 | 580 |
structure TypeInterpretation = InterpretationFun(type T = string * serial val eq = eq_snd (op =) : T * T -> bool); |
25462 | 581 |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
582 |
fun add_datatype raw_cs thy = |
24219 | 583 |
let |
25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25501
diff
changeset
|
584 |
val cs = map (fn c_ty as (_, ty) => (AxClass.unoverload_const thy c_ty, ty)) raw_cs; |
28054 | 585 |
val (tyco, vs_cos) = Code_Unit.constrset_of_consts thy cs; |
30076 | 586 |
val old_cs = (map fst o snd o get_datatype thy) tyco; |
587 |
fun drop_outdated_cases cases = fold Symtab.delete_safe |
|
588 |
(Symtab.fold (fn (c, (_, (_, cos))) => |
|
589 |
if exists (member (op =) old_cs) cos |
|
590 |
then insert (op =) c else I) cases []) cases; |
|
24219 | 591 |
in |
592 |
thy |
|
28703 | 593 |
|> map_exec_purge NONE |
28695 | 594 |
((map_dtyps o Symtab.map_default (tyco, [])) (cons (serial (), vs_cos)) |
30076 | 595 |
#> map_eqns (fold (Symtab.delete_safe o fst) cs) |
596 |
#> (map_cases o apfst) drop_outdated_cases) |
|
25485 | 597 |
|> TypeInterpretation.data (tyco, serial ()) |
24219 | 598 |
end; |
599 |
||
25485 | 600 |
fun type_interpretation f = TypeInterpretation.interpretation |
601 |
(fn (tyco, _) => fn thy => f (tyco, get_datatype thy tyco) thy); |
|
602 |
||
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
603 |
fun add_datatype_cmd raw_cs thy = |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
604 |
let |
28054 | 605 |
val cs = map (Code_Unit.read_bare_const thy) raw_cs; |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
606 |
in add_datatype cs thy end; |
24219 | 607 |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
608 |
fun add_case thm thy = |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
609 |
let |
30076 | 610 |
val (c, (k, case_pats)) = Code_Unit.case_cert thm; |
611 |
val _ = case filter (is_none o get_datatype_of_constr thy) case_pats |
|
612 |
of [] => () |
|
613 |
| cs => error ("Non-constructor(s) in case certificate: " ^ commas (map quote cs)); |
|
614 |
val entry = (1 + Int.max (1, length case_pats), (k, case_pats)) |
|
615 |
in (map_exec_purge (SOME [c]) o map_cases o apfst) (Symtab.update (c, entry)) thy end; |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
616 |
|
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
617 |
fun add_undefined c thy = |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
618 |
(map_exec_purge (SOME [c]) o map_cases o apsnd) (Symtab.update (c, ())) thy; |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
619 |
|
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
620 |
val map_pre = map_exec_purge NONE o map_thmproc o apfst o apfst; |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
621 |
val map_post = map_exec_purge NONE o map_thmproc o apfst o apsnd; |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
622 |
|
28525 | 623 |
val add_inline = map_pre o MetaSimplifier.add_simp; |
624 |
val del_inline = map_pre o MetaSimplifier.del_simp; |
|
625 |
val add_post = map_post o MetaSimplifier.add_simp; |
|
626 |
val del_post = map_post o MetaSimplifier.del_simp; |
|
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
627 |
|
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
628 |
fun add_functrans (name, f) = |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
629 |
(map_exec_purge NONE o map_thmproc o apsnd) |
24219 | 630 |
(AList.update (op =) (name, (serial (), f))); |
631 |
||
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
632 |
fun del_functrans name = |
24219 | 633 |
(map_exec_purge NONE o map_thmproc o apsnd) |
27609 | 634 |
(delete_force "function transformer" name); |
24219 | 635 |
|
26463 | 636 |
val _ = Context.>> (Context.map_theory |
24219 | 637 |
(let |
638 |
fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I); |
|
639 |
fun add_simple_attribute (name, f) = |
|
640 |
add_attribute (name, Scan.succeed (mk_attribute f)); |
|
641 |
fun add_del_attribute (name, (add, del)) = |
|
642 |
add_attribute (name, Args.del |-- Scan.succeed (mk_attribute del) |
|
643 |
|| Scan.succeed (mk_attribute add)) |
|
644 |
in |
|
25462 | 645 |
TypeInterpretation.init |
28562 | 646 |
#> add_del_attribute ("", (add_eqn, del_eqn)) |
28368 | 647 |
#> add_simple_attribute ("nbe", add_nonlinear_eqn) |
24219 | 648 |
#> add_del_attribute ("inline", (add_inline, del_inline)) |
649 |
#> add_del_attribute ("post", (add_post, del_post)) |
|
26463 | 650 |
end)); |
24219 | 651 |
|
652 |
||
653 |
(** post- and preprocessing **) |
|
654 |
||
655 |
local |
|
656 |
||
28423 | 657 |
fun apply_functrans thy c _ [] = [] |
658 |
| apply_functrans thy c [] eqns = eqns |
|
659 |
| apply_functrans thy c functrans eqns = eqns |
|
660 |
|> perhaps (perhaps_loop (perhaps_apply functrans)) |
|
28525 | 661 |
|> (map o apfst) (AxClass.unoverload thy) |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28703
diff
changeset
|
662 |
|> recheck_eqns_const thy c |
28525 | 663 |
|> (map o apfst) (AxClass.overload thy); |
24219 | 664 |
|
28423 | 665 |
fun rhs_conv conv thm = Thm.transitive thm ((conv o Thm.rhs_of) thm); |
24219 | 666 |
|
24837 | 667 |
fun term_of_conv thy f = |
668 |
Thm.cterm_of thy |
|
669 |
#> f |
|
670 |
#> Thm.prop_of |
|
671 |
#> Logic.dest_equals |
|
672 |
#> snd; |
|
673 |
||
28423 | 674 |
fun preprocess thy functrans c eqns = |
27582 | 675 |
let |
676 |
val pre = (Simplifier.theory_context thy o #pre o the_thmproc o the_exec) thy; |
|
677 |
in |
|
28423 | 678 |
eqns |
28525 | 679 |
|> (map o apfst) (AxClass.overload thy) |
28423 | 680 |
|> apply_functrans thy c functrans |
28368 | 681 |
|> (map o apfst) (Code_Unit.rewrite_eqn pre) |
28525 | 682 |
|> (map o apfst) (AxClass.unoverload thy) |
28708
a1a436f09ec6
explicit check for pattern discipline before code translation
haftmann
parents:
28703
diff
changeset
|
683 |
|> map (recheck_eqn thy) |
28423 | 684 |
|> burrow_fst (common_typ_eqns thy) |
27582 | 685 |
end; |
26970 | 686 |
|
28423 | 687 |
in |
688 |
||
689 |
fun preprocess_conv thy ct = |
|
24219 | 690 |
let |
27582 | 691 |
val pre = (Simplifier.theory_context thy o #pre o the_thmproc o the_exec) thy; |
24219 | 692 |
in |
693 |
ct |
|
27582 | 694 |
|> Simplifier.rewrite pre |
25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25501
diff
changeset
|
695 |
|> rhs_conv (AxClass.unoverload_conv thy) |
24219 | 696 |
end; |
697 |
||
28423 | 698 |
fun preprocess_term thy = term_of_conv thy (preprocess_conv thy); |
24837 | 699 |
|
28423 | 700 |
fun postprocess_conv thy ct = |
24219 | 701 |
let |
27582 | 702 |
val post = (Simplifier.theory_context thy o #post o the_thmproc o the_exec) thy; |
24219 | 703 |
in |
704 |
ct |
|
25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25501
diff
changeset
|
705 |
|> AxClass.overload_conv thy |
27582 | 706 |
|> rhs_conv (Simplifier.rewrite post) |
24219 | 707 |
end; |
708 |
||
28423 | 709 |
fun postprocess_term thy = term_of_conv thy (postprocess_conv thy); |
24219 | 710 |
|
28525 | 711 |
fun these_raw_eqns thy c = |
712 |
get_eqns thy c |
|
28695 | 713 |
|> (map o apfst) (Thm.transfer thy) |
28525 | 714 |
|> burrow_fst (common_typ_eqns thy); |
715 |
||
28423 | 716 |
fun these_eqns thy c = |
24219 | 717 |
let |
28423 | 718 |
val functrans = (map (fn (_, (_, f)) => f thy) o #functrans |
719 |
o the_thmproc o the_exec) thy; |
|
24219 | 720 |
in |
28423 | 721 |
get_eqns thy c |
28695 | 722 |
|> (map o apfst) (Thm.transfer thy) |
28423 | 723 |
|> preprocess thy functrans c |
24219 | 724 |
end; |
725 |
||
28525 | 726 |
fun default_typscheme thy c = |
727 |
let |
|
30023
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
728 |
fun the_const_typscheme c = (curry (Code_Unit.typscheme thy) c o snd o dest_Const |
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
729 |
o TermSubst.zero_var_indexes o curry Const "" o Sign.the_const_type thy) c; |
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
730 |
fun strip_sorts (vs, ty) = (map (fn (v, _) => (v, [])) vs, ty); |
28423 | 731 |
in case AxClass.class_of_param thy c |
30023
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
732 |
of SOME class => ([(Name.aT, [class])], snd (the_const_typscheme c)) |
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
733 |
| NONE => if is_some (get_datatype_of_constr thy c) |
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
734 |
then strip_sorts (the_const_typscheme c) |
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
735 |
else case get_eqns thy c |
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
736 |
of (thm, _) :: _ => snd (Code_Unit.head_eqn thy (Drule.zero_var_indexes thm)) |
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
737 |
| [] => strip_sorts (the_const_typscheme c) end; |
24219 | 738 |
|
739 |
end; (*local*) |
|
740 |
||
741 |
end; (*struct*) |
|
742 |
||
743 |
||
744 |
(** type-safe interfaces for data depedent on executable content **) |
|
745 |
||
746 |
functor CodeDataFun(Data: CODE_DATA_ARGS): CODE_DATA = |
|
747 |
struct |
|
748 |
||
749 |
type T = Data.T; |
|
750 |
exception Data of T; |
|
751 |
fun dest (Data x) = x |
|
752 |
||
753 |
val kind = Code.declare_data (Data Data.empty) |
|
27609 | 754 |
(fn thy => fn cs => fn Data x => Data (Data.purge thy cs x)); |
24219 | 755 |
|
756 |
val data_op = (kind, Data, dest); |
|
757 |
||
758 |
val get = Code.get_data data_op; |
|
759 |
val change = Code.change_data data_op; |
|
760 |
fun change_yield thy = Code.change_yield_data data_op thy; |
|
761 |
||
762 |
end; |
|
763 |
||
28143 | 764 |
structure Code : CODE = struct open Code; end; |