1 (* Title: Pure/Isar/code.ML
3 Author: Florian Haftmann, TU Muenchen
5 Abstract executable content of theory. Management of data dependent on
11 val add_func: bool -> thm -> theory -> theory
12 val del_func: thm -> theory -> theory
13 val add_funcl: CodeUnit.const * thm list Susp.T -> theory -> theory
14 val add_func_attr: bool -> Attrib.src
15 val add_inline: thm -> theory -> theory
16 val del_inline: thm -> theory -> theory
17 val add_inline_proc: string * (theory -> cterm list -> thm list) -> theory -> theory
18 val del_inline_proc: string -> theory -> theory
19 val add_preproc: string * (theory -> thm list -> thm list) -> theory -> theory
20 val del_preproc: string -> theory -> theory
21 val add_post: thm -> theory -> theory
22 val del_post: thm -> theory -> theory
23 val add_datatype: string * ((string * sort) list * (string * typ list) list)
25 val add_datatype_consts: CodeUnit.const list -> theory -> theory
26 val add_datatype_consts_cmd: string list -> theory -> theory
28 val coregular_algebra: theory -> Sorts.algebra
29 val operational_algebra: theory -> (sort -> sort) * Sorts.algebra
30 val these_funcs: theory -> CodeUnit.const -> thm list
31 val get_datatype: theory -> string -> ((string * sort) list * (string * typ list) list)
32 val get_datatype_of_constr: theory -> CodeUnit.const -> string option
33 val default_typ: theory -> CodeUnit.const -> typ
35 val preprocess_conv: cterm -> thm
36 val postprocess_conv: cterm -> thm
38 val add_attribute: string * (Args.T list -> attribute * Args.T list) -> theory -> theory
40 val print_codesetup: theory -> unit
43 signature CODE_DATA_ARGS =
47 val merge: Pretty.pp -> T * T -> T
48 val purge: theory option -> CodeUnit.const list option -> T -> T
55 val change: theory -> (T -> T) -> T
56 val change_yield: theory -> (T -> 'a * T) -> 'a * T
59 signature PRIVATE_CODE =
62 val declare_data: Object.T -> (Pretty.pp -> Object.T * Object.T -> Object.T)
63 -> (theory option -> CodeUnit.const list option -> Object.T -> Object.T) -> serial
64 val get_data: serial * ('a -> Object.T) * (Object.T -> 'a)
66 val change_data: serial * ('a -> Object.T) * (Object.T -> 'a)
67 -> theory -> ('a -> 'a) -> 'a
68 val change_yield_data: serial * ('a -> Object.T) * (Object.T -> 'a)
69 -> theory -> ('a -> 'b * 'a) -> 'b * 'a
72 structure Code : PRIVATE_CODE =
77 structure Consttab = CodeUnit.Consttab;
80 (* certificate theorems *)
82 fun string_of_lthms r = case Susp.peek r
83 of SOME thms => (map string_of_thm o rev) thms
86 fun pretty_lthms ctxt r = case Susp.peek r
87 of SOME thms => map (ProofContext.pretty_thm ctxt) thms
88 | NONE => [Pretty.str "[...]"];
90 fun certificate thy f r =
92 of SOME thms => (Susp.value o f thy) thms
94 val thy_ref = Theory.check_thy thy;
95 in Susp.delay (fn () => (f (Theory.deref thy_ref) o Susp.force) r) end;
97 fun merge' _ ([], []) = (false, [])
98 | merge' _ ([], ys) = (true, ys)
99 | merge' eq (xs, ys) = fold_rev
100 (fn y => fn (t, xs) => (t orelse not (member eq xs y), insert eq y xs)) ys (false, xs);
102 fun merge_alist eq_key eq (xys as (xs, ys)) =
103 if eq_list (eq_pair eq_key eq) (xs, ys)
105 else (true, AList.merge eq_key eq xys);
107 val merge_thms = merge' Thm.eq_thm_prop;
109 fun merge_lthms (r1, r2) =
110 if Susp.same (r1, r2)
112 else case Susp.peek r1
113 of SOME [] => (true, r2)
114 | _ => case Susp.peek r2
115 of SOME [] => (true, r1)
116 | _ => (apsnd (Susp.delay o K)) (merge_thms (Susp.force r1, Susp.force r2));
119 (* pairs of (selected, deleted) defining equations *)
121 type sdthms = thm list Susp.T * thm list;
123 fun add_drop_redundant thm (sels, dels) =
125 val thy = Thm.theory_of_thm thm;
126 val args_of = snd o strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of;
127 val args = args_of thm;
128 fun matches [] _ = true
129 | matches (Var _ :: xs) [] = matches xs []
130 | matches (_ :: _) [] = false
131 | matches (x :: xs) (y :: ys) = Pattern.matches thy (x, y) andalso matches xs ys;
132 fun drop thm' = not (matches args (args_of thm'))
133 orelse (warning ("code generator: dropping redundant defining equation\n" ^ string_of_thm thm'); false);
134 val (keeps, drops) = List.partition drop sels;
135 in (thm :: keeps, dels |> remove Thm.eq_thm_prop thm |> fold (insert Thm.eq_thm_prop) drops) end;
137 fun add_thm thm (sels, dels) =
138 apfst Susp.value (add_drop_redundant thm (Susp.force sels, dels));
140 fun add_lthms lthms (sels, []) =
141 (Susp.delay (fn () => fold add_drop_redundant
142 (Susp.force lthms) (Susp.force sels, []) |> fst), [])
144 | add_lthms lthms (sels, dels) =
145 fold add_thm (Susp.force lthms) (sels, dels);
147 fun del_thm thm (sels, dels) =
148 (Susp.value (remove Thm.eq_thm_prop thm (Susp.force sels)), thm :: dels);
150 fun pretty_sdthms ctxt (sels, _) = pretty_lthms ctxt sels;
152 fun merge_sdthms ((sels1, dels1), (sels2, dels2)) =
154 val (dels_t, dels) = merge_thms (dels1, dels2);
157 val (_, sels) = merge_thms
158 (subtract Thm.eq_thm_prop dels2 (Susp.force sels1), Susp.force sels2);
159 val (_, dels) = merge_thms
160 (subtract Thm.eq_thm_prop (Susp.force sels2) dels1, dels2);
161 in (true, ((Susp.delay o K) sels, dels)) end
163 val (sels_t, sels) = merge_lthms (sels1, sels2);
164 in (sels_t, (sels, dels)) end
168 (* code attributes *)
170 structure CodeAttr = TheoryDataFun (
171 type T = (string * (Args.T list -> attribute * Args.T list)) list;
175 fun merge _ = AList.merge (op =) (K true);
178 fun add_attribute (attr as (name, _)) =
180 fun add_parser ("", parser) attrs = attrs @ [("", parser)]
181 | add_parser (name, parser) attrs = (name, Args.$$$ name |-- parser) :: attrs;
182 fun error "" = error ("Code attribute already declared")
183 | error name = error ("Code attribute " ^ name ^ " already declared")
184 in CodeAttr.map (fn attrs => if AList.defined (op =) attrs name
185 then error name else add_parser attr attrs)
190 val code_attr = Attrib.syntax (Scan.peek (fn context =>
191 List.foldr op || Scan.fail (map snd (CodeAttr.get (Context.theory_of context)))));
193 Context.add_setup (Attrib.add_attributes
194 [("code", code_attr, "declare theorems for code generation")])
199 (** exeuctable content **)
201 datatype thmproc = Preproc of {
203 inline_procs: (string * (serial * (theory -> cterm list -> thm list))) list,
204 preprocs: (string * (serial * (theory -> thm list -> thm list))) list,
208 fun mk_thmproc (((inlines, inline_procs), preprocs), posts) =
209 Preproc { inlines = inlines, inline_procs = inline_procs, preprocs = preprocs,
211 fun map_thmproc f (Preproc { inlines, inline_procs, preprocs, posts }) =
212 mk_thmproc (f (((inlines, inline_procs), preprocs), posts));
213 fun merge_thmproc (Preproc { inlines = inlines1, inline_procs = inline_procs1,
214 preprocs = preprocs1, posts = posts1 },
215 Preproc { inlines = inlines2, inline_procs = inline_procs2,
216 preprocs = preprocs2, posts= posts2 }) =
218 val (touched1, inlines) = merge_thms (inlines1, inlines2);
219 val (touched2, inline_procs) = merge_alist (op =) (eq_fst (op =)) (inline_procs1, inline_procs2);
220 val (touched3, preprocs) = merge_alist (op =) (eq_fst (op =)) (preprocs1, preprocs2);
221 val (_, posts) = merge_thms (posts1, posts2);
222 in (touched1 orelse touched2 orelse touched3,
223 mk_thmproc (((inlines, inline_procs), preprocs), posts)) end;
225 fun join_func_thms (tabs as (tab1, tab2)) =
227 val cs1 = Consttab.keys tab1;
228 val cs2 = Consttab.keys tab2;
229 val cs' = filter (member CodeUnit.eq_const cs2) cs1;
230 val cs'' = subtract (op =) cs' cs1 @ subtract (op =) cs' cs2;
231 val cs''' = ref [] : CodeUnit.const list ref;
232 fun merge c x = let val (touched, thms') = merge_sdthms x in
233 (if touched then cs''' := cons c (!cs''') else (); thms') end;
234 in (cs'' @ !cs''', Consttab.join merge tabs) end;
235 fun merge_funcs (thms1, thms2) =
237 val (consts, thms) = join_func_thms (thms1, thms2);
238 in (SOME consts, thms) end;
240 val eq_string = op = : string * string -> bool;
241 val eq_co = op = : (string * typ list) * (string * typ list) -> bool;
242 fun eq_dtyp ((vs1, cs1), (vs2, cs2)) =
243 gen_eq_set (eq_pair eq_string (gen_eq_set eq_string)) (vs1, vs2)
244 andalso gen_eq_set eq_co (cs1, cs2);
245 fun merge_dtyps (tabs as (tab1, tab2)) =
247 val tycos1 = Symtab.keys tab1;
248 val tycos2 = Symtab.keys tab2;
249 val tycos' = filter (member eq_string tycos2) tycos1;
250 val new_types = not (gen_eq_set (op =) (tycos1, tycos2));
251 val diff_types = not (gen_eq_set (eq_pair (op =) eq_dtyp)
252 (AList.make (the o Symtab.lookup tab1) tycos',
253 AList.make (the o Symtab.lookup tab2) tycos'));
254 fun join _ (cos as (_, cos2)) = if eq_dtyp cos
255 then raise Symtab.SAME else cos2;
256 in ((new_types, diff_types), Symtab.join join tabs) end;
258 datatype spec = Spec of {
259 funcs: sdthms Consttab.table,
260 dtyps: ((string * sort) list * (string * typ list) list) Symtab.table
263 fun mk_spec (funcs, dtyps) =
264 Spec { funcs = funcs, dtyps = dtyps };
265 fun map_spec f (Spec { funcs = funcs, dtyps = dtyps }) =
266 mk_spec (f (funcs, dtyps));
267 fun merge_spec (Spec { funcs = funcs1, dtyps = dtyps1 },
268 Spec { funcs = funcs2, dtyps = dtyps2 }) =
270 val (touched_cs, funcs) = merge_funcs (funcs1, funcs2);
271 val ((new_types, diff_types), dtyps) = merge_dtyps (dtyps1, dtyps2);
272 val touched = if new_types orelse diff_types then NONE else touched_cs;
273 in (touched, mk_spec (funcs, dtyps)) end;
275 datatype exec = Exec of {
280 fun mk_exec (thmproc, spec) =
281 Exec { thmproc = thmproc, spec = spec };
282 fun map_exec f (Exec { thmproc = thmproc, spec = spec }) =
283 mk_exec (f (thmproc, spec));
284 fun merge_exec (Exec { thmproc = thmproc1, spec = spec1 },
285 Exec { thmproc = thmproc2, spec = spec2 }) =
287 val (touched', thmproc) = merge_thmproc (thmproc1, thmproc2);
288 val (touched_cs, spec) = merge_spec (spec1, spec2);
289 val touched = if touched' then NONE else touched_cs;
290 in (touched, mk_exec (thmproc, spec)) end;
291 val empty_exec = mk_exec (mk_thmproc ((([], []), []), []),
292 mk_spec (Consttab.empty, Symtab.empty));
294 fun the_thmproc (Exec { thmproc = Preproc x, ...}) = x;
295 fun the_spec (Exec { spec = Spec x, ...}) = x;
296 val the_funcs = #funcs o the_spec;
297 val the_dtyps = #dtyps o the_spec;
298 val map_thmproc = map_exec o apfst o map_thmproc;
299 val map_funcs = map_exec o apsnd o map_spec o apfst;
300 val map_dtyps = map_exec o apsnd o map_spec o apsnd;
303 (* data slots dependent on executable content *)
305 (*private copy avoids potential conflict of table exceptions*)
306 structure Datatab = TableFun(type key = int val ord = int_ord);
312 merge: Pretty.pp -> Object.T * Object.T -> Object.T,
313 purge: theory option -> CodeUnit.const list option -> Object.T -> Object.T
316 val kinds = ref (Datatab.empty: kind Datatab.table);
317 val kind_keys = ref ([]: serial list);
319 fun invoke f k = case Datatab.lookup (! kinds) k
320 of SOME kind => f kind
321 | NONE => sys_error "Invalid code data identifier";
325 fun declare_data empty merge purge =
328 val kind = {empty = empty, merge = merge, purge = purge};
329 val _ = change kinds (Datatab.update (k, kind));
330 val _ = change kind_keys (cons k);
333 fun invoke_empty k = invoke (fn kind => #empty kind) k;
335 fun invoke_merge_all pp = Datatab.join
336 (invoke (fn kind => #merge kind pp));
338 fun invoke_purge_all thy_opt cs =
339 fold (fn k => Datatab.map_entry k
340 (invoke (fn kind => #purge kind thy_opt cs) k)) (! kind_keys);
349 type data = Object.T Datatab.table;
351 structure CodeData = TheoryDataFun
353 type T = exec * data ref;
354 val empty = (empty_exec, ref Datatab.empty : data ref);
355 fun copy (exec, data) = (exec, ref (! data));
357 fun merge pp ((exec1, data1), (exec2, data2)) =
359 val (touched, exec) = merge_exec (exec1, exec2);
360 val data1' = invoke_purge_all NONE touched (! data1);
361 val data2' = invoke_purge_all NONE touched (! data2);
362 val data = invoke_merge_all pp (data1', data2');
363 in (exec, ref data) end;
366 val _ = Context.add_setup CodeData.init;
368 fun ch r f = let val x = f (! r) in (r := x; x) end;
369 fun thy_data f thy = f ((snd o CodeData.get) thy);
371 fun get_ensure_init kind data_ref =
372 case Datatab.lookup (! data_ref) kind
374 | NONE => let val y = invoke_empty kind
375 in (change data_ref (Datatab.update (kind, y)); y) end;
379 (* access to executable content *)
381 val get_exec = fst o CodeData.get;
383 fun map_exec_purge touched f thy =
384 CodeData.map (fn (exec, data) =>
385 (f exec, ref (invoke_purge_all (SOME thy) touched (! data)))) thy;
388 (* access to data dependent on abstract executable content *)
390 fun get_data (kind, _, dest) = thy_data (get_ensure_init kind #> dest);
392 fun change_data (kind, mk, dest) =
394 fun chnge data_ref f =
396 val data = get_ensure_init kind data_ref;
397 val data' = f (dest data);
398 in (change data_ref (Datatab.update (kind, mk data')); data') end;
399 in thy_data chnge end;
401 fun change_yield_data (kind, mk, dest) =
403 fun chnge data_ref f =
405 val data = get_ensure_init kind data_ref;
406 val (x, data') = f (dest data);
407 in (x, (change data_ref (Datatab.update (kind, mk data')); data')) end;
408 in thy_data chnge end;
413 (* print executable content *)
415 fun print_codesetup thy =
417 val ctxt = ProofContext.init thy;
418 val exec = get_exec thy;
419 fun pretty_func (s, lthms) =
420 (Pretty.block o Pretty.fbreaks) (
421 Pretty.str s :: pretty_sdthms ctxt lthms
423 fun pretty_dtyp (s, []) =
425 | pretty_dtyp (s, cos) =
426 (Pretty.block o Pretty.breaks) (
429 :: separate (Pretty.str "|") (map (fn (c, []) => Pretty.str c
431 (Pretty.block o Pretty.breaks)
432 (Pretty.str c :: Pretty.str "of" :: map (Pretty.quote o Sign.pretty_typ thy) tys)) cos)
434 val inlines = (#inlines o the_thmproc) exec;
435 val inline_procs = (map fst o #inline_procs o the_thmproc) exec;
436 val preprocs = (map fst o #preprocs o the_thmproc) exec;
437 val funs = the_funcs exec
439 |> (map o apfst) (CodeUnit.string_of_const thy)
440 |> sort (string_ord o pairself fst);
441 val dtyps = the_dtyps exec
443 |> map (fn (dtco, (vs, cos)) => (Sign.string_of_typ thy (Type (dtco, map TFree vs)), cos))
444 |> sort (string_ord o pairself fst)
446 (Pretty.writeln o Pretty.chunks) [
448 Pretty.str "defining equations:"
450 :: (Pretty.fbreaks o map pretty_func) funs
453 Pretty.str "inlining theorems:"
455 :: (Pretty.fbreaks o map (ProofContext.pretty_thm ctxt)) inlines
458 Pretty.str "inlining procedures:"
460 :: (Pretty.fbreaks o map Pretty.str) inline_procs
463 Pretty.str "preprocessors:"
465 :: (Pretty.fbreaks o map Pretty.str) preprocs
468 Pretty.str "datatypes:"
470 :: (Pretty.fbreaks o map pretty_dtyp) dtyps
477 (** theorem transformation and certification **)
479 fun common_typ_funcs [] = []
480 | common_typ_funcs [thm] = [thm]
481 | common_typ_funcs (thms as thm :: _) =
483 val thy = Thm.theory_of_thm thm;
484 fun incr_thm thm max =
486 val thm' = incr_indexes max thm;
487 val max' = Thm.maxidx_of thm' + 1;
489 val (thms', maxidx) = fold_map incr_thm thms 0;
490 val ty1 :: tys = map (snd o CodeUnit.head_func) thms';
491 fun unify ty env = Sign.typ_unify thy (ty1, ty) env
492 handle Type.TUNIFY =>
493 error ("Type unificaton failed, while unifying defining equations\n"
494 ^ (cat_lines o map Display.string_of_thm) thms
496 ^ (cat_lines o map (CodeUnit.string_of_typ thy)) (ty1 :: tys));
497 val (env, _) = fold unify tys (Vartab.empty, maxidx)
498 val instT = Vartab.fold (fn (x_i, (sort, ty)) =>
499 cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env [];
500 in map (Thm.instantiate (instT, [])) thms' end;
502 fun certify_const thy const thms =
504 fun cert thm = if CodeUnit.eq_const (const, fst (CodeUnit.head_func thm))
505 then thm else error ("Wrong head of defining equation,\nexpected constant "
506 ^ CodeUnit.string_of_const thy const ^ "\n" ^ string_of_thm thm)
507 in map cert thms end;
511 (** operational sort algebra and class discipline **)
515 fun aggr_neutr f y [] = y
516 | aggr_neutr f y (x::xs) = aggr_neutr f (f y x) xs;
518 fun aggregate f [] = NONE
519 | aggregate f (x::xs) = SOME (aggr_neutr f x xs);
521 fun inter_sorts thy =
523 val algebra = Sign.classes_of thy;
524 val inters = curry (Sorts.inter_sort algebra);
525 in aggregate (map2 inters) end;
527 fun specific_constraints thy (class, tyco) =
529 val vs = Name.invents Name.context "" (Sign.arity_number thy tyco);
530 val clsops = (these o Option.map snd o try (AxClass.params_of_class thy)) class;
532 |> map (fn (clsop, _) => (clsop, SOME tyco))
533 |> map (Consttab.lookup ((the_funcs o get_exec) thy))
534 |> (map o Option.map) (Susp.force o fst)
536 |> map (Thm.transfer thy);
537 val sorts = map (map (snd o dest_TVar) o snd o dest_Type o the_single
538 o Sign.const_typargs thy o (fn ((c, _), ty) => (c, ty)) o CodeUnit.head_func) funcs;
541 fun weakest_constraints thy (class, tyco) =
543 val all_superclasses = class :: Graph.all_succs ((#classes o Sorts.rep_algebra o Sign.classes_of) thy) [class];
544 in case inter_sorts thy (maps (fn class => specific_constraints thy (class, tyco)) all_superclasses)
545 of SOME sorts => sorts
546 | NONE => Sign.arity_sorts thy tyco [class]
549 fun strongest_constraints thy (class, tyco) =
551 val algebra = Sign.classes_of thy;
552 val all_subclasses = class :: Graph.all_preds ((#classes o Sorts.rep_algebra) algebra) [class];
553 val inst_subclasses = filter (can (Sorts.mg_domain algebra tyco) o single) all_subclasses;
554 in case inter_sorts thy (maps (fn class => specific_constraints thy (class, tyco)) inst_subclasses)
555 of SOME sorts => sorts
557 (Sign.arity_number thy tyco) (Sign.certify_sort thy (Sign.all_classes thy))
560 fun gen_classop_typ constr thy class (c, tyco) =
562 val (var, cs) = try (AxClass.params_of_class thy) class |> the_default ("'a", [])
563 val ty = (the o AList.lookup (op =) cs) c;
564 val sort_args = Name.names (Name.declare var Name.context) "'a"
565 (constr thy (class, tyco));
566 val ty_inst = Type (tyco, map TFree sort_args);
567 in Logic.varifyT (map_type_tfree (K ty_inst) ty) end;
569 fun retrieve_algebra thy operational =
570 Sorts.subalgebra (Sign.pp thy) operational
571 (weakest_constraints thy)
572 (Sign.classes_of thy);
576 fun coregular_algebra thy = retrieve_algebra thy (K true) |> snd;
577 fun operational_algebra thy =
579 fun add_iff_operational class =
580 can (AxClass.get_definition thy) class ? cons class;
581 val operational_classes = fold add_iff_operational (Sign.all_classes thy) []
582 in retrieve_algebra thy (member (op =) operational_classes) end;
584 val classop_weakest_typ = gen_classop_typ weakest_constraints;
585 val classop_strongest_typ = gen_classop_typ strongest_constraints;
587 fun assert_func_typ thm =
589 val thy = Thm.theory_of_thm thm;
590 fun check_typ_classop class (const as (c, SOME tyco), thm) =
592 val (_, ty) = CodeUnit.head_func thm;
593 val ty_decl = classop_weakest_typ thy class (c, tyco);
594 val ty_strongest = classop_strongest_typ thy class (c, tyco);
597 val max = Thm.maxidx_of thm + 1;
598 val ty_decl' = Logic.incr_tvar max ty_decl;
599 val (_, ty') = CodeUnit.head_func thm;
600 val (env, _) = Sign.typ_unify thy (ty_decl', ty') (Vartab.empty, max);
601 val instT = Vartab.fold (fn (x_i, (sort, ty)) =>
602 cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env [];
603 in Thm.instantiate (instT, []) thm end;
604 in if Sign.typ_instance thy (ty_strongest, ty)
605 then if Sign.typ_instance thy (ty, ty_decl)
607 else (warning ("Constraining type\n" ^ CodeUnit.string_of_typ thy ty
608 ^ "\nof defining equation\n"
610 ^ "\nto permitted most general type\n"
611 ^ CodeUnit.string_of_typ thy ty_decl);
613 else CodeUnit.bad_thm ("Type\n" ^ CodeUnit.string_of_typ thy ty
614 ^ "\nof defining equation\n"
616 ^ "\nis incompatible with permitted least general type\n"
617 ^ CodeUnit.string_of_typ thy ty_strongest)
619 | check_typ_classop class ((c, NONE), thm) =
620 CodeUnit.bad_thm ("Illegal type for class operation " ^ quote c
621 ^ "\nin defining equation\n"
622 ^ string_of_thm thm);
623 fun check_typ_fun (const as (c, _), thm) =
625 val (_, ty) = CodeUnit.head_func thm;
626 val ty_decl = Sign.the_const_type thy c;
627 in if Sign.typ_equiv thy (Type.strip_sorts ty_decl, Type.strip_sorts ty)
629 else CodeUnit.bad_thm ("Type\n" ^ CodeUnit.string_of_typ thy ty
630 ^ "\nof defining equation\n"
632 ^ "\nis incompatible with declared function type\n"
633 ^ CodeUnit.string_of_typ thy ty_decl)
635 fun check_typ (const as (c, _), thm) =
636 case AxClass.class_of_param thy c
637 of SOME class => check_typ_classop class (const, thm)
638 | NONE => check_typ_fun (const, thm);
639 in check_typ (fst (CodeUnit.head_func thm), thm) end;
641 val mk_func = CodeUnit.error_thm
642 (assert_func_typ o CodeUnit.mk_func);
643 val mk_func_liberal = CodeUnit.warning_thm
644 (assert_func_typ o CodeUnit.mk_func);
650 (** interfaces and attributes **)
652 fun add_func true thm thy =
654 val func = mk_func thm;
655 val (const, _) = CodeUnit.head_func func;
656 in map_exec_purge (SOME [const]) (map_funcs
657 (Consttab.map_default
658 (const, (Susp.value [], [])) (add_thm func))) thy
660 | add_func false thm thy =
661 case mk_func_liberal thm
663 val (const, _) = CodeUnit.head_func func
664 in map_exec_purge (SOME [const]) (map_funcs
665 (Consttab.map_default
666 (const, (Susp.value [], [])) (add_thm func))) thy
670 fun delete_force msg key xs =
671 if AList.defined (op =) xs key then AList.delete (op =) key xs
672 else error ("No such " ^ msg ^ ": " ^ quote key);
674 fun del_func thm thy =
676 val func = mk_func thm;
677 val (const, _) = CodeUnit.head_func func;
678 in map_exec_purge (SOME [const]) (map_funcs
680 const (del_thm func))) thy
683 fun add_funcl (const, lthms) thy =
685 val lthms' = certificate thy (fn thy => certify_const thy const) lthms;
686 (*FIXME must check compatibility with sort algebra;
687 alas, naive checking results in non-termination!*)
689 map_exec_purge (SOME [const]) (map_funcs (Consttab.map_default (const, (Susp.value [], []))
690 (add_lthms lthms'))) thy
693 fun add_func_attr strict = Attrib.internal (fn _ => Thm.declaration_attribute
694 (fn thm => Context.mapping (add_func strict thm) I));
698 fun del_datatype tyco thy =
699 case Symtab.lookup ((the_dtyps o get_exec) thy) tyco
700 of SOME (vs, cos) => let
701 val consts = CodeUnit.consts_of_cos thy tyco vs cos;
702 in map_exec_purge (if null consts then NONE else SOME consts)
703 (map_dtyps (Symtab.delete tyco)) thy end
708 fun add_datatype (tyco, (vs_cos as (vs, cos))) thy =
710 val consts = CodeUnit.consts_of_cos thy tyco vs cos;
714 |> map_exec_purge (SOME consts) (map_dtyps (Symtab.update_new (tyco, vs_cos)))
717 fun add_datatype_consts consts thy =
718 add_datatype (CodeUnit.cos_of_consts thy consts) thy;
720 fun add_datatype_consts_cmd raw_cs thy =
721 add_datatype_consts (map (CodeUnit.read_const thy) raw_cs) thy
725 fun add_inline thm thy =
726 (map_exec_purge NONE o map_thmproc o apfst o apfst o apfst)
727 (insert Thm.eq_thm_prop (CodeUnit.error_thm CodeUnit.mk_rew thm)) thy;
728 (*fully applied in order to get right context for mk_rew!*)
730 fun del_inline thm thy =
731 (map_exec_purge NONE o map_thmproc o apfst o apfst o apfst)
732 (remove Thm.eq_thm_prop (CodeUnit.error_thm CodeUnit.mk_rew thm)) thy;
733 (*fully applied in order to get right context for mk_rew!*)
735 fun add_inline_proc (name, f) =
736 (map_exec_purge NONE o map_thmproc o apfst o apfst o apsnd)
737 (AList.update (op =) (name, (serial (), f)));
739 fun del_inline_proc name =
740 (map_exec_purge NONE o map_thmproc o apfst o apfst o apsnd)
741 (delete_force "inline procedure" name);
743 fun add_preproc (name, f) =
744 (map_exec_purge NONE o map_thmproc o apfst o apsnd)
745 (AList.update (op =) (name, (serial (), f)));
747 fun del_preproc name =
748 (map_exec_purge NONE o map_thmproc o apfst o apsnd)
749 (delete_force "preprocessor" name);
751 fun add_post thm thy =
752 (map_exec_purge NONE o map_thmproc o apsnd)
753 (insert Thm.eq_thm_prop (CodeUnit.error_thm CodeUnit.mk_rew thm)) thy;
754 (*fully applied in order to get right context for mk_rew!*)
756 fun del_post thm thy =
757 (map_exec_purge NONE o map_thmproc o apsnd)
758 (remove Thm.eq_thm_prop (CodeUnit.error_thm CodeUnit.mk_rew thm)) thy;
759 (*fully applied in order to get right context for mk_rew!*)
761 val _ = Context.add_setup
763 fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
764 fun add_simple_attribute (name, f) =
765 add_attribute (name, Scan.succeed (mk_attribute f));
766 fun add_del_attribute (name, (add, del)) =
767 add_attribute (name, Args.del |-- Scan.succeed (mk_attribute del)
768 || Scan.succeed (mk_attribute add))
770 add_del_attribute ("func", (add_func true, del_func))
771 #> add_del_attribute ("inline", (add_inline, del_inline))
772 #> add_del_attribute ("post", (add_post, del_post))
776 (** post- and preprocessing **)
780 fun gen_apply_inline_proc prep post thy f x =
783 val rews = map CodeUnit.assert_rew (f thy cts);
786 val apply_inline_proc = gen_apply_inline_proc (maps
787 ((fn [args, rhs] => rhs :: (snd o Drule.strip_comb) args) o snd o Drule.strip_comb o Thm.cprop_of))
788 (fn rews => map (CodeUnit.rewrite_func rews));
789 val apply_inline_proc_cterm = gen_apply_inline_proc single
790 (MetaSimplifier.rewrite false);
792 fun apply_preproc thy f [] = []
793 | apply_preproc thy f (thms as (thm :: _)) =
795 val (const, _) = CodeUnit.head_func thm;
796 val thms' = f thy thms;
797 in certify_const thy const thms' end;
799 fun rhs_conv conv thm =
801 val thm' = (conv o Thm.rhs_of) thm;
802 in Thm.transitive thm thm' end
806 fun preprocess thy thms =
808 |> fold (fn (_, (_, f)) => apply_preproc thy f) ((#preprocs o the_thmproc o get_exec) thy)
809 |> map (CodeUnit.rewrite_func ((#inlines o the_thmproc o get_exec) thy))
810 |> fold (fn (_, (_, f)) => apply_inline_proc thy f) ((#inline_procs o the_thmproc o get_exec) thy)
811 (*FIXME - must check: rewrite rule, defining equation, proper constant |> map (snd o check_func false thy) *)
814 fun preprocess_conv ct =
816 val thy = Thm.theory_of_cterm ct;
819 |> MetaSimplifier.rewrite false ((#inlines o the_thmproc o get_exec) thy)
820 |> fold (fn (_, (_, f)) => rhs_conv (apply_inline_proc_cterm thy f))
821 ((#inline_procs o the_thmproc o get_exec) thy)
824 fun postprocess_conv ct =
826 val thy = Thm.theory_of_cterm ct;
829 |> MetaSimplifier.rewrite false ((#posts o the_thmproc o get_exec) thy)
834 fun get_datatype thy tyco =
835 case Symtab.lookup ((the_dtyps o get_exec) thy) tyco
837 | NONE => Sign.arity_number thy tyco
838 |> Name.invents Name.context "'a"
842 fun get_datatype_of_constr thy const =
843 case CodeUnit.co_of_const' thy const
844 of SOME (tyco, (_, co)) => if member eq_co
845 (Symtab.lookup (((the_dtyps o get_exec) thy)) tyco
847 |> the_default []) co then SOME tyco else NONE
850 fun get_constr_typ thy const =
851 case get_datatype_of_constr thy const
853 val (vs, cos) = get_datatype thy tyco;
854 val (_, (_, (co, tys))) = CodeUnit.co_of_const thy const
855 in (tys ---> Type (tyco, map TFree vs))
856 |> map_atyps (fn TFree (v, _) => TFree (v, AList.lookup (op =) vs v |> the))
861 fun default_typ_proto thy (const as (c, SOME tyco)) = classop_weakest_typ thy
862 ((the o AxClass.class_of_param thy) c) (c, tyco) |> SOME
863 | default_typ_proto thy (const as (c, NONE)) = case AxClass.class_of_param thy c
864 of SOME class => SOME (Term.map_type_tvar
865 (K (TVar (("'a", 0), [class]))) (Sign.the_const_type thy c))
866 | NONE => get_constr_typ thy const;
870 fun get_funcs thy const =
871 Consttab.lookup ((the_funcs o get_exec) thy) const
872 |> Option.map (Susp.force o fst)
874 |> map (Thm.transfer thy);
878 fun these_funcs thy const =
880 fun drop_refl thy = filter_out (is_equal o Term.fast_term_ord o Logic.dest_equals
881 o ObjectLogic.drop_judgment thy o Thm.plain_prop_of);
888 fun default_typ thy (const as (c, _)) = case default_typ_proto thy const
890 | NONE => (case get_funcs thy const
891 of thm :: _ => snd (CodeUnit.head_func thm)
892 | [] => Sign.the_const_type thy c);
899 (** type-safe interfaces for data depedent on executable content **)
901 functor CodeDataFun(Data: CODE_DATA_ARGS): CODE_DATA =
906 fun dest (Data x) = x
908 val kind = Code.declare_data (Data Data.empty)
909 (fn pp => fn (Data x1, Data x2) => Data (Data.merge pp (x1, x2)))
910 (fn thy_opt => fn cs => fn Data x => Data (Data.purge thy_opt cs x));
912 val data_op = (kind, Data, dest);
914 val get = Code.get_data data_op;
915 val change = Code.change_data data_op;
916 fun change_yield thy = Code.change_yield_data data_op thy;
920 structure Code : CODE =