author | wenzelm |
Fri, 19 Jan 2007 22:08:06 +0100 | |
changeset 22099 | 5dc00ac4bd8e |
parent 22004 | a69d21fc6d68 |
child 22385 | cc2be3315e72 |
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 |
||
22099 | 64 |
method_setup evaluation = {* |
21046 | 65 |
let |
66 |
||
67 |
fun evaluation_tac i = Tactical.PRIMITIVE (Drule.fconv_rule |
|
68 |
(Drule.goals_conv (equal i) Codegen.evaluation_conv)); |
|
69 |
||
22099 | 70 |
in Method.no_args (Method.SIMPLE_METHOD' (evaluation_tac THEN' rtac TrueI)) end |
71 |
*} "solve goal by evaluation" |
|
21046 | 72 |
|
73 |
||
74 |
subsection {* Generic code generator setup *} |
|
75 |
||
21904 | 76 |
text {* operational equality for code generation *} |
77 |
||
78 |
axclass eq \<subseteq> type |
|
21951 | 79 |
(attach "op =") |
21904 | 80 |
|
81 |
||
82 |
text {* equality for Haskell *} |
|
83 |
||
84 |
code_class eq |
|
85 |
(Haskell "Eq" where "op =" \<equiv> "(==)") |
|
86 |
||
87 |
code_const "op =" |
|
88 |
(Haskell infixl 4 "==") |
|
89 |
||
90 |
||
91 |
text {* boolean expressions *} |
|
92 |
||
93 |
lemma [code func]: |
|
94 |
shows "(False \<and> x) = False" |
|
95 |
and "(True \<and> x) = x" |
|
96 |
and "(x \<and> False) = False" |
|
97 |
and "(x \<and> True) = x" by simp_all |
|
98 |
||
99 |
lemma [code func]: |
|
100 |
shows "(False \<or> x) = x" |
|
101 |
and "(True \<or> x) = True" |
|
102 |
and "(x \<or> False) = x" |
|
103 |
and "(x \<or> True) = True" by simp_all |
|
104 |
||
105 |
lemma [code func]: |
|
106 |
shows "(\<not> True) = False" |
|
107 |
and "(\<not> False) = True" by (rule HOL.simp_thms)+ |
|
108 |
||
109 |
lemmas [code func] = imp_conv_disj |
|
110 |
||
111 |
lemmas [code func] = if_True if_False |
|
112 |
||
113 |
instance bool :: eq .. |
|
114 |
||
115 |
lemma [code func]: |
|
116 |
"True = P \<longleftrightarrow> P" by simp |
|
117 |
||
118 |
lemma [code func]: |
|
119 |
"False = P \<longleftrightarrow> \<not> P" by simp |
|
120 |
||
121 |
lemma [code func]: |
|
122 |
"P = True \<longleftrightarrow> P" by simp |
|
123 |
||
124 |
lemma [code func]: |
|
125 |
"P = False \<longleftrightarrow> \<not> P" by simp |
|
126 |
||
127 |
code_type bool |
|
128 |
(SML "bool") |
|
129 |
(OCaml "bool") |
|
130 |
(Haskell "Bool") |
|
131 |
||
132 |
code_instance bool :: eq |
|
133 |
(Haskell -) |
|
134 |
||
135 |
code_const "op = \<Colon> bool \<Rightarrow> bool \<Rightarrow> bool" |
|
136 |
(Haskell infixl 4 "==") |
|
137 |
||
138 |
code_const True and False and Not and "op &" and "op |" and If |
|
139 |
(SML "true" and "false" and "not" |
|
140 |
and infixl 1 "andalso" and infixl 0 "orelse" |
|
141 |
and "!(if (_)/ then (_)/ else (_))") |
|
142 |
(OCaml "true" and "false" and "not" |
|
143 |
and infixl 4 "&&" and infixl 2 "||" |
|
144 |
and "!(if (_)/ then (_)/ else (_))") |
|
145 |
(Haskell "True" and "False" and "not" |
|
146 |
and infixl 3 "&&" and infixl 2 "||" |
|
147 |
and "!(if (_)/ then (_)/ else (_))") |
|
148 |
||
149 |
code_reserved SML |
|
150 |
bool true false not |
|
151 |
||
152 |
code_reserved OCaml |
|
153 |
bool true false not |
|
154 |
||
155 |
||
21046 | 156 |
text {* itself as a code generator datatype *} |
157 |
||
158 |
setup {* |
|
159 |
let |
|
160 |
val v = ("'a", []); |
|
161 |
val t = Logic.mk_type (TFree v); |
|
162 |
val Const (c, ty) = t; |
|
163 |
val (_, Type (dtco, _)) = strip_type ty; |
|
164 |
in |
|
22099 | 165 |
CodegenData.add_datatype (dtco, (([v], [(c, [])]), CodegenData.lazy (fn () => []))) |
21046 | 166 |
end |
167 |
*} |
|
168 |
||
169 |
||
170 |
text {* code generation for arbitrary as exception *} |
|
171 |
||
172 |
setup {* |
|
173 |
CodegenSerializer.add_undefined "SML" "arbitrary" "(raise Fail \"arbitrary\")" |
|
21904 | 174 |
#> CodegenSerializer.add_undefined "OCaml" "arbitrary" "(failwith \"arbitrary\")" |
21046 | 175 |
*} |
176 |
||
177 |
code_const arbitrary |
|
21110 | 178 |
(Haskell "error/ \"arbitrary\"") |
21046 | 179 |
|
21079 | 180 |
code_reserved SML Fail |
21904 | 181 |
code_reserved OCaml failwith |
21046 | 182 |
|
183 |
||
21378 | 184 |
subsection {* Evaluation oracle *} |
185 |
||
186 |
ML {* |
|
22099 | 187 |
structure HOL_Eval: |
21378 | 188 |
sig |
22004 | 189 |
val reff: bool option ref |
190 |
val prop: theory -> term -> term |
|
22099 | 191 |
end = |
21378 | 192 |
struct |
193 |
||
22004 | 194 |
val reff : bool option ref = ref NONE; |
21378 | 195 |
|
22004 | 196 |
fun prop thy t = |
21869 | 197 |
if CodegenPackage.eval_term thy |
22004 | 198 |
(("HOL_Eval.reff", reff), t) |
21869 | 199 |
then HOLogic.true_const |
22004 | 200 |
else HOLogic.false_const |
201 |
||
22099 | 202 |
end |
21869 | 203 |
*} |
21378 | 204 |
|
22099 | 205 |
oracle eval_oracle ("term") = {* |
206 |
fn thy => fn t => Logic.mk_equals (t, HOL_Eval.prop thy t) |
|
21869 | 207 |
*} |
21378 | 208 |
|
22099 | 209 |
method_setup eval = {* |
210 |
let |
|
21378 | 211 |
|
212 |
fun conv ct = |
|
22099 | 213 |
let val {thy, t, ...} = rep_cterm ct |
214 |
in eval_oracle thy t end; |
|
21378 | 215 |
|
22099 | 216 |
fun eval_tac i = Tactical.PRIMITIVE (Drule.fconv_rule |
21546 | 217 |
(Drule.goals_conv (equal i) (HOLogic.Trueprop_conv conv))); |
21378 | 218 |
|
22099 | 219 |
in Method.no_args (Method.SIMPLE_METHOD' (eval_tac THEN' rtac TrueI)) end |
220 |
*} "solve goal by evaluation" |
|
21378 | 221 |
|
222 |
||
223 |
subsection {* Normalization by evaluation *} |
|
21046 | 224 |
|
22099 | 225 |
method_setup normalization = {* |
21046 | 226 |
let |
227 |
fun normalization_tac i = Tactical.PRIMITIVE (Drule.fconv_rule |
|
21546 | 228 |
(Drule.goals_conv (equal i) (HOLogic.Trueprop_conv |
21149 | 229 |
NBE.normalization_conv))); |
21046 | 230 |
in |
22099 | 231 |
Method.no_args (Method.SIMPLE_METHOD' (normalization_tac THEN' resolve_tac [TrueI, refl])) |
232 |
end |
|
233 |
*} "solve goal by normalization" |
|
234 |
||
21046 | 235 |
|
21059 | 236 |
text {* lazy @{const If} *} |
237 |
||
238 |
definition |
|
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21378
diff
changeset
|
239 |
if_delayed :: "bool \<Rightarrow> (bool \<Rightarrow> 'a) \<Rightarrow> (bool \<Rightarrow> 'a) \<Rightarrow> 'a" where |
21059 | 240 |
"if_delayed b f g = (if b then f True else g False)" |
241 |
||
242 |
lemma [code func]: |
|
243 |
shows "if_delayed True f g = f True" |
|
244 |
and "if_delayed False f g = g False" |
|
245 |
unfolding if_delayed_def by simp_all |
|
246 |
||
247 |
lemma [normal pre, symmetric, normal post]: |
|
248 |
"(if b then x else y) = if_delayed b (\<lambda>_. x) (\<lambda>_. y)" |
|
249 |
unfolding if_delayed_def .. |
|
250 |
||
251 |
||
21454 | 252 |
hide (open) const if_delayed |
21046 | 253 |
|
22099 | 254 |
end |