src/HOL/Library/Eval.thy
author haftmann
Fri Aug 24 14:14:20 2007 +0200 (2007-08-24)
changeset 24423 ae9cd0e92423
parent 24381 560e8ecdf633
child 24508 c8b82fec6447
permissions -rw-r--r--
overloaded definitions accompanied by explicit constants
haftmann@22525
     1
(*  Title:      HOL/Library/Eval.thy
haftmann@22525
     2
    ID:         $Id$
haftmann@22525
     3
    Author:     Florian Haftmann, TU Muenchen
haftmann@22525
     4
*)
haftmann@22525
     5
haftmann@22525
     6
header {* A simple term evaluation mechanism *}
haftmann@22525
     7
haftmann@22525
     8
theory Eval
haftmann@24280
     9
imports Main Pure_term
haftmann@22525
    10
begin
haftmann@22525
    11
haftmann@22527
    12
subsection {* @{text typ_of} class *}
haftmann@22525
    13
haftmann@23062
    14
class typ_of =
haftmann@23062
    15
  fixes typ_of :: "'a\<Colon>{} itself \<Rightarrow> typ"
haftmann@22525
    16
haftmann@22525
    17
ML {*
haftmann@22525
    18
structure TypOf =
haftmann@22525
    19
struct
haftmann@22525
    20
haftmann@22525
    21
val class_typ_of = Sign.intern_class @{theory} "typ_of";
haftmann@22525
    22
haftmann@22525
    23
fun term_typ_of_type ty =
haftmann@22525
    24
  Const (@{const_name typ_of}, Term.itselfT ty --> @{typ typ})
haftmann@22525
    25
    $ Logic.mk_type ty;
haftmann@22525
    26
haftmann@22525
    27
fun mk_typ_of_def ty =
haftmann@22525
    28
  let
haftmann@22525
    29
    val lhs = Const (@{const_name typ_of}, Term.itselfT ty --> @{typ typ})
haftmann@22525
    30
      $ Free ("x", Term.itselfT ty)
haftmann@22525
    31
    val rhs = Pure_term.mk_typ (fn v => term_typ_of_type (TFree v)) ty
haftmann@22525
    32
  in Logic.mk_equals (lhs, rhs) end;
haftmann@22525
    33
haftmann@22525
    34
end;
haftmann@22525
    35
*}
haftmann@22525
    36
haftmann@23062
    37
instance itself :: (typ_of) typ_of
haftmann@23062
    38
  "typ_of T \<equiv> STR ''itself'' {\<struct>} [typ_of TYPE('a\<Colon>typ_of)]" ..
haftmann@23062
    39
haftmann@23062
    40
instance "prop" :: typ_of
haftmann@23062
    41
  "typ_of T \<equiv> STR ''prop'' {\<struct>} []" ..
haftmann@23062
    42
haftmann@23020
    43
instance int :: typ_of
haftmann@23020
    44
  "typ_of T \<equiv> STR ''IntDef.int'' {\<struct>} []" ..
haftmann@23020
    45
haftmann@22525
    46
setup {*
haftmann@22525
    47
let
haftmann@22525
    48
  fun mk arities _ thy =
haftmann@22525
    49
    (maps (fn (tyco, asorts, _) => [(("", []), TypOf.mk_typ_of_def
haftmann@22525
    50
      (Type (tyco,
haftmann@22525
    51
        map TFree (Name.names Name.context "'a" asorts))))]) arities, thy);
haftmann@22525
    52
  fun hook specs =
haftmann@24219
    53
    DatatypeCodegen.prove_codetypes_arities (Class.intro_classes_tac [])
haftmann@22525
    54
      (map (fn (tyco, (is_dt, _)) => (tyco, is_dt)) specs)
haftmann@24423
    55
      [TypOf.class_typ_of] mk ((K o K) (fold (Code.add_func true)))
haftmann@22525
    56
in DatatypeCodegen.add_codetypes_hook_bootstrap hook end
haftmann@22525
    57
*}
haftmann@22525
    58
haftmann@22525
    59
haftmann@22527
    60
subsection {* @{text term_of} class *}
haftmann@22525
    61
haftmann@22525
    62
class term_of = typ_of +
haftmann@23062
    63
  constrains typ_of :: "'a\<Colon>{} itself \<Rightarrow> typ"
haftmann@22525
    64
  fixes term_of :: "'a \<Rightarrow> term"
haftmann@22525
    65
haftmann@22525
    66
ML {*
haftmann@22525
    67
structure TermOf =
haftmann@22525
    68
struct
haftmann@22525
    69
haftmann@22525
    70
local
haftmann@22525
    71
  fun term_term_of ty =
haftmann@22525
    72
    Const (@{const_name term_of}, ty --> @{typ term});
haftmann@22525
    73
in
haftmann@22525
    74
  val class_term_of = Sign.intern_class @{theory} "term_of";
haftmann@22525
    75
  fun mk_terms_of_defs vs (tyco, cs) =
haftmann@22525
    76
    let
haftmann@22525
    77
      val dty = Type (tyco, map TFree vs);
haftmann@22525
    78
      fun mk_eq c =
haftmann@22525
    79
        let
haftmann@22525
    80
          val lhs : term = term_term_of dty $ c;
haftmann@22525
    81
          val rhs : term = Pure_term.mk_term
haftmann@22525
    82
            (fn (v, ty) => term_term_of ty $ Free (v, ty))
haftmann@22525
    83
            (Pure_term.mk_typ (fn (v, sort) => TypOf.term_typ_of_type (TFree (v, sort)))) c
haftmann@22525
    84
        in
haftmann@22525
    85
          HOLogic.mk_eq (lhs, rhs)
haftmann@22525
    86
        end;
haftmann@22525
    87
    in map mk_eq cs end;
haftmann@22525
    88
  fun mk_term_of t =
haftmann@22525
    89
    term_term_of (Term.fastype_of t) $ t;
haftmann@22525
    90
end;
haftmann@22525
    91
haftmann@22525
    92
end;
haftmann@22525
    93
*}
haftmann@22525
    94
haftmann@22525
    95
setup {*
haftmann@22525
    96
let
haftmann@22525
    97
  fun thy_note ((name, atts), thms) =
haftmann@22525
    98
    PureThy.add_thmss [((name, thms), atts)] #-> (fn [thms] => pair (name, thms));
haftmann@22525
    99
  fun thy_def ((name, atts), t) =
haftmann@22525
   100
    PureThy.add_defs_i false [((name, t), atts)] #-> (fn [thm] => pair (name, thm));
haftmann@24423
   101
  fun mk arities css _ thy =
haftmann@22525
   102
    let
haftmann@22525
   103
      val (_, asorts, _) :: _ = arities;
haftmann@22525
   104
      val vs = Name.names Name.context "'a" asorts;
haftmann@22525
   105
      val defs = map (TermOf.mk_terms_of_defs vs) css;
haftmann@22525
   106
      val defs' = (map (pair ("", []) o ObjectLogic.ensure_propT thy) o flat) defs;
haftmann@22525
   107
    in
haftmann@22525
   108
      thy
haftmann@22525
   109
      |> PrimrecPackage.gen_primrec thy_note thy_def "" defs'
haftmann@22525
   110
      |> snd
haftmann@22525
   111
    end;
haftmann@22525
   112
  fun hook specs =
haftmann@22525
   113
    if (fst o hd) specs = (fst o dest_Type) @{typ typ} then I
haftmann@22525
   114
    else
haftmann@24219
   115
      DatatypeCodegen.prove_codetypes_arities (Class.intro_classes_tac [])
haftmann@22525
   116
      (map (fn (tyco, (is_dt, _)) => (tyco, is_dt)) specs)
haftmann@22525
   117
      [TermOf.class_term_of] ((K o K o pair) []) mk
haftmann@22525
   118
in DatatypeCodegen.add_codetypes_hook_bootstrap hook end
haftmann@22525
   119
*}
haftmann@22525
   120
haftmann@23062
   121
abbreviation
haftmann@23062
   122
  intT :: "typ"
haftmann@23062
   123
where
haftmann@23062
   124
  "intT \<equiv> STR ''IntDef.int'' {\<struct>} []"
haftmann@23062
   125
haftmann@23133
   126
abbreviation
haftmann@23133
   127
  bitT :: "typ"
haftmann@23133
   128
where
haftmann@23133
   129
  "bitT \<equiv> STR ''Numeral.bit'' {\<struct>} []"
haftmann@23133
   130
haftmann@23062
   131
function
haftmann@23062
   132
  mk_int :: "int \<Rightarrow> term"
haftmann@23062
   133
where
haftmann@23062
   134
  "mk_int k = (if k = 0 then STR ''Numeral.Pls'' \<Colon>\<subseteq> intT
haftmann@23062
   135
    else if k = -1 then STR ''Numeral.Min'' \<Colon>\<subseteq> intT
haftmann@23062
   136
    else let (l, m) = divAlg (k, 2)
haftmann@23133
   137
  in STR ''Numeral.Bit'' \<Colon>\<subseteq> intT \<rightarrow> bitT \<rightarrow> intT \<bullet> mk_int l \<bullet>
haftmann@23133
   138
    (if m = 0 then STR ''Numeral.bit.B0'' \<Colon>\<subseteq> bitT else STR ''Numeral.bit.B1'' \<Colon>\<subseteq> bitT))"
haftmann@23062
   139
by pat_completeness auto
haftmann@23062
   140
termination by (relation "measure (nat o abs)") (auto simp add: divAlg_mod_div)
haftmann@23062
   141
haftmann@23062
   142
instance int :: term_of
haftmann@23133
   143
  "term_of k \<equiv> STR ''Numeral.number_class.number_of'' \<Colon>\<subseteq> intT \<rightarrow> intT \<bullet> mk_int k" ..
haftmann@23062
   144
haftmann@23062
   145
haftmann@22804
   146
text {* Adaption for @{typ ml_string}s *}
haftmann@22525
   147
haftmann@22845
   148
lemmas [code func, code func del] = term_of_ml_string_def
haftmann@22525
   149
haftmann@22525
   150
haftmann@22525
   151
subsection {* Evaluation infrastructure *}
haftmann@22525
   152
haftmann@22525
   153
ML {*
haftmann@22525
   154
signature EVAL =
haftmann@22525
   155
sig
haftmann@22804
   156
  val eval_ref: term option ref
haftmann@24280
   157
  val eval_conv: cterm -> thm
haftmann@24280
   158
  val eval_print: (cterm -> thm) -> Proof.context -> term -> unit
haftmann@24280
   159
  val eval_print_cmd: (cterm -> thm) -> string -> Toplevel.state -> unit
haftmann@22525
   160
end;
haftmann@22525
   161
haftmann@24280
   162
structure Eval =
haftmann@22525
   163
struct
haftmann@22525
   164
haftmann@22525
   165
val eval_ref = ref (NONE : term option);
haftmann@22525
   166
haftmann@24280
   167
end;
haftmann@24280
   168
*}
haftmann@24280
   169
haftmann@24381
   170
oracle eval_oracle ("term * CodeThingol.code * (CodeThingol.typscheme * CodeThingol.iterm) * cterm") =
haftmann@24381
   171
{* fn thy => fn (t0, code, ((vs, ty), t), ct) => 
haftmann@24280
   172
let
haftmann@24280
   173
  val _ = (Term.map_types o Term.map_atyps) (fn _ =>
haftmann@24280
   174
    error ("Term " ^ Sign.string_of_term thy t0 ^ " contains polymorphic type"))
haftmann@24280
   175
    t0;
haftmann@24280
   176
in Logic.mk_equals (t0, CodeTarget.eval_term thy ("Eval.eval_ref", Eval.eval_ref) code (t, ty) []) end;
haftmann@24280
   177
*}
haftmann@24280
   178
haftmann@24280
   179
ML {*
haftmann@24280
   180
structure Eval : EVAL =
haftmann@24280
   181
struct
haftmann@24280
   182
haftmann@24280
   183
open Eval;
haftmann@24280
   184
haftmann@24381
   185
fun eval_invoke thy t0 code vs_ty_t _ ct = eval_oracle thy (t0, code, vs_ty_t, ct);
haftmann@22525
   186
haftmann@24280
   187
fun eval_conv ct =
haftmann@24280
   188
  let
haftmann@24280
   189
    val thy = Thm.theory_of_cterm ct;
haftmann@24280
   190
    val ct' = (Thm.cterm_of thy o TermOf.mk_term_of o Thm.term_of) ct;
haftmann@24280
   191
  in
haftmann@24280
   192
    CodePackage.eval_term thy
haftmann@24280
   193
      (eval_invoke thy (Thm.term_of ct)) ct'
haftmann@24280
   194
  end;
haftmann@24280
   195
haftmann@24280
   196
fun eval_print conv ctxt t =
haftmann@24280
   197
  let
haftmann@24280
   198
    val thy = ProofContext.theory_of ctxt;
haftmann@24280
   199
    val ct = Thm.cterm_of thy t;
haftmann@24280
   200
    val (_, t') = (Logic.dest_equals o Thm.prop_of o conv) ct;
haftmann@24280
   201
    val ty = Term.type_of t';
haftmann@24280
   202
    val p = 
haftmann@24280
   203
      Pretty.block [Pretty.quote (ProofContext.pretty_term ctxt t'), Pretty.fbrk,
haftmann@24280
   204
        Pretty.str "::", Pretty.brk 1, Pretty.quote (ProofContext.pretty_typ ctxt ty)];
haftmann@24280
   205
  in Pretty.writeln p end;
haftmann@24280
   206
haftmann@24280
   207
fun eval_print_cmd conv raw_t state =
haftmann@22525
   208
  let
haftmann@22804
   209
    val ctxt = Toplevel.context_of state;
haftmann@24280
   210
    val t = ProofContext.read_term ctxt raw_t;
haftmann@22804
   211
    val thy = ProofContext.theory_of ctxt;
haftmann@24280
   212
    val ct = Thm.cterm_of thy t;
haftmann@24280
   213
    val (_, t') = (Logic.dest_equals o Thm.prop_of o conv) ct;
haftmann@24280
   214
    val ty = Term.type_of t';
haftmann@24280
   215
    val p = 
haftmann@24280
   216
      Pretty.block [Pretty.quote (ProofContext.pretty_term ctxt t'), Pretty.fbrk,
haftmann@24280
   217
        Pretty.str "::", Pretty.brk 1, Pretty.quote (ProofContext.pretty_typ ctxt ty)];
haftmann@24280
   218
  in Pretty.writeln p end;
haftmann@22525
   219
haftmann@22525
   220
end;
haftmann@22525
   221
*}
haftmann@22525
   222
haftmann@22804
   223
ML {*
haftmann@22804
   224
val valueP =
haftmann@22804
   225
  OuterSyntax.improper_command "value" "read, evaluate and print term" OuterKeyword.diag
haftmann@22804
   226
    ((OuterParse.opt_keyword "overloaded" -- OuterParse.term)
haftmann@24280
   227
      >> (fn (b, t) => Toplevel.no_timing o Toplevel.keep
haftmann@24280
   228
           (Eval.eval_print_cmd (if b then Eval.eval_conv else Codegen.evaluation_conv) t)));
haftmann@22804
   229
haftmann@22804
   230
val _ = OuterSyntax.add_parsers [valueP];
haftmann@22804
   231
*}
haftmann@22804
   232
wenzelm@22665
   233
end