src/Tools/Code/code_preproc.ML
author wenzelm
Mon Mar 31 12:35:39 2014 +0200 (2014-03-31 ago)
changeset 56334 6b3739fee456
parent 55757 9fc71814b8c1
child 56920 d651b944c67e
permissions -rw-r--r--
some shortcuts for chunks, which sometimes avoid bulky string output;
     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: (Proof.context -> Proof.context) -> theory -> theory
    11   val map_post: (Proof.context -> Proof.context) -> theory -> theory
    12   val add_unfold: thm -> theory -> theory
    13   val add_functrans: string * (Proof.context -> (thm * bool) list -> (thm * bool) list option) -> theory -> theory
    14   val del_functrans: string -> theory -> theory
    15   val simple_functrans: (Proof.context -> thm list -> thm list option)
    16     -> Proof.context -> (thm * bool) list -> (thm * bool) list option
    17   val print_codeproc: Proof.context -> unit
    18 
    19   type code_algebra
    20   type code_graph
    21   val cert: code_graph -> string -> Code.cert
    22   val sortargs: code_graph -> string -> sort list
    23   val all: code_graph -> string list
    24   val pretty: Proof.context -> code_graph -> Pretty.T
    25   val obtain: bool -> theory -> string list -> term list -> code_algebra * code_graph
    26   val dynamic_conv: Proof.context
    27     -> (code_algebra -> code_graph -> (string * sort) list -> term -> conv) -> conv
    28   val dynamic_value: Proof.context -> ((term -> term) -> 'a -> 'a)
    29     -> (code_algebra -> code_graph -> (string * sort) list -> term -> 'a) -> term -> 'a
    30   val static_conv: Proof.context -> string list
    31     -> (code_algebra -> code_graph -> Proof.context -> (string * sort) list -> term -> conv)
    32     -> Proof.context -> conv
    33   val static_value: Proof.context -> ((term -> term) -> 'a -> 'a) -> string list
    34     -> (code_algebra -> code_graph -> Proof.context -> (string * sort) list -> term -> 'a)
    35     -> Proof.context -> term -> 'a
    36 
    37   val setup: theory -> theory
    38 end
    39 
    40 structure Code_Preproc : CODE_PREPROC =
    41 struct
    42 
    43 (** preprocessor administration **)
    44 
    45 (* theory data *)
    46 
    47 datatype thmproc = Thmproc of {
    48   pre: simpset,
    49   post: simpset,
    50   functrans: (string * (serial * (Proof.context -> (thm * bool) list -> (thm * bool) list option))) list
    51 };
    52 
    53 fun make_thmproc ((pre, post), functrans) =
    54   Thmproc { pre = pre, post = post, functrans = functrans };
    55 fun map_thmproc f (Thmproc { pre, post, functrans }) =
    56   make_thmproc (f ((pre, post), functrans));
    57 fun merge_thmproc (Thmproc { pre = pre1, post = post1, functrans = functrans1 },
    58   Thmproc { pre = pre2, post = post2, functrans = functrans2 }) =
    59     let
    60       val pre = Simplifier.merge_ss (pre1, pre2);
    61       val post = Simplifier.merge_ss (post1, post2);
    62       val functrans = AList.merge (op =) (eq_fst (op =)) (functrans1, functrans2)
    63         handle AList.DUP => error ("Duplicate function transformer");
    64     in make_thmproc ((pre, post), functrans) end;
    65 
    66 structure Code_Preproc_Data = Theory_Data
    67 (
    68   type T = thmproc;
    69   val empty = make_thmproc ((Simplifier.empty_ss, Simplifier.empty_ss), []);
    70   val extend = I;
    71   val merge = merge_thmproc;
    72 );
    73 
    74 fun the_thmproc thy = case Code_Preproc_Data.get thy
    75  of Thmproc x => x;
    76 
    77 fun delete_force msg key xs =
    78   if AList.defined (op =) xs key then AList.delete (op =) key xs
    79   else error ("No such " ^ msg ^ ": " ^ quote key);
    80 
    81 val map_data = Code_Preproc_Data.map o map_thmproc;
    82 
    83 val map_pre_post = map_data o apfst;
    84 
    85 fun map_simpset which f thy =
    86   map_pre_post (which (simpset_map (Proof_Context.init_global thy) f)) thy;
    87 val map_pre = map_simpset apfst;
    88 val map_post = map_simpset apsnd;
    89 
    90 val add_unfold = map_pre o Simplifier.add_simp;
    91 val del_unfold = map_pre o Simplifier.del_simp;
    92 val add_post = map_post o Simplifier.add_simp;
    93 val del_post = map_post o Simplifier.del_simp;
    94 
    95 fun add_code_abbrev raw_thm thy =
    96   let
    97     val ctxt = Proof_Context.init_global thy;
    98     val thm = Local_Defs.meta_rewrite_rule ctxt raw_thm;
    99     val thm_sym = Thm.symmetric thm;
   100   in
   101     thy |> map_pre_post (fn (pre, post) =>
   102       (pre |> simpset_map ctxt (Simplifier.add_simp thm_sym),
   103        post |> simpset_map ctxt (Simplifier.add_simp thm)))
   104   end;
   105 
   106 fun add_functrans (name, f) = (map_data o apsnd)
   107   (AList.update (op =) (name, (serial (), f)));
   108 
   109 fun del_functrans name = (map_data o apsnd)
   110   (delete_force "function transformer" name);
   111 
   112 
   113 (* post- and preprocessing *)
   114 
   115 fun no_variables_conv ctxt conv ct =
   116   let
   117     val thy = Proof_Context.theory_of ctxt;
   118     val cert = Thm.cterm_of thy;
   119     val all_vars = fold_aterms (fn t as Free _ => insert (op aconvc) (cert t)
   120       | t as Var _ => insert (op aconvc) (cert t)
   121       | _ => I) (Thm.term_of ct) [];
   122     fun apply_beta var thm = Thm.combination thm (Thm.reflexive var)
   123       |> Conv.fconv_rule (Conv.arg_conv (Conv.try_conv (Thm.beta_conversion false)))
   124       |> Conv.fconv_rule (Conv.arg1_conv (Thm.beta_conversion false));
   125   in
   126     ct
   127     |> fold_rev Thm.lambda all_vars
   128     |> conv
   129     |> fold apply_beta all_vars
   130   end;
   131 
   132 fun trans_conv_rule conv thm = Thm.transitive thm ((conv o Thm.rhs_of) thm);
   133 
   134 fun term_of_conv ctxt conv =
   135   Thm.cterm_of (Proof_Context.theory_of ctxt)
   136   #> conv ctxt
   137   #> Thm.prop_of
   138   #> Logic.dest_equals
   139   #> snd;
   140 
   141 fun term_of_conv_resubst ctxt conv t =
   142   let
   143     val all_vars = fold_aterms (fn t as Free _ => insert (op aconv) t
   144       | t as Var _ => insert (op aconv) t
   145       | _ => I) t [];
   146     val resubst = curry (Term.betapplys o swap) all_vars;
   147   in (resubst, term_of_conv ctxt conv (fold_rev lambda all_vars t)) end;
   148 
   149 fun preprocess_conv ctxt =
   150   let
   151     val thy = Proof_Context.theory_of ctxt;
   152     val ss = (#pre o the_thmproc) thy;
   153   in fn ctxt' =>
   154     Simplifier.rewrite (put_simpset ss ctxt')
   155     #> trans_conv_rule (Axclass.unoverload_conv (Proof_Context.theory_of ctxt'))
   156   end;
   157 
   158 fun preprocess_term ctxt =
   159   let
   160     val conv = preprocess_conv ctxt;
   161   in fn ctxt' => term_of_conv_resubst ctxt' conv end;
   162 
   163 fun postprocess_conv ctxt =
   164   let
   165     val thy = Proof_Context.theory_of ctxt;
   166     val ss = (#post o the_thmproc) thy;
   167   in fn ctxt' =>
   168     Axclass.overload_conv (Proof_Context.theory_of ctxt')
   169     #> trans_conv_rule (Simplifier.rewrite (put_simpset ss ctxt'))
   170   end;
   171 
   172 fun postprocess_term ctxt =
   173   let
   174     val conv = postprocess_conv ctxt;
   175   in fn ctxt' => term_of_conv ctxt' conv end;
   176 
   177 fun print_codeproc ctxt =
   178   let
   179     val thy = Proof_Context.theory_of ctxt;
   180     val pre = (#pre o the_thmproc) thy;
   181     val post = (#post o the_thmproc) thy;
   182     val functrans = (map fst o #functrans o the_thmproc) thy;
   183   in
   184     Pretty.writeln_chunks [
   185       Pretty.block [
   186         Pretty.str "preprocessing simpset:",
   187         Pretty.fbrk,
   188         Simplifier.pretty_simpset (put_simpset pre ctxt)
   189       ],
   190       Pretty.block [
   191         Pretty.str "postprocessing simpset:",
   192         Pretty.fbrk,
   193         Simplifier.pretty_simpset (put_simpset post ctxt)
   194       ],
   195       Pretty.block (
   196         Pretty.str "function transformers:"
   197         :: Pretty.fbrk
   198         :: (Pretty.fbreaks o map Pretty.str) functrans
   199       )
   200     ]
   201   end;
   202 
   203 fun simple_functrans f ctxt eqns = case f ctxt (map fst eqns)
   204  of SOME thms' => SOME (map (rpair (forall snd eqns)) thms')
   205   | NONE => NONE;
   206 
   207 
   208 (** sort algebra and code equation graph types **)
   209 
   210 type code_algebra = (sort -> sort) * Sorts.algebra;
   211 type code_graph = ((string * sort) list * Code.cert) Graph.T;
   212 
   213 fun get_node eqngr const = Graph.get_node eqngr const
   214   handle Graph.UNDEF _ => error ("No such constant in code equation graph: " ^ quote const);
   215 
   216 fun cert eqngr = snd o get_node eqngr;
   217 fun sortargs eqngr = map snd o fst o get_node eqngr;
   218 fun all eqngr = Graph.keys eqngr;
   219 
   220 fun pretty ctxt eqngr =
   221   let
   222     val thy = Proof_Context.theory_of ctxt;
   223   in
   224     AList.make (snd o Graph.get_node eqngr) (Graph.keys eqngr)
   225     |> (map o apfst) (Code.string_of_const thy)
   226     |> sort (string_ord o pairself fst)
   227     |> map (fn (s, cert) => (Pretty.block o Pretty.fbreaks) (Pretty.str s :: Code.pretty_cert thy cert))
   228     |> Pretty.chunks
   229   end;
   230 
   231 
   232 (** the Waisenhaus algorithm **)
   233 
   234 (* auxiliary *)
   235 
   236 fun is_proper_class thy = can (Axclass.get_info thy);
   237 
   238 fun complete_proper_sort thy =
   239   Sign.complete_sort thy #> filter (is_proper_class thy);
   240 
   241 fun inst_params thy tyco =
   242   map (fn (c, _) => Axclass.param_of_inst thy (c, tyco))
   243     o maps (#params o Axclass.get_info thy);
   244 
   245 
   246 (* data structures *)
   247 
   248 datatype const = Fun of string | Inst of class * string;
   249 
   250 fun const_ord (Fun c1, Fun c2) = fast_string_ord (c1, c2)
   251   | const_ord (Inst class_tyco1, Inst class_tyco2) =
   252       prod_ord fast_string_ord fast_string_ord (class_tyco1, class_tyco2)
   253   | const_ord (Fun _, Inst _) = LESS
   254   | const_ord (Inst _, Fun _) = GREATER;
   255 
   256 type var = const * int;
   257 
   258 structure Vargraph =
   259   Graph(type key = var val ord = prod_ord const_ord int_ord);
   260 
   261 datatype styp = Tyco of string * styp list | Var of var | Free;
   262 
   263 fun styp_of c_lhs (Type (tyco, tys)) = Tyco (tyco, map (styp_of c_lhs) tys)
   264   | styp_of c_lhs (TFree (v, _)) = case c_lhs
   265      of SOME (c, lhs) => Var (Fun c, find_index (fn (v', _) => v = v') lhs)
   266       | NONE => Free;
   267 
   268 type vardeps_data = ((string * styp list) list * class list) Vargraph.T
   269   * (((string * sort) list * Code.cert) Symtab.table
   270     * (class * string) list);
   271 
   272 val empty_vardeps_data : vardeps_data =
   273   (Vargraph.empty, (Symtab.empty, []));
   274 
   275 
   276 (* retrieving equations and instances from the background context *)
   277 
   278 fun obtain_eqns ctxt eqngr c =
   279   case try (Graph.get_node eqngr) c
   280    of SOME (lhs, cert) => ((lhs, []), cert)
   281     | NONE => let
   282         val thy = Proof_Context.theory_of ctxt;
   283         val functrans = (map (fn (_, (_, f)) => f ctxt)
   284           o #functrans o the_thmproc) thy;
   285         val cert = Code.get_cert thy { functrans = functrans, ss = simpset_of ctxt } c; (*FIXME*)
   286         val (lhs, rhss) =
   287           Code.typargs_deps_of_cert thy cert;
   288       in ((lhs, rhss), cert) end;
   289 
   290 fun obtain_instance ctxt arities (inst as (class, tyco)) =
   291   case AList.lookup (op =) arities inst
   292    of SOME classess => (classess, ([], []))
   293     | NONE => let
   294         val thy = Proof_Context.theory_of ctxt;
   295         val all_classes = complete_proper_sort thy [class];
   296         val super_classes = remove (op =) class all_classes;
   297         val classess = map (complete_proper_sort thy)
   298           (Sign.arity_sorts thy tyco [class]);
   299         val inst_params = inst_params thy tyco all_classes;
   300       in (classess, (super_classes, inst_params)) end;
   301 
   302 
   303 (* computing instantiations *)
   304 
   305 fun add_classes ctxt arities eqngr c_k new_classes vardeps_data =
   306   let
   307     val (styps, old_classes) = Vargraph.get_node (fst vardeps_data) c_k;
   308     val diff_classes = new_classes |> subtract (op =) old_classes;
   309   in if null diff_classes then vardeps_data
   310   else let
   311     val c_ks = Vargraph.immediate_succs (fst vardeps_data) c_k |> insert (op =) c_k;
   312   in
   313     vardeps_data
   314     |> (apfst o Vargraph.map_node c_k o apsnd) (append diff_classes)
   315     |> fold (fn styp => fold (ensure_typmatch_inst ctxt arities eqngr styp) new_classes) styps
   316     |> fold (fn c_k => add_classes ctxt arities eqngr c_k diff_classes) c_ks
   317   end end
   318 and add_styp ctxt arities eqngr c_k new_tyco_styps vardeps_data =
   319   let
   320     val (old_tyco_stypss, classes) = Vargraph.get_node (fst vardeps_data) c_k;
   321   in if member (op =) old_tyco_stypss new_tyco_styps then vardeps_data
   322   else
   323     vardeps_data
   324     |> (apfst o Vargraph.map_node c_k o apfst) (cons new_tyco_styps)
   325     |> fold (ensure_typmatch_inst ctxt arities eqngr new_tyco_styps) classes
   326   end
   327 and add_dep ctxt arities eqngr c_k c_k' vardeps_data =
   328   let
   329     val (_, classes) = Vargraph.get_node (fst vardeps_data) c_k;
   330   in
   331     vardeps_data
   332     |> add_classes ctxt arities eqngr c_k' classes
   333     |> apfst (Vargraph.add_edge (c_k, c_k'))
   334   end
   335 and ensure_typmatch_inst ctxt arities eqngr (tyco, styps) class vardeps_data =
   336   if can (Sign.arity_sorts (Proof_Context.theory_of ctxt) tyco) [class]
   337   then vardeps_data
   338     |> ensure_inst ctxt arities eqngr (class, tyco)
   339     |> fold_index (fn (k, styp) =>
   340          ensure_typmatch ctxt arities eqngr styp (Inst (class, tyco), k)) styps
   341   else vardeps_data (*permissive!*)
   342 and ensure_inst ctxt arities eqngr (inst as (class, tyco)) (vardeps_data as (_, (_, insts))) =
   343   if member (op =) insts inst then vardeps_data
   344   else let
   345     val (classess, (super_classes, inst_params)) =
   346       obtain_instance ctxt arities inst;
   347   in
   348     vardeps_data
   349     |> (apsnd o apsnd) (insert (op =) inst)
   350     |> fold_index (fn (k, _) =>
   351          apfst (Vargraph.new_node ((Inst (class, tyco), k), ([] ,[])))) classess
   352     |> fold (fn super_class => ensure_inst ctxt arities eqngr (super_class, tyco)) super_classes
   353     |> fold (ensure_fun ctxt arities eqngr) inst_params
   354     |> fold_index (fn (k, classes) =>
   355          add_classes ctxt arities eqngr (Inst (class, tyco), k) classes
   356          #> fold (fn super_class =>
   357              add_dep ctxt arities eqngr (Inst (super_class, tyco), k)
   358              (Inst (class, tyco), k)) super_classes
   359          #> fold (fn inst_param =>
   360              add_dep ctxt arities eqngr (Fun inst_param, k)
   361              (Inst (class, tyco), k)
   362              ) inst_params
   363          ) classess
   364   end
   365 and ensure_typmatch ctxt arities eqngr (Tyco tyco_styps) c_k vardeps_data =
   366       vardeps_data
   367       |> add_styp ctxt arities eqngr c_k tyco_styps
   368   | ensure_typmatch ctxt arities eqngr (Var c_k') c_k vardeps_data =
   369       vardeps_data
   370       |> add_dep ctxt arities eqngr c_k c_k'
   371   | ensure_typmatch ctxt arities eqngr Free c_k vardeps_data =
   372       vardeps_data
   373 and ensure_rhs ctxt arities eqngr (c', styps) vardeps_data =
   374   vardeps_data
   375   |> ensure_fun ctxt arities eqngr c'
   376   |> fold_index (fn (k, styp) =>
   377        ensure_typmatch ctxt arities eqngr styp (Fun c', k)) styps
   378 and ensure_fun ctxt arities eqngr c (vardeps_data as (_, (eqntab, _))) =
   379   if Symtab.defined eqntab c then vardeps_data
   380   else let
   381     val ((lhs, rhss), eqns) = obtain_eqns ctxt eqngr c;
   382     val rhss' = (map o apsnd o map) (styp_of (SOME (c, lhs))) rhss;
   383   in
   384     vardeps_data
   385     |> (apsnd o apfst) (Symtab.update_new (c, (lhs, eqns)))
   386     |> fold_index (fn (k, _) =>
   387          apfst (Vargraph.new_node ((Fun c, k), ([] ,[])))) lhs
   388     |> fold_index (fn (k, (_, sort)) => add_classes ctxt arities eqngr (Fun c, k)
   389          (complete_proper_sort (Proof_Context.theory_of ctxt) sort)) lhs
   390     |> fold (ensure_rhs ctxt arities eqngr) rhss'
   391   end;
   392 
   393 
   394 (* applying instantiations *)
   395 
   396 fun dicts_of ctxt (proj_sort, algebra) (T, sort) =
   397   let
   398     val thy = Proof_Context.theory_of ctxt;
   399     fun class_relation (x, _) _ = x;
   400     fun type_constructor (tyco, _) xs class =
   401       inst_params thy tyco (Sorts.complete_sort algebra [class])
   402         @ (maps o maps) fst xs;
   403     fun type_variable (TFree (_, sort)) = map (pair []) (proj_sort sort);
   404   in
   405     flat (Sorts.of_sort_derivation algebra
   406       { class_relation = K class_relation, type_constructor = type_constructor,
   407         type_variable = type_variable } (T, proj_sort sort)
   408        handle Sorts.CLASS_ERROR _ => [] (*permissive!*))
   409   end;
   410 
   411 fun add_arity ctxt vardeps (class, tyco) =
   412   AList.default (op =) ((class, tyco),
   413     map_range (fn k => (snd o Vargraph.get_node vardeps) (Inst (class, tyco), k))
   414       (Sign.arity_number (Proof_Context.theory_of ctxt) tyco));
   415 
   416 fun add_cert ctxt vardeps (c, (proto_lhs, proto_cert)) (rhss, eqngr) =
   417   if can (Graph.get_node eqngr) c then (rhss, eqngr)
   418   else let
   419     val thy = Proof_Context.theory_of ctxt;
   420     val lhs = map_index (fn (k, (v, _)) =>
   421       (v, snd (Vargraph.get_node vardeps (Fun c, k)))) proto_lhs;
   422     val cert = proto_cert
   423       |> Code.constrain_cert thy (map (Sign.minimize_sort thy o snd) lhs)
   424       |> Code.conclude_cert;
   425     val (vs, rhss') = Code.typargs_deps_of_cert thy cert;
   426     val eqngr' = Graph.new_node (c, (vs, cert)) eqngr;
   427   in (map (pair c) rhss' @ rhss, eqngr') end;
   428 
   429 fun extend_arities_eqngr raw_ctxt cs ts (arities, (eqngr : code_graph)) =
   430   let
   431     val thy = Proof_Context.theory_of raw_ctxt;
   432     val {pre, ...} = the_thmproc thy;
   433     val ctxt = put_simpset pre raw_ctxt;
   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 ctxt arities eqngr) cs
   438       |> fold (ensure_rhs ctxt arities eqngr) cs_rhss;
   439     val arities' = fold (add_arity ctxt vardeps) insts arities;
   440     val algebra = Sorts.subalgebra (Context.pretty_global thy) (is_proper_class thy)
   441       (AList.lookup (op =) arities') (Sign.classes_of thy);
   442     val (rhss, eqngr') = Symtab.fold (add_cert ctxt vardeps) eqntab ([], eqngr);
   443     fun deps_of (c, rhs) = c :: maps (dicts_of ctxt algebra)
   444       (rhs ~~ sortargs eqngr' c);
   445     val eqngr'' = fold (fn (c, rhs) => fold
   446       (curry Graph.add_edge c) (deps_of rhs)) rhss eqngr';
   447   in (algebra, (arities', eqngr'')) end;
   448 
   449 
   450 (** store for preprocessed arities and code equations **)
   451 
   452 structure Wellsorted = Code_Data
   453 (
   454   type T = ((string * class) * sort list) list * code_graph;
   455   val empty = ([], Graph.empty);
   456 );
   457 
   458 
   459 (** retrieval and evaluation interfaces **)
   460 
   461 fun obtain ignore_cache thy consts ts = apsnd snd
   462   (Wellsorted.change_yield (if ignore_cache then NONE else SOME thy)
   463     (extend_arities_eqngr (Proof_Context.init_global thy) consts ts));
   464 
   465 fun dest_cterm ct = let val t = Thm.term_of ct in (Term.add_tfrees t [], t) end;
   466 
   467 fun dynamic_conv ctxt conv = no_variables_conv ctxt (fn ct =>
   468   let
   469     val thm1 = preprocess_conv ctxt ctxt ct;
   470     val ct' = Thm.rhs_of thm1;
   471     val (vs', t') = dest_cterm ct';
   472     val consts = fold_aterms
   473       (fn Const (c, _) => insert (op =) c | _ => I) t' [];
   474     val (algebra', eqngr') = obtain false (Proof_Context.theory_of ctxt) consts [t'];
   475     val thm2 = conv algebra' eqngr' vs' t' ct';
   476     val thm3 = postprocess_conv ctxt ctxt (Thm.rhs_of thm2);
   477   in
   478     Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ =>
   479       error ("could not construct evaluation proof:\n"
   480       ^ (cat_lines o map (Display.string_of_thm ctxt)) [thm1, thm2, thm3])
   481   end);
   482 
   483 fun dynamic_value ctxt postproc evaluator t =
   484   let
   485     val (resubst, t') = preprocess_term ctxt ctxt t;
   486     val vs' = Term.add_tfrees t' [];
   487     val consts = fold_aterms
   488       (fn Const (c, _) => insert (op =) c | _ => I) t' [];
   489     val (algebra', eqngr') = obtain false (Proof_Context.theory_of ctxt) consts [t'];
   490   in
   491     t'
   492     |> evaluator algebra' eqngr' vs'
   493     |> postproc (postprocess_term ctxt ctxt o resubst)
   494   end;
   495 
   496 fun static_conv ctxt consts conv =
   497   let
   498     val (algebra, eqngr) = obtain true (Proof_Context.theory_of ctxt) consts [];
   499     val pre_conv = preprocess_conv ctxt;
   500     val conv' = conv algebra eqngr;
   501     val post_conv = postprocess_conv ctxt;
   502   in fn ctxt' => no_variables_conv ctxt' ((pre_conv ctxt')
   503     then_conv (fn ct => uncurry (conv' ctxt') (dest_cterm ct) ct)
   504     then_conv (post_conv ctxt'))
   505   end;
   506 
   507 fun static_value ctxt postproc consts evaluator =
   508   let
   509     val (algebra, eqngr) = obtain true (Proof_Context.theory_of ctxt) consts [];
   510     val preproc = preprocess_term ctxt;
   511     val evaluator' = evaluator algebra eqngr;
   512     val postproc' = postprocess_term ctxt;
   513   in fn ctxt' => 
   514     preproc ctxt'
   515     #-> (fn resubst => fn t => t
   516       |> evaluator' ctxt' (Term.add_tfrees t [])
   517       |> postproc (postproc' ctxt' o resubst))
   518   end;
   519 
   520 
   521 (** setup **)
   522 
   523 val setup = 
   524   let
   525     fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
   526     fun add_del_attribute_parser add del =
   527       Attrib.add_del (mk_attribute add) (mk_attribute del);
   528   in
   529     Attrib.setup @{binding code_unfold} (add_del_attribute_parser add_unfold del_unfold)
   530         "preprocessing equations for code generator"
   531     #> Attrib.setup @{binding code_post} (add_del_attribute_parser add_post del_post)
   532         "postprocessing equations for code generator"
   533     #> Attrib.setup @{binding code_abbrev} (Scan.succeed (mk_attribute add_code_abbrev))
   534         "post- and preprocessing equations for code generator"
   535   end;
   536 
   537 val _ =
   538   Outer_Syntax.improper_command @{command_spec "print_codeproc"} "print code preprocessor setup"
   539     (Scan.succeed (Toplevel.unknown_context o Toplevel.keep (print_codeproc o Toplevel.context_of)));
   540 
   541 end; (*struct*)