| author | haftmann | 
| Sun, 05 Oct 2014 20:30:57 +0200 | |
| changeset 58558 | 30cc7ee5ac10 | 
| parent 57430 | 020cea57eaa4 | 
| child 58893 | 9e0ecb66d6a7 | 
| 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 | 
| 55757 | 12 | val add_functrans: string * (Proof.context -> (thm * bool) list -> (thm * bool) list option) -> theory -> theory | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 13 | val del_functrans: string -> theory -> theory | 
| 55757 | 14 | val simple_functrans: (Proof.context -> thm list -> thm list option) | 
| 15 | -> Proof.context -> (thm * bool) list -> (thm * bool) list option | |
| 16 | val print_codeproc: Proof.context -> unit | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 17 | |
| 30947 | 18 | type code_algebra | 
| 19 | type code_graph | |
| 34891 
99b9a6290446
code certificates as integral part of code generation
 haftmann parents: 
34251diff
changeset | 20 | val cert: code_graph -> string -> Code.cert | 
| 32873 | 21 | val sortargs: code_graph -> string -> sort list | 
| 30947 | 22 | val all: code_graph -> string list | 
| 55757 | 23 | val pretty: Proof.context -> code_graph -> Pretty.T | 
| 39398 
2e30660a2e21
ignore code cache optionally; corrected scope of term value in static_eval_conv
 haftmann parents: 
39133diff
changeset | 24 | val obtain: bool -> theory -> string list -> term list -> code_algebra * code_graph | 
| 55757 | 25 | val dynamic_conv: Proof.context | 
| 56920 
d651b944c67e
normalizing of type variables before evaluation with explicit resubstitution function: make nbe work with funny type variables like \<AA>;
 haftmann parents: 
56334diff
changeset | 26 | -> (code_algebra -> code_graph -> term -> conv) -> conv | 
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 27 | val dynamic_value: Proof.context -> ((term -> term) -> 'a -> 'b) | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 28 | -> (code_algebra -> code_graph -> term -> 'a) -> term -> 'b | 
| 56973 
62da80041afd
syntactic means to prevent accidental mixup of static and dynamic context
 haftmann parents: 
56971diff
changeset | 29 |   val static_conv: { ctxt: Proof.context, consts: string list }
 | 
| 
62da80041afd
syntactic means to prevent accidental mixup of static and dynamic context
 haftmann parents: 
56971diff
changeset | 30 |     -> ({ algebra: code_algebra, eqngr: code_graph } -> Proof.context -> term -> conv)
 | 
| 55757 | 31 | -> Proof.context -> conv | 
| 56973 
62da80041afd
syntactic means to prevent accidental mixup of static and dynamic context
 haftmann parents: 
56971diff
changeset | 32 |   val static_value: { ctxt: Proof.context, lift_postproc: ((term -> term) -> 'a -> 'b), consts: string list }
 | 
| 
62da80041afd
syntactic means to prevent accidental mixup of static and dynamic context
 haftmann parents: 
56971diff
changeset | 33 |     -> ({ algebra: code_algebra, eqngr: code_graph } -> Proof.context -> term -> 'a)
 | 
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 34 | -> Proof.context -> term -> 'b | 
| 57430 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 35 | |
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 36 | val trace_none: Context.generic -> Context.generic | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 37 | val trace_all: Context.generic -> Context.generic | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 38 | val trace_only: string list -> Context.generic -> Context.generic | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 39 | val trace_only_ext: string list -> Context.generic -> Context.generic | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 40 | end | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 41 | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 42 | structure Code_Preproc : CODE_PREPROC = | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 43 | struct | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 44 | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 45 | (** preprocessor administration **) | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 46 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 47 | (* theory data *) | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 48 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 49 | datatype thmproc = Thmproc of {
 | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 50 | pre: simpset, | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 51 | post: simpset, | 
| 55757 | 52 | functrans: (string * (serial * (Proof.context -> (thm * bool) list -> (thm * bool) list option))) list | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 53 | }; | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 54 | |
| 31599 | 55 | fun make_thmproc ((pre, post), functrans) = | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 56 |   Thmproc { pre = pre, post = post, functrans = functrans };
 | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 57 | fun map_thmproc f (Thmproc { pre, post, functrans }) =
 | 
| 31599 | 58 | make_thmproc (f ((pre, post), functrans)); | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 59 | fun merge_thmproc (Thmproc { pre = pre1, post = post1, functrans = functrans1 },
 | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 60 |   Thmproc { pre = pre2, post = post2, functrans = functrans2 }) =
 | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 61 | let | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 62 | val pre = Simplifier.merge_ss (pre1, pre2); | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 63 | val post = Simplifier.merge_ss (post1, post2); | 
| 34891 
99b9a6290446
code certificates as integral part of code generation
 haftmann parents: 
34251diff
changeset | 64 | val functrans = AList.merge (op =) (eq_fst (op =)) (functrans1, functrans2) | 
| 
99b9a6290446
code certificates as integral part of code generation
 haftmann parents: 
34251diff
changeset | 65 |         handle AList.DUP => error ("Duplicate function transformer");
 | 
| 31599 | 66 | in make_thmproc ((pre, post), functrans) end; | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 67 | |
| 33522 | 68 | structure Code_Preproc_Data = Theory_Data | 
| 31125 
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 | type T = thmproc; | 
| 31599 | 71 | val empty = make_thmproc ((Simplifier.empty_ss, Simplifier.empty_ss), []); | 
| 33522 | 72 | val extend = I; | 
| 73 | val merge = merge_thmproc; | |
| 31125 
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 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 76 | fun the_thmproc thy = case Code_Preproc_Data.get thy | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 77 | of Thmproc x => x; | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 78 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 79 | fun delete_force msg key xs = | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 80 | if AList.defined (op =) xs key then AList.delete (op =) key xs | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 81 |   else error ("No such " ^ msg ^ ": " ^ quote key);
 | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 82 | |
| 38669 
9ff76d0f0610
refined and unified naming convention for dynamic code evaluation techniques
 haftmann parents: 
38291diff
changeset | 83 | val map_data = Code_Preproc_Data.map o map_thmproc; | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 84 | |
| 32072 | 85 | val map_pre_post = map_data o apfst; | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51685diff
changeset | 86 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51685diff
changeset | 87 | fun map_simpset which f thy = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51685diff
changeset | 88 | 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 | 89 | val map_pre = map_simpset apfst; | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51685diff
changeset | 90 | val map_post = map_simpset apsnd; | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 91 | |
| 56929 | 92 | fun process_unfold add_del = map_pre o add_del; | 
| 93 | fun process_post add_del = map_post o add_del; | |
| 32072 | 94 | |
| 56929 | 95 | fun process_abbrev add_del raw_thm thy = | 
| 32072 | 96 | let | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51685diff
changeset | 97 | val ctxt = Proof_Context.init_global thy; | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51685diff
changeset | 98 | val thm = Local_Defs.meta_rewrite_rule ctxt raw_thm; | 
| 32072 | 99 | val thm_sym = Thm.symmetric thm; | 
| 100 | in | |
| 101 | thy |> map_pre_post (fn (pre, post) => | |
| 56929 | 102 | (pre |> simpset_map ctxt (add_del thm_sym), | 
| 103 | post |> simpset_map ctxt (add_del thm))) | |
| 32072 | 104 | end; | 
| 105 | ||
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 106 | fun add_functrans (name, f) = (map_data o apsnd) | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 107 | (AList.update (op =) (name, (serial (), f))); | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 108 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 109 | fun del_functrans name = (map_data o apsnd) | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 110 | (delete_force "function transformer" name); | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 111 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 112 | |
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 113 | (* algebra of sandwiches: cterm transformations with pending postprocessors *) | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 114 | |
| 56976 | 115 | fun matches_transitive eq1 eq2 = Thm.rhs_of eq1 aconvc Thm.lhs_of eq2; | 
| 116 | ||
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 117 | fun trans_comb eq1 eq2 = | 
| 56976 | 118 | (*explicit assertions: evaluation conversion stacks are error-prone*) | 
| 119 |   if Thm.is_reflexive eq1 then (@{assert} (matches_transitive eq1 eq2); eq2)
 | |
| 120 |   else if Thm.is_reflexive eq2 then (@{assert} (matches_transitive eq1 eq2); eq1)
 | |
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 121 | else Thm.transitive eq1 eq2; | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 122 | |
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 123 | fun trans_conv_rule conv eq = trans_comb eq (conv (Thm.rhs_of eq)); | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 124 | |
| 56976 | 125 | structure Sandwich : sig | 
| 126 | type T = Proof.context -> cterm -> (thm -> thm) * cterm; | |
| 127 | val chain: T -> T -> T | |
| 128 | val lift: (Proof.context -> cterm -> (cterm -> thm) * thm) -> T | |
| 129 | val conversion: T -> (Proof.context -> term -> conv) -> Proof.context -> conv; | |
| 130 | val evaluation: T -> ((term -> term) -> 'a -> 'b) -> | |
| 131 | (Proof.context -> term -> 'a) -> Proof.context -> term -> 'b; | |
| 132 | end = struct | |
| 133 | ||
| 134 | type T = Proof.context -> cterm -> (thm -> thm) * cterm; | |
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 135 | |
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 136 | fun chain sandwich2 sandwich1 ctxt = | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 137 | sandwich1 ctxt | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 138 | ##>> sandwich2 ctxt | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 139 | #>> (op o); | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 140 | |
| 56976 | 141 | fun lift conv_sandwhich ctxt ct = | 
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 142 | let | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 143 | val (postproc_conv, eq) = conv_sandwhich ctxt ct; | 
| 56976 | 144 | fun potentail_trans_comb eq1 eq2 = | 
| 145 | if matches_transitive eq1 eq2 then trans_comb eq1 eq2 else eq2; | |
| 146 | (*weakened protocol for plain term evaluation*) | |
| 147 | in (trans_conv_rule postproc_conv o potentail_trans_comb eq, Thm.rhs_of eq) end; | |
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 148 | |
| 56976 | 149 | fun conversion sandwich conv ctxt ct = | 
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 150 | let | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 151 | val (postproc, ct') = sandwich ctxt ct; | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 152 | in postproc (conv ctxt (term_of ct') ct') end; | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 153 | |
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 154 | fun evaluation sandwich lift_postproc eval ctxt t = | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 155 | let | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 156 | val cert = Thm.cterm_of (Proof_Context.theory_of ctxt); | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 157 | val (postproc, ct') = sandwich ctxt (cert t); | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 158 | in | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 159 | term_of ct' | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 160 | |> eval ctxt | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 161 | |> lift_postproc (term_of o Thm.rhs_of o postproc o Thm.reflexive o cert) | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 162 | end; | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 163 | |
| 56976 | 164 | end; | 
| 165 | ||
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 166 | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 167 | (* post- and preprocessing *) | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 168 | |
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 169 | fun normalized_tfrees_sandwich ctxt ct = | 
| 56967 
c3746e999805
normalize type variables of evaluation term by conversion
 haftmann parents: 
56929diff
changeset | 170 | let | 
| 
c3746e999805
normalize type variables of evaluation term by conversion
 haftmann parents: 
56929diff
changeset | 171 | val cert = cterm_of (Proof_Context.theory_of ctxt); | 
| 
c3746e999805
normalize type variables of evaluation term by conversion
 haftmann parents: 
56929diff
changeset | 172 | val t = term_of ct; | 
| 
c3746e999805
normalize type variables of evaluation term by conversion
 haftmann parents: 
56929diff
changeset | 173 | val vs_original = fold_term_types (K (fold_atyps (insert (eq_fst op =) | 
| 
c3746e999805
normalize type variables of evaluation term by conversion
 haftmann parents: 
56929diff
changeset | 174 | o dest_TFree))) t []; | 
| 
c3746e999805
normalize type variables of evaluation term by conversion
 haftmann parents: 
56929diff
changeset | 175 | val vs_normalized = Name.invent_names Name.context Name.aT (map snd vs_original); | 
| 
c3746e999805
normalize type variables of evaluation term by conversion
 haftmann parents: 
56929diff
changeset | 176 | val normalize = map_type_tfree (TFree o the o AList.lookup (op =) (vs_original ~~ vs_normalized)); | 
| 
c3746e999805
normalize type variables of evaluation term by conversion
 haftmann parents: 
56929diff
changeset | 177 | val normalization = | 
| 
c3746e999805
normalize type variables of evaluation term by conversion
 haftmann parents: 
56929diff
changeset | 178 | map2 (fn (v, sort) => fn (v', _) => (((v', 0), sort), TFree (v, sort))) vs_original vs_normalized; | 
| 
c3746e999805
normalize type variables of evaluation term by conversion
 haftmann parents: 
56929diff
changeset | 179 | in | 
| 56971 | 180 | if eq_list (eq_fst (op =)) (vs_normalized, vs_original) | 
| 181 | then (I, ct) | |
| 182 | else (Thm.certify_instantiate (normalization, []) o Thm.varifyT_global, cert (map_types normalize t)) | |
| 56967 
c3746e999805
normalize type variables of evaluation term by conversion
 haftmann parents: 
56929diff
changeset | 183 | end; | 
| 
c3746e999805
normalize type variables of evaluation term by conversion
 haftmann parents: 
56929diff
changeset | 184 | |
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 185 | fun no_variables_sandwich ctxt ct = | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 186 | let | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 187 | val thy = Proof_Context.theory_of ctxt; | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 188 | val cert = Thm.cterm_of thy; | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 189 | val all_vars = fold_aterms (fn t as Free _ => insert (op aconvc) (cert t) | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 190 | | t as Var _ => insert (op aconvc) (cert t) | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 191 | | _ => I) (Thm.term_of ct) []; | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 192 | fun apply_beta var thm = Thm.combination thm (Thm.reflexive var) | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 193 | |> Conv.fconv_rule (Conv.arg_conv (Conv.try_conv (Thm.beta_conversion false))) | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 194 | |> Conv.fconv_rule (Conv.arg1_conv (Thm.beta_conversion false)); | 
| 56971 | 195 | in | 
| 196 | if null all_vars | |
| 197 | then (I, ct) | |
| 198 | else (fold apply_beta all_vars, fold_rev Thm.lambda all_vars ct) | |
| 199 | end; | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 200 | |
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 201 | fun simplifier_conv_sandwich ctxt = | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 202 | let | 
| 55757 | 203 | val thy = Proof_Context.theory_of ctxt; | 
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 204 | val pre = (#pre o the_thmproc) thy; | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 205 | val post = (#post o the_thmproc) thy; | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 206 | fun pre_conv ctxt' = | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 207 | Simplifier.rewrite (put_simpset pre ctxt') | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 208 | #> trans_conv_rule (Axclass.unoverload_conv (Proof_Context.theory_of ctxt')) | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 209 | fun post_conv ctxt' = | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 210 | Axclass.overload_conv (Proof_Context.theory_of ctxt') | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 211 | #> trans_conv_rule (Simplifier.rewrite (put_simpset post ctxt')) | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 212 | in fn ctxt' => pre_conv ctxt' #> pair (post_conv ctxt') end; | 
| 55757 | 213 | |
| 56976 | 214 | fun simplifier_sandwich ctxt = Sandwich.lift (simplifier_conv_sandwich ctxt); | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 215 | |
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 216 | fun value_sandwich ctxt = | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 217 | normalized_tfrees_sandwich | 
| 56976 | 218 | |> Sandwich.chain no_variables_sandwich | 
| 219 | |> Sandwich.chain (simplifier_sandwich ctxt); | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 220 | |
| 55757 | 221 | fun print_codeproc ctxt = | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 222 | let | 
| 55757 | 223 | val thy = Proof_Context.theory_of ctxt; | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 224 | val pre = (#pre o the_thmproc) thy; | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 225 | val post = (#post o the_thmproc) thy; | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 226 | val functrans = (map fst o #functrans o the_thmproc) thy; | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 227 | in | 
| 56334 
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
 wenzelm parents: 
55757diff
changeset | 228 | Pretty.writeln_chunks [ | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 229 | Pretty.block [ | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 230 | Pretty.str "preprocessing simpset:", | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 231 | Pretty.fbrk, | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51685diff
changeset | 232 | Simplifier.pretty_simpset (put_simpset pre ctxt) | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 233 | ], | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 234 | Pretty.block [ | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 235 | Pretty.str "postprocessing simpset:", | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 236 | Pretty.fbrk, | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51685diff
changeset | 237 | Simplifier.pretty_simpset (put_simpset post ctxt) | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 238 | ], | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 239 | Pretty.block ( | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 240 | Pretty.str "function transformers:" | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 241 | :: Pretty.fbrk | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 242 | :: (Pretty.fbreaks o map Pretty.str) functrans | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 243 | ) | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 244 | ] | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 245 | end; | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 246 | |
| 55757 | 247 | fun simple_functrans f ctxt eqns = case f ctxt (map fst eqns) | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 248 | of SOME thms' => SOME (map (rpair (forall snd eqns)) thms') | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 249 | | NONE => NONE; | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 250 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 251 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 252 | (** sort algebra and code equation graph types **) | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 253 | |
| 30947 | 254 | type code_algebra = (sort -> sort) * Sorts.algebra; | 
| 34891 
99b9a6290446
code certificates as integral part of code generation
 haftmann parents: 
34251diff
changeset | 255 | type code_graph = ((string * sort) list * Code.cert) Graph.T; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 256 | |
| 39604 
f17fb9ccb836
avoid frees and vars in terms to be evaluated by abstracting and applying
 haftmann parents: 
39475diff
changeset | 257 | 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 | 258 |   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 | 259 | |
| 
f17fb9ccb836
avoid frees and vars in terms to be evaluated by abstracting and applying
 haftmann parents: 
39475diff
changeset | 260 | 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 | 261 | fun sortargs eqngr = map snd o fst o get_node eqngr; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 262 | fun all eqngr = Graph.keys eqngr; | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 263 | |
| 55757 | 264 | fun pretty ctxt eqngr = | 
| 265 | let | |
| 266 | val thy = Proof_Context.theory_of ctxt; | |
| 267 | in | |
| 268 | AList.make (snd o Graph.get_node eqngr) (Graph.keys eqngr) | |
| 269 | |> (map o apfst) (Code.string_of_const thy) | |
| 270 | |> sort (string_ord o pairself fst) | |
| 271 | |> map (fn (s, cert) => (Pretty.block o Pretty.fbreaks) (Pretty.str s :: Code.pretty_cert thy cert)) | |
| 272 | |> Pretty.chunks | |
| 273 | end; | |
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 274 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 275 | |
| 57430 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 276 | (** simplifier tracing **) | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 277 | |
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 278 | structure Trace_Switch = Generic_Data | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 279 | ( | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 280 | type T = string list option; | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 281 | val empty = SOME []; | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 282 | val extend = I; | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 283 | fun merge (NONE, d2) = NONE | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 284 | | merge (d1, NONE) = NONE | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 285 | | merge (SOME cs1, SOME cs2) = SOME (Library.merge (op =) (cs1, cs2)); | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 286 | ); | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 287 | |
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 288 | val trace_none = Trace_Switch.put (SOME []); | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 289 | |
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 290 | val trace_all = Trace_Switch.put NONE; | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 291 | |
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 292 | fun gen_trace_only prep_const raw_cs context = | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 293 | let | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 294 | val cs = map (prep_const (Context.theory_of context)) raw_cs; | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 295 | in Trace_Switch.put (SOME cs) context end; | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 296 | |
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 297 | val trace_only = gen_trace_only (K I); | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 298 | val trace_only_ext = gen_trace_only Code.read_const; | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 299 | |
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 300 | fun switch_trace c ctxt = | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 301 | let | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 302 | val d = Trace_Switch.get (Context.Proof ctxt); | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 303 | val switch = case d of NONE => true | SOME cs => member (op =) cs c; | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 304 | val _ = if switch | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 305 |       then tracing ("Preprocessing function equations for "
 | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 306 | ^ Code.string_of_const (Proof_Context.theory_of ctxt) c) | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 307 | else (); | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 308 | in Config.put simp_trace switch ctxt end; | 
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 309 | |
| 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 310 | |
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 311 | (** the Waisenhaus algorithm **) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 312 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 313 | (* auxiliary *) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 314 | |
| 51685 
385ef6706252
more standard module name Axclass (according to file name);
 wenzelm parents: 
51658diff
changeset | 315 | 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 | 316 | |
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 317 | fun complete_proper_sort thy = | 
| 30942 
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
 haftmann parents: 
30876diff
changeset | 318 | Sign.complete_sort thy #> filter (is_proper_class thy); | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 319 | |
| 30029 | 320 | fun inst_params thy tyco = | 
| 51685 
385ef6706252
more standard module name Axclass (according to file name);
 wenzelm parents: 
51658diff
changeset | 321 | map (fn (c, _) => Axclass.param_of_inst thy (c, tyco)) | 
| 
385ef6706252
more standard module name Axclass (according to file name);
 wenzelm parents: 
51658diff
changeset | 322 | o maps (#params o Axclass.get_info thy); | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 323 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 324 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 325 | (* data structures *) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 326 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 327 | datatype const = Fun of string | Inst of class * string; | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 328 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 329 | fun const_ord (Fun c1, Fun c2) = fast_string_ord (c1, c2) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 330 | | const_ord (Inst class_tyco1, Inst class_tyco2) = | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 331 | prod_ord fast_string_ord fast_string_ord (class_tyco1, class_tyco2) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 332 | | const_ord (Fun _, Inst _) = LESS | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 333 | | const_ord (Inst _, Fun _) = GREATER; | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 334 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 335 | type var = const * int; | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 336 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 337 | structure Vargraph = | 
| 31971 
8c1b845ed105
renamed functor TableFun to Table, and GraphFun to Graph;
 wenzelm parents: 
31962diff
changeset | 338 | 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 | 339 | |
| 30054 | 340 | datatype styp = Tyco of string * styp list | Var of var | Free; | 
| 341 | ||
| 342 | fun styp_of c_lhs (Type (tyco, tys)) = Tyco (tyco, map (styp_of c_lhs) tys) | |
| 343 | | styp_of c_lhs (TFree (v, _)) = case c_lhs | |
| 344 | of SOME (c, lhs) => Var (Fun c, find_index (fn (v', _) => v = v') lhs) | |
| 345 | | NONE => Free; | |
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 346 | |
| 30054 | 347 | 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 | 348 | * (((string * sort) list * Code.cert) Symtab.table | 
| 30054 | 349 | * (class * string) list); | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 350 | |
| 30054 | 351 | val empty_vardeps_data : vardeps_data = | 
| 352 | (Vargraph.empty, (Symtab.empty, [])); | |
| 353 | ||
| 30876 | 354 | |
| 30054 | 355 | (* retrieving equations and instances from the background context *) | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 356 | |
| 55364 | 357 | fun obtain_eqns ctxt eqngr c = | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 358 | case try (Graph.get_node eqngr) c | 
| 34891 
99b9a6290446
code certificates as integral part of code generation
 haftmann parents: 
34251diff
changeset | 359 | of SOME (lhs, cert) => ((lhs, []), cert) | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 360 | | NONE => let | 
| 55364 | 361 | val thy = Proof_Context.theory_of ctxt; | 
| 55757 | 362 | val functrans = (map (fn (_, (_, f)) => f ctxt) | 
| 48075 
ec5e62b868eb
apply preprocessing simpset also to rhs of abstract code equations
 haftmann parents: 
47005diff
changeset | 363 | o #functrans o the_thmproc) thy; | 
| 57430 
020cea57eaa4
tracing facilities for the code generator preprocessor
 haftmann parents: 
57429diff
changeset | 364 | val cert = Code.get_cert (switch_trace c ctxt) functrans c; | 
| 55364 | 365 | val (lhs, rhss) = | 
| 366 | Code.typargs_deps_of_cert thy cert; | |
| 34891 
99b9a6290446
code certificates as integral part of code generation
 haftmann parents: 
34251diff
changeset | 367 | in ((lhs, rhss), cert) end; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 368 | |
| 55757 | 369 | fun obtain_instance ctxt arities (inst as (class, tyco)) = | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 370 | case AList.lookup (op =) arities inst | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 371 | of SOME classess => (classess, ([], [])) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 372 | | NONE => let | 
| 55757 | 373 | val thy = Proof_Context.theory_of ctxt; | 
| 30029 | 374 | val all_classes = complete_proper_sort thy [class]; | 
| 37384 
5aba26803073
more consistent naming aroud type classes and instances
 haftmann parents: 
36960diff
changeset | 375 | val super_classes = remove (op =) class all_classes; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 376 | val classess = map (complete_proper_sort thy) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 377 | (Sign.arity_sorts thy tyco [class]); | 
| 30029 | 378 | val inst_params = inst_params thy tyco all_classes; | 
| 37384 
5aba26803073
more consistent naming aroud type classes and instances
 haftmann parents: 
36960diff
changeset | 379 | in (classess, (super_classes, inst_params)) end; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 380 | |
| 30054 | 381 | |
| 382 | (* computing instantiations *) | |
| 383 | ||
| 55364 | 384 | fun add_classes ctxt arities eqngr c_k new_classes vardeps_data = | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 385 | let | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 386 | val (styps, old_classes) = Vargraph.get_node (fst vardeps_data) c_k; | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 387 | val diff_classes = new_classes |> subtract (op =) old_classes; | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 388 | in if null diff_classes then vardeps_data | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 389 | 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 | 390 | 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 | 391 | in | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 392 | vardeps_data | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 393 | |> (apfst o Vargraph.map_node c_k o apsnd) (append diff_classes) | 
| 55364 | 394 | |> fold (fn styp => fold (ensure_typmatch_inst ctxt arities eqngr styp) new_classes) styps | 
| 395 | |> fold (fn c_k => add_classes ctxt arities eqngr c_k diff_classes) c_ks | |
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 396 | end end | 
| 55364 | 397 | and add_styp ctxt arities eqngr c_k new_tyco_styps vardeps_data = | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 398 | let | 
| 37384 
5aba26803073
more consistent naming aroud type classes and instances
 haftmann parents: 
36960diff
changeset | 399 | 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 | 400 | 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 | 401 | else | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 402 | vardeps_data | 
| 37384 
5aba26803073
more consistent naming aroud type classes and instances
 haftmann parents: 
36960diff
changeset | 403 | |> (apfst o Vargraph.map_node c_k o apfst) (cons new_tyco_styps) | 
| 55364 | 404 | |> fold (ensure_typmatch_inst ctxt arities eqngr new_tyco_styps) classes | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 405 | end | 
| 55364 | 406 | and add_dep ctxt arities eqngr c_k c_k' vardeps_data = | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 407 | let | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 408 | val (_, classes) = Vargraph.get_node (fst vardeps_data) c_k; | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 409 | in | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 410 | vardeps_data | 
| 55364 | 411 | |> add_classes ctxt arities eqngr c_k' classes | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 412 | |> apfst (Vargraph.add_edge (c_k, c_k')) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 413 | end | 
| 55364 | 414 | and ensure_typmatch_inst ctxt arities eqngr (tyco, styps) class vardeps_data = | 
| 415 | if can (Sign.arity_sorts (Proof_Context.theory_of ctxt) tyco) [class] | |
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 416 | then vardeps_data | 
| 55364 | 417 | |> ensure_inst ctxt arities eqngr (class, tyco) | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 418 | |> fold_index (fn (k, styp) => | 
| 55364 | 419 | ensure_typmatch ctxt arities eqngr styp (Inst (class, tyco), k)) styps | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 420 | else vardeps_data (*permissive!*) | 
| 55364 | 421 | and ensure_inst ctxt arities eqngr (inst as (class, tyco)) (vardeps_data as (_, (_, insts))) = | 
| 30054 | 422 | if member (op =) insts inst then vardeps_data | 
| 423 | else let | |
| 37384 
5aba26803073
more consistent naming aroud type classes and instances
 haftmann parents: 
36960diff
changeset | 424 | val (classess, (super_classes, inst_params)) = | 
| 55757 | 425 | obtain_instance ctxt arities inst; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 426 | in | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 427 | vardeps_data | 
| 30054 | 428 | |> (apsnd o apsnd) (insert (op =) inst) | 
| 30083 | 429 | |> fold_index (fn (k, _) => | 
| 430 | apfst (Vargraph.new_node ((Inst (class, tyco), k), ([] ,[])))) classess | |
| 55364 | 431 | |> fold (fn super_class => ensure_inst ctxt arities eqngr (super_class, tyco)) super_classes | 
| 432 | |> fold (ensure_fun ctxt arities eqngr) inst_params | |
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 433 | |> fold_index (fn (k, classes) => | 
| 55364 | 434 | add_classes ctxt arities eqngr (Inst (class, tyco), k) classes | 
| 37384 
5aba26803073
more consistent naming aroud type classes and instances
 haftmann parents: 
36960diff
changeset | 435 | #> fold (fn super_class => | 
| 55364 | 436 | add_dep ctxt arities eqngr (Inst (super_class, tyco), k) | 
| 37384 
5aba26803073
more consistent naming aroud type classes and instances
 haftmann parents: 
36960diff
changeset | 437 | (Inst (class, tyco), k)) super_classes | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 438 | #> fold (fn inst_param => | 
| 55364 | 439 | add_dep ctxt arities eqngr (Fun inst_param, k) | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 440 | (Inst (class, tyco), k) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 441 | ) inst_params | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 442 | ) classess | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 443 | end | 
| 55364 | 444 | and ensure_typmatch ctxt arities eqngr (Tyco tyco_styps) c_k vardeps_data = | 
| 30054 | 445 | vardeps_data | 
| 55364 | 446 | |> add_styp ctxt arities eqngr c_k tyco_styps | 
| 447 | | ensure_typmatch ctxt arities eqngr (Var c_k') c_k vardeps_data = | |
| 30054 | 448 | vardeps_data | 
| 55364 | 449 | |> add_dep ctxt arities eqngr c_k c_k' | 
| 450 | | ensure_typmatch ctxt arities eqngr Free c_k vardeps_data = | |
| 30054 | 451 | vardeps_data | 
| 55364 | 452 | and ensure_rhs ctxt arities eqngr (c', styps) vardeps_data = | 
| 30054 | 453 | vardeps_data | 
| 55364 | 454 | |> ensure_fun ctxt arities eqngr c' | 
| 30054 | 455 | |> fold_index (fn (k, styp) => | 
| 55364 | 456 | ensure_typmatch ctxt arities eqngr styp (Fun c', k)) styps | 
| 457 | and ensure_fun ctxt arities eqngr c (vardeps_data as (_, (eqntab, _))) = | |
| 30054 | 458 | if Symtab.defined eqntab c then vardeps_data | 
| 459 | else let | |
| 55364 | 460 | val ((lhs, rhss), eqns) = obtain_eqns ctxt eqngr c; | 
| 30054 | 461 | 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 | 462 | in | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 463 | vardeps_data | 
| 30054 | 464 | |> (apsnd o apfst) (Symtab.update_new (c, (lhs, eqns))) | 
| 30083 | 465 | |> fold_index (fn (k, _) => | 
| 466 | apfst (Vargraph.new_node ((Fun c, k), ([] ,[])))) lhs | |
| 55364 | 467 | |> fold_index (fn (k, (_, sort)) => add_classes ctxt arities eqngr (Fun c, k) | 
| 468 | (complete_proper_sort (Proof_Context.theory_of ctxt) sort)) lhs | |
| 469 | |> fold (ensure_rhs ctxt arities eqngr) rhss' | |
| 30054 | 470 | end; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 471 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 472 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 473 | (* applying instantiations *) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 474 | |
| 55757 | 475 | fun dicts_of ctxt (proj_sort, algebra) (T, sort) = | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 476 | let | 
| 55757 | 477 | val thy = Proof_Context.theory_of ctxt; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 478 | 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 | 479 | fun type_constructor (tyco, _) xs class = | 
| 30054 | 480 | inst_params thy tyco (Sorts.complete_sort algebra [class]) | 
| 481 | @ (maps o maps) fst xs; | |
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 482 | fun type_variable (TFree (_, sort)) = map (pair []) (proj_sort sort); | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 483 | in | 
| 32795 | 484 | 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 | 485 |       { class_relation = K class_relation, type_constructor = type_constructor,
 | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 486 | type_variable = type_variable } (T, proj_sort sort) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 487 | handle Sorts.CLASS_ERROR _ => [] (*permissive!*)) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 488 | end; | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 489 | |
| 55757 | 490 | fun add_arity ctxt vardeps (class, tyco) = | 
| 33063 | 491 | AList.default (op =) ((class, tyco), | 
| 34891 
99b9a6290446
code certificates as integral part of code generation
 haftmann parents: 
34251diff
changeset | 492 | map_range (fn k => (snd o Vargraph.get_node vardeps) (Inst (class, tyco), k)) | 
| 55757 | 493 | (Sign.arity_number (Proof_Context.theory_of ctxt) tyco)); | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 494 | |
| 55757 | 495 | fun add_cert ctxt vardeps (c, (proto_lhs, proto_cert)) (rhss, eqngr) = | 
| 30058 | 496 | if can (Graph.get_node eqngr) c then (rhss, eqngr) | 
| 497 | else let | |
| 55757 | 498 | val thy = Proof_Context.theory_of ctxt; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 499 | val lhs = map_index (fn (k, (v, _)) => | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 500 | (v, snd (Vargraph.get_node vardeps (Fun c, k)))) proto_lhs; | 
| 49971 
8b50286c36d3
close code theorems explicitly after preprocessing
 haftmann parents: 
48075diff
changeset | 501 | val cert = proto_cert | 
| 
8b50286c36d3
close code theorems explicitly after preprocessing
 haftmann parents: 
48075diff
changeset | 502 | |> Code.constrain_cert thy (map (Sign.minimize_sort thy o snd) lhs) | 
| 
8b50286c36d3
close code theorems explicitly after preprocessing
 haftmann parents: 
48075diff
changeset | 503 | |> Code.conclude_cert; | 
| 35224 | 504 | val (vs, rhss') = Code.typargs_deps_of_cert thy cert; | 
| 34891 
99b9a6290446
code certificates as integral part of code generation
 haftmann parents: 
34251diff
changeset | 505 | val eqngr' = Graph.new_node (c, (vs, cert)) eqngr; | 
| 30054 | 506 | in (map (pair c) rhss' @ rhss, eqngr') end; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 507 | |
| 55757 | 508 | fun extend_arities_eqngr raw_ctxt cs ts (arities, (eqngr : code_graph)) = | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 509 | let | 
| 55757 | 510 | val thy = Proof_Context.theory_of raw_ctxt; | 
| 55364 | 511 |     val {pre, ...} = the_thmproc thy;
 | 
| 55757 | 512 | val ctxt = put_simpset pre raw_ctxt; | 
| 30942 
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
 haftmann parents: 
30876diff
changeset | 513 | 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 | 514 | insert (op =) (c, (map (styp_of NONE) o Sign.const_typargs thy) c_ty) | _ => I) ts []; | 
| 30054 | 515 | val (vardeps, (eqntab, insts)) = empty_vardeps_data | 
| 55364 | 516 | |> fold (ensure_fun ctxt arities eqngr) cs | 
| 517 | |> fold (ensure_rhs ctxt arities eqngr) cs_rhss; | |
| 55757 | 518 | val arities' = fold (add_arity ctxt vardeps) insts arities; | 
| 47005 
421760a1efe7
maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
 wenzelm parents: 
46961diff
changeset | 519 | val algebra = Sorts.subalgebra (Context.pretty_global thy) (is_proper_class thy) | 
| 30064 | 520 | (AList.lookup (op =) arities') (Sign.classes_of thy); | 
| 55757 | 521 | val (rhss, eqngr') = Symtab.fold (add_cert ctxt vardeps) eqntab ([], eqngr); | 
| 522 | fun deps_of (c, rhs) = c :: maps (dicts_of ctxt algebra) | |
| 32873 | 523 | (rhs ~~ sortargs eqngr' c); | 
| 30054 | 524 | val eqngr'' = fold (fn (c, rhs) => fold | 
| 525 | (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 | 526 | in (algebra, (arities', eqngr'')) end; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 527 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 528 | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 529 | (** store for preprocessed arities and code equations **) | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 530 | |
| 34173 
458ced35abb8
reduced code generator cache to the baremost minimum
 haftmann parents: 
33942diff
changeset | 531 | structure Wellsorted = Code_Data | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 532 | ( | 
| 30947 | 533 | type T = ((string * class) * sort list) list * code_graph; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 534 | val empty = ([], Graph.empty); | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 535 | ); | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 536 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 537 | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 538 | (** retrieval and evaluation interfaces **) | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 539 | |
| 39398 
2e30660a2e21
ignore code cache optionally; corrected scope of term value in static_eval_conv
 haftmann parents: 
39133diff
changeset | 540 | fun obtain ignore_cache thy consts ts = apsnd snd | 
| 55757 | 541 | (Wellsorted.change_yield (if ignore_cache then NONE else SOME thy) | 
| 542 | (extend_arities_eqngr (Proof_Context.init_global thy) consts ts)); | |
| 38670 | 543 | |
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 544 | fun dynamic_evaluator eval ctxt t = | 
| 30942 
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
 haftmann parents: 
30876diff
changeset | 545 | let | 
| 30947 | 546 | val consts = fold_aterms | 
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 547 | (fn Const (c, _) => insert (op =) c | _ => I) t []; | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 548 | val (algebra, eqngr) = obtain false (Proof_Context.theory_of ctxt) consts [t]; | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 549 | in eval algebra eqngr t end; | 
| 30947 | 550 | |
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 551 | fun dynamic_conv ctxt conv = | 
| 56976 | 552 | Sandwich.conversion (value_sandwich ctxt) (dynamic_evaluator conv) ctxt; | 
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 553 | |
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 554 | fun dynamic_value ctxt lift_postproc evaluator = | 
| 56976 | 555 | Sandwich.evaluation (value_sandwich ctxt) lift_postproc (dynamic_evaluator evaluator) ctxt; | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 556 | |
| 56973 
62da80041afd
syntactic means to prevent accidental mixup of static and dynamic context
 haftmann parents: 
56971diff
changeset | 557 | fun static_conv { ctxt, consts } conv =
 | 
| 38670 | 558 | let | 
| 55757 | 559 | val (algebra, eqngr) = obtain true (Proof_Context.theory_of ctxt) consts []; | 
| 56976 | 560 |   in Sandwich.conversion (value_sandwich ctxt) (conv { algebra = algebra, eqngr = eqngr }) end;
 | 
| 38670 | 561 | |
| 56973 
62da80041afd
syntactic means to prevent accidental mixup of static and dynamic context
 haftmann parents: 
56971diff
changeset | 562 | fun static_value { ctxt, lift_postproc, consts } evaluator =
 | 
| 39475 | 563 | let | 
| 55757 | 564 | val (algebra, eqngr) = obtain true (Proof_Context.theory_of ctxt) consts []; | 
| 56976 | 565 |   in Sandwich.evaluation (value_sandwich ctxt) lift_postproc (evaluator { algebra = algebra, eqngr = eqngr }) end;
 | 
| 39475 | 566 | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 567 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 568 | (** setup **) | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 569 | |
| 56970 | 570 | val _ = | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 571 | let | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 572 | fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I); | 
| 56929 | 573 | fun add_del_attribute_parser process = | 
| 574 | Attrib.add_del (mk_attribute (process Simplifier.add_simp)) | |
| 575 | (mk_attribute (process Simplifier.del_simp)); | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 576 | in | 
| 56970 | 577 | Context.>> (Context.map_theory | 
| 578 |       (Attrib.setup @{binding code_unfold} (add_del_attribute_parser process_unfold)
 | |
| 45189 
80cb73210612
removing declaration of code_unfold to address the old code generator
 bulwahn parents: 
44338diff
changeset | 579 | "preprocessing equations for code generator" | 
| 56970 | 580 |       #> Attrib.setup @{binding code_post} (add_del_attribute_parser process_post)
 | 
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31977diff
changeset | 581 | "postprocessing equations for code generator" | 
| 56970 | 582 |       #> Attrib.setup @{binding code_abbrev} (add_del_attribute_parser process_abbrev)
 | 
| 583 | "post- and preprocessing equations for code generator")) | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 584 | end; | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 585 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 586 | val _ = | 
| 46961 
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
 wenzelm parents: 
46497diff
changeset | 587 |   Outer_Syntax.improper_command @{command_spec "print_codeproc"} "print code preprocessor setup"
 | 
| 55757 | 588 | (Scan.succeed (Toplevel.unknown_context o Toplevel.keep (print_codeproc o Toplevel.context_of))); | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 589 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 590 | end; (*struct*) |