| author | wenzelm | 
| Sat, 22 Mar 2014 18:16:54 +0100 | |
| changeset 56253 | 83b3c110f22d | 
| parent 56241 | 029246729dc0 | 
| child 56925 | 601edd9a6859 | 
| permissions | -rw-r--r-- | 
| 32657 | 1  | 
(* Title: HOL/Code_Evaluation.thy  | 
| 28228 | 2  | 
Author: Florian Haftmann, TU Muenchen  | 
3  | 
*)  | 
|
4  | 
||
5  | 
header {* Term evaluation using the generic code generator *}
 | 
|
6  | 
||
| 32657 | 7  | 
theory Code_Evaluation  | 
| 
51126
 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 
haftmann 
parents: 
51112 
diff
changeset
 | 
8  | 
imports Typerep Limited_Sequence  | 
| 28228 | 9  | 
begin  | 
10  | 
||
11  | 
subsection {* Term representation *}
 | 
|
12  | 
||
13  | 
subsubsection {* Terms and class @{text term_of} *}
 | 
|
14  | 
||
15  | 
datatype "term" = dummy_term  | 
|
16  | 
||
| 
31205
 
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
 
haftmann 
parents: 
31203 
diff
changeset
 | 
17  | 
definition Const :: "String.literal \<Rightarrow> typerep \<Rightarrow> term" where  | 
| 28228 | 18  | 
"Const _ _ = dummy_term"  | 
19  | 
||
| 28661 | 20  | 
definition App :: "term \<Rightarrow> term \<Rightarrow> term" where  | 
| 28228 | 21  | 
"App _ _ = dummy_term"  | 
22  | 
||
| 
40638
 
6b137c96df07
adding dummy definition for Code_Evaluation.Abs and hiding constants App less strict
 
bulwahn 
parents: 
39567 
diff
changeset
 | 
23  | 
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
 | 
24  | 
"Abs _ _ _ = dummy_term"  | 
| 
 
6b137c96df07
adding dummy definition for Code_Evaluation.Abs and hiding constants App less strict
 
bulwahn 
parents: 
39567 
diff
changeset
 | 
25  | 
|
| 
42979
 
5b9e16259341
extending terms of Code_Evaluation by Free to allow partial terms
 
bulwahn 
parents: 
40884 
diff
changeset
 | 
26  | 
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
 | 
27  | 
"Free _ _ = dummy_term"  | 
| 
 
5b9e16259341
extending terms of Code_Evaluation by Free to allow partial terms
 
bulwahn 
parents: 
40884 
diff
changeset
 | 
28  | 
|
| 
 
5b9e16259341
extending terms of Code_Evaluation by Free to allow partial terms
 
bulwahn 
parents: 
40884 
diff
changeset
 | 
29  | 
code_datatype Const App Abs Free  | 
| 28228 | 30  | 
|
| 28335 | 31  | 
class term_of = typerep +  | 
| 31031 | 32  | 
fixes term_of :: "'a \<Rightarrow> term"  | 
| 28228 | 33  | 
|
34  | 
lemma term_of_anything: "term_of x \<equiv> t"  | 
|
35  | 
by (rule eq_reflection) (cases "term_of x", cases t, simp)  | 
|
36  | 
||
| 31191 | 37  | 
definition valapp :: "('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)
 | 
| 31178 | 38  | 
\<Rightarrow> 'a \<times> (unit \<Rightarrow> term) \<Rightarrow> 'b \<times> (unit \<Rightarrow> term)" where  | 
| 31191 | 39  | 
"valapp f x = (fst f (fst x), \<lambda>u. App (snd f ()) (snd x ()))"  | 
| 31178 | 40  | 
|
| 
32069
 
6d28bbd33e2c
prefer code_inline over code_unfold; use code_unfold_post where appropriate
 
haftmann 
parents: 
31998 
diff
changeset
 | 
41  | 
lemma valapp_code [code, code_unfold]:  | 
| 31191 | 42  | 
"valapp (f, tf) (x, tx) = (f x, \<lambda>u. App (tf ()) (tx ()))"  | 
43  | 
by (simp only: valapp_def fst_conv snd_conv)  | 
|
| 31178 | 44  | 
|
| 28228 | 45  | 
|
| 31178 | 46  | 
subsubsection {* Syntax *}
 | 
47  | 
||
| 31191 | 48  | 
definition termify :: "'a \<Rightarrow> term" where  | 
49  | 
[code del]: "termify x = dummy_term"  | 
|
50  | 
||
51  | 
abbreviation valtermify :: "'a \<Rightarrow> 'a \<times> (unit \<Rightarrow> term)" where  | 
|
52  | 
"valtermify x \<equiv> (x, \<lambda>u. termify x)"  | 
|
| 31178 | 53  | 
|
54  | 
locale term_syntax  | 
|
55  | 
begin  | 
|
56  | 
||
| 31191 | 57  | 
notation App (infixl "<\<cdot>>" 70)  | 
58  | 
  and valapp (infixl "{\<cdot>}" 70)
 | 
|
| 31178 | 59  | 
|
60  | 
end  | 
|
61  | 
||
62  | 
interpretation term_syntax .  | 
|
63  | 
||
| 31191 | 64  | 
no_notation App (infixl "<\<cdot>>" 70)  | 
65  | 
  and valapp (infixl "{\<cdot>}" 70)
 | 
|
66  | 
||
67  | 
||
| 39564 | 68  | 
subsection {* Tools setup and evaluation *}
 | 
69  | 
||
| 
39567
 
5ee997fbe5cc
dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
 
haftmann 
parents: 
39564 
diff
changeset
 | 
70  | 
lemma eq_eq_TrueD:  | 
| 
 
5ee997fbe5cc
dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
 
haftmann 
parents: 
39564 
diff
changeset
 | 
71  | 
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
 | 
72  | 
shows "x \<equiv> y"  | 
| 
 
5ee997fbe5cc
dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
 
haftmann 
parents: 
39564 
diff
changeset
 | 
73  | 
using assms by simp  | 
| 
 
5ee997fbe5cc
dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
 
haftmann 
parents: 
39564 
diff
changeset
 | 
74  | 
|
| 48891 | 75  | 
ML_file "Tools/code_evaluation.ML"  | 
| 39564 | 76  | 
|
77  | 
code_reserved Eval Code_Evaluation  | 
|
78  | 
||
79  | 
setup {* Code_Evaluation.setup *}
 | 
|
80  | 
||
81  | 
||
82  | 
subsection {* @{text term_of} instances *}
 | 
|
83  | 
||
84  | 
instantiation "fun" :: (typerep, typerep) term_of  | 
|
85  | 
begin  | 
|
86  | 
||
87  | 
definition  | 
|
| 56241 | 88  | 
"term_of (f \<Colon> 'a \<Rightarrow> 'b) =  | 
89  | 
Const (STR ''Pure.dummy_pattern'')  | 
|
90  | 
      (Typerep.Typerep (STR ''fun'') [Typerep.typerep TYPE('a), Typerep.typerep TYPE('b)])"
 | 
|
| 39564 | 91  | 
|
92  | 
instance ..  | 
|
93  | 
||
94  | 
end  | 
|
95  | 
||
96  | 
instantiation String.literal :: term_of  | 
|
97  | 
begin  | 
|
98  | 
||
99  | 
definition  | 
|
100  | 
"term_of s = App (Const (STR ''STR'')  | 
|
101  | 
(Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''list'') [Typerep.Typerep (STR ''char'') []],  | 
|
102  | 
Typerep.Typerep (STR ''String.literal'') []])) (term_of (String.explode s))"  | 
|
103  | 
||
104  | 
instance ..  | 
|
105  | 
||
106  | 
end  | 
|
107  | 
||
108  | 
||
109  | 
subsubsection {* Code generator setup *}
 | 
|
110  | 
||
| 
55642
 
63beb38e9258
adapted to renaming of datatype 'cases' and 'recs' to 'case' and 'rec'
 
blanchet 
parents: 
52435 
diff
changeset
 | 
111  | 
lemmas [code del] = term.rec term.case term.size  | 
| 39564 | 112  | 
lemma [code, code del]: "HOL.equal (t1\<Colon>term) t2 \<longleftrightarrow> HOL.equal t1 t2" ..  | 
113  | 
||
114  | 
lemma [code, code del]: "(term_of \<Colon> typerep \<Rightarrow> term) = term_of" ..  | 
|
115  | 
lemma [code, code del]: "(term_of \<Colon> term \<Rightarrow> term) = term_of" ..  | 
|
116  | 
lemma [code, code del]: "(term_of \<Colon> String.literal \<Rightarrow> term) = term_of" ..  | 
|
117  | 
lemma [code, code del]: "(Code_Evaluation.term_of \<Colon> 'a::{type, term_of} Predicate.pred \<Rightarrow> Code_Evaluation.term)
 | 
|
118  | 
= Code_Evaluation.term_of" ..  | 
|
119  | 
lemma [code, code del]: "(Code_Evaluation.term_of \<Colon> 'a::{type, term_of} Predicate.seq \<Rightarrow> Code_Evaluation.term)
 | 
|
120  | 
= Code_Evaluation.term_of" ..  | 
|
121  | 
||
122  | 
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
 | 
123  | 
"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
 | 
124  | 
Code_Evaluation.App (Code_Evaluation.App  | 
| 39564 | 125  | 
(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
 | 
126  | 
(Code_Evaluation.term_of x)) (Code_Evaluation.term_of y))"  | 
| 39564 | 127  | 
by (subst term_of_anything) rule  | 
128  | 
||
| 
52435
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
52286 
diff
changeset
 | 
129  | 
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
 | 
130  | 
type_constructor "term" \<rightharpoonup> (Eval) "Term.term"  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
52286 
diff
changeset
 | 
131  | 
| constant Const \<rightharpoonup> (Eval) "Term.Const/ ((_), (_))"  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
52286 
diff
changeset
 | 
132  | 
| constant App \<rightharpoonup> (Eval) "Term.$/ ((_), (_))"  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
52286 
diff
changeset
 | 
133  | 
| constant Abs \<rightharpoonup> (Eval) "Term.Abs/ ((_), (_), (_))"  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
52286 
diff
changeset
 | 
134  | 
| constant Free \<rightharpoonup> (Eval) "Term.Free/ ((_), (_))"  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
52286 
diff
changeset
 | 
135  | 
| constant "term_of \<Colon> integer \<Rightarrow> term" \<rightharpoonup> (Eval) "HOLogic.mk'_number/ HOLogic.code'_integerT"  | 
| 
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
52286 
diff
changeset
 | 
136  | 
| constant "term_of \<Colon> String.literal \<Rightarrow> term" \<rightharpoonup> (Eval) "HOLogic.mk'_literal"  | 
| 39564 | 137  | 
|
138  | 
code_reserved Eval HOLogic  | 
|
139  | 
||
140  | 
||
141  | 
subsubsection {* Obfuscation *}
 | 
|
| 31178 | 142  | 
|
143  | 
print_translation {*
 | 
|
| 52143 | 144  | 
let  | 
145  | 
    val term = Const ("<TERM>", dummyT);
 | 
|
146  | 
fun tr1' _ [_, _] = term;  | 
|
147  | 
fun tr2' _ [] = term;  | 
|
148  | 
in  | 
|
149  | 
   [(@{const_syntax Const}, tr1'),
 | 
|
| 31178 | 150  | 
    (@{const_syntax App}, tr1'),
 | 
151  | 
    (@{const_syntax dummy_term}, tr2')]
 | 
|
| 52143 | 152  | 
end  | 
| 31178 | 153  | 
*}  | 
154  | 
||
155  | 
||
| 39564 | 156  | 
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
 | 
157  | 
|
| 
 
6608c4838ff9
replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references; tuned structures
 
haftmann 
parents: 
39274 
diff
changeset
 | 
158  | 
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
 | 
159  | 
[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
 | 
160  | 
|
| 
52435
 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 
haftmann 
parents: 
52286 
diff
changeset
 | 
161  | 
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
 | 
162  | 
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
 | 
163  | 
|
| 31178 | 164  | 
|
| 52286 | 165  | 
subsection {* Generic reification *}
 | 
166  | 
||
167  | 
ML_file "~~/src/HOL/Tools/reification.ML"  | 
|
168  | 
||
169  | 
||
| 
40638
 
6b137c96df07
adding dummy definition for Code_Evaluation.Abs and hiding constants App less strict
 
bulwahn 
parents: 
39567 
diff
changeset
 | 
170  | 
hide_const dummy_term valapp  | 
| 51144 | 171  | 
hide_const (open) Const App Abs Free termify valtermify term_of tracing  | 
| 28228 | 172  | 
|
173  | 
end  | 
|
| 
51143
 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 
haftmann 
parents: 
51126 
diff
changeset
 | 
174  |