src/HOL/Code_Eval.thy
author haftmann
Fri, 15 May 2009 16:39:15 +0200
changeset 31178 27afaaa6547a
parent 31144 bdc1504ad456
child 31191 7733125bac3c
permissions -rw-r--r--
syntax support for term expressions
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/Code_Eval.thy
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
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
     7
theory Code_Eval
28952
15a4b2cf8c34 made repository layout more coherent with logical distribution structure; stripped some $Id$s
haftmann
parents: 28661
diff changeset
     8
imports Plain Typerep
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
28661
a287d0e8aa9d slightly tuned
haftmann
parents: 28562
diff changeset
    17
definition Const :: "message_string \<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
31178
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
    31
definition app :: "('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)
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
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
    33
  "app f x = (fst f (fst x), \<lambda>u. Code_Eval.App (snd f ()) (snd x ()))"
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
    34
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
    35
lemma app_code [code, code inline]:
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
    36
  "app (f, tf) (x, tx) = (f x, \<lambda>u. Code_Eval.App (tf ()) (tx ()))"
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
    37
  by (simp only: app_def fst_conv snd_conv)
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
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    42
setup {*
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    43
let
31139
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    44
  fun add_term_of tyco raw_vs thy =
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    45
    let
31139
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    46
      val vs = map (fn (v, _) => (v, @{sort typerep})) raw_vs;
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    47
      val ty = Type (tyco, map TFree vs);
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    48
      val lhs = Const (@{const_name term_of}, ty --> @{typ term})
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    49
        $ Free ("x", ty);
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    50
      val rhs = @{term "undefined \<Colon> term"};
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    51
      val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
28243
84d90ec67059 moved term_of syntax to separate theory
haftmann
parents: 28228
diff changeset
    52
      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
    53
        o HOLogic.dest_eq o HOLogic.dest_Trueprop) t ^ "_triv";
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    54
    in
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    55
      thy
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    56
      |> TheoryTarget.instantiation ([tyco], vs, @{sort term_of})
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    57
      |> `(fn lthy => Syntax.check_term lthy eq)
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28952
diff changeset
    58
      |-> (fn eq => Specification.definition (NONE, ((Binding.name (triv_name_of eq), []), eq)))
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    59
      |> snd
31139
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    60
      |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    61
    end;
31139
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    62
  fun ensure_term_of (tyco, (raw_vs, _)) thy =
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    63
    let
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    64
      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
    65
        andalso can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort typerep};
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    66
    in
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    67
      thy
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    68
      |> need_inst ? add_term_of tyco raw_vs
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    69
    end;
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    70
in
31139
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    71
  Code.type_interpretation ensure_term_of
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    72
end
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    73
*}
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    74
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    75
setup {*
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    76
let
31139
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    77
  fun mk_term_of_eq thy ty vs tyco (c, tys) =
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    78
    let
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    79
      val t = list_comb (Const (c, tys ---> ty),
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    80
        map Free (Name.names Name.context "a" tys));
31139
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    81
      val (arg, rhs) = pairself (Thm.cterm_of thy o map_types Logic.unvarifyT o Logic.varify)
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    82
        (t, (map_aterms (fn t as Free (v, ty) => HOLogic.mk_term_of ty t | t => t) o HOLogic.reflect_term) t)
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    83
      val cty = Thm.ctyp_of thy ty;
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    84
    in
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    85
      @{thm term_of_anything}
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    86
      |> Drule.instantiate' [SOME cty] [SOME arg, SOME rhs]
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    87
      |> Thm.varifyT
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    88
    end;
31139
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    89
  fun add_term_of_code tyco raw_vs raw_cs thy =
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
    90
    let
31139
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    91
      val vs = map (fn (v, _) => (v, @{sort typerep})) raw_vs;
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    92
      val ty = Type (tyco, map TFree vs);
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    93
      val cs = (map o apsnd o map o map_atyps)
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    94
        (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
    95
      val const = AxClass.param_of_inst thy (@{const_name term_of}, tyco);
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    96
      val eqs = map (mk_term_of_eq thy ty vs tyco) cs;
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    97
   in
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    98
      thy
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
    99
      |> Code.del_eqns const
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
   100
      |> fold Code.add_eqn eqs
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
   101
    end;
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
   102
  fun ensure_term_of_code (tyco, (raw_vs, cs)) thy =
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
   103
    let
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
   104
      val has_inst = can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of};
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   105
    in
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   106
      thy
31139
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
   107
      |> has_inst ? add_term_of_code tyco raw_vs cs
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   108
    end;
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   109
in
31139
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
   110
  Code.type_interpretation ensure_term_of_code
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   111
end
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   112
*}
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   113
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   114
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   115
subsubsection {* Code generator setup *}
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   116
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28394
diff changeset
   117
lemmas [code del] = term.recs term.cases term.size
4e74209f113e `code func` now just `code`
haftmann
parents: 28394
diff changeset
   118
lemma [code, code del]: "eq_class.eq (t1\<Colon>term) t2 \<longleftrightarrow> eq_class.eq t1 t2" ..
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   119
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28394
diff changeset
   120
lemma [code, code del]: "(term_of \<Colon> typerep \<Rightarrow> term) = term_of" ..
4e74209f113e `code func` now just `code`
haftmann
parents: 28394
diff changeset
   121
lemma [code, code del]: "(term_of \<Colon> term \<Rightarrow> term) = term_of" ..
4e74209f113e `code func` now just `code`
haftmann
parents: 28394
diff changeset
   122
lemma [code, code del]: "(term_of \<Colon> message_string \<Rightarrow> term) = term_of" ..
30427
dfd31c1db060 delete code equations for types pred and seq
haftmann
parents: 29575
diff changeset
   123
lemma [code, code del]:
dfd31c1db060 delete code equations for types pred and seq
haftmann
parents: 29575
diff changeset
   124
  "(Code_Eval.term_of \<Colon> 'a::{type, term_of} Predicate.pred \<Rightarrow> Code_Eval.term) = Code_Eval.term_of" ..
dfd31c1db060 delete code equations for types pred and seq
haftmann
parents: 29575
diff changeset
   125
lemma [code, code del]:
dfd31c1db060 delete code equations for types pred and seq
haftmann
parents: 29575
diff changeset
   126
  "(Code_Eval.term_of \<Colon> 'a::{type, term_of} Predicate.seq \<Rightarrow> Code_Eval.term) = Code_Eval.term_of" ..
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   127
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28394
diff changeset
   128
lemma term_of_char [unfolded typerep_fun_def typerep_char_def typerep_nibble_def, code]: "Code_Eval.term_of c =
28243
84d90ec67059 moved term_of syntax to separate theory
haftmann
parents: 28228
diff changeset
   129
    (let (n, m) = nibble_pair_of_char c
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28313
diff changeset
   130
  in Code_Eval.App (Code_Eval.App (Code_Eval.Const (STR ''Pair'') (TYPEREP(nibble \<Rightarrow> nibble \<Rightarrow> char)))
28243
84d90ec67059 moved term_of syntax to separate theory
haftmann
parents: 28228
diff changeset
   131
    (Code_Eval.term_of n)) (Code_Eval.term_of m))"
84d90ec67059 moved term_of syntax to separate theory
haftmann
parents: 28228
diff changeset
   132
  by (subst term_of_anything) rule 
84d90ec67059 moved term_of syntax to separate theory
haftmann
parents: 28228
diff changeset
   133
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   134
code_type "term"
31031
cbec39ebf8f2 class typerep inherits from type
haftmann
parents: 30970
diff changeset
   135
  (Eval "Term.term")
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   136
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   137
code_const Const and App
31031
cbec39ebf8f2 class typerep inherits from type
haftmann
parents: 30970
diff changeset
   138
  (Eval "Term.Const/ (_, _)" and "Term.$/ (_, _)")
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   139
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   140
code_const "term_of \<Colon> message_string \<Rightarrow> term"
31048
ac146fc38b51 refined HOL string theories and corresponding ML fragments
haftmann
parents: 31031
diff changeset
   141
  (Eval "HOLogic.mk'_message'_string")
ac146fc38b51 refined HOL string theories and corresponding ML fragments
haftmann
parents: 31031
diff changeset
   142
ac146fc38b51 refined HOL string theories and corresponding ML fragments
haftmann
parents: 31031
diff changeset
   143
code_reserved Eval HOLogic
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   144
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   145
31178
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   146
subsubsection {* Syntax *}
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   147
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   148
definition termify :: "'a \<Rightarrow> 'a \<times> (unit \<Rightarrow> term)" where
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   149
  [code del]: "termify x = (x, \<lambda>u. dummy_term)"
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   150
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   151
setup {*
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   152
let
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   153
  fun map_default f xs =
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   154
    let val ys = map f xs
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   155
    in if exists is_some ys
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   156
      then SOME (map2 the_default xs ys)
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   157
      else NONE
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   158
    end;
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   159
  fun subst_termify_app (Const (@{const_name termify}, T), [t]) =
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   160
        if not (Term.has_abs t)
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   161
        then if fold_aterms (fn Const _ => I | _ => K false) t true
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   162
          then SOME (HOLogic.mk_prod
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   163
            (t, Abs ("u", HOLogic.unitT, HOLogic.reflect_term t)))
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   164
          else error "Cannot termify expression containing variables"
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   165
        else error "Cannot termify expression containing abstraction"
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   166
    | subst_termify_app (t, ts) = case map_default subst_termify ts
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   167
       of SOME ts' => SOME (list_comb (t, ts'))
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   168
        | NONE => NONE
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   169
  and subst_termify (Abs (v, T, t)) = (case subst_termify t
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   170
       of SOME t' => SOME (Abs (v, T, t'))
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   171
        | NONE => NONE)
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   172
    | subst_termify t = subst_termify_app (strip_comb t) 
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   173
  fun check_termify ts ctxt = map_default subst_termify ts
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   174
    |> Option.map (rpair ctxt)
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   175
in
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   176
  Context.theory_map (Syntax.add_term_check 0 "termify" check_termify)
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   177
end;
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   178
*}
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   179
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   180
locale term_syntax
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   181
begin
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   182
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   183
notation app (infixl "<\<cdot>>" 70) and termify ("termify")
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   184
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   185
end
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   186
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   187
interpretation term_syntax .
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   188
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   189
no_notation app (infixl "<\<cdot>>" 70) and termify ("termify")
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   190
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   191
print_translation {*
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   192
let
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   193
  val term = Const ("<TERM>", dummyT);
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   194
  fun tr1' [_, _] = term;
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   195
  fun tr2' [] = term;
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   196
in
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   197
  [(@{const_syntax Const}, tr1'),
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   198
    (@{const_syntax App}, tr1'),
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   199
    (@{const_syntax dummy_term}, tr2')]
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   200
end
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   201
*}
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   202
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   203
hide const dummy_term termify app
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   204
hide (open) const Const App term_of
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   205
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   206
27afaaa6547a syntax support for term expressions
haftmann
parents: 31144
diff changeset
   207
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   208
subsection {* Evaluation setup *}
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   209
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   210
ML {*
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   211
signature EVAL =
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   212
sig
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   213
  val eval_ref: (unit -> term) option ref
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   214
  val eval_term: theory -> term -> term
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   215
end;
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   216
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   217
structure Eval : EVAL =
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   218
struct
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   219
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   220
val eval_ref = ref (NONE : (unit -> term) option);
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   221
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   222
fun eval_term thy t =
31139
0b615ac7eeaf tuned construction of term_of instances
haftmann
parents: 31048
diff changeset
   223
  Code_ML.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
   224
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   225
end;
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   226
*}
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   227
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   228
setup {*
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   229
  Value.add_evaluator ("code", Eval.eval_term o ProofContext.theory_of)
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   230
*}
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   231
7ebe8dc06cbb evaluation using code generator
haftmann
parents:
diff changeset
   232
end