src/HOL/Code_Evaluation.thy
changeset 39387 6608c4838ff9
parent 39274 b17ffa965223
child 39401 887f4218a39a
equal deleted inserted replaced
39381:9717ea8d42b3 39387:6608c4838ff9
   276 
   276 
   277 lemma (in term_syntax) term_of_code_numeral_code [code]:
   277 lemma (in term_syntax) term_of_code_numeral_code [code]:
   278   "term_of (k::code_numeral) = termify (number_of :: int \<Rightarrow> code_numeral) <\<cdot>> term_of_num (2::code_numeral) k"
   278   "term_of (k::code_numeral) = termify (number_of :: int \<Rightarrow> code_numeral) <\<cdot>> term_of_num (2::code_numeral) k"
   279   by (simp only: term_of_anything)
   279   by (simp only: term_of_anything)
   280 
   280 
       
   281 
   281 subsection {* Obfuscate *}
   282 subsection {* Obfuscate *}
   282 
   283 
   283 print_translation {*
   284 print_translation {*
   284 let
   285 let
   285   val term = Const ("<TERM>", dummyT);
   286   val term = Const ("<TERM>", dummyT);
   290     (@{const_syntax App}, tr1'),
   291     (@{const_syntax App}, tr1'),
   291     (@{const_syntax dummy_term}, tr2')]
   292     (@{const_syntax dummy_term}, tr2')]
   292 end
   293 end
   293 *}
   294 *}
   294 
   295 
   295 hide_const dummy_term App valapp
   296 
   296 hide_const (open) Const termify valtermify term_of term_of_num
   297 subsection {* Evaluation setup *}
   297 
   298 
   298 subsection {* Tracing of generated and evaluated code *}
   299 ML {*
   299 
   300 signature CODE_EVALUATION =
   300 definition tracing :: "String.literal => 'a => 'a"
   301 sig
   301 where
   302   val eval_term: theory -> term -> term
       
   303   val put_term: (unit -> term) -> Proof.context -> Proof.context
       
   304   val tracing: string -> 'a -> 'a
       
   305 end;
       
   306 
       
   307 structure Code_Evaluation : CODE_EVALUATION =
       
   308 struct
       
   309 
       
   310 structure Evaluation = Proof_Data (
       
   311   type T = unit -> term
       
   312   fun init _ () = error "Evaluation"
       
   313 );
       
   314 val put_term = Evaluation.put;
       
   315 
       
   316 fun tracing s x = (Output.tracing s; x);
       
   317 
       
   318 fun eval_term thy t = Code_Eval.eval NONE (Evaluation.get, put_term, "Code_Evaluation.put_term")
       
   319   I thy (HOLogic.mk_term_of (fastype_of t) t) [];
       
   320 
       
   321 end
       
   322 *}
       
   323 
       
   324 setup {*
       
   325   Value.add_evaluator ("code", Code_Evaluation.eval_term o ProofContext.theory_of)
       
   326 *}
       
   327 
       
   328 definition tracing :: "String.literal \<Rightarrow> 'a \<Rightarrow> 'a" where
   302   [code del]: "tracing s x = x"
   329   [code del]: "tracing s x = x"
   303 
       
   304 ML {*
       
   305 structure Code_Evaluation =
       
   306 struct
       
   307 
       
   308 fun tracing s x = (Output.tracing s; x)
       
   309 
       
   310 end
       
   311 *}
       
   312 
   330 
   313 code_const "tracing :: String.literal => 'a => 'a"
   331 code_const "tracing :: String.literal => 'a => 'a"
   314   (Eval "Code'_Evaluation.tracing")
   332   (Eval "Code'_Evaluation.tracing")
   315 
   333 
   316 hide_const (open) tracing
       
   317 code_reserved Eval Code_Evaluation
   334 code_reserved Eval Code_Evaluation
   318 
   335 
   319 subsection {* Evaluation setup *}
   336 hide_const dummy_term App valapp
   320 
   337 hide_const (open) Const termify valtermify term_of term_of_num tracing
   321 ML {*
   338 
   322 signature EVAL =
   339 end
   323 sig
       
   324   val eval_ref: (unit -> term) option Unsynchronized.ref
       
   325   val eval_term: theory -> term -> term
       
   326 end;
       
   327 
       
   328 structure Eval : EVAL =
       
   329 struct
       
   330 
       
   331 val eval_ref = Unsynchronized.ref (NONE : (unit -> term) option);
       
   332 
       
   333 fun eval_term thy t =
       
   334   Code_Eval.eval NONE ("Eval.eval_ref", eval_ref) I thy (HOLogic.mk_term_of (fastype_of t) t) [];
       
   335 
       
   336 end;
       
   337 *}
       
   338 
       
   339 setup {*
       
   340   Value.add_evaluator ("code", Eval.eval_term o ProofContext.theory_of)
       
   341 *}
       
   342 
       
   343 end