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 |
|
|
67 |
fun evaluation_tac i = Tactical.PRIMITIVE (Drule.fconv_rule
|
|
68 |
(Drule.goals_conv (equal i) Codegen.evaluation_conv));
|
|
69 |
|
|
70 |
val evaluation_meth =
|
|
71 |
Method.no_args (Method.METHOD (fn _ => evaluation_tac 1 THEN rtac HOL.TrueI 1));
|
|
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 |
|
|
83 |
text {* itself as a code generator datatype *}
|
|
84 |
|
|
85 |
setup {*
|
|
86 |
let fun add_itself thy =
|
|
87 |
let
|
|
88 |
val v = ("'a", []);
|
|
89 |
val t = Logic.mk_type (TFree v);
|
|
90 |
val Const (c, ty) = t;
|
|
91 |
val (_, Type (dtco, _)) = strip_type ty;
|
|
92 |
in
|
|
93 |
thy
|
|
94 |
|> CodegenData.add_datatype (dtco, (([v], [(c, [])]), CodegenData.lazy (fn () => [])))
|
|
95 |
end
|
|
96 |
in add_itself end;
|
|
97 |
*}
|
|
98 |
|
|
99 |
|
|
100 |
text {* code generation for arbitrary as exception *}
|
|
101 |
|
|
102 |
setup {*
|
|
103 |
CodegenSerializer.add_undefined "SML" "arbitrary" "(raise Fail \"arbitrary\")"
|
|
104 |
*}
|
|
105 |
|
|
106 |
code_const arbitrary
|
21110
|
107 |
(Haskell "error/ \"arbitrary\"")
|
21046
|
108 |
|
21079
|
109 |
code_reserved SML Fail
|
|
110 |
code_reserved Haskell error
|
21046
|
111 |
|
|
112 |
|
21378
|
113 |
subsection {* Evaluation oracle *}
|
|
114 |
|
|
115 |
ML {*
|
|
116 |
signature HOL_EVAL =
|
|
117 |
sig
|
|
118 |
val eval_ref: bool option ref
|
|
119 |
val oracle: string * (theory * exn -> term)
|
|
120 |
val tac: int -> tactic
|
|
121 |
val method: Method.src -> Proof.context -> Method.method
|
|
122 |
end;
|
|
123 |
|
|
124 |
structure HOLEval : HOL_EVAL =
|
|
125 |
struct
|
|
126 |
|
|
127 |
val eval_ref : bool option ref = ref NONE;
|
|
128 |
|
|
129 |
fun eval_prop thy t =
|
|
130 |
CodegenPackage.eval_term
|
|
131 |
thy (("HOLEval.eval_ref", eval_ref), t);
|
|
132 |
|
|
133 |
exception Eval of term;
|
|
134 |
|
|
135 |
val oracle = ("Eval", fn (thy, Eval t) =>
|
|
136 |
Logic.mk_equals (t, if eval_prop thy t then HOLogic.true_const else HOLogic.false_const));
|
|
137 |
|
|
138 |
val oracle_name = NameSpace.pack [Context.theory_name (the_context ()), fst oracle];
|
|
139 |
|
|
140 |
fun conv ct =
|
|
141 |
let
|
|
142 |
val {thy, t, ...} = rep_cterm ct;
|
|
143 |
in Thm.invoke_oracle_i thy oracle_name (thy, Eval t) end;
|
|
144 |
|
|
145 |
fun tac i = Tactical.PRIMITIVE (Drule.fconv_rule
|
|
146 |
(Drule.goals_conv (equal i) (HOL.Trueprop_conv conv)));
|
|
147 |
|
|
148 |
val method =
|
|
149 |
Method.no_args (Method.METHOD (fn _ =>
|
|
150 |
tac 1 THEN rtac TrueI 1));
|
|
151 |
|
|
152 |
end;
|
|
153 |
*}
|
|
154 |
|
|
155 |
setup {*
|
|
156 |
Theory.add_oracle HOLEval.oracle
|
|
157 |
#> Method.add_method ("eval", HOLEval.method, "solve goal by evaluation")
|
|
158 |
*}
|
|
159 |
|
|
160 |
|
|
161 |
subsection {* Normalization by evaluation *}
|
21046
|
162 |
|
|
163 |
setup {*
|
|
164 |
let
|
|
165 |
fun normalization_tac i = Tactical.PRIMITIVE (Drule.fconv_rule
|
21110
|
166 |
(Drule.goals_conv (equal i) (HOL.Trueprop_conv
|
21149
|
167 |
NBE.normalization_conv)));
|
21046
|
168 |
val normalization_meth =
|
21110
|
169 |
Method.no_args (Method.METHOD (fn _ => normalization_tac 1 THEN resolve_tac [TrueI, refl] 1));
|
21046
|
170 |
in
|
|
171 |
Method.add_method ("normalization", normalization_meth, "solve goal by normalization")
|
|
172 |
end;
|
|
173 |
*}
|
|
174 |
|
21059
|
175 |
text {* lazy @{const If} *}
|
|
176 |
|
|
177 |
definition
|
|
178 |
if_delayed :: "bool \<Rightarrow> (bool \<Rightarrow> 'a) \<Rightarrow> (bool \<Rightarrow> 'a) \<Rightarrow> 'a"
|
|
179 |
"if_delayed b f g = (if b then f True else g False)"
|
|
180 |
|
|
181 |
lemma [code func]:
|
|
182 |
shows "if_delayed True f g = f True"
|
|
183 |
and "if_delayed False f g = g False"
|
|
184 |
unfolding if_delayed_def by simp_all
|
|
185 |
|
|
186 |
lemma [normal pre, symmetric, normal post]:
|
|
187 |
"(if b then x else y) = if_delayed b (\<lambda>_. x) (\<lambda>_. y)"
|
|
188 |
unfolding if_delayed_def ..
|
|
189 |
|
|
190 |
|
21110
|
191 |
subsection {* Operational equality for code generation *}
|
|
192 |
|
|
193 |
subsubsection {* eq class *}
|
|
194 |
|
|
195 |
class eq =
|
|
196 |
fixes eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
|
|
197 |
|
|
198 |
defs
|
|
199 |
eq_def [normal post]: "eq \<equiv> (op =)"
|
|
200 |
|
|
201 |
lemmas [symmetric, code inline, code func] = eq_def
|
|
202 |
|
|
203 |
|
|
204 |
subsubsection {* bool type *}
|
|
205 |
|
|
206 |
instance bool :: eq ..
|
|
207 |
|
|
208 |
lemma [code func]:
|
|
209 |
"eq True p = p" unfolding eq_def by auto
|
|
210 |
|
|
211 |
lemma [code func]:
|
|
212 |
"eq False p = (\<not> p)" unfolding eq_def by auto
|
|
213 |
|
|
214 |
lemma [code func]:
|
|
215 |
"eq p True = p" unfolding eq_def by auto
|
|
216 |
|
|
217 |
lemma [code func]:
|
|
218 |
"eq p False = (\<not> p)" unfolding eq_def by auto
|
|
219 |
|
|
220 |
|
|
221 |
subsubsection {* Haskell *}
|
|
222 |
|
|
223 |
code_class eq
|
|
224 |
(Haskell "Eq" where eq \<equiv> "(==)")
|
|
225 |
|
|
226 |
code_const eq
|
|
227 |
(Haskell infixl 4 "==")
|
|
228 |
|
|
229 |
code_instance bool :: eq
|
|
230 |
(Haskell -)
|
|
231 |
|
|
232 |
code_const "eq \<Colon> bool \<Rightarrow> bool \<Rightarrow> bool"
|
|
233 |
(Haskell infixl 4 "==")
|
|
234 |
|
|
235 |
code_reserved Haskell
|
|
236 |
Eq eq
|
|
237 |
|
|
238 |
|
21059
|
239 |
hide (open) const eq if_delayed
|
21046
|
240 |
|
|
241 |
end |