3 *) |
3 *) |
4 |
4 |
5 header {* Term evaluation using the generic code generator *} |
5 header {* Term evaluation using the generic code generator *} |
6 |
6 |
7 theory Code_Eval |
7 theory Code_Eval |
8 imports Plain Typerep Code_Index |
8 imports Plain Typerep Code_Numeral |
9 begin |
9 begin |
10 |
10 |
11 subsection {* Term representation *} |
11 subsection {* Term representation *} |
12 |
12 |
13 subsubsection {* Terms and class @{text term_of} *} |
13 subsubsection {* Terms and class @{text term_of} *} |
14 |
14 |
15 datatype "term" = dummy_term |
15 datatype "term" = dummy_term |
16 |
16 |
17 definition Const :: "message_string \<Rightarrow> typerep \<Rightarrow> term" where |
17 definition Const :: "String.literal \<Rightarrow> typerep \<Rightarrow> term" where |
18 "Const _ _ = dummy_term" |
18 "Const _ _ = dummy_term" |
19 |
19 |
20 definition App :: "term \<Rightarrow> term \<Rightarrow> term" where |
20 definition App :: "term \<Rightarrow> term \<Rightarrow> term" where |
21 "App _ _ = dummy_term" |
21 "App _ _ = dummy_term" |
22 |
22 |
61 end; |
61 end; |
62 fun ensure_term_of (tyco, (raw_vs, _)) thy = |
62 fun ensure_term_of (tyco, (raw_vs, _)) thy = |
63 let |
63 let |
64 val need_inst = not (can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of}) |
64 val need_inst = not (can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of}) |
65 andalso can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort typerep}; |
65 andalso can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort typerep}; |
66 in |
66 in if need_inst then add_term_of tyco raw_vs thy else thy end; |
67 thy |
|
68 |> need_inst ? add_term_of tyco raw_vs |
|
69 end; |
|
70 in |
67 in |
71 Code.type_interpretation ensure_term_of |
68 Code.type_interpretation ensure_term_of |
72 end |
69 end |
73 *} |
70 *} |
74 |
71 |
100 |> fold Code.add_eqn eqs |
97 |> fold Code.add_eqn eqs |
101 end; |
98 end; |
102 fun ensure_term_of_code (tyco, (raw_vs, cs)) thy = |
99 fun ensure_term_of_code (tyco, (raw_vs, cs)) thy = |
103 let |
100 let |
104 val has_inst = can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of}; |
101 val has_inst = can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of}; |
105 in |
102 in if has_inst then add_term_of_code tyco raw_vs cs thy else thy end; |
106 thy |
|
107 |> has_inst ? add_term_of_code tyco raw_vs cs |
|
108 end; |
|
109 in |
103 in |
110 Code.type_interpretation ensure_term_of_code |
104 Code.type_interpretation ensure_term_of_code |
111 end |
105 end |
112 *} |
106 *} |
113 |
107 |
117 lemmas [code del] = term.recs term.cases term.size |
111 lemmas [code del] = term.recs term.cases term.size |
118 lemma [code, code del]: "eq_class.eq (t1\<Colon>term) t2 \<longleftrightarrow> eq_class.eq t1 t2" .. |
112 lemma [code, code del]: "eq_class.eq (t1\<Colon>term) t2 \<longleftrightarrow> eq_class.eq t1 t2" .. |
119 |
113 |
120 lemma [code, code del]: "(term_of \<Colon> typerep \<Rightarrow> term) = term_of" .. |
114 lemma [code, code del]: "(term_of \<Colon> typerep \<Rightarrow> term) = term_of" .. |
121 lemma [code, code del]: "(term_of \<Colon> term \<Rightarrow> term) = term_of" .. |
115 lemma [code, code del]: "(term_of \<Colon> term \<Rightarrow> term) = term_of" .. |
122 lemma [code, code del]: "(term_of \<Colon> message_string \<Rightarrow> term) = term_of" .. |
116 lemma [code, code del]: "(term_of \<Colon> String.literal \<Rightarrow> term) = term_of" .. |
123 lemma [code, code del]: |
117 lemma [code, code del]: |
124 "(Code_Eval.term_of \<Colon> 'a::{type, term_of} Predicate.pred \<Rightarrow> Code_Eval.term) = Code_Eval.term_of" .. |
118 "(Code_Eval.term_of \<Colon> 'a::{type, term_of} Predicate.pred \<Rightarrow> Code_Eval.term) = Code_Eval.term_of" .. |
125 lemma [code, code del]: |
119 lemma [code, code del]: |
126 "(Code_Eval.term_of \<Colon> 'a::{type, term_of} Predicate.seq \<Rightarrow> Code_Eval.term) = Code_Eval.term_of" .. |
120 "(Code_Eval.term_of \<Colon> 'a::{type, term_of} Predicate.seq \<Rightarrow> Code_Eval.term) = Code_Eval.term_of" .. |
127 |
121 |
135 (Eval "Term.term") |
129 (Eval "Term.term") |
136 |
130 |
137 code_const Const and App |
131 code_const Const and App |
138 (Eval "Term.Const/ (_, _)" and "Term.$/ (_, _)") |
132 (Eval "Term.Const/ (_, _)" and "Term.$/ (_, _)") |
139 |
133 |
140 code_const "term_of \<Colon> message_string \<Rightarrow> term" |
134 code_const "term_of \<Colon> String.literal \<Rightarrow> term" |
141 (Eval "HOLogic.mk'_message'_string") |
135 (Eval "HOLogic.mk'_message'_string") |
142 |
136 |
143 code_reserved Eval HOLogic |
137 code_reserved Eval HOLogic |
144 |
138 |
145 |
139 |
213 "term_of (k::int) = (if k = 0 then termify (0 :: int) |
207 "term_of (k::int) = (if k = 0 then termify (0 :: int) |
214 else if k > 0 then termify (number_of :: int \<Rightarrow> int) <\<cdot>> term_of_num (2::int) k |
208 else if k > 0 then termify (number_of :: int \<Rightarrow> int) <\<cdot>> term_of_num (2::int) k |
215 else termify (uminus :: int \<Rightarrow> int) <\<cdot>> (termify (number_of :: int \<Rightarrow> int) <\<cdot>> term_of_num (2::int) (- k)))" |
209 else termify (uminus :: int \<Rightarrow> int) <\<cdot>> (termify (number_of :: int \<Rightarrow> int) <\<cdot>> term_of_num (2::int) (- k)))" |
216 by (simp only: term_of_anything) |
210 by (simp only: term_of_anything) |
217 |
211 |
218 lemma (in term_syntax) term_of_index_code [code]: |
212 lemma (in term_syntax) term_of_code_numeral_code [code]: |
219 "term_of (k::index) = termify (number_of :: int \<Rightarrow> index) <\<cdot>> term_of_num (2::index) k" |
213 "term_of (k::code_numeral) = termify (number_of :: int \<Rightarrow> code_numeral) <\<cdot>> term_of_num (2::code_numeral) k" |
220 by (simp only: term_of_anything) |
214 by (simp only: term_of_anything) |
221 |
215 |
222 subsection {* Obfuscate *} |
216 subsection {* Obfuscate *} |
223 |
217 |
224 print_translation {* |
218 print_translation {* |