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