src/HOL/Code_Evaluation.thy
author haftmann
Fri May 09 08:13:37 2014 +0200 (2014-05-09)
changeset 56928 1e50fddbe1f5
parent 56927 4044a7d1720f
child 58036 f23045003476
permissions -rw-r--r--
dropped term_of obfuscation -- not really required;
tuned theory structure
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@56925
     9
keywords "value" :: diag
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@56928
    76
code_printing
haftmann@56928
    77
  type_constructor "term" \<rightharpoonup> (Eval) "Term.term"
haftmann@56928
    78
| constant Const \<rightharpoonup> (Eval) "Term.Const/ ((_), (_))"
haftmann@56928
    79
| constant App \<rightharpoonup> (Eval) "Term.$/ ((_), (_))"
haftmann@56928
    80
| constant Abs \<rightharpoonup> (Eval) "Term.Abs/ ((_), (_), (_))"
haftmann@56928
    81
| constant Free \<rightharpoonup> (Eval) "Term.Free/ ((_), (_))"
haftmann@56928
    82
wenzelm@48891
    83
ML_file "Tools/code_evaluation.ML"
haftmann@39564
    84
haftmann@39564
    85
code_reserved Eval Code_Evaluation
haftmann@39564
    86
haftmann@56928
    87
ML_file "~~/src/HOL/Tools/value.ML"
haftmann@56928
    88
haftmann@39564
    89
haftmann@39564
    90
subsection {* @{text term_of} instances *}
haftmann@39564
    91
haftmann@39564
    92
instantiation "fun" :: (typerep, typerep) term_of
haftmann@39564
    93
begin
haftmann@39564
    94
haftmann@39564
    95
definition
wenzelm@56241
    96
  "term_of (f \<Colon> 'a \<Rightarrow> 'b) =
wenzelm@56241
    97
    Const (STR ''Pure.dummy_pattern'')
wenzelm@56241
    98
      (Typerep.Typerep (STR ''fun'') [Typerep.typerep TYPE('a), Typerep.typerep TYPE('b)])"
haftmann@39564
    99
haftmann@39564
   100
instance ..
haftmann@39564
   101
haftmann@39564
   102
end
haftmann@39564
   103
haftmann@39564
   104
instantiation String.literal :: term_of
haftmann@39564
   105
begin
haftmann@39564
   106
haftmann@39564
   107
definition
haftmann@39564
   108
  "term_of s = App (Const (STR ''STR'')
haftmann@39564
   109
    (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''list'') [Typerep.Typerep (STR ''char'') []],
haftmann@39564
   110
      Typerep.Typerep (STR ''String.literal'') []])) (term_of (String.explode s))"
haftmann@39564
   111
haftmann@39564
   112
instance ..
haftmann@39564
   113
haftmann@39564
   114
end
haftmann@39564
   115
haftmann@56926
   116
declare [[code drop: rec_term case_term size_term "size :: term \<Rightarrow> _" "HOL.equal :: term \<Rightarrow> _"
haftmann@56926
   117
  "term_of :: typerep \<Rightarrow> _" "term_of :: term \<Rightarrow> _" "term_of :: String.literal \<Rightarrow> _"
haftmann@56926
   118
  "term_of :: _ Predicate.pred \<Rightarrow> term" "term_of :: _ Predicate.seq \<Rightarrow> term"]]
haftmann@39564
   119
haftmann@39564
   120
lemma term_of_char [unfolded typerep_fun_def typerep_char_def typerep_nibble_def, code]:
haftmann@51160
   121
  "Code_Evaluation.term_of c = (case c of Char x y \<Rightarrow>
haftmann@51160
   122
   Code_Evaluation.App (Code_Evaluation.App
haftmann@39564
   123
    (Code_Evaluation.Const (STR ''String.char.Char'') (TYPEREP(nibble \<Rightarrow> nibble \<Rightarrow> char)))
haftmann@51160
   124
      (Code_Evaluation.term_of x)) (Code_Evaluation.term_of y))"
haftmann@39564
   125
  by (subst term_of_anything) rule 
haftmann@39564
   126
haftmann@52435
   127
code_printing
haftmann@56928
   128
  constant "term_of \<Colon> integer \<Rightarrow> term" \<rightharpoonup> (Eval) "HOLogic.mk'_number/ HOLogic.code'_integerT"
haftmann@52435
   129
| constant "term_of \<Colon> String.literal \<Rightarrow> term" \<rightharpoonup> (Eval) "HOLogic.mk'_literal"
haftmann@39564
   130
haftmann@39564
   131
code_reserved Eval HOLogic
haftmann@39564
   132
haftmann@39564
   133
haftmann@56928
   134
subsection {* Generic reification *}
haftmann@31178
   135
haftmann@56928
   136
ML_file "~~/src/HOL/Tools/reification.ML"
haftmann@31178
   137
haftmann@31178
   138
haftmann@39564
   139
subsection {* Diagnostic *}
haftmann@39387
   140
haftmann@39387
   141
definition tracing :: "String.literal \<Rightarrow> 'a \<Rightarrow> 'a" where
haftmann@39387
   142
  [code del]: "tracing s x = x"
haftmann@39387
   143
haftmann@52435
   144
code_printing
haftmann@52435
   145
  constant "tracing :: String.literal => 'a => 'a" \<rightharpoonup> (Eval) "Code'_Evaluation.tracing"
bulwahn@33473
   146
haftmann@31178
   147
bulwahn@40638
   148
hide_const dummy_term valapp
haftmann@51144
   149
hide_const (open) Const App Abs Free termify valtermify term_of tracing
haftmann@28228
   150
haftmann@28228
   151
end
haftmann@51143
   152