src/HOL/Library/Eval.thy
author haftmann
Sun, 09 Mar 2008 07:57:30 +0100
changeset 26242 d64510d3c7b7
parent 26168 3bd9ac4e0b97
child 26267 ba710daf77a7
permissions -rw-r--r--
tuned
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/Library/Eval.thy
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
     2
    ID:         $Id$
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
     4
*)
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
     5
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
     6
header {* A simple term evaluation mechanism *}
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
     7
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
     8
theory Eval
26037
8bf9e480a7b8 suppport for messages and indices
haftmann
parents: 26032
diff changeset
     9
imports
26168
3bd9ac4e0b97 added theory for reflected types
haftmann
parents: 26037
diff changeset
    10
  RType
26037
8bf9e480a7b8 suppport for messages and indices
haftmann
parents: 26032
diff changeset
    11
  Code_Index (* this theory is just imported for a term_of setup *)
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    12
begin
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    13
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    14
subsection {* Term representation *}
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    15
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    16
subsubsection {* Definitions *}
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    17
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    18
datatype "term" = dummy_term
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    19
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    20
definition
26168
3bd9ac4e0b97 added theory for reflected types
haftmann
parents: 26037
diff changeset
    21
  Const :: "message_string \<Rightarrow> rtype \<Rightarrow> term"
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    22
where
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    23
  "Const _ _ = dummy_term"
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    24
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    25
definition
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    26
  App :: "term \<Rightarrow> term \<Rightarrow> term"
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    27
where
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    28
  "App _ _ = dummy_term"
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    29
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    30
code_datatype Const App
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    31
26168
3bd9ac4e0b97 added theory for reflected types
haftmann
parents: 26037
diff changeset
    32
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    33
subsubsection {* Class @{term term_of} *}
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    34
26168
3bd9ac4e0b97 added theory for reflected types
haftmann
parents: 26037
diff changeset
    35
class term_of = rtype +
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    36
  fixes term_of :: "'a \<Rightarrow> term"
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    37
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    38
lemma term_of_anything: "term_of x \<equiv> t"
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    39
  by (rule eq_reflection) (cases "term_of x", cases t, simp)
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    40
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    41
ML {*
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    42
structure Eval =
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    43
struct
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    44
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    45
fun mk_term f g (Const (c, ty)) =
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    46
      @{term Const} $ Message_String.mk c $ g ty
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    47
  | mk_term f g (t1 $ t2) =
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    48
      @{term App} $ mk_term f g t1 $ mk_term f g t2
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    49
  | mk_term f g (Free v) = f v;
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    50
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    51
fun mk_term_of ty t = Const (@{const_name term_of}, ty --> @{typ term}) $ t;
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    52
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    53
end;
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    54
*}
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    55
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    56
setup {*
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    57
let
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    58
  fun add_term_of_def ty vs tyco thy =
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    59
    let
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    60
      val lhs = Const (@{const_name term_of}, ty --> @{typ term})
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    61
        $ Free ("x", ty);
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    62
      val rhs = @{term "undefined \<Colon> term"};
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    63
      val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    64
    in
25536
01753a944433 improved
haftmann
parents: 25502
diff changeset
    65
      thy
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    66
      |> TheoryTarget.instantiation ([tyco], vs, @{sort term_of})
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    67
      |> `(fn lthy => Syntax.check_term lthy eq)
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    68
      |-> (fn eq => Specification.definition (NONE, (("", []), eq)))
25569
c597835d5de4 dropped Instance.instantiate
haftmann
parents: 25559
diff changeset
    69
      |> snd
c597835d5de4 dropped Instance.instantiate
haftmann
parents: 25559
diff changeset
    70
      |> Class.prove_instantiation_instance (K (Class.intro_classes_tac []))
c597835d5de4 dropped Instance.instantiate
haftmann
parents: 25559
diff changeset
    71
      |> LocalTheory.exit
c597835d5de4 dropped Instance.instantiate
haftmann
parents: 25559
diff changeset
    72
      |> ProofContext.theory_of
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    73
    end;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    74
  fun interpretator (tyco, (raw_vs, _)) thy =
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    75
    let
26168
3bd9ac4e0b97 added theory for reflected types
haftmann
parents: 26037
diff changeset
    76
      val has_inst = can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of};
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    77
      val constrain_sort =
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    78
        curry (Sorts.inter_sort (Sign.classes_of thy)) @{sort term_of};
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    79
      val vs = (map o apsnd) constrain_sort raw_vs;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    80
      val ty = Type (tyco, map TFree vs);
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    81
    in
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    82
      thy
26168
3bd9ac4e0b97 added theory for reflected types
haftmann
parents: 26037
diff changeset
    83
      |> RType.perhaps_add_def tyco
3bd9ac4e0b97 added theory for reflected types
haftmann
parents: 26037
diff changeset
    84
      |> not has_inst ? add_term_of_def ty vs tyco
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    85
    end;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    86
in
26168
3bd9ac4e0b97 added theory for reflected types
haftmann
parents: 26037
diff changeset
    87
  Code.type_interpretation interpretator
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    88
end
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    89
*}
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    90
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    91
setup {*
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    92
let
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    93
  fun mk_term_of_eq ty vs tyco (c, tys) =
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    94
    let
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    95
      val t = list_comb (Const (c, tys ---> ty),
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    96
        map Free (Name.names Name.context "a" tys));
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    97
    in (map_aterms (fn Free (v, ty) => Var ((v, 0), ty) | t => t) t, Eval.mk_term
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    98
      (fn (v, ty) => Eval.mk_term_of ty (Var ((v, 0), ty)))
26168
3bd9ac4e0b97 added theory for reflected types
haftmann
parents: 26037
diff changeset
    99
      (RType.mk (fn (v, sort) => RType.rtype (TFree (v, sort)))) t)
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   100
    end;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   101
  fun prove_term_of_eq ty eq thy =
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   102
    let
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   103
      val cty = Thm.ctyp_of thy ty;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   104
      val (arg, rhs) = pairself (Thm.cterm_of thy) eq;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   105
      val thm = @{thm term_of_anything}
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   106
        |> Drule.instantiate' [SOME cty] [SOME arg, SOME rhs]
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   107
        |> Thm.varifyT;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   108
    in
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   109
      thy
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   110
      |> Code.add_func thm
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   111
    end;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   112
  fun interpretator (tyco, (raw_vs, raw_cs)) thy =
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   113
    let
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   114
      val constrain_sort =
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   115
        curry (Sorts.inter_sort (Sign.classes_of thy)) @{sort term_of};
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   116
      val vs = (map o apsnd) constrain_sort raw_vs;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   117
      val cs = (map o apsnd o map o map_atyps)
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   118
        (fn TFree (v, sort) => TFree (v, constrain_sort sort)) raw_cs;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   119
      val ty = Type (tyco, map TFree vs);
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   120
      val eqs = map (mk_term_of_eq ty vs tyco) cs;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   121
      val const = AxClass.param_of_inst thy (@{const_name term_of}, tyco);
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   122
    in
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   123
      thy
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   124
      |> Code.del_funcs const
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   125
      |> fold (prove_term_of_eq ty) eqs
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   126
    end;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   127
in
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   128
  Code.type_interpretation interpretator
25569
c597835d5de4 dropped Instance.instantiate
haftmann
parents: 25559
diff changeset
   129
end
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   130
*}
23062
d88d2087436d evaluation for integers
haftmann
parents: 23020
diff changeset
   131
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   132
subsubsection {* Code generator setup *}
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   133
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   134
lemmas [code func del] = term.recs term.cases term.size
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   135
lemma [code func, code func del]: "(t1\<Colon>term) = t2 \<longleftrightarrow> t1 = t2" ..
26037
8bf9e480a7b8 suppport for messages and indices
haftmann
parents: 26032
diff changeset
   136
26168
3bd9ac4e0b97 added theory for reflected types
haftmann
parents: 26037
diff changeset
   137
lemma [code func, code func del]: "(term_of \<Colon> rtype \<Rightarrow> term) = term_of" ..
26037
8bf9e480a7b8 suppport for messages and indices
haftmann
parents: 26032
diff changeset
   138
lemma [code func, code func del]: "(term_of \<Colon> term \<Rightarrow> term) = term_of" ..
8bf9e480a7b8 suppport for messages and indices
haftmann
parents: 26032
diff changeset
   139
lemma [code func, code func del]: "(term_of \<Colon> index \<Rightarrow> term) = term_of" ..
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   140
lemma [code func, code func del]: "(term_of \<Colon> message_string \<Rightarrow> term) = term_of" ..
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   141
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   142
code_type "term"
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   143
  (SML "Term.term")
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   144
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   145
code_const Const and App
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   146
  (SML "Term.Const/ (_, _)" and "Term.$/ (_, _)")
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   147
26037
8bf9e480a7b8 suppport for messages and indices
haftmann
parents: 26032
diff changeset
   148
code_const "term_of \<Colon> index \<Rightarrow> term"
8bf9e480a7b8 suppport for messages and indices
haftmann
parents: 26032
diff changeset
   149
  (SML "HOLogic.mk'_number/ HOLogic.indexT")
8bf9e480a7b8 suppport for messages and indices
haftmann
parents: 26032
diff changeset
   150
8bf9e480a7b8 suppport for messages and indices
haftmann
parents: 26032
diff changeset
   151
code_const "term_of \<Colon> message_string \<Rightarrow> term"
8bf9e480a7b8 suppport for messages and indices
haftmann
parents: 26032
diff changeset
   152
  (SML "Message'_String.mk")
8bf9e480a7b8 suppport for messages and indices
haftmann
parents: 26032
diff changeset
   153
8bf9e480a7b8 suppport for messages and indices
haftmann
parents: 26032
diff changeset
   154
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   155
subsection {* Evaluation setup *}
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   156
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   157
ML {*
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   158
signature EVAL =
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   159
sig
24587
4f2cbf6e563f multi-functional value keyword
haftmann
parents: 24508
diff changeset
   160
  val eval_ref: (unit -> term) option ref
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   161
  val eval_term: theory -> term -> term
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   162
  val evaluate: Proof.context -> term -> unit
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   163
  val evaluate': string -> Proof.context -> term -> unit
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   164
  val evaluate_cmd: string option -> string -> Toplevel.state -> unit
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   165
end;
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   166
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   167
structure Eval : EVAL =
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   168
struct
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   169
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   170
open Eval;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   171
24587
4f2cbf6e563f multi-functional value keyword
haftmann
parents: 24508
diff changeset
   172
val eval_ref = ref (NONE : (unit -> term) option);
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   173
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   174
fun eval_term thy t =
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   175
  t 
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   176
  |> Eval.mk_term_of (fastype_of t)
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   177
  |> (fn t => CodePackage.eval_term ("Eval.eval_ref", eval_ref) thy t [])
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   178
  |> Code.postprocess_term thy;
24280
c9867bdf2424 updated code generator setup
haftmann
parents: 24219
diff changeset
   179
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   180
val evaluators = [
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   181
  ("code", eval_term),
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   182
  ("SML", Codegen.eval_term),
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   183
  ("normal_form", Nbe.norm_term)
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   184
];
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   185
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   186
fun gen_evaluate evaluators ctxt t =
24280
c9867bdf2424 updated code generator setup
haftmann
parents: 24219
diff changeset
   187
  let
c9867bdf2424 updated code generator setup
haftmann
parents: 24219
diff changeset
   188
    val thy = ProofContext.theory_of ctxt;
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   189
    val (evls, evl) = split_last evaluators;
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   190
    val t' = case get_first (fn f => try (f thy) t) evls
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   191
     of SOME t' => t'
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   192
      | NONE => evl thy t;
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   193
    val ty' = Term.type_of t';
24920
2a45e400fdad generic Syntax.pretty/string_of operations;
wenzelm
parents: 24916
diff changeset
   194
    val p = Pretty.block [Pretty.quote (Syntax.pretty_term ctxt t'),
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   195
      Pretty.fbrk, Pretty.str "::", Pretty.brk 1,
24920
2a45e400fdad generic Syntax.pretty/string_of operations;
wenzelm
parents: 24916
diff changeset
   196
      Pretty.quote (Syntax.pretty_typ ctxt ty')];
24280
c9867bdf2424 updated code generator setup
haftmann
parents: 24219
diff changeset
   197
  in Pretty.writeln p end;
c9867bdf2424 updated code generator setup
haftmann
parents: 24219
diff changeset
   198
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   199
val evaluate = gen_evaluate (map snd evaluators);
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   200
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   201
fun evaluate' name = gen_evaluate
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   202
  [(the o AList.lookup (op =) evaluators) name];
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   203
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   204
fun evaluate_cmd some_name raw_t state =
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   205
  let
22804
haftmann
parents: 22665
diff changeset
   206
    val ctxt = Toplevel.context_of state;
24508
c8b82fec6447 replaced ProofContext.read_term/prop by general Syntax.read_term/prop;
wenzelm
parents: 24423
diff changeset
   207
    val t = Syntax.read_term ctxt raw_t;
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   208
  in case some_name
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   209
   of NONE => evaluate ctxt t
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   210
    | SOME name => evaluate' name ctxt t
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   211
  end;
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   212
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   213
end;
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   214
*}
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   215
22804
haftmann
parents: 22665
diff changeset
   216
ML {*
haftmann
parents: 22665
diff changeset
   217
  OuterSyntax.improper_command "value" "read, evaluate and print term" OuterKeyword.diag
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   218
    (Scan.option (OuterParse.$$$ "(" |-- OuterParse.name --| OuterParse.$$$ ")")
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   219
    -- OuterParse.term
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   220
      >> (fn (some_name, t) => Toplevel.no_timing o Toplevel.keep
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   221
           (Eval.evaluate_cmd some_name t)));
22804
haftmann
parents: 22665
diff changeset
   222
*}
haftmann
parents: 22665
diff changeset
   223
26032
377b7aa0b5b5 <TERM> syntax
haftmann
parents: 26020
diff changeset
   224
print_translation {*
377b7aa0b5b5 <TERM> syntax
haftmann
parents: 26020
diff changeset
   225
let
377b7aa0b5b5 <TERM> syntax
haftmann
parents: 26020
diff changeset
   226
  val term = Const ("<TERM>", dummyT);
377b7aa0b5b5 <TERM> syntax
haftmann
parents: 26020
diff changeset
   227
  fun tr1' [_, _] = term;
377b7aa0b5b5 <TERM> syntax
haftmann
parents: 26020
diff changeset
   228
  fun tr2' [] = term;
377b7aa0b5b5 <TERM> syntax
haftmann
parents: 26020
diff changeset
   229
in
377b7aa0b5b5 <TERM> syntax
haftmann
parents: 26020
diff changeset
   230
  [(@{const_syntax Const}, tr1'),
377b7aa0b5b5 <TERM> syntax
haftmann
parents: 26020
diff changeset
   231
    (@{const_syntax App}, tr1'),
377b7aa0b5b5 <TERM> syntax
haftmann
parents: 26020
diff changeset
   232
    (@{const_syntax dummy_term}, tr2')]
377b7aa0b5b5 <TERM> syntax
haftmann
parents: 26020
diff changeset
   233
end
377b7aa0b5b5 <TERM> syntax
haftmann
parents: 26020
diff changeset
   234
*}
377b7aa0b5b5 <TERM> syntax
haftmann
parents: 26020
diff changeset
   235
26168
3bd9ac4e0b97 added theory for reflected types
haftmann
parents: 26037
diff changeset
   236
hide (open) const term_of
26032
377b7aa0b5b5 <TERM> syntax
haftmann
parents: 26020
diff changeset
   237
hide const Const App dummy_term
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   238
22665
cf152ff55d16 tuned document (headers, sections, spacing);
wenzelm
parents: 22527
diff changeset
   239
end