author | haftmann |
Thu, 25 Sep 2008 11:14:01 +0200 | |
changeset 28353 | 40306cc4d16a |
parent 28350 | 715163ec93c0 |
child 28359 | bd4750bcb4e6 |
permissions | -rw-r--r-- |
24219 | 1 |
(* Title: Pure/Isar/code.ML |
2 |
ID: $Id$ |
|
3 |
Author: Florian Haftmann, TU Muenchen |
|
4 |
||
5 |
Abstract executable content of theory. Management of data dependent on |
|
25485 | 6 |
executable content. Cache assumes non-concurrent processing of a single theory. |
24219 | 7 |
*) |
8 |
||
9 |
signature CODE = |
|
10 |
sig |
|
24624
b8383b1bbae3
distinction between regular and default code theorems
haftmann
parents:
24585
diff
changeset
|
11 |
val add_func: thm -> theory -> theory |
28350 | 12 |
val add_nonlinear_func: thm -> theory -> theory |
24624
b8383b1bbae3
distinction between regular and default code theorems
haftmann
parents:
24585
diff
changeset
|
13 |
val add_liberal_func: thm -> theory -> theory |
b8383b1bbae3
distinction between regular and default code theorems
haftmann
parents:
24585
diff
changeset
|
14 |
val add_default_func: thm -> theory -> theory |
b8383b1bbae3
distinction between regular and default code theorems
haftmann
parents:
24585
diff
changeset
|
15 |
val add_default_func_attr: Attrib.src |
24219 | 16 |
val del_func: thm -> theory -> theory |
26021 | 17 |
val del_funcs: string -> theory -> theory |
28350 | 18 |
val add_funcl: string * (thm * bool) list Susp.T -> theory -> theory |
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
19 |
val map_pre: (MetaSimplifier.simpset -> MetaSimplifier.simpset) -> theory -> theory |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
20 |
val map_post: (MetaSimplifier.simpset -> MetaSimplifier.simpset) -> theory -> theory |
24219 | 21 |
val add_inline: thm -> theory -> theory |
22 |
val del_inline: thm -> theory -> theory |
|
23 |
val add_post: thm -> theory -> theory |
|
24 |
val del_post: thm -> theory -> theory |
|
27609 | 25 |
val add_functrans: string * (theory -> thm list -> thm list option) -> theory -> theory |
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
26 |
val del_functrans: string -> theory -> theory |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
27 |
val add_datatype: (string * typ) list -> theory -> theory |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
28 |
val add_datatype_cmd: string list -> theory -> theory |
25485 | 29 |
val type_interpretation: |
30 |
(string * ((string * sort) list * (string * typ list) list) |
|
31 |
-> theory -> theory) -> theory -> theory |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
32 |
val add_case: thm -> theory -> theory |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
33 |
val add_undefined: string -> theory -> theory |
27675 | 34 |
val purge_data: theory -> theory |
24219 | 35 |
|
36 |
val coregular_algebra: theory -> Sorts.algebra |
|
37 |
val operational_algebra: theory -> (sort -> sort) * Sorts.algebra |
|
28350 | 38 |
val these_funcs: theory -> string -> (thm * bool) list |
24219 | 39 |
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
|
40 |
val get_datatype_of_constr: theory -> string -> string option |
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
41 |
val get_case_data: theory -> string -> (int * string list) option |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
42 |
val is_undefined: theory -> string -> bool |
26970 | 43 |
val default_typ: theory -> string -> (string * sort) list * typ |
24219 | 44 |
|
45 |
val preprocess_conv: cterm -> thm |
|
24837 | 46 |
val preprocess_term: theory -> term -> term |
24219 | 47 |
val postprocess_conv: cterm -> thm |
24837 | 48 |
val postprocess_term: theory -> term -> term |
24219 | 49 |
|
50 |
val add_attribute: string * (Args.T list -> attribute * Args.T list) -> theory -> theory |
|
51 |
||
52 |
val print_codesetup: theory -> unit |
|
53 |
end; |
|
54 |
||
55 |
signature CODE_DATA_ARGS = |
|
56 |
sig |
|
57 |
type T |
|
58 |
val empty: T |
|
27609 | 59 |
val purge: theory -> string list -> T -> T |
24219 | 60 |
end; |
61 |
||
62 |
signature CODE_DATA = |
|
63 |
sig |
|
64 |
type T |
|
65 |
val get: theory -> T |
|
66 |
val change: theory -> (T -> T) -> T |
|
67 |
val change_yield: theory -> (T -> 'a * T) -> 'a * T |
|
68 |
end; |
|
69 |
||
70 |
signature PRIVATE_CODE = |
|
71 |
sig |
|
72 |
include CODE |
|
27609 | 73 |
val declare_data: Object.T -> (theory -> string list -> Object.T -> Object.T) |
74 |
-> serial |
|
24219 | 75 |
val get_data: serial * ('a -> Object.T) * (Object.T -> 'a) |
76 |
-> theory -> 'a |
|
77 |
val change_data: serial * ('a -> Object.T) * (Object.T -> 'a) |
|
78 |
-> theory -> ('a -> 'a) -> 'a |
|
79 |
val change_yield_data: serial * ('a -> Object.T) * (Object.T -> 'a) |
|
80 |
-> theory -> ('a -> 'b * 'a) -> 'b * 'a |
|
81 |
end; |
|
82 |
||
83 |
structure Code : PRIVATE_CODE = |
|
84 |
struct |
|
85 |
||
25312 | 86 |
(** code attributes **) |
87 |
||
88 |
structure CodeAttr = TheoryDataFun ( |
|
89 |
type T = (string * (Args.T list -> attribute * Args.T list)) list; |
|
90 |
val empty = []; |
|
91 |
val copy = I; |
|
92 |
val extend = I; |
|
26970 | 93 |
fun merge _ = AList.merge (op = : string * string -> bool) (K true); |
25312 | 94 |
); |
24219 | 95 |
|
25312 | 96 |
fun add_attribute (attr as (name, _)) = |
97 |
let |
|
98 |
fun add_parser ("", parser) attrs = attrs @ [("", parser)] |
|
99 |
| add_parser (name, parser) attrs = (name, Args.$$$ name |-- parser) :: attrs; |
|
100 |
fun error "" = error ("Code attribute already declared") |
|
101 |
| error name = error ("Code attribute " ^ name ^ " already declared") |
|
102 |
in CodeAttr.map (fn attrs => if AList.defined (op =) attrs name |
|
103 |
then error name else add_parser attr attrs) |
|
104 |
end; |
|
105 |
||
106 |
val _ = |
|
107 |
let |
|
108 |
val code_attr = Attrib.syntax (Scan.peek (fn context => |
|
109 |
List.foldr op || Scan.fail (map snd (CodeAttr.get (Context.theory_of context))))); |
|
110 |
in |
|
26463 | 111 |
Context.>> (Context.map_theory |
112 |
(Attrib.add_attributes |
|
113 |
[("code", code_attr, "declare theorems for code generation")])) |
|
25312 | 114 |
end; |
115 |
||
116 |
||
28143 | 117 |
(** logical and syntactical specification of executable code **) |
24219 | 118 |
|
28350 | 119 |
(* defining equations with linear flag, default flag and lazy theorems *) |
24219 | 120 |
|
121 |
fun pretty_lthms ctxt r = case Susp.peek r |
|
28350 | 122 |
of SOME thms => map (ProofContext.pretty_thm ctxt o fst) thms |
24219 | 123 |
| NONE => [Pretty.str "[...]"]; |
124 |
||
125 |
fun certificate thy f r = |
|
126 |
case Susp.peek r |
|
28350 | 127 |
of SOME thms => (Susp.value o burrow_fst (f thy)) thms |
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
128 |
| NONE => let |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
129 |
val thy_ref = Theory.check_thy thy; |
28350 | 130 |
in Susp.delay (fn () => (burrow_fst (f (Theory.deref thy_ref)) o Susp.force) r) end; |
24219 | 131 |
|
28350 | 132 |
fun add_drop_redundant (thm, linear) thms = |
24219 | 133 |
let |
134 |
val thy = Thm.theory_of_thm thm; |
|
135 |
val args_of = snd o strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of; |
|
136 |
val args = args_of thm; |
|
28350 | 137 |
fun matches_args args' = length args <= length args' andalso |
138 |
Pattern.matchess thy (args, curry Library.take (length args) args'); |
|
139 |
fun drop (thm', _) = if matches_args (args_of thm') then |
|
140 |
(warning ("Code generator: dropping redundant defining equation\n" ^ Display.string_of_thm thm'); true) |
|
141 |
else false; |
|
142 |
in (thm, linear) :: filter_out drop thms end; |
|
24219 | 143 |
|
28350 | 144 |
fun add_thm _ thm (false, thms) = (false, Susp.map_force (add_drop_redundant thm) thms) |
145 |
| add_thm true thm (true, thms) = (true, Susp.map_force (fn thms => thms @ [thm]) thms) |
|
28143 | 146 |
| add_thm false thm (true, thms) = (false, Susp.value [thm]); |
24219 | 147 |
|
28143 | 148 |
fun add_lthms lthms _ = (false, lthms); |
25312 | 149 |
|
28350 | 150 |
fun del_thm thm = (apsnd o Susp.map_force) (remove (eq_fst Thm.eq_thm_prop) (thm, true)); |
25312 | 151 |
|
28143 | 152 |
fun merge_defthms ((true, _), defthms2) = defthms2 |
153 |
| merge_defthms (defthms1 as (false, _), (true, _)) = defthms1 |
|
154 |
| merge_defthms ((false, _), defthms2 as (false, _)) = defthms2; |
|
24219 | 155 |
|
156 |
||
28143 | 157 |
(* syntactic datatypes *) |
24219 | 158 |
|
159 |
val eq_string = op = : string * string -> bool; |
|
28143 | 160 |
|
24219 | 161 |
fun eq_dtyp ((vs1, cs1), (vs2, cs2)) = |
162 |
gen_eq_set (eq_pair eq_string (gen_eq_set eq_string)) (vs1, vs2) |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
163 |
andalso gen_eq_set (eq_fst eq_string) (cs1, cs2); |
28143 | 164 |
|
27582 | 165 |
fun merge_dtyps (tabs as (tab1, tab2)) = |
24219 | 166 |
let |
27582 | 167 |
fun join _ (cos as (_, cos2)) = if eq_dtyp cos then raise Symtab.SAME else cos2; |
168 |
in Symtab.join join tabs end; |
|
24219 | 169 |
|
28143 | 170 |
|
171 |
(* specification data *) |
|
172 |
||
24219 | 173 |
datatype spec = Spec of { |
28350 | 174 |
funcs: (bool * (thm * bool) list Susp.T) Symtab.table, |
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
175 |
dtyps: ((string * sort) list * (string * typ list) list) Symtab.table, |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
176 |
cases: (int * string list) Symtab.table * unit Symtab.table |
24219 | 177 |
}; |
178 |
||
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
179 |
fun mk_spec (funcs, (dtyps, cases)) = |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
180 |
Spec { funcs = funcs, dtyps = dtyps, cases = cases }; |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
181 |
fun map_spec f (Spec { funcs = funcs, dtyps = dtyps, cases = cases }) = |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
182 |
mk_spec (f (funcs, (dtyps, cases))); |
27609 | 183 |
fun merge_spec (Spec { funcs = funcs1, dtyps = dtyps1, cases = (cases1, undefs1) }, |
184 |
Spec { funcs = funcs2, dtyps = dtyps2, cases = (cases2, undefs2) }) = |
|
24219 | 185 |
let |
28143 | 186 |
val funcs = Symtab.join (K merge_defthms) (funcs1, funcs2); |
27582 | 187 |
val dtyps = merge_dtyps (dtyps1, dtyps2); |
27609 | 188 |
val cases = (Symtab.merge (K true) (cases1, cases2), |
189 |
Symtab.merge (K true) (undefs1, undefs2)); |
|
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
190 |
in mk_spec (funcs, (dtyps, cases)) end; |
24219 | 191 |
|
25312 | 192 |
|
193 |
(* pre- and postprocessor *) |
|
194 |
||
195 |
datatype thmproc = Thmproc of { |
|
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
196 |
pre: MetaSimplifier.simpset, |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
197 |
post: MetaSimplifier.simpset, |
27609 | 198 |
functrans: (string * (serial * (theory -> thm list -> thm list option))) list |
25312 | 199 |
}; |
200 |
||
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
201 |
fun mk_thmproc ((pre, post), functrans) = |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
202 |
Thmproc { pre = pre, post = post, functrans = functrans }; |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
203 |
fun map_thmproc f (Thmproc { pre, post, functrans }) = |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
204 |
mk_thmproc (f ((pre, post), functrans)); |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
205 |
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
|
206 |
Thmproc { pre = pre2, post = post2, functrans = functrans2 }) = |
25312 | 207 |
let |
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
208 |
val pre = MetaSimplifier.merge_ss (pre1, pre2); |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
209 |
val post = MetaSimplifier.merge_ss (post1, post2); |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
210 |
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
|
211 |
in mk_thmproc ((pre, post), functrans) end; |
25312 | 212 |
|
24219 | 213 |
datatype exec = Exec of { |
214 |
thmproc: thmproc, |
|
215 |
spec: spec |
|
216 |
}; |
|
217 |
||
28143 | 218 |
|
219 |
(* code setup data *) |
|
220 |
||
24219 | 221 |
fun mk_exec (thmproc, spec) = |
222 |
Exec { thmproc = thmproc, spec = spec }; |
|
223 |
fun map_exec f (Exec { thmproc = thmproc, spec = spec }) = |
|
224 |
mk_exec (f (thmproc, spec)); |
|
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
225 |
fun merge_exec (Exec { thmproc = thmproc1, spec = spec1 }, |
24219 | 226 |
Exec { thmproc = thmproc2, spec = spec2 }) = |
227 |
let |
|
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
228 |
val thmproc = merge_thmproc (thmproc1, thmproc2); |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
229 |
val spec = merge_spec (spec1, spec2); |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
230 |
in mk_exec (thmproc, spec) end; |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
231 |
val empty_exec = mk_exec (mk_thmproc ((MetaSimplifier.empty_ss, MetaSimplifier.empty_ss), []), |
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
232 |
mk_spec (Symtab.empty, (Symtab.empty, (Symtab.empty, Symtab.empty)))); |
24219 | 233 |
|
25312 | 234 |
fun the_thmproc (Exec { thmproc = Thmproc x, ...}) = x; |
24219 | 235 |
fun the_spec (Exec { spec = Spec x, ...}) = x; |
236 |
val the_funcs = #funcs o the_spec; |
|
237 |
val the_dtyps = #dtyps o the_spec; |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
238 |
val the_cases = #cases o the_spec; |
24219 | 239 |
val map_thmproc = map_exec o apfst o map_thmproc; |
240 |
val map_funcs = map_exec o apsnd o map_spec o apfst; |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
241 |
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
|
242 |
val map_cases = map_exec o apsnd o map_spec o apsnd o apsnd; |
24219 | 243 |
|
244 |
||
245 |
(* data slots dependent on executable content *) |
|
246 |
||
247 |
(*private copy avoids potential conflict of table exceptions*) |
|
248 |
structure Datatab = TableFun(type key = int val ord = int_ord); |
|
249 |
||
250 |
local |
|
251 |
||
252 |
type kind = { |
|
253 |
empty: Object.T, |
|
27609 | 254 |
purge: theory -> string list -> Object.T -> Object.T |
24219 | 255 |
}; |
256 |
||
257 |
val kinds = ref (Datatab.empty: kind Datatab.table); |
|
258 |
val kind_keys = ref ([]: serial list); |
|
259 |
||
260 |
fun invoke f k = case Datatab.lookup (! kinds) k |
|
261 |
of SOME kind => f kind |
|
262 |
| NONE => sys_error "Invalid code data identifier"; |
|
263 |
||
264 |
in |
|
265 |
||
27609 | 266 |
fun declare_data empty purge = |
24219 | 267 |
let |
268 |
val k = serial (); |
|
27609 | 269 |
val kind = {empty = empty, purge = purge}; |
24219 | 270 |
val _ = change kinds (Datatab.update (k, kind)); |
271 |
val _ = change kind_keys (cons k); |
|
272 |
in k end; |
|
273 |
||
27609 | 274 |
fun invoke_init k = invoke (fn kind => #empty kind) k; |
24219 | 275 |
|
27609 | 276 |
fun invoke_purge_all thy cs = |
24219 | 277 |
fold (fn k => Datatab.map_entry k |
27609 | 278 |
(invoke (fn kind => #purge kind thy cs) k)) (! kind_keys); |
24219 | 279 |
|
280 |
end; (*local*) |
|
281 |
||
282 |
||
25312 | 283 |
(** theory store **) |
24219 | 284 |
|
285 |
local |
|
286 |
||
287 |
type data = Object.T Datatab.table; |
|
27609 | 288 |
val empty_data = Datatab.empty : data; |
24219 | 289 |
|
290 |
structure CodeData = TheoryDataFun |
|
291 |
( |
|
292 |
type T = exec * data ref; |
|
27609 | 293 |
val empty = (empty_exec, ref empty_data); |
24219 | 294 |
fun copy (exec, data) = (exec, ref (! data)); |
295 |
val extend = copy; |
|
296 |
fun merge pp ((exec1, data1), (exec2, data2)) = |
|
27609 | 297 |
(merge_exec (exec1, exec2), ref empty_data); |
24219 | 298 |
); |
299 |
||
26463 | 300 |
val _ = Context.>> (Context.map_theory CodeData.init); |
24219 | 301 |
|
302 |
fun thy_data f thy = f ((snd o CodeData.get) thy); |
|
303 |
||
304 |
fun get_ensure_init kind data_ref = |
|
305 |
case Datatab.lookup (! data_ref) kind |
|
306 |
of SOME x => x |
|
27609 | 307 |
| NONE => let val y = invoke_init kind |
24219 | 308 |
in (change data_ref (Datatab.update (kind, y)); y) end; |
309 |
||
310 |
in |
|
311 |
||
312 |
(* access to executable content *) |
|
313 |
||
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
314 |
val the_exec = fst o CodeData.get; |
24219 | 315 |
|
27983 | 316 |
fun complete_class_params thy cs = |
317 |
fold (fn c => case AxClass.inst_of_param thy c |
|
318 |
of NONE => insert (op =) c |
|
319 |
| SOME (c', _) => insert (op =) c' #> insert (op =) c) cs []; |
|
320 |
||
24219 | 321 |
fun map_exec_purge touched f thy = |
27609 | 322 |
CodeData.map (fn (exec, data) => (f exec, ref (case touched |
27983 | 323 |
of SOME cs => invoke_purge_all thy (complete_class_params thy cs) (! data) |
27609 | 324 |
| NONE => empty_data))) thy; |
24219 | 325 |
|
27675 | 326 |
val purge_data = (CodeData.map o apsnd) (K (ref empty_data)); |
327 |
||
24219 | 328 |
|
329 |
(* access to data dependent on abstract executable content *) |
|
330 |
||
331 |
fun get_data (kind, _, dest) = thy_data (get_ensure_init kind #> dest); |
|
332 |
||
333 |
fun change_data (kind, mk, dest) = |
|
334 |
let |
|
335 |
fun chnge data_ref f = |
|
336 |
let |
|
337 |
val data = get_ensure_init kind data_ref; |
|
338 |
val data' = f (dest data); |
|
339 |
in (change data_ref (Datatab.update (kind, mk data')); data') end; |
|
340 |
in thy_data chnge end; |
|
341 |
||
342 |
fun change_yield_data (kind, mk, dest) = |
|
343 |
let |
|
344 |
fun chnge data_ref f = |
|
345 |
let |
|
346 |
val data = get_ensure_init kind data_ref; |
|
347 |
val (x, data') = f (dest data); |
|
348 |
in (x, (change data_ref (Datatab.update (kind, mk data')); data')) end; |
|
349 |
in thy_data chnge end; |
|
350 |
||
351 |
end; (*local*) |
|
352 |
||
353 |
||
354 |
(* print executable content *) |
|
355 |
||
356 |
fun print_codesetup thy = |
|
357 |
let |
|
358 |
val ctxt = ProofContext.init thy; |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
359 |
val exec = the_exec thy; |
28143 | 360 |
fun pretty_func (s, (_, lthms)) = |
24219 | 361 |
(Pretty.block o Pretty.fbreaks) ( |
28143 | 362 |
Pretty.str s :: pretty_lthms ctxt lthms |
24219 | 363 |
); |
364 |
fun pretty_dtyp (s, []) = |
|
365 |
Pretty.str s |
|
366 |
| pretty_dtyp (s, cos) = |
|
367 |
(Pretty.block o Pretty.breaks) ( |
|
368 |
Pretty.str s |
|
369 |
:: Pretty.str "=" |
|
370 |
:: separate (Pretty.str "|") (map (fn (c, []) => Pretty.str c |
|
371 |
| (c, tys) => |
|
372 |
(Pretty.block o Pretty.breaks) |
|
28054 | 373 |
(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
|
374 |
:: Pretty.str "of" |
133905a0c493
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26928
diff
changeset
|
375 |
:: map (Pretty.quote o Syntax.pretty_typ_global thy) tys)) cos) |
24219 | 376 |
); |
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
377 |
val pre = (#pre o the_thmproc) exec; |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
378 |
val post = (#post o the_thmproc) exec; |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
379 |
val functrans = (map fst o #functrans o the_thmproc) exec; |
27609 | 380 |
val funcs = the_funcs exec |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
381 |
|> Symtab.dest |
28054 | 382 |
|> (map o apfst) (Code_Unit.string_of_const thy) |
24219 | 383 |
|> sort (string_ord o pairself fst); |
384 |
val dtyps = the_dtyps exec |
|
385 |
|> Symtab.dest |
|
26947
133905a0c493
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26928
diff
changeset
|
386 |
|> map (fn (dtco, (vs, cos)) => |
133905a0c493
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26928
diff
changeset
|
387 |
(Syntax.string_of_typ_global thy (Type (dtco, map TFree vs)), cos)) |
24219 | 388 |
|> sort (string_ord o pairself fst) |
389 |
in |
|
390 |
(Pretty.writeln o Pretty.chunks) [ |
|
391 |
Pretty.block ( |
|
392 |
Pretty.str "defining equations:" |
|
393 |
:: Pretty.fbrk |
|
27609 | 394 |
:: (Pretty.fbreaks o map pretty_func) funcs |
24219 | 395 |
), |
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
396 |
Pretty.block [ |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
397 |
Pretty.str "preprocessing simpset:", |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
398 |
Pretty.fbrk, |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
399 |
MetaSimplifier.pretty_ss pre |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
400 |
], |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
401 |
Pretty.block [ |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
402 |
Pretty.str "postprocessing simpset:", |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
403 |
Pretty.fbrk, |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
404 |
MetaSimplifier.pretty_ss post |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
405 |
], |
24219 | 406 |
Pretty.block ( |
27609 | 407 |
Pretty.str "function transformers:" |
24219 | 408 |
:: Pretty.fbrk |
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
409 |
:: (Pretty.fbreaks o map Pretty.str) functrans |
25968 | 410 |
), |
411 |
Pretty.block ( |
|
24219 | 412 |
Pretty.str "datatypes:" |
413 |
:: Pretty.fbrk |
|
414 |
:: (Pretty.fbreaks o map pretty_dtyp) dtyps |
|
415 |
) |
|
416 |
] |
|
417 |
end; |
|
418 |
||
419 |
||
420 |
||
421 |
(** theorem transformation and certification **) |
|
422 |
||
26970 | 423 |
fun const_of thy = dest_Const o fst o strip_comb o fst o Logic.dest_equals |
424 |
o ObjectLogic.drop_judgment thy o Thm.plain_prop_of; |
|
425 |
||
426 |
fun const_of_func thy = AxClass.unoverload_const thy o const_of thy; |
|
427 |
||
24219 | 428 |
fun common_typ_funcs [] = [] |
429 |
| common_typ_funcs [thm] = [thm] |
|
26970 | 430 |
| common_typ_funcs (thms as thm :: _) = (*FIXME is too general*) |
24219 | 431 |
let |
432 |
val thy = Thm.theory_of_thm thm; |
|
433 |
fun incr_thm thm max = |
|
434 |
let |
|
435 |
val thm' = incr_indexes max thm; |
|
436 |
val max' = Thm.maxidx_of thm' + 1; |
|
437 |
in (thm', max') end; |
|
438 |
val (thms', maxidx) = fold_map incr_thm thms 0; |
|
26970 | 439 |
val ty1 :: tys = map (snd o const_of thy) thms'; |
24219 | 440 |
fun unify ty env = Sign.typ_unify thy (ty1, ty) env |
441 |
handle Type.TUNIFY => |
|
442 |
error ("Type unificaton failed, while unifying defining equations\n" |
|
443 |
^ (cat_lines o map Display.string_of_thm) thms |
|
444 |
^ "\nwith types\n" |
|
28054 | 445 |
^ (cat_lines o map (Code_Unit.string_of_typ thy)) (ty1 :: tys)); |
24219 | 446 |
val (env, _) = fold unify tys (Vartab.empty, maxidx) |
447 |
val instT = Vartab.fold (fn (x_i, (sort, ty)) => |
|
448 |
cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env []; |
|
449 |
in map (Thm.instantiate (instT, [])) thms' end; |
|
450 |
||
451 |
fun certify_const thy const thms = |
|
452 |
let |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
453 |
fun cert thm = if const = const_of_func thy thm |
24219 | 454 |
then thm else error ("Wrong head of defining equation,\nexpected constant " |
28054 | 455 |
^ Code_Unit.string_of_const thy const ^ "\n" ^ Display.string_of_thm thm) |
24219 | 456 |
in map cert thms end; |
457 |
||
458 |
||
459 |
||
460 |
(** operational sort algebra and class discipline **) |
|
461 |
||
462 |
local |
|
463 |
||
464 |
fun aggr_neutr f y [] = y |
|
465 |
| aggr_neutr f y (x::xs) = aggr_neutr f (f y x) xs; |
|
466 |
||
467 |
fun aggregate f [] = NONE |
|
468 |
| aggregate f (x::xs) = SOME (aggr_neutr f x xs); |
|
469 |
||
25462 | 470 |
fun inter_sorts algebra = |
471 |
aggregate (map2 (curry (Sorts.inter_sort algebra))); |
|
24219 | 472 |
|
473 |
fun specific_constraints thy (class, tyco) = |
|
474 |
let |
|
475 |
val vs = Name.invents Name.context "" (Sign.arity_number thy tyco); |
|
24969 | 476 |
val classparams = (map fst o these o try (#params o AxClass.get_info thy)) class; |
24837 | 477 |
val funcs = classparams |
25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25501
diff
changeset
|
478 |
|> map_filter (fn c => try (AxClass.param_of_inst thy) (c, tyco)) |
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
479 |
|> map (Symtab.lookup ((the_funcs o the_exec) thy)) |
28350 | 480 |
|> (map o Option.map) (map fst o Susp.force o snd) |
24219 | 481 |
|> maps these |
28143 | 482 |
|> map (Thm.transfer thy); |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
483 |
fun sorts_of [Type (_, tys)] = map (snd o dest_TVar) tys |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
484 |
| sorts_of tys = map (snd o dest_TVar) tys; |
26970 | 485 |
val sorts = map (sorts_of o Sign.const_typargs thy o const_of thy) funcs; |
24219 | 486 |
in sorts end; |
487 |
||
25462 | 488 |
fun weakest_constraints thy algebra (class, tyco) = |
24219 | 489 |
let |
25462 | 490 |
val all_superclasses = Sorts.complete_sort algebra [class]; |
491 |
in case inter_sorts algebra (maps (fn class => specific_constraints thy (class, tyco)) all_superclasses) |
|
24219 | 492 |
of SOME sorts => sorts |
25462 | 493 |
| NONE => Sorts.mg_domain algebra tyco [class] |
24219 | 494 |
end; |
495 |
||
25462 | 496 |
fun strongest_constraints thy algebra (class, tyco) = |
24219 | 497 |
let |
498 |
val all_subclasses = class :: Graph.all_preds ((#classes o Sorts.rep_algebra) algebra) [class]; |
|
499 |
val inst_subclasses = filter (can (Sorts.mg_domain algebra tyco) o single) all_subclasses; |
|
25462 | 500 |
in case inter_sorts algebra (maps (fn class => specific_constraints thy (class, tyco)) inst_subclasses) |
24219 | 501 |
of SOME sorts => sorts |
502 |
| NONE => replicate |
|
25462 | 503 |
(Sign.arity_number thy tyco) (Sorts.minimize_sort algebra (Sorts.all_classes algebra)) |
504 |
end; |
|
505 |
||
506 |
fun get_algebra thy (class, tyco) = |
|
507 |
let |
|
508 |
val base_algebra = Sign.classes_of thy; |
|
509 |
in if can (Sorts.mg_domain base_algebra tyco) [class] |
|
510 |
then base_algebra |
|
511 |
else let |
|
512 |
val superclasses = Sorts.super_classes base_algebra class; |
|
513 |
val sorts = inter_sorts base_algebra |
|
514 |
(map_filter (fn class => try (Sorts.mg_domain base_algebra tyco) [class]) superclasses) |
|
515 |
|> the_default (replicate (Sign.arity_number thy tyco) []) |
|
516 |
in |
|
517 |
base_algebra |
|
26947
133905a0c493
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26928
diff
changeset
|
518 |
|> Sorts.add_arities (Syntax.pp_global thy) (tyco, [(class, sorts)]) |
25462 | 519 |
end |
24219 | 520 |
end; |
521 |
||
24837 | 522 |
fun gen_classparam_typ constr thy class (c, tyco) = |
24219 | 523 |
let |
25462 | 524 |
val algebra = get_algebra thy (class, tyco); |
24969 | 525 |
val cs = these (try (#params o AxClass.get_info thy) class); |
25462 | 526 |
val SOME ty = AList.lookup (op =) cs c; |
24969 | 527 |
val sort_args = Name.names (Name.declare Name.aT Name.context) Name.aT |
25462 | 528 |
(constr thy algebra (class, tyco)); |
24219 | 529 |
val ty_inst = Type (tyco, map TFree sort_args); |
530 |
in Logic.varifyT (map_type_tfree (K ty_inst) ty) end; |
|
531 |
||
532 |
fun retrieve_algebra thy operational = |
|
26947
133905a0c493
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26928
diff
changeset
|
533 |
Sorts.subalgebra (Syntax.pp_global thy) operational |
25462 | 534 |
(weakest_constraints thy (Sign.classes_of thy)) |
24219 | 535 |
(Sign.classes_of thy); |
536 |
||
537 |
in |
|
538 |
||
539 |
fun coregular_algebra thy = retrieve_algebra thy (K true) |> snd; |
|
540 |
fun operational_algebra thy = |
|
541 |
let |
|
542 |
fun add_iff_operational class = |
|
24928
3419943838f5
renamed AxClass.get_definition to AxClass.get_info (again);
wenzelm
parents:
24848
diff
changeset
|
543 |
can (AxClass.get_info thy) class ? cons class; |
24219 | 544 |
val operational_classes = fold add_iff_operational (Sign.all_classes thy) [] |
545 |
in retrieve_algebra thy (member (op =) operational_classes) end; |
|
546 |
||
24837 | 547 |
val classparam_weakest_typ = gen_classparam_typ weakest_constraints; |
548 |
val classparam_strongest_typ = gen_classparam_typ strongest_constraints; |
|
24219 | 549 |
|
550 |
fun assert_func_typ thm = |
|
551 |
let |
|
552 |
val thy = Thm.theory_of_thm thm; |
|
24837 | 553 |
fun check_typ_classparam tyco (c, thm) = |
24219 | 554 |
let |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
555 |
val SOME class = AxClass.class_of_param thy c; |
26970 | 556 |
val (_, ty) = const_of thy thm; |
24837 | 557 |
val ty_decl = classparam_weakest_typ thy class (c, tyco); |
558 |
val ty_strongest = classparam_strongest_typ thy class (c, tyco); |
|
24219 | 559 |
fun constrain thm = |
560 |
let |
|
561 |
val max = Thm.maxidx_of thm + 1; |
|
562 |
val ty_decl' = Logic.incr_tvar max ty_decl; |
|
26970 | 563 |
val (_, ty') = const_of thy thm; |
24219 | 564 |
val (env, _) = Sign.typ_unify thy (ty_decl', ty') (Vartab.empty, max); |
565 |
val instT = Vartab.fold (fn (x_i, (sort, ty)) => |
|
566 |
cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env []; |
|
567 |
in Thm.instantiate (instT, []) thm end; |
|
568 |
in if Sign.typ_instance thy (ty_strongest, ty) |
|
569 |
then if Sign.typ_instance thy (ty, ty_decl) |
|
570 |
then thm |
|
28054 | 571 |
else (warning ("Constraining type\n" ^ Code_Unit.string_of_typ thy ty |
24219 | 572 |
^ "\nof defining equation\n" |
26928 | 573 |
^ Display.string_of_thm thm |
24219 | 574 |
^ "\nto permitted most general type\n" |
28054 | 575 |
^ Code_Unit.string_of_typ thy ty_decl); |
24219 | 576 |
constrain thm) |
28054 | 577 |
else Code_Unit.bad_thm ("Type\n" ^ Code_Unit.string_of_typ thy ty |
24219 | 578 |
^ "\nof defining equation\n" |
26928 | 579 |
^ Display.string_of_thm thm |
24219 | 580 |
^ "\nis incompatible with permitted least general type\n" |
28054 | 581 |
^ Code_Unit.string_of_typ thy ty_strongest) |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
582 |
end; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
583 |
fun check_typ_fun (c, thm) = |
24219 | 584 |
let |
26970 | 585 |
val (_, ty) = const_of thy thm; |
24219 | 586 |
val ty_decl = Sign.the_const_type thy c; |
587 |
in if Sign.typ_equiv thy (Type.strip_sorts ty_decl, Type.strip_sorts ty) |
|
588 |
then thm |
|
28054 | 589 |
else Code_Unit.bad_thm ("Type\n" ^ Code_Unit.string_of_typ thy ty |
24219 | 590 |
^ "\nof defining equation\n" |
26928 | 591 |
^ Display.string_of_thm thm |
24219 | 592 |
^ "\nis incompatible with declared function type\n" |
28054 | 593 |
^ Code_Unit.string_of_typ thy ty_decl) |
24219 | 594 |
end; |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
595 |
fun check_typ (c, thm) = |
25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25501
diff
changeset
|
596 |
case AxClass.inst_of_param thy c |
24837 | 597 |
of SOME (c, tyco) => check_typ_classparam tyco (c, thm) |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
598 |
| NONE => check_typ_fun (c, thm); |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
599 |
in check_typ (const_of_func thy thm, thm) end; |
24219 | 600 |
|
28350 | 601 |
fun mk_func linear = Code_Unit.error_thm (assert_func_typ o Code_Unit.mk_func linear); |
602 |
val mk_liberal_func = Code_Unit.warning_thm (assert_func_typ o Code_Unit.mk_func true); |
|
603 |
val mk_syntactic_func = Code_Unit.warning_thm (assert_func_typ o Code_Unit.mk_func false); |
|
604 |
val mk_default_func = Code_Unit.try_thm (assert_func_typ o Code_Unit.mk_func true); |
|
24219 | 605 |
|
28143 | 606 |
end; (*local*) |
24219 | 607 |
|
608 |
||
609 |
(** interfaces and attributes **) |
|
610 |
||
24624
b8383b1bbae3
distinction between regular and default code theorems
haftmann
parents:
24585
diff
changeset
|
611 |
fun delete_force msg key xs = |
b8383b1bbae3
distinction between regular and default code theorems
haftmann
parents:
24585
diff
changeset
|
612 |
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
|
613 |
else error ("No such " ^ msg ^ ": " ^ quote key); |
b8383b1bbae3
distinction between regular and default code theorems
haftmann
parents:
24585
diff
changeset
|
614 |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
615 |
fun get_datatype thy tyco = |
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
616 |
case Symtab.lookup ((the_dtyps o the_exec) thy) tyco |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
617 |
of SOME spec => spec |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
618 |
| NONE => Sign.arity_number thy tyco |
24848 | 619 |
|> Name.invents Name.context Name.aT |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
620 |
|> map (rpair []) |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
621 |
|> rpair []; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
622 |
|
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
623 |
fun get_datatype_of_constr thy c = |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
624 |
case (snd o strip_type o Sign.the_const_type thy) c |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
625 |
of Type (tyco, _) => if member (op =) |
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
626 |
((the_default [] o Option.map (map fst o snd) o Symtab.lookup ((the_dtyps o the_exec) thy)) tyco) c |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
627 |
then SOME tyco else NONE |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
628 |
| _ => NONE; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
629 |
|
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
630 |
fun get_constr_typ thy c = |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
631 |
case get_datatype_of_constr thy c |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
632 |
of SOME tyco => let |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
633 |
val (vs, cos) = get_datatype thy tyco; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
634 |
val SOME tys = AList.lookup (op =) cos c; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
635 |
val ty = tys ---> Type (tyco, map TFree vs); |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
636 |
in SOME (Logic.varifyT ty) end |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
637 |
| NONE => NONE; |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
638 |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
639 |
val get_case_data = Symtab.lookup o fst o the_cases o the_exec; |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
640 |
|
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
641 |
val is_undefined = Symtab.defined o snd o the_cases o the_exec; |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
642 |
|
28350 | 643 |
fun gen_add_func linear strict default thm thy = |
644 |
case (if strict then SOME o mk_func linear else mk_liberal_func) thm |
|
28143 | 645 |
of SOME func => |
646 |
let |
|
647 |
val c = const_of_func thy func; |
|
648 |
val _ = if strict andalso (is_some o AxClass.class_of_param thy) c |
|
649 |
then error ("Rejected polymorphic equation for overloaded constant:\n" |
|
650 |
^ Display.string_of_thm thm) |
|
651 |
else (); |
|
652 |
val _ = if strict andalso (is_some o get_datatype_of_constr thy) c |
|
653 |
then error ("Rejected equation for datatype constructor:\n" |
|
654 |
^ Display.string_of_thm func) |
|
655 |
else (); |
|
656 |
in |
|
657 |
(map_exec_purge (SOME [c]) o map_funcs) (Symtab.map_default |
|
28350 | 658 |
(c, (true, Susp.value [])) (add_thm default (func, linear))) thy |
24624
b8383b1bbae3
distinction between regular and default code theorems
haftmann
parents:
24585
diff
changeset
|
659 |
end |
b8383b1bbae3
distinction between regular and default code theorems
haftmann
parents:
24585
diff
changeset
|
660 |
| NONE => thy; |
b8383b1bbae3
distinction between regular and default code theorems
haftmann
parents:
24585
diff
changeset
|
661 |
|
28350 | 662 |
val add_func = gen_add_func true true false; |
663 |
val add_liberal_func = gen_add_func true false false; |
|
664 |
val add_default_func = gen_add_func true false true; |
|
665 |
val add_nonlinear_func = gen_add_func false true false; |
|
24219 | 666 |
|
28350 | 667 |
fun del_func thm thy = case mk_syntactic_func thm |
28143 | 668 |
of SOME func => let |
669 |
val c = const_of_func thy func; |
|
670 |
in map_exec_purge (SOME [c]) (map_funcs |
|
671 |
(Symtab.map_entry c (del_thm func))) thy |
|
672 |
end |
|
673 |
| NONE => thy; |
|
24219 | 674 |
|
28143 | 675 |
fun del_funcs c = map_exec_purge (SOME [c]) |
676 |
(map_funcs (Symtab.map_entry c (K (false, Susp.value [])))); |
|
26021 | 677 |
|
28143 | 678 |
fun add_funcl (c, lthms) thy = |
24219 | 679 |
let |
28143 | 680 |
val lthms' = certificate thy (fn thy => certify_const thy c) lthms; |
24219 | 681 |
(*FIXME must check compatibility with sort algebra; |
682 |
alas, naive checking results in non-termination!*) |
|
683 |
in |
|
28143 | 684 |
map_exec_purge (SOME [c]) |
685 |
(map_funcs (Symtab.map_default (c, (true, Susp.value [])) |
|
686 |
(add_lthms lthms'))) thy |
|
24219 | 687 |
end; |
688 |
||
24624
b8383b1bbae3
distinction between regular and default code theorems
haftmann
parents:
24585
diff
changeset
|
689 |
val add_default_func_attr = Attrib.internal (fn _ => Thm.declaration_attribute |
b8383b1bbae3
distinction between regular and default code theorems
haftmann
parents:
24585
diff
changeset
|
690 |
(fn thm => Context.mapping (add_default_func thm) I)); |
24219 | 691 |
|
25485 | 692 |
structure TypeInterpretation = InterpretationFun(type T = string * serial val eq = eq_snd (op =) : T * T -> bool); |
25462 | 693 |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
694 |
fun add_datatype raw_cs thy = |
24219 | 695 |
let |
25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25501
diff
changeset
|
696 |
val cs = map (fn c_ty as (_, ty) => (AxClass.unoverload_const thy c_ty, ty)) raw_cs; |
28054 | 697 |
val (tyco, vs_cos) = Code_Unit.constrset_of_consts thy cs; |
25462 | 698 |
val cs' = map fst (snd vs_cos); |
699 |
val purge_cs = case Symtab.lookup ((the_dtyps o the_exec) thy) tyco |
|
700 |
of SOME (vs, cos) => if null cos then NONE else SOME (cs' @ map fst cos) |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
701 |
| NONE => NONE; |
24219 | 702 |
in |
703 |
thy |
|
25462 | 704 |
|> map_exec_purge purge_cs (map_dtyps (Symtab.update (tyco, vs_cos)) |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
705 |
#> map_funcs (fold (Symtab.delete_safe o fst) cs)) |
25485 | 706 |
|> TypeInterpretation.data (tyco, serial ()) |
24219 | 707 |
end; |
708 |
||
25485 | 709 |
fun type_interpretation f = TypeInterpretation.interpretation |
710 |
(fn (tyco, _) => fn thy => f (tyco, get_datatype thy tyco) thy); |
|
711 |
||
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
712 |
fun add_datatype_cmd raw_cs thy = |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
713 |
let |
28054 | 714 |
val cs = map (Code_Unit.read_bare_const thy) raw_cs; |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
715 |
in add_datatype cs thy end; |
24219 | 716 |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
717 |
fun add_case thm thy = |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
718 |
let |
28054 | 719 |
val entry as (c, _) = Code_Unit.case_cert thm; |
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
720 |
in |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
721 |
(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
|
722 |
end; |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
723 |
|
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
724 |
fun add_undefined c thy = |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
725 |
(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
|
726 |
|
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
727 |
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
|
728 |
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
|
729 |
|
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
730 |
fun add_inline thm thy = (map_pre o MetaSimplifier.add_simp) |
28054 | 731 |
(Code_Unit.error_thm Code_Unit.mk_rew thm) thy; |
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
732 |
(*fully applied in order to get right context for mk_rew!*) |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
733 |
|
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
734 |
fun del_inline thm thy = (map_pre o MetaSimplifier.del_simp) |
28054 | 735 |
(Code_Unit.error_thm Code_Unit.mk_rew thm) thy; |
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
736 |
(*fully applied in order to get right context for mk_rew!*) |
24219 | 737 |
|
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
738 |
fun add_post thm thy = (map_post o MetaSimplifier.add_simp) |
28054 | 739 |
(Code_Unit.error_thm Code_Unit.mk_rew thm) thy; |
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
740 |
(*fully applied in order to get right context for mk_rew!*) |
24219 | 741 |
|
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
742 |
fun del_post thm thy = (map_post o MetaSimplifier.del_simp) |
28054 | 743 |
(Code_Unit.error_thm Code_Unit.mk_rew thm) thy; |
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
744 |
(*fully applied in order to get right context for mk_rew!*) |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
745 |
|
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
746 |
fun add_functrans (name, f) = |
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
747 |
(map_exec_purge NONE o map_thmproc o apsnd) |
24219 | 748 |
(AList.update (op =) (name, (serial (), f))); |
749 |
||
27557
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
haftmann
parents:
26970
diff
changeset
|
750 |
fun del_functrans name = |
24219 | 751 |
(map_exec_purge NONE o map_thmproc o apsnd) |
27609 | 752 |
(delete_force "function transformer" name); |
24219 | 753 |
|
26463 | 754 |
val _ = Context.>> (Context.map_theory |
24219 | 755 |
(let |
756 |
fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I); |
|
757 |
fun add_simple_attribute (name, f) = |
|
758 |
add_attribute (name, Scan.succeed (mk_attribute f)); |
|
759 |
fun add_del_attribute (name, (add, del)) = |
|
760 |
add_attribute (name, Args.del |-- Scan.succeed (mk_attribute del) |
|
761 |
|| Scan.succeed (mk_attribute add)) |
|
762 |
in |
|
25462 | 763 |
TypeInterpretation.init |
764 |
#> add_del_attribute ("func", (add_func, del_func)) |
|
28350 | 765 |
#> add_simple_attribute ("nbe", add_nonlinear_func) |
24219 | 766 |
#> add_del_attribute ("inline", (add_inline, del_inline)) |
767 |
#> add_del_attribute ("post", (add_post, del_post)) |
|
26463 | 768 |
end)); |
24219 | 769 |
|
770 |
||
771 |
(** post- and preprocessing **) |
|
772 |
||
773 |
local |
|
774 |
||
27609 | 775 |
fun apply_functrans thy [] = [] |
28353 | 776 |
| apply_functrans thy (thms as (thm, _) :: _) = |
24219 | 777 |
let |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
778 |
val const = const_of_func thy thm; |
27609 | 779 |
val functrans = (map (fn (_, (_, f)) => f thy) o #functrans |
780 |
o the_thmproc o the_exec) thy; |
|
28353 | 781 |
val thms' = perhaps (perhaps_loop (perhaps_apply functrans)) (map fst thms); |
782 |
val thms'' = certify_const thy const thms'; |
|
783 |
val linears = map snd thms; |
|
784 |
in (*FIXME temporary workaround*) if length thms'' = length linears |
|
785 |
then thms'' ~~ linears |
|
786 |
else map (rpair true) thms'' |
|
787 |
end; |
|
24219 | 788 |
|
789 |
fun rhs_conv conv thm = |
|
790 |
let |
|
791 |
val thm' = (conv o Thm.rhs_of) thm; |
|
792 |
in Thm.transitive thm thm' end |
|
793 |
||
24837 | 794 |
fun term_of_conv thy f = |
795 |
Thm.cterm_of thy |
|
796 |
#> f |
|
797 |
#> Thm.prop_of |
|
798 |
#> Logic.dest_equals |
|
799 |
#> snd; |
|
800 |
||
24219 | 801 |
in |
802 |
||
803 |
fun preprocess thy thms = |
|
27582 | 804 |
let |
805 |
val pre = (Simplifier.theory_context thy o #pre o the_thmproc o the_exec) thy; |
|
806 |
in |
|
807 |
thms |
|
27609 | 808 |
|> apply_functrans thy |
28353 | 809 |
|> (map o apfst) (Code_Unit.rewrite_func pre) |
28350 | 810 |
(*FIXME - must check here: rewrite rule, defining equation, proper constant *) |
28353 | 811 |
|> (map o apfst) (AxClass.unoverload thy) |
812 |
|> burrow_fst common_typ_funcs |
|
27582 | 813 |
end; |
26970 | 814 |
|
24219 | 815 |
|
816 |
fun preprocess_conv ct = |
|
817 |
let |
|
818 |
val thy = Thm.theory_of_cterm ct; |
|
27582 | 819 |
val pre = (Simplifier.theory_context thy o #pre o the_thmproc o the_exec) thy; |
24219 | 820 |
in |
821 |
ct |
|
27582 | 822 |
|> Simplifier.rewrite pre |
25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25501
diff
changeset
|
823 |
|> rhs_conv (AxClass.unoverload_conv thy) |
24219 | 824 |
end; |
825 |
||
24837 | 826 |
fun preprocess_term thy = term_of_conv thy preprocess_conv; |
827 |
||
24219 | 828 |
fun postprocess_conv ct = |
829 |
let |
|
830 |
val thy = Thm.theory_of_cterm ct; |
|
27582 | 831 |
val post = (Simplifier.theory_context thy o #post o the_thmproc o the_exec) thy; |
24219 | 832 |
in |
833 |
ct |
|
25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25501
diff
changeset
|
834 |
|> AxClass.overload_conv thy |
27582 | 835 |
|> rhs_conv (Simplifier.rewrite post) |
24219 | 836 |
end; |
837 |
||
24837 | 838 |
fun postprocess_term thy = term_of_conv thy postprocess_conv; |
839 |
||
24219 | 840 |
end; (*local*) |
841 |
||
25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25501
diff
changeset
|
842 |
fun default_typ_proto thy c = case AxClass.inst_of_param thy c |
24837 | 843 |
of SOME (c, tyco) => classparam_weakest_typ thy ((the o AxClass.class_of_param thy) c) |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
844 |
(c, tyco) |> SOME |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
845 |
| NONE => (case AxClass.class_of_param thy c |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
846 |
of SOME class => SOME (Term.map_type_tvar |
24848 | 847 |
(K (TVar ((Name.aT, 0), [class]))) (Sign.the_const_type thy c)) |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
848 |
| NONE => get_constr_typ thy c); |
24219 | 849 |
|
850 |
local |
|
851 |
||
852 |
fun get_funcs thy const = |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
853 |
Symtab.lookup ((the_funcs o the_exec) thy) const |
28143 | 854 |
|> Option.map (Susp.force o snd) |
24219 | 855 |
|> these |
28350 | 856 |
|> (map o apfst) (Thm.transfer thy); |
24219 | 857 |
|
858 |
in |
|
859 |
||
860 |
fun these_funcs thy const = |
|
861 |
let |
|
862 |
fun drop_refl thy = filter_out (is_equal o Term.fast_term_ord o Logic.dest_equals |
|
28350 | 863 |
o ObjectLogic.drop_judgment thy o Thm.plain_prop_of o fst); |
24219 | 864 |
in |
865 |
get_funcs thy const |
|
28353 | 866 |
|> preprocess thy |
24219 | 867 |
|> drop_refl thy |
868 |
end; |
|
869 |
||
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
870 |
fun default_typ thy c = case default_typ_proto thy c |
28054 | 871 |
of SOME ty => Code_Unit.typscheme thy (c, ty) |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
872 |
| NONE => (case get_funcs thy c |
28350 | 873 |
of (thm, _) :: _ => snd (Code_Unit.head_func (AxClass.unoverload thy thm)) |
28054 | 874 |
| [] => Code_Unit.typscheme thy (c, Sign.the_const_type thy c)); |
24219 | 875 |
|
876 |
end; (*local*) |
|
877 |
||
878 |
end; (*struct*) |
|
879 |
||
880 |
||
881 |
(** type-safe interfaces for data depedent on executable content **) |
|
882 |
||
883 |
functor CodeDataFun(Data: CODE_DATA_ARGS): CODE_DATA = |
|
884 |
struct |
|
885 |
||
886 |
type T = Data.T; |
|
887 |
exception Data of T; |
|
888 |
fun dest (Data x) = x |
|
889 |
||
890 |
val kind = Code.declare_data (Data Data.empty) |
|
27609 | 891 |
(fn thy => fn cs => fn Data x => Data (Data.purge thy cs x)); |
24219 | 892 |
|
893 |
val data_op = (kind, Data, dest); |
|
894 |
||
895 |
val get = Code.get_data data_op; |
|
896 |
val change = Code.change_data data_op; |
|
897 |
fun change_yield thy = Code.change_yield_data data_op thy; |
|
898 |
||
899 |
end; |
|
900 |
||
28143 | 901 |
structure Code : CODE = struct open Code; end; |