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