src/HOL/Code_Evaluation.thy
author haftmann
Wed, 23 Sep 2009 14:00:12 +0200
changeset 32657 5f13912245ff
parent 32371 src/HOL/Code_Eval.thy@3186fa3a4f88
child 32740 9dd0a2f83429
permissions -rw-r--r--
Code_Eval(uation)
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@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
haftmann@28228
    23
code_datatype Const App
haftmann@28228
    24
haftmann@28335
    25
class term_of = typerep +
haftmann@31031
    26
  fixes term_of :: "'a \<Rightarrow> term"
haftmann@28228
    27
haftmann@28228
    28
lemma term_of_anything: "term_of x \<equiv> t"
haftmann@28228
    29
  by (rule eq_reflection) (cases "term_of x", cases t, simp)
haftmann@28228
    30
haftmann@31191
    31
definition valapp :: "('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)
haftmann@31178
    32
  \<Rightarrow> 'a \<times> (unit \<Rightarrow> term) \<Rightarrow> 'b \<times> (unit \<Rightarrow> term)" where
haftmann@31191
    33
  "valapp f x = (fst f (fst x), \<lambda>u. App (snd f ()) (snd x ()))"
haftmann@31178
    34
haftmann@32061
    35
lemma valapp_code [code, code_unfold]:
haftmann@31191
    36
  "valapp (f, tf) (x, tx) = (f x, \<lambda>u. App (tf ()) (tx ()))"
haftmann@31191
    37
  by (simp only: valapp_def fst_conv snd_conv)
haftmann@31178
    38
haftmann@28228
    39
haftmann@28228
    40
subsubsection {* @{text term_of} instances *}
haftmann@28228
    41
haftmann@32344
    42
instantiation "fun" :: (typerep, typerep) term_of
haftmann@32344
    43
begin
haftmann@32344
    44
haftmann@32344
    45
definition
haftmann@32344
    46
  "term_of (f \<Colon> 'a \<Rightarrow> 'b) = Const (STR ''dummy_pattern'') (Typerep.Typerep (STR ''fun'')
haftmann@32344
    47
     [Typerep.typerep TYPE('a), Typerep.typerep TYPE('b)])"
haftmann@32344
    48
haftmann@32344
    49
instance ..
haftmann@32344
    50
haftmann@32344
    51
end
haftmann@32344
    52
haftmann@28228
    53
setup {*
haftmann@28228
    54
let
haftmann@31139
    55
  fun add_term_of tyco raw_vs thy =
haftmann@28228
    56
    let
haftmann@31139
    57
      val vs = map (fn (v, _) => (v, @{sort typerep})) raw_vs;
haftmann@31139
    58
      val ty = Type (tyco, map TFree vs);
haftmann@28228
    59
      val lhs = Const (@{const_name term_of}, ty --> @{typ term})
haftmann@28228
    60
        $ Free ("x", ty);
haftmann@28228
    61
      val rhs = @{term "undefined \<Colon> term"};
haftmann@28228
    62
      val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
haftmann@28243
    63
      fun triv_name_of t = (fst o dest_Free o fst o strip_comb o fst
haftmann@28243
    64
        o HOLogic.dest_eq o HOLogic.dest_Trueprop) t ^ "_triv";
haftmann@28228
    65
    in
haftmann@28228
    66
      thy
haftmann@28228
    67
      |> TheoryTarget.instantiation ([tyco], vs, @{sort term_of})
haftmann@28228
    68
      |> `(fn lthy => Syntax.check_term lthy eq)
haftmann@28965
    69
      |-> (fn eq => Specification.definition (NONE, ((Binding.name (triv_name_of eq), []), eq)))
haftmann@28228
    70
      |> snd
haftmann@31139
    71
      |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
haftmann@28228
    72
    end;
haftmann@31139
    73
  fun ensure_term_of (tyco, (raw_vs, _)) thy =
haftmann@31139
    74
    let
haftmann@31139
    75
      val need_inst = not (can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of})
haftmann@31139
    76
        andalso can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort typerep};
haftmann@31205
    77
    in if need_inst then add_term_of tyco raw_vs thy else thy end;
haftmann@28228
    78
in
haftmann@31139
    79
  Code.type_interpretation ensure_term_of
haftmann@28228
    80
end
haftmann@28228
    81
*}
haftmann@28228
    82
haftmann@28228
    83
setup {*
haftmann@28228
    84
let
haftmann@31139
    85
  fun mk_term_of_eq thy ty vs tyco (c, tys) =
haftmann@28228
    86
    let
haftmann@28228
    87
      val t = list_comb (Const (c, tys ---> ty),
haftmann@28228
    88
        map Free (Name.names Name.context "a" tys));
haftmann@31139
    89
      val (arg, rhs) = pairself (Thm.cterm_of thy o map_types Logic.unvarifyT o Logic.varify)
haftmann@31139
    90
        (t, (map_aterms (fn t as Free (v, ty) => HOLogic.mk_term_of ty t | t => t) o HOLogic.reflect_term) t)
haftmann@31139
    91
      val cty = Thm.ctyp_of thy ty;
haftmann@31139
    92
    in
haftmann@31139
    93
      @{thm term_of_anything}
haftmann@31139
    94
      |> Drule.instantiate' [SOME cty] [SOME arg, SOME rhs]
haftmann@31139
    95
      |> Thm.varifyT
haftmann@28228
    96
    end;
haftmann@31139
    97
  fun add_term_of_code tyco raw_vs raw_cs thy =
haftmann@28228
    98
    let
haftmann@31746
    99
      val algebra = Sign.classes_of thy;
haftmann@31746
   100
      val vs = map (fn (v, sort) =>
haftmann@31746
   101
        (v, curry (Sorts.inter_sort algebra) @{sort typerep} sort)) raw_vs;
haftmann@31139
   102
      val ty = Type (tyco, map TFree vs);
haftmann@31139
   103
      val cs = (map o apsnd o map o map_atyps)
haftmann@31139
   104
        (fn TFree (v, _) => TFree (v, (the o AList.lookup (op =) vs) v)) raw_cs;
haftmann@31139
   105
      val const = AxClass.param_of_inst thy (@{const_name term_of}, tyco);
haftmann@31139
   106
      val eqs = map (mk_term_of_eq thy ty vs tyco) cs;
haftmann@31139
   107
   in
haftmann@31139
   108
      thy
haftmann@31139
   109
      |> Code.del_eqns const
haftmann@31139
   110
      |> fold Code.add_eqn eqs
haftmann@31139
   111
    end;
haftmann@31139
   112
  fun ensure_term_of_code (tyco, (raw_vs, cs)) thy =
haftmann@31139
   113
    let
haftmann@31139
   114
      val has_inst = can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of};
haftmann@31205
   115
    in if has_inst then add_term_of_code tyco raw_vs cs thy else thy end;
haftmann@28228
   116
in
haftmann@31139
   117
  Code.type_interpretation ensure_term_of_code
haftmann@28228
   118
end
haftmann@28228
   119
*}
haftmann@28228
   120
haftmann@28228
   121
haftmann@28228
   122
subsubsection {* Code generator setup *}
haftmann@28228
   123
haftmann@28562
   124
lemmas [code del] = term.recs term.cases term.size
haftmann@28562
   125
lemma [code, code del]: "eq_class.eq (t1\<Colon>term) t2 \<longleftrightarrow> eq_class.eq t1 t2" ..
haftmann@28228
   126
haftmann@28562
   127
lemma [code, code del]: "(term_of \<Colon> typerep \<Rightarrow> term) = term_of" ..
haftmann@28562
   128
lemma [code, code del]: "(term_of \<Colon> term \<Rightarrow> term) = term_of" ..
haftmann@31205
   129
lemma [code, code del]: "(term_of \<Colon> String.literal \<Rightarrow> term) = term_of" ..
haftmann@30427
   130
lemma [code, code del]:
haftmann@32657
   131
  "(Code_Evaluation.term_of \<Colon> 'a::{type, term_of} Predicate.pred \<Rightarrow> Code_Evaluation.term) = Code_Evaluation.term_of" ..
haftmann@30427
   132
lemma [code, code del]:
haftmann@32657
   133
  "(Code_Evaluation.term_of \<Colon> 'a::{type, term_of} Predicate.seq \<Rightarrow> Code_Evaluation.term) = Code_Evaluation.term_of" ..
haftmann@28228
   134
haftmann@32657
   135
lemma term_of_char [unfolded typerep_fun_def typerep_char_def typerep_nibble_def, code]: "Code_Evaluation.term_of c =
haftmann@28243
   136
    (let (n, m) = nibble_pair_of_char c
haftmann@32657
   137
  in Code_Evaluation.App (Code_Evaluation.App (Code_Evaluation.Const (STR ''String.char.Char'') (TYPEREP(nibble \<Rightarrow> nibble \<Rightarrow> char)))
haftmann@32657
   138
    (Code_Evaluation.term_of n)) (Code_Evaluation.term_of m))"
haftmann@28243
   139
  by (subst term_of_anything) rule 
haftmann@28243
   140
haftmann@28228
   141
code_type "term"
haftmann@31031
   142
  (Eval "Term.term")
haftmann@28228
   143
haftmann@28228
   144
code_const Const and App
haftmann@31594
   145
  (Eval "Term.Const/ ((_), (_))" and "Term.$/ ((_), (_))")
haftmann@28228
   146
haftmann@31205
   147
code_const "term_of \<Colon> String.literal \<Rightarrow> term"
haftmann@31048
   148
  (Eval "HOLogic.mk'_message'_string")
haftmann@31048
   149
haftmann@31048
   150
code_reserved Eval HOLogic
haftmann@28228
   151
haftmann@28228
   152
haftmann@31178
   153
subsubsection {* Syntax *}
haftmann@31178
   154
haftmann@31191
   155
definition termify :: "'a \<Rightarrow> term" where
haftmann@31191
   156
  [code del]: "termify x = dummy_term"
haftmann@31191
   157
haftmann@31191
   158
abbreviation valtermify :: "'a \<Rightarrow> 'a \<times> (unit \<Rightarrow> term)" where
haftmann@31191
   159
  "valtermify x \<equiv> (x, \<lambda>u. termify x)"
haftmann@31178
   160
haftmann@31178
   161
setup {*
haftmann@31178
   162
let
haftmann@31178
   163
  fun map_default f xs =
haftmann@31178
   164
    let val ys = map f xs
haftmann@31178
   165
    in if exists is_some ys
haftmann@31178
   166
      then SOME (map2 the_default xs ys)
haftmann@31178
   167
      else NONE
haftmann@31178
   168
    end;
haftmann@31178
   169
  fun subst_termify_app (Const (@{const_name termify}, T), [t]) =
haftmann@31178
   170
        if not (Term.has_abs t)
haftmann@31178
   171
        then if fold_aterms (fn Const _ => I | _ => K false) t true
haftmann@31191
   172
          then SOME (HOLogic.reflect_term t)
haftmann@31178
   173
          else error "Cannot termify expression containing variables"
haftmann@31178
   174
        else error "Cannot termify expression containing abstraction"
haftmann@31178
   175
    | subst_termify_app (t, ts) = case map_default subst_termify ts
haftmann@31178
   176
       of SOME ts' => SOME (list_comb (t, ts'))
haftmann@31178
   177
        | NONE => NONE
haftmann@31178
   178
  and subst_termify (Abs (v, T, t)) = (case subst_termify t
haftmann@31178
   179
       of SOME t' => SOME (Abs (v, T, t'))
haftmann@31178
   180
        | NONE => NONE)
haftmann@31178
   181
    | subst_termify t = subst_termify_app (strip_comb t) 
haftmann@31178
   182
  fun check_termify ts ctxt = map_default subst_termify ts
haftmann@31178
   183
    |> Option.map (rpair ctxt)
haftmann@31178
   184
in
haftmann@31178
   185
  Context.theory_map (Syntax.add_term_check 0 "termify" check_termify)
haftmann@31178
   186
end;
haftmann@31178
   187
*}
haftmann@31178
   188
haftmann@31178
   189
locale term_syntax
haftmann@31178
   190
begin
haftmann@31178
   191
haftmann@31191
   192
notation App (infixl "<\<cdot>>" 70)
haftmann@31191
   193
  and valapp (infixl "{\<cdot>}" 70)
haftmann@31178
   194
haftmann@31178
   195
end
haftmann@31178
   196
haftmann@31178
   197
interpretation term_syntax .
haftmann@31178
   198
haftmann@31191
   199
no_notation App (infixl "<\<cdot>>" 70)
haftmann@31191
   200
  and valapp (infixl "{\<cdot>}" 70)
haftmann@31191
   201
haftmann@31191
   202
haftmann@31191
   203
subsection {* Numeric types *}
haftmann@31191
   204
haftmann@31191
   205
definition term_of_num :: "'a\<Colon>{semiring_div} \<Rightarrow> 'a\<Colon>{semiring_div} \<Rightarrow> term" where
haftmann@31191
   206
  "term_of_num two = (\<lambda>_. dummy_term)"
haftmann@31191
   207
haftmann@31191
   208
lemma (in term_syntax) term_of_num_code [code]:
haftmann@31191
   209
  "term_of_num two k = (if k = 0 then termify Int.Pls
haftmann@31191
   210
    else (if k mod two = 0
haftmann@31191
   211
      then termify Int.Bit0 <\<cdot>> term_of_num two (k div two)
haftmann@31191
   212
      else termify Int.Bit1 <\<cdot>> term_of_num two (k div two)))"
haftmann@31191
   213
  by (auto simp add: term_of_anything Const_def App_def term_of_num_def Let_def)
haftmann@31191
   214
haftmann@31191
   215
lemma (in term_syntax) term_of_nat_code [code]:
haftmann@31191
   216
  "term_of (n::nat) = termify (number_of :: int \<Rightarrow> nat) <\<cdot>> term_of_num (2::nat) n"
haftmann@31191
   217
  by (simp only: term_of_anything)
haftmann@31191
   218
haftmann@31191
   219
lemma (in term_syntax) term_of_int_code [code]:
haftmann@31191
   220
  "term_of (k::int) = (if k = 0 then termify (0 :: int)
haftmann@31191
   221
    else if k > 0 then termify (number_of :: int \<Rightarrow> int) <\<cdot>> term_of_num (2::int) k
haftmann@31191
   222
      else termify (uminus :: int \<Rightarrow> int) <\<cdot>> (termify (number_of :: int \<Rightarrow> int) <\<cdot>> term_of_num (2::int) (- k)))"
haftmann@31191
   223
  by (simp only: term_of_anything)
haftmann@31191
   224
haftmann@31205
   225
lemma (in term_syntax) term_of_code_numeral_code [code]:
haftmann@31205
   226
  "term_of (k::code_numeral) = termify (number_of :: int \<Rightarrow> code_numeral) <\<cdot>> term_of_num (2::code_numeral) k"
haftmann@31203
   227
  by (simp only: term_of_anything)
haftmann@31191
   228
haftmann@31191
   229
subsection {* Obfuscate *}
haftmann@31178
   230
haftmann@31178
   231
print_translation {*
haftmann@31178
   232
let
haftmann@31178
   233
  val term = Const ("<TERM>", dummyT);
haftmann@31178
   234
  fun tr1' [_, _] = term;
haftmann@31178
   235
  fun tr2' [] = term;
haftmann@31178
   236
in
haftmann@31178
   237
  [(@{const_syntax Const}, tr1'),
haftmann@31178
   238
    (@{const_syntax App}, tr1'),
haftmann@31178
   239
    (@{const_syntax dummy_term}, tr2')]
haftmann@31178
   240
end
haftmann@31178
   241
*}
haftmann@31178
   242
haftmann@31191
   243
hide const dummy_term App valapp
haftmann@31191
   244
hide (open) const Const termify valtermify term_of term_of_num
haftmann@31178
   245
haftmann@31178
   246
haftmann@28228
   247
subsection {* Evaluation setup *}
haftmann@28228
   248
haftmann@28228
   249
ML {*
haftmann@28228
   250
signature EVAL =
haftmann@28228
   251
sig
haftmann@28228
   252
  val eval_ref: (unit -> term) option ref
haftmann@28228
   253
  val eval_term: theory -> term -> term
haftmann@28228
   254
end;
haftmann@28228
   255
haftmann@28228
   256
structure Eval : EVAL =
haftmann@28228
   257
struct
haftmann@28228
   258
haftmann@28228
   259
val eval_ref = ref (NONE : (unit -> term) option);
haftmann@28228
   260
haftmann@28228
   261
fun eval_term thy t =
haftmann@31139
   262
  Code_ML.eval NONE ("Eval.eval_ref", eval_ref) I thy (HOLogic.mk_term_of (fastype_of t) t) [];
haftmann@28228
   263
haftmann@28228
   264
end;
haftmann@28228
   265
*}
haftmann@28228
   266
haftmann@28228
   267
setup {*
haftmann@28228
   268
  Value.add_evaluator ("code", Eval.eval_term o ProofContext.theory_of)
haftmann@28228
   269
*}
haftmann@28228
   270
haftmann@28228
   271
end