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