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