| author | wenzelm | 
| Tue, 29 Aug 2023 17:19:19 +0200 | |
| changeset 78609 | 67492b2a3a62 | 
| parent 72607 | feebdaa346e5 | 
| child 80932 | 261cd8722677 | 
| permissions | -rw-r--r-- | 
| 32657 | 1  | 
(* Title: HOL/Code_Evaluation.thy  | 
| 28228 | 2  | 
Author: Florian Haftmann, TU Muenchen  | 
3  | 
*)  | 
|
4  | 
||
| 60758 | 5  | 
section \<open>Term evaluation using the generic code generator\<close>  | 
| 28228 | 6  | 
|
| 32657 | 7  | 
theory Code_Evaluation  | 
| 
51126
 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 
haftmann 
parents: 
51112 
diff
changeset
 | 
8  | 
imports Typerep Limited_Sequence  | 
| 56925 | 9  | 
keywords "value" :: diag  | 
| 28228 | 10  | 
begin  | 
11  | 
||
| 60758 | 12  | 
subsection \<open>Term representation\<close>  | 
| 28228 | 13  | 
|
| 61799 | 14  | 
subsubsection \<open>Terms and class \<open>term_of\<close>\<close>  | 
| 28228 | 15  | 
|
| 66013 | 16  | 
datatype (plugins only: extraction) "term" = dummy_term  | 
| 28228 | 17  | 
|
| 
31205
 
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
 
haftmann 
parents: 
31203 
diff
changeset
 | 
18  | 
definition Const :: "String.literal \<Rightarrow> typerep \<Rightarrow> term" where  | 
| 28228 | 19  | 
"Const _ _ = dummy_term"  | 
20  | 
||
| 28661 | 21  | 
definition App :: "term \<Rightarrow> term \<Rightarrow> term" where  | 
| 28228 | 22  | 
"App _ _ = dummy_term"  | 
23  | 
||
| 
40638
 
6b137c96df07
adding dummy definition for Code_Evaluation.Abs and hiding constants App less strict
 
bulwahn 
parents: 
39567 
diff
changeset
 | 
24  | 
definition Abs :: "String.literal \<Rightarrow> typerep \<Rightarrow> term \<Rightarrow> term" where  | 
| 
 
6b137c96df07
adding dummy definition for Code_Evaluation.Abs and hiding constants App less strict
 
bulwahn 
parents: 
39567 
diff
changeset
 | 
25  | 
"Abs _ _ _ = dummy_term"  | 
| 
 
6b137c96df07
adding dummy definition for Code_Evaluation.Abs and hiding constants App less strict
 
bulwahn 
parents: 
39567 
diff
changeset
 | 
26  | 
|
| 
42979
 
5b9e16259341
extending terms of Code_Evaluation by Free to allow partial terms
 
bulwahn 
parents: 
40884 
diff
changeset
 | 
27  | 
definition Free :: "String.literal \<Rightarrow> typerep \<Rightarrow> term" where  | 
| 
 
5b9e16259341
extending terms of Code_Evaluation by Free to allow partial terms
 
bulwahn 
parents: 
40884 
diff
changeset
 | 
28  | 
"Free _ _ = dummy_term"  | 
| 
 
5b9e16259341
extending terms of Code_Evaluation by Free to allow partial terms
 
bulwahn 
parents: 
40884 
diff
changeset
 | 
29  | 
|
| 
 
5b9e16259341
extending terms of Code_Evaluation by Free to allow partial terms
 
bulwahn 
parents: 
40884 
diff
changeset
 | 
30  | 
code_datatype Const App Abs Free  | 
| 28228 | 31  | 
|
| 28335 | 32  | 
class term_of = typerep +  | 
| 31031 | 33  | 
fixes term_of :: "'a \<Rightarrow> term"  | 
| 28228 | 34  | 
|
35  | 
lemma term_of_anything: "term_of x \<equiv> t"  | 
|
36  | 
by (rule eq_reflection) (cases "term_of x", cases t, simp)  | 
|
37  | 
||
| 31191 | 38  | 
definition valapp :: "('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)
 | 
| 31178 | 39  | 
\<Rightarrow> 'a \<times> (unit \<Rightarrow> term) \<Rightarrow> 'b \<times> (unit \<Rightarrow> term)" where  | 
| 31191 | 40  | 
"valapp f x = (fst f (fst x), \<lambda>u. App (snd f ()) (snd x ()))"  | 
| 31178 | 41  | 
|
| 
32069
 
6d28bbd33e2c
prefer code_inline over code_unfold; use code_unfold_post where appropriate
 
haftmann 
parents: 
31998 
diff
changeset
 | 
42  | 
lemma valapp_code [code, code_unfold]:  | 
| 31191 | 43  | 
"valapp (f, tf) (x, tx) = (f x, \<lambda>u. App (tf ()) (tx ()))"  | 
44  | 
by (simp only: valapp_def fst_conv snd_conv)  | 
|
| 31178 | 45  | 
|
| 28228 | 46  | 
|
| 60758 | 47  | 
subsubsection \<open>Syntax\<close>  | 
| 31178 | 48  | 
|
| 31191 | 49  | 
definition termify :: "'a \<Rightarrow> term" where  | 
50  | 
[code del]: "termify x = dummy_term"  | 
|
51  | 
||
52  | 
abbreviation valtermify :: "'a \<Rightarrow> 'a \<times> (unit \<Rightarrow> term)" where  | 
|
53  | 
"valtermify x \<equiv> (x, \<lambda>u. termify x)"  | 
|
| 31178 | 54  | 
|
| 72607 | 55  | 
bundle term_syntax  | 
| 31178 | 56  | 
begin  | 
57  | 
||
| 31191 | 58  | 
notation App (infixl "<\<cdot>>" 70)  | 
59  | 
  and valapp (infixl "{\<cdot>}" 70)
 | 
|
| 31178 | 60  | 
|
61  | 
end  | 
|
62  | 
||
| 31191 | 63  | 
|
| 60758 | 64  | 
subsection \<open>Tools setup and evaluation\<close>  | 
| 39564 | 65  | 
|
| 
63161
 
2660ba498798
delegate inclusion of required dictionaries to user-space instead of half-working magic
 
haftmann 
parents: 
62958 
diff
changeset
 | 
66  | 
context  | 
| 
 
2660ba498798
delegate inclusion of required dictionaries to user-space instead of half-working magic
 
haftmann 
parents: 
62958 
diff
changeset
 | 
67  | 
begin  | 
| 
 
2660ba498798
delegate inclusion of required dictionaries to user-space instead of half-working magic
 
haftmann 
parents: 
62958 
diff
changeset
 | 
68  | 
|
| 
 
2660ba498798
delegate inclusion of required dictionaries to user-space instead of half-working magic
 
haftmann 
parents: 
62958 
diff
changeset
 | 
69  | 
qualified definition TERM_OF :: "'a::term_of itself"  | 
| 
 
2660ba498798
delegate inclusion of required dictionaries to user-space instead of half-working magic
 
haftmann 
parents: 
62958 
diff
changeset
 | 
70  | 
where  | 
| 
 
2660ba498798
delegate inclusion of required dictionaries to user-space instead of half-working magic
 
haftmann 
parents: 
62958 
diff
changeset
 | 
71  | 
  "TERM_OF = snd (Code_Evaluation.term_of :: 'a \<Rightarrow> _, TYPE('a))"
 | 
| 
 
2660ba498798
delegate inclusion of required dictionaries to user-space instead of half-working magic
 
haftmann 
parents: 
62958 
diff
changeset
 | 
72  | 
|
| 
 
2660ba498798
delegate inclusion of required dictionaries to user-space instead of half-working magic
 
haftmann 
parents: 
62958 
diff
changeset
 | 
73  | 
qualified definition TERM_OF_EQUAL :: "'a::term_of itself"  | 
| 
 
2660ba498798
delegate inclusion of required dictionaries to user-space instead of half-working magic
 
haftmann 
parents: 
62958 
diff
changeset
 | 
74  | 
where  | 
| 
 
2660ba498798
delegate inclusion of required dictionaries to user-space instead of half-working magic
 
haftmann 
parents: 
62958 
diff
changeset
 | 
75  | 
  "TERM_OF_EQUAL = snd (\<lambda>(a::'a). (Code_Evaluation.term_of a, HOL.eq a), TYPE('a))"
 | 
| 
 
2660ba498798
delegate inclusion of required dictionaries to user-space instead of half-working magic
 
haftmann 
parents: 
62958 
diff
changeset
 | 
76  | 
|
| 
 
2660ba498798
delegate inclusion of required dictionaries to user-space instead of half-working magic
 
haftmann 
parents: 
62958 
diff
changeset
 | 
77  | 
end  | 
| 
 
2660ba498798
delegate inclusion of required dictionaries to user-space instead of half-working magic
 
haftmann 
parents: 
62958 
diff
changeset
 | 
78  | 
|
| 
39567
 
5ee997fbe5cc
dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
 
haftmann 
parents: 
39564 
diff
changeset
 | 
79  | 
lemma eq_eq_TrueD:  | 
| 
62958
 
b41c1cb5e251
Type_Infer.object_logic controls improvement of type inference result;
 
wenzelm 
parents: 
62597 
diff
changeset
 | 
80  | 
  fixes x y :: "'a::{}"
 | 
| 
39567
 
5ee997fbe5cc
dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
 
haftmann 
parents: 
39564 
diff
changeset
 | 
81  | 
assumes "(x \<equiv> y) \<equiv> Trueprop True"  | 
| 
 
5ee997fbe5cc
dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
 
haftmann 
parents: 
39564 
diff
changeset
 | 
82  | 
shows "x \<equiv> y"  | 
| 
 
5ee997fbe5cc
dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
 
haftmann 
parents: 
39564 
diff
changeset
 | 
83  | 
using assms by simp  | 
| 
 
5ee997fbe5cc
dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
 
haftmann 
parents: 
39564 
diff
changeset
 | 
84  | 
|
| 
56928
 
1e50fddbe1f5
dropped term_of obfuscation -- not really required;
 
haftmann 
parents: 
56927 
diff
changeset
 | 
85  | 
code_printing  | 
| 
 
1e50fddbe1f5
dropped term_of obfuscation -- not really required;
 
haftmann 
parents: 
56927 
diff
changeset
 | 
86  | 
type_constructor "term" \<rightharpoonup> (Eval) "Term.term"  | 
| 
 
1e50fddbe1f5
dropped term_of obfuscation -- not really required;
 
haftmann 
parents: 
56927 
diff
changeset
 | 
87  | 
| constant Const \<rightharpoonup> (Eval) "Term.Const/ ((_), (_))"  | 
| 
 
1e50fddbe1f5
dropped term_of obfuscation -- not really required;
 
haftmann 
parents: 
56927 
diff
changeset
 | 
88  | 
| constant App \<rightharpoonup> (Eval) "Term.$/ ((_), (_))"  | 
| 
 
1e50fddbe1f5
dropped term_of obfuscation -- not really required;
 
haftmann 
parents: 
56927 
diff
changeset
 | 
89  | 
| constant Abs \<rightharpoonup> (Eval) "Term.Abs/ ((_), (_), (_))"  | 
| 
 
1e50fddbe1f5
dropped term_of obfuscation -- not really required;
 
haftmann 
parents: 
56927 
diff
changeset
 | 
90  | 
| constant Free \<rightharpoonup> (Eval) "Term.Free/ ((_), (_))"  | 
| 
 
1e50fddbe1f5
dropped term_of obfuscation -- not really required;
 
haftmann 
parents: 
56927 
diff
changeset
 | 
91  | 
|
| 69605 | 92  | 
ML_file \<open>Tools/code_evaluation.ML\<close>  | 
| 39564 | 93  | 
|
94  | 
code_reserved Eval Code_Evaluation  | 
|
95  | 
||
| 69605 | 96  | 
ML_file \<open>~~/src/HOL/Tools/value_command.ML\<close>  | 
| 
56928
 
1e50fddbe1f5
dropped term_of obfuscation -- not really required;
 
haftmann 
parents: 
56927 
diff
changeset
 | 
97  | 
|
| 39564 | 98  | 
|
| 68028 | 99  | 
subsection \<open>Dedicated \<open>term_of\<close> instances\<close>  | 
| 39564 | 100  | 
|
101  | 
instantiation "fun" :: (typerep, typerep) term_of  | 
|
102  | 
begin  | 
|
103  | 
||
104  | 
definition  | 
|
| 61076 | 105  | 
"term_of (f :: 'a \<Rightarrow> 'b) =  | 
| 56241 | 106  | 
Const (STR ''Pure.dummy_pattern'')  | 
107  | 
      (Typerep.Typerep (STR ''fun'') [Typerep.typerep TYPE('a), Typerep.typerep TYPE('b)])"
 | 
|
| 39564 | 108  | 
|
109  | 
instance ..  | 
|
110  | 
||
111  | 
end  | 
|
112  | 
||
| 66013 | 113  | 
declare [[code drop: rec_term case_term  | 
| 56926 | 114  | 
"term_of :: typerep \<Rightarrow> _" "term_of :: term \<Rightarrow> _" "term_of :: String.literal \<Rightarrow> _"  | 
115  | 
"term_of :: _ Predicate.pred \<Rightarrow> term" "term_of :: _ Predicate.seq \<Rightarrow> term"]]  | 
|
| 39564 | 116  | 
|
| 
52435
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
52286 
diff
changeset
 | 
117  | 
code_printing  | 
| 61076 | 118  | 
constant "term_of :: integer \<Rightarrow> term" \<rightharpoonup> (Eval) "HOLogic.mk'_number/ HOLogic.code'_integerT"  | 
119  | 
| constant "term_of :: String.literal \<Rightarrow> term" \<rightharpoonup> (Eval) "HOLogic.mk'_literal"  | 
|
| 39564 | 120  | 
|
| 
58036
 
f23045003476
add code equation for term_of on integer
 
Andreas Lochbihler 
parents: 
56928 
diff
changeset
 | 
121  | 
declare [[code drop: "term_of :: integer \<Rightarrow> _"]]  | 
| 
 
f23045003476
add code equation for term_of on integer
 
Andreas Lochbihler 
parents: 
56928 
diff
changeset
 | 
122  | 
|
| 
 
f23045003476
add code equation for term_of on integer
 
Andreas Lochbihler 
parents: 
56928 
diff
changeset
 | 
123  | 
lemma term_of_integer [unfolded typerep_fun_def typerep_num_def typerep_integer_def, code]:  | 
| 
58038
 
f8e6197668c9
correct code equation for term_of on integer
 
Andreas Lochbihler 
parents: 
58036 
diff
changeset
 | 
124  | 
"term_of (i :: integer) =  | 
| 
 
f8e6197668c9
correct code equation for term_of on integer
 
Andreas Lochbihler 
parents: 
58036 
diff
changeset
 | 
125  | 
(if i > 0 then  | 
| 
 
f8e6197668c9
correct code equation for term_of on integer
 
Andreas Lochbihler 
parents: 
58036 
diff
changeset
 | 
126  | 
App (Const (STR ''Num.numeral_class.numeral'') (TYPEREP(num \<Rightarrow> integer)))  | 
| 
 
f8e6197668c9
correct code equation for term_of on integer
 
Andreas Lochbihler 
parents: 
58036 
diff
changeset
 | 
127  | 
(term_of (num_of_integer i))  | 
| 
 
f8e6197668c9
correct code equation for term_of on integer
 
Andreas Lochbihler 
parents: 
58036 
diff
changeset
 | 
128  | 
else if i = 0 then Const (STR ''Groups.zero_class.zero'') TYPEREP(integer)  | 
| 
 
f8e6197668c9
correct code equation for term_of on integer
 
Andreas Lochbihler 
parents: 
58036 
diff
changeset
 | 
129  | 
else  | 
| 
 
f8e6197668c9
correct code equation for term_of on integer
 
Andreas Lochbihler 
parents: 
58036 
diff
changeset
 | 
130  | 
App (Const (STR ''Groups.uminus_class.uminus'') TYPEREP(integer \<Rightarrow> integer))  | 
| 
 
f8e6197668c9
correct code equation for term_of on integer
 
Andreas Lochbihler 
parents: 
58036 
diff
changeset
 | 
131  | 
(term_of (- i)))"  | 
| 62597 | 132  | 
by (rule term_of_anything [THEN meta_eq_to_obj_eq])  | 
| 
58036
 
f23045003476
add code equation for term_of on integer
 
Andreas Lochbihler 
parents: 
56928 
diff
changeset
 | 
133  | 
|
| 39564 | 134  | 
code_reserved Eval HOLogic  | 
135  | 
||
136  | 
||
| 60758 | 137  | 
subsection \<open>Generic reification\<close>  | 
| 31178 | 138  | 
|
| 69605 | 139  | 
ML_file \<open>~~/src/HOL/Tools/reification.ML\<close>  | 
| 31178 | 140  | 
|
141  | 
||
| 60758 | 142  | 
subsection \<open>Diagnostic\<close>  | 
| 
39387
 
6608c4838ff9
replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references; tuned structures
 
haftmann 
parents: 
39274 
diff
changeset
 | 
143  | 
|
| 
 
6608c4838ff9
replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references; tuned structures
 
haftmann 
parents: 
39274 
diff
changeset
 | 
144  | 
definition tracing :: "String.literal \<Rightarrow> 'a \<Rightarrow> 'a" where  | 
| 
 
6608c4838ff9
replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references; tuned structures
 
haftmann 
parents: 
39274 
diff
changeset
 | 
145  | 
[code del]: "tracing s x = x"  | 
| 
 
6608c4838ff9
replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references; tuned structures
 
haftmann 
parents: 
39274 
diff
changeset
 | 
146  | 
|
| 
52435
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
52286 
diff
changeset
 | 
147  | 
code_printing  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
52286 
diff
changeset
 | 
148  | 
constant "tracing :: String.literal => 'a => 'a" \<rightharpoonup> (Eval) "Code'_Evaluation.tracing"  | 
| 
33473
 
3b275a0bf18c
adding tracing function for evaluated code; annotated compilation in the predicate compiler
 
bulwahn 
parents: 
32740 
diff
changeset
 | 
149  | 
|
| 31178 | 150  | 
|
| 
40638
 
6b137c96df07
adding dummy definition for Code_Evaluation.Abs and hiding constants App less strict
 
bulwahn 
parents: 
39567 
diff
changeset
 | 
151  | 
hide_const dummy_term valapp  | 
| 51144 | 152  | 
hide_const (open) Const App Abs Free termify valtermify term_of tracing  | 
| 28228 | 153  | 
|
154  | 
end  |