src/HOL/Code_Evaluation.thy
author wenzelm
Tue Sep 01 22:32:58 2015 +0200 (2015-09-01)
changeset 61076 bdc1e2f0a86a
parent 60758 d8d85a8172b5
child 61799 4cf66f21b764
permissions -rw-r--r--
eliminated \<Colon>;
     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 @{text term_of}\<close>
    15 
    16 datatype (plugins only: code 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 lemma eq_eq_TrueD:
    72   assumes "(x \<equiv> y) \<equiv> Trueprop True"
    73   shows "x \<equiv> y"
    74   using assms by simp
    75 
    76 code_printing
    77   type_constructor "term" \<rightharpoonup> (Eval) "Term.term"
    78 | constant Const \<rightharpoonup> (Eval) "Term.Const/ ((_), (_))"
    79 | constant App \<rightharpoonup> (Eval) "Term.$/ ((_), (_))"
    80 | constant Abs \<rightharpoonup> (Eval) "Term.Abs/ ((_), (_), (_))"
    81 | constant Free \<rightharpoonup> (Eval) "Term.Free/ ((_), (_))"
    82 
    83 ML_file "Tools/code_evaluation.ML"
    84 
    85 code_reserved Eval Code_Evaluation
    86 
    87 ML_file "~~/src/HOL/Tools/value.ML"
    88 
    89 
    90 subsection \<open>@{text term_of} instances\<close>
    91 
    92 instantiation "fun" :: (typerep, typerep) term_of
    93 begin
    94 
    95 definition
    96   "term_of (f :: 'a \<Rightarrow> 'b) =
    97     Const (STR ''Pure.dummy_pattern'')
    98       (Typerep.Typerep (STR ''fun'') [Typerep.typerep TYPE('a), Typerep.typerep TYPE('b)])"
    99 
   100 instance ..
   101 
   102 end
   103 
   104 declare [[code drop: rec_term case_term "HOL.equal :: term \<Rightarrow> _"
   105   "term_of :: typerep \<Rightarrow> _" "term_of :: term \<Rightarrow> _" "term_of :: String.literal \<Rightarrow> _"
   106   "term_of :: _ Predicate.pred \<Rightarrow> term" "term_of :: _ Predicate.seq \<Rightarrow> term"]]
   107 
   108 lemma term_of_char [unfolded typerep_fun_def typerep_char_def typerep_nibble_def, code]:
   109   "Code_Evaluation.term_of c = (case c of Char x y \<Rightarrow>
   110    Code_Evaluation.App (Code_Evaluation.App
   111     (Code_Evaluation.Const (STR ''String.char.Char'') (TYPEREP(nibble \<Rightarrow> nibble \<Rightarrow> char)))
   112       (Code_Evaluation.term_of x)) (Code_Evaluation.term_of y))"
   113   by (subst term_of_anything) rule 
   114 
   115 lemma term_of_string [code]:
   116    "term_of s = App (Const (STR ''STR'')
   117      (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''list'') [Typerep.Typerep (STR ''char'') []],
   118        Typerep.Typerep (STR ''String.literal'') []])) (term_of (String.explode s))"
   119   by (subst term_of_anything) rule 
   120   
   121 code_printing
   122   constant "term_of :: integer \<Rightarrow> term" \<rightharpoonup> (Eval) "HOLogic.mk'_number/ HOLogic.code'_integerT"
   123 | constant "term_of :: String.literal \<Rightarrow> term" \<rightharpoonup> (Eval) "HOLogic.mk'_literal"
   124 
   125 declare [[code drop: "term_of :: integer \<Rightarrow> _"]]
   126 
   127 lemma term_of_integer [unfolded typerep_fun_def typerep_num_def typerep_integer_def, code]:
   128   "term_of (i :: integer) =
   129   (if i > 0 then 
   130      App (Const (STR ''Num.numeral_class.numeral'') (TYPEREP(num \<Rightarrow> integer)))
   131       (term_of (num_of_integer i))
   132    else if i = 0 then Const (STR ''Groups.zero_class.zero'') TYPEREP(integer)
   133    else
   134      App (Const (STR ''Groups.uminus_class.uminus'') TYPEREP(integer \<Rightarrow> integer))
   135        (term_of (- i)))"
   136 by(rule term_of_anything[THEN meta_eq_to_obj_eq])
   137 
   138 code_reserved Eval HOLogic
   139 
   140 
   141 subsection \<open>Generic reification\<close>
   142 
   143 ML_file "~~/src/HOL/Tools/reification.ML"
   144 
   145 
   146 subsection \<open>Diagnostic\<close>
   147 
   148 definition tracing :: "String.literal \<Rightarrow> 'a \<Rightarrow> 'a" where
   149   [code del]: "tracing s x = x"
   150 
   151 code_printing
   152   constant "tracing :: String.literal => 'a => 'a" \<rightharpoonup> (Eval) "Code'_Evaluation.tracing"
   153 
   154 
   155 hide_const dummy_term valapp
   156 hide_const (open) Const App Abs Free termify valtermify term_of tracing
   157 
   158 end
   159