author | haftmann |
Mon, 05 Oct 2009 15:04:45 +0200 | |
changeset 32873 | 333945c9ac6a |
parent 32872 | 019201eb7e07 |
child 32928 | 6bcc35f7ff6d |
permissions | -rw-r--r-- |
24219 | 1 |
(* Title: Pure/Isar/code.ML |
2 |
Author: Florian Haftmann, TU Muenchen |
|
3 |
||
31962 | 4 |
Abstract executable code of theory. Management of data dependent on |
5 |
executable code. Cache assumes non-concurrent processing of a single theory. |
|
24219 | 6 |
*) |
7 |
||
8 |
signature CODE = |
|
9 |
sig |
|
31957 | 10 |
(*constants*) |
11 |
val check_const: theory -> term -> string |
|
12 |
val read_bare_const: theory -> string -> string * typ |
|
13 |
val read_const: theory -> string -> string |
|
31962 | 14 |
val string_of_const: theory -> string -> string |
15 |
val args_number: theory -> string -> int |
|
31957 | 16 |
|
31156 | 17 |
(*constructor sets*) |
18 |
val constrset_of_consts: theory -> (string * typ) list |
|
19 |
-> string * ((string * sort) list * (string * typ list) list) |
|
20 |
||
21 |
(*code equations*) |
|
31962 | 22 |
val mk_eqn: theory -> thm * bool -> thm * bool |
23 |
val mk_eqn_warning: theory -> thm -> (thm * bool) option |
|
24 |
val mk_eqn_liberal: theory -> thm -> (thm * bool) option |
|
31156 | 25 |
val assert_eqn: theory -> thm * bool -> thm * bool |
26 |
val assert_eqns_const: theory -> string |
|
27 |
-> (thm * bool) list -> (thm * bool) list |
|
31957 | 28 |
val const_typ_eqn: theory -> thm -> string * typ |
31156 | 29 |
val typscheme_eqn: theory -> thm -> (string * sort) list * typ |
32873 | 30 |
val typscheme_eqns: theory -> string -> thm list -> (string * sort) list * typ |
31156 | 31 |
|
31962 | 32 |
(*executable code*) |
31156 | 33 |
val add_datatype: (string * typ) list -> theory -> theory |
34 |
val add_datatype_cmd: string list -> theory -> theory |
|
35 |
val type_interpretation: |
|
36 |
(string * ((string * sort) list * (string * typ list) list) |
|
37 |
-> theory -> theory) -> theory -> theory |
|
28368 | 38 |
val add_eqn: thm -> theory -> theory |
31957 | 39 |
val add_eqnl: string * (thm * bool) list lazy -> theory -> theory |
31088 | 40 |
val add_nbe_eqn: thm -> theory -> theory |
28368 | 41 |
val add_default_eqn: thm -> theory -> theory |
28703 | 42 |
val add_default_eqn_attribute: attribute |
43 |
val add_default_eqn_attrib: Attrib.src |
|
28368 | 44 |
val del_eqn: thm -> theory -> theory |
45 |
val del_eqns: string -> theory -> theory |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
46 |
val add_case: thm -> theory -> theory |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
47 |
val add_undefined: string -> theory -> theory |
24219 | 48 |
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
|
49 |
val get_datatype_of_constr: theory -> string -> string option |
31156 | 50 |
val these_eqns: theory -> string -> (thm * bool) list |
31642 | 51 |
val all_eqns: theory -> (thm * bool) list |
30023
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
52 |
val get_case_scheme: theory -> string -> (int * (int * string list)) option |
31890
e943b039f0ac
an intermediate step towards a refined translation of cases
haftmann
parents:
31642
diff
changeset
|
53 |
val undefineds: theory -> string list |
24219 | 54 |
val print_codesetup: theory -> unit |
31957 | 55 |
|
56 |
(*infrastructure*) |
|
31998
2c7a24f74db9
code attributes use common underscore convention
haftmann
parents:
31971
diff
changeset
|
57 |
val set_code_target_attr: (string -> thm -> theory -> theory) -> theory -> theory |
31957 | 58 |
val purge_data: theory -> theory |
24219 | 59 |
end; |
60 |
||
61 |
signature CODE_DATA_ARGS = |
|
62 |
sig |
|
63 |
type T |
|
64 |
val empty: T |
|
27609 | 65 |
val purge: theory -> string list -> T -> T |
24219 | 66 |
end; |
67 |
||
68 |
signature CODE_DATA = |
|
69 |
sig |
|
70 |
type T |
|
71 |
val get: theory -> T |
|
72 |
val change: theory -> (T -> T) -> T |
|
73 |
val change_yield: theory -> (T -> 'a * T) -> 'a * T |
|
74 |
end; |
|
75 |
||
76 |
signature PRIVATE_CODE = |
|
77 |
sig |
|
78 |
include CODE |
|
27609 | 79 |
val declare_data: Object.T -> (theory -> string list -> Object.T -> Object.T) |
80 |
-> serial |
|
24219 | 81 |
val get_data: serial * ('a -> Object.T) * (Object.T -> 'a) |
82 |
-> theory -> 'a |
|
83 |
val change_data: serial * ('a -> Object.T) * (Object.T -> 'a) |
|
84 |
-> theory -> ('a -> 'a) -> 'a |
|
85 |
val change_yield_data: serial * ('a -> Object.T) * (Object.T -> 'a) |
|
86 |
-> theory -> ('a -> 'b * 'a) -> 'b * 'a |
|
87 |
end; |
|
88 |
||
89 |
structure Code : PRIVATE_CODE = |
|
90 |
struct |
|
91 |
||
31962 | 92 |
(** auxiliary **) |
93 |
||
94 |
(* printing *) |
|
31156 | 95 |
|
96 |
fun string_of_typ thy = setmp show_sorts true (Syntax.string_of_typ_global thy); |
|
31962 | 97 |
|
31156 | 98 |
fun string_of_const thy c = case AxClass.inst_of_param thy c |
99 |
of SOME (c, tyco) => Sign.extern_const thy c ^ " " ^ enclose "[" "]" (Sign.extern_type thy tyco) |
|
100 |
| NONE => Sign.extern_const thy c; |
|
101 |
||
31962 | 102 |
|
103 |
(* constants *) |
|
31156 | 104 |
|
31962 | 105 |
fun check_bare_const thy t = case try dest_Const t |
106 |
of SOME c_ty => c_ty |
|
107 |
| NONE => error ("Not a constant: " ^ Syntax.string_of_term_global thy t); |
|
31156 | 108 |
|
31962 | 109 |
fun check_const thy = AxClass.unoverload_const thy o check_bare_const thy; |
110 |
||
111 |
fun read_bare_const thy = check_bare_const thy o Syntax.read_term_global thy; |
|
112 |
||
113 |
fun read_const thy = AxClass.unoverload_const thy o read_bare_const thy; |
|
31156 | 114 |
|
32872
019201eb7e07
variables in type schemes must be renamed simultaneously with variables in equations
haftmann
parents:
32738
diff
changeset
|
115 |
|
31962 | 116 |
|
117 |
(** data store **) |
|
118 |
||
119 |
(* code equations *) |
|
120 |
||
121 |
type eqns = bool * (thm * bool) list lazy; |
|
122 |
(*default flag, theorems with proper flag (perhaps lazy)*) |
|
123 |
||
124 |
fun pretty_lthms ctxt r = case Lazy.peek r |
|
32091
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
32070
diff
changeset
|
125 |
of SOME thms => map (Display.pretty_thm ctxt o fst) (Exn.release thms) |
31962 | 126 |
| NONE => [Pretty.str "[...]"]; |
127 |
||
128 |
fun certificate thy f r = |
|
129 |
case Lazy.peek r |
|
130 |
of SOME thms => (Lazy.value o f thy) (Exn.release thms) |
|
131 |
| NONE => let |
|
132 |
val thy_ref = Theory.check_thy thy; |
|
133 |
in Lazy.lazy (fn () => (f (Theory.deref thy_ref) o Lazy.force) r) end; |
|
134 |
||
135 |
fun add_drop_redundant thy (thm, proper) thms = |
|
136 |
let |
|
137 |
val args_of = snd o strip_comb o map_types Type.strip_sorts |
|
138 |
o fst o Logic.dest_equals o Thm.plain_prop_of; |
|
139 |
val args = args_of thm; |
|
140 |
val incr_idx = Logic.incr_indexes ([], Thm.maxidx_of thm + 1); |
|
141 |
fun matches_args args' = length args <= length args' andalso |
|
142 |
Pattern.matchess thy (args, (map incr_idx o curry Library.take (length args)) args'); |
|
143 |
fun drop (thm', proper') = if (proper orelse not proper') |
|
144 |
andalso matches_args (args_of thm') then |
|
32091
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
32070
diff
changeset
|
145 |
(warning ("Code generator: dropping redundant code equation\n" ^ |
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
32070
diff
changeset
|
146 |
Display.string_of_thm_global thy thm'); true) |
31962 | 147 |
else false; |
148 |
in (thm, proper) :: filter_out drop thms end; |
|
149 |
||
150 |
fun add_thm thy _ thm (false, thms) = (false, Lazy.map_force (add_drop_redundant thy thm) thms) |
|
151 |
| add_thm thy true thm (true, thms) = (true, Lazy.map_force (fn thms => thms @ [thm]) thms) |
|
152 |
| add_thm thy false thm (true, thms) = (false, Lazy.value [thm]); |
|
153 |
||
154 |
fun add_lthms lthms _ = (false, lthms); |
|
155 |
||
156 |
fun del_thm thm = (apsnd o Lazy.map_force) (remove (eq_fst Thm.eq_thm_prop) (thm, true)); |
|
157 |
||
158 |
||
159 |
(* executable code data *) |
|
160 |
||
161 |
datatype spec = Spec of { |
|
162 |
history_concluded: bool, |
|
163 |
eqns: ((bool * eqns) * (serial * eqns) list) Symtab.table |
|
164 |
(*with explicit history*), |
|
165 |
dtyps: ((serial * ((string * sort) list * (string * typ list) list)) list) Symtab.table |
|
166 |
(*with explicit history*), |
|
167 |
cases: (int * (int * string list)) Symtab.table * unit Symtab.table |
|
168 |
}; |
|
169 |
||
32544 | 170 |
fun make_spec (history_concluded, (eqns, (dtyps, cases))) = |
171 |
Spec { history_concluded = history_concluded, eqns = eqns, dtyps = dtyps, cases = cases }; |
|
172 |
fun map_spec f (Spec { history_concluded = history_concluded, eqns = eqns, |
|
31962 | 173 |
dtyps = dtyps, cases = cases }) = |
32544 | 174 |
make_spec (f (history_concluded, (eqns, (dtyps, cases)))); |
175 |
fun merge_spec (Spec { history_concluded = _, eqns = eqns1, |
|
31962 | 176 |
dtyps = dtyps1, cases = (cases1, undefs1) }, |
32544 | 177 |
Spec { history_concluded = _, eqns = eqns2, |
31962 | 178 |
dtyps = dtyps2, cases = (cases2, undefs2) }) = |
31156 | 179 |
let |
31962 | 180 |
fun merge_eqns ((_, history1), (_, history2)) = |
181 |
let |
|
182 |
val raw_history = AList.merge (op = : serial * serial -> bool) |
|
183 |
(K true) (history1, history2) |
|
184 |
val filtered_history = filter_out (fst o snd) raw_history |
|
185 |
val history = if null filtered_history |
|
186 |
then raw_history else filtered_history; |
|
187 |
in ((false, (snd o hd) history), history) end; |
|
188 |
val eqns = Symtab.join (K merge_eqns) (eqns1, eqns2); |
|
189 |
val dtyps = Symtab.join (K (AList.merge (op =) (K true))) (dtyps1, dtyps2); |
|
190 |
val cases = (Symtab.merge (K true) (cases1, cases2), |
|
191 |
Symtab.merge (K true) (undefs1, undefs2)); |
|
32544 | 192 |
in make_spec (false, (eqns, (dtyps, cases))) end; |
31962 | 193 |
|
194 |
fun history_concluded (Spec { history_concluded, ... }) = history_concluded; |
|
195 |
fun the_eqns (Spec { eqns, ... }) = eqns; |
|
196 |
fun the_dtyps (Spec { dtyps, ... }) = dtyps; |
|
197 |
fun the_cases (Spec { cases, ... }) = cases; |
|
32544 | 198 |
val map_history_concluded = map_spec o apfst; |
31962 | 199 |
val map_eqns = map_spec o apsnd o apfst; |
200 |
val map_dtyps = map_spec o apsnd o apsnd o apfst; |
|
201 |
val map_cases = map_spec o apsnd o apsnd o apsnd; |
|
202 |
||
203 |
||
204 |
(* data slots dependent on executable code *) |
|
205 |
||
206 |
(*private copy avoids potential conflict of table exceptions*) |
|
31971
8c1b845ed105
renamed functor TableFun to Table, and GraphFun to Graph;
wenzelm
parents:
31962
diff
changeset
|
207 |
structure Datatab = Table(type key = int val ord = int_ord); |
31962 | 208 |
|
209 |
local |
|
210 |
||
211 |
type kind = { |
|
212 |
empty: Object.T, |
|
213 |
purge: theory -> string list -> Object.T -> Object.T |
|
214 |
}; |
|
215 |
||
32738 | 216 |
val kinds = Unsynchronized.ref (Datatab.empty: kind Datatab.table); |
217 |
val kind_keys = Unsynchronized.ref ([]: serial list); |
|
31962 | 218 |
|
219 |
fun invoke f k = case Datatab.lookup (! kinds) k |
|
220 |
of SOME kind => f kind |
|
221 |
| NONE => sys_error "Invalid code data identifier"; |
|
222 |
||
223 |
in |
|
224 |
||
225 |
fun declare_data empty purge = |
|
226 |
let |
|
227 |
val k = serial (); |
|
228 |
val kind = {empty = empty, purge = purge}; |
|
32738 | 229 |
val _ = Unsynchronized.change kinds (Datatab.update (k, kind)); |
230 |
val _ = Unsynchronized.change kind_keys (cons k); |
|
31962 | 231 |
in k end; |
232 |
||
233 |
fun invoke_init k = invoke (fn kind => #empty kind) k; |
|
234 |
||
235 |
fun invoke_purge_all thy cs = |
|
236 |
fold (fn k => Datatab.map_entry k |
|
237 |
(invoke (fn kind => #purge kind thy cs) k)) (! kind_keys); |
|
238 |
||
239 |
end; (*local*) |
|
240 |
||
241 |
||
242 |
(* theory store *) |
|
243 |
||
244 |
local |
|
245 |
||
246 |
type data = Object.T Datatab.table; |
|
247 |
val empty_data = Datatab.empty : data; |
|
248 |
||
249 |
structure Code_Data = TheoryDataFun |
|
250 |
( |
|
32738 | 251 |
type T = spec * data Unsynchronized.ref; |
32544 | 252 |
val empty = (make_spec (false, |
32738 | 253 |
(Symtab.empty, (Symtab.empty, (Symtab.empty, Symtab.empty)))), Unsynchronized.ref empty_data); |
254 |
fun copy (spec, data) = (spec, Unsynchronized.ref (! data)); |
|
31962 | 255 |
val extend = copy; |
256 |
fun merge pp ((spec1, data1), (spec2, data2)) = |
|
32738 | 257 |
(merge_spec (spec1, spec2), Unsynchronized.ref empty_data); |
31962 | 258 |
); |
259 |
||
260 |
fun thy_data f thy = f ((snd o Code_Data.get) thy); |
|
261 |
||
262 |
fun get_ensure_init kind data_ref = |
|
263 |
case Datatab.lookup (! data_ref) kind |
|
264 |
of SOME x => x |
|
265 |
| NONE => let val y = invoke_init kind |
|
32738 | 266 |
in (Unsynchronized.change data_ref (Datatab.update (kind, y)); y) end; |
31962 | 267 |
|
268 |
in |
|
269 |
||
270 |
(* access to executable code *) |
|
271 |
||
272 |
val the_exec = fst o Code_Data.get; |
|
273 |
||
274 |
fun complete_class_params thy cs = |
|
275 |
fold (fn c => case AxClass.inst_of_param thy c |
|
276 |
of NONE => insert (op =) c |
|
277 |
| SOME (c', _) => insert (op =) c' #> insert (op =) c) cs []; |
|
278 |
||
279 |
fun map_exec_purge touched f thy = |
|
32738 | 280 |
Code_Data.map (fn (exec, data) => (f exec, Unsynchronized.ref (case touched |
31962 | 281 |
of SOME cs => invoke_purge_all thy (complete_class_params thy cs) (! data) |
282 |
| NONE => empty_data))) thy; |
|
283 |
||
32738 | 284 |
val purge_data = (Code_Data.map o apsnd) (K (Unsynchronized.ref empty_data)); |
31962 | 285 |
|
286 |
fun change_eqns delete c f = (map_exec_purge (SOME [c]) o map_eqns |
|
287 |
o (if delete then Symtab.map_entry c else Symtab.map_default (c, ((false, (true, Lazy.value [])), []))) |
|
288 |
o apfst) (fn (_, eqns) => (true, f eqns)); |
|
289 |
||
290 |
fun del_eqns c = change_eqns true c (K (false, Lazy.value [])); |
|
291 |
||
292 |
||
293 |
(* tackling equation history *) |
|
294 |
||
295 |
fun get_eqns thy c = |
|
296 |
Symtab.lookup ((the_eqns o the_exec) thy) c |
|
297 |
|> Option.map (Lazy.force o snd o snd o fst) |
|
298 |
|> these; |
|
299 |
||
300 |
fun continue_history thy = if (history_concluded o the_exec) thy |
|
301 |
then thy |
|
302 |
|> (Code_Data.map o apfst o map_history_concluded) (K false) |
|
303 |
|> SOME |
|
304 |
else NONE; |
|
305 |
||
306 |
fun conclude_history thy = if (history_concluded o the_exec) thy |
|
307 |
then NONE |
|
308 |
else thy |
|
309 |
|> (Code_Data.map o apfst) |
|
310 |
((map_eqns o Symtab.map) (fn ((changed, current), history) => |
|
311 |
((false, current), |
|
312 |
if changed then (serial (), current) :: history else history)) |
|
313 |
#> map_history_concluded (K true)) |
|
314 |
|> SOME; |
|
315 |
||
316 |
val _ = Context.>> (Context.map_theory (Code_Data.init |
|
317 |
#> Theory.at_begin continue_history |
|
318 |
#> Theory.at_end conclude_history)); |
|
319 |
||
320 |
||
321 |
(* access to data dependent on abstract executable code *) |
|
322 |
||
323 |
fun get_data (kind, _, dest) = thy_data (get_ensure_init kind #> dest); |
|
324 |
||
325 |
fun change_data (kind, mk, dest) = |
|
326 |
let |
|
327 |
fun chnge data_ref f = |
|
328 |
let |
|
329 |
val data = get_ensure_init kind data_ref; |
|
330 |
val data' = f (dest data); |
|
32738 | 331 |
in (Unsynchronized.change data_ref (Datatab.update (kind, mk data')); data') end; |
31962 | 332 |
in thy_data chnge end; |
333 |
||
334 |
fun change_yield_data (kind, mk, dest) = |
|
335 |
let |
|
336 |
fun chnge data_ref f = |
|
337 |
let |
|
338 |
val data = get_ensure_init kind data_ref; |
|
339 |
val (x, data') = f (dest data); |
|
32738 | 340 |
in (x, (Unsynchronized.change data_ref (Datatab.update (kind, mk data')); data')) end; |
31962 | 341 |
in thy_data chnge end; |
342 |
||
343 |
end; (*local*) |
|
344 |
||
345 |
||
346 |
(** foundation **) |
|
347 |
||
348 |
(* constants *) |
|
349 |
||
350 |
fun args_number thy = length o fst o strip_type o Sign.the_const_type thy; |
|
351 |
||
352 |
||
353 |
(* datatypes *) |
|
31156 | 354 |
|
355 |
fun constrset_of_consts thy cs = |
|
356 |
let |
|
357 |
val _ = map (fn (c, _) => if (is_some o AxClass.class_of_param thy) c |
|
358 |
then error ("Is a class parameter: " ^ string_of_const thy c) else ()) cs; |
|
359 |
fun no_constr (c, ty) = error ("Not a datatype constructor: " ^ string_of_const thy c |
|
360 |
^ " :: " ^ string_of_typ thy ty); |
|
361 |
fun last_typ c_ty ty = |
|
362 |
let |
|
363 |
val frees = OldTerm.typ_tfrees ty; |
|
364 |
val (tyco, vs) = ((apsnd o map) (dest_TFree) o dest_Type o snd o strip_type) ty |
|
365 |
handle TYPE _ => no_constr c_ty |
|
366 |
val _ = if has_duplicates (eq_fst (op =)) vs then no_constr c_ty else (); |
|
367 |
val _ = if length frees <> length vs then no_constr c_ty else (); |
|
368 |
in (tyco, vs) end; |
|
369 |
fun ty_sorts (c, ty) = |
|
370 |
let |
|
371 |
val ty_decl = (Logic.unvarifyT o Sign.the_const_type thy) c; |
|
372 |
val (tyco, _) = last_typ (c, ty) ty_decl; |
|
373 |
val (_, vs) = last_typ (c, ty) ty; |
|
374 |
in ((tyco, map snd vs), (c, (map fst vs, ty))) end; |
|
375 |
fun add ((tyco', sorts'), c) ((tyco, sorts), cs) = |
|
376 |
let |
|
31998
2c7a24f74db9
code attributes use common underscore convention
haftmann
parents:
31971
diff
changeset
|
377 |
val _ = if (tyco' : string) <> tyco |
31156 | 378 |
then error "Different type constructors in constructor set" |
379 |
else (); |
|
380 |
val sorts'' = map2 (curry (Sorts.inter_sort (Sign.classes_of thy))) sorts' sorts |
|
381 |
in ((tyco, sorts), c :: cs) end; |
|
382 |
fun inst vs' (c, (vs, ty)) = |
|
383 |
let |
|
384 |
val the_v = the o AList.lookup (op =) (vs ~~ vs'); |
|
385 |
val ty' = map_atyps (fn TFree (v, _) => TFree (the_v v)) ty; |
|
386 |
in (c, (fst o strip_type) ty') end; |
|
387 |
val c' :: cs' = map ty_sorts cs; |
|
388 |
val ((tyco, sorts), cs'') = fold add cs' (apsnd single c'); |
|
389 |
val vs = Name.names Name.context Name.aT sorts; |
|
390 |
val cs''' = map (inst vs) cs''; |
|
391 |
in (tyco, (vs, rev cs''')) end; |
|
392 |
||
31962 | 393 |
fun get_datatype thy tyco = |
394 |
case these (Symtab.lookup ((the_dtyps o the_exec) thy) tyco) |
|
395 |
of (_, spec) :: _ => spec |
|
396 |
| [] => Sign.arity_number thy tyco |
|
397 |
|> Name.invents Name.context Name.aT |
|
398 |
|> map (rpair []) |
|
399 |
|> rpair []; |
|
400 |
||
401 |
fun get_datatype_of_constr thy c = |
|
402 |
case (snd o strip_type o Sign.the_const_type thy) c |
|
403 |
of Type (tyco, _) => if member (op =) ((map fst o snd o get_datatype thy) tyco) c |
|
404 |
then SOME tyco else NONE |
|
405 |
| _ => NONE; |
|
406 |
||
407 |
fun is_constr thy = is_some o get_datatype_of_constr thy; |
|
408 |
||
31156 | 409 |
|
410 |
(* code equations *) |
|
411 |
||
412 |
exception BAD_THM of string; |
|
413 |
fun bad_thm msg = raise BAD_THM msg; |
|
414 |
fun error_thm f thm = f thm handle BAD_THM msg => error msg; |
|
31642 | 415 |
fun warning_thm f thm = SOME (f thm) handle BAD_THM msg => (warning msg; NONE) |
31156 | 416 |
fun try_thm f thm = SOME (f thm) handle BAD_THM _ => NONE; |
417 |
||
418 |
fun is_linear thm = |
|
419 |
let val (_, args) = (strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of) thm |
|
420 |
in not (has_duplicates (op =) ((fold o fold_aterms) |
|
421 |
(fn Var (v, _) => cons v | _ => I) args [])) end; |
|
422 |
||
31962 | 423 |
fun gen_assert_eqn thy is_constr_pat (thm, proper) = |
31156 | 424 |
let |
425 |
val (lhs, rhs) = (Logic.dest_equals o Thm.plain_prop_of) thm |
|
32091
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
32070
diff
changeset
|
426 |
handle TERM _ => bad_thm ("Not an equation: " ^ Display.string_of_thm_global thy thm) |
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
32070
diff
changeset
|
427 |
| THM _ => bad_thm ("Not an equation: " ^ Display.string_of_thm_global thy thm); |
31156 | 428 |
fun vars_of t = fold_aterms (fn Var (v, _) => insert (op =) v |
429 |
| Free _ => bad_thm ("Illegal free variable in equation\n" |
|
32091
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
32070
diff
changeset
|
430 |
^ Display.string_of_thm_global thy thm) |
31156 | 431 |
| _ => I) t []; |
432 |
fun tvars_of t = fold_term_types (fn _ => |
|
433 |
fold_atyps (fn TVar (v, _) => insert (op =) v |
|
434 |
| TFree _ => bad_thm |
|
32091
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
32070
diff
changeset
|
435 |
("Illegal free type variable in equation\n" ^ Display.string_of_thm_global thy thm))) t []; |
31156 | 436 |
val lhs_vs = vars_of lhs; |
437 |
val rhs_vs = vars_of rhs; |
|
438 |
val lhs_tvs = tvars_of lhs; |
|
439 |
val rhs_tvs = tvars_of rhs; |
|
440 |
val _ = if null (subtract (op =) lhs_vs rhs_vs) |
|
441 |
then () |
|
442 |
else bad_thm ("Free variables on right hand side of equation\n" |
|
32091
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
32070
diff
changeset
|
443 |
^ Display.string_of_thm_global thy thm); |
31156 | 444 |
val _ = if null (subtract (op =) lhs_tvs rhs_tvs) |
445 |
then () |
|
446 |
else bad_thm ("Free type variables on right hand side of equation\n" |
|
32091
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
32070
diff
changeset
|
447 |
^ Display.string_of_thm_global thy thm) |
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
32070
diff
changeset
|
448 |
val (head, args) = (strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of) thm; |
31156 | 449 |
val (c, ty) = case head |
450 |
of Const (c_ty as (_, ty)) => (AxClass.unoverload_const thy c_ty, ty) |
|
32091
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
32070
diff
changeset
|
451 |
| _ => bad_thm ("Equation not headed by constant\n" ^ Display.string_of_thm_global thy thm); |
31156 | 452 |
fun check _ (Abs _) = bad_thm |
453 |
("Abstraction on left hand side of equation\n" |
|
32091
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
32070
diff
changeset
|
454 |
^ Display.string_of_thm_global thy thm) |
31156 | 455 |
| check 0 (Var _) = () |
456 |
| check _ (Var _) = bad_thm |
|
457 |
("Variable with application on left hand side of equation\n" |
|
32091
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
32070
diff
changeset
|
458 |
^ Display.string_of_thm_global thy thm) |
31156 | 459 |
| check n (t1 $ t2) = (check (n+1) t1; check 0 t2) |
460 |
| check n (Const (c_ty as (c, ty))) = if n = (length o fst o strip_type) ty |
|
461 |
then if not proper orelse is_constr_pat (AxClass.unoverload_const thy c_ty) |
|
462 |
then () |
|
463 |
else bad_thm (quote c ^ " is not a constructor, on left hand side of equation\n" |
|
32091
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
32070
diff
changeset
|
464 |
^ Display.string_of_thm_global thy thm) |
31156 | 465 |
else bad_thm |
466 |
("Partially applied constant " ^ quote c ^ " on left hand side of equation\n" |
|
32091
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
32070
diff
changeset
|
467 |
^ Display.string_of_thm_global thy thm); |
31156 | 468 |
val _ = map (check 0) args; |
469 |
val _ = if not proper orelse is_linear thm then () |
|
470 |
else bad_thm ("Duplicate variables on left hand side of equation\n" |
|
32091
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
32070
diff
changeset
|
471 |
^ Display.string_of_thm_global thy thm); |
31156 | 472 |
val _ = if (is_none o AxClass.class_of_param thy) c |
473 |
then () |
|
474 |
else bad_thm ("Polymorphic constant as head in equation\n" |
|
32091
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
32070
diff
changeset
|
475 |
^ Display.string_of_thm_global thy thm) |
31962 | 476 |
val _ = if not (is_constr thy c) |
31156 | 477 |
then () |
478 |
else bad_thm ("Constructor as head in equation\n" |
|
32091
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
32070
diff
changeset
|
479 |
^ Display.string_of_thm_global thy thm) |
31156 | 480 |
val ty_decl = Sign.the_const_type thy c; |
481 |
val _ = if Sign.typ_equiv thy (Type.strip_sorts ty_decl, Type.strip_sorts ty) |
|
482 |
then () else bad_thm ("Type\n" ^ string_of_typ thy ty |
|
483 |
^ "\nof equation\n" |
|
32091
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
32070
diff
changeset
|
484 |
^ Display.string_of_thm_global thy thm |
31156 | 485 |
^ "\nis incompatible with declared function type\n" |
486 |
^ string_of_typ thy ty_decl) |
|
487 |
in (thm, proper) end; |
|
488 |
||
31962 | 489 |
fun assert_eqn thy = error_thm (gen_assert_eqn thy (is_constr thy)); |
490 |
||
491 |
fun meta_rewrite thy = LocalDefs.meta_rewrite_rule (ProofContext.init thy); |
|
31156 | 492 |
|
31962 | 493 |
fun mk_eqn thy = error_thm (gen_assert_eqn thy (K true)) o |
494 |
apfst (meta_rewrite thy); |
|
495 |
||
496 |
fun mk_eqn_warning thy = Option.map (fn (thm, _) => (thm, is_linear thm)) |
|
497 |
o warning_thm (gen_assert_eqn thy (K true)) o rpair false o meta_rewrite thy; |
|
498 |
||
499 |
fun mk_eqn_liberal thy = Option.map (fn (thm, _) => (thm, is_linear thm)) |
|
500 |
o try_thm (gen_assert_eqn thy (K true)) o rpair false o meta_rewrite thy; |
|
31156 | 501 |
|
502 |
(*those following are permissive wrt. to overloaded constants!*) |
|
503 |
||
32640
ba6531df2c64
corrected order of type variables in code equations; more precise certificate for cases
haftmann
parents:
32544
diff
changeset
|
504 |
val head_eqn = dest_Const o fst o strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of; |
31957 | 505 |
fun const_typ_eqn thy thm = |
31156 | 506 |
let |
32640
ba6531df2c64
corrected order of type variables in code equations; more precise certificate for cases
haftmann
parents:
32544
diff
changeset
|
507 |
val (c, ty) = head_eqn thm; |
31156 | 508 |
val c' = AxClass.unoverload_const thy (c, ty); |
509 |
in (c', ty) end; |
|
31957 | 510 |
fun const_eqn thy = fst o const_typ_eqn thy; |
31156 | 511 |
|
32873 | 512 |
fun typscheme thy (c, ty) = |
513 |
(map dest_TFree (Sign.const_typargs thy (c, ty)), Type.strip_sorts ty); |
|
514 |
fun typscheme_eqn thy = typscheme thy o apsnd Logic.unvarifyT o const_typ_eqn thy; |
|
515 |
fun typscheme_eqns thy c [] = |
|
516 |
let |
|
517 |
val raw_ty = Sign.the_const_type thy c; |
|
518 |
val tvars = Term.add_tvar_namesT raw_ty []; |
|
519 |
val tvars' = case AxClass.class_of_param thy c |
|
520 |
of SOME class => [TFree (Name.aT, [class])] |
|
521 |
| NONE => Name.invent_list [] Name.aT (length tvars) |
|
522 |
|> map (fn v => TFree (v, [])); |
|
523 |
val ty = typ_subst_TVars (tvars ~~ tvars') raw_ty; |
|
524 |
in typscheme thy (c, ty) end |
|
525 |
| typscheme_eqns thy c (thms as thm :: _) = typscheme_eqn thy thm; |
|
32872
019201eb7e07
variables in type schemes must be renamed simultaneously with variables in equations
haftmann
parents:
32738
diff
changeset
|
526 |
|
31962 | 527 |
fun assert_eqns_const thy c eqns = |
528 |
let |
|
529 |
fun cert (eqn as (thm, _)) = if c = const_eqn thy thm |
|
530 |
then eqn else error ("Wrong head of code equation,\nexpected constant " |
|
32091
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
32070
diff
changeset
|
531 |
^ string_of_const thy c ^ "\n" ^ Display.string_of_thm_global thy thm) |
31962 | 532 |
in map (cert o assert_eqn thy) eqns end; |
31156 | 533 |
|
32354 | 534 |
fun same_typscheme thy thms = |
535 |
let |
|
32640
ba6531df2c64
corrected order of type variables in code equations; more precise certificate for cases
haftmann
parents:
32544
diff
changeset
|
536 |
fun tvars_of T = rev (Term.add_tvarsT T []); |
ba6531df2c64
corrected order of type variables in code equations; more precise certificate for cases
haftmann
parents:
32544
diff
changeset
|
537 |
val vss = map (tvars_of o snd o head_eqn) thms; |
32354 | 538 |
fun inter_sorts vs = |
539 |
fold (curry (Sorts.inter_sort (Sign.classes_of thy)) o snd) vs []; |
|
540 |
val sorts = map_transpose inter_sorts vss; |
|
32873 | 541 |
val vts = Name.names Name.context Name.aT sorts |
32354 | 542 |
|> map (fn (v, sort) => TVar ((v, 0), sort)); |
543 |
in map2 (fn vs => Thm.certify_instantiate (vs ~~ vts, [])) vss thms end; |
|
31962 | 544 |
|
545 |
fun these_eqns thy c = |
|
546 |
get_eqns thy c |
|
547 |
|> (map o apfst) (Thm.transfer thy) |
|
32354 | 548 |
|> burrow_fst (same_typscheme thy); |
31962 | 549 |
|
550 |
fun all_eqns thy = |
|
551 |
Symtab.dest ((the_eqns o the_exec) thy) |
|
552 |
|> maps (Lazy.force o snd o snd o fst o snd); |
|
553 |
||
554 |
||
555 |
(* cases *) |
|
31156 | 556 |
|
557 |
fun case_certificate thm = |
|
558 |
let |
|
559 |
val ((head, raw_case_expr), cases) = (apfst Logic.dest_equals |
|
32640
ba6531df2c64
corrected order of type variables in code equations; more precise certificate for cases
haftmann
parents:
32544
diff
changeset
|
560 |
o apsnd Logic.dest_conjunctions o Logic.dest_implies o Thm.plain_prop_of) thm; |
31156 | 561 |
val _ = case head of Free _ => true |
562 |
| Var _ => true |
|
563 |
| _ => raise TERM ("case_cert", []); |
|
564 |
val ([(case_var, _)], case_expr) = Term.strip_abs_eta 1 raw_case_expr; |
|
565 |
val (Const (case_const, _), raw_params) = strip_comb case_expr; |
|
566 |
val n = find_index (fn Free (v, _) => v = case_var | _ => false) raw_params; |
|
567 |
val _ = if n = ~1 then raise TERM ("case_cert", []) else (); |
|
568 |
val params = map (fst o dest_Var) (nth_drop n raw_params); |
|
569 |
fun dest_case t = |
|
570 |
let |
|
571 |
val (head' $ t_co, rhs) = Logic.dest_equals t; |
|
572 |
val _ = if head' = head then () else raise TERM ("case_cert", []); |
|
573 |
val (Const (co, _), args) = strip_comb t_co; |
|
574 |
val (Var (param, _), args') = strip_comb rhs; |
|
575 |
val _ = if args' = args then () else raise TERM ("case_cert", []); |
|
576 |
in (param, co) end; |
|
577 |
fun analyze_cases cases = |
|
578 |
let |
|
579 |
val co_list = fold (AList.update (op =) o dest_case) cases []; |
|
580 |
in map (the o AList.lookup (op =) co_list) params end; |
|
581 |
fun analyze_let t = |
|
582 |
let |
|
583 |
val (head' $ arg, Var (param', _) $ arg') = Logic.dest_equals t; |
|
584 |
val _ = if head' = head then () else raise TERM ("case_cert", []); |
|
585 |
val _ = if arg' = arg then () else raise TERM ("case_cert", []); |
|
586 |
val _ = if [param'] = params then () else raise TERM ("case_cert", []); |
|
587 |
in [] end; |
|
588 |
fun analyze (cases as [let_case]) = |
|
589 |
(analyze_cases cases handle Bind => analyze_let let_case) |
|
590 |
| analyze cases = analyze_cases cases; |
|
591 |
in (case_const, (n, analyze cases)) end; |
|
592 |
||
593 |
fun case_cert thm = case_certificate thm |
|
594 |
handle Bind => error "bad case certificate" |
|
595 |
| TERM _ => error "bad case certificate"; |
|
596 |
||
31962 | 597 |
fun get_case_scheme thy = Symtab.lookup ((fst o the_cases o the_exec) thy); |
24219 | 598 |
|
31962 | 599 |
val undefineds = Symtab.keys o snd o the_cases o the_exec; |
24219 | 600 |
|
601 |
||
31962 | 602 |
(* diagnostic *) |
24219 | 603 |
|
604 |
fun print_codesetup thy = |
|
605 |
let |
|
606 |
val ctxt = ProofContext.init thy; |
|
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
607 |
val exec = the_exec thy; |
28368 | 608 |
fun pretty_eqn (s, (_, lthms)) = |
24219 | 609 |
(Pretty.block o Pretty.fbreaks) ( |
28143 | 610 |
Pretty.str s :: pretty_lthms ctxt lthms |
24219 | 611 |
); |
612 |
fun pretty_dtyp (s, []) = |
|
613 |
Pretty.str s |
|
614 |
| pretty_dtyp (s, cos) = |
|
615 |
(Pretty.block o Pretty.breaks) ( |
|
616 |
Pretty.str s |
|
617 |
:: Pretty.str "=" |
|
31156 | 618 |
:: separate (Pretty.str "|") (map (fn (c, []) => Pretty.str (string_of_const thy c) |
24219 | 619 |
| (c, tys) => |
620 |
(Pretty.block o Pretty.breaks) |
|
31156 | 621 |
(Pretty.str (string_of_const thy c) |
26947
133905a0c493
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26928
diff
changeset
|
622 |
:: Pretty.str "of" |
133905a0c493
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26928
diff
changeset
|
623 |
:: map (Pretty.quote o Syntax.pretty_typ_global thy) tys)) cos) |
24219 | 624 |
); |
28368 | 625 |
val eqns = the_eqns exec |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
626 |
|> Symtab.dest |
31156 | 627 |
|> (map o apfst) (string_of_const thy) |
28695 | 628 |
|> (map o apsnd) (snd o fst) |
24219 | 629 |
|> sort (string_ord o pairself fst); |
630 |
val dtyps = the_dtyps exec |
|
631 |
|> Symtab.dest |
|
28695 | 632 |
|> map (fn (dtco, (_, (vs, cos)) :: _) => |
31957 | 633 |
(string_of_typ thy (Type (dtco, map TFree vs)), cos)) |
24219 | 634 |
|> sort (string_ord o pairself fst) |
635 |
in |
|
636 |
(Pretty.writeln o Pretty.chunks) [ |
|
637 |
Pretty.block ( |
|
30023
55954f726043
permissive check for pattern discipline in case schemes
haftmann
parents:
29970
diff
changeset
|
638 |
Pretty.str "code equations:" |
24219 | 639 |
:: Pretty.fbrk |
28368 | 640 |
:: (Pretty.fbreaks o map pretty_eqn) eqns |
24219 | 641 |
), |
25968 | 642 |
Pretty.block ( |
24219 | 643 |
Pretty.str "datatypes:" |
644 |
:: Pretty.fbrk |
|
645 |
:: (Pretty.fbreaks o map pretty_dtyp) dtyps |
|
646 |
) |
|
647 |
] |
|
648 |
end; |
|
649 |
||
650 |
||
31962 | 651 |
(** declaring executable ingredients **) |
652 |
||
653 |
(* datatypes *) |
|
31090
3be41b271023
clarified matter of "proper" flag in code equations
haftmann
parents:
31088
diff
changeset
|
654 |
|
31962 | 655 |
structure Type_Interpretation = InterpretationFun(type T = string * serial val eq = eq_snd (op =) : T * T -> bool); |
25462 | 656 |
|
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
657 |
fun add_datatype raw_cs thy = |
24219 | 658 |
let |
25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25501
diff
changeset
|
659 |
val cs = map (fn c_ty as (_, ty) => (AxClass.unoverload_const thy c_ty, ty)) raw_cs; |
31156 | 660 |
val (tyco, vs_cos) = constrset_of_consts thy cs; |
30076 | 661 |
val old_cs = (map fst o snd o get_datatype thy) tyco; |
662 |
fun drop_outdated_cases cases = fold Symtab.delete_safe |
|
663 |
(Symtab.fold (fn (c, (_, (_, cos))) => |
|
664 |
if exists (member (op =) old_cs) cos |
|
665 |
then insert (op =) c else I) cases []) cases; |
|
24219 | 666 |
in |
667 |
thy |
|
31091
8316d22f10f5
corrected deletetion of code equations for constructors
haftmann
parents:
31090
diff
changeset
|
668 |
|> fold (del_eqns o fst) cs |
28703 | 669 |
|> map_exec_purge NONE |
28695 | 670 |
((map_dtyps o Symtab.map_default (tyco, [])) (cons (serial (), vs_cos)) |
30076 | 671 |
#> (map_cases o apfst) drop_outdated_cases) |
31962 | 672 |
|> Type_Interpretation.data (tyco, serial ()) |
24219 | 673 |
end; |
674 |
||
31962 | 675 |
fun type_interpretation f = Type_Interpretation.interpretation |
25485 | 676 |
(fn (tyco, _) => fn thy => f (tyco, get_datatype thy tyco) thy); |
677 |
||
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
678 |
fun add_datatype_cmd raw_cs thy = |
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
679 |
let |
31156 | 680 |
val cs = map (read_bare_const thy) raw_cs; |
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24283
diff
changeset
|
681 |
in add_datatype cs thy end; |
24219 | 682 |
|
31962 | 683 |
|
684 |
(* code equations *) |
|
685 |
||
686 |
fun gen_add_eqn default (eqn as (thm, _)) thy = |
|
687 |
let val c = const_eqn thy thm |
|
688 |
in change_eqns false c (add_thm thy default eqn) thy end; |
|
689 |
||
690 |
fun add_eqn thm thy = |
|
691 |
gen_add_eqn false (mk_eqn thy (thm, true)) thy; |
|
692 |
||
693 |
fun add_warning_eqn thm thy = |
|
694 |
case mk_eqn_warning thy thm |
|
695 |
of SOME eqn => gen_add_eqn false eqn thy |
|
696 |
| NONE => thy; |
|
697 |
||
698 |
fun add_default_eqn thm thy = |
|
699 |
case mk_eqn_liberal thy thm |
|
700 |
of SOME eqn => gen_add_eqn true eqn thy |
|
701 |
| NONE => thy; |
|
702 |
||
703 |
fun add_nbe_eqn thm thy = |
|
704 |
gen_add_eqn false (mk_eqn thy (thm, false)) thy; |
|
705 |
||
706 |
fun add_eqnl (c, lthms) thy = |
|
707 |
let |
|
708 |
val lthms' = certificate thy (fn thy => assert_eqns_const thy c) lthms; |
|
709 |
in change_eqns false c (add_lthms lthms') thy end; |
|
710 |
||
711 |
val add_default_eqn_attribute = Thm.declaration_attribute |
|
712 |
(fn thm => Context.mapping (add_default_eqn thm) I); |
|
713 |
val add_default_eqn_attrib = Attrib.internal (K add_default_eqn_attribute); |
|
714 |
||
715 |
fun del_eqn thm thy = case mk_eqn_liberal thy thm |
|
716 |
of SOME (thm, _) => change_eqns true (const_eqn thy thm) (del_thm thm) thy |
|
717 |
| NONE => thy; |
|
718 |
||
32070 | 719 |
(* c.f. src/HOL/Tools/recfun_codegen.ML *) |
720 |
||
721 |
structure Code_Target_Attr = TheoryDataFun ( |
|
31998
2c7a24f74db9
code attributes use common underscore convention
haftmann
parents:
31971
diff
changeset
|
722 |
type T = (string -> thm -> theory -> theory) option; |
2c7a24f74db9
code attributes use common underscore convention
haftmann
parents:
31971
diff
changeset
|
723 |
val empty = NONE; |
2c7a24f74db9
code attributes use common underscore convention
haftmann
parents:
31971
diff
changeset
|
724 |
val copy = I; |
2c7a24f74db9
code attributes use common underscore convention
haftmann
parents:
31971
diff
changeset
|
725 |
val extend = I; |
2c7a24f74db9
code attributes use common underscore convention
haftmann
parents:
31971
diff
changeset
|
726 |
fun merge _ (NONE, f2) = f2 |
2c7a24f74db9
code attributes use common underscore convention
haftmann
parents:
31971
diff
changeset
|
727 |
| merge _ (f1, _) = f1; |
2c7a24f74db9
code attributes use common underscore convention
haftmann
parents:
31971
diff
changeset
|
728 |
); |
2c7a24f74db9
code attributes use common underscore convention
haftmann
parents:
31971
diff
changeset
|
729 |
|
32070 | 730 |
fun set_code_target_attr f = Code_Target_Attr.map (K (SOME f)); |
731 |
||
732 |
fun code_target_attr prefix thm thy = |
|
733 |
let |
|
734 |
val attr = the_default ((K o K) I) (Code_Target_Attr.get thy); |
|
735 |
in thy |> add_warning_eqn thm |> attr prefix thm end; |
|
736 |
||
737 |
(* setup *) |
|
31998
2c7a24f74db9
code attributes use common underscore convention
haftmann
parents:
31971
diff
changeset
|
738 |
|
31962 | 739 |
val _ = Context.>> (Context.map_theory |
740 |
(let |
|
741 |
fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I); |
|
31998
2c7a24f74db9
code attributes use common underscore convention
haftmann
parents:
31971
diff
changeset
|
742 |
val code_attribute_parser = |
2c7a24f74db9
code attributes use common underscore convention
haftmann
parents:
31971
diff
changeset
|
743 |
Args.del |-- Scan.succeed (mk_attribute del_eqn) |
2c7a24f74db9
code attributes use common underscore convention
haftmann
parents:
31971
diff
changeset
|
744 |
|| Args.$$$ "nbe" |-- Scan.succeed (mk_attribute add_nbe_eqn) |
2c7a24f74db9
code attributes use common underscore convention
haftmann
parents:
31971
diff
changeset
|
745 |
|| (Args.$$$ "target" |-- Args.colon |-- Args.name >> |
32070 | 746 |
(mk_attribute o code_target_attr)) |
31998
2c7a24f74db9
code attributes use common underscore convention
haftmann
parents:
31971
diff
changeset
|
747 |
|| Scan.succeed (mk_attribute add_warning_eqn); |
31962 | 748 |
in |
749 |
Type_Interpretation.init |
|
31998
2c7a24f74db9
code attributes use common underscore convention
haftmann
parents:
31971
diff
changeset
|
750 |
#> Attrib.setup (Binding.name "code") (Scan.lift code_attribute_parser) |
2c7a24f74db9
code attributes use common underscore convention
haftmann
parents:
31971
diff
changeset
|
751 |
"declare theorems for code generation" |
31962 | 752 |
end)); |
753 |
||
754 |
||
755 |
(* cases *) |
|
756 |
||
24844
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
757 |
fun add_case thm thy = |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
758 |
let |
31156 | 759 |
val (c, (k, case_pats)) = case_cert thm; |
31088 | 760 |
val _ = case filter_out (is_constr thy) case_pats |
30076 | 761 |
of [] => () |
762 |
| cs => error ("Non-constructor(s) in case certificate: " ^ commas (map quote cs)); |
|
763 |
val entry = (1 + Int.max (1, length case_pats), (k, case_pats)) |
|
764 |
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
|
765 |
|
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
766 |
fun add_undefined c thy = |
98c006a30218
certificates for code generator case expressions
haftmann
parents:
24837
diff
changeset
|
767 |
(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
|
768 |
|
24219 | 769 |
end; (*struct*) |
770 |
||
771 |
||
32661
f4d179d91af2
experimenting to add some useful interface for definitions
bulwahn
parents:
32640
diff
changeset
|
772 |
(** type-safe interfaces for data dependent on executable code **) |
24219 | 773 |
|
31962 | 774 |
functor Code_Data_Fun(Data: CODE_DATA_ARGS): CODE_DATA = |
24219 | 775 |
struct |
776 |
||
777 |
type T = Data.T; |
|
778 |
exception Data of T; |
|
779 |
fun dest (Data x) = x |
|
780 |
||
781 |
val kind = Code.declare_data (Data Data.empty) |
|
27609 | 782 |
(fn thy => fn cs => fn Data x => Data (Data.purge thy cs x)); |
24219 | 783 |
|
784 |
val data_op = (kind, Data, dest); |
|
785 |
||
786 |
val get = Code.get_data data_op; |
|
787 |
val change = Code.change_data data_op; |
|
788 |
fun change_yield thy = Code.change_yield_data data_op thy; |
|
789 |
||
790 |
end; |
|
791 |
||
32661
f4d179d91af2
experimenting to add some useful interface for definitions
bulwahn
parents:
32640
diff
changeset
|
792 |
(** datastructure to log definitions for preprocessing of the predicate compiler **) |
32662 | 793 |
(* obviously a clone of Named_Thms *) |
794 |
||
795 |
signature PREDICATE_COMPILE_PREPROC_CONST_DEFS = |
|
796 |
sig |
|
797 |
val get: Proof.context -> thm list |
|
798 |
val add_thm: thm -> Context.generic -> Context.generic |
|
799 |
val del_thm: thm -> Context.generic -> Context.generic |
|
800 |
||
801 |
val add_attribute : attribute |
|
802 |
val del_attribute : attribute |
|
803 |
||
804 |
val add_attrib : Attrib.src |
|
805 |
||
806 |
val setup: theory -> theory |
|
807 |
end; |
|
808 |
||
809 |
structure Predicate_Compile_Preproc_Const_Defs : PREDICATE_COMPILE_PREPROC_CONST_DEFS = |
|
810 |
struct |
|
811 |
||
812 |
structure Data = GenericDataFun |
|
32661
f4d179d91af2
experimenting to add some useful interface for definitions
bulwahn
parents:
32640
diff
changeset
|
813 |
( |
32662 | 814 |
type T = thm list; |
815 |
val empty = []; |
|
816 |
val extend = I; |
|
817 |
fun merge _ = Thm.merge_thms; |
|
818 |
); |
|
819 |
||
820 |
val get = Data.get o Context.Proof; |
|
821 |
||
822 |
val add_thm = Data.map o Thm.add_thm; |
|
823 |
val del_thm = Data.map o Thm.del_thm; |
|
824 |
||
825 |
val add_attribute = Thm.declaration_attribute add_thm; |
|
826 |
val del_attribute = Thm.declaration_attribute del_thm; |
|
827 |
||
828 |
val add_attrib = Attrib.internal (K add_attribute) |
|
829 |
||
830 |
val setup = |
|
831 |
Attrib.setup (Binding.name "pred_compile_preproc") (Attrib.add_del add_attribute del_attribute) |
|
832 |
("declaration of definition for preprocessing of the predicate compiler") #> |
|
833 |
PureThy.add_thms_dynamic (Binding.name "pred_compile_preproc", Data.get); |
|
834 |
||
835 |
end; |
|
836 |
||
28143 | 837 |
structure Code : CODE = struct open Code; end; |