src/Tools/Code/code_preproc.ML
author wenzelm
Thu Jul 09 22:01:41 2009 +0200 (2009-07-09)
changeset 31971 8c1b845ed105
parent 31962 baa8dce5bc45
child 31977 e03059ae2d82
permissions -rw-r--r--
renamed functor TableFun to Table, and GraphFun to Graph;
     1 (*  Title:      Tools/code/code_preproc.ML
     2     Author:     Florian Haftmann, TU Muenchen
     3 
     4 Preprocessing code equations into a well-sorted system
     5 in a graph with explicit dependencies.
     6 *)
     7 
     8 signature CODE_PREPROC =
     9 sig
    10   val map_pre: (simpset -> simpset) -> theory -> theory
    11   val map_post: (simpset -> simpset) -> theory -> theory
    12   val add_inline: thm -> theory -> theory
    13   val add_functrans: string * (theory -> (thm * bool) list -> (thm * bool) list option) -> theory -> theory
    14   val del_functrans: string -> theory -> theory
    15   val simple_functrans: (theory -> thm list -> thm list option)
    16     -> theory -> (thm * bool) list -> (thm * bool) list option
    17   val print_codeproc: theory -> unit
    18 
    19   type code_algebra
    20   type code_graph
    21   val eqns: code_graph -> string -> (thm * bool) list
    22   val typ: code_graph -> string -> (string * sort) list * typ
    23   val all: code_graph -> string list
    24   val pretty: theory -> code_graph -> Pretty.T
    25   val obtain: theory -> string list -> term list -> code_algebra * code_graph
    26   val eval_conv: theory -> (sort -> sort)
    27     -> (code_algebra -> code_graph -> (string * sort) list -> term -> cterm -> thm) -> cterm -> thm
    28   val eval: theory -> (sort -> sort) -> ((term -> term) -> 'a -> 'a)
    29     -> (code_algebra -> code_graph -> (string * sort) list -> term -> 'a) -> term -> 'a
    30 
    31   val setup: theory -> theory
    32 end
    33 
    34 structure Code_Preproc : CODE_PREPROC =
    35 struct
    36 
    37 (** preprocessor administration **)
    38 
    39 (* theory data *)
    40 
    41 datatype thmproc = Thmproc of {
    42   pre: simpset,
    43   post: simpset,
    44   functrans: (string * (serial * (theory -> (thm * bool) list -> (thm * bool) list option))) list
    45 };
    46 
    47 fun make_thmproc ((pre, post), functrans) =
    48   Thmproc { pre = pre, post = post, functrans = functrans };
    49 fun map_thmproc f (Thmproc { pre, post, functrans }) =
    50   make_thmproc (f ((pre, post), functrans));
    51 fun merge_thmproc (Thmproc { pre = pre1, post = post1, functrans = functrans1 },
    52   Thmproc { pre = pre2, post = post2, functrans = functrans2 }) =
    53     let
    54       val pre = Simplifier.merge_ss (pre1, pre2);
    55       val post = Simplifier.merge_ss (post1, post2);
    56       val functrans = AList.merge (op =) (eq_fst (op =)) (functrans1, functrans2);
    57     in make_thmproc ((pre, post), functrans) end;
    58 
    59 structure Code_Preproc_Data = TheoryDataFun
    60 (
    61   type T = thmproc;
    62   val empty = make_thmproc ((Simplifier.empty_ss, Simplifier.empty_ss), []);
    63   fun copy spec = spec;
    64   val extend = copy;
    65   fun merge pp = merge_thmproc;
    66 );
    67 
    68 fun the_thmproc thy = case Code_Preproc_Data.get thy
    69  of Thmproc x => x;
    70 
    71 fun delete_force msg key xs =
    72   if AList.defined (op =) xs key then AList.delete (op =) key xs
    73   else error ("No such " ^ msg ^ ": " ^ quote key);
    74 
    75 fun map_data f thy =
    76   thy
    77   |> Code.purge_data
    78   |> (Code_Preproc_Data.map o map_thmproc) f;
    79 
    80 val map_pre = map_data o apfst o apfst;
    81 val map_post = map_data o apfst o apsnd;
    82 
    83 val add_inline = map_pre o MetaSimplifier.add_simp;
    84 val del_inline = map_pre o MetaSimplifier.del_simp;
    85 val add_post = map_post o MetaSimplifier.add_simp;
    86 val del_post = map_post o MetaSimplifier.del_simp;
    87   
    88 fun add_functrans (name, f) = (map_data o apsnd)
    89   (AList.update (op =) (name, (serial (), f)));
    90 
    91 fun del_functrans name = (map_data o apsnd)
    92   (delete_force "function transformer" name);
    93 
    94 
    95 (* post- and preprocessing *)
    96 
    97 fun apply_functrans thy c _ [] = []
    98   | apply_functrans thy c [] eqns = eqns
    99   | apply_functrans thy c functrans eqns = eqns
   100       |> perhaps (perhaps_loop (perhaps_apply functrans))
   101       |> Code.assert_eqns_const thy c;
   102 
   103 fun rhs_conv conv thm = Thm.transitive thm ((conv o Thm.rhs_of) thm);
   104 
   105 fun eqn_conv conv =
   106   let
   107     fun lhs_conv ct = if can Thm.dest_comb ct
   108       then Conv.combination_conv lhs_conv conv ct
   109       else Conv.all_conv ct;
   110   in Conv.combination_conv (Conv.arg_conv lhs_conv) conv end;
   111 
   112 val rewrite_eqn = Conv.fconv_rule o eqn_conv o Simplifier.rewrite;
   113 
   114 fun term_of_conv thy f =
   115   Thm.cterm_of thy
   116   #> f
   117   #> Thm.prop_of
   118   #> Logic.dest_equals
   119   #> snd;
   120 
   121 fun preprocess thy c eqns =
   122   let
   123     val pre = (Simplifier.theory_context thy o #pre o the_thmproc) thy;
   124     val functrans = (map (fn (_, (_, f)) => f thy) o #functrans
   125       o the_thmproc) thy;
   126   in
   127     eqns
   128     |> apply_functrans thy c functrans
   129     |> (map o apfst) (rewrite_eqn pre)
   130     |> (map o apfst) (AxClass.unoverload thy)
   131     |> map (Code.assert_eqn thy)
   132     |> burrow_fst (Code.norm_args thy)
   133     |> burrow_fst (Code.norm_varnames thy)
   134   end;
   135 
   136 fun preprocess_conv thy ct =
   137   let
   138     val pre = (Simplifier.theory_context thy o #pre o the_thmproc) thy;
   139   in
   140     ct
   141     |> Simplifier.rewrite pre
   142     |> rhs_conv (AxClass.unoverload_conv thy)
   143   end;
   144 
   145 fun postprocess_conv thy ct =
   146   let
   147     val post = (Simplifier.theory_context thy o #post o the_thmproc) thy;
   148   in
   149     ct
   150     |> AxClass.overload_conv thy
   151     |> rhs_conv (Simplifier.rewrite post)
   152   end;
   153 
   154 fun postprocess_term thy = term_of_conv thy (postprocess_conv thy);
   155 
   156 fun print_codeproc thy =
   157   let
   158     val ctxt = ProofContext.init thy;
   159     val pre = (#pre o the_thmproc) thy;
   160     val post = (#post o the_thmproc) thy;
   161     val functrans = (map fst o #functrans o the_thmproc) thy;
   162   in
   163     (Pretty.writeln o Pretty.chunks) [
   164       Pretty.block [
   165         Pretty.str "preprocessing simpset:",
   166         Pretty.fbrk,
   167         Simplifier.pretty_ss ctxt pre
   168       ],
   169       Pretty.block [
   170         Pretty.str "postprocessing simpset:",
   171         Pretty.fbrk,
   172         Simplifier.pretty_ss ctxt post
   173       ],
   174       Pretty.block (
   175         Pretty.str "function transformers:"
   176         :: Pretty.fbrk
   177         :: (Pretty.fbreaks o map Pretty.str) functrans
   178       )
   179     ]
   180   end;
   181 
   182 fun simple_functrans f thy eqns = case f thy (map fst eqns)
   183  of SOME thms' => SOME (map (rpair (forall snd eqns)) thms')
   184   | NONE => NONE;
   185 
   186 
   187 (** sort algebra and code equation graph types **)
   188 
   189 type code_algebra = (sort -> sort) * Sorts.algebra;
   190 type code_graph = (((string * sort) list * typ) * (thm * bool) list) Graph.T;
   191 
   192 fun eqns eqngr = these o Option.map snd o try (Graph.get_node eqngr);
   193 fun typ eqngr = fst o Graph.get_node eqngr;
   194 fun all eqngr = Graph.keys eqngr;
   195 
   196 fun pretty thy eqngr =
   197   AList.make (snd o Graph.get_node eqngr) (Graph.keys eqngr)
   198   |> (map o apfst) (Code.string_of_const thy)
   199   |> sort (string_ord o pairself fst)
   200   |> map (fn (s, thms) =>
   201        (Pretty.block o Pretty.fbreaks) (
   202          Pretty.str s
   203          :: map (Display.pretty_thm o fst) thms
   204        ))
   205   |> Pretty.chunks;
   206 
   207 
   208 (** the Waisenhaus algorithm **)
   209 
   210 (* auxiliary *)
   211 
   212 fun is_proper_class thy = can (AxClass.get_info thy);
   213 
   214 fun complete_proper_sort thy =
   215   Sign.complete_sort thy #> filter (is_proper_class thy);
   216 
   217 fun inst_params thy tyco =
   218   map (fn (c, _) => AxClass.param_of_inst thy (c, tyco))
   219     o maps (#params o AxClass.get_info thy);
   220 
   221 fun consts_of thy eqns = [] |> (fold o fold o fold_aterms)
   222   (fn Const (c, ty) => insert (op =) (c, Sign.const_typargs thy (c, Logic.unvarifyT ty)) | _ => I)
   223     (map (op :: o swap o apfst (snd o strip_comb) o Logic.dest_equals o Thm.plain_prop_of o fst) eqns);
   224 
   225 fun default_typscheme_of thy c =
   226   let
   227     val ty = (snd o dest_Const o TermSubst.zero_var_indexes o curry Const c
   228       o Type.strip_sorts o Sign.the_const_type thy) c;
   229   in case AxClass.class_of_param thy c
   230    of SOME class => ([(Name.aT, [class])], ty)
   231     | NONE => Code.typscheme thy (c, ty)
   232   end;
   233 
   234 fun tyscm_rhss_of thy c eqns =
   235   let
   236     val tyscm = case eqns
   237      of [] => default_typscheme_of thy c
   238       | ((thm, _) :: _) => Code.typscheme_eqn thy thm;
   239     val rhss = consts_of thy eqns;
   240   in (tyscm, rhss) end;
   241 
   242 
   243 (* data structures *)
   244 
   245 datatype const = Fun of string | Inst of class * string;
   246 
   247 fun const_ord (Fun c1, Fun c2) = fast_string_ord (c1, c2)
   248   | const_ord (Inst class_tyco1, Inst class_tyco2) =
   249       prod_ord fast_string_ord fast_string_ord (class_tyco1, class_tyco2)
   250   | const_ord (Fun _, Inst _) = LESS
   251   | const_ord (Inst _, Fun _) = GREATER;
   252 
   253 type var = const * int;
   254 
   255 structure Vargraph =
   256   Graph(type key = var val ord = prod_ord const_ord int_ord);
   257 
   258 datatype styp = Tyco of string * styp list | Var of var | Free;
   259 
   260 fun styp_of c_lhs (Type (tyco, tys)) = Tyco (tyco, map (styp_of c_lhs) tys)
   261   | styp_of c_lhs (TFree (v, _)) = case c_lhs
   262      of SOME (c, lhs) => Var (Fun c, find_index (fn (v', _) => v = v') lhs)
   263       | NONE => Free;
   264 
   265 type vardeps_data = ((string * styp list) list * class list) Vargraph.T
   266   * (((string * sort) list * (thm * bool) list) Symtab.table
   267     * (class * string) list);
   268 
   269 val empty_vardeps_data : vardeps_data =
   270   (Vargraph.empty, (Symtab.empty, []));
   271 
   272 
   273 (* retrieving equations and instances from the background context *)
   274 
   275 fun obtain_eqns thy eqngr c =
   276   case try (Graph.get_node eqngr) c
   277    of SOME ((lhs, _), eqns) => ((lhs, []), [])
   278     | NONE => let
   279         val eqns = Code.these_eqns thy c
   280           |> preprocess thy c;
   281         val ((lhs, _), rhss) = tyscm_rhss_of thy c eqns;
   282       in ((lhs, rhss), eqns) end;
   283 
   284 fun obtain_instance thy arities (inst as (class, tyco)) =
   285   case AList.lookup (op =) arities inst
   286    of SOME classess => (classess, ([], []))
   287     | NONE => let
   288         val all_classes = complete_proper_sort thy [class];
   289         val superclasses = remove (op =) class all_classes
   290         val classess = map (complete_proper_sort thy)
   291           (Sign.arity_sorts thy tyco [class]);
   292         val inst_params = inst_params thy tyco all_classes;
   293       in (classess, (superclasses, inst_params)) end;
   294 
   295 
   296 (* computing instantiations *)
   297 
   298 fun add_classes thy arities eqngr c_k new_classes vardeps_data =
   299   let
   300     val (styps, old_classes) = Vargraph.get_node (fst vardeps_data) c_k;
   301     val diff_classes = new_classes |> subtract (op =) old_classes;
   302   in if null diff_classes then vardeps_data
   303   else let
   304     val c_ks = Vargraph.imm_succs (fst vardeps_data) c_k |> insert (op =) c_k;
   305   in
   306     vardeps_data
   307     |> (apfst o Vargraph.map_node c_k o apsnd) (append diff_classes)
   308     |> fold (fn styp => fold (ensure_typmatch_inst thy arities eqngr styp) new_classes) styps
   309     |> fold (fn c_k => add_classes thy arities eqngr c_k diff_classes) c_ks
   310   end end
   311 and add_styp thy arities eqngr c_k tyco_styps vardeps_data =
   312   let
   313     val (old_styps, classes) = Vargraph.get_node (fst vardeps_data) c_k;
   314   in if member (op =) old_styps tyco_styps then vardeps_data
   315   else
   316     vardeps_data
   317     |> (apfst o Vargraph.map_node c_k o apfst) (cons tyco_styps)
   318     |> fold (ensure_typmatch_inst thy arities eqngr tyco_styps) classes
   319   end
   320 and add_dep thy arities eqngr c_k c_k' vardeps_data =
   321   let
   322     val (_, classes) = Vargraph.get_node (fst vardeps_data) c_k;
   323   in
   324     vardeps_data
   325     |> add_classes thy arities eqngr c_k' classes
   326     |> apfst (Vargraph.add_edge (c_k, c_k'))
   327   end
   328 and ensure_typmatch_inst thy arities eqngr (tyco, styps) class vardeps_data =
   329   if can (Sign.arity_sorts thy tyco) [class]
   330   then vardeps_data
   331     |> ensure_inst thy arities eqngr (class, tyco)
   332     |> fold_index (fn (k, styp) =>
   333          ensure_typmatch thy arities eqngr styp (Inst (class, tyco), k)) styps
   334   else vardeps_data (*permissive!*)
   335 and ensure_inst thy arities eqngr (inst as (class, tyco)) (vardeps_data as (_, (_, insts))) =
   336   if member (op =) insts inst then vardeps_data
   337   else let
   338     val (classess, (superclasses, inst_params)) =
   339       obtain_instance thy arities inst;
   340   in
   341     vardeps_data
   342     |> (apsnd o apsnd) (insert (op =) inst)
   343     |> fold_index (fn (k, _) =>
   344          apfst (Vargraph.new_node ((Inst (class, tyco), k), ([] ,[])))) classess
   345     |> fold (fn superclass => ensure_inst thy arities eqngr (superclass, tyco)) superclasses
   346     |> fold (ensure_fun thy arities eqngr) inst_params
   347     |> fold_index (fn (k, classes) =>
   348          add_classes thy arities eqngr (Inst (class, tyco), k) classes
   349          #> fold (fn superclass =>
   350              add_dep thy arities eqngr (Inst (superclass, tyco), k)
   351              (Inst (class, tyco), k)) superclasses
   352          #> fold (fn inst_param =>
   353              add_dep thy arities eqngr (Fun inst_param, k)
   354              (Inst (class, tyco), k)
   355              ) inst_params
   356          ) classess
   357   end
   358 and ensure_typmatch thy arities eqngr (Tyco tyco_styps) c_k vardeps_data =
   359       vardeps_data
   360       |> add_styp thy arities eqngr c_k tyco_styps
   361   | ensure_typmatch thy arities eqngr (Var c_k') c_k vardeps_data =
   362       vardeps_data
   363       |> add_dep thy arities eqngr c_k c_k'
   364   | ensure_typmatch thy arities eqngr Free c_k vardeps_data =
   365       vardeps_data
   366 and ensure_rhs thy arities eqngr (c', styps) vardeps_data =
   367   vardeps_data
   368   |> ensure_fun thy arities eqngr c'
   369   |> fold_index (fn (k, styp) =>
   370        ensure_typmatch thy arities eqngr styp (Fun c', k)) styps
   371 and ensure_fun thy arities eqngr c (vardeps_data as (_, (eqntab, _))) =
   372   if Symtab.defined eqntab c then vardeps_data
   373   else let
   374     val ((lhs, rhss), eqns) = obtain_eqns thy eqngr c;
   375     val rhss' = (map o apsnd o map) (styp_of (SOME (c, lhs))) rhss;
   376   in
   377     vardeps_data
   378     |> (apsnd o apfst) (Symtab.update_new (c, (lhs, eqns)))
   379     |> fold_index (fn (k, _) =>
   380          apfst (Vargraph.new_node ((Fun c, k), ([] ,[])))) lhs
   381     |> fold_index (fn (k, (_, sort)) =>
   382          add_classes thy arities eqngr (Fun c, k) (complete_proper_sort thy sort)) lhs
   383     |> fold (ensure_rhs thy arities eqngr) rhss'
   384   end;
   385 
   386 
   387 (* applying instantiations *)
   388 
   389 fun dicts_of thy (proj_sort, algebra) (T, sort) =
   390   let
   391     fun class_relation (x, _) _ = x;
   392     fun type_constructor tyco xs class =
   393       inst_params thy tyco (Sorts.complete_sort algebra [class])
   394         @ (maps o maps) fst xs;
   395     fun type_variable (TFree (_, sort)) = map (pair []) (proj_sort sort);
   396   in
   397     flat (Sorts.of_sort_derivation (Syntax.pp_global thy) algebra
   398       { class_relation = class_relation, type_constructor = type_constructor,
   399         type_variable = type_variable } (T, proj_sort sort)
   400        handle Sorts.CLASS_ERROR _ => [] (*permissive!*))
   401   end;
   402 
   403 fun inst_thm thy tvars' thm =
   404   let
   405     val tvars = (Term.add_tvars o Thm.prop_of) thm [];
   406     val inter_sort = Sorts.inter_sort (Sign.classes_of thy);
   407     fun mk_inst (tvar as (v, sort)) = case Vartab.lookup tvars' v
   408      of SOME sort' => SOME (pairself (Thm.ctyp_of thy o TVar)
   409           (tvar, (v, inter_sort (sort, sort'))))
   410       | NONE => NONE;
   411     val insts = map_filter mk_inst tvars;
   412   in Thm.instantiate (insts, []) thm end;
   413 
   414 fun add_arity thy vardeps (class, tyco) =
   415   AList.default (op =)
   416     ((class, tyco), map (fn k => (snd o Vargraph.get_node vardeps) (Inst (class, tyco), k))
   417       (0 upto Sign.arity_number thy tyco - 1));
   418 
   419 fun add_eqs thy vardeps (c, (proto_lhs, proto_eqns)) (rhss, eqngr) =
   420   if can (Graph.get_node eqngr) c then (rhss, eqngr)
   421   else let
   422     val lhs = map_index (fn (k, (v, _)) =>
   423       (v, snd (Vargraph.get_node vardeps (Fun c, k)))) proto_lhs;
   424     val inst_tab = Vartab.empty |> fold (fn (v, sort) =>
   425       Vartab.update ((v, 0), sort)) lhs;
   426     val eqns = proto_eqns
   427       |> (map o apfst) (inst_thm thy inst_tab);
   428     val (tyscm, rhss') = tyscm_rhss_of thy c eqns;
   429     val eqngr' = Graph.new_node (c, (tyscm, eqns)) eqngr;
   430   in (map (pair c) rhss' @ rhss, eqngr') end;
   431 
   432 fun extend_arities_eqngr thy cs ts (arities, eqngr) =
   433   let
   434     val cs_rhss = (fold o fold_aterms) (fn Const (c_ty as (c, _)) =>
   435       insert (op =) (c, (map (styp_of NONE) o Sign.const_typargs thy) c_ty) | _ => I) ts [];
   436     val (vardeps, (eqntab, insts)) = empty_vardeps_data
   437       |> fold (ensure_fun thy arities eqngr) cs
   438       |> fold (ensure_rhs thy arities eqngr) cs_rhss;
   439     val arities' = fold (add_arity thy vardeps) insts arities;
   440     val pp = Syntax.pp_global thy;
   441     val algebra = Sorts.subalgebra pp (is_proper_class thy)
   442       (AList.lookup (op =) arities') (Sign.classes_of thy);
   443     val (rhss, eqngr') = Symtab.fold (add_eqs thy vardeps) eqntab ([], eqngr);
   444     fun deps_of (c, rhs) = c :: maps (dicts_of thy algebra)
   445       (rhs ~~ (map snd o fst o fst o Graph.get_node eqngr') c);
   446     val eqngr'' = fold (fn (c, rhs) => fold
   447       (curry Graph.add_edge c) (deps_of rhs)) rhss eqngr';
   448   in (algebra, (arities', eqngr'')) end;
   449 
   450 
   451 (** store for preprocessed arities and code equations **)
   452 
   453 structure Wellsorted = Code_Data_Fun
   454 (
   455   type T = ((string * class) * sort list) list * code_graph;
   456   val empty = ([], Graph.empty);
   457   fun purge thy cs (arities, eqngr) =
   458     let
   459       val del_cs = ((Graph.all_preds eqngr
   460         o filter (can (Graph.get_node eqngr))) cs);
   461       val del_arities = del_cs
   462         |> map_filter (AxClass.inst_of_param thy)
   463         |> maps (fn (c, tyco) =>
   464              (map (rpair tyco) o Sign.complete_sort thy o the_list
   465                o AxClass.class_of_param thy) c);
   466       val arities' = fold (AList.delete (op =)) del_arities arities;
   467       val eqngr' = Graph.del_nodes del_cs eqngr;
   468     in (arities', eqngr') end;
   469 );
   470 
   471 
   472 (** retrieval and evaluation interfaces **)
   473 
   474 fun obtain thy cs ts = apsnd snd
   475   (Wellsorted.change_yield thy (extend_arities_eqngr thy cs ts));
   476 
   477 fun prepare_sorts_typ prep_sort
   478   = map_type_tfree (fn (v, sort) => TFree (v, prep_sort sort));
   479 
   480 fun prepare_sorts prep_sort (Const (c, ty)) =
   481       Const (c, prepare_sorts_typ prep_sort ty)
   482   | prepare_sorts prep_sort (t1 $ t2) =
   483       prepare_sorts prep_sort t1 $ prepare_sorts prep_sort t2
   484   | prepare_sorts prep_sort (Abs (v, ty, t)) =
   485       Abs (v, prepare_sorts_typ prep_sort ty, prepare_sorts prep_sort t)
   486   | prepare_sorts _ (t as Bound _) = t;
   487 
   488 fun gen_eval thy cterm_of conclude_evaluation prep_sort evaluator proto_ct =
   489   let
   490     val pp = Syntax.pp_global thy;
   491     val ct = cterm_of proto_ct;
   492     val _ = (Sign.no_frees pp o map_types (K dummyT) o Sign.no_vars pp)
   493       (Thm.term_of ct);
   494     val thm = preprocess_conv thy ct;
   495     val ct' = Thm.rhs_of thm;
   496     val t' = Thm.term_of ct';
   497     val vs = Term.add_tfrees t' [];
   498     val consts = fold_aterms
   499       (fn Const (c, _) => insert (op =) c | _ => I) t' [];
   500  
   501     val t'' = prepare_sorts prep_sort t';
   502     val (algebra', eqngr') = obtain thy consts [t''];
   503   in conclude_evaluation (evaluator algebra' eqngr' vs t'' ct') thm end;
   504 
   505 fun simple_evaluator evaluator algebra eqngr vs t ct =
   506   evaluator algebra eqngr vs t;
   507 
   508 fun eval_conv thy =
   509   let
   510     fun conclude_evaluation thm2 thm1 =
   511       let
   512         val thm3 = postprocess_conv thy (Thm.rhs_of thm2);
   513       in
   514         Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ =>
   515           error ("could not construct evaluation proof:\n"
   516           ^ (cat_lines o map Display.string_of_thm) [thm1, thm2, thm3])
   517       end;
   518   in gen_eval thy I conclude_evaluation end;
   519 
   520 fun eval thy prep_sort postproc evaluator = gen_eval thy (Thm.cterm_of thy)
   521   (K o postproc (postprocess_term thy)) prep_sort (simple_evaluator evaluator);
   522 
   523 
   524 (** setup **)
   525 
   526 val setup = 
   527   let
   528     fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
   529     fun add_del_attribute (name, (add, del)) =
   530       Code.add_attribute (name, Args.del |-- Scan.succeed (mk_attribute del)
   531         || Scan.succeed (mk_attribute add))
   532   in
   533     add_del_attribute ("inline", (add_inline, del_inline))
   534     #> add_del_attribute ("post", (add_post, del_post))
   535     #> Code.add_attribute ("unfold", Scan.succeed (Thm.declaration_attribute
   536        (fn thm => Context.mapping (Codegen.add_unfold thm #> add_inline thm) I)))
   537   end;
   538 
   539 val _ =
   540   OuterSyntax.improper_command "print_codeproc" "print code preprocessor setup"
   541   OuterKeyword.diag (Scan.succeed
   542       (Toplevel.no_timing o Toplevel.unknown_theory o Toplevel.keep
   543         (print_codeproc o Toplevel.theory_of)));
   544 
   545 end; (*struct*)