2 Author: Florian Haftmann, TU Muenchen
5 header {* Setup of code generator tools *}
11 subsection {* SML code generator setup *}
16 fun term_of_bool b = if b then HOLogic.true_const else HOLogic.false_const;
19 fun gen_bool i = one_of [false, true];
24 HOLogic.mk_Trueprop (if b then HOLogic.true_const else HOLogic.false_const);
32 "op |" ("(_ orelse/ _)")
33 "op &" ("(_ andalso/ _)")
34 "HOL.If" ("(if _/ then _/ else _)")
39 fun eq_codegen thy defs gr dep thyname b t =
41 (Const ("op =", Type (_, [Type ("fun", _), _])), _) => NONE
42 | (Const ("op =", _), [t, u]) =>
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)
48 SOME (gr''', Codegen.parens
49 (Pretty.block [pt, Pretty.str " =", Pretty.brk 1, pu]))
51 | (t as Const ("op =", _), ts) => SOME (Codegen.invoke_codegen
52 thy defs dep thyname b (gr, Codegen.eta_expand t ts 2))
57 Codegen.add_codegen "eq_codegen" eq_codegen
64 method_setup evaluation = {*
67 fun evaluation_tac i = Tactical.PRIMITIVE (Drule.fconv_rule
68 (Drule.goals_conv (equal i) Codegen.evaluation_conv));
70 in Method.no_args (Method.SIMPLE_METHOD' (evaluation_tac THEN' rtac TrueI)) end
71 *} "solve goal by evaluation"
74 subsection {* Generic code generator setup *}
76 text {* operational equality for code generation *}
78 class eq (attach "op =") = type
81 text {* equality for Haskell *}
84 (Haskell "Eq" where "op =" \<equiv> "(==)")
87 (Haskell infixl 4 "==")
92 code_datatype True False
95 shows "(False \<and> x) = False"
96 and "(True \<and> x) = x"
97 and "(x \<and> False) = False"
98 and "(x \<and> True) = x" by simp_all
101 shows "(False \<or> x) = x"
102 and "(True \<or> x) = True"
103 and "(x \<or> False) = x"
104 and "(x \<or> True) = True" by simp_all
107 shows "(\<not> True) = False"
108 and "(\<not> False) = True" by (rule HOL.simp_thms)+
110 lemmas [code func] = imp_conv_disj
112 lemmas [code func] = if_True if_False
114 instance bool :: eq ..
117 "True = P \<longleftrightarrow> P" by simp
120 "False = P \<longleftrightarrow> \<not> P" by simp
123 "P = True \<longleftrightarrow> P" by simp
126 "P = False \<longleftrightarrow> \<not> P" by simp
133 code_instance bool :: eq
136 code_const "op = \<Colon> bool \<Rightarrow> bool \<Rightarrow> bool"
137 (Haskell infixl 4 "==")
139 code_const True and False and Not and "op &" and "op |" and If
140 (SML "true" and "false" and "not"
141 and infixl 1 "andalso" and infixl 0 "orelse"
142 and "!(if (_)/ then (_)/ else (_))")
143 (OCaml "true" and "false" and "not"
144 and infixl 4 "&&" and infixl 2 "||"
145 and "!(if (_)/ then (_)/ else (_))")
146 (Haskell "True" and "False" and "not"
147 and infixl 3 "&&" and infixl 2 "||"
148 and "!(if (_)/ then (_)/ else (_))")
159 code_datatype Trueprop "prop"
162 text {* type itself *}
164 code_datatype "TYPE('a)"
167 text {* prevent unfolding of quantifier definitions *}
175 text {* code generation for undefined as exception *}
178 (SML "(raise Fail \"undefined\")")
179 (OCaml "(failwith \"undefined\")")
180 (Haskell "error/ \"undefined\"")
182 code_reserved SML Fail
183 code_reserved OCaml failwith
186 subsection {* Evaluation oracle *}
188 oracle eval_oracle ("term") = {* fn thy => fn t =>
189 if CodegenPackage.satisfies thy (HOLogic.dest_Trueprop t) []
191 else HOLogic.Trueprop $ (HOLogic.true_const) (*dummy*)
194 method_setup eval = {*
197 SUBGOAL (fn (t, i) => rtac (eval_oracle thy t) i)
199 Method.ctxt_args (fn ctxt =>
200 Method.SIMPLE_METHOD' (eval_tac (ProofContext.theory_of ctxt)))
202 *} "solve goal by evaluation"
205 subsection {* Normalization by evaluation *}
207 method_setup normalization = {*
209 fun normalization_tac i = Tactical.PRIMITIVE (Drule.fconv_rule
210 (Drule.goals_conv (equal i) (HOLogic.Trueprop_conv
211 NBE.normalization_conv)));
213 Method.no_args (Method.SIMPLE_METHOD' (normalization_tac THEN' resolve_tac [TrueI, refl]))
215 *} "solve goal by normalization"
218 text {* lazy @{const If} *}
221 if_delayed :: "bool \<Rightarrow> (bool \<Rightarrow> 'a) \<Rightarrow> (bool \<Rightarrow> 'a) \<Rightarrow> 'a" where
222 "if_delayed b f g = (if b then f True else g False)"
225 shows "if_delayed True f g = f True"
226 and "if_delayed False f g = g False"
227 unfolding if_delayed_def by simp_all
229 lemma [normal pre, symmetric, normal post]:
230 "(if b then x else y) = if_delayed b (\<lambda>_. x) (\<lambda>_. y)"
231 unfolding if_delayed_def ..
234 hide (open) const if_delayed