src/HOL/Code_Evaluation.thy
author wenzelm
Tue Sep 26 20:54:40 2017 +0200 (21 months ago)
changeset 66695 91500c024c7f
parent 66013 03002d10bf1d
child 68028 1f9f973eed2a
permissions -rw-r--r--
tuned;
haftmann@32657
     1
(*  Title:      HOL/Code_Evaluation.thy
haftmann@28228
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@28228
     3
*)
haftmann@28228
     4
wenzelm@60758
     5
section \<open>Term evaluation using the generic code generator\<close>
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
wenzelm@60758
    12
subsection \<open>Term representation\<close>
haftmann@28228
    13
wenzelm@61799
    14
subsubsection \<open>Terms and class \<open>term_of\<close>\<close>
haftmann@28228
    15
haftmann@66013
    16
datatype (plugins only: extraction) "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
wenzelm@60758
    47
subsubsection \<open>Syntax\<close>
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
wenzelm@60758
    69
subsection \<open>Tools setup and evaluation\<close>
haftmann@39564
    70
haftmann@63161
    71
context
haftmann@63161
    72
begin
haftmann@63161
    73
haftmann@63161
    74
qualified definition TERM_OF :: "'a::term_of itself"
haftmann@63161
    75
where
haftmann@63161
    76
  "TERM_OF = snd (Code_Evaluation.term_of :: 'a \<Rightarrow> _, TYPE('a))"
haftmann@63161
    77
haftmann@63161
    78
qualified definition TERM_OF_EQUAL :: "'a::term_of itself"
haftmann@63161
    79
where
haftmann@63161
    80
  "TERM_OF_EQUAL = snd (\<lambda>(a::'a). (Code_Evaluation.term_of a, HOL.eq a), TYPE('a))"
haftmann@63161
    81
haftmann@63161
    82
end
haftmann@63161
    83
haftmann@39567
    84
lemma eq_eq_TrueD:
wenzelm@62958
    85
  fixes x y :: "'a::{}"
haftmann@39567
    86
  assumes "(x \<equiv> y) \<equiv> Trueprop True"
haftmann@39567
    87
  shows "x \<equiv> y"
haftmann@39567
    88
  using assms by simp
haftmann@39567
    89
haftmann@56928
    90
code_printing
haftmann@56928
    91
  type_constructor "term" \<rightharpoonup> (Eval) "Term.term"
haftmann@56928
    92
| constant Const \<rightharpoonup> (Eval) "Term.Const/ ((_), (_))"
haftmann@56928
    93
| constant App \<rightharpoonup> (Eval) "Term.$/ ((_), (_))"
haftmann@56928
    94
| constant Abs \<rightharpoonup> (Eval) "Term.Abs/ ((_), (_), (_))"
haftmann@56928
    95
| constant Free \<rightharpoonup> (Eval) "Term.Free/ ((_), (_))"
haftmann@56928
    96
wenzelm@48891
    97
ML_file "Tools/code_evaluation.ML"
haftmann@39564
    98
haftmann@39564
    99
code_reserved Eval Code_Evaluation
haftmann@39564
   100
wenzelm@63806
   101
ML_file "~~/src/HOL/Tools/value_command.ML"
haftmann@56928
   102
haftmann@39564
   103
wenzelm@61799
   104
subsection \<open>\<open>term_of\<close> instances\<close>
haftmann@39564
   105
haftmann@39564
   106
instantiation "fun" :: (typerep, typerep) term_of
haftmann@39564
   107
begin
haftmann@39564
   108
haftmann@39564
   109
definition
wenzelm@61076
   110
  "term_of (f :: 'a \<Rightarrow> 'b) =
wenzelm@56241
   111
    Const (STR ''Pure.dummy_pattern'')
wenzelm@56241
   112
      (Typerep.Typerep (STR ''fun'') [Typerep.typerep TYPE('a), Typerep.typerep TYPE('b)])"
haftmann@39564
   113
haftmann@39564
   114
instance ..
haftmann@39564
   115
haftmann@39564
   116
end
haftmann@39564
   117
haftmann@66013
   118
declare [[code drop: rec_term case_term
haftmann@56926
   119
  "term_of :: typerep \<Rightarrow> _" "term_of :: term \<Rightarrow> _" "term_of :: String.literal \<Rightarrow> _"
haftmann@56926
   120
  "term_of :: _ Predicate.pred \<Rightarrow> term" "term_of :: _ Predicate.seq \<Rightarrow> term"]]
haftmann@39564
   121
haftmann@62597
   122
definition case_char :: "'a \<Rightarrow> (num \<Rightarrow> 'a) \<Rightarrow> char \<Rightarrow> 'a"
haftmann@62597
   123
  where "case_char f g c = (if c = 0 then f else g (num_of_nat (nat_of_char c)))"
haftmann@62597
   124
haftmann@62597
   125
lemma term_of_char [unfolded typerep_fun_def typerep_char_def typerep_num_def, code]:
haftmann@62597
   126
  "term_of =
haftmann@62597
   127
    case_char (Const (STR ''Groups.zero_class.zero'') (TYPEREP(char)))
haftmann@62597
   128
    (\<lambda>k. App (Const (STR ''String.Char'') (TYPEREP(num \<Rightarrow> char))) (term_of k))"
haftmann@62597
   129
  by (rule ext) (rule term_of_anything [THEN meta_eq_to_obj_eq])
haftmann@39564
   130
haftmann@59484
   131
lemma term_of_string [code]:
haftmann@59484
   132
   "term_of s = App (Const (STR ''STR'')
haftmann@59484
   133
     (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''list'') [Typerep.Typerep (STR ''char'') []],
haftmann@59484
   134
       Typerep.Typerep (STR ''String.literal'') []])) (term_of (String.explode s))"
haftmann@59484
   135
  by (subst term_of_anything) rule 
haftmann@59484
   136
  
haftmann@52435
   137
code_printing
wenzelm@61076
   138
  constant "term_of :: integer \<Rightarrow> term" \<rightharpoonup> (Eval) "HOLogic.mk'_number/ HOLogic.code'_integerT"
wenzelm@61076
   139
| constant "term_of :: String.literal \<Rightarrow> term" \<rightharpoonup> (Eval) "HOLogic.mk'_literal"
haftmann@39564
   140
Andreas@58036
   141
declare [[code drop: "term_of :: integer \<Rightarrow> _"]]
Andreas@58036
   142
Andreas@58036
   143
lemma term_of_integer [unfolded typerep_fun_def typerep_num_def typerep_integer_def, code]:
Andreas@58038
   144
  "term_of (i :: integer) =
Andreas@58038
   145
  (if i > 0 then 
Andreas@58038
   146
     App (Const (STR ''Num.numeral_class.numeral'') (TYPEREP(num \<Rightarrow> integer)))
Andreas@58038
   147
      (term_of (num_of_integer i))
Andreas@58038
   148
   else if i = 0 then Const (STR ''Groups.zero_class.zero'') TYPEREP(integer)
Andreas@58038
   149
   else
Andreas@58038
   150
     App (Const (STR ''Groups.uminus_class.uminus'') TYPEREP(integer \<Rightarrow> integer))
Andreas@58038
   151
       (term_of (- i)))"
haftmann@62597
   152
  by (rule term_of_anything [THEN meta_eq_to_obj_eq])
Andreas@58036
   153
haftmann@39564
   154
code_reserved Eval HOLogic
haftmann@39564
   155
haftmann@39564
   156
wenzelm@60758
   157
subsection \<open>Generic reification\<close>
haftmann@31178
   158
haftmann@56928
   159
ML_file "~~/src/HOL/Tools/reification.ML"
haftmann@31178
   160
haftmann@31178
   161
wenzelm@60758
   162
subsection \<open>Diagnostic\<close>
haftmann@39387
   163
haftmann@39387
   164
definition tracing :: "String.literal \<Rightarrow> 'a \<Rightarrow> 'a" where
haftmann@39387
   165
  [code del]: "tracing s x = x"
haftmann@39387
   166
haftmann@52435
   167
code_printing
haftmann@52435
   168
  constant "tracing :: String.literal => 'a => 'a" \<rightharpoonup> (Eval) "Code'_Evaluation.tracing"
bulwahn@33473
   169
haftmann@31178
   170
bulwahn@40638
   171
hide_const dummy_term valapp
haftmann@51144
   172
hide_const (open) Const App Abs Free termify valtermify term_of tracing
haftmann@28228
   173
haftmann@28228
   174
end