| author | blanchet | 
| Mon, 04 Apr 2016 09:45:04 +0200 | |
| changeset 62842 | db9f95ca2a8f | 
| parent 62538 | 85ebb645b1a3 | 
| child 63073 | 413184c7a2a2 | 
| 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 | 
| 60697 | 24 | val obtain: bool -> Proof.context -> 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; | 
| 59582 | 152 | in postproc (conv ctxt (Thm.term_of ct') ct') end; | 
| 56968 
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 | 
| 59633 | 156 | val (postproc, ct') = sandwich ctxt (Thm.cterm_of ctxt t); | 
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 157 | in | 
| 59582 | 158 | Thm.term_of ct' | 
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 159 | |> eval ctxt | 
| 59633 | 160 | |> lift_postproc (Thm.term_of o Thm.rhs_of o postproc o Thm.reflexive o Thm.cterm_of ctxt) | 
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 161 | end; | 
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 162 | |
| 56976 | 163 | end; | 
| 164 | ||
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 165 | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 166 | (* post- and preprocessing *) | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 167 | |
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 168 | fun normalized_tfrees_sandwich ctxt ct = | 
| 56967 
c3746e999805
normalize type variables of evaluation term by conversion
 haftmann parents: 
56929diff
changeset | 169 | let | 
| 59582 | 170 | val t = Thm.term_of ct; | 
| 60805 | 171 | val vs_original = | 
| 172 | fold_term_types (K (fold_atyps (insert (eq_fst op =) o dest_TFree))) t []; | |
| 56967 
c3746e999805
normalize type variables of evaluation term by conversion
 haftmann parents: 
56929diff
changeset | 173 | val vs_normalized = Name.invent_names Name.context Name.aT (map snd vs_original); | 
| 60805 | 174 | val normalize = | 
| 175 | map_type_tfree (TFree o the o AList.lookup (op =) (vs_original ~~ vs_normalized)); | |
| 56967 
c3746e999805
normalize type variables of evaluation term by conversion
 haftmann parents: 
56929diff
changeset | 176 | val normalization = | 
| 60805 | 177 | map2 (fn (v, sort) => fn (v', _) => (((v', 0), sort), Thm.ctyp_of ctxt (TFree (v, sort)))) | 
| 178 | vs_original vs_normalized; | |
| 56967 
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) | |
| 59633 | 182 | else | 
| 60805 | 183 | (Thm.instantiate (normalization, []) o Thm.varifyT_global, | 
| 59633 | 184 | Thm.cterm_of ctxt (map_types normalize t)) | 
| 56967 
c3746e999805
normalize type variables of evaluation term by conversion
 haftmann parents: 
56929diff
changeset | 185 | end; | 
| 
c3746e999805
normalize type variables of evaluation term by conversion
 haftmann parents: 
56929diff
changeset | 186 | |
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 187 | 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 | 188 | let | 
| 59633 | 189 | val all_vars = fold_aterms (fn t as Free _ => insert (op aconvc) (Thm.cterm_of ctxt t) | 
| 190 | | t as Var _ => insert (op aconvc) (Thm.cterm_of ctxt t) | |
| 56968 
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, | 
| 59917 
9830c944670f
more uniform "verbose" option to print name space;
 wenzelm parents: 
59840diff
changeset | 232 | Simplifier.pretty_simpset true (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, | 
| 59917 
9830c944670f
more uniform "verbose" option to print name space;
 wenzelm parents: 
59840diff
changeset | 237 | Simplifier.pretty_simpset true (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) | |
| 59058 
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
 wenzelm parents: 
58893diff
changeset | 270 | |> sort (string_ord o apply2 fst) | 
| 55757 | 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; | 
| 59323 | 283 | fun merge (NONE, _) = NONE | 
| 284 | | merge (_, NONE) = NONE | |
| 57430 
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; | 
| 59840 | 376 | val classess = | 
| 377 | map (complete_proper_sort thy) | |
| 378 | (Proof_Context.arity_sorts ctxt tyco [class]); | |
| 30029 | 379 | val inst_params = inst_params thy tyco all_classes; | 
| 37384 
5aba26803073
more consistent naming aroud type classes and instances
 haftmann parents: 
36960diff
changeset | 380 | in (classess, (super_classes, inst_params)) end; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 381 | |
| 30054 | 382 | |
| 383 | (* computing instantiations *) | |
| 384 | ||
| 55364 | 385 | 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 | 386 | let | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 387 | val (styps, old_classes) = Vargraph.get_node (fst vardeps_data) c_k; | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 388 | val diff_classes = new_classes |> subtract (op =) old_classes; | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 389 | in if null diff_classes then vardeps_data | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 390 | 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 | 391 | 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 | 392 | in | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 393 | vardeps_data | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 394 | |> (apfst o Vargraph.map_node c_k o apsnd) (append diff_classes) | 
| 55364 | 395 | |> fold (fn styp => fold (ensure_typmatch_inst ctxt arities eqngr styp) new_classes) styps | 
| 396 | |> 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 | 397 | end end | 
| 55364 | 398 | 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 | 399 | let | 
| 37384 
5aba26803073
more consistent naming aroud type classes and instances
 haftmann parents: 
36960diff
changeset | 400 | 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 | 401 | 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 | 402 | else | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 403 | vardeps_data | 
| 37384 
5aba26803073
more consistent naming aroud type classes and instances
 haftmann parents: 
36960diff
changeset | 404 | |> (apfst o Vargraph.map_node c_k o apfst) (cons new_tyco_styps) | 
| 55364 | 405 | |> fold (ensure_typmatch_inst ctxt arities eqngr new_tyco_styps) classes | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 406 | end | 
| 55364 | 407 | 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 | 408 | let | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 409 | val (_, classes) = Vargraph.get_node (fst vardeps_data) c_k; | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 410 | in | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 411 | vardeps_data | 
| 55364 | 412 | |> add_classes ctxt arities eqngr c_k' classes | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 413 | |> apfst (Vargraph.add_edge (c_k, c_k')) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 414 | end | 
| 55364 | 415 | and ensure_typmatch_inst ctxt arities eqngr (tyco, styps) class vardeps_data = | 
| 59840 | 416 | if can (Proof_Context.arity_sorts ctxt tyco) [class] | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 417 | then vardeps_data | 
| 55364 | 418 | |> ensure_inst ctxt arities eqngr (class, tyco) | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 419 | |> fold_index (fn (k, styp) => | 
| 55364 | 420 | ensure_typmatch ctxt arities eqngr styp (Inst (class, tyco), k)) styps | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 421 | else vardeps_data (*permissive!*) | 
| 55364 | 422 | and ensure_inst ctxt arities eqngr (inst as (class, tyco)) (vardeps_data as (_, (_, insts))) = | 
| 30054 | 423 | if member (op =) insts inst then vardeps_data | 
| 424 | else let | |
| 37384 
5aba26803073
more consistent naming aroud type classes and instances
 haftmann parents: 
36960diff
changeset | 425 | val (classess, (super_classes, inst_params)) = | 
| 55757 | 426 | obtain_instance ctxt arities inst; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 427 | in | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 428 | vardeps_data | 
| 30054 | 429 | |> (apsnd o apsnd) (insert (op =) inst) | 
| 30083 | 430 | |> fold_index (fn (k, _) => | 
| 431 | apfst (Vargraph.new_node ((Inst (class, tyco), k), ([] ,[])))) classess | |
| 55364 | 432 | |> fold (fn super_class => ensure_inst ctxt arities eqngr (super_class, tyco)) super_classes | 
| 433 | |> fold (ensure_fun ctxt arities eqngr) inst_params | |
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 434 | |> fold_index (fn (k, classes) => | 
| 55364 | 435 | add_classes ctxt arities eqngr (Inst (class, tyco), k) classes | 
| 37384 
5aba26803073
more consistent naming aroud type classes and instances
 haftmann parents: 
36960diff
changeset | 436 | #> fold (fn super_class => | 
| 55364 | 437 | add_dep ctxt arities eqngr (Inst (super_class, tyco), k) | 
| 37384 
5aba26803073
more consistent naming aroud type classes and instances
 haftmann parents: 
36960diff
changeset | 438 | (Inst (class, tyco), k)) super_classes | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 439 | #> fold (fn inst_param => | 
| 55364 | 440 | add_dep ctxt arities eqngr (Fun inst_param, k) | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 441 | (Inst (class, tyco), k) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 442 | ) inst_params | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 443 | ) classess | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 444 | end | 
| 55364 | 445 | and ensure_typmatch ctxt arities eqngr (Tyco tyco_styps) c_k vardeps_data = | 
| 30054 | 446 | vardeps_data | 
| 55364 | 447 | |> add_styp ctxt arities eqngr c_k tyco_styps | 
| 448 | | ensure_typmatch ctxt arities eqngr (Var c_k') c_k vardeps_data = | |
| 30054 | 449 | vardeps_data | 
| 55364 | 450 | |> add_dep ctxt arities eqngr c_k c_k' | 
| 451 | | ensure_typmatch ctxt arities eqngr Free c_k vardeps_data = | |
| 30054 | 452 | vardeps_data | 
| 55364 | 453 | and ensure_rhs ctxt arities eqngr (c', styps) vardeps_data = | 
| 30054 | 454 | vardeps_data | 
| 55364 | 455 | |> ensure_fun ctxt arities eqngr c' | 
| 30054 | 456 | |> fold_index (fn (k, styp) => | 
| 55364 | 457 | ensure_typmatch ctxt arities eqngr styp (Fun c', k)) styps | 
| 458 | and ensure_fun ctxt arities eqngr c (vardeps_data as (_, (eqntab, _))) = | |
| 30054 | 459 | if Symtab.defined eqntab c then vardeps_data | 
| 460 | else let | |
| 55364 | 461 | val ((lhs, rhss), eqns) = obtain_eqns ctxt eqngr c; | 
| 30054 | 462 | 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 | 463 | in | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 464 | vardeps_data | 
| 30054 | 465 | |> (apsnd o apfst) (Symtab.update_new (c, (lhs, eqns))) | 
| 30083 | 466 | |> fold_index (fn (k, _) => | 
| 467 | apfst (Vargraph.new_node ((Fun c, k), ([] ,[])))) lhs | |
| 55364 | 468 | |> fold_index (fn (k, (_, sort)) => add_classes ctxt arities eqngr (Fun c, k) | 
| 469 | (complete_proper_sort (Proof_Context.theory_of ctxt) sort)) lhs | |
| 470 | |> fold (ensure_rhs ctxt arities eqngr) rhss' | |
| 30054 | 471 | end; | 
| 30010 
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 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 474 | (* applying instantiations *) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 475 | |
| 55757 | 476 | fun dicts_of ctxt (proj_sort, algebra) (T, sort) = | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 477 | let | 
| 55757 | 478 | val thy = Proof_Context.theory_of ctxt; | 
| 62538 
85ebb645b1a3
provide explicit hint concering uniqueness of derivation
 haftmann parents: 
61262diff
changeset | 479 | 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 | 480 | fun type_constructor (tyco, _) xs class = | 
| 30054 | 481 | inst_params thy tyco (Sorts.complete_sort algebra [class]) | 
| 482 | @ (maps o maps) fst xs; | |
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 483 | fun type_variable (TFree (_, sort)) = map (pair []) (proj_sort sort); | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 484 | in | 
| 32795 | 485 | 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 | 486 |       { class_relation = K class_relation, type_constructor = type_constructor,
 | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 487 | type_variable = type_variable } (T, proj_sort sort) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 488 | handle Sorts.CLASS_ERROR _ => [] (*permissive!*)) | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 489 | end; | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 490 | |
| 55757 | 491 | fun add_arity ctxt vardeps (class, tyco) = | 
| 33063 | 492 | AList.default (op =) ((class, tyco), | 
| 34891 
99b9a6290446
code certificates as integral part of code generation
 haftmann parents: 
34251diff
changeset | 493 | map_range (fn k => (snd o Vargraph.get_node vardeps) (Inst (class, tyco), k)) | 
| 55757 | 494 | (Sign.arity_number (Proof_Context.theory_of ctxt) tyco)); | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 495 | |
| 55757 | 496 | fun add_cert ctxt vardeps (c, (proto_lhs, proto_cert)) (rhss, eqngr) = | 
| 30058 | 497 | if can (Graph.get_node eqngr) c then (rhss, eqngr) | 
| 498 | else let | |
| 55757 | 499 | val thy = Proof_Context.theory_of ctxt; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 500 | val lhs = map_index (fn (k, (v, _)) => | 
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 501 | (v, snd (Vargraph.get_node vardeps (Fun c, k)))) proto_lhs; | 
| 49971 
8b50286c36d3
close code theorems explicitly after preprocessing
 haftmann parents: 
48075diff
changeset | 502 | val cert = proto_cert | 
| 
8b50286c36d3
close code theorems explicitly after preprocessing
 haftmann parents: 
48075diff
changeset | 503 | |> Code.constrain_cert thy (map (Sign.minimize_sort thy o snd) lhs) | 
| 
8b50286c36d3
close code theorems explicitly after preprocessing
 haftmann parents: 
48075diff
changeset | 504 | |> Code.conclude_cert; | 
| 35224 | 505 | val (vs, rhss') = Code.typargs_deps_of_cert thy cert; | 
| 34891 
99b9a6290446
code certificates as integral part of code generation
 haftmann parents: 
34251diff
changeset | 506 | val eqngr' = Graph.new_node (c, (vs, cert)) eqngr; | 
| 30054 | 507 | in (map (pair c) rhss' @ rhss, eqngr') end; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 508 | |
| 55757 | 509 | 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 | 510 | let | 
| 55757 | 511 | val thy = Proof_Context.theory_of raw_ctxt; | 
| 55364 | 512 |     val {pre, ...} = the_thmproc thy;
 | 
| 55757 | 513 | val ctxt = put_simpset pre raw_ctxt; | 
| 30942 
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
 haftmann parents: 
30876diff
changeset | 514 | 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 | 515 | insert (op =) (c, (map (styp_of NONE) o Sign.const_typargs thy) c_ty) | _ => I) ts []; | 
| 30054 | 516 | val (vardeps, (eqntab, insts)) = empty_vardeps_data | 
| 55364 | 517 | |> fold (ensure_fun ctxt arities eqngr) cs | 
| 518 | |> fold (ensure_rhs ctxt arities eqngr) cs_rhss; | |
| 55757 | 519 | val arities' = fold (add_arity ctxt vardeps) insts arities; | 
| 61262 
7bd1eb4b056e
tuned signature: eliminated pointless type Context.pretty;
 wenzelm parents: 
60805diff
changeset | 520 | val algebra = Sorts.subalgebra (Context.Theory thy) (is_proper_class thy) | 
| 30064 | 521 | (AList.lookup (op =) arities') (Sign.classes_of thy); | 
| 55757 | 522 | val (rhss, eqngr') = Symtab.fold (add_cert ctxt vardeps) eqntab ([], eqngr); | 
| 523 | fun deps_of (c, rhs) = c :: maps (dicts_of ctxt algebra) | |
| 32873 | 524 | (rhs ~~ sortargs eqngr' c); | 
| 30054 | 525 | val eqngr'' = fold (fn (c, rhs) => fold | 
| 526 | (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 | 527 | in (algebra, (arities', eqngr'')) end; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 528 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 529 | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 530 | (** store for preprocessed arities and code equations **) | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 531 | |
| 34173 
458ced35abb8
reduced code generator cache to the baremost minimum
 haftmann parents: 
33942diff
changeset | 532 | structure Wellsorted = Code_Data | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 533 | ( | 
| 30947 | 534 | type T = ((string * class) * sort list) list * code_graph; | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 535 | val empty = ([], Graph.empty); | 
| 
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 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 538 | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 539 | (** retrieval and evaluation interfaces **) | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 540 | |
| 60697 | 541 | fun obtain ignore_cache ctxt consts ts = apsnd snd | 
| 542 | (Wellsorted.change_yield (if ignore_cache then NONE else SOME (Proof_Context.theory_of ctxt)) | |
| 543 | (extend_arities_eqngr ctxt consts ts)); | |
| 38670 | 544 | |
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 545 | fun dynamic_evaluator eval ctxt t = | 
| 30942 
1e246776f876
diagnostic commands now in code_thingol; tuned code of funny continuations
 haftmann parents: 
30876diff
changeset | 546 | let | 
| 30947 | 547 | val consts = fold_aterms | 
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 548 | (fn Const (c, _) => insert (op =) c | _ => I) t []; | 
| 60697 | 549 | val (algebra, eqngr) = obtain false ctxt consts [t]; | 
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 550 | in eval algebra eqngr t end; | 
| 30947 | 551 | |
| 56968 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 552 | fun dynamic_conv ctxt conv = | 
| 56976 | 553 | 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 | 554 | |
| 
d2b1d95eb722
unified approach toward conversions and simple term rewriting in preprocessor by means of sandwiches
 haftmann parents: 
56967diff
changeset | 555 | fun dynamic_value ctxt lift_postproc evaluator = | 
| 56976 | 556 | Sandwich.evaluation (value_sandwich ctxt) lift_postproc (dynamic_evaluator evaluator) ctxt; | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 557 | |
| 56973 
62da80041afd
syntactic means to prevent accidental mixup of static and dynamic context
 haftmann parents: 
56971diff
changeset | 558 | fun static_conv { ctxt, consts } conv =
 | 
| 38670 | 559 | let | 
| 60697 | 560 | val (algebra, eqngr) = obtain true ctxt consts []; | 
| 56976 | 561 |   in Sandwich.conversion (value_sandwich ctxt) (conv { algebra = algebra, eqngr = eqngr }) end;
 | 
| 38670 | 562 | |
| 56973 
62da80041afd
syntactic means to prevent accidental mixup of static and dynamic context
 haftmann parents: 
56971diff
changeset | 563 | fun static_value { ctxt, lift_postproc, consts } evaluator =
 | 
| 39475 | 564 | let | 
| 60697 | 565 | val (algebra, eqngr) = obtain true ctxt consts []; | 
| 56976 | 566 |   in Sandwich.evaluation (value_sandwich ctxt) lift_postproc (evaluator { algebra = algebra, eqngr = eqngr }) end;
 | 
| 39475 | 567 | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 568 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 569 | (** setup **) | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 570 | |
| 59323 | 571 | val _ = Theory.setup ( | 
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 572 | let | 
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 573 | fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I); | 
| 56929 | 574 | fun add_del_attribute_parser process = | 
| 575 | Attrib.add_del (mk_attribute (process Simplifier.add_simp)) | |
| 576 | (mk_attribute (process Simplifier.del_simp)); | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 577 | in | 
| 59323 | 578 |     Attrib.setup @{binding code_unfold} (add_del_attribute_parser process_unfold)
 | 
| 579 | "preprocessing equations for code generator" | |
| 580 |     #> Attrib.setup @{binding code_post} (add_del_attribute_parser process_post)
 | |
| 581 | "postprocessing equations for code generator" | |
| 582 |     #> Attrib.setup @{binding code_abbrev} (add_del_attribute_parser process_abbrev)
 | |
| 583 | "post- and preprocessing equations for code generator" | |
| 584 |     #> Attrib.setup @{binding code_preproc_trace}
 | |
| 585 | ((Scan.lift (Args.$$$ "off" >> K trace_none) | |
| 586 | || (Scan.lift (Args.$$$ "only" |-- Args.colon |-- Scan.repeat1 Parse.term)) | |
| 587 | >> trace_only_ext | |
| 588 | || Scan.succeed trace_all) | |
| 589 | >> (Thm.declaration_attribute o K)) "tracing of the code generator preprocessor" | |
| 590 | end); | |
| 31125 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 591 | |
| 
80218ee73167
transferred code generator preprocessor into separate module
 haftmann parents: 
31089diff
changeset | 592 | val _ = | 
| 59936 
b8ffc3dc9e24
@{command_spec} is superseded by @{command_keyword};
 wenzelm parents: 
59917diff
changeset | 593 |   Outer_Syntax.command @{command_keyword print_codeproc} "print code preprocessor setup"
 | 
| 60097 
d20ca79d50e4
discontinued pointless warnings: commands are only defined inside a theory context;
 wenzelm parents: 
59936diff
changeset | 594 | (Scan.succeed (Toplevel.keep (print_codeproc o Toplevel.context_of))); | 
| 30010 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 595 | |
| 
862fc7751a15
tuned and incremental version of wellsorting algorithm
 haftmann parents: diff
changeset | 596 | end; (*struct*) |