src/HOL/Code_Evaluation.thy
author bulwahn
Fri, 10 Sep 2010 10:59:09 +0200
changeset 39274 b17ffa965223
parent 38857 97775f3e8722
child 39387 6608c4838ff9
permissions -rw-r--r--
fiddling with the correct setup for String.literal
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32657
5f13912245ff Code_Eval(uation)
haftmann
parents: 32371
diff changeset
     1
(*  Title:      HOL/Code_Evaluation.thy
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
     3
*)
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
     4
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
     5
header {* Term evaluation using the generic code generator *}
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
     6
32657
5f13912245ff Code_Eval(uation)
haftmann
parents: 32371
diff changeset
     7
theory Code_Evaluation
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
     8
imports Plain Typerep Code_Numeral
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
     9
begin
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    10
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    11
subsection {* Term representation *}
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    12
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    13
subsubsection {* Terms and class @{text term_of} *}
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    14
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    15
datatype "term" = dummy_term
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    16
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    17
definition Const :: "String.literal \<Rightarrow> typerep \<Rightarrow> term" where
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    18
  "Const _ _ = dummy_term"
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    19
28661
a287d0e8aa9d slightly tuned
haftmann
parents: 28562
diff changeset
    20
definition App :: "term \<Rightarrow> term \<Rightarrow> term" where
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    21
  "App _ _ = dummy_term"
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    22
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    23
code_datatype Const App
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    24
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28313
diff changeset
    25
class term_of = typerep +
31031
cbec39ebf8f2 class typerep inherits from type
haftmann
parents: 30970
diff changeset
    26
  fixes term_of :: "'a \<Rightarrow> term"
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    27
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    28
lemma term_of_anything: "term_of x \<equiv> t"
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    29
  by (rule eq_reflection) (cases "term_of x", cases t, simp)
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    30
31191
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
    31
definition valapp :: "('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)
31178
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
    32
  \<Rightarrow> 'a \<times> (unit \<Rightarrow> term) \<Rightarrow> 'b \<times> (unit \<Rightarrow> term)" where
31191
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
    33
  "valapp f x = (fst f (fst x), \<lambda>u. App (snd f ()) (snd x ()))"
31178
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
    34
32069
6d28bbd33e2c prefer code_inline over code_unfold; use code_unfold_post where appropriate
haftmann
parents: 31998
diff changeset
    35
lemma valapp_code [code, code_unfold]:
31191
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
    36
  "valapp (f, tf) (x, tx) = (f x, \<lambda>u. App (tf ()) (tx ()))"
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
    37
  by (simp only: valapp_def fst_conv snd_conv)
31178
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
    38
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    39
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    40
subsubsection {* @{text term_of} instances *}
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    41
32344
55ca0df19af5 more appropriate printing of function terms
haftmann
parents: 32069
diff changeset
    42
instantiation "fun" :: (typerep, typerep) term_of
55ca0df19af5 more appropriate printing of function terms
haftmann
parents: 32069
diff changeset
    43
begin
55ca0df19af5 more appropriate printing of function terms
haftmann
parents: 32069
diff changeset
    44
55ca0df19af5 more appropriate printing of function terms
haftmann
parents: 32069
diff changeset
    45
definition
55ca0df19af5 more appropriate printing of function terms
haftmann
parents: 32069
diff changeset
    46
  "term_of (f \<Colon> 'a \<Rightarrow> 'b) = Const (STR ''dummy_pattern'') (Typerep.Typerep (STR ''fun'')
55ca0df19af5 more appropriate printing of function terms
haftmann
parents: 32069
diff changeset
    47
     [Typerep.typerep TYPE('a), Typerep.typerep TYPE('b)])"
55ca0df19af5 more appropriate printing of function terms
haftmann
parents: 32069
diff changeset
    48
55ca0df19af5 more appropriate printing of function terms
haftmann
parents: 32069
diff changeset
    49
instance ..
55ca0df19af5 more appropriate printing of function terms
haftmann
parents: 32069
diff changeset
    50
55ca0df19af5 more appropriate printing of function terms
haftmann
parents: 32069
diff changeset
    51
end
55ca0df19af5 more appropriate printing of function terms
haftmann
parents: 32069
diff changeset
    52
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    53
setup {*
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    54
let
31139
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    55
  fun add_term_of tyco raw_vs thy =
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    56
    let
31139
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    57
      val vs = map (fn (v, _) => (v, @{sort typerep})) raw_vs;
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    58
      val ty = Type (tyco, map TFree vs);
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    59
      val lhs = Const (@{const_name term_of}, ty --> @{typ term})
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    60
        $ Free ("x", ty);
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    61
      val rhs = @{term "undefined \<Colon> term"};
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    62
      val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
28243
84d90ec67059 moved term_of syntax to separate theory
haftmann
parents: 28228
diff changeset
    63
      fun triv_name_of t = (fst o dest_Free o fst o strip_comb o fst
84d90ec67059 moved term_of syntax to separate theory
haftmann
parents: 28228
diff changeset
    64
        o HOLogic.dest_eq o HOLogic.dest_Trueprop) t ^ "_triv";
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    65
    in
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    66
      thy
38348
cf7b2121ad9d moved instantiation target formally to class_target.ML
haftmann
parents: 36176
diff changeset
    67
      |> Class.instantiation ([tyco], vs, @{sort term_of})
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    68
      |> `(fn lthy => Syntax.check_term lthy eq)
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28952
diff changeset
    69
      |-> (fn eq => Specification.definition (NONE, ((Binding.name (triv_name_of eq), []), eq)))
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    70
      |> snd
31139
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    71
      |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    72
    end;
31139
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    73
  fun ensure_term_of (tyco, (raw_vs, _)) thy =
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    74
    let
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    75
      val need_inst = not (can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of})
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    76
        andalso can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort typerep};
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    77
    in if need_inst then add_term_of tyco raw_vs thy else thy end;
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    78
in
35299
4f4d5bf4ea08 proper distinction of code datatypes and abstypes
haftmann
parents: 34028
diff changeset
    79
  Code.datatype_interpretation ensure_term_of
4f4d5bf4ea08 proper distinction of code datatypes and abstypes
haftmann
parents: 34028
diff changeset
    80
  #> Code.abstype_interpretation ensure_term_of
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    81
end
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    82
*}
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    83
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    84
setup {*
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    85
let
31139
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    86
  fun mk_term_of_eq thy ty vs tyco (c, tys) =
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    87
    let
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    88
      val t = list_comb (Const (c, tys ---> ty),
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    89
        map Free (Name.names Name.context "a" tys));
35845
e5980f0ad025 renamed varify/unvarify operations to varify_global/unvarify_global to emphasize that these only work in a global situation;
wenzelm
parents: 35366
diff changeset
    90
      val (arg, rhs) =
e5980f0ad025 renamed varify/unvarify operations to varify_global/unvarify_global to emphasize that these only work in a global situation;
wenzelm
parents: 35366
diff changeset
    91
        pairself (Thm.cterm_of thy o map_types Logic.unvarifyT_global o Logic.varify_global)
e5980f0ad025 renamed varify/unvarify operations to varify_global/unvarify_global to emphasize that these only work in a global situation;
wenzelm
parents: 35366
diff changeset
    92
          (t, (map_aterms (fn t as Free (v, ty) => HOLogic.mk_term_of ty t | t => t) o HOLogic.reflect_term) t)
31139
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    93
      val cty = Thm.ctyp_of thy ty;
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    94
    in
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    95
      @{thm term_of_anything}
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    96
      |> Drule.instantiate' [SOME cty] [SOME arg, SOME rhs]
35845
e5980f0ad025 renamed varify/unvarify operations to varify_global/unvarify_global to emphasize that these only work in a global situation;
wenzelm
parents: 35366
diff changeset
    97
      |> Thm.varifyT_global
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    98
    end;
31139
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    99
  fun add_term_of_code tyco raw_vs raw_cs thy =
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   100
    let
31746
75fe3304015c code equation observes default sort constraints for types
haftmann
parents: 31594
diff changeset
   101
      val algebra = Sign.classes_of thy;
75fe3304015c code equation observes default sort constraints for types
haftmann
parents: 31594
diff changeset
   102
      val vs = map (fn (v, sort) =>
75fe3304015c code equation observes default sort constraints for types
haftmann
parents: 31594
diff changeset
   103
        (v, curry (Sorts.inter_sort algebra) @{sort typerep} sort)) raw_vs;
31139
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
   104
      val ty = Type (tyco, map TFree vs);
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
   105
      val cs = (map o apsnd o map o map_atyps)
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
   106
        (fn TFree (v, _) => TFree (v, (the o AList.lookup (op =) vs) v)) raw_cs;
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
   107
      val const = AxClass.param_of_inst thy (@{const_name term_of}, tyco);
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
   108
      val eqs = map (mk_term_of_eq thy ty vs tyco) cs;
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
   109
   in
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
   110
      thy
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
   111
      |> Code.del_eqns const
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
   112
      |> fold Code.add_eqn eqs
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
   113
    end;
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
   114
  fun ensure_term_of_code (tyco, (raw_vs, cs)) thy =
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
   115
    let
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
   116
      val has_inst = can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of};
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   117
    in if has_inst then add_term_of_code tyco raw_vs cs thy else thy end;
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   118
in
35299
4f4d5bf4ea08 proper distinction of code datatypes and abstypes
haftmann
parents: 34028
diff changeset
   119
  Code.datatype_interpretation ensure_term_of_code
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   120
end
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   121
*}
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   122
35366
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   123
setup {*
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   124
let
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   125
  fun mk_term_of_eq thy ty vs tyco abs ty_rep proj =
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   126
    let
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   127
      val arg = Var (("x", 0), ty);
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   128
      val rhs = Abs ("y", @{typ term}, HOLogic.reflect_term (Const (abs, ty_rep --> ty) $ Bound 0)) $
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   129
        (HOLogic.mk_term_of ty_rep (Const (proj, ty --> ty_rep) $ arg))
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   130
        |> Thm.cterm_of thy;
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   131
      val cty = Thm.ctyp_of thy ty;
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   132
    in
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   133
      @{thm term_of_anything}
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   134
      |> Drule.instantiate' [SOME cty] [SOME (Thm.cterm_of thy arg), SOME rhs]
35845
e5980f0ad025 renamed varify/unvarify operations to varify_global/unvarify_global to emphasize that these only work in a global situation;
wenzelm
parents: 35366
diff changeset
   135
      |> Thm.varifyT_global
35366
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   136
    end;
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   137
  fun add_term_of_code tyco raw_vs abs raw_ty_rep proj thy =
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   138
    let
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   139
      val algebra = Sign.classes_of thy;
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   140
      val vs = map (fn (v, sort) =>
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   141
        (v, curry (Sorts.inter_sort algebra) @{sort typerep} sort)) raw_vs;
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   142
      val ty = Type (tyco, map TFree vs);
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   143
      val ty_rep = map_atyps
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   144
        (fn TFree (v, _) => TFree (v, (the o AList.lookup (op =) vs) v)) raw_ty_rep;
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   145
      val const = AxClass.param_of_inst thy (@{const_name term_of}, tyco);
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   146
      val eq = mk_term_of_eq thy ty vs tyco abs ty_rep proj;
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   147
   in
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   148
      thy
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   149
      |> Code.del_eqns const
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   150
      |> Code.add_eqn eq
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   151
    end;
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   152
  fun ensure_term_of_code (tyco, (raw_vs, ((abs, ty), (proj, _)))) thy =
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   153
    let
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   154
      val has_inst = can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of};
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   155
    in if has_inst then add_term_of_code tyco raw_vs abs ty proj thy else thy end;
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   156
in
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   157
  Code.abstype_interpretation ensure_term_of_code
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   158
end
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   159
*}
6d474096698c evaluation for abstypes
haftmann
parents: 35299
diff changeset
   160
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   161
39274
b17ffa965223 fiddling with the correct setup for String.literal
bulwahn
parents: 38857
diff changeset
   162
instantiation String.literal :: term_of
b17ffa965223 fiddling with the correct setup for String.literal
bulwahn
parents: 38857
diff changeset
   163
begin
b17ffa965223 fiddling with the correct setup for String.literal
bulwahn
parents: 38857
diff changeset
   164
b17ffa965223 fiddling with the correct setup for String.literal
bulwahn
parents: 38857
diff changeset
   165
definition
b17ffa965223 fiddling with the correct setup for String.literal
bulwahn
parents: 38857
diff changeset
   166
  "term_of s = App (Const (STR ''STR'')
b17ffa965223 fiddling with the correct setup for String.literal
bulwahn
parents: 38857
diff changeset
   167
    (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''list'') [Typerep.Typerep (STR ''char'') []],
b17ffa965223 fiddling with the correct setup for String.literal
bulwahn
parents: 38857
diff changeset
   168
      Typerep.Typerep (STR ''String.literal'') []])) (term_of (String.explode s))"
b17ffa965223 fiddling with the correct setup for String.literal
bulwahn
parents: 38857
diff changeset
   169
b17ffa965223 fiddling with the correct setup for String.literal
bulwahn
parents: 38857
diff changeset
   170
instance ..
b17ffa965223 fiddling with the correct setup for String.literal
bulwahn
parents: 38857
diff changeset
   171
b17ffa965223 fiddling with the correct setup for String.literal
bulwahn
parents: 38857
diff changeset
   172
end
b17ffa965223 fiddling with the correct setup for String.literal
bulwahn
parents: 38857
diff changeset
   173
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   174
subsubsection {* Code generator setup *}
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   175
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28394
diff changeset
   176
lemmas [code del] = term.recs term.cases term.size
38857
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 38348
diff changeset
   177
lemma [code, code del]: "HOL.equal (t1\<Colon>term) t2 \<longleftrightarrow> HOL.equal t1 t2" ..
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   178
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28394
diff changeset
   179
lemma [code, code del]: "(term_of \<Colon> typerep \<Rightarrow> term) = term_of" ..
4e74209f113e `code func` now just `code`
haftmann
parents: 28394
diff changeset
   180
lemma [code, code del]: "(term_of \<Colon> term \<Rightarrow> term) = term_of" ..
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   181
lemma [code, code del]: "(term_of \<Colon> String.literal \<Rightarrow> term) = term_of" ..
30427
dfd31c1db060 delete code equations for types pred and seq
haftmann
parents: 29575
diff changeset
   182
lemma [code, code del]:
32657
5f13912245ff Code_Eval(uation)
haftmann
parents: 32371
diff changeset
   183
  "(Code_Evaluation.term_of \<Colon> 'a::{type, term_of} Predicate.pred \<Rightarrow> Code_Evaluation.term) = Code_Evaluation.term_of" ..
30427
dfd31c1db060 delete code equations for types pred and seq
haftmann
parents: 29575
diff changeset
   184
lemma [code, code del]:
32657
5f13912245ff Code_Eval(uation)
haftmann
parents: 32371
diff changeset
   185
  "(Code_Evaluation.term_of \<Colon> 'a::{type, term_of} Predicate.seq \<Rightarrow> Code_Evaluation.term) = Code_Evaluation.term_of" ..
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   186
32657
5f13912245ff Code_Eval(uation)
haftmann
parents: 32371
diff changeset
   187
lemma term_of_char [unfolded typerep_fun_def typerep_char_def typerep_nibble_def, code]: "Code_Evaluation.term_of c =
28243
84d90ec67059 moved term_of syntax to separate theory
haftmann
parents: 28228
diff changeset
   188
    (let (n, m) = nibble_pair_of_char c
32657
5f13912245ff Code_Eval(uation)
haftmann
parents: 32371
diff changeset
   189
  in Code_Evaluation.App (Code_Evaluation.App (Code_Evaluation.Const (STR ''String.char.Char'') (TYPEREP(nibble \<Rightarrow> nibble \<Rightarrow> char)))
5f13912245ff Code_Eval(uation)
haftmann
parents: 32371
diff changeset
   190
    (Code_Evaluation.term_of n)) (Code_Evaluation.term_of m))"
28243
84d90ec67059 moved term_of syntax to separate theory
haftmann
parents: 28228
diff changeset
   191
  by (subst term_of_anything) rule 
84d90ec67059 moved term_of syntax to separate theory
haftmann
parents: 28228
diff changeset
   192
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   193
code_type "term"
31031
cbec39ebf8f2 class typerep inherits from type
haftmann
parents: 30970
diff changeset
   194
  (Eval "Term.term")
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   195
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   196
code_const Const and App
31594
a94aa5f045fb less brackets
haftmann
parents: 31205
diff changeset
   197
  (Eval "Term.Const/ ((_), (_))" and "Term.$/ ((_), (_))")
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   198
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   199
code_const "term_of \<Colon> String.literal \<Rightarrow> term"
33632
6ea8a4cce9e7 repaired broken code_const for term_of [String.literal]
haftmann
parents: 33553
diff changeset
   200
  (Eval "HOLogic.mk'_literal")
31048
ac146fc38b51 refined HOL string theories and corresponding ML fragments
haftmann
parents: 31031
diff changeset
   201
ac146fc38b51 refined HOL string theories and corresponding ML fragments
haftmann
parents: 31031
diff changeset
   202
code_reserved Eval HOLogic
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   203
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   204
31178
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   205
subsubsection {* Syntax *}
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   206
31191
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   207
definition termify :: "'a \<Rightarrow> term" where
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   208
  [code del]: "termify x = dummy_term"
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   209
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   210
abbreviation valtermify :: "'a \<Rightarrow> 'a \<times> (unit \<Rightarrow> term)" where
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   211
  "valtermify x \<equiv> (x, \<lambda>u. termify x)"
31178
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   212
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   213
setup {*
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   214
let
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   215
  fun map_default f xs =
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   216
    let val ys = map f xs
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   217
    in if exists is_some ys
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   218
      then SOME (map2 the_default xs ys)
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   219
      else NONE
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   220
    end;
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   221
  fun subst_termify_app (Const (@{const_name termify}, T), [t]) =
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   222
        if not (Term.has_abs t)
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   223
        then if fold_aterms (fn Const _ => I | _ => K false) t true
31191
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   224
          then SOME (HOLogic.reflect_term t)
31178
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   225
          else error "Cannot termify expression containing variables"
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   226
        else error "Cannot termify expression containing abstraction"
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   227
    | subst_termify_app (t, ts) = case map_default subst_termify ts
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   228
       of SOME ts' => SOME (list_comb (t, ts'))
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   229
        | NONE => NONE
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   230
  and subst_termify (Abs (v, T, t)) = (case subst_termify t
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   231
       of SOME t' => SOME (Abs (v, T, t'))
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   232
        | NONE => NONE)
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   233
    | subst_termify t = subst_termify_app (strip_comb t) 
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   234
  fun check_termify ts ctxt = map_default subst_termify ts
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   235
    |> Option.map (rpair ctxt)
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   236
in
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   237
  Context.theory_map (Syntax.add_term_check 0 "termify" check_termify)
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   238
end;
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   239
*}
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   240
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   241
locale term_syntax
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   242
begin
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   243
31191
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   244
notation App (infixl "<\<cdot>>" 70)
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   245
  and valapp (infixl "{\<cdot>}" 70)
31178
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   246
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   247
end
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   248
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   249
interpretation term_syntax .
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   250
31191
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   251
no_notation App (infixl "<\<cdot>>" 70)
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   252
  and valapp (infixl "{\<cdot>}" 70)
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   253
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   254
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   255
subsection {* Numeric types *}
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   256
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   257
definition term_of_num :: "'a\<Colon>{semiring_div} \<Rightarrow> 'a\<Colon>{semiring_div} \<Rightarrow> term" where
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   258
  "term_of_num two = (\<lambda>_. dummy_term)"
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   259
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   260
lemma (in term_syntax) term_of_num_code [code]:
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   261
  "term_of_num two k = (if k = 0 then termify Int.Pls
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   262
    else (if k mod two = 0
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   263
      then termify Int.Bit0 <\<cdot>> term_of_num two (k div two)
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   264
      else termify Int.Bit1 <\<cdot>> term_of_num two (k div two)))"
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   265
  by (auto simp add: term_of_anything Const_def App_def term_of_num_def Let_def)
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   266
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   267
lemma (in term_syntax) term_of_nat_code [code]:
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   268
  "term_of (n::nat) = termify (number_of :: int \<Rightarrow> nat) <\<cdot>> term_of_num (2::nat) n"
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   269
  by (simp only: term_of_anything)
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   270
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   271
lemma (in term_syntax) term_of_int_code [code]:
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   272
  "term_of (k::int) = (if k = 0 then termify (0 :: int)
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   273
    else if k > 0 then termify (number_of :: int \<Rightarrow> int) <\<cdot>> term_of_num (2::int) k
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   274
      else termify (uminus :: int \<Rightarrow> int) <\<cdot>> (termify (number_of :: int \<Rightarrow> int) <\<cdot>> term_of_num (2::int) (- k)))"
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   275
  by (simp only: term_of_anything)
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   276
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   277
lemma (in term_syntax) term_of_code_numeral_code [code]:
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   278
  "term_of (k::code_numeral) = termify (number_of :: int \<Rightarrow> code_numeral) <\<cdot>> term_of_num (2::code_numeral) k"
31203
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31191
diff changeset
   279
  by (simp only: term_of_anything)
31191
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   280
7733125bac3c tuned term input syntax
haftmann
parents: 31178
diff changeset
   281
subsection {* Obfuscate *}
31178
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   282
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   283
print_translation {*
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   284
let
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   285
  val term = Const ("<TERM>", dummyT);
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   286
  fun tr1' [_, _] = term;
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   287
  fun tr2' [] = term;
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   288
in
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   289
  [(@{const_syntax Const}, tr1'),
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   290
    (@{const_syntax App}, tr1'),
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   291
    (@{const_syntax dummy_term}, tr2')]
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   292
end
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   293
*}
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   294
36176
3fe7e97ccca8 replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
wenzelm
parents: 35845
diff changeset
   295
hide_const dummy_term App valapp
3fe7e97ccca8 replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
wenzelm
parents: 35845
diff changeset
   296
hide_const (open) Const termify valtermify term_of term_of_num
31178
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   297
33473
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 32740
diff changeset
   298
subsection {* Tracing of generated and evaluated code *}
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 32740
diff changeset
   299
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 32740
diff changeset
   300
definition tracing :: "String.literal => 'a => 'a"
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 32740
diff changeset
   301
where
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 32740
diff changeset
   302
  [code del]: "tracing s x = x"
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 32740
diff changeset
   303
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 32740
diff changeset
   304
ML {*
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 32740
diff changeset
   305
structure Code_Evaluation =
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 32740
diff changeset
   306
struct
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 32740
diff changeset
   307
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 32740
diff changeset
   308
fun tracing s x = (Output.tracing s; x)
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 32740
diff changeset
   309
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 32740
diff changeset
   310
end
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 32740
diff changeset
   311
*}
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 32740
diff changeset
   312
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 32740
diff changeset
   313
code_const "tracing :: String.literal => 'a => 'a"
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 32740
diff changeset
   314
  (Eval "Code'_Evaluation.tracing")
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 32740
diff changeset
   315
36176
3fe7e97ccca8 replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
wenzelm
parents: 35845
diff changeset
   316
hide_const (open) tracing
33473
3b275a0bf18c adding tracing function for evaluated code; annotated compilation in the predicate compiler
bulwahn
parents: 32740
diff changeset
   317
code_reserved Eval Code_Evaluation
31178
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   318
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   319
subsection {* Evaluation setup *}
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   320
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   321
ML {*
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   322
signature EVAL =
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   323
sig
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32657
diff changeset
   324
  val eval_ref: (unit -> term) option Unsynchronized.ref
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   325
  val eval_term: theory -> term -> term
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   326
end;
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   327
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   328
structure Eval : EVAL =
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   329
struct
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   330
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32657
diff changeset
   331
val eval_ref = Unsynchronized.ref (NONE : (unit -> term) option);
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   332
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   333
fun eval_term thy t =
34028
1e6206763036 split off evaluation mechanisms in separte module Code_Eval
haftmann
parents: 33632
diff changeset
   334
  Code_Eval.eval NONE ("Eval.eval_ref", eval_ref) I thy (HOLogic.mk_term_of (fastype_of t) t) [];
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   335
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   336
end;
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   337
*}
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   338
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   339
setup {*
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   340
  Value.add_evaluator ("code", Eval.eval_term o ProofContext.theory_of)
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   341
*}
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   342
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   343
end