src/HOL/Code_Generator.thy
changeset 22099 5dc00ac4bd8e
parent 22004 a69d21fc6d68
child 22385 cc2be3315e72
equal deleted inserted replaced
22098:88be1b7775c8 22099:5dc00ac4bd8e
    59 end
    59 end
    60 *}
    60 *}
    61 
    61 
    62 text {* Evaluation *}
    62 text {* Evaluation *}
    63 
    63 
    64 setup {*
    64 method_setup evaluation = {*
    65 let
    65 let
    66 
    66 
    67 val TrueI = thm "TrueI"
       
    68 fun evaluation_tac i = Tactical.PRIMITIVE (Drule.fconv_rule
    67 fun evaluation_tac i = Tactical.PRIMITIVE (Drule.fconv_rule
    69   (Drule.goals_conv (equal i) Codegen.evaluation_conv));
    68   (Drule.goals_conv (equal i) Codegen.evaluation_conv));
    70 val evaluation_meth =
    69 
    71   Method.no_args (Method.SIMPLE_METHOD' (evaluation_tac THEN' rtac TrueI));
    70 in Method.no_args (Method.SIMPLE_METHOD' (evaluation_tac THEN' rtac TrueI)) end
    72 
    71 *} "solve goal by evaluation"
    73 in
       
    74 
       
    75 Method.add_method ("evaluation", evaluation_meth, "solve goal by evaluation")
       
    76 
       
    77 end;
       
    78 *}
       
    79 
    72 
    80 
    73 
    81 subsection {* Generic code generator setup *}
    74 subsection {* Generic code generator setup *}
    82 
    75 
    83 text {* operational equality for code generation *}
    76 text {* operational equality for code generation *}
   161 
   154 
   162 
   155 
   163 text {* itself as a code generator datatype *}
   156 text {* itself as a code generator datatype *}
   164 
   157 
   165 setup {*
   158 setup {*
   166 let fun add_itself thy =
       
   167   let
   159   let
   168     val v = ("'a", []);
   160     val v = ("'a", []);
   169     val t = Logic.mk_type (TFree v);
   161     val t = Logic.mk_type (TFree v);
   170     val Const (c, ty) = t;
   162     val Const (c, ty) = t;
   171     val (_, Type (dtco, _)) = strip_type ty;
   163     val (_, Type (dtco, _)) = strip_type ty;
   172   in
   164   in
   173     thy
   165     CodegenData.add_datatype (dtco, (([v], [(c, [])]), CodegenData.lazy (fn () => [])))
   174     |> CodegenData.add_datatype (dtco, (([v], [(c, [])]), CodegenData.lazy (fn () => [])))
       
   175   end
   166   end
   176 in add_itself end;
       
   177 *} 
   167 *} 
   178 
   168 
   179 
   169 
   180 text {* code generation for arbitrary as exception *}
   170 text {* code generation for arbitrary as exception *}
   181 
   171 
   192 
   182 
   193 
   183 
   194 subsection {* Evaluation oracle *}
   184 subsection {* Evaluation oracle *}
   195 
   185 
   196 ML {*
   186 ML {*
   197 signature HOL_EVAL =
   187 structure HOL_Eval:
   198 sig
   188 sig
   199   val reff: bool option ref
   189   val reff: bool option ref
   200   val prop: theory -> term -> term
   190   val prop: theory -> term -> term
   201   val tac: int -> tactic
   191 end =
   202   val method: Method.src -> Proof.context -> Method.method
       
   203 end;
       
   204 
       
   205 structure HOL_Eval =
       
   206 struct
   192 struct
   207 
   193 
   208 val reff : bool option ref = ref NONE;
   194 val reff : bool option ref = ref NONE;
   209 
   195 
   210 fun prop thy t =
   196 fun prop thy t =
   211   if CodegenPackage.eval_term thy
   197   if CodegenPackage.eval_term thy
   212     (("HOL_Eval.reff", reff), t)
   198     (("HOL_Eval.reff", reff), t)
   213     then HOLogic.true_const
   199     then HOLogic.true_const
   214     else HOLogic.false_const
   200     else HOLogic.false_const
   215 
   201 
   216 fun mk_eq thy t =
   202 end
   217   Logic.mk_equals (t, prop thy t)
   203 *}
   218 
   204 
   219 end;
   205 oracle eval_oracle ("term") = {*
   220 *}
   206   fn thy => fn t => Logic.mk_equals (t, HOL_Eval.prop thy t)
   221 
   207 *}
   222 setup {*
   208 
   223   PureThy.add_oracle ("invoke", "term", "HOL_Eval.mk_eq")
   209 method_setup eval = {*
   224 *}
   210 let
   225 
       
   226 ML {*
       
   227 structure HOL_Eval : HOL_EVAL =
       
   228 struct
       
   229 
       
   230 open HOL_Eval;
       
   231 
   211 
   232 fun conv ct =
   212 fun conv ct =
   233   let
   213   let val {thy, t, ...} = rep_cterm ct
   234     val {thy, t, ...} = rep_cterm ct;
   214   in eval_oracle thy t end;
   235   in invoke thy t end;
   215 
   236 
   216 fun eval_tac i = Tactical.PRIMITIVE (Drule.fconv_rule
   237 fun tac i = Tactical.PRIMITIVE (Drule.fconv_rule
       
   238   (Drule.goals_conv (equal i) (HOLogic.Trueprop_conv conv)));
   217   (Drule.goals_conv (equal i) (HOLogic.Trueprop_conv conv)));
   239 
   218 
   240 val method =
   219 in Method.no_args (Method.SIMPLE_METHOD' (eval_tac THEN' rtac TrueI)) end
   241   Method.no_args (Method.SIMPLE_METHOD' (tac THEN' rtac TrueI));
   220 *} "solve goal by evaluation"
   242 
       
   243 end;
       
   244 *}
       
   245 
       
   246 setup {*
       
   247   Method.add_method ("eval", HOL_Eval.method, "solve goal by evaluation")
       
   248 *}
       
   249 
   221 
   250 
   222 
   251 subsection {* Normalization by evaluation *}
   223 subsection {* Normalization by evaluation *}
   252 
   224 
   253 setup {*
   225 method_setup normalization = {*
   254 let
   226 let
   255   fun normalization_tac i = Tactical.PRIMITIVE (Drule.fconv_rule
   227   fun normalization_tac i = Tactical.PRIMITIVE (Drule.fconv_rule
   256     (Drule.goals_conv (equal i) (HOLogic.Trueprop_conv
   228     (Drule.goals_conv (equal i) (HOLogic.Trueprop_conv
   257       NBE.normalization_conv)));
   229       NBE.normalization_conv)));
   258   val normalization_meth =
       
   259     Method.no_args (Method.SIMPLE_METHOD' (normalization_tac THEN' resolve_tac [TrueI, refl]));
       
   260 in
   230 in
   261   Method.add_method ("normalization", normalization_meth, "solve goal by normalization")
   231   Method.no_args (Method.SIMPLE_METHOD' (normalization_tac THEN' resolve_tac [TrueI, refl]))
   262 end;
   232 end
   263 *}
   233 *} "solve goal by normalization"
       
   234 
   264 
   235 
   265 text {* lazy @{const If} *}
   236 text {* lazy @{const If} *}
   266 
   237 
   267 definition
   238 definition
   268   if_delayed :: "bool \<Rightarrow> (bool \<Rightarrow> 'a) \<Rightarrow> (bool \<Rightarrow> 'a) \<Rightarrow> 'a" where
   239   if_delayed :: "bool \<Rightarrow> (bool \<Rightarrow> 'a) \<Rightarrow> (bool \<Rightarrow> 'a) \<Rightarrow> 'a" where