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