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