src/Tools/Code/code_preproc.ML
author haftmann
Wed Jan 13 12:20:37 2010 +0100 (2010-01-13)
changeset 34895 19fd499cddff
parent 34893 ecdc526af73a
child 35224 1c9866c5f6fb
child 35232 f588e1169c8b
permissions -rw-r--r--
explicit abstract type of code certificates
     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_unfold: 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 preprocess_functrans: theory -> (thm * bool) list -> (thm * bool) list
    18   val print_codeproc: theory -> unit
    19 
    20   type code_algebra
    21   type code_graph
    22   val cert: code_graph -> string -> Code.cert
    23   val sortargs: code_graph -> string -> sort list
    24   val all: code_graph -> string list
    25   val pretty: theory -> code_graph -> Pretty.T
    26   val obtain: theory -> string list -> term list -> code_algebra * code_graph
    27   val eval_conv: theory
    28     -> (code_algebra -> code_graph -> (string * sort) list -> term -> cterm -> thm) -> cterm -> thm
    29   val eval: theory -> ((term -> term) -> 'a -> 'a)
    30     -> (code_algebra -> code_graph -> (string * sort) list -> term -> 'a) -> term -> 'a
    31 
    32   val setup: theory -> theory
    33 end
    34 
    35 structure Code_Preproc : CODE_PREPROC =
    36 struct
    37 
    38 (** preprocessor administration **)
    39 
    40 (* theory data *)
    41 
    42 datatype thmproc = Thmproc of {
    43   pre: simpset,
    44   post: simpset,
    45   functrans: (string * (serial * (theory -> (thm * bool) list -> (thm * bool) list option))) list
    46 };
    47 
    48 fun make_thmproc ((pre, post), functrans) =
    49   Thmproc { pre = pre, post = post, functrans = functrans };
    50 fun map_thmproc f (Thmproc { pre, post, functrans }) =
    51   make_thmproc (f ((pre, post), functrans));
    52 fun merge_thmproc (Thmproc { pre = pre1, post = post1, functrans = functrans1 },
    53   Thmproc { pre = pre2, post = post2, functrans = functrans2 }) =
    54     let
    55       val pre = Simplifier.merge_ss (pre1, pre2);
    56       val post = Simplifier.merge_ss (post1, post2);
    57       val functrans = AList.merge (op =) (eq_fst (op =)) (functrans1, functrans2)
    58         handle AList.DUP => error ("Duplicate function transformer");
    59     in make_thmproc ((pre, post), functrans) end;
    60 
    61 structure Code_Preproc_Data = Theory_Data
    62 (
    63   type T = thmproc;
    64   val empty = make_thmproc ((Simplifier.empty_ss, Simplifier.empty_ss), []);
    65   val extend = I;
    66   val merge = merge_thmproc;
    67 );
    68 
    69 fun the_thmproc thy = case Code_Preproc_Data.get thy
    70  of Thmproc x => x;
    71 
    72 fun delete_force msg key xs =
    73   if AList.defined (op =) xs key then AList.delete (op =) key xs
    74   else error ("No such " ^ msg ^ ": " ^ quote key);
    75 
    76 fun map_data f = Code.purge_data
    77   #> (Code_Preproc_Data.map o map_thmproc) f;
    78 
    79 val map_pre_post = map_data o apfst;
    80 val map_pre = map_pre_post o apfst;
    81 val map_post = map_pre_post o apsnd;
    82 
    83 val add_unfold = map_pre o MetaSimplifier.add_simp;
    84 val del_unfold = 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_unfold_post raw_thm thy =
    89   let
    90     val thm = LocalDefs.meta_rewrite_rule (ProofContext.init thy) raw_thm;
    91     val thm_sym = Thm.symmetric thm;
    92   in
    93     thy |> map_pre_post (fn (pre, post) =>
    94       (pre |> MetaSimplifier.add_simp thm, post |> MetaSimplifier.del_simp thm_sym))
    95   end;
    96 
    97 fun add_functrans (name, f) = (map_data o apsnd)
    98   (AList.update (op =) (name, (serial (), f)));
    99 
   100 fun del_functrans name = (map_data o apsnd)
   101   (delete_force "function transformer" name);
   102 
   103 
   104 (* post- and preprocessing *)
   105 
   106 fun trans_conv_rule conv thm = Thm.transitive thm ((conv o Thm.rhs_of) thm);
   107 
   108 fun eqn_conv conv ct =
   109   let
   110     fun lhs_conv ct = if can Thm.dest_comb ct
   111       then Conv.combination_conv lhs_conv conv ct
   112       else Conv.all_conv ct;
   113   in Conv.combination_conv (Conv.arg_conv lhs_conv) conv ct end;
   114 
   115 val rewrite_eqn = Conv.fconv_rule o eqn_conv o Simplifier.rewrite;
   116 
   117 fun term_of_conv thy f =
   118   Thm.cterm_of thy
   119   #> f
   120   #> Thm.prop_of
   121   #> Logic.dest_equals
   122   #> snd;
   123 
   124 fun preprocess_functrans thy = 
   125   let
   126     val functrans = (map (fn (_, (_, f)) => f thy) o #functrans
   127       o the_thmproc) thy;
   128   in perhaps (perhaps_loop (perhaps_apply functrans)) end;
   129 
   130 fun preprocess thy =
   131   let
   132     val pre = (Simplifier.theory_context thy o #pre o the_thmproc) thy;
   133   in
   134     preprocess_functrans thy
   135     #> (map o apfst) (rewrite_eqn pre)
   136   end;
   137 
   138 fun preprocess_conv thy ct =
   139   let
   140     val pre = (Simplifier.theory_context thy o #pre o the_thmproc) thy;
   141   in
   142     ct
   143     |> Simplifier.rewrite pre
   144     |> trans_conv_rule (AxClass.unoverload_conv thy)
   145   end;
   146 
   147 fun postprocess_conv thy ct =
   148   let
   149     val post = (Simplifier.theory_context thy o #post o the_thmproc) thy;
   150   in
   151     ct
   152     |> AxClass.overload_conv thy
   153     |> trans_conv_rule (Simplifier.rewrite post)
   154   end;
   155 
   156 fun postprocess_term thy = term_of_conv thy (postprocess_conv thy);
   157 
   158 fun print_codeproc thy =
   159   let
   160     val ctxt = ProofContext.init thy;
   161     val pre = (#pre o the_thmproc) thy;
   162     val post = (#post o the_thmproc) thy;
   163     val functrans = (map fst o #functrans o the_thmproc) thy;
   164   in
   165     (Pretty.writeln o Pretty.chunks) [
   166       Pretty.block [
   167         Pretty.str "preprocessing simpset:",
   168         Pretty.fbrk,
   169         Simplifier.pretty_ss ctxt pre
   170       ],
   171       Pretty.block [
   172         Pretty.str "postprocessing simpset:",
   173         Pretty.fbrk,
   174         Simplifier.pretty_ss ctxt post
   175       ],
   176       Pretty.block (
   177         Pretty.str "function transformers:"
   178         :: Pretty.fbrk
   179         :: (Pretty.fbreaks o map Pretty.str) functrans
   180       )
   181     ]
   182   end;
   183 
   184 fun simple_functrans f thy eqns = case f thy (map fst eqns)
   185  of SOME thms' => SOME (map (rpair (forall snd eqns)) thms')
   186   | NONE => NONE;
   187 
   188 
   189 (** sort algebra and code equation graph types **)
   190 
   191 type code_algebra = (sort -> sort) * Sorts.algebra;
   192 type code_graph = ((string * sort) list * Code.cert) Graph.T;
   193 
   194 fun cert eqngr = snd o Graph.get_node eqngr;
   195 fun sortargs eqngr = map snd o fst o Graph.get_node eqngr;
   196 fun all eqngr = Graph.keys eqngr;
   197 
   198 fun pretty thy eqngr =
   199   AList.make (snd o Graph.get_node eqngr) (Graph.keys eqngr)
   200   |> (map o apfst) (Code.string_of_const thy)
   201   |> sort (string_ord o pairself fst)
   202   |> map (fn (s, cert) => (Pretty.block o Pretty.fbreaks) (Pretty.str s :: Code.pretty_cert thy cert))
   203   |> Pretty.chunks;
   204 
   205 
   206 (** the Waisenhaus algorithm **)
   207 
   208 (* auxiliary *)
   209 
   210 fun is_proper_class thy = can (AxClass.get_info thy);
   211 
   212 fun complete_proper_sort thy =
   213   Sign.complete_sort thy #> filter (is_proper_class thy);
   214 
   215 fun inst_params thy tyco =
   216   map (fn (c, _) => AxClass.param_of_inst thy (c, tyco))
   217     o maps (#params o AxClass.get_info thy);
   218 
   219 fun typargs_rhss thy c cert =
   220   let
   221     val ((vs, _), equations) = Code.equations_cert thy cert;
   222     val rhss = [] |> (fold o fold o fold_aterms)
   223       (fn Const (c, ty) => insert (op =) (c, Sign.const_typargs thy (c, ty)) | _ => I)
   224         (map (op :: o swap) equations);
   225   in (vs, rhss) end;
   226 
   227 
   228 (* data structures *)
   229 
   230 datatype const = Fun of string | Inst of class * string;
   231 
   232 fun const_ord (Fun c1, Fun c2) = fast_string_ord (c1, c2)
   233   | const_ord (Inst class_tyco1, Inst class_tyco2) =
   234       prod_ord fast_string_ord fast_string_ord (class_tyco1, class_tyco2)
   235   | const_ord (Fun _, Inst _) = LESS
   236   | const_ord (Inst _, Fun _) = GREATER;
   237 
   238 type var = const * int;
   239 
   240 structure Vargraph =
   241   Graph(type key = var val ord = prod_ord const_ord int_ord);
   242 
   243 datatype styp = Tyco of string * styp list | Var of var | Free;
   244 
   245 fun styp_of c_lhs (Type (tyco, tys)) = Tyco (tyco, map (styp_of c_lhs) tys)
   246   | styp_of c_lhs (TFree (v, _)) = case c_lhs
   247      of SOME (c, lhs) => Var (Fun c, find_index (fn (v', _) => v = v') lhs)
   248       | NONE => Free;
   249 
   250 type vardeps_data = ((string * styp list) list * class list) Vargraph.T
   251   * (((string * sort) list * Code.cert) Symtab.table
   252     * (class * string) list);
   253 
   254 val empty_vardeps_data : vardeps_data =
   255   (Vargraph.empty, (Symtab.empty, []));
   256 
   257 
   258 (* retrieving equations and instances from the background context *)
   259 
   260 fun obtain_eqns thy eqngr c =
   261   case try (Graph.get_node eqngr) c
   262    of SOME (lhs, cert) => ((lhs, []), cert)
   263     | NONE => let
   264         val cert = Code.get_cert thy (preprocess thy) c;
   265         val (lhs, rhss) = typargs_rhss thy c cert;
   266       in ((lhs, rhss), cert) end;
   267 
   268 fun obtain_instance thy arities (inst as (class, tyco)) =
   269   case AList.lookup (op =) arities inst
   270    of SOME classess => (classess, ([], []))
   271     | NONE => let
   272         val all_classes = complete_proper_sort thy [class];
   273         val superclasses = remove (op =) class all_classes;
   274         val classess = map (complete_proper_sort thy)
   275           (Sign.arity_sorts thy tyco [class]);
   276         val inst_params = inst_params thy tyco all_classes;
   277       in (classess, (superclasses, inst_params)) end;
   278 
   279 
   280 (* computing instantiations *)
   281 
   282 fun add_classes thy arities eqngr c_k new_classes vardeps_data =
   283   let
   284     val (styps, old_classes) = Vargraph.get_node (fst vardeps_data) c_k;
   285     val diff_classes = new_classes |> subtract (op =) old_classes;
   286   in if null diff_classes then vardeps_data
   287   else let
   288     val c_ks = Vargraph.imm_succs (fst vardeps_data) c_k |> insert (op =) c_k;
   289   in
   290     vardeps_data
   291     |> (apfst o Vargraph.map_node c_k o apsnd) (append diff_classes)
   292     |> fold (fn styp => fold (ensure_typmatch_inst thy arities eqngr styp) new_classes) styps
   293     |> fold (fn c_k => add_classes thy arities eqngr c_k diff_classes) c_ks
   294   end end
   295 and add_styp thy arities eqngr c_k tyco_styps vardeps_data =
   296   let
   297     val (old_styps, classes) = Vargraph.get_node (fst vardeps_data) c_k;
   298   in if member (op =) old_styps tyco_styps then vardeps_data
   299   else
   300     vardeps_data
   301     |> (apfst o Vargraph.map_node c_k o apfst) (cons tyco_styps)
   302     |> fold (ensure_typmatch_inst thy arities eqngr tyco_styps) classes
   303   end
   304 and add_dep thy arities eqngr c_k c_k' vardeps_data =
   305   let
   306     val (_, classes) = Vargraph.get_node (fst vardeps_data) c_k;
   307   in
   308     vardeps_data
   309     |> add_classes thy arities eqngr c_k' classes
   310     |> apfst (Vargraph.add_edge (c_k, c_k'))
   311   end
   312 and ensure_typmatch_inst thy arities eqngr (tyco, styps) class vardeps_data =
   313   if can (Sign.arity_sorts thy tyco) [class]
   314   then vardeps_data
   315     |> ensure_inst thy arities eqngr (class, tyco)
   316     |> fold_index (fn (k, styp) =>
   317          ensure_typmatch thy arities eqngr styp (Inst (class, tyco), k)) styps
   318   else vardeps_data (*permissive!*)
   319 and ensure_inst thy arities eqngr (inst as (class, tyco)) (vardeps_data as (_, (_, insts))) =
   320   if member (op =) insts inst then vardeps_data
   321   else let
   322     val (classess, (superclasses, inst_params)) =
   323       obtain_instance thy arities inst;
   324   in
   325     vardeps_data
   326     |> (apsnd o apsnd) (insert (op =) inst)
   327     |> fold_index (fn (k, _) =>
   328          apfst (Vargraph.new_node ((Inst (class, tyco), k), ([] ,[])))) classess
   329     |> fold (fn superclass => ensure_inst thy arities eqngr (superclass, tyco)) superclasses
   330     |> fold (ensure_fun thy arities eqngr) inst_params
   331     |> fold_index (fn (k, classes) =>
   332          add_classes thy arities eqngr (Inst (class, tyco), k) classes
   333          #> fold (fn superclass =>
   334              add_dep thy arities eqngr (Inst (superclass, tyco), k)
   335              (Inst (class, tyco), k)) superclasses
   336          #> fold (fn inst_param =>
   337              add_dep thy arities eqngr (Fun inst_param, k)
   338              (Inst (class, tyco), k)
   339              ) inst_params
   340          ) classess
   341   end
   342 and ensure_typmatch thy arities eqngr (Tyco tyco_styps) c_k vardeps_data =
   343       vardeps_data
   344       |> add_styp thy arities eqngr c_k tyco_styps
   345   | ensure_typmatch thy arities eqngr (Var c_k') c_k vardeps_data =
   346       vardeps_data
   347       |> add_dep thy arities eqngr c_k c_k'
   348   | ensure_typmatch thy arities eqngr Free c_k vardeps_data =
   349       vardeps_data
   350 and ensure_rhs thy arities eqngr (c', styps) vardeps_data =
   351   vardeps_data
   352   |> ensure_fun thy arities eqngr c'
   353   |> fold_index (fn (k, styp) =>
   354        ensure_typmatch thy arities eqngr styp (Fun c', k)) styps
   355 and ensure_fun thy arities eqngr c (vardeps_data as (_, (eqntab, _))) =
   356   if Symtab.defined eqntab c then vardeps_data
   357   else let
   358     val ((lhs, rhss), eqns) = obtain_eqns thy eqngr c;
   359     val rhss' = (map o apsnd o map) (styp_of (SOME (c, lhs))) rhss;
   360   in
   361     vardeps_data
   362     |> (apsnd o apfst) (Symtab.update_new (c, (lhs, eqns)))
   363     |> fold_index (fn (k, _) =>
   364          apfst (Vargraph.new_node ((Fun c, k), ([] ,[])))) lhs
   365     |> fold_index (fn (k, (_, sort)) =>
   366          add_classes thy arities eqngr (Fun c, k) (complete_proper_sort thy sort)) lhs
   367     |> fold (ensure_rhs thy arities eqngr) rhss'
   368   end;
   369 
   370 
   371 (* applying instantiations *)
   372 
   373 fun dicts_of thy (proj_sort, algebra) (T, sort) =
   374   let
   375     fun class_relation (x, _) _ = x;
   376     fun type_constructor tyco xs class =
   377       inst_params thy tyco (Sorts.complete_sort algebra [class])
   378         @ (maps o maps) fst xs;
   379     fun type_variable (TFree (_, sort)) = map (pair []) (proj_sort sort);
   380   in
   381     flat (Sorts.of_sort_derivation algebra
   382       { class_relation = class_relation, type_constructor = type_constructor,
   383         type_variable = type_variable } (T, proj_sort sort)
   384        handle Sorts.CLASS_ERROR _ => [] (*permissive!*))
   385   end;
   386 
   387 fun add_arity thy vardeps (class, tyco) =
   388   AList.default (op =) ((class, tyco),
   389     map_range (fn k => (snd o Vargraph.get_node vardeps) (Inst (class, tyco), k))
   390       (Sign.arity_number thy tyco));
   391 
   392 fun add_cert thy vardeps (c, (proto_lhs, proto_cert)) (rhss, eqngr) =
   393   if can (Graph.get_node eqngr) c then (rhss, eqngr)
   394   else let
   395     val lhs = map_index (fn (k, (v, _)) =>
   396       (v, snd (Vargraph.get_node vardeps (Fun c, k)))) proto_lhs;
   397     val cert = Code.constrain_cert thy (map snd lhs) proto_cert;
   398     val (vs, rhss') = typargs_rhss thy c cert;
   399     val eqngr' = Graph.new_node (c, (vs, cert)) eqngr;
   400   in (map (pair c) rhss' @ rhss, eqngr') end;
   401 
   402 fun extend_arities_eqngr thy cs ts (arities, (eqngr : code_graph)) =
   403   let
   404     val cs_rhss = (fold o fold_aterms) (fn Const (c_ty as (c, _)) =>
   405       insert (op =) (c, (map (styp_of NONE) o Sign.const_typargs thy) c_ty) | _ => I) ts [];
   406     val (vardeps, (eqntab, insts)) = empty_vardeps_data
   407       |> fold (ensure_fun thy arities eqngr) cs
   408       |> fold (ensure_rhs thy arities eqngr) cs_rhss;
   409     val arities' = fold (add_arity thy vardeps) insts arities;
   410     val pp = Syntax.pp_global thy;
   411     val algebra = Sorts.subalgebra pp (is_proper_class thy)
   412       (AList.lookup (op =) arities') (Sign.classes_of thy);
   413     val (rhss, eqngr') = Symtab.fold (add_cert thy vardeps) eqntab ([], eqngr);
   414     fun deps_of (c, rhs) = c :: maps (dicts_of thy algebra)
   415       (rhs ~~ sortargs eqngr' c);
   416     val eqngr'' = fold (fn (c, rhs) => fold
   417       (curry Graph.add_edge c) (deps_of rhs)) rhss eqngr';
   418   in (algebra, (arities', eqngr'')) end;
   419 
   420 
   421 (** store for preprocessed arities and code equations **)
   422 
   423 structure Wellsorted = Code_Data
   424 (
   425   type T = ((string * class) * sort list) list * code_graph;
   426   val empty = ([], Graph.empty);
   427 );
   428 
   429 
   430 (** retrieval and evaluation interfaces **)
   431 
   432 fun obtain thy cs ts = apsnd snd
   433   (Wellsorted.change_yield thy (extend_arities_eqngr thy cs ts));
   434 
   435 fun gen_eval thy cterm_of conclude_evaluation evaluator proto_ct =
   436   let
   437     val pp = Syntax.pp_global thy;
   438     val ct = cterm_of proto_ct;
   439     val _ = (Sign.no_frees pp o map_types (K dummyT) o Sign.no_vars pp)
   440       (Thm.term_of ct);
   441     val thm = preprocess_conv thy ct;
   442     val ct' = Thm.rhs_of thm;
   443     val t' = Thm.term_of ct';
   444     val vs = Term.add_tfrees t' [];
   445     val consts = fold_aterms
   446       (fn Const (c, _) => insert (op =) c | _ => I) t' [];
   447     val (algebra', eqngr') = obtain thy consts [t'];
   448   in conclude_evaluation (evaluator algebra' eqngr' vs t' ct') thm end;
   449 
   450 fun simple_evaluator evaluator algebra eqngr vs t ct =
   451   evaluator algebra eqngr vs t;
   452 
   453 fun eval_conv thy =
   454   let
   455     fun conclude_evaluation thm2 thm1 =
   456       let
   457         val thm3 = postprocess_conv thy (Thm.rhs_of thm2);
   458       in
   459         Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ =>
   460           error ("could not construct evaluation proof:\n"
   461           ^ (cat_lines o map (Display.string_of_thm_global thy)) [thm1, thm2, thm3])
   462       end;
   463   in gen_eval thy I conclude_evaluation end;
   464 
   465 fun eval thy postproc evaluator = gen_eval thy (Thm.cterm_of thy)
   466   (K o postproc (postprocess_term thy)) (simple_evaluator evaluator);
   467 
   468 
   469 (** setup **)
   470 
   471 val setup = 
   472   let
   473     fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
   474     fun add_del_attribute_parser add del =
   475       Attrib.add_del (mk_attribute add) (mk_attribute del);
   476     fun both f g thm = f thm #> g thm;
   477   in
   478     Attrib.setup @{binding code_unfold} (add_del_attribute_parser 
   479        (both Codegen.add_unfold add_unfold) (both Codegen.del_unfold del_unfold))
   480         "preprocessing equations for code generators"
   481     #> Attrib.setup @{binding code_inline} (add_del_attribute_parser add_unfold del_unfold)
   482         "preprocessing equations for code generator"
   483     #> Attrib.setup @{binding code_post} (add_del_attribute_parser add_post del_post)
   484         "postprocessing equations for code generator"
   485     #> Attrib.setup @{binding code_unfold_post} (Scan.succeed (mk_attribute add_unfold_post))
   486         "pre- and postprocessing equations for code generator"
   487   end;
   488 
   489 val _ =
   490   OuterSyntax.improper_command "print_codeproc" "print code preprocessor setup"
   491   OuterKeyword.diag (Scan.succeed
   492       (Toplevel.no_timing o Toplevel.unknown_theory o Toplevel.keep
   493         (print_codeproc o Toplevel.theory_of)));
   494 
   495 end; (*struct*)