src/HOL/Code_Evaluation.thy
author haftmann
Sun Jun 23 21:16:07 2013 +0200 (2013-06-23)
changeset 52435 6646bb548c6b
parent 52286 8170e5327c02
child 55642 63beb38e9258
permissions -rw-r--r--
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
haftmann@32657
     1
(*  Title:      HOL/Code_Evaluation.thy
haftmann@28228
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@28228
     3
*)
haftmann@28228
     4
haftmann@28228
     5
header {* Term evaluation using the generic code generator *}
haftmann@28228
     6
haftmann@32657
     7
theory Code_Evaluation
haftmann@51126
     8
imports Typerep Limited_Sequence
haftmann@28228
     9
begin
haftmann@28228
    10
haftmann@28228
    11
subsection {* Term representation *}
haftmann@28228
    12
haftmann@28228
    13
subsubsection {* Terms and class @{text term_of} *}
haftmann@28228
    14
haftmann@28228
    15
datatype "term" = dummy_term
haftmann@28228
    16
haftmann@31205
    17
definition Const :: "String.literal \<Rightarrow> typerep \<Rightarrow> term" where
haftmann@28228
    18
  "Const _ _ = dummy_term"
haftmann@28228
    19
haftmann@28661
    20
definition App :: "term \<Rightarrow> term \<Rightarrow> term" where
haftmann@28228
    21
  "App _ _ = dummy_term"
haftmann@28228
    22
bulwahn@40638
    23
definition Abs :: "String.literal \<Rightarrow> typerep \<Rightarrow> term \<Rightarrow> term" where
bulwahn@40638
    24
  "Abs _ _ _ = dummy_term"
bulwahn@40638
    25
bulwahn@42979
    26
definition Free :: "String.literal \<Rightarrow> typerep \<Rightarrow> term" where
bulwahn@42979
    27
  "Free _ _ = dummy_term"
bulwahn@42979
    28
bulwahn@42979
    29
code_datatype Const App Abs Free
haftmann@28228
    30
haftmann@28335
    31
class term_of = typerep +
haftmann@31031
    32
  fixes term_of :: "'a \<Rightarrow> term"
haftmann@28228
    33
haftmann@28228
    34
lemma term_of_anything: "term_of x \<equiv> t"
haftmann@28228
    35
  by (rule eq_reflection) (cases "term_of x", cases t, simp)
haftmann@28228
    36
haftmann@31191
    37
definition valapp :: "('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)
haftmann@31178
    38
  \<Rightarrow> 'a \<times> (unit \<Rightarrow> term) \<Rightarrow> 'b \<times> (unit \<Rightarrow> term)" where
haftmann@31191
    39
  "valapp f x = (fst f (fst x), \<lambda>u. App (snd f ()) (snd x ()))"
haftmann@31178
    40
haftmann@32069
    41
lemma valapp_code [code, code_unfold]:
haftmann@31191
    42
  "valapp (f, tf) (x, tx) = (f x, \<lambda>u. App (tf ()) (tx ()))"
haftmann@31191
    43
  by (simp only: valapp_def fst_conv snd_conv)
haftmann@31178
    44
haftmann@28228
    45
haftmann@31178
    46
subsubsection {* Syntax *}
haftmann@31178
    47
haftmann@31191
    48
definition termify :: "'a \<Rightarrow> term" where
haftmann@31191
    49
  [code del]: "termify x = dummy_term"
haftmann@31191
    50
haftmann@31191
    51
abbreviation valtermify :: "'a \<Rightarrow> 'a \<times> (unit \<Rightarrow> term)" where
haftmann@31191
    52
  "valtermify x \<equiv> (x, \<lambda>u. termify x)"
haftmann@31178
    53
haftmann@31178
    54
locale term_syntax
haftmann@31178
    55
begin
haftmann@31178
    56
haftmann@31191
    57
notation App (infixl "<\<cdot>>" 70)
haftmann@31191
    58
  and valapp (infixl "{\<cdot>}" 70)
haftmann@31178
    59
haftmann@31178
    60
end
haftmann@31178
    61
haftmann@31178
    62
interpretation term_syntax .
haftmann@31178
    63
haftmann@31191
    64
no_notation App (infixl "<\<cdot>>" 70)
haftmann@31191
    65
  and valapp (infixl "{\<cdot>}" 70)
haftmann@31191
    66
haftmann@31191
    67
haftmann@39564
    68
subsection {* Tools setup and evaluation *}
haftmann@39564
    69
haftmann@39567
    70
lemma eq_eq_TrueD:
haftmann@39567
    71
  assumes "(x \<equiv> y) \<equiv> Trueprop True"
haftmann@39567
    72
  shows "x \<equiv> y"
haftmann@39567
    73
  using assms by simp
haftmann@39567
    74
wenzelm@48891
    75
ML_file "Tools/code_evaluation.ML"
haftmann@39564
    76
haftmann@39564
    77
code_reserved Eval Code_Evaluation
haftmann@39564
    78
haftmann@39564
    79
setup {* Code_Evaluation.setup *}
haftmann@39564
    80
haftmann@39564
    81
haftmann@39564
    82
subsection {* @{text term_of} instances *}
haftmann@39564
    83
haftmann@39564
    84
instantiation "fun" :: (typerep, typerep) term_of
haftmann@39564
    85
begin
haftmann@39564
    86
haftmann@39564
    87
definition
haftmann@39564
    88
  "term_of (f \<Colon> 'a \<Rightarrow> 'b) = Const (STR ''dummy_pattern'') (Typerep.Typerep (STR ''fun'')
haftmann@39564
    89
     [Typerep.typerep TYPE('a), Typerep.typerep TYPE('b)])"
haftmann@39564
    90
haftmann@39564
    91
instance ..
haftmann@39564
    92
haftmann@39564
    93
end
haftmann@39564
    94
haftmann@39564
    95
instantiation String.literal :: term_of
haftmann@39564
    96
begin
haftmann@39564
    97
haftmann@39564
    98
definition
haftmann@39564
    99
  "term_of s = App (Const (STR ''STR'')
haftmann@39564
   100
    (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''list'') [Typerep.Typerep (STR ''char'') []],
haftmann@39564
   101
      Typerep.Typerep (STR ''String.literal'') []])) (term_of (String.explode s))"
haftmann@39564
   102
haftmann@39564
   103
instance ..
haftmann@39564
   104
haftmann@39564
   105
end
haftmann@39564
   106
haftmann@39564
   107
haftmann@39564
   108
subsubsection {* Code generator setup *}
haftmann@39564
   109
haftmann@39564
   110
lemmas [code del] = term.recs term.cases term.size
haftmann@39564
   111
lemma [code, code del]: "HOL.equal (t1\<Colon>term) t2 \<longleftrightarrow> HOL.equal t1 t2" ..
haftmann@39564
   112
haftmann@39564
   113
lemma [code, code del]: "(term_of \<Colon> typerep \<Rightarrow> term) = term_of" ..
haftmann@39564
   114
lemma [code, code del]: "(term_of \<Colon> term \<Rightarrow> term) = term_of" ..
haftmann@39564
   115
lemma [code, code del]: "(term_of \<Colon> String.literal \<Rightarrow> term) = term_of" ..
haftmann@39564
   116
lemma [code, code del]: "(Code_Evaluation.term_of \<Colon> 'a::{type, term_of} Predicate.pred \<Rightarrow> Code_Evaluation.term)
haftmann@39564
   117
  = Code_Evaluation.term_of" ..
haftmann@39564
   118
lemma [code, code del]: "(Code_Evaluation.term_of \<Colon> 'a::{type, term_of} Predicate.seq \<Rightarrow> Code_Evaluation.term)
haftmann@39564
   119
  = Code_Evaluation.term_of" ..
haftmann@39564
   120
haftmann@39564
   121
lemma term_of_char [unfolded typerep_fun_def typerep_char_def typerep_nibble_def, code]:
haftmann@51160
   122
  "Code_Evaluation.term_of c = (case c of Char x y \<Rightarrow>
haftmann@51160
   123
   Code_Evaluation.App (Code_Evaluation.App
haftmann@39564
   124
    (Code_Evaluation.Const (STR ''String.char.Char'') (TYPEREP(nibble \<Rightarrow> nibble \<Rightarrow> char)))
haftmann@51160
   125
      (Code_Evaluation.term_of x)) (Code_Evaluation.term_of y))"
haftmann@39564
   126
  by (subst term_of_anything) rule 
haftmann@39564
   127
haftmann@52435
   128
code_printing
haftmann@52435
   129
  type_constructor "term" \<rightharpoonup> (Eval) "Term.term"
haftmann@52435
   130
| constant Const \<rightharpoonup> (Eval) "Term.Const/ ((_), (_))"
haftmann@52435
   131
| constant App \<rightharpoonup> (Eval) "Term.$/ ((_), (_))"
haftmann@52435
   132
| constant Abs \<rightharpoonup> (Eval) "Term.Abs/ ((_), (_), (_))"
haftmann@52435
   133
| constant Free \<rightharpoonup> (Eval) "Term.Free/ ((_), (_))"
haftmann@52435
   134
| constant "term_of \<Colon> integer \<Rightarrow> term" \<rightharpoonup> (Eval) "HOLogic.mk'_number/ HOLogic.code'_integerT"
haftmann@52435
   135
| constant "term_of \<Colon> String.literal \<Rightarrow> term" \<rightharpoonup> (Eval) "HOLogic.mk'_literal"
haftmann@39564
   136
haftmann@39564
   137
code_reserved Eval HOLogic
haftmann@39564
   138
haftmann@39564
   139
haftmann@39564
   140
subsubsection {* Obfuscation *}
haftmann@31178
   141
haftmann@31178
   142
print_translation {*
wenzelm@52143
   143
  let
wenzelm@52143
   144
    val term = Const ("<TERM>", dummyT);
wenzelm@52143
   145
    fun tr1' _ [_, _] = term;
wenzelm@52143
   146
    fun tr2' _ [] = term;
wenzelm@52143
   147
  in
wenzelm@52143
   148
   [(@{const_syntax Const}, tr1'),
haftmann@31178
   149
    (@{const_syntax App}, tr1'),
haftmann@31178
   150
    (@{const_syntax dummy_term}, tr2')]
wenzelm@52143
   151
  end
haftmann@31178
   152
*}
haftmann@31178
   153
haftmann@31178
   154
haftmann@39564
   155
subsection {* Diagnostic *}
haftmann@39387
   156
haftmann@39387
   157
definition tracing :: "String.literal \<Rightarrow> 'a \<Rightarrow> 'a" where
haftmann@39387
   158
  [code del]: "tracing s x = x"
haftmann@39387
   159
haftmann@52435
   160
code_printing
haftmann@52435
   161
  constant "tracing :: String.literal => 'a => 'a" \<rightharpoonup> (Eval) "Code'_Evaluation.tracing"
bulwahn@33473
   162
haftmann@31178
   163
haftmann@52286
   164
subsection {* Generic reification *}
haftmann@52286
   165
haftmann@52286
   166
ML_file "~~/src/HOL/Tools/reification.ML"
haftmann@52286
   167
haftmann@52286
   168
bulwahn@40638
   169
hide_const dummy_term valapp
haftmann@51144
   170
hide_const (open) Const App Abs Free termify valtermify term_of tracing
haftmann@28228
   171
haftmann@28228
   172
end
haftmann@51143
   173