1 (* Title: Pure/Isar/code.ML
3 Author: Florian Haftmann, TU Muenchen
5 Abstract executable content of theory. Management of data dependent on
6 executable content. Cache assumes non-concurrent processing of a single theory.
11 val add_eqn: thm -> theory -> theory
12 val add_nonlinear_eqn: thm -> theory -> theory
13 val add_liberal_eqn: thm -> theory -> theory
14 val add_default_eqn: thm -> theory -> theory
15 val add_default_eqn_attr: Attrib.src
16 val del_eqn: thm -> theory -> theory
17 val del_eqns: string -> theory -> theory
18 val add_eqnl: string * (thm * bool) list Susp.T -> theory -> theory
19 val map_pre: (MetaSimplifier.simpset -> MetaSimplifier.simpset) -> theory -> theory
20 val map_post: (MetaSimplifier.simpset -> MetaSimplifier.simpset) -> theory -> theory
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
25 val add_functrans: string * (theory -> thm list -> thm list option) -> theory -> theory
26 val del_functrans: string -> theory -> theory
27 val add_datatype: (string * typ) list -> theory -> theory
28 val add_datatype_cmd: string list -> theory -> theory
29 val type_interpretation:
30 (string * ((string * sort) list * (string * typ list) list)
31 -> theory -> theory) -> theory -> theory
32 val add_case: thm -> theory -> theory
33 val add_undefined: string -> theory -> theory
34 val purge_data: theory -> theory
36 val coregular_algebra: theory -> Sorts.algebra
37 val operational_algebra: theory -> (sort -> sort) * Sorts.algebra
38 val these_eqns: theory -> string -> (thm * bool) list
39 val get_datatype: theory -> string -> ((string * sort) list * (string * typ list) list)
40 val get_datatype_of_constr: theory -> string -> string option
41 val get_case_data: theory -> string -> (int * string list) option
42 val is_undefined: theory -> string -> bool
43 val default_typ: theory -> string -> (string * sort) list * typ
45 val preprocess_conv: cterm -> thm
46 val preprocess_term: theory -> term -> term
47 val postprocess_conv: cterm -> thm
48 val postprocess_term: theory -> term -> term
50 val add_attribute: string * (Args.T list -> attribute * Args.T list) -> theory -> theory
52 val print_codesetup: theory -> unit
55 signature CODE_DATA_ARGS =
59 val purge: theory -> string list -> T -> T
66 val change: theory -> (T -> T) -> T
67 val change_yield: theory -> (T -> 'a * T) -> 'a * T
70 signature PRIVATE_CODE =
73 val declare_data: Object.T -> (theory -> string list -> Object.T -> Object.T)
75 val get_data: serial * ('a -> Object.T) * (Object.T -> '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
83 structure Code : PRIVATE_CODE =
86 (** code attributes **)
88 structure CodeAttr = TheoryDataFun (
89 type T = (string * (Args.T list -> attribute * Args.T list)) list;
93 fun merge _ = AList.merge (op = : string * string -> bool) (K true);
96 fun add_attribute (attr as (name, _)) =
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)
108 val code_attr = Attrib.syntax (Scan.peek (fn context =>
109 List.foldr op || Scan.fail (map snd (CodeAttr.get (Context.theory_of context)))));
111 Context.>> (Context.map_theory
112 (Attrib.add_attributes
113 [("code", code_attr, "declare theorems for code generation")]))
117 (** logical and syntactical specification of executable code **)
119 (* defining equations with linear flag, default flag and lazy theorems *)
121 fun pretty_lthms ctxt r = case Susp.peek r
122 of SOME thms => map (ProofContext.pretty_thm ctxt o fst) thms
123 | NONE => [Pretty.str "[...]"];
125 fun certificate thy f r =
127 of SOME thms => (Susp.value o burrow_fst (f thy)) thms
129 val thy_ref = Theory.check_thy thy;
130 in Susp.delay (fn () => (burrow_fst (f (Theory.deref thy_ref)) o Susp.force) r) end;
132 fun add_drop_redundant (thm, linear) thms =
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;
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', linear') = if (linear orelse not linear')
140 andalso matches_args (args_of thm') then
141 (warning ("Code generator: dropping redundant defining equation\n" ^ Display.string_of_thm thm'); true)
143 in (thm, linear) :: filter_out drop thms end;
145 fun add_thm _ thm (false, thms) = (false, Susp.map_force (add_drop_redundant thm) thms)
146 | add_thm true thm (true, thms) = (true, Susp.map_force (fn thms => thms @ [thm]) thms)
147 | add_thm false thm (true, thms) = (false, Susp.value [thm]);
149 fun add_lthms lthms _ = (false, lthms);
151 fun del_thm thm = (apsnd o Susp.map_force) (remove (eq_fst Thm.eq_thm_prop) (thm, true));
153 fun merge_defthms ((true, _), defthms2) = defthms2
154 | merge_defthms (defthms1 as (false, _), (true, _)) = defthms1
155 | merge_defthms ((false, _), defthms2 as (false, _)) = defthms2;
158 (* syntactic datatypes *)
160 val eq_string = op = : string * string -> bool;
162 fun eq_dtyp ((vs1, cs1), (vs2, cs2)) =
163 gen_eq_set (eq_pair eq_string (gen_eq_set eq_string)) (vs1, vs2)
164 andalso gen_eq_set (eq_fst eq_string) (cs1, cs2);
166 fun merge_dtyps (tabs as (tab1, tab2)) =
168 fun join _ (cos as (_, cos2)) = if eq_dtyp cos then raise Symtab.SAME else cos2;
169 in Symtab.join join tabs end;
172 (* specification data *)
174 datatype spec = Spec of {
175 eqns: (bool * (thm * bool) list Susp.T) Symtab.table,
176 dtyps: ((string * sort) list * (string * typ list) list) Symtab.table,
177 cases: (int * string list) Symtab.table * unit Symtab.table
180 fun mk_spec (eqns, (dtyps, cases)) =
181 Spec { eqns = eqns, dtyps = dtyps, cases = cases };
182 fun map_spec f (Spec { eqns = eqns, dtyps = dtyps, cases = cases }) =
183 mk_spec (f (eqns, (dtyps, cases)));
184 fun merge_spec (Spec { eqns = eqns1, dtyps = dtyps1, cases = (cases1, undefs1) },
185 Spec { eqns = eqns2, dtyps = dtyps2, cases = (cases2, undefs2) }) =
187 val eqns = Symtab.join (K merge_defthms) (eqns1, eqns2);
188 val dtyps = merge_dtyps (dtyps1, dtyps2);
189 val cases = (Symtab.merge (K true) (cases1, cases2),
190 Symtab.merge (K true) (undefs1, undefs2));
191 in mk_spec (eqns, (dtyps, cases)) end;
194 (* pre- and postprocessor *)
196 datatype thmproc = Thmproc of {
197 pre: MetaSimplifier.simpset,
198 post: MetaSimplifier.simpset,
199 functrans: (string * (serial * (theory -> thm list -> thm list option))) list
202 fun mk_thmproc ((pre, post), functrans) =
203 Thmproc { pre = pre, post = post, functrans = functrans };
204 fun map_thmproc f (Thmproc { pre, post, functrans }) =
205 mk_thmproc (f ((pre, post), functrans));
206 fun merge_thmproc (Thmproc { pre = pre1, post = post1, functrans = functrans1 },
207 Thmproc { pre = pre2, post = post2, functrans = functrans2 }) =
209 val pre = MetaSimplifier.merge_ss (pre1, pre2);
210 val post = MetaSimplifier.merge_ss (post1, post2);
211 val functrans = AList.merge (op =) (eq_fst (op =)) (functrans1, functrans2);
212 in mk_thmproc ((pre, post), functrans) end;
214 datatype exec = Exec of {
220 (* code setup data *)
222 fun mk_exec (thmproc, spec) =
223 Exec { thmproc = thmproc, spec = spec };
224 fun map_exec f (Exec { thmproc = thmproc, spec = spec }) =
225 mk_exec (f (thmproc, spec));
226 fun merge_exec (Exec { thmproc = thmproc1, spec = spec1 },
227 Exec { thmproc = thmproc2, spec = spec2 }) =
229 val thmproc = merge_thmproc (thmproc1, thmproc2);
230 val spec = merge_spec (spec1, spec2);
231 in mk_exec (thmproc, spec) end;
232 val empty_exec = mk_exec (mk_thmproc ((MetaSimplifier.empty_ss, MetaSimplifier.empty_ss), []),
233 mk_spec (Symtab.empty, (Symtab.empty, (Symtab.empty, Symtab.empty))));
235 fun the_thmproc (Exec { thmproc = Thmproc x, ...}) = x;
236 fun the_spec (Exec { spec = Spec x, ...}) = x;
237 val the_eqns = #eqns o the_spec;
238 val the_dtyps = #dtyps o the_spec;
239 val the_cases = #cases o the_spec;
240 val map_thmproc = map_exec o apfst o map_thmproc;
241 val map_eqns = map_exec o apsnd o map_spec o apfst;
242 val map_dtyps = map_exec o apsnd o map_spec o apsnd o apfst;
243 val map_cases = map_exec o apsnd o map_spec o apsnd o apsnd;
246 (* data slots dependent on executable content *)
248 (*private copy avoids potential conflict of table exceptions*)
249 structure Datatab = TableFun(type key = int val ord = int_ord);
255 purge: theory -> string list -> Object.T -> Object.T
258 val kinds = ref (Datatab.empty: kind Datatab.table);
259 val kind_keys = ref ([]: serial list);
261 fun invoke f k = case Datatab.lookup (! kinds) k
262 of SOME kind => f kind
263 | NONE => sys_error "Invalid code data identifier";
267 fun declare_data empty purge =
270 val kind = {empty = empty, purge = purge};
271 val _ = change kinds (Datatab.update (k, kind));
272 val _ = change kind_keys (cons k);
275 fun invoke_init k = invoke (fn kind => #empty kind) k;
277 fun invoke_purge_all thy cs =
278 fold (fn k => Datatab.map_entry k
279 (invoke (fn kind => #purge kind thy cs) k)) (! kind_keys);
288 type data = Object.T Datatab.table;
289 val empty_data = Datatab.empty : data;
291 structure CodeData = TheoryDataFun
293 type T = exec * data ref;
294 val empty = (empty_exec, ref empty_data);
295 fun copy (exec, data) = (exec, ref (! data));
297 fun merge pp ((exec1, data1), (exec2, data2)) =
298 (merge_exec (exec1, exec2), ref empty_data);
301 val _ = Context.>> (Context.map_theory CodeData.init);
303 fun thy_data f thy = f ((snd o CodeData.get) thy);
305 fun get_ensure_init kind data_ref =
306 case Datatab.lookup (! data_ref) kind
308 | NONE => let val y = invoke_init kind
309 in (change data_ref (Datatab.update (kind, y)); y) end;
313 (* access to executable content *)
315 val the_exec = fst o CodeData.get;
317 fun complete_class_params thy cs =
318 fold (fn c => case AxClass.inst_of_param thy c
319 of NONE => insert (op =) c
320 | SOME (c', _) => insert (op =) c' #> insert (op =) c) cs [];
322 fun map_exec_purge touched f thy =
323 CodeData.map (fn (exec, data) => (f exec, ref (case touched
324 of SOME cs => invoke_purge_all thy (complete_class_params thy cs) (! data)
325 | NONE => empty_data))) thy;
327 val purge_data = (CodeData.map o apsnd) (K (ref empty_data));
330 (* access to data dependent on abstract executable content *)
332 fun get_data (kind, _, dest) = thy_data (get_ensure_init kind #> dest);
334 fun change_data (kind, mk, dest) =
336 fun chnge data_ref f =
338 val data = get_ensure_init kind data_ref;
339 val data' = f (dest data);
340 in (change data_ref (Datatab.update (kind, mk data')); data') end;
341 in thy_data chnge end;
343 fun change_yield_data (kind, mk, dest) =
345 fun chnge data_ref f =
347 val data = get_ensure_init kind data_ref;
348 val (x, data') = f (dest data);
349 in (x, (change data_ref (Datatab.update (kind, mk data')); data')) end;
350 in thy_data chnge end;
355 (* print executable content *)
357 fun print_codesetup thy =
359 val ctxt = ProofContext.init thy;
360 val exec = the_exec thy;
361 fun pretty_eqn (s, (_, lthms)) =
362 (Pretty.block o Pretty.fbreaks) (
363 Pretty.str s :: pretty_lthms ctxt lthms
365 fun pretty_dtyp (s, []) =
367 | pretty_dtyp (s, cos) =
368 (Pretty.block o Pretty.breaks) (
371 :: separate (Pretty.str "|") (map (fn (c, []) => Pretty.str c
373 (Pretty.block o Pretty.breaks)
374 (Pretty.str (Code_Unit.string_of_const thy c)
376 :: map (Pretty.quote o Syntax.pretty_typ_global thy) tys)) cos)
378 val pre = (#pre o the_thmproc) exec;
379 val post = (#post o the_thmproc) exec;
380 val functrans = (map fst o #functrans o the_thmproc) exec;
381 val eqns = the_eqns exec
383 |> (map o apfst) (Code_Unit.string_of_const thy)
384 |> sort (string_ord o pairself fst);
385 val dtyps = the_dtyps exec
387 |> map (fn (dtco, (vs, cos)) =>
388 (Syntax.string_of_typ_global thy (Type (dtco, map TFree vs)), cos))
389 |> sort (string_ord o pairself fst)
391 (Pretty.writeln o Pretty.chunks) [
393 Pretty.str "defining equations:"
395 :: (Pretty.fbreaks o map pretty_eqn) eqns
398 Pretty.str "preprocessing simpset:",
400 MetaSimplifier.pretty_ss pre
403 Pretty.str "postprocessing simpset:",
405 MetaSimplifier.pretty_ss post
408 Pretty.str "function transformers:"
410 :: (Pretty.fbreaks o map Pretty.str) functrans
413 Pretty.str "datatypes:"
415 :: (Pretty.fbreaks o map pretty_dtyp) dtyps
422 (** theorem transformation and certification **)
424 fun const_of thy = dest_Const o fst o strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of;
426 fun const_of_eqn thy = AxClass.unoverload_const thy o const_of thy;
428 fun common_typ_eqns [] = []
429 | common_typ_eqns [thm] = [thm]
430 | common_typ_eqns (thms as thm :: _) = (*FIXME is too general*)
432 val thy = Thm.theory_of_thm thm;
433 fun incr_thm thm max =
435 val thm' = incr_indexes max thm;
436 val max' = Thm.maxidx_of thm' + 1;
438 val (thms', maxidx) = fold_map incr_thm thms 0;
439 val ty1 :: tys = map (snd o const_of thy) thms';
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
445 ^ (cat_lines o map (Code_Unit.string_of_typ thy)) (ty1 :: tys));
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;
451 fun certify_const thy const thms =
453 fun cert thm = if const = const_of_eqn thy thm
454 then thm else error ("Wrong head of defining equation,\nexpected constant "
455 ^ Code_Unit.string_of_const thy const ^ "\n" ^ Display.string_of_thm thm)
456 in map cert thms end;
460 (** operational sort algebra and class discipline **)
464 fun aggr_neutr f y [] = y
465 | aggr_neutr f y (x::xs) = aggr_neutr f (f y x) xs;
467 fun aggregate f [] = NONE
468 | aggregate f (x::xs) = SOME (aggr_neutr f x xs);
470 fun inter_sorts algebra =
471 aggregate (map2 (curry (Sorts.inter_sort algebra)));
473 fun specific_constraints thy (class, tyco) =
475 val vs = Name.invents Name.context "" (Sign.arity_number thy tyco);
476 val classparams = (map fst o these o try (#params o AxClass.get_info thy)) class;
477 val eqns = classparams
478 |> map_filter (fn c => try (AxClass.param_of_inst thy) (c, tyco))
479 |> map (Symtab.lookup ((the_eqns o the_exec) thy))
480 |> (map o Option.map) (map fst o Susp.force o snd)
482 |> map (Thm.transfer thy);
483 fun sorts_of [Type (_, tys)] = map (snd o dest_TVar) tys
484 | sorts_of tys = map (snd o dest_TVar) tys;
485 val sorts = map (sorts_of o Sign.const_typargs thy o const_of thy) eqns;
488 fun weakest_constraints thy algebra (class, tyco) =
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)
492 of SOME sorts => sorts
493 | NONE => Sorts.mg_domain algebra tyco [class]
496 fun strongest_constraints thy algebra (class, tyco) =
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;
500 in case inter_sorts algebra (maps (fn class => specific_constraints thy (class, tyco)) inst_subclasses)
501 of SOME sorts => sorts
503 (Sign.arity_number thy tyco) (Sorts.minimize_sort algebra (Sorts.all_classes algebra))
506 fun get_algebra thy (class, tyco) =
508 val base_algebra = Sign.classes_of thy;
509 in if can (Sorts.mg_domain base_algebra tyco) [class]
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) [])
518 |> Sorts.add_arities (Syntax.pp_global thy) (tyco, [(class, sorts)])
522 fun gen_classparam_typ constr thy class (c, tyco) =
524 val algebra = get_algebra thy (class, tyco);
525 val cs = these (try (#params o AxClass.get_info thy) class);
526 val SOME ty = AList.lookup (op =) cs c;
527 val sort_args = Name.names (Name.declare Name.aT Name.context) Name.aT
528 (constr thy algebra (class, tyco));
529 val ty_inst = Type (tyco, map TFree sort_args);
530 in Logic.varifyT (map_type_tfree (K ty_inst) ty) end;
532 fun retrieve_algebra thy operational =
533 Sorts.subalgebra (Syntax.pp_global thy) operational
534 (weakest_constraints thy (Sign.classes_of thy))
535 (Sign.classes_of thy);
539 fun coregular_algebra thy = retrieve_algebra thy (K true) |> snd;
540 fun operational_algebra thy =
542 fun add_iff_operational class =
543 can (AxClass.get_info thy) class ? cons class;
544 val operational_classes = fold add_iff_operational (Sign.all_classes thy) []
545 in retrieve_algebra thy (member (op =) operational_classes) end;
547 val classparam_weakest_typ = gen_classparam_typ weakest_constraints;
548 val classparam_strongest_typ = gen_classparam_typ strongest_constraints;
550 fun assert_eqn_linear (eqn as (thm, linear)) =
551 if linear then eqn else Code_Unit.bad_thm
552 ("Duplicate variables on left hand side of defining equation:\n"
553 ^ Display.string_of_thm thm);
555 fun assert_eqn_typ (thm, linear) =
557 val thy = Thm.theory_of_thm thm;
558 fun check_typ_classparam tyco (c, thm) =
560 val SOME class = AxClass.class_of_param thy c;
561 val (_, ty) = const_of thy thm;
562 val ty_decl = classparam_weakest_typ thy class (c, tyco);
563 val ty_strongest = classparam_strongest_typ thy class (c, tyco);
566 val max = Thm.maxidx_of thm + 1;
567 val ty_decl' = Logic.incr_tvar max ty_decl;
568 val (_, ty') = const_of thy thm;
569 val (env, _) = Sign.typ_unify thy (ty_decl', ty') (Vartab.empty, max);
570 val instT = Vartab.fold (fn (x_i, (sort, ty)) =>
571 cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env [];
572 in Thm.instantiate (instT, []) thm end;
573 in if Sign.typ_instance thy (ty_strongest, ty)
574 then if Sign.typ_instance thy (ty, ty_decl)
576 else (warning ("Constraining type\n" ^ Code_Unit.string_of_typ thy ty
577 ^ "\nof defining equation\n"
578 ^ Display.string_of_thm thm
579 ^ "\nto permitted most general type\n"
580 ^ Code_Unit.string_of_typ thy ty_decl);
582 else Code_Unit.bad_thm ("Type\n" ^ Code_Unit.string_of_typ thy ty
583 ^ "\nof defining equation\n"
584 ^ Display.string_of_thm thm
585 ^ "\nis incompatible with permitted least general type\n"
586 ^ Code_Unit.string_of_typ thy ty_strongest)
588 fun check_typ_fun (c, thm) =
590 val (_, ty) = const_of thy thm;
591 val ty_decl = Sign.the_const_type thy c;
592 in if Sign.typ_equiv thy (Type.strip_sorts ty_decl, Type.strip_sorts ty)
594 else Code_Unit.bad_thm ("Type\n" ^ Code_Unit.string_of_typ thy ty
595 ^ "\nof defining equation\n"
596 ^ Display.string_of_thm thm
597 ^ "\nis incompatible with declared function type\n"
598 ^ Code_Unit.string_of_typ thy ty_decl)
600 fun check_typ (c, thm) =
601 case AxClass.inst_of_param thy c
602 of SOME (c, tyco) => check_typ_classparam tyco (c, thm)
603 | NONE => check_typ_fun (c, thm);
604 val c = const_of_eqn thy thm;
605 val thm' = check_typ (c, thm);
606 in (thm', linear) end;
608 fun mk_eqn linear = Code_Unit.error_thm
609 (assert_eqn_typ o (if linear then assert_eqn_linear else I) o Code_Unit.mk_eqn);
610 val mk_liberal_eqn = Code_Unit.warning_thm
611 (assert_eqn_typ o assert_eqn_linear o Code_Unit.mk_eqn);
612 val mk_syntactic_eqn = Code_Unit.warning_thm
613 (assert_eqn_typ o Code_Unit.mk_eqn);
614 val mk_default_eqn = Code_Unit.try_thm
615 (assert_eqn_typ o assert_eqn_linear o Code_Unit.mk_eqn);
620 (** interfaces and attributes **)
622 fun delete_force msg key xs =
623 if AList.defined (op =) xs key then AList.delete (op =) key xs
624 else error ("No such " ^ msg ^ ": " ^ quote key);
626 fun get_datatype thy tyco =
627 case Symtab.lookup ((the_dtyps o the_exec) thy) tyco
629 | NONE => Sign.arity_number thy tyco
630 |> Name.invents Name.context Name.aT
634 fun get_datatype_of_constr thy c =
635 case (snd o strip_type o Sign.the_const_type thy) c
636 of Type (tyco, _) => if member (op =)
637 ((the_default [] o Option.map (map fst o snd) o Symtab.lookup ((the_dtyps o the_exec) thy)) tyco) c
638 then SOME tyco else NONE
641 fun get_constr_typ thy c =
642 case get_datatype_of_constr thy c
644 val (vs, cos) = get_datatype thy tyco;
645 val SOME tys = AList.lookup (op =) cos c;
646 val ty = tys ---> Type (tyco, map TFree vs);
647 in SOME (Logic.varifyT ty) end
650 val get_case_data = Symtab.lookup o fst o the_cases o the_exec;
652 val is_undefined = Symtab.defined o snd o the_cases o the_exec;
654 fun gen_add_eqn linear strict default thm thy =
655 case (if strict then SOME o mk_eqn linear else mk_liberal_eqn) thm
658 val c = const_of_eqn thy thm;
659 val _ = if strict andalso (is_some o AxClass.class_of_param thy) c
660 then error ("Rejected polymorphic equation for overloaded constant:\n"
661 ^ Display.string_of_thm thm)
663 val _ = if strict andalso (is_some o get_datatype_of_constr thy) c
664 then error ("Rejected equation for datatype constructor:\n"
665 ^ Display.string_of_thm thm)
668 (map_exec_purge (SOME [c]) o map_eqns) (Symtab.map_default
669 (c, (true, Susp.value [])) (add_thm default (thm, linear))) thy
673 val add_eqn = gen_add_eqn true true false;
674 val add_liberal_eqn = gen_add_eqn true false false;
675 val add_default_eqn = gen_add_eqn true false true;
676 val add_nonlinear_eqn = gen_add_eqn false true false;
678 fun del_eqn thm thy = case mk_syntactic_eqn thm
679 of SOME (thm, _) => let
680 val c = const_of_eqn thy thm;
681 in map_exec_purge (SOME [c]) (map_eqns
682 (Symtab.map_entry c (del_thm thm))) thy
686 fun del_eqns c = map_exec_purge (SOME [c])
687 (map_eqns (Symtab.map_entry c (K (false, Susp.value []))));
689 fun add_eqnl (c, lthms) thy =
691 val lthms' = certificate thy (fn thy => certify_const thy c) lthms;
692 (*FIXME must check compatibility with sort algebra;
693 alas, naive checking results in non-termination!*)
695 map_exec_purge (SOME [c])
696 (map_eqns (Symtab.map_default (c, (true, Susp.value []))
697 (add_lthms lthms'))) thy
700 val add_default_eqn_attr = Attrib.internal (fn _ => Thm.declaration_attribute
701 (fn thm => Context.mapping (add_default_eqn thm) I));
703 structure TypeInterpretation = InterpretationFun(type T = string * serial val eq = eq_snd (op =) : T * T -> bool);
705 fun add_datatype raw_cs thy =
707 val cs = map (fn c_ty as (_, ty) => (AxClass.unoverload_const thy c_ty, ty)) raw_cs;
708 val (tyco, vs_cos) = Code_Unit.constrset_of_consts thy cs;
709 val cs' = map fst (snd vs_cos);
710 val purge_cs = case Symtab.lookup ((the_dtyps o the_exec) thy) tyco
711 of SOME (vs, cos) => if null cos then NONE else SOME (cs' @ map fst cos)
715 |> map_exec_purge purge_cs (map_dtyps (Symtab.update (tyco, vs_cos))
716 #> map_eqns (fold (Symtab.delete_safe o fst) cs))
717 |> TypeInterpretation.data (tyco, serial ())
720 fun type_interpretation f = TypeInterpretation.interpretation
721 (fn (tyco, _) => fn thy => f (tyco, get_datatype thy tyco) thy);
723 fun add_datatype_cmd raw_cs thy =
725 val cs = map (Code_Unit.read_bare_const thy) raw_cs;
726 in add_datatype cs thy end;
728 fun add_case thm thy =
730 val entry as (c, _) = Code_Unit.case_cert thm;
732 (map_exec_purge (SOME [c]) o map_cases o apfst) (Symtab.update entry) thy
735 fun add_undefined c thy =
736 (map_exec_purge (SOME [c]) o map_cases o apsnd) (Symtab.update (c, ())) thy;
738 val map_pre = map_exec_purge NONE o map_thmproc o apfst o apfst;
739 val map_post = map_exec_purge NONE o map_thmproc o apfst o apsnd;
741 fun add_inline thm thy = (map_pre o MetaSimplifier.add_simp)
742 (Code_Unit.error_thm Code_Unit.mk_rew thm) thy;
743 (*fully applied in order to get right context for mk_rew!*)
745 fun del_inline thm thy = (map_pre o MetaSimplifier.del_simp)
746 (Code_Unit.error_thm Code_Unit.mk_rew thm) thy;
747 (*fully applied in order to get right context for mk_rew!*)
749 fun add_post thm thy = (map_post o MetaSimplifier.add_simp)
750 (Code_Unit.error_thm Code_Unit.mk_rew thm) thy;
751 (*fully applied in order to get right context for mk_rew!*)
753 fun del_post thm thy = (map_post o MetaSimplifier.del_simp)
754 (Code_Unit.error_thm Code_Unit.mk_rew thm) thy;
755 (*fully applied in order to get right context for mk_rew!*)
757 fun add_functrans (name, f) =
758 (map_exec_purge NONE o map_thmproc o apsnd)
759 (AList.update (op =) (name, (serial (), f)));
761 fun del_functrans name =
762 (map_exec_purge NONE o map_thmproc o apsnd)
763 (delete_force "function transformer" name);
765 val _ = Context.>> (Context.map_theory
767 fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
768 fun add_simple_attribute (name, f) =
769 add_attribute (name, Scan.succeed (mk_attribute f));
770 fun add_del_attribute (name, (add, del)) =
771 add_attribute (name, Args.del |-- Scan.succeed (mk_attribute del)
772 || Scan.succeed (mk_attribute add))
774 TypeInterpretation.init
775 #> add_del_attribute ("func", (add_eqn, del_eqn))
776 #> add_simple_attribute ("nbe", add_nonlinear_eqn)
777 #> add_del_attribute ("inline", (add_inline, del_inline))
778 #> add_del_attribute ("post", (add_post, del_post))
782 (** post- and preprocessing **)
786 fun apply_functrans thy [] = []
787 | apply_functrans thy (thms as (thm, _) :: _) =
789 val const = const_of_eqn thy thm;
790 val functrans = (map (fn (_, (_, f)) => f thy) o #functrans
791 o the_thmproc o the_exec) thy;
792 val thms' = perhaps (perhaps_loop (perhaps_apply functrans)) (map fst thms);
793 val thms'' = certify_const thy const thms';
794 in map Code_Unit.add_linear thms'' end;
796 fun rhs_conv conv thm =
798 val thm' = (conv o Thm.rhs_of) thm;
799 in Thm.transitive thm thm' end
801 fun term_of_conv thy f =
810 fun preprocess thy thms =
812 val pre = (Simplifier.theory_context thy o #pre o the_thmproc o the_exec) thy;
815 |> apply_functrans thy
816 |> (map o apfst) (Code_Unit.rewrite_eqn pre)
817 (*FIXME - must check here: rewrite rule, defining equation, proper constant *)
818 |> (map o apfst) (AxClass.unoverload thy)
819 |> burrow_fst common_typ_eqns
823 fun preprocess_conv ct =
825 val thy = Thm.theory_of_cterm ct;
826 val pre = (Simplifier.theory_context thy o #pre o the_thmproc o the_exec) thy;
829 |> Simplifier.rewrite pre
830 |> rhs_conv (AxClass.unoverload_conv thy)
833 fun preprocess_term thy = term_of_conv thy preprocess_conv;
835 fun postprocess_conv ct =
837 val thy = Thm.theory_of_cterm ct;
838 val post = (Simplifier.theory_context thy o #post o the_thmproc o the_exec) thy;
841 |> AxClass.overload_conv thy
842 |> rhs_conv (Simplifier.rewrite post)
845 fun postprocess_term thy = term_of_conv thy postprocess_conv;
849 fun default_typ_proto thy c = case AxClass.inst_of_param thy c
850 of SOME (c, tyco) => classparam_weakest_typ thy ((the o AxClass.class_of_param thy) c)
852 | NONE => (case AxClass.class_of_param thy c
853 of SOME class => SOME (Term.map_type_tvar
854 (K (TVar ((Name.aT, 0), [class]))) (Sign.the_const_type thy c))
855 | NONE => get_constr_typ thy c);
859 fun get_eqns thy const =
860 Symtab.lookup ((the_eqns o the_exec) thy) const
861 |> Option.map (Susp.force o snd)
863 |> (map o apfst) (Thm.transfer thy);
867 fun these_eqns thy const =
869 val drop_refl = filter_out
870 (is_equal o Term.fast_term_ord o Logic.dest_equals o Thm.plain_prop_of o fst);
877 fun default_typ thy c = case default_typ_proto thy c
878 of SOME ty => Code_Unit.typscheme thy (c, ty)
879 | NONE => (case get_eqns thy c
880 of (thm, _) :: _ => snd (Code_Unit.head_eqn (AxClass.unoverload thy thm))
881 | [] => Code_Unit.typscheme thy (c, Sign.the_const_type thy c));
888 (** type-safe interfaces for data depedent on executable content **)
890 functor CodeDataFun(Data: CODE_DATA_ARGS): CODE_DATA =
895 fun dest (Data x) = x
897 val kind = Code.declare_data (Data Data.empty)
898 (fn thy => fn cs => fn Data x => Data (Data.purge thy cs x));
900 val data_op = (kind, Data, dest);
902 val get = Code.get_data data_op;
903 val change = Code.change_data data_op;
904 fun change_yield thy = Code.change_yield_data data_op thy;
908 structure Code : CODE = struct open Code; end;