src/HOL/Code_Evaluation.thy
author wenzelm
Fri Dec 17 17:43:54 2010 +0100 (2010-12-17)
changeset 41229 d797baa3d57c
parent 40884 3113fd4810bd
child 42979 5b9e16259341
permissions -rw-r--r--
replaced command 'nonterminals' by slightly modernized version 'nonterminal';
     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 Plain Typerep Code_Numeral
     9 uses ("Tools/code_evaluation.ML")
    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 code_datatype Const App Abs
    28 
    29 class term_of = typerep +
    30   fixes term_of :: "'a \<Rightarrow> term"
    31 
    32 lemma term_of_anything: "term_of x \<equiv> t"
    33   by (rule eq_reflection) (cases "term_of x", cases t, simp)
    34 
    35 definition valapp :: "('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)
    36   \<Rightarrow> 'a \<times> (unit \<Rightarrow> term) \<Rightarrow> 'b \<times> (unit \<Rightarrow> term)" where
    37   "valapp f x = (fst f (fst x), \<lambda>u. App (snd f ()) (snd x ()))"
    38 
    39 lemma valapp_code [code, code_unfold]:
    40   "valapp (f, tf) (x, tx) = (f x, \<lambda>u. App (tf ()) (tx ()))"
    41   by (simp only: valapp_def fst_conv snd_conv)
    42 
    43 
    44 subsubsection {* Syntax *}
    45 
    46 definition termify :: "'a \<Rightarrow> term" where
    47   [code del]: "termify x = dummy_term"
    48 
    49 abbreviation valtermify :: "'a \<Rightarrow> 'a \<times> (unit \<Rightarrow> term)" where
    50   "valtermify x \<equiv> (x, \<lambda>u. termify x)"
    51 
    52 locale term_syntax
    53 begin
    54 
    55 notation App (infixl "<\<cdot>>" 70)
    56   and valapp (infixl "{\<cdot>}" 70)
    57 
    58 end
    59 
    60 interpretation term_syntax .
    61 
    62 no_notation App (infixl "<\<cdot>>" 70)
    63   and valapp (infixl "{\<cdot>}" 70)
    64 
    65 
    66 subsection {* Tools setup and evaluation *}
    67 
    68 lemma eq_eq_TrueD:
    69   assumes "(x \<equiv> y) \<equiv> Trueprop True"
    70   shows "x \<equiv> y"
    71   using assms by simp
    72 
    73 use "Tools/code_evaluation.ML"
    74 
    75 code_reserved Eval Code_Evaluation
    76 
    77 setup {* Code_Evaluation.setup *}
    78 
    79 
    80 subsection {* @{text term_of} instances *}
    81 
    82 instantiation "fun" :: (typerep, typerep) term_of
    83 begin
    84 
    85 definition
    86   "term_of (f \<Colon> 'a \<Rightarrow> 'b) = Const (STR ''dummy_pattern'') (Typerep.Typerep (STR ''fun'')
    87      [Typerep.typerep TYPE('a), Typerep.typerep TYPE('b)])"
    88 
    89 instance ..
    90 
    91 end
    92 
    93 instantiation String.literal :: term_of
    94 begin
    95 
    96 definition
    97   "term_of s = App (Const (STR ''STR'')
    98     (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''list'') [Typerep.Typerep (STR ''char'') []],
    99       Typerep.Typerep (STR ''String.literal'') []])) (term_of (String.explode s))"
   100 
   101 instance ..
   102 
   103 end
   104 
   105 
   106 subsubsection {* Code generator setup *}
   107 
   108 lemmas [code del] = term.recs term.cases term.size
   109 lemma [code, code del]: "HOL.equal (t1\<Colon>term) t2 \<longleftrightarrow> HOL.equal t1 t2" ..
   110 
   111 lemma [code, code del]: "(term_of \<Colon> typerep \<Rightarrow> term) = term_of" ..
   112 lemma [code, code del]: "(term_of \<Colon> term \<Rightarrow> term) = term_of" ..
   113 lemma [code, code del]: "(term_of \<Colon> String.literal \<Rightarrow> term) = term_of" ..
   114 lemma [code, code del]: "(Code_Evaluation.term_of \<Colon> 'a::{type, term_of} Predicate.pred \<Rightarrow> Code_Evaluation.term)
   115   = Code_Evaluation.term_of" ..
   116 lemma [code, code del]: "(Code_Evaluation.term_of \<Colon> 'a::{type, term_of} Predicate.seq \<Rightarrow> Code_Evaluation.term)
   117   = Code_Evaluation.term_of" ..
   118 
   119 lemma term_of_char [unfolded typerep_fun_def typerep_char_def typerep_nibble_def, code]:
   120   "Code_Evaluation.term_of c =
   121     (let (n, m) = nibble_pair_of_char c
   122   in Code_Evaluation.App (Code_Evaluation.App
   123     (Code_Evaluation.Const (STR ''String.char.Char'') (TYPEREP(nibble \<Rightarrow> nibble \<Rightarrow> char)))
   124       (Code_Evaluation.term_of n)) (Code_Evaluation.term_of m))"
   125   by (subst term_of_anything) rule 
   126 
   127 code_type "term"
   128   (Eval "Term.term")
   129 
   130 code_const Const and App and Abs
   131   (Eval "Term.Const/ ((_), (_))" and "Term.$/ ((_), (_))" and "Term.Abs/ ((_), (_), (_))")
   132 
   133 code_const "term_of \<Colon> String.literal \<Rightarrow> term"
   134   (Eval "HOLogic.mk'_literal")
   135 
   136 code_reserved Eval HOLogic
   137 
   138 
   139 subsubsection {* Numeric types *}
   140 
   141 definition term_of_num_semiring :: "'a\<Colon>semiring_div \<Rightarrow> 'a \<Rightarrow> term" where
   142   "term_of_num_semiring two = (\<lambda>_. dummy_term)"
   143 
   144 lemma (in term_syntax) term_of_num_semiring_code [code]:
   145   "term_of_num_semiring two k = (if k = 0 then termify Int.Pls
   146     else (if k mod two = 0
   147       then termify Int.Bit0 <\<cdot>> term_of_num_semiring two (k div two)
   148       else termify Int.Bit1 <\<cdot>> term_of_num_semiring two (k div two)))"
   149   by (auto simp add: term_of_anything Const_def App_def term_of_num_semiring_def Let_def)
   150 
   151 lemma (in term_syntax) term_of_nat_code [code]:
   152   "term_of (n::nat) = termify (number_of :: int \<Rightarrow> nat) <\<cdot>> term_of_num_semiring (2::nat) n"
   153   by (simp only: term_of_anything)
   154 
   155 lemma (in term_syntax) term_of_code_numeral_code [code]:
   156   "term_of (k::code_numeral) = termify (number_of :: int \<Rightarrow> code_numeral) <\<cdot>> term_of_num_semiring (2::code_numeral) k"
   157   by (simp only: term_of_anything)
   158 
   159 definition term_of_num_ring :: "'a\<Colon>ring_div \<Rightarrow> 'a \<Rightarrow> term" where
   160   "term_of_num_ring two = (\<lambda>_. dummy_term)"
   161 
   162 lemma (in term_syntax) term_of_num_ring_code [code]:
   163   "term_of_num_ring two k = (if k = 0 then termify Int.Pls
   164     else if k = -1 then termify Int.Min
   165     else if k mod two = 0 then termify Int.Bit0 <\<cdot>> term_of_num_ring two (k div two)
   166     else termify Int.Bit1 <\<cdot>> term_of_num_ring two (k div two))"
   167   by (auto simp add: term_of_anything Const_def App_def term_of_num_ring_def Let_def)
   168 
   169 lemma (in term_syntax) term_of_int_code [code]:
   170   "term_of (k::int) = (if k = 0 then termify (0 :: int)
   171     else termify (number_of :: int \<Rightarrow> int) <\<cdot>> term_of_num_ring (2::int) k)"
   172   by (simp only: term_of_anything)
   173 
   174 
   175 subsubsection {* Obfuscation *}
   176 
   177 print_translation {*
   178 let
   179   val term = Const ("<TERM>", dummyT);
   180   fun tr1' [_, _] = term;
   181   fun tr2' [] = term;
   182 in
   183   [(@{const_syntax Const}, tr1'),
   184     (@{const_syntax App}, tr1'),
   185     (@{const_syntax dummy_term}, tr2')]
   186 end
   187 *}
   188 
   189 
   190 subsection {* Diagnostic *}
   191 
   192 definition tracing :: "String.literal \<Rightarrow> 'a \<Rightarrow> 'a" where
   193   [code del]: "tracing s x = x"
   194 
   195 code_const "tracing :: String.literal => 'a => 'a"
   196   (Eval "Code'_Evaluation.tracing")
   197 
   198 
   199 hide_const dummy_term valapp
   200 hide_const (open) Const App Abs termify valtermify term_of term_of_num_semiring term_of_num_ring tracing
   201 
   202 end