src/HOL/Code_Evaluation.thy
author wenzelm
Sat Nov 04 15:24:40 2017 +0100 (19 months ago)
changeset 67003 49850a679c2c
parent 66013 03002d10bf1d
child 68028 1f9f973eed2a
permissions -rw-r--r--
more robust sorted_entries;
     1 (*  Title:      HOL/Code_Evaluation.thy
     2     Author:     Florian Haftmann, TU Muenchen
     3 *)
     4 
     5 section \<open>Term evaluation using the generic code generator\<close>
     6 
     7 theory Code_Evaluation
     8 imports Typerep Limited_Sequence
     9 keywords "value" :: diag
    10 begin
    11 
    12 subsection \<open>Term representation\<close>
    13 
    14 subsubsection \<open>Terms and class \<open>term_of\<close>\<close>
    15 
    16 datatype (plugins only: extraction) "term" = dummy_term
    17 
    18 definition Const :: "String.literal \<Rightarrow> typerep \<Rightarrow> term" where
    19   "Const _ _ = dummy_term"
    20 
    21 definition App :: "term \<Rightarrow> term \<Rightarrow> term" where
    22   "App _ _ = dummy_term"
    23 
    24 definition Abs :: "String.literal \<Rightarrow> typerep \<Rightarrow> term \<Rightarrow> term" where
    25   "Abs _ _ _ = dummy_term"
    26 
    27 definition Free :: "String.literal \<Rightarrow> typerep \<Rightarrow> term" where
    28   "Free _ _ = dummy_term"
    29 
    30 code_datatype Const App Abs Free
    31 
    32 class term_of = typerep +
    33   fixes term_of :: "'a \<Rightarrow> term"
    34 
    35 lemma term_of_anything: "term_of x \<equiv> t"
    36   by (rule eq_reflection) (cases "term_of x", cases t, simp)
    37 
    38 definition valapp :: "('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)
    39   \<Rightarrow> 'a \<times> (unit \<Rightarrow> term) \<Rightarrow> 'b \<times> (unit \<Rightarrow> term)" where
    40   "valapp f x = (fst f (fst x), \<lambda>u. App (snd f ()) (snd x ()))"
    41 
    42 lemma valapp_code [code, code_unfold]:
    43   "valapp (f, tf) (x, tx) = (f x, \<lambda>u. App (tf ()) (tx ()))"
    44   by (simp only: valapp_def fst_conv snd_conv)
    45 
    46 
    47 subsubsection \<open>Syntax\<close>
    48 
    49 definition termify :: "'a \<Rightarrow> term" where
    50   [code del]: "termify x = dummy_term"
    51 
    52 abbreviation valtermify :: "'a \<Rightarrow> 'a \<times> (unit \<Rightarrow> term)" where
    53   "valtermify x \<equiv> (x, \<lambda>u. termify x)"
    54 
    55 locale term_syntax
    56 begin
    57 
    58 notation App (infixl "<\<cdot>>" 70)
    59   and valapp (infixl "{\<cdot>}" 70)
    60 
    61 end
    62 
    63 interpretation term_syntax .
    64 
    65 no_notation App (infixl "<\<cdot>>" 70)
    66   and valapp (infixl "{\<cdot>}" 70)
    67 
    68 
    69 subsection \<open>Tools setup and evaluation\<close>
    70 
    71 context
    72 begin
    73 
    74 qualified definition TERM_OF :: "'a::term_of itself"
    75 where
    76   "TERM_OF = snd (Code_Evaluation.term_of :: 'a \<Rightarrow> _, TYPE('a))"
    77 
    78 qualified definition TERM_OF_EQUAL :: "'a::term_of itself"
    79 where
    80   "TERM_OF_EQUAL = snd (\<lambda>(a::'a). (Code_Evaluation.term_of a, HOL.eq a), TYPE('a))"
    81 
    82 end
    83 
    84 lemma eq_eq_TrueD:
    85   fixes x y :: "'a::{}"
    86   assumes "(x \<equiv> y) \<equiv> Trueprop True"
    87   shows "x \<equiv> y"
    88   using assms by simp
    89 
    90 code_printing
    91   type_constructor "term" \<rightharpoonup> (Eval) "Term.term"
    92 | constant Const \<rightharpoonup> (Eval) "Term.Const/ ((_), (_))"
    93 | constant App \<rightharpoonup> (Eval) "Term.$/ ((_), (_))"
    94 | constant Abs \<rightharpoonup> (Eval) "Term.Abs/ ((_), (_), (_))"
    95 | constant Free \<rightharpoonup> (Eval) "Term.Free/ ((_), (_))"
    96 
    97 ML_file "Tools/code_evaluation.ML"
    98 
    99 code_reserved Eval Code_Evaluation
   100 
   101 ML_file "~~/src/HOL/Tools/value_command.ML"
   102 
   103 
   104 subsection \<open>\<open>term_of\<close> instances\<close>
   105 
   106 instantiation "fun" :: (typerep, typerep) term_of
   107 begin
   108 
   109 definition
   110   "term_of (f :: 'a \<Rightarrow> 'b) =
   111     Const (STR ''Pure.dummy_pattern'')
   112       (Typerep.Typerep (STR ''fun'') [Typerep.typerep TYPE('a), Typerep.typerep TYPE('b)])"
   113 
   114 instance ..
   115 
   116 end
   117 
   118 declare [[code drop: rec_term case_term
   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"]]
   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
   138   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"
   140 
   141 declare [[code drop: "term_of :: integer \<Rightarrow> _"]]
   142 
   143 lemma term_of_integer [unfolded typerep_fun_def typerep_num_def typerep_integer_def, code]:
   144   "term_of (i :: integer) =
   145   (if i > 0 then 
   146      App (Const (STR ''Num.numeral_class.numeral'') (TYPEREP(num \<Rightarrow> integer)))
   147       (term_of (num_of_integer i))
   148    else if i = 0 then Const (STR ''Groups.zero_class.zero'') TYPEREP(integer)
   149    else
   150      App (Const (STR ''Groups.uminus_class.uminus'') TYPEREP(integer \<Rightarrow> integer))
   151        (term_of (- i)))"
   152   by (rule term_of_anything [THEN meta_eq_to_obj_eq])
   153 
   154 code_reserved Eval HOLogic
   155 
   156 
   157 subsection \<open>Generic reification\<close>
   158 
   159 ML_file "~~/src/HOL/Tools/reification.ML"
   160 
   161 
   162 subsection \<open>Diagnostic\<close>
   163 
   164 definition tracing :: "String.literal \<Rightarrow> 'a \<Rightarrow> 'a" where
   165   [code del]: "tracing s x = x"
   166 
   167 code_printing
   168   constant "tracing :: String.literal => 'a => 'a" \<rightharpoonup> (Eval) "Code'_Evaluation.tracing"
   169 
   170 
   171 hide_const dummy_term valapp
   172 hide_const (open) Const App Abs Free termify valtermify term_of tracing
   173 
   174 end