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