| author | blanchet | 
| Wed, 05 Jun 2013 13:19:26 +0200 | |
| changeset 52303 | 16d7708aba40 | 
| parent 51717 | 9e7d1c139569 | 
| child 52736 | 317663b422bb | 
| permissions | -rw-r--r-- | 
| 37744 | 1 | (* Title: Tools/Code/code_preproc.ML | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 2 | Author: Florian Haftmann, TU Muenchen | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 3 | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 4 | Preprocessing code equations into a well-sorted system | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 5 | in a graph with explicit dependencies. | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 6 | *) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 7 | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 8 | signature CODE_PREPROC = | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 9 | sig | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51685diff
changeset | 10 | val map_pre: (Proof.context -> Proof.context) -> theory -> theory | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51685diff
changeset | 11 | val map_post: (Proof.context -> Proof.context) -> theory -> theory | 
| 32072 | 12 | val add_unfold: thm -> theory -> theory | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 13 | val add_functrans: string * (theory -> (thm * bool) list -> (thm * bool) list option) -> theory -> theory | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 14 | val del_functrans: string -> theory -> theory | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 15 | val simple_functrans: (theory -> thm list -> thm list option) | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 16 | -> theory -> (thm * bool) list -> (thm * bool) list option | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 17 | val print_codeproc: theory -> unit | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 18 | |
| 30947 | 19 | type code_algebra | 
| 20 | type code_graph | |
| 34891 
99b9a6290446
code certificates as integral part of code generation
 haftmann parents: 
34251diff
changeset | 21 | val cert: code_graph -> string -> Code.cert | 
| 32873 | 22 | val sortargs: code_graph -> string -> sort list | 
| 30947 | 23 | val all: code_graph -> string list | 
| 24 | val pretty: theory -> code_graph -> Pretty.T | |
| 39398 
2e30660a2e21
ignore code cache optionally; corrected scope of term value in static_eval_conv
 haftmann parents: 
39133diff
changeset | 25 | val obtain: bool -> theory -> string list -> term list -> code_algebra * code_graph | 
| 41184 | 26 | val dynamic_conv: theory | 
| 38670 | 27 | -> (code_algebra -> code_graph -> (string * sort) list -> term -> conv) -> conv | 
| 41184 | 28 | val dynamic_value: theory -> ((term -> term) -> 'a -> 'a) | 
| 30947 | 29 | -> (code_algebra -> code_graph -> (string * sort) list -> term -> 'a) -> term -> 'a | 
| 41184 | 30 | val static_conv: theory -> string list | 
| 41346 | 31 | -> (code_algebra -> code_graph -> (string * sort) list -> term -> conv) -> conv | 
| 41184 | 32 | val static_value: theory -> ((term -> term) -> 'a -> 'a) -> string list | 
| 41346 | 33 | -> (code_algebra -> code_graph -> (string * sort) list -> term -> 'a) -> term -> 'a | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 34 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 35 | val setup: theory -> theory | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 36 | end | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 37 | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 38 | structure Code_Preproc : CODE_PREPROC = | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 39 | struct | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 40 | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 41 | (** preprocessor administration **) | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 42 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 43 | (* theory data *) | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 44 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 45 | datatype thmproc = Thmproc of {
 | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 46 | pre: simpset, | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 47 | post: simpset, | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 48 | functrans: (string * (serial * (theory -> (thm * bool) list -> (thm * bool) list option))) list | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 49 | }; | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 50 | |
| 31599 | 51 | fun make_thmproc ((pre, post), functrans) = | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 52 |   Thmproc { pre = pre, post = post, functrans = functrans };
 | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 53 | fun map_thmproc f (Thmproc { pre, post, functrans }) =
 | 
| 31599 | 54 | make_thmproc (f ((pre, post), functrans)); | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 55 | fun merge_thmproc (Thmproc { pre = pre1, post = post1, functrans = functrans1 },
 | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 56 |   Thmproc { pre = pre2, post = post2, functrans = functrans2 }) =
 | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 57 | let | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 58 | val pre = Simplifier.merge_ss (pre1, pre2); | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 59 | val post = Simplifier.merge_ss (post1, post2); | 
| 34891 
99b9a6290446
code certificates as integral part of code generation
 haftmann parents: 
34251diff
changeset | 60 | val functrans = AList.merge (op =) (eq_fst (op =)) (functrans1, functrans2) | 
| 
99b9a6290446
code certificates as integral part of code generation
 haftmann parents: 
34251diff
changeset | 61 |         handle AList.DUP => error ("Duplicate function transformer");
 | 
| 31599 | 62 | in make_thmproc ((pre, post), functrans) end; | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 63 | |
| 33522 | 64 | structure Code_Preproc_Data = Theory_Data | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 65 | ( | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 66 | type T = thmproc; | 
| 31599 | 67 | val empty = make_thmproc ((Simplifier.empty_ss, Simplifier.empty_ss), []); | 
| 33522 | 68 | val extend = I; | 
| 69 | val merge = merge_thmproc; | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 70 | ); | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 71 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 72 | fun the_thmproc thy = case Code_Preproc_Data.get thy | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 73 | of Thmproc x => x; | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 74 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 75 | fun delete_force msg key xs = | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 76 | if AList.defined (op =) xs key then AList.delete (op =) key xs | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 77 |   else error ("No such " ^ msg ^ ": " ^ quote key);
 | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 78 | |
| 38669 
9ff76d0f0610
refined and unified naming convention for dynamic code evaluation techniques
 haftmann parents: 
38291diff
changeset | 79 | val map_data = Code_Preproc_Data.map o map_thmproc; | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 80 | |
| 32072 | 81 | val map_pre_post = map_data o apfst; | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51685diff
changeset | 82 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51685diff
changeset | 83 | fun map_simpset which f thy = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51685diff
changeset | 84 | map_pre_post (which (simpset_map (Proof_Context.init_global thy) f)) thy; | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51685diff
changeset | 85 | val map_pre = map_simpset apfst; | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51685diff
changeset | 86 | val map_post = map_simpset apsnd; | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 87 | |
| 41226 | 88 | val add_unfold = map_pre o Simplifier.add_simp; | 
| 89 | val del_unfold = map_pre o Simplifier.del_simp; | |
| 90 | val add_post = map_post o Simplifier.add_simp; | |
| 91 | val del_post = map_post o Simplifier.del_simp; | |
| 32072 | 92 | |
| 46026 
83caa4f4bd56
semiring_numeral_0_eq_0, semiring_numeral_1_eq_1 now [simp], superseeding corresponding simp rules on type nat
 haftmann parents: 
45230diff
changeset | 93 | fun add_code_abbrev raw_thm thy = | 
| 32072 | 94 | let | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51685diff
changeset | 95 | val ctxt = Proof_Context.init_global thy; | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51685diff
changeset | 96 | val thm = Local_Defs.meta_rewrite_rule ctxt raw_thm; | 
| 32072 | 97 | val thm_sym = Thm.symmetric thm; | 
| 98 | in | |
| 99 | thy |> map_pre_post (fn (pre, post) => | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51685diff
changeset | 100 | (pre |> simpset_map ctxt (Simplifier.add_simp thm_sym), | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51685diff
changeset | 101 | post |> simpset_map ctxt (Simplifier.add_simp thm))) | 
| 32072 | 102 | end; | 
| 103 | ||
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 104 | fun add_functrans (name, f) = (map_data o apsnd) | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 105 | (AList.update (op =) (name, (serial (), f))); | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 106 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 107 | fun del_functrans name = (map_data o apsnd) | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 108 | (delete_force "function transformer" name); | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 109 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 110 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 111 | (* post- and preprocessing *) | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 112 | |
| 39604 
f17fb9ccb836
avoid frees and vars in terms to be evaluated by abstracting and applying
 haftmann parents: 
39475diff
changeset | 113 | fun no_variables_conv conv ct = | 
| 
f17fb9ccb836
avoid frees and vars in terms to be evaluated by abstracting and applying
 haftmann parents: 
39475diff
changeset | 114 | let | 
| 
f17fb9ccb836
avoid frees and vars in terms to be evaluated by abstracting and applying
 haftmann parents: 
39475diff
changeset | 115 | val cert = Thm.cterm_of (Thm.theory_of_cterm ct); | 
| 
f17fb9ccb836
avoid frees and vars in terms to be evaluated by abstracting and applying
 haftmann parents: 
39475diff
changeset | 116 | val all_vars = fold_aterms (fn t as Free _ => insert (op aconvc) (cert t) | 
| 
f17fb9ccb836
avoid frees and vars in terms to be evaluated by abstracting and applying
 haftmann parents: 
39475diff
changeset | 117 | | t as Var _ => insert (op aconvc) (cert t) | 
| 
f17fb9ccb836
avoid frees and vars in terms to be evaluated by abstracting and applying
 haftmann parents: 
39475diff
changeset | 118 | | _ => I) (Thm.term_of ct) []; | 
| 
f17fb9ccb836
avoid frees and vars in terms to be evaluated by abstracting and applying
 haftmann parents: 
39475diff
changeset | 119 | fun apply_beta var thm = Thm.combination thm (Thm.reflexive var) | 
| 
f17fb9ccb836
avoid frees and vars in terms to be evaluated by abstracting and applying
 haftmann parents: 
39475diff
changeset | 120 | |> Conv.fconv_rule (Conv.arg_conv (Conv.try_conv (Thm.beta_conversion false))) | 
| 
f17fb9ccb836
avoid frees and vars in terms to be evaluated by abstracting and applying
 haftmann parents: 
39475diff
changeset | 121 | |> Conv.fconv_rule (Conv.arg1_conv (Thm.beta_conversion false)); | 
| 
f17fb9ccb836
avoid frees and vars in terms to be evaluated by abstracting and applying
 haftmann parents: 
39475diff
changeset | 122 | in | 
| 
f17fb9ccb836
avoid frees and vars in terms to be evaluated by abstracting and applying
 haftmann parents: 
39475diff
changeset | 123 | ct | 
| 46497 
89ccf66aa73d
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
 wenzelm parents: 
46026diff
changeset | 124 | |> fold_rev Thm.lambda all_vars | 
| 39604 
f17fb9ccb836
avoid frees and vars in terms to be evaluated by abstracting and applying
 haftmann parents: 
39475diff
changeset | 125 | |> conv | 
| 
f17fb9ccb836
avoid frees and vars in terms to be evaluated by abstracting and applying
 haftmann parents: 
39475diff
changeset | 126 | |> fold apply_beta all_vars | 
| 
f17fb9ccb836
avoid frees and vars in terms to be evaluated by abstracting and applying
 haftmann parents: 
39475diff
changeset | 127 | end; | 
| 
f17fb9ccb836
avoid frees and vars in terms to be evaluated by abstracting and applying
 haftmann parents: 
39475diff
changeset | 128 | |
| 33942 | 129 | fun trans_conv_rule conv thm = Thm.transitive thm ((conv o Thm.rhs_of) thm); | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 130 | |
| 41189 | 131 | fun term_of_conv thy conv = | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 132 | Thm.cterm_of thy | 
| 41189 | 133 | #> conv | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 134 | #> Thm.prop_of | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 135 | #> Logic.dest_equals | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 136 | #> snd; | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 137 | |
| 41189 | 138 | fun term_of_conv_resubst thy conv t = | 
| 139 | let | |
| 140 | val all_vars = fold_aterms (fn t as Free _ => insert (op aconv) t | |
| 141 | | t as Var _ => insert (op aconv) t | |
| 142 | | _ => I) t []; | |
| 143 | val resubst = curry (Term.betapplys o swap) all_vars; | |
| 144 | in (resubst, term_of_conv thy conv (fold_rev lambda all_vars t)) end; | |
| 145 | ||
| 146 | fun preprocess_conv thy = | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 147 | let | 
| 35232 
f588e1169c8b
renamed Simplifier.theory_context to Simplifier.global_context to emphasize that this is not the real thing;
 wenzelm parents: 
34895diff
changeset | 148 | val pre = (Simplifier.global_context thy o #pre o the_thmproc) thy; | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 149 | in | 
| 41189 | 150 | Simplifier.rewrite pre | 
| 51685 
385ef6706252
more standard module name Axclass (according to file name);
 wenzelm parents: 
51658diff
changeset | 151 | #> trans_conv_rule (Axclass.unoverload_conv thy) | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 152 | end; | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 153 | |
| 41189 | 154 | fun preprocess_term thy = term_of_conv_resubst thy (preprocess_conv thy); | 
| 39475 | 155 | |
| 41189 | 156 | fun postprocess_conv thy = | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 157 | let | 
| 35232 
f588e1169c8b
renamed Simplifier.theory_context to Simplifier.global_context to emphasize that this is not the real thing;
 wenzelm parents: 
34895diff
changeset | 158 | val post = (Simplifier.global_context thy o #post o the_thmproc) thy; | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 159 | in | 
| 51685 
385ef6706252
more standard module name Axclass (according to file name);
 wenzelm parents: 
51658diff
changeset | 160 | Axclass.overload_conv thy | 
| 41189 | 161 | #> trans_conv_rule (Simplifier.rewrite post) | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 162 | end; | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 163 | |
| 32544 | 164 | fun postprocess_term thy = term_of_conv thy (postprocess_conv thy); | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 165 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 166 | fun print_codeproc thy = | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 167 | let | 
| 42361 | 168 | val ctxt = Proof_Context.init_global thy; | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 169 | val pre = (#pre o the_thmproc) thy; | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 170 | val post = (#post o the_thmproc) thy; | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 171 | val functrans = (map fst o #functrans o the_thmproc) thy; | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 172 | in | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 173 | (Pretty.writeln o Pretty.chunks) [ | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 174 | Pretty.block [ | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 175 | Pretty.str "preprocessing simpset:", | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 176 | Pretty.fbrk, | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51685diff
changeset | 177 | Simplifier.pretty_simpset (put_simpset pre ctxt) | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 178 | ], | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 179 | Pretty.block [ | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 180 | Pretty.str "postprocessing simpset:", | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 181 | Pretty.fbrk, | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51685diff
changeset | 182 | Simplifier.pretty_simpset (put_simpset post ctxt) | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 183 | ], | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 184 | Pretty.block ( | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 185 | Pretty.str "function transformers:" | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 186 | :: Pretty.fbrk | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 187 | :: (Pretty.fbreaks o map Pretty.str) functrans | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 188 | ) | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 189 | ] | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 190 | end; | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 191 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 192 | fun simple_functrans f thy eqns = case f thy (map fst eqns) | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 193 | of SOME thms' => SOME (map (rpair (forall snd eqns)) thms') | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 194 | | NONE => NONE; | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 195 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 196 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 197 | (** sort algebra and code equation graph types **) | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 198 | |
| 30947 | 199 | type code_algebra = (sort -> sort) * Sorts.algebra; | 
| 34891 
99b9a6290446
code certificates as integral part of code generation
 haftmann parents: 
34251diff
changeset | 200 | type code_graph = ((string * sort) list * Code.cert) Graph.T; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 201 | |
| 39604 
f17fb9ccb836
avoid frees and vars in terms to be evaluated by abstracting and applying
 haftmann parents: 
39475diff
changeset | 202 | fun get_node eqngr const = Graph.get_node eqngr const | 
| 
f17fb9ccb836
avoid frees and vars in terms to be evaluated by abstracting and applying
 haftmann parents: 
39475diff
changeset | 203 |   handle Graph.UNDEF _ => error ("No such constant in code equation graph: " ^ quote const);
 | 
| 
f17fb9ccb836
avoid frees and vars in terms to be evaluated by abstracting and applying
 haftmann parents: 
39475diff
changeset | 204 | |
| 
f17fb9ccb836
avoid frees and vars in terms to be evaluated by abstracting and applying
 haftmann parents: 
39475diff
changeset | 205 | fun cert eqngr = snd o get_node eqngr; | 
| 
f17fb9ccb836
avoid frees and vars in terms to be evaluated by abstracting and applying
 haftmann parents: 
39475diff
changeset | 206 | fun sortargs eqngr = map snd o fst o get_node eqngr; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 207 | fun all eqngr = Graph.keys eqngr; | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 208 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 209 | fun pretty thy eqngr = | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 210 | AList.make (snd o Graph.get_node eqngr) (Graph.keys eqngr) | 
| 31156 | 211 | |> (map o apfst) (Code.string_of_const thy) | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 212 | |> sort (string_ord o pairself fst) | 
| 34895 | 213 | |> map (fn (s, cert) => (Pretty.block o Pretty.fbreaks) (Pretty.str s :: Code.pretty_cert thy cert)) | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 214 | |> Pretty.chunks; | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 215 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 216 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 217 | (** the Waisenhaus algorithm **) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 218 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 219 | (* auxiliary *) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 220 | |
| 51685 
385ef6706252
more standard module name Axclass (according to file name);
 wenzelm parents: 
51658diff
changeset | 221 | fun is_proper_class thy = can (Axclass.get_info thy); | 
| 30942 
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
 haftmann parents: 
30876diff
changeset | 222 | |
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 223 | fun complete_proper_sort thy = | 
| 30942 
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
 haftmann parents: 
30876diff
changeset | 224 | Sign.complete_sort thy #> filter (is_proper_class thy); | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 225 | |
| 30029 | 226 | fun inst_params thy tyco = | 
| 51685 
385ef6706252
more standard module name Axclass (according to file name);
 wenzelm parents: 
51658diff
changeset | 227 | map (fn (c, _) => Axclass.param_of_inst thy (c, tyco)) | 
| 
385ef6706252
more standard module name Axclass (according to file name);
 wenzelm parents: 
51658diff
changeset | 228 | o maps (#params o Axclass.get_info thy); | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 229 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 230 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 231 | (* data structures *) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 232 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 233 | datatype const = Fun of string | Inst of class * string; | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 234 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 235 | fun const_ord (Fun c1, Fun c2) = fast_string_ord (c1, c2) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 236 | | const_ord (Inst class_tyco1, Inst class_tyco2) = | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 237 | prod_ord fast_string_ord fast_string_ord (class_tyco1, class_tyco2) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 238 | | const_ord (Fun _, Inst _) = LESS | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 239 | | const_ord (Inst _, Fun _) = GREATER; | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 240 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 241 | type var = const * int; | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 242 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 243 | structure Vargraph = | 
| 31971 
8c1b845ed105
renamed functor TableFun to Table, and GraphFun to Graph;
 wenzelm parents: 
31962diff
changeset | 244 | Graph(type key = var val ord = prod_ord const_ord int_ord); | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 245 | |
| 30054 | 246 | datatype styp = Tyco of string * styp list | Var of var | Free; | 
| 247 | ||
| 248 | fun styp_of c_lhs (Type (tyco, tys)) = Tyco (tyco, map (styp_of c_lhs) tys) | |
| 249 | | styp_of c_lhs (TFree (v, _)) = case c_lhs | |
| 250 | of SOME (c, lhs) => Var (Fun c, find_index (fn (v', _) => v = v') lhs) | |
| 251 | | NONE => Free; | |
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 252 | |
| 30054 | 253 | type vardeps_data = ((string * styp list) list * class list) Vargraph.T | 
| 34891 
99b9a6290446
code certificates as integral part of code generation
 haftmann parents: 
34251diff
changeset | 254 | * (((string * sort) list * Code.cert) Symtab.table | 
| 30054 | 255 | * (class * string) list); | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 256 | |
| 30054 | 257 | val empty_vardeps_data : vardeps_data = | 
| 258 | (Vargraph.empty, (Symtab.empty, [])); | |
| 259 | ||
| 30876 | 260 | |
| 30054 | 261 | (* retrieving equations and instances from the background context *) | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 262 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 263 | fun obtain_eqns thy eqngr c = | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 264 | case try (Graph.get_node eqngr) c | 
| 34891 
99b9a6290446
code certificates as integral part of code generation
 haftmann parents: 
34251diff
changeset | 265 | of SOME (lhs, cert) => ((lhs, []), cert) | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 266 | | NONE => let | 
| 48075 
ec5e62b868eb
apply preprocessing simpset also to rhs of abstract code equations
 haftmann parents: 
47005diff
changeset | 267 | val functrans = (map (fn (_, (_, f)) => f thy) | 
| 
ec5e62b868eb
apply preprocessing simpset also to rhs of abstract code equations
 haftmann parents: 
47005diff
changeset | 268 | o #functrans o the_thmproc) thy; | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51685diff
changeset | 269 |         val {pre, ...} = the_thmproc thy;
 | 
| 48075 
ec5e62b868eb
apply preprocessing simpset also to rhs of abstract code equations
 haftmann parents: 
47005diff
changeset | 270 |         val cert = Code.get_cert thy { functrans = functrans, ss = pre } c;
 | 
| 35224 | 271 | val (lhs, rhss) = Code.typargs_deps_of_cert thy cert; | 
| 34891 
99b9a6290446
code certificates as integral part of code generation
 haftmann parents: 
34251diff
changeset | 272 | in ((lhs, rhss), cert) end; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 273 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 274 | fun obtain_instance thy arities (inst as (class, tyco)) = | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 275 | case AList.lookup (op =) arities inst | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 276 | of SOME classess => (classess, ([], [])) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 277 | | NONE => let | 
| 30029 | 278 | val all_classes = complete_proper_sort thy [class]; | 
| 37384 
5aba26803073
more consistent naming aroud type classes and instances
 haftmann parents: 
36960diff
changeset | 279 | val super_classes = remove (op =) class all_classes; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 280 | val classess = map (complete_proper_sort thy) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 281 | (Sign.arity_sorts thy tyco [class]); | 
| 30029 | 282 | val inst_params = inst_params thy tyco all_classes; | 
| 37384 
5aba26803073
more consistent naming aroud type classes and instances
 haftmann parents: 
36960diff
changeset | 283 | in (classess, (super_classes, inst_params)) end; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 284 | |
| 30054 | 285 | |
| 286 | (* computing instantiations *) | |
| 287 | ||
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 288 | fun add_classes thy arities eqngr c_k new_classes vardeps_data = | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 289 | let | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 290 | val (styps, old_classes) = Vargraph.get_node (fst vardeps_data) c_k; | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 291 | val diff_classes = new_classes |> subtract (op =) old_classes; | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 292 | in if null diff_classes then vardeps_data | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 293 | else let | 
| 44338 
700008399ee5
refined Graph implementation: more abstract/scalable Graph.Keys instead of plain lists -- order of adjacency is now standardized wrt. Key.ord;
 wenzelm parents: 
43122diff
changeset | 294 | val c_ks = Vargraph.immediate_succs (fst vardeps_data) c_k |> insert (op =) c_k; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 295 | in | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 296 | vardeps_data | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 297 | |> (apfst o Vargraph.map_node c_k o apsnd) (append diff_classes) | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 298 | |> fold (fn styp => fold (ensure_typmatch_inst thy arities eqngr styp) new_classes) styps | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 299 | |> fold (fn c_k => add_classes thy arities eqngr c_k diff_classes) c_ks | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 300 | end end | 
| 37384 
5aba26803073
more consistent naming aroud type classes and instances
 haftmann parents: 
36960diff
changeset | 301 | and add_styp thy arities eqngr c_k new_tyco_styps vardeps_data = | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 302 | let | 
| 37384 
5aba26803073
more consistent naming aroud type classes and instances
 haftmann parents: 
36960diff
changeset | 303 | val (old_tyco_stypss, classes) = Vargraph.get_node (fst vardeps_data) c_k; | 
| 
5aba26803073
more consistent naming aroud type classes and instances
 haftmann parents: 
36960diff
changeset | 304 | in if member (op =) old_tyco_stypss new_tyco_styps then vardeps_data | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 305 | else | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 306 | vardeps_data | 
| 37384 
5aba26803073
more consistent naming aroud type classes and instances
 haftmann parents: 
36960diff
changeset | 307 | |> (apfst o Vargraph.map_node c_k o apfst) (cons new_tyco_styps) | 
| 
5aba26803073
more consistent naming aroud type classes and instances
 haftmann parents: 
36960diff
changeset | 308 | |> fold (ensure_typmatch_inst thy arities eqngr new_tyco_styps) classes | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 309 | end | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 310 | and add_dep thy arities eqngr c_k c_k' vardeps_data = | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 311 | let | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 312 | val (_, classes) = Vargraph.get_node (fst vardeps_data) c_k; | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 313 | in | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 314 | vardeps_data | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 315 | |> add_classes thy arities eqngr c_k' classes | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 316 | |> apfst (Vargraph.add_edge (c_k, c_k')) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 317 | end | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 318 | and ensure_typmatch_inst thy arities eqngr (tyco, styps) class vardeps_data = | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 319 | if can (Sign.arity_sorts thy tyco) [class] | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 320 | then vardeps_data | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 321 | |> ensure_inst thy arities eqngr (class, tyco) | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 322 | |> fold_index (fn (k, styp) => | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 323 | ensure_typmatch thy arities eqngr styp (Inst (class, tyco), k)) styps | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 324 | else vardeps_data (*permissive!*) | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 325 | and ensure_inst thy arities eqngr (inst as (class, tyco)) (vardeps_data as (_, (_, insts))) = | 
| 30054 | 326 | if member (op =) insts inst then vardeps_data | 
| 327 | else let | |
| 37384 
5aba26803073
more consistent naming aroud type classes and instances
 haftmann parents: 
36960diff
changeset | 328 | val (classess, (super_classes, inst_params)) = | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 329 | obtain_instance thy arities inst; | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 330 | in | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 331 | vardeps_data | 
| 30054 | 332 | |> (apsnd o apsnd) (insert (op =) inst) | 
| 30083 | 333 | |> fold_index (fn (k, _) => | 
| 334 | apfst (Vargraph.new_node ((Inst (class, tyco), k), ([] ,[])))) classess | |
| 37384 
5aba26803073
more consistent naming aroud type classes and instances
 haftmann parents: 
36960diff
changeset | 335 | |> fold (fn super_class => ensure_inst thy arities eqngr (super_class, tyco)) super_classes | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 336 | |> fold (ensure_fun thy arities eqngr) inst_params | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 337 | |> fold_index (fn (k, classes) => | 
| 30075 | 338 | add_classes thy arities eqngr (Inst (class, tyco), k) classes | 
| 37384 
5aba26803073
more consistent naming aroud type classes and instances
 haftmann parents: 
36960diff
changeset | 339 | #> fold (fn super_class => | 
| 
5aba26803073
more consistent naming aroud type classes and instances
 haftmann parents: 
36960diff
changeset | 340 | add_dep thy arities eqngr (Inst (super_class, tyco), k) | 
| 
5aba26803073
more consistent naming aroud type classes and instances
 haftmann parents: 
36960diff
changeset | 341 | (Inst (class, tyco), k)) super_classes | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 342 | #> fold (fn inst_param => | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 343 | add_dep thy arities eqngr (Fun inst_param, k) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 344 | (Inst (class, tyco), k) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 345 | ) inst_params | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 346 | ) classess | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 347 | end | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 348 | and ensure_typmatch thy arities eqngr (Tyco tyco_styps) c_k vardeps_data = | 
| 30054 | 349 | vardeps_data | 
| 350 | |> add_styp thy arities eqngr c_k tyco_styps | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 351 | | ensure_typmatch thy arities eqngr (Var c_k') c_k vardeps_data = | 
| 30054 | 352 | vardeps_data | 
| 353 | |> add_dep thy arities eqngr c_k c_k' | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 354 | | ensure_typmatch thy arities eqngr Free c_k vardeps_data = | 
| 30054 | 355 | vardeps_data | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 356 | and ensure_rhs thy arities eqngr (c', styps) vardeps_data = | 
| 30054 | 357 | vardeps_data | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 358 | |> ensure_fun thy arities eqngr c' | 
| 30054 | 359 | |> fold_index (fn (k, styp) => | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 360 | ensure_typmatch thy arities eqngr styp (Fun c', k)) styps | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 361 | and ensure_fun thy arities eqngr c (vardeps_data as (_, (eqntab, _))) = | 
| 30054 | 362 | if Symtab.defined eqntab c then vardeps_data | 
| 363 | else let | |
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 364 | val ((lhs, rhss), eqns) = obtain_eqns thy eqngr c; | 
| 30054 | 365 | val rhss' = (map o apsnd o map) (styp_of (SOME (c, lhs))) rhss; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 366 | in | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 367 | vardeps_data | 
| 30054 | 368 | |> (apsnd o apfst) (Symtab.update_new (c, (lhs, eqns))) | 
| 30083 | 369 | |> fold_index (fn (k, _) => | 
| 370 | apfst (Vargraph.new_node ((Fun c, k), ([] ,[])))) lhs | |
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 371 | |> fold_index (fn (k, (_, sort)) => | 
| 30083 | 372 | add_classes thy arities eqngr (Fun c, k) (complete_proper_sort thy sort)) lhs | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 373 | |> fold (ensure_rhs thy arities eqngr) rhss' | 
| 30054 | 374 | end; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 375 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 376 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 377 | (* applying instantiations *) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 378 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 379 | fun dicts_of thy (proj_sort, algebra) (T, sort) = | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 380 | let | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 381 | fun class_relation (x, _) _ = x; | 
| 36102 
a51d1d154c71
of_sort_derivation: pass-through full type information -- following the version by krauss/schropp;
 wenzelm parents: 
35624diff
changeset | 382 | fun type_constructor (tyco, _) xs class = | 
| 30054 | 383 | inst_params thy tyco (Sorts.complete_sort algebra [class]) | 
| 384 | @ (maps o maps) fst xs; | |
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 385 | fun type_variable (TFree (_, sort)) = map (pair []) (proj_sort sort); | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 386 | in | 
| 32795 | 387 | flat (Sorts.of_sort_derivation algebra | 
| 36102 
a51d1d154c71
of_sort_derivation: pass-through full type information -- following the version by krauss/schropp;
 wenzelm parents: 
35624diff
changeset | 388 |       { class_relation = K class_relation, type_constructor = type_constructor,
 | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 389 | type_variable = type_variable } (T, proj_sort sort) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 390 | handle Sorts.CLASS_ERROR _ => [] (*permissive!*)) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 391 | end; | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 392 | |
| 30054 | 393 | fun add_arity thy vardeps (class, tyco) = | 
| 33063 | 394 | AList.default (op =) ((class, tyco), | 
| 34891 
99b9a6290446
code certificates as integral part of code generation
 haftmann parents: 
34251diff
changeset | 395 | map_range (fn k => (snd o Vargraph.get_node vardeps) (Inst (class, tyco), k)) | 
| 
99b9a6290446
code certificates as integral part of code generation
 haftmann parents: 
34251diff
changeset | 396 | (Sign.arity_number thy tyco)); | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 397 | |
| 34891 
99b9a6290446
code certificates as integral part of code generation
 haftmann parents: 
34251diff
changeset | 398 | fun add_cert thy vardeps (c, (proto_lhs, proto_cert)) (rhss, eqngr) = | 
| 30058 | 399 | if can (Graph.get_node eqngr) c then (rhss, eqngr) | 
| 400 | else let | |
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 401 | val lhs = map_index (fn (k, (v, _)) => | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 402 | (v, snd (Vargraph.get_node vardeps (Fun c, k)))) proto_lhs; | 
| 49971 
8b50286c36d3
close code theorems explicitly after preprocessing
 haftmann parents: 
48075diff
changeset | 403 | val cert = proto_cert | 
| 
8b50286c36d3
close code theorems explicitly after preprocessing
 haftmann parents: 
48075diff
changeset | 404 | |> Code.constrain_cert thy (map (Sign.minimize_sort thy o snd) lhs) | 
| 
8b50286c36d3
close code theorems explicitly after preprocessing
 haftmann parents: 
48075diff
changeset | 405 | |> Code.conclude_cert; | 
| 35224 | 406 | val (vs, rhss') = Code.typargs_deps_of_cert thy cert; | 
| 34891 
99b9a6290446
code certificates as integral part of code generation
 haftmann parents: 
34251diff
changeset | 407 | val eqngr' = Graph.new_node (c, (vs, cert)) eqngr; | 
| 30054 | 408 | in (map (pair c) rhss' @ rhss, eqngr') end; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 409 | |
| 32873 | 410 | fun extend_arities_eqngr thy cs ts (arities, (eqngr : code_graph)) = | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 411 | let | 
| 30942 
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
 haftmann parents: 
30876diff
changeset | 412 | val cs_rhss = (fold o fold_aterms) (fn Const (c_ty as (c, _)) => | 
| 
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
 haftmann parents: 
30876diff
changeset | 413 | insert (op =) (c, (map (styp_of NONE) o Sign.const_typargs thy) c_ty) | _ => I) ts []; | 
| 30054 | 414 | val (vardeps, (eqntab, insts)) = empty_vardeps_data | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 415 | |> fold (ensure_fun thy arities eqngr) cs | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 416 | |> fold (ensure_rhs thy arities eqngr) cs_rhss; | 
| 30054 | 417 | val arities' = fold (add_arity thy vardeps) insts arities; | 
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46961diff
changeset | 418 | val algebra = Sorts.subalgebra (Context.pretty_global thy) (is_proper_class thy) | 
| 30064 | 419 | (AList.lookup (op =) arities') (Sign.classes_of thy); | 
| 34891 
99b9a6290446
code certificates as integral part of code generation
 haftmann parents: 
34251diff
changeset | 420 | val (rhss, eqngr') = Symtab.fold (add_cert thy vardeps) eqntab ([], eqngr); | 
| 30942 
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
 haftmann parents: 
30876diff
changeset | 421 | fun deps_of (c, rhs) = c :: maps (dicts_of thy algebra) | 
| 32873 | 422 | (rhs ~~ sortargs eqngr' c); | 
| 30054 | 423 | val eqngr'' = fold (fn (c, rhs) => fold | 
| 424 | (curry Graph.add_edge c) (deps_of rhs)) rhss eqngr'; | |
| 30942 
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
 haftmann parents: 
30876diff
changeset | 425 | in (algebra, (arities', eqngr'')) end; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 426 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 427 | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 428 | (** store for preprocessed arities and code equations **) | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 429 | |
| 34173 
458ced35abb8
reduced code generator cache to the baremost minimum
 haftmann parents: 
33942diff
changeset | 430 | structure Wellsorted = Code_Data | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 431 | ( | 
| 30947 | 432 | type T = ((string * class) * sort list) list * code_graph; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 433 | val empty = ([], Graph.empty); | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 434 | ); | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 435 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 436 | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 437 | (** retrieval and evaluation interfaces **) | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 438 | |
| 39398 
2e30660a2e21
ignore code cache optionally; corrected scope of term value in static_eval_conv
 haftmann parents: 
39133diff
changeset | 439 | fun obtain ignore_cache thy consts ts = apsnd snd | 
| 
2e30660a2e21
ignore code cache optionally; corrected scope of term value in static_eval_conv
 haftmann parents: 
39133diff
changeset | 440 | (Wellsorted.change_yield (if ignore_cache then NONE else SOME thy) (extend_arities_eqngr thy consts ts)); | 
| 38670 | 441 | |
| 442 | fun dest_cterm ct = let val t = Thm.term_of ct in (Term.add_tfrees t [], t) end; | |
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 443 | |
| 41184 | 444 | fun dynamic_conv thy conv = no_variables_conv (fn ct => | 
| 30942 
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
 haftmann parents: 
30876diff
changeset | 445 | let | 
| 39475 | 446 | val thm1 = preprocess_conv thy ct; | 
| 447 | val ct' = Thm.rhs_of thm1; | |
| 38670 | 448 | val (vs', t') = dest_cterm ct'; | 
| 30947 | 449 | val consts = fold_aterms | 
| 450 | (fn Const (c, _) => insert (op =) c | _ => I) t' []; | |
| 39398 
2e30660a2e21
ignore code cache optionally; corrected scope of term value in static_eval_conv
 haftmann parents: 
39133diff
changeset | 451 | val (algebra', eqngr') = obtain false thy consts [t']; | 
| 39475 | 452 | val thm2 = conv algebra' eqngr' vs' t' ct'; | 
| 453 | val thm3 = postprocess_conv thy (Thm.rhs_of thm2); | |
| 454 | in | |
| 455 | Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ => | |
| 456 |       error ("could not construct evaluation proof:\n"
 | |
| 457 | ^ (cat_lines o map (Display.string_of_thm_global thy)) [thm1, thm2, thm3]) | |
| 39604 
f17fb9ccb836
avoid frees and vars in terms to be evaluated by abstracting and applying
 haftmann parents: 
39475diff
changeset | 458 | end); | 
| 30947 | 459 | |
| 41184 | 460 | fun dynamic_value thy postproc evaluator t = | 
| 30942 
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
 haftmann parents: 
30876diff
changeset | 461 | let | 
| 39604 
f17fb9ccb836
avoid frees and vars in terms to be evaluated by abstracting and applying
 haftmann parents: 
39475diff
changeset | 462 | val (resubst, t') = preprocess_term thy t; | 
| 39475 | 463 | val vs' = Term.add_tfrees t' []; | 
| 464 | val consts = fold_aterms | |
| 465 | (fn Const (c, _) => insert (op =) c | _ => I) t' []; | |
| 466 | val (algebra', eqngr') = obtain false thy consts [t']; | |
| 467 | in | |
| 40176 | 468 | t' | 
| 469 | |> evaluator algebra' eqngr' vs' | |
| 39604 
f17fb9ccb836
avoid frees and vars in terms to be evaluated by abstracting and applying
 haftmann parents: 
39475diff
changeset | 470 | |> postproc (postprocess_term thy o resubst) | 
| 39475 | 471 | end; | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 472 | |
| 41184 | 473 | fun static_conv thy consts conv = | 
| 38670 | 474 | let | 
| 39398 
2e30660a2e21
ignore code cache optionally; corrected scope of term value in static_eval_conv
 haftmann parents: 
39133diff
changeset | 475 | val (algebra, eqngr) = obtain true thy consts []; | 
| 41346 | 476 | val conv' = conv algebra eqngr; | 
| 39398 
2e30660a2e21
ignore code cache optionally; corrected scope of term value in static_eval_conv
 haftmann parents: 
39133diff
changeset | 477 | in | 
| 41189 | 478 | no_variables_conv ((preprocess_conv thy) | 
| 479 | then_conv (fn ct => uncurry conv' (dest_cterm ct) ct) | |
| 480 | then_conv (postprocess_conv thy)) | |
| 39398 
2e30660a2e21
ignore code cache optionally; corrected scope of term value in static_eval_conv
 haftmann parents: 
39133diff
changeset | 481 | end; | 
| 38670 | 482 | |
| 41184 | 483 | fun static_value thy postproc consts evaluator = | 
| 39475 | 484 | let | 
| 485 | val (algebra, eqngr) = obtain true thy consts []; | |
| 486 | val evaluator' = evaluator algebra eqngr; | |
| 41189 | 487 | in | 
| 488 | preprocess_term thy | |
| 489 | #-> (fn resubst => fn t => t | |
| 41346 | 490 | |> evaluator' (Term.add_tfrees t []) | 
| 41189 | 491 | |> postproc (postprocess_term thy o resubst)) | 
| 39475 | 492 | end; | 
| 493 | ||
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 494 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 495 | (** setup **) | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 496 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 497 | val setup = | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 498 | let | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 499 | fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I); | 
| 32072 | 500 | fun add_del_attribute_parser add del = | 
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31977diff
changeset | 501 | Attrib.add_del (mk_attribute add) (mk_attribute del); | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 502 | in | 
| 45189 
80cb73210612
removing declaration of code_unfold to address the old code generator
 bulwahn parents: 
44338diff
changeset | 503 |     Attrib.setup @{binding code_unfold} (add_del_attribute_parser add_unfold del_unfold)
 | 
| 
80cb73210612
removing declaration of code_unfold to address the old code generator
 bulwahn parents: 
44338diff
changeset | 504 | "preprocessing equations for code generator" | 
| 32072 | 505 |     #> Attrib.setup @{binding code_post} (add_del_attribute_parser add_post del_post)
 | 
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31977diff
changeset | 506 | "postprocessing equations for code generator" | 
| 46026 
83caa4f4bd56
semiring_numeral_0_eq_0, semiring_numeral_1_eq_1 now [simp], superseeding corresponding simp rules on type nat
 haftmann parents: 
45230diff
changeset | 507 |     #> Attrib.setup @{binding code_abbrev} (Scan.succeed (mk_attribute add_code_abbrev))
 | 
| 
83caa4f4bd56
semiring_numeral_0_eq_0, semiring_numeral_1_eq_1 now [simp], superseeding corresponding simp rules on type nat
 haftmann parents: 
45230diff
changeset | 508 | "post- and preprocessing equations for code generator" | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 509 | end; | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 510 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 511 | val _ = | 
| 46961 
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
 wenzelm parents: 
46497diff
changeset | 512 |   Outer_Syntax.improper_command @{command_spec "print_codeproc"} "print code preprocessor setup"
 | 
| 51658 
21c10672633b
discontinued Toplevel.no_timing complication -- also recovers timing of diagnostic commands, e.g. 'find_theorems';
 wenzelm parents: 
49971diff
changeset | 513 | (Scan.succeed (Toplevel.unknown_theory o | 
| 
21c10672633b
discontinued Toplevel.no_timing complication -- also recovers timing of diagnostic commands, e.g. 'find_theorems';
 wenzelm parents: 
49971diff
changeset | 514 | Toplevel.keep (print_codeproc o Toplevel.theory_of))); | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 515 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 516 | end; (*struct*) |