src/HOL/Library/Eval.thy
author haftmann
Thu, 31 Jan 2008 11:44:43 +0100
changeset 26020 ffe1a032d24b
parent 26011 d55224947082
child 26032 377b7aa0b5b5
permissions -rw-r--r--
proper term_of functions
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
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
     9
imports ATP_Linkup Code_Message
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    10
begin
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    11
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    12
subsection {* Type reflection *}
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    13
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    14
subsubsection {* Definition *}
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    15
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    16
types vname = message_string;
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    17
types "class" = message_string;
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    18
types sort = "class list"
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    19
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    20
datatype "typ" =
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    21
    Type message_string "typ list"
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    22
  | TFree vname sort  
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    23
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    24
ML {*
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    25
structure Eval =
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    26
struct
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    27
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    28
val mk_sort = HOLogic.mk_list @{typ class} o map Message_String.mk;
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    29
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    30
fun mk_typ f (Type (tyco, tys)) =
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    31
      @{term Type} $ Message_String.mk tyco
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    32
        $ HOLogic.mk_list @{typ typ} (map (mk_typ f) tys)
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    33
  | mk_typ f (TFree v) =
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    34
      f v;
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    35
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    36
end;
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    37
*}
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    38
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    39
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    40
subsubsection {* Class @{text typ_of} *}
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    41
23062
d88d2087436d evaluation for integers
haftmann
parents: 23020
diff changeset
    42
class typ_of =
d88d2087436d evaluation for integers
haftmann
parents: 23020
diff changeset
    43
  fixes typ_of :: "'a\<Colon>{} itself \<Rightarrow> typ"
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    44
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    45
ML {*
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    46
structure Eval =
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    47
struct
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    48
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    49
open Eval;
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    50
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    51
fun mk_typ_of ty =
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    52
  Const (@{const_name typ_of}, Term.itselfT ty --> @{typ typ})
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    53
    $ Logic.mk_type ty;
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    54
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    55
fun add_typ_of_def tyco thy =
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    56
  let
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    57
    val sorts = replicate (Sign.arity_number thy tyco) @{sort typ_of};
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    58
    val vs = Name.names Name.context "'a" sorts;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    59
    val ty = Type (tyco, map TFree vs);
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    60
    val lhs = Const (@{const_name typ_of}, Term.itselfT ty --> @{typ typ})
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    61
      $ Free ("T", Term.itselfT ty);
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    62
    val rhs = mk_typ (fn v => mk_typ_of (TFree v)) ty;
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
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    65
    thy
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    66
    |> TheoryTarget.instantiation ([tyco], vs, @{sort typ_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)))
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    69
    |> snd
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    70
    |> Class.prove_instantiation_instance (K (Class.intro_classes_tac []))
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    71
    |> LocalTheory.exit
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    72
    |> ProofContext.theory_of
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    73
  end;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    74
25536
01753a944433 improved
haftmann
parents: 25502
diff changeset
    75
end
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    76
*}
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    77
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    78
(*setup {*
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    79
let
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    80
  open Eval;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    81
in
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    82
  Eval.add_typ_of_def @{type_name prop}
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    83
  #> Eval.add_typ_of_def @{type_name itself}
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    84
  #> Eval.add_typ_of_def @{type_name bool}
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    85
  #> Eval.add_typ_of_def @{type_name set}
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    86
  #> TypedefPackage.interpretation Eval.add_typ_of_def
25536
01753a944433 improved
haftmann
parents: 25502
diff changeset
    87
end
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    88
*}*)
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    89
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    90
subsubsection {* Code generator setup *}
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    91
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    92
lemma [code func]:
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    93
  "Type tyco1 tys1 = Type tyco2 tys2 \<longleftrightarrow> tyco1 = tyco2
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    94
     \<and> list_all2 (op =) tys1 tys2"
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    95
  by (auto simp add: list_all2_eq [symmetric])
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    96
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    97
code_type "typ"
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    98
  (SML "Term.typ")
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    99
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   100
code_const Type and TFree
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   101
  (SML "Term.Type/ (_, _)" and "Term.TFree/ (_, _)")
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   102
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   103
code_reserved SML Term
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   104
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   105
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   106
subsection {* Term representation *}
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   107
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   108
subsubsection {* Definitions *}
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   109
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   110
datatype "term" = dummy_term
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   111
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   112
definition
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   113
  Const :: "message_string \<Rightarrow> typ \<Rightarrow> term"
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   114
where
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   115
  "Const _ _ = dummy_term"
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   116
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   117
definition
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   118
  App :: "term \<Rightarrow> term \<Rightarrow> term"
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   119
where
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   120
  "App _ _ = dummy_term"
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   121
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   122
code_datatype Const App
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   123
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   124
subsubsection {* Class @{term term_of} *}
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   125
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   126
class term_of = typ_of +
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   127
  constrains typ_of :: "'a\<Colon>{} itself \<Rightarrow> typ"
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   128
  fixes term_of :: "'a \<Rightarrow> term"
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   129
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   130
lemma term_of_anything: "term_of x \<equiv> t"
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   131
  by (rule eq_reflection) (cases "term_of x", cases t, simp)
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   132
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   133
ML {*
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   134
structure Eval =
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   135
struct
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   136
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   137
open Eval;
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   138
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   139
fun mk_term f g (Const (c, ty)) =
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   140
      @{term Const} $ Message_String.mk c $ g ty
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   141
  | mk_term f g (t1 $ t2) =
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   142
      @{term App} $ mk_term f g t1 $ mk_term f g t2
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   143
  | mk_term f g (Free v) = f v;
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   144
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   145
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
   146
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   147
end;
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   148
*}
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   149
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   150
setup {*
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   151
let
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   152
  fun has_no_inst tyco sort thy =
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   153
    not (can (Sorts.mg_domain (Sign.classes_of thy) tyco)
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   154
      sort);
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   155
  fun add_term_of_def ty vs tyco thy =
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   156
    let
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   157
      val lhs = Const (@{const_name term_of}, ty --> @{typ term})
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   158
        $ Free ("x", ty);
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   159
      val rhs = @{term "undefined \<Colon> term"};
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   160
      val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   161
    in
25536
01753a944433 improved
haftmann
parents: 25502
diff changeset
   162
      thy
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   163
      |> TheoryTarget.instantiation ([tyco], vs, @{sort term_of})
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   164
      |> `(fn lthy => Syntax.check_term lthy eq)
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   165
      |-> (fn eq => Specification.definition (NONE, (("", []), eq)))
25569
c597835d5de4 dropped Instance.instantiate
haftmann
parents: 25559
diff changeset
   166
      |> snd
c597835d5de4 dropped Instance.instantiate
haftmann
parents: 25559
diff changeset
   167
      |> Class.prove_instantiation_instance (K (Class.intro_classes_tac []))
c597835d5de4 dropped Instance.instantiate
haftmann
parents: 25559
diff changeset
   168
      |> LocalTheory.exit
c597835d5de4 dropped Instance.instantiate
haftmann
parents: 25559
diff changeset
   169
      |> ProofContext.theory_of
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   170
    end;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   171
  fun interpretator (tyco, (raw_vs, _)) thy =
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   172
    let
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   173
      val constrain_sort =
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   174
        curry (Sorts.inter_sort (Sign.classes_of thy)) @{sort term_of};
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   175
      val vs = (map o apsnd) constrain_sort raw_vs;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   176
      val ty = Type (tyco, map TFree vs);
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   177
    in
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   178
      thy
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   179
      |> `(has_no_inst tyco @{sort typ_of})
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   180
      |-> (fn no_typ_of => no_typ_of ? Eval.add_typ_of_def tyco)
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   181
      |> `(has_no_inst tyco @{sort term_of})
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   182
      |-> (fn no_term_of => no_term_of ? add_term_of_def ty vs tyco)
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   183
    end;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   184
in
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   185
  Code.type_interpretation interpretator
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   186
end
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   187
*}
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   188
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   189
setup {*
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   190
let
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   191
  fun mk_term_of_eq ty vs tyco (c, tys) =
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   192
    let
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   193
      val t = list_comb (Const (c, tys ---> ty),
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   194
        map Free (Name.names Name.context "a" tys));
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   195
    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
   196
      (fn (v, ty) => Eval.mk_term_of ty (Var ((v, 0), ty)))
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   197
      (Eval.mk_typ (fn (v, sort) => Eval.mk_typ_of (TFree (v, sort)))) t)
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   198
    end;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   199
  fun prove_term_of_eq ty eq thy =
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   200
    let
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   201
      val cty = Thm.ctyp_of thy ty;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   202
      val (arg, rhs) = pairself (Thm.cterm_of thy) eq;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   203
      val thm = @{thm term_of_anything}
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   204
        |> Drule.instantiate' [SOME cty] [SOME arg, SOME rhs]
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   205
        |> Thm.varifyT;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   206
    in
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   207
      thy
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   208
      |> Code.add_func thm
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   209
    end;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   210
  fun interpretator (tyco, (raw_vs, raw_cs)) thy =
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   211
    let
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   212
      val constrain_sort =
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   213
        curry (Sorts.inter_sort (Sign.classes_of thy)) @{sort term_of};
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   214
      val vs = (map o apsnd) constrain_sort raw_vs;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   215
      val cs = (map o apsnd o map o map_atyps)
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   216
        (fn TFree (v, sort) => TFree (v, constrain_sort sort)) raw_cs;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   217
      val ty = Type (tyco, map TFree vs);
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   218
      val eqs = map (mk_term_of_eq ty vs tyco) cs;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   219
      val const = AxClass.param_of_inst thy (@{const_name term_of}, tyco);
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   220
    in
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   221
      thy
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   222
      |> Code.del_funcs const
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   223
      |> fold (prove_term_of_eq ty) eqs
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   224
    end;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   225
in
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   226
  Code.type_interpretation interpretator
25569
c597835d5de4 dropped Instance.instantiate
haftmann
parents: 25559
diff changeset
   227
end
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   228
*}
23062
d88d2087436d evaluation for integers
haftmann
parents: 23020
diff changeset
   229
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   230
subsubsection {* Code generator setup *}
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   231
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   232
lemmas [code func del] = term.recs term.cases term.size
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   233
lemma [code func, code func del]: "(t1\<Colon>term) = t2 \<longleftrightarrow> t1 = t2" ..
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   234
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
   235
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   236
code_type "term"
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   237
  (SML "Term.term")
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   238
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   239
code_const Const and App
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   240
  (SML "Term.Const/ (_, _)" and "Term.$/ (_, _)")
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   241
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   242
subsection {* Evaluation setup *}
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   243
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   244
ML {*
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   245
signature EVAL =
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   246
sig
24587
4f2cbf6e563f multi-functional value keyword
haftmann
parents: 24508
diff changeset
   247
  val eval_ref: (unit -> term) option ref
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   248
  val eval_term: theory -> term -> term
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   249
  val evaluate: Proof.context -> term -> unit
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   250
  val evaluate': string -> Proof.context -> term -> unit
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   251
  val evaluate_cmd: string option -> string -> Toplevel.state -> unit
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   252
end;
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   253
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   254
structure Eval : EVAL =
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   255
struct
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   256
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   257
open Eval;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   258
24587
4f2cbf6e563f multi-functional value keyword
haftmann
parents: 24508
diff changeset
   259
val eval_ref = ref (NONE : (unit -> term) option);
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   260
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   261
fun eval_term thy t =
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   262
  t 
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   263
  |> Eval.mk_term_of (fastype_of t)
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   264
  |> (fn t => CodePackage.eval_term ("Eval.eval_ref", eval_ref) thy t [])
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   265
  |> Code.postprocess_term thy;
24280
c9867bdf2424 updated code generator setup
haftmann
parents: 24219
diff changeset
   266
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   267
val evaluators = [
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   268
  ("code", eval_term),
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   269
  ("SML", Codegen.eval_term),
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   270
  ("normal_form", Nbe.norm_term)
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   271
];
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   272
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   273
fun gen_evaluate evaluators ctxt t =
24280
c9867bdf2424 updated code generator setup
haftmann
parents: 24219
diff changeset
   274
  let
c9867bdf2424 updated code generator setup
haftmann
parents: 24219
diff changeset
   275
    val thy = ProofContext.theory_of ctxt;
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   276
    val (evls, evl) = split_last evaluators;
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   277
    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
   278
     of SOME t' => t'
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   279
      | NONE => evl thy t;
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   280
    val ty' = Term.type_of t';
24920
2a45e400fdad generic Syntax.pretty/string_of operations;
wenzelm
parents: 24916
diff changeset
   281
    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
   282
      Pretty.fbrk, Pretty.str "::", Pretty.brk 1,
24920
2a45e400fdad generic Syntax.pretty/string_of operations;
wenzelm
parents: 24916
diff changeset
   283
      Pretty.quote (Syntax.pretty_typ ctxt ty')];
24280
c9867bdf2424 updated code generator setup
haftmann
parents: 24219
diff changeset
   284
  in Pretty.writeln p end;
c9867bdf2424 updated code generator setup
haftmann
parents: 24219
diff changeset
   285
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   286
val evaluate = gen_evaluate (map snd evaluators);
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   287
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   288
fun evaluate' name = gen_evaluate
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   289
  [(the o AList.lookup (op =) evaluators) name];
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   290
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   291
fun evaluate_cmd some_name raw_t state =
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   292
  let
22804
haftmann
parents: 22665
diff changeset
   293
    val ctxt = Toplevel.context_of state;
24508
c8b82fec6447 replaced ProofContext.read_term/prop by general Syntax.read_term/prop;
wenzelm
parents: 24423
diff changeset
   294
    val t = Syntax.read_term ctxt raw_t;
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   295
  in case some_name
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   296
   of NONE => evaluate ctxt t
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   297
    | SOME name => evaluate' name ctxt t
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   298
  end;
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   299
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   300
end;
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   301
*}
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   302
22804
haftmann
parents: 22665
diff changeset
   303
ML {*
haftmann
parents: 22665
diff changeset
   304
  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
   305
    (Scan.option (OuterParse.$$$ "(" |-- OuterParse.name --| OuterParse.$$$ ")")
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   306
    -- OuterParse.term
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   307
      >> (fn (some_name, t) => Toplevel.no_timing o Toplevel.keep
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   308
           (Eval.evaluate_cmd some_name t)));
22804
haftmann
parents: 22665
diff changeset
   309
*}
haftmann
parents: 22665
diff changeset
   310
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   311
hide (open) const Type TFree Const App dummy_term typ_of term_of
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   312
22665
cf152ff55d16 tuned document (headers, sections, spacing);
wenzelm
parents: 22527
diff changeset
   313
end