src/HOL/Library/Eval.thy
author chaieb
Mon Jun 11 11:06:04 2007 +0200 (2007-06-11)
changeset 23315 df3a7e9ebadb
parent 23133 5a6935d598c3
child 24219 e558fe311376
permissions -rw-r--r--
tuned Proof
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@22525
     9
imports 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@22525
    53
    DatatypeCodegen.prove_codetypes_arities (ClassPackage.intro_classes_tac [])
haftmann@22525
    54
      (map (fn (tyco, (is_dt, _)) => (tyco, is_dt)) specs)
haftmann@22525
    55
      [TypOf.class_typ_of] mk ((K o K) I)
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@22525
   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@22525
   115
      DatatypeCodegen.prove_codetypes_arities (ClassPackage.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@22525
   157
  val eval_term: theory -> term -> term
haftmann@22804
   158
  val print: (theory -> term -> term) -> string
haftmann@22804
   159
    -> Toplevel.transition -> Toplevel.transition
haftmann@22525
   160
end;
haftmann@22525
   161
haftmann@22525
   162
structure Eval : EVAL =
haftmann@22525
   163
struct
haftmann@22525
   164
haftmann@22525
   165
val eval_ref = ref (NONE : term option);
haftmann@22525
   166
haftmann@22525
   167
fun eval_term thy t =
haftmann@22525
   168
  CodegenPackage.eval_term
haftmann@22525
   169
    thy (("Eval.eval_ref", eval_ref), TermOf.mk_term_of t);
haftmann@22525
   170
haftmann@22804
   171
fun print eval s = Toplevel.keep (fn state =>
haftmann@22525
   172
  let
haftmann@22804
   173
    val ctxt = Toplevel.context_of state;
haftmann@22804
   174
    val thy = ProofContext.theory_of ctxt;
haftmann@22804
   175
    val t = eval thy (ProofContext.read_term ctxt s);
haftmann@22804
   176
    val T = Term.type_of t;
haftmann@22804
   177
  in
haftmann@22804
   178
    writeln (Pretty.string_of
haftmann@22804
   179
      (Pretty.block [Pretty.quote (ProofContext.pretty_term ctxt t), Pretty.fbrk,
haftmann@22804
   180
        Pretty.str "::", Pretty.brk 1, Pretty.quote (ProofContext.pretty_typ ctxt T)]))
haftmann@22804
   181
  end);
haftmann@22525
   182
haftmann@22525
   183
end;
haftmann@22525
   184
*}
haftmann@22525
   185
haftmann@22804
   186
ML {*
haftmann@22804
   187
val valueP =
haftmann@22804
   188
  OuterSyntax.improper_command "value" "read, evaluate and print term" OuterKeyword.diag
haftmann@22804
   189
    ((OuterParse.opt_keyword "overloaded" -- OuterParse.term)
haftmann@22804
   190
      >> (fn (b, t) => (Toplevel.no_timing o Eval.print
haftmann@22804
   191
            (if b then Eval.eval_term else Codegen.eval_term) t)));
haftmann@22804
   192
haftmann@22804
   193
val _ = OuterSyntax.add_parsers [valueP];
haftmann@22804
   194
*}
haftmann@22804
   195
wenzelm@22665
   196
end