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