src/HOL/Code_Eval.thy
author haftmann
Thu, 14 May 2009 08:22:07 +0200
changeset 31144 bdc1504ad456
parent 31139 0b615ac7eeaf
child 31178 27afaaa6547a
permissions -rw-r--r--
dropped accidental debug messages
haftmann@28228
     1
(*  Title:      HOL/Code_Eval.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@28228
     7
theory Code_Eval
haftmann@28952
     8
imports Plain Typerep
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@28661
    17
definition Const :: "message_string \<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@28228
    31
haftmann@28228
    32
subsubsection {* @{text term_of} instances *}
haftmann@28228
    33
haftmann@28228
    34
setup {*
haftmann@28228
    35
let
haftmann@31139
    36
  fun add_term_of tyco raw_vs thy =
haftmann@28228
    37
    let
haftmann@31139
    38
      val vs = map (fn (v, _) => (v, @{sort typerep})) raw_vs;
haftmann@31139
    39
      val ty = Type (tyco, map TFree vs);
haftmann@28228
    40
      val lhs = Const (@{const_name term_of}, ty --> @{typ term})
haftmann@28228
    41
        $ Free ("x", ty);
haftmann@28228
    42
      val rhs = @{term "undefined \<Colon> term"};
haftmann@28228
    43
      val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
haftmann@28243
    44
      fun triv_name_of t = (fst o dest_Free o fst o strip_comb o fst
haftmann@28243
    45
        o HOLogic.dest_eq o HOLogic.dest_Trueprop) t ^ "_triv";
haftmann@28228
    46
    in
haftmann@28228
    47
      thy
haftmann@28228
    48
      |> TheoryTarget.instantiation ([tyco], vs, @{sort term_of})
haftmann@28228
    49
      |> `(fn lthy => Syntax.check_term lthy eq)
haftmann@28965
    50
      |-> (fn eq => Specification.definition (NONE, ((Binding.name (triv_name_of eq), []), eq)))
haftmann@28228
    51
      |> snd
haftmann@31139
    52
      |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
haftmann@28228
    53
    end;
haftmann@31139
    54
  fun ensure_term_of (tyco, (raw_vs, _)) thy =
haftmann@31139
    55
    let
haftmann@31139
    56
      val need_inst = not (can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of})
haftmann@31139
    57
        andalso can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort typerep};
haftmann@31139
    58
    in
haftmann@31139
    59
      thy
haftmann@31139
    60
      |> need_inst ? add_term_of tyco raw_vs
haftmann@31139
    61
    end;
haftmann@28228
    62
in
haftmann@31139
    63
  Code.type_interpretation ensure_term_of
haftmann@28228
    64
end
haftmann@28228
    65
*}
haftmann@28228
    66
haftmann@28228
    67
setup {*
haftmann@28228
    68
let
haftmann@31139
    69
  fun mk_term_of_eq thy ty vs tyco (c, tys) =
haftmann@28228
    70
    let
haftmann@28228
    71
      val t = list_comb (Const (c, tys ---> ty),
haftmann@28228
    72
        map Free (Name.names Name.context "a" tys));
haftmann@31139
    73
      val (arg, rhs) = pairself (Thm.cterm_of thy o map_types Logic.unvarifyT o Logic.varify)
haftmann@31139
    74
        (t, (map_aterms (fn t as Free (v, ty) => HOLogic.mk_term_of ty t | t => t) o HOLogic.reflect_term) t)
haftmann@31139
    75
      val cty = Thm.ctyp_of thy ty;
haftmann@31139
    76
    in
haftmann@31139
    77
      @{thm term_of_anything}
haftmann@31139
    78
      |> Drule.instantiate' [SOME cty] [SOME arg, SOME rhs]
haftmann@31139
    79
      |> Thm.varifyT
haftmann@28228
    80
    end;
haftmann@31139
    81
  fun add_term_of_code tyco raw_vs raw_cs thy =
haftmann@28228
    82
    let
haftmann@31139
    83
      val vs = map (fn (v, _) => (v, @{sort typerep})) raw_vs;
haftmann@31139
    84
      val ty = Type (tyco, map TFree vs);
haftmann@31139
    85
      val cs = (map o apsnd o map o map_atyps)
haftmann@31139
    86
        (fn TFree (v, _) => TFree (v, (the o AList.lookup (op =) vs) v)) raw_cs;
haftmann@31139
    87
      val const = AxClass.param_of_inst thy (@{const_name term_of}, tyco);
haftmann@31139
    88
      val eqs = map (mk_term_of_eq thy ty vs tyco) cs;
haftmann@31139
    89
   in
haftmann@31139
    90
      thy
haftmann@31139
    91
      |> Code.del_eqns const
haftmann@31139
    92
      |> fold Code.add_eqn eqs
haftmann@31139
    93
    end;
haftmann@31139
    94
  fun ensure_term_of_code (tyco, (raw_vs, cs)) thy =
haftmann@31139
    95
    let
haftmann@31139
    96
      val has_inst = can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of};
haftmann@28228
    97
    in
haftmann@28228
    98
      thy
haftmann@31139
    99
      |> has_inst ? add_term_of_code tyco raw_vs cs
haftmann@28228
   100
    end;
haftmann@28228
   101
in
haftmann@31139
   102
  Code.type_interpretation ensure_term_of_code
haftmann@28228
   103
end
haftmann@28228
   104
*}
haftmann@28228
   105
haftmann@28228
   106
haftmann@28228
   107
subsubsection {* Code generator setup *}
haftmann@28228
   108
haftmann@28562
   109
lemmas [code del] = term.recs term.cases term.size
haftmann@28562
   110
lemma [code, code del]: "eq_class.eq (t1\<Colon>term) t2 \<longleftrightarrow> eq_class.eq t1 t2" ..
haftmann@28228
   111
haftmann@28562
   112
lemma [code, code del]: "(term_of \<Colon> typerep \<Rightarrow> term) = term_of" ..
haftmann@28562
   113
lemma [code, code del]: "(term_of \<Colon> term \<Rightarrow> term) = term_of" ..
haftmann@28562
   114
lemma [code, code del]: "(term_of \<Colon> message_string \<Rightarrow> term) = term_of" ..
haftmann@30427
   115
lemma [code, code del]:
haftmann@30427
   116
  "(Code_Eval.term_of \<Colon> 'a::{type, term_of} Predicate.pred \<Rightarrow> Code_Eval.term) = Code_Eval.term_of" ..
haftmann@30427
   117
lemma [code, code del]:
haftmann@30427
   118
  "(Code_Eval.term_of \<Colon> 'a::{type, term_of} Predicate.seq \<Rightarrow> Code_Eval.term) = Code_Eval.term_of" ..
haftmann@28228
   119
haftmann@28562
   120
lemma term_of_char [unfolded typerep_fun_def typerep_char_def typerep_nibble_def, code]: "Code_Eval.term_of c =
haftmann@28243
   121
    (let (n, m) = nibble_pair_of_char c
haftmann@28335
   122
  in Code_Eval.App (Code_Eval.App (Code_Eval.Const (STR ''Pair'') (TYPEREP(nibble \<Rightarrow> nibble \<Rightarrow> char)))
haftmann@28243
   123
    (Code_Eval.term_of n)) (Code_Eval.term_of m))"
haftmann@28243
   124
  by (subst term_of_anything) rule 
haftmann@28243
   125
haftmann@28228
   126
code_type "term"
haftmann@31031
   127
  (Eval "Term.term")
haftmann@28228
   128
haftmann@28228
   129
code_const Const and App
haftmann@31031
   130
  (Eval "Term.Const/ (_, _)" and "Term.$/ (_, _)")
haftmann@28228
   131
haftmann@28228
   132
code_const "term_of \<Colon> message_string \<Rightarrow> term"
haftmann@31048
   133
  (Eval "HOLogic.mk'_message'_string")
haftmann@31048
   134
haftmann@31048
   135
code_reserved Eval HOLogic
haftmann@28228
   136
haftmann@28228
   137
haftmann@28228
   138
subsection {* Evaluation setup *}
haftmann@28228
   139
haftmann@28228
   140
ML {*
haftmann@28228
   141
signature EVAL =
haftmann@28228
   142
sig
haftmann@28228
   143
  val eval_ref: (unit -> term) option ref
haftmann@28228
   144
  val eval_term: theory -> term -> term
haftmann@28228
   145
end;
haftmann@28228
   146
haftmann@28228
   147
structure Eval : EVAL =
haftmann@28228
   148
struct
haftmann@28228
   149
haftmann@28228
   150
val eval_ref = ref (NONE : (unit -> term) option);
haftmann@28228
   151
haftmann@28228
   152
fun eval_term thy t =
haftmann@31139
   153
  Code_ML.eval NONE ("Eval.eval_ref", eval_ref) I thy (HOLogic.mk_term_of (fastype_of t) t) [];
haftmann@28228
   154
haftmann@28228
   155
end;
haftmann@28228
   156
*}
haftmann@28228
   157
haftmann@28228
   158
setup {*
haftmann@28228
   159
  Value.add_evaluator ("code", Eval.eval_term o ProofContext.theory_of)
haftmann@28228
   160
*}
haftmann@28228
   161
haftmann@28243
   162
haftmann@28243
   163
subsubsection {* Syntax *}
haftmann@28243
   164
haftmann@28243
   165
print_translation {*
haftmann@28243
   166
let
haftmann@28243
   167
  val term = Const ("<TERM>", dummyT);
haftmann@28243
   168
  fun tr1' [_, _] = term;
haftmann@28243
   169
  fun tr2' [] = term;
haftmann@28243
   170
in
haftmann@28243
   171
  [(@{const_syntax Const}, tr1'),
haftmann@28243
   172
    (@{const_syntax App}, tr1'),
haftmann@28243
   173
    (@{const_syntax dummy_term}, tr2')]
haftmann@28228
   174
end
haftmann@28243
   175
*}
haftmann@28243
   176
haftmann@28243
   177
hide const dummy_term
haftmann@28243
   178
hide (open) const Const App
haftmann@28243
   179
hide (open) const term_of
haftmann@28243
   180
haftmann@28243
   181
end