src/HOL/Code_Evaluation.thy
changeset 68028 1f9f973eed2a
parent 66013 03002d10bf1d
child 69605 a96320074298
equal deleted inserted replaced
68027:64559e1ca05b 68028:1f9f973eed2a
    99 code_reserved Eval Code_Evaluation
    99 code_reserved Eval Code_Evaluation
   100 
   100 
   101 ML_file "~~/src/HOL/Tools/value_command.ML"
   101 ML_file "~~/src/HOL/Tools/value_command.ML"
   102 
   102 
   103 
   103 
   104 subsection \<open>\<open>term_of\<close> instances\<close>
   104 subsection \<open>Dedicated \<open>term_of\<close> instances\<close>
   105 
   105 
   106 instantiation "fun" :: (typerep, typerep) term_of
   106 instantiation "fun" :: (typerep, typerep) term_of
   107 begin
   107 begin
   108 
   108 
   109 definition
   109 definition
   117 
   117 
   118 declare [[code drop: rec_term case_term
   118 declare [[code drop: rec_term case_term
   119   "term_of :: typerep \<Rightarrow> _" "term_of :: term \<Rightarrow> _" "term_of :: String.literal \<Rightarrow> _"
   119   "term_of :: typerep \<Rightarrow> _" "term_of :: term \<Rightarrow> _" "term_of :: String.literal \<Rightarrow> _"
   120   "term_of :: _ Predicate.pred \<Rightarrow> term" "term_of :: _ Predicate.seq \<Rightarrow> term"]]
   120   "term_of :: _ Predicate.pred \<Rightarrow> term" "term_of :: _ Predicate.seq \<Rightarrow> term"]]
   121 
   121 
   122 definition case_char :: "'a \<Rightarrow> (num \<Rightarrow> 'a) \<Rightarrow> char \<Rightarrow> 'a"
       
   123   where "case_char f g c = (if c = 0 then f else g (num_of_nat (nat_of_char c)))"
       
   124 
       
   125 lemma term_of_char [unfolded typerep_fun_def typerep_char_def typerep_num_def, code]:
       
   126   "term_of =
       
   127     case_char (Const (STR ''Groups.zero_class.zero'') (TYPEREP(char)))
       
   128     (\<lambda>k. App (Const (STR ''String.Char'') (TYPEREP(num \<Rightarrow> char))) (term_of k))"
       
   129   by (rule ext) (rule term_of_anything [THEN meta_eq_to_obj_eq])
       
   130 
       
   131 lemma term_of_string [code]:
       
   132    "term_of s = App (Const (STR ''STR'')
       
   133      (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''list'') [Typerep.Typerep (STR ''char'') []],
       
   134        Typerep.Typerep (STR ''String.literal'') []])) (term_of (String.explode s))"
       
   135   by (subst term_of_anything) rule 
       
   136   
       
   137 code_printing
   122 code_printing
   138   constant "term_of :: integer \<Rightarrow> term" \<rightharpoonup> (Eval) "HOLogic.mk'_number/ HOLogic.code'_integerT"
   123   constant "term_of :: integer \<Rightarrow> term" \<rightharpoonup> (Eval) "HOLogic.mk'_number/ HOLogic.code'_integerT"
   139 | constant "term_of :: String.literal \<Rightarrow> term" \<rightharpoonup> (Eval) "HOLogic.mk'_literal"
   124 | constant "term_of :: String.literal \<Rightarrow> term" \<rightharpoonup> (Eval) "HOLogic.mk'_literal"
   140 
   125 
   141 declare [[code drop: "term_of :: integer \<Rightarrow> _"]]
   126 declare [[code drop: "term_of :: integer \<Rightarrow> _"]]