| author | blanchet | 
| Thu, 28 May 2015 10:18:46 +0200 | |
| changeset 60306 | 6b7c64ab8bd2 | 
| parent 59484 | a130ae7a9398 | 
| child 60758 | d8d85a8172b5 | 
| permissions | -rw-r--r-- | 
| 32657 | 1  | 
(* Title: HOL/Code_Evaluation.thy  | 
| 28228 | 2  | 
Author: Florian Haftmann, TU Muenchen  | 
3  | 
*)  | 
|
4  | 
||
| 58889 | 5  | 
section {* Term evaluation using the generic code generator *}
 | 
| 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  | 
||
12  | 
subsection {* Term representation *}
 | 
|
13  | 
||
14  | 
subsubsection {* Terms and class @{text term_of} *}
 | 
|
15  | 
||
| 
58350
 
919149921e46
added 'extraction' plugins -- this might help 'HOL-Proofs'
 
blanchet 
parents: 
58334 
diff
changeset
 | 
16  | 
datatype (plugins only: code 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  | 
|
| 31178 | 47  | 
subsubsection {* Syntax *}
 | 
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  | 
|
55  | 
locale term_syntax  | 
|
56  | 
begin  | 
|
57  | 
||
| 31191 | 58  | 
notation App (infixl "<\<cdot>>" 70)  | 
59  | 
  and valapp (infixl "{\<cdot>}" 70)
 | 
|
| 31178 | 60  | 
|
61  | 
end  | 
|
62  | 
||
63  | 
interpretation term_syntax .  | 
|
64  | 
||
| 31191 | 65  | 
no_notation App (infixl "<\<cdot>>" 70)  | 
66  | 
  and valapp (infixl "{\<cdot>}" 70)
 | 
|
67  | 
||
68  | 
||
| 39564 | 69  | 
subsection {* Tools setup and evaluation *}
 | 
70  | 
||
| 
39567
 
5ee997fbe5cc
dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
 
haftmann 
parents: 
39564 
diff
changeset
 | 
71  | 
lemma eq_eq_TrueD:  | 
| 
 
5ee997fbe5cc
dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
 
haftmann 
parents: 
39564 
diff
changeset
 | 
72  | 
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
 | 
73  | 
shows "x \<equiv> y"  | 
| 
 
5ee997fbe5cc
dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
 
haftmann 
parents: 
39564 
diff
changeset
 | 
74  | 
using assms by simp  | 
| 
 
5ee997fbe5cc
dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
 
haftmann 
parents: 
39564 
diff
changeset
 | 
75  | 
|
| 
56928
 
1e50fddbe1f5
dropped term_of obfuscation -- not really required;
 
haftmann 
parents: 
56927 
diff
changeset
 | 
76  | 
code_printing  | 
| 
 
1e50fddbe1f5
dropped term_of obfuscation -- not really required;
 
haftmann 
parents: 
56927 
diff
changeset
 | 
77  | 
type_constructor "term" \<rightharpoonup> (Eval) "Term.term"  | 
| 
 
1e50fddbe1f5
dropped term_of obfuscation -- not really required;
 
haftmann 
parents: 
56927 
diff
changeset
 | 
78  | 
| constant Const \<rightharpoonup> (Eval) "Term.Const/ ((_), (_))"  | 
| 
 
1e50fddbe1f5
dropped term_of obfuscation -- not really required;
 
haftmann 
parents: 
56927 
diff
changeset
 | 
79  | 
| constant App \<rightharpoonup> (Eval) "Term.$/ ((_), (_))"  | 
| 
 
1e50fddbe1f5
dropped term_of obfuscation -- not really required;
 
haftmann 
parents: 
56927 
diff
changeset
 | 
80  | 
| constant Abs \<rightharpoonup> (Eval) "Term.Abs/ ((_), (_), (_))"  | 
| 
 
1e50fddbe1f5
dropped term_of obfuscation -- not really required;
 
haftmann 
parents: 
56927 
diff
changeset
 | 
81  | 
| constant Free \<rightharpoonup> (Eval) "Term.Free/ ((_), (_))"  | 
| 
 
1e50fddbe1f5
dropped term_of obfuscation -- not really required;
 
haftmann 
parents: 
56927 
diff
changeset
 | 
82  | 
|
| 48891 | 83  | 
ML_file "Tools/code_evaluation.ML"  | 
| 39564 | 84  | 
|
85  | 
code_reserved Eval Code_Evaluation  | 
|
86  | 
||
| 
56928
 
1e50fddbe1f5
dropped term_of obfuscation -- not really required;
 
haftmann 
parents: 
56927 
diff
changeset
 | 
87  | 
ML_file "~~/src/HOL/Tools/value.ML"  | 
| 
 
1e50fddbe1f5
dropped term_of obfuscation -- not really required;
 
haftmann 
parents: 
56927 
diff
changeset
 | 
88  | 
|
| 39564 | 89  | 
|
90  | 
subsection {* @{text term_of} instances *}
 | 
|
91  | 
||
92  | 
instantiation "fun" :: (typerep, typerep) term_of  | 
|
93  | 
begin  | 
|
94  | 
||
95  | 
definition  | 
|
| 56241 | 96  | 
"term_of (f \<Colon> 'a \<Rightarrow> 'b) =  | 
97  | 
Const (STR ''Pure.dummy_pattern'')  | 
|
98  | 
      (Typerep.Typerep (STR ''fun'') [Typerep.typerep TYPE('a), Typerep.typerep TYPE('b)])"
 | 
|
| 39564 | 99  | 
|
100  | 
instance ..  | 
|
101  | 
||
102  | 
end  | 
|
103  | 
||
| 58334 | 104  | 
declare [[code drop: rec_term case_term "HOL.equal :: term \<Rightarrow> _"  | 
| 56926 | 105  | 
"term_of :: typerep \<Rightarrow> _" "term_of :: term \<Rightarrow> _" "term_of :: String.literal \<Rightarrow> _"  | 
106  | 
"term_of :: _ Predicate.pred \<Rightarrow> term" "term_of :: _ Predicate.seq \<Rightarrow> term"]]  | 
|
| 39564 | 107  | 
|
108  | 
lemma term_of_char [unfolded typerep_fun_def typerep_char_def typerep_nibble_def, code]:  | 
|
| 
51160
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51144 
diff
changeset
 | 
109  | 
"Code_Evaluation.term_of c = (case c of Char x y \<Rightarrow>  | 
| 
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51144 
diff
changeset
 | 
110  | 
Code_Evaluation.App (Code_Evaluation.App  | 
| 39564 | 111  | 
(Code_Evaluation.Const (STR ''String.char.Char'') (TYPEREP(nibble \<Rightarrow> nibble \<Rightarrow> char)))  | 
| 
51160
 
599ff65b85e2
systematic conversions between nat and nibble/char;
 
haftmann 
parents: 
51144 
diff
changeset
 | 
112  | 
(Code_Evaluation.term_of x)) (Code_Evaluation.term_of y))"  | 
| 39564 | 113  | 
by (subst term_of_anything) rule  | 
114  | 
||
| 
59484
 
a130ae7a9398
slightly more standard code setup for String.literal, with explicit special case in predicate compiler
 
haftmann 
parents: 
58889 
diff
changeset
 | 
115  | 
lemma term_of_string [code]:  | 
| 
 
a130ae7a9398
slightly more standard code setup for String.literal, with explicit special case in predicate compiler
 
haftmann 
parents: 
58889 
diff
changeset
 | 
116  | 
"term_of s = App (Const (STR ''STR'')  | 
| 
 
a130ae7a9398
slightly more standard code setup for String.literal, with explicit special case in predicate compiler
 
haftmann 
parents: 
58889 
diff
changeset
 | 
117  | 
(Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''list'') [Typerep.Typerep (STR ''char'') []],  | 
| 
 
a130ae7a9398
slightly more standard code setup for String.literal, with explicit special case in predicate compiler
 
haftmann 
parents: 
58889 
diff
changeset
 | 
118  | 
Typerep.Typerep (STR ''String.literal'') []])) (term_of (String.explode s))"  | 
| 
 
a130ae7a9398
slightly more standard code setup for String.literal, with explicit special case in predicate compiler
 
haftmann 
parents: 
58889 
diff
changeset
 | 
119  | 
by (subst term_of_anything) rule  | 
| 
 
a130ae7a9398
slightly more standard code setup for String.literal, with explicit special case in predicate compiler
 
haftmann 
parents: 
58889 
diff
changeset
 | 
120  | 
|
| 
52435
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
52286 
diff
changeset
 | 
121  | 
code_printing  | 
| 
56928
 
1e50fddbe1f5
dropped term_of obfuscation -- not really required;
 
haftmann 
parents: 
56927 
diff
changeset
 | 
122  | 
constant "term_of \<Colon> integer \<Rightarrow> term" \<rightharpoonup> (Eval) "HOLogic.mk'_number/ HOLogic.code'_integerT"  | 
| 
52435
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
52286 
diff
changeset
 | 
123  | 
| constant "term_of \<Colon> String.literal \<Rightarrow> term" \<rightharpoonup> (Eval) "HOLogic.mk'_literal"  | 
| 39564 | 124  | 
|
| 
58036
 
f23045003476
add code equation for term_of on integer
 
Andreas Lochbihler 
parents: 
56928 
diff
changeset
 | 
125  | 
declare [[code drop: "term_of :: integer \<Rightarrow> _"]]  | 
| 
 
f23045003476
add code equation for term_of on integer
 
Andreas Lochbihler 
parents: 
56928 
diff
changeset
 | 
126  | 
|
| 
 
f23045003476
add code equation for term_of on integer
 
Andreas Lochbihler 
parents: 
56928 
diff
changeset
 | 
127  | 
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
 | 
128  | 
"term_of (i :: integer) =  | 
| 
 
f8e6197668c9
correct code equation for term_of on integer
 
Andreas Lochbihler 
parents: 
58036 
diff
changeset
 | 
129  | 
(if i > 0 then  | 
| 
 
f8e6197668c9
correct code equation for term_of on integer
 
Andreas Lochbihler 
parents: 
58036 
diff
changeset
 | 
130  | 
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
 | 
131  | 
(term_of (num_of_integer i))  | 
| 
 
f8e6197668c9
correct code equation for term_of on integer
 
Andreas Lochbihler 
parents: 
58036 
diff
changeset
 | 
132  | 
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
 | 
133  | 
else  | 
| 
 
f8e6197668c9
correct code equation for term_of on integer
 
Andreas Lochbihler 
parents: 
58036 
diff
changeset
 | 
134  | 
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
 | 
135  | 
(term_of (- i)))"  | 
| 
58036
 
f23045003476
add code equation for term_of on integer
 
Andreas Lochbihler 
parents: 
56928 
diff
changeset
 | 
136  | 
by(rule term_of_anything[THEN meta_eq_to_obj_eq])  | 
| 
 
f23045003476
add code equation for term_of on integer
 
Andreas Lochbihler 
parents: 
56928 
diff
changeset
 | 
137  | 
|
| 39564 | 138  | 
code_reserved Eval HOLogic  | 
139  | 
||
140  | 
||
| 
56928
 
1e50fddbe1f5
dropped term_of obfuscation -- not really required;
 
haftmann 
parents: 
56927 
diff
changeset
 | 
141  | 
subsection {* Generic reification *}
 | 
| 31178 | 142  | 
|
| 
56928
 
1e50fddbe1f5
dropped term_of obfuscation -- not really required;
 
haftmann 
parents: 
56927 
diff
changeset
 | 
143  | 
ML_file "~~/src/HOL/Tools/reification.ML"  | 
| 31178 | 144  | 
|
145  | 
||
| 39564 | 146  | 
subsection {* Diagnostic *}
 | 
| 
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
 | 
147  | 
|
| 
 
6608c4838ff9
replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references; tuned structures
 
haftmann 
parents: 
39274 
diff
changeset
 | 
148  | 
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
 | 
149  | 
[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
 | 
150  | 
|
| 
52435
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
52286 
diff
changeset
 | 
151  | 
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
 | 
152  | 
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
 | 
153  | 
|
| 31178 | 154  | 
|
| 
40638
 
6b137c96df07
adding dummy definition for Code_Evaluation.Abs and hiding constants App less strict
 
bulwahn 
parents: 
39567 
diff
changeset
 | 
155  | 
hide_const dummy_term valapp  | 
| 51144 | 156  | 
hide_const (open) Const App Abs Free termify valtermify term_of tracing  | 
| 28228 | 157  | 
|
158  | 
end  | 
|
| 
51143
 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 
haftmann 
parents: 
51126 
diff
changeset
 | 
159  |