src/HOL/Code_Evaluation.thy
author haftmann
Fri May 09 08:13:36 2014 +0200 (2014-05-09)
changeset 56925 601edd9a6859
parent 56241 029246729dc0
child 56926 aaea99edc040
permissions -rw-r--r--
degeneralized value command into HOL
     1 (*  Title:      HOL/Code_Evaluation.thy
     2     Author:     Florian Haftmann, TU Muenchen
     3 *)
     4 
     5 header {* Term evaluation using the generic code generator *}
     6 
     7 theory Code_Evaluation
     8 imports Typerep Limited_Sequence
     9 keywords "value" :: diag
    10 begin
    11 
    12 subsection {* Term representation *}
    13 
    14 subsubsection {* Terms and class @{text term_of} *}
    15 
    16 datatype "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 {* Syntax *}
    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 {* Tools setup and evaluation *}
    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 ML_file "Tools/code_evaluation.ML"
    77 
    78 code_reserved Eval Code_Evaluation
    79 
    80 setup {* Code_Evaluation.setup *}
    81 
    82 
    83 subsection {* @{text term_of} instances *}
    84 
    85 instantiation "fun" :: (typerep, typerep) term_of
    86 begin
    87 
    88 definition
    89   "term_of (f \<Colon> 'a \<Rightarrow> 'b) =
    90     Const (STR ''Pure.dummy_pattern'')
    91       (Typerep.Typerep (STR ''fun'') [Typerep.typerep TYPE('a), Typerep.typerep TYPE('b)])"
    92 
    93 instance ..
    94 
    95 end
    96 
    97 instantiation String.literal :: term_of
    98 begin
    99 
   100 definition
   101   "term_of s = App (Const (STR ''STR'')
   102     (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''list'') [Typerep.Typerep (STR ''char'') []],
   103       Typerep.Typerep (STR ''String.literal'') []])) (term_of (String.explode s))"
   104 
   105 instance ..
   106 
   107 end
   108 
   109 
   110 subsubsection {* Code generator setup *}
   111 
   112 lemmas [code del] = term.rec term.case term.size
   113 lemma [code, code del]: "HOL.equal (t1\<Colon>term) t2 \<longleftrightarrow> HOL.equal t1 t2" ..
   114 
   115 lemma [code, code del]: "(term_of \<Colon> typerep \<Rightarrow> term) = term_of" ..
   116 lemma [code, code del]: "(term_of \<Colon> term \<Rightarrow> term) = term_of" ..
   117 lemma [code, code del]: "(term_of \<Colon> String.literal \<Rightarrow> term) = term_of" ..
   118 lemma [code, code del]: "(Code_Evaluation.term_of \<Colon> 'a::{type, term_of} Predicate.pred \<Rightarrow> Code_Evaluation.term)
   119   = Code_Evaluation.term_of" ..
   120 lemma [code, code del]: "(Code_Evaluation.term_of \<Colon> 'a::{type, term_of} Predicate.seq \<Rightarrow> Code_Evaluation.term)
   121   = Code_Evaluation.term_of" ..
   122 
   123 lemma term_of_char [unfolded typerep_fun_def typerep_char_def typerep_nibble_def, code]:
   124   "Code_Evaluation.term_of c = (case c of Char x y \<Rightarrow>
   125    Code_Evaluation.App (Code_Evaluation.App
   126     (Code_Evaluation.Const (STR ''String.char.Char'') (TYPEREP(nibble \<Rightarrow> nibble \<Rightarrow> char)))
   127       (Code_Evaluation.term_of x)) (Code_Evaluation.term_of y))"
   128   by (subst term_of_anything) rule 
   129 
   130 code_printing
   131   type_constructor "term" \<rightharpoonup> (Eval) "Term.term"
   132 | constant Const \<rightharpoonup> (Eval) "Term.Const/ ((_), (_))"
   133 | constant App \<rightharpoonup> (Eval) "Term.$/ ((_), (_))"
   134 | constant Abs \<rightharpoonup> (Eval) "Term.Abs/ ((_), (_), (_))"
   135 | constant Free \<rightharpoonup> (Eval) "Term.Free/ ((_), (_))"
   136 | constant "term_of \<Colon> integer \<Rightarrow> term" \<rightharpoonup> (Eval) "HOLogic.mk'_number/ HOLogic.code'_integerT"
   137 | constant "term_of \<Colon> String.literal \<Rightarrow> term" \<rightharpoonup> (Eval) "HOLogic.mk'_literal"
   138 
   139 code_reserved Eval HOLogic
   140 
   141 
   142 subsubsection {* Obfuscation *}
   143 
   144 print_translation {*
   145   let
   146     val term = Const ("<TERM>", dummyT);
   147     fun tr1' _ [_, _] = term;
   148     fun tr2' _ [] = term;
   149   in
   150    [(@{const_syntax Const}, tr1'),
   151     (@{const_syntax App}, tr1'),
   152     (@{const_syntax dummy_term}, tr2')]
   153   end
   154 *}
   155 
   156 
   157 subsection {* Diagnostic *}
   158 
   159 definition tracing :: "String.literal \<Rightarrow> 'a \<Rightarrow> 'a" where
   160   [code del]: "tracing s x = x"
   161 
   162 code_printing
   163   constant "tracing :: String.literal => 'a => 'a" \<rightharpoonup> (Eval) "Code'_Evaluation.tracing"
   164 
   165 
   166 subsection {* Interactive evaluation *}
   167 
   168 ML_file "~~/src/HOL/Tools/value.ML"
   169 
   170 setup {*
   171   Value.setup
   172   #> Value.add_evaluator ("nbe", Nbe.dynamic_value)
   173   #> Value.add_evaluator ("code", Code_Evaluation.dynamic_value_strict)
   174 *}
   175 
   176 
   177 subsection {* Generic reification *}
   178 
   179 ML_file "~~/src/HOL/Tools/reification.ML"
   180 
   181 
   182 hide_const dummy_term valapp
   183 hide_const (open) Const App Abs Free termify valtermify term_of tracing
   184 
   185 end
   186