src/Tools/Code/code_preproc.ML
changeset 31775 2b04504fcb69
parent 31599 97b4d289c646
child 31957 a9742afd403e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Tools/Code/code_preproc.ML	Tue Jun 23 12:09:30 2009 +0200
     1.3 @@ -0,0 +1,515 @@
     1.4 +(*  Title:      Tools/code/code_preproc.ML
     1.5 +    Author:     Florian Haftmann, TU Muenchen
     1.6 +
     1.7 +Preprocessing code equations into a well-sorted system
     1.8 +in a graph with explicit dependencies.
     1.9 +*)
    1.10 +
    1.11 +signature CODE_PREPROC =
    1.12 +sig
    1.13 +  val map_pre: (simpset -> simpset) -> theory -> theory
    1.14 +  val map_post: (simpset -> simpset) -> theory -> theory
    1.15 +  val add_inline: thm -> theory -> theory
    1.16 +  val add_functrans: string * (theory -> (thm * bool) list -> (thm * bool) list option) -> theory -> theory
    1.17 +  val del_functrans: string -> theory -> theory
    1.18 +  val simple_functrans: (theory -> thm list -> thm list option)
    1.19 +    -> theory -> (thm * bool) list -> (thm * bool) list option
    1.20 +  val print_codeproc: theory -> unit
    1.21 +
    1.22 +  type code_algebra
    1.23 +  type code_graph
    1.24 +  val eqns: code_graph -> string -> (thm * bool) list
    1.25 +  val typ: code_graph -> string -> (string * sort) list * typ
    1.26 +  val all: code_graph -> string list
    1.27 +  val pretty: theory -> code_graph -> Pretty.T
    1.28 +  val obtain: theory -> string list -> term list -> code_algebra * code_graph
    1.29 +  val eval_conv: theory -> (sort -> sort)
    1.30 +    -> (code_algebra -> code_graph -> (string * sort) list -> term -> cterm -> thm) -> cterm -> thm
    1.31 +  val eval: theory -> (sort -> sort) -> ((term -> term) -> 'a -> 'a)
    1.32 +    -> (code_algebra -> code_graph -> (string * sort) list -> term -> 'a) -> term -> 'a
    1.33 +
    1.34 +  val setup: theory -> theory
    1.35 +end
    1.36 +
    1.37 +structure Code_Preproc : CODE_PREPROC =
    1.38 +struct
    1.39 +
    1.40 +(** preprocessor administration **)
    1.41 +
    1.42 +(* theory data *)
    1.43 +
    1.44 +datatype thmproc = Thmproc of {
    1.45 +  pre: simpset,
    1.46 +  post: simpset,
    1.47 +  functrans: (string * (serial * (theory -> (thm * bool) list -> (thm * bool) list option))) list
    1.48 +};
    1.49 +
    1.50 +fun make_thmproc ((pre, post), functrans) =
    1.51 +  Thmproc { pre = pre, post = post, functrans = functrans };
    1.52 +fun map_thmproc f (Thmproc { pre, post, functrans }) =
    1.53 +  make_thmproc (f ((pre, post), functrans));
    1.54 +fun merge_thmproc (Thmproc { pre = pre1, post = post1, functrans = functrans1 },
    1.55 +  Thmproc { pre = pre2, post = post2, functrans = functrans2 }) =
    1.56 +    let
    1.57 +      val pre = Simplifier.merge_ss (pre1, pre2);
    1.58 +      val post = Simplifier.merge_ss (post1, post2);
    1.59 +      val functrans = AList.merge (op =) (eq_fst (op =)) (functrans1, functrans2);
    1.60 +    in make_thmproc ((pre, post), functrans) end;
    1.61 +
    1.62 +structure Code_Preproc_Data = TheoryDataFun
    1.63 +(
    1.64 +  type T = thmproc;
    1.65 +  val empty = make_thmproc ((Simplifier.empty_ss, Simplifier.empty_ss), []);
    1.66 +  fun copy spec = spec;
    1.67 +  val extend = copy;
    1.68 +  fun merge pp = merge_thmproc;
    1.69 +);
    1.70 +
    1.71 +fun the_thmproc thy = case Code_Preproc_Data.get thy
    1.72 + of Thmproc x => x;
    1.73 +
    1.74 +fun delete_force msg key xs =
    1.75 +  if AList.defined (op =) xs key then AList.delete (op =) key xs
    1.76 +  else error ("No such " ^ msg ^ ": " ^ quote key);
    1.77 +
    1.78 +fun map_data f thy =
    1.79 +  thy
    1.80 +  |> Code.purge_data
    1.81 +  |> (Code_Preproc_Data.map o map_thmproc) f;
    1.82 +
    1.83 +val map_pre = map_data o apfst o apfst;
    1.84 +val map_post = map_data o apfst o apsnd;
    1.85 +
    1.86 +val add_inline = map_pre o MetaSimplifier.add_simp;
    1.87 +val del_inline = map_pre o MetaSimplifier.del_simp;
    1.88 +val add_post = map_post o MetaSimplifier.add_simp;
    1.89 +val del_post = map_post o MetaSimplifier.del_simp;
    1.90 +  
    1.91 +fun add_functrans (name, f) = (map_data o apsnd)
    1.92 +  (AList.update (op =) (name, (serial (), f)));
    1.93 +
    1.94 +fun del_functrans name = (map_data o apsnd)
    1.95 +  (delete_force "function transformer" name);
    1.96 +
    1.97 +
    1.98 +(* post- and preprocessing *)
    1.99 +
   1.100 +fun apply_functrans thy c _ [] = []
   1.101 +  | apply_functrans thy c [] eqns = eqns
   1.102 +  | apply_functrans thy c functrans eqns = eqns
   1.103 +      |> perhaps (perhaps_loop (perhaps_apply functrans))
   1.104 +      |> Code.assert_eqns_const thy c;
   1.105 +
   1.106 +fun rhs_conv conv thm = Thm.transitive thm ((conv o Thm.rhs_of) thm);
   1.107 +
   1.108 +fun term_of_conv thy f =
   1.109 +  Thm.cterm_of thy
   1.110 +  #> f
   1.111 +  #> Thm.prop_of
   1.112 +  #> Logic.dest_equals
   1.113 +  #> snd;
   1.114 +
   1.115 +fun preprocess thy c eqns =
   1.116 +  let
   1.117 +    val pre = (Simplifier.theory_context thy o #pre o the_thmproc) thy;
   1.118 +    val functrans = (map (fn (_, (_, f)) => f thy) o #functrans
   1.119 +      o the_thmproc) thy;
   1.120 +  in
   1.121 +    eqns
   1.122 +    |> apply_functrans thy c functrans
   1.123 +    |> (map o apfst) (Code.rewrite_eqn pre)
   1.124 +    |> (map o apfst) (AxClass.unoverload thy)
   1.125 +    |> map (Code.assert_eqn thy)
   1.126 +    |> burrow_fst (Code.norm_args thy)
   1.127 +    |> burrow_fst (Code.norm_varnames thy)
   1.128 +  end;
   1.129 +
   1.130 +fun preprocess_conv thy ct =
   1.131 +  let
   1.132 +    val pre = (Simplifier.theory_context thy o #pre o the_thmproc) thy;
   1.133 +  in
   1.134 +    ct
   1.135 +    |> Simplifier.rewrite pre
   1.136 +    |> rhs_conv (AxClass.unoverload_conv thy)
   1.137 +  end;
   1.138 +
   1.139 +fun postprocess_conv thy ct =
   1.140 +  let
   1.141 +    val post = (Simplifier.theory_context thy o #post o the_thmproc) thy;
   1.142 +  in
   1.143 +    ct
   1.144 +    |> AxClass.overload_conv thy
   1.145 +    |> rhs_conv (Simplifier.rewrite post)
   1.146 +  end;
   1.147 +
   1.148 +fun postprocess_term thy = term_of_conv thy (postprocess_conv thy);
   1.149 +
   1.150 +fun print_codeproc thy =
   1.151 +  let
   1.152 +    val ctxt = ProofContext.init thy;
   1.153 +    val pre = (#pre o the_thmproc) thy;
   1.154 +    val post = (#post o the_thmproc) thy;
   1.155 +    val functrans = (map fst o #functrans o the_thmproc) thy;
   1.156 +  in
   1.157 +    (Pretty.writeln o Pretty.chunks) [
   1.158 +      Pretty.block [
   1.159 +        Pretty.str "preprocessing simpset:",
   1.160 +        Pretty.fbrk,
   1.161 +        Simplifier.pretty_ss ctxt pre
   1.162 +      ],
   1.163 +      Pretty.block [
   1.164 +        Pretty.str "postprocessing simpset:",
   1.165 +        Pretty.fbrk,
   1.166 +        Simplifier.pretty_ss ctxt post
   1.167 +      ],
   1.168 +      Pretty.block (
   1.169 +        Pretty.str "function transformers:"
   1.170 +        :: Pretty.fbrk
   1.171 +        :: (Pretty.fbreaks o map Pretty.str) functrans
   1.172 +      )
   1.173 +    ]
   1.174 +  end;
   1.175 +
   1.176 +fun simple_functrans f thy eqns = case f thy (map fst eqns)
   1.177 + of SOME thms' => SOME (map (rpair (forall snd eqns)) thms')
   1.178 +  | NONE => NONE;
   1.179 +
   1.180 +
   1.181 +(** sort algebra and code equation graph types **)
   1.182 +
   1.183 +type code_algebra = (sort -> sort) * Sorts.algebra;
   1.184 +type code_graph = (((string * sort) list * typ) * (thm * bool) list) Graph.T;
   1.185 +
   1.186 +fun eqns eqngr = these o Option.map snd o try (Graph.get_node eqngr);
   1.187 +fun typ eqngr = fst o Graph.get_node eqngr;
   1.188 +fun all eqngr = Graph.keys eqngr;
   1.189 +
   1.190 +fun pretty thy eqngr =
   1.191 +  AList.make (snd o Graph.get_node eqngr) (Graph.keys eqngr)
   1.192 +  |> (map o apfst) (Code.string_of_const thy)
   1.193 +  |> sort (string_ord o pairself fst)
   1.194 +  |> map (fn (s, thms) =>
   1.195 +       (Pretty.block o Pretty.fbreaks) (
   1.196 +         Pretty.str s
   1.197 +         :: map (Display.pretty_thm o fst) thms
   1.198 +       ))
   1.199 +  |> Pretty.chunks;
   1.200 +
   1.201 +
   1.202 +(** the Waisenhaus algorithm **)
   1.203 +
   1.204 +(* auxiliary *)
   1.205 +
   1.206 +fun is_proper_class thy = can (AxClass.get_info thy);
   1.207 +
   1.208 +fun complete_proper_sort thy =
   1.209 +  Sign.complete_sort thy #> filter (is_proper_class thy);
   1.210 +
   1.211 +fun inst_params thy tyco =
   1.212 +  map (fn (c, _) => AxClass.param_of_inst thy (c, tyco))
   1.213 +    o maps (#params o AxClass.get_info thy);
   1.214 +
   1.215 +fun consts_of thy eqns = [] |> (fold o fold o fold_aterms)
   1.216 +  (fn Const (c, ty) => insert (op =) (c, Sign.const_typargs thy (c, Logic.unvarifyT ty)) | _ => I)
   1.217 +    (map (op :: o swap o apfst (snd o strip_comb) o Logic.dest_equals o Thm.plain_prop_of o fst) eqns);
   1.218 +
   1.219 +fun tyscm_rhss_of thy c eqns =
   1.220 +  let
   1.221 +    val tyscm = case eqns of [] => Code.default_typscheme thy c
   1.222 +      | ((thm, _) :: _) => Code.typscheme_eqn thy thm;
   1.223 +    val rhss = consts_of thy eqns;
   1.224 +  in (tyscm, rhss) end;
   1.225 +
   1.226 +
   1.227 +(* data structures *)
   1.228 +
   1.229 +datatype const = Fun of string | Inst of class * string;
   1.230 +
   1.231 +fun const_ord (Fun c1, Fun c2) = fast_string_ord (c1, c2)
   1.232 +  | const_ord (Inst class_tyco1, Inst class_tyco2) =
   1.233 +      prod_ord fast_string_ord fast_string_ord (class_tyco1, class_tyco2)
   1.234 +  | const_ord (Fun _, Inst _) = LESS
   1.235 +  | const_ord (Inst _, Fun _) = GREATER;
   1.236 +
   1.237 +type var = const * int;
   1.238 +
   1.239 +structure Vargraph =
   1.240 +  GraphFun(type key = var val ord = prod_ord const_ord int_ord);
   1.241 +
   1.242 +datatype styp = Tyco of string * styp list | Var of var | Free;
   1.243 +
   1.244 +fun styp_of c_lhs (Type (tyco, tys)) = Tyco (tyco, map (styp_of c_lhs) tys)
   1.245 +  | styp_of c_lhs (TFree (v, _)) = case c_lhs
   1.246 +     of SOME (c, lhs) => Var (Fun c, find_index (fn (v', _) => v = v') lhs)
   1.247 +      | NONE => Free;
   1.248 +
   1.249 +type vardeps_data = ((string * styp list) list * class list) Vargraph.T
   1.250 +  * (((string * sort) list * (thm * bool) list) Symtab.table
   1.251 +    * (class * string) list);
   1.252 +
   1.253 +val empty_vardeps_data : vardeps_data =
   1.254 +  (Vargraph.empty, (Symtab.empty, []));
   1.255 +
   1.256 +
   1.257 +(* retrieving equations and instances from the background context *)
   1.258 +
   1.259 +fun obtain_eqns thy eqngr c =
   1.260 +  case try (Graph.get_node eqngr) c
   1.261 +   of SOME ((lhs, _), eqns) => ((lhs, []), [])
   1.262 +    | NONE => let
   1.263 +        val eqns = Code.these_eqns thy c
   1.264 +          |> preprocess thy c;
   1.265 +        val ((lhs, _), rhss) = tyscm_rhss_of thy c eqns;
   1.266 +      in ((lhs, rhss), eqns) end;
   1.267 +
   1.268 +fun obtain_instance thy arities (inst as (class, tyco)) =
   1.269 +  case AList.lookup (op =) arities inst
   1.270 +   of SOME classess => (classess, ([], []))
   1.271 +    | NONE => let
   1.272 +        val all_classes = complete_proper_sort thy [class];
   1.273 +        val superclasses = remove (op =) class all_classes
   1.274 +        val classess = map (complete_proper_sort thy)
   1.275 +          (Sign.arity_sorts thy tyco [class]);
   1.276 +        val inst_params = inst_params thy tyco all_classes;
   1.277 +      in (classess, (superclasses, inst_params)) end;
   1.278 +
   1.279 +
   1.280 +(* computing instantiations *)
   1.281 +
   1.282 +fun add_classes thy arities eqngr c_k new_classes vardeps_data =
   1.283 +  let
   1.284 +    val (styps, old_classes) = Vargraph.get_node (fst vardeps_data) c_k;
   1.285 +    val diff_classes = new_classes |> subtract (op =) old_classes;
   1.286 +  in if null diff_classes then vardeps_data
   1.287 +  else let
   1.288 +    val c_ks = Vargraph.imm_succs (fst vardeps_data) c_k |> insert (op =) c_k;
   1.289 +  in
   1.290 +    vardeps_data
   1.291 +    |> (apfst o Vargraph.map_node c_k o apsnd) (append diff_classes)
   1.292 +    |> fold (fn styp => fold (ensure_typmatch_inst thy arities eqngr styp) new_classes) styps
   1.293 +    |> fold (fn c_k => add_classes thy arities eqngr c_k diff_classes) c_ks
   1.294 +  end end
   1.295 +and add_styp thy arities eqngr c_k tyco_styps vardeps_data =
   1.296 +  let
   1.297 +    val (old_styps, classes) = Vargraph.get_node (fst vardeps_data) c_k;
   1.298 +  in if member (op =) old_styps tyco_styps then vardeps_data
   1.299 +  else
   1.300 +    vardeps_data
   1.301 +    |> (apfst o Vargraph.map_node c_k o apfst) (cons tyco_styps)
   1.302 +    |> fold (ensure_typmatch_inst thy arities eqngr tyco_styps) classes
   1.303 +  end
   1.304 +and add_dep thy arities eqngr c_k c_k' vardeps_data =
   1.305 +  let
   1.306 +    val (_, classes) = Vargraph.get_node (fst vardeps_data) c_k;
   1.307 +  in
   1.308 +    vardeps_data
   1.309 +    |> add_classes thy arities eqngr c_k' classes
   1.310 +    |> apfst (Vargraph.add_edge (c_k, c_k'))
   1.311 +  end
   1.312 +and ensure_typmatch_inst thy arities eqngr (tyco, styps) class vardeps_data =
   1.313 +  if can (Sign.arity_sorts thy tyco) [class]
   1.314 +  then vardeps_data
   1.315 +    |> ensure_inst thy arities eqngr (class, tyco)
   1.316 +    |> fold_index (fn (k, styp) =>
   1.317 +         ensure_typmatch thy arities eqngr styp (Inst (class, tyco), k)) styps
   1.318 +  else vardeps_data (*permissive!*)
   1.319 +and ensure_inst thy arities eqngr (inst as (class, tyco)) (vardeps_data as (_, (_, insts))) =
   1.320 +  if member (op =) insts inst then vardeps_data
   1.321 +  else let
   1.322 +    val (classess, (superclasses, inst_params)) =
   1.323 +      obtain_instance thy arities inst;
   1.324 +  in
   1.325 +    vardeps_data
   1.326 +    |> (apsnd o apsnd) (insert (op =) inst)
   1.327 +    |> fold_index (fn (k, _) =>
   1.328 +         apfst (Vargraph.new_node ((Inst (class, tyco), k), ([] ,[])))) classess
   1.329 +    |> fold (fn superclass => ensure_inst thy arities eqngr (superclass, tyco)) superclasses
   1.330 +    |> fold (ensure_fun thy arities eqngr) inst_params
   1.331 +    |> fold_index (fn (k, classes) =>
   1.332 +         add_classes thy arities eqngr (Inst (class, tyco), k) classes
   1.333 +         #> fold (fn superclass =>
   1.334 +             add_dep thy arities eqngr (Inst (superclass, tyco), k)
   1.335 +             (Inst (class, tyco), k)) superclasses
   1.336 +         #> fold (fn inst_param =>
   1.337 +             add_dep thy arities eqngr (Fun inst_param, k)
   1.338 +             (Inst (class, tyco), k)
   1.339 +             ) inst_params
   1.340 +         ) classess
   1.341 +  end
   1.342 +and ensure_typmatch thy arities eqngr (Tyco tyco_styps) c_k vardeps_data =
   1.343 +      vardeps_data
   1.344 +      |> add_styp thy arities eqngr c_k tyco_styps
   1.345 +  | ensure_typmatch thy arities eqngr (Var c_k') c_k vardeps_data =
   1.346 +      vardeps_data
   1.347 +      |> add_dep thy arities eqngr c_k c_k'
   1.348 +  | ensure_typmatch thy arities eqngr Free c_k vardeps_data =
   1.349 +      vardeps_data
   1.350 +and ensure_rhs thy arities eqngr (c', styps) vardeps_data =
   1.351 +  vardeps_data
   1.352 +  |> ensure_fun thy arities eqngr c'
   1.353 +  |> fold_index (fn (k, styp) =>
   1.354 +       ensure_typmatch thy arities eqngr styp (Fun c', k)) styps
   1.355 +and ensure_fun thy arities eqngr c (vardeps_data as (_, (eqntab, _))) =
   1.356 +  if Symtab.defined eqntab c then vardeps_data
   1.357 +  else let
   1.358 +    val ((lhs, rhss), eqns) = obtain_eqns thy eqngr c;
   1.359 +    val rhss' = (map o apsnd o map) (styp_of (SOME (c, lhs))) rhss;
   1.360 +  in
   1.361 +    vardeps_data
   1.362 +    |> (apsnd o apfst) (Symtab.update_new (c, (lhs, eqns)))
   1.363 +    |> fold_index (fn (k, _) =>
   1.364 +         apfst (Vargraph.new_node ((Fun c, k), ([] ,[])))) lhs
   1.365 +    |> fold_index (fn (k, (_, sort)) =>
   1.366 +         add_classes thy arities eqngr (Fun c, k) (complete_proper_sort thy sort)) lhs
   1.367 +    |> fold (ensure_rhs thy arities eqngr) rhss'
   1.368 +  end;
   1.369 +
   1.370 +
   1.371 +(* applying instantiations *)
   1.372 +
   1.373 +fun dicts_of thy (proj_sort, algebra) (T, sort) =
   1.374 +  let
   1.375 +    fun class_relation (x, _) _ = x;
   1.376 +    fun type_constructor tyco xs class =
   1.377 +      inst_params thy tyco (Sorts.complete_sort algebra [class])
   1.378 +        @ (maps o maps) fst xs;
   1.379 +    fun type_variable (TFree (_, sort)) = map (pair []) (proj_sort sort);
   1.380 +  in
   1.381 +    flat (Sorts.of_sort_derivation (Syntax.pp_global thy) algebra
   1.382 +      { class_relation = class_relation, type_constructor = type_constructor,
   1.383 +        type_variable = type_variable } (T, proj_sort sort)
   1.384 +       handle Sorts.CLASS_ERROR _ => [] (*permissive!*))
   1.385 +  end;
   1.386 +
   1.387 +fun add_arity thy vardeps (class, tyco) =
   1.388 +  AList.default (op =)
   1.389 +    ((class, tyco), map (fn k => (snd o Vargraph.get_node vardeps) (Inst (class, tyco), k))
   1.390 +      (0 upto Sign.arity_number thy tyco - 1));
   1.391 +
   1.392 +fun add_eqs thy vardeps (c, (proto_lhs, proto_eqns)) (rhss, eqngr) =
   1.393 +  if can (Graph.get_node eqngr) c then (rhss, eqngr)
   1.394 +  else let
   1.395 +    val lhs = map_index (fn (k, (v, _)) =>
   1.396 +      (v, snd (Vargraph.get_node vardeps (Fun c, k)))) proto_lhs;
   1.397 +    val inst_tab = Vartab.empty |> fold (fn (v, sort) =>
   1.398 +      Vartab.update ((v, 0), sort)) lhs;
   1.399 +    val eqns = proto_eqns
   1.400 +      |> (map o apfst) (Code.inst_thm thy inst_tab);
   1.401 +    val (tyscm, rhss') = tyscm_rhss_of thy c eqns;
   1.402 +    val eqngr' = Graph.new_node (c, (tyscm, eqns)) eqngr;
   1.403 +  in (map (pair c) rhss' @ rhss, eqngr') end;
   1.404 +
   1.405 +fun extend_arities_eqngr thy cs ts (arities, eqngr) =
   1.406 +  let
   1.407 +    val cs_rhss = (fold o fold_aterms) (fn Const (c_ty as (c, _)) =>
   1.408 +      insert (op =) (c, (map (styp_of NONE) o Sign.const_typargs thy) c_ty) | _ => I) ts [];
   1.409 +    val (vardeps, (eqntab, insts)) = empty_vardeps_data
   1.410 +      |> fold (ensure_fun thy arities eqngr) cs
   1.411 +      |> fold (ensure_rhs thy arities eqngr) cs_rhss;
   1.412 +    val arities' = fold (add_arity thy vardeps) insts arities;
   1.413 +    val pp = Syntax.pp_global thy;
   1.414 +    val algebra = Sorts.subalgebra pp (is_proper_class thy)
   1.415 +      (AList.lookup (op =) arities') (Sign.classes_of thy);
   1.416 +    val (rhss, eqngr') = Symtab.fold (add_eqs thy vardeps) eqntab ([], eqngr);
   1.417 +    fun deps_of (c, rhs) = c :: maps (dicts_of thy algebra)
   1.418 +      (rhs ~~ (map snd o fst o fst o Graph.get_node eqngr') c);
   1.419 +    val eqngr'' = fold (fn (c, rhs) => fold
   1.420 +      (curry Graph.add_edge c) (deps_of rhs)) rhss eqngr';
   1.421 +  in (algebra, (arities', eqngr'')) end;
   1.422 +
   1.423 +
   1.424 +(** store for preprocessed arities and code equations **)
   1.425 +
   1.426 +structure Wellsorted = CodeDataFun
   1.427 +(
   1.428 +  type T = ((string * class) * sort list) list * code_graph;
   1.429 +  val empty = ([], Graph.empty);
   1.430 +  fun purge thy cs (arities, eqngr) =
   1.431 +    let
   1.432 +      val del_cs = ((Graph.all_preds eqngr
   1.433 +        o filter (can (Graph.get_node eqngr))) cs);
   1.434 +      val del_arities = del_cs
   1.435 +        |> map_filter (AxClass.inst_of_param thy)
   1.436 +        |> maps (fn (c, tyco) =>
   1.437 +             (map (rpair tyco) o Sign.complete_sort thy o the_list
   1.438 +               o AxClass.class_of_param thy) c);
   1.439 +      val arities' = fold (AList.delete (op =)) del_arities arities;
   1.440 +      val eqngr' = Graph.del_nodes del_cs eqngr;
   1.441 +    in (arities', eqngr') end;
   1.442 +);
   1.443 +
   1.444 +
   1.445 +(** retrieval and evaluation interfaces **)
   1.446 +
   1.447 +fun obtain thy cs ts = apsnd snd
   1.448 +  (Wellsorted.change_yield thy (extend_arities_eqngr thy cs ts));
   1.449 +
   1.450 +fun prepare_sorts_typ prep_sort
   1.451 +  = map_type_tfree (fn (v, sort) => TFree (v, prep_sort sort));
   1.452 +
   1.453 +fun prepare_sorts prep_sort (Const (c, ty)) =
   1.454 +      Const (c, prepare_sorts_typ prep_sort ty)
   1.455 +  | prepare_sorts prep_sort (t1 $ t2) =
   1.456 +      prepare_sorts prep_sort t1 $ prepare_sorts prep_sort t2
   1.457 +  | prepare_sorts prep_sort (Abs (v, ty, t)) =
   1.458 +      Abs (v, prepare_sorts_typ prep_sort ty, prepare_sorts prep_sort t)
   1.459 +  | prepare_sorts _ (t as Bound _) = t;
   1.460 +
   1.461 +fun gen_eval thy cterm_of conclude_evaluation prep_sort evaluator proto_ct =
   1.462 +  let
   1.463 +    val pp = Syntax.pp_global thy;
   1.464 +    val ct = cterm_of proto_ct;
   1.465 +    val _ = (Sign.no_frees pp o map_types (K dummyT) o Sign.no_vars pp)
   1.466 +      (Thm.term_of ct);
   1.467 +    val thm = preprocess_conv thy ct;
   1.468 +    val ct' = Thm.rhs_of thm;
   1.469 +    val t' = Thm.term_of ct';
   1.470 +    val vs = Term.add_tfrees t' [];
   1.471 +    val consts = fold_aterms
   1.472 +      (fn Const (c, _) => insert (op =) c | _ => I) t' [];
   1.473 + 
   1.474 +    val t'' = prepare_sorts prep_sort t';
   1.475 +    val (algebra', eqngr') = obtain thy consts [t''];
   1.476 +  in conclude_evaluation (evaluator algebra' eqngr' vs t'' ct') thm end;
   1.477 +
   1.478 +fun simple_evaluator evaluator algebra eqngr vs t ct =
   1.479 +  evaluator algebra eqngr vs t;
   1.480 +
   1.481 +fun eval_conv thy =
   1.482 +  let
   1.483 +    fun conclude_evaluation thm2 thm1 =
   1.484 +      let
   1.485 +        val thm3 = postprocess_conv thy (Thm.rhs_of thm2);
   1.486 +      in
   1.487 +        Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ =>
   1.488 +          error ("could not construct evaluation proof:\n"
   1.489 +          ^ (cat_lines o map Display.string_of_thm) [thm1, thm2, thm3])
   1.490 +      end;
   1.491 +  in gen_eval thy I conclude_evaluation end;
   1.492 +
   1.493 +fun eval thy prep_sort postproc evaluator = gen_eval thy (Thm.cterm_of thy)
   1.494 +  (K o postproc (postprocess_term thy)) prep_sort (simple_evaluator evaluator);
   1.495 +
   1.496 +
   1.497 +(** setup **)
   1.498 +
   1.499 +val setup = 
   1.500 +  let
   1.501 +    fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
   1.502 +    fun add_del_attribute (name, (add, del)) =
   1.503 +      Code.add_attribute (name, Args.del |-- Scan.succeed (mk_attribute del)
   1.504 +        || Scan.succeed (mk_attribute add))
   1.505 +  in
   1.506 +    add_del_attribute ("inline", (add_inline, del_inline))
   1.507 +    #> add_del_attribute ("post", (add_post, del_post))
   1.508 +    #> Code.add_attribute ("unfold", Scan.succeed (Thm.declaration_attribute
   1.509 +       (fn thm => Context.mapping (Codegen.add_unfold thm #> add_inline thm) I)))
   1.510 +  end;
   1.511 +
   1.512 +val _ =
   1.513 +  OuterSyntax.improper_command "print_codeproc" "print code preprocessor setup"
   1.514 +  OuterKeyword.diag (Scan.succeed
   1.515 +      (Toplevel.no_timing o Toplevel.unknown_theory o Toplevel.keep
   1.516 +        (print_codeproc o Toplevel.theory_of)));
   1.517 +
   1.518 +end; (*struct*)