src/Pure/Isar/code.ML
changeset 27609 b23c9ad0fe7d
parent 27582 367aff8d7ffd
child 27675 cb0021d56e11
equal deleted inserted replaced
27608:8fd5662ccd97 27609:b23c9ad0fe7d
    19   val map_post: (MetaSimplifier.simpset -> MetaSimplifier.simpset) -> theory -> theory
    19   val map_post: (MetaSimplifier.simpset -> MetaSimplifier.simpset) -> theory -> theory
    20   val add_inline: thm -> theory -> theory
    20   val add_inline: thm -> theory -> theory
    21   val del_inline: thm -> theory -> theory
    21   val del_inline: thm -> theory -> theory
    22   val add_post: thm -> theory -> theory
    22   val add_post: thm -> theory -> theory
    23   val del_post: thm -> theory -> theory
    23   val del_post: thm -> theory -> theory
    24   val add_functrans: string * (theory -> thm list -> thm list) -> theory -> theory
    24   val add_functrans: string * (theory -> thm list -> thm list option) -> theory -> theory
    25   val del_functrans: string -> theory -> theory
    25   val del_functrans: string -> theory -> theory
    26   val add_datatype: (string * typ) list -> theory -> theory
    26   val add_datatype: (string * typ) list -> theory -> theory
    27   val add_datatype_cmd: string list -> theory -> theory
    27   val add_datatype_cmd: string list -> theory -> theory
    28   val type_interpretation:
    28   val type_interpretation:
    29     (string * ((string * sort) list * (string * typ list) list)
    29     (string * ((string * sort) list * (string * typ list) list)
    52 
    52 
    53 signature CODE_DATA_ARGS =
    53 signature CODE_DATA_ARGS =
    54 sig
    54 sig
    55   type T
    55   type T
    56   val empty: T
    56   val empty: T
    57   val merge: Pretty.pp -> T * T -> T
    57   val purge: theory -> string list -> T -> T
    58   val purge: theory option -> string list option -> T -> T
       
    59 end;
    58 end;
    60 
    59 
    61 signature CODE_DATA =
    60 signature CODE_DATA =
    62 sig
    61 sig
    63   type T
    62   type T
    67 end;
    66 end;
    68 
    67 
    69 signature PRIVATE_CODE =
    68 signature PRIVATE_CODE =
    70 sig
    69 sig
    71   include CODE
    70   include CODE
    72   val declare_data: Object.T -> (Pretty.pp -> Object.T * Object.T -> Object.T)
    71   val declare_data: Object.T -> (theory -> string list -> Object.T -> Object.T)
    73     -> (theory option -> string list option -> Object.T -> Object.T) -> serial
    72     -> serial
    74   val get_data: serial * ('a -> Object.T) * (Object.T -> 'a)
    73   val get_data: serial * ('a -> Object.T) * (Object.T -> 'a)
    75     -> theory -> 'a
    74     -> theory -> 'a
    76   val change_data: serial * ('a -> Object.T) * (Object.T -> 'a)
    75   val change_data: serial * ('a -> Object.T) * (Object.T -> 'a)
    77     -> theory -> ('a -> 'a) -> 'a
    76     -> theory -> ('a -> 'a) -> 'a
    78   val change_yield_data: serial * ('a -> Object.T) * (Object.T -> 'a)
    77   val change_yield_data: serial * ('a -> Object.T) * (Object.T -> 'a)
   169     val all_sels = Susp.force sels;
   168     val all_sels = Susp.force sels;
   170   in (Susp.value [], rev all_sels @ dels) end;
   169   in (Susp.value [], rev all_sels @ dels) end;
   171 
   170 
   172 fun pretty_sdthms ctxt (sels, _) = pretty_lthms ctxt sels;
   171 fun pretty_sdthms ctxt (sels, _) = pretty_lthms ctxt sels;
   173 
   172 
   174 
       
   175 (* fundamental melting operations *)
       
   176 (*FIXME delete*)
       
   177 
       
   178 fun melt _ ([], []) = (false, [])
   173 fun melt _ ([], []) = (false, [])
   179   | melt _ ([], ys) = (true, ys)
   174   | melt _ ([], ys) = (true, ys)
   180   | melt eq (xs, ys) = fold_rev
   175   | melt eq (xs, ys) = fold_rev
   181       (fn y => fn (t, xs) => (t orelse not (member eq xs y), insert eq y xs)) ys (false, xs);
   176       (fn y => fn (t, xs) => (t orelse not (member eq xs y), insert eq y xs)) ys (false, xs);
   182 
   177 
   198     then let
   193     then let
   199       val (_, sels) = melt_thms
   194       val (_, sels) = melt_thms
   200         (subtract Thm.eq_thm_prop dels2 (Susp.force sels1), Susp.force sels2);
   195         (subtract Thm.eq_thm_prop dels2 (Susp.force sels1), Susp.force sels2);
   201       val (_, dels) = melt_thms
   196       val (_, dels) = melt_thms
   202         (subtract Thm.eq_thm_prop (Susp.force sels2) dels1, dels2);
   197         (subtract Thm.eq_thm_prop (Susp.force sels2) dels1, dels2);
   203     in (true, ((Susp.delay o K) sels, dels)) end
   198     in ((Susp.delay o K) sels, dels) end
   204     else let
   199     else let
   205       val (sels_t, sels) = melt_lthms (sels1, sels2);
   200       val (_, sels) = melt_lthms (sels1, sels2);
   206     in (sels_t, (sels, dels)) end
   201     in (sels, dels) end
   207   end;
   202   end;
   208 
   203 
   209 
   204 
   210 (* specification data *)
   205 (* specification data *)
   211 
       
   212 val merge_funcs = Symtab.join (fn _ => fn ((_, a), (_, b)) => melt_sdthms (a, b));
       
   213 
   206 
   214 val eq_string = op = : string * string -> bool;
   207 val eq_string = op = : string * string -> bool;
   215 fun eq_dtyp ((vs1, cs1), (vs2, cs2)) = 
   208 fun eq_dtyp ((vs1, cs1), (vs2, cs2)) = 
   216   gen_eq_set (eq_pair eq_string (gen_eq_set eq_string)) (vs1, vs2)
   209   gen_eq_set (eq_pair eq_string (gen_eq_set eq_string)) (vs1, vs2)
   217     andalso gen_eq_set (eq_fst eq_string) (cs1, cs2);
   210     andalso gen_eq_set (eq_fst eq_string) (cs1, cs2);
   218 fun merge_dtyps (tabs as (tab1, tab2)) =
   211 fun merge_dtyps (tabs as (tab1, tab2)) =
   219   let
   212   let
   220     fun join _ (cos as (_, cos2)) = if eq_dtyp cos then raise Symtab.SAME else cos2;
   213     fun join _ (cos as (_, cos2)) = if eq_dtyp cos then raise Symtab.SAME else cos2;
   221   in Symtab.join join tabs end;
   214   in Symtab.join join tabs end;
   222 
   215 
   223 fun merge_cases ((cases1, undefs1), (cases2, undefs2)) =
       
   224   (Symtab.merge (K true) (cases1, cases2), Symtab.merge (K true) (undefs1, undefs2));
       
   225 
       
   226 datatype spec = Spec of {
   216 datatype spec = Spec of {
   227   funcs: (bool * sdthms) Symtab.table,
   217   funcs: sdthms Symtab.table,
   228   dtyps: ((string * sort) list * (string * typ list) list) Symtab.table,
   218   dtyps: ((string * sort) list * (string * typ list) list) Symtab.table,
   229   cases: (int * string list) Symtab.table * unit Symtab.table
   219   cases: (int * string list) Symtab.table * unit Symtab.table
   230 };
   220 };
   231 
   221 
   232 fun mk_spec (funcs, (dtyps, cases)) =
   222 fun mk_spec (funcs, (dtyps, cases)) =
   233   Spec { funcs = funcs, dtyps = dtyps, cases = cases };
   223   Spec { funcs = funcs, dtyps = dtyps, cases = cases };
   234 fun map_spec f (Spec { funcs = funcs, dtyps = dtyps, cases = cases }) =
   224 fun map_spec f (Spec { funcs = funcs, dtyps = dtyps, cases = cases }) =
   235   mk_spec (f (funcs, (dtyps, cases)));
   225   mk_spec (f (funcs, (dtyps, cases)));
   236 fun merge_spec (Spec { funcs = funcs1, dtyps = dtyps1, cases = cases1 },
   226 fun merge_spec (Spec { funcs = funcs1, dtyps = dtyps1, cases = (cases1, undefs1) },
   237   Spec { funcs = funcs2, dtyps = dtyps2, cases = cases2 }) =
   227   Spec { funcs = funcs2, dtyps = dtyps2, cases = (cases2, undefs2) }) =
   238   let
   228   let
   239     val funcs = merge_funcs (funcs1, funcs2);
   229     val funcs = Symtab.join (K melt_sdthms) (funcs1, funcs2);
   240     val dtyps = merge_dtyps (dtyps1, dtyps2);
   230     val dtyps = merge_dtyps (dtyps1, dtyps2);
   241     val cases = merge_cases (cases1, cases2);
   231     val cases = (Symtab.merge (K true) (cases1, cases2),
       
   232       Symtab.merge (K true) (undefs1, undefs2));
   242   in mk_spec (funcs, (dtyps, cases)) end;
   233   in mk_spec (funcs, (dtyps, cases)) end;
   243 
   234 
   244 
   235 
   245 (* pre- and postprocessor *)
   236 (* pre- and postprocessor *)
   246 
   237 
   247 datatype thmproc = Thmproc of {
   238 datatype thmproc = Thmproc of {
   248   pre: MetaSimplifier.simpset,
   239   pre: MetaSimplifier.simpset,
   249   post: MetaSimplifier.simpset,
   240   post: MetaSimplifier.simpset,
   250   functrans: (string * (serial * (theory -> thm list -> thm list))) list
   241   functrans: (string * (serial * (theory -> thm list -> thm list option))) list
   251 };
   242 };
   252 
   243 
   253 fun mk_thmproc ((pre, post), functrans) =
   244 fun mk_thmproc ((pre, post), functrans) =
   254   Thmproc { pre = pre, post = post, functrans = functrans };
   245   Thmproc { pre = pre, post = post, functrans = functrans };
   255 fun map_thmproc f (Thmproc { pre, post, functrans }) =
   246 fun map_thmproc f (Thmproc { pre, post, functrans }) =
   298 
   289 
   299 local
   290 local
   300 
   291 
   301 type kind = {
   292 type kind = {
   302   empty: Object.T,
   293   empty: Object.T,
   303   merge: Pretty.pp -> Object.T * Object.T -> Object.T,
   294   purge: theory -> string list -> Object.T -> Object.T
   304   purge: theory option -> string list option -> Object.T -> Object.T
       
   305 };
   295 };
   306 
   296 
   307 val kinds = ref (Datatab.empty: kind Datatab.table);
   297 val kinds = ref (Datatab.empty: kind Datatab.table);
   308 val kind_keys = ref ([]: serial list);
   298 val kind_keys = ref ([]: serial list);
   309 
   299 
   311  of SOME kind => f kind
   301  of SOME kind => f kind
   312   | NONE => sys_error "Invalid code data identifier";
   302   | NONE => sys_error "Invalid code data identifier";
   313 
   303 
   314 in
   304 in
   315 
   305 
   316 fun declare_data empty merge purge =
   306 fun declare_data empty purge =
   317   let
   307   let
   318     val k = serial ();
   308     val k = serial ();
   319     val kind = {empty = empty, merge = merge, purge = purge};
   309     val kind = {empty = empty, purge = purge};
   320     val _ = change kinds (Datatab.update (k, kind));
   310     val _ = change kinds (Datatab.update (k, kind));
   321     val _ = change kind_keys (cons k);
   311     val _ = change kind_keys (cons k);
   322   in k end;
   312   in k end;
   323 
   313 
   324 fun invoke_empty k = invoke (fn kind => #empty kind) k;
   314 fun invoke_init k = invoke (fn kind => #empty kind) k;
   325 
   315 
   326 fun invoke_merge_all pp = Datatab.join
   316 fun invoke_purge_all thy cs =
   327   (invoke (fn kind => #merge kind pp));
       
   328 
       
   329 fun invoke_purge_all thy_opt cs =
       
   330   fold (fn k => Datatab.map_entry k
   317   fold (fn k => Datatab.map_entry k
   331     (invoke (fn kind => #purge kind thy_opt cs) k)) (! kind_keys);
   318     (invoke (fn kind => #purge kind thy cs) k)) (! kind_keys);
   332 
   319 
   333 end; (*local*)
   320 end; (*local*)
   334 
   321 
   335 
   322 
   336 (** theory store **)
   323 (** theory store **)
   337 
   324 
   338 local
   325 local
   339 
   326 
   340 type data = Object.T Datatab.table;
   327 type data = Object.T Datatab.table;
       
   328 val empty_data = Datatab.empty : data;
   341 
   329 
   342 structure CodeData = TheoryDataFun
   330 structure CodeData = TheoryDataFun
   343 (
   331 (
   344   type T = exec * data ref;
   332   type T = exec * data ref;
   345   val empty = (empty_exec, ref Datatab.empty : data ref);
   333   val empty = (empty_exec, ref empty_data);
   346   fun copy (exec, data) = (exec, ref (! data));
   334   fun copy (exec, data) = (exec, ref (! data));
   347   val extend = copy;
   335   val extend = copy;
   348   fun merge pp ((exec1, data1), (exec2, data2)) =
   336   fun merge pp ((exec1, data1), (exec2, data2)) =
   349     let
   337     (merge_exec (exec1, exec2), ref empty_data);
   350       val exec = merge_exec (exec1, exec2);
       
   351       val data1' = invoke_purge_all NONE NONE (! data1);
       
   352       val data2' = invoke_purge_all NONE NONE (! data2);
       
   353       val data = invoke_merge_all pp (data1', data2');
       
   354     in (exec, ref data) end;
       
   355 );
   338 );
   356 
   339 
   357 val _ = Context.>> (Context.map_theory CodeData.init);
   340 val _ = Context.>> (Context.map_theory CodeData.init);
   358 
   341 
   359 fun thy_data f thy = f ((snd o CodeData.get) thy);
   342 fun thy_data f thy = f ((snd o CodeData.get) thy);
   360 
   343 
   361 fun get_ensure_init kind data_ref =
   344 fun get_ensure_init kind data_ref =
   362   case Datatab.lookup (! data_ref) kind
   345   case Datatab.lookup (! data_ref) kind
   363    of SOME x => x
   346    of SOME x => x
   364     | NONE => let val y = invoke_empty kind
   347     | NONE => let val y = invoke_init kind
   365         in (change data_ref (Datatab.update (kind, y)); y) end;
   348         in (change data_ref (Datatab.update (kind, y)); y) end;
   366 
   349 
   367 in
   350 in
   368 
   351 
   369 (* access to executable content *)
   352 (* access to executable content *)
   370 
   353 
   371 val the_exec = fst o CodeData.get;
   354 val the_exec = fst o CodeData.get;
   372 
   355 
   373 fun map_exec_purge touched f thy =
   356 fun map_exec_purge touched f thy =
   374   CodeData.map (fn (exec, data) => 
   357   CodeData.map (fn (exec, data) => (f exec, ref (case touched
   375     (f exec, ref (invoke_purge_all (SOME thy) touched (! data)))) thy;
   358    of SOME cs => invoke_purge_all thy cs (! data)
       
   359     | NONE => empty_data))) thy;
   376 
   360 
   377 
   361 
   378 (* access to data dependent on abstract executable content *)
   362 (* access to data dependent on abstract executable content *)
   379 
   363 
   380 fun get_data (kind, _, dest) = thy_data (get_ensure_init kind #> dest);
   364 fun get_data (kind, _, dest) = thy_data (get_ensure_init kind #> dest);
   424                           :: map (Pretty.quote o Syntax.pretty_typ_global thy) tys)) cos)
   408                           :: map (Pretty.quote o Syntax.pretty_typ_global thy) tys)) cos)
   425           );
   409           );
   426     val pre = (#pre o the_thmproc) exec;
   410     val pre = (#pre o the_thmproc) exec;
   427     val post = (#post o the_thmproc) exec;
   411     val post = (#post o the_thmproc) exec;
   428     val functrans = (map fst o #functrans o the_thmproc) exec;
   412     val functrans = (map fst o #functrans o the_thmproc) exec;
   429     val funs = the_funcs exec
   413     val funcs = the_funcs exec
   430       |> Symtab.dest
   414       |> Symtab.dest
   431       |> (map o apsnd) snd
       
   432       |> (map o apfst) (CodeUnit.string_of_const thy)
   415       |> (map o apfst) (CodeUnit.string_of_const thy)
   433       |> sort (string_ord o pairself fst);
   416       |> sort (string_ord o pairself fst);
   434     val dtyps = the_dtyps exec
   417     val dtyps = the_dtyps exec
   435       |> Symtab.dest
   418       |> Symtab.dest
   436       |> map (fn (dtco, (vs, cos)) =>
   419       |> map (fn (dtco, (vs, cos)) =>
   439   in
   422   in
   440     (Pretty.writeln o Pretty.chunks) [
   423     (Pretty.writeln o Pretty.chunks) [
   441       Pretty.block (
   424       Pretty.block (
   442         Pretty.str "defining equations:"
   425         Pretty.str "defining equations:"
   443         :: Pretty.fbrk
   426         :: Pretty.fbrk
   444         :: (Pretty.fbreaks o map pretty_func) funs
   427         :: (Pretty.fbreaks o map pretty_func) funcs
   445       ),
   428       ),
   446       Pretty.block [
   429       Pretty.block [
   447         Pretty.str "preprocessing simpset:",
   430         Pretty.str "preprocessing simpset:",
   448         Pretty.fbrk,
   431         Pretty.fbrk,
   449         MetaSimplifier.pretty_ss pre
   432         MetaSimplifier.pretty_ss pre
   452         Pretty.str "postprocessing simpset:",
   435         Pretty.str "postprocessing simpset:",
   453         Pretty.fbrk,
   436         Pretty.fbrk,
   454         MetaSimplifier.pretty_ss post
   437         MetaSimplifier.pretty_ss post
   455       ],
   438       ],
   456       Pretty.block (
   439       Pretty.block (
   457         Pretty.str "function transformators:"
   440         Pretty.str "function transformers:"
   458         :: Pretty.fbrk
   441         :: Pretty.fbrk
   459         :: (Pretty.fbreaks o map Pretty.str) functrans
   442         :: (Pretty.fbreaks o map Pretty.str) functrans
   460       ),
   443       ),
   461       Pretty.block (
   444       Pretty.block (
   462         Pretty.str "datatypes:"
   445         Pretty.str "datatypes:"
   525     val vs = Name.invents Name.context "" (Sign.arity_number thy tyco);
   508     val vs = Name.invents Name.context "" (Sign.arity_number thy tyco);
   526     val classparams = (map fst o these o try (#params o AxClass.get_info thy)) class;
   509     val classparams = (map fst o these o try (#params o AxClass.get_info thy)) class;
   527     val funcs = classparams
   510     val funcs = classparams
   528       |> map_filter (fn c => try (AxClass.param_of_inst thy) (c, tyco))
   511       |> map_filter (fn c => try (AxClass.param_of_inst thy) (c, tyco))
   529       |> map (Symtab.lookup ((the_funcs o the_exec) thy))
   512       |> map (Symtab.lookup ((the_funcs o the_exec) thy))
   530       |> (map o Option.map) (Susp.force o fst o snd)
   513       |> (map o Option.map) (Susp.force o fst)
   531       |> maps these
   514       |> maps these
   532       |> map (Thm.transfer thy)
   515       |> map (Thm.transfer thy)
   533     fun sorts_of [Type (_, tys)] = map (snd o dest_TVar) tys
   516     fun sorts_of [Type (_, tys)] = map (snd o dest_TVar) tys
   534       | sorts_of tys = map (snd o dest_TVar) tys;
   517       | sorts_of tys = map (snd o dest_TVar) tys;
   535     val sorts = map (sorts_of o Sign.const_typargs thy o const_of thy) funcs;
   518     val sorts = map (sorts_of o Sign.const_typargs thy o const_of thy) funcs;
   702       then error ("Rejected equation for datatype constructor:\n"
   685       then error ("Rejected equation for datatype constructor:\n"
   703         ^ Display.string_of_thm func)
   686         ^ Display.string_of_thm func)
   704       else ();
   687       else ();
   705   in
   688   in
   706     (map_exec_purge (SOME [c]) o map_funcs) (Symtab.map_default
   689     (map_exec_purge (SOME [c]) o map_funcs) (Symtab.map_default
   707       (c, (false, (Susp.value [], []))) (apsnd (add_thm func))) thy
   690       (c, (Susp.value [], [])) (add_thm func)) thy
   708   end;
   691   end;
   709 
   692 
   710 fun add_liberal_func thm thy =
   693 fun add_liberal_func thm thy =
   711   case mk_liberal_func thm
   694   case mk_liberal_func thm
   712    of SOME func => let
   695    of SOME func => let
   714         in if (is_some o AxClass.class_of_param thy) c
   697         in if (is_some o AxClass.class_of_param thy) c
   715           orelse (is_some o get_datatype_of_constr thy) c
   698           orelse (is_some o get_datatype_of_constr thy) c
   716           then thy
   699           then thy
   717           else map_exec_purge (SOME [c]) (map_funcs
   700           else map_exec_purge (SOME [c]) (map_funcs
   718             (Symtab.map_default
   701             (Symtab.map_default
   719               (c, (false, (Susp.value [], []))) (apsnd (add_thm func)))) thy
   702               (c, (Susp.value [], [])) (add_thm func))) thy
   720         end
   703         end
   721     | NONE => thy;
   704     | NONE => thy;
   722 
   705 
   723 fun add_default_func thm thy =
   706 fun add_default_func thm thy =
   724   case mk_default_func thm
   707   case mk_default_func thm
   727         in if (is_some o AxClass.class_of_param thy) c
   710         in if (is_some o AxClass.class_of_param thy) c
   728           orelse (is_some o get_datatype_of_constr thy) c
   711           orelse (is_some o get_datatype_of_constr thy) c
   729           then thy
   712           then thy
   730           else map_exec_purge (SOME [c]) (map_funcs
   713           else map_exec_purge (SOME [c]) (map_funcs
   731           (Symtab.map_default
   714           (Symtab.map_default
   732             (c, (false, (Susp.value [], []))) (apsnd (add_thm func)))) thy
   715             (c, (Susp.value [], [])) (add_thm func))) thy
   733         end
   716         end
   734     | NONE => thy;
   717     | NONE => thy;
   735 
   718 
   736 fun del_func thm thy =
   719 fun del_func thm thy =
   737   case mk_liberal_func thm
   720   case mk_liberal_func thm
   738    of SOME func => let
   721    of SOME func => let
   739           val c = const_of_func thy func;
   722           val c = const_of_func thy func;
   740         in map_exec_purge (SOME [c]) (map_funcs
   723         in map_exec_purge (SOME [c]) (map_funcs
   741           (Symtab.map_entry c (apsnd (del_thm func)))) thy
   724           (Symtab.map_entry c (del_thm func))) thy
   742         end
   725         end
   743     | NONE => thy;
   726     | NONE => thy;
   744 
   727 
   745 fun del_funcs const = map_exec_purge (SOME [const])
   728 fun del_funcs const = map_exec_purge (SOME [const])
   746   (map_funcs (Symtab.map_entry const (apsnd del_thms)));
   729   (map_funcs (Symtab.map_entry const del_thms));
   747 
   730 
   748 fun add_funcl (const, lthms) thy =
   731 fun add_funcl (const, lthms) thy =
   749   let
   732   let
   750     val lthms' = certificate thy (fn thy => certify_const thy const) lthms;
   733     val lthms' = certificate thy (fn thy => certify_const thy const) lthms;
   751       (*FIXME must check compatibility with sort algebra;
   734       (*FIXME must check compatibility with sort algebra;
   752         alas, naive checking results in non-termination!*)
   735         alas, naive checking results in non-termination!*)
   753   in
   736   in
   754     map_exec_purge (SOME [const])
   737     map_exec_purge (SOME [const])
   755       (map_funcs (Symtab.map_default (const, (false, (Susp.value [], [])))
   738       (map_funcs (Symtab.map_default (const, (Susp.value [], []))
   756       (apsnd (add_lthms lthms')))) thy
   739       (add_lthms lthms'))) thy
   757   end;
   740   end;
   758 
   741 
   759 val add_default_func_attr = Attrib.internal (fn _ => Thm.declaration_attribute
   742 val add_default_func_attr = Attrib.internal (fn _ => Thm.declaration_attribute
   760   (fn thm => Context.mapping (add_default_func thm) I));
   743   (fn thm => Context.mapping (add_default_func thm) I));
   761 
   744 
   817   (map_exec_purge NONE o map_thmproc o apsnd)
   800   (map_exec_purge NONE o map_thmproc o apsnd)
   818     (AList.update (op =) (name, (serial (), f)));
   801     (AList.update (op =) (name, (serial (), f)));
   819 
   802 
   820 fun del_functrans name =
   803 fun del_functrans name =
   821   (map_exec_purge NONE o map_thmproc o apsnd)
   804   (map_exec_purge NONE o map_thmproc o apsnd)
   822     (delete_force "function transformator" name);
   805     (delete_force "function transformer" name);
   823 
   806 
   824 val _ = Context.>> (Context.map_theory
   807 val _ = Context.>> (Context.map_theory
   825   (let
   808   (let
   826     fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
   809     fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
   827     fun add_simple_attribute (name, f) =
   810     fun add_simple_attribute (name, f) =
   839 
   822 
   840 (** post- and preprocessing **)
   823 (** post- and preprocessing **)
   841 
   824 
   842 local
   825 local
   843 
   826 
   844 fun apply_functrans thy f [] = []
   827 fun apply_functrans thy [] = []
   845   | apply_functrans thy f (thms as (thm :: _)) =
   828   | apply_functrans thy (thms as thm :: _) =
   846       let
   829       let
   847         val const = const_of_func thy thm;
   830         val const = const_of_func thy thm;
   848         val thms' = f thy thms;
   831         val functrans = (map (fn (_, (_, f)) => f thy) o #functrans
       
   832           o the_thmproc o the_exec) thy;
       
   833         val thms' = perhaps (perhaps_loop (perhaps_apply functrans)) thms;
   849       in certify_const thy const thms' end;
   834       in certify_const thy const thms' end;
   850 
   835 
   851 fun rhs_conv conv thm =
   836 fun rhs_conv conv thm =
   852   let
   837   let
   853     val thm' = (conv o Thm.rhs_of) thm;
   838     val thm' = (conv o Thm.rhs_of) thm;
   865 fun preprocess thy thms =
   850 fun preprocess thy thms =
   866   let
   851   let
   867     val pre = (Simplifier.theory_context thy o #pre o the_thmproc o the_exec) thy;
   852     val pre = (Simplifier.theory_context thy o #pre o the_thmproc o the_exec) thy;
   868   in
   853   in
   869     thms
   854     thms
   870     |> fold (fn (_, (_, f)) => apply_functrans thy f) ((#functrans o the_thmproc o the_exec) thy)
   855     |> apply_functrans thy
   871     |> map (CodeUnit.rewrite_func pre)
   856     |> map (CodeUnit.rewrite_func pre)
   872     (*FIXME - must check gere: rewrite rule, defining equation, proper constant *)
   857     (*FIXME - must check gere: rewrite rule, defining equation, proper constant *)
   873     |> map (AxClass.unoverload thy)
   858     |> map (AxClass.unoverload thy)
   874     |> common_typ_funcs
   859     |> common_typ_funcs
   875   end;
   860   end;
   911 
   896 
   912 local
   897 local
   913 
   898 
   914 fun get_funcs thy const =
   899 fun get_funcs thy const =
   915   Symtab.lookup ((the_funcs o the_exec) thy) const
   900   Symtab.lookup ((the_funcs o the_exec) thy) const
   916   |> Option.map (Susp.force o fst o snd)
   901   |> Option.map (Susp.force o fst)
   917   |> these
   902   |> these
   918   |> map (Thm.transfer thy);
   903   |> map (Thm.transfer thy);
   919 
   904 
   920 in
   905 in
   921 
   906 
   948 type T = Data.T;
   933 type T = Data.T;
   949 exception Data of T;
   934 exception Data of T;
   950 fun dest (Data x) = x
   935 fun dest (Data x) = x
   951 
   936 
   952 val kind = Code.declare_data (Data Data.empty)
   937 val kind = Code.declare_data (Data Data.empty)
   953   (fn pp => fn (Data x1, Data x2) => Data (Data.merge pp (x1, x2)))
   938   (fn thy => fn cs => fn Data x => Data (Data.purge thy cs x));
   954   (fn thy_opt => fn cs => fn Data x => Data (Data.purge thy_opt cs x));
       
   955 
   939 
   956 val data_op = (kind, Data, dest);
   940 val data_op = (kind, Data, dest);
   957 
   941 
   958 val get = Code.get_data data_op;
   942 val get = Code.get_data data_op;
   959 val change = Code.change_data data_op;
   943 val change = Code.change_data data_op;