src/HOL/Code_Evaluation.thy
author hoelzl
Tue Nov 05 09:44:57 2013 +0100 (2013-11-05)
changeset 54257 5c7a3b6b05a9
parent 52435 6646bb548c6b
child 55642 63beb38e9258
permissions -rw-r--r--
generalize SUP and INF to the syntactic type classes Sup and Inf
     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 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 = (case c of Char x y \<Rightarrow>
   123    Code_Evaluation.App (Code_Evaluation.App
   124     (Code_Evaluation.Const (STR ''String.char.Char'') (TYPEREP(nibble \<Rightarrow> nibble \<Rightarrow> char)))
   125       (Code_Evaluation.term_of x)) (Code_Evaluation.term_of y))"
   126   by (subst term_of_anything) rule 
   127 
   128 code_printing
   129   type_constructor "term" \<rightharpoonup> (Eval) "Term.term"
   130 | constant Const \<rightharpoonup> (Eval) "Term.Const/ ((_), (_))"
   131 | constant App \<rightharpoonup> (Eval) "Term.$/ ((_), (_))"
   132 | constant Abs \<rightharpoonup> (Eval) "Term.Abs/ ((_), (_), (_))"
   133 | constant Free \<rightharpoonup> (Eval) "Term.Free/ ((_), (_))"
   134 | constant "term_of \<Colon> integer \<Rightarrow> term" \<rightharpoonup> (Eval) "HOLogic.mk'_number/ HOLogic.code'_integerT"
   135 | constant "term_of \<Colon> String.literal \<Rightarrow> term" \<rightharpoonup> (Eval) "HOLogic.mk'_literal"
   136 
   137 code_reserved Eval HOLogic
   138 
   139 
   140 subsubsection {* Obfuscation *}
   141 
   142 print_translation {*
   143   let
   144     val term = Const ("<TERM>", dummyT);
   145     fun tr1' _ [_, _] = term;
   146     fun tr2' _ [] = term;
   147   in
   148    [(@{const_syntax Const}, tr1'),
   149     (@{const_syntax App}, tr1'),
   150     (@{const_syntax dummy_term}, tr2')]
   151   end
   152 *}
   153 
   154 
   155 subsection {* Diagnostic *}
   156 
   157 definition tracing :: "String.literal \<Rightarrow> 'a \<Rightarrow> 'a" where
   158   [code del]: "tracing s x = x"
   159 
   160 code_printing
   161   constant "tracing :: String.literal => 'a => 'a" \<rightharpoonup> (Eval) "Code'_Evaluation.tracing"
   162 
   163 
   164 subsection {* Generic reification *}
   165 
   166 ML_file "~~/src/HOL/Tools/reification.ML"
   167 
   168 
   169 hide_const dummy_term valapp
   170 hide_const (open) Const App Abs Free termify valtermify term_of tracing
   171 
   172 end
   173