| author | bulwahn | 
| Mon, 22 Nov 2010 10:41:56 +0100 | |
| changeset 40636 | 3bd9512ca486 | 
| parent 39567 | 5ee997fbe5cc | 
| child 40638 | 6b137c96df07 | 
| 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  | 
| 
31205
 
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
 
haftmann 
parents: 
31203 
diff
changeset
 | 
8  | 
imports Plain Typerep Code_Numeral  | 
| 39564 | 9  | 
uses ("Tools/code_evaluation.ML")
 | 
| 28228 | 10  | 
begin  | 
11  | 
||
12  | 
subsection {* Term representation *}
 | 
|
13  | 
||
14  | 
subsubsection {* Terms and class @{text term_of} *}
 | 
|
15  | 
||
16  | 
datatype "term" = dummy_term  | 
|
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  | 
||
24  | 
code_datatype Const App  | 
|
25  | 
||
| 28335 | 26  | 
class term_of = typerep +  | 
| 31031 | 27  | 
fixes term_of :: "'a \<Rightarrow> term"  | 
| 28228 | 28  | 
|
29  | 
lemma term_of_anything: "term_of x \<equiv> t"  | 
|
30  | 
by (rule eq_reflection) (cases "term_of x", cases t, simp)  | 
|
31  | 
||
| 31191 | 32  | 
definition valapp :: "('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)
 | 
| 31178 | 33  | 
\<Rightarrow> 'a \<times> (unit \<Rightarrow> term) \<Rightarrow> 'b \<times> (unit \<Rightarrow> term)" where  | 
| 31191 | 34  | 
"valapp f x = (fst f (fst x), \<lambda>u. App (snd f ()) (snd x ()))"  | 
| 31178 | 35  | 
|
| 
32069
 
6d28bbd33e2c
prefer code_inline over code_unfold; use code_unfold_post where appropriate
 
haftmann 
parents: 
31998 
diff
changeset
 | 
36  | 
lemma valapp_code [code, code_unfold]:  | 
| 31191 | 37  | 
"valapp (f, tf) (x, tx) = (f x, \<lambda>u. App (tf ()) (tx ()))"  | 
38  | 
by (simp only: valapp_def fst_conv snd_conv)  | 
|
| 31178 | 39  | 
|
| 28228 | 40  | 
|
| 31178 | 41  | 
subsubsection {* Syntax *}
 | 
42  | 
||
| 31191 | 43  | 
definition termify :: "'a \<Rightarrow> term" where  | 
44  | 
[code del]: "termify x = dummy_term"  | 
|
45  | 
||
46  | 
abbreviation valtermify :: "'a \<Rightarrow> 'a \<times> (unit \<Rightarrow> term)" where  | 
|
47  | 
"valtermify x \<equiv> (x, \<lambda>u. termify x)"  | 
|
| 31178 | 48  | 
|
49  | 
locale term_syntax  | 
|
50  | 
begin  | 
|
51  | 
||
| 31191 | 52  | 
notation App (infixl "<\<cdot>>" 70)  | 
53  | 
  and valapp (infixl "{\<cdot>}" 70)
 | 
|
| 31178 | 54  | 
|
55  | 
end  | 
|
56  | 
||
57  | 
interpretation term_syntax .  | 
|
58  | 
||
| 31191 | 59  | 
no_notation App (infixl "<\<cdot>>" 70)  | 
60  | 
  and valapp (infixl "{\<cdot>}" 70)
 | 
|
61  | 
||
62  | 
||
| 39564 | 63  | 
subsection {* Tools setup and evaluation *}
 | 
64  | 
||
| 
39567
 
5ee997fbe5cc
dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
 
haftmann 
parents: 
39564 
diff
changeset
 | 
65  | 
lemma eq_eq_TrueD:  | 
| 
 
5ee997fbe5cc
dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
 
haftmann 
parents: 
39564 
diff
changeset
 | 
66  | 
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
 | 
67  | 
shows "x \<equiv> y"  | 
| 
 
5ee997fbe5cc
dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
 
haftmann 
parents: 
39564 
diff
changeset
 | 
68  | 
using assms by simp  | 
| 
 
5ee997fbe5cc
dynamic_eval_conv static_eval_conv: certification of previously unreliably reconstructed evaluated term
 
haftmann 
parents: 
39564 
diff
changeset
 | 
69  | 
|
| 39564 | 70  | 
use "Tools/code_evaluation.ML"  | 
71  | 
||
72  | 
code_reserved Eval Code_Evaluation  | 
|
73  | 
||
74  | 
setup {* Code_Evaluation.setup *}
 | 
|
75  | 
||
76  | 
||
77  | 
subsection {* @{text term_of} instances *}
 | 
|
78  | 
||
79  | 
instantiation "fun" :: (typerep, typerep) term_of  | 
|
80  | 
begin  | 
|
81  | 
||
82  | 
definition  | 
|
83  | 
"term_of (f \<Colon> 'a \<Rightarrow> 'b) = Const (STR ''dummy_pattern'') (Typerep.Typerep (STR ''fun'')  | 
|
84  | 
     [Typerep.typerep TYPE('a), Typerep.typerep TYPE('b)])"
 | 
|
85  | 
||
86  | 
instance ..  | 
|
87  | 
||
88  | 
end  | 
|
89  | 
||
90  | 
instantiation String.literal :: term_of  | 
|
91  | 
begin  | 
|
92  | 
||
93  | 
definition  | 
|
94  | 
"term_of s = App (Const (STR ''STR'')  | 
|
95  | 
(Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''list'') [Typerep.Typerep (STR ''char'') []],  | 
|
96  | 
Typerep.Typerep (STR ''String.literal'') []])) (term_of (String.explode s))"  | 
|
97  | 
||
98  | 
instance ..  | 
|
99  | 
||
100  | 
end  | 
|
101  | 
||
102  | 
||
103  | 
subsubsection {* Code generator setup *}
 | 
|
104  | 
||
105  | 
lemmas [code del] = term.recs term.cases term.size  | 
|
106  | 
lemma [code, code del]: "HOL.equal (t1\<Colon>term) t2 \<longleftrightarrow> HOL.equal t1 t2" ..  | 
|
107  | 
||
108  | 
lemma [code, code del]: "(term_of \<Colon> typerep \<Rightarrow> term) = term_of" ..  | 
|
109  | 
lemma [code, code del]: "(term_of \<Colon> term \<Rightarrow> term) = term_of" ..  | 
|
110  | 
lemma [code, code del]: "(term_of \<Colon> String.literal \<Rightarrow> term) = term_of" ..  | 
|
111  | 
lemma [code, code del]: "(Code_Evaluation.term_of \<Colon> 'a::{type, term_of} Predicate.pred \<Rightarrow> Code_Evaluation.term)
 | 
|
112  | 
= Code_Evaluation.term_of" ..  | 
|
113  | 
lemma [code, code del]: "(Code_Evaluation.term_of \<Colon> 'a::{type, term_of} Predicate.seq \<Rightarrow> Code_Evaluation.term)
 | 
|
114  | 
= Code_Evaluation.term_of" ..  | 
|
115  | 
||
116  | 
lemma term_of_char [unfolded typerep_fun_def typerep_char_def typerep_nibble_def, code]:  | 
|
117  | 
"Code_Evaluation.term_of c =  | 
|
118  | 
(let (n, m) = nibble_pair_of_char c  | 
|
119  | 
in Code_Evaluation.App (Code_Evaluation.App  | 
|
120  | 
(Code_Evaluation.Const (STR ''String.char.Char'') (TYPEREP(nibble \<Rightarrow> nibble \<Rightarrow> char)))  | 
|
121  | 
(Code_Evaluation.term_of n)) (Code_Evaluation.term_of m))"  | 
|
122  | 
by (subst term_of_anything) rule  | 
|
123  | 
||
124  | 
code_type "term"  | 
|
125  | 
(Eval "Term.term")  | 
|
126  | 
||
127  | 
code_const Const and App  | 
|
128  | 
(Eval "Term.Const/ ((_), (_))" and "Term.$/ ((_), (_))")  | 
|
129  | 
||
130  | 
code_const "term_of \<Colon> String.literal \<Rightarrow> term"  | 
|
131  | 
(Eval "HOLogic.mk'_literal")  | 
|
132  | 
||
133  | 
code_reserved Eval HOLogic  | 
|
134  | 
||
135  | 
||
136  | 
subsubsection {* Numeric types *}
 | 
|
| 31191 | 137  | 
|
138  | 
definition term_of_num :: "'a\<Colon>{semiring_div} \<Rightarrow> 'a\<Colon>{semiring_div} \<Rightarrow> term" where
 | 
|
139  | 
"term_of_num two = (\<lambda>_. dummy_term)"  | 
|
140  | 
||
141  | 
lemma (in term_syntax) term_of_num_code [code]:  | 
|
142  | 
"term_of_num two k = (if k = 0 then termify Int.Pls  | 
|
143  | 
else (if k mod two = 0  | 
|
144  | 
then termify Int.Bit0 <\<cdot>> term_of_num two (k div two)  | 
|
145  | 
else termify Int.Bit1 <\<cdot>> term_of_num two (k div two)))"  | 
|
146  | 
by (auto simp add: term_of_anything Const_def App_def term_of_num_def Let_def)  | 
|
147  | 
||
148  | 
lemma (in term_syntax) term_of_nat_code [code]:  | 
|
149  | 
"term_of (n::nat) = termify (number_of :: int \<Rightarrow> nat) <\<cdot>> term_of_num (2::nat) n"  | 
|
150  | 
by (simp only: term_of_anything)  | 
|
151  | 
||
152  | 
lemma (in term_syntax) term_of_int_code [code]:  | 
|
153  | 
"term_of (k::int) = (if k = 0 then termify (0 :: int)  | 
|
154  | 
else if k > 0 then termify (number_of :: int \<Rightarrow> int) <\<cdot>> term_of_num (2::int) k  | 
|
155  | 
else termify (uminus :: int \<Rightarrow> int) <\<cdot>> (termify (number_of :: int \<Rightarrow> int) <\<cdot>> term_of_num (2::int) (- k)))"  | 
|
156  | 
by (simp only: term_of_anything)  | 
|
157  | 
||
| 
31205
 
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
 
haftmann 
parents: 
31203 
diff
changeset
 | 
158  | 
lemma (in term_syntax) term_of_code_numeral_code [code]:  | 
| 
 
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
 
haftmann 
parents: 
31203 
diff
changeset
 | 
159  | 
"term_of (k::code_numeral) = termify (number_of :: int \<Rightarrow> code_numeral) <\<cdot>> term_of_num (2::code_numeral) k"  | 
| 
31203
 
5c8fb4fd67e0
moved Code_Index, Random and Quickcheck before Main
 
haftmann 
parents: 
31191 
diff
changeset
 | 
160  | 
by (simp only: term_of_anything)  | 
| 31191 | 161  | 
|
| 
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
 | 
162  | 
|
| 39564 | 163  | 
subsubsection {* Obfuscation *}
 | 
| 31178 | 164  | 
|
165  | 
print_translation {*
 | 
|
166  | 
let  | 
|
167  | 
  val term = Const ("<TERM>", dummyT);
 | 
|
168  | 
fun tr1' [_, _] = term;  | 
|
169  | 
fun tr2' [] = term;  | 
|
170  | 
in  | 
|
171  | 
  [(@{const_syntax Const}, tr1'),
 | 
|
172  | 
    (@{const_syntax App}, tr1'),
 | 
|
173  | 
    (@{const_syntax dummy_term}, tr2')]
 | 
|
174  | 
end  | 
|
175  | 
*}  | 
|
176  | 
||
177  | 
||
| 39564 | 178  | 
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
 | 
179  | 
|
| 
 
6608c4838ff9
replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references; tuned structures
 
haftmann 
parents: 
39274 
diff
changeset
 | 
180  | 
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
 | 
181  | 
[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
 | 
182  | 
|
| 
33473
 
3b275a0bf18c
adding tracing function for evaluated code; annotated compilation in the predicate compiler
 
bulwahn 
parents: 
32740 
diff
changeset
 | 
183  | 
code_const "tracing :: String.literal => 'a => 'a"  | 
| 
 
3b275a0bf18c
adding tracing function for evaluated code; annotated compilation in the predicate compiler
 
bulwahn 
parents: 
32740 
diff
changeset
 | 
184  | 
(Eval "Code'_Evaluation.tracing")  | 
| 
 
3b275a0bf18c
adding tracing function for evaluated code; annotated compilation in the predicate compiler
 
bulwahn 
parents: 
32740 
diff
changeset
 | 
185  | 
|
| 31178 | 186  | 
|
| 
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
 | 
187  | 
hide_const dummy_term App valapp  | 
| 
 
6608c4838ff9
replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references; tuned structures
 
haftmann 
parents: 
39274 
diff
changeset
 | 
188  | 
hide_const (open) Const termify valtermify term_of term_of_num tracing  | 
| 28228 | 189  | 
|
190  | 
end  |