| author | paulson |
| Tue, 09 Jan 2007 18:12:59 +0100 | |
| changeset 22044 | 6c0702a96076 |
| parent 22004 | a69d21fc6d68 |
| child 22099 | 5dc00ac4bd8e |
| 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 |
|
| 22004 | 199 |
val reff: bool option ref |
200 |
val 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 |
||
| 22004 | 208 |
val reff : bool option ref = ref NONE; |
| 21378 | 209 |
|
| 22004 | 210 |
fun prop thy t = |
| 21869 | 211 |
if CodegenPackage.eval_term thy |
| 22004 | 212 |
(("HOL_Eval.reff", reff), t)
|
| 21869 | 213 |
then HOLogic.true_const |
| 22004 | 214 |
else HOLogic.false_const |
215 |
||
216 |
fun mk_eq thy t = |
|
217 |
Logic.mk_equals (t, prop thy t) |
|
| 21378 | 218 |
|
| 21869 | 219 |
end; |
220 |
*} |
|
| 21378 | 221 |
|
| 21869 | 222 |
setup {*
|
| 22004 | 223 |
PureThy.add_oracle ("invoke", "term", "HOL_Eval.mk_eq")
|
| 21869 | 224 |
*} |
| 21378 | 225 |
|
| 21869 | 226 |
ML {*
|
227 |
structure HOL_Eval : HOL_EVAL = |
|
228 |
struct |
|
229 |
||
230 |
open HOL_Eval; |
|
| 21378 | 231 |
|
232 |
fun conv ct = |
|
233 |
let |
|
234 |
val {thy, t, ...} = rep_cterm ct;
|
|
| 21869 | 235 |
in invoke thy t end; |
| 21378 | 236 |
|
237 |
fun tac i = Tactical.PRIMITIVE (Drule.fconv_rule |
|
| 21546 | 238 |
(Drule.goals_conv (equal i) (HOLogic.Trueprop_conv conv))); |
| 21378 | 239 |
|
240 |
val method = |
|
| 21587 | 241 |
Method.no_args (Method.SIMPLE_METHOD' (tac THEN' rtac TrueI)); |
| 21378 | 242 |
|
243 |
end; |
|
244 |
*} |
|
245 |
||
246 |
setup {*
|
|
| 21869 | 247 |
Method.add_method ("eval", HOL_Eval.method, "solve goal by evaluation")
|
| 21378 | 248 |
*} |
249 |
||
250 |
||
251 |
subsection {* Normalization by evaluation *}
|
|
| 21046 | 252 |
|
253 |
setup {*
|
|
254 |
let |
|
255 |
fun normalization_tac i = Tactical.PRIMITIVE (Drule.fconv_rule |
|
| 21546 | 256 |
(Drule.goals_conv (equal i) (HOLogic.Trueprop_conv |
| 21149 | 257 |
NBE.normalization_conv))); |
| 21046 | 258 |
val normalization_meth = |
| 21587 | 259 |
Method.no_args (Method.SIMPLE_METHOD' (normalization_tac THEN' resolve_tac [TrueI, refl])); |
| 21046 | 260 |
in |
261 |
Method.add_method ("normalization", normalization_meth, "solve goal by normalization")
|
|
262 |
end; |
|
263 |
*} |
|
264 |
||
| 21059 | 265 |
text {* lazy @{const If} *}
|
266 |
||
267 |
definition |
|
|
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21378
diff
changeset
|
268 |
if_delayed :: "bool \<Rightarrow> (bool \<Rightarrow> 'a) \<Rightarrow> (bool \<Rightarrow> 'a) \<Rightarrow> 'a" where |
| 21059 | 269 |
"if_delayed b f g = (if b then f True else g False)" |
270 |
||
271 |
lemma [code func]: |
|
272 |
shows "if_delayed True f g = f True" |
|
273 |
and "if_delayed False f g = g False" |
|
274 |
unfolding if_delayed_def by simp_all |
|
275 |
||
276 |
lemma [normal pre, symmetric, normal post]: |
|
277 |
"(if b then x else y) = if_delayed b (\<lambda>_. x) (\<lambda>_. y)" |
|
278 |
unfolding if_delayed_def .. |
|
279 |
||
280 |
||
| 21454 | 281 |
hide (open) const if_delayed |
| 21046 | 282 |
|
283 |
end |