author | wenzelm |
Sat, 30 Dec 2006 16:08:06 +0100 | |
changeset 21966 | edab0ecfbd7c |
parent 21951 | 56abe5f3c612 |
child 22004 | a69d21fc6d68 |
permissions | -rw-r--r-- |
21046 | 1 |
(* ID: $Id$ |
2 |
Author: Florian Haftmann, TU Muenchen |
|
3 |
*) |
|
4 |
||
5 |
header {* Setup of code generator tools *} |
|
6 |
||
7 |
theory Code_Generator |
|
8 |
imports HOL |
|
9 |
begin |
|
10 |
||
11 |
subsection {* ML code generator *} |
|
12 |
||
13 |
types_code |
|
14 |
"bool" ("bool") |
|
15 |
attach (term_of) {* |
|
16 |
fun term_of_bool b = if b then HOLogic.true_const else HOLogic.false_const; |
|
17 |
*} |
|
18 |
attach (test) {* |
|
19 |
fun gen_bool i = one_of [false, true]; |
|
20 |
*} |
|
21 |
"prop" ("bool") |
|
22 |
attach (term_of) {* |
|
23 |
fun term_of_prop b = |
|
24 |
HOLogic.mk_Trueprop (if b then HOLogic.true_const else HOLogic.false_const); |
|
25 |
*} |
|
26 |
||
27 |
consts_code |
|
28 |
"Trueprop" ("(_)") |
|
29 |
"True" ("true") |
|
30 |
"False" ("false") |
|
31 |
"Not" ("not") |
|
32 |
"op |" ("(_ orelse/ _)") |
|
33 |
"op &" ("(_ andalso/ _)") |
|
34 |
"HOL.If" ("(if _/ then _/ else _)") |
|
35 |
||
36 |
setup {* |
|
37 |
let |
|
38 |
||
39 |
fun eq_codegen thy defs gr dep thyname b t = |
|
40 |
(case strip_comb t of |
|
41 |
(Const ("op =", Type (_, [Type ("fun", _), _])), _) => NONE |
|
42 |
| (Const ("op =", _), [t, u]) => |
|
43 |
let |
|
44 |
val (gr', pt) = Codegen.invoke_codegen thy defs dep thyname false (gr, t); |
|
45 |
val (gr'', pu) = Codegen.invoke_codegen thy defs dep thyname false (gr', u); |
|
46 |
val (gr''', _) = Codegen.invoke_tycodegen thy defs dep thyname false (gr'', HOLogic.boolT) |
|
47 |
in |
|
48 |
SOME (gr''', Codegen.parens |
|
49 |
(Pretty.block [pt, Pretty.str " =", Pretty.brk 1, pu])) |
|
50 |
end |
|
51 |
| (t as Const ("op =", _), ts) => SOME (Codegen.invoke_codegen |
|
52 |
thy defs dep thyname b (gr, Codegen.eta_expand t ts 2)) |
|
53 |
| _ => NONE); |
|
54 |
||
55 |
in |
|
56 |
||
57 |
Codegen.add_codegen "eq_codegen" eq_codegen |
|
58 |
||
59 |
end |
|
60 |
*} |
|
61 |
||
62 |
text {* Evaluation *} |
|
63 |
||
64 |
setup {* |
|
65 |
let |
|
66 |
||
21546 | 67 |
val TrueI = thm "TrueI" |
21046 | 68 |
fun evaluation_tac i = Tactical.PRIMITIVE (Drule.fconv_rule |
69 |
(Drule.goals_conv (equal i) Codegen.evaluation_conv)); |
|
70 |
val evaluation_meth = |
|
21587 | 71 |
Method.no_args (Method.SIMPLE_METHOD' (evaluation_tac THEN' rtac TrueI)); |
21046 | 72 |
|
73 |
in |
|
74 |
||
75 |
Method.add_method ("evaluation", evaluation_meth, "solve goal by evaluation") |
|
76 |
||
77 |
end; |
|
78 |
*} |
|
79 |
||
80 |
||
81 |
subsection {* Generic code generator setup *} |
|
82 |
||
21904 | 83 |
text {* operational equality for code generation *} |
84 |
||
85 |
axclass eq \<subseteq> type |
|
21951 | 86 |
(attach "op =") |
21904 | 87 |
|
88 |
||
89 |
text {* equality for Haskell *} |
|
90 |
||
91 |
code_class eq |
|
92 |
(Haskell "Eq" where "op =" \<equiv> "(==)") |
|
93 |
||
94 |
code_const "op =" |
|
95 |
(Haskell infixl 4 "==") |
|
96 |
||
97 |
||
98 |
text {* boolean expressions *} |
|
99 |
||
100 |
lemma [code func]: |
|
101 |
shows "(False \<and> x) = False" |
|
102 |
and "(True \<and> x) = x" |
|
103 |
and "(x \<and> False) = False" |
|
104 |
and "(x \<and> True) = x" by simp_all |
|
105 |
||
106 |
lemma [code func]: |
|
107 |
shows "(False \<or> x) = x" |
|
108 |
and "(True \<or> x) = True" |
|
109 |
and "(x \<or> False) = x" |
|
110 |
and "(x \<or> True) = True" by simp_all |
|
111 |
||
112 |
lemma [code func]: |
|
113 |
shows "(\<not> True) = False" |
|
114 |
and "(\<not> False) = True" by (rule HOL.simp_thms)+ |
|
115 |
||
116 |
lemmas [code func] = imp_conv_disj |
|
117 |
||
118 |
lemmas [code func] = if_True if_False |
|
119 |
||
120 |
instance bool :: eq .. |
|
121 |
||
122 |
lemma [code func]: |
|
123 |
"True = P \<longleftrightarrow> P" by simp |
|
124 |
||
125 |
lemma [code func]: |
|
126 |
"False = P \<longleftrightarrow> \<not> P" by simp |
|
127 |
||
128 |
lemma [code func]: |
|
129 |
"P = True \<longleftrightarrow> P" by simp |
|
130 |
||
131 |
lemma [code func]: |
|
132 |
"P = False \<longleftrightarrow> \<not> P" by simp |
|
133 |
||
134 |
code_type bool |
|
135 |
(SML "bool") |
|
136 |
(OCaml "bool") |
|
137 |
(Haskell "Bool") |
|
138 |
||
139 |
code_instance bool :: eq |
|
140 |
(Haskell -) |
|
141 |
||
142 |
code_const "op = \<Colon> bool \<Rightarrow> bool \<Rightarrow> bool" |
|
143 |
(Haskell infixl 4 "==") |
|
144 |
||
145 |
code_const True and False and Not and "op &" and "op |" and If |
|
146 |
(SML "true" and "false" and "not" |
|
147 |
and infixl 1 "andalso" and infixl 0 "orelse" |
|
148 |
and "!(if (_)/ then (_)/ else (_))") |
|
149 |
(OCaml "true" and "false" and "not" |
|
150 |
and infixl 4 "&&" and infixl 2 "||" |
|
151 |
and "!(if (_)/ then (_)/ else (_))") |
|
152 |
(Haskell "True" and "False" and "not" |
|
153 |
and infixl 3 "&&" and infixl 2 "||" |
|
154 |
and "!(if (_)/ then (_)/ else (_))") |
|
155 |
||
156 |
code_reserved SML |
|
157 |
bool true false not |
|
158 |
||
159 |
code_reserved OCaml |
|
160 |
bool true false not |
|
161 |
||
162 |
||
21046 | 163 |
text {* itself as a code generator datatype *} |
164 |
||
165 |
setup {* |
|
166 |
let fun add_itself thy = |
|
167 |
let |
|
168 |
val v = ("'a", []); |
|
169 |
val t = Logic.mk_type (TFree v); |
|
170 |
val Const (c, ty) = t; |
|
171 |
val (_, Type (dtco, _)) = strip_type ty; |
|
172 |
in |
|
173 |
thy |
|
174 |
|> CodegenData.add_datatype (dtco, (([v], [(c, [])]), CodegenData.lazy (fn () => []))) |
|
175 |
end |
|
176 |
in add_itself end; |
|
177 |
*} |
|
178 |
||
179 |
||
180 |
text {* code generation for arbitrary as exception *} |
|
181 |
||
182 |
setup {* |
|
183 |
CodegenSerializer.add_undefined "SML" "arbitrary" "(raise Fail \"arbitrary\")" |
|
21904 | 184 |
#> CodegenSerializer.add_undefined "OCaml" "arbitrary" "(failwith \"arbitrary\")" |
21046 | 185 |
*} |
186 |
||
187 |
code_const arbitrary |
|
21110 | 188 |
(Haskell "error/ \"arbitrary\"") |
21046 | 189 |
|
21079 | 190 |
code_reserved SML Fail |
21904 | 191 |
code_reserved OCaml failwith |
21046 | 192 |
|
193 |
||
21378 | 194 |
subsection {* Evaluation oracle *} |
195 |
||
196 |
ML {* |
|
197 |
signature HOL_EVAL = |
|
198 |
sig |
|
199 |
val eval_ref: bool option ref |
|
21869 | 200 |
val eval_prop: theory -> term -> term |
21378 | 201 |
val tac: int -> tactic |
202 |
val method: Method.src -> Proof.context -> Method.method |
|
203 |
end; |
|
204 |
||
21869 | 205 |
structure HOL_Eval = |
21378 | 206 |
struct |
207 |
||
208 |
val eval_ref : bool option ref = ref NONE; |
|
209 |
||
210 |
fun eval_prop thy t = |
|
21869 | 211 |
if CodegenPackage.eval_term thy |
212 |
(("HOL_Eval.eval_ref", eval_ref), t) |
|
213 |
then HOLogic.true_const |
|
214 |
else HOLogic.false_const; |
|
21378 | 215 |
|
21869 | 216 |
end; |
217 |
*} |
|
21378 | 218 |
|
21869 | 219 |
setup {* |
220 |
PureThy.add_oracle ("invoke", "term", "HOL_Eval.eval_prop") |
|
221 |
*} |
|
21378 | 222 |
|
21869 | 223 |
ML {* |
224 |
structure HOL_Eval : HOL_EVAL = |
|
225 |
struct |
|
226 |
||
227 |
open HOL_Eval; |
|
21378 | 228 |
|
229 |
fun conv ct = |
|
230 |
let |
|
231 |
val {thy, t, ...} = rep_cterm ct; |
|
21869 | 232 |
in invoke thy t end; |
21378 | 233 |
|
234 |
fun tac i = Tactical.PRIMITIVE (Drule.fconv_rule |
|
21546 | 235 |
(Drule.goals_conv (equal i) (HOLogic.Trueprop_conv conv))); |
21378 | 236 |
|
237 |
val method = |
|
21587 | 238 |
Method.no_args (Method.SIMPLE_METHOD' (tac THEN' rtac TrueI)); |
21378 | 239 |
|
240 |
end; |
|
241 |
*} |
|
242 |
||
243 |
setup {* |
|
21869 | 244 |
Method.add_method ("eval", HOL_Eval.method, "solve goal by evaluation") |
21378 | 245 |
*} |
246 |
||
247 |
||
248 |
subsection {* Normalization by evaluation *} |
|
21046 | 249 |
|
250 |
setup {* |
|
251 |
let |
|
252 |
fun normalization_tac i = Tactical.PRIMITIVE (Drule.fconv_rule |
|
21546 | 253 |
(Drule.goals_conv (equal i) (HOLogic.Trueprop_conv |
21149 | 254 |
NBE.normalization_conv))); |
21046 | 255 |
val normalization_meth = |
21587 | 256 |
Method.no_args (Method.SIMPLE_METHOD' (normalization_tac THEN' resolve_tac [TrueI, refl])); |
21046 | 257 |
in |
258 |
Method.add_method ("normalization", normalization_meth, "solve goal by normalization") |
|
259 |
end; |
|
260 |
*} |
|
261 |
||
21059 | 262 |
text {* lazy @{const If} *} |
263 |
||
264 |
definition |
|
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21378
diff
changeset
|
265 |
if_delayed :: "bool \<Rightarrow> (bool \<Rightarrow> 'a) \<Rightarrow> (bool \<Rightarrow> 'a) \<Rightarrow> 'a" where |
21059 | 266 |
"if_delayed b f g = (if b then f True else g False)" |
267 |
||
268 |
lemma [code func]: |
|
269 |
shows "if_delayed True f g = f True" |
|
270 |
and "if_delayed False f g = g False" |
|
271 |
unfolding if_delayed_def by simp_all |
|
272 |
||
273 |
lemma [normal pre, symmetric, normal post]: |
|
274 |
"(if b then x else y) = if_delayed b (\<lambda>_. x) (\<lambda>_. y)" |
|
275 |
unfolding if_delayed_def .. |
|
276 |
||
277 |
||
21454 | 278 |
hide (open) const if_delayed |
21046 | 279 |
|
280 |
end |